blob: d75bc5fe2026ef9511beaf0cdb1b4552836e8748 [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**
drh86f8c192007-08-22 00:39:19 +000021** @(#) $Id: pager.c,v 1.365 2007/08/22 00:39:20 drh 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**
50** PAGERID() takes a pointer to a Pager struct as it's argument. The
danielk197762079062007-08-15 17:08:46 +000051** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
danielk1977599fcba2004-11-08 07:13:13 +000052** struct as it's argument.
53*/
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
134/*
drhed7c8552001-04-11 14:29:21 +0000135** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +0000136** This header is only visible to this pager module. The client
137** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +0000138**
danielk19773b8a05f2007-03-19 17:44:26 +0000139** Client code should call sqlite3PagerWrite() on a page prior to making
140** any modifications to that page. The first time sqlite3PagerWrite()
drhf6038712004-02-08 18:07:34 +0000141** is called, the original page contents are written into the rollback
142** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
143** the journal page has made it onto the disk surface, PgHdr.needSync
144** is cleared. The modified page cannot be written back into the original
145** database file until the journal pages has been synced to disk and the
146** PgHdr.needSync has been cleared.
147**
danielk19773b8a05f2007-03-19 17:44:26 +0000148** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
drhf6038712004-02-08 18:07:34 +0000149** is cleared again when the page content is written back to the original
150** database file.
drh732c8172007-06-16 11:17:45 +0000151**
152** Details of important structure elements:
153**
154** needSync
155**
156** If this is true, this means that it is not safe to write the page
157** content to the database because the original content needed
158** for rollback has not by synced to the main rollback journal.
159** The original content may have been written to the rollback journal
160** but it has not yet been synced. So we cannot write to the database
161** file because power failure might cause the page in the journal file
162** to never reach the disk. It is as if the write to the journal file
163** does not occur until the journal file is synced.
164**
165** This flag is false if the page content exactly matches what
166** currently exists in the database file. The needSync flag is also
167** false if the original content has been written to the main rollback
168** journal and synced. If the page represents a new page that has
169** been added onto the end of the database during the current
170** transaction, the needSync flag is true until the original database
171** size in the journal header has been synced to disk.
172**
173** inJournal
174**
175** This is true if the original page has been written into the main
176** rollback journal. This is always false for new pages added to
177** the end of the database file during the current transaction.
178** And this flag says nothing about whether or not the journal
179** has been synced to disk. For pages that are in the original
180** database file, the following expression should always be true:
181**
182** inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0
183**
184** The pPager->aInJournal[] array is only valid for the original
185** pages of the database, not new pages that are added to the end
186** of the database, so obviously the above expression cannot be
187** valid for new pages. For new pages inJournal is always 0.
188**
189** dirty
190**
191** When true, this means that the content of the page has been
192** modified and needs to be written back to the database file.
193** If false, it means that either the content of the page is
194** unchanged or else the content is unimportant and we do not
195** care whether or not it is preserved.
196**
197** alwaysRollback
198**
199** This means that the sqlite3PagerDontRollback() API should be
200** ignored for this page. The DontRollback() API attempts to say
201** that the content of the page on disk is unimportant (it is an
202** unused page on the freelist) so that it is unnecessary to
203** rollback changes to this page because the content of the page
204** can change without changing the meaning of the database. This
205** flag overrides any DontRollback() attempt. This flag is set
206** when a page that originally contained valid data is added to
207** the freelist. Later in the same transaction, this page might
208** be pulled from the freelist and reused for something different
209** and at that point the DontRollback() API will be called because
210** pages taken from the freelist do not need to be protected by
211** the rollback journal. But this flag says that the page was
212** not originally part of the freelist so that it still needs to
213** be rolled back in spite of any subsequent DontRollback() calls.
214**
215** needRead
216**
217** This flag means (when true) that the content of the page has
218** not yet been loaded from disk. The in-memory content is just
219** garbage. (Actually, we zero the content, but you should not
220** make any assumptions about the content nevertheless.) If the
221** content is needed in the future, it should be read from the
222** original database file.
drhed7c8552001-04-11 14:29:21 +0000223*/
drhd9b02572001-04-15 00:37:09 +0000224typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +0000225struct PgHdr {
226 Pager *pPager; /* The pager to which this page belongs */
227 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000228 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhd9b02572001-04-15 00:37:09 +0000229 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
drhac69b052004-05-12 13:30:07 +0000230 PgHdr *pNextAll; /* A list of all pages */
drh193a6b42002-07-07 16:52:46 +0000231 u8 inJournal; /* TRUE if has been written to journal */
drh193a6b42002-07-07 16:52:46 +0000232 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000233 u8 needSync; /* Sync journal before writing this page */
drh80e35f42007-03-30 14:06:34 +0000234 u8 alwaysRollback; /* Disable DontRollback() for this page */
drh538f5702007-04-13 02:14:30 +0000235 u8 needRead; /* Read content if PagerWrite() is called */
drhac69b052004-05-12 13:30:07 +0000236 short int nRef; /* Number of users of this page */
drhdc3e10b2006-06-15 14:31:06 +0000237 PgHdr *pDirty, *pPrevDirty; /* Dirty pages */
drh605ad8f2006-05-03 23:34:05 +0000238 u32 notUsed; /* Buffer space */
danielk19773c407372005-02-15 02:54:14 +0000239#ifdef SQLITE_CHECK_PAGES
240 u32 pageHash;
241#endif
drh1c7880e2005-05-20 20:01:55 +0000242 /* pPager->pageSize bytes of page data follow this header */
drh973b6e32003-02-12 14:09:42 +0000243 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +0000244};
245
drhac69b052004-05-12 13:30:07 +0000246/*
247** For an in-memory only database, some extra information is recorded about
248** each page so that changes can be rolled back. (Journal files are not
249** used for in-memory databases.) The following information is added to
250** the end of every EXTRA block for in-memory databases.
251**
252** This information could have been added directly to the PgHdr structure.
253** But then it would take up an extra 8 bytes of storage on every PgHdr
254** even for disk-based databases. Splitting it out saves 8 bytes. This
255** is only a savings of 0.8% but those percentages add up.
256*/
257typedef struct PgHistory PgHistory;
258struct PgHistory {
259 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
260 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000261 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
262 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000263};
drh9eb9e262004-02-11 02:18:05 +0000264
265/*
266** A macro used for invoking the codec if there is one
267*/
268#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000269# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
270# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000271#else
drhc001c582006-03-06 18:23:16 +0000272# define CODEC1(P,D,N,X) /* NO-OP */
273# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000274#endif
275
drhed7c8552001-04-11 14:29:21 +0000276/*
drh69688d52001-04-14 16:38:23 +0000277** Convert a pointer to a PgHdr into a pointer to its data
278** and back again.
drhed7c8552001-04-11 14:29:21 +0000279*/
280#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
281#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drh1c7880e2005-05-20 20:01:55 +0000282#define PGHDR_TO_EXTRA(G,P) ((void*)&((char*)(&(G)[1]))[(P)->pageSize])
drhac69b052004-05-12 13:30:07 +0000283#define PGHDR_TO_HIST(P,PGR) \
drh1c7880e2005-05-20 20:01:55 +0000284 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000285
286/*
drhed7c8552001-04-11 14:29:21 +0000287** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000288**
drh4f0ee682007-03-30 20:43:40 +0000289** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000290** or SQLITE_FULL. Once one of the first three errors occurs, it persists
291** and is returned as the result of every major pager API call. The
292** SQLITE_FULL return code is slightly different. It persists only until the
293** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000294** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000295** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000296*/
297struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000298 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000299 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000300 u8 journalStarted; /* True if header of journal is synced */
301 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000302 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000303 u8 stmtOpen; /* True if the statement subjournal is open */
304 u8 stmtInUse; /* True we are in a statement subtransaction */
305 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000306 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000307 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000308 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000309 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000310 u8 tempFile; /* zFilename is a temporary file */
311 u8 readOnly; /* True for a read-only database */
312 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000313 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000314 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000315 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000316 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000317 u8 doNotSync; /* Boolean. While true, do not spill the cache */
318 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
319 u8 changeCountDone; /* Set after incrementing the change-counter */
drhe49f9822006-09-15 12:29:16 +0000320 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000321 int dbSize; /* Number of pages in the file */
322 int origDbSize; /* dbSize before the current change */
323 int stmtSize; /* Size of database (in pages) at stmt_begin() */
324 int nRec; /* Number of pages written to the journal */
325 u32 cksumInit; /* Quasi-random value added to every checksum */
326 int stmtNRec; /* Number of records in stmt subjournal */
327 int nExtra; /* Add this many bytes to each in-memory page */
328 int pageSize; /* Number of bytes in a page */
329 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000330 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
331 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000332 Pgno mxPgno; /* Maximum allowed size of the database */
drh603240c2002-03-05 01:11:12 +0000333 u8 *aInJournal; /* One bit for each page in the database file */
drhac69b052004-05-12 13:30:07 +0000334 u8 *aInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000335 char *zFilename; /* Name of the database file */
336 char *zJournal; /* Name of the journal file */
337 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000338 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
339 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000340 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
drhed7c8552001-04-11 14:29:21 +0000341 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000342 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000343 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000344 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000345 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000346 i64 journalOff; /* Current byte offset in the journal file */
347 i64 journalHdr; /* Byte offset to previous journal header */
348 i64 stmtHdrOff; /* First journal header written this statement */
349 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000350 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000351 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000352#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000353 int nHit, nMiss; /* Cache hits and missing */
354 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000355#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000356 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
357 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000358#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000359 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000360 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000361#endif
drh8ca0c722006-05-07 17:49:38 +0000362 int nHash; /* Size of the pager hash table */
363 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000364#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +0000365 Pager *pNext; /* Doubly linked list of pagers on which */
366 Pager *pPrev; /* sqlite3_release_memory() will work */
367 int iInUseMM; /* Non-zero if unavailable to MM */
368 int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
danielk197713f72992005-12-18 08:51:22 +0000369#endif
danielk19778186df82007-03-06 13:45:59 +0000370 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000371 char dbFileVers[16]; /* Changes whenever database file changes */
drhd9b02572001-04-15 00:37:09 +0000372};
373
374/*
drh538f5702007-04-13 02:14:30 +0000375** The following global variables hold counters used for
376** testing purposes only. These variables do not exist in
377** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000378*/
379#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000380int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
381int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
382int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
383int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
384# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000385#else
drh538f5702007-04-13 02:14:30 +0000386# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000387#endif
388
drh86f8c192007-08-22 00:39:19 +0000389/*
390** The following variable points to the head of a double-linked list
391** of all pagers that are eligible for page stealing by the
392** sqlite3_release_memory() interface. Access to this list is
393** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
394*/
395#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
396static Pager *sqlite3PagerList = 0;
397#endif
drh538f5702007-04-13 02:14:30 +0000398
399
drhfcd35c72005-05-21 02:48:08 +0000400/*
drh5e00f6c2001-09-13 13:46:56 +0000401** Journal files begin with the following magic string. The data
402** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000403**
drhae2b40c2004-06-09 19:03:54 +0000404** Since version 2.8.0, the journal format contains additional sanity
405** checking information. If the power fails while the journal is begin
406** written, semi-random garbage data might appear in the journal
407** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000408** to roll the journal back, the database could be corrupted. The additional
409** sanity checking data is an attempt to discover the garbage in the
410** journal and ignore it.
411**
drhae2b40c2004-06-09 19:03:54 +0000412** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000413** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000414** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000415** This cksum is initialized to a 32-bit random value that appears in the
416** journal file right after the header. The random initializer is important,
417** because garbage data that appears at the end of a journal is likely
418** data that was once in other files that have now been deleted. If the
419** garbage data came from an obsolete journal file, the checksums might
420** be correct. But by initializing the checksum to random value which
421** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000422*/
drhae2b40c2004-06-09 19:03:54 +0000423static const unsigned char aJournalMagic[] = {
424 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000425};
426
427/*
drh726de592004-06-10 23:35:50 +0000428** The size of the header and of each page in the journal is determined
429** by the following macros.
drh968af522003-02-11 14:55:40 +0000430*/
drhae2b40c2004-06-09 19:03:54 +0000431#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000432
danielk197776572402004-06-25 02:38:54 +0000433/*
434** The journal header size for this pager. In the future, this could be
435** set to some value read from the disk controller. The important
436** characteristic is that it is the same size as a disk sector.
437*/
438#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
439
drhb7f91642004-10-31 02:22:47 +0000440/*
441** The macro MEMDB is true if we are dealing with an in-memory database.
442** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
443** the value of MEMDB will be a constant and the compiler will optimize
444** out code that would never execute.
445*/
446#ifdef SQLITE_OMIT_MEMORYDB
447# define MEMDB 0
448#else
449# define MEMDB pPager->memDb
450#endif
451
452/*
danielk197776572402004-06-25 02:38:54 +0000453** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
454** reserved for working around a windows/posix incompatibility). It is
455** used in the journal to signify that the remainder of the journal file
456** is devoted to storing a master journal name - there are no more pages to
457** roll back. See comments for function writeMasterJournal() for details.
458*/
danielk1977599fcba2004-11-08 07:13:13 +0000459/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
460#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000461
drh968af522003-02-11 14:55:40 +0000462/*
danielk197726836652005-01-17 01:33:13 +0000463** The maximum legal page number is (2^31 - 1).
464*/
465#define PAGER_MAX_PGNO 2147483647
466
467/*
drh86f8c192007-08-22 00:39:19 +0000468** The pagerEnter() and pagerLeave() routines acquire and release
469** a mutex on each pager. The mutex is recursive.
470**
471** This is a special-purpose mutex. It only provides mutual exclusion
472** between the Btree and the Memory Management sqlite3_release_memory()
473** function. It does not prevent, for example, two Btrees from accessing
474** the same pager at the same time. Other general-purpose mutexes in
475** the btree layer handle that chore.
476*/
477#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
478 static void pagerEnter(Pager *p){
479 p->iInUseDB++;
480 if( p->iInUseMM && p->iInUseDB==1 ){
481 sqlite3_mutex *mutex;
482 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
483 p->iInUseDB = 0;
484 sqlite3_mutex_enter(mutex);
485 p->iInUseDB = 1;
486 sqlite3_mutex_leave(mutex);
487 }
488 assert( p->iInUseMM==0 );
489 }
490 static void pagerLeave(Pager *p){
491 p->iInUseDB--;
492 assert( p->iInUseDB>=0 );
493 }
494#else
495# define pagerEnter(X)
496# define pagerLeave(X)
497#endif
498
499/*
drh726de592004-06-10 23:35:50 +0000500** Enable reference count tracking (for debugging) here:
drhdd793422001-06-28 01:54:48 +0000501*/
drh7c4ac0c2007-04-05 11:25:58 +0000502#ifdef SQLITE_DEBUG
drh3aac2dd2004-04-26 14:10:20 +0000503 int pager3_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000504 static void pager_refinfo(PgHdr *p){
505 static int cnt = 0;
drh3aac2dd2004-04-26 14:10:20 +0000506 if( !pager3_refinfo_enable ) return;
drhfe63d1c2004-09-08 20:13:04 +0000507 sqlite3DebugPrintf(
drh24c9a2e2007-01-05 02:00:47 +0000508 "REFCNT: %4d addr=%p nRef=%-3d total=%d\n",
509 p->pgno, PGHDR_TO_DATA(p), p->nRef, p->pPager->nRef
drhdd793422001-06-28 01:54:48 +0000510 );
511 cnt++; /* Something to set a breakpoint on */
512 }
513# define REFINFO(X) pager_refinfo(X)
514#else
515# define REFINFO(X)
516#endif
517
danielk1977f35843b2007-04-07 15:03:17 +0000518/*
519** Return true if page *pPg has already been written to the statement
520** journal (or statement snapshot has been created, if *pPg is part
521** of an in-memory database).
522*/
523static int pageInStatement(PgHdr *pPg){
524 Pager *pPager = pPg->pPager;
525 if( MEMDB ){
526 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
527 }else{
528 Pgno pgno = pPg->pgno;
529 u8 *a = pPager->aInStmt;
530 return (a && (int)pgno<=pPager->stmtSize && (a[pgno/8] & (1<<(pgno&7))));
531 }
532}
drh8ca0c722006-05-07 17:49:38 +0000533
534/*
535** Change the size of the pager hash table to N. N must be a power
536** of two.
537*/
538static void pager_resize_hash_table(Pager *pPager, int N){
539 PgHdr **aHash, *pPg;
540 assert( N>0 && (N&(N-1))==0 );
drh17435752007-08-16 04:30:38 +0000541 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drh8ca0c722006-05-07 17:49:38 +0000542 if( aHash==0 ){
543 /* Failure to rehash is not an error. It is only a performance hit. */
544 return;
545 }
drh17435752007-08-16 04:30:38 +0000546 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000547 pPager->nHash = N;
548 pPager->aHash = aHash;
549 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000550 int h;
551 if( pPg->pgno==0 ){
552 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
553 continue;
554 }
555 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000556 pPg->pNextHash = aHash[h];
557 if( aHash[h] ){
558 aHash[h]->pPrevHash = pPg;
559 }
560 aHash[h] = pPg;
561 pPg->pPrevHash = 0;
562 }
563}
564
drhdd793422001-06-28 01:54:48 +0000565/*
drh34e79ce2004-02-08 06:05:46 +0000566** Read a 32-bit integer from the given file descriptor. Store the integer
567** that is read in *pRes. Return SQLITE_OK if everything worked, or an
568** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000569**
570** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000571*/
danielk197762079062007-08-15 17:08:46 +0000572static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000573 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000574 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000575 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000576 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000577 }
drh94f33312002-08-12 12:29:56 +0000578 return rc;
579}
580
581/*
drh97b57482006-01-10 20:32:32 +0000582** Write a 32-bit integer into a string buffer in big-endian byte order.
583*/
drha3152892007-05-05 11:48:52 +0000584#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000585
586/*
drh34e79ce2004-02-08 06:05:46 +0000587** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
588** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000589*/
danielk197762079062007-08-15 17:08:46 +0000590static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000591 char ac[4];
drh97b57482006-01-10 20:32:32 +0000592 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000593 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000594}
595
drh2554f8b2003-01-22 01:26:44 +0000596/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000597** If file pFd is open, call sqlite3OsUnlock() on it.
598*/
599static int osUnlock(sqlite3_file *pFd, int eLock){
600 if( !pFd->pMethods ){
601 return SQLITE_OK;
602 }
603 return sqlite3OsUnlock(pFd, eLock);
604}
605
606/*
danielk1977aef0bf62005-12-30 16:28:01 +0000607** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000608** code. The first argument is a pointer to the pager structure, the
609** second the error-code about to be returned by a pager API function.
610** The value returned is a copy of the second argument to this function.
611**
drh4f0ee682007-03-30 20:43:40 +0000612** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977a96a7102006-01-16 12:46:41 +0000613** the error becomes persistent. All subsequent API calls on this Pager
614** will immediately return the same error code.
danielk1977aef0bf62005-12-30 16:28:01 +0000615*/
616static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000617 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000618 assert(
619 pPager->errCode==SQLITE_FULL ||
620 pPager->errCode==SQLITE_OK ||
621 (pPager->errCode & 0xff)==SQLITE_IOERR
622 );
danielk1977979f38e2007-03-27 16:19:51 +0000623 if(
drh4ac285a2006-09-15 07:28:50 +0000624 rc2==SQLITE_FULL ||
625 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000626 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000627 ){
628 pPager->errCode = rc;
danielk1977aef0bf62005-12-30 16:28:01 +0000629 }
630 return rc;
631}
632
drh477731b2007-06-16 03:06:27 +0000633/*
634** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
635** on the cache using a hash function. This is used for testing
636** and debugging only.
637*/
danielk19773c407372005-02-15 02:54:14 +0000638#ifdef SQLITE_CHECK_PAGES
639/*
640** Return a 32-bit hash of the page data for pPage.
641*/
drh477731b2007-06-16 03:06:27 +0000642static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000643 u32 hash = 0;
644 int i;
drh477731b2007-06-16 03:06:27 +0000645 for(i=0; i<nByte; i++){
646 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000647 }
648 return hash;
649}
drh477731b2007-06-16 03:06:27 +0000650static u32 pager_pagehash(PgHdr *pPage){
651 return pager_datahash(pPage->pPager->pageSize,
652 (unsigned char *)PGHDR_TO_DATA(pPage));
653}
danielk19773c407372005-02-15 02:54:14 +0000654
655/*
656** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
657** is defined, and NDEBUG is not defined, an assert() statement checks
658** that the page is either dirty or still matches the calculated page-hash.
659*/
660#define CHECK_PAGE(x) checkPage(x)
661static void checkPage(PgHdr *pPg){
662 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000663 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000664 pPg->pageHash==pager_pagehash(pPg) );
665}
666
667#else
drh8ffa8172007-06-18 17:25:17 +0000668#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000669#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000670#define CHECK_PAGE(x)
671#endif
672
drhed7c8552001-04-11 14:29:21 +0000673/*
danielk197776572402004-06-25 02:38:54 +0000674** When this is called the journal file for pager pPager must be open.
675** The master journal file name is read from the end of the file and
drh17435752007-08-16 04:30:38 +0000676** written into memory obtained from sqlite3_malloc(). *pzMaster is
danielk197776572402004-06-25 02:38:54 +0000677** set to point at the memory and SQLITE_OK returned. The caller must
drh17435752007-08-16 04:30:38 +0000678** sqlite3_free() *pzMaster.
danielk197776572402004-06-25 02:38:54 +0000679**
680** If no master journal file name is present *pzMaster is set to 0 and
681** SQLITE_OK returned.
682*/
danielk197762079062007-08-15 17:08:46 +0000683static int readMasterJournal(sqlite3_file *pJrnl, char **pzMaster){
danielk197776572402004-06-25 02:38:54 +0000684 int rc;
685 u32 len;
drheb206252004-10-01 02:00:31 +0000686 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000687 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000688 int i;
danielk197776572402004-06-25 02:38:54 +0000689 unsigned char aMagic[8]; /* A buffer to hold the magic header */
690
691 *pzMaster = 0;
692
drh054889e2005-11-30 03:20:31 +0000693 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000694 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000695
danielk197762079062007-08-15 17:08:46 +0000696 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000697 if( rc!=SQLITE_OK ) return rc;
698
danielk197762079062007-08-15 17:08:46 +0000699 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000700 if( rc!=SQLITE_OK ) return rc;
701
danielk197762079062007-08-15 17:08:46 +0000702 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000703 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
704
drh17435752007-08-16 04:30:38 +0000705 *pzMaster = (char *)sqlite3MallocZero(len+1);
danielk197776572402004-06-25 02:38:54 +0000706 if( !*pzMaster ){
707 return SQLITE_NOMEM;
708 }
danielk197762079062007-08-15 17:08:46 +0000709 rc = sqlite3OsRead(pJrnl, *pzMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000710 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000711 sqlite3_free(*pzMaster);
danielk197776572402004-06-25 02:38:54 +0000712 *pzMaster = 0;
713 return rc;
714 }
danielk1977cafadba2004-06-25 11:11:54 +0000715
716 /* See if the checksum matches the master journal name */
717 for(i=0; i<len; i++){
danielk19778191bff2004-06-28 04:52:30 +0000718 cksum -= (*pzMaster)[i];
danielk1977cafadba2004-06-25 11:11:54 +0000719 }
danielk19778191bff2004-06-28 04:52:30 +0000720 if( cksum ){
721 /* If the checksum doesn't add up, then one or more of the disk sectors
722 ** containing the master journal filename is corrupted. This means
723 ** definitely roll back, so just return SQLITE_OK and report a (nul)
724 ** master-journal filename.
725 */
drh17435752007-08-16 04:30:38 +0000726 sqlite3_free(*pzMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000727 *pzMaster = 0;
danielk1977aca790a2005-01-13 11:07:52 +0000728 }else{
729 (*pzMaster)[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000730 }
danielk197776572402004-06-25 02:38:54 +0000731
732 return SQLITE_OK;
733}
734
735/*
736** Seek the journal file descriptor to the next sector boundary where a
737** journal header may be read or written. Pager.journalOff is updated with
738** the new seek offset.
739**
740** i.e for a sector size of 512:
741**
742** Input Offset Output Offset
743** ---------------------------------------
744** 0 0
745** 512 512
746** 100 512
747** 2000 2048
748**
749*/
danielk197762079062007-08-15 17:08:46 +0000750static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000751 i64 offset = 0;
752 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000753 if( c ){
754 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
755 }
756 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
757 assert( offset>=c );
758 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
759 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000760}
761
762/*
763** The journal file must be open when this routine is called. A journal
764** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
765** current location.
766**
767** The format for the journal header is as follows:
768** - 8 bytes: Magic identifying journal format.
769** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
770** - 4 bytes: Random number used for page hash.
771** - 4 bytes: Initial database page count.
772** - 4 bytes: Sector size used by the process that wrote this journal.
773**
danielk1977f42f25c2004-06-25 07:21:28 +0000774** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000775*/
776static int writeJournalHdr(Pager *pPager){
drh97b57482006-01-10 20:32:32 +0000777 char zHeader[sizeof(aJournalMagic)+16];
danielk19774099f6e2007-03-19 11:25:20 +0000778 int rc;
danielk197776572402004-06-25 02:38:54 +0000779
danielk19774099f6e2007-03-19 11:25:20 +0000780 if( pPager->stmtHdrOff==0 ){
781 pPager->stmtHdrOff = pPager->journalOff;
782 }
783
danielk197762079062007-08-15 17:08:46 +0000784 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000785 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000786
787 /* FIX ME:
788 **
789 ** Possibly for a pager not in no-sync mode, the journal magic should not
790 ** be written until nRec is filled in as part of next syncJournal().
791 **
792 ** Actually maybe the whole journal header should be delayed until that
793 ** point. Think about this.
794 */
drh97b57482006-01-10 20:32:32 +0000795 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
796 /* The nRec Field. 0xFFFFFFFF for no-sync journals. */
797 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->noSync ? 0xffffffff : 0);
798 /* The random check-hash initialiser */
799 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
800 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
801 /* The initial database size */
802 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
803 /* The assumed sector size for this process */
804 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drhb0603412007-02-28 04:47:26 +0000805 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
danielk197762079062007-08-15 17:08:46 +0000806 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
807 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197776572402004-06-25 02:38:54 +0000808
809 /* The journal header has been written successfully. Seek the journal
810 ** file descriptor to the end of the journal header sector.
811 */
812 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +0000813 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
danielk197762079062007-08-15 17:08:46 +0000814 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
danielk197776572402004-06-25 02:38:54 +0000815 }
816 return rc;
817}
818
819/*
820** The journal file must be open when this is called. A journal header file
821** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
822** file. See comments above function writeJournalHdr() for a description of
823** the journal header format.
824**
825** If the header is read successfully, *nRec is set to the number of
826** page records following this header and *dbSize is set to the size of the
827** database before the transaction began, in pages. Also, pPager->cksumInit
828** is set to the value read from the journal header. SQLITE_OK is returned
829** in this case.
830**
831** If the journal header file appears to be corrupted, SQLITE_DONE is
832** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
833** cannot be read from the journal file an error code is returned.
834*/
835static int readJournalHdr(
836 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000837 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000838 u32 *pNRec,
839 u32 *pDbSize
840){
841 int rc;
842 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000843 i64 jrnlOff;
danielk197776572402004-06-25 02:38:54 +0000844
danielk197762079062007-08-15 17:08:46 +0000845 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000846 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
847 return SQLITE_DONE;
848 }
danielk197762079062007-08-15 17:08:46 +0000849 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000850
danielk197762079062007-08-15 17:08:46 +0000851 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000852 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000853 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000854
855 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
856 return SQLITE_DONE;
857 }
858
danielk197762079062007-08-15 17:08:46 +0000859 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000860 if( rc ) return rc;
861
danielk197762079062007-08-15 17:08:46 +0000862 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000863 if( rc ) return rc;
864
danielk197762079062007-08-15 17:08:46 +0000865 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000866 if( rc ) return rc;
867
868 /* Update the assumed sector-size to match the value used by
869 ** the process that created this journal. If this journal was
870 ** created by a process other than this one, then this routine
871 ** is being called from within pager_playback(). The local value
872 ** of Pager.sectorSize is restored at the end of that routine.
873 */
danielk197762079062007-08-15 17:08:46 +0000874 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000875 if( rc ) return rc;
876
877 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000878 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000879}
880
881
882/*
883** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000884** pPager at the current location. The master journal name must be the last
885** thing written to a journal file. If the pager is in full-sync mode, the
886** journal file descriptor is advanced to the next sector boundary before
887** anything is written. The format is:
888**
889** + 4 bytes: PAGER_MJ_PGNO.
890** + N bytes: length of master journal name.
891** + 4 bytes: N
892** + 4 bytes: Master journal name checksum.
893** + 8 bytes: aJournalMagic[].
894**
895** The master journal page checksum is the sum of the bytes in the master
896** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000897**
898** If zMaster is a NULL pointer (occurs for a single database transaction),
899** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000900*/
901static int writeMasterJournal(Pager *pPager, const char *zMaster){
902 int rc;
903 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000904 int i;
danielk197762079062007-08-15 17:08:46 +0000905 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +0000906 u32 cksum = 0;
907 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000908
909 if( !zMaster || pPager->setMaster) return SQLITE_OK;
910 pPager->setMaster = 1;
911
912 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000913 for(i=0; i<len; i++){
914 cksum += zMaster[i];
915 }
danielk197776572402004-06-25 02:38:54 +0000916
917 /* If in full-sync mode, advance to the next disk sector before writing
918 ** the master journal name. This is in case the previous page written to
919 ** the journal has already been synced.
920 */
921 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000922 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000923 }
danielk197762079062007-08-15 17:08:46 +0000924 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000925 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000926
danielk197762079062007-08-15 17:08:46 +0000927 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000928 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000929 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000930
danielk197762079062007-08-15 17:08:46 +0000931 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000932 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000933 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000934
drh97b57482006-01-10 20:32:32 +0000935 put32bits(zBuf, len);
936 put32bits(&zBuf[4], cksum);
937 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000938 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +0000939 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +0000940 return rc;
941}
942
943/*
drh03eb96a2002-11-10 23:32:56 +0000944** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +0000945** statement journal.
drh03eb96a2002-11-10 23:32:56 +0000946**
947** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +0000948** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +0000949** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +0000950** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +0000951*/
drh3aac2dd2004-04-26 14:10:20 +0000952static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +0000953 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +0000954 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
955 assert( MEMDB );
956 if( !pHist->inStmt ){
957 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
958 if( pPager->pStmt ){
959 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
960 }
961 pHist->pNextStmt = pPager->pStmt;
962 pPager->pStmt = pPg;
963 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +0000964 }
drh03eb96a2002-11-10 23:32:56 +0000965}
966
967/*
drhed7c8552001-04-11 14:29:21 +0000968** Find a page in the hash table given its page number. Return
969** a pointer to the page or NULL if not found.
970*/
drhd9b02572001-04-15 00:37:09 +0000971static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000972 PgHdr *p;
973 if( pPager->aHash==0 ) return 0;
974 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +0000975 while( p && p->pgno!=pgno ){
976 p = p->pNextHash;
977 }
978 return p;
979}
980
981/*
drh1aa2d8b2007-01-03 15:34:29 +0000982** Unlock the database file.
drh1aa2d8b2007-01-03 15:34:29 +0000983*/
984static void pager_unlock(Pager *pPager){
danielk197741483462007-03-24 16:45:04 +0000985 if( !pPager->exclusiveMode ){
986 if( !MEMDB ){
danielk19777a2b1ee2007-08-21 14:27:01 +0000987 osUnlock(pPager->fd, NO_LOCK);
danielk197741483462007-03-24 16:45:04 +0000988 pPager->dbSize = -1;
989 IOTRACE(("UNLOCK %p\n", pPager))
990 }
991 pPager->state = PAGER_UNLOCK;
992 pPager->changeCountDone = 0;
drh1aa2d8b2007-01-03 15:34:29 +0000993 }
danielk1977e277be02007-03-23 18:12:06 +0000994}
995
996/*
997** Execute a rollback if a transaction is active and unlock the
998** database file. This is a no-op if the pager has already entered
999** the error-state.
1000*/
danielk1977334cdb62007-03-26 08:05:12 +00001001static void pagerUnlockAndRollback(Pager *p){
1002 if( p->errCode ) return;
danielk1977c5859712007-03-26 12:26:27 +00001003 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
danielk1977334cdb62007-03-26 08:05:12 +00001004 if( p->state>=PAGER_RESERVED ){
1005 sqlite3PagerRollback(p);
danielk1977e277be02007-03-23 18:12:06 +00001006 }
danielk1977334cdb62007-03-26 08:05:12 +00001007 pager_unlock(p);
1008 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1009 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drh1aa2d8b2007-01-03 15:34:29 +00001010}
1011
1012
1013/*
danielk1977e180dd92007-04-05 17:15:52 +00001014** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001015** sets the state of the pager back to what it was when it was first
1016** opened. Any outstanding pages are invalidated and subsequent attempts
1017** to access those pages will likely result in a coredump.
1018*/
drhd9b02572001-04-15 00:37:09 +00001019static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001020 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001021 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001022 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001023 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1024 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001025 pNext = pPg->pNextAll;
drh17435752007-08-16 04:30:38 +00001026 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001027 }
danielk1977b94bf852007-03-19 13:53:37 +00001028 pPager->pStmt = 0;
drhed7c8552001-04-11 14:29:21 +00001029 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +00001030 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +00001031 pPager->pLast = 0;
1032 pPager->pAll = 0;
drh8ca0c722006-05-07 17:49:38 +00001033 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001034 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001035 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001036 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001037 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001038}
1039
1040/*
drh80e35f42007-03-30 14:06:34 +00001041** This routine ends a transaction. A transaction is ended by either
1042** a COMMIT or a ROLLBACK.
1043**
drhed7c8552001-04-11 14:29:21 +00001044** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001045** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1046** the database lock and acquires a SHARED lock in its place if that is
1047** the appropriate thing to do. Release locks usually is appropriate,
1048** unless we are in exclusive access mode or unless this is a
1049** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1050**
1051** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001052**
1053** TODO: Consider keeping the journal file open for temporary databases.
1054** This might give a performance improvement on windows where opening
1055** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001056*/
drh80e35f42007-03-30 14:06:34 +00001057static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001058 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001059 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001060 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001061 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001062 if( pPager->state<PAGER_RESERVED ){
1063 return SQLITE_OK;
1064 }
danielk19773b8a05f2007-03-19 17:44:26 +00001065 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001066 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001067 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001068 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001069 }
drhda47d772002-12-02 04:25:19 +00001070 if( pPager->journalOpen ){
drh01fa4c32007-04-02 11:22:22 +00001071 if( pPager->exclusiveMode
1072 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
danielk197741483462007-03-24 16:45:04 +00001073 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001074 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001075 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001076 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001077 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001078 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001079 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001080 }
danielk197741483462007-03-24 16:45:04 +00001081 }
drh17435752007-08-16 04:30:38 +00001082 sqlite3_free( pPager->aInJournal );
drhda47d772002-12-02 04:25:19 +00001083 pPager->aInJournal = 0;
1084 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1085 pPg->inJournal = 0;
1086 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001087 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001088 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001089#ifdef SQLITE_CHECK_PAGES
1090 pPg->pageHash = pager_pagehash(pPg);
1091#endif
drhda47d772002-12-02 04:25:19 +00001092 }
drh605ad8f2006-05-03 23:34:05 +00001093 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001094 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001095 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001096 }else{
drh88b01a12005-03-28 18:04:27 +00001097 assert( pPager->aInJournal==0 );
drha6abd042004-06-09 17:37:22 +00001098 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001099 }
danielk1977979f38e2007-03-27 16:19:51 +00001100
danielk197741483462007-03-24 16:45:04 +00001101 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001102 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001103 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001104 }else if( pPager->state==PAGER_SYNCED ){
1105 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001106 }
danielk1977334cdb62007-03-26 08:05:12 +00001107 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001108 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001109 pPager->needSync = 0;
1110 pPager->pFirstSynced = pPager->pFirst;
drh588f5bc2007-01-02 18:41:54 +00001111 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001112
1113 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001114}
1115
drhed7c8552001-04-11 14:29:21 +00001116/*
drh968af522003-02-11 14:55:40 +00001117** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001118**
1119** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001120** random initial value and the page number. We experimented with
1121** a checksum of the entire data, but that was found to be too slow.
1122**
1123** Note that the page number is stored at the beginning of data and
1124** the checksum is stored at the end. This is important. If journal
1125** corruption occurs due to a power failure, the most likely scenario
1126** is that one end or the other of the record will be changed. It is
1127** much less likely that the two ends of the journal record will be
1128** correct and the middle be corrupt. Thus, this "checksum" scheme,
1129** though fast and simple, catches the mostly likely kind of corruption.
1130**
1131** FIX ME: Consider adding every 200th (or so) byte of the data to the
1132** checksum. That way if a single page spans 3 or more disk sectors and
1133** only the middle sector is corrupt, we will still have a reasonable
1134** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001135*/
drh74161702006-02-24 02:53:49 +00001136static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001137 u32 cksum = pPager->cksumInit;
1138 int i = pPager->pageSize-200;
1139 while( i>0 ){
1140 cksum += aData[i];
1141 i -= 200;
1142 }
drh968af522003-02-11 14:55:40 +00001143 return cksum;
1144}
1145
drh605ad8f2006-05-03 23:34:05 +00001146/* Forward declaration */
1147static void makeClean(PgHdr*);
1148
drh968af522003-02-11 14:55:40 +00001149/*
drhfa86c412002-02-02 15:01:15 +00001150** Read a single page from the journal file opened on file descriptor
1151** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001152**
drh726de592004-06-10 23:35:50 +00001153** If useCksum==0 it means this journal does not use checksums. Checksums
1154** are not used in statement journals because statement journals do not
1155** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001156*/
danielk197762079062007-08-15 17:08:46 +00001157static int pager_playback_one_page(
1158 Pager *pPager,
1159 sqlite3_file *jfd,
1160 i64 offset,
1161 int useCksum
1162){
drhfa86c412002-02-02 15:01:15 +00001163 int rc;
drhae2b40c2004-06-09 19:03:54 +00001164 PgHdr *pPg; /* An existing page in the cache */
1165 Pgno pgno; /* The page number of a page in journal */
1166 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001167 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001168
drh96362842005-03-20 22:47:56 +00001169 /* useCksum should be true for the main journal and false for
1170 ** statement journals. Verify that this is always the case
1171 */
drh9cbe6352005-11-29 03:13:21 +00001172 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001173 assert( aData );
drh96362842005-03-20 22:47:56 +00001174
danielk197762079062007-08-15 17:08:46 +00001175 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001176 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001177 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001178 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001179 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001180
drh968af522003-02-11 14:55:40 +00001181 /* Sanity checking on the page. This is more important that I originally
1182 ** thought. If a power failure occurs while the journal is being written,
1183 ** it could cause invalid data to be written into the journal. We need to
1184 ** detect this invalid data (with high probability) and ignore it.
1185 */
danielk197775edc162004-06-26 01:48:18 +00001186 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001187 return SQLITE_DONE;
1188 }
drhae2b40c2004-06-09 19:03:54 +00001189 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001190 return SQLITE_OK;
1191 }
drhae2b40c2004-06-09 19:03:54 +00001192 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001193 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001194 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001195 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001196 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001197 return SQLITE_DONE;
1198 }
1199 }
drhfa86c412002-02-02 15:01:15 +00001200
danielk1977aa5ccdf2004-06-14 05:10:42 +00001201 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001202
1203 /* If the pager is in RESERVED state, then there must be a copy of this
1204 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001205 ** not the database file. The page is left marked dirty in this case.
1206 **
danielk19772df71c72007-05-24 07:22:42 +00001207 ** An exception to the above rule: If the database is in no-sync mode
1208 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001209 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1210 ** during a Movepage() call, then the page may not be in the cache
1211 ** either. So the condition described in the above paragraph is not
1212 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001213 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001214 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1215 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001216 **
1217 ** Ticket #1171: The statement journal might contain page content that is
1218 ** different from the page content at the start of the transaction.
1219 ** This occurs when a page is changed prior to the start of a statement
1220 ** then changed again within the statement. When rolling back such a
1221 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001222 ** for certain that original page contents are synced into the main rollback
1223 ** journal. Otherwise, a power loss might leave modified data in the
1224 ** database file without an entry in the rollback journal that can
1225 ** restore the database to its original form. Two conditions must be
1226 ** met before writing to the database files. (1) the database must be
1227 ** locked. (2) we know that the original page content is fully synced
1228 ** in the main journal either because the page is not in cache or else
1229 ** the page is marked as needSync==0.
drhfa86c412002-02-02 15:01:15 +00001230 */
drhae2b40c2004-06-09 19:03:54 +00001231 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001232 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1233 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh96362842005-03-20 22:47:56 +00001234 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
danielk197762079062007-08-15 17:08:46 +00001235 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1236 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001237 if( pPg ){
1238 makeClean(pPg);
1239 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001240 }
drhfa86c412002-02-02 15:01:15 +00001241 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001242 /* No page should ever be explicitly rolled back that is in use, except
1243 ** for page 1 which is held in use in order to keep the lock on the
1244 ** database active. However such a page may be rolled back as a result
1245 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001246 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001247 */
drhb6f41482004-05-14 01:58:11 +00001248 void *pData;
danielk197728129562005-01-11 10:25:06 +00001249 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001250 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001251 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001252 if( pPager->xReiniter ){
1253 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001254 }
danielk19773c407372005-02-15 02:54:14 +00001255#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001256 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001257#endif
drh86a88112007-04-16 15:02:19 +00001258 /* If this was page 1, then restore the value of Pager.dbFileVers.
1259 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001260 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001261 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001262 }
drh86a88112007-04-16 15:02:19 +00001263
1264 /* Decode the page just read from disk */
1265 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001266 }
1267 return rc;
1268}
1269
1270/*
danielk197713adf8a2004-06-03 16:08:41 +00001271** Parameter zMaster is the name of a master journal file. A single journal
1272** file that referred to the master journal file has just been rolled back.
1273** This routine checks if it is possible to delete the master journal file,
1274** and does so if it is.
drh726de592004-06-10 23:35:50 +00001275**
1276** The master journal file contains the names of all child journals.
1277** To tell if a master journal can be deleted, check to each of the
1278** children. If all children are either missing or do not refer to
1279** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001280*/
danielk1977b4b47412007-08-17 15:53:36 +00001281static int pager_delmaster(Pager *pPager, const char *zMaster){
1282 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001283 int rc;
1284 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001285 sqlite3_file *pMaster;
1286 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001287 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001288 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001289
1290 /* Open the master journal file exclusively in case some other process
1291 ** is running this routine also. Not that it makes too much difference.
1292 */
danielk1977b4b47412007-08-17 15:53:36 +00001293 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001294 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001295 if( !pMaster ){
1296 rc = SQLITE_NOMEM;
1297 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001298 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1299 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001300 }
danielk197713adf8a2004-06-03 16:08:41 +00001301 if( rc!=SQLITE_OK ) goto delmaster_out;
1302 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001303
1304 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001305 if( rc!=SQLITE_OK ) goto delmaster_out;
1306
1307 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001308 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001309 char *zMasterPtr = 0;
danielk19775865e3d2004-06-14 06:03:57 +00001310
1311 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001312 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001313 */
drh17435752007-08-16 04:30:38 +00001314 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001315 if( !zMasterJournal ){
1316 rc = SQLITE_NOMEM;
1317 goto delmaster_out;
1318 }
danielk1977b4b47412007-08-17 15:53:36 +00001319 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001320 if( rc!=SQLITE_OK ) goto delmaster_out;
1321
danielk19775865e3d2004-06-14 06:03:57 +00001322 zJournal = zMasterJournal;
1323 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977b4b47412007-08-17 15:53:36 +00001324 if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
danielk197713adf8a2004-06-03 16:08:41 +00001325 /* One of the journals pointed to by the master journal exists.
1326 ** Open it and check if it points at the master journal. If
1327 ** so, return without deleting the master journal file.
1328 */
drh3b7b78b2004-11-24 01:16:43 +00001329 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001330 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1331 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001332 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001333 goto delmaster_out;
1334 }
danielk197713adf8a2004-06-03 16:08:41 +00001335
danielk1977b4b47412007-08-17 15:53:36 +00001336 rc = readMasterJournal(pJournal, &zMasterPtr);
1337 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001338 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001339 goto delmaster_out;
1340 }
danielk197776572402004-06-25 02:38:54 +00001341
drh3b7b78b2004-11-24 01:16:43 +00001342 c = zMasterPtr!=0 && strcmp(zMasterPtr, zMaster)==0;
drh17435752007-08-16 04:30:38 +00001343 sqlite3_free(zMasterPtr);
drh3b7b78b2004-11-24 01:16:43 +00001344 if( c ){
danielk197776572402004-06-25 02:38:54 +00001345 /* We have a match. Do not delete the master journal file. */
1346 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001347 }
1348 }
danielk19775865e3d2004-06-14 06:03:57 +00001349 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001350 }
1351 }
1352
danielk1977fee2d252007-08-18 10:59:19 +00001353 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001354
1355delmaster_out:
1356 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001357 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001358 }
1359 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001360 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001361 }
danielk1977b4b47412007-08-17 15:53:36 +00001362 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001363 return rc;
1364}
1365
drha6abd042004-06-09 17:37:22 +00001366
danielk1977e180dd92007-04-05 17:15:52 +00001367static void pager_truncate_cache(Pager *pPager);
1368
drha6abd042004-06-09 17:37:22 +00001369/*
drhcb4c40b2004-08-18 19:09:43 +00001370** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001371** indicated. Also truncate the cached representation of the file.
drhcb4c40b2004-08-18 19:09:43 +00001372*/
1373static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001374 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001375 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
danielk1977e180dd92007-04-05 17:15:52 +00001376 rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
1377 }
1378 if( rc==SQLITE_OK ){
1379 pPager->dbSize = nPage;
1380 pager_truncate_cache(pPager);
1381 }
1382 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001383}
1384
1385/*
drhc80f0582007-05-01 16:59:48 +00001386** Set the sectorSize for the given pager.
1387**
1388** The sector size is the larger of the sector size reported
1389** by sqlite3OsSectorSize() and the pageSize.
1390*/
1391static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001392 assert(pPager->fd->pMethods||pPager->tempFile);
1393 if( !pPager->tempFile ){
1394 /* Sector size doesn't matter for temporary files. Also, the file
1395 ** may not have been opened yet, in whcih case the OsSectorSize()
1396 ** call will segfault.
1397 */
1398 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1399 }
drhc80f0582007-05-01 16:59:48 +00001400 if( pPager->sectorSize<pPager->pageSize ){
1401 pPager->sectorSize = pPager->pageSize;
1402 }
1403}
1404
1405/*
drhed7c8552001-04-11 14:29:21 +00001406** Playback the journal and thus restore the database file to
1407** the state it was in before we started making changes.
1408**
drh34e79ce2004-02-08 06:05:46 +00001409** The journal file format is as follows:
1410**
drhae2b40c2004-06-09 19:03:54 +00001411** (1) 8 byte prefix. A copy of aJournalMagic[].
1412** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001413** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001414** number of page records from the journal size.
1415** (3) 4 byte big-endian integer which is the initial value for the
1416** sanity checksum.
1417** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001418** database to during a rollback.
drhae2b40c2004-06-09 19:03:54 +00001419** (5) 4 byte integer which is the number of bytes in the master journal
1420** name. The value may be zero (indicate that there is no master
1421** journal.)
1422** (6) N bytes of the master journal name. The name will be nul-terminated
1423** and might be shorter than the value read from (5). If the first byte
1424** of the name is \000 then there is no master journal. The master
1425** journal name is stored in UTF-8.
1426** (7) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001427** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001428** + pPager->pageSize bytes of data.
1429** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001430**
drhae2b40c2004-06-09 19:03:54 +00001431** When we speak of the journal header, we mean the first 6 items above.
1432** Each entry in the journal is an instance of the 7th item.
drh34e79ce2004-02-08 06:05:46 +00001433**
1434** Call the value from the second bullet "nRec". nRec is the number of
1435** valid page entries in the journal. In most cases, you can compute the
1436** value of nRec from the size of the journal file. But if a power
1437** failure occurred while the journal was being written, it could be the
1438** case that the size of the journal file had already been increased but
1439** the extra entries had not yet made it safely to disk. In such a case,
1440** the value of nRec computed from the file size would be too large. For
1441** that reason, we always use the nRec value in the header.
1442**
1443** If the nRec value is 0xffffffff it means that nRec should be computed
1444** from the file size. This value is used when the user selects the
1445** no-sync option for the journal. A power failure could lead to corruption
1446** in this case. But for things like temporary table (which will be
1447** deleted when the power is restored) we don't care.
1448**
drhd9b02572001-04-15 00:37:09 +00001449** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001450** journal file then all pages up to the first corrupted page are rolled
1451** back (or no pages if the journal header is corrupted). The journal file
1452** is then deleted and SQLITE_OK returned, just as if no corruption had
1453** been encountered.
1454**
1455** If an I/O or malloc() error occurs, the journal-file is not deleted
1456** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001457*/
danielk1977e277be02007-03-23 18:12:06 +00001458static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001459 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001460 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001461 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001462 int i; /* Loop counter */
1463 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001464 int rc; /* Result code of a subroutine */
danielk197713adf8a2004-06-03 16:08:41 +00001465 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001466
drhc3a64ba2001-11-22 00:01:27 +00001467 /* Figure out how many records are in the journal. Abort early if
1468 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001469 */
drh8cfbf082001-09-19 13:22:39 +00001470 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001471 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001472 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001473 goto end_playback;
1474 }
drh240c5792004-02-08 00:40:52 +00001475
danielk197776572402004-06-25 02:38:54 +00001476 /* Read the master journal name from the journal, if it is present.
1477 ** If a master journal file name is specified, but the file is not
1478 ** present on disk, then the journal is not hot and does not need to be
1479 ** played back.
drh240c5792004-02-08 00:40:52 +00001480 */
drh9cbe6352005-11-29 03:13:21 +00001481 rc = readMasterJournal(pPager->jfd, &zMaster);
danielk197776572402004-06-25 02:38:54 +00001482 assert( rc!=SQLITE_DONE );
danielk1977b4b47412007-08-17 15:53:36 +00001483 if( rc!=SQLITE_OK
1484 || (zMaster && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))
1485 ){
drh17435752007-08-16 04:30:38 +00001486 sqlite3_free(zMaster);
danielk197776572402004-06-25 02:38:54 +00001487 zMaster = 0;
1488 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drhc3a64ba2001-11-22 00:01:27 +00001489 goto end_playback;
1490 }
danielk197776572402004-06-25 02:38:54 +00001491 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001492
danielk197776572402004-06-25 02:38:54 +00001493 /* This loop terminates either when the readJournalHdr() call returns
1494 ** SQLITE_DONE or an IO error occurs. */
1495 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001496
danielk197776572402004-06-25 02:38:54 +00001497 /* Read the next journal header from the journal file. If there are
1498 ** not enough bytes left in the journal file for a complete header, or
1499 ** it is corrupted, then a process must of failed while writing it.
1500 ** This indicates nothing more needs to be rolled back.
1501 */
1502 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1503 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001504 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001505 rc = SQLITE_OK;
1506 }
danielk197776572402004-06-25 02:38:54 +00001507 goto end_playback;
1508 }
1509
1510 /* If nRec is 0xffffffff, then this journal was created by a process
1511 ** working in no-sync mode. This means that the rest of the journal
1512 ** file consists of pages, there are no more journal headers. Compute
1513 ** the value of nRec based on this assumption.
1514 */
1515 if( nRec==0xffffffff ){
1516 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1517 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1518 }
1519
danielk1977e277be02007-03-23 18:12:06 +00001520 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001521 ** process and if this is the final header in the journal, then it means
1522 ** that this part of the journal was being filled but has not yet been
1523 ** synced to disk. Compute the number of pages based on the remaining
1524 ** size of the file.
1525 **
1526 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001527 */
drh8940f4e2007-08-11 00:26:20 +00001528 if( nRec==0 && !isHot &&
1529 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001530 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1531 }
1532
danielk197776572402004-06-25 02:38:54 +00001533 /* If this is the first header read from the journal, truncate the
1534 ** database file back to it's original size.
1535 */
danielk1977e180dd92007-04-05 17:15:52 +00001536 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001537 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001538 if( rc!=SQLITE_OK ){
1539 goto end_playback;
1540 }
danielk197776572402004-06-25 02:38:54 +00001541 }
1542
danielk197776572402004-06-25 02:38:54 +00001543 /* Copy original pages out of the journal and back into the database file.
1544 */
1545 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001546 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001547 if( rc!=SQLITE_OK ){
1548 if( rc==SQLITE_DONE ){
1549 rc = SQLITE_OK;
1550 pPager->journalOff = szJ;
1551 break;
1552 }else{
1553 goto end_playback;
1554 }
1555 }
drh968af522003-02-11 14:55:40 +00001556 }
drhed7c8552001-04-11 14:29:21 +00001557 }
drh580eeaf2006-02-24 03:09:37 +00001558 /*NOTREACHED*/
1559 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001560
1561end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001562 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001563 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001564 }
danielk197713adf8a2004-06-03 16:08:41 +00001565 if( zMaster ){
danielk1977979f38e2007-03-27 16:19:51 +00001566 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001567 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001568 */
1569 if( rc==SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001570 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001571 }
drh17435752007-08-16 04:30:38 +00001572 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001573 }
danielk197776572402004-06-25 02:38:54 +00001574
1575 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001576 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001577 ** value. Reset it to the correct value for this process.
1578 */
drhc80f0582007-05-01 16:59:48 +00001579 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001580 return rc;
drhed7c8552001-04-11 14:29:21 +00001581}
1582
1583/*
drhac69b052004-05-12 13:30:07 +00001584** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001585**
1586** This is similar to playing back the transaction journal but with
1587** a few extra twists.
1588**
drh663fc632002-02-02 18:49:19 +00001589** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001590** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001591** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001592**
drhac69b052004-05-12 13:30:07 +00001593** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001594** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001595** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001596*/
drh3aac2dd2004-04-26 14:10:20 +00001597static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001598 i64 szJ; /* Size of the full journal */
1599 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001600 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001601 int i; /* Loop counter */
1602 int rc;
1603
danielk197776572402004-06-25 02:38:54 +00001604 szJ = pPager->journalOff;
1605#ifndef NDEBUG
1606 {
drheb206252004-10-01 02:00:31 +00001607 i64 os_szJ;
drh054889e2005-11-30 03:20:31 +00001608 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
danielk197776572402004-06-25 02:38:54 +00001609 if( rc!=SQLITE_OK ) return rc;
1610 assert( szJ==os_szJ );
1611 }
1612#endif
1613
danielk19774099f6e2007-03-19 11:25:20 +00001614 /* Set hdrOff to be the offset just after the end of the last journal
1615 ** page written before the first journal-header for this statement
1616 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001617 ** header was written.
1618 */
1619 hdrOff = pPager->stmtHdrOff;
1620 assert( pPager->fullSync || !hdrOff );
1621 if( !hdrOff ){
1622 hdrOff = szJ;
1623 }
1624
drhfa86c412002-02-02 15:01:15 +00001625 /* Truncate the database back to its original size.
1626 */
danielk1977e180dd92007-04-05 17:15:52 +00001627 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001628 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001629
drhac69b052004-05-12 13:30:07 +00001630 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001631 */
drhac69b052004-05-12 13:30:07 +00001632 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001633 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001634
drhac69b052004-05-12 13:30:07 +00001635 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001636 ** database file. Note that the statement journal omits checksums from
1637 ** each record since power-failure recovery is not important to statement
1638 ** journals.
drhfa86c412002-02-02 15:01:15 +00001639 */
danielk197762079062007-08-15 17:08:46 +00001640 for(i=0; i<nRec; i++){
1641 i64 offset = i*(4+pPager->pageSize);
1642 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001643 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001644 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001645 }
1646
danielk197776572402004-06-25 02:38:54 +00001647 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1648 ** was the size of the journal file when this statement was started, so
1649 ** everything after that needs to be rolled back, either into the
1650 ** database, the memory cache, or both.
1651 **
1652 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1653 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001654 */
danielk197776572402004-06-25 02:38:54 +00001655 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001656 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001657 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001658 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001659 assert( rc!=SQLITE_DONE );
1660 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1661 }
1662
1663 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001664 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001665 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001666 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001667 if( rc!=SQLITE_OK ){
1668 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001669 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001670 }
danielk1977f0113002006-01-24 12:09:17 +00001671 if( nJRec==0 ){
1672 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001673 }
danielk1977f0113002006-01-24 12:09:17 +00001674 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001675 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001676 assert( rc!=SQLITE_DONE );
1677 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1678 }
drhfa86c412002-02-02 15:01:15 +00001679 }
danielk197776572402004-06-25 02:38:54 +00001680
1681 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001682
drh3aac2dd2004-04-26 14:10:20 +00001683end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001684 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001685 pPager->journalOff = szJ;
1686 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001687 }
1688 return rc;
1689}
1690
1691/*
drhf57b14a2001-09-14 18:54:08 +00001692** Change the maximum number of in-memory pages that are allowed.
1693*/
danielk19773b8a05f2007-03-19 17:44:26 +00001694void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00001695 if( mxPage>10 ){
1696 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00001697 }else{
1698 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00001699 }
1700}
1701
1702/*
drh973b6e32003-02-12 14:09:42 +00001703** Adjust the robustness of the database to damage due to OS crashes
1704** or power failures by changing the number of syncs()s when writing
1705** the rollback journal. There are three levels:
1706**
drh054889e2005-11-30 03:20:31 +00001707** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001708** for temporary and transient files.
1709**
1710** NORMAL The journal is synced once before writes begin on the
1711** database. This is normally adequate protection, but
1712** it is theoretically possible, though very unlikely,
1713** that an inopertune power failure could leave the journal
1714** in a state which would cause damage to the database
1715** when it is rolled back.
1716**
1717** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001718** database (with some additional information - the nRec field
1719** of the journal header - being written in between the two
1720** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001721** single disk sector is atomic, then this mode provides
1722** assurance that the journal will not be corrupted to the
1723** point of causing damage to the database during rollback.
1724**
1725** Numeric values associated with these states are OFF==1, NORMAL=2,
1726** and FULL=3.
1727*/
danielk197793758c82005-01-21 08:13:14 +00001728#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00001729void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00001730 pPager->noSync = level==1 || pPager->tempFile;
1731 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00001732 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001733 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001734}
danielk197793758c82005-01-21 08:13:14 +00001735#endif
drh973b6e32003-02-12 14:09:42 +00001736
1737/*
drhaf6df112005-06-07 02:12:30 +00001738** The following global variable is incremented whenever the library
1739** attempts to open a temporary file. This information is used for
1740** testing and analysis only.
1741*/
drh0f7eb612006-08-08 13:51:43 +00001742#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001743int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001744#endif
drhaf6df112005-06-07 02:12:30 +00001745
1746/*
drh3f56e6e2007-03-15 01:16:47 +00001747** Open a temporary file.
1748**
1749** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001750** other error code if we fail. The OS will automatically delete the temporary
1751** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001752**
danielk1977fee2d252007-08-18 10:59:19 +00001753** If zNameOut is 0, then SQLITE_OPEN_SUBJOURNAL is passed to the OS layer.
1754** If zNameOut is not 0, SQLITE_OPEN_TEMP_DB is passed.
drhfa86c412002-02-02 15:01:15 +00001755*/
danielk1977b4b47412007-08-17 15:53:36 +00001756static int sqlite3PagerOpentemp(
1757 sqlite3_vfs *pVfs,
1758 sqlite3_file *pFile,
1759 char *zNameOut
1760){
drhfa86c412002-02-02 15:01:15 +00001761 int cnt = 8;
1762 int rc;
danielk1977b4b47412007-08-17 15:53:36 +00001763 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_EXCLUSIVE);
1764
1765 char *zFree = 0;
1766 if( zNameOut==0 ){
1767 zFree = (char *)sqlite3_malloc(pVfs->mxPathname);
1768 if( !zFree ){
1769 return SQLITE_NOMEM;
1770 }
1771 zNameOut = zFree;
danielk1977fee2d252007-08-18 10:59:19 +00001772 flags |= SQLITE_OPEN_SUBJOURNAL;
1773 }else{
1774 flags |= SQLITE_OPEN_TEMP_DB;
danielk1977b4b47412007-08-17 15:53:36 +00001775 }
drh3f56e6e2007-03-15 01:16:47 +00001776
drh0f7eb612006-08-08 13:51:43 +00001777#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001778 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001779#endif
danielk1977b4b47412007-08-17 15:53:36 +00001780
drhfa86c412002-02-02 15:01:15 +00001781 do{
1782 cnt--;
danielk1977b4b47412007-08-17 15:53:36 +00001783 sqlite3OsGetTempName(pVfs, zNameOut);
1784 rc = sqlite3OsOpen(pVfs, zNameOut, pFile, flags, 0);
1785 assert( rc!=SQLITE_OK || pFile->pMethods );
danielk197713073932004-06-30 11:54:06 +00001786 }while( cnt>0 && rc!=SQLITE_OK && rc!=SQLITE_NOMEM );
danielk1977b4b47412007-08-17 15:53:36 +00001787
1788 sqlite3_free(zFree);
drhfa86c412002-02-02 15:01:15 +00001789 return rc;
1790}
1791
1792/*
drhed7c8552001-04-11 14:29:21 +00001793** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001794** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001795** the first call to sqlite3PagerGet() and is only held open until the
1796** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001797**
drh6446c4d2001-12-15 14:22:18 +00001798** If zFilename is NULL then a randomly-named temporary file is created
1799** and used as the file to be cached. The file will be deleted
1800** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001801**
1802** If zFilename is ":memory:" then all information is held in cache.
1803** It is never written to disk. This can be used to implement an
1804** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001805*/
danielk19773b8a05f2007-03-19 17:44:26 +00001806int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001807 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001808 Pager **ppPager, /* Return the Pager structure here */
1809 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001810 int nExtra, /* Extra bytes append to each in-memory page */
drh7bec5052005-02-06 02:45:41 +00001811 int flags /* flags controlling this file */
drh7e3b0a02001-04-28 16:52:40 +00001812){
danielk1977b4b47412007-08-17 15:53:36 +00001813 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001814 Pager *pPager = 0;
danielk19778def5ea2004-06-16 10:39:52 +00001815 char *zFullPathname = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001816 int rc = SQLITE_OK;
1817 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001818 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001819 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001820 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001821 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1822 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977b4b47412007-08-17 15:53:36 +00001823
drh86f8c192007-08-22 00:39:19 +00001824 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001825 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001826
danielk1977b4b47412007-08-17 15:53:36 +00001827 /* Allocate memory for the pager structure */
1828 pPager = sqlite3MallocZero(
1829 sizeof(*pPager) + /* Pager structure */
1830 pVfs->szOsFile * 3 + /* The db, journal and stmt journal files */
1831 pVfs->mxPathname * 3 + 30 /* zFilename, zDirectory, zJournal */
1832 );
1833 if( !pPager ){
1834 return SQLITE_NOMEM;
1835 }
1836 pPtr = (u8 *)&pPager[1];
1837 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
1838 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
1839 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
1840 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*3];
1841 pPager->zDirectory = &pPager->zFilename[pVfs->mxPathname];
1842 pPager->zJournal = &pPager->zDirectory[pVfs->mxPathname];
1843 pPager->pVfs = pVfs;
1844
danielk1977aef0bf62005-12-30 16:28:01 +00001845 /* Open the pager file and set zFullPathname to point at malloc()ed
1846 ** memory containing the complete filename (i.e. including the directory).
1847 */
drh901afd42003-08-26 11:25:58 +00001848 if( zFilename && zFilename[0] ){
drhb7f91642004-10-31 02:22:47 +00001849#ifndef SQLITE_OMIT_MEMORYDB
drhac69b052004-05-12 13:30:07 +00001850 if( strcmp(zFilename,":memory:")==0 ){
1851 memDb = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001852 pPager->zFilename[0] = '\0';
drhb7f91642004-10-31 02:22:47 +00001853 }else
1854#endif
1855 {
danielk1977b4b47412007-08-17 15:53:36 +00001856 rc = sqlite3OsFullPathname(pVfs, zFilename, pPager->zFilename);
1857 if( rc==SQLITE_OK ){
1858 if( strlen(pPager->zFilename)>(pVfs->mxPathname - strlen("-journal")) ){
1859 rc = SQLITE_CANTOPEN;
1860 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001861 int oflag =
1862 (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB);
danielk1977b4b47412007-08-17 15:53:36 +00001863 int fout = 0;
danielk1977fee2d252007-08-18 10:59:19 +00001864 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, oflag, &fout);
danielk1977b4b47412007-08-17 15:53:36 +00001865 readOnly = (fout&SQLITE_OPEN_READONLY);
1866 }
danielk19778def5ea2004-06-16 10:39:52 +00001867 }
drhac69b052004-05-12 13:30:07 +00001868 }
drh5e00f6c2001-09-13 13:46:56 +00001869 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00001870 /* If a temporary file is requested, it is not opened immediately.
1871 ** In this case we accept the default page size and delay actually
1872 ** opening the file until the first call to OsWrite().
1873 */
1874 /* rc = sqlite3PagerOpentemp(pVfs, pPager->fd, pPager->zFilename); */
1875 tempFile = 1;
1876 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00001877 }
danielk1977aef0bf62005-12-30 16:28:01 +00001878
danielk1977b4b47412007-08-17 15:53:36 +00001879 if( pPager && rc==SQLITE_OK ){
1880 pPager->pTmpSpace = (char *)sqlite3_malloc(SQLITE_DEFAULT_PAGE_SIZE);
drh3e7a6092002-12-07 21:45:14 +00001881 }
danielk1977aef0bf62005-12-30 16:28:01 +00001882
1883 /* If an error occured in either of the blocks above, free the memory
1884 ** pointed to by zFullPathname, free the Pager structure and close the
1885 ** file. Since the pager is not allocated there is no need to set
1886 ** any Pager.errMask variables.
1887 */
danielk1977b4b47412007-08-17 15:53:36 +00001888 if( !pPager || !pPager->pTmpSpace ){
1889 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00001890 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00001891 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001892 }
danielk1977aef0bf62005-12-30 16:28:01 +00001893
drh4f0c5872007-03-26 22:05:01 +00001894 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(fd), zFullPathname);
drhb0603412007-02-28 04:47:26 +00001895 IOTRACE(("OPEN %p %s\n", pPager, zFullPathname))
danielk1977aef0bf62005-12-30 16:28:01 +00001896
danielk1977b4b47412007-08-17 15:53:36 +00001897 /* Fill in Pager.zDirectory[] */
1898 memcpy(pPager->zDirectory, pPager->zFilename, pVfs->mxPathname);
1899 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00001900 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001901
1902 /* Fill in Pager.zJournal[] */
1903 memcpy(pPager->zJournal, pPager->zFilename, pVfs->mxPathname);
1904 memcpy(&pPager->zJournal[strlen(pPager->zJournal)], "-journal", 9);
1905
drh3b59a5c2006-01-15 20:28:28 +00001906 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00001907 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00001908 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001909 /* pPager->stmtOpen = 0; */
1910 /* pPager->stmtInUse = 0; */
1911 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00001912 pPager->dbSize = memDb-1;
drh90f5ecb2004-07-22 01:19:35 +00001913 pPager->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
drh3b59a5c2006-01-15 20:28:28 +00001914 /* pPager->stmtSize = 0; */
1915 /* pPager->stmtJSize = 0; */
1916 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00001917 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00001918 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00001919 assert( PAGER_UNLOCK==0 );
1920 /* pPager->state = PAGER_UNLOCK; */
1921 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00001922 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00001923 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
1924 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
1925 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
1926 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00001927 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001928 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001929 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00001930 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00001931 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00001932 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00001933 /* pPager->pFirst = 0; */
1934 /* pPager->pFirstSynced = 0; */
1935 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00001936 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00001937 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00001938 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00001939 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00001940 }
drh3b59a5c2006-01-15 20:28:28 +00001941 /* pPager->pBusyHandler = 0; */
1942 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00001943 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00001944#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00001945 pPager->iInUseMM = 0;
1946 pPager->iInUseDB = 0;
1947 if( !memDb ){
1948 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
1949 sqlite3_mutex_enter(mutex);
1950 pPager->pNext = sqlite3PagerList;
1951 if( sqlite3PagerList ){
1952 assert( sqlite3PagerList->pPrev==0 );
1953 sqlite3PagerList->pPrev = pPager;
1954 }
1955 pPager->pPrev = 0;
1956 sqlite3PagerList = pPager;
1957 sqlite3_mutex_leave(mutex);
1958 }
danielk197752622822006-01-09 09:59:49 +00001959#endif
drhed7c8552001-04-11 14:29:21 +00001960 return SQLITE_OK;
1961}
1962
1963/*
drh90f5ecb2004-07-22 01:19:35 +00001964** Set the busy handler function.
1965*/
danielk19773b8a05f2007-03-19 17:44:26 +00001966void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00001967 pPager->pBusyHandler = pBusyHandler;
1968}
1969
1970/*
drh72f82862001-05-24 21:06:34 +00001971** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00001972** when the reference count on each page reaches zero. The destructor can
1973** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00001974**
danielk19773b8a05f2007-03-19 17:44:26 +00001975** The destructor is not called as a result sqlite3PagerClose().
1976** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00001977*/
danielk19773b8a05f2007-03-19 17:44:26 +00001978void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00001979 pPager->xDestructor = xDesc;
1980}
1981
1982/*
drha6abd042004-06-09 17:37:22 +00001983** Set the reinitializer for this pager. If not NULL, the reinitializer
1984** is called when the content of a page in cache is restored to its original
1985** value as a result of a rollback. The callback gives higher-level code
1986** an opportunity to restore the EXTRA section to agree with the restored
1987** page data.
1988*/
danielk19773b8a05f2007-03-19 17:44:26 +00001989void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00001990 pPager->xReiniter = xReinit;
1991}
1992
1993/*
drh1c7880e2005-05-20 20:01:55 +00001994** Set the page size. Return the new size. If the suggest new page
1995** size is inappropriate, then an alternative page size is selected
1996** and returned.
drh90f5ecb2004-07-22 01:19:35 +00001997*/
danielk19773b8a05f2007-03-19 17:44:26 +00001998int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){
drh90f5ecb2004-07-22 01:19:35 +00001999 assert( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE );
danielk1977c7c7e622007-03-26 15:46:00 +00002000 if( !pPager->memDb && pPager->nRef==0 ){
drh86f8c192007-08-22 00:39:19 +00002001 pagerEnter(pPager);
danielk1977c7c7e622007-03-26 15:46:00 +00002002 pager_reset(pPager);
drh1c7880e2005-05-20 20:01:55 +00002003 pPager->pageSize = pageSize;
drh86f8c192007-08-22 00:39:19 +00002004 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00002005 sqlite3_free(pPager->pTmpSpace);
2006 pPager->pTmpSpace = sqlite3_malloc(pageSize);
drh1c7880e2005-05-20 20:01:55 +00002007 }
2008 return pPager->pageSize;
drh90f5ecb2004-07-22 01:19:35 +00002009}
2010
2011/*
drhf8e632b2007-05-08 14:51:36 +00002012** Attempt to set the maximum database page count if mxPage is positive.
2013** Make no changes if mxPage is zero or negative. And never reduce the
2014** maximum page count below the current size of the database.
2015**
2016** Regardless of mxPage, return the current maximum page count.
2017*/
2018int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2019 if( mxPage>0 ){
2020 pPager->mxPgno = mxPage;
2021 }
2022 sqlite3PagerPagecount(pPager);
2023 return pPager->mxPgno;
2024}
2025
2026/*
drhc9ac5ca2005-11-04 22:03:30 +00002027** The following set of routines are used to disable the simulated
2028** I/O error mechanism. These routines are used to avoid simulated
2029** errors in places where we do not care about errors.
2030**
2031** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2032** and generate no code.
2033*/
2034#ifdef SQLITE_TEST
2035extern int sqlite3_io_error_pending;
2036extern int sqlite3_io_error_hit;
2037static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002038void disable_simulated_io_errors(void){
2039 saved_cnt = sqlite3_io_error_pending;
2040 sqlite3_io_error_pending = -1;
2041}
2042void enable_simulated_io_errors(void){
2043 sqlite3_io_error_pending = saved_cnt;
2044}
2045#else
drh152410f2005-11-05 15:11:22 +00002046# define disable_simulated_io_errors()
2047# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002048#endif
2049
2050/*
drh90f5ecb2004-07-22 01:19:35 +00002051** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002052** that pDest points to.
2053**
2054** No error checking is done. The rational for this is that this function
2055** may be called even if the file does not exist or contain a header. In
2056** these cases sqlite3OsRead() will return an error, to which the correct
2057** response is to zero the memory at pDest and continue. A real IO error
2058** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002059*/
danielk19773b8a05f2007-03-19 17:44:26 +00002060int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002061 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002062 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002063 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2064 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002065 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002066 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002067 if( rc==SQLITE_IOERR_SHORT_READ ){
2068 rc = SQLITE_OK;
2069 }
drh90f5ecb2004-07-22 01:19:35 +00002070 }
drh551b7732006-11-06 21:20:25 +00002071 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002072}
2073
2074/*
drh5e00f6c2001-09-13 13:46:56 +00002075** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002076** pPager.
2077**
2078** If the PENDING_BYTE lies on the page directly after the end of the
2079** file, then consider this page part of the file too. For example, if
2080** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2081** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002082*/
danielk19773b8a05f2007-03-19 17:44:26 +00002083int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002084 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002085 int rc;
drhd9b02572001-04-15 00:37:09 +00002086 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002087 if( pPager->errCode ){
2088 return 0;
2089 }
drhed7c8552001-04-11 14:29:21 +00002090 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002091 n = pPager->dbSize;
2092 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002093 assert(pPager->fd->pMethods||pPager->tempFile);
2094 if( (pPager->fd->pMethods)
2095 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002096 pager_error(pPager, rc);
danielk197715f411d2005-09-16 10:18:45 +00002097 return 0;
2098 }
2099 if( n>0 && n<pPager->pageSize ){
2100 n = 1;
2101 }else{
2102 n /= pPager->pageSize;
2103 }
2104 if( pPager->state!=PAGER_UNLOCK ){
2105 pPager->dbSize = n;
2106 }
drhed7c8552001-04-11 14:29:21 +00002107 }
danielk197715f411d2005-09-16 10:18:45 +00002108 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002109 n++;
2110 }
drhf8e632b2007-05-08 14:51:36 +00002111 if( n>pPager->mxPgno ){
2112 pPager->mxPgno = n;
2113 }
drhed7c8552001-04-11 14:29:21 +00002114 return n;
2115}
2116
drhf07e7d52006-04-07 13:54:46 +00002117
2118#ifndef SQLITE_OMIT_MEMORYDB
2119/*
2120** Clear a PgHistory block
2121*/
2122static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002123 sqlite3_free(pHist->pOrig);
2124 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002125 pHist->pOrig = 0;
2126 pHist->pStmt = 0;
2127}
2128#else
2129#define clearHistory(x)
2130#endif
2131
drhed7c8552001-04-11 14:29:21 +00002132/*
drhf7c57532003-04-25 13:22:51 +00002133** Forward declaration
2134*/
danielk197776572402004-06-25 02:38:54 +00002135static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002136
2137/*
danielk1977f5fdda82004-11-03 08:44:05 +00002138** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate
2139** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002140** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002141** pNextFree/pPrevFree list that is not a part of any hash-chain.
2142*/
2143static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2144 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002145 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002146 return;
2147 }
2148 if( pPg->pNextHash ){
2149 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2150 }
2151 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002152 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002153 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2154 }else{
drh8ca0c722006-05-07 17:49:38 +00002155 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002156 pPager->aHash[h] = pPg->pNextHash;
2157 }
drh5229ae42006-03-23 23:29:04 +00002158 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002159 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2160 }
danielk1977f5fdda82004-11-03 08:44:05 +00002161 pPg->pgno = 0;
2162 pPg->pNextHash = pPg->pPrevHash = 0;
2163}
2164
2165/*
drhac69b052004-05-12 13:30:07 +00002166** Unlink a page from the free list (the list of all pages where nRef==0)
2167** and from its hash collision chain.
2168*/
2169static void unlinkPage(PgHdr *pPg){
2170 Pager *pPager = pPg->pPager;
2171
2172 /* Keep the pFirstSynced pointer pointing at the first synchronized page */
2173 if( pPg==pPager->pFirstSynced ){
2174 PgHdr *p = pPg->pNextFree;
2175 while( p && p->needSync ){ p = p->pNextFree; }
2176 pPager->pFirstSynced = p;
2177 }
2178
2179 /* Unlink from the freelist */
2180 if( pPg->pPrevFree ){
2181 pPg->pPrevFree->pNextFree = pPg->pNextFree;
2182 }else{
2183 assert( pPager->pFirst==pPg );
2184 pPager->pFirst = pPg->pNextFree;
2185 }
2186 if( pPg->pNextFree ){
2187 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
2188 }else{
2189 assert( pPager->pLast==pPg );
2190 pPager->pLast = pPg->pPrevFree;
2191 }
2192 pPg->pNextFree = pPg->pPrevFree = 0;
2193
2194 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002195 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002196}
2197
2198/*
danielk1977e180dd92007-04-05 17:15:52 +00002199** This routine is used to truncate the cache when a database
2200** is truncated. Drop from the cache all pages whose pgno is
2201** larger than pPager->dbSize and is unreferenced.
2202**
drhac69b052004-05-12 13:30:07 +00002203** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002204**
2205** Actually, at the point this routine is called, it would be
2206** an error to have a referenced page. But rather than delete
2207** that page and guarantee a subsequent segfault, it seems better
2208** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002209*/
danielk1977e180dd92007-04-05 17:15:52 +00002210static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002211 PgHdr *pPg;
2212 PgHdr **ppPg;
2213 int dbSize = pPager->dbSize;
2214
2215 ppPg = &pPager->pAll;
2216 while( (pPg = *ppPg)!=0 ){
2217 if( pPg->pgno<=dbSize ){
2218 ppPg = &pPg->pNextAll;
2219 }else if( pPg->nRef>0 ){
2220 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2221 ppPg = &pPg->pNextAll;
2222 }else{
2223 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002224 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2225 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002226 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002227 makeClean(pPg);
drh17435752007-08-16 04:30:38 +00002228 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002229 pPager->nPage--;
2230 }
2231 }
2232}
2233
drhf7c57532003-04-25 13:22:51 +00002234/*
danielk197717221812005-02-15 03:38:05 +00002235** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002236** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002237** false or until the lock succeeds.
2238**
2239** Return SQLITE_OK on success and an error code if we cannot obtain
2240** the lock.
2241*/
2242static int pager_wait_on_lock(Pager *pPager, int locktype){
2243 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002244
2245 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002246 assert( PAGER_SHARED==SHARED_LOCK );
2247 assert( PAGER_RESERVED==RESERVED_LOCK );
2248 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002249
2250 /* If the file is currently unlocked then the size must be unknown */
2251 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2252
danielk197717221812005-02-15 03:38:05 +00002253 if( pPager->state>=locktype ){
2254 rc = SQLITE_OK;
2255 }else{
danielk197717221812005-02-15 03:38:05 +00002256 do {
drh054889e2005-11-30 03:20:31 +00002257 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002258 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002259 if( rc==SQLITE_OK ){
2260 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002261 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002262 }
2263 }
2264 return rc;
2265}
2266
2267/*
drhf7c57532003-04-25 13:22:51 +00002268** Truncate the file to the number of pages specified.
2269*/
danielk19773b8a05f2007-03-19 17:44:26 +00002270int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002271 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002272 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002273 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002274 if( pPager->errCode ){
2275 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002276 return rc;
2277 }
drh7d02cb72003-06-04 16:24:39 +00002278 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002279 return SQLITE_OK;
2280 }
drhb7f91642004-10-31 02:22:47 +00002281 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002282 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002283 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002284 return SQLITE_OK;
2285 }
drh86f8c192007-08-22 00:39:19 +00002286 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002287 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002288 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002289 if( rc!=SQLITE_OK ){
2290 return rc;
2291 }
danielk197717221812005-02-15 03:38:05 +00002292
2293 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002294 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002295 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002296 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002297 if( rc!=SQLITE_OK ){
2298 return rc;
2299 }
2300
drhcb4c40b2004-08-18 19:09:43 +00002301 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002302 return rc;
2303}
2304
2305/*
drhed7c8552001-04-11 14:29:21 +00002306** Shutdown the page cache. Free all memory and close all files.
2307**
2308** If a transaction was in progress when this routine is called, that
2309** transaction is rolled back. All outstanding pages are invalidated
2310** and their memory is freed. Any attempt to use a page associated
2311** with this page cache after this function returns will likely
2312** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002313**
2314** This function always succeeds. If a transaction is active an attempt
2315** is made to roll it back. If an error occurs during the rollback
2316** a hot journal may be left in the filesystem but no error is returned
2317** to the caller.
drhed7c8552001-04-11 14:29:21 +00002318*/
danielk19773b8a05f2007-03-19 17:44:26 +00002319int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002320#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002321 if( !MEMDB ){
2322 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2323 sqlite3_mutex_enter(mutex);
2324 if( pPager->pPrev ){
2325 pPager->pPrev->pNext = pPager->pNext;
2326 }else{
2327 sqlite3PagerList = pPager->pNext;
2328 }
2329 if( pPager->pNext ){
2330 pPager->pNext->pPrev = pPager->pPrev;
2331 }
2332 sqlite3_mutex_leave(mutex);
2333 }
danielk197713f72992005-12-18 08:51:22 +00002334#endif
2335
drhbafda092007-01-03 23:36:22 +00002336 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002337 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002338 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002339 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002340 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002341 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002342 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002343 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002344 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002345 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002346 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002347 }
drh17435752007-08-16 04:30:38 +00002348 sqlite3_free(pPager->aInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002349 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002350 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002351 }
danielk1977b4b47412007-08-17 15:53:36 +00002352 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002353 /* Temp files are automatically deleted by the OS
2354 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002355 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002356 ** }
2357 */
danielk1977aca790a2005-01-13 11:07:52 +00002358
drh17435752007-08-16 04:30:38 +00002359 sqlite3_free(pPager->aHash);
2360 sqlite3_free(pPager->pTmpSpace);
2361 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002362 return SQLITE_OK;
2363}
2364
drh87cc3b32007-05-08 21:45:27 +00002365#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002366/*
drh5e00f6c2001-09-13 13:46:56 +00002367** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002368*/
danielk19773b8a05f2007-03-19 17:44:26 +00002369Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002370 return p->pgno;
2371}
drh87cc3b32007-05-08 21:45:27 +00002372#endif
drhed7c8552001-04-11 14:29:21 +00002373
2374/*
drhc8629a12004-05-08 20:07:40 +00002375** The page_ref() function increments the reference count for a page.
2376** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002377** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002378**
2379** For non-test systems, page_ref() is a macro that calls _page_ref()
2380** online of the reference count is zero. For test systems, page_ref()
2381** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002382*/
drh836faa42003-01-11 13:30:57 +00002383static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002384 if( pPg->nRef==0 ){
2385 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +00002386 if( pPg==pPg->pPager->pFirstSynced ){
2387 PgHdr *p = pPg->pNextFree;
2388 while( p && p->needSync ){ p = p->pNextFree; }
2389 pPg->pPager->pFirstSynced = p;
2390 }
drh7e3b0a02001-04-28 16:52:40 +00002391 if( pPg->pPrevFree ){
2392 pPg->pPrevFree->pNextFree = pPg->pNextFree;
2393 }else{
2394 pPg->pPager->pFirst = pPg->pNextFree;
2395 }
2396 if( pPg->pNextFree ){
2397 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
2398 }else{
2399 pPg->pPager->pLast = pPg->pPrevFree;
2400 }
2401 pPg->pPager->nRef++;
2402 }
2403 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00002404 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00002405}
danielk1977aca790a2005-01-13 11:07:52 +00002406#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002407 static void page_ref(PgHdr *pPg){
2408 if( pPg->nRef==0 ){
2409 _page_ref(pPg);
2410 }else{
2411 pPg->nRef++;
2412 REFINFO(pPg);
2413 }
2414 }
2415#else
2416# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2417#endif
drhdf0b3b02001-06-23 11:36:20 +00002418
2419/*
2420** Increment the reference count for a page. The input pointer is
2421** a reference to the page data.
2422*/
danielk19773b8a05f2007-03-19 17:44:26 +00002423int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002424 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002425 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002426 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002427 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002428}
2429
2430/*
drh34e79ce2004-02-08 06:05:46 +00002431** Sync the journal. In other words, make sure all the pages that have
2432** been written to the journal have actually reached the surface of the
2433** disk. It is not safe to modify the original database file until after
2434** the journal has been synced. If the original database is modified before
2435** the journal is synced and a power failure occurs, the unsynced journal
2436** data would be lost and we would be unable to completely rollback the
2437** database changes. Database corruption would occur.
2438**
2439** This routine also updates the nRec field in the header of the journal.
2440** (See comments on the pager_playback() routine for additional information.)
2441** If the sync mode is FULL, two syncs will occur. First the whole journal
2442** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002443**
drh34e79ce2004-02-08 06:05:46 +00002444** For temporary databases, we do not care if we are able to rollback
2445** after a power failure, so sync occurs.
drhfa86c412002-02-02 15:01:15 +00002446**
drh34e79ce2004-02-08 06:05:46 +00002447** This routine clears the needSync field of every page current held in
2448** memory.
drh50e5dad2001-09-15 00:57:28 +00002449*/
danielk197776572402004-06-25 02:38:54 +00002450static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002451 PgHdr *pPg;
2452 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002453
2454 /* Sync the journal before modifying the main database
2455 ** (assuming there is a journal and it needs to be synced.)
2456 */
danielk197776572402004-06-25 02:38:54 +00002457 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002458 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00002459 assert( pPager->journalOpen );
drh946966f2004-02-25 02:20:41 +00002460 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2461 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00002462#ifndef NDEBUG
2463 {
drh34e79ce2004-02-08 06:05:46 +00002464 /* Make sure the pPager->nRec counter we are keeping agrees
2465 ** with the nRec computed from the size of the journal file.
2466 */
drheb206252004-10-01 02:00:31 +00002467 i64 jSz;
drh054889e2005-11-30 03:20:31 +00002468 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00002469 if( rc!=0 ) return rc;
danielk197776572402004-06-25 02:38:54 +00002470 assert( pPager->journalOff==jSz );
drh968af522003-02-11 14:55:40 +00002471 }
2472#endif
drhae2b40c2004-06-09 19:03:54 +00002473 {
danielk197762079062007-08-15 17:08:46 +00002474 i64 jrnlOff;
2475
danielk197776572402004-06-25 02:38:54 +00002476 /* Write the nRec value into the journal file header. If in
2477 ** full-synchronous mode, sync the journal first. This ensures that
2478 ** all data has really hit the disk before nRec is updated to mark
2479 ** it as a candidate for rollback.
2480 */
drhd8d66e82003-02-12 02:10:15 +00002481 if( pPager->fullSync ){
drh4f0c5872007-03-26 22:05:01 +00002482 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002483 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002484 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002485 if( rc!=0 ) return rc;
2486 }
danielk197713adf8a2004-06-03 16:08:41 +00002487
danielk197762079062007-08-15 17:08:46 +00002488 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2489 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2490 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002491 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002492 }
drh4f0c5872007-03-26 22:05:01 +00002493 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drh126afe62007-05-04 12:01:02 +00002494 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002495 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2496 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2497 );
drhfa86c412002-02-02 15:01:15 +00002498 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00002499 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002500 }
drh50e5dad2001-09-15 00:57:28 +00002501 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002502
2503 /* Erase the needSync flag from every page.
2504 */
2505 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2506 pPg->needSync = 0;
2507 }
2508 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00002509 }
drh03eb96a2002-11-10 23:32:56 +00002510
drh341eae82003-01-21 02:39:36 +00002511#ifndef NDEBUG
2512 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2513 ** flag must also be clear for all pages. Verify that this
2514 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002515 */
drh341eae82003-01-21 02:39:36 +00002516 else{
2517 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2518 assert( pPg->needSync==0 );
2519 }
2520 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00002521 }
drh341eae82003-01-21 02:39:36 +00002522#endif
drhdb48ee02003-01-16 13:42:43 +00002523
drh81a20f22001-10-12 17:30:04 +00002524 return rc;
drh50e5dad2001-09-15 00:57:28 +00002525}
2526
2527/*
drhdc3e10b2006-06-15 14:31:06 +00002528** Merge two lists of pages connected by pDirty and in pgno order.
2529** Do not both fixing the pPrevDirty pointers.
2530*/
2531static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2532 PgHdr result, *pTail;
2533 pTail = &result;
2534 while( pA && pB ){
2535 if( pA->pgno<pB->pgno ){
2536 pTail->pDirty = pA;
2537 pTail = pA;
2538 pA = pA->pDirty;
2539 }else{
2540 pTail->pDirty = pB;
2541 pTail = pB;
2542 pB = pB->pDirty;
2543 }
2544 }
2545 if( pA ){
2546 pTail->pDirty = pA;
2547 }else if( pB ){
2548 pTail->pDirty = pB;
2549 }else{
2550 pTail->pDirty = 0;
2551 }
2552 return result.pDirty;
2553}
2554
2555/*
2556** Sort the list of pages in accending order by pgno. Pages are
2557** connected by pDirty pointers. The pPrevDirty pointers are
2558** corrupted by this sort.
2559*/
danielk197724168722007-04-02 05:07:47 +00002560#define N_SORT_BUCKET_ALLOC 25
2561#define N_SORT_BUCKET 25
2562#ifdef SQLITE_TEST
2563 int sqlite3_pager_n_sort_bucket = 0;
2564 #undef N_SORT_BUCKET
2565 #define N_SORT_BUCKET \
2566 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2567#endif
drhdc3e10b2006-06-15 14:31:06 +00002568static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002569 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002570 int i;
2571 memset(a, 0, sizeof(a));
2572 while( pIn ){
2573 p = pIn;
2574 pIn = p->pDirty;
2575 p->pDirty = 0;
2576 for(i=0; i<N_SORT_BUCKET-1; i++){
2577 if( a[i]==0 ){
2578 a[i] = p;
2579 break;
2580 }else{
2581 p = merge_pagelist(a[i], p);
2582 a[i] = 0;
2583 }
2584 }
2585 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002586 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2587 ** elements in the input list. This is possible, but impractical.
2588 ** Testing this line is the point of global variable
2589 ** sqlite3_pager_n_sort_bucket.
2590 */
drhdc3e10b2006-06-15 14:31:06 +00002591 a[i] = merge_pagelist(a[i], p);
2592 }
2593 }
2594 p = a[0];
2595 for(i=1; i<N_SORT_BUCKET; i++){
2596 p = merge_pagelist(p, a[i]);
2597 }
2598 return p;
2599}
2600
2601/*
drh2554f8b2003-01-22 01:26:44 +00002602** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2603** every one of those pages out to the database file and mark them all
2604** as clean.
2605*/
2606static int pager_write_pagelist(PgHdr *pList){
2607 Pager *pPager;
2608 int rc;
2609
2610 if( pList==0 ) return SQLITE_OK;
2611 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002612
2613 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2614 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002615 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002616 **
drha6abd042004-06-09 17:37:22 +00002617 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2618 ** through an intermediate state PENDING. A PENDING lock prevents new
2619 ** readers from attaching to the database but is unsufficient for us to
2620 ** write. The idea of a PENDING lock is to prevent new readers from
2621 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002622 **
drha6abd042004-06-09 17:37:22 +00002623 ** While the pager is in the RESERVED state, the original database file
2624 ** is unchanged and we can rollback without having to playback the
2625 ** journal into the original database file. Once we transition to
2626 ** EXCLUSIVE, it means the database file has been changed and any rollback
2627 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002628 */
drh684917c2004-10-05 02:41:42 +00002629 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002630 if( rc!=SQLITE_OK ){
2631 return rc;
2632 }
2633
drhdc3e10b2006-06-15 14:31:06 +00002634 pList = sort_pagelist(pList);
drh2554f8b2003-01-22 01:26:44 +00002635 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002636
2637 /* If the file has not yet been opened, open it now. */
2638 if( !pPager->fd->pMethods ){
2639 assert(pPager->tempFile);
2640 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename);
2641 if( rc ) return rc;
2642 }
2643
drh2554f8b2003-01-22 01:26:44 +00002644 assert( pList->dirty );
danielk1977687566d2004-11-02 12:56:41 +00002645 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002646 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002647 ** make the file smaller (presumably by auto-vacuum code). Do not write
2648 ** any such pages to the file.
2649 */
2650 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00002651 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00002652 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002653 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2654 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002655 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002656 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002657 PAGER_INCR(sqlite3_pager_writedb_count);
2658 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002659 if( pList->pgno==1 ){
2660 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2661 }
danielk1977687566d2004-11-02 12:56:41 +00002662 }
2663#ifndef NDEBUG
2664 else{
drh4f0c5872007-03-26 22:05:01 +00002665 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002666 }
2667#endif
drh2554f8b2003-01-22 01:26:44 +00002668 if( rc ) return rc;
2669 pList->dirty = 0;
danielk19773c407372005-02-15 02:54:14 +00002670#ifdef SQLITE_CHECK_PAGES
2671 pList->pageHash = pager_pagehash(pList);
2672#endif
drh2554f8b2003-01-22 01:26:44 +00002673 pList = pList->pDirty;
2674 }
2675 return SQLITE_OK;
2676}
2677
2678/*
2679** Collect every dirty page into a dirty list and
2680** return a pointer to the head of that list. All pages are
2681** collected even if they are still in use.
2682*/
2683static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh605ad8f2006-05-03 23:34:05 +00002684 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00002685}
2686
2687/*
drh165ffe92005-03-15 17:09:30 +00002688** Return TRUE if there is a hot journal on the given pager.
2689** A hot journal is one that needs to be played back.
2690**
2691** If the current size of the database file is 0 but a journal file
2692** exists, that is probably an old journal left over from a prior
2693** database with the same name. Just delete the journal.
2694*/
2695static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00002696 sqlite3_vfs *pVfs = pPager->pVfs;
drh165ffe92005-03-15 17:09:30 +00002697 if( !pPager->useJournal ) return 0;
danielk1977b4b47412007-08-17 15:53:36 +00002698 if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
drhbb5f18d2007-04-06 18:23:17 +00002699 return 0;
2700 }
2701 if( sqlite3OsCheckReservedLock(pPager->fd) ){
2702 return 0;
2703 }
danielk19773b8a05f2007-03-19 17:44:26 +00002704 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00002705 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00002706 return 0;
2707 }else{
2708 return 1;
2709 }
2710}
2711
danielk19775591df52005-12-20 09:19:37 +00002712/*
danielk1977aef0bf62005-12-30 16:28:01 +00002713** Try to find a page in the cache that can be recycled.
2714**
2715** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00002716** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00002717*/
danielk197713f72992005-12-18 08:51:22 +00002718static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
2719 PgHdr *pPg;
2720 *ppPg = 0;
2721
danielk1977c551edc2007-04-05 13:12:13 +00002722 assert(!MEMDB);
2723
danielk197713f72992005-12-18 08:51:22 +00002724 /* Find a page to recycle. Try to locate a page that does not
2725 ** require us to do an fsync() on the journal.
2726 */
2727 pPg = pPager->pFirstSynced;
2728
2729 /* If we could not find a page that does not require an fsync()
2730 ** on the journal file then fsync the journal file. This is a
2731 ** very slow operation, so we work hard to avoid it. But sometimes
2732 ** it can't be helped.
2733 */
drh8940f4e2007-08-11 00:26:20 +00002734 if( pPg==0 && pPager->pFirst && syncOk && !MEMDB){
danielk197713f72992005-12-18 08:51:22 +00002735 int rc = syncJournal(pPager);
2736 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002737 return rc;
danielk197713f72992005-12-18 08:51:22 +00002738 }
2739 if( pPager->fullSync ){
2740 /* If in full-sync mode, write a new journal header into the
2741 ** journal file. This is done to avoid ever modifying a journal
2742 ** header that is involved in the rollback of pages that have
2743 ** already been written to the database (in case the header is
2744 ** trashed when the nRec field is updated).
2745 */
2746 pPager->nRec = 0;
2747 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00002748 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00002749 rc = writeJournalHdr(pPager);
2750 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002751 return rc;
danielk197713f72992005-12-18 08:51:22 +00002752 }
2753 }
2754 pPg = pPager->pFirst;
2755 }
2756 if( pPg==0 ){
2757 return SQLITE_OK;
2758 }
2759
2760 assert( pPg->nRef==0 );
2761
2762 /* Write the page to the database file if it is dirty.
2763 */
2764 if( pPg->dirty ){
2765 int rc;
2766 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00002767 makeClean(pPg);
2768 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00002769 pPg->pDirty = 0;
2770 rc = pager_write_pagelist( pPg );
2771 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00002772 return rc;
danielk197713f72992005-12-18 08:51:22 +00002773 }
2774 }
2775 assert( pPg->dirty==0 );
2776
2777 /* If the page we are recycling is marked as alwaysRollback, then
2778 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00002779 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00002780 ** It is necessary to do this because the page marked alwaysRollback
2781 ** might be reloaded at a later time but at that point we won't remember
2782 ** that is was marked alwaysRollback. This means that all pages must
2783 ** be marked as alwaysRollback from here on out.
2784 */
2785 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00002786 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00002787 pPager->alwaysRollback = 1;
2788 }
2789
2790 /* Unlink the old page from the free list and the hash table
2791 */
2792 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00002793 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00002794
2795 *ppPg = pPg;
2796 return SQLITE_OK;
2797}
2798
drh86f8c192007-08-22 00:39:19 +00002799#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00002800/*
2801** This function is called to free superfluous dynamically allocated memory
2802** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00002803** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00002804**
2805** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00002806** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00002807** of bytes of memory released.
2808*/
danielk19773b8a05f2007-03-19 17:44:26 +00002809int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00002810 int nReleased = 0; /* Bytes of memory released so far */
2811 sqlite3_mutex *mutex; /* The MEM2 mutex */
2812 Pager *pPager; /* For looping over pagers */
2813 int i; /* Passes over pagers */
danielk197713f72992005-12-18 08:51:22 +00002814
drh86f8c192007-08-22 00:39:19 +00002815 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00002816 */
drh86f8c192007-08-22 00:39:19 +00002817 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2818 sqlite3_mutex_enter(mutex);
2819
2820 /* Signal all database connections that memory management wants
2821 ** to have access to the pagers.
2822 */
2823 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
2824 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00002825 }
2826
danielk197713f72992005-12-18 08:51:22 +00002827 /* Outermost loop runs for at most two iterations. First iteration we
2828 ** try to find memory that can be released without calling fsync(). Second
2829 ** iteration (which only runs if the first failed to free nReq bytes of
2830 ** memory) is permitted to call fsync(). This is of course much more
2831 ** expensive.
2832 */
drh29c636b2006-01-09 23:40:25 +00002833 for(i=0; i<=1; i++){
danielk197713f72992005-12-18 08:51:22 +00002834
2835 /* Loop through all the SQLite pagers opened by the current thread. */
drh86f8c192007-08-22 00:39:19 +00002836 Pager *pPager = sqlite3PagerList;
danielk197738ec0cb2007-04-05 14:29:42 +00002837 for( ; pPager && (nReq<0 || nReleased<nReq); pPager=pPager->pNext){
danielk197713f72992005-12-18 08:51:22 +00002838 PgHdr *pPg;
2839 int rc;
2840
drh86f8c192007-08-22 00:39:19 +00002841 /* In-memory databases should not appear on the pager list */
2842 assert( !MEMDB );
2843
2844 /* Skip pagers that are currently in use by the b-tree layer */
2845 if( pPager->iInUseDB ) continue;
danielk1977c551edc2007-04-05 13:12:13 +00002846
danielk197713f72992005-12-18 08:51:22 +00002847 /* For each pager, try to free as many pages as possible (without
2848 ** calling fsync() if this is the first iteration of the outermost
2849 ** loop).
2850 */
danielk197738ec0cb2007-04-05 14:29:42 +00002851 while( SQLITE_OK==(rc = pager_recycle(pPager, i, &pPg)) && pPg) {
danielk1977aef0bf62005-12-30 16:28:01 +00002852 /* We've found a page to free. At this point the page has been
danielk197713f72992005-12-18 08:51:22 +00002853 ** removed from the page hash-table, free-list and synced-list
danielk1977aef0bf62005-12-30 16:28:01 +00002854 ** (pFirstSynced). It is still in the all pages (pAll) list.
danielk197713f72992005-12-18 08:51:22 +00002855 ** Remove it from this list before freeing.
2856 **
2857 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
2858 ** probably is though.
2859 */
2860 PgHdr *pTmp;
danielk19770190d1d2005-12-19 14:18:11 +00002861 assert( pPg );
danielk197738ec0cb2007-04-05 14:29:42 +00002862 if( pPg==pPager->pAll ){
2863 pPager->pAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00002864 }else{
danielk197738ec0cb2007-04-05 14:29:42 +00002865 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
danielk197713f72992005-12-18 08:51:22 +00002866 pTmp->pNextAll = pPg->pNextAll;
2867 }
danielk19771e536952007-08-16 10:09:01 +00002868 nReleased += (
2869 sizeof(*pPg) + pPager->pageSize
2870 + sizeof(u32) + pPager->nExtra
2871 + MEMDB*sizeof(PgHistory)
2872 );
drh34f56212007-08-10 23:56:35 +00002873 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
drh538f5702007-04-13 02:14:30 +00002874 PAGER_INCR(sqlite3_pager_pgfree_count);
drh17435752007-08-16 04:30:38 +00002875 sqlite3_free(pPg);
danielk197713f72992005-12-18 08:51:22 +00002876 }
2877
2878 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00002879 /* An error occured whilst writing to the database file or
2880 ** journal in pager_recycle(). The error is not returned to the
danielk1977efaaf572006-01-16 11:29:19 +00002881 ** caller of this function. Instead, set the Pager.errCode variable.
danielk1977aef0bf62005-12-30 16:28:01 +00002882 ** The error will be returned to the user (or users, in the case
2883 ** of a shared pager cache) of the pager for which the error occured.
danielk197713f72992005-12-18 08:51:22 +00002884 */
drh34f56212007-08-10 23:56:35 +00002885 assert(
2886 (rc&0xff)==SQLITE_IOERR ||
2887 rc==SQLITE_FULL ||
2888 rc==SQLITE_BUSY
2889 );
danielk197738ec0cb2007-04-05 14:29:42 +00002890 assert( pPager->state>=PAGER_RESERVED );
2891 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00002892 }
2893 }
2894 }
danielk1977aef0bf62005-12-30 16:28:01 +00002895
drh86f8c192007-08-22 00:39:19 +00002896 /* Clear the memory management flags and release the mutex
2897 */
2898 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
2899 pPager->iInUseMM = 0;
2900 }
2901 sqlite3_mutex_leave(mutex);
2902
2903 /* Return the number of bytes released
2904 */
danielk197713f72992005-12-18 08:51:22 +00002905 return nReleased;
2906}
drh86f8c192007-08-22 00:39:19 +00002907#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00002908
drh165ffe92005-03-15 17:09:30 +00002909/*
danielk1977e180dd92007-04-05 17:15:52 +00002910** Read the content of page pPg out of the database file.
2911*/
2912static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2913 int rc;
danielk197762079062007-08-15 17:08:46 +00002914 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002915 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002916 assert(pPager->fd->pMethods||pPager->tempFile);
2917 if( !pPager->fd->pMethods ){
2918 return SQLITE_IOERR_SHORT_READ;
2919 }
danielk197762079062007-08-15 17:08:46 +00002920 offset = (pgno-1)*(i64)pPager->pageSize;
2921 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002922 PAGER_INCR(sqlite3_pager_readdb_count);
2923 PAGER_INCR(pPager->nRead);
2924 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002925 if( pgno==1 ){
2926 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
2927 sizeof(pPager->dbFileVers));
2928 }
danielk1977e180dd92007-04-05 17:15:52 +00002929 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00002930 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
2931 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00002932 return rc;
2933}
2934
2935
2936/*
danielk1977e277be02007-03-23 18:12:06 +00002937** This function is called to obtain the shared lock required before
2938** data may be read from the pager cache. If the shared lock has already
2939** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002940**
2941** Immediately after obtaining the shared lock (if required), this function
2942** checks for a hot-journal file. If one is found, an emergency rollback
2943** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002944*/
2945static int pagerSharedLock(Pager *pPager){
2946 int rc = SQLITE_OK;
2947
2948 if( pPager->state==PAGER_UNLOCK ){
danielk1977b4b47412007-08-17 15:53:36 +00002949 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00002950 if( !MEMDB ){
2951 assert( pPager->nRef==0 );
2952 if( !pPager->noReadlock ){
2953 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2954 if( rc!=SQLITE_OK ){
2955 return pager_error(pPager, rc);
2956 }
2957 assert( pPager->state>=SHARED_LOCK );
2958 }
2959
2960 /* If a journal file exists, and there is no RESERVED lock on the
2961 ** database file, then it either needs to be played back or deleted.
2962 */
2963 if( hasHotJournal(pPager) ){
2964 /* Get an EXCLUSIVE lock on the database file. At this point it is
2965 ** important that a RESERVED lock is not obtained on the way to the
2966 ** EXCLUSIVE lock. If it were, another process might open the
2967 ** database file, detect the RESERVED lock, and conclude that the
2968 ** database is safe to read while this process is still rolling it
2969 ** back.
2970 **
2971 ** Because the intermediate RESERVED lock is not requested, the
2972 ** second process will get to this point in the code and fail to
2973 ** obtain it's own EXCLUSIVE lock on the database file.
2974 */
2975 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
2976 if( rc!=SQLITE_OK ){
2977 pager_unlock(pPager);
2978 return pager_error(pPager, rc);
2979 }
2980 pPager->state = PAGER_EXCLUSIVE;
2981
2982 /* Open the journal for reading only. Return SQLITE_BUSY if
2983 ** we are unable to open the journal file.
2984 **
2985 ** The journal file does not need to be locked itself. The
2986 ** journal file is never open unless the main database file holds
2987 ** a write lock, so there is never any chance of two or more
2988 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00002989 **
drhfd131da2007-08-07 17:13:03 +00002990 ** Open the journal for read/write access. This is because in
2991 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00002992 ** possibly used for a transaction later on. On some systems, the
2993 ** OsTruncate() call used in exclusive-access mode also requires
2994 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00002995 */
danielk1977979f38e2007-03-27 16:19:51 +00002996 rc = SQLITE_BUSY;
danielk1977b4b47412007-08-17 15:53:36 +00002997 if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
2998 int fout = 0;
danielk1977fee2d252007-08-18 10:59:19 +00002999 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
danielk19777152de82007-03-29 17:28:14 +00003000 assert( !pPager->tempFile );
danielk1977b4b47412007-08-17 15:53:36 +00003001 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, &fout);
3002 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3003 if( fout&SQLITE_OPEN_READONLY ){
danielk1977979f38e2007-03-27 16:19:51 +00003004 rc = SQLITE_BUSY;
danielk1977b4b47412007-08-17 15:53:36 +00003005 sqlite3OsClose(pPager->jfd);
danielk1977979f38e2007-03-27 16:19:51 +00003006 }
3007 }
danielk1977e277be02007-03-23 18:12:06 +00003008 if( rc!=SQLITE_OK ){
3009 pager_unlock(pPager);
3010 return SQLITE_BUSY;
3011 }
3012 pPager->journalOpen = 1;
3013 pPager->journalStarted = 0;
3014 pPager->journalOff = 0;
3015 pPager->setMaster = 0;
3016 pPager->journalHdr = 0;
3017
3018 /* Playback and delete the journal. Drop the database write
3019 ** lock and reacquire the read lock.
3020 */
3021 rc = pager_playback(pPager, 1);
3022 if( rc!=SQLITE_OK ){
3023 return pager_error(pPager, rc);
3024 }
danielk1977c5859712007-03-26 12:26:27 +00003025 assert(pPager->state==PAGER_SHARED ||
3026 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3027 );
danielk1977e277be02007-03-23 18:12:06 +00003028 }
3029
3030 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003031 /* The shared-lock has just been acquired on the database file
3032 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003033 ** read or write transaction). Check to see if the database
3034 ** has been modified. If the database has changed, flush the
3035 ** cache.
3036 **
3037 ** Database changes is detected by looking at 15 bytes beginning
3038 ** at offset 24 into the file. The first 4 of these 16 bytes are
3039 ** a 32-bit counter that is incremented with each change. The
3040 ** other bytes change randomly with each file change when
3041 ** a codec is in use.
3042 **
3043 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003044 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003045 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003046 */
drh86a88112007-04-16 15:02:19 +00003047 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003048 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003049
danielk1977e180dd92007-04-05 17:15:52 +00003050 if( pPager->errCode ){
3051 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00003052 }
3053
danielk1977e180dd92007-04-05 17:15:52 +00003054 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003055 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003056 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003057 if( rc!=SQLITE_OK ){
3058 return rc;
3059 }
drh86a88112007-04-16 15:02:19 +00003060 }else{
3061 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003062 }
3063
drh86a88112007-04-16 15:02:19 +00003064 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003065 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003066 }
3067 }
3068 }
danielk1977c5859712007-03-26 12:26:27 +00003069 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3070 if( pPager->state==PAGER_UNLOCK ){
3071 pPager->state = PAGER_SHARED;
3072 }
danielk1977e277be02007-03-23 18:12:06 +00003073 }
3074
3075 return rc;
3076}
3077
3078/*
danielk1977e180dd92007-04-05 17:15:52 +00003079** Allocate a PgHdr object. Either create a new one or reuse
3080** an existing one that is not otherwise in use.
3081**
3082** A new PgHdr structure is created if any of the following are
3083** true:
3084**
3085** (1) We have not exceeded our maximum allocated cache size
3086** as set by the "PRAGMA cache_size" command.
3087**
3088** (2) There are no unused PgHdr objects available at this time.
3089**
3090** (3) This is an in-memory database.
3091**
3092** (4) There are no PgHdr objects that do not require a journal
3093** file sync and a sync of the journal file is currently
3094** prohibited.
3095**
3096** Otherwise, reuse an existing PgHdr. In other words, reuse an
3097** existing PgHdr if all of the following are true:
3098**
3099** (1) We have reached or exceeded the maximum cache size
3100** allowed by "PRAGMA cache_size".
3101**
3102** (2) There is a PgHdr available with PgHdr->nRef==0
3103**
3104** (3) We are not in an in-memory database
3105**
3106** (4) Either there is an available PgHdr that does not need
3107** to be synced to disk or else disk syncing is currently
3108** allowed.
danielk197724168722007-04-02 05:07:47 +00003109*/
3110static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3111 int rc = SQLITE_OK;
3112 PgHdr *pPg;
3113
danielk1977e180dd92007-04-05 17:15:52 +00003114 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003115 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003116 if( pPager->nPage<pPager->mxPage
3117 || pPager->pFirst==0
3118 || MEMDB
3119 || (pPager->pFirstSynced==0 && pPager->doNotSync)
3120 ){
danielk197724168722007-04-02 05:07:47 +00003121 if( pPager->nPage>=pPager->nHash ){
3122 pager_resize_hash_table(pPager,
3123 pPager->nHash<256 ? 256 : pPager->nHash*2);
3124 if( pPager->nHash==0 ){
3125 rc = SQLITE_NOMEM;
3126 goto pager_allocate_out;
3127 }
3128 }
drh17435752007-08-16 04:30:38 +00003129 pPg = sqlite3_malloc( sizeof(*pPg) + pPager->pageSize
danielk197724168722007-04-02 05:07:47 +00003130 + sizeof(u32) + pPager->nExtra
3131 + MEMDB*sizeof(PgHistory) );
3132 if( pPg==0 ){
3133 rc = SQLITE_NOMEM;
3134 goto pager_allocate_out;
3135 }
3136 memset(pPg, 0, sizeof(*pPg));
3137 if( MEMDB ){
3138 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
3139 }
3140 pPg->pPager = pPager;
3141 pPg->pNextAll = pPager->pAll;
3142 pPager->pAll = pPg;
3143 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003144 }else{
3145 /* Recycle an existing page with a zero ref-count. */
3146 rc = pager_recycle(pPager, 1, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003147 if( rc==SQLITE_BUSY ){
3148 rc = SQLITE_IOERR_BLOCKED;
3149 }
danielk197724168722007-04-02 05:07:47 +00003150 if( rc!=SQLITE_OK ){
3151 goto pager_allocate_out;
3152 }
3153 assert( pPager->state>=SHARED_LOCK );
3154 assert(pPg);
3155 }
3156 *ppPg = pPg;
3157
3158pager_allocate_out:
3159 return rc;
3160}
3161
3162/*
drhd33d5a82007-04-26 12:11:28 +00003163** Make sure we have the content for a page. If the page was
3164** previously acquired with noContent==1, then the content was
3165** just initialized to zeros instead of being read from disk.
3166** But now we need the real data off of disk. So make sure we
3167** have it. Read it in if we do not have it already.
3168*/
3169static int pager_get_content(PgHdr *pPg){
3170 if( pPg->needRead ){
3171 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3172 if( rc==SQLITE_OK ){
3173 pPg->needRead = 0;
3174 }else{
3175 return rc;
3176 }
3177 }
3178 return SQLITE_OK;
3179}
3180
3181/*
drhd9b02572001-04-15 00:37:09 +00003182** Acquire a page.
3183**
drh58a11682001-11-10 13:51:08 +00003184** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003185** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003186**
drhd33d5a82007-04-26 12:11:28 +00003187** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003188** file is smaller than the requested page, then no actual disk
3189** read occurs and the memory image of the page is initialized to
3190** all zeros. The extra data appended to a page is always initialized
3191** to zeros the first time a page is loaded into memory.
3192**
drhd9b02572001-04-15 00:37:09 +00003193** The acquisition might fail for several reasons. In all cases,
3194** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003195**
drhd33d5a82007-04-26 12:11:28 +00003196** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003197** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003198** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003199** just returns 0. This routine acquires a read-lock the first time it
3200** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003201** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003202** or journal files.
drh0787db62007-03-04 13:15:27 +00003203**
drh538f5702007-04-13 02:14:30 +00003204** If noContent is false, the page contents are actually read from disk.
3205** If noContent is true, it means that we do not care about the contents
3206** of the page at this time, so do not do a disk read. Just fill in the
3207** page content with zeros. But mark the fact that we have not read the
3208** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003209** sqlite3PagerWrite() is called on this page or if this routine is
3210** called again with noContent==0, that means that the content is needed
3211** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003212*/
drh86f8c192007-08-22 00:39:19 +00003213static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003214 Pager *pPager, /* The pager open on the database file */
3215 Pgno pgno, /* Page number to fetch */
3216 DbPage **ppPage, /* Write a pointer to the page here */
3217 int noContent /* Do not bother reading content from disk if true */
3218){
drhed7c8552001-04-11 14:29:21 +00003219 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003220 int rc;
drhed7c8552001-04-11 14:29:21 +00003221
danielk1977e277be02007-03-23 18:12:06 +00003222 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3223
danielk197726836652005-01-17 01:33:13 +00003224 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3225 ** number greater than this, or zero, is requested.
3226 */
drh267cb322005-09-16 17:16:52 +00003227 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003228 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003229 }
3230
drhd9b02572001-04-15 00:37:09 +00003231 /* Make sure we have not hit any critical errors.
3232 */
drh836faa42003-01-11 13:30:57 +00003233 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003234 *ppPage = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003235 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3236 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003237 }
3238
danielk197713adf8a2004-06-03 16:08:41 +00003239 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003240 ** on the database file. pagerSharedLock() is a no-op if
3241 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003242 */
danielk1977e277be02007-03-23 18:12:06 +00003243 rc = pagerSharedLock(pPager);
3244 if( rc!=SQLITE_OK ){
3245 return rc;
drhed7c8552001-04-11 14:29:21 +00003246 }
danielk1977e277be02007-03-23 18:12:06 +00003247 assert( pPager->state!=PAGER_UNLOCK );
3248
3249 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003250 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003251 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003252 int nMax;
drhed7c8552001-04-11 14:29:21 +00003253 int h;
drh538f5702007-04-13 02:14:30 +00003254 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003255 rc = pagerAllocatePage(pPager, &pPg);
3256 if( rc!=SQLITE_OK ){
3257 return rc;
drhed7c8552001-04-11 14:29:21 +00003258 }
danielk197724168722007-04-02 05:07:47 +00003259
drhed7c8552001-04-11 14:29:21 +00003260 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003261 assert( !MEMDB || pgno>pPager->stmtSize );
drh1ab43002002-01-14 09:28:19 +00003262 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19771e536952007-08-16 10:09:01 +00003263#if 0
danielk19774adee202004-05-08 08:23:19 +00003264 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
danielk19771e536952007-08-16 10:09:01 +00003265#endif
drhdb48ee02003-01-16 13:42:43 +00003266 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00003267 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00003268 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003269 }else{
3270 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00003271 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003272 }
danielk1977f35843b2007-04-07 15:03:17 +00003273
drh605ad8f2006-05-03 23:34:05 +00003274 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003275 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00003276 REFINFO(pPg);
danielk197775bab7d2006-01-23 13:09:45 +00003277
drhd9b02572001-04-15 00:37:09 +00003278 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003279 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003280 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003281 }
danielk1977979f38e2007-03-27 16:19:51 +00003282 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003283 if( pPager->errCode ){
danielk19773b8a05f2007-03-19 17:44:26 +00003284 sqlite3PagerUnref(pPg);
danielk1977efaaf572006-01-16 11:29:19 +00003285 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00003286 return rc;
3287 }
danielk197775bab7d2006-01-23 13:09:45 +00003288
3289 /* Populate the page with data, either by reading from the database
3290 ** file, or by setting the entire page to zero.
3291 */
drhd215acf2007-04-13 04:01:58 +00003292 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003293 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003294 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003295 return SQLITE_FULL;
3296 }
drh90f5ecb2004-07-22 01:19:35 +00003297 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003298 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003299 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003300 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003301 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003302 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3303 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003304 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003305 return rc;
drh81a20f22001-10-12 17:30:04 +00003306 }
danielk1977b4626a32007-04-28 15:47:43 +00003307 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003308 }
danielk197775bab7d2006-01-23 13:09:45 +00003309
3310 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003311 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003312 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003313 pPg->pNextHash = pPager->aHash[h];
3314 pPager->aHash[h] = pPg;
3315 if( pPg->pNextHash ){
3316 assert( pPg->pNextHash->pPrevHash==0 );
3317 pPg->pNextHash->pPrevHash = pPg;
3318 }
3319
danielk19773c407372005-02-15 02:54:14 +00003320#ifdef SQLITE_CHECK_PAGES
3321 pPg->pageHash = pager_pagehash(pPg);
3322#endif
drhed7c8552001-04-11 14:29:21 +00003323 }else{
drhd9b02572001-04-15 00:37:09 +00003324 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003325 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003326 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003327 if( !noContent ){
3328 rc = pager_get_content(pPg);
3329 if( rc ){
3330 return rc;
3331 }
3332 }
drhdf0b3b02001-06-23 11:36:20 +00003333 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003334 }
danielk19773b8a05f2007-03-19 17:44:26 +00003335 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003336 return SQLITE_OK;
3337}
drh86f8c192007-08-22 00:39:19 +00003338int sqlite3PagerAcquire(
3339 Pager *pPager, /* The pager open on the database file */
3340 Pgno pgno, /* Page number to fetch */
3341 DbPage **ppPage, /* Write a pointer to the page here */
3342 int noContent /* Do not bother reading content from disk if true */
3343){
3344 int rc;
3345 pagerEnter(pPager);
3346 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3347 pagerLeave(pPager);
3348 return rc;
3349}
3350
drhed7c8552001-04-11 14:29:21 +00003351
3352/*
drh7e3b0a02001-04-28 16:52:40 +00003353** Acquire a page if it is already in the in-memory cache. Do
3354** not read the page from disk. Return a pointer to the page,
3355** or 0 if the page is not in cache.
3356**
danielk19773b8a05f2007-03-19 17:44:26 +00003357** See also sqlite3PagerGet(). The difference between this routine
3358** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003359** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003360** returns NULL if the page is not in cache or if a disk I/O error
3361** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003362*/
danielk19773b8a05f2007-03-19 17:44:26 +00003363DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003364 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003365
drh836faa42003-01-11 13:30:57 +00003366 assert( pPager!=0 );
3367 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003368
drh86f8c192007-08-22 00:39:19 +00003369 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003370 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003371 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003372 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3373 /* Do nothing */
3374 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3375 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003376 }
drh86f8c192007-08-22 00:39:19 +00003377 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003378 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003379}
3380
3381/*
drhed7c8552001-04-11 14:29:21 +00003382** Release a page.
3383**
3384** If the number of references to the page drop to zero, then the
3385** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003386** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003387** removed.
3388*/
danielk19773b8a05f2007-03-19 17:44:26 +00003389int sqlite3PagerUnref(DbPage *pPg){
drhd9b02572001-04-15 00:37:09 +00003390
3391 /* Decrement the reference count for this page
3392 */
drhed7c8552001-04-11 14:29:21 +00003393 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003394 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003395 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00003396 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00003397
danielk19773c407372005-02-15 02:54:14 +00003398 CHECK_PAGE(pPg);
3399
drh72f82862001-05-24 21:06:34 +00003400 /* When the number of references to a page reach 0, call the
3401 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003402 */
drhed7c8552001-04-11 14:29:21 +00003403 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00003404 Pager *pPager;
3405 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003406 pPg->pNextFree = 0;
3407 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00003408 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00003409 if( pPg->pPrevFree ){
3410 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00003411 }else{
3412 pPager->pFirst = pPg;
3413 }
drh341eae82003-01-21 02:39:36 +00003414 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
3415 pPager->pFirstSynced = pPg;
3416 }
drh72f82862001-05-24 21:06:34 +00003417 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003418 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003419 }
drhd9b02572001-04-15 00:37:09 +00003420
3421 /* When all pages reach the freelist, drop the read lock from
3422 ** the database file.
3423 */
3424 pPager->nRef--;
3425 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003426 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003427 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003428 }
drhed7c8552001-04-11 14:29:21 +00003429 }
drh86f8c192007-08-22 00:39:19 +00003430 pagerLeave(pPg->pPager);
drhd9b02572001-04-15 00:37:09 +00003431 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003432}
3433
3434/*
drha6abd042004-06-09 17:37:22 +00003435** Create a journal file for pPager. There should already be a RESERVED
3436** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003437**
3438** Return SQLITE_OK if everything. Return an error code and release the
3439** write lock if anything goes wrong.
3440*/
3441static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003442 sqlite3_vfs *pVfs = pPager->pVfs;
3443 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3444
drhda47d772002-12-02 04:25:19 +00003445 int rc;
drhb7f91642004-10-31 02:22:47 +00003446 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003447 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003448 assert( pPager->journalOpen==0 );
3449 assert( pPager->useJournal );
drh88b01a12005-03-28 18:04:27 +00003450 assert( pPager->aInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003451 sqlite3PagerPagecount(pPager);
drh17435752007-08-16 04:30:38 +00003452 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhda47d772002-12-02 04:25:19 +00003453 if( pPager->aInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003454 rc = SQLITE_NOMEM;
3455 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003456 }
danielk1977b4b47412007-08-17 15:53:36 +00003457
3458 if( pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00003459 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3460 }else{
3461 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
danielk1977b4b47412007-08-17 15:53:36 +00003462 }
3463 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
3464 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk197776572402004-06-25 02:38:54 +00003465 pPager->journalOff = 0;
3466 pPager->setMaster = 0;
3467 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003468 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003469 if( rc==SQLITE_NOMEM ){
danielk1977fee2d252007-08-18 10:59:19 +00003470 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh600e46a2007-03-28 01:59:33 +00003471 }
drh9c105bb2004-10-02 20:38:28 +00003472 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003473 }
3474 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003475 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003476 pPager->needSync = 0;
3477 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003478 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003479 if( pPager->errCode ){
3480 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003481 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003482 }
drhda47d772002-12-02 04:25:19 +00003483 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003484
danielk197776572402004-06-25 02:38:54 +00003485 rc = writeJournalHdr(pPager);
3486
drhac69b052004-05-12 13:30:07 +00003487 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003488 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003489 }
danielk1977261919c2005-12-06 12:52:59 +00003490 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003491 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003492 if( rc==SQLITE_OK ){
3493 rc = SQLITE_FULL;
3494 }
3495 }
drh9c105bb2004-10-02 20:38:28 +00003496 return rc;
3497
3498failed_to_open_journal:
drh17435752007-08-16 04:30:38 +00003499 sqlite3_free(pPager->aInJournal);
drh9c105bb2004-10-02 20:38:28 +00003500 pPager->aInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003501 return rc;
drhda47d772002-12-02 04:25:19 +00003502}
3503
3504/*
drh4b845d72002-03-05 12:41:19 +00003505** Acquire a write-lock on the database. The lock is removed when
3506** the any of the following happen:
3507**
drh80e35f42007-03-30 14:06:34 +00003508** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003509** * sqlite3PagerRollback() is called.
3510** * sqlite3PagerClose() is called.
3511** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003512**
danielk197713adf8a2004-06-03 16:08:41 +00003513** The first parameter to this routine is a pointer to any open page of the
3514** database file. Nothing changes about the page - it is used merely to
3515** acquire a pointer to the Pager structure and as proof that there is
3516** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003517**
danielk197713adf8a2004-06-03 16:08:41 +00003518** The second parameter indicates how much space in bytes to reserve for a
3519** master journal file-name at the start of the journal when it is created.
3520**
3521** A journal file is opened if this is not a temporary file. For temporary
3522** files, the opening of the journal file is deferred until there is an
3523** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003524**
drha6abd042004-06-09 17:37:22 +00003525** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003526**
3527** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3528** immediately instead of waiting until we try to flush the cache. The
3529** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003530*/
danielk19773b8a05f2007-03-19 17:44:26 +00003531int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003532 Pager *pPager = pPg->pPager;
3533 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003534 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003535 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003536 assert( pPager->state!=PAGER_UNLOCK );
3537 if( pPager->state==PAGER_SHARED ){
drh4b845d72002-03-05 12:41:19 +00003538 assert( pPager->aInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003539 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003540 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003541 pPager->origDbSize = pPager->dbSize;
3542 }else{
drh054889e2005-11-30 03:20:31 +00003543 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003544 if( rc==SQLITE_OK ){
3545 pPager->state = PAGER_RESERVED;
3546 if( exFlag ){
3547 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3548 }
3549 }
danielk19779eed5052004-06-04 10:38:30 +00003550 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003551 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003552 return rc;
drhac69b052004-05-12 13:30:07 +00003553 }
drha6abd042004-06-09 17:37:22 +00003554 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003555 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003556 if( pPager->useJournal && !pPager->tempFile ){
3557 rc = pager_open_journal(pPager);
3558 }
drh4b845d72002-03-05 12:41:19 +00003559 }
danielk1977334cdb62007-03-26 08:05:12 +00003560 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3561 /* This happens when the pager was in exclusive-access mode last
3562 ** time a (read or write) transaction was successfully concluded
3563 ** by this connection. Instead of deleting the journal file it was
3564 ** kept open and truncated to 0 bytes.
3565 */
3566 assert( pPager->nRec==0 );
3567 assert( pPager->origDbSize==0 );
drh369339d2007-03-30 16:01:55 +00003568 assert( pPager->aInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003569 sqlite3PagerPagecount(pPager);
drh17435752007-08-16 04:30:38 +00003570 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
danielk1977334cdb62007-03-26 08:05:12 +00003571 if( !pPager->aInJournal ){
3572 rc = SQLITE_NOMEM;
3573 }else{
drh369339d2007-03-30 16:01:55 +00003574 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003575 rc = writeJournalHdr(pPager);
3576 }
drh4b845d72002-03-05 12:41:19 +00003577 }
danielk1977334cdb62007-03-26 08:05:12 +00003578 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00003579 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00003580 return rc;
3581}
3582
3583/*
drh605ad8f2006-05-03 23:34:05 +00003584** Make a page dirty. Set its dirty flag and add it to the dirty
3585** page list.
3586*/
3587static void makeDirty(PgHdr *pPg){
3588 if( pPg->dirty==0 ){
3589 Pager *pPager = pPg->pPager;
3590 pPg->dirty = 1;
3591 pPg->pDirty = pPager->pDirty;
3592 if( pPager->pDirty ){
3593 pPager->pDirty->pPrevDirty = pPg;
3594 }
3595 pPg->pPrevDirty = 0;
3596 pPager->pDirty = pPg;
3597 }
3598}
3599
3600/*
3601** Make a page clean. Clear its dirty bit and remove it from the
3602** dirty page list.
3603*/
3604static void makeClean(PgHdr *pPg){
3605 if( pPg->dirty ){
3606 pPg->dirty = 0;
3607 if( pPg->pDirty ){
3608 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
3609 }
3610 if( pPg->pPrevDirty ){
3611 pPg->pPrevDirty->pDirty = pPg->pDirty;
3612 }else{
3613 pPg->pPager->pDirty = pPg->pDirty;
3614 }
3615 }
3616}
3617
3618
3619/*
drhed7c8552001-04-11 14:29:21 +00003620** Mark a data page as writeable. The page is written into the journal
3621** if it is not there already. This routine must be called before making
3622** changes to a page.
3623**
3624** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003625** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003626** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003627** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003628** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003629**
3630** If the journal file could not be written because the disk is full,
3631** then this routine returns SQLITE_FULL and does an immediate rollback.
3632** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003633** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003634** reset.
drhed7c8552001-04-11 14:29:21 +00003635*/
danielk19773b8a05f2007-03-19 17:44:26 +00003636static int pager_write(PgHdr *pPg){
3637 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00003638 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003639 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003640
drh6446c4d2001-12-15 14:22:18 +00003641 /* Check for errors
3642 */
danielk1977efaaf572006-01-16 11:29:19 +00003643 if( pPager->errCode ){
3644 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003645 }
drh5e00f6c2001-09-13 13:46:56 +00003646 if( pPager->readOnly ){
3647 return SQLITE_PERM;
3648 }
drh6446c4d2001-12-15 14:22:18 +00003649
danielk197776572402004-06-25 02:38:54 +00003650 assert( !pPager->setMaster );
3651
danielk19773c407372005-02-15 02:54:14 +00003652 CHECK_PAGE(pPg);
3653
drh538f5702007-04-13 02:14:30 +00003654 /* If this page was previously acquired with noContent==1, that means
3655 ** we didn't really read in the content of the page. This can happen
3656 ** (for example) when the page is being moved to the freelist. But
3657 ** now we are (perhaps) moving the page off of the freelist for
3658 ** reuse and we need to know its original content so that content
3659 ** can be stored in the rollback journal. So do the read at this
3660 ** time.
3661 */
drhd33d5a82007-04-26 12:11:28 +00003662 rc = pager_get_content(pPg);
3663 if( rc ){
3664 return rc;
drh538f5702007-04-13 02:14:30 +00003665 }
3666
drh6446c4d2001-12-15 14:22:18 +00003667 /* Mark the page as dirty. If the page has already been written
3668 ** to the journal then we can return right away.
3669 */
drh605ad8f2006-05-03 23:34:05 +00003670 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00003671 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00003672 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003673 }else{
drh6446c4d2001-12-15 14:22:18 +00003674
danielk1977a0bf2652004-11-04 14:30:04 +00003675 /* If we get this far, it means that the page needs to be
3676 ** written to the transaction journal or the ckeckpoint journal
3677 ** or both.
3678 **
3679 ** First check to see that the transaction journal exists and
3680 ** create it if it does not.
3681 */
3682 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003683 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003684 if( rc!=SQLITE_OK ){
3685 return rc;
3686 }
3687 assert( pPager->state>=PAGER_RESERVED );
3688 if( !pPager->journalOpen && pPager->useJournal ){
3689 rc = pager_open_journal(pPager);
3690 if( rc!=SQLITE_OK ) return rc;
3691 }
3692 assert( pPager->journalOpen || !pPager->useJournal );
3693 pPager->dirtyCache = 1;
3694
3695 /* The transaction journal now exists and we have a RESERVED or an
3696 ** EXCLUSIVE lock on the main database file. Write the current page to
3697 ** the transaction journal if it is not there already.
3698 */
3699 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
3700 if( (int)pPg->pgno <= pPager->origDbSize ){
3701 int szPg;
danielk1977a0bf2652004-11-04 14:30:04 +00003702 if( MEMDB ){
3703 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00003704 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00003705 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00003706 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003707 if( pHist->pOrig ){
3708 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
3709 }
3710 }else{
drhc001c582006-03-06 18:23:16 +00003711 u32 cksum, saved;
3712 char *pData2, *pEnd;
drh267cb322005-09-16 17:16:52 +00003713 /* We should never write to the journal file the page that
3714 ** contains the database locks. The following assert verifies
3715 ** that we do not. */
3716 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00003717 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00003718 cksum = pager_cksum(pPager, (u8*)pData2);
drhc001c582006-03-06 18:23:16 +00003719 pEnd = pData2 + pPager->pageSize;
3720 pData2 -= 4;
3721 saved = *(u32*)pEnd;
3722 put32bits(pEnd, cksum);
danielk1977a0bf2652004-11-04 14:30:04 +00003723 szPg = pPager->pageSize+8;
drhc001c582006-03-06 18:23:16 +00003724 put32bits(pData2, pPg->pgno);
danielk197762079062007-08-15 17:08:46 +00003725 rc = sqlite3OsWrite(pPager->jfd, pData2, szPg, pPager->journalOff);
drhb0603412007-02-28 04:47:26 +00003726 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
drh538f5702007-04-13 02:14:30 +00003727 pPager->journalOff, szPg));
3728 PAGER_INCR(sqlite3_pager_writej_count);
danielk1977a0bf2652004-11-04 14:30:04 +00003729 pPager->journalOff += szPg;
drh477731b2007-06-16 03:06:27 +00003730 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
3731 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
drhc001c582006-03-06 18:23:16 +00003732 *(u32*)pEnd = saved;
danielk197707cb5602006-01-20 10:55:05 +00003733
drhfd131da2007-08-07 17:13:03 +00003734 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00003735 ** transaction will be rolled back by the layer above.
3736 */
danielk1977a0bf2652004-11-04 14:30:04 +00003737 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00003738 return rc;
3739 }
danielk197707cb5602006-01-20 10:55:05 +00003740
danielk1977a0bf2652004-11-04 14:30:04 +00003741 pPager->nRec++;
3742 assert( pPager->aInJournal!=0 );
3743 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
3744 pPg->needSync = !pPager->noSync;
3745 if( pPager->stmtInUse ){
3746 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
danielk1977a0bf2652004-11-04 14:30:04 +00003747 }
drhac69b052004-05-12 13:30:07 +00003748 }
danielk197713adf8a2004-06-03 16:08:41 +00003749 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00003750 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00003751 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00003752 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00003753 }
3754 if( pPg->needSync ){
3755 pPager->needSync = 1;
3756 }
3757 pPg->inJournal = 1;
3758 }
3759
3760 /* If the statement journal is open and the page is not in it,
3761 ** then write the current page to the statement journal. Note that
3762 ** the statement journal format differs from the standard journal format
3763 ** in that it omits the checksums and the header.
3764 */
danielk1977f35843b2007-04-07 15:03:17 +00003765 if( pPager->stmtInUse
3766 && !pageInStatement(pPg)
3767 && (int)pPg->pgno<=pPager->stmtSize
3768 ){
danielk1977a0bf2652004-11-04 14:30:04 +00003769 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
3770 if( MEMDB ){
3771 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
3772 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00003773 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003774 if( pHist->pStmt ){
3775 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
3776 }
drh4f0c5872007-03-26 22:05:01 +00003777 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00003778 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00003779 }else{
danielk197762079062007-08-15 17:08:46 +00003780 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhc001c582006-03-06 18:23:16 +00003781 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4;
3782 put32bits(pData2, pPg->pgno);
danielk197762079062007-08-15 17:08:46 +00003783 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4, offset);
drh4f0c5872007-03-26 22:05:01 +00003784 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00003785 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00003786 return rc;
3787 }
danielk1977a0bf2652004-11-04 14:30:04 +00003788 pPager->stmtNRec++;
3789 assert( pPager->aInStmt!=0 );
3790 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhdb48ee02003-01-16 13:42:43 +00003791 }
drhd9b02572001-04-15 00:37:09 +00003792 }
drhfa86c412002-02-02 15:01:15 +00003793 }
3794
3795 /* Update the database size and return.
3796 */
drh1aa2d8b2007-01-03 15:34:29 +00003797 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00003798 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003799 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00003800 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00003801 pPager->dbSize++;
3802 }
drh306dc212001-05-21 13:45:10 +00003803 }
drh69688d52001-04-14 16:38:23 +00003804 return rc;
drhed7c8552001-04-11 14:29:21 +00003805}
3806
3807/*
danielk19774099f6e2007-03-19 11:25:20 +00003808** This function is used to mark a data-page as writable. It uses
3809** pager_write() to open a journal file (if it is not already open)
3810** and write the page *pData to the journal.
3811**
3812** The difference between this function and pager_write() is that this
3813** function also deals with the special case where 2 or more pages
3814** fit on a single disk sector. In this case all co-resident pages
3815** must have been written to the journal file before returning.
3816*/
danielk19773b8a05f2007-03-19 17:44:26 +00003817int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003818 int rc = SQLITE_OK;
3819
danielk19773b8a05f2007-03-19 17:44:26 +00003820 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003821 Pager *pPager = pPg->pPager;
3822 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3823
drh86f8c192007-08-22 00:39:19 +00003824 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003825 if( !MEMDB && nPagePerSector>1 ){
3826 Pgno nPageCount; /* Total number of pages in database file */
3827 Pgno pg1; /* First page of the sector pPg is located on. */
3828 int nPage; /* Number of pages starting at pg1 to journal */
3829 int ii;
3830
3831 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3832 ** header to be written between the pages journaled by this function.
3833 */
3834 assert( pPager->doNotSync==0 );
3835 pPager->doNotSync = 1;
3836
3837 /* This trick assumes that both the page-size and sector-size are
3838 ** an integer power of 2. It sets variable pg1 to the identifier
3839 ** of the first page of the sector pPg is located on.
3840 */
3841 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3842
danielk19773b8a05f2007-03-19 17:44:26 +00003843 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003844 if( pPg->pgno>nPageCount ){
3845 nPage = (pPg->pgno - pg1)+1;
3846 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3847 nPage = nPageCount+1-pg1;
3848 }else{
3849 nPage = nPagePerSector;
3850 }
3851 assert(nPage>0);
3852 assert(pg1<=pPg->pgno);
3853 assert((pg1+nPage)>pPg->pgno);
3854
3855 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3856 Pgno pg = pg1+ii;
3857 if( !pPager->aInJournal || pg==pPg->pgno ||
3858 pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
3859 ) {
3860 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003861 PgHdr *pPage;
3862 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003863 if( rc==SQLITE_OK ){
3864 rc = pager_write(pPage);
danielk19773b8a05f2007-03-19 17:44:26 +00003865 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003866 }
3867 }
3868 }
3869 }
3870
3871 assert( pPager->doNotSync==1 );
3872 pPager->doNotSync = 0;
3873 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003874 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003875 }
drh86f8c192007-08-22 00:39:19 +00003876 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003877 return rc;
3878}
3879
3880/*
drhaacc5432002-01-06 17:07:40 +00003881** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003882** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003883** to change the content of the page.
3884*/
danielk19777d3a6662006-01-23 16:21:05 +00003885#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003886int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00003887 return pPg->dirty;
3888}
danielk19777d3a6662006-01-23 16:21:05 +00003889#endif
drh6019e162001-07-02 17:51:45 +00003890
danielk1977b84f96f2005-01-20 11:32:23 +00003891#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00003892/*
drh001bbcb2003-03-19 03:14:00 +00003893** Replace the content of a single page with the information in the third
3894** argument.
3895*/
danielk19773b8a05f2007-03-19 17:44:26 +00003896int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
3897 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00003898 int rc;
3899
drh86f8c192007-08-22 00:39:19 +00003900 pagerEnter(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003901 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00003902 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003903 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00003904 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003905 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00003906 }
danielk19773b8a05f2007-03-19 17:44:26 +00003907 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00003908 }
drh86f8c192007-08-22 00:39:19 +00003909 pagerLeave(pPager);
drh001bbcb2003-03-19 03:14:00 +00003910 return rc;
3911}
danielk1977b84f96f2005-01-20 11:32:23 +00003912#endif
drh001bbcb2003-03-19 03:14:00 +00003913
3914/*
drh30e58752002-03-02 20:41:57 +00003915** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003916** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00003917** that page might be marked as dirty.
3918**
3919** The overlying software layer calls this routine when all of the data
3920** on the given page is unused. The pager marks the page as clean so
3921** that it does not get written to disk.
3922**
3923** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003924** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003925** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003926**
3927** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003928** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003929** will thereafter be ignored. This is necessary to avoid a problem
3930** where a page with data is added to the freelist during one part of
3931** a transaction then removed from the freelist during a later part
3932** of the same transaction and reused for some other purpose. When it
3933** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003934** the sqlite3PagerDontRollback() routine is called. But because the
3935** page contains critical data, we still need to be sure it gets
3936** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003937*/
drh538f5702007-04-13 02:14:30 +00003938void sqlite3PagerDontWrite(DbPage *pDbPage){
3939 PgHdr *pPg = pDbPage;
3940 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00003941
drhb7f91642004-10-31 02:22:47 +00003942 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00003943 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00003944 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00003945 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00003946 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00003947 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
3948 /* If this pages is the last page in the file and the file has grown
3949 ** during the current transaction, then do NOT mark the page as clean.
3950 ** When the database file grows, we must make sure that the last page
3951 ** gets written at least once so that the disk file will be the correct
3952 ** size. If you do not write this page and the size of the file
3953 ** on the disk ends up being too small, that can lead to database
3954 ** corruption during the next transaction.
3955 */
3956 }else{
drh538f5702007-04-13 02:14:30 +00003957 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
3958 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00003959 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00003960#ifdef SQLITE_CHECK_PAGES
3961 pPg->pageHash = pager_pagehash(pPg);
3962#endif
drh8124a302002-06-25 14:43:57 +00003963 }
drh30e58752002-03-02 20:41:57 +00003964 }
drh86f8c192007-08-22 00:39:19 +00003965 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00003966}
3967
3968/*
3969** A call to this routine tells the pager that if a rollback occurs,
3970** it is not necessary to restore the data on the given page. This
3971** means that the pager does not have to record the given page in the
3972** rollback journal.
drh538f5702007-04-13 02:14:30 +00003973**
3974** If we have not yet actually read the content of this page (if
3975** the PgHdr.needRead flag is set) then this routine acts as a promise
3976** that we will never need to read the page content in the future.
3977** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00003978*/
danielk19773b8a05f2007-03-19 17:44:26 +00003979void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003980 Pager *pPager = pPg->pPager;
3981
drh86f8c192007-08-22 00:39:19 +00003982 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00003983 assert( pPager->state>=PAGER_RESERVED );
3984 if( pPager->journalOpen==0 ) return;
drhb7f91642004-10-31 02:22:47 +00003985 if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
drh30e58752002-03-02 20:41:57 +00003986 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
3987 assert( pPager->aInJournal!=0 );
3988 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
3989 pPg->inJournal = 1;
drh538f5702007-04-13 02:14:30 +00003990 pPg->needRead = 0;
drhac69b052004-05-12 13:30:07 +00003991 if( pPager->stmtInUse ){
3992 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00003993 }
drh4f0c5872007-03-26 22:05:01 +00003994 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00003995 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00003996 }
danielk1977f35843b2007-04-07 15:03:17 +00003997 if( pPager->stmtInUse
3998 && !pageInStatement(pPg)
3999 && (int)pPg->pgno<=pPager->stmtSize
4000 ){
drh30e58752002-03-02 20:41:57 +00004001 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00004002 assert( pPager->aInStmt!=0 );
4003 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00004004 }
drh86f8c192007-08-22 00:39:19 +00004005 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004006}
4007
drhac69b052004-05-12 13:30:07 +00004008
drh30e58752002-03-02 20:41:57 +00004009/*
drh80e35f42007-03-30 14:06:34 +00004010** This routine is called to increment the database file change-counter,
4011** stored at byte 24 of the pager file.
4012*/
4013static int pager_incr_changecounter(Pager *pPager){
4014 PgHdr *pPgHdr;
4015 u32 change_counter;
4016 int rc;
4017
4018 if( !pPager->changeCountDone ){
4019 /* Open page 1 of the file for writing. */
4020 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4021 if( rc!=SQLITE_OK ) return rc;
4022 rc = sqlite3PagerWrite(pPgHdr);
4023 if( rc!=SQLITE_OK ) return rc;
4024
drh80e35f42007-03-30 14:06:34 +00004025 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004026 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004027 change_counter++;
4028 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
drh80e35f42007-03-30 14:06:34 +00004029 /* Release the page reference. */
4030 sqlite3PagerUnref(pPgHdr);
4031 pPager->changeCountDone = 1;
4032 }
4033 return SQLITE_OK;
4034}
4035
4036/*
4037** Sync the database file for the pager pPager. zMaster points to the name
4038** of a master journal file that should be written into the individual
4039** journal file. zMaster may be NULL, which is interpreted as no master
4040** journal (a single database transaction).
4041**
4042** This routine ensures that the journal is synced, all dirty pages written
4043** to the database file and the database file synced. The only thing that
4044** remains to commit the transaction is to delete the journal file (or
4045** master journal file if specified).
4046**
4047** Note that if zMaster==NULL, this does not overwrite a previous value
4048** passed to an sqlite3PagerCommitPhaseOne() call.
4049**
4050** If parameter nTrunc is non-zero, then the pager file is truncated to
4051** nTrunc pages (this is used by auto-vacuum databases).
4052*/
4053int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
4054 int rc = SQLITE_OK;
4055
4056 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4057 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004058 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004059
4060 /* If this is an in-memory db, or no pages have been written to, or this
4061 ** function has already been called, it is a no-op.
4062 */
4063 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4064 PgHdr *pPg;
4065 assert( pPager->journalOpen );
4066
4067 /* If a master journal file name has already been written to the
4068 ** journal file, then no sync is required. This happens when it is
4069 ** written, then the process fails to upgrade from a RESERVED to an
4070 ** EXCLUSIVE lock. The next time the process tries to commit the
4071 ** transaction the m-j name will have already been written.
4072 */
4073 if( !pPager->setMaster ){
4074 rc = pager_incr_changecounter(pPager);
4075 if( rc!=SQLITE_OK ) goto sync_exit;
4076#ifndef SQLITE_OMIT_AUTOVACUUM
4077 if( nTrunc!=0 ){
4078 /* If this transaction has made the database smaller, then all pages
4079 ** being discarded by the truncation must be written to the journal
4080 ** file.
4081 */
4082 Pgno i;
4083 int iSkip = PAGER_MJ_PGNO(pPager);
4084 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
4085 if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){
4086 rc = sqlite3PagerGet(pPager, i, &pPg);
4087 if( rc!=SQLITE_OK ) goto sync_exit;
4088 rc = sqlite3PagerWrite(pPg);
4089 sqlite3PagerUnref(pPg);
4090 if( rc!=SQLITE_OK ) goto sync_exit;
4091 }
4092 }
4093 }
4094#endif
4095 rc = writeMasterJournal(pPager, zMaster);
4096 if( rc!=SQLITE_OK ) goto sync_exit;
4097 rc = syncJournal(pPager);
4098 if( rc!=SQLITE_OK ) goto sync_exit;
4099 }
4100
4101#ifndef SQLITE_OMIT_AUTOVACUUM
4102 if( nTrunc!=0 ){
4103 rc = sqlite3PagerTruncate(pPager, nTrunc);
4104 if( rc!=SQLITE_OK ) goto sync_exit;
4105 }
4106#endif
4107
4108 /* Write all dirty pages to the database file */
4109 pPg = pager_get_all_dirty_pages(pPager);
4110 rc = pager_write_pagelist(pPg);
4111 if( rc!=SQLITE_OK ) goto sync_exit;
drh3cdd7d32007-03-30 14:46:01 +00004112 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004113
4114 /* Sync the database file. */
4115 if( !pPager->noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004116 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004117 }
4118 IOTRACE(("DBSYNC %p\n", pPager))
4119
4120 pPager->state = PAGER_SYNCED;
4121 }else if( MEMDB && nTrunc!=0 ){
4122 rc = sqlite3PagerTruncate(pPager, nTrunc);
4123 }
4124
4125sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004126 if( rc==SQLITE_IOERR_BLOCKED ){
4127 /* pager_incr_changecounter() may attempt to obtain an exclusive
4128 * lock to spill the cache and return IOERR_BLOCKED. But since
4129 * there is no chance the cache is inconsistent, it's
4130 * better to return SQLITE_BUSY.
4131 */
4132 rc = SQLITE_BUSY;
4133 }
drh86f8c192007-08-22 00:39:19 +00004134 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004135 return rc;
4136}
4137
4138
4139/*
drhed7c8552001-04-11 14:29:21 +00004140** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004141**
4142** If the commit fails for any reason, a rollback attempt is made
4143** and an error code is returned. If the commit worked, SQLITE_OK
4144** is returned.
drhed7c8552001-04-11 14:29:21 +00004145*/
drh80e35f42007-03-30 14:06:34 +00004146int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004147 int rc;
drhed7c8552001-04-11 14:29:21 +00004148 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004149
danielk1977efaaf572006-01-16 11:29:19 +00004150 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004151 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004152 }
drha6abd042004-06-09 17:37:22 +00004153 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004154 return SQLITE_ERROR;
4155 }
drh86f8c192007-08-22 00:39:19 +00004156 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004157 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004158 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004159 pPg = pager_get_all_dirty_pages(pPager);
4160 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004161 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4162 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004163 pPg->dirty = 0;
4164 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004165 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004166 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004167 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004168 pPg = pPg->pDirty;
4169 }
drh605ad8f2006-05-03 23:34:05 +00004170 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004171#ifndef NDEBUG
4172 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4173 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4174 assert( !pPg->alwaysRollback );
4175 assert( !pHist->pOrig );
4176 assert( !pHist->pStmt );
4177 }
4178#endif
drhac69b052004-05-12 13:30:07 +00004179 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004180 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004181 return SQLITE_OK;
4182 }
drh3cdd7d32007-03-30 14:46:01 +00004183 assert( pPager->journalOpen || !pPager->dirtyCache );
4184 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4185 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004186 rc = pager_error(pPager, rc);
4187 pagerLeave(pPager);
4188 return rc;
drhed7c8552001-04-11 14:29:21 +00004189}
4190
4191/*
drha6abd042004-06-09 17:37:22 +00004192** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004193** All in-memory cache pages revert to their original data contents.
4194** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004195**
4196** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004197** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004198** process is writing trash into the journal file (SQLITE_CORRUPT) or
4199** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4200** codes are returned for all these occasions. Otherwise,
4201** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004202*/
danielk19773b8a05f2007-03-19 17:44:26 +00004203int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004204 int rc;
drh4f0c5872007-03-26 22:05:01 +00004205 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004206 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004207 PgHdr *p;
4208 for(p=pPager->pAll; p; p=p->pNextAll){
4209 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004210 assert( !p->alwaysRollback );
4211 if( !p->dirty ){
4212 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4213 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4214 continue;
4215 }
4216
drhac69b052004-05-12 13:30:07 +00004217 pHist = PGHDR_TO_HIST(p, pPager);
4218 if( pHist->pOrig ){
4219 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004220 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004221 }else{
drh4f0c5872007-03-26 22:05:01 +00004222 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004223 }
4224 clearHistory(pHist);
4225 p->dirty = 0;
4226 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004227 pHist->inStmt = 0;
4228 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004229 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004230 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004231 }
drhac69b052004-05-12 13:30:07 +00004232 }
drh605ad8f2006-05-03 23:34:05 +00004233 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004234 pPager->pStmt = 0;
4235 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004236 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004237 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004238 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004239 return SQLITE_OK;
4240 }
4241
drh86f8c192007-08-22 00:39:19 +00004242 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004243 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004244 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004245 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004246 return rc;
4247 }
drhdb48ee02003-01-16 13:42:43 +00004248
danielk1977efaaf572006-01-16 11:29:19 +00004249 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004250 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004251 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004252 }
drh86f8c192007-08-22 00:39:19 +00004253 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004254 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004255 }
drha6abd042004-06-09 17:37:22 +00004256 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004257 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004258 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004259 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004260 if( rc==SQLITE_OK ){
4261 rc = rc2;
4262 }
4263 }else{
danielk1977e277be02007-03-23 18:12:06 +00004264 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004265 }
danielk1977e180dd92007-04-05 17:15:52 +00004266 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004267 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004268
4269 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4270 ** cache. So call pager_error() on the way out to make any error
4271 ** persistent.
4272 */
drh86f8c192007-08-22 00:39:19 +00004273 rc = pager_error(pPager, rc);
4274 pagerLeave(pPager);
4275 return rc;
drh98808ba2001-10-18 12:34:46 +00004276}
drhd9b02572001-04-15 00:37:09 +00004277
4278/*
drh5e00f6c2001-09-13 13:46:56 +00004279** Return TRUE if the database file is opened read-only. Return FALSE
4280** if the database is (in theory) writable.
4281*/
danielk19773b8a05f2007-03-19 17:44:26 +00004282int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004283 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004284}
4285
4286/*
drh0f7eb612006-08-08 13:51:43 +00004287** Return the number of references to the pager.
4288*/
danielk19773b8a05f2007-03-19 17:44:26 +00004289int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004290 return pPager->nRef;
4291}
4292
4293#ifdef SQLITE_TEST
4294/*
drhd9b02572001-04-15 00:37:09 +00004295** This routine is used for testing and analysis only.
4296*/
danielk19773b8a05f2007-03-19 17:44:26 +00004297int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004298 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004299 a[0] = pPager->nRef;
4300 a[1] = pPager->nPage;
4301 a[2] = pPager->mxPage;
4302 a[3] = pPager->dbSize;
4303 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004304 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004305 a[6] = pPager->nHit;
4306 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004307 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004308 a[9] = pPager->nRead;
4309 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004310 return a;
4311}
drh0f7eb612006-08-08 13:51:43 +00004312#endif
drhdd793422001-06-28 01:54:48 +00004313
drhfa86c412002-02-02 15:01:15 +00004314/*
drhac69b052004-05-12 13:30:07 +00004315** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004316**
4317** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004318** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004319** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004320*/
drh86f8c192007-08-22 00:39:19 +00004321static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004322 int rc;
drhac69b052004-05-12 13:30:07 +00004323 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004324 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004325 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004326 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004327 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004328 pPager->stmtInUse = 1;
4329 pPager->stmtSize = pPager->dbSize;
4330 return SQLITE_OK;
4331 }
drhda47d772002-12-02 04:25:19 +00004332 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004333 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004334 return SQLITE_OK;
4335 }
drhfa86c412002-02-02 15:01:15 +00004336 assert( pPager->journalOpen );
drh17435752007-08-16 04:30:38 +00004337 pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhac69b052004-05-12 13:30:07 +00004338 if( pPager->aInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004339 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004340 return SQLITE_NOMEM;
4341 }
drh968af522003-02-11 14:55:40 +00004342#ifndef NDEBUG
drh054889e2005-11-30 03:20:31 +00004343 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
drhac69b052004-05-12 13:30:07 +00004344 if( rc ) goto stmt_begin_failed;
danielk197776572402004-06-25 02:38:54 +00004345 assert( pPager->stmtJSize == pPager->journalOff );
drh968af522003-02-11 14:55:40 +00004346#endif
danielk197776572402004-06-25 02:38:54 +00004347 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004348 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004349 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004350 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004351 if( !pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00004352 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, 0);
drhac69b052004-05-12 13:30:07 +00004353 if( rc ) goto stmt_begin_failed;
4354 pPager->stmtOpen = 1;
4355 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004356 }
drhac69b052004-05-12 13:30:07 +00004357 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004358 return SQLITE_OK;
4359
drhac69b052004-05-12 13:30:07 +00004360stmt_begin_failed:
4361 if( pPager->aInStmt ){
drh17435752007-08-16 04:30:38 +00004362 sqlite3_free(pPager->aInStmt);
drhac69b052004-05-12 13:30:07 +00004363 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004364 }
4365 return rc;
4366}
drh86f8c192007-08-22 00:39:19 +00004367int sqlite3PagerStmtBegin(Pager *pPager){
4368 int rc;
4369 pagerEnter(pPager);
4370 rc = pagerStmtBegin(pPager);
4371 pagerLeave(pPager);
4372 return rc;
4373}
drhfa86c412002-02-02 15:01:15 +00004374
4375/*
drhac69b052004-05-12 13:30:07 +00004376** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004377*/
danielk19773b8a05f2007-03-19 17:44:26 +00004378int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004379 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004380 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004381 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004382 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004383 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004384 /* sqlite3OsTruncate(pPager->stfd, 0); */
drh17435752007-08-16 04:30:38 +00004385 sqlite3_free( pPager->aInStmt );
drhac69b052004-05-12 13:30:07 +00004386 pPager->aInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004387 }else{
4388 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004389 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004390 pNext = pHist->pNextStmt;
4391 assert( pHist->inStmt );
4392 pHist->inStmt = 0;
4393 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004394 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004395 pHist->pStmt = 0;
4396 }
4397 }
4398 pPager->stmtNRec = 0;
4399 pPager->stmtInUse = 0;
4400 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004401 }
drhac69b052004-05-12 13:30:07 +00004402 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004403 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004404 return SQLITE_OK;
4405}
4406
4407/*
drhac69b052004-05-12 13:30:07 +00004408** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004409*/
danielk19773b8a05f2007-03-19 17:44:26 +00004410int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004411 int rc;
drh86f8c192007-08-22 00:39:19 +00004412 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004413 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004414 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004415 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004416 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004417 PgHistory *pHist;
4418 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4419 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004420 if( pHist->pStmt ){
4421 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004422 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004423 pHist->pStmt = 0;
4424 }
4425 }
4426 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004427 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004428 rc = SQLITE_OK;
4429 }else{
4430 rc = pager_stmt_playback(pPager);
4431 }
danielk19773b8a05f2007-03-19 17:44:26 +00004432 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004433 }else{
4434 rc = SQLITE_OK;
4435 }
drhac69b052004-05-12 13:30:07 +00004436 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004437 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004438 return rc;
4439}
4440
drh73509ee2003-04-06 20:44:45 +00004441/*
4442** Return the full pathname of the database file.
4443*/
danielk19773b8a05f2007-03-19 17:44:26 +00004444const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004445 return pPager->zFilename;
4446}
4447
drhb20ea9d2004-02-09 01:20:36 +00004448/*
danielk19775865e3d2004-06-14 06:03:57 +00004449** Return the directory of the database file.
4450*/
danielk19773b8a05f2007-03-19 17:44:26 +00004451const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004452 return pPager->zDirectory;
4453}
4454
4455/*
4456** Return the full pathname of the journal file.
4457*/
danielk19773b8a05f2007-03-19 17:44:26 +00004458const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004459 return pPager->zJournal;
4460}
4461
4462/*
drh2c8997b2005-08-27 16:36:48 +00004463** Return true if fsync() calls are disabled for this pager. Return FALSE
4464** if fsync()s are executed normally.
4465*/
danielk19773b8a05f2007-03-19 17:44:26 +00004466int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004467 return pPager->noSync;
4468}
4469
drh7c4ac0c2007-04-05 11:25:58 +00004470#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004471/*
drhb20ea9d2004-02-09 01:20:36 +00004472** Set the codec for this pager
4473*/
danielk19773b8a05f2007-03-19 17:44:26 +00004474void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004475 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004476 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004477 void *pCodecArg
4478){
4479 pPager->xCodec = xCodec;
4480 pPager->pCodecArg = pCodecArg;
4481}
drh7c4ac0c2007-04-05 11:25:58 +00004482#endif
drhb20ea9d2004-02-09 01:20:36 +00004483
danielk1977687566d2004-11-02 12:56:41 +00004484#ifndef SQLITE_OMIT_AUTOVACUUM
4485/*
drh5e385312007-06-16 04:42:12 +00004486** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004487**
drh5e385312007-06-16 04:42:12 +00004488** There must be no references to the page previously located at
4489** pgno (which we call pPgOld) though that page is allowed to be
4490** in cache. If the page previous located at pgno is not already
4491** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004492**
drh5e385312007-06-16 04:42:12 +00004493** References to the page pPg remain valid. Updating any
4494** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004495** allocated along with the page) is the responsibility of the caller.
4496**
danielk19775fd057a2005-03-09 13:09:43 +00004497** A transaction must be active when this routine is called. It used to be
4498** required that a statement transaction was not active, but this restriction
4499** has been removed (CREATE INDEX needs to move a page when a statement
4500** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00004501*/
danielk19773b8a05f2007-03-19 17:44:26 +00004502int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00004503 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00004504 int h;
danielk197794daf7f2004-11-08 09:26:09 +00004505 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004506
drh86f8c192007-08-22 00:39:19 +00004507 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004508 assert( pPg->nRef>0 );
4509
drh4f0c5872007-03-26 22:05:01 +00004510 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00004511 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00004512 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004513
danielk1977b4626a32007-04-28 15:47:43 +00004514 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00004515 if( pPg->needSync ){
4516 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00004517 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00004518 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00004519 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004520 }
4521
danielk1977687566d2004-11-02 12:56:41 +00004522 /* Unlink pPg from it's hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00004523 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00004524
danielk1977ef73ee92004-11-06 12:26:07 +00004525 /* If the cache contains a page with page-number pgno, remove it
danielk1977599fcba2004-11-08 07:13:13 +00004526 ** from it's hash chain. Also, if the PgHdr.needSync was set for
4527 ** page pgno before the 'move' operation, it needs to be retained
4528 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004529 */
drh5e385312007-06-16 04:42:12 +00004530 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00004531 pPgOld = pager_lookup(pPager, pgno);
4532 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00004533 assert( pPgOld->nRef==0 );
4534 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00004535 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00004536 pPg->needSync = pPgOld->needSync;
4537 }else{
4538 pPg->needSync = 0;
4539 }
4540 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
4541 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drh5e385312007-06-16 04:42:12 +00004542 }else{
4543 pPg->inJournal = 0;
drh732c8172007-06-16 11:17:45 +00004544 assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize );
danielk1977687566d2004-11-02 12:56:41 +00004545 }
4546
danielk1977f5fdda82004-11-03 08:44:05 +00004547 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00004548 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00004549 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00004550 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00004551 if( pPager->aHash[h] ){
4552 assert( pPager->aHash[h]->pPrevHash==0 );
4553 pPager->aHash[h]->pPrevHash = pPg;
4554 }
4555 pPg->pNextHash = pPager->aHash[h];
4556 pPager->aHash[h] = pPg;
4557 pPg->pPrevHash = 0;
4558
drh605ad8f2006-05-03 23:34:05 +00004559 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004560 pPager->dirtyCache = 1;
4561
danielk197794daf7f2004-11-08 09:26:09 +00004562 if( needSyncPgno ){
4563 /* If needSyncPgno is non-zero, then the journal file needs to be
4564 ** sync()ed before any data is written to database file page needSyncPgno.
4565 ** Currently, no such page exists in the page-cache and the
4566 ** Pager.aInJournal bit has been set. This needs to be remedied by loading
4567 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00004568 **
danielk19773b8a05f2007-03-19 17:44:26 +00004569 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004570 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004571 */
4572 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004573 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004574 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004575 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004576 if( rc!=SQLITE_OK ) return rc;
danielk1977ae825582004-11-23 09:06:55 +00004577 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00004578 pPgHdr->needSync = 1;
4579 pPgHdr->inJournal = 1;
4580 makeDirty(pPgHdr);
4581 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004582 }
4583
drh86f8c192007-08-22 00:39:19 +00004584 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004585 return SQLITE_OK;
4586}
4587#endif
4588
danielk19773b8a05f2007-03-19 17:44:26 +00004589/*
4590** Return a pointer to the data for the specified page.
4591*/
4592void *sqlite3PagerGetData(DbPage *pPg){
4593 return PGHDR_TO_DATA(pPg);
4594}
4595
4596/*
4597** Return a pointer to the Pager.nExtra bytes of "extra" space
4598** allocated along with the specified page.
4599*/
4600void *sqlite3PagerGetExtra(DbPage *pPg){
4601 Pager *pPager = pPg->pPager;
4602 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
4603}
4604
danielk197741483462007-03-24 16:45:04 +00004605/*
4606** Get/set the locking-mode for this pager. Parameter eMode must be one
4607** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4608** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4609** the locking-mode is set to the value specified.
4610**
4611** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4612** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4613** locking-mode.
4614*/
4615int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004616 assert( eMode==PAGER_LOCKINGMODE_QUERY
4617 || eMode==PAGER_LOCKINGMODE_NORMAL
4618 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4619 assert( PAGER_LOCKINGMODE_QUERY<0 );
4620 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4621 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00004622 pPager->exclusiveMode = eMode;
4623 }
4624 return (int)pPager->exclusiveMode;
4625}
4626
dougcurrie81c95ef2004-06-18 23:21:47 +00004627#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00004628/*
4629** Return the current state of the file lock for the given pager.
4630** The return value is one of NO_LOCK, SHARED_LOCK, RESERVED_LOCK,
4631** PENDING_LOCK, or EXCLUSIVE_LOCK.
4632*/
danielk19773b8a05f2007-03-19 17:44:26 +00004633int sqlite3PagerLockstate(Pager *pPager){
drh054889e2005-11-30 03:20:31 +00004634 return sqlite3OsLockState(pPager->fd);
drh89ac8c12004-06-09 14:17:20 +00004635}
4636#endif
4637
danielk197793758c82005-01-21 08:13:14 +00004638#ifdef SQLITE_DEBUG
drhdd793422001-06-28 01:54:48 +00004639/*
4640** Print a listing of all referenced pages and their ref count.
4641*/
danielk19773b8a05f2007-03-19 17:44:26 +00004642void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00004643 PgHdr *pPg;
4644 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4645 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00004646 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
4647 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00004648 }
4649}
4650#endif
drh2e66f0b2005-04-28 17:18:48 +00004651
4652#endif /* SQLITE_OMIT_DISKIO */