blob: e2514fdb424aaf8cf7e05814dc9898c37834019b [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**
danielk197724168722007-04-02 05:07:47 +000021** @(#) $Id: pager.c,v 1.314 2007/04/02 05:07:47 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
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
53** associated file-descriptor is returned. FILEHANDLEID() takes an OsFile
54** 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.
drhed7c8552001-04-11 14:29:21 +0000153*/
drhd9b02572001-04-15 00:37:09 +0000154typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +0000155struct PgHdr {
156 Pager *pPager; /* The pager to which this page belongs */
157 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000158 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhd9b02572001-04-15 00:37:09 +0000159 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
drhac69b052004-05-12 13:30:07 +0000160 PgHdr *pNextAll; /* A list of all pages */
161 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
drh193a6b42002-07-07 16:52:46 +0000162 u8 inJournal; /* TRUE if has been written to journal */
drhac69b052004-05-12 13:30:07 +0000163 u8 inStmt; /* TRUE if in the statement subjournal */
drh193a6b42002-07-07 16:52:46 +0000164 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000165 u8 needSync; /* Sync journal before writing this page */
drh80e35f42007-03-30 14:06:34 +0000166 u8 alwaysRollback; /* Disable DontRollback() for this page */
drhac69b052004-05-12 13:30:07 +0000167 short int nRef; /* Number of users of this page */
drhdc3e10b2006-06-15 14:31:06 +0000168 PgHdr *pDirty, *pPrevDirty; /* Dirty pages */
drh605ad8f2006-05-03 23:34:05 +0000169 u32 notUsed; /* Buffer space */
danielk19773c407372005-02-15 02:54:14 +0000170#ifdef SQLITE_CHECK_PAGES
171 u32 pageHash;
172#endif
drh1c7880e2005-05-20 20:01:55 +0000173 /* pPager->pageSize bytes of page data follow this header */
drh973b6e32003-02-12 14:09:42 +0000174 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +0000175};
176
drhac69b052004-05-12 13:30:07 +0000177/*
178** For an in-memory only database, some extra information is recorded about
179** each page so that changes can be rolled back. (Journal files are not
180** used for in-memory databases.) The following information is added to
181** the end of every EXTRA block for in-memory databases.
182**
183** This information could have been added directly to the PgHdr structure.
184** But then it would take up an extra 8 bytes of storage on every PgHdr
185** even for disk-based databases. Splitting it out saves 8 bytes. This
186** is only a savings of 0.8% but those percentages add up.
187*/
188typedef struct PgHistory PgHistory;
189struct PgHistory {
190 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
191 u8 *pStmt; /* Text as it was at the beginning of the current statement */
192};
drh9eb9e262004-02-11 02:18:05 +0000193
194/*
195** A macro used for invoking the codec if there is one
196*/
197#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000198# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
199# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000200#else
drhc001c582006-03-06 18:23:16 +0000201# define CODEC1(P,D,N,X) /* NO-OP */
202# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000203#endif
204
drhed7c8552001-04-11 14:29:21 +0000205/*
drh69688d52001-04-14 16:38:23 +0000206** Convert a pointer to a PgHdr into a pointer to its data
207** and back again.
drhed7c8552001-04-11 14:29:21 +0000208*/
209#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
210#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drh1c7880e2005-05-20 20:01:55 +0000211#define PGHDR_TO_EXTRA(G,P) ((void*)&((char*)(&(G)[1]))[(P)->pageSize])
drhac69b052004-05-12 13:30:07 +0000212#define PGHDR_TO_HIST(P,PGR) \
drh1c7880e2005-05-20 20:01:55 +0000213 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000214
215/*
drhed7c8552001-04-11 14:29:21 +0000216** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000217**
drh4f0ee682007-03-30 20:43:40 +0000218** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000219** or SQLITE_FULL. Once one of the first three errors occurs, it persists
220** and is returned as the result of every major pager API call. The
221** SQLITE_FULL return code is slightly different. It persists only until the
222** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000223** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000224** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000225*/
226struct Pager {
drh603240c2002-03-05 01:11:12 +0000227 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000228 u8 journalStarted; /* True if header of journal is synced */
229 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000230 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000231 u8 stmtOpen; /* True if the statement subjournal is open */
232 u8 stmtInUse; /* True we are in a statement subtransaction */
233 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000234 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000235 u8 fullSync; /* Do extra syncs of the journal for robustness */
drhac530b12006-02-11 01:25:50 +0000236 u8 full_fsync; /* Use F_FULLFSYNC when available */
drha6abd042004-06-09 17:37:22 +0000237 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000238 u8 tempFile; /* zFilename is a temporary file */
239 u8 readOnly; /* True for a read-only database */
240 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000241 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000242 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000243 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000244 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000245 u8 doNotSync; /* Boolean. While true, do not spill the cache */
246 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
247 u8 changeCountDone; /* Set after incrementing the change-counter */
drhe49f9822006-09-15 12:29:16 +0000248 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000249 int dbSize; /* Number of pages in the file */
250 int origDbSize; /* dbSize before the current change */
251 int stmtSize; /* Size of database (in pages) at stmt_begin() */
252 int nRec; /* Number of pages written to the journal */
253 u32 cksumInit; /* Quasi-random value added to every checksum */
254 int stmtNRec; /* Number of records in stmt subjournal */
255 int nExtra; /* Add this many bytes to each in-memory page */
256 int pageSize; /* Number of bytes in a page */
257 int nPage; /* Total number of in-memory pages */
258 int nMaxPage; /* High water mark of nPage */
259 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
260 int mxPage; /* Maximum number of pages to hold in cache */
drh603240c2002-03-05 01:11:12 +0000261 u8 *aInJournal; /* One bit for each page in the database file */
drhac69b052004-05-12 13:30:07 +0000262 u8 *aInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000263 char *zFilename; /* Name of the database file */
264 char *zJournal; /* Name of the journal file */
265 char *zDirectory; /* Directory hold database and journal files */
drh9cbe6352005-11-29 03:13:21 +0000266 OsFile *fd, *jfd; /* File descriptors for database and journal */
267 OsFile *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000268 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
drhed7c8552001-04-11 14:29:21 +0000269 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000270 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000271 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000272 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000273 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000274 i64 journalOff; /* Current byte offset in the journal file */
275 i64 journalHdr; /* Byte offset to previous journal header */
276 i64 stmtHdrOff; /* First journal header written this statement */
277 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000278 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000279 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000280#ifdef SQLITE_TEST
drh6d156e42005-05-20 20:11:20 +0000281 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
282 int nRead,nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000283#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000284 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
285 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drhc001c582006-03-06 18:23:16 +0000286 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000287 void *pCodecArg; /* First argument to xCodec() */
drh8ca0c722006-05-07 17:49:38 +0000288 int nHash; /* Size of the pager hash table */
289 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000290#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +0000291 Pager *pNext; /* Linked list of pagers in this thread */
292#endif
danielk19778186df82007-03-06 13:45:59 +0000293 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
danielk1977e277be02007-03-23 18:12:06 +0000294 u32 iChangeCount; /* Db change-counter for which cache is valid */
drhd9b02572001-04-15 00:37:09 +0000295};
296
297/*
drhfcd35c72005-05-21 02:48:08 +0000298** If SQLITE_TEST is defined then increment the variable given in
299** the argument
300*/
301#ifdef SQLITE_TEST
302# define TEST_INCR(x) x++
303#else
304# define TEST_INCR(x)
305#endif
306
307/*
drh5e00f6c2001-09-13 13:46:56 +0000308** Journal files begin with the following magic string. The data
309** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000310**
drhae2b40c2004-06-09 19:03:54 +0000311** Since version 2.8.0, the journal format contains additional sanity
312** checking information. If the power fails while the journal is begin
313** written, semi-random garbage data might appear in the journal
314** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000315** to roll the journal back, the database could be corrupted. The additional
316** sanity checking data is an attempt to discover the garbage in the
317** journal and ignore it.
318**
drhae2b40c2004-06-09 19:03:54 +0000319** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000320** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000321** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000322** This cksum is initialized to a 32-bit random value that appears in the
323** journal file right after the header. The random initializer is important,
324** because garbage data that appears at the end of a journal is likely
325** data that was once in other files that have now been deleted. If the
326** garbage data came from an obsolete journal file, the checksums might
327** be correct. But by initializing the checksum to random value which
328** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000329*/
drhae2b40c2004-06-09 19:03:54 +0000330static const unsigned char aJournalMagic[] = {
331 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000332};
333
334/*
drh726de592004-06-10 23:35:50 +0000335** The size of the header and of each page in the journal is determined
336** by the following macros.
drh968af522003-02-11 14:55:40 +0000337*/
drhae2b40c2004-06-09 19:03:54 +0000338#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000339
danielk197776572402004-06-25 02:38:54 +0000340/*
341** The journal header size for this pager. In the future, this could be
342** set to some value read from the disk controller. The important
343** characteristic is that it is the same size as a disk sector.
344*/
345#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
346
drhb7f91642004-10-31 02:22:47 +0000347/*
348** The macro MEMDB is true if we are dealing with an in-memory database.
349** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
350** the value of MEMDB will be a constant and the compiler will optimize
351** out code that would never execute.
352*/
353#ifdef SQLITE_OMIT_MEMORYDB
354# define MEMDB 0
355#else
356# define MEMDB pPager->memDb
357#endif
358
359/*
danielk197776572402004-06-25 02:38:54 +0000360** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
361** reserved for working around a windows/posix incompatibility). It is
362** used in the journal to signify that the remainder of the journal file
363** is devoted to storing a master journal name - there are no more pages to
364** roll back. See comments for function writeMasterJournal() for details.
365*/
danielk1977599fcba2004-11-08 07:13:13 +0000366/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
367#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000368
drh968af522003-02-11 14:55:40 +0000369/*
danielk197726836652005-01-17 01:33:13 +0000370** The maximum legal page number is (2^31 - 1).
371*/
372#define PAGER_MAX_PGNO 2147483647
373
374/*
drh726de592004-06-10 23:35:50 +0000375** Enable reference count tracking (for debugging) here:
drhdd793422001-06-28 01:54:48 +0000376*/
drh0f7eb612006-08-08 13:51:43 +0000377#ifdef SQLITE_TEST
drh3aac2dd2004-04-26 14:10:20 +0000378 int pager3_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000379 static void pager_refinfo(PgHdr *p){
380 static int cnt = 0;
drh3aac2dd2004-04-26 14:10:20 +0000381 if( !pager3_refinfo_enable ) return;
drhfe63d1c2004-09-08 20:13:04 +0000382 sqlite3DebugPrintf(
drh24c9a2e2007-01-05 02:00:47 +0000383 "REFCNT: %4d addr=%p nRef=%-3d total=%d\n",
384 p->pgno, PGHDR_TO_DATA(p), p->nRef, p->pPager->nRef
drhdd793422001-06-28 01:54:48 +0000385 );
386 cnt++; /* Something to set a breakpoint on */
387 }
388# define REFINFO(X) pager_refinfo(X)
389#else
390# define REFINFO(X)
391#endif
392
drh8ca0c722006-05-07 17:49:38 +0000393
394/*
395** Change the size of the pager hash table to N. N must be a power
396** of two.
397*/
398static void pager_resize_hash_table(Pager *pPager, int N){
399 PgHdr **aHash, *pPg;
400 assert( N>0 && (N&(N-1))==0 );
401 aHash = sqliteMalloc( sizeof(aHash[0])*N );
402 if( aHash==0 ){
403 /* Failure to rehash is not an error. It is only a performance hit. */
404 return;
405 }
406 sqliteFree(pPager->aHash);
407 pPager->nHash = N;
408 pPager->aHash = aHash;
409 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000410 int h;
411 if( pPg->pgno==0 ){
412 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
413 continue;
414 }
415 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000416 pPg->pNextHash = aHash[h];
417 if( aHash[h] ){
418 aHash[h]->pPrevHash = pPg;
419 }
420 aHash[h] = pPg;
421 pPg->pPrevHash = 0;
422 }
423}
424
drhdd793422001-06-28 01:54:48 +0000425/*
drh34e79ce2004-02-08 06:05:46 +0000426** Read a 32-bit integer from the given file descriptor. Store the integer
427** that is read in *pRes. Return SQLITE_OK if everything worked, or an
428** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000429**
430** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000431*/
drhae2b40c2004-06-09 19:03:54 +0000432static int read32bits(OsFile *fd, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000433 unsigned char ac[4];
434 int rc = sqlite3OsRead(fd, ac, sizeof(ac));
drhae2b40c2004-06-09 19:03:54 +0000435 if( rc==SQLITE_OK ){
drh3b59a5c2006-01-15 20:28:28 +0000436 *pRes = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
drh94f33312002-08-12 12:29:56 +0000437 }
drh94f33312002-08-12 12:29:56 +0000438 return rc;
439}
440
441/*
drh97b57482006-01-10 20:32:32 +0000442** Write a 32-bit integer into a string buffer in big-endian byte order.
443*/
444static void put32bits(char *ac, u32 val){
445 ac[0] = (val>>24) & 0xff;
446 ac[1] = (val>>16) & 0xff;
447 ac[2] = (val>>8) & 0xff;
448 ac[3] = val & 0xff;
449}
450
451/*
drh34e79ce2004-02-08 06:05:46 +0000452** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
453** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000454*/
455static int write32bits(OsFile *fd, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000456 char ac[4];
drh97b57482006-01-10 20:32:32 +0000457 put32bits(ac, val);
drh054889e2005-11-30 03:20:31 +0000458 return sqlite3OsWrite(fd, ac, 4);
drh94f33312002-08-12 12:29:56 +0000459}
460
drh2554f8b2003-01-22 01:26:44 +0000461/*
danielk1977aa5ccdf2004-06-14 05:10:42 +0000462** Read a 32-bit integer at offset 'offset' from the page identified by
463** page header 'p'.
464*/
465static u32 retrieve32bits(PgHdr *p, int offset){
466 unsigned char *ac;
467 ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset];
468 return (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
469}
470
drh94f33312002-08-12 12:29:56 +0000471
472/*
danielk1977aef0bf62005-12-30 16:28:01 +0000473** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000474** code. The first argument is a pointer to the pager structure, the
475** second the error-code about to be returned by a pager API function.
476** The value returned is a copy of the second argument to this function.
477**
drh4f0ee682007-03-30 20:43:40 +0000478** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977a96a7102006-01-16 12:46:41 +0000479** the error becomes persistent. All subsequent API calls on this Pager
480** will immediately return the same error code.
danielk1977aef0bf62005-12-30 16:28:01 +0000481*/
482static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000483 int rc2 = rc & 0xff;
danielk1977efaaf572006-01-16 11:29:19 +0000484 assert( pPager->errCode==SQLITE_FULL || pPager->errCode==SQLITE_OK );
danielk1977979f38e2007-03-27 16:19:51 +0000485 if(
drh4ac285a2006-09-15 07:28:50 +0000486 rc2==SQLITE_FULL ||
487 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000488 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000489 ){
490 pPager->errCode = rc;
danielk1977aef0bf62005-12-30 16:28:01 +0000491 }
492 return rc;
493}
494
danielk19773c407372005-02-15 02:54:14 +0000495#ifdef SQLITE_CHECK_PAGES
496/*
497** Return a 32-bit hash of the page data for pPage.
498*/
499static u32 pager_pagehash(PgHdr *pPage){
500 u32 hash = 0;
501 int i;
502 unsigned char *pData = (unsigned char *)PGHDR_TO_DATA(pPage);
503 for(i=0; i<pPage->pPager->pageSize; i++){
504 hash = (hash+i)^pData[i];
505 }
506 return hash;
507}
508
509/*
510** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
511** is defined, and NDEBUG is not defined, an assert() statement checks
512** that the page is either dirty or still matches the calculated page-hash.
513*/
514#define CHECK_PAGE(x) checkPage(x)
515static void checkPage(PgHdr *pPg){
516 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000517 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000518 pPg->pageHash==pager_pagehash(pPg) );
519}
520
521#else
522#define CHECK_PAGE(x)
523#endif
524
drhed7c8552001-04-11 14:29:21 +0000525/*
danielk197776572402004-06-25 02:38:54 +0000526** When this is called the journal file for pager pPager must be open.
527** The master journal file name is read from the end of the file and
528** written into memory obtained from sqliteMalloc(). *pzMaster is
529** set to point at the memory and SQLITE_OK returned. The caller must
530** sqliteFree() *pzMaster.
531**
532** If no master journal file name is present *pzMaster is set to 0 and
533** SQLITE_OK returned.
534*/
535static int readMasterJournal(OsFile *pJrnl, char **pzMaster){
536 int rc;
537 u32 len;
drheb206252004-10-01 02:00:31 +0000538 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000539 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000540 int i;
danielk197776572402004-06-25 02:38:54 +0000541 unsigned char aMagic[8]; /* A buffer to hold the magic header */
542
543 *pzMaster = 0;
544
drh054889e2005-11-30 03:20:31 +0000545 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000546 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000547
drh054889e2005-11-30 03:20:31 +0000548 rc = sqlite3OsSeek(pJrnl, szJ-16);
danielk197776572402004-06-25 02:38:54 +0000549 if( rc!=SQLITE_OK ) return rc;
550
551 rc = read32bits(pJrnl, &len);
552 if( rc!=SQLITE_OK ) return rc;
553
danielk1977cafadba2004-06-25 11:11:54 +0000554 rc = read32bits(pJrnl, &cksum);
555 if( rc!=SQLITE_OK ) return rc;
556
drh054889e2005-11-30 03:20:31 +0000557 rc = sqlite3OsRead(pJrnl, aMagic, 8);
danielk197776572402004-06-25 02:38:54 +0000558 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
559
drh054889e2005-11-30 03:20:31 +0000560 rc = sqlite3OsSeek(pJrnl, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000561 if( rc!=SQLITE_OK ) return rc;
562
danielk19778191bff2004-06-28 04:52:30 +0000563 *pzMaster = (char *)sqliteMalloc(len+1);
danielk197776572402004-06-25 02:38:54 +0000564 if( !*pzMaster ){
565 return SQLITE_NOMEM;
566 }
drh054889e2005-11-30 03:20:31 +0000567 rc = sqlite3OsRead(pJrnl, *pzMaster, len);
danielk197776572402004-06-25 02:38:54 +0000568 if( rc!=SQLITE_OK ){
569 sqliteFree(*pzMaster);
570 *pzMaster = 0;
571 return rc;
572 }
danielk1977cafadba2004-06-25 11:11:54 +0000573
574 /* See if the checksum matches the master journal name */
575 for(i=0; i<len; i++){
danielk19778191bff2004-06-28 04:52:30 +0000576 cksum -= (*pzMaster)[i];
danielk1977cafadba2004-06-25 11:11:54 +0000577 }
danielk19778191bff2004-06-28 04:52:30 +0000578 if( cksum ){
579 /* If the checksum doesn't add up, then one or more of the disk sectors
580 ** containing the master journal filename is corrupted. This means
581 ** definitely roll back, so just return SQLITE_OK and report a (nul)
582 ** master-journal filename.
583 */
danielk1977cafadba2004-06-25 11:11:54 +0000584 sqliteFree(*pzMaster);
585 *pzMaster = 0;
danielk1977aca790a2005-01-13 11:07:52 +0000586 }else{
587 (*pzMaster)[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000588 }
danielk197776572402004-06-25 02:38:54 +0000589
590 return SQLITE_OK;
591}
592
593/*
594** Seek the journal file descriptor to the next sector boundary where a
595** journal header may be read or written. Pager.journalOff is updated with
596** the new seek offset.
597**
598** i.e for a sector size of 512:
599**
600** Input Offset Output Offset
601** ---------------------------------------
602** 0 0
603** 512 512
604** 100 512
605** 2000 2048
606**
607*/
608static int seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000609 i64 offset = 0;
610 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000611 if( c ){
612 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
613 }
614 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
615 assert( offset>=c );
616 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
617 pPager->journalOff = offset;
drh054889e2005-11-30 03:20:31 +0000618 return sqlite3OsSeek(pPager->jfd, pPager->journalOff);
danielk197776572402004-06-25 02:38:54 +0000619}
620
621/*
622** The journal file must be open when this routine is called. A journal
623** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
624** current location.
625**
626** The format for the journal header is as follows:
627** - 8 bytes: Magic identifying journal format.
628** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
629** - 4 bytes: Random number used for page hash.
630** - 4 bytes: Initial database page count.
631** - 4 bytes: Sector size used by the process that wrote this journal.
632**
danielk1977f42f25c2004-06-25 07:21:28 +0000633** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000634*/
635static int writeJournalHdr(Pager *pPager){
drh97b57482006-01-10 20:32:32 +0000636 char zHeader[sizeof(aJournalMagic)+16];
danielk19774099f6e2007-03-19 11:25:20 +0000637 int rc;
danielk197776572402004-06-25 02:38:54 +0000638
danielk19774099f6e2007-03-19 11:25:20 +0000639 if( pPager->stmtHdrOff==0 ){
640 pPager->stmtHdrOff = pPager->journalOff;
641 }
642
643 rc = seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000644 if( rc ) return rc;
645
646 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000647 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
648
649 /* FIX ME:
650 **
651 ** Possibly for a pager not in no-sync mode, the journal magic should not
652 ** be written until nRec is filled in as part of next syncJournal().
653 **
654 ** Actually maybe the whole journal header should be delayed until that
655 ** point. Think about this.
656 */
drh97b57482006-01-10 20:32:32 +0000657 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
658 /* The nRec Field. 0xFFFFFFFF for no-sync journals. */
659 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->noSync ? 0xffffffff : 0);
660 /* The random check-hash initialiser */
661 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
662 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
663 /* The initial database size */
664 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
665 /* The assumed sector size for this process */
666 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drhb0603412007-02-28 04:47:26 +0000667 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
drh97b57482006-01-10 20:32:32 +0000668 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader));
danielk197776572402004-06-25 02:38:54 +0000669
670 /* The journal header has been written successfully. Seek the journal
671 ** file descriptor to the end of the journal header sector.
672 */
673 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +0000674 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
drh054889e2005-11-30 03:20:31 +0000675 rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff-1);
drhb4746b92005-09-09 01:32:06 +0000676 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +0000677 rc = sqlite3OsWrite(pPager->jfd, "\000", 1);
drhb4746b92005-09-09 01:32:06 +0000678 }
danielk197776572402004-06-25 02:38:54 +0000679 }
680 return rc;
681}
682
683/*
684** The journal file must be open when this is called. A journal header file
685** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
686** file. See comments above function writeJournalHdr() for a description of
687** the journal header format.
688**
689** If the header is read successfully, *nRec is set to the number of
690** page records following this header and *dbSize is set to the size of the
691** database before the transaction began, in pages. Also, pPager->cksumInit
692** is set to the value read from the journal header. SQLITE_OK is returned
693** in this case.
694**
695** If the journal header file appears to be corrupted, SQLITE_DONE is
696** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
697** cannot be read from the journal file an error code is returned.
698*/
699static int readJournalHdr(
700 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000701 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000702 u32 *pNRec,
703 u32 *pDbSize
704){
705 int rc;
706 unsigned char aMagic[8]; /* A buffer to hold the magic header */
707
708 rc = seekJournalHdr(pPager);
709 if( rc ) return rc;
710
711 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
712 return SQLITE_DONE;
713 }
714
drh054889e2005-11-30 03:20:31 +0000715 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic));
danielk197776572402004-06-25 02:38:54 +0000716 if( rc ) return rc;
717
718 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
719 return SQLITE_DONE;
720 }
721
drh9cbe6352005-11-29 03:13:21 +0000722 rc = read32bits(pPager->jfd, pNRec);
danielk197776572402004-06-25 02:38:54 +0000723 if( rc ) return rc;
724
drh9cbe6352005-11-29 03:13:21 +0000725 rc = read32bits(pPager->jfd, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000726 if( rc ) return rc;
727
drh9cbe6352005-11-29 03:13:21 +0000728 rc = read32bits(pPager->jfd, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000729 if( rc ) return rc;
730
731 /* Update the assumed sector-size to match the value used by
732 ** the process that created this journal. If this journal was
733 ** created by a process other than this one, then this routine
734 ** is being called from within pager_playback(). The local value
735 ** of Pager.sectorSize is restored at the end of that routine.
736 */
drh9cbe6352005-11-29 03:13:21 +0000737 rc = read32bits(pPager->jfd, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000738 if( rc ) return rc;
739
740 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
drh054889e2005-11-30 03:20:31 +0000741 rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff);
danielk197776572402004-06-25 02:38:54 +0000742 return rc;
743}
744
745
746/*
747** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000748** pPager at the current location. The master journal name must be the last
749** thing written to a journal file. If the pager is in full-sync mode, the
750** journal file descriptor is advanced to the next sector boundary before
751** anything is written. The format is:
752**
753** + 4 bytes: PAGER_MJ_PGNO.
754** + N bytes: length of master journal name.
755** + 4 bytes: N
756** + 4 bytes: Master journal name checksum.
757** + 8 bytes: aJournalMagic[].
758**
759** The master journal page checksum is the sum of the bytes in the master
760** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000761**
762** If zMaster is a NULL pointer (occurs for a single database transaction),
763** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000764*/
765static int writeMasterJournal(Pager *pPager, const char *zMaster){
766 int rc;
767 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000768 int i;
drh97b57482006-01-10 20:32:32 +0000769 u32 cksum = 0;
770 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000771
772 if( !zMaster || pPager->setMaster) return SQLITE_OK;
773 pPager->setMaster = 1;
774
775 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000776 for(i=0; i<len; i++){
777 cksum += zMaster[i];
778 }
danielk197776572402004-06-25 02:38:54 +0000779
780 /* If in full-sync mode, advance to the next disk sector before writing
781 ** the master journal name. This is in case the previous page written to
782 ** the journal has already been synced.
783 */
784 if( pPager->fullSync ){
785 rc = seekJournalHdr(pPager);
786 if( rc!=SQLITE_OK ) return rc;
787 }
danielk1977cafadba2004-06-25 11:11:54 +0000788 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000789
drh9cbe6352005-11-29 03:13:21 +0000790 rc = write32bits(pPager->jfd, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000791 if( rc!=SQLITE_OK ) return rc;
792
drh054889e2005-11-30 03:20:31 +0000793 rc = sqlite3OsWrite(pPager->jfd, zMaster, len);
danielk197776572402004-06-25 02:38:54 +0000794 if( rc!=SQLITE_OK ) return rc;
795
drh97b57482006-01-10 20:32:32 +0000796 put32bits(zBuf, len);
797 put32bits(&zBuf[4], cksum);
798 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
799 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic));
drh2c8997b2005-08-27 16:36:48 +0000800 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +0000801 return rc;
802}
803
804/*
drh03eb96a2002-11-10 23:32:56 +0000805** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +0000806** statement journal.
drh03eb96a2002-11-10 23:32:56 +0000807**
808** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +0000809** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +0000810** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +0000811** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +0000812*/
drh3aac2dd2004-04-26 14:10:20 +0000813static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +0000814 Pager *pPager = pPg->pPager;
drhac69b052004-05-12 13:30:07 +0000815 if( pPg->inStmt ) return;
816 assert( pPg->pPrevStmt==0 && pPg->pNextStmt==0 );
817 pPg->pPrevStmt = 0;
818 if( pPager->pStmt ){
819 pPager->pStmt->pPrevStmt = pPg;
drh03eb96a2002-11-10 23:32:56 +0000820 }
drhac69b052004-05-12 13:30:07 +0000821 pPg->pNextStmt = pPager->pStmt;
822 pPager->pStmt = pPg;
823 pPg->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +0000824}
drh3aac2dd2004-04-26 14:10:20 +0000825static void page_remove_from_stmt_list(PgHdr *pPg){
drhac69b052004-05-12 13:30:07 +0000826 if( !pPg->inStmt ) return;
827 if( pPg->pPrevStmt ){
828 assert( pPg->pPrevStmt->pNextStmt==pPg );
829 pPg->pPrevStmt->pNextStmt = pPg->pNextStmt;
drh03eb96a2002-11-10 23:32:56 +0000830 }else{
drhac69b052004-05-12 13:30:07 +0000831 assert( pPg->pPager->pStmt==pPg );
832 pPg->pPager->pStmt = pPg->pNextStmt;
drh03eb96a2002-11-10 23:32:56 +0000833 }
drhac69b052004-05-12 13:30:07 +0000834 if( pPg->pNextStmt ){
835 assert( pPg->pNextStmt->pPrevStmt==pPg );
836 pPg->pNextStmt->pPrevStmt = pPg->pPrevStmt;
drh03eb96a2002-11-10 23:32:56 +0000837 }
drhac69b052004-05-12 13:30:07 +0000838 pPg->pNextStmt = 0;
839 pPg->pPrevStmt = 0;
840 pPg->inStmt = 0;
drh03eb96a2002-11-10 23:32:56 +0000841}
842
843/*
drhed7c8552001-04-11 14:29:21 +0000844** Find a page in the hash table given its page number. Return
845** a pointer to the page or NULL if not found.
846*/
drhd9b02572001-04-15 00:37:09 +0000847static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000848 PgHdr *p;
849 if( pPager->aHash==0 ) return 0;
850 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +0000851 while( p && p->pgno!=pgno ){
852 p = p->pNextHash;
853 }
854 return p;
855}
856
857/*
drh1aa2d8b2007-01-03 15:34:29 +0000858** Unlock the database file.
drh1aa2d8b2007-01-03 15:34:29 +0000859*/
860static void pager_unlock(Pager *pPager){
danielk197741483462007-03-24 16:45:04 +0000861 if( !pPager->exclusiveMode ){
862 if( !MEMDB ){
863 sqlite3OsUnlock(pPager->fd, NO_LOCK);
864 pPager->dbSize = -1;
865 IOTRACE(("UNLOCK %p\n", pPager))
866 }
867 pPager->state = PAGER_UNLOCK;
868 pPager->changeCountDone = 0;
drh1aa2d8b2007-01-03 15:34:29 +0000869 }
danielk1977e277be02007-03-23 18:12:06 +0000870}
871
872/*
873** Execute a rollback if a transaction is active and unlock the
874** database file. This is a no-op if the pager has already entered
875** the error-state.
876*/
danielk1977334cdb62007-03-26 08:05:12 +0000877static void pagerUnlockAndRollback(Pager *p){
878 if( p->errCode ) return;
danielk1977c5859712007-03-26 12:26:27 +0000879 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
danielk1977334cdb62007-03-26 08:05:12 +0000880 if( p->state>=PAGER_RESERVED ){
881 sqlite3PagerRollback(p);
danielk1977e277be02007-03-23 18:12:06 +0000882 }
danielk1977334cdb62007-03-26 08:05:12 +0000883 pager_unlock(p);
884 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
885 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drh1aa2d8b2007-01-03 15:34:29 +0000886}
887
888
889/*
drhed7c8552001-04-11 14:29:21 +0000890** Unlock the database and clear the in-memory cache. This routine
891** sets the state of the pager back to what it was when it was first
892** opened. Any outstanding pages are invalidated and subsequent attempts
893** to access those pages will likely result in a coredump.
894*/
drhd9b02572001-04-15 00:37:09 +0000895static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000896 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +0000897 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +0000898 for(pPg=pPager->pAll; pPg; pPg=pNext){
899 pNext = pPg->pNextAll;
900 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000901 }
danielk1977b94bf852007-03-19 13:53:37 +0000902 pPager->pStmt = 0;
drhed7c8552001-04-11 14:29:21 +0000903 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000904 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +0000905 pPager->pLast = 0;
906 pPager->pAll = 0;
drh8ca0c722006-05-07 17:49:38 +0000907 pPager->nHash = 0;
908 sqliteFree(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +0000909 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +0000910 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +0000911 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +0000912}
913
914/*
drh80e35f42007-03-30 14:06:34 +0000915** This routine ends a transaction. A transaction is ended by either
916** a COMMIT or a ROLLBACK.
917**
drhed7c8552001-04-11 14:29:21 +0000918** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +0000919** a RESERVED or EXCLUSIVE lock on the database. This routine will release
920** the database lock and acquires a SHARED lock in its place if that is
921** the appropriate thing to do. Release locks usually is appropriate,
922** unless we are in exclusive access mode or unless this is a
923** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
924**
925** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +0000926**
927** TODO: Consider keeping the journal file open for temporary databases.
928** This might give a performance improvement on windows where opening
929** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +0000930*/
drh80e35f42007-03-30 14:06:34 +0000931static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +0000932 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +0000933 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +0000934 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +0000935 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +0000936 if( pPager->state<PAGER_RESERVED ){
937 return SQLITE_OK;
938 }
danielk19773b8a05f2007-03-19 17:44:26 +0000939 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +0000940 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977979f38e2007-03-27 16:19:51 +0000941 sqlite3OsClose(&pPager->stfd);
942 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +0000943 }
drhda47d772002-12-02 04:25:19 +0000944 if( pPager->journalOpen ){
danielk197741483462007-03-24 16:45:04 +0000945 if( pPager->exclusiveMode ){
danielk1977979f38e2007-03-27 16:19:51 +0000946 rc = sqlite3OsTruncate(pPager->jfd, 0);
danielk197741483462007-03-24 16:45:04 +0000947 sqlite3OsSeek(pPager->jfd, 0);
948 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +0000949 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +0000950 }else{
951 sqlite3OsClose(&pPager->jfd);
952 pPager->journalOpen = 0;
danielk19777152de82007-03-29 17:28:14 +0000953 /* If this is a temporary pager file, then the journal file should
954 ** have been configured as delete-on-close. Otherwise, it should still
955 ** be in the file system. This pager still holds a RESERVED or greater
956 ** lock on the database file, so there is no chance another process
957 ** could create or remove a journal file.
drh1abd4222007-03-30 17:18:50 +0000958 **
959 ** These asserts are not valid for asynchronous I/O such as is found
960 ** in async.test
danielk19777152de82007-03-29 17:28:14 +0000961 */
drh1abd4222007-03-30 17:18:50 +0000962 /*assert( sqlite3OsFileExists(pPager->zJournal) || pPager->tempFile );*/
963 /*assert( !sqlite3OsFileExists(pPager->zJournal) || !pPager->tempFile );*/
danielk19777152de82007-03-29 17:28:14 +0000964 if( !pPager->tempFile ){
965 rc = sqlite3OsDelete(pPager->zJournal);
966 }
danielk197741483462007-03-24 16:45:04 +0000967 }
drhda47d772002-12-02 04:25:19 +0000968 sqliteFree( pPager->aInJournal );
969 pPager->aInJournal = 0;
970 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
971 pPg->inJournal = 0;
972 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +0000973 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +0000974 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +0000975#ifdef SQLITE_CHECK_PAGES
976 pPg->pageHash = pager_pagehash(pPg);
977#endif
drhda47d772002-12-02 04:25:19 +0000978 }
drh605ad8f2006-05-03 23:34:05 +0000979 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +0000980 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +0000981 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +0000982 }else{
drh88b01a12005-03-28 18:04:27 +0000983 assert( pPager->aInJournal==0 );
drha6abd042004-06-09 17:37:22 +0000984 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +0000985 }
danielk1977979f38e2007-03-27 16:19:51 +0000986
danielk197741483462007-03-24 16:45:04 +0000987 if( !pPager->exclusiveMode ){
danielk1977979f38e2007-03-27 16:19:51 +0000988 rc2 = sqlite3OsUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +0000989 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +0000990 }else if( pPager->state==PAGER_SYNCED ){
991 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +0000992 }
danielk1977334cdb62007-03-26 08:05:12 +0000993 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +0000994 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +0000995 pPager->needSync = 0;
996 pPager->pFirstSynced = pPager->pFirst;
drh588f5bc2007-01-02 18:41:54 +0000997 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +0000998
999 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001000}
1001
drhed7c8552001-04-11 14:29:21 +00001002/*
drh968af522003-02-11 14:55:40 +00001003** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001004**
1005** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001006** random initial value and the page number. We experimented with
1007** a checksum of the entire data, but that was found to be too slow.
1008**
1009** Note that the page number is stored at the beginning of data and
1010** the checksum is stored at the end. This is important. If journal
1011** corruption occurs due to a power failure, the most likely scenario
1012** is that one end or the other of the record will be changed. It is
1013** much less likely that the two ends of the journal record will be
1014** correct and the middle be corrupt. Thus, this "checksum" scheme,
1015** though fast and simple, catches the mostly likely kind of corruption.
1016**
1017** FIX ME: Consider adding every 200th (or so) byte of the data to the
1018** checksum. That way if a single page spans 3 or more disk sectors and
1019** only the middle sector is corrupt, we will still have a reasonable
1020** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001021*/
drh74161702006-02-24 02:53:49 +00001022static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001023 u32 cksum = pPager->cksumInit;
1024 int i = pPager->pageSize-200;
1025 while( i>0 ){
1026 cksum += aData[i];
1027 i -= 200;
1028 }
drh968af522003-02-11 14:55:40 +00001029 return cksum;
1030}
1031
drh605ad8f2006-05-03 23:34:05 +00001032/* Forward declaration */
1033static void makeClean(PgHdr*);
1034
drh968af522003-02-11 14:55:40 +00001035/*
drhfa86c412002-02-02 15:01:15 +00001036** Read a single page from the journal file opened on file descriptor
1037** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001038**
drh726de592004-06-10 23:35:50 +00001039** If useCksum==0 it means this journal does not use checksums. Checksums
1040** are not used in statement journals because statement journals do not
1041** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001042*/
drhae2b40c2004-06-09 19:03:54 +00001043static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int useCksum){
drhfa86c412002-02-02 15:01:15 +00001044 int rc;
drhae2b40c2004-06-09 19:03:54 +00001045 PgHdr *pPg; /* An existing page in the cache */
1046 Pgno pgno; /* The page number of a page in journal */
1047 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001048 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001049
drh96362842005-03-20 22:47:56 +00001050 /* useCksum should be true for the main journal and false for
1051 ** statement journals. Verify that this is always the case
1052 */
drh9cbe6352005-11-29 03:13:21 +00001053 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001054 assert( aData );
drh96362842005-03-20 22:47:56 +00001055
drhae2b40c2004-06-09 19:03:54 +00001056 rc = read32bits(jfd, &pgno);
drh99ee3602003-02-16 19:13:36 +00001057 if( rc!=SQLITE_OK ) return rc;
danielk19778186df82007-03-06 13:45:59 +00001058 rc = sqlite3OsRead(jfd, aData, pPager->pageSize);
drh99ee3602003-02-16 19:13:36 +00001059 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001060 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001061
drh968af522003-02-11 14:55:40 +00001062 /* Sanity checking on the page. This is more important that I originally
1063 ** thought. If a power failure occurs while the journal is being written,
1064 ** it could cause invalid data to be written into the journal. We need to
1065 ** detect this invalid data (with high probability) and ignore it.
1066 */
danielk197775edc162004-06-26 01:48:18 +00001067 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001068 return SQLITE_DONE;
1069 }
drhae2b40c2004-06-09 19:03:54 +00001070 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001071 return SQLITE_OK;
1072 }
drhae2b40c2004-06-09 19:03:54 +00001073 if( useCksum ){
1074 rc = read32bits(jfd, &cksum);
drh99ee3602003-02-16 19:13:36 +00001075 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001076 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001077 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001078 return SQLITE_DONE;
1079 }
1080 }
drhfa86c412002-02-02 15:01:15 +00001081
danielk1977aa5ccdf2004-06-14 05:10:42 +00001082 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001083
1084 /* If the pager is in RESERVED state, then there must be a copy of this
1085 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001086 ** not the database file. The page is left marked dirty in this case.
1087 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001088 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1089 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001090 **
1091 ** Ticket #1171: The statement journal might contain page content that is
1092 ** different from the page content at the start of the transaction.
1093 ** This occurs when a page is changed prior to the start of a statement
1094 ** then changed again within the statement. When rolling back such a
1095 ** statement we must not write to the original database unless we know
1096 ** for certain that original page contents are in the main rollback
1097 ** journal. Otherwise, if a full ROLLBACK occurs after the statement
1098 ** rollback the full ROLLBACK will not restore the page to its original
1099 ** content. Two conditions must be met before writing to the database
1100 ** files. (1) the database must be locked. (2) we know that the original
1101 ** page content is in the main journal either because the page is not in
1102 ** cache or else it is marked as needSync==0.
drhfa86c412002-02-02 15:01:15 +00001103 */
drhae2b40c2004-06-09 19:03:54 +00001104 pPg = pager_lookup(pPager, pgno);
drh96362842005-03-20 22:47:56 +00001105 assert( pPager->state>=PAGER_EXCLUSIVE || pPg!=0 );
drh4f0c5872007-03-26 22:05:01 +00001106 PAGERTRACE3("PLAYBACK %d page %d\n", PAGERID(pPager), pgno);
drh96362842005-03-20 22:47:56 +00001107 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
drh054889e2005-11-30 03:20:31 +00001108 rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize);
drhb4746b92005-09-09 01:32:06 +00001109 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001110 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize);
drhb4746b92005-09-09 01:32:06 +00001111 }
drh605ad8f2006-05-03 23:34:05 +00001112 if( pPg ){
1113 makeClean(pPg);
1114 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001115 }
drhfa86c412002-02-02 15:01:15 +00001116 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001117 /* No page should ever be explicitly rolled back that is in use, except
1118 ** for page 1 which is held in use in order to keep the lock on the
1119 ** database active. However such a page may be rolled back as a result
1120 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001121 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001122 */
drhb6f41482004-05-14 01:58:11 +00001123 void *pData;
danielk197728129562005-01-11 10:25:06 +00001124 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001125 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001126 memcpy(pData, aData, pPager->pageSize);
drh726de592004-06-10 23:35:50 +00001127 if( pPager->xDestructor ){ /*** FIX ME: Should this be xReinit? ***/
danielk19773b8a05f2007-03-19 17:44:26 +00001128 pPager->xDestructor(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001129 }
danielk19773c407372005-02-15 02:54:14 +00001130#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001131 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001132#endif
drhc001c582006-03-06 18:23:16 +00001133 CODEC1(pPager, pData, pPg->pgno, 3);
danielk197741483462007-03-24 16:45:04 +00001134
1135 /* If this was page 1, then restore the value of Pager.iChangeCount */
1136 if( pgno==1 ){
1137 pPager->iChangeCount = retrieve32bits(pPg, 24);
1138 }
drhfa86c412002-02-02 15:01:15 +00001139 }
1140 return rc;
1141}
1142
1143/*
danielk197713adf8a2004-06-03 16:08:41 +00001144** Parameter zMaster is the name of a master journal file. A single journal
1145** file that referred to the master journal file has just been rolled back.
1146** This routine checks if it is possible to delete the master journal file,
1147** and does so if it is.
drh726de592004-06-10 23:35:50 +00001148**
1149** The master journal file contains the names of all child journals.
1150** To tell if a master journal can be deleted, check to each of the
1151** children. If all children are either missing or do not refer to
1152** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001153*/
1154static int pager_delmaster(const char *zMaster){
1155 int rc;
1156 int master_open = 0;
drh9cbe6352005-11-29 03:13:21 +00001157 OsFile *master = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001158 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001159 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001160
1161 /* Open the master journal file exclusively in case some other process
1162 ** is running this routine also. Not that it makes too much difference.
1163 */
drh66560ad2006-01-06 14:32:19 +00001164 rc = sqlite3OsOpenReadOnly(zMaster, &master);
danielk197713adf8a2004-06-03 16:08:41 +00001165 if( rc!=SQLITE_OK ) goto delmaster_out;
1166 master_open = 1;
drh054889e2005-11-30 03:20:31 +00001167 rc = sqlite3OsFileSize(master, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001168 if( rc!=SQLITE_OK ) goto delmaster_out;
1169
1170 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001171 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001172 char *zMasterPtr = 0;
danielk19775865e3d2004-06-14 06:03:57 +00001173
1174 /* Load the entire master journal file into space obtained from
1175 ** sqliteMalloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001176 */
danielk197776572402004-06-25 02:38:54 +00001177 zMasterJournal = (char *)sqliteMalloc(nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001178 if( !zMasterJournal ){
1179 rc = SQLITE_NOMEM;
1180 goto delmaster_out;
1181 }
drh054889e2005-11-30 03:20:31 +00001182 rc = sqlite3OsRead(master, zMasterJournal, nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001183 if( rc!=SQLITE_OK ) goto delmaster_out;
1184
danielk19775865e3d2004-06-14 06:03:57 +00001185 zJournal = zMasterJournal;
1186 while( (zJournal-zMasterJournal)<nMasterJournal ){
drh66560ad2006-01-06 14:32:19 +00001187 if( sqlite3OsFileExists(zJournal) ){
danielk197713adf8a2004-06-03 16:08:41 +00001188 /* One of the journals pointed to by the master journal exists.
1189 ** Open it and check if it points at the master journal. If
1190 ** so, return without deleting the master journal file.
1191 */
drh9cbe6352005-11-29 03:13:21 +00001192 OsFile *journal = 0;
drh3b7b78b2004-11-24 01:16:43 +00001193 int c;
danielk197713adf8a2004-06-03 16:08:41 +00001194
drh66560ad2006-01-06 14:32:19 +00001195 rc = sqlite3OsOpenReadOnly(zJournal, &journal);
danielk197713adf8a2004-06-03 16:08:41 +00001196 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001197 goto delmaster_out;
1198 }
danielk197713adf8a2004-06-03 16:08:41 +00001199
drh9cbe6352005-11-29 03:13:21 +00001200 rc = readMasterJournal(journal, &zMasterPtr);
drh054889e2005-11-30 03:20:31 +00001201 sqlite3OsClose(&journal);
danielk19779eed5052004-06-04 10:38:30 +00001202 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001203 goto delmaster_out;
1204 }
danielk197776572402004-06-25 02:38:54 +00001205
drh3b7b78b2004-11-24 01:16:43 +00001206 c = zMasterPtr!=0 && strcmp(zMasterPtr, zMaster)==0;
1207 sqliteFree(zMasterPtr);
1208 if( c ){
danielk197776572402004-06-25 02:38:54 +00001209 /* We have a match. Do not delete the master journal file. */
1210 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001211 }
1212 }
danielk19775865e3d2004-06-14 06:03:57 +00001213 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001214 }
1215 }
1216
danielk1977979f38e2007-03-27 16:19:51 +00001217 rc = sqlite3OsDelete(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001218
1219delmaster_out:
1220 if( zMasterJournal ){
1221 sqliteFree(zMasterJournal);
1222 }
1223 if( master_open ){
drh054889e2005-11-30 03:20:31 +00001224 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001225 }
1226 return rc;
1227}
1228
danielk197741483462007-03-24 16:45:04 +00001229#if 0
danielk197713adf8a2004-06-03 16:08:41 +00001230/*
drha6abd042004-06-09 17:37:22 +00001231** Make every page in the cache agree with what is on disk. In other words,
1232** reread the disk to reset the state of the cache.
1233**
1234** This routine is called after a rollback in which some of the dirty cache
1235** pages had never been written out to disk. We need to roll back the
1236** cache content and the easiest way to do that is to reread the old content
1237** back from the disk.
1238*/
1239static int pager_reload_cache(Pager *pPager){
1240 PgHdr *pPg;
1241 int rc = SQLITE_OK;
1242 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
danielk19778186df82007-03-06 13:45:59 +00001243 char *zBuf = pPager->pTmpSpace; /* Temp storage for one page */
drha6abd042004-06-09 17:37:22 +00001244 if( !pPg->dirty ) continue;
1245 if( (int)pPg->pgno <= pPager->origDbSize ){
drh054889e2005-11-30 03:20:31 +00001246 rc = sqlite3OsSeek(pPager->fd, pPager->pageSize*(i64)(pPg->pgno-1));
drhb4746b92005-09-09 01:32:06 +00001247 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001248 rc = sqlite3OsRead(pPager->fd, zBuf, pPager->pageSize);
drhb4746b92005-09-09 01:32:06 +00001249 }
drh4f0c5872007-03-26 22:05:01 +00001250 PAGERTRACE3("REFETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
drha6abd042004-06-09 17:37:22 +00001251 if( rc ) break;
drhc001c582006-03-06 18:23:16 +00001252 CODEC1(pPager, zBuf, pPg->pgno, 2);
drha6abd042004-06-09 17:37:22 +00001253 }else{
drh90f5ecb2004-07-22 01:19:35 +00001254 memset(zBuf, 0, pPager->pageSize);
drha6abd042004-06-09 17:37:22 +00001255 }
drh90f5ecb2004-07-22 01:19:35 +00001256 if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), pPager->pageSize) ){
1257 memcpy(PGHDR_TO_DATA(pPg), zBuf, pPager->pageSize);
drha6abd042004-06-09 17:37:22 +00001258 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00001259 pPager->xReiniter(pPg, pPager->pageSize);
drha6abd042004-06-09 17:37:22 +00001260 }else{
drh90f5ecb2004-07-22 01:19:35 +00001261 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drha6abd042004-06-09 17:37:22 +00001262 }
1263 }
1264 pPg->needSync = 0;
1265 pPg->dirty = 0;
danielk19773c407372005-02-15 02:54:14 +00001266#ifdef SQLITE_CHECK_PAGES
1267 pPg->pageHash = pager_pagehash(pPg);
1268#endif
drha6abd042004-06-09 17:37:22 +00001269 }
drh605ad8f2006-05-03 23:34:05 +00001270 pPager->pDirty = 0;
drha6abd042004-06-09 17:37:22 +00001271 return rc;
1272}
danielk197741483462007-03-24 16:45:04 +00001273#endif
drha6abd042004-06-09 17:37:22 +00001274
drha6abd042004-06-09 17:37:22 +00001275/*
drhcb4c40b2004-08-18 19:09:43 +00001276** Truncate the main file of the given pager to the number of pages
1277** indicated.
1278*/
1279static int pager_truncate(Pager *pPager, int nPage){
danielk197717221812005-02-15 03:38:05 +00001280 assert( pPager->state>=PAGER_EXCLUSIVE );
drh054889e2005-11-30 03:20:31 +00001281 return sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
drhcb4c40b2004-08-18 19:09:43 +00001282}
1283
1284/*
drhed7c8552001-04-11 14:29:21 +00001285** Playback the journal and thus restore the database file to
1286** the state it was in before we started making changes.
1287**
drh34e79ce2004-02-08 06:05:46 +00001288** The journal file format is as follows:
1289**
drhae2b40c2004-06-09 19:03:54 +00001290** (1) 8 byte prefix. A copy of aJournalMagic[].
1291** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001292** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001293** number of page records from the journal size.
1294** (3) 4 byte big-endian integer which is the initial value for the
1295** sanity checksum.
1296** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001297** database to during a rollback.
drhae2b40c2004-06-09 19:03:54 +00001298** (5) 4 byte integer which is the number of bytes in the master journal
1299** name. The value may be zero (indicate that there is no master
1300** journal.)
1301** (6) N bytes of the master journal name. The name will be nul-terminated
1302** and might be shorter than the value read from (5). If the first byte
1303** of the name is \000 then there is no master journal. The master
1304** journal name is stored in UTF-8.
1305** (7) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001306** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001307** + pPager->pageSize bytes of data.
1308** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001309**
drhae2b40c2004-06-09 19:03:54 +00001310** When we speak of the journal header, we mean the first 6 items above.
1311** Each entry in the journal is an instance of the 7th item.
drh34e79ce2004-02-08 06:05:46 +00001312**
1313** Call the value from the second bullet "nRec". nRec is the number of
1314** valid page entries in the journal. In most cases, you can compute the
1315** value of nRec from the size of the journal file. But if a power
1316** failure occurred while the journal was being written, it could be the
1317** case that the size of the journal file had already been increased but
1318** the extra entries had not yet made it safely to disk. In such a case,
1319** the value of nRec computed from the file size would be too large. For
1320** that reason, we always use the nRec value in the header.
1321**
1322** If the nRec value is 0xffffffff it means that nRec should be computed
1323** from the file size. This value is used when the user selects the
1324** no-sync option for the journal. A power failure could lead to corruption
1325** in this case. But for things like temporary table (which will be
1326** deleted when the power is restored) we don't care.
1327**
drhd9b02572001-04-15 00:37:09 +00001328** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001329** journal file then all pages up to the first corrupted page are rolled
1330** back (or no pages if the journal header is corrupted). The journal file
1331** is then deleted and SQLITE_OK returned, just as if no corruption had
1332** been encountered.
1333**
1334** If an I/O or malloc() error occurs, the journal-file is not deleted
1335** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001336*/
danielk1977e277be02007-03-23 18:12:06 +00001337static int pager_playback(Pager *pPager, int isHot){
drheb206252004-10-01 02:00:31 +00001338 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001339 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001340 int i; /* Loop counter */
1341 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001342 int rc; /* Result code of a subroutine */
danielk197713adf8a2004-06-03 16:08:41 +00001343 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001344
drhc3a64ba2001-11-22 00:01:27 +00001345 /* Figure out how many records are in the journal. Abort early if
1346 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001347 */
drh8cfbf082001-09-19 13:22:39 +00001348 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001349 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001350 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001351 goto end_playback;
1352 }
drh240c5792004-02-08 00:40:52 +00001353
danielk197776572402004-06-25 02:38:54 +00001354 /* Read the master journal name from the journal, if it is present.
1355 ** If a master journal file name is specified, but the file is not
1356 ** present on disk, then the journal is not hot and does not need to be
1357 ** played back.
drh240c5792004-02-08 00:40:52 +00001358 */
drh9cbe6352005-11-29 03:13:21 +00001359 rc = readMasterJournal(pPager->jfd, &zMaster);
danielk197776572402004-06-25 02:38:54 +00001360 assert( rc!=SQLITE_DONE );
drh66560ad2006-01-06 14:32:19 +00001361 if( rc!=SQLITE_OK || (zMaster && !sqlite3OsFileExists(zMaster)) ){
danielk197776572402004-06-25 02:38:54 +00001362 sqliteFree(zMaster);
1363 zMaster = 0;
1364 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drhc3a64ba2001-11-22 00:01:27 +00001365 goto end_playback;
1366 }
drh054889e2005-11-30 03:20:31 +00001367 sqlite3OsSeek(pPager->jfd, 0);
danielk197776572402004-06-25 02:38:54 +00001368 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001369
danielk197776572402004-06-25 02:38:54 +00001370 /* This loop terminates either when the readJournalHdr() call returns
1371 ** SQLITE_DONE or an IO error occurs. */
1372 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001373
danielk197776572402004-06-25 02:38:54 +00001374 /* Read the next journal header from the journal file. If there are
1375 ** not enough bytes left in the journal file for a complete header, or
1376 ** it is corrupted, then a process must of failed while writing it.
1377 ** This indicates nothing more needs to be rolled back.
1378 */
1379 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1380 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001381 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001382 rc = SQLITE_OK;
1383 }
danielk197776572402004-06-25 02:38:54 +00001384 goto end_playback;
1385 }
1386
1387 /* If nRec is 0xffffffff, then this journal was created by a process
1388 ** working in no-sync mode. This means that the rest of the journal
1389 ** file consists of pages, there are no more journal headers. Compute
1390 ** the value of nRec based on this assumption.
1391 */
1392 if( nRec==0xffffffff ){
1393 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1394 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1395 }
1396
danielk1977e277be02007-03-23 18:12:06 +00001397 /* If nRec is 0 and this rollback is of a transaction created by this
1398 ** process. In this case the rest of the journal file consists of
1399 ** journalled copies of pages that need to be read back into the cache.
1400 */
1401 if( nRec==0 && !isHot ){
1402 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1403 }
1404
danielk197776572402004-06-25 02:38:54 +00001405 /* If this is the first header read from the journal, truncate the
1406 ** database file back to it's original size.
1407 */
danielk197717221812005-02-15 03:38:05 +00001408 if( pPager->state>=PAGER_EXCLUSIVE &&
1409 pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
danielk197776572402004-06-25 02:38:54 +00001410 assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg );
drhcb4c40b2004-08-18 19:09:43 +00001411 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001412 if( rc!=SQLITE_OK ){
1413 goto end_playback;
1414 }
1415 pPager->dbSize = mxPg;
1416 }
1417
danielk197776572402004-06-25 02:38:54 +00001418 /* Copy original pages out of the journal and back into the database file.
1419 */
1420 for(i=0; i<nRec; i++){
drh9cbe6352005-11-29 03:13:21 +00001421 rc = pager_playback_one_page(pPager, pPager->jfd, 1);
danielk197776572402004-06-25 02:38:54 +00001422 if( rc!=SQLITE_OK ){
1423 if( rc==SQLITE_DONE ){
1424 rc = SQLITE_OK;
1425 pPager->journalOff = szJ;
1426 break;
1427 }else{
1428 goto end_playback;
1429 }
1430 }
drh968af522003-02-11 14:55:40 +00001431 }
drhed7c8552001-04-11 14:29:21 +00001432 }
drh580eeaf2006-02-24 03:09:37 +00001433 /*NOTREACHED*/
1434 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001435
1436end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001437 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001438 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001439 }
danielk197713adf8a2004-06-03 16:08:41 +00001440 if( zMaster ){
danielk1977979f38e2007-03-27 16:19:51 +00001441 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001442 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001443 */
1444 if( rc==SQLITE_OK ){
danielk197732554c12005-01-22 03:39:39 +00001445 rc = pager_delmaster(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001446 }
1447 sqliteFree(zMaster);
1448 }
danielk197776572402004-06-25 02:38:54 +00001449
1450 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001451 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001452 ** value. Reset it to the correct value for this process.
1453 */
danielk1977b4721172007-03-19 05:54:48 +00001454 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
drhd9b02572001-04-15 00:37:09 +00001455 return rc;
drhed7c8552001-04-11 14:29:21 +00001456}
1457
1458/*
drhac69b052004-05-12 13:30:07 +00001459** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001460**
1461** This is similar to playing back the transaction journal but with
1462** a few extra twists.
1463**
drh663fc632002-02-02 18:49:19 +00001464** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001465** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001466** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001467**
drhac69b052004-05-12 13:30:07 +00001468** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001469** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001470** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001471*/
drh3aac2dd2004-04-26 14:10:20 +00001472static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001473 i64 szJ; /* Size of the full journal */
1474 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001475 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001476 int i; /* Loop counter */
1477 int rc;
1478
danielk197776572402004-06-25 02:38:54 +00001479 szJ = pPager->journalOff;
1480#ifndef NDEBUG
1481 {
drheb206252004-10-01 02:00:31 +00001482 i64 os_szJ;
drh054889e2005-11-30 03:20:31 +00001483 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
danielk197776572402004-06-25 02:38:54 +00001484 if( rc!=SQLITE_OK ) return rc;
1485 assert( szJ==os_szJ );
1486 }
1487#endif
1488
danielk19774099f6e2007-03-19 11:25:20 +00001489 /* Set hdrOff to be the offset just after the end of the last journal
1490 ** page written before the first journal-header for this statement
1491 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001492 ** header was written.
1493 */
1494 hdrOff = pPager->stmtHdrOff;
1495 assert( pPager->fullSync || !hdrOff );
1496 if( !hdrOff ){
1497 hdrOff = szJ;
1498 }
1499
drhfa86c412002-02-02 15:01:15 +00001500 /* Truncate the database back to its original size.
1501 */
danielk197717221812005-02-15 03:38:05 +00001502 if( pPager->state>=PAGER_EXCLUSIVE ){
1503 rc = pager_truncate(pPager, pPager->stmtSize);
1504 }
drh1aa2d8b2007-01-03 15:34:29 +00001505 assert( pPager->state>=PAGER_SHARED );
drhac69b052004-05-12 13:30:07 +00001506 pPager->dbSize = pPager->stmtSize;
drhfa86c412002-02-02 15:01:15 +00001507
drhac69b052004-05-12 13:30:07 +00001508 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001509 */
drhac69b052004-05-12 13:30:07 +00001510 assert( pPager->stmtInUse && pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001511 sqlite3OsSeek(pPager->stfd, 0);
drhac69b052004-05-12 13:30:07 +00001512 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001513
drhac69b052004-05-12 13:30:07 +00001514 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001515 ** database file. Note that the statement journal omits checksums from
1516 ** each record since power-failure recovery is not important to statement
1517 ** journals.
drhfa86c412002-02-02 15:01:15 +00001518 */
1519 for(i=nRec-1; i>=0; i--){
drh9cbe6352005-11-29 03:13:21 +00001520 rc = pager_playback_one_page(pPager, pPager->stfd, 0);
drh968af522003-02-11 14:55:40 +00001521 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001522 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001523 }
1524
danielk197776572402004-06-25 02:38:54 +00001525 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1526 ** was the size of the journal file when this statement was started, so
1527 ** everything after that needs to be rolled back, either into the
1528 ** database, the memory cache, or both.
1529 **
1530 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1531 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001532 */
drh054889e2005-11-30 03:20:31 +00001533 rc = sqlite3OsSeek(pPager->jfd, pPager->stmtJSize);
drhfa86c412002-02-02 15:01:15 +00001534 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00001535 goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001536 }
danielk197776572402004-06-25 02:38:54 +00001537 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001538 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001539 while( pPager->journalOff < hdrOff ){
drh9cbe6352005-11-29 03:13:21 +00001540 rc = pager_playback_one_page(pPager, pPager->jfd, 1);
danielk197776572402004-06-25 02:38:54 +00001541 assert( rc!=SQLITE_DONE );
1542 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1543 }
1544
1545 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001546 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001547 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001548 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001549 if( rc!=SQLITE_OK ){
1550 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001551 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001552 }
danielk1977f0113002006-01-24 12:09:17 +00001553 if( nJRec==0 ){
1554 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001555 }
danielk1977f0113002006-01-24 12:09:17 +00001556 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
drh9cbe6352005-11-29 03:13:21 +00001557 rc = pager_playback_one_page(pPager, pPager->jfd, 1);
danielk197776572402004-06-25 02:38:54 +00001558 assert( rc!=SQLITE_DONE );
1559 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1560 }
drhfa86c412002-02-02 15:01:15 +00001561 }
danielk197776572402004-06-25 02:38:54 +00001562
1563 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001564
drh3aac2dd2004-04-26 14:10:20 +00001565end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001566 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001567 pPager->journalOff = szJ;
1568 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001569 }
1570 return rc;
1571}
1572
1573/*
drhf57b14a2001-09-14 18:54:08 +00001574** Change the maximum number of in-memory pages that are allowed.
1575*/
danielk19773b8a05f2007-03-19 17:44:26 +00001576void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00001577 if( mxPage>10 ){
1578 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00001579 }else{
1580 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00001581 }
1582}
1583
1584/*
drh973b6e32003-02-12 14:09:42 +00001585** Adjust the robustness of the database to damage due to OS crashes
1586** or power failures by changing the number of syncs()s when writing
1587** the rollback journal. There are three levels:
1588**
drh054889e2005-11-30 03:20:31 +00001589** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001590** for temporary and transient files.
1591**
1592** NORMAL The journal is synced once before writes begin on the
1593** database. This is normally adequate protection, but
1594** it is theoretically possible, though very unlikely,
1595** that an inopertune power failure could leave the journal
1596** in a state which would cause damage to the database
1597** when it is rolled back.
1598**
1599** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001600** database (with some additional information - the nRec field
1601** of the journal header - being written in between the two
1602** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001603** single disk sector is atomic, then this mode provides
1604** assurance that the journal will not be corrupted to the
1605** point of causing damage to the database during rollback.
1606**
1607** Numeric values associated with these states are OFF==1, NORMAL=2,
1608** and FULL=3.
1609*/
danielk197793758c82005-01-21 08:13:14 +00001610#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00001611void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00001612 pPager->noSync = level==1 || pPager->tempFile;
1613 pPager->fullSync = level==3 && !pPager->tempFile;
drhac530b12006-02-11 01:25:50 +00001614 pPager->full_fsync = full_fsync;
danielk19771d850a72004-05-31 08:26:49 +00001615 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001616}
danielk197793758c82005-01-21 08:13:14 +00001617#endif
drh973b6e32003-02-12 14:09:42 +00001618
1619/*
drhaf6df112005-06-07 02:12:30 +00001620** The following global variable is incremented whenever the library
1621** attempts to open a temporary file. This information is used for
1622** testing and analysis only.
1623*/
drh0f7eb612006-08-08 13:51:43 +00001624#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001625int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001626#endif
drhaf6df112005-06-07 02:12:30 +00001627
1628/*
drh3f56e6e2007-03-15 01:16:47 +00001629** Open a temporary file.
1630**
1631** Write the file descriptor into *fd. Return SQLITE_OK on success or some
drhfa86c412002-02-02 15:01:15 +00001632** other error code if we fail.
1633**
1634** The OS will automatically delete the temporary file when it is
1635** closed.
1636*/
danielk19773b8a05f2007-03-19 17:44:26 +00001637static int sqlite3PagerOpentemp(OsFile **pFd){
drhfa86c412002-02-02 15:01:15 +00001638 int cnt = 8;
1639 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001640 char zFile[SQLITE_TEMPNAME_SIZE];
1641
drh0f7eb612006-08-08 13:51:43 +00001642#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001643 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001644#endif
drhfa86c412002-02-02 15:01:15 +00001645 do{
1646 cnt--;
drh66560ad2006-01-06 14:32:19 +00001647 sqlite3OsTempFileName(zFile);
1648 rc = sqlite3OsOpenExclusive(zFile, pFd, 1);
danielk197713073932004-06-30 11:54:06 +00001649 }while( cnt>0 && rc!=SQLITE_OK && rc!=SQLITE_NOMEM );
drhfa86c412002-02-02 15:01:15 +00001650 return rc;
1651}
1652
1653/*
drhed7c8552001-04-11 14:29:21 +00001654** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001655** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001656** the first call to sqlite3PagerGet() and is only held open until the
1657** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001658**
drh6446c4d2001-12-15 14:22:18 +00001659** If zFilename is NULL then a randomly-named temporary file is created
1660** and used as the file to be cached. The file will be deleted
1661** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001662**
1663** If zFilename is ":memory:" then all information is held in cache.
1664** It is never written to disk. This can be used to implement an
1665** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001666*/
danielk19773b8a05f2007-03-19 17:44:26 +00001667int sqlite3PagerOpen(
drh7e3b0a02001-04-28 16:52:40 +00001668 Pager **ppPager, /* Return the Pager structure here */
1669 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001670 int nExtra, /* Extra bytes append to each in-memory page */
drh7bec5052005-02-06 02:45:41 +00001671 int flags /* flags controlling this file */
drh7e3b0a02001-04-28 16:52:40 +00001672){
danielk1977aef0bf62005-12-30 16:28:01 +00001673 Pager *pPager = 0;
danielk19778def5ea2004-06-16 10:39:52 +00001674 char *zFullPathname = 0;
drh02afc862006-01-20 18:10:57 +00001675 int nameLen; /* Compiler is wrong. This is always initialized before use */
danielk1977393f0682007-03-31 10:00:48 +00001676 OsFile *fd = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001677 int rc = SQLITE_OK;
1678 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001679 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001680 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001681 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001682 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1683 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
drh8cfbf082001-09-19 13:22:39 +00001684 char zTemp[SQLITE_TEMPNAME_SIZE];
drh6f7adc82006-01-11 21:41:20 +00001685#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197754f01982006-01-18 15:25:17 +00001686 /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to
1687 ** malloc() must have already been made by this thread before it gets
1688 ** to this point. This means the ThreadData must have been allocated already
1689 ** so that ThreadData.nAlloc can be set. It would be nice to assert
1690 ** that ThreadData.nAlloc is non-zero, but alas this breaks test cases
1691 ** written to invoke the pager directly.
1692 */
danielk19778212def2006-01-16 15:32:23 +00001693 ThreadData *pTsd = sqlite3ThreadData();
danielk197754f01982006-01-18 15:25:17 +00001694 assert( pTsd );
danielk197752622822006-01-09 09:59:49 +00001695#endif
drhed7c8552001-04-11 14:29:21 +00001696
danielk1977393f0682007-03-31 10:00:48 +00001697 /* We used to test if malloc() had already failed before proceeding.
1698 ** But the way this function is used in SQLite means that can never
1699 ** happen. Furthermore, if the malloc-failed flag is already set,
1700 ** either the call to sqliteStrDup() or sqliteMalloc() below will
1701 ** fail shortly and SQLITE_NOMEM returned anyway.
danielk1977aef0bf62005-12-30 16:28:01 +00001702 */
drhd9b02572001-04-15 00:37:09 +00001703 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001704
1705 /* Open the pager file and set zFullPathname to point at malloc()ed
1706 ** memory containing the complete filename (i.e. including the directory).
1707 */
drh901afd42003-08-26 11:25:58 +00001708 if( zFilename && zFilename[0] ){
drhb7f91642004-10-31 02:22:47 +00001709#ifndef SQLITE_OMIT_MEMORYDB
drhac69b052004-05-12 13:30:07 +00001710 if( strcmp(zFilename,":memory:")==0 ){
1711 memDb = 1;
drhda71ce12004-06-21 18:14:45 +00001712 zFullPathname = sqliteStrDup("");
drhb7f91642004-10-31 02:22:47 +00001713 }else
1714#endif
1715 {
drh66560ad2006-01-06 14:32:19 +00001716 zFullPathname = sqlite3OsFullPathname(zFilename);
danielk19778def5ea2004-06-16 10:39:52 +00001717 if( zFullPathname ){
drh66560ad2006-01-06 14:32:19 +00001718 rc = sqlite3OsOpenReadWrite(zFullPathname, &fd, &readOnly);
danielk19778def5ea2004-06-16 10:39:52 +00001719 }
drhac69b052004-05-12 13:30:07 +00001720 }
drh5e00f6c2001-09-13 13:46:56 +00001721 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00001722 rc = sqlite3PagerOpentemp(&fd);
drh3f56e6e2007-03-15 01:16:47 +00001723 sqlite3OsTempFileName(zTemp);
drh5e00f6c2001-09-13 13:46:56 +00001724 zFilename = zTemp;
drh66560ad2006-01-06 14:32:19 +00001725 zFullPathname = sqlite3OsFullPathname(zFilename);
danielk19778def5ea2004-06-16 10:39:52 +00001726 if( rc==SQLITE_OK ){
1727 tempFile = 1;
1728 }
drh5e00f6c2001-09-13 13:46:56 +00001729 }
danielk1977aef0bf62005-12-30 16:28:01 +00001730
1731 /* Allocate the Pager structure. As part of the same allocation, allocate
1732 ** space for the full paths of the file, directory and journal
1733 ** (Pager.zFilename, Pager.zDirectory and Pager.zJournal).
1734 */
1735 if( zFullPathname ){
1736 nameLen = strlen(zFullPathname);
1737 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*3 + 30 );
danielk19778186df82007-03-06 13:45:59 +00001738 if( pPager && rc==SQLITE_OK ){
1739 pPager->pTmpSpace = (char *)sqliteMallocRaw(SQLITE_DEFAULT_PAGE_SIZE);
1740 }
drh3e7a6092002-12-07 21:45:14 +00001741 }
danielk1977aef0bf62005-12-30 16:28:01 +00001742
danielk19778186df82007-03-06 13:45:59 +00001743
danielk1977aef0bf62005-12-30 16:28:01 +00001744 /* If an error occured in either of the blocks above, free the memory
1745 ** pointed to by zFullPathname, free the Pager structure and close the
1746 ** file. Since the pager is not allocated there is no need to set
1747 ** any Pager.errMask variables.
1748 */
danielk19778186df82007-03-06 13:45:59 +00001749 if( !pPager || !zFullPathname || !pPager->pTmpSpace || rc!=SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001750 sqlite3OsClose(&fd);
drhda71ce12004-06-21 18:14:45 +00001751 sqliteFree(zFullPathname);
danielk1977aef0bf62005-12-30 16:28:01 +00001752 sqliteFree(pPager);
1753 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001754 }
danielk1977aef0bf62005-12-30 16:28:01 +00001755
drh4f0c5872007-03-26 22:05:01 +00001756 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(fd), zFullPathname);
drhb0603412007-02-28 04:47:26 +00001757 IOTRACE(("OPEN %p %s\n", pPager, zFullPathname))
drhed7c8552001-04-11 14:29:21 +00001758 pPager->zFilename = (char*)&pPager[1];
drha76c82e2003-07-27 18:59:42 +00001759 pPager->zDirectory = &pPager->zFilename[nameLen+1];
1760 pPager->zJournal = &pPager->zDirectory[nameLen+1];
drh3e7a6092002-12-07 21:45:14 +00001761 strcpy(pPager->zFilename, zFullPathname);
drha76c82e2003-07-27 18:59:42 +00001762 strcpy(pPager->zDirectory, zFullPathname);
danielk1977aef0bf62005-12-30 16:28:01 +00001763
drha76c82e2003-07-27 18:59:42 +00001764 for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){}
1765 if( i>0 ) pPager->zDirectory[i-1] = 0;
drh3e7a6092002-12-07 21:45:14 +00001766 strcpy(pPager->zJournal, zFullPathname);
1767 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +00001768 strcpy(&pPager->zJournal[nameLen], "-journal");
drh9cbe6352005-11-29 03:13:21 +00001769 pPager->fd = fd;
drh3b59a5c2006-01-15 20:28:28 +00001770 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00001771 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00001772 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001773 /* pPager->stmtOpen = 0; */
1774 /* pPager->stmtInUse = 0; */
1775 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00001776 pPager->dbSize = memDb-1;
drh90f5ecb2004-07-22 01:19:35 +00001777 pPager->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
drh3b59a5c2006-01-15 20:28:28 +00001778 /* pPager->stmtSize = 0; */
1779 /* pPager->stmtJSize = 0; */
1780 /* pPager->nPage = 0; */
1781 /* pPager->nMaxPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00001782 pPager->mxPage = 100;
drh3b59a5c2006-01-15 20:28:28 +00001783 assert( PAGER_UNLOCK==0 );
1784 /* pPager->state = PAGER_UNLOCK; */
1785 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00001786 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00001787 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
1788 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
1789 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
1790 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00001791 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001792 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001793 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00001794 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00001795 pPager->fullSync = (pPager->noSync?0:1);
drh3b59a5c2006-01-15 20:28:28 +00001796 /* pPager->pFirst = 0; */
1797 /* pPager->pFirstSynced = 0; */
1798 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00001799 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk1977b4721172007-03-19 05:54:48 +00001800 assert(fd||memDb);
1801 if( !memDb ){
1802 pPager->sectorSize = sqlite3OsSectorSize(fd);
1803 }
drh3b59a5c2006-01-15 20:28:28 +00001804 /* pPager->pBusyHandler = 0; */
1805 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00001806 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00001807#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk19778212def2006-01-16 15:32:23 +00001808 pPager->pNext = pTsd->pPager;
1809 pTsd->pPager = pPager;
danielk197752622822006-01-09 09:59:49 +00001810#endif
drhed7c8552001-04-11 14:29:21 +00001811 return SQLITE_OK;
1812}
1813
1814/*
drh90f5ecb2004-07-22 01:19:35 +00001815** Set the busy handler function.
1816*/
danielk19773b8a05f2007-03-19 17:44:26 +00001817void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00001818 pPager->pBusyHandler = pBusyHandler;
1819}
1820
1821/*
drh72f82862001-05-24 21:06:34 +00001822** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00001823** when the reference count on each page reaches zero. The destructor can
1824** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00001825**
danielk19773b8a05f2007-03-19 17:44:26 +00001826** The destructor is not called as a result sqlite3PagerClose().
1827** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00001828*/
danielk19773b8a05f2007-03-19 17:44:26 +00001829void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00001830 pPager->xDestructor = xDesc;
1831}
1832
1833/*
drha6abd042004-06-09 17:37:22 +00001834** Set the reinitializer for this pager. If not NULL, the reinitializer
1835** is called when the content of a page in cache is restored to its original
1836** value as a result of a rollback. The callback gives higher-level code
1837** an opportunity to restore the EXTRA section to agree with the restored
1838** page data.
1839*/
danielk19773b8a05f2007-03-19 17:44:26 +00001840void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00001841 pPager->xReiniter = xReinit;
1842}
1843
1844/*
drh1c7880e2005-05-20 20:01:55 +00001845** Set the page size. Return the new size. If the suggest new page
1846** size is inappropriate, then an alternative page size is selected
1847** and returned.
drh90f5ecb2004-07-22 01:19:35 +00001848*/
danielk19773b8a05f2007-03-19 17:44:26 +00001849int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){
drh90f5ecb2004-07-22 01:19:35 +00001850 assert( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE );
danielk1977c7c7e622007-03-26 15:46:00 +00001851 if( !pPager->memDb && pPager->nRef==0 ){
1852 pager_reset(pPager);
drh1c7880e2005-05-20 20:01:55 +00001853 pPager->pageSize = pageSize;
drhcf643722007-03-27 13:36:37 +00001854 pPager->pTmpSpace = sqlite3ReallocOrFree(pPager->pTmpSpace, pageSize);
drh1c7880e2005-05-20 20:01:55 +00001855 }
1856 return pPager->pageSize;
drh90f5ecb2004-07-22 01:19:35 +00001857}
1858
1859/*
drhc9ac5ca2005-11-04 22:03:30 +00001860** The following set of routines are used to disable the simulated
1861** I/O error mechanism. These routines are used to avoid simulated
1862** errors in places where we do not care about errors.
1863**
1864** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
1865** and generate no code.
1866*/
1867#ifdef SQLITE_TEST
1868extern int sqlite3_io_error_pending;
1869extern int sqlite3_io_error_hit;
1870static int saved_cnt;
1871void clear_simulated_io_error(){
1872 sqlite3_io_error_hit = 0;
1873}
1874void disable_simulated_io_errors(void){
1875 saved_cnt = sqlite3_io_error_pending;
1876 sqlite3_io_error_pending = -1;
1877}
1878void enable_simulated_io_errors(void){
1879 sqlite3_io_error_pending = saved_cnt;
1880}
1881#else
drh152410f2005-11-05 15:11:22 +00001882# define clear_simulated_io_error()
1883# define disable_simulated_io_errors()
1884# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00001885#endif
1886
1887/*
drh90f5ecb2004-07-22 01:19:35 +00001888** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00001889** that pDest points to.
1890**
1891** No error checking is done. The rational for this is that this function
1892** may be called even if the file does not exist or contain a header. In
1893** these cases sqlite3OsRead() will return an error, to which the correct
1894** response is to zero the memory at pDest and continue. A real IO error
1895** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00001896*/
danielk19773b8a05f2007-03-19 17:44:26 +00001897int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00001898 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00001899 memset(pDest, 0, N);
drhb7f91642004-10-31 02:22:47 +00001900 if( MEMDB==0 ){
danielk1977f2fa8312006-01-24 13:09:33 +00001901 disable_simulated_io_errors();
drh054889e2005-11-30 03:20:31 +00001902 sqlite3OsSeek(pPager->fd, 0);
danielk1977f2fa8312006-01-24 13:09:33 +00001903 enable_simulated_io_errors();
drhb0603412007-02-28 04:47:26 +00001904 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
drh551b7732006-11-06 21:20:25 +00001905 rc = sqlite3OsRead(pPager->fd, pDest, N);
1906 if( rc==SQLITE_IOERR_SHORT_READ ){
1907 rc = SQLITE_OK;
1908 }
drh90f5ecb2004-07-22 01:19:35 +00001909 }
drh551b7732006-11-06 21:20:25 +00001910 return rc;
drh90f5ecb2004-07-22 01:19:35 +00001911}
1912
1913/*
drh5e00f6c2001-09-13 13:46:56 +00001914** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00001915** pPager.
1916**
1917** If the PENDING_BYTE lies on the page directly after the end of the
1918** file, then consider this page part of the file too. For example, if
1919** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
1920** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00001921*/
danielk19773b8a05f2007-03-19 17:44:26 +00001922int sqlite3PagerPagecount(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001923 i64 n;
drhe49f9822006-09-15 12:29:16 +00001924 int rc;
drhd9b02572001-04-15 00:37:09 +00001925 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00001926 if( pPager->errCode ){
1927 return 0;
1928 }
drhed7c8552001-04-11 14:29:21 +00001929 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00001930 n = pPager->dbSize;
1931 } else {
drhe49f9822006-09-15 12:29:16 +00001932 if( (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
1933 pager_error(pPager, rc);
danielk197715f411d2005-09-16 10:18:45 +00001934 return 0;
1935 }
1936 if( n>0 && n<pPager->pageSize ){
1937 n = 1;
1938 }else{
1939 n /= pPager->pageSize;
1940 }
1941 if( pPager->state!=PAGER_UNLOCK ){
1942 pPager->dbSize = n;
1943 }
drhed7c8552001-04-11 14:29:21 +00001944 }
danielk197715f411d2005-09-16 10:18:45 +00001945 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00001946 n++;
1947 }
drhed7c8552001-04-11 14:29:21 +00001948 return n;
1949}
1950
drhf07e7d52006-04-07 13:54:46 +00001951
1952#ifndef SQLITE_OMIT_MEMORYDB
1953/*
1954** Clear a PgHistory block
1955*/
1956static void clearHistory(PgHistory *pHist){
1957 sqliteFree(pHist->pOrig);
1958 sqliteFree(pHist->pStmt);
1959 pHist->pOrig = 0;
1960 pHist->pStmt = 0;
1961}
1962#else
1963#define clearHistory(x)
1964#endif
1965
drhed7c8552001-04-11 14:29:21 +00001966/*
drhf7c57532003-04-25 13:22:51 +00001967** Forward declaration
1968*/
danielk197776572402004-06-25 02:38:54 +00001969static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00001970
1971/*
danielk1977f5fdda82004-11-03 08:44:05 +00001972** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate
1973** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00001974** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00001975** pNextFree/pPrevFree list that is not a part of any hash-chain.
1976*/
1977static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
1978 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00001979 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00001980 return;
1981 }
1982 if( pPg->pNextHash ){
1983 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
1984 }
1985 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00001986 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00001987 pPg->pPrevHash->pNextHash = pPg->pNextHash;
1988 }else{
drh8ca0c722006-05-07 17:49:38 +00001989 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00001990 pPager->aHash[h] = pPg->pNextHash;
1991 }
drh5229ae42006-03-23 23:29:04 +00001992 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00001993 clearHistory(PGHDR_TO_HIST(pPg, pPager));
1994 }
danielk1977f5fdda82004-11-03 08:44:05 +00001995 pPg->pgno = 0;
1996 pPg->pNextHash = pPg->pPrevHash = 0;
1997}
1998
1999/*
drhac69b052004-05-12 13:30:07 +00002000** Unlink a page from the free list (the list of all pages where nRef==0)
2001** and from its hash collision chain.
2002*/
2003static void unlinkPage(PgHdr *pPg){
2004 Pager *pPager = pPg->pPager;
2005
2006 /* Keep the pFirstSynced pointer pointing at the first synchronized page */
2007 if( pPg==pPager->pFirstSynced ){
2008 PgHdr *p = pPg->pNextFree;
2009 while( p && p->needSync ){ p = p->pNextFree; }
2010 pPager->pFirstSynced = p;
2011 }
2012
2013 /* Unlink from the freelist */
2014 if( pPg->pPrevFree ){
2015 pPg->pPrevFree->pNextFree = pPg->pNextFree;
2016 }else{
2017 assert( pPager->pFirst==pPg );
2018 pPager->pFirst = pPg->pNextFree;
2019 }
2020 if( pPg->pNextFree ){
2021 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
2022 }else{
2023 assert( pPager->pLast==pPg );
2024 pPager->pLast = pPg->pPrevFree;
2025 }
2026 pPg->pNextFree = pPg->pPrevFree = 0;
2027
2028 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002029 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002030}
2031
danielk1977b84f96f2005-01-20 11:32:23 +00002032#ifndef SQLITE_OMIT_MEMORYDB
drhac69b052004-05-12 13:30:07 +00002033/*
2034** This routine is used to truncate an in-memory database. Delete
danielk1977369f27e2004-06-15 11:40:04 +00002035** all pages whose pgno is larger than pPager->dbSize and is unreferenced.
drhac69b052004-05-12 13:30:07 +00002036** Referenced pages larger than pPager->dbSize are zeroed.
2037*/
2038static void memoryTruncate(Pager *pPager){
2039 PgHdr *pPg;
2040 PgHdr **ppPg;
2041 int dbSize = pPager->dbSize;
2042
2043 ppPg = &pPager->pAll;
2044 while( (pPg = *ppPg)!=0 ){
2045 if( pPg->pgno<=dbSize ){
2046 ppPg = &pPg->pNextAll;
2047 }else if( pPg->nRef>0 ){
2048 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2049 ppPg = &pPg->pNextAll;
2050 }else{
2051 *ppPg = pPg->pNextAll;
2052 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002053 makeClean(pPg);
drhac69b052004-05-12 13:30:07 +00002054 sqliteFree(pPg);
2055 pPager->nPage--;
2056 }
2057 }
2058}
danielk1977b84f96f2005-01-20 11:32:23 +00002059#else
2060#define memoryTruncate(p)
2061#endif
drhac69b052004-05-12 13:30:07 +00002062
drhf7c57532003-04-25 13:22:51 +00002063/*
danielk197717221812005-02-15 03:38:05 +00002064** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002065** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002066** false or until the lock succeeds.
2067**
2068** Return SQLITE_OK on success and an error code if we cannot obtain
2069** the lock.
2070*/
2071static int pager_wait_on_lock(Pager *pPager, int locktype){
2072 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002073
2074 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002075 assert( PAGER_SHARED==SHARED_LOCK );
2076 assert( PAGER_RESERVED==RESERVED_LOCK );
2077 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002078
2079 /* If the file is currently unlocked then the size must be unknown */
2080 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2081
danielk197717221812005-02-15 03:38:05 +00002082 if( pPager->state>=locktype ){
2083 rc = SQLITE_OK;
2084 }else{
danielk197717221812005-02-15 03:38:05 +00002085 do {
drh054889e2005-11-30 03:20:31 +00002086 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002087 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002088 if( rc==SQLITE_OK ){
2089 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002090 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002091 }
2092 }
2093 return rc;
2094}
2095
2096/*
drhf7c57532003-04-25 13:22:51 +00002097** Truncate the file to the number of pages specified.
2098*/
danielk19773b8a05f2007-03-19 17:44:26 +00002099int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002100 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002101 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002102 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002103 if( pPager->errCode ){
2104 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002105 return rc;
2106 }
drh7d02cb72003-06-04 16:24:39 +00002107 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002108 return SQLITE_OK;
2109 }
drhb7f91642004-10-31 02:22:47 +00002110 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002111 pPager->dbSize = nPage;
2112 memoryTruncate(pPager);
2113 return SQLITE_OK;
2114 }
danielk197776572402004-06-25 02:38:54 +00002115 rc = syncJournal(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002116 if( rc!=SQLITE_OK ){
2117 return rc;
2118 }
danielk197717221812005-02-15 03:38:05 +00002119
2120 /* Get an exclusive lock on the database before truncating. */
2121 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2122 if( rc!=SQLITE_OK ){
2123 return rc;
2124 }
2125
drhcb4c40b2004-08-18 19:09:43 +00002126 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002127 if( rc==SQLITE_OK ){
2128 pPager->dbSize = nPage;
2129 }
2130 return rc;
2131}
2132
2133/*
drhed7c8552001-04-11 14:29:21 +00002134** Shutdown the page cache. Free all memory and close all files.
2135**
2136** If a transaction was in progress when this routine is called, that
2137** transaction is rolled back. All outstanding pages are invalidated
2138** and their memory is freed. Any attempt to use a page associated
2139** with this page cache after this function returns will likely
2140** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002141**
2142** This function always succeeds. If a transaction is active an attempt
2143** is made to roll it back. If an error occurs during the rollback
2144** a hot journal may be left in the filesystem but no error is returned
2145** to the caller.
drhed7c8552001-04-11 14:29:21 +00002146*/
danielk19773b8a05f2007-03-19 17:44:26 +00002147int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002148#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197754f01982006-01-18 15:25:17 +00002149 /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to
2150 ** malloc() must have already been made by this thread before it gets
2151 ** to this point. This means the ThreadData must have been allocated already
2152 ** so that ThreadData.nAlloc can be set.
2153 */
danielk19778212def2006-01-16 15:32:23 +00002154 ThreadData *pTsd = sqlite3ThreadData();
danielk197776e8d1a2006-01-18 18:22:43 +00002155 assert( pPager );
danielk197754f01982006-01-18 15:25:17 +00002156 assert( pTsd && pTsd->nAlloc );
danielk197713f72992005-12-18 08:51:22 +00002157#endif
2158
drhbafda092007-01-03 23:36:22 +00002159 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002160 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002161 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002162 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002163 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002164 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002165 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002166 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002167 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002168 if( pPager->journalOpen ){
drh054889e2005-11-30 03:20:31 +00002169 sqlite3OsClose(&pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002170 }
drh88b01a12005-03-28 18:04:27 +00002171 sqliteFree(pPager->aInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002172 if( pPager->stmtOpen ){
drh054889e2005-11-30 03:20:31 +00002173 sqlite3OsClose(&pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002174 }
drh054889e2005-11-30 03:20:31 +00002175 sqlite3OsClose(&pPager->fd);
drh0f892532002-05-30 12:27:03 +00002176 /* Temp files are automatically deleted by the OS
2177 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002178 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002179 ** }
2180 */
danielk1977aca790a2005-01-13 11:07:52 +00002181
drh6f7adc82006-01-11 21:41:20 +00002182#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977aef0bf62005-12-30 16:28:01 +00002183 /* Remove the pager from the linked list of pagers starting at
danielk197752622822006-01-09 09:59:49 +00002184 ** ThreadData.pPager if memory-management is enabled.
danielk1977aef0bf62005-12-30 16:28:01 +00002185 */
danielk19778212def2006-01-16 15:32:23 +00002186 if( pPager==pTsd->pPager ){
2187 pTsd->pPager = pPager->pNext;
2188 }else{
2189 Pager *pTmp;
drh74161702006-02-24 02:53:49 +00002190 for(pTmp = pTsd->pPager; pTmp->pNext!=pPager; pTmp=pTmp->pNext){}
danielk19778212def2006-01-16 15:32:23 +00002191 pTmp->pNext = pPager->pNext;
danielk197713f72992005-12-18 08:51:22 +00002192 }
2193#endif
drh8ca0c722006-05-07 17:49:38 +00002194 sqliteFree(pPager->aHash);
danielk19778186df82007-03-06 13:45:59 +00002195 sqliteFree(pPager->pTmpSpace);
drhed7c8552001-04-11 14:29:21 +00002196 sqliteFree(pPager);
2197 return SQLITE_OK;
2198}
2199
2200/*
drh5e00f6c2001-09-13 13:46:56 +00002201** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002202*/
danielk19773b8a05f2007-03-19 17:44:26 +00002203Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002204 return p->pgno;
2205}
2206
2207/*
drhc8629a12004-05-08 20:07:40 +00002208** The page_ref() function increments the reference count for a page.
2209** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002210** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002211**
2212** For non-test systems, page_ref() is a macro that calls _page_ref()
2213** online of the reference count is zero. For test systems, page_ref()
2214** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002215*/
drh836faa42003-01-11 13:30:57 +00002216static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002217 if( pPg->nRef==0 ){
2218 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +00002219 if( pPg==pPg->pPager->pFirstSynced ){
2220 PgHdr *p = pPg->pNextFree;
2221 while( p && p->needSync ){ p = p->pNextFree; }
2222 pPg->pPager->pFirstSynced = p;
2223 }
drh7e3b0a02001-04-28 16:52:40 +00002224 if( pPg->pPrevFree ){
2225 pPg->pPrevFree->pNextFree = pPg->pNextFree;
2226 }else{
2227 pPg->pPager->pFirst = pPg->pNextFree;
2228 }
2229 if( pPg->pNextFree ){
2230 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
2231 }else{
2232 pPg->pPager->pLast = pPg->pPrevFree;
2233 }
2234 pPg->pPager->nRef++;
2235 }
2236 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00002237 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00002238}
danielk1977aca790a2005-01-13 11:07:52 +00002239#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002240 static void page_ref(PgHdr *pPg){
2241 if( pPg->nRef==0 ){
2242 _page_ref(pPg);
2243 }else{
2244 pPg->nRef++;
2245 REFINFO(pPg);
2246 }
2247 }
2248#else
2249# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2250#endif
drhdf0b3b02001-06-23 11:36:20 +00002251
2252/*
2253** Increment the reference count for a page. The input pointer is
2254** a reference to the page data.
2255*/
danielk19773b8a05f2007-03-19 17:44:26 +00002256int sqlite3PagerRef(DbPage *pPg){
drhdf0b3b02001-06-23 11:36:20 +00002257 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +00002258 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002259}
2260
2261/*
drh34e79ce2004-02-08 06:05:46 +00002262** Sync the journal. In other words, make sure all the pages that have
2263** been written to the journal have actually reached the surface of the
2264** disk. It is not safe to modify the original database file until after
2265** the journal has been synced. If the original database is modified before
2266** the journal is synced and a power failure occurs, the unsynced journal
2267** data would be lost and we would be unable to completely rollback the
2268** database changes. Database corruption would occur.
2269**
2270** This routine also updates the nRec field in the header of the journal.
2271** (See comments on the pager_playback() routine for additional information.)
2272** If the sync mode is FULL, two syncs will occur. First the whole journal
2273** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002274**
drh34e79ce2004-02-08 06:05:46 +00002275** For temporary databases, we do not care if we are able to rollback
2276** after a power failure, so sync occurs.
drhfa86c412002-02-02 15:01:15 +00002277**
drh34e79ce2004-02-08 06:05:46 +00002278** This routine clears the needSync field of every page current held in
2279** memory.
drh50e5dad2001-09-15 00:57:28 +00002280*/
danielk197776572402004-06-25 02:38:54 +00002281static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002282 PgHdr *pPg;
2283 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002284
2285 /* Sync the journal before modifying the main database
2286 ** (assuming there is a journal and it needs to be synced.)
2287 */
danielk197776572402004-06-25 02:38:54 +00002288 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002289 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00002290 assert( pPager->journalOpen );
drh946966f2004-02-25 02:20:41 +00002291 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2292 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00002293#ifndef NDEBUG
2294 {
drh34e79ce2004-02-08 06:05:46 +00002295 /* Make sure the pPager->nRec counter we are keeping agrees
2296 ** with the nRec computed from the size of the journal file.
2297 */
drheb206252004-10-01 02:00:31 +00002298 i64 jSz;
drh054889e2005-11-30 03:20:31 +00002299 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00002300 if( rc!=0 ) return rc;
danielk197776572402004-06-25 02:38:54 +00002301 assert( pPager->journalOff==jSz );
drh968af522003-02-11 14:55:40 +00002302 }
2303#endif
drhae2b40c2004-06-09 19:03:54 +00002304 {
danielk197776572402004-06-25 02:38:54 +00002305 /* Write the nRec value into the journal file header. If in
2306 ** full-synchronous mode, sync the journal first. This ensures that
2307 ** all data has really hit the disk before nRec is updated to mark
2308 ** it as a candidate for rollback.
2309 */
drhd8d66e82003-02-12 02:10:15 +00002310 if( pPager->fullSync ){
drh4f0c5872007-03-26 22:05:01 +00002311 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002312 IOTRACE(("JSYNC %p\n", pPager))
drh054889e2005-11-30 03:20:31 +00002313 rc = sqlite3OsSync(pPager->jfd, 0);
drhd8d66e82003-02-12 02:10:15 +00002314 if( rc!=0 ) return rc;
2315 }
drh054889e2005-11-30 03:20:31 +00002316 rc = sqlite3OsSeek(pPager->jfd,
drhb4746b92005-09-09 01:32:06 +00002317 pPager->journalHdr + sizeof(aJournalMagic));
2318 if( rc ) return rc;
drhb0603412007-02-28 04:47:26 +00002319 IOTRACE(("JHDR %p %lld %d\n", pPager,
2320 pPager->journalHdr + sizeof(aJournalMagic), 4))
drh9cbe6352005-11-29 03:13:21 +00002321 rc = write32bits(pPager->jfd, pPager->nRec);
drh99ee3602003-02-16 19:13:36 +00002322 if( rc ) return rc;
danielk197713adf8a2004-06-03 16:08:41 +00002323
drh054889e2005-11-30 03:20:31 +00002324 rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff);
drhb4746b92005-09-09 01:32:06 +00002325 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002326 }
drh4f0c5872007-03-26 22:05:01 +00002327 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002328 IOTRACE(("JSYNC %d\n", pPager))
drhac530b12006-02-11 01:25:50 +00002329 rc = sqlite3OsSync(pPager->jfd, pPager->full_fsync);
drhfa86c412002-02-02 15:01:15 +00002330 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00002331 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002332 }
drh50e5dad2001-09-15 00:57:28 +00002333 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002334
2335 /* Erase the needSync flag from every page.
2336 */
2337 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2338 pPg->needSync = 0;
2339 }
2340 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00002341 }
drh03eb96a2002-11-10 23:32:56 +00002342
drh341eae82003-01-21 02:39:36 +00002343#ifndef NDEBUG
2344 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2345 ** flag must also be clear for all pages. Verify that this
2346 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002347 */
drh341eae82003-01-21 02:39:36 +00002348 else{
2349 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2350 assert( pPg->needSync==0 );
2351 }
2352 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00002353 }
drh341eae82003-01-21 02:39:36 +00002354#endif
drhdb48ee02003-01-16 13:42:43 +00002355
drh81a20f22001-10-12 17:30:04 +00002356 return rc;
drh50e5dad2001-09-15 00:57:28 +00002357}
2358
2359/*
drhdc3e10b2006-06-15 14:31:06 +00002360** Merge two lists of pages connected by pDirty and in pgno order.
2361** Do not both fixing the pPrevDirty pointers.
2362*/
2363static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2364 PgHdr result, *pTail;
2365 pTail = &result;
2366 while( pA && pB ){
2367 if( pA->pgno<pB->pgno ){
2368 pTail->pDirty = pA;
2369 pTail = pA;
2370 pA = pA->pDirty;
2371 }else{
2372 pTail->pDirty = pB;
2373 pTail = pB;
2374 pB = pB->pDirty;
2375 }
2376 }
2377 if( pA ){
2378 pTail->pDirty = pA;
2379 }else if( pB ){
2380 pTail->pDirty = pB;
2381 }else{
2382 pTail->pDirty = 0;
2383 }
2384 return result.pDirty;
2385}
2386
2387/*
2388** Sort the list of pages in accending order by pgno. Pages are
2389** connected by pDirty pointers. The pPrevDirty pointers are
2390** corrupted by this sort.
2391*/
danielk197724168722007-04-02 05:07:47 +00002392#define N_SORT_BUCKET_ALLOC 25
2393#define N_SORT_BUCKET 25
2394#ifdef SQLITE_TEST
2395 int sqlite3_pager_n_sort_bucket = 0;
2396 #undef N_SORT_BUCKET
2397 #define N_SORT_BUCKET \
2398 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2399#endif
drhdc3e10b2006-06-15 14:31:06 +00002400static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002401 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002402 int i;
2403 memset(a, 0, sizeof(a));
2404 while( pIn ){
2405 p = pIn;
2406 pIn = p->pDirty;
2407 p->pDirty = 0;
2408 for(i=0; i<N_SORT_BUCKET-1; i++){
2409 if( a[i]==0 ){
2410 a[i] = p;
2411 break;
2412 }else{
2413 p = merge_pagelist(a[i], p);
2414 a[i] = 0;
2415 }
2416 }
2417 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002418 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2419 ** elements in the input list. This is possible, but impractical.
2420 ** Testing this line is the point of global variable
2421 ** sqlite3_pager_n_sort_bucket.
2422 */
drhdc3e10b2006-06-15 14:31:06 +00002423 a[i] = merge_pagelist(a[i], p);
2424 }
2425 }
2426 p = a[0];
2427 for(i=1; i<N_SORT_BUCKET; i++){
2428 p = merge_pagelist(p, a[i]);
2429 }
2430 return p;
2431}
2432
2433/*
drh2554f8b2003-01-22 01:26:44 +00002434** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2435** every one of those pages out to the database file and mark them all
2436** as clean.
2437*/
2438static int pager_write_pagelist(PgHdr *pList){
2439 Pager *pPager;
2440 int rc;
2441
2442 if( pList==0 ) return SQLITE_OK;
2443 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002444
2445 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2446 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002447 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002448 **
drha6abd042004-06-09 17:37:22 +00002449 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2450 ** through an intermediate state PENDING. A PENDING lock prevents new
2451 ** readers from attaching to the database but is unsufficient for us to
2452 ** write. The idea of a PENDING lock is to prevent new readers from
2453 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002454 **
drha6abd042004-06-09 17:37:22 +00002455 ** While the pager is in the RESERVED state, the original database file
2456 ** is unchanged and we can rollback without having to playback the
2457 ** journal into the original database file. Once we transition to
2458 ** EXCLUSIVE, it means the database file has been changed and any rollback
2459 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002460 */
drh684917c2004-10-05 02:41:42 +00002461 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002462 if( rc!=SQLITE_OK ){
2463 return rc;
2464 }
2465
drhdc3e10b2006-06-15 14:31:06 +00002466 pList = sort_pagelist(pList);
drh2554f8b2003-01-22 01:26:44 +00002467 while( pList ){
2468 assert( pList->dirty );
drh054889e2005-11-30 03:20:31 +00002469 rc = sqlite3OsSeek(pPager->fd, (pList->pgno-1)*(i64)pPager->pageSize);
drhb4746b92005-09-09 01:32:06 +00002470 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00002471 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002472 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002473 ** make the file smaller (presumably by auto-vacuum code). Do not write
2474 ** any such pages to the file.
2475 */
2476 if( pList->pgno<=pPager->dbSize ){
drhc001c582006-03-06 18:23:16 +00002477 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh4f0c5872007-03-26 22:05:01 +00002478 PAGERTRACE3("STORE %d page %d\n", PAGERID(pPager), pList->pgno);
drhb0603412007-02-28 04:47:26 +00002479 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno))
drhc001c582006-03-06 18:23:16 +00002480 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize);
drhfcd35c72005-05-21 02:48:08 +00002481 TEST_INCR(pPager->nWrite);
danielk1977687566d2004-11-02 12:56:41 +00002482 }
2483#ifndef NDEBUG
2484 else{
drh4f0c5872007-03-26 22:05:01 +00002485 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002486 }
2487#endif
drh2554f8b2003-01-22 01:26:44 +00002488 if( rc ) return rc;
2489 pList->dirty = 0;
danielk19773c407372005-02-15 02:54:14 +00002490#ifdef SQLITE_CHECK_PAGES
2491 pList->pageHash = pager_pagehash(pList);
2492#endif
drh2554f8b2003-01-22 01:26:44 +00002493 pList = pList->pDirty;
2494 }
2495 return SQLITE_OK;
2496}
2497
2498/*
2499** Collect every dirty page into a dirty list and
2500** return a pointer to the head of that list. All pages are
2501** collected even if they are still in use.
2502*/
2503static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh605ad8f2006-05-03 23:34:05 +00002504 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00002505}
2506
2507/*
drh165ffe92005-03-15 17:09:30 +00002508** Return TRUE if there is a hot journal on the given pager.
2509** A hot journal is one that needs to be played back.
2510**
2511** If the current size of the database file is 0 but a journal file
2512** exists, that is probably an old journal left over from a prior
2513** database with the same name. Just delete the journal.
2514*/
2515static int hasHotJournal(Pager *pPager){
2516 if( !pPager->useJournal ) return 0;
drh66560ad2006-01-06 14:32:19 +00002517 if( !sqlite3OsFileExists(pPager->zJournal) ) return 0;
drh054889e2005-11-30 03:20:31 +00002518 if( sqlite3OsCheckReservedLock(pPager->fd) ) return 0;
danielk19773b8a05f2007-03-19 17:44:26 +00002519 if( sqlite3PagerPagecount(pPager)==0 ){
drh66560ad2006-01-06 14:32:19 +00002520 sqlite3OsDelete(pPager->zJournal);
drh165ffe92005-03-15 17:09:30 +00002521 return 0;
2522 }else{
2523 return 1;
2524 }
2525}
2526
danielk19775591df52005-12-20 09:19:37 +00002527/*
danielk1977aef0bf62005-12-30 16:28:01 +00002528** Try to find a page in the cache that can be recycled.
2529**
2530** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00002531** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00002532*/
danielk197713f72992005-12-18 08:51:22 +00002533static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
2534 PgHdr *pPg;
2535 *ppPg = 0;
2536
2537 /* Find a page to recycle. Try to locate a page that does not
2538 ** require us to do an fsync() on the journal.
2539 */
2540 pPg = pPager->pFirstSynced;
2541
2542 /* If we could not find a page that does not require an fsync()
2543 ** on the journal file then fsync the journal file. This is a
2544 ** very slow operation, so we work hard to avoid it. But sometimes
2545 ** it can't be helped.
2546 */
danielk19775591df52005-12-20 09:19:37 +00002547 if( pPg==0 && pPager->pFirst && syncOk && !MEMDB){
danielk197713f72992005-12-18 08:51:22 +00002548 int rc = syncJournal(pPager);
2549 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002550 return rc;
danielk197713f72992005-12-18 08:51:22 +00002551 }
2552 if( pPager->fullSync ){
2553 /* If in full-sync mode, write a new journal header into the
2554 ** journal file. This is done to avoid ever modifying a journal
2555 ** header that is involved in the rollback of pages that have
2556 ** already been written to the database (in case the header is
2557 ** trashed when the nRec field is updated).
2558 */
2559 pPager->nRec = 0;
2560 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00002561 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00002562 rc = writeJournalHdr(pPager);
2563 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002564 return rc;
danielk197713f72992005-12-18 08:51:22 +00002565 }
2566 }
2567 pPg = pPager->pFirst;
2568 }
2569 if( pPg==0 ){
2570 return SQLITE_OK;
2571 }
2572
2573 assert( pPg->nRef==0 );
2574
2575 /* Write the page to the database file if it is dirty.
2576 */
2577 if( pPg->dirty ){
2578 int rc;
2579 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00002580 makeClean(pPg);
2581 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00002582 pPg->pDirty = 0;
2583 rc = pager_write_pagelist( pPg );
2584 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00002585 return rc;
danielk197713f72992005-12-18 08:51:22 +00002586 }
2587 }
2588 assert( pPg->dirty==0 );
2589
2590 /* If the page we are recycling is marked as alwaysRollback, then
2591 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00002592 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00002593 ** It is necessary to do this because the page marked alwaysRollback
2594 ** might be reloaded at a later time but at that point we won't remember
2595 ** that is was marked alwaysRollback. This means that all pages must
2596 ** be marked as alwaysRollback from here on out.
2597 */
2598 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00002599 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00002600 pPager->alwaysRollback = 1;
2601 }
2602
2603 /* Unlink the old page from the free list and the hash table
2604 */
2605 unlinkPage(pPg);
danielk197724168722007-04-02 05:07:47 +00002606 if( pPg && pPg->pgno!=0 ){
2607 TEST_INCR(pPager->nOvfl);
2608 }
danielk197713f72992005-12-18 08:51:22 +00002609
2610 *ppPg = pPg;
2611 return SQLITE_OK;
2612}
2613
2614/*
2615** This function is called to free superfluous dynamically allocated memory
2616** held by the pager system. Memory in use by any SQLite pager allocated
2617** by the current thread may be sqliteFree()ed.
2618**
2619** nReq is the number of bytes of memory required. Once this much has
danielk19770190d1d2005-12-19 14:18:11 +00002620** been released, the function returns. A negative value for nReq means
2621** free as much memory as possible. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00002622** of bytes of memory released.
2623*/
drh6f7adc82006-01-11 21:41:20 +00002624#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk19773b8a05f2007-03-19 17:44:26 +00002625int sqlite3PagerReleaseMemory(int nReq){
drh6f7adc82006-01-11 21:41:20 +00002626 const ThreadData *pTsdro = sqlite3ThreadDataReadOnly();
danielk19770190d1d2005-12-19 14:18:11 +00002627 Pager *p;
danielk197713f72992005-12-18 08:51:22 +00002628 int nReleased = 0;
2629 int i;
2630
drh29c636b2006-01-09 23:40:25 +00002631 /* If the the global mutex is held, this subroutine becomes a
2632 ** o-op; zero bytes of memory are freed. This is because
2633 ** some of the code invoked by this function may also
2634 ** try to obtain the mutex, resulting in a deadlock.
danielk1977441b09a2006-01-05 13:48:29 +00002635 */
drh757b04e2006-01-18 17:25:45 +00002636 if( sqlite3OsInMutex(0) ){
danielk1977441b09a2006-01-05 13:48:29 +00002637 return 0;
2638 }
2639
danielk197713f72992005-12-18 08:51:22 +00002640 /* Outermost loop runs for at most two iterations. First iteration we
2641 ** try to find memory that can be released without calling fsync(). Second
2642 ** iteration (which only runs if the first failed to free nReq bytes of
2643 ** memory) is permitted to call fsync(). This is of course much more
2644 ** expensive.
2645 */
drh29c636b2006-01-09 23:40:25 +00002646 for(i=0; i<=1; i++){
danielk197713f72992005-12-18 08:51:22 +00002647
2648 /* Loop through all the SQLite pagers opened by the current thread. */
drh6f7adc82006-01-11 21:41:20 +00002649 for(p=pTsdro->pPager; p && (nReq<0 || nReleased<nReq); p=p->pNext){
danielk197713f72992005-12-18 08:51:22 +00002650 PgHdr *pPg;
2651 int rc;
2652
2653 /* For each pager, try to free as many pages as possible (without
2654 ** calling fsync() if this is the first iteration of the outermost
2655 ** loop).
2656 */
danielk19770190d1d2005-12-19 14:18:11 +00002657 while( SQLITE_OK==(rc = pager_recycle(p, i, &pPg)) && pPg) {
danielk1977aef0bf62005-12-30 16:28:01 +00002658 /* We've found a page to free. At this point the page has been
danielk197713f72992005-12-18 08:51:22 +00002659 ** removed from the page hash-table, free-list and synced-list
danielk1977aef0bf62005-12-30 16:28:01 +00002660 ** (pFirstSynced). It is still in the all pages (pAll) list.
danielk197713f72992005-12-18 08:51:22 +00002661 ** Remove it from this list before freeing.
2662 **
2663 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
2664 ** probably is though.
2665 */
2666 PgHdr *pTmp;
danielk19770190d1d2005-12-19 14:18:11 +00002667 assert( pPg );
danielk1977aef0bf62005-12-30 16:28:01 +00002668 page_remove_from_stmt_list(pPg);
danielk19770190d1d2005-12-19 14:18:11 +00002669 if( pPg==p->pAll ){
2670 p->pAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00002671 }else{
drh74161702006-02-24 02:53:49 +00002672 for( pTmp=p->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
danielk197713f72992005-12-18 08:51:22 +00002673 pTmp->pNextAll = pPg->pNextAll;
2674 }
2675 nReleased += sqliteAllocSize(pPg);
2676 sqliteFree(pPg);
2677 }
2678
2679 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00002680 /* An error occured whilst writing to the database file or
2681 ** journal in pager_recycle(). The error is not returned to the
danielk1977efaaf572006-01-16 11:29:19 +00002682 ** caller of this function. Instead, set the Pager.errCode variable.
danielk1977aef0bf62005-12-30 16:28:01 +00002683 ** The error will be returned to the user (or users, in the case
2684 ** of a shared pager cache) of the pager for which the error occured.
danielk197713f72992005-12-18 08:51:22 +00002685 */
drh4ac285a2006-09-15 07:28:50 +00002686 assert( (rc&0xff)==SQLITE_IOERR || rc==SQLITE_FULL );
danielk1977aef0bf62005-12-30 16:28:01 +00002687 assert( p->state>=PAGER_RESERVED );
2688 pager_error(p, rc);
danielk197713f72992005-12-18 08:51:22 +00002689 }
2690 }
2691 }
danielk1977aef0bf62005-12-30 16:28:01 +00002692
danielk197713f72992005-12-18 08:51:22 +00002693 return nReleased;
2694}
drh6f7adc82006-01-11 21:41:20 +00002695#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00002696
drh165ffe92005-03-15 17:09:30 +00002697/*
danielk1977e277be02007-03-23 18:12:06 +00002698** This function is called to obtain the shared lock required before
2699** data may be read from the pager cache. If the shared lock has already
2700** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002701**
2702** Immediately after obtaining the shared lock (if required), this function
2703** checks for a hot-journal file. If one is found, an emergency rollback
2704** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002705*/
2706static int pagerSharedLock(Pager *pPager){
2707 int rc = SQLITE_OK;
2708
2709 if( pPager->state==PAGER_UNLOCK ){
2710 if( !MEMDB ){
2711 assert( pPager->nRef==0 );
2712 if( !pPager->noReadlock ){
2713 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2714 if( rc!=SQLITE_OK ){
2715 return pager_error(pPager, rc);
2716 }
2717 assert( pPager->state>=SHARED_LOCK );
2718 }
2719
2720 /* If a journal file exists, and there is no RESERVED lock on the
2721 ** database file, then it either needs to be played back or deleted.
2722 */
2723 if( hasHotJournal(pPager) ){
2724 /* Get an EXCLUSIVE lock on the database file. At this point it is
2725 ** important that a RESERVED lock is not obtained on the way to the
2726 ** EXCLUSIVE lock. If it were, another process might open the
2727 ** database file, detect the RESERVED lock, and conclude that the
2728 ** database is safe to read while this process is still rolling it
2729 ** back.
2730 **
2731 ** Because the intermediate RESERVED lock is not requested, the
2732 ** second process will get to this point in the code and fail to
2733 ** obtain it's own EXCLUSIVE lock on the database file.
2734 */
2735 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
2736 if( rc!=SQLITE_OK ){
2737 pager_unlock(pPager);
2738 return pager_error(pPager, rc);
2739 }
2740 pPager->state = PAGER_EXCLUSIVE;
2741
2742 /* Open the journal for reading only. Return SQLITE_BUSY if
2743 ** we are unable to open the journal file.
2744 **
2745 ** The journal file does not need to be locked itself. The
2746 ** journal file is never open unless the main database file holds
2747 ** a write lock, so there is never any chance of two or more
2748 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00002749 **
2750 ** Open the journal for read/write access. This is because in
2751 ** exclusive-access mode the file descriptor will be kept open and
2752 ** possibly used for a transaction later on. On some systems, the
2753 ** OsTruncate() call used in exclusive-access mode also requires
2754 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00002755 */
danielk1977979f38e2007-03-27 16:19:51 +00002756 rc = SQLITE_BUSY;
2757 if( sqlite3OsFileExists(pPager->zJournal) ){
2758 int ro;
danielk19777152de82007-03-29 17:28:14 +00002759 assert( !pPager->tempFile );
danielk1977979f38e2007-03-27 16:19:51 +00002760 rc = sqlite3OsOpenReadWrite(pPager->zJournal, &pPager->jfd, &ro);
2761 if( ro ){
2762 rc = SQLITE_BUSY;
2763 }
2764 }
danielk1977e277be02007-03-23 18:12:06 +00002765 if( rc!=SQLITE_OK ){
2766 pager_unlock(pPager);
2767 return SQLITE_BUSY;
2768 }
2769 pPager->journalOpen = 1;
2770 pPager->journalStarted = 0;
2771 pPager->journalOff = 0;
2772 pPager->setMaster = 0;
2773 pPager->journalHdr = 0;
2774
2775 /* Playback and delete the journal. Drop the database write
2776 ** lock and reacquire the read lock.
2777 */
2778 rc = pager_playback(pPager, 1);
2779 if( rc!=SQLITE_OK ){
2780 return pager_error(pPager, rc);
2781 }
danielk1977c5859712007-03-26 12:26:27 +00002782 assert(pPager->state==PAGER_SHARED ||
2783 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
2784 );
danielk1977e277be02007-03-23 18:12:06 +00002785 }
2786
2787 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00002788 /* The shared-lock has just been acquired on the database file
2789 ** and there are already pages in the cache (from a previous
2790 ** read or write transaction). If the value of the change-counter
2791 ** stored in Pager.iChangeCount matches that found on page 1 of
2792 ** the database file, then no database changes have occured since
2793 ** the cache was last valid and it is safe to retain the cached
2794 ** pages. Otherwise, if Pager.iChangeCount does not match the
2795 ** change-counter on page 1 of the file, the current cache contents
2796 ** must be discarded.
2797 */
2798
danielk1977e277be02007-03-23 18:12:06 +00002799 PgHdr *pPage1 = pager_lookup(pPager, 1);
2800 if( pPage1 ){
danielk197724168722007-04-02 05:07:47 +00002801 unlinkPage(pPage1);
2802
2803 assert( pPager->pFirst==pPager->pFirstSynced );
2804 pPage1->pNextFree = pPager->pFirst;
2805 if( pPager->pFirst ){
2806 pPager->pFirst->pPrevFree = pPage1;
2807 }else{
2808 assert( !pPager->pLast );
2809 pPager->pLast = pPage1;
2810 }
2811 pPager->pFirst = pPage1;
2812 pPager->pFirstSynced = pPage1;
2813
danielk1977e277be02007-03-23 18:12:06 +00002814 }
2815
2816 assert( !pager_lookup(pPager, 1) );
2817 rc = sqlite3PagerAcquire(pPager, 1, &pPage1, 0);
2818 if( rc==SQLITE_OK ){
2819 /* The change-counter is stored at offset 24. See also
2820 ** pager_incr_changecounter().
2821 */
2822 u32 iChangeCount = retrieve32bits(pPage1, 24);
2823 pPager->nRef++;
2824 sqlite3PagerUnref(pPage1);
2825 pPager->nRef--;
2826 if( iChangeCount!=pPager->iChangeCount ){
2827 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002828 }
2829 pPager->iChangeCount = iChangeCount;
2830 }
2831 }
2832 }
danielk1977c5859712007-03-26 12:26:27 +00002833 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
2834 if( pPager->state==PAGER_UNLOCK ){
2835 pPager->state = PAGER_SHARED;
2836 }
danielk1977e277be02007-03-23 18:12:06 +00002837 }
2838
2839 return rc;
2840}
2841
2842/*
danielk197724168722007-04-02 05:07:47 +00002843** Allocate or recycle space for a single page.
2844*/
2845static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
2846 int rc = SQLITE_OK;
2847 PgHdr *pPg;
2848
2849 if( !(pPager->pFirstSynced && pPager->pFirstSynced->pgno==0) && (
2850 pPager->nPage<pPager->mxPage || pPager->pFirst==0 || MEMDB ||
2851 (pPager->pFirstSynced==0 && pPager->doNotSync)
2852 ) ){
2853 /* Create a new page */
2854 if( pPager->nPage>=pPager->nHash ){
2855 pager_resize_hash_table(pPager,
2856 pPager->nHash<256 ? 256 : pPager->nHash*2);
2857 if( pPager->nHash==0 ){
2858 rc = SQLITE_NOMEM;
2859 goto pager_allocate_out;
2860 }
2861 }
2862 pPg = sqliteMallocRaw( sizeof(*pPg) + pPager->pageSize
2863 + sizeof(u32) + pPager->nExtra
2864 + MEMDB*sizeof(PgHistory) );
2865 if( pPg==0 ){
2866 rc = SQLITE_NOMEM;
2867 goto pager_allocate_out;
2868 }
2869 memset(pPg, 0, sizeof(*pPg));
2870 if( MEMDB ){
2871 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
2872 }
2873 pPg->pPager = pPager;
2874 pPg->pNextAll = pPager->pAll;
2875 pPager->pAll = pPg;
2876 pPager->nPage++;
2877 if( pPager->nPage>pPager->nMaxPage ){
2878 assert( pPager->nMaxPage==(pPager->nPage-1) );
2879 pPager->nMaxPage++;
2880 }
2881 }else{
2882 /* Recycle an existing page with a zero ref-count. */
2883 rc = pager_recycle(pPager, 1, &pPg);
2884 if( rc!=SQLITE_OK ){
2885 goto pager_allocate_out;
2886 }
2887 assert( pPager->state>=SHARED_LOCK );
2888 assert(pPg);
2889 }
2890 *ppPg = pPg;
2891
2892pager_allocate_out:
2893 return rc;
2894}
2895
2896/*
drhd9b02572001-04-15 00:37:09 +00002897** Acquire a page.
2898**
drh58a11682001-11-10 13:51:08 +00002899** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00002900** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00002901**
drh306dc212001-05-21 13:45:10 +00002902** A _get works for any page number greater than 0. If the database
2903** file is smaller than the requested page, then no actual disk
2904** read occurs and the memory image of the page is initialized to
2905** all zeros. The extra data appended to a page is always initialized
2906** to zeros the first time a page is loaded into memory.
2907**
drhd9b02572001-04-15 00:37:09 +00002908** The acquisition might fail for several reasons. In all cases,
2909** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00002910**
danielk19773b8a05f2007-03-19 17:44:26 +00002911** See also sqlite3PagerLookup(). Both this routine and _lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00002912** to find a page in the in-memory cache first. If the page is not already
drh5e00f6c2001-09-13 13:46:56 +00002913** in memory, this routine goes to disk to read it in whereas _lookup()
drh7e3b0a02001-04-28 16:52:40 +00002914** just returns 0. This routine acquires a read-lock the first time it
2915** has to go to disk, and could also playback an old journal if necessary.
2916** Since _lookup() never goes to disk, it never has to deal with locks
2917** or journal files.
drh0787db62007-03-04 13:15:27 +00002918**
2919** If clrFlag is false, the page contents are actually read from disk.
2920** If clfFlag is true, it means the page is about to be erased and
2921** rewritten without first being read so there is no point it doing
2922** the disk I/O.
drhed7c8552001-04-11 14:29:21 +00002923*/
danielk19773b8a05f2007-03-19 17:44:26 +00002924int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag){
drhed7c8552001-04-11 14:29:21 +00002925 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00002926 int rc;
drhed7c8552001-04-11 14:29:21 +00002927
danielk1977e277be02007-03-23 18:12:06 +00002928 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
2929
danielk197726836652005-01-17 01:33:13 +00002930 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
2931 ** number greater than this, or zero, is requested.
2932 */
drh267cb322005-09-16 17:16:52 +00002933 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00002934 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00002935 }
2936
drhd9b02572001-04-15 00:37:09 +00002937 /* Make sure we have not hit any critical errors.
2938 */
drh836faa42003-01-11 13:30:57 +00002939 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00002940 *ppPage = 0;
danielk1977efaaf572006-01-16 11:29:19 +00002941 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
2942 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00002943 }
2944
danielk197713adf8a2004-06-03 16:08:41 +00002945 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00002946 ** on the database file. pagerSharedLock() is a no-op if
2947 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00002948 */
danielk1977e277be02007-03-23 18:12:06 +00002949 rc = pagerSharedLock(pPager);
2950 if( rc!=SQLITE_OK ){
2951 return rc;
drhed7c8552001-04-11 14:29:21 +00002952 }
danielk1977e277be02007-03-23 18:12:06 +00002953 assert( pPager->state!=PAGER_UNLOCK );
2954
2955 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00002956 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00002957 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00002958 int nMax;
drhed7c8552001-04-11 14:29:21 +00002959 int h;
drhfcd35c72005-05-21 02:48:08 +00002960 TEST_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00002961 rc = pagerAllocatePage(pPager, &pPg);
2962 if( rc!=SQLITE_OK ){
2963 return rc;
drhed7c8552001-04-11 14:29:21 +00002964 }
danielk197724168722007-04-02 05:07:47 +00002965
drhed7c8552001-04-11 14:29:21 +00002966 pPg->pgno = pgno;
drh1ab43002002-01-14 09:28:19 +00002967 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19774adee202004-05-08 08:23:19 +00002968 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
drhdb48ee02003-01-16 13:42:43 +00002969 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00002970 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00002971 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00002972 }else{
2973 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00002974 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00002975 }
drhac69b052004-05-12 13:30:07 +00002976 if( pPager->aInStmt && (int)pgno<=pPager->stmtSize
2977 && (pPager->aInStmt[pgno/8] & (1<<(pgno&7)))!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00002978 page_add_to_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00002979 }else{
drh3aac2dd2004-04-26 14:10:20 +00002980 page_remove_from_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00002981 }
drh605ad8f2006-05-03 23:34:05 +00002982 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00002983 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00002984 REFINFO(pPg);
danielk197775bab7d2006-01-23 13:09:45 +00002985
drhd9b02572001-04-15 00:37:09 +00002986 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00002987 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00002988 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00002989 }
danielk1977979f38e2007-03-27 16:19:51 +00002990 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002991 if( pPager->errCode ){
danielk19773b8a05f2007-03-19 17:44:26 +00002992 sqlite3PagerUnref(pPg);
danielk1977efaaf572006-01-16 11:29:19 +00002993 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002994 return rc;
2995 }
danielk197775bab7d2006-01-23 13:09:45 +00002996
2997 /* Populate the page with data, either by reading from the database
2998 ** file, or by setting the entire page to zero.
2999 */
danielk1977979f38e2007-03-27 16:19:51 +00003000 if( nMax<(int)pgno || MEMDB || (clrFlag && !pPager->alwaysRollback) ){
drh90f5ecb2004-07-22 01:19:35 +00003001 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drh306dc212001-05-21 13:45:10 +00003002 }else{
drhb7f91642004-10-31 02:22:47 +00003003 assert( MEMDB==0 );
drh054889e2005-11-30 03:20:31 +00003004 rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize);
drhb4746b92005-09-09 01:32:06 +00003005 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00003006 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg),
drh9c06c952005-11-26 00:25:00 +00003007 pPager->pageSize);
drhb4746b92005-09-09 01:32:06 +00003008 }
drhb0603412007-02-28 04:47:26 +00003009 IOTRACE(("PGIN %p %d\n", pPager, pgno))
drh4f0c5872007-03-26 22:05:01 +00003010 PAGERTRACE3("FETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
drhc001c582006-03-06 18:23:16 +00003011 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh551b7732006-11-06 21:20:25 +00003012 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3013 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003014 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003015 return rc;
danielk197742741be2005-01-08 12:42:39 +00003016 }else{
drhfcd35c72005-05-21 02:48:08 +00003017 TEST_INCR(pPager->nRead);
drh81a20f22001-10-12 17:30:04 +00003018 }
drh306dc212001-05-21 13:45:10 +00003019 }
danielk197775bab7d2006-01-23 13:09:45 +00003020
3021 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003022 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003023 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003024 pPg->pNextHash = pPager->aHash[h];
3025 pPager->aHash[h] = pPg;
3026 if( pPg->pNextHash ){
3027 assert( pPg->pNextHash->pPrevHash==0 );
3028 pPg->pNextHash->pPrevHash = pPg;
3029 }
3030
danielk19773c407372005-02-15 02:54:14 +00003031#ifdef SQLITE_CHECK_PAGES
3032 pPg->pageHash = pager_pagehash(pPg);
3033#endif
drhed7c8552001-04-11 14:29:21 +00003034 }else{
drhd9b02572001-04-15 00:37:09 +00003035 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003036 assert(pPager->nRef>0 || pgno==1);
drhfcd35c72005-05-21 02:48:08 +00003037 TEST_INCR(pPager->nHit);
drhdf0b3b02001-06-23 11:36:20 +00003038 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003039 }
danielk19773b8a05f2007-03-19 17:44:26 +00003040 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003041 return SQLITE_OK;
3042}
3043
3044/*
drh7e3b0a02001-04-28 16:52:40 +00003045** Acquire a page if it is already in the in-memory cache. Do
3046** not read the page from disk. Return a pointer to the page,
3047** or 0 if the page is not in cache.
3048**
danielk19773b8a05f2007-03-19 17:44:26 +00003049** See also sqlite3PagerGet(). The difference between this routine
3050** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003051** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003052** returns NULL if the page is not in cache or if a disk I/O error
3053** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003054*/
danielk19773b8a05f2007-03-19 17:44:26 +00003055DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh7e3b0a02001-04-28 16:52:40 +00003056 PgHdr *pPg;
3057
drh836faa42003-01-11 13:30:57 +00003058 assert( pPager!=0 );
3059 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003060
3061 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003062 assert( !pPager->pAll || pPager->exclusiveMode );
danielk1977e277be02007-03-23 18:12:06 +00003063 return 0;
3064 }
danielk1977334cdb62007-03-26 08:05:12 +00003065 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drh7e3b0a02001-04-28 16:52:40 +00003066 return 0;
3067 }
drh7e3b0a02001-04-28 16:52:40 +00003068 pPg = pager_lookup(pPager, pgno);
3069 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +00003070 page_ref(pPg);
danielk19773b8a05f2007-03-19 17:44:26 +00003071 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003072}
3073
3074/*
drhed7c8552001-04-11 14:29:21 +00003075** Release a page.
3076**
3077** If the number of references to the page drop to zero, then the
3078** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003079** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003080** removed.
3081*/
danielk19773b8a05f2007-03-19 17:44:26 +00003082int sqlite3PagerUnref(DbPage *pPg){
drhd9b02572001-04-15 00:37:09 +00003083
3084 /* Decrement the reference count for this page
3085 */
drhed7c8552001-04-11 14:29:21 +00003086 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +00003087 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00003088 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00003089
danielk19773c407372005-02-15 02:54:14 +00003090 CHECK_PAGE(pPg);
3091
drh72f82862001-05-24 21:06:34 +00003092 /* When the number of references to a page reach 0, call the
3093 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003094 */
drhed7c8552001-04-11 14:29:21 +00003095 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00003096 Pager *pPager;
3097 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003098 pPg->pNextFree = 0;
3099 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00003100 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00003101 if( pPg->pPrevFree ){
3102 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00003103 }else{
3104 pPager->pFirst = pPg;
3105 }
drh341eae82003-01-21 02:39:36 +00003106 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
3107 pPager->pFirstSynced = pPg;
3108 }
drh72f82862001-05-24 21:06:34 +00003109 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003110 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003111 }
drhd9b02572001-04-15 00:37:09 +00003112
3113 /* When all pages reach the freelist, drop the read lock from
3114 ** the database file.
3115 */
3116 pPager->nRef--;
3117 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003118 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003119 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003120 }
drhed7c8552001-04-11 14:29:21 +00003121 }
drhd9b02572001-04-15 00:37:09 +00003122 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003123}
3124
3125/*
drha6abd042004-06-09 17:37:22 +00003126** Create a journal file for pPager. There should already be a RESERVED
3127** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003128**
3129** Return SQLITE_OK if everything. Return an error code and release the
3130** write lock if anything goes wrong.
3131*/
3132static int pager_open_journal(Pager *pPager){
3133 int rc;
drhb7f91642004-10-31 02:22:47 +00003134 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003135 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003136 assert( pPager->journalOpen==0 );
3137 assert( pPager->useJournal );
drh88b01a12005-03-28 18:04:27 +00003138 assert( pPager->aInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003139 sqlite3PagerPagecount(pPager);
drhda47d772002-12-02 04:25:19 +00003140 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
3141 if( pPager->aInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003142 rc = SQLITE_NOMEM;
3143 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003144 }
drh66560ad2006-01-06 14:32:19 +00003145 rc = sqlite3OsOpenExclusive(pPager->zJournal, &pPager->jfd,
drh9c06c952005-11-26 00:25:00 +00003146 pPager->tempFile);
danielk197776572402004-06-25 02:38:54 +00003147 pPager->journalOff = 0;
3148 pPager->setMaster = 0;
3149 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003150 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003151 if( rc==SQLITE_NOMEM ){
3152 sqlite3OsDelete(pPager->zJournal);
3153 }
drh9c105bb2004-10-02 20:38:28 +00003154 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003155 }
drhac530b12006-02-11 01:25:50 +00003156 sqlite3OsSetFullSync(pPager->jfd, pPager->full_fsync);
3157 sqlite3OsSetFullSync(pPager->fd, pPager->full_fsync);
drh054889e2005-11-30 03:20:31 +00003158 sqlite3OsOpenDirectory(pPager->jfd, pPager->zDirectory);
drhda47d772002-12-02 04:25:19 +00003159 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003160 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003161 pPager->needSync = 0;
3162 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003163 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003164 if( pPager->errCode ){
3165 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003166 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003167 }
drhda47d772002-12-02 04:25:19 +00003168 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003169
danielk197776572402004-06-25 02:38:54 +00003170 rc = writeJournalHdr(pPager);
3171
drhac69b052004-05-12 13:30:07 +00003172 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003173 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003174 }
danielk1977261919c2005-12-06 12:52:59 +00003175 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003176 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003177 if( rc==SQLITE_OK ){
3178 rc = SQLITE_FULL;
3179 }
3180 }
drh9c105bb2004-10-02 20:38:28 +00003181 return rc;
3182
3183failed_to_open_journal:
3184 sqliteFree(pPager->aInJournal);
3185 pPager->aInJournal = 0;
drh600e46a2007-03-28 01:59:33 +00003186#if 0
danielk1977261919c2005-12-06 12:52:59 +00003187 if( rc==SQLITE_NOMEM ){
3188 /* If this was a malloc() failure, then we will not be closing the pager
3189 ** file. So delete any journal file we may have just created. Otherwise,
3190 ** the system will get confused, we have a read-lock on the file and a
3191 ** mysterious journal has appeared in the filesystem.
3192 */
danielk1977979f38e2007-03-27 16:19:51 +00003193 /* sqlite3OsDelete(pPager->zJournal); */
danielk1977261919c2005-12-06 12:52:59 +00003194 }else{
drh600e46a2007-03-28 01:59:33 +00003195 /* If we reset the pager here, we will delete pages out from under
3196 ** various cursors and will ultimately segfault. */
3197 /* pager_reset(pPager); */
danielk1977261919c2005-12-06 12:52:59 +00003198 }
drh600e46a2007-03-28 01:59:33 +00003199#endif
drh9c105bb2004-10-02 20:38:28 +00003200 return rc;
drhda47d772002-12-02 04:25:19 +00003201}
3202
3203/*
drh4b845d72002-03-05 12:41:19 +00003204** Acquire a write-lock on the database. The lock is removed when
3205** the any of the following happen:
3206**
drh80e35f42007-03-30 14:06:34 +00003207** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003208** * sqlite3PagerRollback() is called.
3209** * sqlite3PagerClose() is called.
3210** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003211**
danielk197713adf8a2004-06-03 16:08:41 +00003212** The first parameter to this routine is a pointer to any open page of the
3213** database file. Nothing changes about the page - it is used merely to
3214** acquire a pointer to the Pager structure and as proof that there is
3215** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003216**
danielk197713adf8a2004-06-03 16:08:41 +00003217** The second parameter indicates how much space in bytes to reserve for a
3218** master journal file-name at the start of the journal when it is created.
3219**
3220** A journal file is opened if this is not a temporary file. For temporary
3221** files, the opening of the journal file is deferred until there is an
3222** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003223**
drha6abd042004-06-09 17:37:22 +00003224** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003225**
3226** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3227** immediately instead of waiting until we try to flush the cache. The
3228** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003229*/
danielk19773b8a05f2007-03-19 17:44:26 +00003230int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003231 Pager *pPager = pPg->pPager;
3232 int rc = SQLITE_OK;
3233 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003234 assert( pPager->state!=PAGER_UNLOCK );
3235 if( pPager->state==PAGER_SHARED ){
drh4b845d72002-03-05 12:41:19 +00003236 assert( pPager->aInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003237 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003238 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003239 pPager->origDbSize = pPager->dbSize;
3240 }else{
drh054889e2005-11-30 03:20:31 +00003241 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003242 if( rc==SQLITE_OK ){
3243 pPager->state = PAGER_RESERVED;
3244 if( exFlag ){
3245 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3246 }
3247 }
danielk19779eed5052004-06-04 10:38:30 +00003248 if( rc!=SQLITE_OK ){
3249 return rc;
drhac69b052004-05-12 13:30:07 +00003250 }
drha6abd042004-06-09 17:37:22 +00003251 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003252 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003253 if( pPager->useJournal && !pPager->tempFile ){
3254 rc = pager_open_journal(pPager);
3255 }
drh4b845d72002-03-05 12:41:19 +00003256 }
danielk1977334cdb62007-03-26 08:05:12 +00003257 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3258 /* This happens when the pager was in exclusive-access mode last
3259 ** time a (read or write) transaction was successfully concluded
3260 ** by this connection. Instead of deleting the journal file it was
3261 ** kept open and truncated to 0 bytes.
3262 */
3263 assert( pPager->nRec==0 );
3264 assert( pPager->origDbSize==0 );
drh369339d2007-03-30 16:01:55 +00003265 assert( pPager->aInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003266 sqlite3PagerPagecount(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00003267 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
3268 if( !pPager->aInJournal ){
3269 rc = SQLITE_NOMEM;
3270 }else{
drh369339d2007-03-30 16:01:55 +00003271 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003272 rc = writeJournalHdr(pPager);
3273 }
drh4b845d72002-03-05 12:41:19 +00003274 }
danielk1977334cdb62007-03-26 08:05:12 +00003275 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003276 return rc;
3277}
3278
3279/*
drh605ad8f2006-05-03 23:34:05 +00003280** Make a page dirty. Set its dirty flag and add it to the dirty
3281** page list.
3282*/
3283static void makeDirty(PgHdr *pPg){
3284 if( pPg->dirty==0 ){
3285 Pager *pPager = pPg->pPager;
3286 pPg->dirty = 1;
3287 pPg->pDirty = pPager->pDirty;
3288 if( pPager->pDirty ){
3289 pPager->pDirty->pPrevDirty = pPg;
3290 }
3291 pPg->pPrevDirty = 0;
3292 pPager->pDirty = pPg;
3293 }
3294}
3295
3296/*
3297** Make a page clean. Clear its dirty bit and remove it from the
3298** dirty page list.
3299*/
3300static void makeClean(PgHdr *pPg){
3301 if( pPg->dirty ){
3302 pPg->dirty = 0;
3303 if( pPg->pDirty ){
3304 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
3305 }
3306 if( pPg->pPrevDirty ){
3307 pPg->pPrevDirty->pDirty = pPg->pDirty;
3308 }else{
3309 pPg->pPager->pDirty = pPg->pDirty;
3310 }
3311 }
3312}
3313
3314
3315/*
drhed7c8552001-04-11 14:29:21 +00003316** Mark a data page as writeable. The page is written into the journal
3317** if it is not there already. This routine must be called before making
3318** changes to a page.
3319**
3320** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003321** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003322** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003323** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003324** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003325**
3326** If the journal file could not be written because the disk is full,
3327** then this routine returns SQLITE_FULL and does an immediate rollback.
3328** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003329** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003330** reset.
drhed7c8552001-04-11 14:29:21 +00003331*/
danielk19773b8a05f2007-03-19 17:44:26 +00003332static int pager_write(PgHdr *pPg){
3333 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00003334 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003335 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003336
drh6446c4d2001-12-15 14:22:18 +00003337 /* Check for errors
3338 */
danielk1977efaaf572006-01-16 11:29:19 +00003339 if( pPager->errCode ){
3340 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003341 }
drh5e00f6c2001-09-13 13:46:56 +00003342 if( pPager->readOnly ){
3343 return SQLITE_PERM;
3344 }
drh6446c4d2001-12-15 14:22:18 +00003345
danielk197776572402004-06-25 02:38:54 +00003346 assert( !pPager->setMaster );
3347
danielk19773c407372005-02-15 02:54:14 +00003348 CHECK_PAGE(pPg);
3349
drh6446c4d2001-12-15 14:22:18 +00003350 /* Mark the page as dirty. If the page has already been written
3351 ** to the journal then we can return right away.
3352 */
drh605ad8f2006-05-03 23:34:05 +00003353 makeDirty(pPg);
drhac69b052004-05-12 13:30:07 +00003354 if( pPg->inJournal && (pPg->inStmt || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00003355 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003356 }else{
drh6446c4d2001-12-15 14:22:18 +00003357
danielk1977a0bf2652004-11-04 14:30:04 +00003358 /* If we get this far, it means that the page needs to be
3359 ** written to the transaction journal or the ckeckpoint journal
3360 ** or both.
3361 **
3362 ** First check to see that the transaction journal exists and
3363 ** create it if it does not.
3364 */
3365 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003366 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003367 if( rc!=SQLITE_OK ){
3368 return rc;
3369 }
3370 assert( pPager->state>=PAGER_RESERVED );
3371 if( !pPager->journalOpen && pPager->useJournal ){
3372 rc = pager_open_journal(pPager);
3373 if( rc!=SQLITE_OK ) return rc;
3374 }
3375 assert( pPager->journalOpen || !pPager->useJournal );
3376 pPager->dirtyCache = 1;
3377
3378 /* The transaction journal now exists and we have a RESERVED or an
3379 ** EXCLUSIVE lock on the main database file. Write the current page to
3380 ** the transaction journal if it is not there already.
3381 */
3382 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
3383 if( (int)pPg->pgno <= pPager->origDbSize ){
3384 int szPg;
danielk1977a0bf2652004-11-04 14:30:04 +00003385 if( MEMDB ){
3386 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00003387 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00003388 assert( pHist->pOrig==0 );
3389 pHist->pOrig = sqliteMallocRaw( pPager->pageSize );
3390 if( pHist->pOrig ){
3391 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
3392 }
3393 }else{
drhc001c582006-03-06 18:23:16 +00003394 u32 cksum, saved;
3395 char *pData2, *pEnd;
drh267cb322005-09-16 17:16:52 +00003396 /* We should never write to the journal file the page that
3397 ** contains the database locks. The following assert verifies
3398 ** that we do not. */
3399 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00003400 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00003401 cksum = pager_cksum(pPager, (u8*)pData2);
drhc001c582006-03-06 18:23:16 +00003402 pEnd = pData2 + pPager->pageSize;
3403 pData2 -= 4;
3404 saved = *(u32*)pEnd;
3405 put32bits(pEnd, cksum);
danielk1977a0bf2652004-11-04 14:30:04 +00003406 szPg = pPager->pageSize+8;
drhc001c582006-03-06 18:23:16 +00003407 put32bits(pData2, pPg->pgno);
3408 rc = sqlite3OsWrite(pPager->jfd, pData2, szPg);
drhb0603412007-02-28 04:47:26 +00003409 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
3410 pPager->journalOff, szPg))
danielk1977a0bf2652004-11-04 14:30:04 +00003411 pPager->journalOff += szPg;
drh4f0c5872007-03-26 22:05:01 +00003412 PAGERTRACE4("JOURNAL %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00003413 PAGERID(pPager), pPg->pgno, pPg->needSync);
drhc001c582006-03-06 18:23:16 +00003414 *(u32*)pEnd = saved;
danielk197707cb5602006-01-20 10:55:05 +00003415
3416 /* An error has occured writing to the journal file. The
3417 ** transaction will be rolled back by the layer above.
3418 */
danielk1977a0bf2652004-11-04 14:30:04 +00003419 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00003420 return rc;
3421 }
danielk197707cb5602006-01-20 10:55:05 +00003422
danielk1977a0bf2652004-11-04 14:30:04 +00003423 pPager->nRec++;
3424 assert( pPager->aInJournal!=0 );
3425 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
3426 pPg->needSync = !pPager->noSync;
3427 if( pPager->stmtInUse ){
3428 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
3429 page_add_to_stmt_list(pPg);
3430 }
drhac69b052004-05-12 13:30:07 +00003431 }
danielk197713adf8a2004-06-03 16:08:41 +00003432 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00003433 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00003434 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00003435 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00003436 }
3437 if( pPg->needSync ){
3438 pPager->needSync = 1;
3439 }
3440 pPg->inJournal = 1;
3441 }
3442
3443 /* If the statement journal is open and the page is not in it,
3444 ** then write the current page to the statement journal. Note that
3445 ** the statement journal format differs from the standard journal format
3446 ** in that it omits the checksums and the header.
3447 */
3448 if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
3449 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
3450 if( MEMDB ){
3451 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
3452 assert( pHist->pStmt==0 );
3453 pHist->pStmt = sqliteMallocRaw( pPager->pageSize );
3454 if( pHist->pStmt ){
3455 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
3456 }
drh4f0c5872007-03-26 22:05:01 +00003457 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00003458 }else{
drhc001c582006-03-06 18:23:16 +00003459 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4;
3460 put32bits(pData2, pPg->pgno);
3461 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4);
drh4f0c5872007-03-26 22:05:01 +00003462 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00003463 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00003464 return rc;
3465 }
danielk1977a0bf2652004-11-04 14:30:04 +00003466 pPager->stmtNRec++;
3467 assert( pPager->aInStmt!=0 );
3468 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhdb48ee02003-01-16 13:42:43 +00003469 }
danielk1977a0bf2652004-11-04 14:30:04 +00003470 page_add_to_stmt_list(pPg);
drhd9b02572001-04-15 00:37:09 +00003471 }
drhfa86c412002-02-02 15:01:15 +00003472 }
3473
3474 /* Update the database size and return.
3475 */
drh1aa2d8b2007-01-03 15:34:29 +00003476 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00003477 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003478 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00003479 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00003480 pPager->dbSize++;
3481 }
drh306dc212001-05-21 13:45:10 +00003482 }
drh69688d52001-04-14 16:38:23 +00003483 return rc;
drhed7c8552001-04-11 14:29:21 +00003484}
3485
3486/*
danielk19774099f6e2007-03-19 11:25:20 +00003487** This function is used to mark a data-page as writable. It uses
3488** pager_write() to open a journal file (if it is not already open)
3489** and write the page *pData to the journal.
3490**
3491** The difference between this function and pager_write() is that this
3492** function also deals with the special case where 2 or more pages
3493** fit on a single disk sector. In this case all co-resident pages
3494** must have been written to the journal file before returning.
3495*/
danielk19773b8a05f2007-03-19 17:44:26 +00003496int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003497 int rc = SQLITE_OK;
3498
danielk19773b8a05f2007-03-19 17:44:26 +00003499 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003500 Pager *pPager = pPg->pPager;
3501 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3502
3503 if( !MEMDB && nPagePerSector>1 ){
3504 Pgno nPageCount; /* Total number of pages in database file */
3505 Pgno pg1; /* First page of the sector pPg is located on. */
3506 int nPage; /* Number of pages starting at pg1 to journal */
3507 int ii;
3508
3509 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3510 ** header to be written between the pages journaled by this function.
3511 */
3512 assert( pPager->doNotSync==0 );
3513 pPager->doNotSync = 1;
3514
3515 /* This trick assumes that both the page-size and sector-size are
3516 ** an integer power of 2. It sets variable pg1 to the identifier
3517 ** of the first page of the sector pPg is located on.
3518 */
3519 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3520
danielk19773b8a05f2007-03-19 17:44:26 +00003521 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003522 if( pPg->pgno>nPageCount ){
3523 nPage = (pPg->pgno - pg1)+1;
3524 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3525 nPage = nPageCount+1-pg1;
3526 }else{
3527 nPage = nPagePerSector;
3528 }
3529 assert(nPage>0);
3530 assert(pg1<=pPg->pgno);
3531 assert((pg1+nPage)>pPg->pgno);
3532
3533 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3534 Pgno pg = pg1+ii;
3535 if( !pPager->aInJournal || pg==pPg->pgno ||
3536 pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
3537 ) {
3538 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003539 PgHdr *pPage;
3540 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003541 if( rc==SQLITE_OK ){
3542 rc = pager_write(pPage);
danielk19773b8a05f2007-03-19 17:44:26 +00003543 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003544 }
3545 }
3546 }
3547 }
3548
3549 assert( pPager->doNotSync==1 );
3550 pPager->doNotSync = 0;
3551 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003552 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003553 }
3554 return rc;
3555}
3556
3557/*
drhaacc5432002-01-06 17:07:40 +00003558** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003559** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003560** to change the content of the page.
3561*/
danielk19777d3a6662006-01-23 16:21:05 +00003562#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003563int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00003564 return pPg->dirty;
3565}
danielk19777d3a6662006-01-23 16:21:05 +00003566#endif
drh6019e162001-07-02 17:51:45 +00003567
danielk1977b84f96f2005-01-20 11:32:23 +00003568#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00003569/*
drh001bbcb2003-03-19 03:14:00 +00003570** Replace the content of a single page with the information in the third
3571** argument.
3572*/
danielk19773b8a05f2007-03-19 17:44:26 +00003573int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
3574 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00003575 int rc;
3576
danielk19773b8a05f2007-03-19 17:44:26 +00003577 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00003578 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003579 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00003580 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003581 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00003582 }
danielk19773b8a05f2007-03-19 17:44:26 +00003583 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00003584 }
3585 return rc;
3586}
danielk1977b84f96f2005-01-20 11:32:23 +00003587#endif
drh001bbcb2003-03-19 03:14:00 +00003588
3589/*
drh30e58752002-03-02 20:41:57 +00003590** A call to this routine tells the pager that it is not necessary to
3591** write the information on page "pgno" back to the disk, even though
3592** that page might be marked as dirty.
3593**
3594** The overlying software layer calls this routine when all of the data
3595** on the given page is unused. The pager marks the page as clean so
3596** that it does not get written to disk.
3597**
3598** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003599** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003600** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003601**
3602** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003603** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003604** will thereafter be ignored. This is necessary to avoid a problem
3605** where a page with data is added to the freelist during one part of
3606** a transaction then removed from the freelist during a later part
3607** of the same transaction and reused for some other purpose. When it
3608** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003609** the sqlite3PagerDontRollback() routine is called. But because the
3610** page contains critical data, we still need to be sure it gets
3611** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003612*/
danielk19773b8a05f2007-03-19 17:44:26 +00003613void sqlite3PagerDontWrite(Pager *pPager, Pgno pgno){
drh30e58752002-03-02 20:41:57 +00003614 PgHdr *pPg;
drh8e298f92002-07-06 16:28:47 +00003615
drhb7f91642004-10-31 02:22:47 +00003616 if( MEMDB ) return;
danielk1977eac7a362004-06-16 07:45:24 +00003617
drh30e58752002-03-02 20:41:57 +00003618 pPg = pager_lookup(pPager, pgno);
drh43617e92006-03-06 20:55:46 +00003619 assert( pPg!=0 ); /* We never call _dont_write unless the page is in mem */
drh8e298f92002-07-06 16:28:47 +00003620 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00003621 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00003622 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00003623 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
3624 /* If this pages is the last page in the file and the file has grown
3625 ** during the current transaction, then do NOT mark the page as clean.
3626 ** When the database file grows, we must make sure that the last page
3627 ** gets written at least once so that the disk file will be the correct
3628 ** size. If you do not write this page and the size of the file
3629 ** on the disk ends up being too small, that can lead to database
3630 ** corruption during the next transaction.
3631 */
3632 }else{
drh4f0c5872007-03-26 22:05:01 +00003633 PAGERTRACE3("DONT_WRITE page %d of %d\n", pgno, PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00003634 IOTRACE(("CLEAN %p %d\n", pPager, pgno))
drh605ad8f2006-05-03 23:34:05 +00003635 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00003636#ifdef SQLITE_CHECK_PAGES
3637 pPg->pageHash = pager_pagehash(pPg);
3638#endif
drh8124a302002-06-25 14:43:57 +00003639 }
drh30e58752002-03-02 20:41:57 +00003640 }
3641}
3642
3643/*
3644** A call to this routine tells the pager that if a rollback occurs,
3645** it is not necessary to restore the data on the given page. This
3646** means that the pager does not have to record the given page in the
3647** rollback journal.
3648*/
danielk19773b8a05f2007-03-19 17:44:26 +00003649void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003650 Pager *pPager = pPg->pPager;
3651
drhd3627af2006-12-18 18:34:51 +00003652 assert( pPager->state>=PAGER_RESERVED );
3653 if( pPager->journalOpen==0 ) return;
drhb7f91642004-10-31 02:22:47 +00003654 if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
drh30e58752002-03-02 20:41:57 +00003655 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
3656 assert( pPager->aInJournal!=0 );
3657 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
3658 pPg->inJournal = 1;
drhac69b052004-05-12 13:30:07 +00003659 if( pPager->stmtInUse ){
3660 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh3aac2dd2004-04-26 14:10:20 +00003661 page_add_to_stmt_list(pPg);
drh30e58752002-03-02 20:41:57 +00003662 }
drh4f0c5872007-03-26 22:05:01 +00003663 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00003664 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00003665 }
drhac69b052004-05-12 13:30:07 +00003666 if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
drh30e58752002-03-02 20:41:57 +00003667 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00003668 assert( pPager->aInStmt!=0 );
3669 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh3aac2dd2004-04-26 14:10:20 +00003670 page_add_to_stmt_list(pPg);
drh30e58752002-03-02 20:41:57 +00003671 }
3672}
3673
drhac69b052004-05-12 13:30:07 +00003674
drh30e58752002-03-02 20:41:57 +00003675/*
drh80e35f42007-03-30 14:06:34 +00003676** This routine is called to increment the database file change-counter,
3677** stored at byte 24 of the pager file.
3678*/
3679static int pager_incr_changecounter(Pager *pPager){
3680 PgHdr *pPgHdr;
3681 u32 change_counter;
3682 int rc;
3683
3684 if( !pPager->changeCountDone ){
3685 /* Open page 1 of the file for writing. */
3686 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3687 if( rc!=SQLITE_OK ) return rc;
3688 rc = sqlite3PagerWrite(pPgHdr);
3689 if( rc!=SQLITE_OK ) return rc;
3690
3691 /* Read the current value at byte 24. */
3692 change_counter = retrieve32bits(pPgHdr, 24);
3693
3694 /* Increment the value just read and write it back to byte 24. */
3695 change_counter++;
3696 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
3697 pPager->iChangeCount = change_counter;
3698
3699 /* Release the page reference. */
3700 sqlite3PagerUnref(pPgHdr);
3701 pPager->changeCountDone = 1;
3702 }
3703 return SQLITE_OK;
3704}
3705
3706/*
3707** Sync the database file for the pager pPager. zMaster points to the name
3708** of a master journal file that should be written into the individual
3709** journal file. zMaster may be NULL, which is interpreted as no master
3710** journal (a single database transaction).
3711**
3712** This routine ensures that the journal is synced, all dirty pages written
3713** to the database file and the database file synced. The only thing that
3714** remains to commit the transaction is to delete the journal file (or
3715** master journal file if specified).
3716**
3717** Note that if zMaster==NULL, this does not overwrite a previous value
3718** passed to an sqlite3PagerCommitPhaseOne() call.
3719**
3720** If parameter nTrunc is non-zero, then the pager file is truncated to
3721** nTrunc pages (this is used by auto-vacuum databases).
3722*/
3723int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
3724 int rc = SQLITE_OK;
3725
3726 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
3727 pPager->zFilename, zMaster, nTrunc);
3728
3729 /* If this is an in-memory db, or no pages have been written to, or this
3730 ** function has already been called, it is a no-op.
3731 */
3732 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
3733 PgHdr *pPg;
3734 assert( pPager->journalOpen );
3735
3736 /* If a master journal file name has already been written to the
3737 ** journal file, then no sync is required. This happens when it is
3738 ** written, then the process fails to upgrade from a RESERVED to an
3739 ** EXCLUSIVE lock. The next time the process tries to commit the
3740 ** transaction the m-j name will have already been written.
3741 */
3742 if( !pPager->setMaster ){
3743 rc = pager_incr_changecounter(pPager);
3744 if( rc!=SQLITE_OK ) goto sync_exit;
3745#ifndef SQLITE_OMIT_AUTOVACUUM
3746 if( nTrunc!=0 ){
3747 /* If this transaction has made the database smaller, then all pages
3748 ** being discarded by the truncation must be written to the journal
3749 ** file.
3750 */
3751 Pgno i;
3752 int iSkip = PAGER_MJ_PGNO(pPager);
3753 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
3754 if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){
3755 rc = sqlite3PagerGet(pPager, i, &pPg);
3756 if( rc!=SQLITE_OK ) goto sync_exit;
3757 rc = sqlite3PagerWrite(pPg);
3758 sqlite3PagerUnref(pPg);
3759 if( rc!=SQLITE_OK ) goto sync_exit;
3760 }
3761 }
3762 }
3763#endif
3764 rc = writeMasterJournal(pPager, zMaster);
3765 if( rc!=SQLITE_OK ) goto sync_exit;
3766 rc = syncJournal(pPager);
3767 if( rc!=SQLITE_OK ) goto sync_exit;
3768 }
3769
3770#ifndef SQLITE_OMIT_AUTOVACUUM
3771 if( nTrunc!=0 ){
3772 rc = sqlite3PagerTruncate(pPager, nTrunc);
3773 if( rc!=SQLITE_OK ) goto sync_exit;
3774 }
3775#endif
3776
3777 /* Write all dirty pages to the database file */
3778 pPg = pager_get_all_dirty_pages(pPager);
3779 rc = pager_write_pagelist(pPg);
3780 if( rc!=SQLITE_OK ) goto sync_exit;
drh3cdd7d32007-03-30 14:46:01 +00003781 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00003782
3783 /* Sync the database file. */
3784 if( !pPager->noSync ){
3785 rc = sqlite3OsSync(pPager->fd, 0);
3786 }
3787 IOTRACE(("DBSYNC %p\n", pPager))
3788
3789 pPager->state = PAGER_SYNCED;
3790 }else if( MEMDB && nTrunc!=0 ){
3791 rc = sqlite3PagerTruncate(pPager, nTrunc);
3792 }
3793
3794sync_exit:
3795 return rc;
3796}
3797
3798
3799/*
drhed7c8552001-04-11 14:29:21 +00003800** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00003801**
3802** If the commit fails for any reason, a rollback attempt is made
3803** and an error code is returned. If the commit worked, SQLITE_OK
3804** is returned.
drhed7c8552001-04-11 14:29:21 +00003805*/
drh80e35f42007-03-30 14:06:34 +00003806int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00003807 int rc;
drhed7c8552001-04-11 14:29:21 +00003808 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00003809
danielk1977efaaf572006-01-16 11:29:19 +00003810 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00003811 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003812 }
drha6abd042004-06-09 17:37:22 +00003813 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00003814 return SQLITE_ERROR;
3815 }
drh4f0c5872007-03-26 22:05:01 +00003816 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003817 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00003818 pPg = pager_get_all_dirty_pages(pPager);
3819 while( pPg ){
3820 clearHistory(PGHDR_TO_HIST(pPg, pPager));
3821 pPg->dirty = 0;
3822 pPg->inJournal = 0;
3823 pPg->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00003824 pPg->needSync = 0;
drhac69b052004-05-12 13:30:07 +00003825 pPg->pPrevStmt = pPg->pNextStmt = 0;
3826 pPg = pPg->pDirty;
3827 }
drh605ad8f2006-05-03 23:34:05 +00003828 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00003829#ifndef NDEBUG
3830 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
3831 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
3832 assert( !pPg->alwaysRollback );
3833 assert( !pHist->pOrig );
3834 assert( !pHist->pStmt );
3835 }
3836#endif
drhac69b052004-05-12 13:30:07 +00003837 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00003838 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00003839 return SQLITE_OK;
3840 }
drh3cdd7d32007-03-30 14:46:01 +00003841 assert( pPager->journalOpen || !pPager->dirtyCache );
3842 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
3843 rc = pager_end_transaction(pPager);
danielk1977979f38e2007-03-27 16:19:51 +00003844 return pager_error(pPager, rc);
drhed7c8552001-04-11 14:29:21 +00003845}
3846
3847/*
drha6abd042004-06-09 17:37:22 +00003848** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00003849** All in-memory cache pages revert to their original data contents.
3850** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00003851**
3852** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00003853** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00003854** process is writing trash into the journal file (SQLITE_CORRUPT) or
3855** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
3856** codes are returned for all these occasions. Otherwise,
3857** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00003858*/
danielk19773b8a05f2007-03-19 17:44:26 +00003859int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00003860 int rc;
drh4f0c5872007-03-26 22:05:01 +00003861 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003862 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00003863 PgHdr *p;
3864 for(p=pPager->pAll; p; p=p->pNextAll){
3865 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00003866 assert( !p->alwaysRollback );
3867 if( !p->dirty ){
3868 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
3869 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
3870 continue;
3871 }
3872
drhac69b052004-05-12 13:30:07 +00003873 pHist = PGHDR_TO_HIST(p, pPager);
3874 if( pHist->pOrig ){
3875 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00003876 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003877 }else{
drh4f0c5872007-03-26 22:05:01 +00003878 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003879 }
3880 clearHistory(pHist);
3881 p->dirty = 0;
3882 p->inJournal = 0;
3883 p->inStmt = 0;
3884 p->pPrevStmt = p->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00003885 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00003886 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00003887 }
drhac69b052004-05-12 13:30:07 +00003888 }
drh605ad8f2006-05-03 23:34:05 +00003889 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00003890 pPager->pStmt = 0;
3891 pPager->dbSize = pPager->origDbSize;
3892 memoryTruncate(pPager);
3893 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00003894 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00003895 return SQLITE_OK;
3896 }
3897
drha6abd042004-06-09 17:37:22 +00003898 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00003899 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003900 return rc;
3901 }
drhdb48ee02003-01-16 13:42:43 +00003902
danielk1977efaaf572006-01-16 11:29:19 +00003903 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00003904 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00003905 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00003906 }
danielk1977efaaf572006-01-16 11:29:19 +00003907 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00003908 }
drha6abd042004-06-09 17:37:22 +00003909 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00003910 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00003911 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00003912 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00003913 if( rc==SQLITE_OK ){
3914 rc = rc2;
3915 }
3916 }else{
danielk1977e277be02007-03-23 18:12:06 +00003917 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00003918 }
drhd9b02572001-04-15 00:37:09 +00003919 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00003920
3921 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
3922 ** cache. So call pager_error() on the way out to make any error
3923 ** persistent.
3924 */
3925 return pager_error(pPager, rc);
drh98808ba2001-10-18 12:34:46 +00003926}
drhd9b02572001-04-15 00:37:09 +00003927
3928/*
drh5e00f6c2001-09-13 13:46:56 +00003929** Return TRUE if the database file is opened read-only. Return FALSE
3930** if the database is (in theory) writable.
3931*/
danielk19773b8a05f2007-03-19 17:44:26 +00003932int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00003933 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00003934}
3935
3936/*
drh0f7eb612006-08-08 13:51:43 +00003937** Return the number of references to the pager.
3938*/
danielk19773b8a05f2007-03-19 17:44:26 +00003939int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00003940 return pPager->nRef;
3941}
3942
3943#ifdef SQLITE_TEST
3944/*
drhd9b02572001-04-15 00:37:09 +00003945** This routine is used for testing and analysis only.
3946*/
danielk19773b8a05f2007-03-19 17:44:26 +00003947int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00003948 static int a[11];
drhd9b02572001-04-15 00:37:09 +00003949 a[0] = pPager->nRef;
3950 a[1] = pPager->nPage;
3951 a[2] = pPager->mxPage;
3952 a[3] = pPager->dbSize;
3953 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00003954 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003955 a[6] = pPager->nHit;
3956 a[7] = pPager->nMiss;
3957 a[8] = pPager->nOvfl;
danielk197742741be2005-01-08 12:42:39 +00003958 a[9] = pPager->nRead;
3959 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00003960 return a;
3961}
drh0f7eb612006-08-08 13:51:43 +00003962#endif
drhdd793422001-06-28 01:54:48 +00003963
drhfa86c412002-02-02 15:01:15 +00003964/*
drhac69b052004-05-12 13:30:07 +00003965** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00003966**
3967** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00003968** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00003969** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00003970*/
danielk19773b8a05f2007-03-19 17:44:26 +00003971int sqlite3PagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00003972 int rc;
drhac69b052004-05-12 13:30:07 +00003973 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00003974 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00003975 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00003976 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003977 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00003978 pPager->stmtInUse = 1;
3979 pPager->stmtSize = pPager->dbSize;
3980 return SQLITE_OK;
3981 }
drhda47d772002-12-02 04:25:19 +00003982 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00003983 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00003984 return SQLITE_OK;
3985 }
drhfa86c412002-02-02 15:01:15 +00003986 assert( pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00003987 pPager->aInStmt = sqliteMalloc( pPager->dbSize/8 + 1 );
3988 if( pPager->aInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003989 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00003990 return SQLITE_NOMEM;
3991 }
drh968af522003-02-11 14:55:40 +00003992#ifndef NDEBUG
drh054889e2005-11-30 03:20:31 +00003993 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
drhac69b052004-05-12 13:30:07 +00003994 if( rc ) goto stmt_begin_failed;
danielk197776572402004-06-25 02:38:54 +00003995 assert( pPager->stmtJSize == pPager->journalOff );
drh968af522003-02-11 14:55:40 +00003996#endif
danielk197776572402004-06-25 02:38:54 +00003997 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00003998 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00003999 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004000 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004001 if( !pPager->stmtOpen ){
danielk19773b8a05f2007-03-19 17:44:26 +00004002 rc = sqlite3PagerOpentemp(&pPager->stfd);
drhac69b052004-05-12 13:30:07 +00004003 if( rc ) goto stmt_begin_failed;
4004 pPager->stmtOpen = 1;
4005 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004006 }
drhac69b052004-05-12 13:30:07 +00004007 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004008 return SQLITE_OK;
4009
drhac69b052004-05-12 13:30:07 +00004010stmt_begin_failed:
4011 if( pPager->aInStmt ){
4012 sqliteFree(pPager->aInStmt);
4013 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004014 }
4015 return rc;
4016}
4017
4018/*
drhac69b052004-05-12 13:30:07 +00004019** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004020*/
danielk19773b8a05f2007-03-19 17:44:26 +00004021int sqlite3PagerStmtCommit(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00004022 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004023 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004024 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004025 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004026 sqlite3OsSeek(pPager->stfd, 0);
4027 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhac69b052004-05-12 13:30:07 +00004028 sqliteFree( pPager->aInStmt );
4029 pPager->aInStmt = 0;
drh663fc632002-02-02 18:49:19 +00004030 }
drhac69b052004-05-12 13:30:07 +00004031 for(pPg=pPager->pStmt; pPg; pPg=pNext){
4032 pNext = pPg->pNextStmt;
4033 assert( pPg->inStmt );
4034 pPg->inStmt = 0;
4035 pPg->pPrevStmt = pPg->pNextStmt = 0;
drhb7f91642004-10-31 02:22:47 +00004036 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004037 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4038 sqliteFree(pHist->pStmt);
4039 pHist->pStmt = 0;
4040 }
4041 }
4042 pPager->stmtNRec = 0;
4043 pPager->stmtInUse = 0;
4044 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004045 }
drhac69b052004-05-12 13:30:07 +00004046 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00004047 return SQLITE_OK;
4048}
4049
4050/*
drhac69b052004-05-12 13:30:07 +00004051** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004052*/
danielk19773b8a05f2007-03-19 17:44:26 +00004053int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004054 int rc;
drhac69b052004-05-12 13:30:07 +00004055 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004056 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004057 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004058 PgHdr *pPg;
4059 for(pPg=pPager->pStmt; pPg; pPg=pPg->pNextStmt){
4060 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4061 if( pHist->pStmt ){
4062 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
4063 sqliteFree(pHist->pStmt);
4064 pHist->pStmt = 0;
4065 }
4066 }
4067 pPager->dbSize = pPager->stmtSize;
4068 memoryTruncate(pPager);
4069 rc = SQLITE_OK;
4070 }else{
4071 rc = pager_stmt_playback(pPager);
4072 }
danielk19773b8a05f2007-03-19 17:44:26 +00004073 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004074 }else{
4075 rc = SQLITE_OK;
4076 }
drhac69b052004-05-12 13:30:07 +00004077 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00004078 return rc;
4079}
4080
drh73509ee2003-04-06 20:44:45 +00004081/*
4082** Return the full pathname of the database file.
4083*/
danielk19773b8a05f2007-03-19 17:44:26 +00004084const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004085 return pPager->zFilename;
4086}
4087
drhb20ea9d2004-02-09 01:20:36 +00004088/*
danielk19775865e3d2004-06-14 06:03:57 +00004089** Return the directory of the database file.
4090*/
danielk19773b8a05f2007-03-19 17:44:26 +00004091const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004092 return pPager->zDirectory;
4093}
4094
4095/*
4096** Return the full pathname of the journal file.
4097*/
danielk19773b8a05f2007-03-19 17:44:26 +00004098const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004099 return pPager->zJournal;
4100}
4101
4102/*
drh2c8997b2005-08-27 16:36:48 +00004103** Return true if fsync() calls are disabled for this pager. Return FALSE
4104** if fsync()s are executed normally.
4105*/
danielk19773b8a05f2007-03-19 17:44:26 +00004106int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004107 return pPager->noSync;
4108}
4109
4110/*
drhb20ea9d2004-02-09 01:20:36 +00004111** Set the codec for this pager
4112*/
danielk19773b8a05f2007-03-19 17:44:26 +00004113void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004114 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004115 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004116 void *pCodecArg
4117){
4118 pPager->xCodec = xCodec;
4119 pPager->pCodecArg = pCodecArg;
4120}
4121
danielk1977687566d2004-11-02 12:56:41 +00004122#ifndef SQLITE_OMIT_AUTOVACUUM
4123/*
4124** Move the page identified by pData to location pgno in the file.
4125**
4126** There must be no references to the current page pgno. If current page
4127** pgno is not already in the rollback journal, it is not written there by
4128** by this routine. The same applies to the page pData refers to on entry to
4129** this routine.
4130**
4131** References to the page refered to by pData remain valid. Updating any
4132** meta-data associated with page pData (i.e. data stored in the nExtra bytes
4133** allocated along with the page) is the responsibility of the caller.
4134**
danielk19775fd057a2005-03-09 13:09:43 +00004135** A transaction must be active when this routine is called. It used to be
4136** required that a statement transaction was not active, but this restriction
4137** has been removed (CREATE INDEX needs to move a page when a statement
4138** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00004139*/
danielk19773b8a05f2007-03-19 17:44:26 +00004140int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
danielk1977687566d2004-11-02 12:56:41 +00004141 PgHdr *pPgOld;
danielk1977f5fdda82004-11-03 08:44:05 +00004142 int h;
danielk197794daf7f2004-11-08 09:26:09 +00004143 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004144
danielk1977687566d2004-11-02 12:56:41 +00004145 assert( pPg->nRef>0 );
4146
drh4f0c5872007-03-26 22:05:01 +00004147 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00004148 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00004149 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004150
danielk197794daf7f2004-11-08 09:26:09 +00004151 if( pPg->needSync ){
4152 needSyncPgno = pPg->pgno;
4153 assert( pPg->inJournal );
4154 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00004155 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004156 }
4157
danielk1977687566d2004-11-02 12:56:41 +00004158 /* Unlink pPg from it's hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00004159 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00004160
danielk1977ef73ee92004-11-06 12:26:07 +00004161 /* If the cache contains a page with page-number pgno, remove it
danielk1977599fcba2004-11-08 07:13:13 +00004162 ** from it's hash chain. Also, if the PgHdr.needSync was set for
4163 ** page pgno before the 'move' operation, it needs to be retained
4164 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004165 */
danielk1977687566d2004-11-02 12:56:41 +00004166 pPgOld = pager_lookup(pPager, pgno);
4167 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00004168 assert( pPgOld->nRef==0 );
4169 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00004170 makeClean(pPgOld);
danielk1977ef73ee92004-11-06 12:26:07 +00004171 if( pPgOld->needSync ){
danielk197745901d62004-11-10 15:27:38 +00004172 assert( pPgOld->inJournal );
4173 pPg->inJournal = 1;
danielk1977ef73ee92004-11-06 12:26:07 +00004174 pPg->needSync = 1;
danielk1977ae825582004-11-23 09:06:55 +00004175 assert( pPager->needSync );
danielk1977ef73ee92004-11-06 12:26:07 +00004176 }
danielk1977687566d2004-11-02 12:56:41 +00004177 }
4178
danielk1977f5fdda82004-11-03 08:44:05 +00004179 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00004180 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00004181 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00004182 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00004183 if( pPager->aHash[h] ){
4184 assert( pPager->aHash[h]->pPrevHash==0 );
4185 pPager->aHash[h]->pPrevHash = pPg;
4186 }
4187 pPg->pNextHash = pPager->aHash[h];
4188 pPager->aHash[h] = pPg;
4189 pPg->pPrevHash = 0;
4190
drh605ad8f2006-05-03 23:34:05 +00004191 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004192 pPager->dirtyCache = 1;
4193
danielk197794daf7f2004-11-08 09:26:09 +00004194 if( needSyncPgno ){
4195 /* If needSyncPgno is non-zero, then the journal file needs to be
4196 ** sync()ed before any data is written to database file page needSyncPgno.
4197 ** Currently, no such page exists in the page-cache and the
4198 ** Pager.aInJournal bit has been set. This needs to be remedied by loading
4199 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00004200 **
danielk19773b8a05f2007-03-19 17:44:26 +00004201 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004202 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004203 */
4204 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004205 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004206 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004207 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004208 if( rc!=SQLITE_OK ) return rc;
danielk1977ae825582004-11-23 09:06:55 +00004209 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00004210 pPgHdr->needSync = 1;
4211 pPgHdr->inJournal = 1;
4212 makeDirty(pPgHdr);
4213 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004214 }
4215
danielk1977687566d2004-11-02 12:56:41 +00004216 return SQLITE_OK;
4217}
4218#endif
4219
danielk19773b8a05f2007-03-19 17:44:26 +00004220/*
4221** Return a pointer to the data for the specified page.
4222*/
4223void *sqlite3PagerGetData(DbPage *pPg){
4224 return PGHDR_TO_DATA(pPg);
4225}
4226
4227/*
4228** Return a pointer to the Pager.nExtra bytes of "extra" space
4229** allocated along with the specified page.
4230*/
4231void *sqlite3PagerGetExtra(DbPage *pPg){
4232 Pager *pPager = pPg->pPager;
4233 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
4234}
4235
danielk197741483462007-03-24 16:45:04 +00004236/*
4237** Get/set the locking-mode for this pager. Parameter eMode must be one
4238** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4239** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4240** the locking-mode is set to the value specified.
4241**
4242** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4243** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4244** locking-mode.
4245*/
4246int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004247 assert( eMode==PAGER_LOCKINGMODE_QUERY
4248 || eMode==PAGER_LOCKINGMODE_NORMAL
4249 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4250 assert( PAGER_LOCKINGMODE_QUERY<0 );
4251 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4252 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00004253 pPager->exclusiveMode = eMode;
4254 }
4255 return (int)pPager->exclusiveMode;
4256}
4257
dougcurrie81c95ef2004-06-18 23:21:47 +00004258#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00004259/*
4260** Return the current state of the file lock for the given pager.
4261** The return value is one of NO_LOCK, SHARED_LOCK, RESERVED_LOCK,
4262** PENDING_LOCK, or EXCLUSIVE_LOCK.
4263*/
danielk19773b8a05f2007-03-19 17:44:26 +00004264int sqlite3PagerLockstate(Pager *pPager){
drh054889e2005-11-30 03:20:31 +00004265 return sqlite3OsLockState(pPager->fd);
drh89ac8c12004-06-09 14:17:20 +00004266}
4267#endif
4268
danielk197793758c82005-01-21 08:13:14 +00004269#ifdef SQLITE_DEBUG
drhdd793422001-06-28 01:54:48 +00004270/*
4271** Print a listing of all referenced pages and their ref count.
4272*/
danielk19773b8a05f2007-03-19 17:44:26 +00004273void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00004274 PgHdr *pPg;
4275 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4276 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00004277 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
4278 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00004279 }
4280}
4281#endif
drh2e66f0b2005-04-28 17:18:48 +00004282
4283#endif /* SQLITE_OMIT_DISKIO */