blob: 7f76c13d5a13ae726aa48cf138c41555b0dc36be [file] [log] [blame]
drhed7c8552001-04-11 14:29:21 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhed7c8552001-04-11 14:29:21 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhed7c8552001-04-11 14:29:21 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drhed7c8552001-04-11 14:29:21 +000010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:26 +000012** This is the implementation of the page cache subsystem or "pager".
drhed7c8552001-04-11 14:29:21 +000013**
drhb19a2bc2001-09-16 00:13:26 +000014** The pager is used to access a database disk file. It implements
15** atomic commit and rollback through the use of a journal file that
16** is separate from the database file. The pager also implements file
17** locking to prevent two processes from writing the same database
18** file simultaneously, or one process from reading the database while
19** another is writing.
drhed7c8552001-04-11 14:29:21 +000020**
danielk1977f653d782008-03-20 11:04:21 +000021** @(#) $Id: pager.c,v 1.420 2008/03/20 11:04:21 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000026#include <string.h>
drhed7c8552001-04-11 14:29:21 +000027
28/*
drhdb48ee02003-01-16 13:42:43 +000029** Macros for troubleshooting. Normally turned off
30*/
danielk1977466be562004-06-10 02:16:01 +000031#if 0
drhd3627af2006-12-18 18:34:51 +000032#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000033#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
34#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
35#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
36#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
37#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000038#else
drh4f0c5872007-03-26 22:05:01 +000039#define PAGERTRACE1(X)
40#define PAGERTRACE2(X,Y)
41#define PAGERTRACE3(X,Y,Z)
42#define PAGERTRACE4(X,Y,Z,W)
43#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000044#endif
45
danielk1977599fcba2004-11-08 07:13:13 +000046/*
drh4f0c5872007-03-26 22:05:01 +000047** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000048** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000049**
drh85b623f2007-12-13 21:54:09 +000050** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000051** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000052** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000053*/
drhc001c582006-03-06 18:23:16 +000054#define PAGERID(p) ((int)(p->fd))
55#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000056
57/*
drhed7c8552001-04-11 14:29:21 +000058** The page cache as a whole is always in one of the following
59** states:
60**
drha6abd042004-06-09 17:37:22 +000061** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000062** writing the database file. There is no
63** data held in memory. This is the initial
64** state.
65**
drha6abd042004-06-09 17:37:22 +000066** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000067** Writing is not permitted. There can be
68** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000069** file at the same time.
drhed7c8552001-04-11 14:29:21 +000070**
drh726de592004-06-10 23:35:50 +000071** PAGER_RESERVED This process has reserved the database for writing
72** but has not yet made any changes. Only one process
73** at a time can reserve the database. The original
74** database file has not been modified so other
75** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000076** database file.
77**
78** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000079** Access is exclusive. No other processes or
80** threads can be reading or writing while one
81** process is writing.
82**
danielk1977aa5ccdf2004-06-14 05:10:42 +000083** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
84** after all dirty pages have been written to the
85** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000086** disk. All that remains to do is to remove or
87** truncate the journal file and the transaction
88** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000089**
drha6abd042004-06-09 17:37:22 +000090** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000091** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000092** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000093** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000094** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000095** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000096** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000097** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000098** PAGER_RESERVED means that there is an open rollback journal.
99** The transition to PAGER_EXCLUSIVE occurs before any changes
100** are made to the database file, though writes to the rollback
101** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
102** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
103** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000104*/
drha6abd042004-06-09 17:37:22 +0000105#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000106#define PAGER_SHARED 1 /* same as SHARED_LOCK */
107#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
108#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
109#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000110
drh684917c2004-10-05 02:41:42 +0000111/*
112** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
113** then failed attempts to get a reserved lock will invoke the busy callback.
114** This is off by default. To see why, consider the following scenario:
115**
116** Suppose thread A already has a shared lock and wants a reserved lock.
117** Thread B already has a reserved lock and wants an exclusive lock. If
118** both threads are using their busy callbacks, it might be a long time
119** be for one of the threads give up and allows the other to proceed.
120** But if the thread trying to get the reserved lock gives up quickly
121** (if it never invokes its busy callback) then the contention will be
122** resolved quickly.
123*/
124#ifndef SQLITE_BUSY_RESERVED_LOCK
125# define SQLITE_BUSY_RESERVED_LOCK 0
126#endif
drhd9b02572001-04-15 00:37:09 +0000127
drhed7c8552001-04-11 14:29:21 +0000128/*
drh887dc4c2004-10-22 16:22:57 +0000129** This macro rounds values up so that if the value is an address it
130** is guaranteed to be an address that is aligned to an 8-byte boundary.
131*/
132#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
133
danielk19779f61c2f2007-08-27 17:27:49 +0000134typedef struct PgHdr PgHdr;
danielk197784f786f2007-08-28 08:00:17 +0000135
136/*
137** Each pager stores all currently unreferenced pages in a list sorted
138** in least-recently-used (LRU) order (i.e. the first item on the list has
139** not been referenced in a long time, the last item has been recently
140** used). An instance of this structure is included as part of each
141** pager structure for this purpose (variable Pager.lru).
142**
143** Additionally, if memory-management is enabled, all unreferenced pages
144** are stored in a global LRU list (global variable sqlite3LruPageList).
145**
146** In both cases, the PagerLruList.pFirstSynced variable points to
147** the first page in the corresponding list that does not require an
drh85b623f2007-12-13 21:54:09 +0000148** fsync() operation before its memory can be reclaimed. If no such
danielk197784f786f2007-08-28 08:00:17 +0000149** page exists, PagerLruList.pFirstSynced is set to NULL.
150*/
151typedef struct PagerLruList PagerLruList;
152struct PagerLruList {
153 PgHdr *pFirst; /* First page in LRU list */
154 PgHdr *pLast; /* Last page in LRU list (the most recently used) */
155 PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0 */
156};
157
158/*
159** The following structure contains the next and previous pointers used
160** to link a PgHdr structure into a PagerLruList linked list.
161*/
danielk19779f61c2f2007-08-27 17:27:49 +0000162typedef struct PagerLruLink PagerLruLink;
163struct PagerLruLink {
164 PgHdr *pNext;
165 PgHdr *pPrev;
166};
167
drh887dc4c2004-10-22 16:22:57 +0000168/*
drhed7c8552001-04-11 14:29:21 +0000169** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +0000170** This header is only visible to this pager module. The client
171** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +0000172**
danielk19773b8a05f2007-03-19 17:44:26 +0000173** Client code should call sqlite3PagerWrite() on a page prior to making
174** any modifications to that page. The first time sqlite3PagerWrite()
drhf6038712004-02-08 18:07:34 +0000175** is called, the original page contents are written into the rollback
176** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
177** the journal page has made it onto the disk surface, PgHdr.needSync
178** is cleared. The modified page cannot be written back into the original
179** database file until the journal pages has been synced to disk and the
180** PgHdr.needSync has been cleared.
181**
danielk19773b8a05f2007-03-19 17:44:26 +0000182** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
drhf6038712004-02-08 18:07:34 +0000183** is cleared again when the page content is written back to the original
184** database file.
drh732c8172007-06-16 11:17:45 +0000185**
186** Details of important structure elements:
187**
188** needSync
189**
190** If this is true, this means that it is not safe to write the page
191** content to the database because the original content needed
192** for rollback has not by synced to the main rollback journal.
193** The original content may have been written to the rollback journal
194** but it has not yet been synced. So we cannot write to the database
195** file because power failure might cause the page in the journal file
196** to never reach the disk. It is as if the write to the journal file
197** does not occur until the journal file is synced.
198**
199** This flag is false if the page content exactly matches what
200** currently exists in the database file. The needSync flag is also
201** false if the original content has been written to the main rollback
202** journal and synced. If the page represents a new page that has
203** been added onto the end of the database during the current
204** transaction, the needSync flag is true until the original database
205** size in the journal header has been synced to disk.
206**
207** inJournal
208**
209** This is true if the original page has been written into the main
210** rollback journal. This is always false for new pages added to
211** the end of the database file during the current transaction.
212** And this flag says nothing about whether or not the journal
213** has been synced to disk. For pages that are in the original
214** database file, the following expression should always be true:
215**
drhf5e7bb52008-02-18 14:47:33 +0000216** inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno)
drh732c8172007-06-16 11:17:45 +0000217**
drhf5e7bb52008-02-18 14:47:33 +0000218** The pPager->pInJournal object is only valid for the original
drh732c8172007-06-16 11:17:45 +0000219** pages of the database, not new pages that are added to the end
220** of the database, so obviously the above expression cannot be
221** valid for new pages. For new pages inJournal is always 0.
222**
223** dirty
224**
225** When true, this means that the content of the page has been
226** modified and needs to be written back to the database file.
227** If false, it means that either the content of the page is
228** unchanged or else the content is unimportant and we do not
229** care whether or not it is preserved.
230**
231** alwaysRollback
232**
233** This means that the sqlite3PagerDontRollback() API should be
234** ignored for this page. The DontRollback() API attempts to say
235** that the content of the page on disk is unimportant (it is an
236** unused page on the freelist) so that it is unnecessary to
237** rollback changes to this page because the content of the page
238** can change without changing the meaning of the database. This
239** flag overrides any DontRollback() attempt. This flag is set
240** when a page that originally contained valid data is added to
241** the freelist. Later in the same transaction, this page might
242** be pulled from the freelist and reused for something different
243** and at that point the DontRollback() API will be called because
244** pages taken from the freelist do not need to be protected by
245** the rollback journal. But this flag says that the page was
246** not originally part of the freelist so that it still needs to
247** be rolled back in spite of any subsequent DontRollback() calls.
248**
249** needRead
250**
251** This flag means (when true) that the content of the page has
252** not yet been loaded from disk. The in-memory content is just
253** garbage. (Actually, we zero the content, but you should not
254** make any assumptions about the content nevertheless.) If the
255** content is needed in the future, it should be read from the
256** original database file.
drhed7c8552001-04-11 14:29:21 +0000257*/
258struct PgHdr {
259 Pager *pPager; /* The pager to which this page belongs */
260 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000261 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
danielk19779f61c2f2007-08-27 17:27:49 +0000262 PagerLruLink free; /* Next and previous free pages */
drhac69b052004-05-12 13:30:07 +0000263 PgHdr *pNextAll; /* A list of all pages */
drh193a6b42002-07-07 16:52:46 +0000264 u8 inJournal; /* TRUE if has been written to journal */
drh193a6b42002-07-07 16:52:46 +0000265 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000266 u8 needSync; /* Sync journal before writing this page */
drh80e35f42007-03-30 14:06:34 +0000267 u8 alwaysRollback; /* Disable DontRollback() for this page */
drh538f5702007-04-13 02:14:30 +0000268 u8 needRead; /* Read content if PagerWrite() is called */
drhac69b052004-05-12 13:30:07 +0000269 short int nRef; /* Number of users of this page */
drhdc3e10b2006-06-15 14:31:06 +0000270 PgHdr *pDirty, *pPrevDirty; /* Dirty pages */
danielk19779f61c2f2007-08-27 17:27:49 +0000271#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
272 PagerLruLink gfree; /* Global list of nRef==0 pages */
273#endif
danielk19773c407372005-02-15 02:54:14 +0000274#ifdef SQLITE_CHECK_PAGES
275 u32 pageHash;
276#endif
drhbf4bca52007-09-06 22:19:14 +0000277 void *pData; /* Page data */
278 /* Pager.nExtra bytes of local data appended to this header */
drhed7c8552001-04-11 14:29:21 +0000279};
280
drhac69b052004-05-12 13:30:07 +0000281/*
282** For an in-memory only database, some extra information is recorded about
283** each page so that changes can be rolled back. (Journal files are not
284** used for in-memory databases.) The following information is added to
285** the end of every EXTRA block for in-memory databases.
286**
287** This information could have been added directly to the PgHdr structure.
288** But then it would take up an extra 8 bytes of storage on every PgHdr
289** even for disk-based databases. Splitting it out saves 8 bytes. This
290** is only a savings of 0.8% but those percentages add up.
291*/
292typedef struct PgHistory PgHistory;
293struct PgHistory {
294 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
295 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000296 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
297 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000298};
drh9eb9e262004-02-11 02:18:05 +0000299
300/*
301** A macro used for invoking the codec if there is one
302*/
303#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000304# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
305# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000306#else
drhc001c582006-03-06 18:23:16 +0000307# define CODEC1(P,D,N,X) /* NO-OP */
308# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000309#endif
310
drhed7c8552001-04-11 14:29:21 +0000311/*
drh69688d52001-04-14 16:38:23 +0000312** Convert a pointer to a PgHdr into a pointer to its data
313** and back again.
drhed7c8552001-04-11 14:29:21 +0000314*/
drhbf4bca52007-09-06 22:19:14 +0000315#define PGHDR_TO_DATA(P) ((P)->pData)
316#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
drhac69b052004-05-12 13:30:07 +0000317#define PGHDR_TO_HIST(P,PGR) \
drhbf4bca52007-09-06 22:19:14 +0000318 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000319
320/*
drhed7c8552001-04-11 14:29:21 +0000321** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000322**
drh4f0ee682007-03-30 20:43:40 +0000323** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000324** or SQLITE_FULL. Once one of the first three errors occurs, it persists
325** and is returned as the result of every major pager API call. The
326** SQLITE_FULL return code is slightly different. It persists only until the
327** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000328** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000329** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000330*/
331struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000332 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000333 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000334 u8 journalStarted; /* True if header of journal is synced */
335 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000336 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000337 u8 stmtOpen; /* True if the statement subjournal is open */
338 u8 stmtInUse; /* True we are in a statement subtransaction */
339 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000340 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000341 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000342 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000343 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000344 u8 tempFile; /* zFilename is a temporary file */
345 u8 readOnly; /* True for a read-only database */
346 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000347 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000348 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000349 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000350 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000351 u8 doNotSync; /* Boolean. While true, do not spill the cache */
352 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
353 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000354 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000355 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000356 int dbSize; /* Number of pages in the file */
357 int origDbSize; /* dbSize before the current change */
358 int stmtSize; /* Size of database (in pages) at stmt_begin() */
359 int nRec; /* Number of pages written to the journal */
360 u32 cksumInit; /* Quasi-random value added to every checksum */
361 int stmtNRec; /* Number of records in stmt subjournal */
362 int nExtra; /* Add this many bytes to each in-memory page */
363 int pageSize; /* Number of bytes in a page */
364 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000365 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
366 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000367 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000368 Bitvec *pInJournal; /* One bit for each page in the database file */
369 Bitvec *pInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000370 char *zFilename; /* Name of the database file */
371 char *zJournal; /* Name of the journal file */
372 char *zDirectory; /* Directory hold database and journal files */
drh334b2992007-09-06 23:28:23 +0000373 char *zStmtJrnl; /* Name of the statement journal file */
danielk197762079062007-08-15 17:08:46 +0000374 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
375 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000376 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
danielk19779f61c2f2007-08-27 17:27:49 +0000377 PagerLruList lru; /* LRU list of free pages */
drhd9b02572001-04-15 00:37:09 +0000378 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000379 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000380 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000381 i64 journalOff; /* Current byte offset in the journal file */
382 i64 journalHdr; /* Byte offset to previous journal header */
383 i64 stmtHdrOff; /* First journal header written this statement */
384 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000385 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000386 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000387#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000388 int nHit, nMiss; /* Cache hits and missing */
389 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000390#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000391 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
392 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000393#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000394 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000395 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000396#endif
drh8ca0c722006-05-07 17:49:38 +0000397 int nHash; /* Size of the pager hash table */
398 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000399#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +0000400 Pager *pNext; /* Doubly linked list of pagers on which */
401 Pager *pPrev; /* sqlite3_release_memory() will work */
402 int iInUseMM; /* Non-zero if unavailable to MM */
403 int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
danielk197713f72992005-12-18 08:51:22 +0000404#endif
danielk19778186df82007-03-06 13:45:59 +0000405 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000406 char dbFileVers[16]; /* Changes whenever database file changes */
drhd9b02572001-04-15 00:37:09 +0000407};
408
409/*
drh538f5702007-04-13 02:14:30 +0000410** The following global variables hold counters used for
411** testing purposes only. These variables do not exist in
412** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000413*/
414#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000415int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
416int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
417int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
418int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
419# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000420#else
drh538f5702007-04-13 02:14:30 +0000421# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000422#endif
423
drh86f8c192007-08-22 00:39:19 +0000424/*
425** The following variable points to the head of a double-linked list
426** of all pagers that are eligible for page stealing by the
427** sqlite3_release_memory() interface. Access to this list is
428** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
429*/
430#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
431static Pager *sqlite3PagerList = 0;
danielk19779f61c2f2007-08-27 17:27:49 +0000432static PagerLruList sqlite3LruPageList = {0, 0, 0};
drh86f8c192007-08-22 00:39:19 +0000433#endif
drh538f5702007-04-13 02:14:30 +0000434
435
drhfcd35c72005-05-21 02:48:08 +0000436/*
drh5e00f6c2001-09-13 13:46:56 +0000437** Journal files begin with the following magic string. The data
438** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000439**
drhae2b40c2004-06-09 19:03:54 +0000440** Since version 2.8.0, the journal format contains additional sanity
441** checking information. If the power fails while the journal is begin
442** written, semi-random garbage data might appear in the journal
443** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000444** to roll the journal back, the database could be corrupted. The additional
445** sanity checking data is an attempt to discover the garbage in the
446** journal and ignore it.
447**
drhae2b40c2004-06-09 19:03:54 +0000448** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000449** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000450** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000451** This cksum is initialized to a 32-bit random value that appears in the
452** journal file right after the header. The random initializer is important,
453** because garbage data that appears at the end of a journal is likely
454** data that was once in other files that have now been deleted. If the
455** garbage data came from an obsolete journal file, the checksums might
456** be correct. But by initializing the checksum to random value which
457** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000458*/
drhae2b40c2004-06-09 19:03:54 +0000459static const unsigned char aJournalMagic[] = {
460 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000461};
462
463/*
drh726de592004-06-10 23:35:50 +0000464** The size of the header and of each page in the journal is determined
465** by the following macros.
drh968af522003-02-11 14:55:40 +0000466*/
drhae2b40c2004-06-09 19:03:54 +0000467#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000468
danielk197776572402004-06-25 02:38:54 +0000469/*
470** The journal header size for this pager. In the future, this could be
471** set to some value read from the disk controller. The important
472** characteristic is that it is the same size as a disk sector.
473*/
474#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
475
drhb7f91642004-10-31 02:22:47 +0000476/*
477** The macro MEMDB is true if we are dealing with an in-memory database.
478** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
479** the value of MEMDB will be a constant and the compiler will optimize
480** out code that would never execute.
481*/
482#ifdef SQLITE_OMIT_MEMORYDB
483# define MEMDB 0
484#else
485# define MEMDB pPager->memDb
486#endif
487
488/*
danielk197776572402004-06-25 02:38:54 +0000489** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
490** reserved for working around a windows/posix incompatibility). It is
491** used in the journal to signify that the remainder of the journal file
492** is devoted to storing a master journal name - there are no more pages to
493** roll back. See comments for function writeMasterJournal() for details.
494*/
danielk1977599fcba2004-11-08 07:13:13 +0000495/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
496#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000497
drh968af522003-02-11 14:55:40 +0000498/*
danielk197726836652005-01-17 01:33:13 +0000499** The maximum legal page number is (2^31 - 1).
500*/
501#define PAGER_MAX_PGNO 2147483647
502
503/*
drh86f8c192007-08-22 00:39:19 +0000504** The pagerEnter() and pagerLeave() routines acquire and release
505** a mutex on each pager. The mutex is recursive.
506**
507** This is a special-purpose mutex. It only provides mutual exclusion
508** between the Btree and the Memory Management sqlite3_release_memory()
509** function. It does not prevent, for example, two Btrees from accessing
510** the same pager at the same time. Other general-purpose mutexes in
511** the btree layer handle that chore.
512*/
513#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
514 static void pagerEnter(Pager *p){
515 p->iInUseDB++;
516 if( p->iInUseMM && p->iInUseDB==1 ){
517 sqlite3_mutex *mutex;
518 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
519 p->iInUseDB = 0;
520 sqlite3_mutex_enter(mutex);
521 p->iInUseDB = 1;
522 sqlite3_mutex_leave(mutex);
523 }
524 assert( p->iInUseMM==0 );
525 }
526 static void pagerLeave(Pager *p){
527 p->iInUseDB--;
528 assert( p->iInUseDB>=0 );
529 }
530#else
531# define pagerEnter(X)
532# define pagerLeave(X)
533#endif
534
535/*
danielk197784f786f2007-08-28 08:00:17 +0000536** Add page pPg to the end of the linked list managed by structure
537** pList (pPg becomes the last entry in the list - the most recently
538** used). Argument pLink should point to either pPg->free or pPg->gfree,
539** depending on whether pPg is being added to the pager-specific or
540** global LRU list.
541*/
danielk19779f61c2f2007-08-27 17:27:49 +0000542static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
543 pLink->pNext = 0;
544 pLink->pPrev = pList->pLast;
545
danielk197784f786f2007-08-28 08:00:17 +0000546#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
547 assert(pLink==&pPg->free || pLink==&pPg->gfree);
548 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
549#endif
550
danielk19779f61c2f2007-08-27 17:27:49 +0000551 if( pList->pLast ){
552 int iOff = (char *)pLink - (char *)pPg;
553 PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
554 pLastLink->pNext = pPg;
555 }else{
556 assert(!pList->pFirst);
557 pList->pFirst = pPg;
558 }
559
560 pList->pLast = pPg;
561 if( !pList->pFirstSynced && pPg->needSync==0 ){
562 pList->pFirstSynced = pPg;
563 }
564}
565
danielk197784f786f2007-08-28 08:00:17 +0000566/*
567** Remove pPg from the list managed by the structure pointed to by pList.
568**
569** Argument pLink should point to either pPg->free or pPg->gfree, depending
570** on whether pPg is being added to the pager-specific or global LRU list.
571*/
danielk19779f61c2f2007-08-27 17:27:49 +0000572static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
573 int iOff = (char *)pLink - (char *)pPg;
574
danielk197784f786f2007-08-28 08:00:17 +0000575#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
576 assert(pLink==&pPg->free || pLink==&pPg->gfree);
577 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
578#endif
579
danielk19779f61c2f2007-08-27 17:27:49 +0000580 if( pPg==pList->pFirst ){
581 pList->pFirst = pLink->pNext;
582 }
583 if( pPg==pList->pLast ){
584 pList->pLast = pLink->pPrev;
585 }
586 if( pLink->pPrev ){
587 PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
588 pPrevLink->pNext = pLink->pNext;
589 }
590 if( pLink->pNext ){
591 PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
592 pNextLink->pPrev = pLink->pPrev;
593 }
594 if( pPg==pList->pFirstSynced ){
595 PgHdr *p = pLink->pNext;
596 while( p && p->needSync ){
597 PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
598 p = pL->pNext;
599 }
600 pList->pFirstSynced = p;
601 }
602
603 pLink->pNext = pLink->pPrev = 0;
604}
605
606/*
danielk197784f786f2007-08-28 08:00:17 +0000607** Add page pPg to the list of free pages for the pager. If
608** memory-management is enabled, also add the page to the global
609** list of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000610*/
611static void lruListAdd(PgHdr *pPg){
612 listAdd(&pPg->pPager->lru, &pPg->free, pPg);
613#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
614 if( !pPg->pPager->memDb ){
615 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
616 listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
617 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
618 }
619#endif
620}
621
622/*
danielk197784f786f2007-08-28 08:00:17 +0000623** Remove page pPg from the list of free pages for the associated pager.
624** If memory-management is enabled, also remove pPg from the global list
625** of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000626*/
627static void lruListRemove(PgHdr *pPg){
628 listRemove(&pPg->pPager->lru, &pPg->free, pPg);
629#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
630 if( !pPg->pPager->memDb ){
631 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
632 listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
633 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
634 }
635#endif
636}
637
638/*
danielk197784f786f2007-08-28 08:00:17 +0000639** This function is called just after the needSync flag has been cleared
640** from all pages managed by pPager (usually because the journal file
641** has just been synced). It updates the pPager->lru.pFirstSynced variable
642** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
643** variable also.
danielk19779f61c2f2007-08-27 17:27:49 +0000644*/
645static void lruListSetFirstSynced(Pager *pPager){
646 pPager->lru.pFirstSynced = pPager->lru.pFirst;
647#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
648 if( !pPager->memDb ){
649 PgHdr *p;
650 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
651 for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
652 assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
653 sqlite3LruPageList.pFirstSynced = p;
654 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
655 }
656#endif
657}
658
danielk1977f35843b2007-04-07 15:03:17 +0000659/*
660** Return true if page *pPg has already been written to the statement
661** journal (or statement snapshot has been created, if *pPg is part
662** of an in-memory database).
663*/
664static int pageInStatement(PgHdr *pPg){
665 Pager *pPager = pPg->pPager;
666 if( MEMDB ){
667 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
668 }else{
drhf5e7bb52008-02-18 14:47:33 +0000669 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000670 }
671}
drh8ca0c722006-05-07 17:49:38 +0000672
673/*
674** Change the size of the pager hash table to N. N must be a power
675** of two.
676*/
677static void pager_resize_hash_table(Pager *pPager, int N){
678 PgHdr **aHash, *pPg;
679 assert( N>0 && (N&(N-1))==0 );
drheee4c8c2008-02-18 22:24:57 +0000680#ifdef SQLITE_MALLOC_SOFT_LIMIT
681 if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){
682 N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]);
683 }
684 if( N==pPager->nHash ) return;
685#endif
drhed138fb2007-08-22 22:04:37 +0000686 pagerLeave(pPager);
drh643167f2008-01-22 21:30:53 +0000687 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pPager->aHash!=0);
drh17435752007-08-16 04:30:38 +0000688 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drh643167f2008-01-22 21:30:53 +0000689 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0);
drhed138fb2007-08-22 22:04:37 +0000690 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000691 if( aHash==0 ){
692 /* Failure to rehash is not an error. It is only a performance hit. */
693 return;
694 }
drh17435752007-08-16 04:30:38 +0000695 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000696 pPager->nHash = N;
697 pPager->aHash = aHash;
698 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000699 int h;
700 if( pPg->pgno==0 ){
701 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
702 continue;
703 }
704 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000705 pPg->pNextHash = aHash[h];
706 if( aHash[h] ){
707 aHash[h]->pPrevHash = pPg;
708 }
709 aHash[h] = pPg;
710 pPg->pPrevHash = 0;
711 }
712}
713
drhdd793422001-06-28 01:54:48 +0000714/*
drh34e79ce2004-02-08 06:05:46 +0000715** Read a 32-bit integer from the given file descriptor. Store the integer
716** that is read in *pRes. Return SQLITE_OK if everything worked, or an
717** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000718**
719** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000720*/
danielk197762079062007-08-15 17:08:46 +0000721static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000722 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000723 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000724 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000725 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000726 }
drh94f33312002-08-12 12:29:56 +0000727 return rc;
728}
729
730/*
drh97b57482006-01-10 20:32:32 +0000731** Write a 32-bit integer into a string buffer in big-endian byte order.
732*/
drha3152892007-05-05 11:48:52 +0000733#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000734
735/*
drh34e79ce2004-02-08 06:05:46 +0000736** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
737** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000738*/
danielk197762079062007-08-15 17:08:46 +0000739static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000740 char ac[4];
drh97b57482006-01-10 20:32:32 +0000741 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000742 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000743}
744
drh2554f8b2003-01-22 01:26:44 +0000745/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000746** If file pFd is open, call sqlite3OsUnlock() on it.
747*/
748static int osUnlock(sqlite3_file *pFd, int eLock){
749 if( !pFd->pMethods ){
750 return SQLITE_OK;
751 }
752 return sqlite3OsUnlock(pFd, eLock);
753}
754
755/*
danielk1977c7b60172007-08-22 11:22:03 +0000756** This function determines whether or not the atomic-write optimization
757** can be used with this pager. The optimization can be used if:
758**
759** (a) the value returned by OsDeviceCharacteristics() indicates that
760** a database page may be written atomically, and
761** (b) the value returned by OsSectorSize() is less than or equal
762** to the page size.
763**
764** If the optimization cannot be used, 0 is returned. If it can be used,
765** then the value returned is the size of the journal file when it
766** contains rollback data for exactly one page.
767*/
768#ifdef SQLITE_ENABLE_ATOMIC_WRITE
769static int jrnlBufferSize(Pager *pPager){
770 int dc; /* Device characteristics */
771 int nSector; /* Sector size */
772 int nPage; /* Page size */
773 sqlite3_file *fd = pPager->fd;
774
775 if( fd->pMethods ){
776 dc = sqlite3OsDeviceCharacteristics(fd);
777 nSector = sqlite3OsSectorSize(fd);
778 nPage = pPager->pageSize;
779 }
780
781 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
782 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
783
danielk1977f8940ae2007-08-23 11:07:10 +0000784 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000785 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
786 }
787 return 0;
788}
789#endif
790
791/*
danielk1977aef0bf62005-12-30 16:28:01 +0000792** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000793** code. The first argument is a pointer to the pager structure, the
794** second the error-code about to be returned by a pager API function.
795** The value returned is a copy of the second argument to this function.
796**
drh4f0ee682007-03-30 20:43:40 +0000797** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000798** the error becomes persistent. Until the persisten error is cleared,
799** subsequent API calls on this Pager will immediately return the same
800** error code.
801**
802** A persistent error indicates that the contents of the pager-cache
803** cannot be trusted. This state can be cleared by completely discarding
804** the contents of the pager-cache. If a transaction was active when
805** the persistent error occured, then the rollback journal may need
806** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000807*/
danielk1977ae72d982007-10-03 08:46:44 +0000808static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000809static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000810 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000811 assert(
812 pPager->errCode==SQLITE_FULL ||
813 pPager->errCode==SQLITE_OK ||
814 (pPager->errCode & 0xff)==SQLITE_IOERR
815 );
danielk1977979f38e2007-03-27 16:19:51 +0000816 if(
drh4ac285a2006-09-15 07:28:50 +0000817 rc2==SQLITE_FULL ||
818 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000819 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000820 ){
821 pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000822 if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
823 /* If the pager is already unlocked, call pager_unlock() now to
824 ** clear the error state and ensure that the pager-cache is
825 ** completely empty.
826 */
827 pager_unlock(pPager);
828 }
danielk1977aef0bf62005-12-30 16:28:01 +0000829 }
830 return rc;
831}
832
drh477731b2007-06-16 03:06:27 +0000833/*
834** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
835** on the cache using a hash function. This is used for testing
836** and debugging only.
837*/
danielk19773c407372005-02-15 02:54:14 +0000838#ifdef SQLITE_CHECK_PAGES
839/*
840** Return a 32-bit hash of the page data for pPage.
841*/
drh477731b2007-06-16 03:06:27 +0000842static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000843 u32 hash = 0;
844 int i;
drh477731b2007-06-16 03:06:27 +0000845 for(i=0; i<nByte; i++){
846 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000847 }
848 return hash;
849}
drh477731b2007-06-16 03:06:27 +0000850static u32 pager_pagehash(PgHdr *pPage){
851 return pager_datahash(pPage->pPager->pageSize,
852 (unsigned char *)PGHDR_TO_DATA(pPage));
853}
danielk19773c407372005-02-15 02:54:14 +0000854
855/*
856** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
857** is defined, and NDEBUG is not defined, an assert() statement checks
858** that the page is either dirty or still matches the calculated page-hash.
859*/
860#define CHECK_PAGE(x) checkPage(x)
861static void checkPage(PgHdr *pPg){
862 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000863 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000864 pPg->pageHash==pager_pagehash(pPg) );
865}
866
867#else
drh8ffa8172007-06-18 17:25:17 +0000868#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000869#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000870#define CHECK_PAGE(x)
871#endif
872
drhed7c8552001-04-11 14:29:21 +0000873/*
danielk197776572402004-06-25 02:38:54 +0000874** When this is called the journal file for pager pPager must be open.
875** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000876** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000877**
danielk197765839c62007-08-30 08:08:17 +0000878** zMaster must point to a buffer of at least nMaster bytes allocated by
879** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
880** enough space to write the master journal name). If the master journal
881** name in the journal is longer than nMaster bytes (including a
882** nul-terminator), then this is handled as if no master journal name
883** were present in the journal.
884**
885** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000886** SQLITE_OK returned.
887*/
danielk197765839c62007-08-30 08:08:17 +0000888static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000889 int rc;
890 u32 len;
drheb206252004-10-01 02:00:31 +0000891 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000892 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000893 int i;
danielk197776572402004-06-25 02:38:54 +0000894 unsigned char aMagic[8]; /* A buffer to hold the magic header */
895
danielk197765839c62007-08-30 08:08:17 +0000896 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000897
drh054889e2005-11-30 03:20:31 +0000898 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000899 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000900
danielk197762079062007-08-15 17:08:46 +0000901 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000902 if( rc!=SQLITE_OK ) return rc;
903
danielk197765839c62007-08-30 08:08:17 +0000904 if( len>=nMaster ){
905 return SQLITE_OK;
906 }
907
danielk197762079062007-08-15 17:08:46 +0000908 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000909 if( rc!=SQLITE_OK ) return rc;
910
danielk197762079062007-08-15 17:08:46 +0000911 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000912 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
913
danielk197765839c62007-08-30 08:08:17 +0000914 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000915 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000916 return rc;
917 }
danielk197765839c62007-08-30 08:08:17 +0000918 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000919
920 /* See if the checksum matches the master journal name */
921 for(i=0; i<len; i++){
danielk197765839c62007-08-30 08:08:17 +0000922 cksum -= zMaster[i];
923 }
danielk19778191bff2004-06-28 04:52:30 +0000924 if( cksum ){
925 /* If the checksum doesn't add up, then one or more of the disk sectors
926 ** containing the master journal filename is corrupted. This means
927 ** definitely roll back, so just return SQLITE_OK and report a (nul)
928 ** master-journal filename.
929 */
danielk197765839c62007-08-30 08:08:17 +0000930 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000931 }
danielk197776572402004-06-25 02:38:54 +0000932
933 return SQLITE_OK;
934}
935
936/*
937** Seek the journal file descriptor to the next sector boundary where a
938** journal header may be read or written. Pager.journalOff is updated with
939** the new seek offset.
940**
941** i.e for a sector size of 512:
942**
943** Input Offset Output Offset
944** ---------------------------------------
945** 0 0
946** 512 512
947** 100 512
948** 2000 2048
949**
950*/
danielk197762079062007-08-15 17:08:46 +0000951static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000952 i64 offset = 0;
953 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000954 if( c ){
955 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
956 }
957 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
958 assert( offset>=c );
959 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
960 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000961}
962
963/*
964** The journal file must be open when this routine is called. A journal
965** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
966** current location.
967**
968** The format for the journal header is as follows:
969** - 8 bytes: Magic identifying journal format.
970** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
971** - 4 bytes: Random number used for page hash.
972** - 4 bytes: Initial database page count.
973** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000974** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000975**
danielk197767c007b2008-03-20 04:45:49 +0000976** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000977*/
978static int writeJournalHdr(Pager *pPager){
danielk197767c007b2008-03-20 04:45:49 +0000979 char zHeader[sizeof(aJournalMagic)+20];
danielk19774099f6e2007-03-19 11:25:20 +0000980 int rc;
danielk197776572402004-06-25 02:38:54 +0000981
danielk19774099f6e2007-03-19 11:25:20 +0000982 if( pPager->stmtHdrOff==0 ){
983 pPager->stmtHdrOff = pPager->journalOff;
984 }
985
danielk197762079062007-08-15 17:08:46 +0000986 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000987 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000988
drh97b57482006-01-10 20:32:32 +0000989 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000990
991 /*
992 ** Write the nRec Field - the number of page records that follow this
993 ** journal header. Normally, zero is written to this value at this time.
994 ** After the records are added to the journal (and the journal synced,
995 ** if in full-sync mode), the zero is overwritten with the true number
996 ** of records (see syncJournal()).
997 **
998 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
999 ** reading the journal this value tells SQLite to assume that the
1000 ** rest of the journal file contains valid page records. This assumption
1001 ** is dangerous, as if a failure occured whilst writing to the journal
1002 ** file it may contain some garbage data. There are two scenarios
1003 ** where this risk can be ignored:
1004 **
1005 ** * When the pager is in no-sync mode. Corruption can follow a
1006 ** power failure in this case anyway.
1007 **
1008 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1009 ** that garbage data is never appended to the journal file.
1010 */
1011 assert(pPager->fd->pMethods||pPager->noSync);
1012 if( (pPager->noSync)
1013 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1014 ){
1015 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1016 }else{
1017 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1018 }
1019
drh97b57482006-01-10 20:32:32 +00001020 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +00001021 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +00001022 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1023 /* The initial database size */
1024 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1025 /* The assumed sector size for this process */
1026 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
danielk197767c007b2008-03-20 04:45:49 +00001027 if( pPager->journalHdr==0 ){
1028 /* The page size */
1029 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
1030 }
drhb0603412007-02-28 04:47:26 +00001031 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
danielk197762079062007-08-15 17:08:46 +00001032 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
1033 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197776572402004-06-25 02:38:54 +00001034
1035 /* The journal header has been written successfully. Seek the journal
1036 ** file descriptor to the end of the journal header sector.
1037 */
1038 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +00001039 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
danielk197762079062007-08-15 17:08:46 +00001040 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
danielk197776572402004-06-25 02:38:54 +00001041 }
1042 return rc;
1043}
1044
1045/*
1046** The journal file must be open when this is called. A journal header file
1047** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1048** file. See comments above function writeJournalHdr() for a description of
1049** the journal header format.
1050**
1051** If the header is read successfully, *nRec is set to the number of
1052** page records following this header and *dbSize is set to the size of the
1053** database before the transaction began, in pages. Also, pPager->cksumInit
1054** is set to the value read from the journal header. SQLITE_OK is returned
1055** in this case.
1056**
1057** If the journal header file appears to be corrupted, SQLITE_DONE is
1058** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1059** cannot be read from the journal file an error code is returned.
1060*/
1061static int readJournalHdr(
1062 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001063 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001064 u32 *pNRec,
1065 u32 *pDbSize
1066){
1067 int rc;
1068 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001069 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +00001070 int iPageSize;
danielk197776572402004-06-25 02:38:54 +00001071
danielk197762079062007-08-15 17:08:46 +00001072 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001073 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1074 return SQLITE_DONE;
1075 }
danielk197762079062007-08-15 17:08:46 +00001076 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001077
danielk197762079062007-08-15 17:08:46 +00001078 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001079 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001080 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001081
1082 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1083 return SQLITE_DONE;
1084 }
1085
danielk197762079062007-08-15 17:08:46 +00001086 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001087 if( rc ) return rc;
1088
danielk197762079062007-08-15 17:08:46 +00001089 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001090 if( rc ) return rc;
1091
danielk197762079062007-08-15 17:08:46 +00001092 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001093 if( rc ) return rc;
1094
danielk197767c007b2008-03-20 04:45:49 +00001095 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
1096 if( rc==SQLITE_OK
1097 && iPageSize>=512
1098 && iPageSize<=SQLITE_MAX_PAGE_SIZE
1099 && ((iPageSize-1)&iPageSize)==0
1100 ){
1101 u16 pagesize = iPageSize;
1102 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
1103 }
1104 if( rc ) return rc;
1105
danielk197776572402004-06-25 02:38:54 +00001106 /* Update the assumed sector-size to match the value used by
1107 ** the process that created this journal. If this journal was
1108 ** created by a process other than this one, then this routine
1109 ** is being called from within pager_playback(). The local value
1110 ** of Pager.sectorSize is restored at the end of that routine.
1111 */
danielk197762079062007-08-15 17:08:46 +00001112 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001113 if( rc ) return rc;
1114
1115 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001116 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001117}
1118
1119
1120/*
1121** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001122** pPager at the current location. The master journal name must be the last
1123** thing written to a journal file. If the pager is in full-sync mode, the
1124** journal file descriptor is advanced to the next sector boundary before
1125** anything is written. The format is:
1126**
1127** + 4 bytes: PAGER_MJ_PGNO.
1128** + N bytes: length of master journal name.
1129** + 4 bytes: N
1130** + 4 bytes: Master journal name checksum.
1131** + 8 bytes: aJournalMagic[].
1132**
1133** The master journal page checksum is the sum of the bytes in the master
1134** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001135**
1136** If zMaster is a NULL pointer (occurs for a single database transaction),
1137** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001138*/
1139static int writeMasterJournal(Pager *pPager, const char *zMaster){
1140 int rc;
1141 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001142 int i;
danielk197762079062007-08-15 17:08:46 +00001143 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +00001144 u32 cksum = 0;
1145 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001146
1147 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1148 pPager->setMaster = 1;
1149
1150 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001151 for(i=0; i<len; i++){
1152 cksum += zMaster[i];
1153 }
danielk197776572402004-06-25 02:38:54 +00001154
1155 /* If in full-sync mode, advance to the next disk sector before writing
1156 ** the master journal name. This is in case the previous page written to
1157 ** the journal has already been synced.
1158 */
1159 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001160 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001161 }
danielk197762079062007-08-15 17:08:46 +00001162 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001163 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001164
danielk197762079062007-08-15 17:08:46 +00001165 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001166 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001167 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001168
danielk197762079062007-08-15 17:08:46 +00001169 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001170 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001171 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001172
drh97b57482006-01-10 20:32:32 +00001173 put32bits(zBuf, len);
1174 put32bits(&zBuf[4], cksum);
1175 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001176 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +00001177 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +00001178 return rc;
1179}
1180
1181/*
drh03eb96a2002-11-10 23:32:56 +00001182** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001183** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001184**
1185** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001186** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001187** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001188** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001189*/
drh3aac2dd2004-04-26 14:10:20 +00001190static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001191 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001192 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1193 assert( MEMDB );
1194 if( !pHist->inStmt ){
1195 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1196 if( pPager->pStmt ){
1197 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1198 }
1199 pHist->pNextStmt = pPager->pStmt;
1200 pPager->pStmt = pPg;
1201 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001202 }
drh03eb96a2002-11-10 23:32:56 +00001203}
1204
1205/*
drhed7c8552001-04-11 14:29:21 +00001206** Find a page in the hash table given its page number. Return
1207** a pointer to the page or NULL if not found.
1208*/
drhd9b02572001-04-15 00:37:09 +00001209static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001210 PgHdr *p;
1211 if( pPager->aHash==0 ) return 0;
1212 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001213 while( p && p->pgno!=pgno ){
1214 p = p->pNextHash;
1215 }
1216 return p;
1217}
1218
1219/*
danielk1977e180dd92007-04-05 17:15:52 +00001220** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001221** sets the state of the pager back to what it was when it was first
1222** opened. Any outstanding pages are invalidated and subsequent attempts
1223** to access those pages will likely result in a coredump.
1224*/
drhd9b02572001-04-15 00:37:09 +00001225static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001226 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001227 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001228 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001229 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1230 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001231 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001232 lruListRemove(pPg);
drh0d180202008-02-14 23:26:56 +00001233 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00001234 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001235 }
danielk19779f61c2f2007-08-27 17:27:49 +00001236 assert(pPager->lru.pFirst==0);
1237 assert(pPager->lru.pFirstSynced==0);
1238 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001239 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001240 pPager->pAll = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001241 pPager->pDirty = 0;
drh8ca0c722006-05-07 17:49:38 +00001242 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001243 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001244 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001245 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001246 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001247}
1248
1249/*
danielk1977ae72d982007-10-03 08:46:44 +00001250** Unlock the database file.
1251**
1252** If the pager is currently in error state, discard the contents of
1253** the cache and reset the Pager structure internal state. If there is
1254** an open journal-file, then the next time a shared-lock is obtained
1255** on the pager file (by this or any other process), it will be
1256** treated as a hot-journal and rolled back.
1257*/
1258static void pager_unlock(Pager *pPager){
1259 if( !pPager->exclusiveMode ){
1260 if( !MEMDB ){
drh1aa5af12008-03-07 19:51:14 +00001261 int rc = osUnlock(pPager->fd, NO_LOCK);
drh308aa322008-03-07 20:14:38 +00001262 if( rc ) pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +00001263 pPager->dbSize = -1;
1264 IOTRACE(("UNLOCK %p\n", pPager))
1265
1266 /* If Pager.errCode is set, the contents of the pager cache cannot be
1267 ** trusted. Now that the pager file is unlocked, the contents of the
1268 ** cache can be discarded and the error code safely cleared.
1269 */
1270 if( pPager->errCode ){
drh1aa5af12008-03-07 19:51:14 +00001271 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00001272 pager_reset(pPager);
1273 if( pPager->stmtOpen ){
1274 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +00001275 sqlite3BitvecDestroy(pPager->pInStmt);
1276 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001277 }
1278 if( pPager->journalOpen ){
1279 sqlite3OsClose(pPager->jfd);
1280 pPager->journalOpen = 0;
drhf5e7bb52008-02-18 14:47:33 +00001281 sqlite3BitvecDestroy(pPager->pInJournal);
1282 pPager->pInJournal = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001283 }
1284 pPager->stmtOpen = 0;
1285 pPager->stmtInUse = 0;
1286 pPager->journalOff = 0;
1287 pPager->journalStarted = 0;
1288 pPager->stmtAutoopen = 0;
1289 pPager->origDbSize = 0;
1290 }
1291 }
1292
1293 if( !MEMDB || pPager->errCode==SQLITE_OK ){
1294 pPager->state = PAGER_UNLOCK;
1295 pPager->changeCountDone = 0;
1296 }
1297 }
1298}
1299
1300/*
1301** Execute a rollback if a transaction is active and unlock the
1302** database file. If the pager has already entered the error state,
1303** do not attempt the rollback.
1304*/
1305static void pagerUnlockAndRollback(Pager *p){
1306 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
1307 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
1308 sqlite3PagerRollback(p);
1309 }
1310 pager_unlock(p);
1311 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1312 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
1313}
1314
1315/*
drh80e35f42007-03-30 14:06:34 +00001316** This routine ends a transaction. A transaction is ended by either
1317** a COMMIT or a ROLLBACK.
1318**
drhed7c8552001-04-11 14:29:21 +00001319** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001320** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1321** the database lock and acquires a SHARED lock in its place if that is
1322** the appropriate thing to do. Release locks usually is appropriate,
1323** unless we are in exclusive access mode or unless this is a
1324** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1325**
1326** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001327**
1328** TODO: Consider keeping the journal file open for temporary databases.
1329** This might give a performance improvement on windows where opening
1330** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001331*/
drh80e35f42007-03-30 14:06:34 +00001332static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001333 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001334 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001335 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001336 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001337 if( pPager->state<PAGER_RESERVED ){
1338 return SQLITE_OK;
1339 }
danielk19773b8a05f2007-03-19 17:44:26 +00001340 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001341 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001342 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001343 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001344 }
drhda47d772002-12-02 04:25:19 +00001345 if( pPager->journalOpen ){
drh01fa4c32007-04-02 11:22:22 +00001346 if( pPager->exclusiveMode
1347 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
danielk197741483462007-03-24 16:45:04 +00001348 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001349 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001350 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001351 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001352 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001353 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001354 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001355 }
danielk197741483462007-03-24 16:45:04 +00001356 }
drhf5e7bb52008-02-18 14:47:33 +00001357 sqlite3BitvecDestroy(pPager->pInJournal);
1358 pPager->pInJournal = 0;
drhda47d772002-12-02 04:25:19 +00001359 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1360 pPg->inJournal = 0;
1361 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001362 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001363 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001364#ifdef SQLITE_CHECK_PAGES
1365 pPg->pageHash = pager_pagehash(pPg);
1366#endif
drhda47d772002-12-02 04:25:19 +00001367 }
drh605ad8f2006-05-03 23:34:05 +00001368 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001369 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001370 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001371 }else{
drhf5e7bb52008-02-18 14:47:33 +00001372 assert( pPager->pInJournal==0 );
drha6abd042004-06-09 17:37:22 +00001373 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001374 }
danielk1977979f38e2007-03-27 16:19:51 +00001375
danielk197741483462007-03-24 16:45:04 +00001376 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001377 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001378 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001379 }else if( pPager->state==PAGER_SYNCED ){
1380 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001381 }
danielk1977334cdb62007-03-26 08:05:12 +00001382 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001383 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001384 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001385 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001386 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001387
1388 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001389}
1390
drhed7c8552001-04-11 14:29:21 +00001391/*
drh968af522003-02-11 14:55:40 +00001392** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001393**
1394** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001395** random initial value and the page number. We experimented with
1396** a checksum of the entire data, but that was found to be too slow.
1397**
1398** Note that the page number is stored at the beginning of data and
1399** the checksum is stored at the end. This is important. If journal
1400** corruption occurs due to a power failure, the most likely scenario
1401** is that one end or the other of the record will be changed. It is
1402** much less likely that the two ends of the journal record will be
1403** correct and the middle be corrupt. Thus, this "checksum" scheme,
1404** though fast and simple, catches the mostly likely kind of corruption.
1405**
1406** FIX ME: Consider adding every 200th (or so) byte of the data to the
1407** checksum. That way if a single page spans 3 or more disk sectors and
1408** only the middle sector is corrupt, we will still have a reasonable
1409** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001410*/
drh74161702006-02-24 02:53:49 +00001411static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001412 u32 cksum = pPager->cksumInit;
1413 int i = pPager->pageSize-200;
1414 while( i>0 ){
1415 cksum += aData[i];
1416 i -= 200;
1417 }
drh968af522003-02-11 14:55:40 +00001418 return cksum;
1419}
1420
drh605ad8f2006-05-03 23:34:05 +00001421/* Forward declaration */
1422static void makeClean(PgHdr*);
1423
drh968af522003-02-11 14:55:40 +00001424/*
drhfa86c412002-02-02 15:01:15 +00001425** Read a single page from the journal file opened on file descriptor
1426** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001427**
drh726de592004-06-10 23:35:50 +00001428** If useCksum==0 it means this journal does not use checksums. Checksums
1429** are not used in statement journals because statement journals do not
1430** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001431*/
danielk197762079062007-08-15 17:08:46 +00001432static int pager_playback_one_page(
1433 Pager *pPager,
1434 sqlite3_file *jfd,
1435 i64 offset,
1436 int useCksum
1437){
drhfa86c412002-02-02 15:01:15 +00001438 int rc;
drhae2b40c2004-06-09 19:03:54 +00001439 PgHdr *pPg; /* An existing page in the cache */
1440 Pgno pgno; /* The page number of a page in journal */
1441 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001442 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001443
drh96362842005-03-20 22:47:56 +00001444 /* useCksum should be true for the main journal and false for
1445 ** statement journals. Verify that this is always the case
1446 */
drh9cbe6352005-11-29 03:13:21 +00001447 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001448 assert( aData );
drh96362842005-03-20 22:47:56 +00001449
danielk197762079062007-08-15 17:08:46 +00001450 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001451 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001452 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001453 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001454 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001455
drh968af522003-02-11 14:55:40 +00001456 /* Sanity checking on the page. This is more important that I originally
1457 ** thought. If a power failure occurs while the journal is being written,
1458 ** it could cause invalid data to be written into the journal. We need to
1459 ** detect this invalid data (with high probability) and ignore it.
1460 */
danielk197775edc162004-06-26 01:48:18 +00001461 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001462 return SQLITE_DONE;
1463 }
drhae2b40c2004-06-09 19:03:54 +00001464 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001465 return SQLITE_OK;
1466 }
drhae2b40c2004-06-09 19:03:54 +00001467 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001468 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001469 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001470 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001471 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001472 return SQLITE_DONE;
1473 }
1474 }
drhfa86c412002-02-02 15:01:15 +00001475
danielk1977aa5ccdf2004-06-14 05:10:42 +00001476 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001477
1478 /* If the pager is in RESERVED state, then there must be a copy of this
1479 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001480 ** not the database file. The page is left marked dirty in this case.
1481 **
danielk19772df71c72007-05-24 07:22:42 +00001482 ** An exception to the above rule: If the database is in no-sync mode
1483 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001484 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1485 ** during a Movepage() call, then the page may not be in the cache
1486 ** either. So the condition described in the above paragraph is not
1487 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001488 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001489 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1490 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001491 **
1492 ** Ticket #1171: The statement journal might contain page content that is
1493 ** different from the page content at the start of the transaction.
1494 ** This occurs when a page is changed prior to the start of a statement
1495 ** then changed again within the statement. When rolling back such a
1496 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001497 ** for certain that original page contents are synced into the main rollback
1498 ** journal. Otherwise, a power loss might leave modified data in the
1499 ** database file without an entry in the rollback journal that can
1500 ** restore the database to its original form. Two conditions must be
1501 ** met before writing to the database files. (1) the database must be
1502 ** locked. (2) we know that the original page content is fully synced
1503 ** in the main journal either because the page is not in cache or else
1504 ** the page is marked as needSync==0.
drhfa86c412002-02-02 15:01:15 +00001505 */
drhae2b40c2004-06-09 19:03:54 +00001506 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001507 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1508 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh96362842005-03-20 22:47:56 +00001509 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
danielk197762079062007-08-15 17:08:46 +00001510 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1511 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001512 if( pPg ){
1513 makeClean(pPg);
1514 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001515 }
drhfa86c412002-02-02 15:01:15 +00001516 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001517 /* No page should ever be explicitly rolled back that is in use, except
1518 ** for page 1 which is held in use in order to keep the lock on the
1519 ** database active. However such a page may be rolled back as a result
1520 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001521 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001522 */
drhb6f41482004-05-14 01:58:11 +00001523 void *pData;
danielk197728129562005-01-11 10:25:06 +00001524 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001525 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001526 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001527 if( pPager->xReiniter ){
1528 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001529 }
danielk19773c407372005-02-15 02:54:14 +00001530#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001531 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001532#endif
drh86a88112007-04-16 15:02:19 +00001533 /* If this was page 1, then restore the value of Pager.dbFileVers.
1534 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001535 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001536 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001537 }
drh86a88112007-04-16 15:02:19 +00001538
1539 /* Decode the page just read from disk */
1540 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001541 }
1542 return rc;
1543}
1544
1545/*
danielk197713adf8a2004-06-03 16:08:41 +00001546** Parameter zMaster is the name of a master journal file. A single journal
1547** file that referred to the master journal file has just been rolled back.
1548** This routine checks if it is possible to delete the master journal file,
1549** and does so if it is.
drh726de592004-06-10 23:35:50 +00001550**
danielk197765839c62007-08-30 08:08:17 +00001551** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1552** available for use within this function.
1553**
1554**
drh726de592004-06-10 23:35:50 +00001555** The master journal file contains the names of all child journals.
1556** To tell if a master journal can be deleted, check to each of the
1557** children. If all children are either missing or do not refer to
1558** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001559*/
danielk1977b4b47412007-08-17 15:53:36 +00001560static int pager_delmaster(Pager *pPager, const char *zMaster){
1561 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001562 int rc;
1563 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001564 sqlite3_file *pMaster;
1565 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001566 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001567 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001568
1569 /* Open the master journal file exclusively in case some other process
1570 ** is running this routine also. Not that it makes too much difference.
1571 */
danielk1977b4b47412007-08-17 15:53:36 +00001572 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001573 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001574 if( !pMaster ){
1575 rc = SQLITE_NOMEM;
1576 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001577 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1578 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001579 }
danielk197713adf8a2004-06-03 16:08:41 +00001580 if( rc!=SQLITE_OK ) goto delmaster_out;
1581 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001582
1583 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001584 if( rc!=SQLITE_OK ) goto delmaster_out;
1585
1586 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001587 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001588 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001589 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001590
1591 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001592 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001593 */
danielk197765839c62007-08-30 08:08:17 +00001594 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001595 if( !zMasterJournal ){
1596 rc = SQLITE_NOMEM;
1597 goto delmaster_out;
1598 }
danielk197765839c62007-08-30 08:08:17 +00001599 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001600 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001601 if( rc!=SQLITE_OK ) goto delmaster_out;
1602
danielk19775865e3d2004-06-14 06:03:57 +00001603 zJournal = zMasterJournal;
1604 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977b4b47412007-08-17 15:53:36 +00001605 if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
danielk197713adf8a2004-06-03 16:08:41 +00001606 /* One of the journals pointed to by the master journal exists.
1607 ** Open it and check if it points at the master journal. If
1608 ** so, return without deleting the master journal file.
1609 */
drh3b7b78b2004-11-24 01:16:43 +00001610 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001611 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1612 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001613 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001614 goto delmaster_out;
1615 }
danielk197713adf8a2004-06-03 16:08:41 +00001616
danielk197765839c62007-08-30 08:08:17 +00001617 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001618 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001619 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001620 goto delmaster_out;
1621 }
danielk197776572402004-06-25 02:38:54 +00001622
danielk197765839c62007-08-30 08:08:17 +00001623 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001624 if( c ){
danielk197776572402004-06-25 02:38:54 +00001625 /* We have a match. Do not delete the master journal file. */
1626 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001627 }
1628 }
danielk19775865e3d2004-06-14 06:03:57 +00001629 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001630 }
1631 }
1632
danielk1977fee2d252007-08-18 10:59:19 +00001633 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001634
1635delmaster_out:
1636 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001637 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001638 }
1639 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001640 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001641 }
danielk1977b4b47412007-08-17 15:53:36 +00001642 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001643 return rc;
1644}
1645
drha6abd042004-06-09 17:37:22 +00001646
danielk1977e180dd92007-04-05 17:15:52 +00001647static void pager_truncate_cache(Pager *pPager);
1648
drha6abd042004-06-09 17:37:22 +00001649/*
drhcb4c40b2004-08-18 19:09:43 +00001650** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001651** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001652**
1653** Might might be the case that the file on disk is smaller than nPage.
1654** This can happen, for example, if we are in the middle of a transaction
1655** which has extended the file size and the new pages are still all held
1656** in cache, then an INSERT or UPDATE does a statement rollback. Some
1657** operating system implementations can get confused if you try to
1658** truncate a file to some size that is larger than it currently is,
1659** so detect this case and do not do the truncation.
drhcb4c40b2004-08-18 19:09:43 +00001660*/
1661static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001662 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001663 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001664 i64 currentSize, newSize;
1665 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1666 newSize = pPager->pageSize*(i64)nPage;
1667 if( rc==SQLITE_OK && currentSize>newSize ){
1668 rc = sqlite3OsTruncate(pPager->fd, newSize);
1669 }
danielk1977e180dd92007-04-05 17:15:52 +00001670 }
1671 if( rc==SQLITE_OK ){
1672 pPager->dbSize = nPage;
1673 pager_truncate_cache(pPager);
1674 }
1675 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001676}
1677
1678/*
drhc80f0582007-05-01 16:59:48 +00001679** Set the sectorSize for the given pager.
1680**
1681** The sector size is the larger of the sector size reported
1682** by sqlite3OsSectorSize() and the pageSize.
1683*/
1684static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001685 assert(pPager->fd->pMethods||pPager->tempFile);
1686 if( !pPager->tempFile ){
1687 /* Sector size doesn't matter for temporary files. Also, the file
1688 ** may not have been opened yet, in whcih case the OsSectorSize()
1689 ** call will segfault.
1690 */
1691 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1692 }
drhc80f0582007-05-01 16:59:48 +00001693 if( pPager->sectorSize<pPager->pageSize ){
1694 pPager->sectorSize = pPager->pageSize;
1695 }
1696}
1697
1698/*
drhed7c8552001-04-11 14:29:21 +00001699** Playback the journal and thus restore the database file to
1700** the state it was in before we started making changes.
1701**
drh34e79ce2004-02-08 06:05:46 +00001702** The journal file format is as follows:
1703**
drhae2b40c2004-06-09 19:03:54 +00001704** (1) 8 byte prefix. A copy of aJournalMagic[].
1705** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001706** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001707** number of page records from the journal size.
1708** (3) 4 byte big-endian integer which is the initial value for the
1709** sanity checksum.
1710** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001711** database to during a rollback.
drhae2b40c2004-06-09 19:03:54 +00001712** (5) 4 byte integer which is the number of bytes in the master journal
1713** name. The value may be zero (indicate that there is no master
1714** journal.)
1715** (6) N bytes of the master journal name. The name will be nul-terminated
1716** and might be shorter than the value read from (5). If the first byte
1717** of the name is \000 then there is no master journal. The master
1718** journal name is stored in UTF-8.
1719** (7) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001720** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001721** + pPager->pageSize bytes of data.
1722** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001723**
drhae2b40c2004-06-09 19:03:54 +00001724** When we speak of the journal header, we mean the first 6 items above.
1725** Each entry in the journal is an instance of the 7th item.
drh34e79ce2004-02-08 06:05:46 +00001726**
1727** Call the value from the second bullet "nRec". nRec is the number of
1728** valid page entries in the journal. In most cases, you can compute the
1729** value of nRec from the size of the journal file. But if a power
1730** failure occurred while the journal was being written, it could be the
1731** case that the size of the journal file had already been increased but
1732** the extra entries had not yet made it safely to disk. In such a case,
1733** the value of nRec computed from the file size would be too large. For
1734** that reason, we always use the nRec value in the header.
1735**
1736** If the nRec value is 0xffffffff it means that nRec should be computed
1737** from the file size. This value is used when the user selects the
1738** no-sync option for the journal. A power failure could lead to corruption
1739** in this case. But for things like temporary table (which will be
1740** deleted when the power is restored) we don't care.
1741**
drhd9b02572001-04-15 00:37:09 +00001742** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001743** journal file then all pages up to the first corrupted page are rolled
1744** back (or no pages if the journal header is corrupted). The journal file
1745** is then deleted and SQLITE_OK returned, just as if no corruption had
1746** been encountered.
1747**
1748** If an I/O or malloc() error occurs, the journal-file is not deleted
1749** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001750*/
danielk1977e277be02007-03-23 18:12:06 +00001751static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001752 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001753 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001754 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001755 int i; /* Loop counter */
1756 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001757 int rc; /* Result code of a subroutine */
danielk197713adf8a2004-06-03 16:08:41 +00001758 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001759
drhc3a64ba2001-11-22 00:01:27 +00001760 /* Figure out how many records are in the journal. Abort early if
1761 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001762 */
drh8cfbf082001-09-19 13:22:39 +00001763 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001764 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001765 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001766 goto end_playback;
1767 }
drh240c5792004-02-08 00:40:52 +00001768
danielk197776572402004-06-25 02:38:54 +00001769 /* Read the master journal name from the journal, if it is present.
1770 ** If a master journal file name is specified, but the file is not
1771 ** present on disk, then the journal is not hot and does not need to be
1772 ** played back.
drh240c5792004-02-08 00:40:52 +00001773 */
danielk197765839c62007-08-30 08:08:17 +00001774 zMaster = pPager->pTmpSpace;
1775 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk197776572402004-06-25 02:38:54 +00001776 assert( rc!=SQLITE_DONE );
danielk1977b4b47412007-08-17 15:53:36 +00001777 if( rc!=SQLITE_OK
danielk197765839c62007-08-30 08:08:17 +00001778 || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))
danielk1977b4b47412007-08-17 15:53:36 +00001779 ){
danielk197776572402004-06-25 02:38:54 +00001780 zMaster = 0;
1781 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drhc3a64ba2001-11-22 00:01:27 +00001782 goto end_playback;
1783 }
danielk197776572402004-06-25 02:38:54 +00001784 pPager->journalOff = 0;
danielk197765839c62007-08-30 08:08:17 +00001785 zMaster = 0;
drhc3a64ba2001-11-22 00:01:27 +00001786
danielk197776572402004-06-25 02:38:54 +00001787 /* This loop terminates either when the readJournalHdr() call returns
1788 ** SQLITE_DONE or an IO error occurs. */
1789 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001790
danielk197776572402004-06-25 02:38:54 +00001791 /* Read the next journal header from the journal file. If there are
1792 ** not enough bytes left in the journal file for a complete header, or
1793 ** it is corrupted, then a process must of failed while writing it.
1794 ** This indicates nothing more needs to be rolled back.
1795 */
1796 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1797 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001798 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001799 rc = SQLITE_OK;
1800 }
danielk197776572402004-06-25 02:38:54 +00001801 goto end_playback;
1802 }
1803
1804 /* If nRec is 0xffffffff, then this journal was created by a process
1805 ** working in no-sync mode. This means that the rest of the journal
1806 ** file consists of pages, there are no more journal headers. Compute
1807 ** the value of nRec based on this assumption.
1808 */
1809 if( nRec==0xffffffff ){
1810 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1811 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1812 }
1813
danielk1977e277be02007-03-23 18:12:06 +00001814 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001815 ** process and if this is the final header in the journal, then it means
1816 ** that this part of the journal was being filled but has not yet been
1817 ** synced to disk. Compute the number of pages based on the remaining
1818 ** size of the file.
1819 **
1820 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001821 */
drh8940f4e2007-08-11 00:26:20 +00001822 if( nRec==0 && !isHot &&
1823 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001824 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1825 }
1826
danielk197776572402004-06-25 02:38:54 +00001827 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001828 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001829 */
danielk1977e180dd92007-04-05 17:15:52 +00001830 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001831 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001832 if( rc!=SQLITE_OK ){
1833 goto end_playback;
1834 }
danielk197776572402004-06-25 02:38:54 +00001835 }
1836
danielk197776572402004-06-25 02:38:54 +00001837 /* Copy original pages out of the journal and back into the database file.
1838 */
1839 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001840 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001841 if( rc!=SQLITE_OK ){
1842 if( rc==SQLITE_DONE ){
1843 rc = SQLITE_OK;
1844 pPager->journalOff = szJ;
1845 break;
1846 }else{
1847 goto end_playback;
1848 }
1849 }
drh968af522003-02-11 14:55:40 +00001850 }
drhed7c8552001-04-11 14:29:21 +00001851 }
drh580eeaf2006-02-24 03:09:37 +00001852 /*NOTREACHED*/
1853 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001854
1855end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001856 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001857 zMaster = pPager->pTmpSpace;
1858 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1859 }
1860 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001861 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001862 }
danielk197765839c62007-08-30 08:08:17 +00001863 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001864 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001865 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001866 */
danielk197765839c62007-08-30 08:08:17 +00001867 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001868 }
danielk197776572402004-06-25 02:38:54 +00001869
1870 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001871 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001872 ** value. Reset it to the correct value for this process.
1873 */
drhc80f0582007-05-01 16:59:48 +00001874 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001875 return rc;
drhed7c8552001-04-11 14:29:21 +00001876}
1877
1878/*
drhac69b052004-05-12 13:30:07 +00001879** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001880**
1881** This is similar to playing back the transaction journal but with
1882** a few extra twists.
1883**
drh663fc632002-02-02 18:49:19 +00001884** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001885** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001886** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001887**
drhac69b052004-05-12 13:30:07 +00001888** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001889** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001890** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001891*/
drh3aac2dd2004-04-26 14:10:20 +00001892static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001893 i64 szJ; /* Size of the full journal */
1894 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001895 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001896 int i; /* Loop counter */
1897 int rc;
1898
danielk197776572402004-06-25 02:38:54 +00001899 szJ = pPager->journalOff;
1900#ifndef NDEBUG
1901 {
drheb206252004-10-01 02:00:31 +00001902 i64 os_szJ;
drh054889e2005-11-30 03:20:31 +00001903 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
danielk197776572402004-06-25 02:38:54 +00001904 if( rc!=SQLITE_OK ) return rc;
1905 assert( szJ==os_szJ );
1906 }
1907#endif
1908
danielk19774099f6e2007-03-19 11:25:20 +00001909 /* Set hdrOff to be the offset just after the end of the last journal
1910 ** page written before the first journal-header for this statement
1911 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001912 ** header was written.
1913 */
1914 hdrOff = pPager->stmtHdrOff;
1915 assert( pPager->fullSync || !hdrOff );
1916 if( !hdrOff ){
1917 hdrOff = szJ;
1918 }
1919
drhfa86c412002-02-02 15:01:15 +00001920 /* Truncate the database back to its original size.
1921 */
danielk1977e180dd92007-04-05 17:15:52 +00001922 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001923 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001924
drhac69b052004-05-12 13:30:07 +00001925 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001926 */
drhac69b052004-05-12 13:30:07 +00001927 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001928 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001929
drhac69b052004-05-12 13:30:07 +00001930 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001931 ** database file. Note that the statement journal omits checksums from
1932 ** each record since power-failure recovery is not important to statement
1933 ** journals.
drhfa86c412002-02-02 15:01:15 +00001934 */
danielk197762079062007-08-15 17:08:46 +00001935 for(i=0; i<nRec; i++){
1936 i64 offset = i*(4+pPager->pageSize);
1937 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001938 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001939 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001940 }
1941
danielk197776572402004-06-25 02:38:54 +00001942 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1943 ** was the size of the journal file when this statement was started, so
1944 ** everything after that needs to be rolled back, either into the
1945 ** database, the memory cache, or both.
1946 **
1947 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1948 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001949 */
danielk197776572402004-06-25 02:38:54 +00001950 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001951 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001952 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001953 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001954 assert( rc!=SQLITE_DONE );
1955 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1956 }
1957
1958 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001959 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001960 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001961 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001962 if( rc!=SQLITE_OK ){
1963 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001964 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001965 }
danielk1977f0113002006-01-24 12:09:17 +00001966 if( nJRec==0 ){
1967 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001968 }
danielk1977f0113002006-01-24 12:09:17 +00001969 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001970 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001971 assert( rc!=SQLITE_DONE );
1972 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1973 }
drhfa86c412002-02-02 15:01:15 +00001974 }
danielk197776572402004-06-25 02:38:54 +00001975
1976 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001977
drh3aac2dd2004-04-26 14:10:20 +00001978end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001979 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001980 pPager->journalOff = szJ;
1981 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001982 }
1983 return rc;
1984}
1985
1986/*
drhf57b14a2001-09-14 18:54:08 +00001987** Change the maximum number of in-memory pages that are allowed.
1988*/
danielk19773b8a05f2007-03-19 17:44:26 +00001989void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00001990 if( mxPage>10 ){
1991 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00001992 }else{
1993 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00001994 }
1995}
1996
1997/*
drh973b6e32003-02-12 14:09:42 +00001998** Adjust the robustness of the database to damage due to OS crashes
1999** or power failures by changing the number of syncs()s when writing
2000** the rollback journal. There are three levels:
2001**
drh054889e2005-11-30 03:20:31 +00002002** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00002003** for temporary and transient files.
2004**
2005** NORMAL The journal is synced once before writes begin on the
2006** database. This is normally adequate protection, but
2007** it is theoretically possible, though very unlikely,
2008** that an inopertune power failure could leave the journal
2009** in a state which would cause damage to the database
2010** when it is rolled back.
2011**
2012** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00002013** database (with some additional information - the nRec field
2014** of the journal header - being written in between the two
2015** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00002016** single disk sector is atomic, then this mode provides
2017** assurance that the journal will not be corrupted to the
2018** point of causing damage to the database during rollback.
2019**
2020** Numeric values associated with these states are OFF==1, NORMAL=2,
2021** and FULL=3.
2022*/
danielk197793758c82005-01-21 08:13:14 +00002023#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00002024void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00002025 pPager->noSync = level==1 || pPager->tempFile;
2026 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00002027 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00002028 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00002029}
danielk197793758c82005-01-21 08:13:14 +00002030#endif
drh973b6e32003-02-12 14:09:42 +00002031
2032/*
drhaf6df112005-06-07 02:12:30 +00002033** The following global variable is incremented whenever the library
2034** attempts to open a temporary file. This information is used for
2035** testing and analysis only.
2036*/
drh0f7eb612006-08-08 13:51:43 +00002037#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002038int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00002039#endif
drhaf6df112005-06-07 02:12:30 +00002040
2041/*
drh3f56e6e2007-03-15 01:16:47 +00002042** Open a temporary file.
2043**
2044** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00002045** other error code if we fail. The OS will automatically delete the temporary
2046** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00002047*/
danielk1977b4b47412007-08-17 15:53:36 +00002048static int sqlite3PagerOpentemp(
drh33f4e022007-09-03 15:19:34 +00002049 sqlite3_vfs *pVfs, /* The virtual file system layer */
2050 sqlite3_file *pFile, /* Write the file descriptor here */
2051 char *zFilename, /* Name of the file. Might be NULL */
2052 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00002053){
drhfa86c412002-02-02 15:01:15 +00002054 int rc;
drh334b2992007-09-06 23:28:23 +00002055 assert( zFilename!=0 );
drh3f56e6e2007-03-15 01:16:47 +00002056
drh0f7eb612006-08-08 13:51:43 +00002057#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002058 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002059#endif
danielk1977b4b47412007-08-17 15:53:36 +00002060
drh33f4e022007-09-03 15:19:34 +00002061 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2062 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
2063 rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00002064 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00002065 return rc;
2066}
2067
2068/*
drhed7c8552001-04-11 14:29:21 +00002069** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002070** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002071** the first call to sqlite3PagerGet() and is only held open until the
2072** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002073**
drh6446c4d2001-12-15 14:22:18 +00002074** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002075** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002076** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002077**
2078** If zFilename is ":memory:" then all information is held in cache.
2079** It is never written to disk. This can be used to implement an
2080** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002081*/
danielk19773b8a05f2007-03-19 17:44:26 +00002082int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002083 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002084 Pager **ppPager, /* Return the Pager structure here */
2085 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002086 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002087 int flags, /* flags controlling this file */
2088 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002089){
danielk1977b4b47412007-08-17 15:53:36 +00002090 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002091 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002092 int rc = SQLITE_OK;
2093 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002094 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002095 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002096 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002097 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2098 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002099 int journalFileSize = sqlite3JournalSize(pVfs);
drh1cc8c442007-08-24 16:08:29 +00002100 int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
2101 char *zPathname;
2102 int nPathname;
drh99b90c32008-03-17 13:50:58 +00002103 char *zStmtJrnl;
2104 int nStmtJrnl;
danielk1977b4b47412007-08-17 15:53:36 +00002105
drh86f8c192007-08-22 00:39:19 +00002106 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002107 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002108
drh1cc8c442007-08-24 16:08:29 +00002109 /* Compute the full pathname */
danielk1977adfb9b02007-09-17 07:02:56 +00002110 nPathname = pVfs->mxPathname+1;
drh99b90c32008-03-17 13:50:58 +00002111 zPathname = sqlite3_malloc(nPathname*2);
drh1cc8c442007-08-24 16:08:29 +00002112 if( zPathname==0 ){
2113 return SQLITE_NOMEM;
2114 }
2115 if( zFilename && zFilename[0] ){
2116#ifndef SQLITE_OMIT_MEMORYDB
2117 if( strcmp(zFilename,":memory:")==0 ){
2118 memDb = 1;
2119 zPathname[0] = 0;
2120 }else
2121#endif
2122 {
danielk1977adfb9b02007-09-17 07:02:56 +00002123 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002124 }
2125 }else{
danielk1977adfb9b02007-09-17 07:02:56 +00002126 rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
drhae28c012007-08-24 16:29:23 +00002127 }
2128 if( rc!=SQLITE_OK ){
2129 sqlite3_free(zPathname);
2130 return rc;
drh1cc8c442007-08-24 16:08:29 +00002131 }
2132 nPathname = strlen(zPathname);
2133
drh99b90c32008-03-17 13:50:58 +00002134 /* Put the statement journal in temporary disk space since this is
2135 ** sometimes RAM disk or other optimized storage. Unlikely the main
2136 ** main journal file, the statement journal does not need to be
2137 ** colocated with the database nor does it need to be persistent.
2138 */
2139 zStmtJrnl = &zPathname[nPathname+1];
2140 rc = sqlite3OsGetTempname(pVfs, pVfs->mxPathname+1, zStmtJrnl);
2141 if( rc!=SQLITE_OK ){
2142 sqlite3_free(zPathname);
2143 return rc;
2144 }
2145 nStmtJrnl = strlen(zStmtJrnl);
2146
danielk1977b4b47412007-08-17 15:53:36 +00002147 /* Allocate memory for the pager structure */
2148 pPager = sqlite3MallocZero(
2149 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00002150 journalFileSize + /* The journal file structure */
drh17c0ba22008-02-26 18:40:11 +00002151 pVfs->szOsFile * 3 + /* The main db and two journal files */
drh99b90c32008-03-17 13:50:58 +00002152 3*nPathname + 40 + /* zFilename, zDirectory, zJournal */
2153 nStmtJrnl /* zStmtJrnl */
danielk1977b4b47412007-08-17 15:53:36 +00002154 );
2155 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002156 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002157 return SQLITE_NOMEM;
2158 }
2159 pPtr = (u8 *)&pPager[1];
drh33f4e022007-09-03 15:19:34 +00002160 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002161 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002162 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2163 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2164 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002165 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2166 pPager->zJournal = &pPager->zDirectory[nPathname+1];
drh334b2992007-09-06 23:28:23 +00002167 pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
danielk1977b4b47412007-08-17 15:53:36 +00002168 pPager->pVfs = pVfs;
drh1cc8c442007-08-24 16:08:29 +00002169 memcpy(pPager->zFilename, zPathname, nPathname+1);
drh99b90c32008-03-17 13:50:58 +00002170 memcpy(pPager->zStmtJrnl, zStmtJrnl, nStmtJrnl+1);
drh1cc8c442007-08-24 16:08:29 +00002171 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002172
drh153c62c2007-08-24 03:51:33 +00002173 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002174 */
drhae28c012007-08-24 16:29:23 +00002175 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002176 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2177 rc = SQLITE_CANTOPEN;
2178 }else{
drh1cc8c442007-08-24 16:08:29 +00002179 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002180 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2181 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002182 readOnly = (fout&SQLITE_OPEN_READONLY);
2183
2184 /* If the file was successfully opened for read/write access,
2185 ** choose a default page size in case we have to create the
2186 ** database file. The default page size is the maximum of:
2187 **
2188 ** + SQLITE_DEFAULT_PAGE_SIZE,
2189 ** + The value returned by sqlite3OsSectorSize()
2190 ** + The largest page size that can be written atomically.
2191 */
2192 if( rc==SQLITE_OK && !readOnly ){
2193 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
2194 if( nDefaultPage<iSectorSize ){
2195 nDefaultPage = iSectorSize;
2196 }
danielk19779663b8f2007-08-24 11:52:28 +00002197#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002198 {
2199 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2200 int ii;
2201 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2202 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2203 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2204 for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2205 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002206 }
danielk1977b4b47412007-08-17 15:53:36 +00002207 }
drh1cc8c442007-08-24 16:08:29 +00002208#endif
2209 if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2210 nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2211 }
danielk19778def5ea2004-06-16 10:39:52 +00002212 }
drhac69b052004-05-12 13:30:07 +00002213 }
drh1cc8c442007-08-24 16:08:29 +00002214 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002215 /* If a temporary file is requested, it is not opened immediately.
2216 ** In this case we accept the default page size and delay actually
2217 ** opening the file until the first call to OsWrite().
2218 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002219 tempFile = 1;
2220 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002221 }
danielk1977aef0bf62005-12-30 16:28:01 +00002222
danielk1977b4b47412007-08-17 15:53:36 +00002223 if( pPager && rc==SQLITE_OK ){
danielk19779663b8f2007-08-24 11:52:28 +00002224 pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
drh3e7a6092002-12-07 21:45:14 +00002225 }
danielk1977aef0bf62005-12-30 16:28:01 +00002226
drh153c62c2007-08-24 03:51:33 +00002227 /* If an error occured in either of the blocks above.
2228 ** Free the Pager structure and close the file.
2229 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002230 ** any Pager.errMask variables.
2231 */
danielk1977b4b47412007-08-17 15:53:36 +00002232 if( !pPager || !pPager->pTmpSpace ){
2233 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002234 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002235 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002236 }
danielk1977aef0bf62005-12-30 16:28:01 +00002237
drh153c62c2007-08-24 03:51:33 +00002238 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2239 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002240
danielk1977b4b47412007-08-17 15:53:36 +00002241 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002242 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002243 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002244 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002245
drh99b90c32008-03-17 13:50:58 +00002246 /* Fill in Pager.zJournal[] */
drh1cc8c442007-08-24 16:08:29 +00002247 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2248 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
danielk1977b4b47412007-08-17 15:53:36 +00002249
drh3b59a5c2006-01-15 20:28:28 +00002250 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002251 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002252 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002253 /* pPager->stmtOpen = 0; */
2254 /* pPager->stmtInUse = 0; */
2255 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002256 pPager->dbSize = memDb-1;
danielk19779663b8f2007-08-24 11:52:28 +00002257 pPager->pageSize = nDefaultPage;
drh3b59a5c2006-01-15 20:28:28 +00002258 /* pPager->stmtSize = 0; */
2259 /* pPager->stmtJSize = 0; */
2260 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002261 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002262 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002263 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002264 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002265 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002266 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002267 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2268 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2269 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2270 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002271 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002272 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002273 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002274 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002275 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002276 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002277 /* pPager->pFirst = 0; */
2278 /* pPager->pFirstSynced = 0; */
2279 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002280 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00002281 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002282 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002283 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002284 }
drh3b59a5c2006-01-15 20:28:28 +00002285 /* pPager->pBusyHandler = 0; */
2286 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002287 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002288#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002289 pPager->iInUseMM = 0;
2290 pPager->iInUseDB = 0;
2291 if( !memDb ){
2292 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2293 sqlite3_mutex_enter(mutex);
2294 pPager->pNext = sqlite3PagerList;
2295 if( sqlite3PagerList ){
2296 assert( sqlite3PagerList->pPrev==0 );
2297 sqlite3PagerList->pPrev = pPager;
2298 }
2299 pPager->pPrev = 0;
2300 sqlite3PagerList = pPager;
2301 sqlite3_mutex_leave(mutex);
2302 }
danielk197752622822006-01-09 09:59:49 +00002303#endif
drhed7c8552001-04-11 14:29:21 +00002304 return SQLITE_OK;
2305}
2306
2307/*
drh90f5ecb2004-07-22 01:19:35 +00002308** Set the busy handler function.
2309*/
danielk19773b8a05f2007-03-19 17:44:26 +00002310void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002311 pPager->pBusyHandler = pBusyHandler;
2312}
2313
2314/*
drh72f82862001-05-24 21:06:34 +00002315** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002316** when the reference count on each page reaches zero. The destructor can
2317** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002318**
danielk19773b8a05f2007-03-19 17:44:26 +00002319** The destructor is not called as a result sqlite3PagerClose().
2320** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002321*/
danielk19773b8a05f2007-03-19 17:44:26 +00002322void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002323 pPager->xDestructor = xDesc;
2324}
2325
2326/*
drha6abd042004-06-09 17:37:22 +00002327** Set the reinitializer for this pager. If not NULL, the reinitializer
2328** is called when the content of a page in cache is restored to its original
2329** value as a result of a rollback. The callback gives higher-level code
2330** an opportunity to restore the EXTRA section to agree with the restored
2331** page data.
2332*/
danielk19773b8a05f2007-03-19 17:44:26 +00002333void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002334 pPager->xReiniter = xReinit;
2335}
2336
2337/*
danielk1977a1644fd2007-08-29 12:31:25 +00002338** Set the page size to *pPageSize. If the suggest new page size is
2339** inappropriate, then an alternative page size is set to that
2340** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002341*/
danielk1977a1644fd2007-08-29 12:31:25 +00002342int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2343 int rc = SQLITE_OK;
2344 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002345 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002346 if( pageSize && pageSize!=pPager->pageSize
2347 && !pPager->memDb && pPager->nRef==0
2348 ){
2349 char *pNew = (char *)sqlite3_malloc(pageSize);
2350 if( !pNew ){
2351 rc = SQLITE_NOMEM;
2352 }else{
2353 pagerEnter(pPager);
2354 pager_reset(pPager);
2355 pPager->pageSize = pageSize;
2356 setSectorSize(pPager);
2357 sqlite3_free(pPager->pTmpSpace);
2358 pPager->pTmpSpace = pNew;
2359 pagerLeave(pPager);
2360 }
drh1c7880e2005-05-20 20:01:55 +00002361 }
danielk1977a1644fd2007-08-29 12:31:25 +00002362 *pPageSize = pPager->pageSize;
2363 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002364}
2365
2366/*
drh26b79942007-11-28 16:19:56 +00002367** Return a pointer to the "temporary page" buffer held internally
2368** by the pager. This is a buffer that is big enough to hold the
2369** entire content of a database page. This buffer is used internally
2370** during rollback and will be overwritten whenever a rollback
2371** occurs. But other modules are free to use it too, as long as
2372** no rollbacks are happening.
2373*/
2374void *sqlite3PagerTempSpace(Pager *pPager){
2375 return pPager->pTmpSpace;
2376}
2377
2378/*
drhf8e632b2007-05-08 14:51:36 +00002379** Attempt to set the maximum database page count if mxPage is positive.
2380** Make no changes if mxPage is zero or negative. And never reduce the
2381** maximum page count below the current size of the database.
2382**
2383** Regardless of mxPage, return the current maximum page count.
2384*/
2385int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2386 if( mxPage>0 ){
2387 pPager->mxPgno = mxPage;
2388 }
2389 sqlite3PagerPagecount(pPager);
2390 return pPager->mxPgno;
2391}
2392
2393/*
drhc9ac5ca2005-11-04 22:03:30 +00002394** The following set of routines are used to disable the simulated
2395** I/O error mechanism. These routines are used to avoid simulated
2396** errors in places where we do not care about errors.
2397**
2398** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2399** and generate no code.
2400*/
2401#ifdef SQLITE_TEST
2402extern int sqlite3_io_error_pending;
2403extern int sqlite3_io_error_hit;
2404static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002405void disable_simulated_io_errors(void){
2406 saved_cnt = sqlite3_io_error_pending;
2407 sqlite3_io_error_pending = -1;
2408}
2409void enable_simulated_io_errors(void){
2410 sqlite3_io_error_pending = saved_cnt;
2411}
2412#else
drh152410f2005-11-05 15:11:22 +00002413# define disable_simulated_io_errors()
2414# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002415#endif
2416
2417/*
drh90f5ecb2004-07-22 01:19:35 +00002418** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002419** that pDest points to.
2420**
2421** No error checking is done. The rational for this is that this function
2422** may be called even if the file does not exist or contain a header. In
2423** these cases sqlite3OsRead() will return an error, to which the correct
2424** response is to zero the memory at pDest and continue. A real IO error
2425** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002426*/
danielk19773b8a05f2007-03-19 17:44:26 +00002427int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002428 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002429 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002430 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2431 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002432 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002433 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002434 if( rc==SQLITE_IOERR_SHORT_READ ){
2435 rc = SQLITE_OK;
2436 }
drh90f5ecb2004-07-22 01:19:35 +00002437 }
drh551b7732006-11-06 21:20:25 +00002438 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002439}
2440
2441/*
drh5e00f6c2001-09-13 13:46:56 +00002442** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002443** pPager.
2444**
2445** If the PENDING_BYTE lies on the page directly after the end of the
2446** file, then consider this page part of the file too. For example, if
2447** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2448** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002449*/
danielk19773b8a05f2007-03-19 17:44:26 +00002450int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002451 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002452 int rc;
drhd9b02572001-04-15 00:37:09 +00002453 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002454 if( pPager->errCode ){
danielk197743468d92008-02-26 06:05:31 +00002455 return -1;
drha7aea3d2007-03-15 12:51:16 +00002456 }
drhed7c8552001-04-11 14:29:21 +00002457 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002458 n = pPager->dbSize;
2459 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002460 assert(pPager->fd->pMethods||pPager->tempFile);
2461 if( (pPager->fd->pMethods)
2462 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002463 pPager->nRef++;
drhe49f9822006-09-15 12:29:16 +00002464 pager_error(pPager, rc);
danielk1977ae72d982007-10-03 08:46:44 +00002465 pPager->nRef--;
danielk197743468d92008-02-26 06:05:31 +00002466 return -1;
danielk197715f411d2005-09-16 10:18:45 +00002467 }
2468 if( n>0 && n<pPager->pageSize ){
2469 n = 1;
2470 }else{
2471 n /= pPager->pageSize;
2472 }
2473 if( pPager->state!=PAGER_UNLOCK ){
2474 pPager->dbSize = n;
2475 }
drhed7c8552001-04-11 14:29:21 +00002476 }
danielk197715f411d2005-09-16 10:18:45 +00002477 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002478 n++;
2479 }
drhf8e632b2007-05-08 14:51:36 +00002480 if( n>pPager->mxPgno ){
2481 pPager->mxPgno = n;
2482 }
drhed7c8552001-04-11 14:29:21 +00002483 return n;
2484}
2485
drhf07e7d52006-04-07 13:54:46 +00002486
2487#ifndef SQLITE_OMIT_MEMORYDB
2488/*
2489** Clear a PgHistory block
2490*/
2491static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002492 sqlite3_free(pHist->pOrig);
2493 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002494 pHist->pOrig = 0;
2495 pHist->pStmt = 0;
2496}
2497#else
2498#define clearHistory(x)
2499#endif
2500
drhed7c8552001-04-11 14:29:21 +00002501/*
drhf7c57532003-04-25 13:22:51 +00002502** Forward declaration
2503*/
danielk197776572402004-06-25 02:38:54 +00002504static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002505
2506/*
drh85b623f2007-12-13 21:54:09 +00002507** Unlink pPg from its hash chain. Also set the page number to 0 to indicate
danielk1977f5fdda82004-11-03 08:44:05 +00002508** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002509** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002510** pNextFree/pPrevFree list that is not a part of any hash-chain.
2511*/
2512static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2513 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002514 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002515 return;
2516 }
2517 if( pPg->pNextHash ){
2518 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2519 }
2520 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002521 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002522 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2523 }else{
drh8ca0c722006-05-07 17:49:38 +00002524 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002525 pPager->aHash[h] = pPg->pNextHash;
2526 }
drh5229ae42006-03-23 23:29:04 +00002527 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002528 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2529 }
danielk1977f5fdda82004-11-03 08:44:05 +00002530 pPg->pgno = 0;
2531 pPg->pNextHash = pPg->pPrevHash = 0;
2532}
2533
2534/*
drhac69b052004-05-12 13:30:07 +00002535** Unlink a page from the free list (the list of all pages where nRef==0)
2536** and from its hash collision chain.
2537*/
2538static void unlinkPage(PgHdr *pPg){
2539 Pager *pPager = pPg->pPager;
2540
danielk19779f61c2f2007-08-27 17:27:49 +00002541 /* Unlink from free page list */
2542 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002543
2544 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002545 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002546}
2547
2548/*
danielk1977e180dd92007-04-05 17:15:52 +00002549** This routine is used to truncate the cache when a database
2550** is truncated. Drop from the cache all pages whose pgno is
2551** larger than pPager->dbSize and is unreferenced.
2552**
drhac69b052004-05-12 13:30:07 +00002553** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002554**
2555** Actually, at the point this routine is called, it would be
2556** an error to have a referenced page. But rather than delete
2557** that page and guarantee a subsequent segfault, it seems better
2558** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002559*/
danielk1977e180dd92007-04-05 17:15:52 +00002560static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002561 PgHdr *pPg;
2562 PgHdr **ppPg;
2563 int dbSize = pPager->dbSize;
2564
2565 ppPg = &pPager->pAll;
2566 while( (pPg = *ppPg)!=0 ){
2567 if( pPg->pgno<=dbSize ){
2568 ppPg = &pPg->pNextAll;
2569 }else if( pPg->nRef>0 ){
2570 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2571 ppPg = &pPg->pNextAll;
2572 }else{
2573 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002574 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2575 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002576 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002577 makeClean(pPg);
drh0d180202008-02-14 23:26:56 +00002578 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00002579 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002580 pPager->nPage--;
2581 }
2582 }
2583}
2584
drhf7c57532003-04-25 13:22:51 +00002585/*
danielk197717221812005-02-15 03:38:05 +00002586** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002587** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002588** false or until the lock succeeds.
2589**
2590** Return SQLITE_OK on success and an error code if we cannot obtain
2591** the lock.
2592*/
2593static int pager_wait_on_lock(Pager *pPager, int locktype){
2594 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002595
2596 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002597 assert( PAGER_SHARED==SHARED_LOCK );
2598 assert( PAGER_RESERVED==RESERVED_LOCK );
2599 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002600
2601 /* If the file is currently unlocked then the size must be unknown */
2602 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2603
danielk197717221812005-02-15 03:38:05 +00002604 if( pPager->state>=locktype ){
2605 rc = SQLITE_OK;
2606 }else{
drhbefdf832008-03-14 19:33:05 +00002607 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002608 do {
drh054889e2005-11-30 03:20:31 +00002609 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002610 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002611 if( rc==SQLITE_OK ){
2612 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002613 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002614 }
2615 }
2616 return rc;
2617}
2618
2619/*
drhf7c57532003-04-25 13:22:51 +00002620** Truncate the file to the number of pages specified.
2621*/
danielk19773b8a05f2007-03-19 17:44:26 +00002622int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002623 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002624 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002625 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002626 if( pPager->errCode ){
2627 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002628 return rc;
2629 }
drh7d02cb72003-06-04 16:24:39 +00002630 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002631 return SQLITE_OK;
2632 }
drhb7f91642004-10-31 02:22:47 +00002633 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002634 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002635 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002636 return SQLITE_OK;
2637 }
drh86f8c192007-08-22 00:39:19 +00002638 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002639 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002640 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002641 if( rc!=SQLITE_OK ){
2642 return rc;
2643 }
danielk197717221812005-02-15 03:38:05 +00002644
2645 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002646 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002647 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002648 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002649 if( rc!=SQLITE_OK ){
2650 return rc;
2651 }
2652
drhcb4c40b2004-08-18 19:09:43 +00002653 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002654 return rc;
2655}
2656
2657/*
drhed7c8552001-04-11 14:29:21 +00002658** Shutdown the page cache. Free all memory and close all files.
2659**
2660** If a transaction was in progress when this routine is called, that
2661** transaction is rolled back. All outstanding pages are invalidated
2662** and their memory is freed. Any attempt to use a page associated
2663** with this page cache after this function returns will likely
2664** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002665**
2666** This function always succeeds. If a transaction is active an attempt
2667** is made to roll it back. If an error occurs during the rollback
2668** a hot journal may be left in the filesystem but no error is returned
2669** to the caller.
drhed7c8552001-04-11 14:29:21 +00002670*/
danielk19773b8a05f2007-03-19 17:44:26 +00002671int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002672#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002673 if( !MEMDB ){
2674 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2675 sqlite3_mutex_enter(mutex);
2676 if( pPager->pPrev ){
2677 pPager->pPrev->pNext = pPager->pNext;
2678 }else{
2679 sqlite3PagerList = pPager->pNext;
2680 }
2681 if( pPager->pNext ){
2682 pPager->pNext->pPrev = pPager->pPrev;
2683 }
2684 sqlite3_mutex_leave(mutex);
2685 }
danielk197713f72992005-12-18 08:51:22 +00002686#endif
2687
drhbafda092007-01-03 23:36:22 +00002688 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002689 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002690 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002691 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002692 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002693 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002694 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002695 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002696 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002697 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002698 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002699 }
drhf5e7bb52008-02-18 14:47:33 +00002700 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002701 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002702 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002703 }
danielk1977b4b47412007-08-17 15:53:36 +00002704 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002705 /* Temp files are automatically deleted by the OS
2706 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002707 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002708 ** }
2709 */
danielk1977aca790a2005-01-13 11:07:52 +00002710
drh17435752007-08-16 04:30:38 +00002711 sqlite3_free(pPager->aHash);
2712 sqlite3_free(pPager->pTmpSpace);
2713 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002714 return SQLITE_OK;
2715}
2716
drh87cc3b32007-05-08 21:45:27 +00002717#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002718/*
drh5e00f6c2001-09-13 13:46:56 +00002719** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002720*/
danielk19773b8a05f2007-03-19 17:44:26 +00002721Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002722 return p->pgno;
2723}
drh87cc3b32007-05-08 21:45:27 +00002724#endif
drhed7c8552001-04-11 14:29:21 +00002725
2726/*
drhc8629a12004-05-08 20:07:40 +00002727** The page_ref() function increments the reference count for a page.
2728** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002729** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002730**
2731** For non-test systems, page_ref() is a macro that calls _page_ref()
2732** online of the reference count is zero. For test systems, page_ref()
2733** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002734*/
drh836faa42003-01-11 13:30:57 +00002735static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002736 if( pPg->nRef==0 ){
2737 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002738 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002739 pPg->pPager->nRef++;
2740 }
2741 pPg->nRef++;
drhdf0b3b02001-06-23 11:36:20 +00002742}
danielk1977aca790a2005-01-13 11:07:52 +00002743#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002744 static void page_ref(PgHdr *pPg){
2745 if( pPg->nRef==0 ){
2746 _page_ref(pPg);
2747 }else{
2748 pPg->nRef++;
drhc8629a12004-05-08 20:07:40 +00002749 }
2750 }
2751#else
2752# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2753#endif
drhdf0b3b02001-06-23 11:36:20 +00002754
2755/*
2756** Increment the reference count for a page. The input pointer is
2757** a reference to the page data.
2758*/
danielk19773b8a05f2007-03-19 17:44:26 +00002759int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002760 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002761 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002762 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002763 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002764}
2765
2766/*
drh34e79ce2004-02-08 06:05:46 +00002767** Sync the journal. In other words, make sure all the pages that have
2768** been written to the journal have actually reached the surface of the
2769** disk. It is not safe to modify the original database file until after
2770** the journal has been synced. If the original database is modified before
2771** the journal is synced and a power failure occurs, the unsynced journal
2772** data would be lost and we would be unable to completely rollback the
2773** database changes. Database corruption would occur.
2774**
2775** This routine also updates the nRec field in the header of the journal.
2776** (See comments on the pager_playback() routine for additional information.)
2777** If the sync mode is FULL, two syncs will occur. First the whole journal
2778** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002779**
drh34e79ce2004-02-08 06:05:46 +00002780** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002781** after a power failure, so no sync occurs.
2782**
2783** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2784** the database is stored, then OsSync() is never called on the journal
2785** file. In this case all that is required is to update the nRec field in
2786** the journal header.
drhfa86c412002-02-02 15:01:15 +00002787**
drh34e79ce2004-02-08 06:05:46 +00002788** This routine clears the needSync field of every page current held in
2789** memory.
drh50e5dad2001-09-15 00:57:28 +00002790*/
danielk197776572402004-06-25 02:38:54 +00002791static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002792 PgHdr *pPg;
2793 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002794
danielk19774cd2cd52007-08-23 14:48:23 +00002795
drh03eb96a2002-11-10 23:32:56 +00002796 /* Sync the journal before modifying the main database
2797 ** (assuming there is a journal and it needs to be synced.)
2798 */
danielk197776572402004-06-25 02:38:54 +00002799 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002800 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002801 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002802 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002803
drh946966f2004-02-25 02:20:41 +00002804 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2805 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00002806#ifndef NDEBUG
2807 {
drh34e79ce2004-02-08 06:05:46 +00002808 /* Make sure the pPager->nRec counter we are keeping agrees
2809 ** with the nRec computed from the size of the journal file.
2810 */
drheb206252004-10-01 02:00:31 +00002811 i64 jSz;
drh054889e2005-11-30 03:20:31 +00002812 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00002813 if( rc!=0 ) return rc;
danielk197776572402004-06-25 02:38:54 +00002814 assert( pPager->journalOff==jSz );
drh968af522003-02-11 14:55:40 +00002815 }
2816#endif
danielk19774cd2cd52007-08-23 14:48:23 +00002817 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002818 /* Write the nRec value into the journal file header. If in
2819 ** full-synchronous mode, sync the journal first. This ensures that
2820 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002821 ** it as a candidate for rollback.
2822 **
2823 ** This is not required if the persistent media supports the
2824 ** SAFE_APPEND property. Because in this case it is not possible
2825 ** for garbage data to be appended to the file, the nRec field
2826 ** is populated with 0xFFFFFFFF when the journal header is written
2827 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002828 */
danielk19774cd2cd52007-08-23 14:48:23 +00002829 i64 jrnlOff;
2830 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002831 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002832 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002833 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002834 if( rc!=0 ) return rc;
2835 }
danielk197713adf8a2004-06-03 16:08:41 +00002836
danielk197762079062007-08-15 17:08:46 +00002837 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2838 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2839 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002840 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002841 }
danielk19774cd2cd52007-08-23 14:48:23 +00002842 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2843 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2844 IOTRACE(("JSYNC %p\n", pPager))
2845 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2846 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2847 );
2848 if( rc!=0 ) return rc;
2849 }
drhdb48ee02003-01-16 13:42:43 +00002850 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002851 }
drh50e5dad2001-09-15 00:57:28 +00002852 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002853
2854 /* Erase the needSync flag from every page.
2855 */
2856 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2857 pPg->needSync = 0;
2858 }
danielk19779f61c2f2007-08-27 17:27:49 +00002859 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002860 }
drh03eb96a2002-11-10 23:32:56 +00002861
drh341eae82003-01-21 02:39:36 +00002862#ifndef NDEBUG
2863 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2864 ** flag must also be clear for all pages. Verify that this
2865 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002866 */
drh341eae82003-01-21 02:39:36 +00002867 else{
2868 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2869 assert( pPg->needSync==0 );
2870 }
danielk19779f61c2f2007-08-27 17:27:49 +00002871 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002872 }
drh341eae82003-01-21 02:39:36 +00002873#endif
drhdb48ee02003-01-16 13:42:43 +00002874
drh81a20f22001-10-12 17:30:04 +00002875 return rc;
drh50e5dad2001-09-15 00:57:28 +00002876}
2877
2878/*
drhdc3e10b2006-06-15 14:31:06 +00002879** Merge two lists of pages connected by pDirty and in pgno order.
2880** Do not both fixing the pPrevDirty pointers.
2881*/
2882static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2883 PgHdr result, *pTail;
2884 pTail = &result;
2885 while( pA && pB ){
2886 if( pA->pgno<pB->pgno ){
2887 pTail->pDirty = pA;
2888 pTail = pA;
2889 pA = pA->pDirty;
2890 }else{
2891 pTail->pDirty = pB;
2892 pTail = pB;
2893 pB = pB->pDirty;
2894 }
2895 }
2896 if( pA ){
2897 pTail->pDirty = pA;
2898 }else if( pB ){
2899 pTail->pDirty = pB;
2900 }else{
2901 pTail->pDirty = 0;
2902 }
2903 return result.pDirty;
2904}
2905
2906/*
2907** Sort the list of pages in accending order by pgno. Pages are
2908** connected by pDirty pointers. The pPrevDirty pointers are
2909** corrupted by this sort.
2910*/
danielk197724168722007-04-02 05:07:47 +00002911#define N_SORT_BUCKET_ALLOC 25
2912#define N_SORT_BUCKET 25
2913#ifdef SQLITE_TEST
2914 int sqlite3_pager_n_sort_bucket = 0;
2915 #undef N_SORT_BUCKET
2916 #define N_SORT_BUCKET \
2917 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2918#endif
drhdc3e10b2006-06-15 14:31:06 +00002919static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002920 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002921 int i;
2922 memset(a, 0, sizeof(a));
2923 while( pIn ){
2924 p = pIn;
2925 pIn = p->pDirty;
2926 p->pDirty = 0;
2927 for(i=0; i<N_SORT_BUCKET-1; i++){
2928 if( a[i]==0 ){
2929 a[i] = p;
2930 break;
2931 }else{
2932 p = merge_pagelist(a[i], p);
2933 a[i] = 0;
2934 }
2935 }
2936 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002937 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2938 ** elements in the input list. This is possible, but impractical.
2939 ** Testing this line is the point of global variable
2940 ** sqlite3_pager_n_sort_bucket.
2941 */
drhdc3e10b2006-06-15 14:31:06 +00002942 a[i] = merge_pagelist(a[i], p);
2943 }
2944 }
2945 p = a[0];
2946 for(i=1; i<N_SORT_BUCKET; i++){
2947 p = merge_pagelist(p, a[i]);
2948 }
2949 return p;
2950}
2951
2952/*
drh2554f8b2003-01-22 01:26:44 +00002953** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2954** every one of those pages out to the database file and mark them all
2955** as clean.
2956*/
2957static int pager_write_pagelist(PgHdr *pList){
2958 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00002959 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00002960 int rc;
2961
2962 if( pList==0 ) return SQLITE_OK;
2963 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002964
2965 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2966 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002967 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002968 **
drha6abd042004-06-09 17:37:22 +00002969 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2970 ** through an intermediate state PENDING. A PENDING lock prevents new
2971 ** readers from attaching to the database but is unsufficient for us to
2972 ** write. The idea of a PENDING lock is to prevent new readers from
2973 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002974 **
drha6abd042004-06-09 17:37:22 +00002975 ** While the pager is in the RESERVED state, the original database file
2976 ** is unchanged and we can rollback without having to playback the
2977 ** journal into the original database file. Once we transition to
2978 ** EXCLUSIVE, it means the database file has been changed and any rollback
2979 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002980 */
drh684917c2004-10-05 02:41:42 +00002981 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002982 if( rc!=SQLITE_OK ){
2983 return rc;
2984 }
2985
drhdc3e10b2006-06-15 14:31:06 +00002986 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00002987 for(p=pList; p; p=p->pDirty){
2988 assert( p->dirty );
2989 p->dirty = 0;
2990 }
drh2554f8b2003-01-22 01:26:44 +00002991 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002992
2993 /* If the file has not yet been opened, open it now. */
2994 if( !pPager->fd->pMethods ){
2995 assert(pPager->tempFile);
drh33f4e022007-09-03 15:19:34 +00002996 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
2997 pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002998 if( rc ) return rc;
2999 }
3000
danielk1977687566d2004-11-02 12:56:41 +00003001 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00003002 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00003003 ** make the file smaller (presumably by auto-vacuum code). Do not write
3004 ** any such pages to the file.
3005 */
3006 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00003007 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00003008 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00003009 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
3010 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00003011 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00003012 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003013 PAGER_INCR(sqlite3_pager_writedb_count);
3014 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00003015 if( pList->pgno==1 ){
3016 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
3017 }
danielk1977687566d2004-11-02 12:56:41 +00003018 }
3019#ifndef NDEBUG
3020 else{
drh4f0c5872007-03-26 22:05:01 +00003021 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00003022 }
3023#endif
drh2554f8b2003-01-22 01:26:44 +00003024 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00003025#ifdef SQLITE_CHECK_PAGES
3026 pList->pageHash = pager_pagehash(pList);
3027#endif
drh2554f8b2003-01-22 01:26:44 +00003028 pList = pList->pDirty;
3029 }
3030 return SQLITE_OK;
3031}
3032
3033/*
3034** Collect every dirty page into a dirty list and
3035** return a pointer to the head of that list. All pages are
3036** collected even if they are still in use.
3037*/
3038static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh8f2e9a12008-02-26 14:46:04 +00003039
3040#ifndef NDEBUG
3041 /* Verify the sanity of the dirty list when we are running
3042 ** in debugging mode. This is expensive, so do not
3043 ** do this on a normal build. */
3044 int n1 = 0;
3045 int n2 = 0;
3046 PgHdr *p;
3047 for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; }
3048 for(p=pPager->pDirty; p; p=p->pDirty){ n2++; }
3049 assert( n1==n2 );
3050#endif
3051
drh605ad8f2006-05-03 23:34:05 +00003052 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00003053}
3054
3055/*
drh165ffe92005-03-15 17:09:30 +00003056** Return TRUE if there is a hot journal on the given pager.
3057** A hot journal is one that needs to be played back.
3058**
3059** If the current size of the database file is 0 but a journal file
3060** exists, that is probably an old journal left over from a prior
3061** database with the same name. Just delete the journal.
3062*/
3063static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003064 sqlite3_vfs *pVfs = pPager->pVfs;
drh165ffe92005-03-15 17:09:30 +00003065 if( !pPager->useJournal ) return 0;
drhd3cf2472007-11-27 16:55:07 +00003066 if( !pPager->fd->pMethods ) return 0;
danielk1977b4b47412007-08-17 15:53:36 +00003067 if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
drhbb5f18d2007-04-06 18:23:17 +00003068 return 0;
3069 }
3070 if( sqlite3OsCheckReservedLock(pPager->fd) ){
3071 return 0;
3072 }
danielk19773b8a05f2007-03-19 17:44:26 +00003073 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003074 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00003075 return 0;
3076 }else{
3077 return 1;
3078 }
3079}
3080
danielk19775591df52005-12-20 09:19:37 +00003081/*
danielk1977aef0bf62005-12-30 16:28:01 +00003082** Try to find a page in the cache that can be recycled.
3083**
3084** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00003085** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00003086*/
danielk1977843e65f2007-09-01 16:16:15 +00003087static int pager_recycle(Pager *pPager, PgHdr **ppPg){
danielk197713f72992005-12-18 08:51:22 +00003088 PgHdr *pPg;
3089 *ppPg = 0;
3090
danielk1977843e65f2007-09-01 16:16:15 +00003091 /* It is illegal to call this function unless the pager object
3092 ** pointed to by pPager has at least one free page (page with nRef==0).
3093 */
danielk1977c551edc2007-04-05 13:12:13 +00003094 assert(!MEMDB);
danielk1977843e65f2007-09-01 16:16:15 +00003095 assert(pPager->lru.pFirst);
danielk1977c551edc2007-04-05 13:12:13 +00003096
danielk197713f72992005-12-18 08:51:22 +00003097 /* Find a page to recycle. Try to locate a page that does not
3098 ** require us to do an fsync() on the journal.
3099 */
danielk19779f61c2f2007-08-27 17:27:49 +00003100 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00003101
3102 /* If we could not find a page that does not require an fsync()
3103 ** on the journal file then fsync the journal file. This is a
3104 ** very slow operation, so we work hard to avoid it. But sometimes
3105 ** it can't be helped.
3106 */
danielk1977843e65f2007-09-01 16:16:15 +00003107 if( pPg==0 && pPager->lru.pFirst){
danielk19774cd2cd52007-08-23 14:48:23 +00003108 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk197713f72992005-12-18 08:51:22 +00003109 int rc = syncJournal(pPager);
3110 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003111 return rc;
danielk197713f72992005-12-18 08:51:22 +00003112 }
danielk19774cd2cd52007-08-23 14:48:23 +00003113 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197713f72992005-12-18 08:51:22 +00003114 /* If in full-sync mode, write a new journal header into the
3115 ** journal file. This is done to avoid ever modifying a journal
3116 ** header that is involved in the rollback of pages that have
3117 ** already been written to the database (in case the header is
3118 ** trashed when the nRec field is updated).
3119 */
3120 pPager->nRec = 0;
3121 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00003122 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00003123 rc = writeJournalHdr(pPager);
3124 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003125 return rc;
danielk197713f72992005-12-18 08:51:22 +00003126 }
3127 }
danielk19779f61c2f2007-08-27 17:27:49 +00003128 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003129 }
danielk197713f72992005-12-18 08:51:22 +00003130
3131 assert( pPg->nRef==0 );
3132
3133 /* Write the page to the database file if it is dirty.
3134 */
3135 if( pPg->dirty ){
3136 int rc;
3137 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003138 makeClean(pPg);
3139 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003140 pPg->pDirty = 0;
3141 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003142 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003143 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003144 return rc;
danielk197713f72992005-12-18 08:51:22 +00003145 }
3146 }
3147 assert( pPg->dirty==0 );
3148
3149 /* If the page we are recycling is marked as alwaysRollback, then
3150 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003151 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003152 ** It is necessary to do this because the page marked alwaysRollback
3153 ** might be reloaded at a later time but at that point we won't remember
3154 ** that is was marked alwaysRollback. This means that all pages must
3155 ** be marked as alwaysRollback from here on out.
3156 */
3157 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003158 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003159 pPager->alwaysRollback = 1;
3160 }
3161
3162 /* Unlink the old page from the free list and the hash table
3163 */
3164 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003165 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003166
3167 *ppPg = pPg;
3168 return SQLITE_OK;
3169}
3170
drh86f8c192007-08-22 00:39:19 +00003171#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003172/*
3173** This function is called to free superfluous dynamically allocated memory
3174** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003175** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003176**
3177** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003178** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003179** of bytes of memory released.
3180*/
danielk19773b8a05f2007-03-19 17:44:26 +00003181int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003182 int nReleased = 0; /* Bytes of memory released so far */
3183 sqlite3_mutex *mutex; /* The MEM2 mutex */
3184 Pager *pPager; /* For looping over pagers */
drhe5fe6902007-12-07 18:55:28 +00003185 BusyHandler *savedBusy; /* Saved copy of the busy handler */
danielk19779f61c2f2007-08-27 17:27:49 +00003186 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003187
drh86f8c192007-08-22 00:39:19 +00003188 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003189 */
drh86f8c192007-08-22 00:39:19 +00003190 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
3191 sqlite3_mutex_enter(mutex);
3192
3193 /* Signal all database connections that memory management wants
3194 ** to have access to the pagers.
3195 */
3196 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3197 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003198 }
3199
danielk19779f61c2f2007-08-27 17:27:49 +00003200 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3201 PgHdr *pPg;
3202 PgHdr *pRecycled;
3203
3204 /* Try to find a page to recycle that does not require a sync(). If
3205 ** this is not possible, find one that does require a sync().
3206 */
3207 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3208 pPg = sqlite3LruPageList.pFirstSynced;
3209 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3210 pPg = pPg->gfree.pNext;
3211 }
3212 if( !pPg ){
3213 pPg = sqlite3LruPageList.pFirst;
3214 while( pPg && pPg->pPager->iInUseDB ){
3215 pPg = pPg->gfree.pNext;
3216 }
3217 }
3218 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003219
danielk197784f786f2007-08-28 08:00:17 +00003220 /* If pPg==0, then the block above has failed to find a page to
3221 ** recycle. In this case return early - no further memory will
3222 ** be released.
3223 */
danielk19779f61c2f2007-08-27 17:27:49 +00003224 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003225
danielk19779f61c2f2007-08-27 17:27:49 +00003226 pPager = pPg->pPager;
3227 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3228 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3229
drhe5fe6902007-12-07 18:55:28 +00003230 savedBusy = pPager->pBusyHandler;
3231 pPager->pBusyHandler = 0;
danielk1977843e65f2007-09-01 16:16:15 +00003232 rc = pager_recycle(pPager, &pRecycled);
drhe5fe6902007-12-07 18:55:28 +00003233 pPager->pBusyHandler = savedBusy;
danielk19779f61c2f2007-08-27 17:27:49 +00003234 assert(pRecycled==pPg || rc!=SQLITE_OK);
3235 if( rc==SQLITE_OK ){
3236 /* We've found a page to free. At this point the page has been
3237 ** removed from the page hash-table, free-list and synced-list
3238 ** (pFirstSynced). It is still in the all pages (pAll) list.
3239 ** Remove it from this list before freeing.
3240 **
3241 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3242 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003243 */
danielk19779f61c2f2007-08-27 17:27:49 +00003244 PgHdr *pTmp;
3245 assert( pPg );
3246 if( pPg==pPager->pAll ){
3247 pPager->pAll = pPg->pNextAll;
3248 }else{
3249 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3250 pTmp->pNextAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00003251 }
danielk19779f61c2f2007-08-27 17:27:49 +00003252 nReleased += (
3253 sizeof(*pPg) + pPager->pageSize
3254 + sizeof(u32) + pPager->nExtra
3255 + MEMDB*sizeof(PgHistory)
3256 );
3257 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3258 PAGER_INCR(sqlite3_pager_pgfree_count);
drh0d180202008-02-14 23:26:56 +00003259 sqlite3_free(pPg->pData);
danielk19779f61c2f2007-08-27 17:27:49 +00003260 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003261 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003262 }else{
3263 /* An error occured whilst writing to the database file or
3264 ** journal in pager_recycle(). The error is not returned to the
3265 ** caller of this function. Instead, set the Pager.errCode variable.
3266 ** The error will be returned to the user (or users, in the case
3267 ** of a shared pager cache) of the pager for which the error occured.
3268 */
3269 assert(
3270 (rc&0xff)==SQLITE_IOERR ||
3271 rc==SQLITE_FULL ||
3272 rc==SQLITE_BUSY
3273 );
3274 assert( pPager->state>=PAGER_RESERVED );
3275 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003276 }
3277 }
danielk1977aef0bf62005-12-30 16:28:01 +00003278
drh86f8c192007-08-22 00:39:19 +00003279 /* Clear the memory management flags and release the mutex
3280 */
3281 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3282 pPager->iInUseMM = 0;
3283 }
3284 sqlite3_mutex_leave(mutex);
3285
3286 /* Return the number of bytes released
3287 */
danielk197713f72992005-12-18 08:51:22 +00003288 return nReleased;
3289}
drh86f8c192007-08-22 00:39:19 +00003290#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003291
drh165ffe92005-03-15 17:09:30 +00003292/*
danielk1977e180dd92007-04-05 17:15:52 +00003293** Read the content of page pPg out of the database file.
3294*/
3295static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3296 int rc;
danielk197762079062007-08-15 17:08:46 +00003297 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003298 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003299 assert(pPager->fd->pMethods||pPager->tempFile);
3300 if( !pPager->fd->pMethods ){
3301 return SQLITE_IOERR_SHORT_READ;
3302 }
danielk197762079062007-08-15 17:08:46 +00003303 offset = (pgno-1)*(i64)pPager->pageSize;
3304 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003305 PAGER_INCR(sqlite3_pager_readdb_count);
3306 PAGER_INCR(pPager->nRead);
3307 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003308 if( pgno==1 ){
3309 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3310 sizeof(pPager->dbFileVers));
3311 }
danielk1977e180dd92007-04-05 17:15:52 +00003312 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003313 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3314 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003315 return rc;
3316}
3317
3318
3319/*
danielk1977e277be02007-03-23 18:12:06 +00003320** This function is called to obtain the shared lock required before
3321** data may be read from the pager cache. If the shared lock has already
3322** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003323**
3324** Immediately after obtaining the shared lock (if required), this function
3325** checks for a hot-journal file. If one is found, an emergency rollback
3326** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003327*/
3328static int pagerSharedLock(Pager *pPager){
3329 int rc = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00003330 int isHot = 0;
danielk1977e277be02007-03-23 18:12:06 +00003331
danielk1977ae72d982007-10-03 08:46:44 +00003332 /* If this database is opened for exclusive access, has no outstanding
3333 ** page references and is in an error-state, now is the chance to clear
3334 ** the error. Discard the contents of the pager-cache and treat any
3335 ** open journal file as a hot-journal.
3336 */
3337 if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
3338 if( pPager->journalOpen ){
3339 isHot = 1;
3340 }
3341 pager_reset(pPager);
3342 pPager->errCode = SQLITE_OK;
3343 }
3344
3345 /* If the pager is still in an error state, do not proceed. The error
3346 ** state will be cleared at some point in the future when all page
3347 ** references are dropped and the cache can be discarded.
3348 */
3349 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3350 return pPager->errCode;
3351 }
3352
3353 if( pPager->state==PAGER_UNLOCK || isHot ){
danielk1977b4b47412007-08-17 15:53:36 +00003354 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003355 if( !MEMDB ){
3356 assert( pPager->nRef==0 );
3357 if( !pPager->noReadlock ){
3358 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3359 if( rc!=SQLITE_OK ){
3360 return pager_error(pPager, rc);
3361 }
3362 assert( pPager->state>=SHARED_LOCK );
3363 }
3364
3365 /* If a journal file exists, and there is no RESERVED lock on the
3366 ** database file, then it either needs to be played back or deleted.
3367 */
danielk1977ae72d982007-10-03 08:46:44 +00003368 if( hasHotJournal(pPager) || isHot ){
danielk1977e277be02007-03-23 18:12:06 +00003369 /* Get an EXCLUSIVE lock on the database file. At this point it is
3370 ** important that a RESERVED lock is not obtained on the way to the
3371 ** EXCLUSIVE lock. If it were, another process might open the
3372 ** database file, detect the RESERVED lock, and conclude that the
3373 ** database is safe to read while this process is still rolling it
3374 ** back.
3375 **
3376 ** Because the intermediate RESERVED lock is not requested, the
3377 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00003378 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00003379 */
danielk1977ae72d982007-10-03 08:46:44 +00003380 if( pPager->state<EXCLUSIVE_LOCK ){
3381 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3382 if( rc!=SQLITE_OK ){
3383 pager_unlock(pPager);
3384 return pager_error(pPager, rc);
3385 }
3386 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00003387 }
danielk1977e277be02007-03-23 18:12:06 +00003388
3389 /* Open the journal for reading only. Return SQLITE_BUSY if
3390 ** we are unable to open the journal file.
3391 **
3392 ** The journal file does not need to be locked itself. The
3393 ** journal file is never open unless the main database file holds
3394 ** a write lock, so there is never any chance of two or more
3395 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00003396 **
drhfd131da2007-08-07 17:13:03 +00003397 ** Open the journal for read/write access. This is because in
3398 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003399 ** possibly used for a transaction later on. On some systems, the
3400 ** OsTruncate() call used in exclusive-access mode also requires
3401 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003402 */
danielk1977ae72d982007-10-03 08:46:44 +00003403 if( !isHot ){
3404 rc = SQLITE_BUSY;
3405 if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
3406 int fout = 0;
3407 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3408 assert( !pPager->tempFile );
3409 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3410 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3411 if( fout&SQLITE_OPEN_READONLY ){
3412 rc = SQLITE_BUSY;
3413 sqlite3OsClose(pPager->jfd);
3414 }
danielk1977979f38e2007-03-27 16:19:51 +00003415 }
3416 }
danielk1977e277be02007-03-23 18:12:06 +00003417 if( rc!=SQLITE_OK ){
3418 pager_unlock(pPager);
drh1aa5af12008-03-07 19:51:14 +00003419 switch( rc ){
3420 case SQLITE_NOMEM:
3421 case SQLITE_IOERR_UNLOCK:
3422 case SQLITE_IOERR_NOMEM:
3423 return rc;
3424 default:
3425 return SQLITE_BUSY;
3426 }
danielk1977e277be02007-03-23 18:12:06 +00003427 }
3428 pPager->journalOpen = 1;
3429 pPager->journalStarted = 0;
3430 pPager->journalOff = 0;
3431 pPager->setMaster = 0;
3432 pPager->journalHdr = 0;
3433
3434 /* Playback and delete the journal. Drop the database write
3435 ** lock and reacquire the read lock.
3436 */
3437 rc = pager_playback(pPager, 1);
3438 if( rc!=SQLITE_OK ){
3439 return pager_error(pPager, rc);
3440 }
danielk1977c5859712007-03-26 12:26:27 +00003441 assert(pPager->state==PAGER_SHARED ||
3442 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3443 );
danielk1977e277be02007-03-23 18:12:06 +00003444 }
3445
3446 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003447 /* The shared-lock has just been acquired on the database file
3448 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003449 ** read or write transaction). Check to see if the database
3450 ** has been modified. If the database has changed, flush the
3451 ** cache.
3452 **
3453 ** Database changes is detected by looking at 15 bytes beginning
3454 ** at offset 24 into the file. The first 4 of these 16 bytes are
3455 ** a 32-bit counter that is incremented with each change. The
3456 ** other bytes change randomly with each file change when
3457 ** a codec is in use.
3458 **
3459 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003460 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003461 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003462 */
drh86a88112007-04-16 15:02:19 +00003463 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003464 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003465
danielk1977e180dd92007-04-05 17:15:52 +00003466 if( pPager->errCode ){
3467 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00003468 }
3469
danielk1977e180dd92007-04-05 17:15:52 +00003470 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003471 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003472 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003473 if( rc!=SQLITE_OK ){
3474 return rc;
3475 }
drh86a88112007-04-16 15:02:19 +00003476 }else{
3477 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003478 }
3479
drh86a88112007-04-16 15:02:19 +00003480 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003481 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003482 }
3483 }
3484 }
danielk1977c5859712007-03-26 12:26:27 +00003485 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3486 if( pPager->state==PAGER_UNLOCK ){
3487 pPager->state = PAGER_SHARED;
3488 }
danielk1977e277be02007-03-23 18:12:06 +00003489 }
3490
3491 return rc;
3492}
3493
3494/*
danielk1977e180dd92007-04-05 17:15:52 +00003495** Allocate a PgHdr object. Either create a new one or reuse
3496** an existing one that is not otherwise in use.
3497**
3498** A new PgHdr structure is created if any of the following are
3499** true:
3500**
3501** (1) We have not exceeded our maximum allocated cache size
3502** as set by the "PRAGMA cache_size" command.
3503**
3504** (2) There are no unused PgHdr objects available at this time.
3505**
3506** (3) This is an in-memory database.
3507**
3508** (4) There are no PgHdr objects that do not require a journal
3509** file sync and a sync of the journal file is currently
3510** prohibited.
3511**
3512** Otherwise, reuse an existing PgHdr. In other words, reuse an
3513** existing PgHdr if all of the following are true:
3514**
3515** (1) We have reached or exceeded the maximum cache size
3516** allowed by "PRAGMA cache_size".
3517**
3518** (2) There is a PgHdr available with PgHdr->nRef==0
3519**
3520** (3) We are not in an in-memory database
3521**
3522** (4) Either there is an available PgHdr that does not need
3523** to be synced to disk or else disk syncing is currently
3524** allowed.
danielk197724168722007-04-02 05:07:47 +00003525*/
3526static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3527 int rc = SQLITE_OK;
3528 PgHdr *pPg;
drh1e3af332007-10-20 13:17:54 +00003529 int nByteHdr;
danielk197724168722007-04-02 05:07:47 +00003530
danielk1977e180dd92007-04-05 17:15:52 +00003531 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003532 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003533 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003534 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003535 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003536 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003537 ){
drh0d180202008-02-14 23:26:56 +00003538 void *pData;
danielk197724168722007-04-02 05:07:47 +00003539 if( pPager->nPage>=pPager->nHash ){
3540 pager_resize_hash_table(pPager,
3541 pPager->nHash<256 ? 256 : pPager->nHash*2);
3542 if( pPager->nHash==0 ){
3543 rc = SQLITE_NOMEM;
3544 goto pager_allocate_out;
3545 }
3546 }
drhed138fb2007-08-22 22:04:37 +00003547 pagerLeave(pPager);
drh1e3af332007-10-20 13:17:54 +00003548 nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra
3549 + MEMDB*sizeof(PgHistory);
drh0d180202008-02-14 23:26:56 +00003550 pPg = sqlite3_malloc( nByteHdr );
3551 if( pPg ){
3552 pData = sqlite3_malloc( pPager->pageSize );
3553 if( pData==0 ){
3554 sqlite3_free(pPg);
3555 pPg = 0;
3556 }
3557 }
drhed138fb2007-08-22 22:04:37 +00003558 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003559 if( pPg==0 ){
3560 rc = SQLITE_NOMEM;
3561 goto pager_allocate_out;
3562 }
drh1e3af332007-10-20 13:17:54 +00003563 memset(pPg, 0, nByteHdr);
drh0d180202008-02-14 23:26:56 +00003564 pPg->pData = pData;
danielk197724168722007-04-02 05:07:47 +00003565 pPg->pPager = pPager;
3566 pPg->pNextAll = pPager->pAll;
3567 pPager->pAll = pPg;
3568 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003569 }else{
3570 /* Recycle an existing page with a zero ref-count. */
danielk1977843e65f2007-09-01 16:16:15 +00003571 rc = pager_recycle(pPager, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003572 if( rc==SQLITE_BUSY ){
3573 rc = SQLITE_IOERR_BLOCKED;
3574 }
danielk197724168722007-04-02 05:07:47 +00003575 if( rc!=SQLITE_OK ){
3576 goto pager_allocate_out;
3577 }
3578 assert( pPager->state>=SHARED_LOCK );
3579 assert(pPg);
3580 }
3581 *ppPg = pPg;
3582
3583pager_allocate_out:
3584 return rc;
3585}
3586
3587/*
drhd33d5a82007-04-26 12:11:28 +00003588** Make sure we have the content for a page. If the page was
3589** previously acquired with noContent==1, then the content was
3590** just initialized to zeros instead of being read from disk.
3591** But now we need the real data off of disk. So make sure we
3592** have it. Read it in if we do not have it already.
3593*/
3594static int pager_get_content(PgHdr *pPg){
3595 if( pPg->needRead ){
3596 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3597 if( rc==SQLITE_OK ){
3598 pPg->needRead = 0;
3599 }else{
3600 return rc;
3601 }
3602 }
3603 return SQLITE_OK;
3604}
3605
3606/*
drhd9b02572001-04-15 00:37:09 +00003607** Acquire a page.
3608**
drh58a11682001-11-10 13:51:08 +00003609** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003610** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003611**
drhd33d5a82007-04-26 12:11:28 +00003612** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003613** file is smaller than the requested page, then no actual disk
3614** read occurs and the memory image of the page is initialized to
3615** all zeros. The extra data appended to a page is always initialized
3616** to zeros the first time a page is loaded into memory.
3617**
drhd9b02572001-04-15 00:37:09 +00003618** The acquisition might fail for several reasons. In all cases,
3619** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003620**
drhd33d5a82007-04-26 12:11:28 +00003621** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003622** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003623** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003624** just returns 0. This routine acquires a read-lock the first time it
3625** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003626** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003627** or journal files.
drh0787db62007-03-04 13:15:27 +00003628**
drh538f5702007-04-13 02:14:30 +00003629** If noContent is false, the page contents are actually read from disk.
3630** If noContent is true, it means that we do not care about the contents
3631** of the page at this time, so do not do a disk read. Just fill in the
3632** page content with zeros. But mark the fact that we have not read the
3633** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003634** sqlite3PagerWrite() is called on this page or if this routine is
3635** called again with noContent==0, that means that the content is needed
3636** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003637*/
drh86f8c192007-08-22 00:39:19 +00003638static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003639 Pager *pPager, /* The pager open on the database file */
3640 Pgno pgno, /* Page number to fetch */
3641 DbPage **ppPage, /* Write a pointer to the page here */
3642 int noContent /* Do not bother reading content from disk if true */
3643){
drhed7c8552001-04-11 14:29:21 +00003644 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003645 int rc;
drhed7c8552001-04-11 14:29:21 +00003646
danielk1977e277be02007-03-23 18:12:06 +00003647 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3648
danielk197726836652005-01-17 01:33:13 +00003649 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3650 ** number greater than this, or zero, is requested.
3651 */
drh267cb322005-09-16 17:16:52 +00003652 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003653 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003654 }
3655
drhd9b02572001-04-15 00:37:09 +00003656 /* Make sure we have not hit any critical errors.
3657 */
drh836faa42003-01-11 13:30:57 +00003658 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003659 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003660
danielk197713adf8a2004-06-03 16:08:41 +00003661 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003662 ** on the database file. pagerSharedLock() is a no-op if
3663 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003664 */
danielk1977e277be02007-03-23 18:12:06 +00003665 rc = pagerSharedLock(pPager);
3666 if( rc!=SQLITE_OK ){
3667 return rc;
drhed7c8552001-04-11 14:29:21 +00003668 }
danielk1977e277be02007-03-23 18:12:06 +00003669 assert( pPager->state!=PAGER_UNLOCK );
3670
3671 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003672 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003673 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003674 int nMax;
drhed7c8552001-04-11 14:29:21 +00003675 int h;
drh538f5702007-04-13 02:14:30 +00003676 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003677 rc = pagerAllocatePage(pPager, &pPg);
3678 if( rc!=SQLITE_OK ){
3679 return rc;
drhed7c8552001-04-11 14:29:21 +00003680 }
danielk197724168722007-04-02 05:07:47 +00003681
drhed7c8552001-04-11 14:29:21 +00003682 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003683 assert( !MEMDB || pgno>pPager->stmtSize );
drhf5e7bb52008-02-18 14:47:33 +00003684 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
3685 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003686
drh605ad8f2006-05-03 23:34:05 +00003687 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003688 pPg->nRef = 1;
danielk197775bab7d2006-01-23 13:09:45 +00003689
drhd9b02572001-04-15 00:37:09 +00003690 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003691 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003692 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003693 }
danielk1977979f38e2007-03-27 16:19:51 +00003694 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003695 if( pPager->errCode ){
danielk1977efaaf572006-01-16 11:29:19 +00003696 rc = pPager->errCode;
danielk1977ae72d982007-10-03 08:46:44 +00003697 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003698 return rc;
3699 }
danielk197775bab7d2006-01-23 13:09:45 +00003700
3701 /* Populate the page with data, either by reading from the database
3702 ** file, or by setting the entire page to zero.
3703 */
drhd215acf2007-04-13 04:01:58 +00003704 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003705 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003706 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003707 return SQLITE_FULL;
3708 }
drh90f5ecb2004-07-22 01:19:35 +00003709 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003710 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003711 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003712 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003713 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003714 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3715 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003716 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003717 return rc;
drh81a20f22001-10-12 17:30:04 +00003718 }
danielk1977b4626a32007-04-28 15:47:43 +00003719 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003720 }
danielk197775bab7d2006-01-23 13:09:45 +00003721
3722 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003723 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003724 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003725 pPg->pNextHash = pPager->aHash[h];
3726 pPager->aHash[h] = pPg;
3727 if( pPg->pNextHash ){
3728 assert( pPg->pNextHash->pPrevHash==0 );
3729 pPg->pNextHash->pPrevHash = pPg;
3730 }
3731
danielk19773c407372005-02-15 02:54:14 +00003732#ifdef SQLITE_CHECK_PAGES
3733 pPg->pageHash = pager_pagehash(pPg);
3734#endif
drhed7c8552001-04-11 14:29:21 +00003735 }else{
drhd9b02572001-04-15 00:37:09 +00003736 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003737 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003738 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003739 if( !noContent ){
3740 rc = pager_get_content(pPg);
3741 if( rc ){
3742 return rc;
3743 }
3744 }
drhdf0b3b02001-06-23 11:36:20 +00003745 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003746 }
danielk19773b8a05f2007-03-19 17:44:26 +00003747 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003748 return SQLITE_OK;
3749}
drh86f8c192007-08-22 00:39:19 +00003750int sqlite3PagerAcquire(
3751 Pager *pPager, /* The pager open on the database file */
3752 Pgno pgno, /* Page number to fetch */
3753 DbPage **ppPage, /* Write a pointer to the page here */
3754 int noContent /* Do not bother reading content from disk if true */
3755){
3756 int rc;
3757 pagerEnter(pPager);
3758 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3759 pagerLeave(pPager);
3760 return rc;
3761}
3762
drhed7c8552001-04-11 14:29:21 +00003763
3764/*
drh7e3b0a02001-04-28 16:52:40 +00003765** Acquire a page if it is already in the in-memory cache. Do
3766** not read the page from disk. Return a pointer to the page,
3767** or 0 if the page is not in cache.
3768**
danielk19773b8a05f2007-03-19 17:44:26 +00003769** See also sqlite3PagerGet(). The difference between this routine
3770** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003771** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003772** returns NULL if the page is not in cache or if a disk I/O error
3773** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003774*/
danielk19773b8a05f2007-03-19 17:44:26 +00003775DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003776 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003777
drh836faa42003-01-11 13:30:57 +00003778 assert( pPager!=0 );
3779 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003780
drh86f8c192007-08-22 00:39:19 +00003781 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003782 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003783 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003784 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3785 /* Do nothing */
3786 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3787 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003788 }
drh86f8c192007-08-22 00:39:19 +00003789 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003790 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003791}
3792
3793/*
drhed7c8552001-04-11 14:29:21 +00003794** Release a page.
3795**
3796** If the number of references to the page drop to zero, then the
3797** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003798** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003799** removed.
3800*/
danielk19773b8a05f2007-03-19 17:44:26 +00003801int sqlite3PagerUnref(DbPage *pPg){
danielk1977ae72d982007-10-03 08:46:44 +00003802 Pager *pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003803
3804 /* Decrement the reference count for this page
3805 */
drhed7c8552001-04-11 14:29:21 +00003806 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003807 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003808 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +00003809
danielk19773c407372005-02-15 02:54:14 +00003810 CHECK_PAGE(pPg);
3811
drh72f82862001-05-24 21:06:34 +00003812 /* When the number of references to a page reach 0, call the
3813 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003814 */
drhed7c8552001-04-11 14:29:21 +00003815 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003816
3817 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003818 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003819 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003820 }
drhd9b02572001-04-15 00:37:09 +00003821
3822 /* When all pages reach the freelist, drop the read lock from
3823 ** the database file.
3824 */
3825 pPager->nRef--;
3826 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003827 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003828 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003829 }
drhed7c8552001-04-11 14:29:21 +00003830 }
danielk1977ae72d982007-10-03 08:46:44 +00003831 pagerLeave(pPager);
drhd9b02572001-04-15 00:37:09 +00003832 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003833}
3834
3835/*
drha6abd042004-06-09 17:37:22 +00003836** Create a journal file for pPager. There should already be a RESERVED
3837** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003838**
3839** Return SQLITE_OK if everything. Return an error code and release the
3840** write lock if anything goes wrong.
3841*/
3842static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003843 sqlite3_vfs *pVfs = pPager->pVfs;
3844 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3845
drhda47d772002-12-02 04:25:19 +00003846 int rc;
drhb7f91642004-10-31 02:22:47 +00003847 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003848 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003849 assert( pPager->journalOpen==0 );
3850 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003851 assert( pPager->pInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003852 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003853 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003854 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00003855 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003856 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003857 rc = SQLITE_NOMEM;
3858 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003859 }
danielk1977b4b47412007-08-17 15:53:36 +00003860
3861 if( pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00003862 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3863 }else{
3864 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
danielk1977b4b47412007-08-17 15:53:36 +00003865 }
danielk1977c7b60172007-08-22 11:22:03 +00003866#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3867 rc = sqlite3JournalOpen(
3868 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3869 );
3870#else
danielk1977b4b47412007-08-17 15:53:36 +00003871 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
danielk1977c7b60172007-08-22 11:22:03 +00003872#endif
danielk1977b4b47412007-08-17 15:53:36 +00003873 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk197776572402004-06-25 02:38:54 +00003874 pPager->journalOff = 0;
3875 pPager->setMaster = 0;
3876 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003877 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003878 if( rc==SQLITE_NOMEM ){
danielk1977fee2d252007-08-18 10:59:19 +00003879 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh600e46a2007-03-28 01:59:33 +00003880 }
drh9c105bb2004-10-02 20:38:28 +00003881 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003882 }
3883 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003884 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003885 pPager->needSync = 0;
3886 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003887 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003888 if( pPager->errCode ){
3889 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003890 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003891 }
drhda47d772002-12-02 04:25:19 +00003892 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003893
danielk197776572402004-06-25 02:38:54 +00003894 rc = writeJournalHdr(pPager);
3895
drhac69b052004-05-12 13:30:07 +00003896 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003897 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003898 }
danielk1977ae72d982007-10-03 08:46:44 +00003899 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003900 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003901 if( rc==SQLITE_OK ){
3902 rc = SQLITE_FULL;
3903 }
3904 }
drh9c105bb2004-10-02 20:38:28 +00003905 return rc;
3906
3907failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003908 sqlite3BitvecDestroy(pPager->pInJournal);
3909 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003910 return rc;
drhda47d772002-12-02 04:25:19 +00003911}
3912
3913/*
drh4b845d72002-03-05 12:41:19 +00003914** Acquire a write-lock on the database. The lock is removed when
3915** the any of the following happen:
3916**
drh80e35f42007-03-30 14:06:34 +00003917** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003918** * sqlite3PagerRollback() is called.
3919** * sqlite3PagerClose() is called.
3920** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003921**
danielk197713adf8a2004-06-03 16:08:41 +00003922** The first parameter to this routine is a pointer to any open page of the
3923** database file. Nothing changes about the page - it is used merely to
3924** acquire a pointer to the Pager structure and as proof that there is
3925** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003926**
danielk197713adf8a2004-06-03 16:08:41 +00003927** The second parameter indicates how much space in bytes to reserve for a
3928** master journal file-name at the start of the journal when it is created.
3929**
3930** A journal file is opened if this is not a temporary file. For temporary
3931** files, the opening of the journal file is deferred until there is an
3932** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003933**
drha6abd042004-06-09 17:37:22 +00003934** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003935**
3936** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3937** immediately instead of waiting until we try to flush the cache. The
3938** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003939*/
danielk19773b8a05f2007-03-19 17:44:26 +00003940int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003941 Pager *pPager = pPg->pPager;
3942 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003943 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003944 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003945 assert( pPager->state!=PAGER_UNLOCK );
3946 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003947 assert( pPager->pInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003948 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003949 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003950 pPager->origDbSize = pPager->dbSize;
3951 }else{
drh054889e2005-11-30 03:20:31 +00003952 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003953 if( rc==SQLITE_OK ){
3954 pPager->state = PAGER_RESERVED;
3955 if( exFlag ){
3956 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3957 }
3958 }
danielk19779eed5052004-06-04 10:38:30 +00003959 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003960 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003961 return rc;
drhac69b052004-05-12 13:30:07 +00003962 }
drha6abd042004-06-09 17:37:22 +00003963 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003964 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003965 if( pPager->useJournal && !pPager->tempFile ){
3966 rc = pager_open_journal(pPager);
3967 }
drh4b845d72002-03-05 12:41:19 +00003968 }
danielk1977334cdb62007-03-26 08:05:12 +00003969 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3970 /* This happens when the pager was in exclusive-access mode last
3971 ** time a (read or write) transaction was successfully concluded
3972 ** by this connection. Instead of deleting the journal file it was
3973 ** kept open and truncated to 0 bytes.
3974 */
3975 assert( pPager->nRec==0 );
3976 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003977 assert( pPager->pInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003978 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003979 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003980 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhed138fb2007-08-22 22:04:37 +00003981 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003982 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003983 rc = SQLITE_NOMEM;
3984 }else{
drh369339d2007-03-30 16:01:55 +00003985 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003986 rc = writeJournalHdr(pPager);
3987 }
drh4b845d72002-03-05 12:41:19 +00003988 }
danielk1977334cdb62007-03-26 08:05:12 +00003989 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00003990 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00003991 return rc;
3992}
3993
3994/*
drh605ad8f2006-05-03 23:34:05 +00003995** Make a page dirty. Set its dirty flag and add it to the dirty
3996** page list.
3997*/
3998static void makeDirty(PgHdr *pPg){
3999 if( pPg->dirty==0 ){
4000 Pager *pPager = pPg->pPager;
4001 pPg->dirty = 1;
4002 pPg->pDirty = pPager->pDirty;
4003 if( pPager->pDirty ){
4004 pPager->pDirty->pPrevDirty = pPg;
4005 }
4006 pPg->pPrevDirty = 0;
4007 pPager->pDirty = pPg;
4008 }
4009}
4010
4011/*
4012** Make a page clean. Clear its dirty bit and remove it from the
4013** dirty page list.
4014*/
4015static void makeClean(PgHdr *pPg){
4016 if( pPg->dirty ){
4017 pPg->dirty = 0;
4018 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00004019 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004020 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
4021 }
4022 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00004023 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004024 pPg->pPrevDirty->pDirty = pPg->pDirty;
4025 }else{
drh153c62c2007-08-24 03:51:33 +00004026 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004027 pPg->pPager->pDirty = pPg->pDirty;
4028 }
4029 }
4030}
4031
4032
4033/*
drhed7c8552001-04-11 14:29:21 +00004034** Mark a data page as writeable. The page is written into the journal
4035** if it is not there already. This routine must be called before making
4036** changes to a page.
4037**
4038** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00004039** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00004040** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00004041** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00004042** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00004043**
4044** If the journal file could not be written because the disk is full,
4045** then this routine returns SQLITE_FULL and does an immediate rollback.
4046** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00004047** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00004048** reset.
drhed7c8552001-04-11 14:29:21 +00004049*/
danielk19773b8a05f2007-03-19 17:44:26 +00004050static int pager_write(PgHdr *pPg){
4051 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00004052 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00004053 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00004054
drh6446c4d2001-12-15 14:22:18 +00004055 /* Check for errors
4056 */
danielk1977efaaf572006-01-16 11:29:19 +00004057 if( pPager->errCode ){
4058 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004059 }
drh5e00f6c2001-09-13 13:46:56 +00004060 if( pPager->readOnly ){
4061 return SQLITE_PERM;
4062 }
drh6446c4d2001-12-15 14:22:18 +00004063
danielk197776572402004-06-25 02:38:54 +00004064 assert( !pPager->setMaster );
4065
danielk19773c407372005-02-15 02:54:14 +00004066 CHECK_PAGE(pPg);
4067
drh538f5702007-04-13 02:14:30 +00004068 /* If this page was previously acquired with noContent==1, that means
4069 ** we didn't really read in the content of the page. This can happen
4070 ** (for example) when the page is being moved to the freelist. But
4071 ** now we are (perhaps) moving the page off of the freelist for
4072 ** reuse and we need to know its original content so that content
4073 ** can be stored in the rollback journal. So do the read at this
4074 ** time.
4075 */
drhd33d5a82007-04-26 12:11:28 +00004076 rc = pager_get_content(pPg);
4077 if( rc ){
4078 return rc;
drh538f5702007-04-13 02:14:30 +00004079 }
4080
drh6446c4d2001-12-15 14:22:18 +00004081 /* Mark the page as dirty. If the page has already been written
4082 ** to the journal then we can return right away.
4083 */
drh605ad8f2006-05-03 23:34:05 +00004084 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00004085 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00004086 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004087 }else{
drh6446c4d2001-12-15 14:22:18 +00004088
danielk1977a0bf2652004-11-04 14:30:04 +00004089 /* If we get this far, it means that the page needs to be
4090 ** written to the transaction journal or the ckeckpoint journal
4091 ** or both.
4092 **
4093 ** First check to see that the transaction journal exists and
4094 ** create it if it does not.
4095 */
4096 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00004097 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00004098 if( rc!=SQLITE_OK ){
4099 return rc;
4100 }
4101 assert( pPager->state>=PAGER_RESERVED );
4102 if( !pPager->journalOpen && pPager->useJournal ){
4103 rc = pager_open_journal(pPager);
4104 if( rc!=SQLITE_OK ) return rc;
4105 }
4106 assert( pPager->journalOpen || !pPager->useJournal );
4107 pPager->dirtyCache = 1;
4108
4109 /* The transaction journal now exists and we have a RESERVED or an
4110 ** EXCLUSIVE lock on the main database file. Write the current page to
4111 ** the transaction journal if it is not there already.
4112 */
4113 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
4114 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00004115 if( MEMDB ){
4116 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00004117 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004118 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00004119 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977662278e2007-11-05 15:30:12 +00004120 if( !pHist->pOrig ){
4121 return SQLITE_NOMEM;
danielk1977a0bf2652004-11-04 14:30:04 +00004122 }
danielk1977662278e2007-11-05 15:30:12 +00004123 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
danielk1977a0bf2652004-11-04 14:30:04 +00004124 }else{
drhbf4bca52007-09-06 22:19:14 +00004125 u32 cksum;
4126 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00004127
drh267cb322005-09-16 17:16:52 +00004128 /* We should never write to the journal file the page that
4129 ** contains the database locks. The following assert verifies
4130 ** that we do not. */
4131 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004132 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004133 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00004134 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4135 if( rc==SQLITE_OK ){
4136 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4137 pPager->journalOff + 4);
4138 pPager->journalOff += pPager->pageSize+4;
4139 }
4140 if( rc==SQLITE_OK ){
4141 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4142 pPager->journalOff += 4;
4143 }
4144 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00004145 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00004146 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00004147 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4148 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00004149
drhfd131da2007-08-07 17:13:03 +00004150 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004151 ** transaction will be rolled back by the layer above.
4152 */
danielk1977a0bf2652004-11-04 14:30:04 +00004153 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004154 return rc;
4155 }
danielk197707cb5602006-01-20 10:55:05 +00004156
danielk1977a0bf2652004-11-04 14:30:04 +00004157 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00004158 assert( pPager->pInJournal!=0 );
4159 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004160 pPg->needSync = !pPager->noSync;
4161 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00004162 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004163 }
drhac69b052004-05-12 13:30:07 +00004164 }
danielk197713adf8a2004-06-03 16:08:41 +00004165 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004166 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004167 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004168 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004169 }
4170 if( pPg->needSync ){
4171 pPager->needSync = 1;
4172 }
4173 pPg->inJournal = 1;
4174 }
4175
4176 /* If the statement journal is open and the page is not in it,
4177 ** then write the current page to the statement journal. Note that
4178 ** the statement journal format differs from the standard journal format
4179 ** in that it omits the checksums and the header.
4180 */
danielk1977f35843b2007-04-07 15:03:17 +00004181 if( pPager->stmtInUse
4182 && !pageInStatement(pPg)
4183 && (int)pPg->pgno<=pPager->stmtSize
4184 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004185 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4186 if( MEMDB ){
4187 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4188 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00004189 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004190 if( pHist->pStmt ){
4191 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4192 }
drh4f0c5872007-03-26 22:05:01 +00004193 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004194 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004195 }else{
danielk197762079062007-08-15 17:08:46 +00004196 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00004197 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4198 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4199 if( rc==SQLITE_OK ){
4200 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4201 }
drh4f0c5872007-03-26 22:05:01 +00004202 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004203 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004204 return rc;
4205 }
danielk1977a0bf2652004-11-04 14:30:04 +00004206 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00004207 assert( pPager->pInStmt!=0 );
4208 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00004209 }
drhd9b02572001-04-15 00:37:09 +00004210 }
drhfa86c412002-02-02 15:01:15 +00004211 }
4212
4213 /* Update the database size and return.
4214 */
drh1aa2d8b2007-01-03 15:34:29 +00004215 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004216 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004217 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004218 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004219 pPager->dbSize++;
4220 }
drh306dc212001-05-21 13:45:10 +00004221 }
drh69688d52001-04-14 16:38:23 +00004222 return rc;
drhed7c8552001-04-11 14:29:21 +00004223}
4224
4225/*
danielk19774099f6e2007-03-19 11:25:20 +00004226** This function is used to mark a data-page as writable. It uses
4227** pager_write() to open a journal file (if it is not already open)
4228** and write the page *pData to the journal.
4229**
4230** The difference between this function and pager_write() is that this
4231** function also deals with the special case where 2 or more pages
4232** fit on a single disk sector. In this case all co-resident pages
4233** must have been written to the journal file before returning.
4234*/
danielk19773b8a05f2007-03-19 17:44:26 +00004235int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004236 int rc = SQLITE_OK;
4237
danielk19773b8a05f2007-03-19 17:44:26 +00004238 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004239 Pager *pPager = pPg->pPager;
4240 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4241
drh86f8c192007-08-22 00:39:19 +00004242 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004243 if( !MEMDB && nPagePerSector>1 ){
4244 Pgno nPageCount; /* Total number of pages in database file */
4245 Pgno pg1; /* First page of the sector pPg is located on. */
4246 int nPage; /* Number of pages starting at pg1 to journal */
4247 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004248 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004249
4250 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4251 ** header to be written between the pages journaled by this function.
4252 */
4253 assert( pPager->doNotSync==0 );
4254 pPager->doNotSync = 1;
4255
4256 /* This trick assumes that both the page-size and sector-size are
4257 ** an integer power of 2. It sets variable pg1 to the identifier
4258 ** of the first page of the sector pPg is located on.
4259 */
4260 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4261
danielk19773b8a05f2007-03-19 17:44:26 +00004262 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004263 if( pPg->pgno>nPageCount ){
4264 nPage = (pPg->pgno - pg1)+1;
4265 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4266 nPage = nPageCount+1-pg1;
4267 }else{
4268 nPage = nPagePerSector;
4269 }
4270 assert(nPage>0);
4271 assert(pg1<=pPg->pgno);
4272 assert((pg1+nPage)>pPg->pgno);
4273
4274 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4275 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004276 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00004277 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00004278 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004279 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004280 if( rc==SQLITE_OK ){
4281 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004282 if( pPage->needSync ){
4283 needSync = 1;
4284 }
danielk19773b8a05f2007-03-19 17:44:26 +00004285 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004286 }
4287 }
drhc81945e2008-03-10 14:12:53 +00004288 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk1977dd97a492007-08-22 18:54:32 +00004289 if( pPage->needSync ){
4290 needSync = 1;
4291 }
danielk19774099f6e2007-03-19 11:25:20 +00004292 }
4293 }
4294
danielk1977dd97a492007-08-22 18:54:32 +00004295 /* If the PgHdr.needSync flag is set for any of the nPage pages
4296 ** starting at pg1, then it needs to be set for all of them. Because
4297 ** writing to any of these nPage pages may damage the others, the
4298 ** journal file must contain sync()ed copies of all of them
4299 ** before any of them can be written out to the database file.
4300 */
4301 if( needSync ){
4302 for(ii=0; ii<nPage && needSync; ii++){
4303 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4304 if( pPage ) pPage->needSync = 1;
4305 }
4306 assert(pPager->needSync);
4307 }
4308
danielk19774099f6e2007-03-19 11:25:20 +00004309 assert( pPager->doNotSync==1 );
4310 pPager->doNotSync = 0;
4311 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004312 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004313 }
drh86f8c192007-08-22 00:39:19 +00004314 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004315 return rc;
4316}
4317
4318/*
drhaacc5432002-01-06 17:07:40 +00004319** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004320** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004321** to change the content of the page.
4322*/
danielk19777d3a6662006-01-23 16:21:05 +00004323#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004324int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004325 return pPg->dirty;
4326}
danielk19777d3a6662006-01-23 16:21:05 +00004327#endif
drh6019e162001-07-02 17:51:45 +00004328
danielk1977b84f96f2005-01-20 11:32:23 +00004329#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00004330/*
drh001bbcb2003-03-19 03:14:00 +00004331** Replace the content of a single page with the information in the third
4332** argument.
4333*/
danielk19773b8a05f2007-03-19 17:44:26 +00004334int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
4335 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00004336 int rc;
4337
drh86f8c192007-08-22 00:39:19 +00004338 pagerEnter(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00004339 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00004340 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004341 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00004342 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004343 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00004344 }
danielk19773b8a05f2007-03-19 17:44:26 +00004345 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00004346 }
drh86f8c192007-08-22 00:39:19 +00004347 pagerLeave(pPager);
drh001bbcb2003-03-19 03:14:00 +00004348 return rc;
4349}
danielk1977b84f96f2005-01-20 11:32:23 +00004350#endif
drh001bbcb2003-03-19 03:14:00 +00004351
4352/*
drh30e58752002-03-02 20:41:57 +00004353** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004354** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004355** that page might be marked as dirty.
4356**
4357** The overlying software layer calls this routine when all of the data
4358** on the given page is unused. The pager marks the page as clean so
4359** that it does not get written to disk.
4360**
4361** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004362** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004363** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004364**
4365** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004366** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004367** will thereafter be ignored. This is necessary to avoid a problem
4368** where a page with data is added to the freelist during one part of
4369** a transaction then removed from the freelist during a later part
4370** of the same transaction and reused for some other purpose. When it
4371** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004372** the sqlite3PagerDontRollback() routine is called. But because the
4373** page contains critical data, we still need to be sure it gets
4374** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004375*/
drh538f5702007-04-13 02:14:30 +00004376void sqlite3PagerDontWrite(DbPage *pDbPage){
4377 PgHdr *pPg = pDbPage;
4378 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004379
drhb7f91642004-10-31 02:22:47 +00004380 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004381 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004382 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004383 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004384 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004385 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4386 /* If this pages is the last page in the file and the file has grown
4387 ** during the current transaction, then do NOT mark the page as clean.
4388 ** When the database file grows, we must make sure that the last page
4389 ** gets written at least once so that the disk file will be the correct
4390 ** size. If you do not write this page and the size of the file
4391 ** on the disk ends up being too small, that can lead to database
4392 ** corruption during the next transaction.
4393 */
4394 }else{
drh538f5702007-04-13 02:14:30 +00004395 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4396 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004397 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004398#ifdef SQLITE_CHECK_PAGES
4399 pPg->pageHash = pager_pagehash(pPg);
4400#endif
drh8124a302002-06-25 14:43:57 +00004401 }
drh30e58752002-03-02 20:41:57 +00004402 }
drh86f8c192007-08-22 00:39:19 +00004403 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004404}
4405
4406/*
4407** A call to this routine tells the pager that if a rollback occurs,
4408** it is not necessary to restore the data on the given page. This
4409** means that the pager does not have to record the given page in the
4410** rollback journal.
drh538f5702007-04-13 02:14:30 +00004411**
4412** If we have not yet actually read the content of this page (if
4413** the PgHdr.needRead flag is set) then this routine acts as a promise
4414** that we will never need to read the page content in the future.
4415** so the needRead flag can be cleared at this point.
danielk1977a55e9352008-01-21 13:04:34 +00004416**
4417** This routine is only called from a single place in the sqlite btree
4418** code (when a leaf is removed from the free-list). This allows the
4419** following assumptions to be made about pPg:
4420**
4421** 1. PagerDontWrite() has been called on the page, OR
4422** PagerWrite() has not yet been called on the page.
4423**
4424** 2. The page existed when the transaction was started.
4425**
4426** Details: DontRollback() (this routine) is only called when a leaf is
4427** removed from the free list. DontWrite() is called whenever a page
4428** becomes a free-list leaf.
drh30e58752002-03-02 20:41:57 +00004429*/
danielk19773b8a05f2007-03-19 17:44:26 +00004430void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004431 Pager *pPager = pPg->pPager;
4432
drh86f8c192007-08-22 00:39:19 +00004433 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004434 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00004435
4436 /* If the journal file is not open, or DontWrite() has been called on
4437 ** this page (DontWrite() sets the alwaysRollback flag), then this
4438 ** function is a no-op.
4439 */
4440 if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){
danielk197787c29a92008-01-18 11:33:16 +00004441 pagerLeave(pPager);
4442 return;
4443 }
danielk1977a55e9352008-01-21 13:04:34 +00004444 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
4445
4446 /* Check that PagerWrite() has not yet been called on this page, and
4447 ** that the page existed when the transaction started.
4448 */
4449 assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize );
4450
drhf5e7bb52008-02-18 14:47:33 +00004451 assert( pPager->pInJournal!=0 );
4452 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a55e9352008-01-21 13:04:34 +00004453 pPg->inJournal = 1;
4454 pPg->needRead = 0;
4455 if( pPager->stmtInUse ){
4456 assert( pPager->stmtSize <= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00004457 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00004458 }
danielk1977a55e9352008-01-21 13:04:34 +00004459 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
4460 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh86f8c192007-08-22 00:39:19 +00004461 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004462}
4463
drhac69b052004-05-12 13:30:07 +00004464
drh30e58752002-03-02 20:41:57 +00004465/*
drh80e35f42007-03-30 14:06:34 +00004466** This routine is called to increment the database file change-counter,
4467** stored at byte 24 of the pager file.
4468*/
danielk1977c7b60172007-08-22 11:22:03 +00004469static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004470 PgHdr *pPgHdr;
4471 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004472 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004473
4474 if( !pPager->changeCountDone ){
4475 /* Open page 1 of the file for writing. */
4476 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4477 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004478
4479 if( !isDirect ){
4480 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00004481 if( rc!=SQLITE_OK ){
4482 sqlite3PagerUnref(pPgHdr);
4483 return rc;
4484 }
danielk1977c7b60172007-08-22 11:22:03 +00004485 }
4486
drh80e35f42007-03-30 14:06:34 +00004487 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004488 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004489 change_counter++;
4490 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004491
4492 if( isDirect && pPager->fd->pMethods ){
4493 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4494 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4495 }
4496
drh80e35f42007-03-30 14:06:34 +00004497 /* Release the page reference. */
4498 sqlite3PagerUnref(pPgHdr);
4499 pPager->changeCountDone = 1;
4500 }
danielk1977c7b60172007-08-22 11:22:03 +00004501 return rc;
drh80e35f42007-03-30 14:06:34 +00004502}
4503
4504/*
danielk1977f653d782008-03-20 11:04:21 +00004505** Sync the pager file to disk.
4506*/
4507int sqlite3PagerSync(Pager *pPager){
4508 int rc;
4509 pagerEnter(pPager);
4510 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4511 pagerLeave(pPager);
4512 return rc;
4513}
4514
4515/*
drh80e35f42007-03-30 14:06:34 +00004516** Sync the database file for the pager pPager. zMaster points to the name
4517** of a master journal file that should be written into the individual
4518** journal file. zMaster may be NULL, which is interpreted as no master
4519** journal (a single database transaction).
4520**
4521** This routine ensures that the journal is synced, all dirty pages written
4522** to the database file and the database file synced. The only thing that
4523** remains to commit the transaction is to delete the journal file (or
4524** master journal file if specified).
4525**
4526** Note that if zMaster==NULL, this does not overwrite a previous value
4527** passed to an sqlite3PagerCommitPhaseOne() call.
4528**
4529** If parameter nTrunc is non-zero, then the pager file is truncated to
4530** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00004531**
4532** If the final parameter - noSync - is true, then the database file itself
4533** is not synced. The caller must call sqlite3PagerSync() directly to
4534** sync the database file before calling CommitPhaseTwo() to delete the
4535** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00004536*/
danielk1977f653d782008-03-20 11:04:21 +00004537int sqlite3PagerCommitPhaseOne(
4538 Pager *pPager,
4539 const char *zMaster,
4540 Pgno nTrunc,
4541 int noSync
4542){
drh80e35f42007-03-30 14:06:34 +00004543 int rc = SQLITE_OK;
4544
4545 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4546 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004547 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004548
4549 /* If this is an in-memory db, or no pages have been written to, or this
4550 ** function has already been called, it is a no-op.
4551 */
4552 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4553 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004554
danielk1977c7b60172007-08-22 11:22:03 +00004555#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4556 /* The atomic-write optimization can be used if all of the
4557 ** following are true:
4558 **
4559 ** + The file-system supports the atomic-write property for
4560 ** blocks of size page-size, and
4561 ** + This commit is not part of a multi-file transaction, and
4562 ** + Exactly one page has been modified and store in the journal file.
4563 **
4564 ** If the optimization can be used, then the journal file will never
4565 ** be created for this transaction.
4566 */
danielk1977f55b8992007-08-24 08:15:53 +00004567 int useAtomicWrite = (
4568 !zMaster &&
4569 pPager->journalOff==jrnlBufferSize(pPager) &&
4570 nTrunc==0 &&
4571 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4572 );
4573 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004574 /* Update the nRec field in the journal file. */
4575 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4576 assert(pPager->nRec==1);
4577 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4578
4579 /* Update the db file change counter. The following call will modify
4580 ** the in-memory representation of page 1 to include the updated
4581 ** change counter and then write page 1 directly to the database
4582 ** file. Because of the atomic-write property of the host file-system,
4583 ** this is safe.
4584 */
danielk1977ae72d982007-10-03 08:46:44 +00004585 if( rc==SQLITE_OK ){
4586 rc = pager_incr_changecounter(pPager, 1);
4587 }
danielk1977f55b8992007-08-24 08:15:53 +00004588 }else{
4589 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00004590 }
4591
danielk1977ae72d982007-10-03 08:46:44 +00004592 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00004593#endif
4594
drh80e35f42007-03-30 14:06:34 +00004595 /* If a master journal file name has already been written to the
4596 ** journal file, then no sync is required. This happens when it is
4597 ** written, then the process fails to upgrade from a RESERVED to an
4598 ** EXCLUSIVE lock. The next time the process tries to commit the
4599 ** transaction the m-j name will have already been written.
4600 */
4601 if( !pPager->setMaster ){
danielk1977f55b8992007-08-24 08:15:53 +00004602 assert( pPager->journalOpen );
danielk1977c7b60172007-08-22 11:22:03 +00004603 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004604 if( rc!=SQLITE_OK ) goto sync_exit;
4605#ifndef SQLITE_OMIT_AUTOVACUUM
4606 if( nTrunc!=0 ){
4607 /* If this transaction has made the database smaller, then all pages
4608 ** being discarded by the truncation must be written to the journal
4609 ** file.
4610 */
4611 Pgno i;
4612 int iSkip = PAGER_MJ_PGNO(pPager);
4613 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
drhf5e7bb52008-02-18 14:47:33 +00004614 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
drh80e35f42007-03-30 14:06:34 +00004615 rc = sqlite3PagerGet(pPager, i, &pPg);
4616 if( rc!=SQLITE_OK ) goto sync_exit;
4617 rc = sqlite3PagerWrite(pPg);
4618 sqlite3PagerUnref(pPg);
4619 if( rc!=SQLITE_OK ) goto sync_exit;
4620 }
4621 }
4622 }
4623#endif
4624 rc = writeMasterJournal(pPager, zMaster);
4625 if( rc!=SQLITE_OK ) goto sync_exit;
4626 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004627 }
danielk1977c7b60172007-08-22 11:22:03 +00004628 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004629
4630#ifndef SQLITE_OMIT_AUTOVACUUM
4631 if( nTrunc!=0 ){
4632 rc = sqlite3PagerTruncate(pPager, nTrunc);
4633 if( rc!=SQLITE_OK ) goto sync_exit;
4634 }
4635#endif
4636
4637 /* Write all dirty pages to the database file */
4638 pPg = pager_get_all_dirty_pages(pPager);
4639 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004640 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004641 assert( rc!=SQLITE_IOERR_BLOCKED );
4642 /* The error might have left the dirty list all fouled up here,
4643 ** but that does not matter because if the if the dirty list did
4644 ** get corrupted, then the transaction will roll back and
4645 ** discard the dirty list. There is an assert in
4646 ** pager_get_all_dirty_pages() that verifies that no attempt
4647 ** is made to use an invalid dirty list.
4648 */
drh153c62c2007-08-24 03:51:33 +00004649 goto sync_exit;
4650 }
drh3cdd7d32007-03-30 14:46:01 +00004651 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004652
4653 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004654 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004655 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004656 }
4657 IOTRACE(("DBSYNC %p\n", pPager))
4658
4659 pPager->state = PAGER_SYNCED;
4660 }else if( MEMDB && nTrunc!=0 ){
4661 rc = sqlite3PagerTruncate(pPager, nTrunc);
4662 }
4663
4664sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004665 if( rc==SQLITE_IOERR_BLOCKED ){
4666 /* pager_incr_changecounter() may attempt to obtain an exclusive
4667 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004668 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004669 * better to return SQLITE_BUSY.
4670 */
4671 rc = SQLITE_BUSY;
4672 }
drh86f8c192007-08-22 00:39:19 +00004673 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004674 return rc;
4675}
4676
4677
4678/*
drhed7c8552001-04-11 14:29:21 +00004679** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004680**
4681** If the commit fails for any reason, a rollback attempt is made
4682** and an error code is returned. If the commit worked, SQLITE_OK
4683** is returned.
drhed7c8552001-04-11 14:29:21 +00004684*/
drh80e35f42007-03-30 14:06:34 +00004685int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004686 int rc;
drhed7c8552001-04-11 14:29:21 +00004687 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004688
danielk1977efaaf572006-01-16 11:29:19 +00004689 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004690 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004691 }
drha6abd042004-06-09 17:37:22 +00004692 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004693 return SQLITE_ERROR;
4694 }
drh86f8c192007-08-22 00:39:19 +00004695 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004696 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004697 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004698 pPg = pager_get_all_dirty_pages(pPager);
4699 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004700 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4701 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004702 pPg->dirty = 0;
4703 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004704 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004705 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004706 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004707 pPg = pPg->pDirty;
4708 }
drh605ad8f2006-05-03 23:34:05 +00004709 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004710#ifndef NDEBUG
4711 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4712 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4713 assert( !pPg->alwaysRollback );
4714 assert( !pHist->pOrig );
4715 assert( !pHist->pStmt );
4716 }
4717#endif
drhac69b052004-05-12 13:30:07 +00004718 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004719 pPager->state = PAGER_SHARED;
danielk197787c29a92008-01-18 11:33:16 +00004720 pagerLeave(pPager);
drhac69b052004-05-12 13:30:07 +00004721 return SQLITE_OK;
4722 }
drh3cdd7d32007-03-30 14:46:01 +00004723 assert( pPager->journalOpen || !pPager->dirtyCache );
4724 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4725 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004726 rc = pager_error(pPager, rc);
4727 pagerLeave(pPager);
4728 return rc;
drhed7c8552001-04-11 14:29:21 +00004729}
4730
4731/*
drha6abd042004-06-09 17:37:22 +00004732** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004733** All in-memory cache pages revert to their original data contents.
4734** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004735**
4736** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004737** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004738** process is writing trash into the journal file (SQLITE_CORRUPT) or
4739** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4740** codes are returned for all these occasions. Otherwise,
4741** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004742*/
danielk19773b8a05f2007-03-19 17:44:26 +00004743int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004744 int rc;
drh4f0c5872007-03-26 22:05:01 +00004745 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004746 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004747 PgHdr *p;
4748 for(p=pPager->pAll; p; p=p->pNextAll){
4749 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004750 assert( !p->alwaysRollback );
4751 if( !p->dirty ){
4752 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4753 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4754 continue;
4755 }
4756
drhac69b052004-05-12 13:30:07 +00004757 pHist = PGHDR_TO_HIST(p, pPager);
4758 if( pHist->pOrig ){
4759 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004760 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004761 }else{
drh4f0c5872007-03-26 22:05:01 +00004762 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004763 }
4764 clearHistory(pHist);
4765 p->dirty = 0;
4766 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004767 pHist->inStmt = 0;
4768 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004769 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004770 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004771 }
drhac69b052004-05-12 13:30:07 +00004772 }
drh605ad8f2006-05-03 23:34:05 +00004773 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004774 pPager->pStmt = 0;
4775 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004776 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004777 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004778 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004779 return SQLITE_OK;
4780 }
4781
drh86f8c192007-08-22 00:39:19 +00004782 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004783 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004784 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004785 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004786 return rc;
4787 }
drhdb48ee02003-01-16 13:42:43 +00004788
danielk1977efaaf572006-01-16 11:29:19 +00004789 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004790 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004791 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004792 }
drh86f8c192007-08-22 00:39:19 +00004793 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004794 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004795 }
drha6abd042004-06-09 17:37:22 +00004796 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004797 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004798 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004799 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004800 if( rc==SQLITE_OK ){
4801 rc = rc2;
4802 }
4803 }else{
danielk1977e277be02007-03-23 18:12:06 +00004804 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004805 }
danielk1977e180dd92007-04-05 17:15:52 +00004806 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004807 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004808
4809 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4810 ** cache. So call pager_error() on the way out to make any error
4811 ** persistent.
4812 */
drh86f8c192007-08-22 00:39:19 +00004813 rc = pager_error(pPager, rc);
4814 pagerLeave(pPager);
4815 return rc;
drh98808ba2001-10-18 12:34:46 +00004816}
drhd9b02572001-04-15 00:37:09 +00004817
4818/*
drh5e00f6c2001-09-13 13:46:56 +00004819** Return TRUE if the database file is opened read-only. Return FALSE
4820** if the database is (in theory) writable.
4821*/
danielk19773b8a05f2007-03-19 17:44:26 +00004822int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004823 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004824}
4825
4826/*
drh0f7eb612006-08-08 13:51:43 +00004827** Return the number of references to the pager.
4828*/
danielk19773b8a05f2007-03-19 17:44:26 +00004829int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004830 return pPager->nRef;
4831}
4832
4833#ifdef SQLITE_TEST
4834/*
drhd9b02572001-04-15 00:37:09 +00004835** This routine is used for testing and analysis only.
4836*/
danielk19773b8a05f2007-03-19 17:44:26 +00004837int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004838 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004839 a[0] = pPager->nRef;
4840 a[1] = pPager->nPage;
4841 a[2] = pPager->mxPage;
4842 a[3] = pPager->dbSize;
4843 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004844 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004845 a[6] = pPager->nHit;
4846 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004847 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004848 a[9] = pPager->nRead;
4849 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004850 return a;
4851}
drh0f7eb612006-08-08 13:51:43 +00004852#endif
drhdd793422001-06-28 01:54:48 +00004853
drhfa86c412002-02-02 15:01:15 +00004854/*
drhac69b052004-05-12 13:30:07 +00004855** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004856**
4857** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004858** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004859** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004860*/
drh86f8c192007-08-22 00:39:19 +00004861static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004862 int rc;
drhac69b052004-05-12 13:30:07 +00004863 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004864 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004865 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004866 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004867 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004868 pPager->stmtInUse = 1;
4869 pPager->stmtSize = pPager->dbSize;
4870 return SQLITE_OK;
4871 }
drhda47d772002-12-02 04:25:19 +00004872 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004873 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004874 return SQLITE_OK;
4875 }
drhfa86c412002-02-02 15:01:15 +00004876 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004877 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004878 assert( pPager->pInStmt==0 );
4879 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00004880 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004881 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004882 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004883 return SQLITE_NOMEM;
4884 }
drh968af522003-02-11 14:55:40 +00004885#ifndef NDEBUG
drh054889e2005-11-30 03:20:31 +00004886 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
drhac69b052004-05-12 13:30:07 +00004887 if( rc ) goto stmt_begin_failed;
danielk197776572402004-06-25 02:38:54 +00004888 assert( pPager->stmtJSize == pPager->journalOff );
drh968af522003-02-11 14:55:40 +00004889#endif
danielk197776572402004-06-25 02:38:54 +00004890 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004891 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004892 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004893 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004894 if( !pPager->stmtOpen ){
drh334b2992007-09-06 23:28:23 +00004895 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
drh33f4e022007-09-03 15:19:34 +00004896 SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00004897 if( rc ){
4898 goto stmt_begin_failed;
4899 }
drhac69b052004-05-12 13:30:07 +00004900 pPager->stmtOpen = 1;
4901 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004902 }
drhac69b052004-05-12 13:30:07 +00004903 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004904 return SQLITE_OK;
4905
drhac69b052004-05-12 13:30:07 +00004906stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00004907 if( pPager->pInStmt ){
4908 sqlite3BitvecDestroy(pPager->pInStmt);
4909 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004910 }
4911 return rc;
4912}
drh86f8c192007-08-22 00:39:19 +00004913int sqlite3PagerStmtBegin(Pager *pPager){
4914 int rc;
4915 pagerEnter(pPager);
4916 rc = pagerStmtBegin(pPager);
4917 pagerLeave(pPager);
4918 return rc;
4919}
drhfa86c412002-02-02 15:01:15 +00004920
4921/*
drhac69b052004-05-12 13:30:07 +00004922** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004923*/
danielk19773b8a05f2007-03-19 17:44:26 +00004924int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004925 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004926 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004927 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004928 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004929 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004930 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00004931 sqlite3BitvecDestroy(pPager->pInStmt);
4932 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004933 }else{
4934 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004935 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004936 pNext = pHist->pNextStmt;
4937 assert( pHist->inStmt );
4938 pHist->inStmt = 0;
4939 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004940 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004941 pHist->pStmt = 0;
4942 }
4943 }
4944 pPager->stmtNRec = 0;
4945 pPager->stmtInUse = 0;
4946 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004947 }
drhac69b052004-05-12 13:30:07 +00004948 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004949 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004950 return SQLITE_OK;
4951}
4952
4953/*
drhac69b052004-05-12 13:30:07 +00004954** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004955*/
danielk19773b8a05f2007-03-19 17:44:26 +00004956int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004957 int rc;
drh86f8c192007-08-22 00:39:19 +00004958 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004959 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004960 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004961 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004962 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004963 PgHistory *pHist;
4964 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4965 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004966 if( pHist->pStmt ){
4967 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004968 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004969 pHist->pStmt = 0;
4970 }
4971 }
4972 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004973 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004974 rc = SQLITE_OK;
4975 }else{
4976 rc = pager_stmt_playback(pPager);
4977 }
danielk19773b8a05f2007-03-19 17:44:26 +00004978 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004979 }else{
4980 rc = SQLITE_OK;
4981 }
drhac69b052004-05-12 13:30:07 +00004982 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004983 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004984 return rc;
4985}
4986
drh73509ee2003-04-06 20:44:45 +00004987/*
4988** Return the full pathname of the database file.
4989*/
danielk19773b8a05f2007-03-19 17:44:26 +00004990const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004991 return pPager->zFilename;
4992}
4993
drhb20ea9d2004-02-09 01:20:36 +00004994/*
drhd0679ed2007-08-28 22:24:34 +00004995** Return the VFS structure for the pager.
4996*/
4997const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4998 return pPager->pVfs;
4999}
5000
5001/*
drhcc6bb3e2007-08-31 16:11:35 +00005002** Return the file handle for the database file associated
5003** with the pager. This might return NULL if the file has
5004** not yet been opened.
5005*/
5006sqlite3_file *sqlite3PagerFile(Pager *pPager){
5007 return pPager->fd;
5008}
5009
5010/*
danielk19775865e3d2004-06-14 06:03:57 +00005011** Return the directory of the database file.
5012*/
danielk19773b8a05f2007-03-19 17:44:26 +00005013const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005014 return pPager->zDirectory;
5015}
5016
5017/*
5018** Return the full pathname of the journal file.
5019*/
danielk19773b8a05f2007-03-19 17:44:26 +00005020const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005021 return pPager->zJournal;
5022}
5023
5024/*
drh2c8997b2005-08-27 16:36:48 +00005025** Return true if fsync() calls are disabled for this pager. Return FALSE
5026** if fsync()s are executed normally.
5027*/
danielk19773b8a05f2007-03-19 17:44:26 +00005028int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00005029 return pPager->noSync;
5030}
5031
drh7c4ac0c2007-04-05 11:25:58 +00005032#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00005033/*
drhb20ea9d2004-02-09 01:20:36 +00005034** Set the codec for this pager
5035*/
danielk19773b8a05f2007-03-19 17:44:26 +00005036void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00005037 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00005038 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00005039 void *pCodecArg
5040){
5041 pPager->xCodec = xCodec;
5042 pPager->pCodecArg = pCodecArg;
5043}
drh7c4ac0c2007-04-05 11:25:58 +00005044#endif
drhb20ea9d2004-02-09 01:20:36 +00005045
danielk1977687566d2004-11-02 12:56:41 +00005046#ifndef SQLITE_OMIT_AUTOVACUUM
5047/*
danielk197787c29a92008-01-18 11:33:16 +00005048** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00005049**
drh5e385312007-06-16 04:42:12 +00005050** There must be no references to the page previously located at
5051** pgno (which we call pPgOld) though that page is allowed to be
5052** in cache. If the page previous located at pgno is not already
5053** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00005054**
drh5e385312007-06-16 04:42:12 +00005055** References to the page pPg remain valid. Updating any
5056** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00005057** allocated along with the page) is the responsibility of the caller.
5058**
danielk19775fd057a2005-03-09 13:09:43 +00005059** A transaction must be active when this routine is called. It used to be
5060** required that a statement transaction was not active, but this restriction
5061** has been removed (CREATE INDEX needs to move a page when a statement
5062** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00005063*/
danielk19773b8a05f2007-03-19 17:44:26 +00005064int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00005065 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00005066 int h;
danielk197794daf7f2004-11-08 09:26:09 +00005067 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00005068
drh86f8c192007-08-22 00:39:19 +00005069 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005070 assert( pPg->nRef>0 );
5071
drh4f0c5872007-03-26 22:05:01 +00005072 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00005073 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00005074 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00005075
danielk1977b4626a32007-04-28 15:47:43 +00005076 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00005077 if( pPg->needSync ){
5078 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00005079 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00005080 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00005081 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00005082 }
5083
drh85b623f2007-12-13 21:54:09 +00005084 /* Unlink pPg from its hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00005085 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00005086
danielk1977ef73ee92004-11-06 12:26:07 +00005087 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00005088 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00005089 ** page pgno before the 'move' operation, it needs to be retained
5090 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00005091 */
drh5e385312007-06-16 04:42:12 +00005092 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00005093 pPgOld = pager_lookup(pPager, pgno);
5094 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00005095 assert( pPgOld->nRef==0 );
5096 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00005097 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00005098 pPg->needSync = pPgOld->needSync;
5099 }else{
5100 pPg->needSync = 0;
5101 }
drhf5e7bb52008-02-18 14:47:33 +00005102 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
danielk1977687566d2004-11-02 12:56:41 +00005103
danielk1977f5fdda82004-11-03 08:44:05 +00005104 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00005105 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00005106 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00005107 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00005108 if( pPager->aHash[h] ){
5109 assert( pPager->aHash[h]->pPrevHash==0 );
5110 pPager->aHash[h]->pPrevHash = pPg;
5111 }
5112 pPg->pNextHash = pPager->aHash[h];
5113 pPager->aHash[h] = pPg;
5114 pPg->pPrevHash = 0;
5115
drh605ad8f2006-05-03 23:34:05 +00005116 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00005117 pPager->dirtyCache = 1;
5118
danielk197794daf7f2004-11-08 09:26:09 +00005119 if( needSyncPgno ){
5120 /* If needSyncPgno is non-zero, then the journal file needs to be
5121 ** sync()ed before any data is written to database file page needSyncPgno.
5122 ** Currently, no such page exists in the page-cache and the
drhf5e7bb52008-02-18 14:47:33 +00005123 ** Pager.pInJournal bit has been set. This needs to be remedied by loading
danielk197794daf7f2004-11-08 09:26:09 +00005124 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00005125 **
danielk1977a98d7b42008-01-18 13:42:54 +00005126 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00005127 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00005128 ** array. Otherwise, if the page is loaded and written again in
5129 ** this transaction, it may be written to the database file before
5130 ** it is synced into the journal file. This way, it may end up in
5131 ** the journal file twice, but that is not a problem.
5132 **
danielk19773b8a05f2007-03-19 17:44:26 +00005133 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00005134 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00005135 */
5136 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005137 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00005138 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00005139 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00005140 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00005141 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
5142 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00005143 }
danielk197787c29a92008-01-18 11:33:16 +00005144 pagerLeave(pPager);
5145 return rc;
5146 }
danielk1977ae825582004-11-23 09:06:55 +00005147 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00005148 pPgHdr->needSync = 1;
5149 pPgHdr->inJournal = 1;
5150 makeDirty(pPgHdr);
5151 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00005152 }
5153
drh86f8c192007-08-22 00:39:19 +00005154 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005155 return SQLITE_OK;
5156}
5157#endif
5158
danielk19773b8a05f2007-03-19 17:44:26 +00005159/*
5160** Return a pointer to the data for the specified page.
5161*/
5162void *sqlite3PagerGetData(DbPage *pPg){
5163 return PGHDR_TO_DATA(pPg);
5164}
5165
5166/*
5167** Return a pointer to the Pager.nExtra bytes of "extra" space
5168** allocated along with the specified page.
5169*/
5170void *sqlite3PagerGetExtra(DbPage *pPg){
5171 Pager *pPager = pPg->pPager;
5172 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
5173}
5174
danielk197741483462007-03-24 16:45:04 +00005175/*
5176** Get/set the locking-mode for this pager. Parameter eMode must be one
5177** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
5178** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
5179** the locking-mode is set to the value specified.
5180**
5181** The returned value is either PAGER_LOCKINGMODE_NORMAL or
5182** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
5183** locking-mode.
5184*/
5185int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00005186 assert( eMode==PAGER_LOCKINGMODE_QUERY
5187 || eMode==PAGER_LOCKINGMODE_NORMAL
5188 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5189 assert( PAGER_LOCKINGMODE_QUERY<0 );
5190 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5191 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00005192 pPager->exclusiveMode = eMode;
5193 }
5194 return (int)pPager->exclusiveMode;
5195}
5196
drhd919fe12007-12-11 19:34:44 +00005197#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00005198/*
5199** Print a listing of all referenced pages and their ref count.
5200*/
danielk19773b8a05f2007-03-19 17:44:26 +00005201void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00005202 PgHdr *pPg;
5203 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5204 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00005205 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5206 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00005207 }
5208}
5209#endif
drh2e66f0b2005-04-28 17:18:48 +00005210
5211#endif /* SQLITE_OMIT_DISKIO */