blob: ca11cf58001ddac786c1bbe50b0f0597478b0d9d [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**
drh17435752007-08-16 04:30:38 +000021** @(#) $Id: pager.c,v 1.357 2007/08/16 04:30:40 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"
drheb206252004-10-01 02:00:31 +000025#include "os.h"
drhed7c8552001-04-11 14:29:21 +000026#include "pager.h"
drhed7c8552001-04-11 14:29:21 +000027#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000028#include <string.h>
drhed7c8552001-04-11 14:29:21 +000029
30/*
drhdb48ee02003-01-16 13:42:43 +000031** Macros for troubleshooting. Normally turned off
32*/
danielk1977466be562004-06-10 02:16:01 +000033#if 0
drhd3627af2006-12-18 18:34:51 +000034#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000035#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
36#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
37#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
38#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
39#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000040#else
drh4f0c5872007-03-26 22:05:01 +000041#define PAGERTRACE1(X)
42#define PAGERTRACE2(X,Y)
43#define PAGERTRACE3(X,Y,Z)
44#define PAGERTRACE4(X,Y,Z,W)
45#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000046#endif
47
danielk1977599fcba2004-11-08 07:13:13 +000048/*
drh4f0c5872007-03-26 22:05:01 +000049** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000050** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000051**
52** PAGERID() takes a pointer to a Pager struct as it's argument. The
danielk197762079062007-08-15 17:08:46 +000053** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
danielk1977599fcba2004-11-08 07:13:13 +000054** struct as it's argument.
55*/
drhc001c582006-03-06 18:23:16 +000056#define PAGERID(p) ((int)(p->fd))
57#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000058
59/*
drhed7c8552001-04-11 14:29:21 +000060** The page cache as a whole is always in one of the following
61** states:
62**
drha6abd042004-06-09 17:37:22 +000063** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000064** writing the database file. There is no
65** data held in memory. This is the initial
66** state.
67**
drha6abd042004-06-09 17:37:22 +000068** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000069** Writing is not permitted. There can be
70** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000071** file at the same time.
drhed7c8552001-04-11 14:29:21 +000072**
drh726de592004-06-10 23:35:50 +000073** PAGER_RESERVED This process has reserved the database for writing
74** but has not yet made any changes. Only one process
75** at a time can reserve the database. The original
76** database file has not been modified so other
77** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000078** database file.
79**
80** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000081** Access is exclusive. No other processes or
82** threads can be reading or writing while one
83** process is writing.
84**
danielk1977aa5ccdf2004-06-14 05:10:42 +000085** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
86** after all dirty pages have been written to the
87** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000088** disk. All that remains to do is to remove or
89** truncate the journal file and the transaction
90** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000091**
drha6abd042004-06-09 17:37:22 +000092** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000093** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000094** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000095** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000096** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000097** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000098** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000099** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +0000100** PAGER_RESERVED means that there is an open rollback journal.
101** The transition to PAGER_EXCLUSIVE occurs before any changes
102** are made to the database file, though writes to the rollback
103** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
104** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
105** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000106*/
drha6abd042004-06-09 17:37:22 +0000107#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000108#define PAGER_SHARED 1 /* same as SHARED_LOCK */
109#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
110#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
111#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000112
drh684917c2004-10-05 02:41:42 +0000113/*
114** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
115** then failed attempts to get a reserved lock will invoke the busy callback.
116** This is off by default. To see why, consider the following scenario:
117**
118** Suppose thread A already has a shared lock and wants a reserved lock.
119** Thread B already has a reserved lock and wants an exclusive lock. If
120** both threads are using their busy callbacks, it might be a long time
121** be for one of the threads give up and allows the other to proceed.
122** But if the thread trying to get the reserved lock gives up quickly
123** (if it never invokes its busy callback) then the contention will be
124** resolved quickly.
125*/
126#ifndef SQLITE_BUSY_RESERVED_LOCK
127# define SQLITE_BUSY_RESERVED_LOCK 0
128#endif
drhd9b02572001-04-15 00:37:09 +0000129
drhed7c8552001-04-11 14:29:21 +0000130/*
drh887dc4c2004-10-22 16:22:57 +0000131** This macro rounds values up so that if the value is an address it
132** is guaranteed to be an address that is aligned to an 8-byte boundary.
133*/
134#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
135
136/*
drhed7c8552001-04-11 14:29:21 +0000137** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +0000138** This header is only visible to this pager module. The client
139** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +0000140**
danielk19773b8a05f2007-03-19 17:44:26 +0000141** Client code should call sqlite3PagerWrite() on a page prior to making
142** any modifications to that page. The first time sqlite3PagerWrite()
drhf6038712004-02-08 18:07:34 +0000143** is called, the original page contents are written into the rollback
144** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
145** the journal page has made it onto the disk surface, PgHdr.needSync
146** is cleared. The modified page cannot be written back into the original
147** database file until the journal pages has been synced to disk and the
148** PgHdr.needSync has been cleared.
149**
danielk19773b8a05f2007-03-19 17:44:26 +0000150** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
drhf6038712004-02-08 18:07:34 +0000151** is cleared again when the page content is written back to the original
152** database file.
drh732c8172007-06-16 11:17:45 +0000153**
154** Details of important structure elements:
155**
156** needSync
157**
158** If this is true, this means that it is not safe to write the page
159** content to the database because the original content needed
160** for rollback has not by synced to the main rollback journal.
161** The original content may have been written to the rollback journal
162** but it has not yet been synced. So we cannot write to the database
163** file because power failure might cause the page in the journal file
164** to never reach the disk. It is as if the write to the journal file
165** does not occur until the journal file is synced.
166**
167** This flag is false if the page content exactly matches what
168** currently exists in the database file. The needSync flag is also
169** false if the original content has been written to the main rollback
170** journal and synced. If the page represents a new page that has
171** been added onto the end of the database during the current
172** transaction, the needSync flag is true until the original database
173** size in the journal header has been synced to disk.
174**
175** inJournal
176**
177** This is true if the original page has been written into the main
178** rollback journal. This is always false for new pages added to
179** the end of the database file during the current transaction.
180** And this flag says nothing about whether or not the journal
181** has been synced to disk. For pages that are in the original
182** database file, the following expression should always be true:
183**
184** inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0
185**
186** The pPager->aInJournal[] array is only valid for the original
187** pages of the database, not new pages that are added to the end
188** of the database, so obviously the above expression cannot be
189** valid for new pages. For new pages inJournal is always 0.
190**
191** dirty
192**
193** When true, this means that the content of the page has been
194** modified and needs to be written back to the database file.
195** If false, it means that either the content of the page is
196** unchanged or else the content is unimportant and we do not
197** care whether or not it is preserved.
198**
199** alwaysRollback
200**
201** This means that the sqlite3PagerDontRollback() API should be
202** ignored for this page. The DontRollback() API attempts to say
203** that the content of the page on disk is unimportant (it is an
204** unused page on the freelist) so that it is unnecessary to
205** rollback changes to this page because the content of the page
206** can change without changing the meaning of the database. This
207** flag overrides any DontRollback() attempt. This flag is set
208** when a page that originally contained valid data is added to
209** the freelist. Later in the same transaction, this page might
210** be pulled from the freelist and reused for something different
211** and at that point the DontRollback() API will be called because
212** pages taken from the freelist do not need to be protected by
213** the rollback journal. But this flag says that the page was
214** not originally part of the freelist so that it still needs to
215** be rolled back in spite of any subsequent DontRollback() calls.
216**
217** needRead
218**
219** This flag means (when true) that the content of the page has
220** not yet been loaded from disk. The in-memory content is just
221** garbage. (Actually, we zero the content, but you should not
222** make any assumptions about the content nevertheless.) If the
223** content is needed in the future, it should be read from the
224** original database file.
drhed7c8552001-04-11 14:29:21 +0000225*/
drhd9b02572001-04-15 00:37:09 +0000226typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +0000227struct PgHdr {
228 Pager *pPager; /* The pager to which this page belongs */
229 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000230 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhd9b02572001-04-15 00:37:09 +0000231 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
drhac69b052004-05-12 13:30:07 +0000232 PgHdr *pNextAll; /* A list of all pages */
drh193a6b42002-07-07 16:52:46 +0000233 u8 inJournal; /* TRUE if has been written to journal */
drh193a6b42002-07-07 16:52:46 +0000234 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000235 u8 needSync; /* Sync journal before writing this page */
drh80e35f42007-03-30 14:06:34 +0000236 u8 alwaysRollback; /* Disable DontRollback() for this page */
drh538f5702007-04-13 02:14:30 +0000237 u8 needRead; /* Read content if PagerWrite() is called */
drhac69b052004-05-12 13:30:07 +0000238 short int nRef; /* Number of users of this page */
drhdc3e10b2006-06-15 14:31:06 +0000239 PgHdr *pDirty, *pPrevDirty; /* Dirty pages */
drh605ad8f2006-05-03 23:34:05 +0000240 u32 notUsed; /* Buffer space */
danielk19773c407372005-02-15 02:54:14 +0000241#ifdef SQLITE_CHECK_PAGES
242 u32 pageHash;
243#endif
drh1c7880e2005-05-20 20:01:55 +0000244 /* pPager->pageSize bytes of page data follow this header */
drh973b6e32003-02-12 14:09:42 +0000245 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +0000246};
247
drhac69b052004-05-12 13:30:07 +0000248/*
249** For an in-memory only database, some extra information is recorded about
250** each page so that changes can be rolled back. (Journal files are not
251** used for in-memory databases.) The following information is added to
252** the end of every EXTRA block for in-memory databases.
253**
254** This information could have been added directly to the PgHdr structure.
255** But then it would take up an extra 8 bytes of storage on every PgHdr
256** even for disk-based databases. Splitting it out saves 8 bytes. This
257** is only a savings of 0.8% but those percentages add up.
258*/
259typedef struct PgHistory PgHistory;
260struct PgHistory {
261 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
262 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000263 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
264 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000265};
drh9eb9e262004-02-11 02:18:05 +0000266
267/*
268** A macro used for invoking the codec if there is one
269*/
270#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000271# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
272# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000273#else
drhc001c582006-03-06 18:23:16 +0000274# define CODEC1(P,D,N,X) /* NO-OP */
275# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000276#endif
277
drhed7c8552001-04-11 14:29:21 +0000278/*
drh69688d52001-04-14 16:38:23 +0000279** Convert a pointer to a PgHdr into a pointer to its data
280** and back again.
drhed7c8552001-04-11 14:29:21 +0000281*/
282#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
283#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drh1c7880e2005-05-20 20:01:55 +0000284#define PGHDR_TO_EXTRA(G,P) ((void*)&((char*)(&(G)[1]))[(P)->pageSize])
drhac69b052004-05-12 13:30:07 +0000285#define PGHDR_TO_HIST(P,PGR) \
drh1c7880e2005-05-20 20:01:55 +0000286 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000287
288/*
drhed7c8552001-04-11 14:29:21 +0000289** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000290**
drh4f0ee682007-03-30 20:43:40 +0000291** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000292** or SQLITE_FULL. Once one of the first three errors occurs, it persists
293** and is returned as the result of every major pager API call. The
294** SQLITE_FULL return code is slightly different. It persists only until the
295** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000296** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000297** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000298*/
299struct Pager {
drh603240c2002-03-05 01:11:12 +0000300 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000301 u8 journalStarted; /* True if header of journal is synced */
302 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000303 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000304 u8 stmtOpen; /* True if the statement subjournal is open */
305 u8 stmtInUse; /* True we are in a statement subtransaction */
306 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000307 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000308 u8 fullSync; /* Do extra syncs of the journal for robustness */
drhac530b12006-02-11 01:25:50 +0000309 u8 full_fsync; /* Use F_FULLFSYNC when available */
drha6abd042004-06-09 17:37:22 +0000310 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000311 u8 tempFile; /* zFilename is a temporary file */
312 u8 readOnly; /* True for a read-only database */
313 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000314 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000315 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000316 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000317 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000318 u8 doNotSync; /* Boolean. While true, do not spill the cache */
319 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
320 u8 changeCountDone; /* Set after incrementing the change-counter */
drhe49f9822006-09-15 12:29:16 +0000321 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000322 int dbSize; /* Number of pages in the file */
323 int origDbSize; /* dbSize before the current change */
324 int stmtSize; /* Size of database (in pages) at stmt_begin() */
325 int nRec; /* Number of pages written to the journal */
326 u32 cksumInit; /* Quasi-random value added to every checksum */
327 int stmtNRec; /* Number of records in stmt subjournal */
328 int nExtra; /* Add this many bytes to each in-memory page */
329 int pageSize; /* Number of bytes in a page */
330 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000331 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
332 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000333 Pgno mxPgno; /* Maximum allowed size of the database */
drh603240c2002-03-05 01:11:12 +0000334 u8 *aInJournal; /* One bit for each page in the database file */
drhac69b052004-05-12 13:30:07 +0000335 u8 *aInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000336 char *zFilename; /* Name of the database file */
337 char *zJournal; /* Name of the journal file */
338 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000339 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
340 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000341 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
drhed7c8552001-04-11 14:29:21 +0000342 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000343 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000344 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000345 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000346 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000347 i64 journalOff; /* Current byte offset in the journal file */
348 i64 journalHdr; /* Byte offset to previous journal header */
349 i64 stmtHdrOff; /* First journal header written this statement */
350 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000351 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000352 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000353#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000354 int nHit, nMiss; /* Cache hits and missing */
355 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000356#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000357 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
358 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000359#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000360 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000361 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000362#endif
drh8ca0c722006-05-07 17:49:38 +0000363 int nHash; /* Size of the pager hash table */
364 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000365#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +0000366 Pager *pNext; /* Linked list of pagers in this thread */
367#endif
danielk19778186df82007-03-06 13:45:59 +0000368 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000369 char dbFileVers[16]; /* Changes whenever database file changes */
drhd9b02572001-04-15 00:37:09 +0000370};
371
372/*
drh538f5702007-04-13 02:14:30 +0000373** The following global variables hold counters used for
374** testing purposes only. These variables do not exist in
375** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000376*/
377#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000378int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
379int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
380int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
381int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
382# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000383#else
drh538f5702007-04-13 02:14:30 +0000384# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000385#endif
386
drh538f5702007-04-13 02:14:30 +0000387
388
drhfcd35c72005-05-21 02:48:08 +0000389/*
drh5e00f6c2001-09-13 13:46:56 +0000390** Journal files begin with the following magic string. The data
391** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000392**
drhae2b40c2004-06-09 19:03:54 +0000393** Since version 2.8.0, the journal format contains additional sanity
394** checking information. If the power fails while the journal is begin
395** written, semi-random garbage data might appear in the journal
396** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000397** to roll the journal back, the database could be corrupted. The additional
398** sanity checking data is an attempt to discover the garbage in the
399** journal and ignore it.
400**
drhae2b40c2004-06-09 19:03:54 +0000401** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000402** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000403** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000404** This cksum is initialized to a 32-bit random value that appears in the
405** journal file right after the header. The random initializer is important,
406** because garbage data that appears at the end of a journal is likely
407** data that was once in other files that have now been deleted. If the
408** garbage data came from an obsolete journal file, the checksums might
409** be correct. But by initializing the checksum to random value which
410** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000411*/
drhae2b40c2004-06-09 19:03:54 +0000412static const unsigned char aJournalMagic[] = {
413 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000414};
415
416/*
drh726de592004-06-10 23:35:50 +0000417** The size of the header and of each page in the journal is determined
418** by the following macros.
drh968af522003-02-11 14:55:40 +0000419*/
drhae2b40c2004-06-09 19:03:54 +0000420#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000421
danielk197776572402004-06-25 02:38:54 +0000422/*
423** The journal header size for this pager. In the future, this could be
424** set to some value read from the disk controller. The important
425** characteristic is that it is the same size as a disk sector.
426*/
427#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
428
drhb7f91642004-10-31 02:22:47 +0000429/*
430** The macro MEMDB is true if we are dealing with an in-memory database.
431** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
432** the value of MEMDB will be a constant and the compiler will optimize
433** out code that would never execute.
434*/
435#ifdef SQLITE_OMIT_MEMORYDB
436# define MEMDB 0
437#else
438# define MEMDB pPager->memDb
439#endif
440
441/*
danielk197776572402004-06-25 02:38:54 +0000442** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
443** reserved for working around a windows/posix incompatibility). It is
444** used in the journal to signify that the remainder of the journal file
445** is devoted to storing a master journal name - there are no more pages to
446** roll back. See comments for function writeMasterJournal() for details.
447*/
danielk1977599fcba2004-11-08 07:13:13 +0000448/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
449#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000450
drh968af522003-02-11 14:55:40 +0000451/*
danielk197726836652005-01-17 01:33:13 +0000452** The maximum legal page number is (2^31 - 1).
453*/
454#define PAGER_MAX_PGNO 2147483647
455
456/*
drh726de592004-06-10 23:35:50 +0000457** Enable reference count tracking (for debugging) here:
drhdd793422001-06-28 01:54:48 +0000458*/
drh7c4ac0c2007-04-05 11:25:58 +0000459#ifdef SQLITE_DEBUG
drh3aac2dd2004-04-26 14:10:20 +0000460 int pager3_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000461 static void pager_refinfo(PgHdr *p){
462 static int cnt = 0;
drh3aac2dd2004-04-26 14:10:20 +0000463 if( !pager3_refinfo_enable ) return;
drhfe63d1c2004-09-08 20:13:04 +0000464 sqlite3DebugPrintf(
drh24c9a2e2007-01-05 02:00:47 +0000465 "REFCNT: %4d addr=%p nRef=%-3d total=%d\n",
466 p->pgno, PGHDR_TO_DATA(p), p->nRef, p->pPager->nRef
drhdd793422001-06-28 01:54:48 +0000467 );
468 cnt++; /* Something to set a breakpoint on */
469 }
470# define REFINFO(X) pager_refinfo(X)
471#else
472# define REFINFO(X)
473#endif
474
danielk1977f35843b2007-04-07 15:03:17 +0000475/*
476** Return true if page *pPg has already been written to the statement
477** journal (or statement snapshot has been created, if *pPg is part
478** of an in-memory database).
479*/
480static int pageInStatement(PgHdr *pPg){
481 Pager *pPager = pPg->pPager;
482 if( MEMDB ){
483 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
484 }else{
485 Pgno pgno = pPg->pgno;
486 u8 *a = pPager->aInStmt;
487 return (a && (int)pgno<=pPager->stmtSize && (a[pgno/8] & (1<<(pgno&7))));
488 }
489}
drh8ca0c722006-05-07 17:49:38 +0000490
491/*
492** Change the size of the pager hash table to N. N must be a power
493** of two.
494*/
495static void pager_resize_hash_table(Pager *pPager, int N){
496 PgHdr **aHash, *pPg;
497 assert( N>0 && (N&(N-1))==0 );
drh17435752007-08-16 04:30:38 +0000498 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drh8ca0c722006-05-07 17:49:38 +0000499 if( aHash==0 ){
500 /* Failure to rehash is not an error. It is only a performance hit. */
501 return;
502 }
drh17435752007-08-16 04:30:38 +0000503 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000504 pPager->nHash = N;
505 pPager->aHash = aHash;
506 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000507 int h;
508 if( pPg->pgno==0 ){
509 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
510 continue;
511 }
512 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000513 pPg->pNextHash = aHash[h];
514 if( aHash[h] ){
515 aHash[h]->pPrevHash = pPg;
516 }
517 aHash[h] = pPg;
518 pPg->pPrevHash = 0;
519 }
520}
521
drhdd793422001-06-28 01:54:48 +0000522/*
drh34e79ce2004-02-08 06:05:46 +0000523** Read a 32-bit integer from the given file descriptor. Store the integer
524** that is read in *pRes. Return SQLITE_OK if everything worked, or an
525** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000526**
527** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000528*/
danielk197762079062007-08-15 17:08:46 +0000529static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000530 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000531 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000532 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000533 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000534 }
drh94f33312002-08-12 12:29:56 +0000535 return rc;
536}
537
538/*
drh97b57482006-01-10 20:32:32 +0000539** Write a 32-bit integer into a string buffer in big-endian byte order.
540*/
drha3152892007-05-05 11:48:52 +0000541#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000542
543/*
drh34e79ce2004-02-08 06:05:46 +0000544** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
545** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000546*/
danielk197762079062007-08-15 17:08:46 +0000547static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000548 char ac[4];
drh97b57482006-01-10 20:32:32 +0000549 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000550 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000551}
552
drh2554f8b2003-01-22 01:26:44 +0000553/*
danielk1977aef0bf62005-12-30 16:28:01 +0000554** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000555** code. The first argument is a pointer to the pager structure, the
556** second the error-code about to be returned by a pager API function.
557** The value returned is a copy of the second argument to this function.
558**
drh4f0ee682007-03-30 20:43:40 +0000559** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977a96a7102006-01-16 12:46:41 +0000560** the error becomes persistent. All subsequent API calls on this Pager
561** will immediately return the same error code.
danielk1977aef0bf62005-12-30 16:28:01 +0000562*/
563static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000564 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000565 assert(
566 pPager->errCode==SQLITE_FULL ||
567 pPager->errCode==SQLITE_OK ||
568 (pPager->errCode & 0xff)==SQLITE_IOERR
569 );
danielk1977979f38e2007-03-27 16:19:51 +0000570 if(
drh4ac285a2006-09-15 07:28:50 +0000571 rc2==SQLITE_FULL ||
572 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000573 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000574 ){
575 pPager->errCode = rc;
danielk1977aef0bf62005-12-30 16:28:01 +0000576 }
577 return rc;
578}
579
drh477731b2007-06-16 03:06:27 +0000580/*
581** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
582** on the cache using a hash function. This is used for testing
583** and debugging only.
584*/
danielk19773c407372005-02-15 02:54:14 +0000585#ifdef SQLITE_CHECK_PAGES
586/*
587** Return a 32-bit hash of the page data for pPage.
588*/
drh477731b2007-06-16 03:06:27 +0000589static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000590 u32 hash = 0;
591 int i;
drh477731b2007-06-16 03:06:27 +0000592 for(i=0; i<nByte; i++){
593 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000594 }
595 return hash;
596}
drh477731b2007-06-16 03:06:27 +0000597static u32 pager_pagehash(PgHdr *pPage){
598 return pager_datahash(pPage->pPager->pageSize,
599 (unsigned char *)PGHDR_TO_DATA(pPage));
600}
danielk19773c407372005-02-15 02:54:14 +0000601
602/*
603** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
604** is defined, and NDEBUG is not defined, an assert() statement checks
605** that the page is either dirty or still matches the calculated page-hash.
606*/
607#define CHECK_PAGE(x) checkPage(x)
608static void checkPage(PgHdr *pPg){
609 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000610 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000611 pPg->pageHash==pager_pagehash(pPg) );
612}
613
614#else
drh8ffa8172007-06-18 17:25:17 +0000615#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000616#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000617#define CHECK_PAGE(x)
618#endif
619
drhed7c8552001-04-11 14:29:21 +0000620/*
danielk197776572402004-06-25 02:38:54 +0000621** When this is called the journal file for pager pPager must be open.
622** The master journal file name is read from the end of the file and
drh17435752007-08-16 04:30:38 +0000623** written into memory obtained from sqlite3_malloc(). *pzMaster is
danielk197776572402004-06-25 02:38:54 +0000624** set to point at the memory and SQLITE_OK returned. The caller must
drh17435752007-08-16 04:30:38 +0000625** sqlite3_free() *pzMaster.
danielk197776572402004-06-25 02:38:54 +0000626**
627** If no master journal file name is present *pzMaster is set to 0 and
628** SQLITE_OK returned.
629*/
danielk197762079062007-08-15 17:08:46 +0000630static int readMasterJournal(sqlite3_file *pJrnl, char **pzMaster){
danielk197776572402004-06-25 02:38:54 +0000631 int rc;
632 u32 len;
drheb206252004-10-01 02:00:31 +0000633 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000634 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000635 int i;
danielk197776572402004-06-25 02:38:54 +0000636 unsigned char aMagic[8]; /* A buffer to hold the magic header */
637
638 *pzMaster = 0;
639
drh054889e2005-11-30 03:20:31 +0000640 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000641 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000642
danielk197762079062007-08-15 17:08:46 +0000643 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000644 if( rc!=SQLITE_OK ) return rc;
645
danielk197762079062007-08-15 17:08:46 +0000646 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000647 if( rc!=SQLITE_OK ) return rc;
648
danielk197762079062007-08-15 17:08:46 +0000649 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000650 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
651
drh17435752007-08-16 04:30:38 +0000652 *pzMaster = (char *)sqlite3MallocZero(len+1);
danielk197776572402004-06-25 02:38:54 +0000653 if( !*pzMaster ){
654 return SQLITE_NOMEM;
655 }
danielk197762079062007-08-15 17:08:46 +0000656 rc = sqlite3OsRead(pJrnl, *pzMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000657 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000658 sqlite3_free(*pzMaster);
danielk197776572402004-06-25 02:38:54 +0000659 *pzMaster = 0;
660 return rc;
661 }
danielk1977cafadba2004-06-25 11:11:54 +0000662
663 /* See if the checksum matches the master journal name */
664 for(i=0; i<len; i++){
danielk19778191bff2004-06-28 04:52:30 +0000665 cksum -= (*pzMaster)[i];
danielk1977cafadba2004-06-25 11:11:54 +0000666 }
danielk19778191bff2004-06-28 04:52:30 +0000667 if( cksum ){
668 /* If the checksum doesn't add up, then one or more of the disk sectors
669 ** containing the master journal filename is corrupted. This means
670 ** definitely roll back, so just return SQLITE_OK and report a (nul)
671 ** master-journal filename.
672 */
drh17435752007-08-16 04:30:38 +0000673 sqlite3_free(*pzMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000674 *pzMaster = 0;
danielk1977aca790a2005-01-13 11:07:52 +0000675 }else{
676 (*pzMaster)[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000677 }
danielk197776572402004-06-25 02:38:54 +0000678
679 return SQLITE_OK;
680}
681
682/*
683** Seek the journal file descriptor to the next sector boundary where a
684** journal header may be read or written. Pager.journalOff is updated with
685** the new seek offset.
686**
687** i.e for a sector size of 512:
688**
689** Input Offset Output Offset
690** ---------------------------------------
691** 0 0
692** 512 512
693** 100 512
694** 2000 2048
695**
696*/
danielk197762079062007-08-15 17:08:46 +0000697static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000698 i64 offset = 0;
699 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000700 if( c ){
701 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
702 }
703 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
704 assert( offset>=c );
705 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
706 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000707}
708
709/*
710** The journal file must be open when this routine is called. A journal
711** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
712** current location.
713**
714** The format for the journal header is as follows:
715** - 8 bytes: Magic identifying journal format.
716** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
717** - 4 bytes: Random number used for page hash.
718** - 4 bytes: Initial database page count.
719** - 4 bytes: Sector size used by the process that wrote this journal.
720**
danielk1977f42f25c2004-06-25 07:21:28 +0000721** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000722*/
723static int writeJournalHdr(Pager *pPager){
drh97b57482006-01-10 20:32:32 +0000724 char zHeader[sizeof(aJournalMagic)+16];
danielk19774099f6e2007-03-19 11:25:20 +0000725 int rc;
danielk197776572402004-06-25 02:38:54 +0000726
danielk19774099f6e2007-03-19 11:25:20 +0000727 if( pPager->stmtHdrOff==0 ){
728 pPager->stmtHdrOff = pPager->journalOff;
729 }
730
danielk197762079062007-08-15 17:08:46 +0000731 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000732 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000733
734 /* FIX ME:
735 **
736 ** Possibly for a pager not in no-sync mode, the journal magic should not
737 ** be written until nRec is filled in as part of next syncJournal().
738 **
739 ** Actually maybe the whole journal header should be delayed until that
740 ** point. Think about this.
741 */
drh97b57482006-01-10 20:32:32 +0000742 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
743 /* The nRec Field. 0xFFFFFFFF for no-sync journals. */
744 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->noSync ? 0xffffffff : 0);
745 /* The random check-hash initialiser */
746 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
747 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
748 /* The initial database size */
749 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
750 /* The assumed sector size for this process */
751 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drhb0603412007-02-28 04:47:26 +0000752 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
danielk197762079062007-08-15 17:08:46 +0000753 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
754 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197776572402004-06-25 02:38:54 +0000755
756 /* The journal header has been written successfully. Seek the journal
757 ** file descriptor to the end of the journal header sector.
758 */
759 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +0000760 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
danielk197762079062007-08-15 17:08:46 +0000761 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
danielk197776572402004-06-25 02:38:54 +0000762 }
763 return rc;
764}
765
766/*
767** The journal file must be open when this is called. A journal header file
768** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
769** file. See comments above function writeJournalHdr() for a description of
770** the journal header format.
771**
772** If the header is read successfully, *nRec is set to the number of
773** page records following this header and *dbSize is set to the size of the
774** database before the transaction began, in pages. Also, pPager->cksumInit
775** is set to the value read from the journal header. SQLITE_OK is returned
776** in this case.
777**
778** If the journal header file appears to be corrupted, SQLITE_DONE is
779** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
780** cannot be read from the journal file an error code is returned.
781*/
782static int readJournalHdr(
783 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000784 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000785 u32 *pNRec,
786 u32 *pDbSize
787){
788 int rc;
789 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000790 i64 jrnlOff;
danielk197776572402004-06-25 02:38:54 +0000791
danielk197762079062007-08-15 17:08:46 +0000792 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000793 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
794 return SQLITE_DONE;
795 }
danielk197762079062007-08-15 17:08:46 +0000796 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000797
danielk197762079062007-08-15 17:08:46 +0000798 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000799 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000800 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000801
802 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
803 return SQLITE_DONE;
804 }
805
danielk197762079062007-08-15 17:08:46 +0000806 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000807 if( rc ) return rc;
808
danielk197762079062007-08-15 17:08:46 +0000809 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000810 if( rc ) return rc;
811
danielk197762079062007-08-15 17:08:46 +0000812 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000813 if( rc ) return rc;
814
815 /* Update the assumed sector-size to match the value used by
816 ** the process that created this journal. If this journal was
817 ** created by a process other than this one, then this routine
818 ** is being called from within pager_playback(). The local value
819 ** of Pager.sectorSize is restored at the end of that routine.
820 */
danielk197762079062007-08-15 17:08:46 +0000821 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000822 if( rc ) return rc;
823
824 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000825 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000826}
827
828
829/*
830** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000831** pPager at the current location. The master journal name must be the last
832** thing written to a journal file. If the pager is in full-sync mode, the
833** journal file descriptor is advanced to the next sector boundary before
834** anything is written. The format is:
835**
836** + 4 bytes: PAGER_MJ_PGNO.
837** + N bytes: length of master journal name.
838** + 4 bytes: N
839** + 4 bytes: Master journal name checksum.
840** + 8 bytes: aJournalMagic[].
841**
842** The master journal page checksum is the sum of the bytes in the master
843** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000844**
845** If zMaster is a NULL pointer (occurs for a single database transaction),
846** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000847*/
848static int writeMasterJournal(Pager *pPager, const char *zMaster){
849 int rc;
850 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000851 int i;
danielk197762079062007-08-15 17:08:46 +0000852 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +0000853 u32 cksum = 0;
854 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000855
856 if( !zMaster || pPager->setMaster) return SQLITE_OK;
857 pPager->setMaster = 1;
858
859 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000860 for(i=0; i<len; i++){
861 cksum += zMaster[i];
862 }
danielk197776572402004-06-25 02:38:54 +0000863
864 /* If in full-sync mode, advance to the next disk sector before writing
865 ** the master journal name. This is in case the previous page written to
866 ** the journal has already been synced.
867 */
868 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000869 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000870 }
danielk197762079062007-08-15 17:08:46 +0000871 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000872 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000873
danielk197762079062007-08-15 17:08:46 +0000874 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000875 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000876 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000877
danielk197762079062007-08-15 17:08:46 +0000878 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000879 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000880 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000881
drh97b57482006-01-10 20:32:32 +0000882 put32bits(zBuf, len);
883 put32bits(&zBuf[4], cksum);
884 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000885 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +0000886 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +0000887 return rc;
888}
889
890/*
drh03eb96a2002-11-10 23:32:56 +0000891** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +0000892** statement journal.
drh03eb96a2002-11-10 23:32:56 +0000893**
894** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +0000895** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +0000896** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +0000897** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +0000898*/
drh3aac2dd2004-04-26 14:10:20 +0000899static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +0000900 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +0000901 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
902 assert( MEMDB );
903 if( !pHist->inStmt ){
904 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
905 if( pPager->pStmt ){
906 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
907 }
908 pHist->pNextStmt = pPager->pStmt;
909 pPager->pStmt = pPg;
910 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +0000911 }
drh03eb96a2002-11-10 23:32:56 +0000912}
913
914/*
drhed7c8552001-04-11 14:29:21 +0000915** Find a page in the hash table given its page number. Return
916** a pointer to the page or NULL if not found.
917*/
drhd9b02572001-04-15 00:37:09 +0000918static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000919 PgHdr *p;
920 if( pPager->aHash==0 ) return 0;
921 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +0000922 while( p && p->pgno!=pgno ){
923 p = p->pNextHash;
924 }
925 return p;
926}
927
928/*
drh1aa2d8b2007-01-03 15:34:29 +0000929** Unlock the database file.
drh1aa2d8b2007-01-03 15:34:29 +0000930*/
931static void pager_unlock(Pager *pPager){
danielk197741483462007-03-24 16:45:04 +0000932 if( !pPager->exclusiveMode ){
933 if( !MEMDB ){
934 sqlite3OsUnlock(pPager->fd, NO_LOCK);
935 pPager->dbSize = -1;
936 IOTRACE(("UNLOCK %p\n", pPager))
937 }
938 pPager->state = PAGER_UNLOCK;
939 pPager->changeCountDone = 0;
drh1aa2d8b2007-01-03 15:34:29 +0000940 }
danielk1977e277be02007-03-23 18:12:06 +0000941}
942
943/*
944** Execute a rollback if a transaction is active and unlock the
945** database file. This is a no-op if the pager has already entered
946** the error-state.
947*/
danielk1977334cdb62007-03-26 08:05:12 +0000948static void pagerUnlockAndRollback(Pager *p){
949 if( p->errCode ) return;
danielk1977c5859712007-03-26 12:26:27 +0000950 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
danielk1977334cdb62007-03-26 08:05:12 +0000951 if( p->state>=PAGER_RESERVED ){
952 sqlite3PagerRollback(p);
danielk1977e277be02007-03-23 18:12:06 +0000953 }
danielk1977334cdb62007-03-26 08:05:12 +0000954 pager_unlock(p);
955 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
956 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drh1aa2d8b2007-01-03 15:34:29 +0000957}
958
959
960/*
danielk1977e180dd92007-04-05 17:15:52 +0000961** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +0000962** sets the state of the pager back to what it was when it was first
963** opened. Any outstanding pages are invalidated and subsequent attempts
964** to access those pages will likely result in a coredump.
965*/
drhd9b02572001-04-15 00:37:09 +0000966static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000967 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +0000968 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +0000969 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +0000970 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
971 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +0000972 pNext = pPg->pNextAll;
drh17435752007-08-16 04:30:38 +0000973 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +0000974 }
danielk1977b94bf852007-03-19 13:53:37 +0000975 pPager->pStmt = 0;
drhed7c8552001-04-11 14:29:21 +0000976 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000977 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +0000978 pPager->pLast = 0;
979 pPager->pAll = 0;
drh8ca0c722006-05-07 17:49:38 +0000980 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +0000981 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +0000982 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +0000983 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +0000984 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +0000985}
986
987/*
drh80e35f42007-03-30 14:06:34 +0000988** This routine ends a transaction. A transaction is ended by either
989** a COMMIT or a ROLLBACK.
990**
drhed7c8552001-04-11 14:29:21 +0000991** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +0000992** a RESERVED or EXCLUSIVE lock on the database. This routine will release
993** the database lock and acquires a SHARED lock in its place if that is
994** the appropriate thing to do. Release locks usually is appropriate,
995** unless we are in exclusive access mode or unless this is a
996** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
997**
998** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +0000999**
1000** TODO: Consider keeping the journal file open for temporary databases.
1001** This might give a performance improvement on windows where opening
1002** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001003*/
drh80e35f42007-03-30 14:06:34 +00001004static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001005 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001006 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001007 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001008 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001009 if( pPager->state<PAGER_RESERVED ){
1010 return SQLITE_OK;
1011 }
danielk19773b8a05f2007-03-19 17:44:26 +00001012 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001013 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977979f38e2007-03-27 16:19:51 +00001014 sqlite3OsClose(&pPager->stfd);
1015 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001016 }
drhda47d772002-12-02 04:25:19 +00001017 if( pPager->journalOpen ){
drh01fa4c32007-04-02 11:22:22 +00001018 if( pPager->exclusiveMode
1019 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
danielk197741483462007-03-24 16:45:04 +00001020 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001021 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001022 }else{
1023 sqlite3OsClose(&pPager->jfd);
1024 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001025 if( rc==SQLITE_OK ){
danielk19777152de82007-03-29 17:28:14 +00001026 rc = sqlite3OsDelete(pPager->zJournal);
1027 }
danielk197741483462007-03-24 16:45:04 +00001028 }
drh17435752007-08-16 04:30:38 +00001029 sqlite3_free( pPager->aInJournal );
drhda47d772002-12-02 04:25:19 +00001030 pPager->aInJournal = 0;
1031 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1032 pPg->inJournal = 0;
1033 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001034 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001035 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001036#ifdef SQLITE_CHECK_PAGES
1037 pPg->pageHash = pager_pagehash(pPg);
1038#endif
drhda47d772002-12-02 04:25:19 +00001039 }
drh605ad8f2006-05-03 23:34:05 +00001040 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001041 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001042 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001043 }else{
drh88b01a12005-03-28 18:04:27 +00001044 assert( pPager->aInJournal==0 );
drha6abd042004-06-09 17:37:22 +00001045 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001046 }
danielk1977979f38e2007-03-27 16:19:51 +00001047
danielk197741483462007-03-24 16:45:04 +00001048 if( !pPager->exclusiveMode ){
danielk1977979f38e2007-03-27 16:19:51 +00001049 rc2 = sqlite3OsUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001050 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001051 }else if( pPager->state==PAGER_SYNCED ){
1052 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001053 }
danielk1977334cdb62007-03-26 08:05:12 +00001054 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001055 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001056 pPager->needSync = 0;
1057 pPager->pFirstSynced = pPager->pFirst;
drh588f5bc2007-01-02 18:41:54 +00001058 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001059
1060 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001061}
1062
drhed7c8552001-04-11 14:29:21 +00001063/*
drh968af522003-02-11 14:55:40 +00001064** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001065**
1066** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001067** random initial value and the page number. We experimented with
1068** a checksum of the entire data, but that was found to be too slow.
1069**
1070** Note that the page number is stored at the beginning of data and
1071** the checksum is stored at the end. This is important. If journal
1072** corruption occurs due to a power failure, the most likely scenario
1073** is that one end or the other of the record will be changed. It is
1074** much less likely that the two ends of the journal record will be
1075** correct and the middle be corrupt. Thus, this "checksum" scheme,
1076** though fast and simple, catches the mostly likely kind of corruption.
1077**
1078** FIX ME: Consider adding every 200th (or so) byte of the data to the
1079** checksum. That way if a single page spans 3 or more disk sectors and
1080** only the middle sector is corrupt, we will still have a reasonable
1081** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001082*/
drh74161702006-02-24 02:53:49 +00001083static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001084 u32 cksum = pPager->cksumInit;
1085 int i = pPager->pageSize-200;
1086 while( i>0 ){
1087 cksum += aData[i];
1088 i -= 200;
1089 }
drh968af522003-02-11 14:55:40 +00001090 return cksum;
1091}
1092
drh605ad8f2006-05-03 23:34:05 +00001093/* Forward declaration */
1094static void makeClean(PgHdr*);
1095
drh968af522003-02-11 14:55:40 +00001096/*
drhfa86c412002-02-02 15:01:15 +00001097** Read a single page from the journal file opened on file descriptor
1098** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001099**
drh726de592004-06-10 23:35:50 +00001100** If useCksum==0 it means this journal does not use checksums. Checksums
1101** are not used in statement journals because statement journals do not
1102** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001103*/
danielk197762079062007-08-15 17:08:46 +00001104static int pager_playback_one_page(
1105 Pager *pPager,
1106 sqlite3_file *jfd,
1107 i64 offset,
1108 int useCksum
1109){
drhfa86c412002-02-02 15:01:15 +00001110 int rc;
drhae2b40c2004-06-09 19:03:54 +00001111 PgHdr *pPg; /* An existing page in the cache */
1112 Pgno pgno; /* The page number of a page in journal */
1113 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001114 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001115
drh96362842005-03-20 22:47:56 +00001116 /* useCksum should be true for the main journal and false for
1117 ** statement journals. Verify that this is always the case
1118 */
drh9cbe6352005-11-29 03:13:21 +00001119 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001120 assert( aData );
drh96362842005-03-20 22:47:56 +00001121
danielk197762079062007-08-15 17:08:46 +00001122 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001123 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001124 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001125 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001126 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001127
drh968af522003-02-11 14:55:40 +00001128 /* Sanity checking on the page. This is more important that I originally
1129 ** thought. If a power failure occurs while the journal is being written,
1130 ** it could cause invalid data to be written into the journal. We need to
1131 ** detect this invalid data (with high probability) and ignore it.
1132 */
danielk197775edc162004-06-26 01:48:18 +00001133 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001134 return SQLITE_DONE;
1135 }
drhae2b40c2004-06-09 19:03:54 +00001136 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001137 return SQLITE_OK;
1138 }
drhae2b40c2004-06-09 19:03:54 +00001139 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001140 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001141 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001142 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001143 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001144 return SQLITE_DONE;
1145 }
1146 }
drhfa86c412002-02-02 15:01:15 +00001147
danielk1977aa5ccdf2004-06-14 05:10:42 +00001148 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001149
1150 /* If the pager is in RESERVED state, then there must be a copy of this
1151 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001152 ** not the database file. The page is left marked dirty in this case.
1153 **
danielk19772df71c72007-05-24 07:22:42 +00001154 ** An exception to the above rule: If the database is in no-sync mode
1155 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001156 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1157 ** during a Movepage() call, then the page may not be in the cache
1158 ** either. So the condition described in the above paragraph is not
1159 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001160 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001161 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1162 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001163 **
1164 ** Ticket #1171: The statement journal might contain page content that is
1165 ** different from the page content at the start of the transaction.
1166 ** This occurs when a page is changed prior to the start of a statement
1167 ** then changed again within the statement. When rolling back such a
1168 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001169 ** for certain that original page contents are synced into the main rollback
1170 ** journal. Otherwise, a power loss might leave modified data in the
1171 ** database file without an entry in the rollback journal that can
1172 ** restore the database to its original form. Two conditions must be
1173 ** met before writing to the database files. (1) the database must be
1174 ** locked. (2) we know that the original page content is fully synced
1175 ** in the main journal either because the page is not in cache or else
1176 ** the page is marked as needSync==0.
drhfa86c412002-02-02 15:01:15 +00001177 */
drhae2b40c2004-06-09 19:03:54 +00001178 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001179 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1180 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh96362842005-03-20 22:47:56 +00001181 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
danielk197762079062007-08-15 17:08:46 +00001182 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1183 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001184 if( pPg ){
1185 makeClean(pPg);
1186 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001187 }
drhfa86c412002-02-02 15:01:15 +00001188 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001189 /* No page should ever be explicitly rolled back that is in use, except
1190 ** for page 1 which is held in use in order to keep the lock on the
1191 ** database active. However such a page may be rolled back as a result
1192 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001193 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001194 */
drhb6f41482004-05-14 01:58:11 +00001195 void *pData;
danielk197728129562005-01-11 10:25:06 +00001196 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001197 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001198 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001199 if( pPager->xReiniter ){
1200 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001201 }
danielk19773c407372005-02-15 02:54:14 +00001202#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001203 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001204#endif
drh86a88112007-04-16 15:02:19 +00001205 /* If this was page 1, then restore the value of Pager.dbFileVers.
1206 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001207 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001208 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001209 }
drh86a88112007-04-16 15:02:19 +00001210
1211 /* Decode the page just read from disk */
1212 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001213 }
1214 return rc;
1215}
1216
1217/*
danielk197713adf8a2004-06-03 16:08:41 +00001218** Parameter zMaster is the name of a master journal file. A single journal
1219** file that referred to the master journal file has just been rolled back.
1220** This routine checks if it is possible to delete the master journal file,
1221** and does so if it is.
drh726de592004-06-10 23:35:50 +00001222**
1223** The master journal file contains the names of all child journals.
1224** To tell if a master journal can be deleted, check to each of the
1225** children. If all children are either missing or do not refer to
1226** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001227*/
1228static int pager_delmaster(const char *zMaster){
1229 int rc;
1230 int master_open = 0;
danielk197762079062007-08-15 17:08:46 +00001231 sqlite3_file *master = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001232 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001233 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001234
1235 /* Open the master journal file exclusively in case some other process
1236 ** is running this routine also. Not that it makes too much difference.
1237 */
drh66560ad2006-01-06 14:32:19 +00001238 rc = sqlite3OsOpenReadOnly(zMaster, &master);
danielk19775bb16fe2007-04-05 11:54:42 +00001239 assert( rc!=SQLITE_OK || master );
danielk197713adf8a2004-06-03 16:08:41 +00001240 if( rc!=SQLITE_OK ) goto delmaster_out;
1241 master_open = 1;
drh054889e2005-11-30 03:20:31 +00001242 rc = sqlite3OsFileSize(master, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001243 if( rc!=SQLITE_OK ) goto delmaster_out;
1244
1245 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001246 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001247 char *zMasterPtr = 0;
danielk19775865e3d2004-06-14 06:03:57 +00001248
1249 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001250 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001251 */
drh17435752007-08-16 04:30:38 +00001252 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001253 if( !zMasterJournal ){
1254 rc = SQLITE_NOMEM;
1255 goto delmaster_out;
1256 }
danielk197762079062007-08-15 17:08:46 +00001257 rc = sqlite3OsRead(master, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001258 if( rc!=SQLITE_OK ) goto delmaster_out;
1259
danielk19775865e3d2004-06-14 06:03:57 +00001260 zJournal = zMasterJournal;
1261 while( (zJournal-zMasterJournal)<nMasterJournal ){
drh66560ad2006-01-06 14:32:19 +00001262 if( sqlite3OsFileExists(zJournal) ){
danielk197713adf8a2004-06-03 16:08:41 +00001263 /* One of the journals pointed to by the master journal exists.
1264 ** Open it and check if it points at the master journal. If
1265 ** so, return without deleting the master journal file.
1266 */
danielk197762079062007-08-15 17:08:46 +00001267 sqlite3_file *journal = 0;
drh3b7b78b2004-11-24 01:16:43 +00001268 int c;
danielk197713adf8a2004-06-03 16:08:41 +00001269
drh66560ad2006-01-06 14:32:19 +00001270 rc = sqlite3OsOpenReadOnly(zJournal, &journal);
danielk19775bb16fe2007-04-05 11:54:42 +00001271 assert( rc!=SQLITE_OK || journal );
danielk197713adf8a2004-06-03 16:08:41 +00001272 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001273 goto delmaster_out;
1274 }
danielk197713adf8a2004-06-03 16:08:41 +00001275
drh9cbe6352005-11-29 03:13:21 +00001276 rc = readMasterJournal(journal, &zMasterPtr);
drh054889e2005-11-30 03:20:31 +00001277 sqlite3OsClose(&journal);
danielk19779eed5052004-06-04 10:38:30 +00001278 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001279 goto delmaster_out;
1280 }
danielk197776572402004-06-25 02:38:54 +00001281
drh3b7b78b2004-11-24 01:16:43 +00001282 c = zMasterPtr!=0 && strcmp(zMasterPtr, zMaster)==0;
drh17435752007-08-16 04:30:38 +00001283 sqlite3_free(zMasterPtr);
drh3b7b78b2004-11-24 01:16:43 +00001284 if( c ){
danielk197776572402004-06-25 02:38:54 +00001285 /* We have a match. Do not delete the master journal file. */
1286 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001287 }
1288 }
danielk19775865e3d2004-06-14 06:03:57 +00001289 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001290 }
1291 }
1292
danielk1977979f38e2007-03-27 16:19:51 +00001293 rc = sqlite3OsDelete(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001294
1295delmaster_out:
1296 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001297 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001298 }
1299 if( master_open ){
drh054889e2005-11-30 03:20:31 +00001300 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001301 }
1302 return rc;
1303}
1304
drha6abd042004-06-09 17:37:22 +00001305
danielk1977e180dd92007-04-05 17:15:52 +00001306static void pager_truncate_cache(Pager *pPager);
1307
drha6abd042004-06-09 17:37:22 +00001308/*
drhcb4c40b2004-08-18 19:09:43 +00001309** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001310** indicated. Also truncate the cached representation of the file.
drhcb4c40b2004-08-18 19:09:43 +00001311*/
1312static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001313 int rc = SQLITE_OK;
1314 if( pPager->state>=PAGER_EXCLUSIVE ){
1315 rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
1316 }
1317 if( rc==SQLITE_OK ){
1318 pPager->dbSize = nPage;
1319 pager_truncate_cache(pPager);
1320 }
1321 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001322}
1323
1324/*
drhc80f0582007-05-01 16:59:48 +00001325** Set the sectorSize for the given pager.
1326**
1327** The sector size is the larger of the sector size reported
1328** by sqlite3OsSectorSize() and the pageSize.
1329*/
1330static void setSectorSize(Pager *pPager){
1331 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1332 if( pPager->sectorSize<pPager->pageSize ){
1333 pPager->sectorSize = pPager->pageSize;
1334 }
1335}
1336
1337/*
drhed7c8552001-04-11 14:29:21 +00001338** Playback the journal and thus restore the database file to
1339** the state it was in before we started making changes.
1340**
drh34e79ce2004-02-08 06:05:46 +00001341** The journal file format is as follows:
1342**
drhae2b40c2004-06-09 19:03:54 +00001343** (1) 8 byte prefix. A copy of aJournalMagic[].
1344** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001345** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001346** number of page records from the journal size.
1347** (3) 4 byte big-endian integer which is the initial value for the
1348** sanity checksum.
1349** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001350** database to during a rollback.
drhae2b40c2004-06-09 19:03:54 +00001351** (5) 4 byte integer which is the number of bytes in the master journal
1352** name. The value may be zero (indicate that there is no master
1353** journal.)
1354** (6) N bytes of the master journal name. The name will be nul-terminated
1355** and might be shorter than the value read from (5). If the first byte
1356** of the name is \000 then there is no master journal. The master
1357** journal name is stored in UTF-8.
1358** (7) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001359** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001360** + pPager->pageSize bytes of data.
1361** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001362**
drhae2b40c2004-06-09 19:03:54 +00001363** When we speak of the journal header, we mean the first 6 items above.
1364** Each entry in the journal is an instance of the 7th item.
drh34e79ce2004-02-08 06:05:46 +00001365**
1366** Call the value from the second bullet "nRec". nRec is the number of
1367** valid page entries in the journal. In most cases, you can compute the
1368** value of nRec from the size of the journal file. But if a power
1369** failure occurred while the journal was being written, it could be the
1370** case that the size of the journal file had already been increased but
1371** the extra entries had not yet made it safely to disk. In such a case,
1372** the value of nRec computed from the file size would be too large. For
1373** that reason, we always use the nRec value in the header.
1374**
1375** If the nRec value is 0xffffffff it means that nRec should be computed
1376** from the file size. This value is used when the user selects the
1377** no-sync option for the journal. A power failure could lead to corruption
1378** in this case. But for things like temporary table (which will be
1379** deleted when the power is restored) we don't care.
1380**
drhd9b02572001-04-15 00:37:09 +00001381** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001382** journal file then all pages up to the first corrupted page are rolled
1383** back (or no pages if the journal header is corrupted). The journal file
1384** is then deleted and SQLITE_OK returned, just as if no corruption had
1385** been encountered.
1386**
1387** If an I/O or malloc() error occurs, the journal-file is not deleted
1388** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001389*/
danielk1977e277be02007-03-23 18:12:06 +00001390static int pager_playback(Pager *pPager, int isHot){
drheb206252004-10-01 02:00:31 +00001391 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001392 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001393 int i; /* Loop counter */
1394 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001395 int rc; /* Result code of a subroutine */
danielk197713adf8a2004-06-03 16:08:41 +00001396 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001397
drhc3a64ba2001-11-22 00:01:27 +00001398 /* Figure out how many records are in the journal. Abort early if
1399 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001400 */
drh8cfbf082001-09-19 13:22:39 +00001401 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001402 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001403 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001404 goto end_playback;
1405 }
drh240c5792004-02-08 00:40:52 +00001406
danielk197776572402004-06-25 02:38:54 +00001407 /* Read the master journal name from the journal, if it is present.
1408 ** If a master journal file name is specified, but the file is not
1409 ** present on disk, then the journal is not hot and does not need to be
1410 ** played back.
drh240c5792004-02-08 00:40:52 +00001411 */
drh9cbe6352005-11-29 03:13:21 +00001412 rc = readMasterJournal(pPager->jfd, &zMaster);
danielk197776572402004-06-25 02:38:54 +00001413 assert( rc!=SQLITE_DONE );
drh66560ad2006-01-06 14:32:19 +00001414 if( rc!=SQLITE_OK || (zMaster && !sqlite3OsFileExists(zMaster)) ){
drh17435752007-08-16 04:30:38 +00001415 sqlite3_free(zMaster);
danielk197776572402004-06-25 02:38:54 +00001416 zMaster = 0;
1417 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drhc3a64ba2001-11-22 00:01:27 +00001418 goto end_playback;
1419 }
danielk197776572402004-06-25 02:38:54 +00001420 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001421
danielk197776572402004-06-25 02:38:54 +00001422 /* This loop terminates either when the readJournalHdr() call returns
1423 ** SQLITE_DONE or an IO error occurs. */
1424 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001425
danielk197776572402004-06-25 02:38:54 +00001426 /* Read the next journal header from the journal file. If there are
1427 ** not enough bytes left in the journal file for a complete header, or
1428 ** it is corrupted, then a process must of failed while writing it.
1429 ** This indicates nothing more needs to be rolled back.
1430 */
1431 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1432 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001433 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001434 rc = SQLITE_OK;
1435 }
danielk197776572402004-06-25 02:38:54 +00001436 goto end_playback;
1437 }
1438
1439 /* If nRec is 0xffffffff, then this journal was created by a process
1440 ** working in no-sync mode. This means that the rest of the journal
1441 ** file consists of pages, there are no more journal headers. Compute
1442 ** the value of nRec based on this assumption.
1443 */
1444 if( nRec==0xffffffff ){
1445 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1446 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1447 }
1448
danielk1977e277be02007-03-23 18:12:06 +00001449 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001450 ** process and if this is the final header in the journal, then it means
1451 ** that this part of the journal was being filled but has not yet been
1452 ** synced to disk. Compute the number of pages based on the remaining
1453 ** size of the file.
1454 **
1455 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001456 */
drh8940f4e2007-08-11 00:26:20 +00001457 if( nRec==0 && !isHot &&
1458 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001459 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1460 }
1461
danielk197776572402004-06-25 02:38:54 +00001462 /* If this is the first header read from the journal, truncate the
1463 ** database file back to it's original size.
1464 */
danielk1977e180dd92007-04-05 17:15:52 +00001465 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001466 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001467 if( rc!=SQLITE_OK ){
1468 goto end_playback;
1469 }
danielk197776572402004-06-25 02:38:54 +00001470 }
1471
danielk197776572402004-06-25 02:38:54 +00001472 /* Copy original pages out of the journal and back into the database file.
1473 */
1474 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001475 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001476 if( rc!=SQLITE_OK ){
1477 if( rc==SQLITE_DONE ){
1478 rc = SQLITE_OK;
1479 pPager->journalOff = szJ;
1480 break;
1481 }else{
1482 goto end_playback;
1483 }
1484 }
drh968af522003-02-11 14:55:40 +00001485 }
drhed7c8552001-04-11 14:29:21 +00001486 }
drh580eeaf2006-02-24 03:09:37 +00001487 /*NOTREACHED*/
1488 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001489
1490end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001491 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001492 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001493 }
danielk197713adf8a2004-06-03 16:08:41 +00001494 if( zMaster ){
danielk1977979f38e2007-03-27 16:19:51 +00001495 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001496 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001497 */
1498 if( rc==SQLITE_OK ){
danielk197732554c12005-01-22 03:39:39 +00001499 rc = pager_delmaster(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001500 }
drh17435752007-08-16 04:30:38 +00001501 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001502 }
danielk197776572402004-06-25 02:38:54 +00001503
1504 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001505 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001506 ** value. Reset it to the correct value for this process.
1507 */
drhc80f0582007-05-01 16:59:48 +00001508 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001509 return rc;
drhed7c8552001-04-11 14:29:21 +00001510}
1511
1512/*
drhac69b052004-05-12 13:30:07 +00001513** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001514**
1515** This is similar to playing back the transaction journal but with
1516** a few extra twists.
1517**
drh663fc632002-02-02 18:49:19 +00001518** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001519** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001520** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001521**
drhac69b052004-05-12 13:30:07 +00001522** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001523** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001524** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001525*/
drh3aac2dd2004-04-26 14:10:20 +00001526static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001527 i64 szJ; /* Size of the full journal */
1528 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001529 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001530 int i; /* Loop counter */
1531 int rc;
1532
danielk197776572402004-06-25 02:38:54 +00001533 szJ = pPager->journalOff;
1534#ifndef NDEBUG
1535 {
drheb206252004-10-01 02:00:31 +00001536 i64 os_szJ;
drh054889e2005-11-30 03:20:31 +00001537 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
danielk197776572402004-06-25 02:38:54 +00001538 if( rc!=SQLITE_OK ) return rc;
1539 assert( szJ==os_szJ );
1540 }
1541#endif
1542
danielk19774099f6e2007-03-19 11:25:20 +00001543 /* Set hdrOff to be the offset just after the end of the last journal
1544 ** page written before the first journal-header for this statement
1545 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001546 ** header was written.
1547 */
1548 hdrOff = pPager->stmtHdrOff;
1549 assert( pPager->fullSync || !hdrOff );
1550 if( !hdrOff ){
1551 hdrOff = szJ;
1552 }
1553
drhfa86c412002-02-02 15:01:15 +00001554 /* Truncate the database back to its original size.
1555 */
danielk1977e180dd92007-04-05 17:15:52 +00001556 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001557 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001558
drhac69b052004-05-12 13:30:07 +00001559 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001560 */
drhac69b052004-05-12 13:30:07 +00001561 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001562 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001563
drhac69b052004-05-12 13:30:07 +00001564 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001565 ** database file. Note that the statement journal omits checksums from
1566 ** each record since power-failure recovery is not important to statement
1567 ** journals.
drhfa86c412002-02-02 15:01:15 +00001568 */
danielk197762079062007-08-15 17:08:46 +00001569 for(i=0; i<nRec; i++){
1570 i64 offset = i*(4+pPager->pageSize);
1571 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001572 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001573 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001574 }
1575
danielk197776572402004-06-25 02:38:54 +00001576 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1577 ** was the size of the journal file when this statement was started, so
1578 ** everything after that needs to be rolled back, either into the
1579 ** database, the memory cache, or both.
1580 **
1581 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1582 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001583 */
danielk197776572402004-06-25 02:38:54 +00001584 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001585 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001586 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001587 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001588 assert( rc!=SQLITE_DONE );
1589 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1590 }
1591
1592 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001593 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001594 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001595 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001596 if( rc!=SQLITE_OK ){
1597 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001598 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001599 }
danielk1977f0113002006-01-24 12:09:17 +00001600 if( nJRec==0 ){
1601 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001602 }
danielk1977f0113002006-01-24 12:09:17 +00001603 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001604 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001605 assert( rc!=SQLITE_DONE );
1606 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1607 }
drhfa86c412002-02-02 15:01:15 +00001608 }
danielk197776572402004-06-25 02:38:54 +00001609
1610 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001611
drh3aac2dd2004-04-26 14:10:20 +00001612end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001613 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001614 pPager->journalOff = szJ;
1615 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001616 }
1617 return rc;
1618}
1619
1620/*
drhf57b14a2001-09-14 18:54:08 +00001621** Change the maximum number of in-memory pages that are allowed.
1622*/
danielk19773b8a05f2007-03-19 17:44:26 +00001623void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00001624 if( mxPage>10 ){
1625 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00001626 }else{
1627 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00001628 }
1629}
1630
1631/*
drh973b6e32003-02-12 14:09:42 +00001632** Adjust the robustness of the database to damage due to OS crashes
1633** or power failures by changing the number of syncs()s when writing
1634** the rollback journal. There are three levels:
1635**
drh054889e2005-11-30 03:20:31 +00001636** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001637** for temporary and transient files.
1638**
1639** NORMAL The journal is synced once before writes begin on the
1640** database. This is normally adequate protection, but
1641** it is theoretically possible, though very unlikely,
1642** that an inopertune power failure could leave the journal
1643** in a state which would cause damage to the database
1644** when it is rolled back.
1645**
1646** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001647** database (with some additional information - the nRec field
1648** of the journal header - being written in between the two
1649** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001650** single disk sector is atomic, then this mode provides
1651** assurance that the journal will not be corrupted to the
1652** point of causing damage to the database during rollback.
1653**
1654** Numeric values associated with these states are OFF==1, NORMAL=2,
1655** and FULL=3.
1656*/
danielk197793758c82005-01-21 08:13:14 +00001657#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00001658void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00001659 pPager->noSync = level==1 || pPager->tempFile;
1660 pPager->fullSync = level==3 && !pPager->tempFile;
drhac530b12006-02-11 01:25:50 +00001661 pPager->full_fsync = full_fsync;
danielk19771d850a72004-05-31 08:26:49 +00001662 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001663}
danielk197793758c82005-01-21 08:13:14 +00001664#endif
drh973b6e32003-02-12 14:09:42 +00001665
1666/*
drhaf6df112005-06-07 02:12:30 +00001667** The following global variable is incremented whenever the library
1668** attempts to open a temporary file. This information is used for
1669** testing and analysis only.
1670*/
drh0f7eb612006-08-08 13:51:43 +00001671#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001672int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001673#endif
drhaf6df112005-06-07 02:12:30 +00001674
1675/*
drh3f56e6e2007-03-15 01:16:47 +00001676** Open a temporary file.
1677**
1678** Write the file descriptor into *fd. Return SQLITE_OK on success or some
drhfa86c412002-02-02 15:01:15 +00001679** other error code if we fail.
1680**
1681** The OS will automatically delete the temporary file when it is
1682** closed.
1683*/
danielk197762079062007-08-15 17:08:46 +00001684static int sqlite3PagerOpentemp(sqlite3_file **pFd){
drhfa86c412002-02-02 15:01:15 +00001685 int cnt = 8;
1686 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001687 char zFile[SQLITE_TEMPNAME_SIZE];
1688
drh0f7eb612006-08-08 13:51:43 +00001689#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001690 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001691#endif
drhfa86c412002-02-02 15:01:15 +00001692 do{
1693 cnt--;
drh66560ad2006-01-06 14:32:19 +00001694 sqlite3OsTempFileName(zFile);
1695 rc = sqlite3OsOpenExclusive(zFile, pFd, 1);
danielk19775bb16fe2007-04-05 11:54:42 +00001696 assert( rc!=SQLITE_OK || *pFd );
danielk197713073932004-06-30 11:54:06 +00001697 }while( cnt>0 && rc!=SQLITE_OK && rc!=SQLITE_NOMEM );
drhfa86c412002-02-02 15:01:15 +00001698 return rc;
1699}
1700
1701/*
drhed7c8552001-04-11 14:29:21 +00001702** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001703** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001704** the first call to sqlite3PagerGet() and is only held open until the
1705** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001706**
drh6446c4d2001-12-15 14:22:18 +00001707** If zFilename is NULL then a randomly-named temporary file is created
1708** and used as the file to be cached. The file will be deleted
1709** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001710**
1711** If zFilename is ":memory:" then all information is held in cache.
1712** It is never written to disk. This can be used to implement an
1713** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001714*/
danielk19773b8a05f2007-03-19 17:44:26 +00001715int sqlite3PagerOpen(
drh7e3b0a02001-04-28 16:52:40 +00001716 Pager **ppPager, /* Return the Pager structure here */
1717 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001718 int nExtra, /* Extra bytes append to each in-memory page */
drh7bec5052005-02-06 02:45:41 +00001719 int flags /* flags controlling this file */
drh7e3b0a02001-04-28 16:52:40 +00001720){
danielk1977aef0bf62005-12-30 16:28:01 +00001721 Pager *pPager = 0;
danielk19778def5ea2004-06-16 10:39:52 +00001722 char *zFullPathname = 0;
drh02afc862006-01-20 18:10:57 +00001723 int nameLen; /* Compiler is wrong. This is always initialized before use */
danielk197762079062007-08-15 17:08:46 +00001724 sqlite3_file *fd = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001725 int rc = SQLITE_OK;
1726 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001727 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001728 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001729 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001730 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1731 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
drh8cfbf082001-09-19 13:22:39 +00001732 char zTemp[SQLITE_TEMPNAME_SIZE];
drh6f7adc82006-01-11 21:41:20 +00001733#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197754f01982006-01-18 15:25:17 +00001734 /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to
1735 ** malloc() must have already been made by this thread before it gets
1736 ** to this point. This means the ThreadData must have been allocated already
1737 ** so that ThreadData.nAlloc can be set. It would be nice to assert
1738 ** that ThreadData.nAlloc is non-zero, but alas this breaks test cases
1739 ** written to invoke the pager directly.
1740 */
danielk19778212def2006-01-16 15:32:23 +00001741 ThreadData *pTsd = sqlite3ThreadData();
danielk197754f01982006-01-18 15:25:17 +00001742 assert( pTsd );
danielk197752622822006-01-09 09:59:49 +00001743#endif
drhed7c8552001-04-11 14:29:21 +00001744
danielk1977393f0682007-03-31 10:00:48 +00001745 /* We used to test if malloc() had already failed before proceeding.
1746 ** But the way this function is used in SQLite means that can never
1747 ** happen. Furthermore, if the malloc-failed flag is already set,
drh17435752007-08-16 04:30:38 +00001748 ** either the call to sqlite3StrDup() or sqlite3_malloc() below will
danielk1977393f0682007-03-31 10:00:48 +00001749 ** fail shortly and SQLITE_NOMEM returned anyway.
danielk1977aef0bf62005-12-30 16:28:01 +00001750 */
drhd9b02572001-04-15 00:37:09 +00001751 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001752
1753 /* Open the pager file and set zFullPathname to point at malloc()ed
1754 ** memory containing the complete filename (i.e. including the directory).
1755 */
drh901afd42003-08-26 11:25:58 +00001756 if( zFilename && zFilename[0] ){
drhb7f91642004-10-31 02:22:47 +00001757#ifndef SQLITE_OMIT_MEMORYDB
drhac69b052004-05-12 13:30:07 +00001758 if( strcmp(zFilename,":memory:")==0 ){
1759 memDb = 1;
drh17435752007-08-16 04:30:38 +00001760 zFullPathname = sqlite3StrDup("");
drhb7f91642004-10-31 02:22:47 +00001761 }else
1762#endif
1763 {
drh66560ad2006-01-06 14:32:19 +00001764 zFullPathname = sqlite3OsFullPathname(zFilename);
danielk19778def5ea2004-06-16 10:39:52 +00001765 if( zFullPathname ){
drh66560ad2006-01-06 14:32:19 +00001766 rc = sqlite3OsOpenReadWrite(zFullPathname, &fd, &readOnly);
danielk19775bb16fe2007-04-05 11:54:42 +00001767 assert( rc!=SQLITE_OK || fd );
danielk19778def5ea2004-06-16 10:39:52 +00001768 }
drhac69b052004-05-12 13:30:07 +00001769 }
drh5e00f6c2001-09-13 13:46:56 +00001770 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00001771 rc = sqlite3PagerOpentemp(&fd);
drh3f56e6e2007-03-15 01:16:47 +00001772 sqlite3OsTempFileName(zTemp);
drh5e00f6c2001-09-13 13:46:56 +00001773 zFilename = zTemp;
drh66560ad2006-01-06 14:32:19 +00001774 zFullPathname = sqlite3OsFullPathname(zFilename);
danielk19778def5ea2004-06-16 10:39:52 +00001775 if( rc==SQLITE_OK ){
1776 tempFile = 1;
1777 }
drh5e00f6c2001-09-13 13:46:56 +00001778 }
danielk1977aef0bf62005-12-30 16:28:01 +00001779
1780 /* Allocate the Pager structure. As part of the same allocation, allocate
1781 ** space for the full paths of the file, directory and journal
1782 ** (Pager.zFilename, Pager.zDirectory and Pager.zJournal).
1783 */
1784 if( zFullPathname ){
1785 nameLen = strlen(zFullPathname);
drh17435752007-08-16 04:30:38 +00001786 pPager = sqlite3MallocZero( sizeof(*pPager) + nameLen*3 + 30 );
danielk19778186df82007-03-06 13:45:59 +00001787 if( pPager && rc==SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001788 pPager->pTmpSpace = (char *)sqlite3_malloc(SQLITE_DEFAULT_PAGE_SIZE);
danielk19778186df82007-03-06 13:45:59 +00001789 }
drh3e7a6092002-12-07 21:45:14 +00001790 }
danielk1977aef0bf62005-12-30 16:28:01 +00001791
danielk19778186df82007-03-06 13:45:59 +00001792
danielk1977aef0bf62005-12-30 16:28:01 +00001793 /* If an error occured in either of the blocks above, free the memory
1794 ** pointed to by zFullPathname, free the Pager structure and close the
1795 ** file. Since the pager is not allocated there is no need to set
1796 ** any Pager.errMask variables.
1797 */
danielk19778186df82007-03-06 13:45:59 +00001798 if( !pPager || !zFullPathname || !pPager->pTmpSpace || rc!=SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001799 sqlite3OsClose(&fd);
drh17435752007-08-16 04:30:38 +00001800 sqlite3_free(zFullPathname);
1801 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00001802 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001803 }
danielk1977aef0bf62005-12-30 16:28:01 +00001804
drh4f0c5872007-03-26 22:05:01 +00001805 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(fd), zFullPathname);
drhb0603412007-02-28 04:47:26 +00001806 IOTRACE(("OPEN %p %s\n", pPager, zFullPathname))
drhed7c8552001-04-11 14:29:21 +00001807 pPager->zFilename = (char*)&pPager[1];
drha76c82e2003-07-27 18:59:42 +00001808 pPager->zDirectory = &pPager->zFilename[nameLen+1];
1809 pPager->zJournal = &pPager->zDirectory[nameLen+1];
drh5bb3eb92007-05-04 13:15:55 +00001810 memcpy(pPager->zFilename, zFullPathname, nameLen+1);
1811 memcpy(pPager->zDirectory, zFullPathname, nameLen+1);
danielk1977aef0bf62005-12-30 16:28:01 +00001812
drha76c82e2003-07-27 18:59:42 +00001813 for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){}
1814 if( i>0 ) pPager->zDirectory[i-1] = 0;
drh5bb3eb92007-05-04 13:15:55 +00001815 memcpy(pPager->zJournal, zFullPathname,nameLen);
drh17435752007-08-16 04:30:38 +00001816 sqlite3_free(zFullPathname);
drh5bb3eb92007-05-04 13:15:55 +00001817 memcpy(&pPager->zJournal[nameLen], "-journal",sizeof("-journal"));
drh9cbe6352005-11-29 03:13:21 +00001818 pPager->fd = fd;
drh3b59a5c2006-01-15 20:28:28 +00001819 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00001820 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00001821 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001822 /* pPager->stmtOpen = 0; */
1823 /* pPager->stmtInUse = 0; */
1824 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00001825 pPager->dbSize = memDb-1;
drh90f5ecb2004-07-22 01:19:35 +00001826 pPager->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
drh3b59a5c2006-01-15 20:28:28 +00001827 /* pPager->stmtSize = 0; */
1828 /* pPager->stmtJSize = 0; */
1829 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00001830 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00001831 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00001832 assert( PAGER_UNLOCK==0 );
1833 /* pPager->state = PAGER_UNLOCK; */
1834 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00001835 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00001836 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
1837 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
1838 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
1839 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00001840 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001841 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001842 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00001843 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00001844 pPager->fullSync = (pPager->noSync?0:1);
drh3b59a5c2006-01-15 20:28:28 +00001845 /* pPager->pFirst = 0; */
1846 /* pPager->pFirstSynced = 0; */
1847 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00001848 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk1977b4721172007-03-19 05:54:48 +00001849 assert(fd||memDb);
1850 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00001851 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00001852 }
drh3b59a5c2006-01-15 20:28:28 +00001853 /* pPager->pBusyHandler = 0; */
1854 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00001855 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00001856#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk19778212def2006-01-16 15:32:23 +00001857 pPager->pNext = pTsd->pPager;
1858 pTsd->pPager = pPager;
danielk197752622822006-01-09 09:59:49 +00001859#endif
drhed7c8552001-04-11 14:29:21 +00001860 return SQLITE_OK;
1861}
1862
1863/*
drh90f5ecb2004-07-22 01:19:35 +00001864** Set the busy handler function.
1865*/
danielk19773b8a05f2007-03-19 17:44:26 +00001866void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00001867 pPager->pBusyHandler = pBusyHandler;
1868}
1869
1870/*
drh72f82862001-05-24 21:06:34 +00001871** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00001872** when the reference count on each page reaches zero. The destructor can
1873** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00001874**
danielk19773b8a05f2007-03-19 17:44:26 +00001875** The destructor is not called as a result sqlite3PagerClose().
1876** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00001877*/
danielk19773b8a05f2007-03-19 17:44:26 +00001878void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00001879 pPager->xDestructor = xDesc;
1880}
1881
1882/*
drha6abd042004-06-09 17:37:22 +00001883** Set the reinitializer for this pager. If not NULL, the reinitializer
1884** is called when the content of a page in cache is restored to its original
1885** value as a result of a rollback. The callback gives higher-level code
1886** an opportunity to restore the EXTRA section to agree with the restored
1887** page data.
1888*/
danielk19773b8a05f2007-03-19 17:44:26 +00001889void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00001890 pPager->xReiniter = xReinit;
1891}
1892
1893/*
drh1c7880e2005-05-20 20:01:55 +00001894** Set the page size. Return the new size. If the suggest new page
1895** size is inappropriate, then an alternative page size is selected
1896** and returned.
drh90f5ecb2004-07-22 01:19:35 +00001897*/
danielk19773b8a05f2007-03-19 17:44:26 +00001898int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){
drh90f5ecb2004-07-22 01:19:35 +00001899 assert( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE );
danielk1977c7c7e622007-03-26 15:46:00 +00001900 if( !pPager->memDb && pPager->nRef==0 ){
1901 pager_reset(pPager);
drh1c7880e2005-05-20 20:01:55 +00001902 pPager->pageSize = pageSize;
drh17435752007-08-16 04:30:38 +00001903 sqlite3_free(pPager->pTmpSpace);
1904 pPager->pTmpSpace = sqlite3_malloc(pageSize);
drh1c7880e2005-05-20 20:01:55 +00001905 }
1906 return pPager->pageSize;
drh90f5ecb2004-07-22 01:19:35 +00001907}
1908
1909/*
drhf8e632b2007-05-08 14:51:36 +00001910** Attempt to set the maximum database page count if mxPage is positive.
1911** Make no changes if mxPage is zero or negative. And never reduce the
1912** maximum page count below the current size of the database.
1913**
1914** Regardless of mxPage, return the current maximum page count.
1915*/
1916int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
1917 if( mxPage>0 ){
1918 pPager->mxPgno = mxPage;
1919 }
1920 sqlite3PagerPagecount(pPager);
1921 return pPager->mxPgno;
1922}
1923
1924/*
drhc9ac5ca2005-11-04 22:03:30 +00001925** The following set of routines are used to disable the simulated
1926** I/O error mechanism. These routines are used to avoid simulated
1927** errors in places where we do not care about errors.
1928**
1929** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
1930** and generate no code.
1931*/
1932#ifdef SQLITE_TEST
1933extern int sqlite3_io_error_pending;
1934extern int sqlite3_io_error_hit;
1935static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00001936void disable_simulated_io_errors(void){
1937 saved_cnt = sqlite3_io_error_pending;
1938 sqlite3_io_error_pending = -1;
1939}
1940void enable_simulated_io_errors(void){
1941 sqlite3_io_error_pending = saved_cnt;
1942}
1943#else
drh152410f2005-11-05 15:11:22 +00001944# define disable_simulated_io_errors()
1945# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00001946#endif
1947
1948/*
drh90f5ecb2004-07-22 01:19:35 +00001949** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00001950** that pDest points to.
1951**
1952** No error checking is done. The rational for this is that this function
1953** may be called even if the file does not exist or contain a header. In
1954** these cases sqlite3OsRead() will return an error, to which the correct
1955** response is to zero the memory at pDest and continue. A real IO error
1956** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00001957*/
danielk19773b8a05f2007-03-19 17:44:26 +00001958int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00001959 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00001960 memset(pDest, 0, N);
drhb7f91642004-10-31 02:22:47 +00001961 if( MEMDB==0 ){
drhb0603412007-02-28 04:47:26 +00001962 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00001963 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00001964 if( rc==SQLITE_IOERR_SHORT_READ ){
1965 rc = SQLITE_OK;
1966 }
drh90f5ecb2004-07-22 01:19:35 +00001967 }
drh551b7732006-11-06 21:20:25 +00001968 return rc;
drh90f5ecb2004-07-22 01:19:35 +00001969}
1970
1971/*
drh5e00f6c2001-09-13 13:46:56 +00001972** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00001973** pPager.
1974**
1975** If the PENDING_BYTE lies on the page directly after the end of the
1976** file, then consider this page part of the file too. For example, if
1977** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
1978** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00001979*/
danielk19773b8a05f2007-03-19 17:44:26 +00001980int sqlite3PagerPagecount(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001981 i64 n;
drhe49f9822006-09-15 12:29:16 +00001982 int rc;
drhd9b02572001-04-15 00:37:09 +00001983 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00001984 if( pPager->errCode ){
1985 return 0;
1986 }
drhed7c8552001-04-11 14:29:21 +00001987 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00001988 n = pPager->dbSize;
1989 } else {
drhe49f9822006-09-15 12:29:16 +00001990 if( (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
1991 pager_error(pPager, rc);
danielk197715f411d2005-09-16 10:18:45 +00001992 return 0;
1993 }
1994 if( n>0 && n<pPager->pageSize ){
1995 n = 1;
1996 }else{
1997 n /= pPager->pageSize;
1998 }
1999 if( pPager->state!=PAGER_UNLOCK ){
2000 pPager->dbSize = n;
2001 }
drhed7c8552001-04-11 14:29:21 +00002002 }
danielk197715f411d2005-09-16 10:18:45 +00002003 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002004 n++;
2005 }
drhf8e632b2007-05-08 14:51:36 +00002006 if( n>pPager->mxPgno ){
2007 pPager->mxPgno = n;
2008 }
drhed7c8552001-04-11 14:29:21 +00002009 return n;
2010}
2011
drhf07e7d52006-04-07 13:54:46 +00002012
2013#ifndef SQLITE_OMIT_MEMORYDB
2014/*
2015** Clear a PgHistory block
2016*/
2017static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002018 sqlite3_free(pHist->pOrig);
2019 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002020 pHist->pOrig = 0;
2021 pHist->pStmt = 0;
2022}
2023#else
2024#define clearHistory(x)
2025#endif
2026
drhed7c8552001-04-11 14:29:21 +00002027/*
drhf7c57532003-04-25 13:22:51 +00002028** Forward declaration
2029*/
danielk197776572402004-06-25 02:38:54 +00002030static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002031
2032/*
danielk1977f5fdda82004-11-03 08:44:05 +00002033** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate
2034** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002035** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002036** pNextFree/pPrevFree list that is not a part of any hash-chain.
2037*/
2038static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2039 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002040 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002041 return;
2042 }
2043 if( pPg->pNextHash ){
2044 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2045 }
2046 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002047 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002048 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2049 }else{
drh8ca0c722006-05-07 17:49:38 +00002050 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002051 pPager->aHash[h] = pPg->pNextHash;
2052 }
drh5229ae42006-03-23 23:29:04 +00002053 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002054 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2055 }
danielk1977f5fdda82004-11-03 08:44:05 +00002056 pPg->pgno = 0;
2057 pPg->pNextHash = pPg->pPrevHash = 0;
2058}
2059
2060/*
drhac69b052004-05-12 13:30:07 +00002061** Unlink a page from the free list (the list of all pages where nRef==0)
2062** and from its hash collision chain.
2063*/
2064static void unlinkPage(PgHdr *pPg){
2065 Pager *pPager = pPg->pPager;
2066
2067 /* Keep the pFirstSynced pointer pointing at the first synchronized page */
2068 if( pPg==pPager->pFirstSynced ){
2069 PgHdr *p = pPg->pNextFree;
2070 while( p && p->needSync ){ p = p->pNextFree; }
2071 pPager->pFirstSynced = p;
2072 }
2073
2074 /* Unlink from the freelist */
2075 if( pPg->pPrevFree ){
2076 pPg->pPrevFree->pNextFree = pPg->pNextFree;
2077 }else{
2078 assert( pPager->pFirst==pPg );
2079 pPager->pFirst = pPg->pNextFree;
2080 }
2081 if( pPg->pNextFree ){
2082 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
2083 }else{
2084 assert( pPager->pLast==pPg );
2085 pPager->pLast = pPg->pPrevFree;
2086 }
2087 pPg->pNextFree = pPg->pPrevFree = 0;
2088
2089 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002090 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002091}
2092
2093/*
danielk1977e180dd92007-04-05 17:15:52 +00002094** This routine is used to truncate the cache when a database
2095** is truncated. Drop from the cache all pages whose pgno is
2096** larger than pPager->dbSize and is unreferenced.
2097**
drhac69b052004-05-12 13:30:07 +00002098** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002099**
2100** Actually, at the point this routine is called, it would be
2101** an error to have a referenced page. But rather than delete
2102** that page and guarantee a subsequent segfault, it seems better
2103** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002104*/
danielk1977e180dd92007-04-05 17:15:52 +00002105static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002106 PgHdr *pPg;
2107 PgHdr **ppPg;
2108 int dbSize = pPager->dbSize;
2109
2110 ppPg = &pPager->pAll;
2111 while( (pPg = *ppPg)!=0 ){
2112 if( pPg->pgno<=dbSize ){
2113 ppPg = &pPg->pNextAll;
2114 }else if( pPg->nRef>0 ){
2115 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2116 ppPg = &pPg->pNextAll;
2117 }else{
2118 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002119 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2120 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002121 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002122 makeClean(pPg);
drh17435752007-08-16 04:30:38 +00002123 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002124 pPager->nPage--;
2125 }
2126 }
2127}
2128
drhf7c57532003-04-25 13:22:51 +00002129/*
danielk197717221812005-02-15 03:38:05 +00002130** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002131** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002132** false or until the lock succeeds.
2133**
2134** Return SQLITE_OK on success and an error code if we cannot obtain
2135** the lock.
2136*/
2137static int pager_wait_on_lock(Pager *pPager, int locktype){
2138 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002139
2140 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002141 assert( PAGER_SHARED==SHARED_LOCK );
2142 assert( PAGER_RESERVED==RESERVED_LOCK );
2143 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002144
2145 /* If the file is currently unlocked then the size must be unknown */
2146 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2147
danielk197717221812005-02-15 03:38:05 +00002148 if( pPager->state>=locktype ){
2149 rc = SQLITE_OK;
2150 }else{
danielk197717221812005-02-15 03:38:05 +00002151 do {
drh054889e2005-11-30 03:20:31 +00002152 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002153 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002154 if( rc==SQLITE_OK ){
2155 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002156 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002157 }
2158 }
2159 return rc;
2160}
2161
2162/*
drhf7c57532003-04-25 13:22:51 +00002163** Truncate the file to the number of pages specified.
2164*/
danielk19773b8a05f2007-03-19 17:44:26 +00002165int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002166 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002167 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002168 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002169 if( pPager->errCode ){
2170 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002171 return rc;
2172 }
drh7d02cb72003-06-04 16:24:39 +00002173 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002174 return SQLITE_OK;
2175 }
drhb7f91642004-10-31 02:22:47 +00002176 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002177 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002178 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002179 return SQLITE_OK;
2180 }
danielk197776572402004-06-25 02:38:54 +00002181 rc = syncJournal(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002182 if( rc!=SQLITE_OK ){
2183 return rc;
2184 }
danielk197717221812005-02-15 03:38:05 +00002185
2186 /* Get an exclusive lock on the database before truncating. */
2187 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2188 if( rc!=SQLITE_OK ){
2189 return rc;
2190 }
2191
drhcb4c40b2004-08-18 19:09:43 +00002192 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002193 return rc;
2194}
2195
2196/*
drhed7c8552001-04-11 14:29:21 +00002197** Shutdown the page cache. Free all memory and close all files.
2198**
2199** If a transaction was in progress when this routine is called, that
2200** transaction is rolled back. All outstanding pages are invalidated
2201** and their memory is freed. Any attempt to use a page associated
2202** with this page cache after this function returns will likely
2203** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002204**
2205** This function always succeeds. If a transaction is active an attempt
2206** is made to roll it back. If an error occurs during the rollback
2207** a hot journal may be left in the filesystem but no error is returned
2208** to the caller.
drhed7c8552001-04-11 14:29:21 +00002209*/
danielk19773b8a05f2007-03-19 17:44:26 +00002210int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002211#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197754f01982006-01-18 15:25:17 +00002212 /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to
2213 ** malloc() must have already been made by this thread before it gets
2214 ** to this point. This means the ThreadData must have been allocated already
2215 ** so that ThreadData.nAlloc can be set.
2216 */
danielk19778212def2006-01-16 15:32:23 +00002217 ThreadData *pTsd = sqlite3ThreadData();
danielk197776e8d1a2006-01-18 18:22:43 +00002218 assert( pPager );
danielk197754f01982006-01-18 15:25:17 +00002219 assert( pTsd && pTsd->nAlloc );
danielk197713f72992005-12-18 08:51:22 +00002220#endif
2221
drhbafda092007-01-03 23:36:22 +00002222 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002223 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002224 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002225 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002226 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002227 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002228 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002229 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002230 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002231 if( pPager->journalOpen ){
drh054889e2005-11-30 03:20:31 +00002232 sqlite3OsClose(&pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002233 }
drh17435752007-08-16 04:30:38 +00002234 sqlite3_free(pPager->aInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002235 if( pPager->stmtOpen ){
drh054889e2005-11-30 03:20:31 +00002236 sqlite3OsClose(&pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002237 }
drh054889e2005-11-30 03:20:31 +00002238 sqlite3OsClose(&pPager->fd);
drh0f892532002-05-30 12:27:03 +00002239 /* Temp files are automatically deleted by the OS
2240 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002241 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002242 ** }
2243 */
danielk1977aca790a2005-01-13 11:07:52 +00002244
drh6f7adc82006-01-11 21:41:20 +00002245#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977aef0bf62005-12-30 16:28:01 +00002246 /* Remove the pager from the linked list of pagers starting at
danielk197752622822006-01-09 09:59:49 +00002247 ** ThreadData.pPager if memory-management is enabled.
danielk1977aef0bf62005-12-30 16:28:01 +00002248 */
danielk19778212def2006-01-16 15:32:23 +00002249 if( pPager==pTsd->pPager ){
2250 pTsd->pPager = pPager->pNext;
2251 }else{
2252 Pager *pTmp;
drh74161702006-02-24 02:53:49 +00002253 for(pTmp = pTsd->pPager; pTmp->pNext!=pPager; pTmp=pTmp->pNext){}
danielk19778212def2006-01-16 15:32:23 +00002254 pTmp->pNext = pPager->pNext;
danielk197713f72992005-12-18 08:51:22 +00002255 }
2256#endif
drh17435752007-08-16 04:30:38 +00002257 sqlite3_free(pPager->aHash);
2258 sqlite3_free(pPager->pTmpSpace);
2259 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002260 return SQLITE_OK;
2261}
2262
drh87cc3b32007-05-08 21:45:27 +00002263#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002264/*
drh5e00f6c2001-09-13 13:46:56 +00002265** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002266*/
danielk19773b8a05f2007-03-19 17:44:26 +00002267Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002268 return p->pgno;
2269}
drh87cc3b32007-05-08 21:45:27 +00002270#endif
drhed7c8552001-04-11 14:29:21 +00002271
2272/*
drhc8629a12004-05-08 20:07:40 +00002273** The page_ref() function increments the reference count for a page.
2274** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002275** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002276**
2277** For non-test systems, page_ref() is a macro that calls _page_ref()
2278** online of the reference count is zero. For test systems, page_ref()
2279** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002280*/
drh836faa42003-01-11 13:30:57 +00002281static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002282 if( pPg->nRef==0 ){
2283 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +00002284 if( pPg==pPg->pPager->pFirstSynced ){
2285 PgHdr *p = pPg->pNextFree;
2286 while( p && p->needSync ){ p = p->pNextFree; }
2287 pPg->pPager->pFirstSynced = p;
2288 }
drh7e3b0a02001-04-28 16:52:40 +00002289 if( pPg->pPrevFree ){
2290 pPg->pPrevFree->pNextFree = pPg->pNextFree;
2291 }else{
2292 pPg->pPager->pFirst = pPg->pNextFree;
2293 }
2294 if( pPg->pNextFree ){
2295 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
2296 }else{
2297 pPg->pPager->pLast = pPg->pPrevFree;
2298 }
2299 pPg->pPager->nRef++;
2300 }
2301 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00002302 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00002303}
danielk1977aca790a2005-01-13 11:07:52 +00002304#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002305 static void page_ref(PgHdr *pPg){
2306 if( pPg->nRef==0 ){
2307 _page_ref(pPg);
2308 }else{
2309 pPg->nRef++;
2310 REFINFO(pPg);
2311 }
2312 }
2313#else
2314# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2315#endif
drhdf0b3b02001-06-23 11:36:20 +00002316
2317/*
2318** Increment the reference count for a page. The input pointer is
2319** a reference to the page data.
2320*/
danielk19773b8a05f2007-03-19 17:44:26 +00002321int sqlite3PagerRef(DbPage *pPg){
drhdf0b3b02001-06-23 11:36:20 +00002322 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +00002323 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002324}
2325
2326/*
drh34e79ce2004-02-08 06:05:46 +00002327** Sync the journal. In other words, make sure all the pages that have
2328** been written to the journal have actually reached the surface of the
2329** disk. It is not safe to modify the original database file until after
2330** the journal has been synced. If the original database is modified before
2331** the journal is synced and a power failure occurs, the unsynced journal
2332** data would be lost and we would be unable to completely rollback the
2333** database changes. Database corruption would occur.
2334**
2335** This routine also updates the nRec field in the header of the journal.
2336** (See comments on the pager_playback() routine for additional information.)
2337** If the sync mode is FULL, two syncs will occur. First the whole journal
2338** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002339**
drh34e79ce2004-02-08 06:05:46 +00002340** For temporary databases, we do not care if we are able to rollback
2341** after a power failure, so sync occurs.
drhfa86c412002-02-02 15:01:15 +00002342**
drh34e79ce2004-02-08 06:05:46 +00002343** This routine clears the needSync field of every page current held in
2344** memory.
drh50e5dad2001-09-15 00:57:28 +00002345*/
danielk197776572402004-06-25 02:38:54 +00002346static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002347 PgHdr *pPg;
2348 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002349
2350 /* Sync the journal before modifying the main database
2351 ** (assuming there is a journal and it needs to be synced.)
2352 */
danielk197776572402004-06-25 02:38:54 +00002353 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002354 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00002355 assert( pPager->journalOpen );
drh946966f2004-02-25 02:20:41 +00002356 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2357 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00002358#ifndef NDEBUG
2359 {
drh34e79ce2004-02-08 06:05:46 +00002360 /* Make sure the pPager->nRec counter we are keeping agrees
2361 ** with the nRec computed from the size of the journal file.
2362 */
drheb206252004-10-01 02:00:31 +00002363 i64 jSz;
drh054889e2005-11-30 03:20:31 +00002364 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00002365 if( rc!=0 ) return rc;
danielk197776572402004-06-25 02:38:54 +00002366 assert( pPager->journalOff==jSz );
drh968af522003-02-11 14:55:40 +00002367 }
2368#endif
drhae2b40c2004-06-09 19:03:54 +00002369 {
danielk197762079062007-08-15 17:08:46 +00002370 i64 jrnlOff;
2371
danielk197776572402004-06-25 02:38:54 +00002372 /* Write the nRec value into the journal file header. If in
2373 ** full-synchronous mode, sync the journal first. This ensures that
2374 ** all data has really hit the disk before nRec is updated to mark
2375 ** it as a candidate for rollback.
2376 */
drhd8d66e82003-02-12 02:10:15 +00002377 if( pPager->fullSync ){
drh4f0c5872007-03-26 22:05:01 +00002378 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002379 IOTRACE(("JSYNC %p\n", pPager))
drh054889e2005-11-30 03:20:31 +00002380 rc = sqlite3OsSync(pPager->jfd, 0);
drhd8d66e82003-02-12 02:10:15 +00002381 if( rc!=0 ) return rc;
2382 }
danielk197713adf8a2004-06-03 16:08:41 +00002383
danielk197762079062007-08-15 17:08:46 +00002384 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2385 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2386 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002387 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002388 }
drh4f0c5872007-03-26 22:05:01 +00002389 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drh126afe62007-05-04 12:01:02 +00002390 IOTRACE(("JSYNC %p\n", pPager))
drhac530b12006-02-11 01:25:50 +00002391 rc = sqlite3OsSync(pPager->jfd, pPager->full_fsync);
drhfa86c412002-02-02 15:01:15 +00002392 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00002393 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002394 }
drh50e5dad2001-09-15 00:57:28 +00002395 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002396
2397 /* Erase the needSync flag from every page.
2398 */
2399 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2400 pPg->needSync = 0;
2401 }
2402 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00002403 }
drh03eb96a2002-11-10 23:32:56 +00002404
drh341eae82003-01-21 02:39:36 +00002405#ifndef NDEBUG
2406 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2407 ** flag must also be clear for all pages. Verify that this
2408 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002409 */
drh341eae82003-01-21 02:39:36 +00002410 else{
2411 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2412 assert( pPg->needSync==0 );
2413 }
2414 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00002415 }
drh341eae82003-01-21 02:39:36 +00002416#endif
drhdb48ee02003-01-16 13:42:43 +00002417
drh81a20f22001-10-12 17:30:04 +00002418 return rc;
drh50e5dad2001-09-15 00:57:28 +00002419}
2420
2421/*
drhdc3e10b2006-06-15 14:31:06 +00002422** Merge two lists of pages connected by pDirty and in pgno order.
2423** Do not both fixing the pPrevDirty pointers.
2424*/
2425static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2426 PgHdr result, *pTail;
2427 pTail = &result;
2428 while( pA && pB ){
2429 if( pA->pgno<pB->pgno ){
2430 pTail->pDirty = pA;
2431 pTail = pA;
2432 pA = pA->pDirty;
2433 }else{
2434 pTail->pDirty = pB;
2435 pTail = pB;
2436 pB = pB->pDirty;
2437 }
2438 }
2439 if( pA ){
2440 pTail->pDirty = pA;
2441 }else if( pB ){
2442 pTail->pDirty = pB;
2443 }else{
2444 pTail->pDirty = 0;
2445 }
2446 return result.pDirty;
2447}
2448
2449/*
2450** Sort the list of pages in accending order by pgno. Pages are
2451** connected by pDirty pointers. The pPrevDirty pointers are
2452** corrupted by this sort.
2453*/
danielk197724168722007-04-02 05:07:47 +00002454#define N_SORT_BUCKET_ALLOC 25
2455#define N_SORT_BUCKET 25
2456#ifdef SQLITE_TEST
2457 int sqlite3_pager_n_sort_bucket = 0;
2458 #undef N_SORT_BUCKET
2459 #define N_SORT_BUCKET \
2460 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2461#endif
drhdc3e10b2006-06-15 14:31:06 +00002462static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002463 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002464 int i;
2465 memset(a, 0, sizeof(a));
2466 while( pIn ){
2467 p = pIn;
2468 pIn = p->pDirty;
2469 p->pDirty = 0;
2470 for(i=0; i<N_SORT_BUCKET-1; i++){
2471 if( a[i]==0 ){
2472 a[i] = p;
2473 break;
2474 }else{
2475 p = merge_pagelist(a[i], p);
2476 a[i] = 0;
2477 }
2478 }
2479 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002480 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2481 ** elements in the input list. This is possible, but impractical.
2482 ** Testing this line is the point of global variable
2483 ** sqlite3_pager_n_sort_bucket.
2484 */
drhdc3e10b2006-06-15 14:31:06 +00002485 a[i] = merge_pagelist(a[i], p);
2486 }
2487 }
2488 p = a[0];
2489 for(i=1; i<N_SORT_BUCKET; i++){
2490 p = merge_pagelist(p, a[i]);
2491 }
2492 return p;
2493}
2494
2495/*
drh2554f8b2003-01-22 01:26:44 +00002496** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2497** every one of those pages out to the database file and mark them all
2498** as clean.
2499*/
2500static int pager_write_pagelist(PgHdr *pList){
2501 Pager *pPager;
2502 int rc;
2503
2504 if( pList==0 ) return SQLITE_OK;
2505 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002506
2507 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2508 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002509 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002510 **
drha6abd042004-06-09 17:37:22 +00002511 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2512 ** through an intermediate state PENDING. A PENDING lock prevents new
2513 ** readers from attaching to the database but is unsufficient for us to
2514 ** write. The idea of a PENDING lock is to prevent new readers from
2515 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002516 **
drha6abd042004-06-09 17:37:22 +00002517 ** While the pager is in the RESERVED state, the original database file
2518 ** is unchanged and we can rollback without having to playback the
2519 ** journal into the original database file. Once we transition to
2520 ** EXCLUSIVE, it means the database file has been changed and any rollback
2521 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002522 */
drh684917c2004-10-05 02:41:42 +00002523 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002524 if( rc!=SQLITE_OK ){
2525 return rc;
2526 }
2527
drhdc3e10b2006-06-15 14:31:06 +00002528 pList = sort_pagelist(pList);
drh2554f8b2003-01-22 01:26:44 +00002529 while( pList ){
2530 assert( pList->dirty );
danielk1977687566d2004-11-02 12:56:41 +00002531 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002532 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002533 ** make the file smaller (presumably by auto-vacuum code). Do not write
2534 ** any such pages to the file.
2535 */
2536 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00002537 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00002538 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002539 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2540 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002541 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002542 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002543 PAGER_INCR(sqlite3_pager_writedb_count);
2544 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002545 if( pList->pgno==1 ){
2546 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2547 }
danielk1977687566d2004-11-02 12:56:41 +00002548 }
2549#ifndef NDEBUG
2550 else{
drh4f0c5872007-03-26 22:05:01 +00002551 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002552 }
2553#endif
drh2554f8b2003-01-22 01:26:44 +00002554 if( rc ) return rc;
2555 pList->dirty = 0;
danielk19773c407372005-02-15 02:54:14 +00002556#ifdef SQLITE_CHECK_PAGES
2557 pList->pageHash = pager_pagehash(pList);
2558#endif
drh2554f8b2003-01-22 01:26:44 +00002559 pList = pList->pDirty;
2560 }
2561 return SQLITE_OK;
2562}
2563
2564/*
2565** Collect every dirty page into a dirty list and
2566** return a pointer to the head of that list. All pages are
2567** collected even if they are still in use.
2568*/
2569static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh605ad8f2006-05-03 23:34:05 +00002570 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00002571}
2572
2573/*
drh165ffe92005-03-15 17:09:30 +00002574** Return TRUE if there is a hot journal on the given pager.
2575** A hot journal is one that needs to be played back.
2576**
2577** If the current size of the database file is 0 but a journal file
2578** exists, that is probably an old journal left over from a prior
2579** database with the same name. Just delete the journal.
2580*/
2581static int hasHotJournal(Pager *pPager){
2582 if( !pPager->useJournal ) return 0;
drhbb5f18d2007-04-06 18:23:17 +00002583 if( !sqlite3OsFileExists(pPager->zJournal) ){
2584 return 0;
2585 }
2586 if( sqlite3OsCheckReservedLock(pPager->fd) ){
2587 return 0;
2588 }
danielk19773b8a05f2007-03-19 17:44:26 +00002589 if( sqlite3PagerPagecount(pPager)==0 ){
drh66560ad2006-01-06 14:32:19 +00002590 sqlite3OsDelete(pPager->zJournal);
drh165ffe92005-03-15 17:09:30 +00002591 return 0;
2592 }else{
2593 return 1;
2594 }
2595}
2596
danielk19775591df52005-12-20 09:19:37 +00002597/*
danielk1977aef0bf62005-12-30 16:28:01 +00002598** Try to find a page in the cache that can be recycled.
2599**
2600** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00002601** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00002602*/
danielk197713f72992005-12-18 08:51:22 +00002603static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
2604 PgHdr *pPg;
2605 *ppPg = 0;
2606
danielk1977c551edc2007-04-05 13:12:13 +00002607 assert(!MEMDB);
2608
danielk197713f72992005-12-18 08:51:22 +00002609 /* Find a page to recycle. Try to locate a page that does not
2610 ** require us to do an fsync() on the journal.
2611 */
2612 pPg = pPager->pFirstSynced;
2613
2614 /* If we could not find a page that does not require an fsync()
2615 ** on the journal file then fsync the journal file. This is a
2616 ** very slow operation, so we work hard to avoid it. But sometimes
2617 ** it can't be helped.
2618 */
drh8940f4e2007-08-11 00:26:20 +00002619 if( pPg==0 && pPager->pFirst && syncOk && !MEMDB){
danielk197713f72992005-12-18 08:51:22 +00002620 int rc = syncJournal(pPager);
2621 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002622 return rc;
danielk197713f72992005-12-18 08:51:22 +00002623 }
2624 if( pPager->fullSync ){
2625 /* If in full-sync mode, write a new journal header into the
2626 ** journal file. This is done to avoid ever modifying a journal
2627 ** header that is involved in the rollback of pages that have
2628 ** already been written to the database (in case the header is
2629 ** trashed when the nRec field is updated).
2630 */
2631 pPager->nRec = 0;
2632 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00002633 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00002634 rc = writeJournalHdr(pPager);
2635 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002636 return rc;
danielk197713f72992005-12-18 08:51:22 +00002637 }
2638 }
2639 pPg = pPager->pFirst;
2640 }
2641 if( pPg==0 ){
2642 return SQLITE_OK;
2643 }
2644
2645 assert( pPg->nRef==0 );
2646
2647 /* Write the page to the database file if it is dirty.
2648 */
2649 if( pPg->dirty ){
2650 int rc;
2651 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00002652 makeClean(pPg);
2653 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00002654 pPg->pDirty = 0;
2655 rc = pager_write_pagelist( pPg );
2656 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00002657 return rc;
danielk197713f72992005-12-18 08:51:22 +00002658 }
2659 }
2660 assert( pPg->dirty==0 );
2661
2662 /* If the page we are recycling is marked as alwaysRollback, then
2663 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00002664 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00002665 ** It is necessary to do this because the page marked alwaysRollback
2666 ** might be reloaded at a later time but at that point we won't remember
2667 ** that is was marked alwaysRollback. This means that all pages must
2668 ** be marked as alwaysRollback from here on out.
2669 */
2670 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00002671 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00002672 pPager->alwaysRollback = 1;
2673 }
2674
2675 /* Unlink the old page from the free list and the hash table
2676 */
2677 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00002678 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00002679
2680 *ppPg = pPg;
2681 return SQLITE_OK;
2682}
2683
2684/*
2685** This function is called to free superfluous dynamically allocated memory
2686** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00002687** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00002688**
2689** nReq is the number of bytes of memory required. Once this much has
danielk19770190d1d2005-12-19 14:18:11 +00002690** been released, the function returns. A negative value for nReq means
2691** free as much memory as possible. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00002692** of bytes of memory released.
2693*/
drh87cc3b32007-05-08 21:45:27 +00002694#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
danielk19773b8a05f2007-03-19 17:44:26 +00002695int sqlite3PagerReleaseMemory(int nReq){
drh6f7adc82006-01-11 21:41:20 +00002696 const ThreadData *pTsdro = sqlite3ThreadDataReadOnly();
danielk197713f72992005-12-18 08:51:22 +00002697 int nReleased = 0;
2698 int i;
2699
drh29c636b2006-01-09 23:40:25 +00002700 /* If the the global mutex is held, this subroutine becomes a
2701 ** o-op; zero bytes of memory are freed. This is because
2702 ** some of the code invoked by this function may also
2703 ** try to obtain the mutex, resulting in a deadlock.
danielk1977441b09a2006-01-05 13:48:29 +00002704 */
drh757b04e2006-01-18 17:25:45 +00002705 if( sqlite3OsInMutex(0) ){
danielk1977441b09a2006-01-05 13:48:29 +00002706 return 0;
2707 }
2708
danielk197713f72992005-12-18 08:51:22 +00002709 /* Outermost loop runs for at most two iterations. First iteration we
2710 ** try to find memory that can be released without calling fsync(). Second
2711 ** iteration (which only runs if the first failed to free nReq bytes of
2712 ** memory) is permitted to call fsync(). This is of course much more
2713 ** expensive.
2714 */
drh29c636b2006-01-09 23:40:25 +00002715 for(i=0; i<=1; i++){
danielk197713f72992005-12-18 08:51:22 +00002716
2717 /* Loop through all the SQLite pagers opened by the current thread. */
danielk197738ec0cb2007-04-05 14:29:42 +00002718 Pager *pPager = pTsdro->pPager;
2719 for( ; pPager && (nReq<0 || nReleased<nReq); pPager=pPager->pNext){
danielk197713f72992005-12-18 08:51:22 +00002720 PgHdr *pPg;
2721 int rc;
2722
danielk197738ec0cb2007-04-05 14:29:42 +00002723 if( MEMDB ){
danielk1977c551edc2007-04-05 13:12:13 +00002724 continue;
2725 }
danielk1977c551edc2007-04-05 13:12:13 +00002726
danielk197713f72992005-12-18 08:51:22 +00002727 /* For each pager, try to free as many pages as possible (without
2728 ** calling fsync() if this is the first iteration of the outermost
2729 ** loop).
2730 */
danielk197738ec0cb2007-04-05 14:29:42 +00002731 while( SQLITE_OK==(rc = pager_recycle(pPager, i, &pPg)) && pPg) {
danielk1977aef0bf62005-12-30 16:28:01 +00002732 /* We've found a page to free. At this point the page has been
danielk197713f72992005-12-18 08:51:22 +00002733 ** removed from the page hash-table, free-list and synced-list
danielk1977aef0bf62005-12-30 16:28:01 +00002734 ** (pFirstSynced). It is still in the all pages (pAll) list.
danielk197713f72992005-12-18 08:51:22 +00002735 ** Remove it from this list before freeing.
2736 **
2737 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
2738 ** probably is though.
2739 */
2740 PgHdr *pTmp;
danielk19770190d1d2005-12-19 14:18:11 +00002741 assert( pPg );
danielk197738ec0cb2007-04-05 14:29:42 +00002742 if( pPg==pPager->pAll ){
2743 pPager->pAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00002744 }else{
danielk197738ec0cb2007-04-05 14:29:42 +00002745 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
danielk197713f72992005-12-18 08:51:22 +00002746 pTmp->pNextAll = pPg->pNextAll;
2747 }
2748 nReleased += sqliteAllocSize(pPg);
drh34f56212007-08-10 23:56:35 +00002749 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
drh538f5702007-04-13 02:14:30 +00002750 PAGER_INCR(sqlite3_pager_pgfree_count);
drh17435752007-08-16 04:30:38 +00002751 sqlite3_free(pPg);
danielk197713f72992005-12-18 08:51:22 +00002752 }
2753
2754 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00002755 /* An error occured whilst writing to the database file or
2756 ** journal in pager_recycle(). The error is not returned to the
danielk1977efaaf572006-01-16 11:29:19 +00002757 ** caller of this function. Instead, set the Pager.errCode variable.
danielk1977aef0bf62005-12-30 16:28:01 +00002758 ** The error will be returned to the user (or users, in the case
2759 ** of a shared pager cache) of the pager for which the error occured.
danielk197713f72992005-12-18 08:51:22 +00002760 */
drh34f56212007-08-10 23:56:35 +00002761 assert(
2762 (rc&0xff)==SQLITE_IOERR ||
2763 rc==SQLITE_FULL ||
2764 rc==SQLITE_BUSY
2765 );
danielk197738ec0cb2007-04-05 14:29:42 +00002766 assert( pPager->state>=PAGER_RESERVED );
2767 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00002768 }
2769 }
2770 }
danielk1977aef0bf62005-12-30 16:28:01 +00002771
danielk197713f72992005-12-18 08:51:22 +00002772 return nReleased;
2773}
drh87cc3b32007-05-08 21:45:27 +00002774#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT && !SQLITE_OMIT_DISKIO */
danielk197713f72992005-12-18 08:51:22 +00002775
drh165ffe92005-03-15 17:09:30 +00002776/*
danielk1977e180dd92007-04-05 17:15:52 +00002777** Read the content of page pPg out of the database file.
2778*/
2779static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2780 int rc;
danielk197762079062007-08-15 17:08:46 +00002781 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002782 assert( MEMDB==0 );
danielk197762079062007-08-15 17:08:46 +00002783 offset = (pgno-1)*(i64)pPager->pageSize;
2784 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002785 PAGER_INCR(sqlite3_pager_readdb_count);
2786 PAGER_INCR(pPager->nRead);
2787 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002788 if( pgno==1 ){
2789 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
2790 sizeof(pPager->dbFileVers));
2791 }
danielk1977e180dd92007-04-05 17:15:52 +00002792 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00002793 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
2794 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00002795 return rc;
2796}
2797
2798
2799/*
danielk1977e277be02007-03-23 18:12:06 +00002800** This function is called to obtain the shared lock required before
2801** data may be read from the pager cache. If the shared lock has already
2802** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002803**
2804** Immediately after obtaining the shared lock (if required), this function
2805** checks for a hot-journal file. If one is found, an emergency rollback
2806** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002807*/
2808static int pagerSharedLock(Pager *pPager){
2809 int rc = SQLITE_OK;
2810
2811 if( pPager->state==PAGER_UNLOCK ){
2812 if( !MEMDB ){
2813 assert( pPager->nRef==0 );
2814 if( !pPager->noReadlock ){
2815 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2816 if( rc!=SQLITE_OK ){
2817 return pager_error(pPager, rc);
2818 }
2819 assert( pPager->state>=SHARED_LOCK );
2820 }
2821
2822 /* If a journal file exists, and there is no RESERVED lock on the
2823 ** database file, then it either needs to be played back or deleted.
2824 */
2825 if( hasHotJournal(pPager) ){
2826 /* Get an EXCLUSIVE lock on the database file. At this point it is
2827 ** important that a RESERVED lock is not obtained on the way to the
2828 ** EXCLUSIVE lock. If it were, another process might open the
2829 ** database file, detect the RESERVED lock, and conclude that the
2830 ** database is safe to read while this process is still rolling it
2831 ** back.
2832 **
2833 ** Because the intermediate RESERVED lock is not requested, the
2834 ** second process will get to this point in the code and fail to
2835 ** obtain it's own EXCLUSIVE lock on the database file.
2836 */
2837 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
2838 if( rc!=SQLITE_OK ){
2839 pager_unlock(pPager);
2840 return pager_error(pPager, rc);
2841 }
2842 pPager->state = PAGER_EXCLUSIVE;
2843
2844 /* Open the journal for reading only. Return SQLITE_BUSY if
2845 ** we are unable to open the journal file.
2846 **
2847 ** The journal file does not need to be locked itself. The
2848 ** journal file is never open unless the main database file holds
2849 ** a write lock, so there is never any chance of two or more
2850 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00002851 **
drhfd131da2007-08-07 17:13:03 +00002852 ** Open the journal for read/write access. This is because in
2853 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00002854 ** possibly used for a transaction later on. On some systems, the
2855 ** OsTruncate() call used in exclusive-access mode also requires
2856 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00002857 */
danielk1977979f38e2007-03-27 16:19:51 +00002858 rc = SQLITE_BUSY;
2859 if( sqlite3OsFileExists(pPager->zJournal) ){
2860 int ro;
danielk19777152de82007-03-29 17:28:14 +00002861 assert( !pPager->tempFile );
danielk1977979f38e2007-03-27 16:19:51 +00002862 rc = sqlite3OsOpenReadWrite(pPager->zJournal, &pPager->jfd, &ro);
danielk19775bb16fe2007-04-05 11:54:42 +00002863 assert( rc!=SQLITE_OK || pPager->jfd );
danielk1977979f38e2007-03-27 16:19:51 +00002864 if( ro ){
2865 rc = SQLITE_BUSY;
danielk197708d31a22007-04-02 11:08:58 +00002866 sqlite3OsClose(&pPager->jfd);
danielk1977979f38e2007-03-27 16:19:51 +00002867 }
2868 }
danielk1977e277be02007-03-23 18:12:06 +00002869 if( rc!=SQLITE_OK ){
2870 pager_unlock(pPager);
2871 return SQLITE_BUSY;
2872 }
2873 pPager->journalOpen = 1;
2874 pPager->journalStarted = 0;
2875 pPager->journalOff = 0;
2876 pPager->setMaster = 0;
2877 pPager->journalHdr = 0;
2878
2879 /* Playback and delete the journal. Drop the database write
2880 ** lock and reacquire the read lock.
2881 */
2882 rc = pager_playback(pPager, 1);
2883 if( rc!=SQLITE_OK ){
2884 return pager_error(pPager, rc);
2885 }
danielk1977c5859712007-03-26 12:26:27 +00002886 assert(pPager->state==PAGER_SHARED ||
2887 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
2888 );
danielk1977e277be02007-03-23 18:12:06 +00002889 }
2890
2891 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00002892 /* The shared-lock has just been acquired on the database file
2893 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00002894 ** read or write transaction). Check to see if the database
2895 ** has been modified. If the database has changed, flush the
2896 ** cache.
2897 **
2898 ** Database changes is detected by looking at 15 bytes beginning
2899 ** at offset 24 into the file. The first 4 of these 16 bytes are
2900 ** a 32-bit counter that is incremented with each change. The
2901 ** other bytes change randomly with each file change when
2902 ** a codec is in use.
2903 **
2904 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00002905 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00002906 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00002907 */
drh86a88112007-04-16 15:02:19 +00002908 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00002909 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00002910
danielk1977e180dd92007-04-05 17:15:52 +00002911 if( pPager->errCode ){
2912 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00002913 }
2914
danielk1977e180dd92007-04-05 17:15:52 +00002915 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00002916 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00002917 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00002918 if( rc!=SQLITE_OK ){
2919 return rc;
2920 }
drh86a88112007-04-16 15:02:19 +00002921 }else{
2922 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00002923 }
2924
drh86a88112007-04-16 15:02:19 +00002925 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00002926 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002927 }
2928 }
2929 }
danielk1977c5859712007-03-26 12:26:27 +00002930 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
2931 if( pPager->state==PAGER_UNLOCK ){
2932 pPager->state = PAGER_SHARED;
2933 }
danielk1977e277be02007-03-23 18:12:06 +00002934 }
2935
2936 return rc;
2937}
2938
2939/*
danielk1977e180dd92007-04-05 17:15:52 +00002940** Allocate a PgHdr object. Either create a new one or reuse
2941** an existing one that is not otherwise in use.
2942**
2943** A new PgHdr structure is created if any of the following are
2944** true:
2945**
2946** (1) We have not exceeded our maximum allocated cache size
2947** as set by the "PRAGMA cache_size" command.
2948**
2949** (2) There are no unused PgHdr objects available at this time.
2950**
2951** (3) This is an in-memory database.
2952**
2953** (4) There are no PgHdr objects that do not require a journal
2954** file sync and a sync of the journal file is currently
2955** prohibited.
2956**
2957** Otherwise, reuse an existing PgHdr. In other words, reuse an
2958** existing PgHdr if all of the following are true:
2959**
2960** (1) We have reached or exceeded the maximum cache size
2961** allowed by "PRAGMA cache_size".
2962**
2963** (2) There is a PgHdr available with PgHdr->nRef==0
2964**
2965** (3) We are not in an in-memory database
2966**
2967** (4) Either there is an available PgHdr that does not need
2968** to be synced to disk or else disk syncing is currently
2969** allowed.
danielk197724168722007-04-02 05:07:47 +00002970*/
2971static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
2972 int rc = SQLITE_OK;
2973 PgHdr *pPg;
2974
danielk1977e180dd92007-04-05 17:15:52 +00002975 /* Create a new PgHdr if any of the four conditions defined
2976 ** above is met: */
2977 if( pPager->nPage<pPager->mxPage
2978 || pPager->pFirst==0
2979 || MEMDB
2980 || (pPager->pFirstSynced==0 && pPager->doNotSync)
2981 ){
danielk197724168722007-04-02 05:07:47 +00002982 if( pPager->nPage>=pPager->nHash ){
2983 pager_resize_hash_table(pPager,
2984 pPager->nHash<256 ? 256 : pPager->nHash*2);
2985 if( pPager->nHash==0 ){
2986 rc = SQLITE_NOMEM;
2987 goto pager_allocate_out;
2988 }
2989 }
drh17435752007-08-16 04:30:38 +00002990 pPg = sqlite3_malloc( sizeof(*pPg) + pPager->pageSize
danielk197724168722007-04-02 05:07:47 +00002991 + sizeof(u32) + pPager->nExtra
2992 + MEMDB*sizeof(PgHistory) );
2993 if( pPg==0 ){
2994 rc = SQLITE_NOMEM;
2995 goto pager_allocate_out;
2996 }
2997 memset(pPg, 0, sizeof(*pPg));
2998 if( MEMDB ){
2999 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
3000 }
3001 pPg->pPager = pPager;
3002 pPg->pNextAll = pPager->pAll;
3003 pPager->pAll = pPg;
3004 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003005 }else{
3006 /* Recycle an existing page with a zero ref-count. */
3007 rc = pager_recycle(pPager, 1, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003008 if( rc==SQLITE_BUSY ){
3009 rc = SQLITE_IOERR_BLOCKED;
3010 }
danielk197724168722007-04-02 05:07:47 +00003011 if( rc!=SQLITE_OK ){
3012 goto pager_allocate_out;
3013 }
3014 assert( pPager->state>=SHARED_LOCK );
3015 assert(pPg);
3016 }
3017 *ppPg = pPg;
3018
3019pager_allocate_out:
3020 return rc;
3021}
3022
3023/*
drhd33d5a82007-04-26 12:11:28 +00003024** Make sure we have the content for a page. If the page was
3025** previously acquired with noContent==1, then the content was
3026** just initialized to zeros instead of being read from disk.
3027** But now we need the real data off of disk. So make sure we
3028** have it. Read it in if we do not have it already.
3029*/
3030static int pager_get_content(PgHdr *pPg){
3031 if( pPg->needRead ){
3032 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3033 if( rc==SQLITE_OK ){
3034 pPg->needRead = 0;
3035 }else{
3036 return rc;
3037 }
3038 }
3039 return SQLITE_OK;
3040}
3041
3042/*
drhd9b02572001-04-15 00:37:09 +00003043** Acquire a page.
3044**
drh58a11682001-11-10 13:51:08 +00003045** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003046** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003047**
drhd33d5a82007-04-26 12:11:28 +00003048** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003049** file is smaller than the requested page, then no actual disk
3050** read occurs and the memory image of the page is initialized to
3051** all zeros. The extra data appended to a page is always initialized
3052** to zeros the first time a page is loaded into memory.
3053**
drhd9b02572001-04-15 00:37:09 +00003054** The acquisition might fail for several reasons. In all cases,
3055** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003056**
drhd33d5a82007-04-26 12:11:28 +00003057** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003058** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003059** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003060** just returns 0. This routine acquires a read-lock the first time it
3061** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003062** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003063** or journal files.
drh0787db62007-03-04 13:15:27 +00003064**
drh538f5702007-04-13 02:14:30 +00003065** If noContent is false, the page contents are actually read from disk.
3066** If noContent is true, it means that we do not care about the contents
3067** of the page at this time, so do not do a disk read. Just fill in the
3068** page content with zeros. But mark the fact that we have not read the
3069** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003070** sqlite3PagerWrite() is called on this page or if this routine is
3071** called again with noContent==0, that means that the content is needed
3072** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003073*/
drh538f5702007-04-13 02:14:30 +00003074int sqlite3PagerAcquire(
3075 Pager *pPager, /* The pager open on the database file */
3076 Pgno pgno, /* Page number to fetch */
3077 DbPage **ppPage, /* Write a pointer to the page here */
3078 int noContent /* Do not bother reading content from disk if true */
3079){
drhed7c8552001-04-11 14:29:21 +00003080 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003081 int rc;
drhed7c8552001-04-11 14:29:21 +00003082
danielk1977e277be02007-03-23 18:12:06 +00003083 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3084
danielk197726836652005-01-17 01:33:13 +00003085 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3086 ** number greater than this, or zero, is requested.
3087 */
drh267cb322005-09-16 17:16:52 +00003088 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003089 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003090 }
3091
drhd9b02572001-04-15 00:37:09 +00003092 /* Make sure we have not hit any critical errors.
3093 */
drh836faa42003-01-11 13:30:57 +00003094 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003095 *ppPage = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003096 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3097 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003098 }
3099
danielk197713adf8a2004-06-03 16:08:41 +00003100 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003101 ** on the database file. pagerSharedLock() is a no-op if
3102 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003103 */
danielk1977e277be02007-03-23 18:12:06 +00003104 rc = pagerSharedLock(pPager);
3105 if( rc!=SQLITE_OK ){
3106 return rc;
drhed7c8552001-04-11 14:29:21 +00003107 }
danielk1977e277be02007-03-23 18:12:06 +00003108 assert( pPager->state!=PAGER_UNLOCK );
3109
3110 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003111 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003112 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003113 int nMax;
drhed7c8552001-04-11 14:29:21 +00003114 int h;
drh538f5702007-04-13 02:14:30 +00003115 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003116 rc = pagerAllocatePage(pPager, &pPg);
3117 if( rc!=SQLITE_OK ){
3118 return rc;
drhed7c8552001-04-11 14:29:21 +00003119 }
danielk197724168722007-04-02 05:07:47 +00003120
drhed7c8552001-04-11 14:29:21 +00003121 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003122 assert( !MEMDB || pgno>pPager->stmtSize );
drh1ab43002002-01-14 09:28:19 +00003123 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19774adee202004-05-08 08:23:19 +00003124 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
drhdb48ee02003-01-16 13:42:43 +00003125 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00003126 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00003127 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003128 }else{
3129 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00003130 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003131 }
danielk1977f35843b2007-04-07 15:03:17 +00003132
drh605ad8f2006-05-03 23:34:05 +00003133 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003134 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00003135 REFINFO(pPg);
danielk197775bab7d2006-01-23 13:09:45 +00003136
drhd9b02572001-04-15 00:37:09 +00003137 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003138 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003139 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003140 }
danielk1977979f38e2007-03-27 16:19:51 +00003141 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003142 if( pPager->errCode ){
danielk19773b8a05f2007-03-19 17:44:26 +00003143 sqlite3PagerUnref(pPg);
danielk1977efaaf572006-01-16 11:29:19 +00003144 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00003145 return rc;
3146 }
danielk197775bab7d2006-01-23 13:09:45 +00003147
3148 /* Populate the page with data, either by reading from the database
3149 ** file, or by setting the entire page to zero.
3150 */
drhd215acf2007-04-13 04:01:58 +00003151 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003152 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003153 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003154 return SQLITE_FULL;
3155 }
drh90f5ecb2004-07-22 01:19:35 +00003156 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003157 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003158 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003159 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003160 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003161 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3162 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003163 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003164 return rc;
drh81a20f22001-10-12 17:30:04 +00003165 }
danielk1977b4626a32007-04-28 15:47:43 +00003166 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003167 }
danielk197775bab7d2006-01-23 13:09:45 +00003168
3169 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003170 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003171 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003172 pPg->pNextHash = pPager->aHash[h];
3173 pPager->aHash[h] = pPg;
3174 if( pPg->pNextHash ){
3175 assert( pPg->pNextHash->pPrevHash==0 );
3176 pPg->pNextHash->pPrevHash = pPg;
3177 }
3178
danielk19773c407372005-02-15 02:54:14 +00003179#ifdef SQLITE_CHECK_PAGES
3180 pPg->pageHash = pager_pagehash(pPg);
3181#endif
drhed7c8552001-04-11 14:29:21 +00003182 }else{
drhd9b02572001-04-15 00:37:09 +00003183 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003184 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003185 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003186 if( !noContent ){
3187 rc = pager_get_content(pPg);
3188 if( rc ){
3189 return rc;
3190 }
3191 }
drhdf0b3b02001-06-23 11:36:20 +00003192 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003193 }
danielk19773b8a05f2007-03-19 17:44:26 +00003194 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003195 return SQLITE_OK;
3196}
3197
3198/*
drh7e3b0a02001-04-28 16:52:40 +00003199** Acquire a page if it is already in the in-memory cache. Do
3200** not read the page from disk. Return a pointer to the page,
3201** or 0 if the page is not in cache.
3202**
danielk19773b8a05f2007-03-19 17:44:26 +00003203** See also sqlite3PagerGet(). The difference between this routine
3204** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003205** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003206** returns NULL if the page is not in cache or if a disk I/O error
3207** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003208*/
danielk19773b8a05f2007-03-19 17:44:26 +00003209DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh7e3b0a02001-04-28 16:52:40 +00003210 PgHdr *pPg;
3211
drh836faa42003-01-11 13:30:57 +00003212 assert( pPager!=0 );
3213 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003214
3215 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003216 assert( !pPager->pAll || pPager->exclusiveMode );
danielk1977e277be02007-03-23 18:12:06 +00003217 return 0;
3218 }
danielk1977334cdb62007-03-26 08:05:12 +00003219 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drh7e3b0a02001-04-28 16:52:40 +00003220 return 0;
3221 }
drh7e3b0a02001-04-28 16:52:40 +00003222 pPg = pager_lookup(pPager, pgno);
3223 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +00003224 page_ref(pPg);
danielk19773b8a05f2007-03-19 17:44:26 +00003225 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003226}
3227
3228/*
drhed7c8552001-04-11 14:29:21 +00003229** Release a page.
3230**
3231** If the number of references to the page drop to zero, then the
3232** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003233** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003234** removed.
3235*/
danielk19773b8a05f2007-03-19 17:44:26 +00003236int sqlite3PagerUnref(DbPage *pPg){
drhd9b02572001-04-15 00:37:09 +00003237
3238 /* Decrement the reference count for this page
3239 */
drhed7c8552001-04-11 14:29:21 +00003240 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +00003241 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00003242 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00003243
danielk19773c407372005-02-15 02:54:14 +00003244 CHECK_PAGE(pPg);
3245
drh72f82862001-05-24 21:06:34 +00003246 /* When the number of references to a page reach 0, call the
3247 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003248 */
drhed7c8552001-04-11 14:29:21 +00003249 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00003250 Pager *pPager;
3251 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003252 pPg->pNextFree = 0;
3253 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00003254 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00003255 if( pPg->pPrevFree ){
3256 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00003257 }else{
3258 pPager->pFirst = pPg;
3259 }
drh341eae82003-01-21 02:39:36 +00003260 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
3261 pPager->pFirstSynced = pPg;
3262 }
drh72f82862001-05-24 21:06:34 +00003263 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003264 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003265 }
drhd9b02572001-04-15 00:37:09 +00003266
3267 /* When all pages reach the freelist, drop the read lock from
3268 ** the database file.
3269 */
3270 pPager->nRef--;
3271 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003272 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003273 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003274 }
drhed7c8552001-04-11 14:29:21 +00003275 }
drhd9b02572001-04-15 00:37:09 +00003276 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003277}
3278
3279/*
drha6abd042004-06-09 17:37:22 +00003280** Create a journal file for pPager. There should already be a RESERVED
3281** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003282**
3283** Return SQLITE_OK if everything. Return an error code and release the
3284** write lock if anything goes wrong.
3285*/
3286static int pager_open_journal(Pager *pPager){
3287 int rc;
drhb7f91642004-10-31 02:22:47 +00003288 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003289 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003290 assert( pPager->journalOpen==0 );
3291 assert( pPager->useJournal );
drh88b01a12005-03-28 18:04:27 +00003292 assert( pPager->aInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003293 sqlite3PagerPagecount(pPager);
drh17435752007-08-16 04:30:38 +00003294 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhda47d772002-12-02 04:25:19 +00003295 if( pPager->aInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003296 rc = SQLITE_NOMEM;
3297 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003298 }
drh66560ad2006-01-06 14:32:19 +00003299 rc = sqlite3OsOpenExclusive(pPager->zJournal, &pPager->jfd,
drh9c06c952005-11-26 00:25:00 +00003300 pPager->tempFile);
danielk19775bb16fe2007-04-05 11:54:42 +00003301 assert( rc!=SQLITE_OK || pPager->jfd );
danielk197776572402004-06-25 02:38:54 +00003302 pPager->journalOff = 0;
3303 pPager->setMaster = 0;
3304 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003305 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003306 if( rc==SQLITE_NOMEM ){
3307 sqlite3OsDelete(pPager->zJournal);
3308 }
drh9c105bb2004-10-02 20:38:28 +00003309 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003310 }
danielk197762079062007-08-15 17:08:46 +00003311#if 0
drhac530b12006-02-11 01:25:50 +00003312 sqlite3OsSetFullSync(pPager->jfd, pPager->full_fsync);
3313 sqlite3OsSetFullSync(pPager->fd, pPager->full_fsync);
drh054889e2005-11-30 03:20:31 +00003314 sqlite3OsOpenDirectory(pPager->jfd, pPager->zDirectory);
danielk197762079062007-08-15 17:08:46 +00003315#endif
drhda47d772002-12-02 04:25:19 +00003316 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003317 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003318 pPager->needSync = 0;
3319 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003320 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003321 if( pPager->errCode ){
3322 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003323 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003324 }
drhda47d772002-12-02 04:25:19 +00003325 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003326
danielk197776572402004-06-25 02:38:54 +00003327 rc = writeJournalHdr(pPager);
3328
drhac69b052004-05-12 13:30:07 +00003329 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003330 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003331 }
danielk1977261919c2005-12-06 12:52:59 +00003332 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003333 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003334 if( rc==SQLITE_OK ){
3335 rc = SQLITE_FULL;
3336 }
3337 }
drh9c105bb2004-10-02 20:38:28 +00003338 return rc;
3339
3340failed_to_open_journal:
drh17435752007-08-16 04:30:38 +00003341 sqlite3_free(pPager->aInJournal);
drh9c105bb2004-10-02 20:38:28 +00003342 pPager->aInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003343 return rc;
drhda47d772002-12-02 04:25:19 +00003344}
3345
3346/*
drh4b845d72002-03-05 12:41:19 +00003347** Acquire a write-lock on the database. The lock is removed when
3348** the any of the following happen:
3349**
drh80e35f42007-03-30 14:06:34 +00003350** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003351** * sqlite3PagerRollback() is called.
3352** * sqlite3PagerClose() is called.
3353** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003354**
danielk197713adf8a2004-06-03 16:08:41 +00003355** The first parameter to this routine is a pointer to any open page of the
3356** database file. Nothing changes about the page - it is used merely to
3357** acquire a pointer to the Pager structure and as proof that there is
3358** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003359**
danielk197713adf8a2004-06-03 16:08:41 +00003360** The second parameter indicates how much space in bytes to reserve for a
3361** master journal file-name at the start of the journal when it is created.
3362**
3363** A journal file is opened if this is not a temporary file. For temporary
3364** files, the opening of the journal file is deferred until there is an
3365** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003366**
drha6abd042004-06-09 17:37:22 +00003367** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003368**
3369** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3370** immediately instead of waiting until we try to flush the cache. The
3371** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003372*/
danielk19773b8a05f2007-03-19 17:44:26 +00003373int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003374 Pager *pPager = pPg->pPager;
3375 int rc = SQLITE_OK;
3376 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003377 assert( pPager->state!=PAGER_UNLOCK );
3378 if( pPager->state==PAGER_SHARED ){
drh4b845d72002-03-05 12:41:19 +00003379 assert( pPager->aInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003380 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003381 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003382 pPager->origDbSize = pPager->dbSize;
3383 }else{
drh054889e2005-11-30 03:20:31 +00003384 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003385 if( rc==SQLITE_OK ){
3386 pPager->state = PAGER_RESERVED;
3387 if( exFlag ){
3388 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3389 }
3390 }
danielk19779eed5052004-06-04 10:38:30 +00003391 if( rc!=SQLITE_OK ){
3392 return rc;
drhac69b052004-05-12 13:30:07 +00003393 }
drha6abd042004-06-09 17:37:22 +00003394 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003395 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003396 if( pPager->useJournal && !pPager->tempFile ){
3397 rc = pager_open_journal(pPager);
3398 }
drh4b845d72002-03-05 12:41:19 +00003399 }
danielk1977334cdb62007-03-26 08:05:12 +00003400 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3401 /* This happens when the pager was in exclusive-access mode last
3402 ** time a (read or write) transaction was successfully concluded
3403 ** by this connection. Instead of deleting the journal file it was
3404 ** kept open and truncated to 0 bytes.
3405 */
3406 assert( pPager->nRec==0 );
3407 assert( pPager->origDbSize==0 );
drh369339d2007-03-30 16:01:55 +00003408 assert( pPager->aInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003409 sqlite3PagerPagecount(pPager);
drh17435752007-08-16 04:30:38 +00003410 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
danielk1977334cdb62007-03-26 08:05:12 +00003411 if( !pPager->aInJournal ){
3412 rc = SQLITE_NOMEM;
3413 }else{
drh369339d2007-03-30 16:01:55 +00003414 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003415 rc = writeJournalHdr(pPager);
3416 }
drh4b845d72002-03-05 12:41:19 +00003417 }
danielk1977334cdb62007-03-26 08:05:12 +00003418 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003419 return rc;
3420}
3421
3422/*
drh605ad8f2006-05-03 23:34:05 +00003423** Make a page dirty. Set its dirty flag and add it to the dirty
3424** page list.
3425*/
3426static void makeDirty(PgHdr *pPg){
3427 if( pPg->dirty==0 ){
3428 Pager *pPager = pPg->pPager;
3429 pPg->dirty = 1;
3430 pPg->pDirty = pPager->pDirty;
3431 if( pPager->pDirty ){
3432 pPager->pDirty->pPrevDirty = pPg;
3433 }
3434 pPg->pPrevDirty = 0;
3435 pPager->pDirty = pPg;
3436 }
3437}
3438
3439/*
3440** Make a page clean. Clear its dirty bit and remove it from the
3441** dirty page list.
3442*/
3443static void makeClean(PgHdr *pPg){
3444 if( pPg->dirty ){
3445 pPg->dirty = 0;
3446 if( pPg->pDirty ){
3447 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
3448 }
3449 if( pPg->pPrevDirty ){
3450 pPg->pPrevDirty->pDirty = pPg->pDirty;
3451 }else{
3452 pPg->pPager->pDirty = pPg->pDirty;
3453 }
3454 }
3455}
3456
3457
3458/*
drhed7c8552001-04-11 14:29:21 +00003459** Mark a data page as writeable. The page is written into the journal
3460** if it is not there already. This routine must be called before making
3461** changes to a page.
3462**
3463** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003464** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003465** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003466** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003467** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003468**
3469** If the journal file could not be written because the disk is full,
3470** then this routine returns SQLITE_FULL and does an immediate rollback.
3471** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003472** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003473** reset.
drhed7c8552001-04-11 14:29:21 +00003474*/
danielk19773b8a05f2007-03-19 17:44:26 +00003475static int pager_write(PgHdr *pPg){
3476 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00003477 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003478 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003479
drh6446c4d2001-12-15 14:22:18 +00003480 /* Check for errors
3481 */
danielk1977efaaf572006-01-16 11:29:19 +00003482 if( pPager->errCode ){
3483 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003484 }
drh5e00f6c2001-09-13 13:46:56 +00003485 if( pPager->readOnly ){
3486 return SQLITE_PERM;
3487 }
drh6446c4d2001-12-15 14:22:18 +00003488
danielk197776572402004-06-25 02:38:54 +00003489 assert( !pPager->setMaster );
3490
danielk19773c407372005-02-15 02:54:14 +00003491 CHECK_PAGE(pPg);
3492
drh538f5702007-04-13 02:14:30 +00003493 /* If this page was previously acquired with noContent==1, that means
3494 ** we didn't really read in the content of the page. This can happen
3495 ** (for example) when the page is being moved to the freelist. But
3496 ** now we are (perhaps) moving the page off of the freelist for
3497 ** reuse and we need to know its original content so that content
3498 ** can be stored in the rollback journal. So do the read at this
3499 ** time.
3500 */
drhd33d5a82007-04-26 12:11:28 +00003501 rc = pager_get_content(pPg);
3502 if( rc ){
3503 return rc;
drh538f5702007-04-13 02:14:30 +00003504 }
3505
drh6446c4d2001-12-15 14:22:18 +00003506 /* Mark the page as dirty. If the page has already been written
3507 ** to the journal then we can return right away.
3508 */
drh605ad8f2006-05-03 23:34:05 +00003509 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00003510 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00003511 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003512 }else{
drh6446c4d2001-12-15 14:22:18 +00003513
danielk1977a0bf2652004-11-04 14:30:04 +00003514 /* If we get this far, it means that the page needs to be
3515 ** written to the transaction journal or the ckeckpoint journal
3516 ** or both.
3517 **
3518 ** First check to see that the transaction journal exists and
3519 ** create it if it does not.
3520 */
3521 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003522 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003523 if( rc!=SQLITE_OK ){
3524 return rc;
3525 }
3526 assert( pPager->state>=PAGER_RESERVED );
3527 if( !pPager->journalOpen && pPager->useJournal ){
3528 rc = pager_open_journal(pPager);
3529 if( rc!=SQLITE_OK ) return rc;
3530 }
3531 assert( pPager->journalOpen || !pPager->useJournal );
3532 pPager->dirtyCache = 1;
3533
3534 /* The transaction journal now exists and we have a RESERVED or an
3535 ** EXCLUSIVE lock on the main database file. Write the current page to
3536 ** the transaction journal if it is not there already.
3537 */
3538 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
3539 if( (int)pPg->pgno <= pPager->origDbSize ){
3540 int szPg;
danielk1977a0bf2652004-11-04 14:30:04 +00003541 if( MEMDB ){
3542 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00003543 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00003544 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00003545 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003546 if( pHist->pOrig ){
3547 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
3548 }
3549 }else{
drhc001c582006-03-06 18:23:16 +00003550 u32 cksum, saved;
3551 char *pData2, *pEnd;
drh267cb322005-09-16 17:16:52 +00003552 /* We should never write to the journal file the page that
3553 ** contains the database locks. The following assert verifies
3554 ** that we do not. */
3555 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00003556 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00003557 cksum = pager_cksum(pPager, (u8*)pData2);
drhc001c582006-03-06 18:23:16 +00003558 pEnd = pData2 + pPager->pageSize;
3559 pData2 -= 4;
3560 saved = *(u32*)pEnd;
3561 put32bits(pEnd, cksum);
danielk1977a0bf2652004-11-04 14:30:04 +00003562 szPg = pPager->pageSize+8;
drhc001c582006-03-06 18:23:16 +00003563 put32bits(pData2, pPg->pgno);
danielk197762079062007-08-15 17:08:46 +00003564 rc = sqlite3OsWrite(pPager->jfd, pData2, szPg, pPager->journalOff);
drhb0603412007-02-28 04:47:26 +00003565 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
drh538f5702007-04-13 02:14:30 +00003566 pPager->journalOff, szPg));
3567 PAGER_INCR(sqlite3_pager_writej_count);
danielk1977a0bf2652004-11-04 14:30:04 +00003568 pPager->journalOff += szPg;
drh477731b2007-06-16 03:06:27 +00003569 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
3570 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
drhc001c582006-03-06 18:23:16 +00003571 *(u32*)pEnd = saved;
danielk197707cb5602006-01-20 10:55:05 +00003572
drhfd131da2007-08-07 17:13:03 +00003573 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00003574 ** transaction will be rolled back by the layer above.
3575 */
danielk1977a0bf2652004-11-04 14:30:04 +00003576 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00003577 return rc;
3578 }
danielk197707cb5602006-01-20 10:55:05 +00003579
danielk1977a0bf2652004-11-04 14:30:04 +00003580 pPager->nRec++;
3581 assert( pPager->aInJournal!=0 );
3582 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
3583 pPg->needSync = !pPager->noSync;
3584 if( pPager->stmtInUse ){
3585 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
danielk1977a0bf2652004-11-04 14:30:04 +00003586 }
drhac69b052004-05-12 13:30:07 +00003587 }
danielk197713adf8a2004-06-03 16:08:41 +00003588 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00003589 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00003590 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00003591 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00003592 }
3593 if( pPg->needSync ){
3594 pPager->needSync = 1;
3595 }
3596 pPg->inJournal = 1;
3597 }
3598
3599 /* If the statement journal is open and the page is not in it,
3600 ** then write the current page to the statement journal. Note that
3601 ** the statement journal format differs from the standard journal format
3602 ** in that it omits the checksums and the header.
3603 */
danielk1977f35843b2007-04-07 15:03:17 +00003604 if( pPager->stmtInUse
3605 && !pageInStatement(pPg)
3606 && (int)pPg->pgno<=pPager->stmtSize
3607 ){
danielk1977a0bf2652004-11-04 14:30:04 +00003608 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
3609 if( MEMDB ){
3610 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
3611 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00003612 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003613 if( pHist->pStmt ){
3614 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
3615 }
drh4f0c5872007-03-26 22:05:01 +00003616 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00003617 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00003618 }else{
danielk197762079062007-08-15 17:08:46 +00003619 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhc001c582006-03-06 18:23:16 +00003620 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4;
3621 put32bits(pData2, pPg->pgno);
danielk197762079062007-08-15 17:08:46 +00003622 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4, offset);
drh4f0c5872007-03-26 22:05:01 +00003623 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00003624 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00003625 return rc;
3626 }
danielk1977a0bf2652004-11-04 14:30:04 +00003627 pPager->stmtNRec++;
3628 assert( pPager->aInStmt!=0 );
3629 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhdb48ee02003-01-16 13:42:43 +00003630 }
drhd9b02572001-04-15 00:37:09 +00003631 }
drhfa86c412002-02-02 15:01:15 +00003632 }
3633
3634 /* Update the database size and return.
3635 */
drh1aa2d8b2007-01-03 15:34:29 +00003636 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00003637 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003638 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00003639 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00003640 pPager->dbSize++;
3641 }
drh306dc212001-05-21 13:45:10 +00003642 }
drh69688d52001-04-14 16:38:23 +00003643 return rc;
drhed7c8552001-04-11 14:29:21 +00003644}
3645
3646/*
danielk19774099f6e2007-03-19 11:25:20 +00003647** This function is used to mark a data-page as writable. It uses
3648** pager_write() to open a journal file (if it is not already open)
3649** and write the page *pData to the journal.
3650**
3651** The difference between this function and pager_write() is that this
3652** function also deals with the special case where 2 or more pages
3653** fit on a single disk sector. In this case all co-resident pages
3654** must have been written to the journal file before returning.
3655*/
danielk19773b8a05f2007-03-19 17:44:26 +00003656int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003657 int rc = SQLITE_OK;
3658
danielk19773b8a05f2007-03-19 17:44:26 +00003659 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003660 Pager *pPager = pPg->pPager;
3661 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3662
3663 if( !MEMDB && nPagePerSector>1 ){
3664 Pgno nPageCount; /* Total number of pages in database file */
3665 Pgno pg1; /* First page of the sector pPg is located on. */
3666 int nPage; /* Number of pages starting at pg1 to journal */
3667 int ii;
3668
3669 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3670 ** header to be written between the pages journaled by this function.
3671 */
3672 assert( pPager->doNotSync==0 );
3673 pPager->doNotSync = 1;
3674
3675 /* This trick assumes that both the page-size and sector-size are
3676 ** an integer power of 2. It sets variable pg1 to the identifier
3677 ** of the first page of the sector pPg is located on.
3678 */
3679 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3680
danielk19773b8a05f2007-03-19 17:44:26 +00003681 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003682 if( pPg->pgno>nPageCount ){
3683 nPage = (pPg->pgno - pg1)+1;
3684 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3685 nPage = nPageCount+1-pg1;
3686 }else{
3687 nPage = nPagePerSector;
3688 }
3689 assert(nPage>0);
3690 assert(pg1<=pPg->pgno);
3691 assert((pg1+nPage)>pPg->pgno);
3692
3693 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3694 Pgno pg = pg1+ii;
3695 if( !pPager->aInJournal || pg==pPg->pgno ||
3696 pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
3697 ) {
3698 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003699 PgHdr *pPage;
3700 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003701 if( rc==SQLITE_OK ){
3702 rc = pager_write(pPage);
danielk19773b8a05f2007-03-19 17:44:26 +00003703 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003704 }
3705 }
3706 }
3707 }
3708
3709 assert( pPager->doNotSync==1 );
3710 pPager->doNotSync = 0;
3711 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003712 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003713 }
3714 return rc;
3715}
3716
3717/*
drhaacc5432002-01-06 17:07:40 +00003718** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003719** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003720** to change the content of the page.
3721*/
danielk19777d3a6662006-01-23 16:21:05 +00003722#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003723int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00003724 return pPg->dirty;
3725}
danielk19777d3a6662006-01-23 16:21:05 +00003726#endif
drh6019e162001-07-02 17:51:45 +00003727
danielk1977b84f96f2005-01-20 11:32:23 +00003728#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00003729/*
drh001bbcb2003-03-19 03:14:00 +00003730** Replace the content of a single page with the information in the third
3731** argument.
3732*/
danielk19773b8a05f2007-03-19 17:44:26 +00003733int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
3734 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00003735 int rc;
3736
danielk19773b8a05f2007-03-19 17:44:26 +00003737 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00003738 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003739 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00003740 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003741 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00003742 }
danielk19773b8a05f2007-03-19 17:44:26 +00003743 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00003744 }
3745 return rc;
3746}
danielk1977b84f96f2005-01-20 11:32:23 +00003747#endif
drh001bbcb2003-03-19 03:14:00 +00003748
3749/*
drh30e58752002-03-02 20:41:57 +00003750** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003751** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00003752** that page might be marked as dirty.
3753**
3754** The overlying software layer calls this routine when all of the data
3755** on the given page is unused. The pager marks the page as clean so
3756** that it does not get written to disk.
3757**
3758** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003759** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003760** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003761**
3762** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003763** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003764** will thereafter be ignored. This is necessary to avoid a problem
3765** where a page with data is added to the freelist during one part of
3766** a transaction then removed from the freelist during a later part
3767** of the same transaction and reused for some other purpose. When it
3768** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003769** the sqlite3PagerDontRollback() routine is called. But because the
3770** page contains critical data, we still need to be sure it gets
3771** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003772*/
drh538f5702007-04-13 02:14:30 +00003773void sqlite3PagerDontWrite(DbPage *pDbPage){
3774 PgHdr *pPg = pDbPage;
3775 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00003776
drhb7f91642004-10-31 02:22:47 +00003777 if( MEMDB ) return;
drh8e298f92002-07-06 16:28:47 +00003778 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00003779 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00003780 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00003781 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
3782 /* If this pages is the last page in the file and the file has grown
3783 ** during the current transaction, then do NOT mark the page as clean.
3784 ** When the database file grows, we must make sure that the last page
3785 ** gets written at least once so that the disk file will be the correct
3786 ** size. If you do not write this page and the size of the file
3787 ** on the disk ends up being too small, that can lead to database
3788 ** corruption during the next transaction.
3789 */
3790 }else{
drh538f5702007-04-13 02:14:30 +00003791 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
3792 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00003793 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00003794#ifdef SQLITE_CHECK_PAGES
3795 pPg->pageHash = pager_pagehash(pPg);
3796#endif
drh8124a302002-06-25 14:43:57 +00003797 }
drh30e58752002-03-02 20:41:57 +00003798 }
3799}
3800
3801/*
3802** A call to this routine tells the pager that if a rollback occurs,
3803** it is not necessary to restore the data on the given page. This
3804** means that the pager does not have to record the given page in the
3805** rollback journal.
drh538f5702007-04-13 02:14:30 +00003806**
3807** If we have not yet actually read the content of this page (if
3808** the PgHdr.needRead flag is set) then this routine acts as a promise
3809** that we will never need to read the page content in the future.
3810** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00003811*/
danielk19773b8a05f2007-03-19 17:44:26 +00003812void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003813 Pager *pPager = pPg->pPager;
3814
drhd3627af2006-12-18 18:34:51 +00003815 assert( pPager->state>=PAGER_RESERVED );
3816 if( pPager->journalOpen==0 ) return;
drhb7f91642004-10-31 02:22:47 +00003817 if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
drh30e58752002-03-02 20:41:57 +00003818 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
3819 assert( pPager->aInJournal!=0 );
3820 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
3821 pPg->inJournal = 1;
drh538f5702007-04-13 02:14:30 +00003822 pPg->needRead = 0;
drhac69b052004-05-12 13:30:07 +00003823 if( pPager->stmtInUse ){
3824 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00003825 }
drh4f0c5872007-03-26 22:05:01 +00003826 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00003827 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00003828 }
danielk1977f35843b2007-04-07 15:03:17 +00003829 if( pPager->stmtInUse
3830 && !pageInStatement(pPg)
3831 && (int)pPg->pgno<=pPager->stmtSize
3832 ){
drh30e58752002-03-02 20:41:57 +00003833 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00003834 assert( pPager->aInStmt!=0 );
3835 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00003836 }
3837}
3838
drhac69b052004-05-12 13:30:07 +00003839
drh30e58752002-03-02 20:41:57 +00003840/*
drh80e35f42007-03-30 14:06:34 +00003841** This routine is called to increment the database file change-counter,
3842** stored at byte 24 of the pager file.
3843*/
3844static int pager_incr_changecounter(Pager *pPager){
3845 PgHdr *pPgHdr;
3846 u32 change_counter;
3847 int rc;
3848
3849 if( !pPager->changeCountDone ){
3850 /* Open page 1 of the file for writing. */
3851 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3852 if( rc!=SQLITE_OK ) return rc;
3853 rc = sqlite3PagerWrite(pPgHdr);
3854 if( rc!=SQLITE_OK ) return rc;
3855
drh80e35f42007-03-30 14:06:34 +00003856 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00003857 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00003858 change_counter++;
3859 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
drh80e35f42007-03-30 14:06:34 +00003860 /* Release the page reference. */
3861 sqlite3PagerUnref(pPgHdr);
3862 pPager->changeCountDone = 1;
3863 }
3864 return SQLITE_OK;
3865}
3866
3867/*
3868** Sync the database file for the pager pPager. zMaster points to the name
3869** of a master journal file that should be written into the individual
3870** journal file. zMaster may be NULL, which is interpreted as no master
3871** journal (a single database transaction).
3872**
3873** This routine ensures that the journal is synced, all dirty pages written
3874** to the database file and the database file synced. The only thing that
3875** remains to commit the transaction is to delete the journal file (or
3876** master journal file if specified).
3877**
3878** Note that if zMaster==NULL, this does not overwrite a previous value
3879** passed to an sqlite3PagerCommitPhaseOne() call.
3880**
3881** If parameter nTrunc is non-zero, then the pager file is truncated to
3882** nTrunc pages (this is used by auto-vacuum databases).
3883*/
3884int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
3885 int rc = SQLITE_OK;
3886
3887 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
3888 pPager->zFilename, zMaster, nTrunc);
3889
3890 /* If this is an in-memory db, or no pages have been written to, or this
3891 ** function has already been called, it is a no-op.
3892 */
3893 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
3894 PgHdr *pPg;
3895 assert( pPager->journalOpen );
3896
3897 /* If a master journal file name has already been written to the
3898 ** journal file, then no sync is required. This happens when it is
3899 ** written, then the process fails to upgrade from a RESERVED to an
3900 ** EXCLUSIVE lock. The next time the process tries to commit the
3901 ** transaction the m-j name will have already been written.
3902 */
3903 if( !pPager->setMaster ){
3904 rc = pager_incr_changecounter(pPager);
3905 if( rc!=SQLITE_OK ) goto sync_exit;
3906#ifndef SQLITE_OMIT_AUTOVACUUM
3907 if( nTrunc!=0 ){
3908 /* If this transaction has made the database smaller, then all pages
3909 ** being discarded by the truncation must be written to the journal
3910 ** file.
3911 */
3912 Pgno i;
3913 int iSkip = PAGER_MJ_PGNO(pPager);
3914 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
3915 if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){
3916 rc = sqlite3PagerGet(pPager, i, &pPg);
3917 if( rc!=SQLITE_OK ) goto sync_exit;
3918 rc = sqlite3PagerWrite(pPg);
3919 sqlite3PagerUnref(pPg);
3920 if( rc!=SQLITE_OK ) goto sync_exit;
3921 }
3922 }
3923 }
3924#endif
3925 rc = writeMasterJournal(pPager, zMaster);
3926 if( rc!=SQLITE_OK ) goto sync_exit;
3927 rc = syncJournal(pPager);
3928 if( rc!=SQLITE_OK ) goto sync_exit;
3929 }
3930
3931#ifndef SQLITE_OMIT_AUTOVACUUM
3932 if( nTrunc!=0 ){
3933 rc = sqlite3PagerTruncate(pPager, nTrunc);
3934 if( rc!=SQLITE_OK ) goto sync_exit;
3935 }
3936#endif
3937
3938 /* Write all dirty pages to the database file */
3939 pPg = pager_get_all_dirty_pages(pPager);
3940 rc = pager_write_pagelist(pPg);
3941 if( rc!=SQLITE_OK ) goto sync_exit;
drh3cdd7d32007-03-30 14:46:01 +00003942 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00003943
3944 /* Sync the database file. */
3945 if( !pPager->noSync ){
3946 rc = sqlite3OsSync(pPager->fd, 0);
3947 }
3948 IOTRACE(("DBSYNC %p\n", pPager))
3949
3950 pPager->state = PAGER_SYNCED;
3951 }else if( MEMDB && nTrunc!=0 ){
3952 rc = sqlite3PagerTruncate(pPager, nTrunc);
3953 }
3954
3955sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00003956 if( rc==SQLITE_IOERR_BLOCKED ){
3957 /* pager_incr_changecounter() may attempt to obtain an exclusive
3958 * lock to spill the cache and return IOERR_BLOCKED. But since
3959 * there is no chance the cache is inconsistent, it's
3960 * better to return SQLITE_BUSY.
3961 */
3962 rc = SQLITE_BUSY;
3963 }
drh80e35f42007-03-30 14:06:34 +00003964 return rc;
3965}
3966
3967
3968/*
drhed7c8552001-04-11 14:29:21 +00003969** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00003970**
3971** If the commit fails for any reason, a rollback attempt is made
3972** and an error code is returned. If the commit worked, SQLITE_OK
3973** is returned.
drhed7c8552001-04-11 14:29:21 +00003974*/
drh80e35f42007-03-30 14:06:34 +00003975int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00003976 int rc;
drhed7c8552001-04-11 14:29:21 +00003977 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00003978
danielk1977efaaf572006-01-16 11:29:19 +00003979 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00003980 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003981 }
drha6abd042004-06-09 17:37:22 +00003982 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00003983 return SQLITE_ERROR;
3984 }
drh4f0c5872007-03-26 22:05:01 +00003985 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003986 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00003987 pPg = pager_get_all_dirty_pages(pPager);
3988 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00003989 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
3990 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00003991 pPg->dirty = 0;
3992 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003993 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00003994 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003995 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00003996 pPg = pPg->pDirty;
3997 }
drh605ad8f2006-05-03 23:34:05 +00003998 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00003999#ifndef NDEBUG
4000 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4001 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4002 assert( !pPg->alwaysRollback );
4003 assert( !pHist->pOrig );
4004 assert( !pHist->pStmt );
4005 }
4006#endif
drhac69b052004-05-12 13:30:07 +00004007 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004008 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004009 return SQLITE_OK;
4010 }
drh3cdd7d32007-03-30 14:46:01 +00004011 assert( pPager->journalOpen || !pPager->dirtyCache );
4012 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4013 rc = pager_end_transaction(pPager);
danielk1977979f38e2007-03-27 16:19:51 +00004014 return pager_error(pPager, rc);
drhed7c8552001-04-11 14:29:21 +00004015}
4016
4017/*
drha6abd042004-06-09 17:37:22 +00004018** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004019** All in-memory cache pages revert to their original data contents.
4020** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004021**
4022** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004023** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004024** process is writing trash into the journal file (SQLITE_CORRUPT) or
4025** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4026** codes are returned for all these occasions. Otherwise,
4027** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004028*/
danielk19773b8a05f2007-03-19 17:44:26 +00004029int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004030 int rc;
drh4f0c5872007-03-26 22:05:01 +00004031 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004032 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004033 PgHdr *p;
4034 for(p=pPager->pAll; p; p=p->pNextAll){
4035 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004036 assert( !p->alwaysRollback );
4037 if( !p->dirty ){
4038 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4039 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4040 continue;
4041 }
4042
drhac69b052004-05-12 13:30:07 +00004043 pHist = PGHDR_TO_HIST(p, pPager);
4044 if( pHist->pOrig ){
4045 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004046 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004047 }else{
drh4f0c5872007-03-26 22:05:01 +00004048 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004049 }
4050 clearHistory(pHist);
4051 p->dirty = 0;
4052 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004053 pHist->inStmt = 0;
4054 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004055 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004056 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004057 }
drhac69b052004-05-12 13:30:07 +00004058 }
drh605ad8f2006-05-03 23:34:05 +00004059 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004060 pPager->pStmt = 0;
4061 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004062 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004063 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004064 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004065 return SQLITE_OK;
4066 }
4067
drha6abd042004-06-09 17:37:22 +00004068 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004069 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00004070 return rc;
4071 }
drhdb48ee02003-01-16 13:42:43 +00004072
danielk1977efaaf572006-01-16 11:29:19 +00004073 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004074 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004075 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004076 }
danielk1977efaaf572006-01-16 11:29:19 +00004077 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004078 }
drha6abd042004-06-09 17:37:22 +00004079 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004080 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004081 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004082 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004083 if( rc==SQLITE_OK ){
4084 rc = rc2;
4085 }
4086 }else{
danielk1977e277be02007-03-23 18:12:06 +00004087 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004088 }
danielk1977e180dd92007-04-05 17:15:52 +00004089 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004090 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004091
4092 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4093 ** cache. So call pager_error() on the way out to make any error
4094 ** persistent.
4095 */
4096 return pager_error(pPager, rc);
drh98808ba2001-10-18 12:34:46 +00004097}
drhd9b02572001-04-15 00:37:09 +00004098
4099/*
drh5e00f6c2001-09-13 13:46:56 +00004100** Return TRUE if the database file is opened read-only. Return FALSE
4101** if the database is (in theory) writable.
4102*/
danielk19773b8a05f2007-03-19 17:44:26 +00004103int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004104 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004105}
4106
4107/*
drh0f7eb612006-08-08 13:51:43 +00004108** Return the number of references to the pager.
4109*/
danielk19773b8a05f2007-03-19 17:44:26 +00004110int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004111 return pPager->nRef;
4112}
4113
4114#ifdef SQLITE_TEST
4115/*
drhd9b02572001-04-15 00:37:09 +00004116** This routine is used for testing and analysis only.
4117*/
danielk19773b8a05f2007-03-19 17:44:26 +00004118int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004119 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004120 a[0] = pPager->nRef;
4121 a[1] = pPager->nPage;
4122 a[2] = pPager->mxPage;
4123 a[3] = pPager->dbSize;
4124 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004125 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004126 a[6] = pPager->nHit;
4127 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004128 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004129 a[9] = pPager->nRead;
4130 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004131 return a;
4132}
drh0f7eb612006-08-08 13:51:43 +00004133#endif
drhdd793422001-06-28 01:54:48 +00004134
drhfa86c412002-02-02 15:01:15 +00004135/*
drhac69b052004-05-12 13:30:07 +00004136** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004137**
4138** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004139** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004140** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004141*/
danielk19773b8a05f2007-03-19 17:44:26 +00004142int sqlite3PagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004143 int rc;
drhac69b052004-05-12 13:30:07 +00004144 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004145 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004146 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004147 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004148 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004149 pPager->stmtInUse = 1;
4150 pPager->stmtSize = pPager->dbSize;
4151 return SQLITE_OK;
4152 }
drhda47d772002-12-02 04:25:19 +00004153 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004154 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004155 return SQLITE_OK;
4156 }
drhfa86c412002-02-02 15:01:15 +00004157 assert( pPager->journalOpen );
drh17435752007-08-16 04:30:38 +00004158 pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhac69b052004-05-12 13:30:07 +00004159 if( pPager->aInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004160 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004161 return SQLITE_NOMEM;
4162 }
drh968af522003-02-11 14:55:40 +00004163#ifndef NDEBUG
drh054889e2005-11-30 03:20:31 +00004164 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
drhac69b052004-05-12 13:30:07 +00004165 if( rc ) goto stmt_begin_failed;
danielk197776572402004-06-25 02:38:54 +00004166 assert( pPager->stmtJSize == pPager->journalOff );
drh968af522003-02-11 14:55:40 +00004167#endif
danielk197776572402004-06-25 02:38:54 +00004168 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004169 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004170 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004171 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004172 if( !pPager->stmtOpen ){
danielk19773b8a05f2007-03-19 17:44:26 +00004173 rc = sqlite3PagerOpentemp(&pPager->stfd);
drhac69b052004-05-12 13:30:07 +00004174 if( rc ) goto stmt_begin_failed;
4175 pPager->stmtOpen = 1;
4176 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004177 }
drhac69b052004-05-12 13:30:07 +00004178 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004179 return SQLITE_OK;
4180
drhac69b052004-05-12 13:30:07 +00004181stmt_begin_failed:
4182 if( pPager->aInStmt ){
drh17435752007-08-16 04:30:38 +00004183 sqlite3_free(pPager->aInStmt);
drhac69b052004-05-12 13:30:07 +00004184 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004185 }
4186 return rc;
4187}
4188
4189/*
drhac69b052004-05-12 13:30:07 +00004190** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004191*/
danielk19773b8a05f2007-03-19 17:44:26 +00004192int sqlite3PagerStmtCommit(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00004193 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004194 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004195 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004196 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004197 /* sqlite3OsTruncate(pPager->stfd, 0); */
drh17435752007-08-16 04:30:38 +00004198 sqlite3_free( pPager->aInStmt );
drhac69b052004-05-12 13:30:07 +00004199 pPager->aInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004200 }else{
4201 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004202 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004203 pNext = pHist->pNextStmt;
4204 assert( pHist->inStmt );
4205 pHist->inStmt = 0;
4206 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004207 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004208 pHist->pStmt = 0;
4209 }
4210 }
4211 pPager->stmtNRec = 0;
4212 pPager->stmtInUse = 0;
4213 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004214 }
drhac69b052004-05-12 13:30:07 +00004215 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00004216 return SQLITE_OK;
4217}
4218
4219/*
drhac69b052004-05-12 13:30:07 +00004220** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004221*/
danielk19773b8a05f2007-03-19 17:44:26 +00004222int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004223 int rc;
drhac69b052004-05-12 13:30:07 +00004224 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004225 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004226 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004227 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004228 PgHistory *pHist;
4229 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4230 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004231 if( pHist->pStmt ){
4232 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004233 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004234 pHist->pStmt = 0;
4235 }
4236 }
4237 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004238 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004239 rc = SQLITE_OK;
4240 }else{
4241 rc = pager_stmt_playback(pPager);
4242 }
danielk19773b8a05f2007-03-19 17:44:26 +00004243 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004244 }else{
4245 rc = SQLITE_OK;
4246 }
drhac69b052004-05-12 13:30:07 +00004247 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00004248 return rc;
4249}
4250
drh73509ee2003-04-06 20:44:45 +00004251/*
4252** Return the full pathname of the database file.
4253*/
danielk19773b8a05f2007-03-19 17:44:26 +00004254const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004255 return pPager->zFilename;
4256}
4257
drhb20ea9d2004-02-09 01:20:36 +00004258/*
danielk19775865e3d2004-06-14 06:03:57 +00004259** Return the directory of the database file.
4260*/
danielk19773b8a05f2007-03-19 17:44:26 +00004261const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004262 return pPager->zDirectory;
4263}
4264
4265/*
4266** Return the full pathname of the journal file.
4267*/
danielk19773b8a05f2007-03-19 17:44:26 +00004268const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004269 return pPager->zJournal;
4270}
4271
4272/*
drh2c8997b2005-08-27 16:36:48 +00004273** Return true if fsync() calls are disabled for this pager. Return FALSE
4274** if fsync()s are executed normally.
4275*/
danielk19773b8a05f2007-03-19 17:44:26 +00004276int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004277 return pPager->noSync;
4278}
4279
drh7c4ac0c2007-04-05 11:25:58 +00004280#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004281/*
drhb20ea9d2004-02-09 01:20:36 +00004282** Set the codec for this pager
4283*/
danielk19773b8a05f2007-03-19 17:44:26 +00004284void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004285 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004286 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004287 void *pCodecArg
4288){
4289 pPager->xCodec = xCodec;
4290 pPager->pCodecArg = pCodecArg;
4291}
drh7c4ac0c2007-04-05 11:25:58 +00004292#endif
drhb20ea9d2004-02-09 01:20:36 +00004293
danielk1977687566d2004-11-02 12:56:41 +00004294#ifndef SQLITE_OMIT_AUTOVACUUM
4295/*
drh5e385312007-06-16 04:42:12 +00004296** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004297**
drh5e385312007-06-16 04:42:12 +00004298** There must be no references to the page previously located at
4299** pgno (which we call pPgOld) though that page is allowed to be
4300** in cache. If the page previous located at pgno is not already
4301** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004302**
drh5e385312007-06-16 04:42:12 +00004303** References to the page pPg remain valid. Updating any
4304** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004305** allocated along with the page) is the responsibility of the caller.
4306**
danielk19775fd057a2005-03-09 13:09:43 +00004307** A transaction must be active when this routine is called. It used to be
4308** required that a statement transaction was not active, but this restriction
4309** has been removed (CREATE INDEX needs to move a page when a statement
4310** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00004311*/
danielk19773b8a05f2007-03-19 17:44:26 +00004312int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00004313 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00004314 int h;
danielk197794daf7f2004-11-08 09:26:09 +00004315 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004316
danielk1977687566d2004-11-02 12:56:41 +00004317 assert( pPg->nRef>0 );
4318
drh4f0c5872007-03-26 22:05:01 +00004319 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00004320 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00004321 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004322
danielk1977b4626a32007-04-28 15:47:43 +00004323 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00004324 if( pPg->needSync ){
4325 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00004326 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00004327 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00004328 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004329 }
4330
danielk1977687566d2004-11-02 12:56:41 +00004331 /* Unlink pPg from it's hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00004332 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00004333
danielk1977ef73ee92004-11-06 12:26:07 +00004334 /* If the cache contains a page with page-number pgno, remove it
danielk1977599fcba2004-11-08 07:13:13 +00004335 ** from it's hash chain. Also, if the PgHdr.needSync was set for
4336 ** page pgno before the 'move' operation, it needs to be retained
4337 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004338 */
drh5e385312007-06-16 04:42:12 +00004339 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00004340 pPgOld = pager_lookup(pPager, pgno);
4341 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00004342 assert( pPgOld->nRef==0 );
4343 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00004344 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00004345 pPg->needSync = pPgOld->needSync;
4346 }else{
4347 pPg->needSync = 0;
4348 }
4349 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
4350 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drh5e385312007-06-16 04:42:12 +00004351 }else{
4352 pPg->inJournal = 0;
drh732c8172007-06-16 11:17:45 +00004353 assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize );
danielk1977687566d2004-11-02 12:56:41 +00004354 }
4355
danielk1977f5fdda82004-11-03 08:44:05 +00004356 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00004357 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00004358 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00004359 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00004360 if( pPager->aHash[h] ){
4361 assert( pPager->aHash[h]->pPrevHash==0 );
4362 pPager->aHash[h]->pPrevHash = pPg;
4363 }
4364 pPg->pNextHash = pPager->aHash[h];
4365 pPager->aHash[h] = pPg;
4366 pPg->pPrevHash = 0;
4367
drh605ad8f2006-05-03 23:34:05 +00004368 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004369 pPager->dirtyCache = 1;
4370
danielk197794daf7f2004-11-08 09:26:09 +00004371 if( needSyncPgno ){
4372 /* If needSyncPgno is non-zero, then the journal file needs to be
4373 ** sync()ed before any data is written to database file page needSyncPgno.
4374 ** Currently, no such page exists in the page-cache and the
4375 ** Pager.aInJournal bit has been set. This needs to be remedied by loading
4376 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00004377 **
danielk19773b8a05f2007-03-19 17:44:26 +00004378 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004379 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004380 */
4381 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004382 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004383 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004384 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004385 if( rc!=SQLITE_OK ) return rc;
danielk1977ae825582004-11-23 09:06:55 +00004386 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00004387 pPgHdr->needSync = 1;
4388 pPgHdr->inJournal = 1;
4389 makeDirty(pPgHdr);
4390 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004391 }
4392
danielk1977687566d2004-11-02 12:56:41 +00004393 return SQLITE_OK;
4394}
4395#endif
4396
danielk19773b8a05f2007-03-19 17:44:26 +00004397/*
4398** Return a pointer to the data for the specified page.
4399*/
4400void *sqlite3PagerGetData(DbPage *pPg){
4401 return PGHDR_TO_DATA(pPg);
4402}
4403
4404/*
4405** Return a pointer to the Pager.nExtra bytes of "extra" space
4406** allocated along with the specified page.
4407*/
4408void *sqlite3PagerGetExtra(DbPage *pPg){
4409 Pager *pPager = pPg->pPager;
4410 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
4411}
4412
danielk197741483462007-03-24 16:45:04 +00004413/*
4414** Get/set the locking-mode for this pager. Parameter eMode must be one
4415** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4416** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4417** the locking-mode is set to the value specified.
4418**
4419** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4420** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4421** locking-mode.
4422*/
4423int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004424 assert( eMode==PAGER_LOCKINGMODE_QUERY
4425 || eMode==PAGER_LOCKINGMODE_NORMAL
4426 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4427 assert( PAGER_LOCKINGMODE_QUERY<0 );
4428 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4429 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00004430 pPager->exclusiveMode = eMode;
4431 }
4432 return (int)pPager->exclusiveMode;
4433}
4434
dougcurrie81c95ef2004-06-18 23:21:47 +00004435#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00004436/*
4437** Return the current state of the file lock for the given pager.
4438** The return value is one of NO_LOCK, SHARED_LOCK, RESERVED_LOCK,
4439** PENDING_LOCK, or EXCLUSIVE_LOCK.
4440*/
danielk19773b8a05f2007-03-19 17:44:26 +00004441int sqlite3PagerLockstate(Pager *pPager){
drh054889e2005-11-30 03:20:31 +00004442 return sqlite3OsLockState(pPager->fd);
drh89ac8c12004-06-09 14:17:20 +00004443}
4444#endif
4445
danielk197793758c82005-01-21 08:13:14 +00004446#ifdef SQLITE_DEBUG
drhdd793422001-06-28 01:54:48 +00004447/*
4448** Print a listing of all referenced pages and their ref count.
4449*/
danielk19773b8a05f2007-03-19 17:44:26 +00004450void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00004451 PgHdr *pPg;
4452 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4453 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00004454 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
4455 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00004456 }
4457}
4458#endif
drh2e66f0b2005-04-28 17:18:48 +00004459
4460#endif /* SQLITE_OMIT_DISKIO */