blob: b261c7fc3e36ebfe639db9d30a9db865cd80a621 [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**
drhed138fb2007-08-22 22:04:37 +000021** @(#) $Id: pager.c,v 1.368 2007/08/22 22:04:37 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000026#include <string.h>
drhed7c8552001-04-11 14:29:21 +000027
28/*
drhdb48ee02003-01-16 13:42:43 +000029** Macros for troubleshooting. Normally turned off
30*/
danielk1977466be562004-06-10 02:16:01 +000031#if 0
drhd3627af2006-12-18 18:34:51 +000032#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000033#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
34#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
35#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
36#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
37#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000038#else
drh4f0c5872007-03-26 22:05:01 +000039#define PAGERTRACE1(X)
40#define PAGERTRACE2(X,Y)
41#define PAGERTRACE3(X,Y,Z)
42#define PAGERTRACE4(X,Y,Z,W)
43#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000044#endif
45
danielk1977599fcba2004-11-08 07:13:13 +000046/*
drh4f0c5872007-03-26 22:05:01 +000047** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000048** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000049**
50** PAGERID() takes a pointer to a Pager struct as it's argument. The
danielk197762079062007-08-15 17:08:46 +000051** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
danielk1977599fcba2004-11-08 07:13:13 +000052** struct as it's argument.
53*/
drhc001c582006-03-06 18:23:16 +000054#define PAGERID(p) ((int)(p->fd))
55#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000056
57/*
drhed7c8552001-04-11 14:29:21 +000058** The page cache as a whole is always in one of the following
59** states:
60**
drha6abd042004-06-09 17:37:22 +000061** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000062** writing the database file. There is no
63** data held in memory. This is the initial
64** state.
65**
drha6abd042004-06-09 17:37:22 +000066** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000067** Writing is not permitted. There can be
68** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000069** file at the same time.
drhed7c8552001-04-11 14:29:21 +000070**
drh726de592004-06-10 23:35:50 +000071** PAGER_RESERVED This process has reserved the database for writing
72** but has not yet made any changes. Only one process
73** at a time can reserve the database. The original
74** database file has not been modified so other
75** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000076** database file.
77**
78** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000079** Access is exclusive. No other processes or
80** threads can be reading or writing while one
81** process is writing.
82**
danielk1977aa5ccdf2004-06-14 05:10:42 +000083** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
84** after all dirty pages have been written to the
85** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000086** disk. All that remains to do is to remove or
87** truncate the journal file and the transaction
88** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000089**
drha6abd042004-06-09 17:37:22 +000090** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000091** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000092** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000093** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000094** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000095** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000096** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000097** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000098** PAGER_RESERVED means that there is an open rollback journal.
99** The transition to PAGER_EXCLUSIVE occurs before any changes
100** are made to the database file, though writes to the rollback
101** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
102** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
103** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000104*/
drha6abd042004-06-09 17:37:22 +0000105#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000106#define PAGER_SHARED 1 /* same as SHARED_LOCK */
107#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
108#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
109#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000110
drh684917c2004-10-05 02:41:42 +0000111/*
112** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
113** then failed attempts to get a reserved lock will invoke the busy callback.
114** This is off by default. To see why, consider the following scenario:
115**
116** Suppose thread A already has a shared lock and wants a reserved lock.
117** Thread B already has a reserved lock and wants an exclusive lock. If
118** both threads are using their busy callbacks, it might be a long time
119** be for one of the threads give up and allows the other to proceed.
120** But if the thread trying to get the reserved lock gives up quickly
121** (if it never invokes its busy callback) then the contention will be
122** resolved quickly.
123*/
124#ifndef SQLITE_BUSY_RESERVED_LOCK
125# define SQLITE_BUSY_RESERVED_LOCK 0
126#endif
drhd9b02572001-04-15 00:37:09 +0000127
drhed7c8552001-04-11 14:29:21 +0000128/*
drh887dc4c2004-10-22 16:22:57 +0000129** This macro rounds values up so that if the value is an address it
130** is guaranteed to be an address that is aligned to an 8-byte boundary.
131*/
132#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
133
134/*
drhed7c8552001-04-11 14:29:21 +0000135** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +0000136** This header is only visible to this pager module. The client
137** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +0000138**
danielk19773b8a05f2007-03-19 17:44:26 +0000139** Client code should call sqlite3PagerWrite() on a page prior to making
140** any modifications to that page. The first time sqlite3PagerWrite()
drhf6038712004-02-08 18:07:34 +0000141** is called, the original page contents are written into the rollback
142** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
143** the journal page has made it onto the disk surface, PgHdr.needSync
144** is cleared. The modified page cannot be written back into the original
145** database file until the journal pages has been synced to disk and the
146** PgHdr.needSync has been cleared.
147**
danielk19773b8a05f2007-03-19 17:44:26 +0000148** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
drhf6038712004-02-08 18:07:34 +0000149** is cleared again when the page content is written back to the original
150** database file.
drh732c8172007-06-16 11:17:45 +0000151**
152** Details of important structure elements:
153**
154** needSync
155**
156** If this is true, this means that it is not safe to write the page
157** content to the database because the original content needed
158** for rollback has not by synced to the main rollback journal.
159** The original content may have been written to the rollback journal
160** but it has not yet been synced. So we cannot write to the database
161** file because power failure might cause the page in the journal file
162** to never reach the disk. It is as if the write to the journal file
163** does not occur until the journal file is synced.
164**
165** This flag is false if the page content exactly matches what
166** currently exists in the database file. The needSync flag is also
167** false if the original content has been written to the main rollback
168** journal and synced. If the page represents a new page that has
169** been added onto the end of the database during the current
170** transaction, the needSync flag is true until the original database
171** size in the journal header has been synced to disk.
172**
173** inJournal
174**
175** This is true if the original page has been written into the main
176** rollback journal. This is always false for new pages added to
177** the end of the database file during the current transaction.
178** And this flag says nothing about whether or not the journal
179** has been synced to disk. For pages that are in the original
180** database file, the following expression should always be true:
181**
182** inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0
183**
184** The pPager->aInJournal[] array is only valid for the original
185** pages of the database, not new pages that are added to the end
186** of the database, so obviously the above expression cannot be
187** valid for new pages. For new pages inJournal is always 0.
188**
189** dirty
190**
191** When true, this means that the content of the page has been
192** modified and needs to be written back to the database file.
193** If false, it means that either the content of the page is
194** unchanged or else the content is unimportant and we do not
195** care whether or not it is preserved.
196**
197** alwaysRollback
198**
199** This means that the sqlite3PagerDontRollback() API should be
200** ignored for this page. The DontRollback() API attempts to say
201** that the content of the page on disk is unimportant (it is an
202** unused page on the freelist) so that it is unnecessary to
203** rollback changes to this page because the content of the page
204** can change without changing the meaning of the database. This
205** flag overrides any DontRollback() attempt. This flag is set
206** when a page that originally contained valid data is added to
207** the freelist. Later in the same transaction, this page might
208** be pulled from the freelist and reused for something different
209** and at that point the DontRollback() API will be called because
210** pages taken from the freelist do not need to be protected by
211** the rollback journal. But this flag says that the page was
212** not originally part of the freelist so that it still needs to
213** be rolled back in spite of any subsequent DontRollback() calls.
214**
215** needRead
216**
217** This flag means (when true) that the content of the page has
218** not yet been loaded from disk. The in-memory content is just
219** garbage. (Actually, we zero the content, but you should not
220** make any assumptions about the content nevertheless.) If the
221** content is needed in the future, it should be read from the
222** original database file.
drhed7c8552001-04-11 14:29:21 +0000223*/
drhd9b02572001-04-15 00:37:09 +0000224typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +0000225struct PgHdr {
226 Pager *pPager; /* The pager to which this page belongs */
227 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000228 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhd9b02572001-04-15 00:37:09 +0000229 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
drhac69b052004-05-12 13:30:07 +0000230 PgHdr *pNextAll; /* A list of all pages */
drh193a6b42002-07-07 16:52:46 +0000231 u8 inJournal; /* TRUE if has been written to journal */
drh193a6b42002-07-07 16:52:46 +0000232 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000233 u8 needSync; /* Sync journal before writing this page */
drh80e35f42007-03-30 14:06:34 +0000234 u8 alwaysRollback; /* Disable DontRollback() for this page */
drh538f5702007-04-13 02:14:30 +0000235 u8 needRead; /* Read content if PagerWrite() is called */
drhac69b052004-05-12 13:30:07 +0000236 short int nRef; /* Number of users of this page */
drhdc3e10b2006-06-15 14:31:06 +0000237 PgHdr *pDirty, *pPrevDirty; /* Dirty pages */
drh605ad8f2006-05-03 23:34:05 +0000238 u32 notUsed; /* Buffer space */
danielk19773c407372005-02-15 02:54:14 +0000239#ifdef SQLITE_CHECK_PAGES
240 u32 pageHash;
241#endif
drh1c7880e2005-05-20 20:01:55 +0000242 /* pPager->pageSize bytes of page data follow this header */
drh973b6e32003-02-12 14:09:42 +0000243 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +0000244};
245
drhac69b052004-05-12 13:30:07 +0000246/*
247** For an in-memory only database, some extra information is recorded about
248** each page so that changes can be rolled back. (Journal files are not
249** used for in-memory databases.) The following information is added to
250** the end of every EXTRA block for in-memory databases.
251**
252** This information could have been added directly to the PgHdr structure.
253** But then it would take up an extra 8 bytes of storage on every PgHdr
254** even for disk-based databases. Splitting it out saves 8 bytes. This
255** is only a savings of 0.8% but those percentages add up.
256*/
257typedef struct PgHistory PgHistory;
258struct PgHistory {
259 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
260 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000261 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
262 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000263};
drh9eb9e262004-02-11 02:18:05 +0000264
265/*
266** A macro used for invoking the codec if there is one
267*/
268#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000269# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
270# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000271#else
drhc001c582006-03-06 18:23:16 +0000272# define CODEC1(P,D,N,X) /* NO-OP */
273# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000274#endif
275
drhed7c8552001-04-11 14:29:21 +0000276/*
drh69688d52001-04-14 16:38:23 +0000277** Convert a pointer to a PgHdr into a pointer to its data
278** and back again.
drhed7c8552001-04-11 14:29:21 +0000279*/
280#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
281#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drh1c7880e2005-05-20 20:01:55 +0000282#define PGHDR_TO_EXTRA(G,P) ((void*)&((char*)(&(G)[1]))[(P)->pageSize])
drhac69b052004-05-12 13:30:07 +0000283#define PGHDR_TO_HIST(P,PGR) \
drh1c7880e2005-05-20 20:01:55 +0000284 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000285
286/*
drhed7c8552001-04-11 14:29:21 +0000287** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000288**
drh4f0ee682007-03-30 20:43:40 +0000289** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000290** or SQLITE_FULL. Once one of the first three errors occurs, it persists
291** and is returned as the result of every major pager API call. The
292** SQLITE_FULL return code is slightly different. It persists only until the
293** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000294** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000295** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000296*/
297struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000298 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000299 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000300 u8 journalStarted; /* True if header of journal is synced */
301 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000302 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000303 u8 stmtOpen; /* True if the statement subjournal is open */
304 u8 stmtInUse; /* True we are in a statement subtransaction */
305 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000306 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000307 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000308 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000309 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000310 u8 tempFile; /* zFilename is a temporary file */
311 u8 readOnly; /* True for a read-only database */
312 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000313 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000314 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000315 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000316 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000317 u8 doNotSync; /* Boolean. While true, do not spill the cache */
318 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
319 u8 changeCountDone; /* Set after incrementing the change-counter */
drhe49f9822006-09-15 12:29:16 +0000320 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000321 int dbSize; /* Number of pages in the file */
322 int origDbSize; /* dbSize before the current change */
323 int stmtSize; /* Size of database (in pages) at stmt_begin() */
324 int nRec; /* Number of pages written to the journal */
325 u32 cksumInit; /* Quasi-random value added to every checksum */
326 int stmtNRec; /* Number of records in stmt subjournal */
327 int nExtra; /* Add this many bytes to each in-memory page */
328 int pageSize; /* Number of bytes in a page */
329 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000330 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
331 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000332 Pgno mxPgno; /* Maximum allowed size of the database */
drh603240c2002-03-05 01:11:12 +0000333 u8 *aInJournal; /* One bit for each page in the database file */
drhac69b052004-05-12 13:30:07 +0000334 u8 *aInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000335 char *zFilename; /* Name of the database file */
336 char *zJournal; /* Name of the journal file */
337 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000338 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
339 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000340 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
drhed7c8552001-04-11 14:29:21 +0000341 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000342 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000343 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000344 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000345 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000346 i64 journalOff; /* Current byte offset in the journal file */
347 i64 journalHdr; /* Byte offset to previous journal header */
348 i64 stmtHdrOff; /* First journal header written this statement */
349 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000350 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000351 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000352#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000353 int nHit, nMiss; /* Cache hits and missing */
354 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000355#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000356 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
357 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000358#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000359 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000360 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000361#endif
drh8ca0c722006-05-07 17:49:38 +0000362 int nHash; /* Size of the pager hash table */
363 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000364#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +0000365 Pager *pNext; /* Doubly linked list of pagers on which */
366 Pager *pPrev; /* sqlite3_release_memory() will work */
367 int iInUseMM; /* Non-zero if unavailable to MM */
368 int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
danielk197713f72992005-12-18 08:51:22 +0000369#endif
danielk19778186df82007-03-06 13:45:59 +0000370 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000371 char dbFileVers[16]; /* Changes whenever database file changes */
drhd9b02572001-04-15 00:37:09 +0000372};
373
374/*
drh538f5702007-04-13 02:14:30 +0000375** The following global variables hold counters used for
376** testing purposes only. These variables do not exist in
377** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000378*/
379#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000380int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
381int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
382int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
383int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
384# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000385#else
drh538f5702007-04-13 02:14:30 +0000386# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000387#endif
388
drh86f8c192007-08-22 00:39:19 +0000389/*
390** The following variable points to the head of a double-linked list
391** of all pagers that are eligible for page stealing by the
392** sqlite3_release_memory() interface. Access to this list is
393** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
394*/
395#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
396static Pager *sqlite3PagerList = 0;
397#endif
drh538f5702007-04-13 02:14:30 +0000398
399
drhfcd35c72005-05-21 02:48:08 +0000400/*
drh5e00f6c2001-09-13 13:46:56 +0000401** Journal files begin with the following magic string. The data
402** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000403**
drhae2b40c2004-06-09 19:03:54 +0000404** Since version 2.8.0, the journal format contains additional sanity
405** checking information. If the power fails while the journal is begin
406** written, semi-random garbage data might appear in the journal
407** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000408** to roll the journal back, the database could be corrupted. The additional
409** sanity checking data is an attempt to discover the garbage in the
410** journal and ignore it.
411**
drhae2b40c2004-06-09 19:03:54 +0000412** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000413** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000414** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000415** This cksum is initialized to a 32-bit random value that appears in the
416** journal file right after the header. The random initializer is important,
417** because garbage data that appears at the end of a journal is likely
418** data that was once in other files that have now been deleted. If the
419** garbage data came from an obsolete journal file, the checksums might
420** be correct. But by initializing the checksum to random value which
421** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000422*/
drhae2b40c2004-06-09 19:03:54 +0000423static const unsigned char aJournalMagic[] = {
424 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000425};
426
427/*
drh726de592004-06-10 23:35:50 +0000428** The size of the header and of each page in the journal is determined
429** by the following macros.
drh968af522003-02-11 14:55:40 +0000430*/
drhae2b40c2004-06-09 19:03:54 +0000431#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000432
danielk197776572402004-06-25 02:38:54 +0000433/*
434** The journal header size for this pager. In the future, this could be
435** set to some value read from the disk controller. The important
436** characteristic is that it is the same size as a disk sector.
437*/
438#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
439
drhb7f91642004-10-31 02:22:47 +0000440/*
441** The macro MEMDB is true if we are dealing with an in-memory database.
442** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
443** the value of MEMDB will be a constant and the compiler will optimize
444** out code that would never execute.
445*/
446#ifdef SQLITE_OMIT_MEMORYDB
447# define MEMDB 0
448#else
449# define MEMDB pPager->memDb
450#endif
451
452/*
danielk197776572402004-06-25 02:38:54 +0000453** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
454** reserved for working around a windows/posix incompatibility). It is
455** used in the journal to signify that the remainder of the journal file
456** is devoted to storing a master journal name - there are no more pages to
457** roll back. See comments for function writeMasterJournal() for details.
458*/
danielk1977599fcba2004-11-08 07:13:13 +0000459/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
460#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000461
drh968af522003-02-11 14:55:40 +0000462/*
danielk197726836652005-01-17 01:33:13 +0000463** The maximum legal page number is (2^31 - 1).
464*/
465#define PAGER_MAX_PGNO 2147483647
466
467/*
drh86f8c192007-08-22 00:39:19 +0000468** The pagerEnter() and pagerLeave() routines acquire and release
469** a mutex on each pager. The mutex is recursive.
470**
471** This is a special-purpose mutex. It only provides mutual exclusion
472** between the Btree and the Memory Management sqlite3_release_memory()
473** function. It does not prevent, for example, two Btrees from accessing
474** the same pager at the same time. Other general-purpose mutexes in
475** the btree layer handle that chore.
476*/
477#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
478 static void pagerEnter(Pager *p){
479 p->iInUseDB++;
480 if( p->iInUseMM && p->iInUseDB==1 ){
481 sqlite3_mutex *mutex;
482 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
483 p->iInUseDB = 0;
484 sqlite3_mutex_enter(mutex);
485 p->iInUseDB = 1;
486 sqlite3_mutex_leave(mutex);
487 }
488 assert( p->iInUseMM==0 );
489 }
490 static void pagerLeave(Pager *p){
491 p->iInUseDB--;
492 assert( p->iInUseDB>=0 );
493 }
494#else
495# define pagerEnter(X)
496# define pagerLeave(X)
497#endif
498
499/*
drh726de592004-06-10 23:35:50 +0000500** Enable reference count tracking (for debugging) here:
drhdd793422001-06-28 01:54:48 +0000501*/
drh7c4ac0c2007-04-05 11:25:58 +0000502#ifdef SQLITE_DEBUG
drh3aac2dd2004-04-26 14:10:20 +0000503 int pager3_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000504 static void pager_refinfo(PgHdr *p){
505 static int cnt = 0;
drh3aac2dd2004-04-26 14:10:20 +0000506 if( !pager3_refinfo_enable ) return;
drhfe63d1c2004-09-08 20:13:04 +0000507 sqlite3DebugPrintf(
drh24c9a2e2007-01-05 02:00:47 +0000508 "REFCNT: %4d addr=%p nRef=%-3d total=%d\n",
509 p->pgno, PGHDR_TO_DATA(p), p->nRef, p->pPager->nRef
drhdd793422001-06-28 01:54:48 +0000510 );
511 cnt++; /* Something to set a breakpoint on */
512 }
513# define REFINFO(X) pager_refinfo(X)
514#else
515# define REFINFO(X)
516#endif
517
danielk1977f35843b2007-04-07 15:03:17 +0000518/*
519** Return true if page *pPg has already been written to the statement
520** journal (or statement snapshot has been created, if *pPg is part
521** of an in-memory database).
522*/
523static int pageInStatement(PgHdr *pPg){
524 Pager *pPager = pPg->pPager;
525 if( MEMDB ){
526 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
527 }else{
528 Pgno pgno = pPg->pgno;
529 u8 *a = pPager->aInStmt;
530 return (a && (int)pgno<=pPager->stmtSize && (a[pgno/8] & (1<<(pgno&7))));
531 }
532}
drh8ca0c722006-05-07 17:49:38 +0000533
534/*
535** Change the size of the pager hash table to N. N must be a power
536** of two.
537*/
538static void pager_resize_hash_table(Pager *pPager, int N){
539 PgHdr **aHash, *pPg;
540 assert( N>0 && (N&(N-1))==0 );
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
637 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage<<8))&&nSector<=nPage) ){
638 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
825 /* FIX ME:
826 **
827 ** Possibly for a pager not in no-sync mode, the journal magic should not
828 ** be written until nRec is filled in as part of next syncJournal().
829 **
830 ** Actually maybe the whole journal header should be delayed until that
831 ** point. Think about this.
832 */
drh97b57482006-01-10 20:32:32 +0000833 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
834 /* The nRec Field. 0xFFFFFFFF for no-sync journals. */
835 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->noSync ? 0xffffffff : 0);
836 /* The random check-hash initialiser */
837 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
838 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
839 /* The initial database size */
840 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
841 /* The assumed sector size for this process */
842 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drhb0603412007-02-28 04:47:26 +0000843 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
danielk197762079062007-08-15 17:08:46 +0000844 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
845 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197776572402004-06-25 02:38:54 +0000846
847 /* The journal header has been written successfully. Seek the journal
848 ** file descriptor to the end of the journal header sector.
849 */
850 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +0000851 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
danielk197762079062007-08-15 17:08:46 +0000852 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
danielk197776572402004-06-25 02:38:54 +0000853 }
854 return rc;
855}
856
857/*
858** The journal file must be open when this is called. A journal header file
859** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
860** file. See comments above function writeJournalHdr() for a description of
861** the journal header format.
862**
863** If the header is read successfully, *nRec is set to the number of
864** page records following this header and *dbSize is set to the size of the
865** database before the transaction began, in pages. Also, pPager->cksumInit
866** is set to the value read from the journal header. SQLITE_OK is returned
867** in this case.
868**
869** If the journal header file appears to be corrupted, SQLITE_DONE is
870** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
871** cannot be read from the journal file an error code is returned.
872*/
873static int readJournalHdr(
874 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000875 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000876 u32 *pNRec,
877 u32 *pDbSize
878){
879 int rc;
880 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000881 i64 jrnlOff;
danielk197776572402004-06-25 02:38:54 +0000882
danielk197762079062007-08-15 17:08:46 +0000883 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000884 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
885 return SQLITE_DONE;
886 }
danielk197762079062007-08-15 17:08:46 +0000887 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000888
danielk197762079062007-08-15 17:08:46 +0000889 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000890 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000891 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000892
893 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
894 return SQLITE_DONE;
895 }
896
danielk197762079062007-08-15 17:08:46 +0000897 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000898 if( rc ) return rc;
899
danielk197762079062007-08-15 17:08:46 +0000900 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000901 if( rc ) return rc;
902
danielk197762079062007-08-15 17:08:46 +0000903 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000904 if( rc ) return rc;
905
906 /* Update the assumed sector-size to match the value used by
907 ** the process that created this journal. If this journal was
908 ** created by a process other than this one, then this routine
909 ** is being called from within pager_playback(). The local value
910 ** of Pager.sectorSize is restored at the end of that routine.
911 */
danielk197762079062007-08-15 17:08:46 +0000912 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000913 if( rc ) return rc;
914
915 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000916 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000917}
918
919
920/*
921** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000922** pPager at the current location. The master journal name must be the last
923** thing written to a journal file. If the pager is in full-sync mode, the
924** journal file descriptor is advanced to the next sector boundary before
925** anything is written. The format is:
926**
927** + 4 bytes: PAGER_MJ_PGNO.
928** + N bytes: length of master journal name.
929** + 4 bytes: N
930** + 4 bytes: Master journal name checksum.
931** + 8 bytes: aJournalMagic[].
932**
933** The master journal page checksum is the sum of the bytes in the master
934** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000935**
936** If zMaster is a NULL pointer (occurs for a single database transaction),
937** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000938*/
939static int writeMasterJournal(Pager *pPager, const char *zMaster){
940 int rc;
941 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000942 int i;
danielk197762079062007-08-15 17:08:46 +0000943 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +0000944 u32 cksum = 0;
945 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000946
947 if( !zMaster || pPager->setMaster) return SQLITE_OK;
948 pPager->setMaster = 1;
949
950 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000951 for(i=0; i<len; i++){
952 cksum += zMaster[i];
953 }
danielk197776572402004-06-25 02:38:54 +0000954
955 /* If in full-sync mode, advance to the next disk sector before writing
956 ** the master journal name. This is in case the previous page written to
957 ** the journal has already been synced.
958 */
959 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000960 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000961 }
danielk197762079062007-08-15 17:08:46 +0000962 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000963 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000964
danielk197762079062007-08-15 17:08:46 +0000965 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000966 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000967 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000968
danielk197762079062007-08-15 17:08:46 +0000969 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000970 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000971 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000972
drh97b57482006-01-10 20:32:32 +0000973 put32bits(zBuf, len);
974 put32bits(&zBuf[4], cksum);
975 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000976 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +0000977 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +0000978 return rc;
979}
980
981/*
drh03eb96a2002-11-10 23:32:56 +0000982** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +0000983** statement journal.
drh03eb96a2002-11-10 23:32:56 +0000984**
985** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +0000986** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +0000987** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +0000988** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +0000989*/
drh3aac2dd2004-04-26 14:10:20 +0000990static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +0000991 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +0000992 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
993 assert( MEMDB );
994 if( !pHist->inStmt ){
995 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
996 if( pPager->pStmt ){
997 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
998 }
999 pHist->pNextStmt = pPager->pStmt;
1000 pPager->pStmt = pPg;
1001 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001002 }
drh03eb96a2002-11-10 23:32:56 +00001003}
1004
1005/*
drhed7c8552001-04-11 14:29:21 +00001006** Find a page in the hash table given its page number. Return
1007** a pointer to the page or NULL if not found.
1008*/
drhd9b02572001-04-15 00:37:09 +00001009static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001010 PgHdr *p;
1011 if( pPager->aHash==0 ) return 0;
1012 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001013 while( p && p->pgno!=pgno ){
1014 p = p->pNextHash;
1015 }
1016 return p;
1017}
1018
1019/*
drh1aa2d8b2007-01-03 15:34:29 +00001020** Unlock the database file.
drh1aa2d8b2007-01-03 15:34:29 +00001021*/
1022static void pager_unlock(Pager *pPager){
danielk197741483462007-03-24 16:45:04 +00001023 if( !pPager->exclusiveMode ){
1024 if( !MEMDB ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001025 osUnlock(pPager->fd, NO_LOCK);
danielk197741483462007-03-24 16:45:04 +00001026 pPager->dbSize = -1;
1027 IOTRACE(("UNLOCK %p\n", pPager))
1028 }
1029 pPager->state = PAGER_UNLOCK;
1030 pPager->changeCountDone = 0;
drh1aa2d8b2007-01-03 15:34:29 +00001031 }
danielk1977e277be02007-03-23 18:12:06 +00001032}
1033
1034/*
1035** Execute a rollback if a transaction is active and unlock the
1036** database file. This is a no-op if the pager has already entered
1037** the error-state.
1038*/
danielk1977334cdb62007-03-26 08:05:12 +00001039static void pagerUnlockAndRollback(Pager *p){
1040 if( p->errCode ) return;
danielk1977c5859712007-03-26 12:26:27 +00001041 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
danielk1977334cdb62007-03-26 08:05:12 +00001042 if( p->state>=PAGER_RESERVED ){
1043 sqlite3PagerRollback(p);
danielk1977e277be02007-03-23 18:12:06 +00001044 }
danielk1977334cdb62007-03-26 08:05:12 +00001045 pager_unlock(p);
1046 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1047 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drh1aa2d8b2007-01-03 15:34:29 +00001048}
1049
1050
1051/*
danielk1977e180dd92007-04-05 17:15:52 +00001052** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001053** sets the state of the pager back to what it was when it was first
1054** opened. Any outstanding pages are invalidated and subsequent attempts
1055** to access those pages will likely result in a coredump.
1056*/
drhd9b02572001-04-15 00:37:09 +00001057static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001058 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001059 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001060 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001061 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1062 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001063 pNext = pPg->pNextAll;
drh17435752007-08-16 04:30:38 +00001064 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001065 }
danielk1977b94bf852007-03-19 13:53:37 +00001066 pPager->pStmt = 0;
drhed7c8552001-04-11 14:29:21 +00001067 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +00001068 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +00001069 pPager->pLast = 0;
1070 pPager->pAll = 0;
drh8ca0c722006-05-07 17:49:38 +00001071 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001072 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001073 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001074 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001075 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001076}
1077
1078/*
drh80e35f42007-03-30 14:06:34 +00001079** This routine ends a transaction. A transaction is ended by either
1080** a COMMIT or a ROLLBACK.
1081**
drhed7c8552001-04-11 14:29:21 +00001082** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001083** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1084** the database lock and acquires a SHARED lock in its place if that is
1085** the appropriate thing to do. Release locks usually is appropriate,
1086** unless we are in exclusive access mode or unless this is a
1087** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1088**
1089** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001090**
1091** TODO: Consider keeping the journal file open for temporary databases.
1092** This might give a performance improvement on windows where opening
1093** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001094*/
drh80e35f42007-03-30 14:06:34 +00001095static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001096 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001097 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001098 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001099 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001100 if( pPager->state<PAGER_RESERVED ){
1101 return SQLITE_OK;
1102 }
danielk19773b8a05f2007-03-19 17:44:26 +00001103 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001104 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001105 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001106 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001107 }
drhda47d772002-12-02 04:25:19 +00001108 if( pPager->journalOpen ){
drh01fa4c32007-04-02 11:22:22 +00001109 if( pPager->exclusiveMode
1110 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
danielk197741483462007-03-24 16:45:04 +00001111 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001112 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001113 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001114 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001115 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001116 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001117 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001118 }
danielk197741483462007-03-24 16:45:04 +00001119 }
drh17435752007-08-16 04:30:38 +00001120 sqlite3_free( pPager->aInJournal );
drhda47d772002-12-02 04:25:19 +00001121 pPager->aInJournal = 0;
1122 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1123 pPg->inJournal = 0;
1124 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001125 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001126 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001127#ifdef SQLITE_CHECK_PAGES
1128 pPg->pageHash = pager_pagehash(pPg);
1129#endif
drhda47d772002-12-02 04:25:19 +00001130 }
drh605ad8f2006-05-03 23:34:05 +00001131 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001132 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001133 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001134 }else{
drh88b01a12005-03-28 18:04:27 +00001135 assert( pPager->aInJournal==0 );
drha6abd042004-06-09 17:37:22 +00001136 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001137 }
danielk1977979f38e2007-03-27 16:19:51 +00001138
danielk197741483462007-03-24 16:45:04 +00001139 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001140 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001141 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001142 }else if( pPager->state==PAGER_SYNCED ){
1143 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001144 }
danielk1977334cdb62007-03-26 08:05:12 +00001145 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001146 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001147 pPager->needSync = 0;
1148 pPager->pFirstSynced = pPager->pFirst;
drh588f5bc2007-01-02 18:41:54 +00001149 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001150
1151 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001152}
1153
drhed7c8552001-04-11 14:29:21 +00001154/*
drh968af522003-02-11 14:55:40 +00001155** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001156**
1157** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001158** random initial value and the page number. We experimented with
1159** a checksum of the entire data, but that was found to be too slow.
1160**
1161** Note that the page number is stored at the beginning of data and
1162** the checksum is stored at the end. This is important. If journal
1163** corruption occurs due to a power failure, the most likely scenario
1164** is that one end or the other of the record will be changed. It is
1165** much less likely that the two ends of the journal record will be
1166** correct and the middle be corrupt. Thus, this "checksum" scheme,
1167** though fast and simple, catches the mostly likely kind of corruption.
1168**
1169** FIX ME: Consider adding every 200th (or so) byte of the data to the
1170** checksum. That way if a single page spans 3 or more disk sectors and
1171** only the middle sector is corrupt, we will still have a reasonable
1172** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001173*/
drh74161702006-02-24 02:53:49 +00001174static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001175 u32 cksum = pPager->cksumInit;
1176 int i = pPager->pageSize-200;
1177 while( i>0 ){
1178 cksum += aData[i];
1179 i -= 200;
1180 }
drh968af522003-02-11 14:55:40 +00001181 return cksum;
1182}
1183
drh605ad8f2006-05-03 23:34:05 +00001184/* Forward declaration */
1185static void makeClean(PgHdr*);
1186
drh968af522003-02-11 14:55:40 +00001187/*
drhfa86c412002-02-02 15:01:15 +00001188** Read a single page from the journal file opened on file descriptor
1189** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001190**
drh726de592004-06-10 23:35:50 +00001191** If useCksum==0 it means this journal does not use checksums. Checksums
1192** are not used in statement journals because statement journals do not
1193** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001194*/
danielk197762079062007-08-15 17:08:46 +00001195static int pager_playback_one_page(
1196 Pager *pPager,
1197 sqlite3_file *jfd,
1198 i64 offset,
1199 int useCksum
1200){
drhfa86c412002-02-02 15:01:15 +00001201 int rc;
drhae2b40c2004-06-09 19:03:54 +00001202 PgHdr *pPg; /* An existing page in the cache */
1203 Pgno pgno; /* The page number of a page in journal */
1204 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001205 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001206
drh96362842005-03-20 22:47:56 +00001207 /* useCksum should be true for the main journal and false for
1208 ** statement journals. Verify that this is always the case
1209 */
drh9cbe6352005-11-29 03:13:21 +00001210 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001211 assert( aData );
drh96362842005-03-20 22:47:56 +00001212
danielk197762079062007-08-15 17:08:46 +00001213 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001214 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001215 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001216 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001217 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001218
drh968af522003-02-11 14:55:40 +00001219 /* Sanity checking on the page. This is more important that I originally
1220 ** thought. If a power failure occurs while the journal is being written,
1221 ** it could cause invalid data to be written into the journal. We need to
1222 ** detect this invalid data (with high probability) and ignore it.
1223 */
danielk197775edc162004-06-26 01:48:18 +00001224 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001225 return SQLITE_DONE;
1226 }
drhae2b40c2004-06-09 19:03:54 +00001227 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001228 return SQLITE_OK;
1229 }
drhae2b40c2004-06-09 19:03:54 +00001230 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001231 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001232 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001233 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001234 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001235 return SQLITE_DONE;
1236 }
1237 }
drhfa86c412002-02-02 15:01:15 +00001238
danielk1977aa5ccdf2004-06-14 05:10:42 +00001239 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001240
1241 /* If the pager is in RESERVED state, then there must be a copy of this
1242 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001243 ** not the database file. The page is left marked dirty in this case.
1244 **
danielk19772df71c72007-05-24 07:22:42 +00001245 ** An exception to the above rule: If the database is in no-sync mode
1246 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001247 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1248 ** during a Movepage() call, then the page may not be in the cache
1249 ** either. So the condition described in the above paragraph is not
1250 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001251 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001252 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1253 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001254 **
1255 ** Ticket #1171: The statement journal might contain page content that is
1256 ** different from the page content at the start of the transaction.
1257 ** This occurs when a page is changed prior to the start of a statement
1258 ** then changed again within the statement. When rolling back such a
1259 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001260 ** for certain that original page contents are synced into the main rollback
1261 ** journal. Otherwise, a power loss might leave modified data in the
1262 ** database file without an entry in the rollback journal that can
1263 ** restore the database to its original form. Two conditions must be
1264 ** met before writing to the database files. (1) the database must be
1265 ** locked. (2) we know that the original page content is fully synced
1266 ** in the main journal either because the page is not in cache or else
1267 ** the page is marked as needSync==0.
drhfa86c412002-02-02 15:01:15 +00001268 */
drhae2b40c2004-06-09 19:03:54 +00001269 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001270 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1271 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh96362842005-03-20 22:47:56 +00001272 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
danielk197762079062007-08-15 17:08:46 +00001273 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1274 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001275 if( pPg ){
1276 makeClean(pPg);
1277 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001278 }
drhfa86c412002-02-02 15:01:15 +00001279 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001280 /* No page should ever be explicitly rolled back that is in use, except
1281 ** for page 1 which is held in use in order to keep the lock on the
1282 ** database active. However such a page may be rolled back as a result
1283 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001284 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001285 */
drhb6f41482004-05-14 01:58:11 +00001286 void *pData;
danielk197728129562005-01-11 10:25:06 +00001287 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001288 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001289 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001290 if( pPager->xReiniter ){
1291 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001292 }
danielk19773c407372005-02-15 02:54:14 +00001293#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001294 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001295#endif
drh86a88112007-04-16 15:02:19 +00001296 /* If this was page 1, then restore the value of Pager.dbFileVers.
1297 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001298 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001299 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001300 }
drh86a88112007-04-16 15:02:19 +00001301
1302 /* Decode the page just read from disk */
1303 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001304 }
1305 return rc;
1306}
1307
1308/*
danielk197713adf8a2004-06-03 16:08:41 +00001309** Parameter zMaster is the name of a master journal file. A single journal
1310** file that referred to the master journal file has just been rolled back.
1311** This routine checks if it is possible to delete the master journal file,
1312** and does so if it is.
drh726de592004-06-10 23:35:50 +00001313**
1314** The master journal file contains the names of all child journals.
1315** To tell if a master journal can be deleted, check to each of the
1316** children. If all children are either missing or do not refer to
1317** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001318*/
danielk1977b4b47412007-08-17 15:53:36 +00001319static int pager_delmaster(Pager *pPager, const char *zMaster){
1320 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001321 int rc;
1322 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001323 sqlite3_file *pMaster;
1324 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001325 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001326 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001327
1328 /* Open the master journal file exclusively in case some other process
1329 ** is running this routine also. Not that it makes too much difference.
1330 */
danielk1977b4b47412007-08-17 15:53:36 +00001331 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001332 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001333 if( !pMaster ){
1334 rc = SQLITE_NOMEM;
1335 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001336 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1337 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001338 }
danielk197713adf8a2004-06-03 16:08:41 +00001339 if( rc!=SQLITE_OK ) goto delmaster_out;
1340 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001341
1342 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001343 if( rc!=SQLITE_OK ) goto delmaster_out;
1344
1345 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001346 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001347 char *zMasterPtr = 0;
danielk19775865e3d2004-06-14 06:03:57 +00001348
1349 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001350 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001351 */
drh17435752007-08-16 04:30:38 +00001352 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001353 if( !zMasterJournal ){
1354 rc = SQLITE_NOMEM;
1355 goto delmaster_out;
1356 }
danielk1977b4b47412007-08-17 15:53:36 +00001357 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001358 if( rc!=SQLITE_OK ) goto delmaster_out;
1359
danielk19775865e3d2004-06-14 06:03:57 +00001360 zJournal = zMasterJournal;
1361 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977b4b47412007-08-17 15:53:36 +00001362 if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
danielk197713adf8a2004-06-03 16:08:41 +00001363 /* One of the journals pointed to by the master journal exists.
1364 ** Open it and check if it points at the master journal. If
1365 ** so, return without deleting the master journal file.
1366 */
drh3b7b78b2004-11-24 01:16:43 +00001367 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001368 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1369 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001370 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001371 goto delmaster_out;
1372 }
danielk197713adf8a2004-06-03 16:08:41 +00001373
danielk1977b4b47412007-08-17 15:53:36 +00001374 rc = readMasterJournal(pJournal, &zMasterPtr);
1375 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001376 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001377 goto delmaster_out;
1378 }
danielk197776572402004-06-25 02:38:54 +00001379
drh3b7b78b2004-11-24 01:16:43 +00001380 c = zMasterPtr!=0 && strcmp(zMasterPtr, zMaster)==0;
drh17435752007-08-16 04:30:38 +00001381 sqlite3_free(zMasterPtr);
drh3b7b78b2004-11-24 01:16:43 +00001382 if( c ){
danielk197776572402004-06-25 02:38:54 +00001383 /* We have a match. Do not delete the master journal file. */
1384 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001385 }
1386 }
danielk19775865e3d2004-06-14 06:03:57 +00001387 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001388 }
1389 }
1390
danielk1977fee2d252007-08-18 10:59:19 +00001391 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001392
1393delmaster_out:
1394 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001395 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001396 }
1397 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001398 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001399 }
danielk1977b4b47412007-08-17 15:53:36 +00001400 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001401 return rc;
1402}
1403
drha6abd042004-06-09 17:37:22 +00001404
danielk1977e180dd92007-04-05 17:15:52 +00001405static void pager_truncate_cache(Pager *pPager);
1406
drha6abd042004-06-09 17:37:22 +00001407/*
drhcb4c40b2004-08-18 19:09:43 +00001408** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001409** indicated. Also truncate the cached representation of the file.
drhcb4c40b2004-08-18 19:09:43 +00001410*/
1411static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001412 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001413 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
danielk1977e180dd92007-04-05 17:15:52 +00001414 rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
1415 }
1416 if( rc==SQLITE_OK ){
1417 pPager->dbSize = nPage;
1418 pager_truncate_cache(pPager);
1419 }
1420 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001421}
1422
1423/*
drhc80f0582007-05-01 16:59:48 +00001424** Set the sectorSize for the given pager.
1425**
1426** The sector size is the larger of the sector size reported
1427** by sqlite3OsSectorSize() and the pageSize.
1428*/
1429static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001430 assert(pPager->fd->pMethods||pPager->tempFile);
1431 if( !pPager->tempFile ){
1432 /* Sector size doesn't matter for temporary files. Also, the file
1433 ** may not have been opened yet, in whcih case the OsSectorSize()
1434 ** call will segfault.
1435 */
1436 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1437 }
drhc80f0582007-05-01 16:59:48 +00001438 if( pPager->sectorSize<pPager->pageSize ){
1439 pPager->sectorSize = pPager->pageSize;
1440 }
1441}
1442
1443/*
drhed7c8552001-04-11 14:29:21 +00001444** Playback the journal and thus restore the database file to
1445** the state it was in before we started making changes.
1446**
drh34e79ce2004-02-08 06:05:46 +00001447** The journal file format is as follows:
1448**
drhae2b40c2004-06-09 19:03:54 +00001449** (1) 8 byte prefix. A copy of aJournalMagic[].
1450** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001451** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001452** number of page records from the journal size.
1453** (3) 4 byte big-endian integer which is the initial value for the
1454** sanity checksum.
1455** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001456** database to during a rollback.
drhae2b40c2004-06-09 19:03:54 +00001457** (5) 4 byte integer which is the number of bytes in the master journal
1458** name. The value may be zero (indicate that there is no master
1459** journal.)
1460** (6) N bytes of the master journal name. The name will be nul-terminated
1461** and might be shorter than the value read from (5). If the first byte
1462** of the name is \000 then there is no master journal. The master
1463** journal name is stored in UTF-8.
1464** (7) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001465** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001466** + pPager->pageSize bytes of data.
1467** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001468**
drhae2b40c2004-06-09 19:03:54 +00001469** When we speak of the journal header, we mean the first 6 items above.
1470** Each entry in the journal is an instance of the 7th item.
drh34e79ce2004-02-08 06:05:46 +00001471**
1472** Call the value from the second bullet "nRec". nRec is the number of
1473** valid page entries in the journal. In most cases, you can compute the
1474** value of nRec from the size of the journal file. But if a power
1475** failure occurred while the journal was being written, it could be the
1476** case that the size of the journal file had already been increased but
1477** the extra entries had not yet made it safely to disk. In such a case,
1478** the value of nRec computed from the file size would be too large. For
1479** that reason, we always use the nRec value in the header.
1480**
1481** If the nRec value is 0xffffffff it means that nRec should be computed
1482** from the file size. This value is used when the user selects the
1483** no-sync option for the journal. A power failure could lead to corruption
1484** in this case. But for things like temporary table (which will be
1485** deleted when the power is restored) we don't care.
1486**
drhd9b02572001-04-15 00:37:09 +00001487** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001488** journal file then all pages up to the first corrupted page are rolled
1489** back (or no pages if the journal header is corrupted). The journal file
1490** is then deleted and SQLITE_OK returned, just as if no corruption had
1491** been encountered.
1492**
1493** If an I/O or malloc() error occurs, the journal-file is not deleted
1494** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001495*/
danielk1977e277be02007-03-23 18:12:06 +00001496static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001497 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001498 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001499 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001500 int i; /* Loop counter */
1501 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001502 int rc; /* Result code of a subroutine */
danielk197713adf8a2004-06-03 16:08:41 +00001503 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001504
drhc3a64ba2001-11-22 00:01:27 +00001505 /* Figure out how many records are in the journal. Abort early if
1506 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001507 */
drh8cfbf082001-09-19 13:22:39 +00001508 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001509 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001510 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001511 goto end_playback;
1512 }
drh240c5792004-02-08 00:40:52 +00001513
danielk197776572402004-06-25 02:38:54 +00001514 /* Read the master journal name from the journal, if it is present.
1515 ** If a master journal file name is specified, but the file is not
1516 ** present on disk, then the journal is not hot and does not need to be
1517 ** played back.
drh240c5792004-02-08 00:40:52 +00001518 */
drh9cbe6352005-11-29 03:13:21 +00001519 rc = readMasterJournal(pPager->jfd, &zMaster);
danielk197776572402004-06-25 02:38:54 +00001520 assert( rc!=SQLITE_DONE );
danielk1977b4b47412007-08-17 15:53:36 +00001521 if( rc!=SQLITE_OK
1522 || (zMaster && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))
1523 ){
drh17435752007-08-16 04:30:38 +00001524 sqlite3_free(zMaster);
danielk197776572402004-06-25 02:38:54 +00001525 zMaster = 0;
1526 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drhc3a64ba2001-11-22 00:01:27 +00001527 goto end_playback;
1528 }
danielk197776572402004-06-25 02:38:54 +00001529 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001530
danielk197776572402004-06-25 02:38:54 +00001531 /* This loop terminates either when the readJournalHdr() call returns
1532 ** SQLITE_DONE or an IO error occurs. */
1533 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001534
danielk197776572402004-06-25 02:38:54 +00001535 /* Read the next journal header from the journal file. If there are
1536 ** not enough bytes left in the journal file for a complete header, or
1537 ** it is corrupted, then a process must of failed while writing it.
1538 ** This indicates nothing more needs to be rolled back.
1539 */
1540 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1541 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001542 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001543 rc = SQLITE_OK;
1544 }
danielk197776572402004-06-25 02:38:54 +00001545 goto end_playback;
1546 }
1547
1548 /* If nRec is 0xffffffff, then this journal was created by a process
1549 ** working in no-sync mode. This means that the rest of the journal
1550 ** file consists of pages, there are no more journal headers. Compute
1551 ** the value of nRec based on this assumption.
1552 */
1553 if( nRec==0xffffffff ){
1554 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1555 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1556 }
1557
danielk1977e277be02007-03-23 18:12:06 +00001558 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001559 ** process and if this is the final header in the journal, then it means
1560 ** that this part of the journal was being filled but has not yet been
1561 ** synced to disk. Compute the number of pages based on the remaining
1562 ** size of the file.
1563 **
1564 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001565 */
drh8940f4e2007-08-11 00:26:20 +00001566 if( nRec==0 && !isHot &&
1567 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001568 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1569 }
1570
danielk197776572402004-06-25 02:38:54 +00001571 /* If this is the first header read from the journal, truncate the
1572 ** database file back to it's original size.
1573 */
danielk1977e180dd92007-04-05 17:15:52 +00001574 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001575 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001576 if( rc!=SQLITE_OK ){
1577 goto end_playback;
1578 }
danielk197776572402004-06-25 02:38:54 +00001579 }
1580
danielk197776572402004-06-25 02:38:54 +00001581 /* Copy original pages out of the journal and back into the database file.
1582 */
1583 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001584 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001585 if( rc!=SQLITE_OK ){
1586 if( rc==SQLITE_DONE ){
1587 rc = SQLITE_OK;
1588 pPager->journalOff = szJ;
1589 break;
1590 }else{
1591 goto end_playback;
1592 }
1593 }
drh968af522003-02-11 14:55:40 +00001594 }
drhed7c8552001-04-11 14:29:21 +00001595 }
drh580eeaf2006-02-24 03:09:37 +00001596 /*NOTREACHED*/
1597 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001598
1599end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001600 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001601 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001602 }
danielk197713adf8a2004-06-03 16:08:41 +00001603 if( zMaster ){
danielk1977979f38e2007-03-27 16:19:51 +00001604 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001605 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001606 */
1607 if( rc==SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001608 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001609 }
drh17435752007-08-16 04:30:38 +00001610 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001611 }
danielk197776572402004-06-25 02:38:54 +00001612
1613 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001614 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001615 ** value. Reset it to the correct value for this process.
1616 */
drhc80f0582007-05-01 16:59:48 +00001617 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001618 return rc;
drhed7c8552001-04-11 14:29:21 +00001619}
1620
1621/*
drhac69b052004-05-12 13:30:07 +00001622** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001623**
1624** This is similar to playing back the transaction journal but with
1625** a few extra twists.
1626**
drh663fc632002-02-02 18:49:19 +00001627** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001628** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001629** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001630**
drhac69b052004-05-12 13:30:07 +00001631** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001632** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001633** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001634*/
drh3aac2dd2004-04-26 14:10:20 +00001635static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001636 i64 szJ; /* Size of the full journal */
1637 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001638 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001639 int i; /* Loop counter */
1640 int rc;
1641
danielk197776572402004-06-25 02:38:54 +00001642 szJ = pPager->journalOff;
1643#ifndef NDEBUG
1644 {
drheb206252004-10-01 02:00:31 +00001645 i64 os_szJ;
drh054889e2005-11-30 03:20:31 +00001646 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
danielk197776572402004-06-25 02:38:54 +00001647 if( rc!=SQLITE_OK ) return rc;
1648 assert( szJ==os_szJ );
1649 }
1650#endif
1651
danielk19774099f6e2007-03-19 11:25:20 +00001652 /* Set hdrOff to be the offset just after the end of the last journal
1653 ** page written before the first journal-header for this statement
1654 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001655 ** header was written.
1656 */
1657 hdrOff = pPager->stmtHdrOff;
1658 assert( pPager->fullSync || !hdrOff );
1659 if( !hdrOff ){
1660 hdrOff = szJ;
1661 }
1662
drhfa86c412002-02-02 15:01:15 +00001663 /* Truncate the database back to its original size.
1664 */
danielk1977e180dd92007-04-05 17:15:52 +00001665 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001666 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001667
drhac69b052004-05-12 13:30:07 +00001668 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001669 */
drhac69b052004-05-12 13:30:07 +00001670 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001671 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001672
drhac69b052004-05-12 13:30:07 +00001673 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001674 ** database file. Note that the statement journal omits checksums from
1675 ** each record since power-failure recovery is not important to statement
1676 ** journals.
drhfa86c412002-02-02 15:01:15 +00001677 */
danielk197762079062007-08-15 17:08:46 +00001678 for(i=0; i<nRec; i++){
1679 i64 offset = i*(4+pPager->pageSize);
1680 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001681 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001682 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001683 }
1684
danielk197776572402004-06-25 02:38:54 +00001685 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1686 ** was the size of the journal file when this statement was started, so
1687 ** everything after that needs to be rolled back, either into the
1688 ** database, the memory cache, or both.
1689 **
1690 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1691 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001692 */
danielk197776572402004-06-25 02:38:54 +00001693 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001694 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001695 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001696 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001697 assert( rc!=SQLITE_DONE );
1698 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1699 }
1700
1701 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001702 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001703 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001704 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001705 if( rc!=SQLITE_OK ){
1706 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001707 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001708 }
danielk1977f0113002006-01-24 12:09:17 +00001709 if( nJRec==0 ){
1710 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001711 }
danielk1977f0113002006-01-24 12:09:17 +00001712 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001713 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001714 assert( rc!=SQLITE_DONE );
1715 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1716 }
drhfa86c412002-02-02 15:01:15 +00001717 }
danielk197776572402004-06-25 02:38:54 +00001718
1719 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001720
drh3aac2dd2004-04-26 14:10:20 +00001721end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001722 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001723 pPager->journalOff = szJ;
1724 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001725 }
1726 return rc;
1727}
1728
1729/*
drhf57b14a2001-09-14 18:54:08 +00001730** Change the maximum number of in-memory pages that are allowed.
1731*/
danielk19773b8a05f2007-03-19 17:44:26 +00001732void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00001733 if( mxPage>10 ){
1734 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00001735 }else{
1736 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00001737 }
1738}
1739
1740/*
drh973b6e32003-02-12 14:09:42 +00001741** Adjust the robustness of the database to damage due to OS crashes
1742** or power failures by changing the number of syncs()s when writing
1743** the rollback journal. There are three levels:
1744**
drh054889e2005-11-30 03:20:31 +00001745** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001746** for temporary and transient files.
1747**
1748** NORMAL The journal is synced once before writes begin on the
1749** database. This is normally adequate protection, but
1750** it is theoretically possible, though very unlikely,
1751** that an inopertune power failure could leave the journal
1752** in a state which would cause damage to the database
1753** when it is rolled back.
1754**
1755** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001756** database (with some additional information - the nRec field
1757** of the journal header - being written in between the two
1758** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001759** single disk sector is atomic, then this mode provides
1760** assurance that the journal will not be corrupted to the
1761** point of causing damage to the database during rollback.
1762**
1763** Numeric values associated with these states are OFF==1, NORMAL=2,
1764** and FULL=3.
1765*/
danielk197793758c82005-01-21 08:13:14 +00001766#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00001767void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00001768 pPager->noSync = level==1 || pPager->tempFile;
1769 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00001770 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001771 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001772}
danielk197793758c82005-01-21 08:13:14 +00001773#endif
drh973b6e32003-02-12 14:09:42 +00001774
1775/*
drhaf6df112005-06-07 02:12:30 +00001776** The following global variable is incremented whenever the library
1777** attempts to open a temporary file. This information is used for
1778** testing and analysis only.
1779*/
drh0f7eb612006-08-08 13:51:43 +00001780#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001781int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001782#endif
drhaf6df112005-06-07 02:12:30 +00001783
1784/*
drh3f56e6e2007-03-15 01:16:47 +00001785** Open a temporary file.
1786**
1787** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001788** other error code if we fail. The OS will automatically delete the temporary
1789** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001790**
danielk1977fee2d252007-08-18 10:59:19 +00001791** If zNameOut is 0, then SQLITE_OPEN_SUBJOURNAL is passed to the OS layer.
1792** If zNameOut is not 0, SQLITE_OPEN_TEMP_DB is passed.
drhfa86c412002-02-02 15:01:15 +00001793*/
danielk1977b4b47412007-08-17 15:53:36 +00001794static int sqlite3PagerOpentemp(
1795 sqlite3_vfs *pVfs,
1796 sqlite3_file *pFile,
1797 char *zNameOut
1798){
drhfa86c412002-02-02 15:01:15 +00001799 int cnt = 8;
1800 int rc;
danielk1977b4b47412007-08-17 15:53:36 +00001801 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_EXCLUSIVE);
1802
1803 char *zFree = 0;
1804 if( zNameOut==0 ){
1805 zFree = (char *)sqlite3_malloc(pVfs->mxPathname);
1806 if( !zFree ){
1807 return SQLITE_NOMEM;
1808 }
1809 zNameOut = zFree;
danielk1977fee2d252007-08-18 10:59:19 +00001810 flags |= SQLITE_OPEN_SUBJOURNAL;
1811 }else{
1812 flags |= SQLITE_OPEN_TEMP_DB;
danielk1977b4b47412007-08-17 15:53:36 +00001813 }
drh3f56e6e2007-03-15 01:16:47 +00001814
drh0f7eb612006-08-08 13:51:43 +00001815#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001816 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001817#endif
danielk1977b4b47412007-08-17 15:53:36 +00001818
drhfa86c412002-02-02 15:01:15 +00001819 do{
1820 cnt--;
danielk1977b4b47412007-08-17 15:53:36 +00001821 sqlite3OsGetTempName(pVfs, zNameOut);
1822 rc = sqlite3OsOpen(pVfs, zNameOut, pFile, flags, 0);
1823 assert( rc!=SQLITE_OK || pFile->pMethods );
danielk197713073932004-06-30 11:54:06 +00001824 }while( cnt>0 && rc!=SQLITE_OK && rc!=SQLITE_NOMEM );
danielk1977b4b47412007-08-17 15:53:36 +00001825
1826 sqlite3_free(zFree);
drhfa86c412002-02-02 15:01:15 +00001827 return rc;
1828}
1829
1830/*
drhed7c8552001-04-11 14:29:21 +00001831** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001832** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001833** the first call to sqlite3PagerGet() and is only held open until the
1834** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001835**
drh6446c4d2001-12-15 14:22:18 +00001836** If zFilename is NULL then a randomly-named temporary file is created
1837** and used as the file to be cached. The file will be deleted
1838** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001839**
1840** If zFilename is ":memory:" then all information is held in cache.
1841** It is never written to disk. This can be used to implement an
1842** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001843*/
danielk19773b8a05f2007-03-19 17:44:26 +00001844int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001845 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001846 Pager **ppPager, /* Return the Pager structure here */
1847 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001848 int nExtra, /* Extra bytes append to each in-memory page */
drh7bec5052005-02-06 02:45:41 +00001849 int flags /* flags controlling this file */
drh7e3b0a02001-04-28 16:52:40 +00001850){
danielk1977b4b47412007-08-17 15:53:36 +00001851 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001852 Pager *pPager = 0;
danielk19778def5ea2004-06-16 10:39:52 +00001853 char *zFullPathname = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001854 int rc = SQLITE_OK;
1855 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001856 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001857 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001858 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001859 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1860 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00001861 int journalFileSize = sqlite3JournalSize(pVfs);
danielk1977b4b47412007-08-17 15:53:36 +00001862
drh86f8c192007-08-22 00:39:19 +00001863 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001864 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001865
danielk1977b4b47412007-08-17 15:53:36 +00001866 /* Allocate memory for the pager structure */
1867 pPager = sqlite3MallocZero(
1868 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00001869 journalFileSize + /* The journal file structure */
1870 pVfs->szOsFile * 2 + /* The db and stmt journal files */
danielk1977b4b47412007-08-17 15:53:36 +00001871 pVfs->mxPathname * 3 + 30 /* zFilename, zDirectory, zJournal */
1872 );
1873 if( !pPager ){
1874 return SQLITE_NOMEM;
1875 }
1876 pPtr = (u8 *)&pPager[1];
1877 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00001878 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
1879 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
1880 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
danielk1977b4b47412007-08-17 15:53:36 +00001881 pPager->zDirectory = &pPager->zFilename[pVfs->mxPathname];
1882 pPager->zJournal = &pPager->zDirectory[pVfs->mxPathname];
1883 pPager->pVfs = pVfs;
1884
danielk1977aef0bf62005-12-30 16:28:01 +00001885 /* Open the pager file and set zFullPathname to point at malloc()ed
1886 ** memory containing the complete filename (i.e. including the directory).
1887 */
drh901afd42003-08-26 11:25:58 +00001888 if( zFilename && zFilename[0] ){
drhb7f91642004-10-31 02:22:47 +00001889#ifndef SQLITE_OMIT_MEMORYDB
drhac69b052004-05-12 13:30:07 +00001890 if( strcmp(zFilename,":memory:")==0 ){
1891 memDb = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001892 pPager->zFilename[0] = '\0';
drhb7f91642004-10-31 02:22:47 +00001893 }else
1894#endif
1895 {
danielk1977b4b47412007-08-17 15:53:36 +00001896 rc = sqlite3OsFullPathname(pVfs, zFilename, pPager->zFilename);
1897 if( rc==SQLITE_OK ){
1898 if( strlen(pPager->zFilename)>(pVfs->mxPathname - strlen("-journal")) ){
1899 rc = SQLITE_CANTOPEN;
1900 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001901 int oflag =
1902 (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB);
danielk1977b4b47412007-08-17 15:53:36 +00001903 int fout = 0;
danielk1977fee2d252007-08-18 10:59:19 +00001904 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, oflag, &fout);
danielk1977b4b47412007-08-17 15:53:36 +00001905 readOnly = (fout&SQLITE_OPEN_READONLY);
1906 }
danielk19778def5ea2004-06-16 10:39:52 +00001907 }
drhac69b052004-05-12 13:30:07 +00001908 }
drh5e00f6c2001-09-13 13:46:56 +00001909 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00001910 /* If a temporary file is requested, it is not opened immediately.
1911 ** In this case we accept the default page size and delay actually
1912 ** opening the file until the first call to OsWrite().
1913 */
danielk19777a2b1ee2007-08-21 14:27:01 +00001914 tempFile = 1;
1915 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00001916 }
danielk1977aef0bf62005-12-30 16:28:01 +00001917
danielk1977b4b47412007-08-17 15:53:36 +00001918 if( pPager && rc==SQLITE_OK ){
1919 pPager->pTmpSpace = (char *)sqlite3_malloc(SQLITE_DEFAULT_PAGE_SIZE);
drh3e7a6092002-12-07 21:45:14 +00001920 }
danielk1977aef0bf62005-12-30 16:28:01 +00001921
1922 /* If an error occured in either of the blocks above, free the memory
1923 ** pointed to by zFullPathname, free the Pager structure and close the
1924 ** file. Since the pager is not allocated there is no need to set
1925 ** any Pager.errMask variables.
1926 */
danielk1977b4b47412007-08-17 15:53:36 +00001927 if( !pPager || !pPager->pTmpSpace ){
1928 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00001929 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00001930 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001931 }
danielk1977aef0bf62005-12-30 16:28:01 +00001932
danielk1977dd97a492007-08-22 18:54:32 +00001933 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), zFullPathname);
drhb0603412007-02-28 04:47:26 +00001934 IOTRACE(("OPEN %p %s\n", pPager, zFullPathname))
danielk1977aef0bf62005-12-30 16:28:01 +00001935
danielk1977b4b47412007-08-17 15:53:36 +00001936 /* Fill in Pager.zDirectory[] */
1937 memcpy(pPager->zDirectory, pPager->zFilename, pVfs->mxPathname);
1938 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00001939 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001940
1941 /* Fill in Pager.zJournal[] */
1942 memcpy(pPager->zJournal, pPager->zFilename, pVfs->mxPathname);
1943 memcpy(&pPager->zJournal[strlen(pPager->zJournal)], "-journal", 9);
1944
drh3b59a5c2006-01-15 20:28:28 +00001945 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00001946 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00001947 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001948 /* pPager->stmtOpen = 0; */
1949 /* pPager->stmtInUse = 0; */
1950 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00001951 pPager->dbSize = memDb-1;
drh90f5ecb2004-07-22 01:19:35 +00001952 pPager->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
drh3b59a5c2006-01-15 20:28:28 +00001953 /* pPager->stmtSize = 0; */
1954 /* pPager->stmtJSize = 0; */
1955 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00001956 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00001957 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00001958 assert( PAGER_UNLOCK==0 );
1959 /* pPager->state = PAGER_UNLOCK; */
1960 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00001961 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00001962 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
1963 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
1964 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
1965 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00001966 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001967 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001968 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00001969 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00001970 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00001971 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00001972 /* pPager->pFirst = 0; */
1973 /* pPager->pFirstSynced = 0; */
1974 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00001975 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00001976 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00001977 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00001978 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00001979 }
drh3b59a5c2006-01-15 20:28:28 +00001980 /* pPager->pBusyHandler = 0; */
1981 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00001982 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00001983#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00001984 pPager->iInUseMM = 0;
1985 pPager->iInUseDB = 0;
1986 if( !memDb ){
1987 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
1988 sqlite3_mutex_enter(mutex);
1989 pPager->pNext = sqlite3PagerList;
1990 if( sqlite3PagerList ){
1991 assert( sqlite3PagerList->pPrev==0 );
1992 sqlite3PagerList->pPrev = pPager;
1993 }
1994 pPager->pPrev = 0;
1995 sqlite3PagerList = pPager;
1996 sqlite3_mutex_leave(mutex);
1997 }
danielk197752622822006-01-09 09:59:49 +00001998#endif
drhed7c8552001-04-11 14:29:21 +00001999 return SQLITE_OK;
2000}
2001
2002/*
drh90f5ecb2004-07-22 01:19:35 +00002003** Set the busy handler function.
2004*/
danielk19773b8a05f2007-03-19 17:44:26 +00002005void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002006 pPager->pBusyHandler = pBusyHandler;
2007}
2008
2009/*
drh72f82862001-05-24 21:06:34 +00002010** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002011** when the reference count on each page reaches zero. The destructor can
2012** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002013**
danielk19773b8a05f2007-03-19 17:44:26 +00002014** The destructor is not called as a result sqlite3PagerClose().
2015** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002016*/
danielk19773b8a05f2007-03-19 17:44:26 +00002017void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002018 pPager->xDestructor = xDesc;
2019}
2020
2021/*
drha6abd042004-06-09 17:37:22 +00002022** Set the reinitializer for this pager. If not NULL, the reinitializer
2023** is called when the content of a page in cache is restored to its original
2024** value as a result of a rollback. The callback gives higher-level code
2025** an opportunity to restore the EXTRA section to agree with the restored
2026** page data.
2027*/
danielk19773b8a05f2007-03-19 17:44:26 +00002028void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002029 pPager->xReiniter = xReinit;
2030}
2031
2032/*
drh1c7880e2005-05-20 20:01:55 +00002033** Set the page size. Return the new size. If the suggest new page
2034** size is inappropriate, then an alternative page size is selected
2035** and returned.
drh90f5ecb2004-07-22 01:19:35 +00002036*/
danielk19773b8a05f2007-03-19 17:44:26 +00002037int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){
drh90f5ecb2004-07-22 01:19:35 +00002038 assert( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE );
danielk1977c7c7e622007-03-26 15:46:00 +00002039 if( !pPager->memDb && pPager->nRef==0 ){
drh86f8c192007-08-22 00:39:19 +00002040 pagerEnter(pPager);
danielk1977c7c7e622007-03-26 15:46:00 +00002041 pager_reset(pPager);
drh1c7880e2005-05-20 20:01:55 +00002042 pPager->pageSize = pageSize;
drh86f8c192007-08-22 00:39:19 +00002043 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00002044 sqlite3_free(pPager->pTmpSpace);
2045 pPager->pTmpSpace = sqlite3_malloc(pageSize);
drh1c7880e2005-05-20 20:01:55 +00002046 }
2047 return pPager->pageSize;
drh90f5ecb2004-07-22 01:19:35 +00002048}
2049
2050/*
drhf8e632b2007-05-08 14:51:36 +00002051** Attempt to set the maximum database page count if mxPage is positive.
2052** Make no changes if mxPage is zero or negative. And never reduce the
2053** maximum page count below the current size of the database.
2054**
2055** Regardless of mxPage, return the current maximum page count.
2056*/
2057int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2058 if( mxPage>0 ){
2059 pPager->mxPgno = mxPage;
2060 }
2061 sqlite3PagerPagecount(pPager);
2062 return pPager->mxPgno;
2063}
2064
2065/*
drhc9ac5ca2005-11-04 22:03:30 +00002066** The following set of routines are used to disable the simulated
2067** I/O error mechanism. These routines are used to avoid simulated
2068** errors in places where we do not care about errors.
2069**
2070** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2071** and generate no code.
2072*/
2073#ifdef SQLITE_TEST
2074extern int sqlite3_io_error_pending;
2075extern int sqlite3_io_error_hit;
2076static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002077void disable_simulated_io_errors(void){
2078 saved_cnt = sqlite3_io_error_pending;
2079 sqlite3_io_error_pending = -1;
2080}
2081void enable_simulated_io_errors(void){
2082 sqlite3_io_error_pending = saved_cnt;
2083}
2084#else
drh152410f2005-11-05 15:11:22 +00002085# define disable_simulated_io_errors()
2086# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002087#endif
2088
2089/*
drh90f5ecb2004-07-22 01:19:35 +00002090** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002091** that pDest points to.
2092**
2093** No error checking is done. The rational for this is that this function
2094** may be called even if the file does not exist or contain a header. In
2095** these cases sqlite3OsRead() will return an error, to which the correct
2096** response is to zero the memory at pDest and continue. A real IO error
2097** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002098*/
danielk19773b8a05f2007-03-19 17:44:26 +00002099int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002100 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002101 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002102 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2103 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002104 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002105 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002106 if( rc==SQLITE_IOERR_SHORT_READ ){
2107 rc = SQLITE_OK;
2108 }
drh90f5ecb2004-07-22 01:19:35 +00002109 }
drh551b7732006-11-06 21:20:25 +00002110 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002111}
2112
2113/*
drh5e00f6c2001-09-13 13:46:56 +00002114** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002115** pPager.
2116**
2117** If the PENDING_BYTE lies on the page directly after the end of the
2118** file, then consider this page part of the file too. For example, if
2119** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2120** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002121*/
danielk19773b8a05f2007-03-19 17:44:26 +00002122int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002123 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002124 int rc;
drhd9b02572001-04-15 00:37:09 +00002125 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002126 if( pPager->errCode ){
2127 return 0;
2128 }
drhed7c8552001-04-11 14:29:21 +00002129 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002130 n = pPager->dbSize;
2131 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002132 assert(pPager->fd->pMethods||pPager->tempFile);
2133 if( (pPager->fd->pMethods)
2134 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002135 pager_error(pPager, rc);
danielk197715f411d2005-09-16 10:18:45 +00002136 return 0;
2137 }
2138 if( n>0 && n<pPager->pageSize ){
2139 n = 1;
2140 }else{
2141 n /= pPager->pageSize;
2142 }
2143 if( pPager->state!=PAGER_UNLOCK ){
2144 pPager->dbSize = n;
2145 }
drhed7c8552001-04-11 14:29:21 +00002146 }
danielk197715f411d2005-09-16 10:18:45 +00002147 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002148 n++;
2149 }
drhf8e632b2007-05-08 14:51:36 +00002150 if( n>pPager->mxPgno ){
2151 pPager->mxPgno = n;
2152 }
drhed7c8552001-04-11 14:29:21 +00002153 return n;
2154}
2155
drhf07e7d52006-04-07 13:54:46 +00002156
2157#ifndef SQLITE_OMIT_MEMORYDB
2158/*
2159** Clear a PgHistory block
2160*/
2161static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002162 sqlite3_free(pHist->pOrig);
2163 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002164 pHist->pOrig = 0;
2165 pHist->pStmt = 0;
2166}
2167#else
2168#define clearHistory(x)
2169#endif
2170
drhed7c8552001-04-11 14:29:21 +00002171/*
drhf7c57532003-04-25 13:22:51 +00002172** Forward declaration
2173*/
danielk197776572402004-06-25 02:38:54 +00002174static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002175
2176/*
danielk1977f5fdda82004-11-03 08:44:05 +00002177** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate
2178** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002179** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002180** pNextFree/pPrevFree list that is not a part of any hash-chain.
2181*/
2182static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2183 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002184 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002185 return;
2186 }
2187 if( pPg->pNextHash ){
2188 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2189 }
2190 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002191 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002192 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2193 }else{
drh8ca0c722006-05-07 17:49:38 +00002194 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002195 pPager->aHash[h] = pPg->pNextHash;
2196 }
drh5229ae42006-03-23 23:29:04 +00002197 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002198 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2199 }
danielk1977f5fdda82004-11-03 08:44:05 +00002200 pPg->pgno = 0;
2201 pPg->pNextHash = pPg->pPrevHash = 0;
2202}
2203
2204/*
drhac69b052004-05-12 13:30:07 +00002205** Unlink a page from the free list (the list of all pages where nRef==0)
2206** and from its hash collision chain.
2207*/
2208static void unlinkPage(PgHdr *pPg){
2209 Pager *pPager = pPg->pPager;
2210
2211 /* Keep the pFirstSynced pointer pointing at the first synchronized page */
2212 if( pPg==pPager->pFirstSynced ){
2213 PgHdr *p = pPg->pNextFree;
2214 while( p && p->needSync ){ p = p->pNextFree; }
2215 pPager->pFirstSynced = p;
2216 }
2217
2218 /* Unlink from the freelist */
2219 if( pPg->pPrevFree ){
2220 pPg->pPrevFree->pNextFree = pPg->pNextFree;
2221 }else{
2222 assert( pPager->pFirst==pPg );
2223 pPager->pFirst = pPg->pNextFree;
2224 }
2225 if( pPg->pNextFree ){
2226 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
2227 }else{
2228 assert( pPager->pLast==pPg );
2229 pPager->pLast = pPg->pPrevFree;
2230 }
2231 pPg->pNextFree = pPg->pPrevFree = 0;
2232
2233 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002234 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002235}
2236
2237/*
danielk1977e180dd92007-04-05 17:15:52 +00002238** This routine is used to truncate the cache when a database
2239** is truncated. Drop from the cache all pages whose pgno is
2240** larger than pPager->dbSize and is unreferenced.
2241**
drhac69b052004-05-12 13:30:07 +00002242** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002243**
2244** Actually, at the point this routine is called, it would be
2245** an error to have a referenced page. But rather than delete
2246** that page and guarantee a subsequent segfault, it seems better
2247** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002248*/
danielk1977e180dd92007-04-05 17:15:52 +00002249static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002250 PgHdr *pPg;
2251 PgHdr **ppPg;
2252 int dbSize = pPager->dbSize;
2253
2254 ppPg = &pPager->pAll;
2255 while( (pPg = *ppPg)!=0 ){
2256 if( pPg->pgno<=dbSize ){
2257 ppPg = &pPg->pNextAll;
2258 }else if( pPg->nRef>0 ){
2259 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2260 ppPg = &pPg->pNextAll;
2261 }else{
2262 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002263 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2264 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002265 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002266 makeClean(pPg);
drh17435752007-08-16 04:30:38 +00002267 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002268 pPager->nPage--;
2269 }
2270 }
2271}
2272
drhf7c57532003-04-25 13:22:51 +00002273/*
danielk197717221812005-02-15 03:38:05 +00002274** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002275** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002276** false or until the lock succeeds.
2277**
2278** Return SQLITE_OK on success and an error code if we cannot obtain
2279** the lock.
2280*/
2281static int pager_wait_on_lock(Pager *pPager, int locktype){
2282 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002283
2284 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002285 assert( PAGER_SHARED==SHARED_LOCK );
2286 assert( PAGER_RESERVED==RESERVED_LOCK );
2287 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002288
2289 /* If the file is currently unlocked then the size must be unknown */
2290 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2291
danielk197717221812005-02-15 03:38:05 +00002292 if( pPager->state>=locktype ){
2293 rc = SQLITE_OK;
2294 }else{
danielk197717221812005-02-15 03:38:05 +00002295 do {
drh054889e2005-11-30 03:20:31 +00002296 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002297 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002298 if( rc==SQLITE_OK ){
2299 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002300 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002301 }
2302 }
2303 return rc;
2304}
2305
2306/*
drhf7c57532003-04-25 13:22:51 +00002307** Truncate the file to the number of pages specified.
2308*/
danielk19773b8a05f2007-03-19 17:44:26 +00002309int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002310 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002311 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002312 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002313 if( pPager->errCode ){
2314 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002315 return rc;
2316 }
drh7d02cb72003-06-04 16:24:39 +00002317 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002318 return SQLITE_OK;
2319 }
drhb7f91642004-10-31 02:22:47 +00002320 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002321 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002322 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002323 return SQLITE_OK;
2324 }
drh86f8c192007-08-22 00:39:19 +00002325 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002326 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002327 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002328 if( rc!=SQLITE_OK ){
2329 return rc;
2330 }
danielk197717221812005-02-15 03:38:05 +00002331
2332 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002333 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002334 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002335 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002336 if( rc!=SQLITE_OK ){
2337 return rc;
2338 }
2339
drhcb4c40b2004-08-18 19:09:43 +00002340 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002341 return rc;
2342}
2343
2344/*
drhed7c8552001-04-11 14:29:21 +00002345** Shutdown the page cache. Free all memory and close all files.
2346**
2347** If a transaction was in progress when this routine is called, that
2348** transaction is rolled back. All outstanding pages are invalidated
2349** and their memory is freed. Any attempt to use a page associated
2350** with this page cache after this function returns will likely
2351** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002352**
2353** This function always succeeds. If a transaction is active an attempt
2354** is made to roll it back. If an error occurs during the rollback
2355** a hot journal may be left in the filesystem but no error is returned
2356** to the caller.
drhed7c8552001-04-11 14:29:21 +00002357*/
danielk19773b8a05f2007-03-19 17:44:26 +00002358int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002359#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002360 if( !MEMDB ){
2361 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2362 sqlite3_mutex_enter(mutex);
2363 if( pPager->pPrev ){
2364 pPager->pPrev->pNext = pPager->pNext;
2365 }else{
2366 sqlite3PagerList = pPager->pNext;
2367 }
2368 if( pPager->pNext ){
2369 pPager->pNext->pPrev = pPager->pPrev;
2370 }
2371 sqlite3_mutex_leave(mutex);
2372 }
danielk197713f72992005-12-18 08:51:22 +00002373#endif
2374
drhbafda092007-01-03 23:36:22 +00002375 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002376 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002377 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002378 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002379 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002380 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002381 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002382 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002383 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002384 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002385 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002386 }
drh17435752007-08-16 04:30:38 +00002387 sqlite3_free(pPager->aInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002388 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002389 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002390 }
danielk1977b4b47412007-08-17 15:53:36 +00002391 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002392 /* Temp files are automatically deleted by the OS
2393 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002394 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002395 ** }
2396 */
danielk1977aca790a2005-01-13 11:07:52 +00002397
drh17435752007-08-16 04:30:38 +00002398 sqlite3_free(pPager->aHash);
2399 sqlite3_free(pPager->pTmpSpace);
2400 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002401 return SQLITE_OK;
2402}
2403
drh87cc3b32007-05-08 21:45:27 +00002404#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002405/*
drh5e00f6c2001-09-13 13:46:56 +00002406** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002407*/
danielk19773b8a05f2007-03-19 17:44:26 +00002408Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002409 return p->pgno;
2410}
drh87cc3b32007-05-08 21:45:27 +00002411#endif
drhed7c8552001-04-11 14:29:21 +00002412
2413/*
drhc8629a12004-05-08 20:07:40 +00002414** The page_ref() function increments the reference count for a page.
2415** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002416** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002417**
2418** For non-test systems, page_ref() is a macro that calls _page_ref()
2419** online of the reference count is zero. For test systems, page_ref()
2420** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002421*/
drh836faa42003-01-11 13:30:57 +00002422static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002423 if( pPg->nRef==0 ){
2424 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +00002425 if( pPg==pPg->pPager->pFirstSynced ){
2426 PgHdr *p = pPg->pNextFree;
2427 while( p && p->needSync ){ p = p->pNextFree; }
2428 pPg->pPager->pFirstSynced = p;
2429 }
drh7e3b0a02001-04-28 16:52:40 +00002430 if( pPg->pPrevFree ){
2431 pPg->pPrevFree->pNextFree = pPg->pNextFree;
2432 }else{
2433 pPg->pPager->pFirst = pPg->pNextFree;
2434 }
2435 if( pPg->pNextFree ){
2436 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
2437 }else{
2438 pPg->pPager->pLast = pPg->pPrevFree;
2439 }
2440 pPg->pPager->nRef++;
2441 }
2442 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00002443 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00002444}
danielk1977aca790a2005-01-13 11:07:52 +00002445#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002446 static void page_ref(PgHdr *pPg){
2447 if( pPg->nRef==0 ){
2448 _page_ref(pPg);
2449 }else{
2450 pPg->nRef++;
2451 REFINFO(pPg);
2452 }
2453 }
2454#else
2455# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2456#endif
drhdf0b3b02001-06-23 11:36:20 +00002457
2458/*
2459** Increment the reference count for a page. The input pointer is
2460** a reference to the page data.
2461*/
danielk19773b8a05f2007-03-19 17:44:26 +00002462int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002463 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002464 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002465 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002466 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002467}
2468
2469/*
drh34e79ce2004-02-08 06:05:46 +00002470** Sync the journal. In other words, make sure all the pages that have
2471** been written to the journal have actually reached the surface of the
2472** disk. It is not safe to modify the original database file until after
2473** the journal has been synced. If the original database is modified before
2474** the journal is synced and a power failure occurs, the unsynced journal
2475** data would be lost and we would be unable to completely rollback the
2476** database changes. Database corruption would occur.
2477**
2478** This routine also updates the nRec field in the header of the journal.
2479** (See comments on the pager_playback() routine for additional information.)
2480** If the sync mode is FULL, two syncs will occur. First the whole journal
2481** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002482**
drh34e79ce2004-02-08 06:05:46 +00002483** For temporary databases, we do not care if we are able to rollback
2484** after a power failure, so sync occurs.
drhfa86c412002-02-02 15:01:15 +00002485**
drh34e79ce2004-02-08 06:05:46 +00002486** This routine clears the needSync field of every page current held in
2487** memory.
drh50e5dad2001-09-15 00:57:28 +00002488*/
danielk197776572402004-06-25 02:38:54 +00002489static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002490 PgHdr *pPg;
2491 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002492
2493 /* Sync the journal before modifying the main database
2494 ** (assuming there is a journal and it needs to be synced.)
2495 */
danielk197776572402004-06-25 02:38:54 +00002496 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002497 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00002498 assert( pPager->journalOpen );
drh946966f2004-02-25 02:20:41 +00002499 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2500 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00002501#ifndef NDEBUG
2502 {
drh34e79ce2004-02-08 06:05:46 +00002503 /* Make sure the pPager->nRec counter we are keeping agrees
2504 ** with the nRec computed from the size of the journal file.
2505 */
drheb206252004-10-01 02:00:31 +00002506 i64 jSz;
drh054889e2005-11-30 03:20:31 +00002507 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00002508 if( rc!=0 ) return rc;
danielk197776572402004-06-25 02:38:54 +00002509 assert( pPager->journalOff==jSz );
drh968af522003-02-11 14:55:40 +00002510 }
2511#endif
drhae2b40c2004-06-09 19:03:54 +00002512 {
danielk197762079062007-08-15 17:08:46 +00002513 i64 jrnlOff;
2514
danielk197776572402004-06-25 02:38:54 +00002515 /* Write the nRec value into the journal file header. If in
2516 ** full-synchronous mode, sync the journal first. This ensures that
2517 ** all data has really hit the disk before nRec is updated to mark
2518 ** it as a candidate for rollback.
2519 */
drhd8d66e82003-02-12 02:10:15 +00002520 if( pPager->fullSync ){
drh4f0c5872007-03-26 22:05:01 +00002521 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002522 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002523 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002524 if( rc!=0 ) return rc;
2525 }
danielk197713adf8a2004-06-03 16:08:41 +00002526
danielk197762079062007-08-15 17:08:46 +00002527 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2528 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2529 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002530 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002531 }
drh4f0c5872007-03-26 22:05:01 +00002532 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drh126afe62007-05-04 12:01:02 +00002533 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002534 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2535 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2536 );
drhfa86c412002-02-02 15:01:15 +00002537 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00002538 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002539 }
drh50e5dad2001-09-15 00:57:28 +00002540 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002541
2542 /* Erase the needSync flag from every page.
2543 */
2544 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2545 pPg->needSync = 0;
2546 }
2547 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00002548 }
drh03eb96a2002-11-10 23:32:56 +00002549
drh341eae82003-01-21 02:39:36 +00002550#ifndef NDEBUG
2551 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2552 ** flag must also be clear for all pages. Verify that this
2553 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002554 */
drh341eae82003-01-21 02:39:36 +00002555 else{
2556 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2557 assert( pPg->needSync==0 );
2558 }
2559 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00002560 }
drh341eae82003-01-21 02:39:36 +00002561#endif
drhdb48ee02003-01-16 13:42:43 +00002562
drh81a20f22001-10-12 17:30:04 +00002563 return rc;
drh50e5dad2001-09-15 00:57:28 +00002564}
2565
2566/*
drhdc3e10b2006-06-15 14:31:06 +00002567** Merge two lists of pages connected by pDirty and in pgno order.
2568** Do not both fixing the pPrevDirty pointers.
2569*/
2570static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2571 PgHdr result, *pTail;
2572 pTail = &result;
2573 while( pA && pB ){
2574 if( pA->pgno<pB->pgno ){
2575 pTail->pDirty = pA;
2576 pTail = pA;
2577 pA = pA->pDirty;
2578 }else{
2579 pTail->pDirty = pB;
2580 pTail = pB;
2581 pB = pB->pDirty;
2582 }
2583 }
2584 if( pA ){
2585 pTail->pDirty = pA;
2586 }else if( pB ){
2587 pTail->pDirty = pB;
2588 }else{
2589 pTail->pDirty = 0;
2590 }
2591 return result.pDirty;
2592}
2593
2594/*
2595** Sort the list of pages in accending order by pgno. Pages are
2596** connected by pDirty pointers. The pPrevDirty pointers are
2597** corrupted by this sort.
2598*/
danielk197724168722007-04-02 05:07:47 +00002599#define N_SORT_BUCKET_ALLOC 25
2600#define N_SORT_BUCKET 25
2601#ifdef SQLITE_TEST
2602 int sqlite3_pager_n_sort_bucket = 0;
2603 #undef N_SORT_BUCKET
2604 #define N_SORT_BUCKET \
2605 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2606#endif
drhdc3e10b2006-06-15 14:31:06 +00002607static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002608 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002609 int i;
2610 memset(a, 0, sizeof(a));
2611 while( pIn ){
2612 p = pIn;
2613 pIn = p->pDirty;
2614 p->pDirty = 0;
2615 for(i=0; i<N_SORT_BUCKET-1; i++){
2616 if( a[i]==0 ){
2617 a[i] = p;
2618 break;
2619 }else{
2620 p = merge_pagelist(a[i], p);
2621 a[i] = 0;
2622 }
2623 }
2624 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002625 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2626 ** elements in the input list. This is possible, but impractical.
2627 ** Testing this line is the point of global variable
2628 ** sqlite3_pager_n_sort_bucket.
2629 */
drhdc3e10b2006-06-15 14:31:06 +00002630 a[i] = merge_pagelist(a[i], p);
2631 }
2632 }
2633 p = a[0];
2634 for(i=1; i<N_SORT_BUCKET; i++){
2635 p = merge_pagelist(p, a[i]);
2636 }
2637 return p;
2638}
2639
2640/*
drh2554f8b2003-01-22 01:26:44 +00002641** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2642** every one of those pages out to the database file and mark them all
2643** as clean.
2644*/
2645static int pager_write_pagelist(PgHdr *pList){
2646 Pager *pPager;
2647 int rc;
2648
2649 if( pList==0 ) return SQLITE_OK;
2650 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002651
2652 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2653 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002654 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002655 **
drha6abd042004-06-09 17:37:22 +00002656 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2657 ** through an intermediate state PENDING. A PENDING lock prevents new
2658 ** readers from attaching to the database but is unsufficient for us to
2659 ** write. The idea of a PENDING lock is to prevent new readers from
2660 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002661 **
drha6abd042004-06-09 17:37:22 +00002662 ** While the pager is in the RESERVED state, the original database file
2663 ** is unchanged and we can rollback without having to playback the
2664 ** journal into the original database file. Once we transition to
2665 ** EXCLUSIVE, it means the database file has been changed and any rollback
2666 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002667 */
drh684917c2004-10-05 02:41:42 +00002668 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002669 if( rc!=SQLITE_OK ){
2670 return rc;
2671 }
2672
drhdc3e10b2006-06-15 14:31:06 +00002673 pList = sort_pagelist(pList);
drh2554f8b2003-01-22 01:26:44 +00002674 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002675
2676 /* If the file has not yet been opened, open it now. */
2677 if( !pPager->fd->pMethods ){
2678 assert(pPager->tempFile);
2679 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename);
2680 if( rc ) return rc;
2681 }
2682
drh2554f8b2003-01-22 01:26:44 +00002683 assert( pList->dirty );
danielk1977687566d2004-11-02 12:56:41 +00002684 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002685 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002686 ** make the file smaller (presumably by auto-vacuum code). Do not write
2687 ** any such pages to the file.
2688 */
2689 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00002690 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00002691 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002692 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2693 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002694 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002695 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002696 PAGER_INCR(sqlite3_pager_writedb_count);
2697 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002698 if( pList->pgno==1 ){
2699 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2700 }
danielk1977687566d2004-11-02 12:56:41 +00002701 }
2702#ifndef NDEBUG
2703 else{
drh4f0c5872007-03-26 22:05:01 +00002704 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002705 }
2706#endif
drh2554f8b2003-01-22 01:26:44 +00002707 if( rc ) return rc;
2708 pList->dirty = 0;
danielk19773c407372005-02-15 02:54:14 +00002709#ifdef SQLITE_CHECK_PAGES
2710 pList->pageHash = pager_pagehash(pList);
2711#endif
drh2554f8b2003-01-22 01:26:44 +00002712 pList = pList->pDirty;
2713 }
2714 return SQLITE_OK;
2715}
2716
2717/*
2718** Collect every dirty page into a dirty list and
2719** return a pointer to the head of that list. All pages are
2720** collected even if they are still in use.
2721*/
2722static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh605ad8f2006-05-03 23:34:05 +00002723 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00002724}
2725
2726/*
drh165ffe92005-03-15 17:09:30 +00002727** Return TRUE if there is a hot journal on the given pager.
2728** A hot journal is one that needs to be played back.
2729**
2730** If the current size of the database file is 0 but a journal file
2731** exists, that is probably an old journal left over from a prior
2732** database with the same name. Just delete the journal.
2733*/
2734static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00002735 sqlite3_vfs *pVfs = pPager->pVfs;
drh165ffe92005-03-15 17:09:30 +00002736 if( !pPager->useJournal ) return 0;
danielk1977b4b47412007-08-17 15:53:36 +00002737 if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
drhbb5f18d2007-04-06 18:23:17 +00002738 return 0;
2739 }
2740 if( sqlite3OsCheckReservedLock(pPager->fd) ){
2741 return 0;
2742 }
danielk19773b8a05f2007-03-19 17:44:26 +00002743 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00002744 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00002745 return 0;
2746 }else{
2747 return 1;
2748 }
2749}
2750
danielk19775591df52005-12-20 09:19:37 +00002751/*
danielk1977aef0bf62005-12-30 16:28:01 +00002752** Try to find a page in the cache that can be recycled.
2753**
2754** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00002755** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00002756*/
danielk197713f72992005-12-18 08:51:22 +00002757static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
2758 PgHdr *pPg;
2759 *ppPg = 0;
2760
danielk1977c551edc2007-04-05 13:12:13 +00002761 assert(!MEMDB);
2762
danielk197713f72992005-12-18 08:51:22 +00002763 /* Find a page to recycle. Try to locate a page that does not
2764 ** require us to do an fsync() on the journal.
2765 */
2766 pPg = pPager->pFirstSynced;
2767
2768 /* If we could not find a page that does not require an fsync()
2769 ** on the journal file then fsync the journal file. This is a
2770 ** very slow operation, so we work hard to avoid it. But sometimes
2771 ** it can't be helped.
2772 */
drh8940f4e2007-08-11 00:26:20 +00002773 if( pPg==0 && pPager->pFirst && syncOk && !MEMDB){
danielk197713f72992005-12-18 08:51:22 +00002774 int rc = syncJournal(pPager);
2775 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002776 return rc;
danielk197713f72992005-12-18 08:51:22 +00002777 }
2778 if( pPager->fullSync ){
2779 /* If in full-sync mode, write a new journal header into the
2780 ** journal file. This is done to avoid ever modifying a journal
2781 ** header that is involved in the rollback of pages that have
2782 ** already been written to the database (in case the header is
2783 ** trashed when the nRec field is updated).
2784 */
2785 pPager->nRec = 0;
2786 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00002787 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00002788 rc = writeJournalHdr(pPager);
2789 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002790 return rc;
danielk197713f72992005-12-18 08:51:22 +00002791 }
2792 }
2793 pPg = pPager->pFirst;
2794 }
2795 if( pPg==0 ){
2796 return SQLITE_OK;
2797 }
2798
2799 assert( pPg->nRef==0 );
2800
2801 /* Write the page to the database file if it is dirty.
2802 */
2803 if( pPg->dirty ){
2804 int rc;
2805 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00002806 makeClean(pPg);
2807 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00002808 pPg->pDirty = 0;
2809 rc = pager_write_pagelist( pPg );
2810 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00002811 return rc;
danielk197713f72992005-12-18 08:51:22 +00002812 }
2813 }
2814 assert( pPg->dirty==0 );
2815
2816 /* If the page we are recycling is marked as alwaysRollback, then
2817 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00002818 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00002819 ** It is necessary to do this because the page marked alwaysRollback
2820 ** might be reloaded at a later time but at that point we won't remember
2821 ** that is was marked alwaysRollback. This means that all pages must
2822 ** be marked as alwaysRollback from here on out.
2823 */
2824 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00002825 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00002826 pPager->alwaysRollback = 1;
2827 }
2828
2829 /* Unlink the old page from the free list and the hash table
2830 */
2831 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00002832 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00002833
2834 *ppPg = pPg;
2835 return SQLITE_OK;
2836}
2837
drh86f8c192007-08-22 00:39:19 +00002838#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00002839/*
2840** This function is called to free superfluous dynamically allocated memory
2841** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00002842** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00002843**
2844** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00002845** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00002846** of bytes of memory released.
2847*/
danielk19773b8a05f2007-03-19 17:44:26 +00002848int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00002849 int nReleased = 0; /* Bytes of memory released so far */
2850 sqlite3_mutex *mutex; /* The MEM2 mutex */
2851 Pager *pPager; /* For looping over pagers */
2852 int i; /* Passes over pagers */
danielk197713f72992005-12-18 08:51:22 +00002853
drh86f8c192007-08-22 00:39:19 +00002854 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00002855 */
drh86f8c192007-08-22 00:39:19 +00002856 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2857 sqlite3_mutex_enter(mutex);
2858
2859 /* Signal all database connections that memory management wants
2860 ** to have access to the pagers.
2861 */
2862 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
2863 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00002864 }
2865
danielk197713f72992005-12-18 08:51:22 +00002866 /* Outermost loop runs for at most two iterations. First iteration we
2867 ** try to find memory that can be released without calling fsync(). Second
2868 ** iteration (which only runs if the first failed to free nReq bytes of
2869 ** memory) is permitted to call fsync(). This is of course much more
2870 ** expensive.
2871 */
drh29c636b2006-01-09 23:40:25 +00002872 for(i=0; i<=1; i++){
danielk197713f72992005-12-18 08:51:22 +00002873
2874 /* Loop through all the SQLite pagers opened by the current thread. */
drh86f8c192007-08-22 00:39:19 +00002875 Pager *pPager = sqlite3PagerList;
danielk197738ec0cb2007-04-05 14:29:42 +00002876 for( ; pPager && (nReq<0 || nReleased<nReq); pPager=pPager->pNext){
danielk197713f72992005-12-18 08:51:22 +00002877 PgHdr *pPg;
drhed138fb2007-08-22 22:04:37 +00002878 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00002879
drh86f8c192007-08-22 00:39:19 +00002880 /* In-memory databases should not appear on the pager list */
2881 assert( !MEMDB );
2882
2883 /* Skip pagers that are currently in use by the b-tree layer */
2884 if( pPager->iInUseDB ) continue;
danielk1977c551edc2007-04-05 13:12:13 +00002885
danielk197713f72992005-12-18 08:51:22 +00002886 /* For each pager, try to free as many pages as possible (without
2887 ** calling fsync() if this is the first iteration of the outermost
2888 ** loop).
2889 */
drhed138fb2007-08-22 22:04:37 +00002890 while( (nReq<0 || nReleased<nReq) &&
2891 SQLITE_OK==(rc = pager_recycle(pPager, i, &pPg)) &&
2892 pPg
2893 ) {
danielk1977aef0bf62005-12-30 16:28:01 +00002894 /* We've found a page to free. At this point the page has been
danielk197713f72992005-12-18 08:51:22 +00002895 ** removed from the page hash-table, free-list and synced-list
danielk1977aef0bf62005-12-30 16:28:01 +00002896 ** (pFirstSynced). It is still in the all pages (pAll) list.
danielk197713f72992005-12-18 08:51:22 +00002897 ** Remove it from this list before freeing.
2898 **
2899 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
2900 ** probably is though.
2901 */
2902 PgHdr *pTmp;
danielk19770190d1d2005-12-19 14:18:11 +00002903 assert( pPg );
danielk197738ec0cb2007-04-05 14:29:42 +00002904 if( pPg==pPager->pAll ){
2905 pPager->pAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00002906 }else{
danielk197738ec0cb2007-04-05 14:29:42 +00002907 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
danielk197713f72992005-12-18 08:51:22 +00002908 pTmp->pNextAll = pPg->pNextAll;
2909 }
danielk19771e536952007-08-16 10:09:01 +00002910 nReleased += (
2911 sizeof(*pPg) + pPager->pageSize
2912 + sizeof(u32) + pPager->nExtra
2913 + MEMDB*sizeof(PgHistory)
2914 );
drh34f56212007-08-10 23:56:35 +00002915 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
drh538f5702007-04-13 02:14:30 +00002916 PAGER_INCR(sqlite3_pager_pgfree_count);
drh17435752007-08-16 04:30:38 +00002917 sqlite3_free(pPg);
danielk197713f72992005-12-18 08:51:22 +00002918 }
2919
2920 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00002921 /* An error occured whilst writing to the database file or
2922 ** journal in pager_recycle(). The error is not returned to the
danielk1977efaaf572006-01-16 11:29:19 +00002923 ** caller of this function. Instead, set the Pager.errCode variable.
danielk1977aef0bf62005-12-30 16:28:01 +00002924 ** The error will be returned to the user (or users, in the case
2925 ** of a shared pager cache) of the pager for which the error occured.
danielk197713f72992005-12-18 08:51:22 +00002926 */
drh34f56212007-08-10 23:56:35 +00002927 assert(
2928 (rc&0xff)==SQLITE_IOERR ||
2929 rc==SQLITE_FULL ||
2930 rc==SQLITE_BUSY
2931 );
danielk197738ec0cb2007-04-05 14:29:42 +00002932 assert( pPager->state>=PAGER_RESERVED );
2933 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00002934 }
2935 }
2936 }
danielk1977aef0bf62005-12-30 16:28:01 +00002937
drh86f8c192007-08-22 00:39:19 +00002938 /* Clear the memory management flags and release the mutex
2939 */
2940 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
2941 pPager->iInUseMM = 0;
2942 }
2943 sqlite3_mutex_leave(mutex);
2944
2945 /* Return the number of bytes released
2946 */
danielk197713f72992005-12-18 08:51:22 +00002947 return nReleased;
2948}
drh86f8c192007-08-22 00:39:19 +00002949#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00002950
drh165ffe92005-03-15 17:09:30 +00002951/*
danielk1977e180dd92007-04-05 17:15:52 +00002952** Read the content of page pPg out of the database file.
2953*/
2954static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2955 int rc;
danielk197762079062007-08-15 17:08:46 +00002956 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002957 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002958 assert(pPager->fd->pMethods||pPager->tempFile);
2959 if( !pPager->fd->pMethods ){
2960 return SQLITE_IOERR_SHORT_READ;
2961 }
danielk197762079062007-08-15 17:08:46 +00002962 offset = (pgno-1)*(i64)pPager->pageSize;
2963 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002964 PAGER_INCR(sqlite3_pager_readdb_count);
2965 PAGER_INCR(pPager->nRead);
2966 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002967 if( pgno==1 ){
2968 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
2969 sizeof(pPager->dbFileVers));
2970 }
danielk1977e180dd92007-04-05 17:15:52 +00002971 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00002972 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
2973 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00002974 return rc;
2975}
2976
2977
2978/*
danielk1977e277be02007-03-23 18:12:06 +00002979** This function is called to obtain the shared lock required before
2980** data may be read from the pager cache. If the shared lock has already
2981** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002982**
2983** Immediately after obtaining the shared lock (if required), this function
2984** checks for a hot-journal file. If one is found, an emergency rollback
2985** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002986*/
2987static int pagerSharedLock(Pager *pPager){
2988 int rc = SQLITE_OK;
2989
2990 if( pPager->state==PAGER_UNLOCK ){
danielk1977b4b47412007-08-17 15:53:36 +00002991 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00002992 if( !MEMDB ){
2993 assert( pPager->nRef==0 );
2994 if( !pPager->noReadlock ){
2995 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2996 if( rc!=SQLITE_OK ){
2997 return pager_error(pPager, rc);
2998 }
2999 assert( pPager->state>=SHARED_LOCK );
3000 }
3001
3002 /* If a journal file exists, and there is no RESERVED lock on the
3003 ** database file, then it either needs to be played back or deleted.
3004 */
3005 if( hasHotJournal(pPager) ){
3006 /* Get an EXCLUSIVE lock on the database file. At this point it is
3007 ** important that a RESERVED lock is not obtained on the way to the
3008 ** EXCLUSIVE lock. If it were, another process might open the
3009 ** database file, detect the RESERVED lock, and conclude that the
3010 ** database is safe to read while this process is still rolling it
3011 ** back.
3012 **
3013 ** Because the intermediate RESERVED lock is not requested, the
3014 ** second process will get to this point in the code and fail to
3015 ** obtain it's own EXCLUSIVE lock on the database file.
3016 */
3017 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3018 if( rc!=SQLITE_OK ){
3019 pager_unlock(pPager);
3020 return pager_error(pPager, rc);
3021 }
3022 pPager->state = PAGER_EXCLUSIVE;
3023
3024 /* Open the journal for reading only. Return SQLITE_BUSY if
3025 ** we are unable to open the journal file.
3026 **
3027 ** The journal file does not need to be locked itself. The
3028 ** journal file is never open unless the main database file holds
3029 ** a write lock, so there is never any chance of two or more
3030 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00003031 **
drhfd131da2007-08-07 17:13:03 +00003032 ** Open the journal for read/write access. This is because in
3033 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003034 ** possibly used for a transaction later on. On some systems, the
3035 ** OsTruncate() call used in exclusive-access mode also requires
3036 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003037 */
danielk1977979f38e2007-03-27 16:19:51 +00003038 rc = SQLITE_BUSY;
danielk1977b4b47412007-08-17 15:53:36 +00003039 if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
3040 int fout = 0;
danielk1977fee2d252007-08-18 10:59:19 +00003041 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
danielk19777152de82007-03-29 17:28:14 +00003042 assert( !pPager->tempFile );
danielk1977c7b60172007-08-22 11:22:03 +00003043 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags,&fout);
danielk1977b4b47412007-08-17 15:53:36 +00003044 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3045 if( fout&SQLITE_OPEN_READONLY ){
danielk1977979f38e2007-03-27 16:19:51 +00003046 rc = SQLITE_BUSY;
danielk1977b4b47412007-08-17 15:53:36 +00003047 sqlite3OsClose(pPager->jfd);
danielk1977979f38e2007-03-27 16:19:51 +00003048 }
3049 }
danielk1977e277be02007-03-23 18:12:06 +00003050 if( rc!=SQLITE_OK ){
3051 pager_unlock(pPager);
3052 return SQLITE_BUSY;
3053 }
3054 pPager->journalOpen = 1;
3055 pPager->journalStarted = 0;
3056 pPager->journalOff = 0;
3057 pPager->setMaster = 0;
3058 pPager->journalHdr = 0;
3059
3060 /* Playback and delete the journal. Drop the database write
3061 ** lock and reacquire the read lock.
3062 */
3063 rc = pager_playback(pPager, 1);
3064 if( rc!=SQLITE_OK ){
3065 return pager_error(pPager, rc);
3066 }
danielk1977c5859712007-03-26 12:26:27 +00003067 assert(pPager->state==PAGER_SHARED ||
3068 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3069 );
danielk1977e277be02007-03-23 18:12:06 +00003070 }
3071
3072 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003073 /* The shared-lock has just been acquired on the database file
3074 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003075 ** read or write transaction). Check to see if the database
3076 ** has been modified. If the database has changed, flush the
3077 ** cache.
3078 **
3079 ** Database changes is detected by looking at 15 bytes beginning
3080 ** at offset 24 into the file. The first 4 of these 16 bytes are
3081 ** a 32-bit counter that is incremented with each change. The
3082 ** other bytes change randomly with each file change when
3083 ** a codec is in use.
3084 **
3085 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003086 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003087 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003088 */
drh86a88112007-04-16 15:02:19 +00003089 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003090 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003091
danielk1977e180dd92007-04-05 17:15:52 +00003092 if( pPager->errCode ){
3093 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00003094 }
3095
danielk1977e180dd92007-04-05 17:15:52 +00003096 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003097 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003098 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003099 if( rc!=SQLITE_OK ){
3100 return rc;
3101 }
drh86a88112007-04-16 15:02:19 +00003102 }else{
3103 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003104 }
3105
drh86a88112007-04-16 15:02:19 +00003106 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003107 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003108 }
3109 }
3110 }
danielk1977c5859712007-03-26 12:26:27 +00003111 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3112 if( pPager->state==PAGER_UNLOCK ){
3113 pPager->state = PAGER_SHARED;
3114 }
danielk1977e277be02007-03-23 18:12:06 +00003115 }
3116
3117 return rc;
3118}
3119
3120/*
danielk1977e180dd92007-04-05 17:15:52 +00003121** Allocate a PgHdr object. Either create a new one or reuse
3122** an existing one that is not otherwise in use.
3123**
3124** A new PgHdr structure is created if any of the following are
3125** true:
3126**
3127** (1) We have not exceeded our maximum allocated cache size
3128** as set by the "PRAGMA cache_size" command.
3129**
3130** (2) There are no unused PgHdr objects available at this time.
3131**
3132** (3) This is an in-memory database.
3133**
3134** (4) There are no PgHdr objects that do not require a journal
3135** file sync and a sync of the journal file is currently
3136** prohibited.
3137**
3138** Otherwise, reuse an existing PgHdr. In other words, reuse an
3139** existing PgHdr if all of the following are true:
3140**
3141** (1) We have reached or exceeded the maximum cache size
3142** allowed by "PRAGMA cache_size".
3143**
3144** (2) There is a PgHdr available with PgHdr->nRef==0
3145**
3146** (3) We are not in an in-memory database
3147**
3148** (4) Either there is an available PgHdr that does not need
3149** to be synced to disk or else disk syncing is currently
3150** allowed.
danielk197724168722007-04-02 05:07:47 +00003151*/
3152static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3153 int rc = SQLITE_OK;
3154 PgHdr *pPg;
3155
danielk1977e180dd92007-04-05 17:15:52 +00003156 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003157 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003158 if( pPager->nPage<pPager->mxPage
3159 || pPager->pFirst==0
3160 || MEMDB
3161 || (pPager->pFirstSynced==0 && pPager->doNotSync)
3162 ){
danielk197724168722007-04-02 05:07:47 +00003163 if( pPager->nPage>=pPager->nHash ){
3164 pager_resize_hash_table(pPager,
3165 pPager->nHash<256 ? 256 : pPager->nHash*2);
3166 if( pPager->nHash==0 ){
3167 rc = SQLITE_NOMEM;
3168 goto pager_allocate_out;
3169 }
3170 }
drhed138fb2007-08-22 22:04:37 +00003171 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003172 pPg = sqlite3_malloc( sizeof(*pPg) + pPager->pageSize
danielk197724168722007-04-02 05:07:47 +00003173 + sizeof(u32) + pPager->nExtra
3174 + MEMDB*sizeof(PgHistory) );
drhed138fb2007-08-22 22:04:37 +00003175 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003176 if( pPg==0 ){
3177 rc = SQLITE_NOMEM;
3178 goto pager_allocate_out;
3179 }
3180 memset(pPg, 0, sizeof(*pPg));
3181 if( MEMDB ){
3182 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
3183 }
3184 pPg->pPager = pPager;
3185 pPg->pNextAll = pPager->pAll;
3186 pPager->pAll = pPg;
3187 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003188 }else{
3189 /* Recycle an existing page with a zero ref-count. */
3190 rc = pager_recycle(pPager, 1, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003191 if( rc==SQLITE_BUSY ){
3192 rc = SQLITE_IOERR_BLOCKED;
3193 }
danielk197724168722007-04-02 05:07:47 +00003194 if( rc!=SQLITE_OK ){
3195 goto pager_allocate_out;
3196 }
3197 assert( pPager->state>=SHARED_LOCK );
3198 assert(pPg);
3199 }
3200 *ppPg = pPg;
3201
3202pager_allocate_out:
3203 return rc;
3204}
3205
3206/*
drhd33d5a82007-04-26 12:11:28 +00003207** Make sure we have the content for a page. If the page was
3208** previously acquired with noContent==1, then the content was
3209** just initialized to zeros instead of being read from disk.
3210** But now we need the real data off of disk. So make sure we
3211** have it. Read it in if we do not have it already.
3212*/
3213static int pager_get_content(PgHdr *pPg){
3214 if( pPg->needRead ){
3215 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3216 if( rc==SQLITE_OK ){
3217 pPg->needRead = 0;
3218 }else{
3219 return rc;
3220 }
3221 }
3222 return SQLITE_OK;
3223}
3224
3225/*
drhd9b02572001-04-15 00:37:09 +00003226** Acquire a page.
3227**
drh58a11682001-11-10 13:51:08 +00003228** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003229** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003230**
drhd33d5a82007-04-26 12:11:28 +00003231** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003232** file is smaller than the requested page, then no actual disk
3233** read occurs and the memory image of the page is initialized to
3234** all zeros. The extra data appended to a page is always initialized
3235** to zeros the first time a page is loaded into memory.
3236**
drhd9b02572001-04-15 00:37:09 +00003237** The acquisition might fail for several reasons. In all cases,
3238** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003239**
drhd33d5a82007-04-26 12:11:28 +00003240** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003241** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003242** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003243** just returns 0. This routine acquires a read-lock the first time it
3244** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003245** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003246** or journal files.
drh0787db62007-03-04 13:15:27 +00003247**
drh538f5702007-04-13 02:14:30 +00003248** If noContent is false, the page contents are actually read from disk.
3249** If noContent is true, it means that we do not care about the contents
3250** of the page at this time, so do not do a disk read. Just fill in the
3251** page content with zeros. But mark the fact that we have not read the
3252** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003253** sqlite3PagerWrite() is called on this page or if this routine is
3254** called again with noContent==0, that means that the content is needed
3255** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003256*/
drh86f8c192007-08-22 00:39:19 +00003257static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003258 Pager *pPager, /* The pager open on the database file */
3259 Pgno pgno, /* Page number to fetch */
3260 DbPage **ppPage, /* Write a pointer to the page here */
3261 int noContent /* Do not bother reading content from disk if true */
3262){
drhed7c8552001-04-11 14:29:21 +00003263 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003264 int rc;
drhed7c8552001-04-11 14:29:21 +00003265
danielk1977e277be02007-03-23 18:12:06 +00003266 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3267
danielk197726836652005-01-17 01:33:13 +00003268 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3269 ** number greater than this, or zero, is requested.
3270 */
drh267cb322005-09-16 17:16:52 +00003271 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003272 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003273 }
3274
drhd9b02572001-04-15 00:37:09 +00003275 /* Make sure we have not hit any critical errors.
3276 */
drh836faa42003-01-11 13:30:57 +00003277 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003278 *ppPage = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003279 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3280 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003281 }
3282
danielk197713adf8a2004-06-03 16:08:41 +00003283 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003284 ** on the database file. pagerSharedLock() is a no-op if
3285 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003286 */
danielk1977e277be02007-03-23 18:12:06 +00003287 rc = pagerSharedLock(pPager);
3288 if( rc!=SQLITE_OK ){
3289 return rc;
drhed7c8552001-04-11 14:29:21 +00003290 }
danielk1977e277be02007-03-23 18:12:06 +00003291 assert( pPager->state!=PAGER_UNLOCK );
3292
3293 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003294 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003295 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003296 int nMax;
drhed7c8552001-04-11 14:29:21 +00003297 int h;
drh538f5702007-04-13 02:14:30 +00003298 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003299 rc = pagerAllocatePage(pPager, &pPg);
3300 if( rc!=SQLITE_OK ){
3301 return rc;
drhed7c8552001-04-11 14:29:21 +00003302 }
danielk197724168722007-04-02 05:07:47 +00003303
drhed7c8552001-04-11 14:29:21 +00003304 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003305 assert( !MEMDB || pgno>pPager->stmtSize );
drh1ab43002002-01-14 09:28:19 +00003306 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19771e536952007-08-16 10:09:01 +00003307#if 0
danielk19774adee202004-05-08 08:23:19 +00003308 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
danielk19771e536952007-08-16 10:09:01 +00003309#endif
drhdb48ee02003-01-16 13:42:43 +00003310 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00003311 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00003312 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003313 }else{
3314 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00003315 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003316 }
danielk1977f35843b2007-04-07 15:03:17 +00003317
drh605ad8f2006-05-03 23:34:05 +00003318 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003319 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00003320 REFINFO(pPg);
danielk197775bab7d2006-01-23 13:09:45 +00003321
drhd9b02572001-04-15 00:37:09 +00003322 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003323 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003324 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003325 }
danielk1977979f38e2007-03-27 16:19:51 +00003326 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003327 if( pPager->errCode ){
danielk19773b8a05f2007-03-19 17:44:26 +00003328 sqlite3PagerUnref(pPg);
danielk1977efaaf572006-01-16 11:29:19 +00003329 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00003330 return rc;
3331 }
danielk197775bab7d2006-01-23 13:09:45 +00003332
3333 /* Populate the page with data, either by reading from the database
3334 ** file, or by setting the entire page to zero.
3335 */
drhd215acf2007-04-13 04:01:58 +00003336 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003337 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003338 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003339 return SQLITE_FULL;
3340 }
drh90f5ecb2004-07-22 01:19:35 +00003341 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003342 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003343 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003344 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003345 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003346 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3347 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003348 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003349 return rc;
drh81a20f22001-10-12 17:30:04 +00003350 }
danielk1977b4626a32007-04-28 15:47:43 +00003351 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003352 }
danielk197775bab7d2006-01-23 13:09:45 +00003353
3354 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003355 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003356 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003357 pPg->pNextHash = pPager->aHash[h];
3358 pPager->aHash[h] = pPg;
3359 if( pPg->pNextHash ){
3360 assert( pPg->pNextHash->pPrevHash==0 );
3361 pPg->pNextHash->pPrevHash = pPg;
3362 }
3363
danielk19773c407372005-02-15 02:54:14 +00003364#ifdef SQLITE_CHECK_PAGES
3365 pPg->pageHash = pager_pagehash(pPg);
3366#endif
drhed7c8552001-04-11 14:29:21 +00003367 }else{
drhd9b02572001-04-15 00:37:09 +00003368 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003369 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003370 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003371 if( !noContent ){
3372 rc = pager_get_content(pPg);
3373 if( rc ){
3374 return rc;
3375 }
3376 }
drhdf0b3b02001-06-23 11:36:20 +00003377 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003378 }
danielk19773b8a05f2007-03-19 17:44:26 +00003379 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003380 return SQLITE_OK;
3381}
drh86f8c192007-08-22 00:39:19 +00003382int sqlite3PagerAcquire(
3383 Pager *pPager, /* The pager open on the database file */
3384 Pgno pgno, /* Page number to fetch */
3385 DbPage **ppPage, /* Write a pointer to the page here */
3386 int noContent /* Do not bother reading content from disk if true */
3387){
3388 int rc;
3389 pagerEnter(pPager);
3390 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3391 pagerLeave(pPager);
3392 return rc;
3393}
3394
drhed7c8552001-04-11 14:29:21 +00003395
3396/*
drh7e3b0a02001-04-28 16:52:40 +00003397** Acquire a page if it is already in the in-memory cache. Do
3398** not read the page from disk. Return a pointer to the page,
3399** or 0 if the page is not in cache.
3400**
danielk19773b8a05f2007-03-19 17:44:26 +00003401** See also sqlite3PagerGet(). The difference between this routine
3402** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003403** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003404** returns NULL if the page is not in cache or if a disk I/O error
3405** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003406*/
danielk19773b8a05f2007-03-19 17:44:26 +00003407DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003408 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003409
drh836faa42003-01-11 13:30:57 +00003410 assert( pPager!=0 );
3411 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003412
drh86f8c192007-08-22 00:39:19 +00003413 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003414 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003415 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003416 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3417 /* Do nothing */
3418 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3419 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003420 }
drh86f8c192007-08-22 00:39:19 +00003421 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003422 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003423}
3424
3425/*
drhed7c8552001-04-11 14:29:21 +00003426** Release a page.
3427**
3428** If the number of references to the page drop to zero, then the
3429** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003430** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003431** removed.
3432*/
danielk19773b8a05f2007-03-19 17:44:26 +00003433int sqlite3PagerUnref(DbPage *pPg){
drhd9b02572001-04-15 00:37:09 +00003434
3435 /* Decrement the reference count for this page
3436 */
drhed7c8552001-04-11 14:29:21 +00003437 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003438 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003439 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00003440 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00003441
danielk19773c407372005-02-15 02:54:14 +00003442 CHECK_PAGE(pPg);
3443
drh72f82862001-05-24 21:06:34 +00003444 /* When the number of references to a page reach 0, call the
3445 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003446 */
drhed7c8552001-04-11 14:29:21 +00003447 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00003448 Pager *pPager;
3449 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003450 pPg->pNextFree = 0;
3451 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00003452 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00003453 if( pPg->pPrevFree ){
3454 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00003455 }else{
3456 pPager->pFirst = pPg;
3457 }
drh341eae82003-01-21 02:39:36 +00003458 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
3459 pPager->pFirstSynced = pPg;
3460 }
drh72f82862001-05-24 21:06:34 +00003461 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003462 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003463 }
drhd9b02572001-04-15 00:37:09 +00003464
3465 /* When all pages reach the freelist, drop the read lock from
3466 ** the database file.
3467 */
3468 pPager->nRef--;
3469 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003470 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003471 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003472 }
drhed7c8552001-04-11 14:29:21 +00003473 }
drh86f8c192007-08-22 00:39:19 +00003474 pagerLeave(pPg->pPager);
drhd9b02572001-04-15 00:37:09 +00003475 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003476}
3477
3478/*
drha6abd042004-06-09 17:37:22 +00003479** Create a journal file for pPager. There should already be a RESERVED
3480** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003481**
3482** Return SQLITE_OK if everything. Return an error code and release the
3483** write lock if anything goes wrong.
3484*/
3485static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003486 sqlite3_vfs *pVfs = pPager->pVfs;
3487 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3488
drhda47d772002-12-02 04:25:19 +00003489 int rc;
drhb7f91642004-10-31 02:22:47 +00003490 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003491 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003492 assert( pPager->journalOpen==0 );
3493 assert( pPager->useJournal );
drh88b01a12005-03-28 18:04:27 +00003494 assert( pPager->aInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003495 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003496 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003497 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00003498 pagerEnter(pPager);
drhda47d772002-12-02 04:25:19 +00003499 if( pPager->aInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003500 rc = SQLITE_NOMEM;
3501 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003502 }
danielk1977b4b47412007-08-17 15:53:36 +00003503
3504 if( pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00003505 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3506 }else{
3507 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
danielk1977b4b47412007-08-17 15:53:36 +00003508 }
danielk1977c7b60172007-08-22 11:22:03 +00003509#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3510 rc = sqlite3JournalOpen(
3511 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3512 );
3513#else
danielk1977b4b47412007-08-17 15:53:36 +00003514 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
danielk1977c7b60172007-08-22 11:22:03 +00003515#endif
danielk1977b4b47412007-08-17 15:53:36 +00003516 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk197776572402004-06-25 02:38:54 +00003517 pPager->journalOff = 0;
3518 pPager->setMaster = 0;
3519 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003520 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003521 if( rc==SQLITE_NOMEM ){
danielk1977fee2d252007-08-18 10:59:19 +00003522 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh600e46a2007-03-28 01:59:33 +00003523 }
drh9c105bb2004-10-02 20:38:28 +00003524 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003525 }
3526 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003527 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003528 pPager->needSync = 0;
3529 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003530 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003531 if( pPager->errCode ){
3532 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003533 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003534 }
drhda47d772002-12-02 04:25:19 +00003535 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003536
danielk197776572402004-06-25 02:38:54 +00003537 rc = writeJournalHdr(pPager);
3538
drhac69b052004-05-12 13:30:07 +00003539 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003540 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003541 }
danielk1977261919c2005-12-06 12:52:59 +00003542 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003543 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003544 if( rc==SQLITE_OK ){
3545 rc = SQLITE_FULL;
3546 }
3547 }
drh9c105bb2004-10-02 20:38:28 +00003548 return rc;
3549
3550failed_to_open_journal:
drh17435752007-08-16 04:30:38 +00003551 sqlite3_free(pPager->aInJournal);
drh9c105bb2004-10-02 20:38:28 +00003552 pPager->aInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003553 return rc;
drhda47d772002-12-02 04:25:19 +00003554}
3555
3556/*
drh4b845d72002-03-05 12:41:19 +00003557** Acquire a write-lock on the database. The lock is removed when
3558** the any of the following happen:
3559**
drh80e35f42007-03-30 14:06:34 +00003560** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003561** * sqlite3PagerRollback() is called.
3562** * sqlite3PagerClose() is called.
3563** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003564**
danielk197713adf8a2004-06-03 16:08:41 +00003565** The first parameter to this routine is a pointer to any open page of the
3566** database file. Nothing changes about the page - it is used merely to
3567** acquire a pointer to the Pager structure and as proof that there is
3568** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003569**
danielk197713adf8a2004-06-03 16:08:41 +00003570** The second parameter indicates how much space in bytes to reserve for a
3571** master journal file-name at the start of the journal when it is created.
3572**
3573** A journal file is opened if this is not a temporary file. For temporary
3574** files, the opening of the journal file is deferred until there is an
3575** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003576**
drha6abd042004-06-09 17:37:22 +00003577** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003578**
3579** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3580** immediately instead of waiting until we try to flush the cache. The
3581** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003582*/
danielk19773b8a05f2007-03-19 17:44:26 +00003583int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003584 Pager *pPager = pPg->pPager;
3585 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003586 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003587 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003588 assert( pPager->state!=PAGER_UNLOCK );
3589 if( pPager->state==PAGER_SHARED ){
drh4b845d72002-03-05 12:41:19 +00003590 assert( pPager->aInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003591 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003592 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003593 pPager->origDbSize = pPager->dbSize;
3594 }else{
drh054889e2005-11-30 03:20:31 +00003595 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003596 if( rc==SQLITE_OK ){
3597 pPager->state = PAGER_RESERVED;
3598 if( exFlag ){
3599 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3600 }
3601 }
danielk19779eed5052004-06-04 10:38:30 +00003602 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003603 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003604 return rc;
drhac69b052004-05-12 13:30:07 +00003605 }
drha6abd042004-06-09 17:37:22 +00003606 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003607 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003608 if( pPager->useJournal && !pPager->tempFile ){
3609 rc = pager_open_journal(pPager);
3610 }
drh4b845d72002-03-05 12:41:19 +00003611 }
danielk1977334cdb62007-03-26 08:05:12 +00003612 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3613 /* This happens when the pager was in exclusive-access mode last
3614 ** time a (read or write) transaction was successfully concluded
3615 ** by this connection. Instead of deleting the journal file it was
3616 ** kept open and truncated to 0 bytes.
3617 */
3618 assert( pPager->nRec==0 );
3619 assert( pPager->origDbSize==0 );
drh369339d2007-03-30 16:01:55 +00003620 assert( pPager->aInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003621 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003622 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003623 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00003624 pagerEnter(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00003625 if( !pPager->aInJournal ){
3626 rc = SQLITE_NOMEM;
3627 }else{
drh369339d2007-03-30 16:01:55 +00003628 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003629 rc = writeJournalHdr(pPager);
3630 }
drh4b845d72002-03-05 12:41:19 +00003631 }
danielk1977334cdb62007-03-26 08:05:12 +00003632 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00003633 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00003634 return rc;
3635}
3636
3637/*
drh605ad8f2006-05-03 23:34:05 +00003638** Make a page dirty. Set its dirty flag and add it to the dirty
3639** page list.
3640*/
3641static void makeDirty(PgHdr *pPg){
3642 if( pPg->dirty==0 ){
3643 Pager *pPager = pPg->pPager;
3644 pPg->dirty = 1;
3645 pPg->pDirty = pPager->pDirty;
3646 if( pPager->pDirty ){
3647 pPager->pDirty->pPrevDirty = pPg;
3648 }
3649 pPg->pPrevDirty = 0;
3650 pPager->pDirty = pPg;
3651 }
3652}
3653
3654/*
3655** Make a page clean. Clear its dirty bit and remove it from the
3656** dirty page list.
3657*/
3658static void makeClean(PgHdr *pPg){
3659 if( pPg->dirty ){
3660 pPg->dirty = 0;
3661 if( pPg->pDirty ){
3662 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
3663 }
3664 if( pPg->pPrevDirty ){
3665 pPg->pPrevDirty->pDirty = pPg->pDirty;
3666 }else{
3667 pPg->pPager->pDirty = pPg->pDirty;
3668 }
3669 }
3670}
3671
3672
3673/*
drhed7c8552001-04-11 14:29:21 +00003674** Mark a data page as writeable. The page is written into the journal
3675** if it is not there already. This routine must be called before making
3676** changes to a page.
3677**
3678** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003679** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003680** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003681** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003682** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003683**
3684** If the journal file could not be written because the disk is full,
3685** then this routine returns SQLITE_FULL and does an immediate rollback.
3686** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003687** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003688** reset.
drhed7c8552001-04-11 14:29:21 +00003689*/
danielk19773b8a05f2007-03-19 17:44:26 +00003690static int pager_write(PgHdr *pPg){
3691 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00003692 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003693 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003694
drh6446c4d2001-12-15 14:22:18 +00003695 /* Check for errors
3696 */
danielk1977efaaf572006-01-16 11:29:19 +00003697 if( pPager->errCode ){
3698 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003699 }
drh5e00f6c2001-09-13 13:46:56 +00003700 if( pPager->readOnly ){
3701 return SQLITE_PERM;
3702 }
drh6446c4d2001-12-15 14:22:18 +00003703
danielk197776572402004-06-25 02:38:54 +00003704 assert( !pPager->setMaster );
3705
danielk19773c407372005-02-15 02:54:14 +00003706 CHECK_PAGE(pPg);
3707
drh538f5702007-04-13 02:14:30 +00003708 /* If this page was previously acquired with noContent==1, that means
3709 ** we didn't really read in the content of the page. This can happen
3710 ** (for example) when the page is being moved to the freelist. But
3711 ** now we are (perhaps) moving the page off of the freelist for
3712 ** reuse and we need to know its original content so that content
3713 ** can be stored in the rollback journal. So do the read at this
3714 ** time.
3715 */
drhd33d5a82007-04-26 12:11:28 +00003716 rc = pager_get_content(pPg);
3717 if( rc ){
3718 return rc;
drh538f5702007-04-13 02:14:30 +00003719 }
3720
drh6446c4d2001-12-15 14:22:18 +00003721 /* Mark the page as dirty. If the page has already been written
3722 ** to the journal then we can return right away.
3723 */
drh605ad8f2006-05-03 23:34:05 +00003724 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00003725 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00003726 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003727 }else{
drh6446c4d2001-12-15 14:22:18 +00003728
danielk1977a0bf2652004-11-04 14:30:04 +00003729 /* If we get this far, it means that the page needs to be
3730 ** written to the transaction journal or the ckeckpoint journal
3731 ** or both.
3732 **
3733 ** First check to see that the transaction journal exists and
3734 ** create it if it does not.
3735 */
3736 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003737 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003738 if( rc!=SQLITE_OK ){
3739 return rc;
3740 }
3741 assert( pPager->state>=PAGER_RESERVED );
3742 if( !pPager->journalOpen && pPager->useJournal ){
3743 rc = pager_open_journal(pPager);
3744 if( rc!=SQLITE_OK ) return rc;
3745 }
3746 assert( pPager->journalOpen || !pPager->useJournal );
3747 pPager->dirtyCache = 1;
3748
3749 /* The transaction journal now exists and we have a RESERVED or an
3750 ** EXCLUSIVE lock on the main database file. Write the current page to
3751 ** the transaction journal if it is not there already.
3752 */
3753 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
3754 if( (int)pPg->pgno <= pPager->origDbSize ){
3755 int szPg;
danielk1977a0bf2652004-11-04 14:30:04 +00003756 if( MEMDB ){
3757 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00003758 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00003759 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00003760 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003761 if( pHist->pOrig ){
3762 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
3763 }
3764 }else{
drhc001c582006-03-06 18:23:16 +00003765 u32 cksum, saved;
3766 char *pData2, *pEnd;
danielk1977dd97a492007-08-22 18:54:32 +00003767
drh267cb322005-09-16 17:16:52 +00003768 /* We should never write to the journal file the page that
3769 ** contains the database locks. The following assert verifies
3770 ** that we do not. */
3771 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00003772 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00003773 cksum = pager_cksum(pPager, (u8*)pData2);
drhc001c582006-03-06 18:23:16 +00003774 pEnd = pData2 + pPager->pageSize;
3775 pData2 -= 4;
3776 saved = *(u32*)pEnd;
3777 put32bits(pEnd, cksum);
danielk1977a0bf2652004-11-04 14:30:04 +00003778 szPg = pPager->pageSize+8;
drhc001c582006-03-06 18:23:16 +00003779 put32bits(pData2, pPg->pgno);
danielk197762079062007-08-15 17:08:46 +00003780 rc = sqlite3OsWrite(pPager->jfd, pData2, szPg, pPager->journalOff);
drhb0603412007-02-28 04:47:26 +00003781 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
drh538f5702007-04-13 02:14:30 +00003782 pPager->journalOff, szPg));
3783 PAGER_INCR(sqlite3_pager_writej_count);
danielk1977a0bf2652004-11-04 14:30:04 +00003784 pPager->journalOff += szPg;
drh477731b2007-06-16 03:06:27 +00003785 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
3786 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
drhc001c582006-03-06 18:23:16 +00003787 *(u32*)pEnd = saved;
danielk197707cb5602006-01-20 10:55:05 +00003788
drhfd131da2007-08-07 17:13:03 +00003789 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00003790 ** transaction will be rolled back by the layer above.
3791 */
danielk1977a0bf2652004-11-04 14:30:04 +00003792 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00003793 return rc;
3794 }
danielk197707cb5602006-01-20 10:55:05 +00003795
danielk1977a0bf2652004-11-04 14:30:04 +00003796 pPager->nRec++;
3797 assert( pPager->aInJournal!=0 );
3798 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
3799 pPg->needSync = !pPager->noSync;
3800 if( pPager->stmtInUse ){
3801 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
danielk1977a0bf2652004-11-04 14:30:04 +00003802 }
drhac69b052004-05-12 13:30:07 +00003803 }
danielk197713adf8a2004-06-03 16:08:41 +00003804 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00003805 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00003806 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00003807 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00003808 }
3809 if( pPg->needSync ){
3810 pPager->needSync = 1;
3811 }
3812 pPg->inJournal = 1;
3813 }
3814
3815 /* If the statement journal is open and the page is not in it,
3816 ** then write the current page to the statement journal. Note that
3817 ** the statement journal format differs from the standard journal format
3818 ** in that it omits the checksums and the header.
3819 */
danielk1977f35843b2007-04-07 15:03:17 +00003820 if( pPager->stmtInUse
3821 && !pageInStatement(pPg)
3822 && (int)pPg->pgno<=pPager->stmtSize
3823 ){
danielk1977a0bf2652004-11-04 14:30:04 +00003824 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
3825 if( MEMDB ){
3826 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
3827 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00003828 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003829 if( pHist->pStmt ){
3830 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
3831 }
drh4f0c5872007-03-26 22:05:01 +00003832 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00003833 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00003834 }else{
danielk197762079062007-08-15 17:08:46 +00003835 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhc001c582006-03-06 18:23:16 +00003836 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4;
3837 put32bits(pData2, pPg->pgno);
danielk197762079062007-08-15 17:08:46 +00003838 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4, offset);
drh4f0c5872007-03-26 22:05:01 +00003839 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00003840 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00003841 return rc;
3842 }
danielk1977a0bf2652004-11-04 14:30:04 +00003843 pPager->stmtNRec++;
3844 assert( pPager->aInStmt!=0 );
3845 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhdb48ee02003-01-16 13:42:43 +00003846 }
drhd9b02572001-04-15 00:37:09 +00003847 }
drhfa86c412002-02-02 15:01:15 +00003848 }
3849
3850 /* Update the database size and return.
3851 */
drh1aa2d8b2007-01-03 15:34:29 +00003852 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00003853 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003854 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00003855 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00003856 pPager->dbSize++;
3857 }
drh306dc212001-05-21 13:45:10 +00003858 }
drh69688d52001-04-14 16:38:23 +00003859 return rc;
drhed7c8552001-04-11 14:29:21 +00003860}
3861
3862/*
danielk19774099f6e2007-03-19 11:25:20 +00003863** This function is used to mark a data-page as writable. It uses
3864** pager_write() to open a journal file (if it is not already open)
3865** and write the page *pData to the journal.
3866**
3867** The difference between this function and pager_write() is that this
3868** function also deals with the special case where 2 or more pages
3869** fit on a single disk sector. In this case all co-resident pages
3870** must have been written to the journal file before returning.
3871*/
danielk19773b8a05f2007-03-19 17:44:26 +00003872int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003873 int rc = SQLITE_OK;
3874
danielk19773b8a05f2007-03-19 17:44:26 +00003875 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003876 Pager *pPager = pPg->pPager;
3877 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3878
drh86f8c192007-08-22 00:39:19 +00003879 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003880 if( !MEMDB && nPagePerSector>1 ){
3881 Pgno nPageCount; /* Total number of pages in database file */
3882 Pgno pg1; /* First page of the sector pPg is located on. */
3883 int nPage; /* Number of pages starting at pg1 to journal */
3884 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003885 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003886
3887 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3888 ** header to be written between the pages journaled by this function.
3889 */
3890 assert( pPager->doNotSync==0 );
3891 pPager->doNotSync = 1;
3892
3893 /* This trick assumes that both the page-size and sector-size are
3894 ** an integer power of 2. It sets variable pg1 to the identifier
3895 ** of the first page of the sector pPg is located on.
3896 */
3897 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3898
danielk19773b8a05f2007-03-19 17:44:26 +00003899 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003900 if( pPg->pgno>nPageCount ){
3901 nPage = (pPg->pgno - pg1)+1;
3902 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3903 nPage = nPageCount+1-pg1;
3904 }else{
3905 nPage = nPagePerSector;
3906 }
3907 assert(nPage>0);
3908 assert(pg1<=pPg->pgno);
3909 assert((pg1+nPage)>pPg->pgno);
3910
3911 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3912 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003913 PgHdr *pPage;
danielk19774099f6e2007-03-19 11:25:20 +00003914 if( !pPager->aInJournal || pg==pPg->pgno ||
3915 pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
3916 ) {
3917 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003918 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003919 if( rc==SQLITE_OK ){
3920 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00003921 if( pPage->needSync ){
3922 needSync = 1;
3923 }
danielk19773b8a05f2007-03-19 17:44:26 +00003924 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003925 }
3926 }
danielk1977dd97a492007-08-22 18:54:32 +00003927 }else if( (pPage = pager_lookup(pPager, pg)) ){
3928 if( pPage->needSync ){
3929 needSync = 1;
3930 }
danielk19774099f6e2007-03-19 11:25:20 +00003931 }
3932 }
3933
danielk1977dd97a492007-08-22 18:54:32 +00003934 /* If the PgHdr.needSync flag is set for any of the nPage pages
3935 ** starting at pg1, then it needs to be set for all of them. Because
3936 ** writing to any of these nPage pages may damage the others, the
3937 ** journal file must contain sync()ed copies of all of them
3938 ** before any of them can be written out to the database file.
3939 */
3940 if( needSync ){
3941 for(ii=0; ii<nPage && needSync; ii++){
3942 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
3943 if( pPage ) pPage->needSync = 1;
3944 }
3945 assert(pPager->needSync);
3946 }
3947
danielk19774099f6e2007-03-19 11:25:20 +00003948 assert( pPager->doNotSync==1 );
3949 pPager->doNotSync = 0;
3950 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003951 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003952 }
drh86f8c192007-08-22 00:39:19 +00003953 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003954 return rc;
3955}
3956
3957/*
drhaacc5432002-01-06 17:07:40 +00003958** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003959** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003960** to change the content of the page.
3961*/
danielk19777d3a6662006-01-23 16:21:05 +00003962#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003963int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00003964 return pPg->dirty;
3965}
danielk19777d3a6662006-01-23 16:21:05 +00003966#endif
drh6019e162001-07-02 17:51:45 +00003967
danielk1977b84f96f2005-01-20 11:32:23 +00003968#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00003969/*
drh001bbcb2003-03-19 03:14:00 +00003970** Replace the content of a single page with the information in the third
3971** argument.
3972*/
danielk19773b8a05f2007-03-19 17:44:26 +00003973int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
3974 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00003975 int rc;
3976
drh86f8c192007-08-22 00:39:19 +00003977 pagerEnter(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003978 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00003979 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003980 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00003981 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003982 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00003983 }
danielk19773b8a05f2007-03-19 17:44:26 +00003984 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00003985 }
drh86f8c192007-08-22 00:39:19 +00003986 pagerLeave(pPager);
drh001bbcb2003-03-19 03:14:00 +00003987 return rc;
3988}
danielk1977b84f96f2005-01-20 11:32:23 +00003989#endif
drh001bbcb2003-03-19 03:14:00 +00003990
3991/*
drh30e58752002-03-02 20:41:57 +00003992** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003993** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00003994** that page might be marked as dirty.
3995**
3996** The overlying software layer calls this routine when all of the data
3997** on the given page is unused. The pager marks the page as clean so
3998** that it does not get written to disk.
3999**
4000** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004001** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004002** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004003**
4004** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004005** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004006** will thereafter be ignored. This is necessary to avoid a problem
4007** where a page with data is added to the freelist during one part of
4008** a transaction then removed from the freelist during a later part
4009** of the same transaction and reused for some other purpose. When it
4010** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004011** the sqlite3PagerDontRollback() routine is called. But because the
4012** page contains critical data, we still need to be sure it gets
4013** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004014*/
drh538f5702007-04-13 02:14:30 +00004015void sqlite3PagerDontWrite(DbPage *pDbPage){
4016 PgHdr *pPg = pDbPage;
4017 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004018
drhb7f91642004-10-31 02:22:47 +00004019 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004020 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004021 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004022 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004023 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004024 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4025 /* If this pages is the last page in the file and the file has grown
4026 ** during the current transaction, then do NOT mark the page as clean.
4027 ** When the database file grows, we must make sure that the last page
4028 ** gets written at least once so that the disk file will be the correct
4029 ** size. If you do not write this page and the size of the file
4030 ** on the disk ends up being too small, that can lead to database
4031 ** corruption during the next transaction.
4032 */
4033 }else{
drh538f5702007-04-13 02:14:30 +00004034 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4035 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004036 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004037#ifdef SQLITE_CHECK_PAGES
4038 pPg->pageHash = pager_pagehash(pPg);
4039#endif
drh8124a302002-06-25 14:43:57 +00004040 }
drh30e58752002-03-02 20:41:57 +00004041 }
drh86f8c192007-08-22 00:39:19 +00004042 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004043}
4044
4045/*
4046** A call to this routine tells the pager that if a rollback occurs,
4047** it is not necessary to restore the data on the given page. This
4048** means that the pager does not have to record the given page in the
4049** rollback journal.
drh538f5702007-04-13 02:14:30 +00004050**
4051** If we have not yet actually read the content of this page (if
4052** the PgHdr.needRead flag is set) then this routine acts as a promise
4053** that we will never need to read the page content in the future.
4054** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00004055*/
danielk19773b8a05f2007-03-19 17:44:26 +00004056void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004057 Pager *pPager = pPg->pPager;
4058
drh86f8c192007-08-22 00:39:19 +00004059 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004060 assert( pPager->state>=PAGER_RESERVED );
4061 if( pPager->journalOpen==0 ) return;
drhb7f91642004-10-31 02:22:47 +00004062 if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
drh30e58752002-03-02 20:41:57 +00004063 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
4064 assert( pPager->aInJournal!=0 );
4065 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4066 pPg->inJournal = 1;
drh538f5702007-04-13 02:14:30 +00004067 pPg->needRead = 0;
drhac69b052004-05-12 13:30:07 +00004068 if( pPager->stmtInUse ){
4069 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00004070 }
drh4f0c5872007-03-26 22:05:01 +00004071 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00004072 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00004073 }
danielk1977f35843b2007-04-07 15:03:17 +00004074 if( pPager->stmtInUse
4075 && !pageInStatement(pPg)
4076 && (int)pPg->pgno<=pPager->stmtSize
4077 ){
drh30e58752002-03-02 20:41:57 +00004078 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00004079 assert( pPager->aInStmt!=0 );
4080 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00004081 }
drh86f8c192007-08-22 00:39:19 +00004082 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004083}
4084
drhac69b052004-05-12 13:30:07 +00004085
drh30e58752002-03-02 20:41:57 +00004086/*
drh80e35f42007-03-30 14:06:34 +00004087** This routine is called to increment the database file change-counter,
4088** stored at byte 24 of the pager file.
4089*/
danielk1977c7b60172007-08-22 11:22:03 +00004090static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004091 PgHdr *pPgHdr;
4092 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004093 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004094
4095 if( !pPager->changeCountDone ){
4096 /* Open page 1 of the file for writing. */
4097 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4098 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004099
4100 if( !isDirect ){
4101 rc = sqlite3PagerWrite(pPgHdr);
4102 if( rc!=SQLITE_OK ) return rc;
4103 }
4104
drh80e35f42007-03-30 14:06:34 +00004105 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004106 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004107 change_counter++;
4108 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004109
4110 if( isDirect && pPager->fd->pMethods ){
4111 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4112 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4113 }
4114
drh80e35f42007-03-30 14:06:34 +00004115 /* Release the page reference. */
4116 sqlite3PagerUnref(pPgHdr);
4117 pPager->changeCountDone = 1;
4118 }
danielk1977c7b60172007-08-22 11:22:03 +00004119 return rc;
drh80e35f42007-03-30 14:06:34 +00004120}
4121
4122/*
4123** Sync the database file for the pager pPager. zMaster points to the name
4124** of a master journal file that should be written into the individual
4125** journal file. zMaster may be NULL, which is interpreted as no master
4126** journal (a single database transaction).
4127**
4128** This routine ensures that the journal is synced, all dirty pages written
4129** to the database file and the database file synced. The only thing that
4130** remains to commit the transaction is to delete the journal file (or
4131** master journal file if specified).
4132**
4133** Note that if zMaster==NULL, this does not overwrite a previous value
4134** passed to an sqlite3PagerCommitPhaseOne() call.
4135**
4136** If parameter nTrunc is non-zero, then the pager file is truncated to
4137** nTrunc pages (this is used by auto-vacuum databases).
4138*/
4139int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
4140 int rc = SQLITE_OK;
4141
4142 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4143 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004144 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004145
4146 /* If this is an in-memory db, or no pages have been written to, or this
4147 ** function has already been called, it is a no-op.
4148 */
4149 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4150 PgHdr *pPg;
4151 assert( pPager->journalOpen );
4152
danielk1977c7b60172007-08-22 11:22:03 +00004153#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4154 /* The atomic-write optimization can be used if all of the
4155 ** following are true:
4156 **
4157 ** + The file-system supports the atomic-write property for
4158 ** blocks of size page-size, and
4159 ** + This commit is not part of a multi-file transaction, and
4160 ** + Exactly one page has been modified and store in the journal file.
4161 **
4162 ** If the optimization can be used, then the journal file will never
4163 ** be created for this transaction.
4164 */
4165 if( !zMaster && pPager->journalOff==jrnlBufferSize(pPager) && nTrunc==0
4166 && (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4167 ){
4168 /* Update the nRec field in the journal file. */
4169 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4170 assert(pPager->nRec==1);
4171 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4172
4173 /* Update the db file change counter. The following call will modify
4174 ** the in-memory representation of page 1 to include the updated
4175 ** change counter and then write page 1 directly to the database
4176 ** file. Because of the atomic-write property of the host file-system,
4177 ** this is safe.
4178 */
4179 rc = pager_incr_changecounter(pPager, 1);
4180 }else
4181#endif
4182
drh80e35f42007-03-30 14:06:34 +00004183 /* If a master journal file name has already been written to the
4184 ** journal file, then no sync is required. This happens when it is
4185 ** written, then the process fails to upgrade from a RESERVED to an
4186 ** EXCLUSIVE lock. The next time the process tries to commit the
4187 ** transaction the m-j name will have already been written.
4188 */
4189 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00004190 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004191 if( rc!=SQLITE_OK ) goto sync_exit;
4192#ifndef SQLITE_OMIT_AUTOVACUUM
4193 if( nTrunc!=0 ){
4194 /* If this transaction has made the database smaller, then all pages
4195 ** being discarded by the truncation must be written to the journal
4196 ** file.
4197 */
4198 Pgno i;
4199 int iSkip = PAGER_MJ_PGNO(pPager);
4200 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
4201 if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){
4202 rc = sqlite3PagerGet(pPager, i, &pPg);
4203 if( rc!=SQLITE_OK ) goto sync_exit;
4204 rc = sqlite3PagerWrite(pPg);
4205 sqlite3PagerUnref(pPg);
4206 if( rc!=SQLITE_OK ) goto sync_exit;
4207 }
4208 }
4209 }
4210#endif
4211 rc = writeMasterJournal(pPager, zMaster);
4212 if( rc!=SQLITE_OK ) goto sync_exit;
4213 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004214 }
danielk1977c7b60172007-08-22 11:22:03 +00004215 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004216
4217#ifndef SQLITE_OMIT_AUTOVACUUM
4218 if( nTrunc!=0 ){
4219 rc = sqlite3PagerTruncate(pPager, nTrunc);
4220 if( rc!=SQLITE_OK ) goto sync_exit;
4221 }
4222#endif
4223
4224 /* Write all dirty pages to the database file */
4225 pPg = pager_get_all_dirty_pages(pPager);
4226 rc = pager_write_pagelist(pPg);
4227 if( rc!=SQLITE_OK ) goto sync_exit;
drh3cdd7d32007-03-30 14:46:01 +00004228 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004229
4230 /* Sync the database file. */
4231 if( !pPager->noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004232 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004233 }
4234 IOTRACE(("DBSYNC %p\n", pPager))
4235
4236 pPager->state = PAGER_SYNCED;
4237 }else if( MEMDB && nTrunc!=0 ){
4238 rc = sqlite3PagerTruncate(pPager, nTrunc);
4239 }
4240
4241sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004242 if( rc==SQLITE_IOERR_BLOCKED ){
4243 /* pager_incr_changecounter() may attempt to obtain an exclusive
4244 * lock to spill the cache and return IOERR_BLOCKED. But since
4245 * there is no chance the cache is inconsistent, it's
4246 * better to return SQLITE_BUSY.
4247 */
4248 rc = SQLITE_BUSY;
4249 }
drh86f8c192007-08-22 00:39:19 +00004250 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004251 return rc;
4252}
4253
4254
4255/*
drhed7c8552001-04-11 14:29:21 +00004256** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004257**
4258** If the commit fails for any reason, a rollback attempt is made
4259** and an error code is returned. If the commit worked, SQLITE_OK
4260** is returned.
drhed7c8552001-04-11 14:29:21 +00004261*/
drh80e35f42007-03-30 14:06:34 +00004262int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004263 int rc;
drhed7c8552001-04-11 14:29:21 +00004264 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004265
danielk1977efaaf572006-01-16 11:29:19 +00004266 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004267 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004268 }
drha6abd042004-06-09 17:37:22 +00004269 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004270 return SQLITE_ERROR;
4271 }
drh86f8c192007-08-22 00:39:19 +00004272 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004273 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004274 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004275 pPg = pager_get_all_dirty_pages(pPager);
4276 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004277 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4278 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004279 pPg->dirty = 0;
4280 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004281 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004282 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004283 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004284 pPg = pPg->pDirty;
4285 }
drh605ad8f2006-05-03 23:34:05 +00004286 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004287#ifndef NDEBUG
4288 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4289 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4290 assert( !pPg->alwaysRollback );
4291 assert( !pHist->pOrig );
4292 assert( !pHist->pStmt );
4293 }
4294#endif
drhac69b052004-05-12 13:30:07 +00004295 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004296 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004297 return SQLITE_OK;
4298 }
drh3cdd7d32007-03-30 14:46:01 +00004299 assert( pPager->journalOpen || !pPager->dirtyCache );
4300 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4301 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004302 rc = pager_error(pPager, rc);
4303 pagerLeave(pPager);
4304 return rc;
drhed7c8552001-04-11 14:29:21 +00004305}
4306
4307/*
drha6abd042004-06-09 17:37:22 +00004308** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004309** All in-memory cache pages revert to their original data contents.
4310** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004311**
4312** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004313** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004314** process is writing trash into the journal file (SQLITE_CORRUPT) or
4315** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4316** codes are returned for all these occasions. Otherwise,
4317** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004318*/
danielk19773b8a05f2007-03-19 17:44:26 +00004319int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004320 int rc;
drh4f0c5872007-03-26 22:05:01 +00004321 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004322 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004323 PgHdr *p;
4324 for(p=pPager->pAll; p; p=p->pNextAll){
4325 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004326 assert( !p->alwaysRollback );
4327 if( !p->dirty ){
4328 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4329 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4330 continue;
4331 }
4332
drhac69b052004-05-12 13:30:07 +00004333 pHist = PGHDR_TO_HIST(p, pPager);
4334 if( pHist->pOrig ){
4335 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004336 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004337 }else{
drh4f0c5872007-03-26 22:05:01 +00004338 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004339 }
4340 clearHistory(pHist);
4341 p->dirty = 0;
4342 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004343 pHist->inStmt = 0;
4344 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004345 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004346 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004347 }
drhac69b052004-05-12 13:30:07 +00004348 }
drh605ad8f2006-05-03 23:34:05 +00004349 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004350 pPager->pStmt = 0;
4351 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004352 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004353 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004354 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004355 return SQLITE_OK;
4356 }
4357
drh86f8c192007-08-22 00:39:19 +00004358 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004359 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004360 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004361 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004362 return rc;
4363 }
drhdb48ee02003-01-16 13:42:43 +00004364
danielk1977efaaf572006-01-16 11:29:19 +00004365 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004366 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004367 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004368 }
drh86f8c192007-08-22 00:39:19 +00004369 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004370 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004371 }
drha6abd042004-06-09 17:37:22 +00004372 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004373 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004374 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004375 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004376 if( rc==SQLITE_OK ){
4377 rc = rc2;
4378 }
4379 }else{
danielk1977e277be02007-03-23 18:12:06 +00004380 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004381 }
danielk1977e180dd92007-04-05 17:15:52 +00004382 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004383 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004384
4385 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4386 ** cache. So call pager_error() on the way out to make any error
4387 ** persistent.
4388 */
drh86f8c192007-08-22 00:39:19 +00004389 rc = pager_error(pPager, rc);
4390 pagerLeave(pPager);
4391 return rc;
drh98808ba2001-10-18 12:34:46 +00004392}
drhd9b02572001-04-15 00:37:09 +00004393
4394/*
drh5e00f6c2001-09-13 13:46:56 +00004395** Return TRUE if the database file is opened read-only. Return FALSE
4396** if the database is (in theory) writable.
4397*/
danielk19773b8a05f2007-03-19 17:44:26 +00004398int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004399 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004400}
4401
4402/*
drh0f7eb612006-08-08 13:51:43 +00004403** Return the number of references to the pager.
4404*/
danielk19773b8a05f2007-03-19 17:44:26 +00004405int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004406 return pPager->nRef;
4407}
4408
4409#ifdef SQLITE_TEST
4410/*
drhd9b02572001-04-15 00:37:09 +00004411** This routine is used for testing and analysis only.
4412*/
danielk19773b8a05f2007-03-19 17:44:26 +00004413int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004414 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004415 a[0] = pPager->nRef;
4416 a[1] = pPager->nPage;
4417 a[2] = pPager->mxPage;
4418 a[3] = pPager->dbSize;
4419 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004420 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004421 a[6] = pPager->nHit;
4422 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004423 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004424 a[9] = pPager->nRead;
4425 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004426 return a;
4427}
drh0f7eb612006-08-08 13:51:43 +00004428#endif
drhdd793422001-06-28 01:54:48 +00004429
drhfa86c412002-02-02 15:01:15 +00004430/*
drhac69b052004-05-12 13:30:07 +00004431** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004432**
4433** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004434** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004435** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004436*/
drh86f8c192007-08-22 00:39:19 +00004437static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004438 int rc;
drhac69b052004-05-12 13:30:07 +00004439 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004440 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004441 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004442 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004443 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004444 pPager->stmtInUse = 1;
4445 pPager->stmtSize = pPager->dbSize;
4446 return SQLITE_OK;
4447 }
drhda47d772002-12-02 04:25:19 +00004448 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004449 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004450 return SQLITE_OK;
4451 }
drhfa86c412002-02-02 15:01:15 +00004452 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004453 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00004454 pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00004455 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004456 if( pPager->aInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004457 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004458 return SQLITE_NOMEM;
4459 }
drh968af522003-02-11 14:55:40 +00004460#ifndef NDEBUG
drh054889e2005-11-30 03:20:31 +00004461 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
drhac69b052004-05-12 13:30:07 +00004462 if( rc ) goto stmt_begin_failed;
danielk197776572402004-06-25 02:38:54 +00004463 assert( pPager->stmtJSize == pPager->journalOff );
drh968af522003-02-11 14:55:40 +00004464#endif
danielk197776572402004-06-25 02:38:54 +00004465 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004466 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004467 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004468 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004469 if( !pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00004470 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, 0);
drhac69b052004-05-12 13:30:07 +00004471 if( rc ) goto stmt_begin_failed;
4472 pPager->stmtOpen = 1;
4473 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004474 }
drhac69b052004-05-12 13:30:07 +00004475 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004476 return SQLITE_OK;
4477
drhac69b052004-05-12 13:30:07 +00004478stmt_begin_failed:
4479 if( pPager->aInStmt ){
drh17435752007-08-16 04:30:38 +00004480 sqlite3_free(pPager->aInStmt);
drhac69b052004-05-12 13:30:07 +00004481 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004482 }
4483 return rc;
4484}
drh86f8c192007-08-22 00:39:19 +00004485int sqlite3PagerStmtBegin(Pager *pPager){
4486 int rc;
4487 pagerEnter(pPager);
4488 rc = pagerStmtBegin(pPager);
4489 pagerLeave(pPager);
4490 return rc;
4491}
drhfa86c412002-02-02 15:01:15 +00004492
4493/*
drhac69b052004-05-12 13:30:07 +00004494** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004495*/
danielk19773b8a05f2007-03-19 17:44:26 +00004496int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004497 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004498 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004499 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004500 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004501 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004502 /* sqlite3OsTruncate(pPager->stfd, 0); */
drh17435752007-08-16 04:30:38 +00004503 sqlite3_free( pPager->aInStmt );
drhac69b052004-05-12 13:30:07 +00004504 pPager->aInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004505 }else{
4506 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004507 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004508 pNext = pHist->pNextStmt;
4509 assert( pHist->inStmt );
4510 pHist->inStmt = 0;
4511 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004512 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004513 pHist->pStmt = 0;
4514 }
4515 }
4516 pPager->stmtNRec = 0;
4517 pPager->stmtInUse = 0;
4518 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004519 }
drhac69b052004-05-12 13:30:07 +00004520 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004521 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004522 return SQLITE_OK;
4523}
4524
4525/*
drhac69b052004-05-12 13:30:07 +00004526** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004527*/
danielk19773b8a05f2007-03-19 17:44:26 +00004528int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004529 int rc;
drh86f8c192007-08-22 00:39:19 +00004530 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004531 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004532 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004533 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004534 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004535 PgHistory *pHist;
4536 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4537 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004538 if( pHist->pStmt ){
4539 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004540 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004541 pHist->pStmt = 0;
4542 }
4543 }
4544 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004545 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004546 rc = SQLITE_OK;
4547 }else{
4548 rc = pager_stmt_playback(pPager);
4549 }
danielk19773b8a05f2007-03-19 17:44:26 +00004550 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004551 }else{
4552 rc = SQLITE_OK;
4553 }
drhac69b052004-05-12 13:30:07 +00004554 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004555 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004556 return rc;
4557}
4558
drh73509ee2003-04-06 20:44:45 +00004559/*
4560** Return the full pathname of the database file.
4561*/
danielk19773b8a05f2007-03-19 17:44:26 +00004562const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004563 return pPager->zFilename;
4564}
4565
drhb20ea9d2004-02-09 01:20:36 +00004566/*
danielk19775865e3d2004-06-14 06:03:57 +00004567** Return the directory of the database file.
4568*/
danielk19773b8a05f2007-03-19 17:44:26 +00004569const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004570 return pPager->zDirectory;
4571}
4572
4573/*
4574** Return the full pathname of the journal file.
4575*/
danielk19773b8a05f2007-03-19 17:44:26 +00004576const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004577 return pPager->zJournal;
4578}
4579
4580/*
drh2c8997b2005-08-27 16:36:48 +00004581** Return true if fsync() calls are disabled for this pager. Return FALSE
4582** if fsync()s are executed normally.
4583*/
danielk19773b8a05f2007-03-19 17:44:26 +00004584int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004585 return pPager->noSync;
4586}
4587
drh7c4ac0c2007-04-05 11:25:58 +00004588#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004589/*
drhb20ea9d2004-02-09 01:20:36 +00004590** Set the codec for this pager
4591*/
danielk19773b8a05f2007-03-19 17:44:26 +00004592void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004593 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004594 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004595 void *pCodecArg
4596){
4597 pPager->xCodec = xCodec;
4598 pPager->pCodecArg = pCodecArg;
4599}
drh7c4ac0c2007-04-05 11:25:58 +00004600#endif
drhb20ea9d2004-02-09 01:20:36 +00004601
danielk1977687566d2004-11-02 12:56:41 +00004602#ifndef SQLITE_OMIT_AUTOVACUUM
4603/*
drh5e385312007-06-16 04:42:12 +00004604** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004605**
drh5e385312007-06-16 04:42:12 +00004606** There must be no references to the page previously located at
4607** pgno (which we call pPgOld) though that page is allowed to be
4608** in cache. If the page previous located at pgno is not already
4609** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004610**
drh5e385312007-06-16 04:42:12 +00004611** References to the page pPg remain valid. Updating any
4612** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004613** allocated along with the page) is the responsibility of the caller.
4614**
danielk19775fd057a2005-03-09 13:09:43 +00004615** A transaction must be active when this routine is called. It used to be
4616** required that a statement transaction was not active, but this restriction
4617** has been removed (CREATE INDEX needs to move a page when a statement
4618** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00004619*/
danielk19773b8a05f2007-03-19 17:44:26 +00004620int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00004621 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00004622 int h;
danielk197794daf7f2004-11-08 09:26:09 +00004623 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004624
drh86f8c192007-08-22 00:39:19 +00004625 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004626 assert( pPg->nRef>0 );
4627
drh4f0c5872007-03-26 22:05:01 +00004628 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00004629 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00004630 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004631
danielk1977b4626a32007-04-28 15:47:43 +00004632 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00004633 if( pPg->needSync ){
4634 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00004635 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00004636 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00004637 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004638 }
4639
danielk1977687566d2004-11-02 12:56:41 +00004640 /* Unlink pPg from it's hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00004641 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00004642
danielk1977ef73ee92004-11-06 12:26:07 +00004643 /* If the cache contains a page with page-number pgno, remove it
danielk1977599fcba2004-11-08 07:13:13 +00004644 ** from it's hash chain. Also, if the PgHdr.needSync was set for
4645 ** page pgno before the 'move' operation, it needs to be retained
4646 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004647 */
drh5e385312007-06-16 04:42:12 +00004648 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00004649 pPgOld = pager_lookup(pPager, pgno);
4650 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00004651 assert( pPgOld->nRef==0 );
4652 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00004653 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00004654 pPg->needSync = pPgOld->needSync;
4655 }else{
4656 pPg->needSync = 0;
4657 }
4658 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
4659 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drh5e385312007-06-16 04:42:12 +00004660 }else{
4661 pPg->inJournal = 0;
drh732c8172007-06-16 11:17:45 +00004662 assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize );
danielk1977687566d2004-11-02 12:56:41 +00004663 }
4664
danielk1977f5fdda82004-11-03 08:44:05 +00004665 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00004666 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00004667 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00004668 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00004669 if( pPager->aHash[h] ){
4670 assert( pPager->aHash[h]->pPrevHash==0 );
4671 pPager->aHash[h]->pPrevHash = pPg;
4672 }
4673 pPg->pNextHash = pPager->aHash[h];
4674 pPager->aHash[h] = pPg;
4675 pPg->pPrevHash = 0;
4676
drh605ad8f2006-05-03 23:34:05 +00004677 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004678 pPager->dirtyCache = 1;
4679
danielk197794daf7f2004-11-08 09:26:09 +00004680 if( needSyncPgno ){
4681 /* If needSyncPgno is non-zero, then the journal file needs to be
4682 ** sync()ed before any data is written to database file page needSyncPgno.
4683 ** Currently, no such page exists in the page-cache and the
4684 ** Pager.aInJournal bit has been set. This needs to be remedied by loading
4685 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00004686 **
danielk19773b8a05f2007-03-19 17:44:26 +00004687 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004688 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004689 */
4690 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004691 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004692 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004693 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004694 if( rc!=SQLITE_OK ) return rc;
danielk1977ae825582004-11-23 09:06:55 +00004695 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00004696 pPgHdr->needSync = 1;
4697 pPgHdr->inJournal = 1;
4698 makeDirty(pPgHdr);
4699 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004700 }
4701
drh86f8c192007-08-22 00:39:19 +00004702 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004703 return SQLITE_OK;
4704}
4705#endif
4706
danielk19773b8a05f2007-03-19 17:44:26 +00004707/*
4708** Return a pointer to the data for the specified page.
4709*/
4710void *sqlite3PagerGetData(DbPage *pPg){
4711 return PGHDR_TO_DATA(pPg);
4712}
4713
4714/*
4715** Return a pointer to the Pager.nExtra bytes of "extra" space
4716** allocated along with the specified page.
4717*/
4718void *sqlite3PagerGetExtra(DbPage *pPg){
4719 Pager *pPager = pPg->pPager;
4720 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
4721}
4722
danielk197741483462007-03-24 16:45:04 +00004723/*
4724** Get/set the locking-mode for this pager. Parameter eMode must be one
4725** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4726** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4727** the locking-mode is set to the value specified.
4728**
4729** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4730** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4731** locking-mode.
4732*/
4733int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004734 assert( eMode==PAGER_LOCKINGMODE_QUERY
4735 || eMode==PAGER_LOCKINGMODE_NORMAL
4736 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4737 assert( PAGER_LOCKINGMODE_QUERY<0 );
4738 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4739 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00004740 pPager->exclusiveMode = eMode;
4741 }
4742 return (int)pPager->exclusiveMode;
4743}
4744
dougcurrie81c95ef2004-06-18 23:21:47 +00004745#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00004746/*
4747** Return the current state of the file lock for the given pager.
4748** The return value is one of NO_LOCK, SHARED_LOCK, RESERVED_LOCK,
4749** PENDING_LOCK, or EXCLUSIVE_LOCK.
4750*/
danielk19773b8a05f2007-03-19 17:44:26 +00004751int sqlite3PagerLockstate(Pager *pPager){
drh054889e2005-11-30 03:20:31 +00004752 return sqlite3OsLockState(pPager->fd);
drh89ac8c12004-06-09 14:17:20 +00004753}
4754#endif
4755
danielk197793758c82005-01-21 08:13:14 +00004756#ifdef SQLITE_DEBUG
drhdd793422001-06-28 01:54:48 +00004757/*
4758** Print a listing of all referenced pages and their ref count.
4759*/
danielk19773b8a05f2007-03-19 17:44:26 +00004760void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00004761 PgHdr *pPg;
4762 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4763 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00004764 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
4765 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00004766 }
4767}
4768#endif
drh2e66f0b2005-04-28 17:18:48 +00004769
4770#endif /* SQLITE_OMIT_DISKIO */