blob: 504bfcbf1b0795c61044cbebee43fba706e61256 [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**
danielk19774cd2cd52007-08-23 14:48:23 +000021** @(#) $Id: pager.c,v 1.371 2007/08/23 14:48:24 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000026#include <string.h>
drhed7c8552001-04-11 14:29:21 +000027
28/*
drhdb48ee02003-01-16 13:42:43 +000029** Macros for troubleshooting. Normally turned off
30*/
danielk1977466be562004-06-10 02:16:01 +000031#if 0
drhd3627af2006-12-18 18:34:51 +000032#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000033#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
34#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
35#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
36#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
37#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000038#else
drh4f0c5872007-03-26 22:05:01 +000039#define PAGERTRACE1(X)
40#define PAGERTRACE2(X,Y)
41#define PAGERTRACE3(X,Y,Z)
42#define PAGERTRACE4(X,Y,Z,W)
43#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000044#endif
45
danielk1977599fcba2004-11-08 07:13:13 +000046/*
drh4f0c5872007-03-26 22:05:01 +000047** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000048** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000049**
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 );
drhed138fb2007-08-22 22:04:37 +0000541 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +0000542 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drhed138fb2007-08-22 22:04:37 +0000543 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000544 if( aHash==0 ){
545 /* Failure to rehash is not an error. It is only a performance hit. */
546 return;
547 }
drh17435752007-08-16 04:30:38 +0000548 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000549 pPager->nHash = N;
550 pPager->aHash = aHash;
551 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000552 int h;
553 if( pPg->pgno==0 ){
554 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
555 continue;
556 }
557 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000558 pPg->pNextHash = aHash[h];
559 if( aHash[h] ){
560 aHash[h]->pPrevHash = pPg;
561 }
562 aHash[h] = pPg;
563 pPg->pPrevHash = 0;
564 }
565}
566
drhdd793422001-06-28 01:54:48 +0000567/*
drh34e79ce2004-02-08 06:05:46 +0000568** Read a 32-bit integer from the given file descriptor. Store the integer
569** that is read in *pRes. Return SQLITE_OK if everything worked, or an
570** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000571**
572** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000573*/
danielk197762079062007-08-15 17:08:46 +0000574static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000575 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000576 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000577 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000578 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000579 }
drh94f33312002-08-12 12:29:56 +0000580 return rc;
581}
582
583/*
drh97b57482006-01-10 20:32:32 +0000584** Write a 32-bit integer into a string buffer in big-endian byte order.
585*/
drha3152892007-05-05 11:48:52 +0000586#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000587
588/*
drh34e79ce2004-02-08 06:05:46 +0000589** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
590** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000591*/
danielk197762079062007-08-15 17:08:46 +0000592static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000593 char ac[4];
drh97b57482006-01-10 20:32:32 +0000594 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000595 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000596}
597
drh2554f8b2003-01-22 01:26:44 +0000598/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000599** If file pFd is open, call sqlite3OsUnlock() on it.
600*/
601static int osUnlock(sqlite3_file *pFd, int eLock){
602 if( !pFd->pMethods ){
603 return SQLITE_OK;
604 }
605 return sqlite3OsUnlock(pFd, eLock);
606}
607
608/*
danielk1977c7b60172007-08-22 11:22:03 +0000609** This function determines whether or not the atomic-write optimization
610** can be used with this pager. The optimization can be used if:
611**
612** (a) the value returned by OsDeviceCharacteristics() indicates that
613** a database page may be written atomically, and
614** (b) the value returned by OsSectorSize() is less than or equal
615** to the page size.
616**
617** If the optimization cannot be used, 0 is returned. If it can be used,
618** then the value returned is the size of the journal file when it
619** contains rollback data for exactly one page.
620*/
621#ifdef SQLITE_ENABLE_ATOMIC_WRITE
622static int jrnlBufferSize(Pager *pPager){
623 int dc; /* Device characteristics */
624 int nSector; /* Sector size */
625 int nPage; /* Page size */
626 sqlite3_file *fd = pPager->fd;
627
628 if( fd->pMethods ){
629 dc = sqlite3OsDeviceCharacteristics(fd);
630 nSector = sqlite3OsSectorSize(fd);
631 nPage = pPager->pageSize;
632 }
633
634 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
635 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
636
danielk1977f8940ae2007-08-23 11:07:10 +0000637 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000638 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
639 }
640 return 0;
641}
642#endif
643
644/*
danielk1977aef0bf62005-12-30 16:28:01 +0000645** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000646** code. The first argument is a pointer to the pager structure, the
647** second the error-code about to be returned by a pager API function.
648** The value returned is a copy of the second argument to this function.
649**
drh4f0ee682007-03-30 20:43:40 +0000650** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977a96a7102006-01-16 12:46:41 +0000651** the error becomes persistent. All subsequent API calls on this Pager
652** will immediately return the same error code.
danielk1977aef0bf62005-12-30 16:28:01 +0000653*/
654static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000655 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000656 assert(
657 pPager->errCode==SQLITE_FULL ||
658 pPager->errCode==SQLITE_OK ||
659 (pPager->errCode & 0xff)==SQLITE_IOERR
660 );
danielk1977979f38e2007-03-27 16:19:51 +0000661 if(
drh4ac285a2006-09-15 07:28:50 +0000662 rc2==SQLITE_FULL ||
663 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000664 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000665 ){
666 pPager->errCode = rc;
danielk1977aef0bf62005-12-30 16:28:01 +0000667 }
668 return rc;
669}
670
drh477731b2007-06-16 03:06:27 +0000671/*
672** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
673** on the cache using a hash function. This is used for testing
674** and debugging only.
675*/
danielk19773c407372005-02-15 02:54:14 +0000676#ifdef SQLITE_CHECK_PAGES
677/*
678** Return a 32-bit hash of the page data for pPage.
679*/
drh477731b2007-06-16 03:06:27 +0000680static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000681 u32 hash = 0;
682 int i;
drh477731b2007-06-16 03:06:27 +0000683 for(i=0; i<nByte; i++){
684 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000685 }
686 return hash;
687}
drh477731b2007-06-16 03:06:27 +0000688static u32 pager_pagehash(PgHdr *pPage){
689 return pager_datahash(pPage->pPager->pageSize,
690 (unsigned char *)PGHDR_TO_DATA(pPage));
691}
danielk19773c407372005-02-15 02:54:14 +0000692
693/*
694** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
695** is defined, and NDEBUG is not defined, an assert() statement checks
696** that the page is either dirty or still matches the calculated page-hash.
697*/
698#define CHECK_PAGE(x) checkPage(x)
699static void checkPage(PgHdr *pPg){
700 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000701 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000702 pPg->pageHash==pager_pagehash(pPg) );
703}
704
705#else
drh8ffa8172007-06-18 17:25:17 +0000706#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000707#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000708#define CHECK_PAGE(x)
709#endif
710
drhed7c8552001-04-11 14:29:21 +0000711/*
danielk197776572402004-06-25 02:38:54 +0000712** When this is called the journal file for pager pPager must be open.
713** The master journal file name is read from the end of the file and
drh17435752007-08-16 04:30:38 +0000714** written into memory obtained from sqlite3_malloc(). *pzMaster is
danielk197776572402004-06-25 02:38:54 +0000715** set to point at the memory and SQLITE_OK returned. The caller must
drh17435752007-08-16 04:30:38 +0000716** sqlite3_free() *pzMaster.
danielk197776572402004-06-25 02:38:54 +0000717**
718** If no master journal file name is present *pzMaster is set to 0 and
719** SQLITE_OK returned.
720*/
danielk197762079062007-08-15 17:08:46 +0000721static int readMasterJournal(sqlite3_file *pJrnl, char **pzMaster){
danielk197776572402004-06-25 02:38:54 +0000722 int rc;
723 u32 len;
drheb206252004-10-01 02:00:31 +0000724 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000725 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000726 int i;
danielk197776572402004-06-25 02:38:54 +0000727 unsigned char aMagic[8]; /* A buffer to hold the magic header */
728
729 *pzMaster = 0;
730
drh054889e2005-11-30 03:20:31 +0000731 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000732 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000733
danielk197762079062007-08-15 17:08:46 +0000734 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000735 if( rc!=SQLITE_OK ) return rc;
736
danielk197762079062007-08-15 17:08:46 +0000737 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000738 if( rc!=SQLITE_OK ) return rc;
739
danielk197762079062007-08-15 17:08:46 +0000740 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000741 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
742
drh17435752007-08-16 04:30:38 +0000743 *pzMaster = (char *)sqlite3MallocZero(len+1);
danielk197776572402004-06-25 02:38:54 +0000744 if( !*pzMaster ){
745 return SQLITE_NOMEM;
746 }
danielk197762079062007-08-15 17:08:46 +0000747 rc = sqlite3OsRead(pJrnl, *pzMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000748 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000749 sqlite3_free(*pzMaster);
danielk197776572402004-06-25 02:38:54 +0000750 *pzMaster = 0;
751 return rc;
752 }
danielk1977cafadba2004-06-25 11:11:54 +0000753
754 /* See if the checksum matches the master journal name */
755 for(i=0; i<len; i++){
danielk19778191bff2004-06-28 04:52:30 +0000756 cksum -= (*pzMaster)[i];
danielk1977cafadba2004-06-25 11:11:54 +0000757 }
danielk19778191bff2004-06-28 04:52:30 +0000758 if( cksum ){
759 /* If the checksum doesn't add up, then one or more of the disk sectors
760 ** containing the master journal filename is corrupted. This means
761 ** definitely roll back, so just return SQLITE_OK and report a (nul)
762 ** master-journal filename.
763 */
drh17435752007-08-16 04:30:38 +0000764 sqlite3_free(*pzMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000765 *pzMaster = 0;
danielk1977aca790a2005-01-13 11:07:52 +0000766 }else{
767 (*pzMaster)[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000768 }
danielk197776572402004-06-25 02:38:54 +0000769
770 return SQLITE_OK;
771}
772
773/*
774** Seek the journal file descriptor to the next sector boundary where a
775** journal header may be read or written. Pager.journalOff is updated with
776** the new seek offset.
777**
778** i.e for a sector size of 512:
779**
780** Input Offset Output Offset
781** ---------------------------------------
782** 0 0
783** 512 512
784** 100 512
785** 2000 2048
786**
787*/
danielk197762079062007-08-15 17:08:46 +0000788static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000789 i64 offset = 0;
790 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000791 if( c ){
792 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
793 }
794 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
795 assert( offset>=c );
796 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
797 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000798}
799
800/*
801** The journal file must be open when this routine is called. A journal
802** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
803** current location.
804**
805** The format for the journal header is as follows:
806** - 8 bytes: Magic identifying journal format.
807** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
808** - 4 bytes: Random number used for page hash.
809** - 4 bytes: Initial database page count.
810** - 4 bytes: Sector size used by the process that wrote this journal.
811**
danielk1977f42f25c2004-06-25 07:21:28 +0000812** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000813*/
814static int writeJournalHdr(Pager *pPager){
drh97b57482006-01-10 20:32:32 +0000815 char zHeader[sizeof(aJournalMagic)+16];
danielk19774099f6e2007-03-19 11:25:20 +0000816 int rc;
danielk197776572402004-06-25 02:38:54 +0000817
danielk19774099f6e2007-03-19 11:25:20 +0000818 if( pPager->stmtHdrOff==0 ){
819 pPager->stmtHdrOff = pPager->journalOff;
820 }
821
danielk197762079062007-08-15 17:08:46 +0000822 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000823 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000824
drh97b57482006-01-10 20:32:32 +0000825 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000826
827 /*
828 ** Write the nRec Field - the number of page records that follow this
829 ** journal header. Normally, zero is written to this value at this time.
830 ** After the records are added to the journal (and the journal synced,
831 ** if in full-sync mode), the zero is overwritten with the true number
832 ** of records (see syncJournal()).
833 **
834 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
835 ** reading the journal this value tells SQLite to assume that the
836 ** rest of the journal file contains valid page records. This assumption
837 ** is dangerous, as if a failure occured whilst writing to the journal
838 ** file it may contain some garbage data. There are two scenarios
839 ** where this risk can be ignored:
840 **
841 ** * When the pager is in no-sync mode. Corruption can follow a
842 ** power failure in this case anyway.
843 **
844 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
845 ** that garbage data is never appended to the journal file.
846 */
847 assert(pPager->fd->pMethods||pPager->noSync);
848 if( (pPager->noSync)
849 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
850 ){
851 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
852 }else{
853 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
854 }
855
drh97b57482006-01-10 20:32:32 +0000856 /* The random check-hash initialiser */
857 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
858 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
859 /* The initial database size */
860 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
861 /* The assumed sector size for this process */
862 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drhb0603412007-02-28 04:47:26 +0000863 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
danielk197762079062007-08-15 17:08:46 +0000864 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
865 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197776572402004-06-25 02:38:54 +0000866
867 /* The journal header has been written successfully. Seek the journal
868 ** file descriptor to the end of the journal header sector.
869 */
870 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +0000871 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
danielk197762079062007-08-15 17:08:46 +0000872 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
danielk197776572402004-06-25 02:38:54 +0000873 }
874 return rc;
875}
876
877/*
878** The journal file must be open when this is called. A journal header file
879** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
880** file. See comments above function writeJournalHdr() for a description of
881** the journal header format.
882**
883** If the header is read successfully, *nRec is set to the number of
884** page records following this header and *dbSize is set to the size of the
885** database before the transaction began, in pages. Also, pPager->cksumInit
886** is set to the value read from the journal header. SQLITE_OK is returned
887** in this case.
888**
889** If the journal header file appears to be corrupted, SQLITE_DONE is
890** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
891** cannot be read from the journal file an error code is returned.
892*/
893static int readJournalHdr(
894 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000895 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000896 u32 *pNRec,
897 u32 *pDbSize
898){
899 int rc;
900 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000901 i64 jrnlOff;
danielk197776572402004-06-25 02:38:54 +0000902
danielk197762079062007-08-15 17:08:46 +0000903 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000904 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
905 return SQLITE_DONE;
906 }
danielk197762079062007-08-15 17:08:46 +0000907 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000908
danielk197762079062007-08-15 17:08:46 +0000909 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000910 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000911 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000912
913 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
914 return SQLITE_DONE;
915 }
916
danielk197762079062007-08-15 17:08:46 +0000917 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000918 if( rc ) return rc;
919
danielk197762079062007-08-15 17:08:46 +0000920 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000921 if( rc ) return rc;
922
danielk197762079062007-08-15 17:08:46 +0000923 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000924 if( rc ) return rc;
925
926 /* Update the assumed sector-size to match the value used by
927 ** the process that created this journal. If this journal was
928 ** created by a process other than this one, then this routine
929 ** is being called from within pager_playback(). The local value
930 ** of Pager.sectorSize is restored at the end of that routine.
931 */
danielk197762079062007-08-15 17:08:46 +0000932 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000933 if( rc ) return rc;
934
935 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000936 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000937}
938
939
940/*
941** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000942** pPager at the current location. The master journal name must be the last
943** thing written to a journal file. If the pager is in full-sync mode, the
944** journal file descriptor is advanced to the next sector boundary before
945** anything is written. The format is:
946**
947** + 4 bytes: PAGER_MJ_PGNO.
948** + N bytes: length of master journal name.
949** + 4 bytes: N
950** + 4 bytes: Master journal name checksum.
951** + 8 bytes: aJournalMagic[].
952**
953** The master journal page checksum is the sum of the bytes in the master
954** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000955**
956** If zMaster is a NULL pointer (occurs for a single database transaction),
957** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000958*/
959static int writeMasterJournal(Pager *pPager, const char *zMaster){
960 int rc;
961 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000962 int i;
danielk197762079062007-08-15 17:08:46 +0000963 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +0000964 u32 cksum = 0;
965 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000966
967 if( !zMaster || pPager->setMaster) return SQLITE_OK;
968 pPager->setMaster = 1;
969
970 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000971 for(i=0; i<len; i++){
972 cksum += zMaster[i];
973 }
danielk197776572402004-06-25 02:38:54 +0000974
975 /* If in full-sync mode, advance to the next disk sector before writing
976 ** the master journal name. This is in case the previous page written to
977 ** the journal has already been synced.
978 */
979 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000980 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000981 }
danielk197762079062007-08-15 17:08:46 +0000982 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000983 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000984
danielk197762079062007-08-15 17:08:46 +0000985 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000986 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000987 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000988
danielk197762079062007-08-15 17:08:46 +0000989 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000990 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000991 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000992
drh97b57482006-01-10 20:32:32 +0000993 put32bits(zBuf, len);
994 put32bits(&zBuf[4], cksum);
995 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000996 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +0000997 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +0000998 return rc;
999}
1000
1001/*
drh03eb96a2002-11-10 23:32:56 +00001002** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001003** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001004**
1005** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001006** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001007** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001008** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001009*/
drh3aac2dd2004-04-26 14:10:20 +00001010static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001011 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001012 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1013 assert( MEMDB );
1014 if( !pHist->inStmt ){
1015 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1016 if( pPager->pStmt ){
1017 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1018 }
1019 pHist->pNextStmt = pPager->pStmt;
1020 pPager->pStmt = pPg;
1021 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001022 }
drh03eb96a2002-11-10 23:32:56 +00001023}
1024
1025/*
drhed7c8552001-04-11 14:29:21 +00001026** Find a page in the hash table given its page number. Return
1027** a pointer to the page or NULL if not found.
1028*/
drhd9b02572001-04-15 00:37:09 +00001029static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001030 PgHdr *p;
1031 if( pPager->aHash==0 ) return 0;
1032 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001033 while( p && p->pgno!=pgno ){
1034 p = p->pNextHash;
1035 }
1036 return p;
1037}
1038
1039/*
drh1aa2d8b2007-01-03 15:34:29 +00001040** Unlock the database file.
drh1aa2d8b2007-01-03 15:34:29 +00001041*/
1042static void pager_unlock(Pager *pPager){
danielk197741483462007-03-24 16:45:04 +00001043 if( !pPager->exclusiveMode ){
1044 if( !MEMDB ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001045 osUnlock(pPager->fd, NO_LOCK);
danielk197741483462007-03-24 16:45:04 +00001046 pPager->dbSize = -1;
1047 IOTRACE(("UNLOCK %p\n", pPager))
1048 }
1049 pPager->state = PAGER_UNLOCK;
1050 pPager->changeCountDone = 0;
drh1aa2d8b2007-01-03 15:34:29 +00001051 }
danielk1977e277be02007-03-23 18:12:06 +00001052}
1053
1054/*
1055** Execute a rollback if a transaction is active and unlock the
1056** database file. This is a no-op if the pager has already entered
1057** the error-state.
1058*/
danielk1977334cdb62007-03-26 08:05:12 +00001059static void pagerUnlockAndRollback(Pager *p){
1060 if( p->errCode ) return;
danielk1977c5859712007-03-26 12:26:27 +00001061 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
danielk1977334cdb62007-03-26 08:05:12 +00001062 if( p->state>=PAGER_RESERVED ){
1063 sqlite3PagerRollback(p);
danielk1977e277be02007-03-23 18:12:06 +00001064 }
danielk1977334cdb62007-03-26 08:05:12 +00001065 pager_unlock(p);
1066 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1067 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drh1aa2d8b2007-01-03 15:34:29 +00001068}
1069
1070
1071/*
danielk1977e180dd92007-04-05 17:15:52 +00001072** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001073** sets the state of the pager back to what it was when it was first
1074** opened. Any outstanding pages are invalidated and subsequent attempts
1075** to access those pages will likely result in a coredump.
1076*/
drhd9b02572001-04-15 00:37:09 +00001077static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001078 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001079 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001080 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001081 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1082 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001083 pNext = pPg->pNextAll;
drh17435752007-08-16 04:30:38 +00001084 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001085 }
danielk1977b94bf852007-03-19 13:53:37 +00001086 pPager->pStmt = 0;
drhed7c8552001-04-11 14:29:21 +00001087 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +00001088 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +00001089 pPager->pLast = 0;
1090 pPager->pAll = 0;
drh8ca0c722006-05-07 17:49:38 +00001091 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001092 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001093 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001094 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001095 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001096}
1097
1098/*
drh80e35f42007-03-30 14:06:34 +00001099** This routine ends a transaction. A transaction is ended by either
1100** a COMMIT or a ROLLBACK.
1101**
drhed7c8552001-04-11 14:29:21 +00001102** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001103** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1104** the database lock and acquires a SHARED lock in its place if that is
1105** the appropriate thing to do. Release locks usually is appropriate,
1106** unless we are in exclusive access mode or unless this is a
1107** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1108**
1109** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001110**
1111** TODO: Consider keeping the journal file open for temporary databases.
1112** This might give a performance improvement on windows where opening
1113** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001114*/
drh80e35f42007-03-30 14:06:34 +00001115static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001116 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001117 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001118 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001119 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001120 if( pPager->state<PAGER_RESERVED ){
1121 return SQLITE_OK;
1122 }
danielk19773b8a05f2007-03-19 17:44:26 +00001123 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001124 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001125 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001126 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001127 }
drhda47d772002-12-02 04:25:19 +00001128 if( pPager->journalOpen ){
drh01fa4c32007-04-02 11:22:22 +00001129 if( pPager->exclusiveMode
1130 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
danielk197741483462007-03-24 16:45:04 +00001131 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001132 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001133 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001134 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001135 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001136 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001137 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001138 }
danielk197741483462007-03-24 16:45:04 +00001139 }
drh17435752007-08-16 04:30:38 +00001140 sqlite3_free( pPager->aInJournal );
drhda47d772002-12-02 04:25:19 +00001141 pPager->aInJournal = 0;
1142 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1143 pPg->inJournal = 0;
1144 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001145 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001146 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001147#ifdef SQLITE_CHECK_PAGES
1148 pPg->pageHash = pager_pagehash(pPg);
1149#endif
drhda47d772002-12-02 04:25:19 +00001150 }
drh605ad8f2006-05-03 23:34:05 +00001151 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001152 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001153 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001154 }else{
drh88b01a12005-03-28 18:04:27 +00001155 assert( pPager->aInJournal==0 );
drha6abd042004-06-09 17:37:22 +00001156 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001157 }
danielk1977979f38e2007-03-27 16:19:51 +00001158
danielk197741483462007-03-24 16:45:04 +00001159 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001160 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001161 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001162 }else if( pPager->state==PAGER_SYNCED ){
1163 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001164 }
danielk1977334cdb62007-03-26 08:05:12 +00001165 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001166 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001167 pPager->needSync = 0;
1168 pPager->pFirstSynced = pPager->pFirst;
drh588f5bc2007-01-02 18:41:54 +00001169 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001170
1171 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001172}
1173
drhed7c8552001-04-11 14:29:21 +00001174/*
drh968af522003-02-11 14:55:40 +00001175** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001176**
1177** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001178** random initial value and the page number. We experimented with
1179** a checksum of the entire data, but that was found to be too slow.
1180**
1181** Note that the page number is stored at the beginning of data and
1182** the checksum is stored at the end. This is important. If journal
1183** corruption occurs due to a power failure, the most likely scenario
1184** is that one end or the other of the record will be changed. It is
1185** much less likely that the two ends of the journal record will be
1186** correct and the middle be corrupt. Thus, this "checksum" scheme,
1187** though fast and simple, catches the mostly likely kind of corruption.
1188**
1189** FIX ME: Consider adding every 200th (or so) byte of the data to the
1190** checksum. That way if a single page spans 3 or more disk sectors and
1191** only the middle sector is corrupt, we will still have a reasonable
1192** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001193*/
drh74161702006-02-24 02:53:49 +00001194static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001195 u32 cksum = pPager->cksumInit;
1196 int i = pPager->pageSize-200;
1197 while( i>0 ){
1198 cksum += aData[i];
1199 i -= 200;
1200 }
drh968af522003-02-11 14:55:40 +00001201 return cksum;
1202}
1203
drh605ad8f2006-05-03 23:34:05 +00001204/* Forward declaration */
1205static void makeClean(PgHdr*);
1206
drh968af522003-02-11 14:55:40 +00001207/*
drhfa86c412002-02-02 15:01:15 +00001208** Read a single page from the journal file opened on file descriptor
1209** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001210**
drh726de592004-06-10 23:35:50 +00001211** If useCksum==0 it means this journal does not use checksums. Checksums
1212** are not used in statement journals because statement journals do not
1213** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001214*/
danielk197762079062007-08-15 17:08:46 +00001215static int pager_playback_one_page(
1216 Pager *pPager,
1217 sqlite3_file *jfd,
1218 i64 offset,
1219 int useCksum
1220){
drhfa86c412002-02-02 15:01:15 +00001221 int rc;
drhae2b40c2004-06-09 19:03:54 +00001222 PgHdr *pPg; /* An existing page in the cache */
1223 Pgno pgno; /* The page number of a page in journal */
1224 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001225 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001226
drh96362842005-03-20 22:47:56 +00001227 /* useCksum should be true for the main journal and false for
1228 ** statement journals. Verify that this is always the case
1229 */
drh9cbe6352005-11-29 03:13:21 +00001230 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001231 assert( aData );
drh96362842005-03-20 22:47:56 +00001232
danielk197762079062007-08-15 17:08:46 +00001233 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001234 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001235 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001236 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001237 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001238
drh968af522003-02-11 14:55:40 +00001239 /* Sanity checking on the page. This is more important that I originally
1240 ** thought. If a power failure occurs while the journal is being written,
1241 ** it could cause invalid data to be written into the journal. We need to
1242 ** detect this invalid data (with high probability) and ignore it.
1243 */
danielk197775edc162004-06-26 01:48:18 +00001244 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001245 return SQLITE_DONE;
1246 }
drhae2b40c2004-06-09 19:03:54 +00001247 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001248 return SQLITE_OK;
1249 }
drhae2b40c2004-06-09 19:03:54 +00001250 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001251 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001252 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001253 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001254 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001255 return SQLITE_DONE;
1256 }
1257 }
drhfa86c412002-02-02 15:01:15 +00001258
danielk1977aa5ccdf2004-06-14 05:10:42 +00001259 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001260
1261 /* If the pager is in RESERVED state, then there must be a copy of this
1262 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001263 ** not the database file. The page is left marked dirty in this case.
1264 **
danielk19772df71c72007-05-24 07:22:42 +00001265 ** An exception to the above rule: If the database is in no-sync mode
1266 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001267 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1268 ** during a Movepage() call, then the page may not be in the cache
1269 ** either. So the condition described in the above paragraph is not
1270 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001271 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001272 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1273 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001274 **
1275 ** Ticket #1171: The statement journal might contain page content that is
1276 ** different from the page content at the start of the transaction.
1277 ** This occurs when a page is changed prior to the start of a statement
1278 ** then changed again within the statement. When rolling back such a
1279 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001280 ** for certain that original page contents are synced into the main rollback
1281 ** journal. Otherwise, a power loss might leave modified data in the
1282 ** database file without an entry in the rollback journal that can
1283 ** restore the database to its original form. Two conditions must be
1284 ** met before writing to the database files. (1) the database must be
1285 ** locked. (2) we know that the original page content is fully synced
1286 ** in the main journal either because the page is not in cache or else
1287 ** the page is marked as needSync==0.
drhfa86c412002-02-02 15:01:15 +00001288 */
drhae2b40c2004-06-09 19:03:54 +00001289 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001290 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1291 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh96362842005-03-20 22:47:56 +00001292 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
danielk197762079062007-08-15 17:08:46 +00001293 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1294 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001295 if( pPg ){
1296 makeClean(pPg);
1297 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001298 }
drhfa86c412002-02-02 15:01:15 +00001299 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001300 /* No page should ever be explicitly rolled back that is in use, except
1301 ** for page 1 which is held in use in order to keep the lock on the
1302 ** database active. However such a page may be rolled back as a result
1303 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001304 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001305 */
drhb6f41482004-05-14 01:58:11 +00001306 void *pData;
danielk197728129562005-01-11 10:25:06 +00001307 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001308 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001309 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001310 if( pPager->xReiniter ){
1311 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001312 }
danielk19773c407372005-02-15 02:54:14 +00001313#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001314 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001315#endif
drh86a88112007-04-16 15:02:19 +00001316 /* If this was page 1, then restore the value of Pager.dbFileVers.
1317 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001318 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001319 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001320 }
drh86a88112007-04-16 15:02:19 +00001321
1322 /* Decode the page just read from disk */
1323 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001324 }
1325 return rc;
1326}
1327
1328/*
danielk197713adf8a2004-06-03 16:08:41 +00001329** Parameter zMaster is the name of a master journal file. A single journal
1330** file that referred to the master journal file has just been rolled back.
1331** This routine checks if it is possible to delete the master journal file,
1332** and does so if it is.
drh726de592004-06-10 23:35:50 +00001333**
1334** The master journal file contains the names of all child journals.
1335** To tell if a master journal can be deleted, check to each of the
1336** children. If all children are either missing or do not refer to
1337** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001338*/
danielk1977b4b47412007-08-17 15:53:36 +00001339static int pager_delmaster(Pager *pPager, const char *zMaster){
1340 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001341 int rc;
1342 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001343 sqlite3_file *pMaster;
1344 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001345 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001346 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001347
1348 /* Open the master journal file exclusively in case some other process
1349 ** is running this routine also. Not that it makes too much difference.
1350 */
danielk1977b4b47412007-08-17 15:53:36 +00001351 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001352 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001353 if( !pMaster ){
1354 rc = SQLITE_NOMEM;
1355 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001356 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1357 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001358 }
danielk197713adf8a2004-06-03 16:08:41 +00001359 if( rc!=SQLITE_OK ) goto delmaster_out;
1360 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001361
1362 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001363 if( rc!=SQLITE_OK ) goto delmaster_out;
1364
1365 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001366 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001367 char *zMasterPtr = 0;
danielk19775865e3d2004-06-14 06:03:57 +00001368
1369 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001370 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001371 */
drh17435752007-08-16 04:30:38 +00001372 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001373 if( !zMasterJournal ){
1374 rc = SQLITE_NOMEM;
1375 goto delmaster_out;
1376 }
danielk1977b4b47412007-08-17 15:53:36 +00001377 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001378 if( rc!=SQLITE_OK ) goto delmaster_out;
1379
danielk19775865e3d2004-06-14 06:03:57 +00001380 zJournal = zMasterJournal;
1381 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977b4b47412007-08-17 15:53:36 +00001382 if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
danielk197713adf8a2004-06-03 16:08:41 +00001383 /* One of the journals pointed to by the master journal exists.
1384 ** Open it and check if it points at the master journal. If
1385 ** so, return without deleting the master journal file.
1386 */
drh3b7b78b2004-11-24 01:16:43 +00001387 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001388 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1389 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001390 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001391 goto delmaster_out;
1392 }
danielk197713adf8a2004-06-03 16:08:41 +00001393
danielk1977b4b47412007-08-17 15:53:36 +00001394 rc = readMasterJournal(pJournal, &zMasterPtr);
1395 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001396 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001397 goto delmaster_out;
1398 }
danielk197776572402004-06-25 02:38:54 +00001399
drh3b7b78b2004-11-24 01:16:43 +00001400 c = zMasterPtr!=0 && strcmp(zMasterPtr, zMaster)==0;
drh17435752007-08-16 04:30:38 +00001401 sqlite3_free(zMasterPtr);
drh3b7b78b2004-11-24 01:16:43 +00001402 if( c ){
danielk197776572402004-06-25 02:38:54 +00001403 /* We have a match. Do not delete the master journal file. */
1404 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001405 }
1406 }
danielk19775865e3d2004-06-14 06:03:57 +00001407 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001408 }
1409 }
1410
danielk1977fee2d252007-08-18 10:59:19 +00001411 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001412
1413delmaster_out:
1414 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001415 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001416 }
1417 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001418 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001419 }
danielk1977b4b47412007-08-17 15:53:36 +00001420 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001421 return rc;
1422}
1423
drha6abd042004-06-09 17:37:22 +00001424
danielk1977e180dd92007-04-05 17:15:52 +00001425static void pager_truncate_cache(Pager *pPager);
1426
drha6abd042004-06-09 17:37:22 +00001427/*
drhcb4c40b2004-08-18 19:09:43 +00001428** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001429** indicated. Also truncate the cached representation of the file.
drhcb4c40b2004-08-18 19:09:43 +00001430*/
1431static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001432 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001433 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
danielk1977e180dd92007-04-05 17:15:52 +00001434 rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
1435 }
1436 if( rc==SQLITE_OK ){
1437 pPager->dbSize = nPage;
1438 pager_truncate_cache(pPager);
1439 }
1440 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001441}
1442
1443/*
drhc80f0582007-05-01 16:59:48 +00001444** Set the sectorSize for the given pager.
1445**
1446** The sector size is the larger of the sector size reported
1447** by sqlite3OsSectorSize() and the pageSize.
1448*/
1449static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001450 assert(pPager->fd->pMethods||pPager->tempFile);
1451 if( !pPager->tempFile ){
1452 /* Sector size doesn't matter for temporary files. Also, the file
1453 ** may not have been opened yet, in whcih case the OsSectorSize()
1454 ** call will segfault.
1455 */
1456 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1457 }
drhc80f0582007-05-01 16:59:48 +00001458 if( pPager->sectorSize<pPager->pageSize ){
1459 pPager->sectorSize = pPager->pageSize;
1460 }
1461}
1462
1463/*
drhed7c8552001-04-11 14:29:21 +00001464** Playback the journal and thus restore the database file to
1465** the state it was in before we started making changes.
1466**
drh34e79ce2004-02-08 06:05:46 +00001467** The journal file format is as follows:
1468**
drhae2b40c2004-06-09 19:03:54 +00001469** (1) 8 byte prefix. A copy of aJournalMagic[].
1470** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001471** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001472** number of page records from the journal size.
1473** (3) 4 byte big-endian integer which is the initial value for the
1474** sanity checksum.
1475** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001476** database to during a rollback.
drhae2b40c2004-06-09 19:03:54 +00001477** (5) 4 byte integer which is the number of bytes in the master journal
1478** name. The value may be zero (indicate that there is no master
1479** journal.)
1480** (6) N bytes of the master journal name. The name will be nul-terminated
1481** and might be shorter than the value read from (5). If the first byte
1482** of the name is \000 then there is no master journal. The master
1483** journal name is stored in UTF-8.
1484** (7) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001485** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001486** + pPager->pageSize bytes of data.
1487** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001488**
drhae2b40c2004-06-09 19:03:54 +00001489** When we speak of the journal header, we mean the first 6 items above.
1490** Each entry in the journal is an instance of the 7th item.
drh34e79ce2004-02-08 06:05:46 +00001491**
1492** Call the value from the second bullet "nRec". nRec is the number of
1493** valid page entries in the journal. In most cases, you can compute the
1494** value of nRec from the size of the journal file. But if a power
1495** failure occurred while the journal was being written, it could be the
1496** case that the size of the journal file had already been increased but
1497** the extra entries had not yet made it safely to disk. In such a case,
1498** the value of nRec computed from the file size would be too large. For
1499** that reason, we always use the nRec value in the header.
1500**
1501** If the nRec value is 0xffffffff it means that nRec should be computed
1502** from the file size. This value is used when the user selects the
1503** no-sync option for the journal. A power failure could lead to corruption
1504** in this case. But for things like temporary table (which will be
1505** deleted when the power is restored) we don't care.
1506**
drhd9b02572001-04-15 00:37:09 +00001507** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001508** journal file then all pages up to the first corrupted page are rolled
1509** back (or no pages if the journal header is corrupted). The journal file
1510** is then deleted and SQLITE_OK returned, just as if no corruption had
1511** been encountered.
1512**
1513** If an I/O or malloc() error occurs, the journal-file is not deleted
1514** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001515*/
danielk1977e277be02007-03-23 18:12:06 +00001516static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001517 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001518 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001519 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001520 int i; /* Loop counter */
1521 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001522 int rc; /* Result code of a subroutine */
danielk197713adf8a2004-06-03 16:08:41 +00001523 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001524
drhc3a64ba2001-11-22 00:01:27 +00001525 /* Figure out how many records are in the journal. Abort early if
1526 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001527 */
drh8cfbf082001-09-19 13:22:39 +00001528 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001529 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001530 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001531 goto end_playback;
1532 }
drh240c5792004-02-08 00:40:52 +00001533
danielk197776572402004-06-25 02:38:54 +00001534 /* Read the master journal name from the journal, if it is present.
1535 ** If a master journal file name is specified, but the file is not
1536 ** present on disk, then the journal is not hot and does not need to be
1537 ** played back.
drh240c5792004-02-08 00:40:52 +00001538 */
drh9cbe6352005-11-29 03:13:21 +00001539 rc = readMasterJournal(pPager->jfd, &zMaster);
danielk197776572402004-06-25 02:38:54 +00001540 assert( rc!=SQLITE_DONE );
danielk1977b4b47412007-08-17 15:53:36 +00001541 if( rc!=SQLITE_OK
1542 || (zMaster && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))
1543 ){
drh17435752007-08-16 04:30:38 +00001544 sqlite3_free(zMaster);
danielk197776572402004-06-25 02:38:54 +00001545 zMaster = 0;
1546 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drhc3a64ba2001-11-22 00:01:27 +00001547 goto end_playback;
1548 }
danielk197776572402004-06-25 02:38:54 +00001549 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001550
danielk197776572402004-06-25 02:38:54 +00001551 /* This loop terminates either when the readJournalHdr() call returns
1552 ** SQLITE_DONE or an IO error occurs. */
1553 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001554
danielk197776572402004-06-25 02:38:54 +00001555 /* Read the next journal header from the journal file. If there are
1556 ** not enough bytes left in the journal file for a complete header, or
1557 ** it is corrupted, then a process must of failed while writing it.
1558 ** This indicates nothing more needs to be rolled back.
1559 */
1560 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1561 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001562 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001563 rc = SQLITE_OK;
1564 }
danielk197776572402004-06-25 02:38:54 +00001565 goto end_playback;
1566 }
1567
1568 /* If nRec is 0xffffffff, then this journal was created by a process
1569 ** working in no-sync mode. This means that the rest of the journal
1570 ** file consists of pages, there are no more journal headers. Compute
1571 ** the value of nRec based on this assumption.
1572 */
1573 if( nRec==0xffffffff ){
1574 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1575 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1576 }
1577
danielk1977e277be02007-03-23 18:12:06 +00001578 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001579 ** process and if this is the final header in the journal, then it means
1580 ** that this part of the journal was being filled but has not yet been
1581 ** synced to disk. Compute the number of pages based on the remaining
1582 ** size of the file.
1583 **
1584 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001585 */
drh8940f4e2007-08-11 00:26:20 +00001586 if( nRec==0 && !isHot &&
1587 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001588 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1589 }
1590
danielk197776572402004-06-25 02:38:54 +00001591 /* If this is the first header read from the journal, truncate the
1592 ** database file back to it's original size.
1593 */
danielk1977e180dd92007-04-05 17:15:52 +00001594 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001595 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001596 if( rc!=SQLITE_OK ){
1597 goto end_playback;
1598 }
danielk197776572402004-06-25 02:38:54 +00001599 }
1600
danielk197776572402004-06-25 02:38:54 +00001601 /* Copy original pages out of the journal and back into the database file.
1602 */
1603 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001604 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001605 if( rc!=SQLITE_OK ){
1606 if( rc==SQLITE_DONE ){
1607 rc = SQLITE_OK;
1608 pPager->journalOff = szJ;
1609 break;
1610 }else{
1611 goto end_playback;
1612 }
1613 }
drh968af522003-02-11 14:55:40 +00001614 }
drhed7c8552001-04-11 14:29:21 +00001615 }
drh580eeaf2006-02-24 03:09:37 +00001616 /*NOTREACHED*/
1617 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001618
1619end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001620 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001621 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001622 }
danielk197713adf8a2004-06-03 16:08:41 +00001623 if( zMaster ){
danielk1977979f38e2007-03-27 16:19:51 +00001624 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001625 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001626 */
1627 if( rc==SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001628 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001629 }
drh17435752007-08-16 04:30:38 +00001630 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001631 }
danielk197776572402004-06-25 02:38:54 +00001632
1633 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001634 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001635 ** value. Reset it to the correct value for this process.
1636 */
drhc80f0582007-05-01 16:59:48 +00001637 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001638 return rc;
drhed7c8552001-04-11 14:29:21 +00001639}
1640
1641/*
drhac69b052004-05-12 13:30:07 +00001642** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001643**
1644** This is similar to playing back the transaction journal but with
1645** a few extra twists.
1646**
drh663fc632002-02-02 18:49:19 +00001647** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001648** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001649** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001650**
drhac69b052004-05-12 13:30:07 +00001651** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001652** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001653** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001654*/
drh3aac2dd2004-04-26 14:10:20 +00001655static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001656 i64 szJ; /* Size of the full journal */
1657 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001658 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001659 int i; /* Loop counter */
1660 int rc;
1661
danielk197776572402004-06-25 02:38:54 +00001662 szJ = pPager->journalOff;
1663#ifndef NDEBUG
1664 {
drheb206252004-10-01 02:00:31 +00001665 i64 os_szJ;
drh054889e2005-11-30 03:20:31 +00001666 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
danielk197776572402004-06-25 02:38:54 +00001667 if( rc!=SQLITE_OK ) return rc;
1668 assert( szJ==os_szJ );
1669 }
1670#endif
1671
danielk19774099f6e2007-03-19 11:25:20 +00001672 /* Set hdrOff to be the offset just after the end of the last journal
1673 ** page written before the first journal-header for this statement
1674 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001675 ** header was written.
1676 */
1677 hdrOff = pPager->stmtHdrOff;
1678 assert( pPager->fullSync || !hdrOff );
1679 if( !hdrOff ){
1680 hdrOff = szJ;
1681 }
1682
drhfa86c412002-02-02 15:01:15 +00001683 /* Truncate the database back to its original size.
1684 */
danielk1977e180dd92007-04-05 17:15:52 +00001685 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001686 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001687
drhac69b052004-05-12 13:30:07 +00001688 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001689 */
drhac69b052004-05-12 13:30:07 +00001690 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001691 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001692
drhac69b052004-05-12 13:30:07 +00001693 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001694 ** database file. Note that the statement journal omits checksums from
1695 ** each record since power-failure recovery is not important to statement
1696 ** journals.
drhfa86c412002-02-02 15:01:15 +00001697 */
danielk197762079062007-08-15 17:08:46 +00001698 for(i=0; i<nRec; i++){
1699 i64 offset = i*(4+pPager->pageSize);
1700 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001701 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001702 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001703 }
1704
danielk197776572402004-06-25 02:38:54 +00001705 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1706 ** was the size of the journal file when this statement was started, so
1707 ** everything after that needs to be rolled back, either into the
1708 ** database, the memory cache, or both.
1709 **
1710 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1711 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001712 */
danielk197776572402004-06-25 02:38:54 +00001713 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001714 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001715 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001716 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001717 assert( rc!=SQLITE_DONE );
1718 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1719 }
1720
1721 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001722 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001723 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001724 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001725 if( rc!=SQLITE_OK ){
1726 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001727 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001728 }
danielk1977f0113002006-01-24 12:09:17 +00001729 if( nJRec==0 ){
1730 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001731 }
danielk1977f0113002006-01-24 12:09:17 +00001732 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001733 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001734 assert( rc!=SQLITE_DONE );
1735 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1736 }
drhfa86c412002-02-02 15:01:15 +00001737 }
danielk197776572402004-06-25 02:38:54 +00001738
1739 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001740
drh3aac2dd2004-04-26 14:10:20 +00001741end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001742 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001743 pPager->journalOff = szJ;
1744 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001745 }
1746 return rc;
1747}
1748
1749/*
drhf57b14a2001-09-14 18:54:08 +00001750** Change the maximum number of in-memory pages that are allowed.
1751*/
danielk19773b8a05f2007-03-19 17:44:26 +00001752void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00001753 if( mxPage>10 ){
1754 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00001755 }else{
1756 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00001757 }
1758}
1759
1760/*
drh973b6e32003-02-12 14:09:42 +00001761** Adjust the robustness of the database to damage due to OS crashes
1762** or power failures by changing the number of syncs()s when writing
1763** the rollback journal. There are three levels:
1764**
drh054889e2005-11-30 03:20:31 +00001765** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001766** for temporary and transient files.
1767**
1768** NORMAL The journal is synced once before writes begin on the
1769** database. This is normally adequate protection, but
1770** it is theoretically possible, though very unlikely,
1771** that an inopertune power failure could leave the journal
1772** in a state which would cause damage to the database
1773** when it is rolled back.
1774**
1775** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001776** database (with some additional information - the nRec field
1777** of the journal header - being written in between the two
1778** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001779** single disk sector is atomic, then this mode provides
1780** assurance that the journal will not be corrupted to the
1781** point of causing damage to the database during rollback.
1782**
1783** Numeric values associated with these states are OFF==1, NORMAL=2,
1784** and FULL=3.
1785*/
danielk197793758c82005-01-21 08:13:14 +00001786#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00001787void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00001788 pPager->noSync = level==1 || pPager->tempFile;
1789 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00001790 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001791 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001792}
danielk197793758c82005-01-21 08:13:14 +00001793#endif
drh973b6e32003-02-12 14:09:42 +00001794
1795/*
drhaf6df112005-06-07 02:12:30 +00001796** The following global variable is incremented whenever the library
1797** attempts to open a temporary file. This information is used for
1798** testing and analysis only.
1799*/
drh0f7eb612006-08-08 13:51:43 +00001800#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001801int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001802#endif
drhaf6df112005-06-07 02:12:30 +00001803
1804/*
drh3f56e6e2007-03-15 01:16:47 +00001805** Open a temporary file.
1806**
1807** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001808** other error code if we fail. The OS will automatically delete the temporary
1809** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001810**
danielk1977fee2d252007-08-18 10:59:19 +00001811** If zNameOut is 0, then SQLITE_OPEN_SUBJOURNAL is passed to the OS layer.
1812** If zNameOut is not 0, SQLITE_OPEN_TEMP_DB is passed.
drhfa86c412002-02-02 15:01:15 +00001813*/
danielk1977b4b47412007-08-17 15:53:36 +00001814static int sqlite3PagerOpentemp(
1815 sqlite3_vfs *pVfs,
1816 sqlite3_file *pFile,
1817 char *zNameOut
1818){
drhfa86c412002-02-02 15:01:15 +00001819 int cnt = 8;
1820 int rc;
danielk1977863c0f92007-08-23 11:47:59 +00001821 int flags = (
1822 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1823 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_DELETEONCLOSE
1824 );
danielk1977b4b47412007-08-17 15:53:36 +00001825
1826 char *zFree = 0;
1827 if( zNameOut==0 ){
1828 zFree = (char *)sqlite3_malloc(pVfs->mxPathname);
1829 if( !zFree ){
1830 return SQLITE_NOMEM;
1831 }
1832 zNameOut = zFree;
danielk1977fee2d252007-08-18 10:59:19 +00001833 flags |= SQLITE_OPEN_SUBJOURNAL;
1834 }else{
1835 flags |= SQLITE_OPEN_TEMP_DB;
danielk1977b4b47412007-08-17 15:53:36 +00001836 }
drh3f56e6e2007-03-15 01:16:47 +00001837
drh0f7eb612006-08-08 13:51:43 +00001838#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001839 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001840#endif
danielk1977b4b47412007-08-17 15:53:36 +00001841
drhfa86c412002-02-02 15:01:15 +00001842 do{
1843 cnt--;
danielk1977b4b47412007-08-17 15:53:36 +00001844 sqlite3OsGetTempName(pVfs, zNameOut);
1845 rc = sqlite3OsOpen(pVfs, zNameOut, pFile, flags, 0);
1846 assert( rc!=SQLITE_OK || pFile->pMethods );
danielk197713073932004-06-30 11:54:06 +00001847 }while( cnt>0 && rc!=SQLITE_OK && rc!=SQLITE_NOMEM );
danielk1977b4b47412007-08-17 15:53:36 +00001848
1849 sqlite3_free(zFree);
drhfa86c412002-02-02 15:01:15 +00001850 return rc;
1851}
1852
1853/*
drhed7c8552001-04-11 14:29:21 +00001854** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001855** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001856** the first call to sqlite3PagerGet() and is only held open until the
1857** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001858**
drh6446c4d2001-12-15 14:22:18 +00001859** If zFilename is NULL then a randomly-named temporary file is created
1860** and used as the file to be cached. The file will be deleted
1861** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001862**
1863** If zFilename is ":memory:" then all information is held in cache.
1864** It is never written to disk. This can be used to implement an
1865** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001866*/
danielk19773b8a05f2007-03-19 17:44:26 +00001867int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001868 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001869 Pager **ppPager, /* Return the Pager structure here */
1870 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001871 int nExtra, /* Extra bytes append to each in-memory page */
drh7bec5052005-02-06 02:45:41 +00001872 int flags /* flags controlling this file */
drh7e3b0a02001-04-28 16:52:40 +00001873){
danielk1977b4b47412007-08-17 15:53:36 +00001874 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001875 Pager *pPager = 0;
danielk19778def5ea2004-06-16 10:39:52 +00001876 char *zFullPathname = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001877 int rc = SQLITE_OK;
1878 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001879 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001880 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001881 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001882 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1883 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00001884 int journalFileSize = sqlite3JournalSize(pVfs);
danielk1977b4b47412007-08-17 15:53:36 +00001885
drh86f8c192007-08-22 00:39:19 +00001886 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001887 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001888
danielk1977b4b47412007-08-17 15:53:36 +00001889 /* Allocate memory for the pager structure */
1890 pPager = sqlite3MallocZero(
1891 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00001892 journalFileSize + /* The journal file structure */
1893 pVfs->szOsFile * 2 + /* The db and stmt journal files */
danielk1977b4b47412007-08-17 15:53:36 +00001894 pVfs->mxPathname * 3 + 30 /* zFilename, zDirectory, zJournal */
1895 );
1896 if( !pPager ){
1897 return SQLITE_NOMEM;
1898 }
1899 pPtr = (u8 *)&pPager[1];
1900 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00001901 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
1902 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
1903 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
danielk1977b4b47412007-08-17 15:53:36 +00001904 pPager->zDirectory = &pPager->zFilename[pVfs->mxPathname];
1905 pPager->zJournal = &pPager->zDirectory[pVfs->mxPathname];
1906 pPager->pVfs = pVfs;
1907
danielk1977aef0bf62005-12-30 16:28:01 +00001908 /* Open the pager file and set zFullPathname to point at malloc()ed
1909 ** memory containing the complete filename (i.e. including the directory).
1910 */
drh901afd42003-08-26 11:25:58 +00001911 if( zFilename && zFilename[0] ){
drhb7f91642004-10-31 02:22:47 +00001912#ifndef SQLITE_OMIT_MEMORYDB
drhac69b052004-05-12 13:30:07 +00001913 if( strcmp(zFilename,":memory:")==0 ){
1914 memDb = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001915 pPager->zFilename[0] = '\0';
drhb7f91642004-10-31 02:22:47 +00001916 }else
1917#endif
1918 {
danielk1977b4b47412007-08-17 15:53:36 +00001919 rc = sqlite3OsFullPathname(pVfs, zFilename, pPager->zFilename);
1920 if( rc==SQLITE_OK ){
1921 if( strlen(pPager->zFilename)>(pVfs->mxPathname - strlen("-journal")) ){
1922 rc = SQLITE_CANTOPEN;
1923 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001924 int oflag =
1925 (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB);
danielk1977b4b47412007-08-17 15:53:36 +00001926 int fout = 0;
danielk1977fee2d252007-08-18 10:59:19 +00001927 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, oflag, &fout);
danielk1977b4b47412007-08-17 15:53:36 +00001928 readOnly = (fout&SQLITE_OPEN_READONLY);
1929 }
danielk19778def5ea2004-06-16 10:39:52 +00001930 }
drhac69b052004-05-12 13:30:07 +00001931 }
drh5e00f6c2001-09-13 13:46:56 +00001932 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00001933 /* If a temporary file is requested, it is not opened immediately.
1934 ** In this case we accept the default page size and delay actually
1935 ** opening the file until the first call to OsWrite().
1936 */
danielk19777a2b1ee2007-08-21 14:27:01 +00001937 tempFile = 1;
1938 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00001939 }
danielk1977aef0bf62005-12-30 16:28:01 +00001940
danielk1977b4b47412007-08-17 15:53:36 +00001941 if( pPager && rc==SQLITE_OK ){
1942 pPager->pTmpSpace = (char *)sqlite3_malloc(SQLITE_DEFAULT_PAGE_SIZE);
drh3e7a6092002-12-07 21:45:14 +00001943 }
danielk1977aef0bf62005-12-30 16:28:01 +00001944
1945 /* If an error occured in either of the blocks above, free the memory
1946 ** pointed to by zFullPathname, free the Pager structure and close the
1947 ** file. Since the pager is not allocated there is no need to set
1948 ** any Pager.errMask variables.
1949 */
danielk1977b4b47412007-08-17 15:53:36 +00001950 if( !pPager || !pPager->pTmpSpace ){
1951 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00001952 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00001953 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001954 }
danielk1977aef0bf62005-12-30 16:28:01 +00001955
danielk1977dd97a492007-08-22 18:54:32 +00001956 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), zFullPathname);
drhb0603412007-02-28 04:47:26 +00001957 IOTRACE(("OPEN %p %s\n", pPager, zFullPathname))
danielk1977aef0bf62005-12-30 16:28:01 +00001958
danielk1977b4b47412007-08-17 15:53:36 +00001959 /* Fill in Pager.zDirectory[] */
1960 memcpy(pPager->zDirectory, pPager->zFilename, pVfs->mxPathname);
1961 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00001962 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001963
1964 /* Fill in Pager.zJournal[] */
1965 memcpy(pPager->zJournal, pPager->zFilename, pVfs->mxPathname);
1966 memcpy(&pPager->zJournal[strlen(pPager->zJournal)], "-journal", 9);
1967
drh3b59a5c2006-01-15 20:28:28 +00001968 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00001969 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00001970 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001971 /* pPager->stmtOpen = 0; */
1972 /* pPager->stmtInUse = 0; */
1973 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00001974 pPager->dbSize = memDb-1;
drh90f5ecb2004-07-22 01:19:35 +00001975 pPager->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
drh3b59a5c2006-01-15 20:28:28 +00001976 /* pPager->stmtSize = 0; */
1977 /* pPager->stmtJSize = 0; */
1978 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00001979 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00001980 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00001981 assert( PAGER_UNLOCK==0 );
1982 /* pPager->state = PAGER_UNLOCK; */
1983 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00001984 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00001985 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
1986 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
1987 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
1988 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00001989 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001990 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001991 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00001992 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00001993 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00001994 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00001995 /* pPager->pFirst = 0; */
1996 /* pPager->pFirstSynced = 0; */
1997 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00001998 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00001999 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002000 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002001 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002002 }
drh3b59a5c2006-01-15 20:28:28 +00002003 /* pPager->pBusyHandler = 0; */
2004 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002005 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002006#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002007 pPager->iInUseMM = 0;
2008 pPager->iInUseDB = 0;
2009 if( !memDb ){
2010 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2011 sqlite3_mutex_enter(mutex);
2012 pPager->pNext = sqlite3PagerList;
2013 if( sqlite3PagerList ){
2014 assert( sqlite3PagerList->pPrev==0 );
2015 sqlite3PagerList->pPrev = pPager;
2016 }
2017 pPager->pPrev = 0;
2018 sqlite3PagerList = pPager;
2019 sqlite3_mutex_leave(mutex);
2020 }
danielk197752622822006-01-09 09:59:49 +00002021#endif
drhed7c8552001-04-11 14:29:21 +00002022 return SQLITE_OK;
2023}
2024
2025/*
drh90f5ecb2004-07-22 01:19:35 +00002026** Set the busy handler function.
2027*/
danielk19773b8a05f2007-03-19 17:44:26 +00002028void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002029 pPager->pBusyHandler = pBusyHandler;
2030}
2031
2032/*
drh72f82862001-05-24 21:06:34 +00002033** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002034** when the reference count on each page reaches zero. The destructor can
2035** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002036**
danielk19773b8a05f2007-03-19 17:44:26 +00002037** The destructor is not called as a result sqlite3PagerClose().
2038** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002039*/
danielk19773b8a05f2007-03-19 17:44:26 +00002040void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002041 pPager->xDestructor = xDesc;
2042}
2043
2044/*
drha6abd042004-06-09 17:37:22 +00002045** Set the reinitializer for this pager. If not NULL, the reinitializer
2046** is called when the content of a page in cache is restored to its original
2047** value as a result of a rollback. The callback gives higher-level code
2048** an opportunity to restore the EXTRA section to agree with the restored
2049** page data.
2050*/
danielk19773b8a05f2007-03-19 17:44:26 +00002051void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002052 pPager->xReiniter = xReinit;
2053}
2054
2055/*
drh1c7880e2005-05-20 20:01:55 +00002056** Set the page size. Return the new size. If the suggest new page
2057** size is inappropriate, then an alternative page size is selected
2058** and returned.
drh90f5ecb2004-07-22 01:19:35 +00002059*/
danielk19773b8a05f2007-03-19 17:44:26 +00002060int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){
drh90f5ecb2004-07-22 01:19:35 +00002061 assert( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE );
danielk1977c7c7e622007-03-26 15:46:00 +00002062 if( !pPager->memDb && pPager->nRef==0 ){
drh86f8c192007-08-22 00:39:19 +00002063 pagerEnter(pPager);
danielk1977c7c7e622007-03-26 15:46:00 +00002064 pager_reset(pPager);
drh1c7880e2005-05-20 20:01:55 +00002065 pPager->pageSize = pageSize;
drh86f8c192007-08-22 00:39:19 +00002066 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00002067 sqlite3_free(pPager->pTmpSpace);
2068 pPager->pTmpSpace = sqlite3_malloc(pageSize);
drh1c7880e2005-05-20 20:01:55 +00002069 }
2070 return pPager->pageSize;
drh90f5ecb2004-07-22 01:19:35 +00002071}
2072
2073/*
drhf8e632b2007-05-08 14:51:36 +00002074** Attempt to set the maximum database page count if mxPage is positive.
2075** Make no changes if mxPage is zero or negative. And never reduce the
2076** maximum page count below the current size of the database.
2077**
2078** Regardless of mxPage, return the current maximum page count.
2079*/
2080int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2081 if( mxPage>0 ){
2082 pPager->mxPgno = mxPage;
2083 }
2084 sqlite3PagerPagecount(pPager);
2085 return pPager->mxPgno;
2086}
2087
2088/*
drhc9ac5ca2005-11-04 22:03:30 +00002089** The following set of routines are used to disable the simulated
2090** I/O error mechanism. These routines are used to avoid simulated
2091** errors in places where we do not care about errors.
2092**
2093** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2094** and generate no code.
2095*/
2096#ifdef SQLITE_TEST
2097extern int sqlite3_io_error_pending;
2098extern int sqlite3_io_error_hit;
2099static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002100void disable_simulated_io_errors(void){
2101 saved_cnt = sqlite3_io_error_pending;
2102 sqlite3_io_error_pending = -1;
2103}
2104void enable_simulated_io_errors(void){
2105 sqlite3_io_error_pending = saved_cnt;
2106}
2107#else
drh152410f2005-11-05 15:11:22 +00002108# define disable_simulated_io_errors()
2109# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002110#endif
2111
2112/*
drh90f5ecb2004-07-22 01:19:35 +00002113** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002114** that pDest points to.
2115**
2116** No error checking is done. The rational for this is that this function
2117** may be called even if the file does not exist or contain a header. In
2118** these cases sqlite3OsRead() will return an error, to which the correct
2119** response is to zero the memory at pDest and continue. A real IO error
2120** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002121*/
danielk19773b8a05f2007-03-19 17:44:26 +00002122int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002123 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002124 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002125 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2126 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002127 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002128 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002129 if( rc==SQLITE_IOERR_SHORT_READ ){
2130 rc = SQLITE_OK;
2131 }
drh90f5ecb2004-07-22 01:19:35 +00002132 }
drh551b7732006-11-06 21:20:25 +00002133 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002134}
2135
2136/*
drh5e00f6c2001-09-13 13:46:56 +00002137** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002138** pPager.
2139**
2140** If the PENDING_BYTE lies on the page directly after the end of the
2141** file, then consider this page part of the file too. For example, if
2142** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2143** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002144*/
danielk19773b8a05f2007-03-19 17:44:26 +00002145int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002146 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002147 int rc;
drhd9b02572001-04-15 00:37:09 +00002148 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002149 if( pPager->errCode ){
2150 return 0;
2151 }
drhed7c8552001-04-11 14:29:21 +00002152 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002153 n = pPager->dbSize;
2154 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002155 assert(pPager->fd->pMethods||pPager->tempFile);
2156 if( (pPager->fd->pMethods)
2157 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002158 pager_error(pPager, rc);
danielk197715f411d2005-09-16 10:18:45 +00002159 return 0;
2160 }
2161 if( n>0 && n<pPager->pageSize ){
2162 n = 1;
2163 }else{
2164 n /= pPager->pageSize;
2165 }
2166 if( pPager->state!=PAGER_UNLOCK ){
2167 pPager->dbSize = n;
2168 }
drhed7c8552001-04-11 14:29:21 +00002169 }
danielk197715f411d2005-09-16 10:18:45 +00002170 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002171 n++;
2172 }
drhf8e632b2007-05-08 14:51:36 +00002173 if( n>pPager->mxPgno ){
2174 pPager->mxPgno = n;
2175 }
drhed7c8552001-04-11 14:29:21 +00002176 return n;
2177}
2178
drhf07e7d52006-04-07 13:54:46 +00002179
2180#ifndef SQLITE_OMIT_MEMORYDB
2181/*
2182** Clear a PgHistory block
2183*/
2184static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002185 sqlite3_free(pHist->pOrig);
2186 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002187 pHist->pOrig = 0;
2188 pHist->pStmt = 0;
2189}
2190#else
2191#define clearHistory(x)
2192#endif
2193
drhed7c8552001-04-11 14:29:21 +00002194/*
drhf7c57532003-04-25 13:22:51 +00002195** Forward declaration
2196*/
danielk197776572402004-06-25 02:38:54 +00002197static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002198
2199/*
danielk1977f5fdda82004-11-03 08:44:05 +00002200** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate
2201** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002202** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002203** pNextFree/pPrevFree list that is not a part of any hash-chain.
2204*/
2205static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2206 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002207 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002208 return;
2209 }
2210 if( pPg->pNextHash ){
2211 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2212 }
2213 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002214 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002215 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2216 }else{
drh8ca0c722006-05-07 17:49:38 +00002217 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002218 pPager->aHash[h] = pPg->pNextHash;
2219 }
drh5229ae42006-03-23 23:29:04 +00002220 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002221 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2222 }
danielk1977f5fdda82004-11-03 08:44:05 +00002223 pPg->pgno = 0;
2224 pPg->pNextHash = pPg->pPrevHash = 0;
2225}
2226
2227/*
drhac69b052004-05-12 13:30:07 +00002228** Unlink a page from the free list (the list of all pages where nRef==0)
2229** and from its hash collision chain.
2230*/
2231static void unlinkPage(PgHdr *pPg){
2232 Pager *pPager = pPg->pPager;
2233
2234 /* Keep the pFirstSynced pointer pointing at the first synchronized page */
2235 if( pPg==pPager->pFirstSynced ){
2236 PgHdr *p = pPg->pNextFree;
2237 while( p && p->needSync ){ p = p->pNextFree; }
2238 pPager->pFirstSynced = p;
2239 }
2240
2241 /* Unlink from the freelist */
2242 if( pPg->pPrevFree ){
2243 pPg->pPrevFree->pNextFree = pPg->pNextFree;
2244 }else{
2245 assert( pPager->pFirst==pPg );
2246 pPager->pFirst = pPg->pNextFree;
2247 }
2248 if( pPg->pNextFree ){
2249 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
2250 }else{
2251 assert( pPager->pLast==pPg );
2252 pPager->pLast = pPg->pPrevFree;
2253 }
2254 pPg->pNextFree = pPg->pPrevFree = 0;
2255
2256 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002257 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002258}
2259
2260/*
danielk1977e180dd92007-04-05 17:15:52 +00002261** This routine is used to truncate the cache when a database
2262** is truncated. Drop from the cache all pages whose pgno is
2263** larger than pPager->dbSize and is unreferenced.
2264**
drhac69b052004-05-12 13:30:07 +00002265** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002266**
2267** Actually, at the point this routine is called, it would be
2268** an error to have a referenced page. But rather than delete
2269** that page and guarantee a subsequent segfault, it seems better
2270** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002271*/
danielk1977e180dd92007-04-05 17:15:52 +00002272static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002273 PgHdr *pPg;
2274 PgHdr **ppPg;
2275 int dbSize = pPager->dbSize;
2276
2277 ppPg = &pPager->pAll;
2278 while( (pPg = *ppPg)!=0 ){
2279 if( pPg->pgno<=dbSize ){
2280 ppPg = &pPg->pNextAll;
2281 }else if( pPg->nRef>0 ){
2282 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2283 ppPg = &pPg->pNextAll;
2284 }else{
2285 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002286 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2287 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002288 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002289 makeClean(pPg);
drh17435752007-08-16 04:30:38 +00002290 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002291 pPager->nPage--;
2292 }
2293 }
2294}
2295
drhf7c57532003-04-25 13:22:51 +00002296/*
danielk197717221812005-02-15 03:38:05 +00002297** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002298** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002299** false or until the lock succeeds.
2300**
2301** Return SQLITE_OK on success and an error code if we cannot obtain
2302** the lock.
2303*/
2304static int pager_wait_on_lock(Pager *pPager, int locktype){
2305 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002306
2307 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002308 assert( PAGER_SHARED==SHARED_LOCK );
2309 assert( PAGER_RESERVED==RESERVED_LOCK );
2310 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002311
2312 /* If the file is currently unlocked then the size must be unknown */
2313 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2314
danielk197717221812005-02-15 03:38:05 +00002315 if( pPager->state>=locktype ){
2316 rc = SQLITE_OK;
2317 }else{
danielk197717221812005-02-15 03:38:05 +00002318 do {
drh054889e2005-11-30 03:20:31 +00002319 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002320 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002321 if( rc==SQLITE_OK ){
2322 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002323 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002324 }
2325 }
2326 return rc;
2327}
2328
2329/*
drhf7c57532003-04-25 13:22:51 +00002330** Truncate the file to the number of pages specified.
2331*/
danielk19773b8a05f2007-03-19 17:44:26 +00002332int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002333 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002334 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002335 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002336 if( pPager->errCode ){
2337 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002338 return rc;
2339 }
drh7d02cb72003-06-04 16:24:39 +00002340 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002341 return SQLITE_OK;
2342 }
drhb7f91642004-10-31 02:22:47 +00002343 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002344 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002345 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002346 return SQLITE_OK;
2347 }
drh86f8c192007-08-22 00:39:19 +00002348 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002349 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002350 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002351 if( rc!=SQLITE_OK ){
2352 return rc;
2353 }
danielk197717221812005-02-15 03:38:05 +00002354
2355 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002356 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002357 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002358 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002359 if( rc!=SQLITE_OK ){
2360 return rc;
2361 }
2362
drhcb4c40b2004-08-18 19:09:43 +00002363 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002364 return rc;
2365}
2366
2367/*
drhed7c8552001-04-11 14:29:21 +00002368** Shutdown the page cache. Free all memory and close all files.
2369**
2370** If a transaction was in progress when this routine is called, that
2371** transaction is rolled back. All outstanding pages are invalidated
2372** and their memory is freed. Any attempt to use a page associated
2373** with this page cache after this function returns will likely
2374** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002375**
2376** This function always succeeds. If a transaction is active an attempt
2377** is made to roll it back. If an error occurs during the rollback
2378** a hot journal may be left in the filesystem but no error is returned
2379** to the caller.
drhed7c8552001-04-11 14:29:21 +00002380*/
danielk19773b8a05f2007-03-19 17:44:26 +00002381int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002382#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002383 if( !MEMDB ){
2384 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2385 sqlite3_mutex_enter(mutex);
2386 if( pPager->pPrev ){
2387 pPager->pPrev->pNext = pPager->pNext;
2388 }else{
2389 sqlite3PagerList = pPager->pNext;
2390 }
2391 if( pPager->pNext ){
2392 pPager->pNext->pPrev = pPager->pPrev;
2393 }
2394 sqlite3_mutex_leave(mutex);
2395 }
danielk197713f72992005-12-18 08:51:22 +00002396#endif
2397
drhbafda092007-01-03 23:36:22 +00002398 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002399 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002400 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002401 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002402 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002403 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002404 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002405 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002406 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002407 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002408 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002409 }
drh17435752007-08-16 04:30:38 +00002410 sqlite3_free(pPager->aInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002411 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002412 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002413 }
danielk1977b4b47412007-08-17 15:53:36 +00002414 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002415 /* Temp files are automatically deleted by the OS
2416 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002417 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002418 ** }
2419 */
danielk1977aca790a2005-01-13 11:07:52 +00002420
drh17435752007-08-16 04:30:38 +00002421 sqlite3_free(pPager->aHash);
2422 sqlite3_free(pPager->pTmpSpace);
2423 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002424 return SQLITE_OK;
2425}
2426
drh87cc3b32007-05-08 21:45:27 +00002427#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002428/*
drh5e00f6c2001-09-13 13:46:56 +00002429** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002430*/
danielk19773b8a05f2007-03-19 17:44:26 +00002431Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002432 return p->pgno;
2433}
drh87cc3b32007-05-08 21:45:27 +00002434#endif
drhed7c8552001-04-11 14:29:21 +00002435
2436/*
drhc8629a12004-05-08 20:07:40 +00002437** The page_ref() function increments the reference count for a page.
2438** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002439** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002440**
2441** For non-test systems, page_ref() is a macro that calls _page_ref()
2442** online of the reference count is zero. For test systems, page_ref()
2443** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002444*/
drh836faa42003-01-11 13:30:57 +00002445static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002446 if( pPg->nRef==0 ){
2447 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +00002448 if( pPg==pPg->pPager->pFirstSynced ){
2449 PgHdr *p = pPg->pNextFree;
2450 while( p && p->needSync ){ p = p->pNextFree; }
2451 pPg->pPager->pFirstSynced = p;
2452 }
drh7e3b0a02001-04-28 16:52:40 +00002453 if( pPg->pPrevFree ){
2454 pPg->pPrevFree->pNextFree = pPg->pNextFree;
2455 }else{
2456 pPg->pPager->pFirst = pPg->pNextFree;
2457 }
2458 if( pPg->pNextFree ){
2459 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
2460 }else{
2461 pPg->pPager->pLast = pPg->pPrevFree;
2462 }
2463 pPg->pPager->nRef++;
2464 }
2465 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00002466 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00002467}
danielk1977aca790a2005-01-13 11:07:52 +00002468#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002469 static void page_ref(PgHdr *pPg){
2470 if( pPg->nRef==0 ){
2471 _page_ref(pPg);
2472 }else{
2473 pPg->nRef++;
2474 REFINFO(pPg);
2475 }
2476 }
2477#else
2478# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2479#endif
drhdf0b3b02001-06-23 11:36:20 +00002480
2481/*
2482** Increment the reference count for a page. The input pointer is
2483** a reference to the page data.
2484*/
danielk19773b8a05f2007-03-19 17:44:26 +00002485int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002486 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002487 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002488 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002489 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002490}
2491
2492/*
drh34e79ce2004-02-08 06:05:46 +00002493** Sync the journal. In other words, make sure all the pages that have
2494** been written to the journal have actually reached the surface of the
2495** disk. It is not safe to modify the original database file until after
2496** the journal has been synced. If the original database is modified before
2497** the journal is synced and a power failure occurs, the unsynced journal
2498** data would be lost and we would be unable to completely rollback the
2499** database changes. Database corruption would occur.
2500**
2501** This routine also updates the nRec field in the header of the journal.
2502** (See comments on the pager_playback() routine for additional information.)
2503** If the sync mode is FULL, two syncs will occur. First the whole journal
2504** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002505**
drh34e79ce2004-02-08 06:05:46 +00002506** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002507** after a power failure, so no sync occurs.
2508**
2509** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2510** the database is stored, then OsSync() is never called on the journal
2511** file. In this case all that is required is to update the nRec field in
2512** the journal header.
drhfa86c412002-02-02 15:01:15 +00002513**
drh34e79ce2004-02-08 06:05:46 +00002514** This routine clears the needSync field of every page current held in
2515** memory.
drh50e5dad2001-09-15 00:57:28 +00002516*/
danielk197776572402004-06-25 02:38:54 +00002517static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002518 PgHdr *pPg;
2519 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002520
danielk19774cd2cd52007-08-23 14:48:23 +00002521
drh03eb96a2002-11-10 23:32:56 +00002522 /* Sync the journal before modifying the main database
2523 ** (assuming there is a journal and it needs to be synced.)
2524 */
danielk197776572402004-06-25 02:38:54 +00002525 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002526 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002527 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002528 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002529
drh946966f2004-02-25 02:20:41 +00002530 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2531 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00002532#ifndef NDEBUG
2533 {
drh34e79ce2004-02-08 06:05:46 +00002534 /* Make sure the pPager->nRec counter we are keeping agrees
2535 ** with the nRec computed from the size of the journal file.
2536 */
drheb206252004-10-01 02:00:31 +00002537 i64 jSz;
drh054889e2005-11-30 03:20:31 +00002538 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00002539 if( rc!=0 ) return rc;
danielk197776572402004-06-25 02:38:54 +00002540 assert( pPager->journalOff==jSz );
drh968af522003-02-11 14:55:40 +00002541 }
2542#endif
danielk19774cd2cd52007-08-23 14:48:23 +00002543 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002544 /* Write the nRec value into the journal file header. If in
2545 ** full-synchronous mode, sync the journal first. This ensures that
2546 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002547 ** it as a candidate for rollback.
2548 **
2549 ** This is not required if the persistent media supports the
2550 ** SAFE_APPEND property. Because in this case it is not possible
2551 ** for garbage data to be appended to the file, the nRec field
2552 ** is populated with 0xFFFFFFFF when the journal header is written
2553 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002554 */
danielk19774cd2cd52007-08-23 14:48:23 +00002555 i64 jrnlOff;
2556 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002557 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002558 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002559 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002560 if( rc!=0 ) return rc;
2561 }
danielk197713adf8a2004-06-03 16:08:41 +00002562
danielk197762079062007-08-15 17:08:46 +00002563 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2564 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2565 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002566 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002567 }
danielk19774cd2cd52007-08-23 14:48:23 +00002568 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2569 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2570 IOTRACE(("JSYNC %p\n", pPager))
2571 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2572 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2573 );
2574 if( rc!=0 ) return rc;
2575 }
drhdb48ee02003-01-16 13:42:43 +00002576 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002577 }
drh50e5dad2001-09-15 00:57:28 +00002578 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002579
2580 /* Erase the needSync flag from every page.
2581 */
2582 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2583 pPg->needSync = 0;
2584 }
2585 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00002586 }
drh03eb96a2002-11-10 23:32:56 +00002587
drh341eae82003-01-21 02:39:36 +00002588#ifndef NDEBUG
2589 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2590 ** flag must also be clear for all pages. Verify that this
2591 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002592 */
drh341eae82003-01-21 02:39:36 +00002593 else{
2594 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2595 assert( pPg->needSync==0 );
2596 }
2597 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00002598 }
drh341eae82003-01-21 02:39:36 +00002599#endif
drhdb48ee02003-01-16 13:42:43 +00002600
drh81a20f22001-10-12 17:30:04 +00002601 return rc;
drh50e5dad2001-09-15 00:57:28 +00002602}
2603
2604/*
drhdc3e10b2006-06-15 14:31:06 +00002605** Merge two lists of pages connected by pDirty and in pgno order.
2606** Do not both fixing the pPrevDirty pointers.
2607*/
2608static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2609 PgHdr result, *pTail;
2610 pTail = &result;
2611 while( pA && pB ){
2612 if( pA->pgno<pB->pgno ){
2613 pTail->pDirty = pA;
2614 pTail = pA;
2615 pA = pA->pDirty;
2616 }else{
2617 pTail->pDirty = pB;
2618 pTail = pB;
2619 pB = pB->pDirty;
2620 }
2621 }
2622 if( pA ){
2623 pTail->pDirty = pA;
2624 }else if( pB ){
2625 pTail->pDirty = pB;
2626 }else{
2627 pTail->pDirty = 0;
2628 }
2629 return result.pDirty;
2630}
2631
2632/*
2633** Sort the list of pages in accending order by pgno. Pages are
2634** connected by pDirty pointers. The pPrevDirty pointers are
2635** corrupted by this sort.
2636*/
danielk197724168722007-04-02 05:07:47 +00002637#define N_SORT_BUCKET_ALLOC 25
2638#define N_SORT_BUCKET 25
2639#ifdef SQLITE_TEST
2640 int sqlite3_pager_n_sort_bucket = 0;
2641 #undef N_SORT_BUCKET
2642 #define N_SORT_BUCKET \
2643 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2644#endif
drhdc3e10b2006-06-15 14:31:06 +00002645static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002646 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002647 int i;
2648 memset(a, 0, sizeof(a));
2649 while( pIn ){
2650 p = pIn;
2651 pIn = p->pDirty;
2652 p->pDirty = 0;
2653 for(i=0; i<N_SORT_BUCKET-1; i++){
2654 if( a[i]==0 ){
2655 a[i] = p;
2656 break;
2657 }else{
2658 p = merge_pagelist(a[i], p);
2659 a[i] = 0;
2660 }
2661 }
2662 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002663 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2664 ** elements in the input list. This is possible, but impractical.
2665 ** Testing this line is the point of global variable
2666 ** sqlite3_pager_n_sort_bucket.
2667 */
drhdc3e10b2006-06-15 14:31:06 +00002668 a[i] = merge_pagelist(a[i], p);
2669 }
2670 }
2671 p = a[0];
2672 for(i=1; i<N_SORT_BUCKET; i++){
2673 p = merge_pagelist(p, a[i]);
2674 }
2675 return p;
2676}
2677
2678/*
drh2554f8b2003-01-22 01:26:44 +00002679** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2680** every one of those pages out to the database file and mark them all
2681** as clean.
2682*/
2683static int pager_write_pagelist(PgHdr *pList){
2684 Pager *pPager;
2685 int rc;
2686
2687 if( pList==0 ) return SQLITE_OK;
2688 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002689
2690 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2691 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002692 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002693 **
drha6abd042004-06-09 17:37:22 +00002694 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2695 ** through an intermediate state PENDING. A PENDING lock prevents new
2696 ** readers from attaching to the database but is unsufficient for us to
2697 ** write. The idea of a PENDING lock is to prevent new readers from
2698 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002699 **
drha6abd042004-06-09 17:37:22 +00002700 ** While the pager is in the RESERVED state, the original database file
2701 ** is unchanged and we can rollback without having to playback the
2702 ** journal into the original database file. Once we transition to
2703 ** EXCLUSIVE, it means the database file has been changed and any rollback
2704 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002705 */
drh684917c2004-10-05 02:41:42 +00002706 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002707 if( rc!=SQLITE_OK ){
2708 return rc;
2709 }
2710
drhdc3e10b2006-06-15 14:31:06 +00002711 pList = sort_pagelist(pList);
drh2554f8b2003-01-22 01:26:44 +00002712 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002713
2714 /* If the file has not yet been opened, open it now. */
2715 if( !pPager->fd->pMethods ){
2716 assert(pPager->tempFile);
2717 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename);
2718 if( rc ) return rc;
2719 }
2720
drh2554f8b2003-01-22 01:26:44 +00002721 assert( pList->dirty );
danielk1977687566d2004-11-02 12:56:41 +00002722 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002723 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002724 ** make the file smaller (presumably by auto-vacuum code). Do not write
2725 ** any such pages to the file.
2726 */
2727 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00002728 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00002729 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002730 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2731 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002732 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002733 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002734 PAGER_INCR(sqlite3_pager_writedb_count);
2735 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002736 if( pList->pgno==1 ){
2737 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2738 }
danielk1977687566d2004-11-02 12:56:41 +00002739 }
2740#ifndef NDEBUG
2741 else{
drh4f0c5872007-03-26 22:05:01 +00002742 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002743 }
2744#endif
drh2554f8b2003-01-22 01:26:44 +00002745 if( rc ) return rc;
2746 pList->dirty = 0;
danielk19773c407372005-02-15 02:54:14 +00002747#ifdef SQLITE_CHECK_PAGES
2748 pList->pageHash = pager_pagehash(pList);
2749#endif
drh2554f8b2003-01-22 01:26:44 +00002750 pList = pList->pDirty;
2751 }
2752 return SQLITE_OK;
2753}
2754
2755/*
2756** Collect every dirty page into a dirty list and
2757** return a pointer to the head of that list. All pages are
2758** collected even if they are still in use.
2759*/
2760static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh605ad8f2006-05-03 23:34:05 +00002761 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00002762}
2763
2764/*
drh165ffe92005-03-15 17:09:30 +00002765** Return TRUE if there is a hot journal on the given pager.
2766** A hot journal is one that needs to be played back.
2767**
2768** If the current size of the database file is 0 but a journal file
2769** exists, that is probably an old journal left over from a prior
2770** database with the same name. Just delete the journal.
2771*/
2772static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00002773 sqlite3_vfs *pVfs = pPager->pVfs;
drh165ffe92005-03-15 17:09:30 +00002774 if( !pPager->useJournal ) return 0;
danielk1977b4b47412007-08-17 15:53:36 +00002775 if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
drhbb5f18d2007-04-06 18:23:17 +00002776 return 0;
2777 }
2778 if( sqlite3OsCheckReservedLock(pPager->fd) ){
2779 return 0;
2780 }
danielk19773b8a05f2007-03-19 17:44:26 +00002781 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00002782 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00002783 return 0;
2784 }else{
2785 return 1;
2786 }
2787}
2788
danielk19775591df52005-12-20 09:19:37 +00002789/*
danielk1977aef0bf62005-12-30 16:28:01 +00002790** Try to find a page in the cache that can be recycled.
2791**
2792** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00002793** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00002794*/
danielk197713f72992005-12-18 08:51:22 +00002795static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
2796 PgHdr *pPg;
2797 *ppPg = 0;
2798
danielk1977c551edc2007-04-05 13:12:13 +00002799 assert(!MEMDB);
2800
danielk197713f72992005-12-18 08:51:22 +00002801 /* Find a page to recycle. Try to locate a page that does not
2802 ** require us to do an fsync() on the journal.
2803 */
2804 pPg = pPager->pFirstSynced;
2805
2806 /* If we could not find a page that does not require an fsync()
2807 ** on the journal file then fsync the journal file. This is a
2808 ** very slow operation, so we work hard to avoid it. But sometimes
2809 ** it can't be helped.
2810 */
drh8940f4e2007-08-11 00:26:20 +00002811 if( pPg==0 && pPager->pFirst && syncOk && !MEMDB){
danielk19774cd2cd52007-08-23 14:48:23 +00002812 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk197713f72992005-12-18 08:51:22 +00002813 int rc = syncJournal(pPager);
2814 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002815 return rc;
danielk197713f72992005-12-18 08:51:22 +00002816 }
danielk19774cd2cd52007-08-23 14:48:23 +00002817 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197713f72992005-12-18 08:51:22 +00002818 /* If in full-sync mode, write a new journal header into the
2819 ** journal file. This is done to avoid ever modifying a journal
2820 ** header that is involved in the rollback of pages that have
2821 ** already been written to the database (in case the header is
2822 ** trashed when the nRec field is updated).
2823 */
2824 pPager->nRec = 0;
2825 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00002826 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00002827 rc = writeJournalHdr(pPager);
2828 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002829 return rc;
danielk197713f72992005-12-18 08:51:22 +00002830 }
2831 }
2832 pPg = pPager->pFirst;
2833 }
2834 if( pPg==0 ){
2835 return SQLITE_OK;
2836 }
2837
2838 assert( pPg->nRef==0 );
2839
2840 /* Write the page to the database file if it is dirty.
2841 */
2842 if( pPg->dirty ){
2843 int rc;
2844 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00002845 makeClean(pPg);
2846 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00002847 pPg->pDirty = 0;
2848 rc = pager_write_pagelist( pPg );
2849 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00002850 return rc;
danielk197713f72992005-12-18 08:51:22 +00002851 }
2852 }
2853 assert( pPg->dirty==0 );
2854
2855 /* If the page we are recycling is marked as alwaysRollback, then
2856 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00002857 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00002858 ** It is necessary to do this because the page marked alwaysRollback
2859 ** might be reloaded at a later time but at that point we won't remember
2860 ** that is was marked alwaysRollback. This means that all pages must
2861 ** be marked as alwaysRollback from here on out.
2862 */
2863 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00002864 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00002865 pPager->alwaysRollback = 1;
2866 }
2867
2868 /* Unlink the old page from the free list and the hash table
2869 */
2870 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00002871 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00002872
2873 *ppPg = pPg;
2874 return SQLITE_OK;
2875}
2876
drh86f8c192007-08-22 00:39:19 +00002877#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00002878/*
2879** This function is called to free superfluous dynamically allocated memory
2880** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00002881** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00002882**
2883** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00002884** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00002885** of bytes of memory released.
2886*/
danielk19773b8a05f2007-03-19 17:44:26 +00002887int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00002888 int nReleased = 0; /* Bytes of memory released so far */
2889 sqlite3_mutex *mutex; /* The MEM2 mutex */
2890 Pager *pPager; /* For looping over pagers */
2891 int i; /* Passes over pagers */
danielk197713f72992005-12-18 08:51:22 +00002892
drh86f8c192007-08-22 00:39:19 +00002893 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00002894 */
drh86f8c192007-08-22 00:39:19 +00002895 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2896 sqlite3_mutex_enter(mutex);
2897
2898 /* Signal all database connections that memory management wants
2899 ** to have access to the pagers.
2900 */
2901 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
2902 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00002903 }
2904
danielk197713f72992005-12-18 08:51:22 +00002905 /* Outermost loop runs for at most two iterations. First iteration we
2906 ** try to find memory that can be released without calling fsync(). Second
2907 ** iteration (which only runs if the first failed to free nReq bytes of
2908 ** memory) is permitted to call fsync(). This is of course much more
2909 ** expensive.
2910 */
drh29c636b2006-01-09 23:40:25 +00002911 for(i=0; i<=1; i++){
danielk197713f72992005-12-18 08:51:22 +00002912
2913 /* Loop through all the SQLite pagers opened by the current thread. */
drh86f8c192007-08-22 00:39:19 +00002914 Pager *pPager = sqlite3PagerList;
danielk197738ec0cb2007-04-05 14:29:42 +00002915 for( ; pPager && (nReq<0 || nReleased<nReq); pPager=pPager->pNext){
danielk197713f72992005-12-18 08:51:22 +00002916 PgHdr *pPg;
drhed138fb2007-08-22 22:04:37 +00002917 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00002918
drh86f8c192007-08-22 00:39:19 +00002919 /* In-memory databases should not appear on the pager list */
2920 assert( !MEMDB );
2921
2922 /* Skip pagers that are currently in use by the b-tree layer */
2923 if( pPager->iInUseDB ) continue;
danielk1977c551edc2007-04-05 13:12:13 +00002924
danielk197713f72992005-12-18 08:51:22 +00002925 /* For each pager, try to free as many pages as possible (without
2926 ** calling fsync() if this is the first iteration of the outermost
2927 ** loop).
2928 */
drhed138fb2007-08-22 22:04:37 +00002929 while( (nReq<0 || nReleased<nReq) &&
2930 SQLITE_OK==(rc = pager_recycle(pPager, i, &pPg)) &&
2931 pPg
2932 ) {
danielk1977aef0bf62005-12-30 16:28:01 +00002933 /* We've found a page to free. At this point the page has been
danielk197713f72992005-12-18 08:51:22 +00002934 ** removed from the page hash-table, free-list and synced-list
danielk1977aef0bf62005-12-30 16:28:01 +00002935 ** (pFirstSynced). It is still in the all pages (pAll) list.
danielk197713f72992005-12-18 08:51:22 +00002936 ** Remove it from this list before freeing.
2937 **
2938 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
2939 ** probably is though.
2940 */
2941 PgHdr *pTmp;
danielk19770190d1d2005-12-19 14:18:11 +00002942 assert( pPg );
danielk197738ec0cb2007-04-05 14:29:42 +00002943 if( pPg==pPager->pAll ){
2944 pPager->pAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00002945 }else{
danielk197738ec0cb2007-04-05 14:29:42 +00002946 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
danielk197713f72992005-12-18 08:51:22 +00002947 pTmp->pNextAll = pPg->pNextAll;
2948 }
danielk19771e536952007-08-16 10:09:01 +00002949 nReleased += (
2950 sizeof(*pPg) + pPager->pageSize
2951 + sizeof(u32) + pPager->nExtra
2952 + MEMDB*sizeof(PgHistory)
2953 );
drh34f56212007-08-10 23:56:35 +00002954 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
drh538f5702007-04-13 02:14:30 +00002955 PAGER_INCR(sqlite3_pager_pgfree_count);
drh17435752007-08-16 04:30:38 +00002956 sqlite3_free(pPg);
danielk197713f72992005-12-18 08:51:22 +00002957 }
2958
2959 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00002960 /* An error occured whilst writing to the database file or
2961 ** journal in pager_recycle(). The error is not returned to the
danielk1977efaaf572006-01-16 11:29:19 +00002962 ** caller of this function. Instead, set the Pager.errCode variable.
danielk1977aef0bf62005-12-30 16:28:01 +00002963 ** The error will be returned to the user (or users, in the case
2964 ** of a shared pager cache) of the pager for which the error occured.
danielk197713f72992005-12-18 08:51:22 +00002965 */
drh34f56212007-08-10 23:56:35 +00002966 assert(
2967 (rc&0xff)==SQLITE_IOERR ||
2968 rc==SQLITE_FULL ||
2969 rc==SQLITE_BUSY
2970 );
danielk197738ec0cb2007-04-05 14:29:42 +00002971 assert( pPager->state>=PAGER_RESERVED );
2972 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00002973 }
2974 }
2975 }
danielk1977aef0bf62005-12-30 16:28:01 +00002976
drh86f8c192007-08-22 00:39:19 +00002977 /* Clear the memory management flags and release the mutex
2978 */
2979 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
2980 pPager->iInUseMM = 0;
2981 }
2982 sqlite3_mutex_leave(mutex);
2983
2984 /* Return the number of bytes released
2985 */
danielk197713f72992005-12-18 08:51:22 +00002986 return nReleased;
2987}
drh86f8c192007-08-22 00:39:19 +00002988#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00002989
drh165ffe92005-03-15 17:09:30 +00002990/*
danielk1977e180dd92007-04-05 17:15:52 +00002991** Read the content of page pPg out of the database file.
2992*/
2993static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2994 int rc;
danielk197762079062007-08-15 17:08:46 +00002995 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002996 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002997 assert(pPager->fd->pMethods||pPager->tempFile);
2998 if( !pPager->fd->pMethods ){
2999 return SQLITE_IOERR_SHORT_READ;
3000 }
danielk197762079062007-08-15 17:08:46 +00003001 offset = (pgno-1)*(i64)pPager->pageSize;
3002 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003003 PAGER_INCR(sqlite3_pager_readdb_count);
3004 PAGER_INCR(pPager->nRead);
3005 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003006 if( pgno==1 ){
3007 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3008 sizeof(pPager->dbFileVers));
3009 }
danielk1977e180dd92007-04-05 17:15:52 +00003010 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003011 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3012 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003013 return rc;
3014}
3015
3016
3017/*
danielk1977e277be02007-03-23 18:12:06 +00003018** This function is called to obtain the shared lock required before
3019** data may be read from the pager cache. If the shared lock has already
3020** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003021**
3022** Immediately after obtaining the shared lock (if required), this function
3023** checks for a hot-journal file. If one is found, an emergency rollback
3024** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003025*/
3026static int pagerSharedLock(Pager *pPager){
3027 int rc = SQLITE_OK;
3028
3029 if( pPager->state==PAGER_UNLOCK ){
danielk1977b4b47412007-08-17 15:53:36 +00003030 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003031 if( !MEMDB ){
3032 assert( pPager->nRef==0 );
3033 if( !pPager->noReadlock ){
3034 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3035 if( rc!=SQLITE_OK ){
3036 return pager_error(pPager, rc);
3037 }
3038 assert( pPager->state>=SHARED_LOCK );
3039 }
3040
3041 /* If a journal file exists, and there is no RESERVED lock on the
3042 ** database file, then it either needs to be played back or deleted.
3043 */
3044 if( hasHotJournal(pPager) ){
3045 /* Get an EXCLUSIVE lock on the database file. At this point it is
3046 ** important that a RESERVED lock is not obtained on the way to the
3047 ** EXCLUSIVE lock. If it were, another process might open the
3048 ** database file, detect the RESERVED lock, and conclude that the
3049 ** database is safe to read while this process is still rolling it
3050 ** back.
3051 **
3052 ** Because the intermediate RESERVED lock is not requested, the
3053 ** second process will get to this point in the code and fail to
3054 ** obtain it's own EXCLUSIVE lock on the database file.
3055 */
3056 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3057 if( rc!=SQLITE_OK ){
3058 pager_unlock(pPager);
3059 return pager_error(pPager, rc);
3060 }
3061 pPager->state = PAGER_EXCLUSIVE;
3062
3063 /* Open the journal for reading only. Return SQLITE_BUSY if
3064 ** we are unable to open the journal file.
3065 **
3066 ** The journal file does not need to be locked itself. The
3067 ** journal file is never open unless the main database file holds
3068 ** a write lock, so there is never any chance of two or more
3069 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00003070 **
drhfd131da2007-08-07 17:13:03 +00003071 ** Open the journal for read/write access. This is because in
3072 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003073 ** possibly used for a transaction later on. On some systems, the
3074 ** OsTruncate() call used in exclusive-access mode also requires
3075 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003076 */
danielk1977979f38e2007-03-27 16:19:51 +00003077 rc = SQLITE_BUSY;
danielk1977b4b47412007-08-17 15:53:36 +00003078 if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
3079 int fout = 0;
danielk1977fee2d252007-08-18 10:59:19 +00003080 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
danielk19777152de82007-03-29 17:28:14 +00003081 assert( !pPager->tempFile );
danielk1977c7b60172007-08-22 11:22:03 +00003082 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags,&fout);
danielk1977b4b47412007-08-17 15:53:36 +00003083 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3084 if( fout&SQLITE_OPEN_READONLY ){
danielk1977979f38e2007-03-27 16:19:51 +00003085 rc = SQLITE_BUSY;
danielk1977b4b47412007-08-17 15:53:36 +00003086 sqlite3OsClose(pPager->jfd);
danielk1977979f38e2007-03-27 16:19:51 +00003087 }
3088 }
danielk1977e277be02007-03-23 18:12:06 +00003089 if( rc!=SQLITE_OK ){
3090 pager_unlock(pPager);
3091 return SQLITE_BUSY;
3092 }
3093 pPager->journalOpen = 1;
3094 pPager->journalStarted = 0;
3095 pPager->journalOff = 0;
3096 pPager->setMaster = 0;
3097 pPager->journalHdr = 0;
3098
3099 /* Playback and delete the journal. Drop the database write
3100 ** lock and reacquire the read lock.
3101 */
3102 rc = pager_playback(pPager, 1);
3103 if( rc!=SQLITE_OK ){
3104 return pager_error(pPager, rc);
3105 }
danielk1977c5859712007-03-26 12:26:27 +00003106 assert(pPager->state==PAGER_SHARED ||
3107 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3108 );
danielk1977e277be02007-03-23 18:12:06 +00003109 }
3110
3111 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003112 /* The shared-lock has just been acquired on the database file
3113 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003114 ** read or write transaction). Check to see if the database
3115 ** has been modified. If the database has changed, flush the
3116 ** cache.
3117 **
3118 ** Database changes is detected by looking at 15 bytes beginning
3119 ** at offset 24 into the file. The first 4 of these 16 bytes are
3120 ** a 32-bit counter that is incremented with each change. The
3121 ** other bytes change randomly with each file change when
3122 ** a codec is in use.
3123 **
3124 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003125 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003126 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003127 */
drh86a88112007-04-16 15:02:19 +00003128 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003129 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003130
danielk1977e180dd92007-04-05 17:15:52 +00003131 if( pPager->errCode ){
3132 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00003133 }
3134
danielk1977e180dd92007-04-05 17:15:52 +00003135 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003136 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003137 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003138 if( rc!=SQLITE_OK ){
3139 return rc;
3140 }
drh86a88112007-04-16 15:02:19 +00003141 }else{
3142 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003143 }
3144
drh86a88112007-04-16 15:02:19 +00003145 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003146 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003147 }
3148 }
3149 }
danielk1977c5859712007-03-26 12:26:27 +00003150 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3151 if( pPager->state==PAGER_UNLOCK ){
3152 pPager->state = PAGER_SHARED;
3153 }
danielk1977e277be02007-03-23 18:12:06 +00003154 }
3155
3156 return rc;
3157}
3158
3159/*
danielk1977e180dd92007-04-05 17:15:52 +00003160** Allocate a PgHdr object. Either create a new one or reuse
3161** an existing one that is not otherwise in use.
3162**
3163** A new PgHdr structure is created if any of the following are
3164** true:
3165**
3166** (1) We have not exceeded our maximum allocated cache size
3167** as set by the "PRAGMA cache_size" command.
3168**
3169** (2) There are no unused PgHdr objects available at this time.
3170**
3171** (3) This is an in-memory database.
3172**
3173** (4) There are no PgHdr objects that do not require a journal
3174** file sync and a sync of the journal file is currently
3175** prohibited.
3176**
3177** Otherwise, reuse an existing PgHdr. In other words, reuse an
3178** existing PgHdr if all of the following are true:
3179**
3180** (1) We have reached or exceeded the maximum cache size
3181** allowed by "PRAGMA cache_size".
3182**
3183** (2) There is a PgHdr available with PgHdr->nRef==0
3184**
3185** (3) We are not in an in-memory database
3186**
3187** (4) Either there is an available PgHdr that does not need
3188** to be synced to disk or else disk syncing is currently
3189** allowed.
danielk197724168722007-04-02 05:07:47 +00003190*/
3191static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3192 int rc = SQLITE_OK;
3193 PgHdr *pPg;
3194
danielk1977e180dd92007-04-05 17:15:52 +00003195 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003196 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003197 if( pPager->nPage<pPager->mxPage
3198 || pPager->pFirst==0
3199 || MEMDB
3200 || (pPager->pFirstSynced==0 && pPager->doNotSync)
3201 ){
danielk197724168722007-04-02 05:07:47 +00003202 if( pPager->nPage>=pPager->nHash ){
3203 pager_resize_hash_table(pPager,
3204 pPager->nHash<256 ? 256 : pPager->nHash*2);
3205 if( pPager->nHash==0 ){
3206 rc = SQLITE_NOMEM;
3207 goto pager_allocate_out;
3208 }
3209 }
drhed138fb2007-08-22 22:04:37 +00003210 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003211 pPg = sqlite3_malloc( sizeof(*pPg) + pPager->pageSize
danielk197724168722007-04-02 05:07:47 +00003212 + sizeof(u32) + pPager->nExtra
3213 + MEMDB*sizeof(PgHistory) );
drhed138fb2007-08-22 22:04:37 +00003214 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003215 if( pPg==0 ){
3216 rc = SQLITE_NOMEM;
3217 goto pager_allocate_out;
3218 }
3219 memset(pPg, 0, sizeof(*pPg));
3220 if( MEMDB ){
3221 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
3222 }
3223 pPg->pPager = pPager;
3224 pPg->pNextAll = pPager->pAll;
3225 pPager->pAll = pPg;
3226 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003227 }else{
3228 /* Recycle an existing page with a zero ref-count. */
3229 rc = pager_recycle(pPager, 1, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003230 if( rc==SQLITE_BUSY ){
3231 rc = SQLITE_IOERR_BLOCKED;
3232 }
danielk197724168722007-04-02 05:07:47 +00003233 if( rc!=SQLITE_OK ){
3234 goto pager_allocate_out;
3235 }
3236 assert( pPager->state>=SHARED_LOCK );
3237 assert(pPg);
3238 }
3239 *ppPg = pPg;
3240
3241pager_allocate_out:
3242 return rc;
3243}
3244
3245/*
drhd33d5a82007-04-26 12:11:28 +00003246** Make sure we have the content for a page. If the page was
3247** previously acquired with noContent==1, then the content was
3248** just initialized to zeros instead of being read from disk.
3249** But now we need the real data off of disk. So make sure we
3250** have it. Read it in if we do not have it already.
3251*/
3252static int pager_get_content(PgHdr *pPg){
3253 if( pPg->needRead ){
3254 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3255 if( rc==SQLITE_OK ){
3256 pPg->needRead = 0;
3257 }else{
3258 return rc;
3259 }
3260 }
3261 return SQLITE_OK;
3262}
3263
3264/*
drhd9b02572001-04-15 00:37:09 +00003265** Acquire a page.
3266**
drh58a11682001-11-10 13:51:08 +00003267** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003268** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003269**
drhd33d5a82007-04-26 12:11:28 +00003270** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003271** file is smaller than the requested page, then no actual disk
3272** read occurs and the memory image of the page is initialized to
3273** all zeros. The extra data appended to a page is always initialized
3274** to zeros the first time a page is loaded into memory.
3275**
drhd9b02572001-04-15 00:37:09 +00003276** The acquisition might fail for several reasons. In all cases,
3277** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003278**
drhd33d5a82007-04-26 12:11:28 +00003279** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003280** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003281** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003282** just returns 0. This routine acquires a read-lock the first time it
3283** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003284** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003285** or journal files.
drh0787db62007-03-04 13:15:27 +00003286**
drh538f5702007-04-13 02:14:30 +00003287** If noContent is false, the page contents are actually read from disk.
3288** If noContent is true, it means that we do not care about the contents
3289** of the page at this time, so do not do a disk read. Just fill in the
3290** page content with zeros. But mark the fact that we have not read the
3291** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003292** sqlite3PagerWrite() is called on this page or if this routine is
3293** called again with noContent==0, that means that the content is needed
3294** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003295*/
drh86f8c192007-08-22 00:39:19 +00003296static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003297 Pager *pPager, /* The pager open on the database file */
3298 Pgno pgno, /* Page number to fetch */
3299 DbPage **ppPage, /* Write a pointer to the page here */
3300 int noContent /* Do not bother reading content from disk if true */
3301){
drhed7c8552001-04-11 14:29:21 +00003302 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003303 int rc;
drhed7c8552001-04-11 14:29:21 +00003304
danielk1977e277be02007-03-23 18:12:06 +00003305 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3306
danielk197726836652005-01-17 01:33:13 +00003307 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3308 ** number greater than this, or zero, is requested.
3309 */
drh267cb322005-09-16 17:16:52 +00003310 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003311 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003312 }
3313
drhd9b02572001-04-15 00:37:09 +00003314 /* Make sure we have not hit any critical errors.
3315 */
drh836faa42003-01-11 13:30:57 +00003316 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003317 *ppPage = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003318 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3319 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003320 }
3321
danielk197713adf8a2004-06-03 16:08:41 +00003322 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003323 ** on the database file. pagerSharedLock() is a no-op if
3324 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003325 */
danielk1977e277be02007-03-23 18:12:06 +00003326 rc = pagerSharedLock(pPager);
3327 if( rc!=SQLITE_OK ){
3328 return rc;
drhed7c8552001-04-11 14:29:21 +00003329 }
danielk1977e277be02007-03-23 18:12:06 +00003330 assert( pPager->state!=PAGER_UNLOCK );
3331
3332 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003333 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003334 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003335 int nMax;
drhed7c8552001-04-11 14:29:21 +00003336 int h;
drh538f5702007-04-13 02:14:30 +00003337 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003338 rc = pagerAllocatePage(pPager, &pPg);
3339 if( rc!=SQLITE_OK ){
3340 return rc;
drhed7c8552001-04-11 14:29:21 +00003341 }
danielk197724168722007-04-02 05:07:47 +00003342
drhed7c8552001-04-11 14:29:21 +00003343 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003344 assert( !MEMDB || pgno>pPager->stmtSize );
drh1ab43002002-01-14 09:28:19 +00003345 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19771e536952007-08-16 10:09:01 +00003346#if 0
danielk19774adee202004-05-08 08:23:19 +00003347 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
danielk19771e536952007-08-16 10:09:01 +00003348#endif
drhdb48ee02003-01-16 13:42:43 +00003349 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00003350 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00003351 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003352 }else{
3353 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00003354 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003355 }
danielk1977f35843b2007-04-07 15:03:17 +00003356
drh605ad8f2006-05-03 23:34:05 +00003357 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003358 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00003359 REFINFO(pPg);
danielk197775bab7d2006-01-23 13:09:45 +00003360
drhd9b02572001-04-15 00:37:09 +00003361 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003362 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003363 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003364 }
danielk1977979f38e2007-03-27 16:19:51 +00003365 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003366 if( pPager->errCode ){
danielk19773b8a05f2007-03-19 17:44:26 +00003367 sqlite3PagerUnref(pPg);
danielk1977efaaf572006-01-16 11:29:19 +00003368 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00003369 return rc;
3370 }
danielk197775bab7d2006-01-23 13:09:45 +00003371
3372 /* Populate the page with data, either by reading from the database
3373 ** file, or by setting the entire page to zero.
3374 */
drhd215acf2007-04-13 04:01:58 +00003375 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003376 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003377 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003378 return SQLITE_FULL;
3379 }
drh90f5ecb2004-07-22 01:19:35 +00003380 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003381 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003382 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003383 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003384 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003385 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3386 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003387 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003388 return rc;
drh81a20f22001-10-12 17:30:04 +00003389 }
danielk1977b4626a32007-04-28 15:47:43 +00003390 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003391 }
danielk197775bab7d2006-01-23 13:09:45 +00003392
3393 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003394 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003395 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003396 pPg->pNextHash = pPager->aHash[h];
3397 pPager->aHash[h] = pPg;
3398 if( pPg->pNextHash ){
3399 assert( pPg->pNextHash->pPrevHash==0 );
3400 pPg->pNextHash->pPrevHash = pPg;
3401 }
3402
danielk19773c407372005-02-15 02:54:14 +00003403#ifdef SQLITE_CHECK_PAGES
3404 pPg->pageHash = pager_pagehash(pPg);
3405#endif
drhed7c8552001-04-11 14:29:21 +00003406 }else{
drhd9b02572001-04-15 00:37:09 +00003407 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003408 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003409 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003410 if( !noContent ){
3411 rc = pager_get_content(pPg);
3412 if( rc ){
3413 return rc;
3414 }
3415 }
drhdf0b3b02001-06-23 11:36:20 +00003416 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003417 }
danielk19773b8a05f2007-03-19 17:44:26 +00003418 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003419 return SQLITE_OK;
3420}
drh86f8c192007-08-22 00:39:19 +00003421int sqlite3PagerAcquire(
3422 Pager *pPager, /* The pager open on the database file */
3423 Pgno pgno, /* Page number to fetch */
3424 DbPage **ppPage, /* Write a pointer to the page here */
3425 int noContent /* Do not bother reading content from disk if true */
3426){
3427 int rc;
3428 pagerEnter(pPager);
3429 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3430 pagerLeave(pPager);
3431 return rc;
3432}
3433
drhed7c8552001-04-11 14:29:21 +00003434
3435/*
drh7e3b0a02001-04-28 16:52:40 +00003436** Acquire a page if it is already in the in-memory cache. Do
3437** not read the page from disk. Return a pointer to the page,
3438** or 0 if the page is not in cache.
3439**
danielk19773b8a05f2007-03-19 17:44:26 +00003440** See also sqlite3PagerGet(). The difference between this routine
3441** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003442** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003443** returns NULL if the page is not in cache or if a disk I/O error
3444** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003445*/
danielk19773b8a05f2007-03-19 17:44:26 +00003446DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003447 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003448
drh836faa42003-01-11 13:30:57 +00003449 assert( pPager!=0 );
3450 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003451
drh86f8c192007-08-22 00:39:19 +00003452 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003453 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003454 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003455 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3456 /* Do nothing */
3457 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3458 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003459 }
drh86f8c192007-08-22 00:39:19 +00003460 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003461 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003462}
3463
3464/*
drhed7c8552001-04-11 14:29:21 +00003465** Release a page.
3466**
3467** If the number of references to the page drop to zero, then the
3468** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003469** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003470** removed.
3471*/
danielk19773b8a05f2007-03-19 17:44:26 +00003472int sqlite3PagerUnref(DbPage *pPg){
drhd9b02572001-04-15 00:37:09 +00003473
3474 /* Decrement the reference count for this page
3475 */
drhed7c8552001-04-11 14:29:21 +00003476 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003477 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003478 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00003479 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00003480
danielk19773c407372005-02-15 02:54:14 +00003481 CHECK_PAGE(pPg);
3482
drh72f82862001-05-24 21:06:34 +00003483 /* When the number of references to a page reach 0, call the
3484 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003485 */
drhed7c8552001-04-11 14:29:21 +00003486 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00003487 Pager *pPager;
3488 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003489 pPg->pNextFree = 0;
3490 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00003491 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00003492 if( pPg->pPrevFree ){
3493 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00003494 }else{
3495 pPager->pFirst = pPg;
3496 }
drh341eae82003-01-21 02:39:36 +00003497 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
3498 pPager->pFirstSynced = pPg;
3499 }
drh72f82862001-05-24 21:06:34 +00003500 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003501 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003502 }
drhd9b02572001-04-15 00:37:09 +00003503
3504 /* When all pages reach the freelist, drop the read lock from
3505 ** the database file.
3506 */
3507 pPager->nRef--;
3508 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003509 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003510 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003511 }
drhed7c8552001-04-11 14:29:21 +00003512 }
drh86f8c192007-08-22 00:39:19 +00003513 pagerLeave(pPg->pPager);
drhd9b02572001-04-15 00:37:09 +00003514 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003515}
3516
3517/*
drha6abd042004-06-09 17:37:22 +00003518** Create a journal file for pPager. There should already be a RESERVED
3519** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003520**
3521** Return SQLITE_OK if everything. Return an error code and release the
3522** write lock if anything goes wrong.
3523*/
3524static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003525 sqlite3_vfs *pVfs = pPager->pVfs;
3526 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3527
drhda47d772002-12-02 04:25:19 +00003528 int rc;
drhb7f91642004-10-31 02:22:47 +00003529 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003530 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003531 assert( pPager->journalOpen==0 );
3532 assert( pPager->useJournal );
drh88b01a12005-03-28 18:04:27 +00003533 assert( pPager->aInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003534 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003535 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003536 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00003537 pagerEnter(pPager);
drhda47d772002-12-02 04:25:19 +00003538 if( pPager->aInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003539 rc = SQLITE_NOMEM;
3540 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003541 }
danielk1977b4b47412007-08-17 15:53:36 +00003542
3543 if( pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00003544 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3545 }else{
3546 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
danielk1977b4b47412007-08-17 15:53:36 +00003547 }
danielk1977c7b60172007-08-22 11:22:03 +00003548#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3549 rc = sqlite3JournalOpen(
3550 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3551 );
3552#else
danielk1977b4b47412007-08-17 15:53:36 +00003553 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
danielk1977c7b60172007-08-22 11:22:03 +00003554#endif
danielk1977b4b47412007-08-17 15:53:36 +00003555 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk197776572402004-06-25 02:38:54 +00003556 pPager->journalOff = 0;
3557 pPager->setMaster = 0;
3558 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003559 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003560 if( rc==SQLITE_NOMEM ){
danielk1977fee2d252007-08-18 10:59:19 +00003561 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh600e46a2007-03-28 01:59:33 +00003562 }
drh9c105bb2004-10-02 20:38:28 +00003563 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003564 }
3565 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003566 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003567 pPager->needSync = 0;
3568 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003569 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003570 if( pPager->errCode ){
3571 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003572 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003573 }
drhda47d772002-12-02 04:25:19 +00003574 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003575
danielk197776572402004-06-25 02:38:54 +00003576 rc = writeJournalHdr(pPager);
3577
drhac69b052004-05-12 13:30:07 +00003578 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003579 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003580 }
danielk1977261919c2005-12-06 12:52:59 +00003581 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003582 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003583 if( rc==SQLITE_OK ){
3584 rc = SQLITE_FULL;
3585 }
3586 }
drh9c105bb2004-10-02 20:38:28 +00003587 return rc;
3588
3589failed_to_open_journal:
drh17435752007-08-16 04:30:38 +00003590 sqlite3_free(pPager->aInJournal);
drh9c105bb2004-10-02 20:38:28 +00003591 pPager->aInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003592 return rc;
drhda47d772002-12-02 04:25:19 +00003593}
3594
3595/*
drh4b845d72002-03-05 12:41:19 +00003596** Acquire a write-lock on the database. The lock is removed when
3597** the any of the following happen:
3598**
drh80e35f42007-03-30 14:06:34 +00003599** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003600** * sqlite3PagerRollback() is called.
3601** * sqlite3PagerClose() is called.
3602** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003603**
danielk197713adf8a2004-06-03 16:08:41 +00003604** The first parameter to this routine is a pointer to any open page of the
3605** database file. Nothing changes about the page - it is used merely to
3606** acquire a pointer to the Pager structure and as proof that there is
3607** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003608**
danielk197713adf8a2004-06-03 16:08:41 +00003609** The second parameter indicates how much space in bytes to reserve for a
3610** master journal file-name at the start of the journal when it is created.
3611**
3612** A journal file is opened if this is not a temporary file. For temporary
3613** files, the opening of the journal file is deferred until there is an
3614** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003615**
drha6abd042004-06-09 17:37:22 +00003616** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003617**
3618** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3619** immediately instead of waiting until we try to flush the cache. The
3620** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003621*/
danielk19773b8a05f2007-03-19 17:44:26 +00003622int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003623 Pager *pPager = pPg->pPager;
3624 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003625 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003626 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003627 assert( pPager->state!=PAGER_UNLOCK );
3628 if( pPager->state==PAGER_SHARED ){
drh4b845d72002-03-05 12:41:19 +00003629 assert( pPager->aInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003630 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003631 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003632 pPager->origDbSize = pPager->dbSize;
3633 }else{
drh054889e2005-11-30 03:20:31 +00003634 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003635 if( rc==SQLITE_OK ){
3636 pPager->state = PAGER_RESERVED;
3637 if( exFlag ){
3638 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3639 }
3640 }
danielk19779eed5052004-06-04 10:38:30 +00003641 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003642 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003643 return rc;
drhac69b052004-05-12 13:30:07 +00003644 }
drha6abd042004-06-09 17:37:22 +00003645 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003646 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003647 if( pPager->useJournal && !pPager->tempFile ){
3648 rc = pager_open_journal(pPager);
3649 }
drh4b845d72002-03-05 12:41:19 +00003650 }
danielk1977334cdb62007-03-26 08:05:12 +00003651 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3652 /* This happens when the pager was in exclusive-access mode last
3653 ** time a (read or write) transaction was successfully concluded
3654 ** by this connection. Instead of deleting the journal file it was
3655 ** kept open and truncated to 0 bytes.
3656 */
3657 assert( pPager->nRec==0 );
3658 assert( pPager->origDbSize==0 );
drh369339d2007-03-30 16:01:55 +00003659 assert( pPager->aInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003660 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003661 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003662 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00003663 pagerEnter(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00003664 if( !pPager->aInJournal ){
3665 rc = SQLITE_NOMEM;
3666 }else{
drh369339d2007-03-30 16:01:55 +00003667 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003668 rc = writeJournalHdr(pPager);
3669 }
drh4b845d72002-03-05 12:41:19 +00003670 }
danielk1977334cdb62007-03-26 08:05:12 +00003671 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00003672 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00003673 return rc;
3674}
3675
3676/*
drh605ad8f2006-05-03 23:34:05 +00003677** Make a page dirty. Set its dirty flag and add it to the dirty
3678** page list.
3679*/
3680static void makeDirty(PgHdr *pPg){
3681 if( pPg->dirty==0 ){
3682 Pager *pPager = pPg->pPager;
3683 pPg->dirty = 1;
3684 pPg->pDirty = pPager->pDirty;
3685 if( pPager->pDirty ){
3686 pPager->pDirty->pPrevDirty = pPg;
3687 }
3688 pPg->pPrevDirty = 0;
3689 pPager->pDirty = pPg;
3690 }
3691}
3692
3693/*
3694** Make a page clean. Clear its dirty bit and remove it from the
3695** dirty page list.
3696*/
3697static void makeClean(PgHdr *pPg){
3698 if( pPg->dirty ){
3699 pPg->dirty = 0;
3700 if( pPg->pDirty ){
3701 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
3702 }
3703 if( pPg->pPrevDirty ){
3704 pPg->pPrevDirty->pDirty = pPg->pDirty;
3705 }else{
3706 pPg->pPager->pDirty = pPg->pDirty;
3707 }
3708 }
3709}
3710
3711
3712/*
drhed7c8552001-04-11 14:29:21 +00003713** Mark a data page as writeable. The page is written into the journal
3714** if it is not there already. This routine must be called before making
3715** changes to a page.
3716**
3717** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003718** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003719** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003720** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003721** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003722**
3723** If the journal file could not be written because the disk is full,
3724** then this routine returns SQLITE_FULL and does an immediate rollback.
3725** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003726** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003727** reset.
drhed7c8552001-04-11 14:29:21 +00003728*/
danielk19773b8a05f2007-03-19 17:44:26 +00003729static int pager_write(PgHdr *pPg){
3730 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00003731 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003732 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003733
drh6446c4d2001-12-15 14:22:18 +00003734 /* Check for errors
3735 */
danielk1977efaaf572006-01-16 11:29:19 +00003736 if( pPager->errCode ){
3737 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003738 }
drh5e00f6c2001-09-13 13:46:56 +00003739 if( pPager->readOnly ){
3740 return SQLITE_PERM;
3741 }
drh6446c4d2001-12-15 14:22:18 +00003742
danielk197776572402004-06-25 02:38:54 +00003743 assert( !pPager->setMaster );
3744
danielk19773c407372005-02-15 02:54:14 +00003745 CHECK_PAGE(pPg);
3746
drh538f5702007-04-13 02:14:30 +00003747 /* If this page was previously acquired with noContent==1, that means
3748 ** we didn't really read in the content of the page. This can happen
3749 ** (for example) when the page is being moved to the freelist. But
3750 ** now we are (perhaps) moving the page off of the freelist for
3751 ** reuse and we need to know its original content so that content
3752 ** can be stored in the rollback journal. So do the read at this
3753 ** time.
3754 */
drhd33d5a82007-04-26 12:11:28 +00003755 rc = pager_get_content(pPg);
3756 if( rc ){
3757 return rc;
drh538f5702007-04-13 02:14:30 +00003758 }
3759
drh6446c4d2001-12-15 14:22:18 +00003760 /* Mark the page as dirty. If the page has already been written
3761 ** to the journal then we can return right away.
3762 */
drh605ad8f2006-05-03 23:34:05 +00003763 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00003764 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00003765 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003766 }else{
drh6446c4d2001-12-15 14:22:18 +00003767
danielk1977a0bf2652004-11-04 14:30:04 +00003768 /* If we get this far, it means that the page needs to be
3769 ** written to the transaction journal or the ckeckpoint journal
3770 ** or both.
3771 **
3772 ** First check to see that the transaction journal exists and
3773 ** create it if it does not.
3774 */
3775 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003776 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003777 if( rc!=SQLITE_OK ){
3778 return rc;
3779 }
3780 assert( pPager->state>=PAGER_RESERVED );
3781 if( !pPager->journalOpen && pPager->useJournal ){
3782 rc = pager_open_journal(pPager);
3783 if( rc!=SQLITE_OK ) return rc;
3784 }
3785 assert( pPager->journalOpen || !pPager->useJournal );
3786 pPager->dirtyCache = 1;
3787
3788 /* The transaction journal now exists and we have a RESERVED or an
3789 ** EXCLUSIVE lock on the main database file. Write the current page to
3790 ** the transaction journal if it is not there already.
3791 */
3792 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
3793 if( (int)pPg->pgno <= pPager->origDbSize ){
3794 int szPg;
danielk1977a0bf2652004-11-04 14:30:04 +00003795 if( MEMDB ){
3796 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00003797 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00003798 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00003799 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003800 if( pHist->pOrig ){
3801 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
3802 }
3803 }else{
drhc001c582006-03-06 18:23:16 +00003804 u32 cksum, saved;
3805 char *pData2, *pEnd;
danielk1977dd97a492007-08-22 18:54:32 +00003806
drh267cb322005-09-16 17:16:52 +00003807 /* We should never write to the journal file the page that
3808 ** contains the database locks. The following assert verifies
3809 ** that we do not. */
3810 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00003811 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00003812 cksum = pager_cksum(pPager, (u8*)pData2);
drhc001c582006-03-06 18:23:16 +00003813 pEnd = pData2 + pPager->pageSize;
3814 pData2 -= 4;
3815 saved = *(u32*)pEnd;
3816 put32bits(pEnd, cksum);
danielk1977a0bf2652004-11-04 14:30:04 +00003817 szPg = pPager->pageSize+8;
drhc001c582006-03-06 18:23:16 +00003818 put32bits(pData2, pPg->pgno);
danielk197762079062007-08-15 17:08:46 +00003819 rc = sqlite3OsWrite(pPager->jfd, pData2, szPg, pPager->journalOff);
drhb0603412007-02-28 04:47:26 +00003820 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
drh538f5702007-04-13 02:14:30 +00003821 pPager->journalOff, szPg));
3822 PAGER_INCR(sqlite3_pager_writej_count);
danielk1977a0bf2652004-11-04 14:30:04 +00003823 pPager->journalOff += szPg;
drh477731b2007-06-16 03:06:27 +00003824 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
3825 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
drhc001c582006-03-06 18:23:16 +00003826 *(u32*)pEnd = saved;
danielk197707cb5602006-01-20 10:55:05 +00003827
drhfd131da2007-08-07 17:13:03 +00003828 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00003829 ** transaction will be rolled back by the layer above.
3830 */
danielk1977a0bf2652004-11-04 14:30:04 +00003831 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00003832 return rc;
3833 }
danielk197707cb5602006-01-20 10:55:05 +00003834
danielk1977a0bf2652004-11-04 14:30:04 +00003835 pPager->nRec++;
3836 assert( pPager->aInJournal!=0 );
3837 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
3838 pPg->needSync = !pPager->noSync;
3839 if( pPager->stmtInUse ){
3840 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
danielk1977a0bf2652004-11-04 14:30:04 +00003841 }
drhac69b052004-05-12 13:30:07 +00003842 }
danielk197713adf8a2004-06-03 16:08:41 +00003843 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00003844 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00003845 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00003846 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00003847 }
3848 if( pPg->needSync ){
3849 pPager->needSync = 1;
3850 }
3851 pPg->inJournal = 1;
3852 }
3853
3854 /* If the statement journal is open and the page is not in it,
3855 ** then write the current page to the statement journal. Note that
3856 ** the statement journal format differs from the standard journal format
3857 ** in that it omits the checksums and the header.
3858 */
danielk1977f35843b2007-04-07 15:03:17 +00003859 if( pPager->stmtInUse
3860 && !pageInStatement(pPg)
3861 && (int)pPg->pgno<=pPager->stmtSize
3862 ){
danielk1977a0bf2652004-11-04 14:30:04 +00003863 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
3864 if( MEMDB ){
3865 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
3866 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00003867 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003868 if( pHist->pStmt ){
3869 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
3870 }
drh4f0c5872007-03-26 22:05:01 +00003871 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00003872 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00003873 }else{
danielk197762079062007-08-15 17:08:46 +00003874 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhc001c582006-03-06 18:23:16 +00003875 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4;
3876 put32bits(pData2, pPg->pgno);
danielk197762079062007-08-15 17:08:46 +00003877 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4, offset);
drh4f0c5872007-03-26 22:05:01 +00003878 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00003879 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00003880 return rc;
3881 }
danielk1977a0bf2652004-11-04 14:30:04 +00003882 pPager->stmtNRec++;
3883 assert( pPager->aInStmt!=0 );
3884 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhdb48ee02003-01-16 13:42:43 +00003885 }
drhd9b02572001-04-15 00:37:09 +00003886 }
drhfa86c412002-02-02 15:01:15 +00003887 }
3888
3889 /* Update the database size and return.
3890 */
drh1aa2d8b2007-01-03 15:34:29 +00003891 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00003892 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003893 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00003894 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00003895 pPager->dbSize++;
3896 }
drh306dc212001-05-21 13:45:10 +00003897 }
drh69688d52001-04-14 16:38:23 +00003898 return rc;
drhed7c8552001-04-11 14:29:21 +00003899}
3900
3901/*
danielk19774099f6e2007-03-19 11:25:20 +00003902** This function is used to mark a data-page as writable. It uses
3903** pager_write() to open a journal file (if it is not already open)
3904** and write the page *pData to the journal.
3905**
3906** The difference between this function and pager_write() is that this
3907** function also deals with the special case where 2 or more pages
3908** fit on a single disk sector. In this case all co-resident pages
3909** must have been written to the journal file before returning.
3910*/
danielk19773b8a05f2007-03-19 17:44:26 +00003911int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003912 int rc = SQLITE_OK;
3913
danielk19773b8a05f2007-03-19 17:44:26 +00003914 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003915 Pager *pPager = pPg->pPager;
3916 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3917
drh86f8c192007-08-22 00:39:19 +00003918 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003919 if( !MEMDB && nPagePerSector>1 ){
3920 Pgno nPageCount; /* Total number of pages in database file */
3921 Pgno pg1; /* First page of the sector pPg is located on. */
3922 int nPage; /* Number of pages starting at pg1 to journal */
3923 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003924 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003925
3926 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3927 ** header to be written between the pages journaled by this function.
3928 */
3929 assert( pPager->doNotSync==0 );
3930 pPager->doNotSync = 1;
3931
3932 /* This trick assumes that both the page-size and sector-size are
3933 ** an integer power of 2. It sets variable pg1 to the identifier
3934 ** of the first page of the sector pPg is located on.
3935 */
3936 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3937
danielk19773b8a05f2007-03-19 17:44:26 +00003938 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003939 if( pPg->pgno>nPageCount ){
3940 nPage = (pPg->pgno - pg1)+1;
3941 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3942 nPage = nPageCount+1-pg1;
3943 }else{
3944 nPage = nPagePerSector;
3945 }
3946 assert(nPage>0);
3947 assert(pg1<=pPg->pgno);
3948 assert((pg1+nPage)>pPg->pgno);
3949
3950 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3951 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003952 PgHdr *pPage;
danielk19774099f6e2007-03-19 11:25:20 +00003953 if( !pPager->aInJournal || pg==pPg->pgno ||
3954 pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
3955 ) {
3956 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003957 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003958 if( rc==SQLITE_OK ){
3959 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00003960 if( pPage->needSync ){
3961 needSync = 1;
3962 }
danielk19773b8a05f2007-03-19 17:44:26 +00003963 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003964 }
3965 }
danielk1977dd97a492007-08-22 18:54:32 +00003966 }else if( (pPage = pager_lookup(pPager, pg)) ){
3967 if( pPage->needSync ){
3968 needSync = 1;
3969 }
danielk19774099f6e2007-03-19 11:25:20 +00003970 }
3971 }
3972
danielk1977dd97a492007-08-22 18:54:32 +00003973 /* If the PgHdr.needSync flag is set for any of the nPage pages
3974 ** starting at pg1, then it needs to be set for all of them. Because
3975 ** writing to any of these nPage pages may damage the others, the
3976 ** journal file must contain sync()ed copies of all of them
3977 ** before any of them can be written out to the database file.
3978 */
3979 if( needSync ){
3980 for(ii=0; ii<nPage && needSync; ii++){
3981 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
3982 if( pPage ) pPage->needSync = 1;
3983 }
3984 assert(pPager->needSync);
3985 }
3986
danielk19774099f6e2007-03-19 11:25:20 +00003987 assert( pPager->doNotSync==1 );
3988 pPager->doNotSync = 0;
3989 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003990 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003991 }
drh86f8c192007-08-22 00:39:19 +00003992 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003993 return rc;
3994}
3995
3996/*
drhaacc5432002-01-06 17:07:40 +00003997** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003998** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003999** to change the content of the page.
4000*/
danielk19777d3a6662006-01-23 16:21:05 +00004001#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004002int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004003 return pPg->dirty;
4004}
danielk19777d3a6662006-01-23 16:21:05 +00004005#endif
drh6019e162001-07-02 17:51:45 +00004006
danielk1977b84f96f2005-01-20 11:32:23 +00004007#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00004008/*
drh001bbcb2003-03-19 03:14:00 +00004009** Replace the content of a single page with the information in the third
4010** argument.
4011*/
danielk19773b8a05f2007-03-19 17:44:26 +00004012int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
4013 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00004014 int rc;
4015
drh86f8c192007-08-22 00:39:19 +00004016 pagerEnter(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00004017 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00004018 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004019 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00004020 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004021 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00004022 }
danielk19773b8a05f2007-03-19 17:44:26 +00004023 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00004024 }
drh86f8c192007-08-22 00:39:19 +00004025 pagerLeave(pPager);
drh001bbcb2003-03-19 03:14:00 +00004026 return rc;
4027}
danielk1977b84f96f2005-01-20 11:32:23 +00004028#endif
drh001bbcb2003-03-19 03:14:00 +00004029
4030/*
drh30e58752002-03-02 20:41:57 +00004031** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004032** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004033** that page might be marked as dirty.
4034**
4035** The overlying software layer calls this routine when all of the data
4036** on the given page is unused. The pager marks the page as clean so
4037** that it does not get written to disk.
4038**
4039** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004040** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004041** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004042**
4043** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004044** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004045** will thereafter be ignored. This is necessary to avoid a problem
4046** where a page with data is added to the freelist during one part of
4047** a transaction then removed from the freelist during a later part
4048** of the same transaction and reused for some other purpose. When it
4049** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004050** the sqlite3PagerDontRollback() routine is called. But because the
4051** page contains critical data, we still need to be sure it gets
4052** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004053*/
drh538f5702007-04-13 02:14:30 +00004054void sqlite3PagerDontWrite(DbPage *pDbPage){
4055 PgHdr *pPg = pDbPage;
4056 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004057
drhb7f91642004-10-31 02:22:47 +00004058 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004059 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004060 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004061 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004062 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004063 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4064 /* If this pages is the last page in the file and the file has grown
4065 ** during the current transaction, then do NOT mark the page as clean.
4066 ** When the database file grows, we must make sure that the last page
4067 ** gets written at least once so that the disk file will be the correct
4068 ** size. If you do not write this page and the size of the file
4069 ** on the disk ends up being too small, that can lead to database
4070 ** corruption during the next transaction.
4071 */
4072 }else{
drh538f5702007-04-13 02:14:30 +00004073 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4074 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004075 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004076#ifdef SQLITE_CHECK_PAGES
4077 pPg->pageHash = pager_pagehash(pPg);
4078#endif
drh8124a302002-06-25 14:43:57 +00004079 }
drh30e58752002-03-02 20:41:57 +00004080 }
drh86f8c192007-08-22 00:39:19 +00004081 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004082}
4083
4084/*
4085** A call to this routine tells the pager that if a rollback occurs,
4086** it is not necessary to restore the data on the given page. This
4087** means that the pager does not have to record the given page in the
4088** rollback journal.
drh538f5702007-04-13 02:14:30 +00004089**
4090** If we have not yet actually read the content of this page (if
4091** the PgHdr.needRead flag is set) then this routine acts as a promise
4092** that we will never need to read the page content in the future.
4093** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00004094*/
danielk19773b8a05f2007-03-19 17:44:26 +00004095void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004096 Pager *pPager = pPg->pPager;
4097
drh86f8c192007-08-22 00:39:19 +00004098 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004099 assert( pPager->state>=PAGER_RESERVED );
4100 if( pPager->journalOpen==0 ) return;
drhb7f91642004-10-31 02:22:47 +00004101 if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
drh30e58752002-03-02 20:41:57 +00004102 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
4103 assert( pPager->aInJournal!=0 );
4104 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4105 pPg->inJournal = 1;
drh538f5702007-04-13 02:14:30 +00004106 pPg->needRead = 0;
drhac69b052004-05-12 13:30:07 +00004107 if( pPager->stmtInUse ){
4108 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00004109 }
drh4f0c5872007-03-26 22:05:01 +00004110 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00004111 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00004112 }
danielk1977f35843b2007-04-07 15:03:17 +00004113 if( pPager->stmtInUse
4114 && !pageInStatement(pPg)
4115 && (int)pPg->pgno<=pPager->stmtSize
4116 ){
drh30e58752002-03-02 20:41:57 +00004117 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00004118 assert( pPager->aInStmt!=0 );
4119 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00004120 }
drh86f8c192007-08-22 00:39:19 +00004121 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004122}
4123
drhac69b052004-05-12 13:30:07 +00004124
drh30e58752002-03-02 20:41:57 +00004125/*
drh80e35f42007-03-30 14:06:34 +00004126** This routine is called to increment the database file change-counter,
4127** stored at byte 24 of the pager file.
4128*/
danielk1977c7b60172007-08-22 11:22:03 +00004129static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004130 PgHdr *pPgHdr;
4131 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004132 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004133
4134 if( !pPager->changeCountDone ){
4135 /* Open page 1 of the file for writing. */
4136 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4137 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004138
4139 if( !isDirect ){
4140 rc = sqlite3PagerWrite(pPgHdr);
4141 if( rc!=SQLITE_OK ) return rc;
4142 }
4143
drh80e35f42007-03-30 14:06:34 +00004144 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004145 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004146 change_counter++;
4147 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004148
4149 if( isDirect && pPager->fd->pMethods ){
4150 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4151 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4152 }
4153
drh80e35f42007-03-30 14:06:34 +00004154 /* Release the page reference. */
4155 sqlite3PagerUnref(pPgHdr);
4156 pPager->changeCountDone = 1;
4157 }
danielk1977c7b60172007-08-22 11:22:03 +00004158 return rc;
drh80e35f42007-03-30 14:06:34 +00004159}
4160
4161/*
4162** Sync the database file for the pager pPager. zMaster points to the name
4163** of a master journal file that should be written into the individual
4164** journal file. zMaster may be NULL, which is interpreted as no master
4165** journal (a single database transaction).
4166**
4167** This routine ensures that the journal is synced, all dirty pages written
4168** to the database file and the database file synced. The only thing that
4169** remains to commit the transaction is to delete the journal file (or
4170** master journal file if specified).
4171**
4172** Note that if zMaster==NULL, this does not overwrite a previous value
4173** passed to an sqlite3PagerCommitPhaseOne() call.
4174**
4175** If parameter nTrunc is non-zero, then the pager file is truncated to
4176** nTrunc pages (this is used by auto-vacuum databases).
4177*/
4178int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
4179 int rc = SQLITE_OK;
4180
4181 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4182 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004183 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004184
4185 /* If this is an in-memory db, or no pages have been written to, or this
4186 ** function has already been called, it is a no-op.
4187 */
4188 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4189 PgHdr *pPg;
4190 assert( pPager->journalOpen );
4191
danielk1977c7b60172007-08-22 11:22:03 +00004192#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4193 /* The atomic-write optimization can be used if all of the
4194 ** following are true:
4195 **
4196 ** + The file-system supports the atomic-write property for
4197 ** blocks of size page-size, and
4198 ** + This commit is not part of a multi-file transaction, and
4199 ** + Exactly one page has been modified and store in the journal file.
4200 **
4201 ** If the optimization can be used, then the journal file will never
4202 ** be created for this transaction.
4203 */
4204 if( !zMaster && pPager->journalOff==jrnlBufferSize(pPager) && nTrunc==0
4205 && (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4206 ){
4207 /* Update the nRec field in the journal file. */
4208 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4209 assert(pPager->nRec==1);
4210 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4211
4212 /* Update the db file change counter. The following call will modify
4213 ** the in-memory representation of page 1 to include the updated
4214 ** change counter and then write page 1 directly to the database
4215 ** file. Because of the atomic-write property of the host file-system,
4216 ** this is safe.
4217 */
4218 rc = pager_incr_changecounter(pPager, 1);
4219 }else
4220#endif
4221
drh80e35f42007-03-30 14:06:34 +00004222 /* If a master journal file name has already been written to the
4223 ** journal file, then no sync is required. This happens when it is
4224 ** written, then the process fails to upgrade from a RESERVED to an
4225 ** EXCLUSIVE lock. The next time the process tries to commit the
4226 ** transaction the m-j name will have already been written.
4227 */
4228 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00004229 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004230 if( rc!=SQLITE_OK ) goto sync_exit;
4231#ifndef SQLITE_OMIT_AUTOVACUUM
4232 if( nTrunc!=0 ){
4233 /* If this transaction has made the database smaller, then all pages
4234 ** being discarded by the truncation must be written to the journal
4235 ** file.
4236 */
4237 Pgno i;
4238 int iSkip = PAGER_MJ_PGNO(pPager);
4239 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
4240 if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){
4241 rc = sqlite3PagerGet(pPager, i, &pPg);
4242 if( rc!=SQLITE_OK ) goto sync_exit;
4243 rc = sqlite3PagerWrite(pPg);
4244 sqlite3PagerUnref(pPg);
4245 if( rc!=SQLITE_OK ) goto sync_exit;
4246 }
4247 }
4248 }
4249#endif
4250 rc = writeMasterJournal(pPager, zMaster);
4251 if( rc!=SQLITE_OK ) goto sync_exit;
4252 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004253 }
danielk1977c7b60172007-08-22 11:22:03 +00004254 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004255
4256#ifndef SQLITE_OMIT_AUTOVACUUM
4257 if( nTrunc!=0 ){
4258 rc = sqlite3PagerTruncate(pPager, nTrunc);
4259 if( rc!=SQLITE_OK ) goto sync_exit;
4260 }
4261#endif
4262
4263 /* Write all dirty pages to the database file */
4264 pPg = pager_get_all_dirty_pages(pPager);
4265 rc = pager_write_pagelist(pPg);
4266 if( rc!=SQLITE_OK ) goto sync_exit;
drh3cdd7d32007-03-30 14:46:01 +00004267 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004268
4269 /* Sync the database file. */
4270 if( !pPager->noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004271 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004272 }
4273 IOTRACE(("DBSYNC %p\n", pPager))
4274
4275 pPager->state = PAGER_SYNCED;
4276 }else if( MEMDB && nTrunc!=0 ){
4277 rc = sqlite3PagerTruncate(pPager, nTrunc);
4278 }
4279
4280sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004281 if( rc==SQLITE_IOERR_BLOCKED ){
4282 /* pager_incr_changecounter() may attempt to obtain an exclusive
4283 * lock to spill the cache and return IOERR_BLOCKED. But since
4284 * there is no chance the cache is inconsistent, it's
4285 * better to return SQLITE_BUSY.
4286 */
4287 rc = SQLITE_BUSY;
4288 }
drh86f8c192007-08-22 00:39:19 +00004289 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004290 return rc;
4291}
4292
4293
4294/*
drhed7c8552001-04-11 14:29:21 +00004295** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004296**
4297** If the commit fails for any reason, a rollback attempt is made
4298** and an error code is returned. If the commit worked, SQLITE_OK
4299** is returned.
drhed7c8552001-04-11 14:29:21 +00004300*/
drh80e35f42007-03-30 14:06:34 +00004301int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004302 int rc;
drhed7c8552001-04-11 14:29:21 +00004303 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004304
danielk1977efaaf572006-01-16 11:29:19 +00004305 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004306 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004307 }
drha6abd042004-06-09 17:37:22 +00004308 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004309 return SQLITE_ERROR;
4310 }
drh86f8c192007-08-22 00:39:19 +00004311 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004312 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004313 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004314 pPg = pager_get_all_dirty_pages(pPager);
4315 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004316 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4317 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004318 pPg->dirty = 0;
4319 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004320 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004321 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004322 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004323 pPg = pPg->pDirty;
4324 }
drh605ad8f2006-05-03 23:34:05 +00004325 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004326#ifndef NDEBUG
4327 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4328 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4329 assert( !pPg->alwaysRollback );
4330 assert( !pHist->pOrig );
4331 assert( !pHist->pStmt );
4332 }
4333#endif
drhac69b052004-05-12 13:30:07 +00004334 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004335 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004336 return SQLITE_OK;
4337 }
drh3cdd7d32007-03-30 14:46:01 +00004338 assert( pPager->journalOpen || !pPager->dirtyCache );
4339 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4340 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004341 rc = pager_error(pPager, rc);
4342 pagerLeave(pPager);
4343 return rc;
drhed7c8552001-04-11 14:29:21 +00004344}
4345
4346/*
drha6abd042004-06-09 17:37:22 +00004347** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004348** All in-memory cache pages revert to their original data contents.
4349** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004350**
4351** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004352** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004353** process is writing trash into the journal file (SQLITE_CORRUPT) or
4354** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4355** codes are returned for all these occasions. Otherwise,
4356** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004357*/
danielk19773b8a05f2007-03-19 17:44:26 +00004358int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004359 int rc;
drh4f0c5872007-03-26 22:05:01 +00004360 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004361 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004362 PgHdr *p;
4363 for(p=pPager->pAll; p; p=p->pNextAll){
4364 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004365 assert( !p->alwaysRollback );
4366 if( !p->dirty ){
4367 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4368 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4369 continue;
4370 }
4371
drhac69b052004-05-12 13:30:07 +00004372 pHist = PGHDR_TO_HIST(p, pPager);
4373 if( pHist->pOrig ){
4374 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004375 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004376 }else{
drh4f0c5872007-03-26 22:05:01 +00004377 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004378 }
4379 clearHistory(pHist);
4380 p->dirty = 0;
4381 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004382 pHist->inStmt = 0;
4383 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004384 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004385 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004386 }
drhac69b052004-05-12 13:30:07 +00004387 }
drh605ad8f2006-05-03 23:34:05 +00004388 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004389 pPager->pStmt = 0;
4390 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004391 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004392 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004393 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004394 return SQLITE_OK;
4395 }
4396
drh86f8c192007-08-22 00:39:19 +00004397 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004398 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004399 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004400 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004401 return rc;
4402 }
drhdb48ee02003-01-16 13:42:43 +00004403
danielk1977efaaf572006-01-16 11:29:19 +00004404 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004405 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004406 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004407 }
drh86f8c192007-08-22 00:39:19 +00004408 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004409 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004410 }
drha6abd042004-06-09 17:37:22 +00004411 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004412 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004413 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004414 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004415 if( rc==SQLITE_OK ){
4416 rc = rc2;
4417 }
4418 }else{
danielk1977e277be02007-03-23 18:12:06 +00004419 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004420 }
danielk1977e180dd92007-04-05 17:15:52 +00004421 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004422 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004423
4424 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4425 ** cache. So call pager_error() on the way out to make any error
4426 ** persistent.
4427 */
drh86f8c192007-08-22 00:39:19 +00004428 rc = pager_error(pPager, rc);
4429 pagerLeave(pPager);
4430 return rc;
drh98808ba2001-10-18 12:34:46 +00004431}
drhd9b02572001-04-15 00:37:09 +00004432
4433/*
drh5e00f6c2001-09-13 13:46:56 +00004434** Return TRUE if the database file is opened read-only. Return FALSE
4435** if the database is (in theory) writable.
4436*/
danielk19773b8a05f2007-03-19 17:44:26 +00004437int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004438 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004439}
4440
4441/*
drh0f7eb612006-08-08 13:51:43 +00004442** Return the number of references to the pager.
4443*/
danielk19773b8a05f2007-03-19 17:44:26 +00004444int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004445 return pPager->nRef;
4446}
4447
4448#ifdef SQLITE_TEST
4449/*
drhd9b02572001-04-15 00:37:09 +00004450** This routine is used for testing and analysis only.
4451*/
danielk19773b8a05f2007-03-19 17:44:26 +00004452int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004453 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004454 a[0] = pPager->nRef;
4455 a[1] = pPager->nPage;
4456 a[2] = pPager->mxPage;
4457 a[3] = pPager->dbSize;
4458 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004459 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004460 a[6] = pPager->nHit;
4461 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004462 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004463 a[9] = pPager->nRead;
4464 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004465 return a;
4466}
drh0f7eb612006-08-08 13:51:43 +00004467#endif
drhdd793422001-06-28 01:54:48 +00004468
drhfa86c412002-02-02 15:01:15 +00004469/*
drhac69b052004-05-12 13:30:07 +00004470** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004471**
4472** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004473** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004474** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004475*/
drh86f8c192007-08-22 00:39:19 +00004476static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004477 int rc;
drhac69b052004-05-12 13:30:07 +00004478 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004479 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004480 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004481 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004482 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004483 pPager->stmtInUse = 1;
4484 pPager->stmtSize = pPager->dbSize;
4485 return SQLITE_OK;
4486 }
drhda47d772002-12-02 04:25:19 +00004487 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004488 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004489 return SQLITE_OK;
4490 }
drhfa86c412002-02-02 15:01:15 +00004491 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004492 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00004493 pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00004494 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004495 if( pPager->aInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004496 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004497 return SQLITE_NOMEM;
4498 }
drh968af522003-02-11 14:55:40 +00004499#ifndef NDEBUG
drh054889e2005-11-30 03:20:31 +00004500 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
drhac69b052004-05-12 13:30:07 +00004501 if( rc ) goto stmt_begin_failed;
danielk197776572402004-06-25 02:38:54 +00004502 assert( pPager->stmtJSize == pPager->journalOff );
drh968af522003-02-11 14:55:40 +00004503#endif
danielk197776572402004-06-25 02:38:54 +00004504 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004505 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004506 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004507 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004508 if( !pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00004509 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, 0);
drhac69b052004-05-12 13:30:07 +00004510 if( rc ) goto stmt_begin_failed;
4511 pPager->stmtOpen = 1;
4512 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004513 }
drhac69b052004-05-12 13:30:07 +00004514 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004515 return SQLITE_OK;
4516
drhac69b052004-05-12 13:30:07 +00004517stmt_begin_failed:
4518 if( pPager->aInStmt ){
drh17435752007-08-16 04:30:38 +00004519 sqlite3_free(pPager->aInStmt);
drhac69b052004-05-12 13:30:07 +00004520 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004521 }
4522 return rc;
4523}
drh86f8c192007-08-22 00:39:19 +00004524int sqlite3PagerStmtBegin(Pager *pPager){
4525 int rc;
4526 pagerEnter(pPager);
4527 rc = pagerStmtBegin(pPager);
4528 pagerLeave(pPager);
4529 return rc;
4530}
drhfa86c412002-02-02 15:01:15 +00004531
4532/*
drhac69b052004-05-12 13:30:07 +00004533** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004534*/
danielk19773b8a05f2007-03-19 17:44:26 +00004535int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004536 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004537 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004538 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004539 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004540 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004541 /* sqlite3OsTruncate(pPager->stfd, 0); */
drh17435752007-08-16 04:30:38 +00004542 sqlite3_free( pPager->aInStmt );
drhac69b052004-05-12 13:30:07 +00004543 pPager->aInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004544 }else{
4545 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004546 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004547 pNext = pHist->pNextStmt;
4548 assert( pHist->inStmt );
4549 pHist->inStmt = 0;
4550 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004551 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004552 pHist->pStmt = 0;
4553 }
4554 }
4555 pPager->stmtNRec = 0;
4556 pPager->stmtInUse = 0;
4557 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004558 }
drhac69b052004-05-12 13:30:07 +00004559 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004560 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004561 return SQLITE_OK;
4562}
4563
4564/*
drhac69b052004-05-12 13:30:07 +00004565** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004566*/
danielk19773b8a05f2007-03-19 17:44:26 +00004567int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004568 int rc;
drh86f8c192007-08-22 00:39:19 +00004569 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004570 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004571 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004572 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004573 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004574 PgHistory *pHist;
4575 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4576 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004577 if( pHist->pStmt ){
4578 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004579 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004580 pHist->pStmt = 0;
4581 }
4582 }
4583 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004584 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004585 rc = SQLITE_OK;
4586 }else{
4587 rc = pager_stmt_playback(pPager);
4588 }
danielk19773b8a05f2007-03-19 17:44:26 +00004589 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004590 }else{
4591 rc = SQLITE_OK;
4592 }
drhac69b052004-05-12 13:30:07 +00004593 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004594 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004595 return rc;
4596}
4597
drh73509ee2003-04-06 20:44:45 +00004598/*
4599** Return the full pathname of the database file.
4600*/
danielk19773b8a05f2007-03-19 17:44:26 +00004601const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004602 return pPager->zFilename;
4603}
4604
drhb20ea9d2004-02-09 01:20:36 +00004605/*
danielk19775865e3d2004-06-14 06:03:57 +00004606** Return the directory of the database file.
4607*/
danielk19773b8a05f2007-03-19 17:44:26 +00004608const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004609 return pPager->zDirectory;
4610}
4611
4612/*
4613** Return the full pathname of the journal file.
4614*/
danielk19773b8a05f2007-03-19 17:44:26 +00004615const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004616 return pPager->zJournal;
4617}
4618
4619/*
drh2c8997b2005-08-27 16:36:48 +00004620** Return true if fsync() calls are disabled for this pager. Return FALSE
4621** if fsync()s are executed normally.
4622*/
danielk19773b8a05f2007-03-19 17:44:26 +00004623int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004624 return pPager->noSync;
4625}
4626
drh7c4ac0c2007-04-05 11:25:58 +00004627#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004628/*
drhb20ea9d2004-02-09 01:20:36 +00004629** Set the codec for this pager
4630*/
danielk19773b8a05f2007-03-19 17:44:26 +00004631void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004632 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004633 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004634 void *pCodecArg
4635){
4636 pPager->xCodec = xCodec;
4637 pPager->pCodecArg = pCodecArg;
4638}
drh7c4ac0c2007-04-05 11:25:58 +00004639#endif
drhb20ea9d2004-02-09 01:20:36 +00004640
danielk1977687566d2004-11-02 12:56:41 +00004641#ifndef SQLITE_OMIT_AUTOVACUUM
4642/*
drh5e385312007-06-16 04:42:12 +00004643** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004644**
drh5e385312007-06-16 04:42:12 +00004645** There must be no references to the page previously located at
4646** pgno (which we call pPgOld) though that page is allowed to be
4647** in cache. If the page previous located at pgno is not already
4648** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004649**
drh5e385312007-06-16 04:42:12 +00004650** References to the page pPg remain valid. Updating any
4651** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004652** allocated along with the page) is the responsibility of the caller.
4653**
danielk19775fd057a2005-03-09 13:09:43 +00004654** A transaction must be active when this routine is called. It used to be
4655** required that a statement transaction was not active, but this restriction
4656** has been removed (CREATE INDEX needs to move a page when a statement
4657** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00004658*/
danielk19773b8a05f2007-03-19 17:44:26 +00004659int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00004660 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00004661 int h;
danielk197794daf7f2004-11-08 09:26:09 +00004662 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004663
drh86f8c192007-08-22 00:39:19 +00004664 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004665 assert( pPg->nRef>0 );
4666
drh4f0c5872007-03-26 22:05:01 +00004667 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00004668 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00004669 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004670
danielk1977b4626a32007-04-28 15:47:43 +00004671 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00004672 if( pPg->needSync ){
4673 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00004674 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00004675 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00004676 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004677 }
4678
danielk1977687566d2004-11-02 12:56:41 +00004679 /* Unlink pPg from it's hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00004680 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00004681
danielk1977ef73ee92004-11-06 12:26:07 +00004682 /* If the cache contains a page with page-number pgno, remove it
danielk1977599fcba2004-11-08 07:13:13 +00004683 ** from it's hash chain. Also, if the PgHdr.needSync was set for
4684 ** page pgno before the 'move' operation, it needs to be retained
4685 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004686 */
drh5e385312007-06-16 04:42:12 +00004687 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00004688 pPgOld = pager_lookup(pPager, pgno);
4689 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00004690 assert( pPgOld->nRef==0 );
4691 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00004692 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00004693 pPg->needSync = pPgOld->needSync;
4694 }else{
4695 pPg->needSync = 0;
4696 }
4697 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
4698 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drh5e385312007-06-16 04:42:12 +00004699 }else{
4700 pPg->inJournal = 0;
drh732c8172007-06-16 11:17:45 +00004701 assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize );
danielk1977687566d2004-11-02 12:56:41 +00004702 }
4703
danielk1977f5fdda82004-11-03 08:44:05 +00004704 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00004705 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00004706 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00004707 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00004708 if( pPager->aHash[h] ){
4709 assert( pPager->aHash[h]->pPrevHash==0 );
4710 pPager->aHash[h]->pPrevHash = pPg;
4711 }
4712 pPg->pNextHash = pPager->aHash[h];
4713 pPager->aHash[h] = pPg;
4714 pPg->pPrevHash = 0;
4715
drh605ad8f2006-05-03 23:34:05 +00004716 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004717 pPager->dirtyCache = 1;
4718
danielk197794daf7f2004-11-08 09:26:09 +00004719 if( needSyncPgno ){
4720 /* If needSyncPgno is non-zero, then the journal file needs to be
4721 ** sync()ed before any data is written to database file page needSyncPgno.
4722 ** Currently, no such page exists in the page-cache and the
4723 ** Pager.aInJournal bit has been set. This needs to be remedied by loading
4724 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00004725 **
danielk19773b8a05f2007-03-19 17:44:26 +00004726 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004727 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004728 */
4729 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004730 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004731 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004732 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004733 if( rc!=SQLITE_OK ) return rc;
danielk1977ae825582004-11-23 09:06:55 +00004734 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00004735 pPgHdr->needSync = 1;
4736 pPgHdr->inJournal = 1;
4737 makeDirty(pPgHdr);
4738 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004739 }
4740
drh86f8c192007-08-22 00:39:19 +00004741 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004742 return SQLITE_OK;
4743}
4744#endif
4745
danielk19773b8a05f2007-03-19 17:44:26 +00004746/*
4747** Return a pointer to the data for the specified page.
4748*/
4749void *sqlite3PagerGetData(DbPage *pPg){
4750 return PGHDR_TO_DATA(pPg);
4751}
4752
4753/*
4754** Return a pointer to the Pager.nExtra bytes of "extra" space
4755** allocated along with the specified page.
4756*/
4757void *sqlite3PagerGetExtra(DbPage *pPg){
4758 Pager *pPager = pPg->pPager;
4759 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
4760}
4761
danielk197741483462007-03-24 16:45:04 +00004762/*
4763** Get/set the locking-mode for this pager. Parameter eMode must be one
4764** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4765** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4766** the locking-mode is set to the value specified.
4767**
4768** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4769** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4770** locking-mode.
4771*/
4772int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004773 assert( eMode==PAGER_LOCKINGMODE_QUERY
4774 || eMode==PAGER_LOCKINGMODE_NORMAL
4775 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4776 assert( PAGER_LOCKINGMODE_QUERY<0 );
4777 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4778 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00004779 pPager->exclusiveMode = eMode;
4780 }
4781 return (int)pPager->exclusiveMode;
4782}
4783
dougcurrie81c95ef2004-06-18 23:21:47 +00004784#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00004785/*
4786** Return the current state of the file lock for the given pager.
4787** The return value is one of NO_LOCK, SHARED_LOCK, RESERVED_LOCK,
4788** PENDING_LOCK, or EXCLUSIVE_LOCK.
4789*/
danielk19773b8a05f2007-03-19 17:44:26 +00004790int sqlite3PagerLockstate(Pager *pPager){
drh054889e2005-11-30 03:20:31 +00004791 return sqlite3OsLockState(pPager->fd);
drh89ac8c12004-06-09 14:17:20 +00004792}
4793#endif
4794
danielk197793758c82005-01-21 08:13:14 +00004795#ifdef SQLITE_DEBUG
drhdd793422001-06-28 01:54:48 +00004796/*
4797** Print a listing of all referenced pages and their ref count.
4798*/
danielk19773b8a05f2007-03-19 17:44:26 +00004799void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00004800 PgHdr *pPg;
4801 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4802 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00004803 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
4804 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00004805 }
4806}
4807#endif
drh2e66f0b2005-04-28 17:18:48 +00004808
4809#endif /* SQLITE_OMIT_DISKIO */