blob: 3d2d71121d7c7c2ab32dce3893292a7b7167d597 [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**
drh4a0681e2003-02-13 01:58:20 +000021** @(#) $Id: pager.c,v 1.76 2003/02/13 01:58:21 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh829e8022002-11-06 14:08:11 +000023#include "os.h" /* Must be first to enable large file support */
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include "pager.h"
drhed7c8552001-04-11 14:29:21 +000026#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000027#include <string.h>
drhed7c8552001-04-11 14:29:21 +000028
29/*
drhdb48ee02003-01-16 13:42:43 +000030** Macros for troubleshooting. Normally turned off
31*/
32#if 0
33static Pager *mainPager = 0;
34#define SET_PAGER(X) if( mainPager==0 ) mainPager = (X)
35#define CLR_PAGER(X) if( mainPager==(X) ) mainPager = 0
36#define TRACE1(X) if( pPager==mainPager ) fprintf(stderr,X)
37#define TRACE2(X,Y) if( pPager==mainPager ) fprintf(stderr,X,Y)
38#define TRACE3(X,Y,Z) if( pPager==mainPager ) fprintf(stderr,X,Y,Z)
39#else
40#define SET_PAGER(X)
41#define CLR_PAGER(X)
42#define TRACE1(X)
43#define TRACE2(X,Y)
44#define TRACE3(X,Y,Z)
45#endif
46
47
48/*
drhed7c8552001-04-11 14:29:21 +000049** The page cache as a whole is always in one of the following
50** states:
51**
52** SQLITE_UNLOCK The page cache is not currently reading or
53** writing the database file. There is no
54** data held in memory. This is the initial
55** state.
56**
57** SQLITE_READLOCK The page cache is reading the database.
58** Writing is not permitted. There can be
59** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000060** file at the same time.
drhed7c8552001-04-11 14:29:21 +000061**
62** SQLITE_WRITELOCK The page cache is writing the database.
63** Access is exclusive. No other processes or
64** threads can be reading or writing while one
65** process is writing.
66**
drh306dc212001-05-21 13:45:10 +000067** The page cache comes up in SQLITE_UNLOCK. The first time a
68** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000069** After all pages have been released using sqlite_page_unref(),
drh306dc212001-05-21 13:45:10 +000070** the state transitions back to SQLITE_UNLOCK. The first time
drhed7c8552001-04-11 14:29:21 +000071** that sqlite_page_write() is called, the state transitions to
drh306dc212001-05-21 13:45:10 +000072** SQLITE_WRITELOCK. (Note that sqlite_page_write() can only be
73** called on an outstanding page which means that the pager must
74** be in SQLITE_READLOCK before it transitions to SQLITE_WRITELOCK.)
75** The sqlite_page_rollback() and sqlite_page_commit() functions
76** transition the state from SQLITE_WRITELOCK back to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000077*/
78#define SQLITE_UNLOCK 0
79#define SQLITE_READLOCK 1
80#define SQLITE_WRITELOCK 2
81
drhd9b02572001-04-15 00:37:09 +000082
drhed7c8552001-04-11 14:29:21 +000083/*
84** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +000085** This header is only visible to this pager module. The client
86** code that calls pager sees only the data that follows the header.
drhed7c8552001-04-11 14:29:21 +000087*/
drhd9b02572001-04-15 00:37:09 +000088typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +000089struct PgHdr {
90 Pager *pPager; /* The pager to which this page belongs */
91 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +000092 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhed7c8552001-04-11 14:29:21 +000093 int nRef; /* Number of users of this page */
drhd9b02572001-04-15 00:37:09 +000094 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
95 PgHdr *pNextAll, *pPrevAll; /* A list of all pages */
drh03eb96a2002-11-10 23:32:56 +000096 PgHdr *pNextCkpt, *pPrevCkpt; /* List of pages in the checkpoint journal */
drh193a6b42002-07-07 16:52:46 +000097 u8 inJournal; /* TRUE if has been written to journal */
98 u8 inCkpt; /* TRUE if written to the checkpoint journal */
99 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000100 u8 needSync; /* Sync journal before writing this page */
drh193a6b42002-07-07 16:52:46 +0000101 u8 alwaysRollback; /* Disable dont_rollback() for this page */
drh2554f8b2003-01-22 01:26:44 +0000102 PgHdr *pDirty; /* Dirty pages sorted by PgHdr.pgno */
drh69688d52001-04-14 16:38:23 +0000103 /* SQLITE_PAGE_SIZE bytes of page data follow this header */
drh973b6e32003-02-12 14:09:42 +0000104 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +0000105};
106
107/*
drh69688d52001-04-14 16:38:23 +0000108** Convert a pointer to a PgHdr into a pointer to its data
109** and back again.
drhed7c8552001-04-11 14:29:21 +0000110*/
111#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
112#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drh7e3b0a02001-04-28 16:52:40 +0000113#define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE])
drhed7c8552001-04-11 14:29:21 +0000114
115/*
drhed7c8552001-04-11 14:29:21 +0000116** How big to make the hash table used for locating in-memory pages
drh836faa42003-01-11 13:30:57 +0000117** by page number.
drhed7c8552001-04-11 14:29:21 +0000118*/
drh836faa42003-01-11 13:30:57 +0000119#define N_PG_HASH 2048
120
121/*
122** Hash a page number
123*/
124#define pager_hash(PN) ((PN)&(N_PG_HASH-1))
drhed7c8552001-04-11 14:29:21 +0000125
126/*
127** A open page cache is an instance of the following structure.
128*/
129struct Pager {
130 char *zFilename; /* Name of the database file */
131 char *zJournal; /* Name of the journal file */
drh8cfbf082001-09-19 13:22:39 +0000132 OsFile fd, jfd; /* File descriptors for database and journal */
drhfa86c412002-02-02 15:01:15 +0000133 OsFile cpfd; /* File descriptor for the checkpoint journal */
drhed7c8552001-04-11 14:29:21 +0000134 int dbSize; /* Number of pages in the file */
drh69688d52001-04-14 16:38:23 +0000135 int origDbSize; /* dbSize before the current change */
drh28be87c2002-11-05 23:03:02 +0000136 int ckptSize; /* Size of database (in pages) at ckpt_begin() */
137 off_t ckptJSize; /* Size of journal at ckpt_begin() */
drh968af522003-02-11 14:55:40 +0000138 int nRec; /* Number of pages written to the journal */
139 u32 cksumInit; /* Quasi-random value added to every checksum */
drh9bd47a92003-01-07 14:46:08 +0000140 int ckptNRec; /* Number of records in the checkpoint journal */
drh7e3b0a02001-04-28 16:52:40 +0000141 int nExtra; /* Add this many bytes to each in-memory page */
drh72f82862001-05-24 21:06:34 +0000142 void (*xDestructor)(void*); /* Call this routine when freeing pages */
drhed7c8552001-04-11 14:29:21 +0000143 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000144 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000145 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000146 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
drh603240c2002-03-05 01:11:12 +0000147 u8 journalOpen; /* True if journal file descriptors is valid */
drhdb48ee02003-01-16 13:42:43 +0000148 u8 journalStarted; /* True if initial magic of journal is synced */
drhda47d772002-12-02 04:25:19 +0000149 u8 useJournal; /* Do not use a rollback journal on this file */
drh603240c2002-03-05 01:11:12 +0000150 u8 ckptOpen; /* True if the checkpoint journal is open */
drh0f892532002-05-30 12:27:03 +0000151 u8 ckptInUse; /* True we are in a checkpoint */
drhda47d772002-12-02 04:25:19 +0000152 u8 ckptAutoopen; /* Open ckpt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000153 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000154 u8 fullSync; /* Do extra syncs of the journal for robustness */
drh603240c2002-03-05 01:11:12 +0000155 u8 state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */
156 u8 errMask; /* One of several kinds of errors */
157 u8 tempFile; /* zFilename is a temporary file */
158 u8 readOnly; /* True for a read-only database */
159 u8 needSync; /* True if an fsync() is needed on the journal */
drha1680452002-04-18 01:56:57 +0000160 u8 dirtyFile; /* True if database file has changed in any way */
drh193a6b42002-07-07 16:52:46 +0000161 u8 alwaysRollback; /* Disable dont_rollback() for all pages */
drh603240c2002-03-05 01:11:12 +0000162 u8 *aInJournal; /* One bit for each page in the database file */
163 u8 *aInCkpt; /* One bit for each page in the database */
drhed7c8552001-04-11 14:29:21 +0000164 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000165 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000166 PgHdr *pAll; /* List of all pages */
drh03eb96a2002-11-10 23:32:56 +0000167 PgHdr *pCkpt; /* List of pages in the checkpoint journal */
drhed7c8552001-04-11 14:29:21 +0000168 PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */
drhd9b02572001-04-15 00:37:09 +0000169};
170
171/*
172** These are bits that can be set in Pager.errMask.
173*/
174#define PAGER_ERR_FULL 0x01 /* a write() failed */
175#define PAGER_ERR_MEM 0x02 /* malloc() failed */
176#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
177#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
drh81a20f22001-10-12 17:30:04 +0000178#define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */
drhd9b02572001-04-15 00:37:09 +0000179
180/*
181** The journal file contains page records in the following
182** format.
drh968af522003-02-11 14:55:40 +0000183**
184** Actually, this structure is the complete page record for pager
185** formats less than 3. Beginning with format 3, this record is surrounded
186** by two checksums.
drhd9b02572001-04-15 00:37:09 +0000187*/
188typedef struct PageRecord PageRecord;
189struct PageRecord {
190 Pgno pgno; /* The page number */
191 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
192};
193
194/*
drh5e00f6c2001-09-13 13:46:56 +0000195** Journal files begin with the following magic string. The data
196** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000197**
drh968af522003-02-11 14:55:40 +0000198** There are three journal formats (so far). The 1st journal format writes
199** 32-bit integers in the byte-order of the host machine. New
200** formats writes integers as big-endian. All new journals use the
drh94f33312002-08-12 12:29:56 +0000201** new format, but we have to be able to read an older journal in order
drh968af522003-02-11 14:55:40 +0000202** to rollback journals created by older versions of the library.
203**
204** The 3rd journal format (added for 2.8.0) adds additional sanity
205** checking information to the journal. If the power fails while the
206** journal is being written, semi-random garbage data might appear in
207** the journal file after power is restored. If an attempt is then made
208** to roll the journal back, the database could be corrupted. The additional
209** sanity checking data is an attempt to discover the garbage in the
210** journal and ignore it.
211**
212** The sanity checking information for the 3rd journal format consists
213** of a 32-bit checksum on each page of data. The checksum covers both
214** the page number and the SQLITE_PAGE_SIZE bytes of data for the page.
215** This cksum is initialized to a 32-bit random value that appears in the
216** journal file right after the header. The random initializer is important,
217** because garbage data that appears at the end of a journal is likely
218** data that was once in other files that have now been deleted. If the
219** garbage data came from an obsolete journal file, the checksums might
220** be correct. But by initializing the checksum to random value which
221** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000222*/
drh968af522003-02-11 14:55:40 +0000223static const unsigned char aJournalMagic1[] = {
drhd9b02572001-04-15 00:37:09 +0000224 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000225};
drh968af522003-02-11 14:55:40 +0000226static const unsigned char aJournalMagic2[] = {
drh94f33312002-08-12 12:29:56 +0000227 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5,
228};
drh968af522003-02-11 14:55:40 +0000229static const unsigned char aJournalMagic3[] = {
230 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd6,
231};
232#define JOURNAL_FORMAT_1 1
233#define JOURNAL_FORMAT_2 2
234#define JOURNAL_FORMAT_3 3
drh94f33312002-08-12 12:29:56 +0000235
236/*
drh968af522003-02-11 14:55:40 +0000237** The following integer determines what format to use when creating
238** new primary journal files. By default we always use format 3.
239** When testing, we can set this value to older journal formats in order to
240** make sure that newer versions of the library are able to rollback older
241** journal files.
242**
243** Note that checkpoint journals always use format 2 and omit the header.
drh94f33312002-08-12 12:29:56 +0000244*/
245#ifdef SQLITE_TEST
drh968af522003-02-11 14:55:40 +0000246int journal_format = 3;
drh74587e52002-08-13 00:01:16 +0000247#else
drh968af522003-02-11 14:55:40 +0000248# define journal_format 3
drh94f33312002-08-12 12:29:56 +0000249#endif
drhed7c8552001-04-11 14:29:21 +0000250
251/*
drh968af522003-02-11 14:55:40 +0000252** The size of the header and of each page in the journal varies according
253** to which journal format is being used. The following macros figure out
254** the sizes based on format numbers.
255*/
256#define JOURNAL_HDR_SZ(X) \
257 (sizeof(aJournalMagic1) + sizeof(Pgno) + ((X)>=3)*2*sizeof(u32))
258#define JOURNAL_PG_SZ(X) \
259 (SQLITE_PAGE_SIZE + sizeof(Pgno) + ((X)>=3)*sizeof(u32))
260
261/*
drhdd793422001-06-28 01:54:48 +0000262** Enable reference count tracking here:
263*/
drh74587e52002-08-13 00:01:16 +0000264#ifdef SQLITE_TEST
drh5e00f6c2001-09-13 13:46:56 +0000265 int pager_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000266 static void pager_refinfo(PgHdr *p){
267 static int cnt = 0;
268 if( !pager_refinfo_enable ) return;
269 printf(
270 "REFCNT: %4d addr=0x%08x nRef=%d\n",
271 p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
272 );
273 cnt++; /* Something to set a breakpoint on */
274 }
275# define REFINFO(X) pager_refinfo(X)
276#else
277# define REFINFO(X)
278#endif
279
280/*
drh94f33312002-08-12 12:29:56 +0000281** Read a 32-bit integer from the given file descriptor
282*/
drh968af522003-02-11 14:55:40 +0000283static int read32bits(int format, OsFile *fd, u32 *pRes){
drh94f33312002-08-12 12:29:56 +0000284 u32 res;
285 int rc;
286 rc = sqliteOsRead(fd, &res, sizeof(res));
drh968af522003-02-11 14:55:40 +0000287 if( rc==SQLITE_OK && format>JOURNAL_FORMAT_1 ){
drh94f33312002-08-12 12:29:56 +0000288 unsigned char ac[4];
289 memcpy(ac, &res, 4);
290 res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
291 }
292 *pRes = res;
293 return rc;
294}
295
296/*
297** Write a 32-bit integer into the given file descriptor. Writing
298** is always done using the new journal format.
299*/
300static int write32bits(OsFile *fd, u32 val){
301 unsigned char ac[4];
drh968af522003-02-11 14:55:40 +0000302 if( journal_format<=1 ){
drh94f33312002-08-12 12:29:56 +0000303 return sqliteOsWrite(fd, &val, 4);
304 }
drh94f33312002-08-12 12:29:56 +0000305 ac[0] = (val>>24) & 0xff;
306 ac[1] = (val>>16) & 0xff;
307 ac[2] = (val>>8) & 0xff;
308 ac[3] = val & 0xff;
309 return sqliteOsWrite(fd, ac, 4);
310}
311
drh2554f8b2003-01-22 01:26:44 +0000312/*
313** Write a 32-bit integer into a page header right before the
314** page data. This will overwrite the PgHdr.pDirty pointer.
315*/
drh968af522003-02-11 14:55:40 +0000316static void store32bits(u32 val, PgHdr *p, int offset){
drh2554f8b2003-01-22 01:26:44 +0000317 unsigned char *ac;
drh968af522003-02-11 14:55:40 +0000318 ac = &((char*)PGHDR_TO_DATA(p))[offset];
319 if( journal_format<=1 ){
drh2554f8b2003-01-22 01:26:44 +0000320 memcpy(ac, &val, 4);
321 }else{
322 ac[0] = (val>>24) & 0xff;
323 ac[1] = (val>>16) & 0xff;
324 ac[2] = (val>>8) & 0xff;
325 ac[3] = val & 0xff;
326 }
327}
328
drh94f33312002-08-12 12:29:56 +0000329
330/*
drhd9b02572001-04-15 00:37:09 +0000331** Convert the bits in the pPager->errMask into an approprate
332** return code.
333*/
334static int pager_errcode(Pager *pPager){
335 int rc = SQLITE_OK;
336 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
drh81a20f22001-10-12 17:30:04 +0000337 if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR;
drhd9b02572001-04-15 00:37:09 +0000338 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
339 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
340 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
341 return rc;
drhed7c8552001-04-11 14:29:21 +0000342}
343
344/*
drh03eb96a2002-11-10 23:32:56 +0000345** Add or remove a page from the list of all pages that are in the
346** checkpoint journal.
347**
348** The Pager keeps a separate list of pages that are currently in
349** the checkpoint journal. This helps the sqlitepager_ckpt_commit()
350** routine run MUCH faster for the common case where there are many
351** pages in memory but only a few are in the checkpoint journal.
352*/
353static void page_add_to_ckpt_list(PgHdr *pPg){
354 Pager *pPager = pPg->pPager;
355 if( pPg->inCkpt ) return;
356 assert( pPg->pPrevCkpt==0 && pPg->pNextCkpt==0 );
357 pPg->pPrevCkpt = 0;
358 if( pPager->pCkpt ){
359 pPager->pCkpt->pPrevCkpt = pPg;
360 }
361 pPg->pNextCkpt = pPager->pCkpt;
362 pPager->pCkpt = pPg;
363 pPg->inCkpt = 1;
364}
365static void page_remove_from_ckpt_list(PgHdr *pPg){
366 if( !pPg->inCkpt ) return;
367 if( pPg->pPrevCkpt ){
368 assert( pPg->pPrevCkpt->pNextCkpt==pPg );
369 pPg->pPrevCkpt->pNextCkpt = pPg->pNextCkpt;
370 }else{
371 assert( pPg->pPager->pCkpt==pPg );
372 pPg->pPager->pCkpt = pPg->pNextCkpt;
373 }
374 if( pPg->pNextCkpt ){
375 assert( pPg->pNextCkpt->pPrevCkpt==pPg );
376 pPg->pNextCkpt->pPrevCkpt = pPg->pPrevCkpt;
377 }
378 pPg->pNextCkpt = 0;
379 pPg->pPrevCkpt = 0;
380 pPg->inCkpt = 0;
381}
382
383/*
drhed7c8552001-04-11 14:29:21 +0000384** Find a page in the hash table given its page number. Return
385** a pointer to the page or NULL if not found.
386*/
drhd9b02572001-04-15 00:37:09 +0000387static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh836faa42003-01-11 13:30:57 +0000388 PgHdr *p = pPager->aHash[pager_hash(pgno)];
drhed7c8552001-04-11 14:29:21 +0000389 while( p && p->pgno!=pgno ){
390 p = p->pNextHash;
391 }
392 return p;
393}
394
395/*
396** Unlock the database and clear the in-memory cache. This routine
397** sets the state of the pager back to what it was when it was first
398** opened. Any outstanding pages are invalidated and subsequent attempts
399** to access those pages will likely result in a coredump.
400*/
drhd9b02572001-04-15 00:37:09 +0000401static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000402 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000403 for(pPg=pPager->pAll; pPg; pPg=pNext){
404 pNext = pPg->pNextAll;
405 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000406 }
407 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000408 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +0000409 pPager->pLast = 0;
410 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000411 memset(pPager->aHash, 0, sizeof(pPager->aHash));
412 pPager->nPage = 0;
drhfa86c412002-02-02 15:01:15 +0000413 if( pPager->state>=SQLITE_WRITELOCK ){
drhd9b02572001-04-15 00:37:09 +0000414 sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000415 }
drha7fcb052001-12-14 15:09:55 +0000416 sqliteOsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000417 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000418 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000419 pPager->nRef = 0;
drh8cfbf082001-09-19 13:22:39 +0000420 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000421}
422
423/*
424** When this routine is called, the pager has the journal file open and
425** a write lock on the database. This routine releases the database
426** write lock and acquires a read lock in its place. The journal file
427** is deleted and closed.
drhed7c8552001-04-11 14:29:21 +0000428*/
drhd9b02572001-04-15 00:37:09 +0000429static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000430 int rc;
drhd9b02572001-04-15 00:37:09 +0000431 PgHdr *pPg;
drhfa86c412002-02-02 15:01:15 +0000432 if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK;
drh663fc632002-02-02 18:49:19 +0000433 sqlitepager_ckpt_commit(pPager);
drh0f892532002-05-30 12:27:03 +0000434 if( pPager->ckptOpen ){
435 sqliteOsClose(&pPager->cpfd);
436 pPager->ckptOpen = 0;
437 }
drhda47d772002-12-02 04:25:19 +0000438 if( pPager->journalOpen ){
439 sqliteOsClose(&pPager->jfd);
440 pPager->journalOpen = 0;
441 sqliteOsDelete(pPager->zJournal);
442 sqliteFree( pPager->aInJournal );
443 pPager->aInJournal = 0;
444 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
445 pPg->inJournal = 0;
446 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +0000447 pPg->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000448 }
449 }else{
450 assert( pPager->dirtyFile==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +0000451 }
drhda47d772002-12-02 04:25:19 +0000452 rc = sqliteOsReadLock(&pPager->fd);
drh8e298f92002-07-06 16:28:47 +0000453 if( rc==SQLITE_OK ){
454 pPager->state = SQLITE_READLOCK;
455 }else{
456 /* This can only happen if a process does a BEGIN, then forks and the
457 ** child process does the COMMIT. Because of the semantics of unix
458 ** file locking, the unlock will fail.
459 */
460 pPager->state = SQLITE_UNLOCK;
461 }
drhed7c8552001-04-11 14:29:21 +0000462 return rc;
463}
464
drhed7c8552001-04-11 14:29:21 +0000465/*
drh968af522003-02-11 14:55:40 +0000466** Compute and return a checksum for the page of data.
467*/
468static u32 pager_cksum(Pager *pPager, Pgno pgno, const char *aData){
469 u32 cksum = pPager->cksumInit + pgno;
drh968af522003-02-11 14:55:40 +0000470 return cksum;
471}
472
473/*
drhfa86c412002-02-02 15:01:15 +0000474** Read a single page from the journal file opened on file descriptor
475** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +0000476**
477** There are three different journal formats. The format parameter determines
478** which format is used by the journal that is played back.
drhfa86c412002-02-02 15:01:15 +0000479*/
drh968af522003-02-11 14:55:40 +0000480static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int format){
drhfa86c412002-02-02 15:01:15 +0000481 int rc;
482 PgHdr *pPg; /* An existing page in the cache */
483 PageRecord pgRec;
drh968af522003-02-11 14:55:40 +0000484 u32 cksum;
drhfa86c412002-02-02 15:01:15 +0000485
drh968af522003-02-11 14:55:40 +0000486 rc = read32bits(format, jfd, &pgRec.pgno);
487 if( rc!=SQLITE_OK ) return SQLITE_DONE;
drh94f33312002-08-12 12:29:56 +0000488 rc = sqliteOsRead(jfd, &pgRec.aData, sizeof(pgRec.aData));
drh968af522003-02-11 14:55:40 +0000489 if( rc!=SQLITE_OK ) return SQLITE_DONE;
drhfa86c412002-02-02 15:01:15 +0000490
drh968af522003-02-11 14:55:40 +0000491 /* Sanity checking on the page. This is more important that I originally
492 ** thought. If a power failure occurs while the journal is being written,
493 ** it could cause invalid data to be written into the journal. We need to
494 ** detect this invalid data (with high probability) and ignore it.
495 */
496 if( pgRec.pgno==0 ){
497 return SQLITE_DONE;
498 }
499 if( pgRec.pgno>pPager->dbSize ){
500 return SQLITE_OK;
501 }
502 if( format>=JOURNAL_FORMAT_3 ){
503 rc = read32bits(format, jfd, &cksum);
504 if( rc ) return SQLITE_DONE;
505 if( pager_cksum(pPager, pgRec.pgno, pgRec.aData)!=cksum ){
506 return SQLITE_DONE;
507 }
508 }
drhfa86c412002-02-02 15:01:15 +0000509
510 /* Playback the page. Update the in-memory copy of the page
511 ** at the same time, if there is one.
512 */
513 pPg = pager_lookup(pPager, pgRec.pgno);
drhdb48ee02003-01-16 13:42:43 +0000514 if( pPg==0 || pPg->needSync==0 ){
515 TRACE2("PLAYBACK %d\n", pgRec.pgno);
516 sqliteOsSeek(&pPager->fd, (pgRec.pgno-1)*(off_t)SQLITE_PAGE_SIZE);
517 rc = sqliteOsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
518 }
drhfa86c412002-02-02 15:01:15 +0000519 if( pPg ){
drh3a840692003-01-29 22:58:26 +0000520 if( pPg->nRef==0 ||
521 memcmp(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE)==0
522 ){
523 /* Do not update the data on this page if the page is in use
524 ** and the page has never been modified. This avoids resetting
525 ** the "extra" data. That in turn avoids invalidating BTree cursors
526 ** in trees that have never been modified. The end result is that
527 ** you can have a SELECT going on in one table and ROLLBACK changes
528 ** to a different table and the SELECT is unaffected by the ROLLBACK.
529 */
530 memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
531 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
532 }
drhdb48ee02003-01-16 13:42:43 +0000533 pPg->dirty = 0;
534 pPg->needSync = 0;
drhfa86c412002-02-02 15:01:15 +0000535 }
536 return rc;
537}
538
539/*
drhed7c8552001-04-11 14:29:21 +0000540** Playback the journal and thus restore the database file to
541** the state it was in before we started making changes.
542**
drhd9b02572001-04-15 00:37:09 +0000543** The journal file format is as follows: There is an initial
544** file-type string for sanity checking. Then there is a single
545** Pgno number which is the number of pages in the database before
546** changes were made. The database is truncated to this size.
drh306dc212001-05-21 13:45:10 +0000547** Next come zero or more page records where each page record
548** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See
549** the PageRecord structure for details.
drhed7c8552001-04-11 14:29:21 +0000550**
drhd9b02572001-04-15 00:37:09 +0000551** If the file opened as the journal file is not a well-formed
552** journal file (as determined by looking at the magic number
553** at the beginning) then this routine returns SQLITE_PROTOCOL.
554** If any other errors occur during playback, the database will
555** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in
556** pPager->errMask and SQLITE_CORRUPT is returned. If it all
557** works, then this routine returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000558*/
drhd9b02572001-04-15 00:37:09 +0000559static int pager_playback(Pager *pPager){
drh968af522003-02-11 14:55:40 +0000560 off_t szJ; /* Size of the journal file in bytes */
561 int nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +0000562 int i; /* Loop counter */
563 Pgno mxPg = 0; /* Size of the original file in pages */
drh968af522003-02-11 14:55:40 +0000564 int format; /* Format of the journal file. */
565 unsigned char aMagic[sizeof(aJournalMagic1)];
drhed7c8552001-04-11 14:29:21 +0000566 int rc;
567
drhc3a64ba2001-11-22 00:01:27 +0000568 /* Figure out how many records are in the journal. Abort early if
569 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +0000570 */
drh8cfbf082001-09-19 13:22:39 +0000571 assert( pPager->journalOpen );
drha7fcb052001-12-14 15:09:55 +0000572 sqliteOsSeek(&pPager->jfd, 0);
drh968af522003-02-11 14:55:40 +0000573 rc = sqliteOsFileSize(&pPager->jfd, &szJ);
drhc3a64ba2001-11-22 00:01:27 +0000574 if( rc!=SQLITE_OK ){
575 goto end_playback;
576 }
drh968af522003-02-11 14:55:40 +0000577 if( szJ < sizeof(aMagic)+sizeof(Pgno) ){
drhc3a64ba2001-11-22 00:01:27 +0000578 goto end_playback;
579 }
580
581 /* Read the beginning of the journal and truncate the
582 ** database file back to its original size.
583 */
drha7fcb052001-12-14 15:09:55 +0000584 rc = sqliteOsRead(&pPager->jfd, aMagic, sizeof(aMagic));
drh94f33312002-08-12 12:29:56 +0000585 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000586 rc = SQLITE_PROTOCOL;
587 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000588 }
drh968af522003-02-11 14:55:40 +0000589 if( memcmp(aMagic, aJournalMagic3, sizeof(aMagic))==0 ){
590 format = JOURNAL_FORMAT_3;
591 }else if( memcmp(aMagic, aJournalMagic2, sizeof(aMagic))==0 ){
592 format = JOURNAL_FORMAT_2;
593 }else if( memcmp(aMagic, aJournalMagic1, sizeof(aMagic))==0 ){
594 format = JOURNAL_FORMAT_1;
drh94f33312002-08-12 12:29:56 +0000595 }else{
596 rc = SQLITE_PROTOCOL;
597 goto end_playback;
598 }
drh968af522003-02-11 14:55:40 +0000599 if( format>=JOURNAL_FORMAT_3 ){
600 rc = read32bits(format, &pPager->jfd, &nRec);
601 if( rc ) goto end_playback;
602 rc = read32bits(format, &pPager->jfd, &pPager->cksumInit);
603 if( rc ) goto end_playback;
604 if( nRec==0xffffffff ){
605 nRec = (szJ - JOURNAL_HDR_SZ(3))/JOURNAL_PG_SZ(3);
606 }
607 }else{
drhd8d66e82003-02-12 02:10:15 +0000608 nRec = (szJ - JOURNAL_HDR_SZ(2))/JOURNAL_PG_SZ(2);
609 assert( nRec*JOURNAL_PG_SZ(2)+JOURNAL_HDR_SZ(2)==szJ );
drh968af522003-02-11 14:55:40 +0000610 }
611 rc = read32bits(format, &pPager->jfd, &mxPg);
drhd9b02572001-04-15 00:37:09 +0000612 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000613 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000614 }
drhd8d66e82003-02-12 02:10:15 +0000615 assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg );
drh28be87c2002-11-05 23:03:02 +0000616 rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg);
drh81a20f22001-10-12 17:30:04 +0000617 if( rc!=SQLITE_OK ){
618 goto end_playback;
619 }
drhd9b02572001-04-15 00:37:09 +0000620 pPager->dbSize = mxPg;
621
drhfa86c412002-02-02 15:01:15 +0000622 /* Copy original pages out of the journal and back into the database file.
drhed7c8552001-04-11 14:29:21 +0000623 */
drh968af522003-02-11 14:55:40 +0000624 for(i=0; i<nRec; i++){
625 rc = pager_playback_one_page(pPager, &pPager->jfd, format);
626 if( rc!=SQLITE_OK ){
627 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +0000628 rc = SQLITE_OK;
629 }
630 break;
631 }
drhed7c8552001-04-11 14:29:21 +0000632 }
drh81a20f22001-10-12 17:30:04 +0000633
drh4a0681e2003-02-13 01:58:20 +0000634 /* Pages that have been written to the journal but never synced
635 ** where not restored by the loop above. We have to restore those
636 ** pages by reading the back from the original database.
drhdb48ee02003-01-16 13:42:43 +0000637 */
638 if( rc==SQLITE_OK ){
639 PgHdr *pPg;
640 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3a840692003-01-29 22:58:26 +0000641 char zBuf[SQLITE_PAGE_SIZE];
drh4a0681e2003-02-13 01:58:20 +0000642 if( !pPg->dirty ) continue;
drhdb48ee02003-01-16 13:42:43 +0000643 if( (int)pPg->pgno <= pPager->origDbSize ){
644 sqliteOsSeek(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)(pPg->pgno-1));
drh3a840692003-01-29 22:58:26 +0000645 rc = sqliteOsRead(&pPager->fd, zBuf, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000646 if( rc ) break;
647 }else{
drh3a840692003-01-29 22:58:26 +0000648 memset(zBuf, 0, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000649 }
drh3a840692003-01-29 22:58:26 +0000650 if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE) ){
651 memcpy(PGHDR_TO_DATA(pPg), zBuf, SQLITE_PAGE_SIZE);
652 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
653 }
drhdb48ee02003-01-16 13:42:43 +0000654 pPg->needSync = 0;
655 pPg->dirty = 0;
656 }
657 }
drh4a0681e2003-02-13 01:58:20 +0000658
659end_playback:
drhd9b02572001-04-15 00:37:09 +0000660 if( rc!=SQLITE_OK ){
661 pager_unwritelock(pPager);
662 pPager->errMask |= PAGER_ERR_CORRUPT;
663 rc = SQLITE_CORRUPT;
664 }else{
665 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000666 }
drhd9b02572001-04-15 00:37:09 +0000667 return rc;
drhed7c8552001-04-11 14:29:21 +0000668}
669
670/*
drhfa86c412002-02-02 15:01:15 +0000671** Playback the checkpoint journal.
672**
673** This is similar to playing back the transaction journal but with
674** a few extra twists.
675**
drh663fc632002-02-02 18:49:19 +0000676** (1) The number of pages in the database file at the start of
677** the checkpoint is stored in pPager->ckptSize, not in the
678** journal file itself.
drhfa86c412002-02-02 15:01:15 +0000679**
680** (2) In addition to playing back the checkpoint journal, also
681** playback all pages of the transaction journal beginning
682** at offset pPager->ckptJSize.
683*/
684static int pager_ckpt_playback(Pager *pPager){
drh968af522003-02-11 14:55:40 +0000685 off_t szJ; /* Size of the full journal */
686 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +0000687 int i; /* Loop counter */
688 int rc;
689
690 /* Truncate the database back to its original size.
691 */
drh28be87c2002-11-05 23:03:02 +0000692 rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)pPager->ckptSize);
drhfa86c412002-02-02 15:01:15 +0000693 pPager->dbSize = pPager->ckptSize;
694
695 /* Figure out how many records are in the checkpoint journal.
696 */
drh0f892532002-05-30 12:27:03 +0000697 assert( pPager->ckptInUse && pPager->journalOpen );
drhfa86c412002-02-02 15:01:15 +0000698 sqliteOsSeek(&pPager->cpfd, 0);
drh9bd47a92003-01-07 14:46:08 +0000699 nRec = pPager->ckptNRec;
drhfa86c412002-02-02 15:01:15 +0000700
701 /* Copy original pages out of the checkpoint journal and back into the
drh968af522003-02-11 14:55:40 +0000702 ** database file. Note that the checkpoint journal always uses format
703 ** 2 instead of format 3 since it does not need to be concerned with
704 ** power failures corrupting the journal and can thus omit the checksums.
drhfa86c412002-02-02 15:01:15 +0000705 */
706 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000707 rc = pager_playback_one_page(pPager, &pPager->cpfd, 2);
708 assert( rc!=SQLITE_DONE );
drhfa86c412002-02-02 15:01:15 +0000709 if( rc!=SQLITE_OK ) goto end_ckpt_playback;
710 }
711
712 /* Figure out how many pages need to be copied out of the transaction
713 ** journal.
714 */
715 rc = sqliteOsSeek(&pPager->jfd, pPager->ckptJSize);
716 if( rc!=SQLITE_OK ){
717 goto end_ckpt_playback;
718 }
drh968af522003-02-11 14:55:40 +0000719 rc = sqliteOsFileSize(&pPager->jfd, &szJ);
drhfa86c412002-02-02 15:01:15 +0000720 if( rc!=SQLITE_OK ){
721 goto end_ckpt_playback;
722 }
drh968af522003-02-11 14:55:40 +0000723 nRec = (szJ - pPager->ckptJSize)/JOURNAL_PG_SZ(journal_format);
drhfa86c412002-02-02 15:01:15 +0000724 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000725 rc = pager_playback_one_page(pPager, &pPager->jfd, journal_format);
726 if( rc!=SQLITE_OK ){
727 assert( rc!=SQLITE_DONE );
728 goto end_ckpt_playback;
729 }
drhfa86c412002-02-02 15:01:15 +0000730 }
731
drhfa86c412002-02-02 15:01:15 +0000732end_ckpt_playback:
drhfa86c412002-02-02 15:01:15 +0000733 if( rc!=SQLITE_OK ){
drhfa86c412002-02-02 15:01:15 +0000734 pPager->errMask |= PAGER_ERR_CORRUPT;
735 rc = SQLITE_CORRUPT;
drhfa86c412002-02-02 15:01:15 +0000736 }
737 return rc;
738}
739
740/*
drhf57b14a2001-09-14 18:54:08 +0000741** Change the maximum number of in-memory pages that are allowed.
drhcd61c282002-03-06 22:01:34 +0000742**
743** The maximum number is the absolute value of the mxPage parameter.
744** If mxPage is negative, the noSync flag is also set. noSync bypasses
745** calls to sqliteOsSync(). The pager runs much faster with noSync on,
746** but if the operating system crashes or there is an abrupt power
747** failure, the database file might be left in an inconsistent and
748** unrepairable state.
drhf57b14a2001-09-14 18:54:08 +0000749*/
750void sqlitepager_set_cachesize(Pager *pPager, int mxPage){
drh603240c2002-03-05 01:11:12 +0000751 if( mxPage>=0 ){
drha1680452002-04-18 01:56:57 +0000752 pPager->noSync = pPager->tempFile;
drh603240c2002-03-05 01:11:12 +0000753 }else{
754 pPager->noSync = 1;
755 mxPage = -mxPage;
756 }
drhf57b14a2001-09-14 18:54:08 +0000757 if( mxPage>10 ){
758 pPager->mxPage = mxPage;
759 }
760}
761
762/*
drh973b6e32003-02-12 14:09:42 +0000763** Adjust the robustness of the database to damage due to OS crashes
764** or power failures by changing the number of syncs()s when writing
765** the rollback journal. There are three levels:
766**
767** OFF sqliteOsSync() is never called. This is the default
768** for temporary and transient files.
769**
770** NORMAL The journal is synced once before writes begin on the
771** database. This is normally adequate protection, but
772** it is theoretically possible, though very unlikely,
773** that an inopertune power failure could leave the journal
774** in a state which would cause damage to the database
775** when it is rolled back.
776**
777** FULL The journal is synced twice before writes begin on the
778** database (with some additional information being written
779** in between the two syncs. If we assume that writing a
780** single disk sector is atomic, then this mode provides
781** assurance that the journal will not be corrupted to the
782** point of causing damage to the database during rollback.
783**
784** Numeric values associated with these states are OFF==1, NORMAL=2,
785** and FULL=3.
786*/
787void sqlitepager_set_safety_level(Pager *pPager, int level){
788 pPager->noSync = level==1 || pPager->tempFile;
789 pPager->fullSync = level==3 && !pPager->tempFile;
790}
791
792/*
drhfa86c412002-02-02 15:01:15 +0000793** Open a temporary file. Write the name of the file into zName
794** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write
795** the file descriptor into *fd. Return SQLITE_OK on success or some
796** other error code if we fail.
797**
798** The OS will automatically delete the temporary file when it is
799** closed.
800*/
801static int sqlitepager_opentemp(char *zFile, OsFile *fd){
802 int cnt = 8;
803 int rc;
804 do{
805 cnt--;
806 sqliteOsTempFileName(zFile);
807 rc = sqliteOsOpenExclusive(zFile, fd, 1);
808 }while( cnt>0 && rc!=SQLITE_OK );
809 return rc;
810}
811
812/*
drhed7c8552001-04-11 14:29:21 +0000813** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +0000814** The file to be cached need not exist. The file is not locked until
drhd9b02572001-04-15 00:37:09 +0000815** the first call to sqlitepager_get() and is only held open until the
816** last page is released using sqlitepager_unref().
drh382c0242001-10-06 16:33:02 +0000817**
drh6446c4d2001-12-15 14:22:18 +0000818** If zFilename is NULL then a randomly-named temporary file is created
819** and used as the file to be cached. The file will be deleted
820** automatically when it is closed.
drhed7c8552001-04-11 14:29:21 +0000821*/
drh7e3b0a02001-04-28 16:52:40 +0000822int sqlitepager_open(
823 Pager **ppPager, /* Return the Pager structure here */
824 const char *zFilename, /* Name of the database file to open */
825 int mxPage, /* Max number of in-memory cache pages */
drhda47d772002-12-02 04:25:19 +0000826 int nExtra, /* Extra bytes append to each in-memory page */
827 int useJournal /* TRUE to use a rollback journal on this file */
drh7e3b0a02001-04-28 16:52:40 +0000828){
drhed7c8552001-04-11 14:29:21 +0000829 Pager *pPager;
drh3e7a6092002-12-07 21:45:14 +0000830 char *zFullPathname;
drhed7c8552001-04-11 14:29:21 +0000831 int nameLen;
drh8cfbf082001-09-19 13:22:39 +0000832 OsFile fd;
833 int rc;
drh5e00f6c2001-09-13 13:46:56 +0000834 int tempFile;
835 int readOnly = 0;
drh8cfbf082001-09-19 13:22:39 +0000836 char zTemp[SQLITE_TEMPNAME_SIZE];
drhed7c8552001-04-11 14:29:21 +0000837
drhd9b02572001-04-15 00:37:09 +0000838 *ppPager = 0;
839 if( sqlite_malloc_failed ){
840 return SQLITE_NOMEM;
841 }
drh5e00f6c2001-09-13 13:46:56 +0000842 if( zFilename ){
drh3e7a6092002-12-07 21:45:14 +0000843 zFullPathname = sqliteOsFullPathname(zFilename);
844 rc = sqliteOsOpenReadWrite(zFullPathname, &fd, &readOnly);
drh5e00f6c2001-09-13 13:46:56 +0000845 tempFile = 0;
846 }else{
drhfa86c412002-02-02 15:01:15 +0000847 rc = sqlitepager_opentemp(zTemp, &fd);
drh5e00f6c2001-09-13 13:46:56 +0000848 zFilename = zTemp;
drh3e7a6092002-12-07 21:45:14 +0000849 zFullPathname = sqliteOsFullPathname(zFilename);
drh5e00f6c2001-09-13 13:46:56 +0000850 tempFile = 1;
851 }
drh3e7a6092002-12-07 21:45:14 +0000852 if( sqlite_malloc_failed ){
853 return SQLITE_NOMEM;
854 }
drh8cfbf082001-09-19 13:22:39 +0000855 if( rc!=SQLITE_OK ){
drh3e7a6092002-12-07 21:45:14 +0000856 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000857 return SQLITE_CANTOPEN;
858 }
drh3e7a6092002-12-07 21:45:14 +0000859 nameLen = strlen(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000860 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 );
drhd9b02572001-04-15 00:37:09 +0000861 if( pPager==0 ){
drha7fcb052001-12-14 15:09:55 +0000862 sqliteOsClose(&fd);
drh3e7a6092002-12-07 21:45:14 +0000863 sqliteFree(zFullPathname);
drhd9b02572001-04-15 00:37:09 +0000864 return SQLITE_NOMEM;
865 }
drhdb48ee02003-01-16 13:42:43 +0000866 SET_PAGER(pPager);
drhed7c8552001-04-11 14:29:21 +0000867 pPager->zFilename = (char*)&pPager[1];
868 pPager->zJournal = &pPager->zFilename[nameLen+1];
drh3e7a6092002-12-07 21:45:14 +0000869 strcpy(pPager->zFilename, zFullPathname);
870 strcpy(pPager->zJournal, zFullPathname);
871 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000872 strcpy(&pPager->zJournal[nameLen], "-journal");
873 pPager->fd = fd;
drh8cfbf082001-09-19 13:22:39 +0000874 pPager->journalOpen = 0;
drhda47d772002-12-02 04:25:19 +0000875 pPager->useJournal = useJournal;
drhfa86c412002-02-02 15:01:15 +0000876 pPager->ckptOpen = 0;
drh0f892532002-05-30 12:27:03 +0000877 pPager->ckptInUse = 0;
drhed7c8552001-04-11 14:29:21 +0000878 pPager->nRef = 0;
879 pPager->dbSize = -1;
drhfa86c412002-02-02 15:01:15 +0000880 pPager->ckptSize = 0;
881 pPager->ckptJSize = 0;
drhed7c8552001-04-11 14:29:21 +0000882 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +0000883 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +0000884 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000885 pPager->errMask = 0;
drh5e00f6c2001-09-13 13:46:56 +0000886 pPager->tempFile = tempFile;
887 pPager->readOnly = readOnly;
drhf57b14a2001-09-14 18:54:08 +0000888 pPager->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000889 pPager->noSync = pPager->tempFile || !useJournal;
drhed7c8552001-04-11 14:29:21 +0000890 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000891 pPager->pFirstSynced = 0;
drhed7c8552001-04-11 14:29:21 +0000892 pPager->pLast = 0;
drh7c717f72001-06-24 20:39:41 +0000893 pPager->nExtra = nExtra;
drhed7c8552001-04-11 14:29:21 +0000894 memset(pPager->aHash, 0, sizeof(pPager->aHash));
895 *ppPager = pPager;
896 return SQLITE_OK;
897}
898
899/*
drh72f82862001-05-24 21:06:34 +0000900** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +0000901** when the reference count on each page reaches zero. The destructor can
902** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +0000903**
904** The destructor is not called as a result sqlitepager_close().
905** Destructors are only called by sqlitepager_unref().
906*/
907void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){
908 pPager->xDestructor = xDesc;
909}
910
911/*
drh5e00f6c2001-09-13 13:46:56 +0000912** Return the total number of pages in the disk file associated with
913** pPager.
drhed7c8552001-04-11 14:29:21 +0000914*/
drhd9b02572001-04-15 00:37:09 +0000915int sqlitepager_pagecount(Pager *pPager){
drh28be87c2002-11-05 23:03:02 +0000916 off_t n;
drhd9b02572001-04-15 00:37:09 +0000917 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +0000918 if( pPager->dbSize>=0 ){
919 return pPager->dbSize;
920 }
drha7fcb052001-12-14 15:09:55 +0000921 if( sqliteOsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000922 pPager->errMask |= PAGER_ERR_DISK;
drh8cfbf082001-09-19 13:22:39 +0000923 return 0;
drhed7c8552001-04-11 14:29:21 +0000924 }
drh8cfbf082001-09-19 13:22:39 +0000925 n /= SQLITE_PAGE_SIZE;
drhd9b02572001-04-15 00:37:09 +0000926 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +0000927 pPager->dbSize = n;
928 }
929 return n;
930}
931
932/*
933** Shutdown the page cache. Free all memory and close all files.
934**
935** If a transaction was in progress when this routine is called, that
936** transaction is rolled back. All outstanding pages are invalidated
937** and their memory is freed. Any attempt to use a page associated
938** with this page cache after this function returns will likely
939** result in a coredump.
940*/
drhd9b02572001-04-15 00:37:09 +0000941int sqlitepager_close(Pager *pPager){
942 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +0000943 switch( pPager->state ){
944 case SQLITE_WRITELOCK: {
drhd9b02572001-04-15 00:37:09 +0000945 sqlitepager_rollback(pPager);
drha7fcb052001-12-14 15:09:55 +0000946 sqliteOsUnlock(&pPager->fd);
drh8cfbf082001-09-19 13:22:39 +0000947 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000948 break;
949 }
950 case SQLITE_READLOCK: {
drha7fcb052001-12-14 15:09:55 +0000951 sqliteOsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000952 break;
953 }
954 default: {
955 /* Do nothing */
956 break;
957 }
958 }
drhd9b02572001-04-15 00:37:09 +0000959 for(pPg=pPager->pAll; pPg; pPg=pNext){
960 pNext = pPg->pNextAll;
961 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000962 }
drha7fcb052001-12-14 15:09:55 +0000963 sqliteOsClose(&pPager->fd);
drh8cfbf082001-09-19 13:22:39 +0000964 assert( pPager->journalOpen==0 );
drh0f892532002-05-30 12:27:03 +0000965 /* Temp files are automatically deleted by the OS
966 ** if( pPager->tempFile ){
967 ** sqliteOsDelete(pPager->zFilename);
968 ** }
969 */
drhdb48ee02003-01-16 13:42:43 +0000970 CLR_PAGER(pPager);
drhed7c8552001-04-11 14:29:21 +0000971 sqliteFree(pPager);
972 return SQLITE_OK;
973}
974
975/*
drh5e00f6c2001-09-13 13:46:56 +0000976** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +0000977*/
drhd9b02572001-04-15 00:37:09 +0000978Pgno sqlitepager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +0000979 PgHdr *p = DATA_TO_PGHDR(pData);
980 return p->pgno;
981}
982
983/*
drh7e3b0a02001-04-28 16:52:40 +0000984** Increment the reference count for a page. If the page is
985** currently on the freelist (the reference count is zero) then
986** remove it from the freelist.
987*/
drh836faa42003-01-11 13:30:57 +0000988#define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
989static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +0000990 if( pPg->nRef==0 ){
991 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +0000992 if( pPg==pPg->pPager->pFirstSynced ){
993 PgHdr *p = pPg->pNextFree;
994 while( p && p->needSync ){ p = p->pNextFree; }
995 pPg->pPager->pFirstSynced = p;
996 }
drh7e3b0a02001-04-28 16:52:40 +0000997 if( pPg->pPrevFree ){
998 pPg->pPrevFree->pNextFree = pPg->pNextFree;
999 }else{
1000 pPg->pPager->pFirst = pPg->pNextFree;
1001 }
1002 if( pPg->pNextFree ){
1003 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1004 }else{
1005 pPg->pPager->pLast = pPg->pPrevFree;
1006 }
1007 pPg->pPager->nRef++;
1008 }
1009 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00001010 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00001011}
1012
1013/*
1014** Increment the reference count for a page. The input pointer is
1015** a reference to the page data.
1016*/
1017int sqlitepager_ref(void *pData){
1018 PgHdr *pPg = DATA_TO_PGHDR(pData);
1019 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +00001020 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001021}
1022
1023/*
drhb19a2bc2001-09-16 00:13:26 +00001024** Sync the journal and then write all free dirty pages to the database
1025** file.
1026**
1027** Writing all free dirty pages to the database after the sync is a
1028** non-obvious optimization. fsync() is an expensive operation so we
drhaaab5722002-02-19 13:39:21 +00001029** want to minimize the number ot times it is called. After an fsync() call,
drh6446c4d2001-12-15 14:22:18 +00001030** we are free to write dirty pages back to the database. It is best
1031** to go ahead and write as many dirty pages as possible to minimize
1032** the risk of having to do another fsync() later on. Writing dirty
1033** free pages in this way was observed to make database operations go
1034** up to 10 times faster.
drhfa86c412002-02-02 15:01:15 +00001035**
1036** If we are writing to temporary database, there is no need to preserve
1037** the integrity of the journal file, so we can save time and skip the
1038** fsync().
drh50e5dad2001-09-15 00:57:28 +00001039*/
1040static int syncAllPages(Pager *pPager){
1041 PgHdr *pPg;
1042 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00001043
1044 /* Sync the journal before modifying the main database
1045 ** (assuming there is a journal and it needs to be synced.)
1046 */
drh50e5dad2001-09-15 00:57:28 +00001047 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00001048 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00001049 assert( pPager->journalOpen );
1050 assert( !pPager->noSync );
drh968af522003-02-11 14:55:40 +00001051#ifndef NDEBUG
1052 {
drh4a0681e2003-02-13 01:58:20 +00001053 off_t hdrSz, pgSz, jSz;
drh968af522003-02-11 14:55:40 +00001054 hdrSz = JOURNAL_HDR_SZ(journal_format);
1055 pgSz = JOURNAL_PG_SZ(journal_format);
drh4a0681e2003-02-13 01:58:20 +00001056 rc = sqliteOsFileSize(&pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00001057 if( rc!=0 ) return rc;
drh4a0681e2003-02-13 01:58:20 +00001058 assert( pPager->nRec*pgSz+hdrSz==jSz );
drh968af522003-02-11 14:55:40 +00001059 }
1060#endif
drhd8d66e82003-02-12 02:10:15 +00001061 if( journal_format>=3 ){
1062 off_t szJ;
1063 if( pPager->fullSync ){
1064 TRACE1("SYNC\n");
1065 rc = sqliteOsSync(&pPager->jfd);
1066 if( rc!=0 ) return rc;
1067 }
1068 sqliteOsSeek(&pPager->jfd, sizeof(aJournalMagic1));
1069 write32bits(&pPager->jfd, pPager->nRec);
1070 szJ = JOURNAL_HDR_SZ(journal_format) +
1071 pPager->nRec*JOURNAL_PG_SZ(journal_format);
1072 sqliteOsSeek(&pPager->jfd, szJ);
drh968af522003-02-11 14:55:40 +00001073 }
drhdb48ee02003-01-16 13:42:43 +00001074 TRACE1("SYNC\n");
drhfa86c412002-02-02 15:01:15 +00001075 rc = sqliteOsSync(&pPager->jfd);
1076 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00001077 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00001078 }
drh50e5dad2001-09-15 00:57:28 +00001079 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00001080
1081 /* Erase the needSync flag from every page.
1082 */
1083 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1084 pPg->needSync = 0;
1085 }
1086 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00001087 }
drh03eb96a2002-11-10 23:32:56 +00001088
drh341eae82003-01-21 02:39:36 +00001089#ifndef NDEBUG
1090 /* If the Pager.needSync flag is clear then the PgHdr.needSync
1091 ** flag must also be clear for all pages. Verify that this
1092 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00001093 */
drh341eae82003-01-21 02:39:36 +00001094 else{
1095 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1096 assert( pPg->needSync==0 );
1097 }
1098 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00001099 }
drh341eae82003-01-21 02:39:36 +00001100#endif
drhdb48ee02003-01-16 13:42:43 +00001101
drh81a20f22001-10-12 17:30:04 +00001102 return rc;
drh50e5dad2001-09-15 00:57:28 +00001103}
1104
1105/*
drh2554f8b2003-01-22 01:26:44 +00001106** Given a list of pages (connected by the PgHdr.pDirty pointer) write
1107** every one of those pages out to the database file and mark them all
1108** as clean.
1109*/
1110static int pager_write_pagelist(PgHdr *pList){
1111 Pager *pPager;
1112 int rc;
1113
1114 if( pList==0 ) return SQLITE_OK;
1115 pPager = pList->pPager;
1116 while( pList ){
1117 assert( pList->dirty );
1118 sqliteOsSeek(&pPager->fd, (pList->pgno-1)*(off_t)SQLITE_PAGE_SIZE);
1119 rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pList), SQLITE_PAGE_SIZE);
1120 if( rc ) return rc;
1121 pList->dirty = 0;
1122 pList = pList->pDirty;
1123 }
1124 return SQLITE_OK;
1125}
1126
1127/*
1128** Collect every dirty page into a dirty list and
1129** return a pointer to the head of that list. All pages are
1130** collected even if they are still in use.
1131*/
1132static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
1133 PgHdr *p, *pList;
1134 pList = 0;
1135 for(p=pPager->pAll; p; p=p->pNextAll){
1136 if( p->dirty ){
1137 p->pDirty = pList;
1138 pList = p;
1139 }
1140 }
1141 return pList;
1142}
1143
1144/*
drhd9b02572001-04-15 00:37:09 +00001145** Acquire a page.
1146**
drh58a11682001-11-10 13:51:08 +00001147** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00001148** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00001149**
drh306dc212001-05-21 13:45:10 +00001150** A _get works for any page number greater than 0. If the database
1151** file is smaller than the requested page, then no actual disk
1152** read occurs and the memory image of the page is initialized to
1153** all zeros. The extra data appended to a page is always initialized
1154** to zeros the first time a page is loaded into memory.
1155**
drhd9b02572001-04-15 00:37:09 +00001156** The acquisition might fail for several reasons. In all cases,
1157** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00001158**
1159** See also sqlitepager_lookup(). Both this routine and _lookup() attempt
1160** to find a page in the in-memory cache first. If the page is not already
drh5e00f6c2001-09-13 13:46:56 +00001161** in memory, this routine goes to disk to read it in whereas _lookup()
drh7e3b0a02001-04-28 16:52:40 +00001162** just returns 0. This routine acquires a read-lock the first time it
1163** has to go to disk, and could also playback an old journal if necessary.
1164** Since _lookup() never goes to disk, it never has to deal with locks
1165** or journal files.
drhed7c8552001-04-11 14:29:21 +00001166*/
drhd9b02572001-04-15 00:37:09 +00001167int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +00001168 PgHdr *pPg;
drh8766c342002-11-09 00:33:15 +00001169 int rc;
drhed7c8552001-04-11 14:29:21 +00001170
drhd9b02572001-04-15 00:37:09 +00001171 /* Make sure we have not hit any critical errors.
1172 */
drh836faa42003-01-11 13:30:57 +00001173 assert( pPager!=0 );
1174 assert( pgno!=0 );
drhd9b02572001-04-15 00:37:09 +00001175 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1176 return pager_errcode(pPager);
1177 }
1178
drhed7c8552001-04-11 14:29:21 +00001179 /* If this is the first page accessed, then get a read lock
1180 ** on the database file.
1181 */
1182 if( pPager->nRef==0 ){
drh8766c342002-11-09 00:33:15 +00001183 rc = sqliteOsReadLock(&pPager->fd);
1184 if( rc!=SQLITE_OK ){
drhed7c8552001-04-11 14:29:21 +00001185 *ppPage = 0;
drh8766c342002-11-09 00:33:15 +00001186 return rc;
drhed7c8552001-04-11 14:29:21 +00001187 }
drhd9b02572001-04-15 00:37:09 +00001188 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +00001189
1190 /* If a journal file exists, try to play it back.
1191 */
drhda47d772002-12-02 04:25:19 +00001192 if( pPager->useJournal && sqliteOsFileExists(pPager->zJournal) ){
drhf57b3392001-10-08 13:22:32 +00001193 int rc, dummy;
drhed7c8552001-04-11 14:29:21 +00001194
drha7fcb052001-12-14 15:09:55 +00001195 /* Get a write lock on the database
1196 */
1197 rc = sqliteOsWriteLock(&pPager->fd);
1198 if( rc!=SQLITE_OK ){
drh8766c342002-11-09 00:33:15 +00001199 if( sqliteOsUnlock(&pPager->fd)!=SQLITE_OK ){
1200 /* This should never happen! */
1201 rc = SQLITE_INTERNAL;
1202 }
drha7fcb052001-12-14 15:09:55 +00001203 *ppPage = 0;
drh8766c342002-11-09 00:33:15 +00001204 return rc;
drha7fcb052001-12-14 15:09:55 +00001205 }
1206 pPager->state = SQLITE_WRITELOCK;
1207
drhed7c8552001-04-11 14:29:21 +00001208 /* Open the journal for exclusive access. Return SQLITE_BUSY if
drhf57b3392001-10-08 13:22:32 +00001209 ** we cannot get exclusive access to the journal file.
1210 **
1211 ** Even though we will only be reading from the journal, not writing,
1212 ** we have to open the journal for writing in order to obtain an
1213 ** exclusive access lock.
drhed7c8552001-04-11 14:29:21 +00001214 */
drhf57b3392001-10-08 13:22:32 +00001215 rc = sqliteOsOpenReadWrite(pPager->zJournal, &pPager->jfd, &dummy);
drha7fcb052001-12-14 15:09:55 +00001216 if( rc!=SQLITE_OK ){
1217 rc = sqliteOsUnlock(&pPager->fd);
1218 assert( rc==SQLITE_OK );
drhed7c8552001-04-11 14:29:21 +00001219 *ppPage = 0;
1220 return SQLITE_BUSY;
1221 }
drha7fcb052001-12-14 15:09:55 +00001222 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001223 pPager->journalStarted = 0;
drhed7c8552001-04-11 14:29:21 +00001224
1225 /* Playback and delete the journal. Drop the database write
1226 ** lock and reacquire the read lock.
1227 */
drhd9b02572001-04-15 00:37:09 +00001228 rc = pager_playback(pPager);
1229 if( rc!=SQLITE_OK ){
1230 return rc;
1231 }
drhed7c8552001-04-11 14:29:21 +00001232 }
1233 pPg = 0;
1234 }else{
1235 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +00001236 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00001237 }
1238 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001239 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +00001240 int h;
drh7e3b0a02001-04-28 16:52:40 +00001241 pPager->nMiss++;
drhed7c8552001-04-11 14:29:21 +00001242 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){
1243 /* Create a new page */
drh968af522003-02-11 14:55:40 +00001244 pPg = sqliteMallocRaw( sizeof(*pPg) + SQLITE_PAGE_SIZE
1245 + sizeof(u32) + pPager->nExtra );
drhd9b02572001-04-15 00:37:09 +00001246 if( pPg==0 ){
1247 *ppPage = 0;
1248 pager_unwritelock(pPager);
1249 pPager->errMask |= PAGER_ERR_MEM;
1250 return SQLITE_NOMEM;
1251 }
drh8c1238a2003-01-02 14:43:55 +00001252 memset(pPg, 0, sizeof(*pPg));
drhed7c8552001-04-11 14:29:21 +00001253 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +00001254 pPg->pNextAll = pPager->pAll;
1255 if( pPager->pAll ){
1256 pPager->pAll->pPrevAll = pPg;
1257 }
1258 pPg->pPrevAll = 0;
drhd79caeb2001-04-15 02:27:24 +00001259 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +00001260 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +00001261 }else{
drhdb48ee02003-01-16 13:42:43 +00001262 /* Find a page to recycle. Try to locate a page that does not
1263 ** require us to do an fsync() on the journal.
1264 */
drh341eae82003-01-21 02:39:36 +00001265 pPg = pPager->pFirstSynced;
drhb19a2bc2001-09-16 00:13:26 +00001266
drhdb48ee02003-01-16 13:42:43 +00001267 /* If we could not find a page that does not require an fsync()
1268 ** on the journal file then fsync the journal file. This is a
1269 ** very slow operation, so we work hard to avoid it. But sometimes
1270 ** it can't be helped.
drhb19a2bc2001-09-16 00:13:26 +00001271 */
drh603240c2002-03-05 01:11:12 +00001272 if( pPg==0 ){
drh50e5dad2001-09-15 00:57:28 +00001273 int rc = syncAllPages(pPager);
1274 if( rc!=0 ){
1275 sqlitepager_rollback(pPager);
1276 *ppPage = 0;
1277 return SQLITE_IOERR;
1278 }
1279 pPg = pPager->pFirst;
1280 }
drhd9b02572001-04-15 00:37:09 +00001281 assert( pPg->nRef==0 );
drhdb48ee02003-01-16 13:42:43 +00001282
1283 /* Write the page to the database file if it is dirty.
1284 */
1285 if( pPg->dirty ){
1286 assert( pPg->needSync==0 );
drh2554f8b2003-01-22 01:26:44 +00001287 pPg->pDirty = 0;
1288 rc = pager_write_pagelist( pPg );
drhdb48ee02003-01-16 13:42:43 +00001289 if( rc!=SQLITE_OK ){
1290 sqlitepager_rollback(pPager);
1291 *ppPage = 0;
1292 return SQLITE_IOERR;
1293 }
drhdb48ee02003-01-16 13:42:43 +00001294 }
drh50e5dad2001-09-15 00:57:28 +00001295 assert( pPg->dirty==0 );
drhd9b02572001-04-15 00:37:09 +00001296
drhdb48ee02003-01-16 13:42:43 +00001297 /* If the page we are recycling is marked as alwaysRollback, then
drh193a6b42002-07-07 16:52:46 +00001298 ** set the global alwaysRollback flag, thus disabling the
1299 ** sqlite_dont_rollback() optimization for the rest of this transaction.
1300 ** It is necessary to do this because the page marked alwaysRollback
1301 ** might be reloaded at a later time but at that point we won't remember
1302 ** that is was marked alwaysRollback. This means that all pages must
1303 ** be marked as alwaysRollback from here on out.
1304 */
1305 if( pPg->alwaysRollback ){
1306 pPager->alwaysRollback = 1;
1307 }
1308
drhd9b02572001-04-15 00:37:09 +00001309 /* Unlink the old page from the free list and the hash table
1310 */
drh341eae82003-01-21 02:39:36 +00001311 if( pPg==pPager->pFirstSynced ){
1312 PgHdr *p = pPg->pNextFree;
1313 while( p && p->needSync ){ p = p->pNextFree; }
1314 pPager->pFirstSynced = p;
1315 }
drh6019e162001-07-02 17:51:45 +00001316 if( pPg->pPrevFree ){
1317 pPg->pPrevFree->pNextFree = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +00001318 }else{
drh6019e162001-07-02 17:51:45 +00001319 assert( pPager->pFirst==pPg );
1320 pPager->pFirst = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +00001321 }
drh6019e162001-07-02 17:51:45 +00001322 if( pPg->pNextFree ){
1323 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1324 }else{
1325 assert( pPager->pLast==pPg );
1326 pPager->pLast = pPg->pPrevFree;
1327 }
1328 pPg->pNextFree = pPg->pPrevFree = 0;
drhed7c8552001-04-11 14:29:21 +00001329 if( pPg->pNextHash ){
1330 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
1331 }
1332 if( pPg->pPrevHash ){
1333 pPg->pPrevHash->pNextHash = pPg->pNextHash;
1334 }else{
drhd9b02572001-04-15 00:37:09 +00001335 h = pager_hash(pPg->pgno);
drhed7c8552001-04-11 14:29:21 +00001336 assert( pPager->aHash[h]==pPg );
1337 pPager->aHash[h] = pPg->pNextHash;
1338 }
drh6019e162001-07-02 17:51:45 +00001339 pPg->pNextHash = pPg->pPrevHash = 0;
drhd9b02572001-04-15 00:37:09 +00001340 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +00001341 }
1342 pPg->pgno = pgno;
drh1ab43002002-01-14 09:28:19 +00001343 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
drhed6c8672003-01-12 18:02:16 +00001344 sqliteCheckMemory(pPager->aInJournal, pgno/8);
drhdb48ee02003-01-16 13:42:43 +00001345 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00001346 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00001347 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001348 }else{
1349 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00001350 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001351 }
drh03eb96a2002-11-10 23:32:56 +00001352 if( pPager->aInCkpt && (int)pgno<=pPager->ckptSize
1353 && (pPager->aInCkpt[pgno/8] & (1<<(pgno&7)))!=0 ){
1354 page_add_to_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001355 }else{
drh03eb96a2002-11-10 23:32:56 +00001356 page_remove_from_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001357 }
drhed7c8552001-04-11 14:29:21 +00001358 pPg->dirty = 0;
1359 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00001360 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001361 pPager->nRef++;
1362 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +00001363 pPg->pNextHash = pPager->aHash[h];
1364 pPager->aHash[h] = pPg;
1365 if( pPg->pNextHash ){
1366 assert( pPg->pNextHash->pPrevHash==0 );
1367 pPg->pNextHash->pPrevHash = pPg;
1368 }
drh306dc212001-05-21 13:45:10 +00001369 if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager);
drh1ab43002002-01-14 09:28:19 +00001370 if( pPager->dbSize<(int)pgno ){
drh306dc212001-05-21 13:45:10 +00001371 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
1372 }else{
drh81a20f22001-10-12 17:30:04 +00001373 int rc;
drhd0d006e2002-12-01 02:00:57 +00001374 sqliteOsSeek(&pPager->fd, (pgno-1)*(off_t)SQLITE_PAGE_SIZE);
drha7fcb052001-12-14 15:09:55 +00001375 rc = sqliteOsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drh81a20f22001-10-12 17:30:04 +00001376 if( rc!=SQLITE_OK ){
drh28be87c2002-11-05 23:03:02 +00001377 off_t fileSize;
drh4e371ee2002-09-05 16:08:27 +00001378 if( sqliteOsFileSize(&pPager->fd,&fileSize)!=SQLITE_OK
1379 || fileSize>=pgno*SQLITE_PAGE_SIZE ){
1380 return rc;
1381 }else{
1382 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
1383 }
drh81a20f22001-10-12 17:30:04 +00001384 }
drh306dc212001-05-21 13:45:10 +00001385 }
drh7e3b0a02001-04-28 16:52:40 +00001386 if( pPager->nExtra>0 ){
1387 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
1388 }
drhed7c8552001-04-11 14:29:21 +00001389 }else{
drhd9b02572001-04-15 00:37:09 +00001390 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +00001391 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +00001392 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00001393 }
1394 *ppPage = PGHDR_TO_DATA(pPg);
1395 return SQLITE_OK;
1396}
1397
1398/*
drh7e3b0a02001-04-28 16:52:40 +00001399** Acquire a page if it is already in the in-memory cache. Do
1400** not read the page from disk. Return a pointer to the page,
1401** or 0 if the page is not in cache.
1402**
1403** See also sqlitepager_get(). The difference between this routine
1404** and sqlitepager_get() is that _get() will go to the disk and read
1405** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00001406** returns NULL if the page is not in cache or if a disk I/O error
1407** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00001408*/
1409void *sqlitepager_lookup(Pager *pPager, Pgno pgno){
1410 PgHdr *pPg;
1411
drh836faa42003-01-11 13:30:57 +00001412 assert( pPager!=0 );
1413 assert( pgno!=0 );
drh7e3b0a02001-04-28 16:52:40 +00001414 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1415 return 0;
1416 }
drh836faa42003-01-11 13:30:57 +00001417 /* if( pPager->nRef==0 ){
1418 ** return 0;
1419 ** }
1420 */
drh7e3b0a02001-04-28 16:52:40 +00001421 pPg = pager_lookup(pPager, pgno);
1422 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +00001423 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +00001424 return PGHDR_TO_DATA(pPg);
1425}
1426
1427/*
drhed7c8552001-04-11 14:29:21 +00001428** Release a page.
1429**
1430** If the number of references to the page drop to zero, then the
1431** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00001432** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00001433** removed.
1434*/
drhd9b02572001-04-15 00:37:09 +00001435int sqlitepager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +00001436 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001437
1438 /* Decrement the reference count for this page
1439 */
drhed7c8552001-04-11 14:29:21 +00001440 pPg = DATA_TO_PGHDR(pData);
1441 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +00001442 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00001443 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001444
drh72f82862001-05-24 21:06:34 +00001445 /* When the number of references to a page reach 0, call the
1446 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00001447 */
drhed7c8552001-04-11 14:29:21 +00001448 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00001449 Pager *pPager;
1450 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00001451 pPg->pNextFree = 0;
1452 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00001453 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00001454 if( pPg->pPrevFree ){
1455 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00001456 }else{
1457 pPager->pFirst = pPg;
1458 }
drh341eae82003-01-21 02:39:36 +00001459 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
1460 pPager->pFirstSynced = pPg;
1461 }
drh72f82862001-05-24 21:06:34 +00001462 if( pPager->xDestructor ){
1463 pPager->xDestructor(pData);
1464 }
drhd9b02572001-04-15 00:37:09 +00001465
1466 /* When all pages reach the freelist, drop the read lock from
1467 ** the database file.
1468 */
1469 pPager->nRef--;
1470 assert( pPager->nRef>=0 );
1471 if( pPager->nRef==0 ){
1472 pager_reset(pPager);
1473 }
drhed7c8552001-04-11 14:29:21 +00001474 }
drhd9b02572001-04-15 00:37:09 +00001475 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00001476}
1477
1478/*
drhda47d772002-12-02 04:25:19 +00001479** Create a journal file for pPager. There should already be a write
1480** lock on the database file when this routine is called.
1481**
1482** Return SQLITE_OK if everything. Return an error code and release the
1483** write lock if anything goes wrong.
1484*/
1485static int pager_open_journal(Pager *pPager){
1486 int rc;
1487 assert( pPager->state==SQLITE_WRITELOCK );
1488 assert( pPager->journalOpen==0 );
1489 assert( pPager->useJournal );
1490 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
1491 if( pPager->aInJournal==0 ){
1492 sqliteOsReadLock(&pPager->fd);
1493 pPager->state = SQLITE_READLOCK;
1494 return SQLITE_NOMEM;
1495 }
1496 rc = sqliteOsOpenExclusive(pPager->zJournal, &pPager->jfd,pPager->tempFile);
1497 if( rc!=SQLITE_OK ){
1498 sqliteFree(pPager->aInJournal);
1499 pPager->aInJournal = 0;
1500 sqliteOsReadLock(&pPager->fd);
1501 pPager->state = SQLITE_READLOCK;
1502 return SQLITE_CANTOPEN;
1503 }
1504 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001505 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00001506 pPager->needSync = 0;
1507 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00001508 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001509 sqlitepager_pagecount(pPager);
1510 pPager->origDbSize = pPager->dbSize;
drh968af522003-02-11 14:55:40 +00001511 if( journal_format==JOURNAL_FORMAT_3 ){
1512 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic3, sizeof(aJournalMagic3));
1513 if( rc==SQLITE_OK ){
1514 rc = write32bits(&pPager->jfd, pPager->tempFile ? 0xffffffff : 0);
1515 }
1516 if( rc==SQLITE_OK ){
1517 pPager->cksumInit = (u32)sqliteRandomInteger();
1518 rc = write32bits(&pPager->jfd, pPager->cksumInit);
1519 }
1520 }else if( journal_format==JOURNAL_FORMAT_2 ){
1521 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic2, sizeof(aJournalMagic2));
drhda47d772002-12-02 04:25:19 +00001522 }else{
drh968af522003-02-11 14:55:40 +00001523 assert( journal_format==JOURNAL_FORMAT_1 );
1524 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic1, sizeof(aJournalMagic1));
drhda47d772002-12-02 04:25:19 +00001525 }
1526 if( rc==SQLITE_OK ){
1527 rc = write32bits(&pPager->jfd, pPager->dbSize);
1528 }
1529 if( pPager->ckptAutoopen && rc==SQLITE_OK ){
1530 rc = sqlitepager_ckpt_begin(pPager);
1531 }
1532 if( rc!=SQLITE_OK ){
1533 rc = pager_unwritelock(pPager);
1534 if( rc==SQLITE_OK ){
1535 rc = SQLITE_FULL;
1536 }
1537 }
1538 return rc;
1539}
1540
1541/*
drh4b845d72002-03-05 12:41:19 +00001542** Acquire a write-lock on the database. The lock is removed when
1543** the any of the following happen:
1544**
1545** * sqlitepager_commit() is called.
1546** * sqlitepager_rollback() is called.
1547** * sqlitepager_close() is called.
1548** * sqlitepager_unref() is called to on every outstanding page.
1549**
1550** The parameter to this routine is a pointer to any open page of the
1551** database file. Nothing changes about the page - it is used merely
1552** to acquire a pointer to the Pager structure and as proof that there
1553** is already a read-lock on the database.
1554**
drhda47d772002-12-02 04:25:19 +00001555** A journal file is opened if this is not a temporary file. For
1556** temporary files, the opening of the journal file is deferred until
1557** there is an actual need to write to the journal.
1558**
drh4b845d72002-03-05 12:41:19 +00001559** If the database is already write-locked, this routine is a no-op.
1560*/
1561int sqlitepager_begin(void *pData){
1562 PgHdr *pPg = DATA_TO_PGHDR(pData);
1563 Pager *pPager = pPg->pPager;
1564 int rc = SQLITE_OK;
1565 assert( pPg->nRef>0 );
1566 assert( pPager->state!=SQLITE_UNLOCK );
1567 if( pPager->state==SQLITE_READLOCK ){
1568 assert( pPager->aInJournal==0 );
1569 rc = sqliteOsWriteLock(&pPager->fd);
1570 if( rc!=SQLITE_OK ){
1571 return rc;
1572 }
drh4b845d72002-03-05 12:41:19 +00001573 pPager->state = SQLITE_WRITELOCK;
drhda47d772002-12-02 04:25:19 +00001574 pPager->dirtyFile = 0;
drhdb48ee02003-01-16 13:42:43 +00001575 TRACE1("TRANSACTION\n");
drhda47d772002-12-02 04:25:19 +00001576 if( pPager->useJournal && !pPager->tempFile ){
1577 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00001578 }
1579 }
1580 return rc;
1581}
1582
1583/*
drhed7c8552001-04-11 14:29:21 +00001584** Mark a data page as writeable. The page is written into the journal
1585** if it is not there already. This routine must be called before making
1586** changes to a page.
1587**
1588** The first time this routine is called, the pager creates a new
1589** journal and acquires a write lock on the database. If the write
1590** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00001591** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00001592** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00001593**
1594** If the journal file could not be written because the disk is full,
1595** then this routine returns SQLITE_FULL and does an immediate rollback.
1596** All subsequent write attempts also return SQLITE_FULL until there
1597** is a call to sqlitepager_commit() or sqlitepager_rollback() to
1598** reset.
drhed7c8552001-04-11 14:29:21 +00001599*/
drhd9b02572001-04-15 00:37:09 +00001600int sqlitepager_write(void *pData){
drh69688d52001-04-14 16:38:23 +00001601 PgHdr *pPg = DATA_TO_PGHDR(pData);
1602 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00001603 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00001604
drh6446c4d2001-12-15 14:22:18 +00001605 /* Check for errors
1606 */
drhd9b02572001-04-15 00:37:09 +00001607 if( pPager->errMask ){
1608 return pager_errcode(pPager);
1609 }
drh5e00f6c2001-09-13 13:46:56 +00001610 if( pPager->readOnly ){
1611 return SQLITE_PERM;
1612 }
drh6446c4d2001-12-15 14:22:18 +00001613
1614 /* Mark the page as dirty. If the page has already been written
1615 ** to the journal then we can return right away.
1616 */
drhd9b02572001-04-15 00:37:09 +00001617 pPg->dirty = 1;
drh0f892532002-05-30 12:27:03 +00001618 if( pPg->inJournal && (pPg->inCkpt || pPager->ckptInUse==0) ){
drha1680452002-04-18 01:56:57 +00001619 pPager->dirtyFile = 1;
drhfa86c412002-02-02 15:01:15 +00001620 return SQLITE_OK;
1621 }
drh6446c4d2001-12-15 14:22:18 +00001622
1623 /* If we get this far, it means that the page needs to be
drhfa86c412002-02-02 15:01:15 +00001624 ** written to the transaction journal or the ckeckpoint journal
1625 ** or both.
1626 **
1627 ** First check to see that the transaction journal exists and
1628 ** create it if it does not.
drh6446c4d2001-12-15 14:22:18 +00001629 */
drhd9b02572001-04-15 00:37:09 +00001630 assert( pPager->state!=SQLITE_UNLOCK );
drh4b845d72002-03-05 12:41:19 +00001631 rc = sqlitepager_begin(pData);
drhda47d772002-12-02 04:25:19 +00001632 if( rc!=SQLITE_OK ){
1633 return rc;
1634 }
drhd9b02572001-04-15 00:37:09 +00001635 assert( pPager->state==SQLITE_WRITELOCK );
drhda47d772002-12-02 04:25:19 +00001636 if( !pPager->journalOpen && pPager->useJournal ){
1637 rc = pager_open_journal(pPager);
1638 if( rc!=SQLITE_OK ) return rc;
1639 }
1640 assert( pPager->journalOpen || !pPager->useJournal );
1641 pPager->dirtyFile = 1;
drh6446c4d2001-12-15 14:22:18 +00001642
drhfa86c412002-02-02 15:01:15 +00001643 /* The transaction journal now exists and we have a write lock on the
1644 ** main database file. Write the current page to the transaction
1645 ** journal if it is not there already.
drh6446c4d2001-12-15 14:22:18 +00001646 */
drhdb48ee02003-01-16 13:42:43 +00001647 if( !pPg->inJournal && pPager->useJournal ){
1648 if( (int)pPg->pgno <= pPager->origDbSize ){
drh968af522003-02-11 14:55:40 +00001649 int szPg;
1650 u32 saved;
1651 if( journal_format>=JOURNAL_FORMAT_3 ){
1652 u32 cksum = pager_cksum(pPager, pPg->pgno, pData);
1653 saved = *(u32*)PGHDR_TO_EXTRA(pPg);
1654 store32bits(cksum, pPg, SQLITE_PAGE_SIZE);
1655 szPg = SQLITE_PAGE_SIZE+8;
1656 }else{
1657 szPg = SQLITE_PAGE_SIZE+4;
1658 }
1659 store32bits(pPg->pgno, pPg, -4);
1660 rc = sqliteOsWrite(&pPager->jfd, &((char*)pData)[-4], szPg);
1661 if( journal_format>=JOURNAL_FORMAT_3 ){
1662 *(u32*)PGHDR_TO_EXTRA(pPg) = saved;
1663 }
1664 pPager->nRec++;
drhdb48ee02003-01-16 13:42:43 +00001665 if( rc!=SQLITE_OK ){
1666 sqlitepager_rollback(pPager);
1667 pPager->errMask |= PAGER_ERR_FULL;
1668 return rc;
1669 }
1670 assert( pPager->aInJournal!=0 );
1671 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1672 pPg->needSync = !pPager->noSync;
1673 pPg->inJournal = 1;
1674 if( pPager->ckptInUse ){
1675 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1676 page_add_to_ckpt_list(pPg);
1677 }
1678 TRACE3("JOURNAL %d %d\n", pPg->pgno, pPg->needSync);
1679 }else{
1680 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
1681 TRACE3("APPEND %d %d\n", pPg->pgno, pPg->needSync);
drhd9b02572001-04-15 00:37:09 +00001682 }
drhdb48ee02003-01-16 13:42:43 +00001683 if( pPg->needSync ){
1684 pPager->needSync = 1;
drhfa86c412002-02-02 15:01:15 +00001685 }
drh69688d52001-04-14 16:38:23 +00001686 }
drh6446c4d2001-12-15 14:22:18 +00001687
drhfa86c412002-02-02 15:01:15 +00001688 /* If the checkpoint journal is open and the page is not in it,
drh968af522003-02-11 14:55:40 +00001689 ** then write the current page to the checkpoint journal. Note that
1690 ** the checkpoint journal always uses the simplier format 2 that lacks
1691 ** checksums. The header is also omitted from the checkpoint journal.
drh6446c4d2001-12-15 14:22:18 +00001692 */
drh0f892532002-05-30 12:27:03 +00001693 if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
drh1e336b42002-02-14 12:50:33 +00001694 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drh968af522003-02-11 14:55:40 +00001695 store32bits(pPg->pgno, pPg, -4);
drh2554f8b2003-01-22 01:26:44 +00001696 rc = sqliteOsWrite(&pPager->cpfd, &((char*)pData)[-4], SQLITE_PAGE_SIZE+4);
drhfa86c412002-02-02 15:01:15 +00001697 if( rc!=SQLITE_OK ){
1698 sqlitepager_rollback(pPager);
1699 pPager->errMask |= PAGER_ERR_FULL;
1700 return rc;
1701 }
drh9bd47a92003-01-07 14:46:08 +00001702 pPager->ckptNRec++;
drhfa86c412002-02-02 15:01:15 +00001703 assert( pPager->aInCkpt!=0 );
1704 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001705 page_add_to_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001706 }
1707
1708 /* Update the database size and return.
1709 */
drh1ab43002002-01-14 09:28:19 +00001710 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00001711 pPager->dbSize = pPg->pgno;
1712 }
drh69688d52001-04-14 16:38:23 +00001713 return rc;
drhed7c8552001-04-11 14:29:21 +00001714}
1715
1716/*
drhaacc5432002-01-06 17:07:40 +00001717** Return TRUE if the page given in the argument was previously passed
drh6019e162001-07-02 17:51:45 +00001718** to sqlitepager_write(). In other words, return TRUE if it is ok
1719** to change the content of the page.
1720*/
1721int sqlitepager_iswriteable(void *pData){
1722 PgHdr *pPg = DATA_TO_PGHDR(pData);
1723 return pPg->dirty;
1724}
1725
1726/*
drh30e58752002-03-02 20:41:57 +00001727** A call to this routine tells the pager that it is not necessary to
1728** write the information on page "pgno" back to the disk, even though
1729** that page might be marked as dirty.
1730**
1731** The overlying software layer calls this routine when all of the data
1732** on the given page is unused. The pager marks the page as clean so
1733** that it does not get written to disk.
1734**
1735** Tests show that this optimization, together with the
1736** sqlitepager_dont_rollback() below, more than double the speed
1737** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00001738**
1739** When this routine is called, set the alwaysRollback flag to true.
1740** Subsequent calls to sqlitepager_dont_rollback() for the same page
1741** will thereafter be ignored. This is necessary to avoid a problem
1742** where a page with data is added to the freelist during one part of
1743** a transaction then removed from the freelist during a later part
1744** of the same transaction and reused for some other purpose. When it
1745** is first added to the freelist, this routine is called. When reused,
1746** the dont_rollback() routine is called. But because the page contains
1747** critical data, we still need to be sure it gets rolled back in spite
1748** of the dont_rollback() call.
drh30e58752002-03-02 20:41:57 +00001749*/
1750void sqlitepager_dont_write(Pager *pPager, Pgno pgno){
1751 PgHdr *pPg;
drh8e298f92002-07-06 16:28:47 +00001752
drh30e58752002-03-02 20:41:57 +00001753 pPg = pager_lookup(pPager, pgno);
drh8e298f92002-07-06 16:28:47 +00001754 pPg->alwaysRollback = 1;
drh30e58752002-03-02 20:41:57 +00001755 if( pPg && pPg->dirty ){
drh8124a302002-06-25 14:43:57 +00001756 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
1757 /* If this pages is the last page in the file and the file has grown
1758 ** during the current transaction, then do NOT mark the page as clean.
1759 ** When the database file grows, we must make sure that the last page
1760 ** gets written at least once so that the disk file will be the correct
1761 ** size. If you do not write this page and the size of the file
1762 ** on the disk ends up being too small, that can lead to database
1763 ** corruption during the next transaction.
1764 */
1765 }else{
drhdb48ee02003-01-16 13:42:43 +00001766 TRACE2("DONT_WRITE %d\n", pgno);
drh8124a302002-06-25 14:43:57 +00001767 pPg->dirty = 0;
1768 }
drh30e58752002-03-02 20:41:57 +00001769 }
1770}
1771
1772/*
1773** A call to this routine tells the pager that if a rollback occurs,
1774** it is not necessary to restore the data on the given page. This
1775** means that the pager does not have to record the given page in the
1776** rollback journal.
1777*/
1778void sqlitepager_dont_rollback(void *pData){
1779 PgHdr *pPg = DATA_TO_PGHDR(pData);
1780 Pager *pPager = pPg->pPager;
1781
1782 if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return;
drh193a6b42002-07-07 16:52:46 +00001783 if( pPg->alwaysRollback || pPager->alwaysRollback ) return;
drh30e58752002-03-02 20:41:57 +00001784 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
1785 assert( pPager->aInJournal!=0 );
1786 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1787 pPg->inJournal = 1;
drh0f892532002-05-30 12:27:03 +00001788 if( pPager->ckptInUse ){
drh30e58752002-03-02 20:41:57 +00001789 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001790 page_add_to_ckpt_list(pPg);
drh30e58752002-03-02 20:41:57 +00001791 }
drhdb48ee02003-01-16 13:42:43 +00001792 TRACE2("DONT_ROLLBACK %d\n", pPg->pgno);
drh30e58752002-03-02 20:41:57 +00001793 }
drh0f892532002-05-30 12:27:03 +00001794 if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
drh30e58752002-03-02 20:41:57 +00001795 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
1796 assert( pPager->aInCkpt!=0 );
1797 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001798 page_add_to_ckpt_list(pPg);
drh30e58752002-03-02 20:41:57 +00001799 }
1800}
1801
1802/*
drhed7c8552001-04-11 14:29:21 +00001803** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00001804**
1805** If the commit fails for any reason, a rollback attempt is made
1806** and an error code is returned. If the commit worked, SQLITE_OK
1807** is returned.
drhed7c8552001-04-11 14:29:21 +00001808*/
drhd9b02572001-04-15 00:37:09 +00001809int sqlitepager_commit(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00001810 int rc;
drhed7c8552001-04-11 14:29:21 +00001811 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001812
1813 if( pPager->errMask==PAGER_ERR_FULL ){
1814 rc = sqlitepager_rollback(pPager);
drh4e371ee2002-09-05 16:08:27 +00001815 if( rc==SQLITE_OK ){
1816 rc = SQLITE_FULL;
1817 }
drhd9b02572001-04-15 00:37:09 +00001818 return rc;
1819 }
1820 if( pPager->errMask!=0 ){
1821 rc = pager_errcode(pPager);
1822 return rc;
1823 }
1824 if( pPager->state!=SQLITE_WRITELOCK ){
1825 return SQLITE_ERROR;
1826 }
drhdb48ee02003-01-16 13:42:43 +00001827 TRACE1("COMMIT\n");
drha1680452002-04-18 01:56:57 +00001828 if( pPager->dirtyFile==0 ){
1829 /* Exit early (without doing the time-consuming sqliteOsSync() calls)
1830 ** if there have been no changes to the database file. */
drh341eae82003-01-21 02:39:36 +00001831 assert( pPager->needSync==0 );
drha1680452002-04-18 01:56:57 +00001832 rc = pager_unwritelock(pPager);
1833 pPager->dbSize = -1;
1834 return rc;
1835 }
drhda47d772002-12-02 04:25:19 +00001836 assert( pPager->journalOpen );
drha7fcb052001-12-14 15:09:55 +00001837 if( pPager->needSync && sqliteOsSync(&pPager->jfd)!=SQLITE_OK ){
drhd9b02572001-04-15 00:37:09 +00001838 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +00001839 }
drh2554f8b2003-01-22 01:26:44 +00001840 pPg = pager_get_all_dirty_pages(pPager);
1841 if( pPg ){
1842 rc = pager_write_pagelist(pPg);
1843 if( rc || (!pPager->noSync && sqliteOsSync(&pPager->fd)!=SQLITE_OK) ){
1844 goto commit_abort;
1845 }
drh603240c2002-03-05 01:11:12 +00001846 }
drhd9b02572001-04-15 00:37:09 +00001847 rc = pager_unwritelock(pPager);
1848 pPager->dbSize = -1;
1849 return rc;
1850
1851 /* Jump here if anything goes wrong during the commit process.
1852 */
1853commit_abort:
1854 rc = sqlitepager_rollback(pPager);
1855 if( rc==SQLITE_OK ){
1856 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +00001857 }
drhed7c8552001-04-11 14:29:21 +00001858 return rc;
1859}
1860
1861/*
1862** Rollback all changes. The database falls back to read-only mode.
1863** All in-memory cache pages revert to their original data contents.
1864** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00001865**
1866** This routine cannot fail unless some other process is not following
1867** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
1868** process is writing trash into the journal file (SQLITE_CORRUPT) or
1869** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
1870** codes are returned for all these occasions. Otherwise,
1871** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00001872*/
drhd9b02572001-04-15 00:37:09 +00001873int sqlitepager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001874 int rc;
drhdb48ee02003-01-16 13:42:43 +00001875 TRACE1("ROLLBACK\n");
drhda47d772002-12-02 04:25:19 +00001876 if( !pPager->dirtyFile || !pPager->journalOpen ){
1877 rc = pager_unwritelock(pPager);
1878 pPager->dbSize = -1;
1879 return rc;
1880 }
drhdb48ee02003-01-16 13:42:43 +00001881
drhd9b02572001-04-15 00:37:09 +00001882 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
drh4b845d72002-03-05 12:41:19 +00001883 if( pPager->state>=SQLITE_WRITELOCK ){
1884 pager_playback(pPager);
1885 }
drhd9b02572001-04-15 00:37:09 +00001886 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00001887 }
drhd9b02572001-04-15 00:37:09 +00001888 if( pPager->state!=SQLITE_WRITELOCK ){
1889 return SQLITE_OK;
1890 }
1891 rc = pager_playback(pPager);
1892 if( rc!=SQLITE_OK ){
1893 rc = SQLITE_CORRUPT;
1894 pPager->errMask |= PAGER_ERR_CORRUPT;
1895 }
1896 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00001897 return rc;
drh98808ba2001-10-18 12:34:46 +00001898}
drhd9b02572001-04-15 00:37:09 +00001899
1900/*
drh5e00f6c2001-09-13 13:46:56 +00001901** Return TRUE if the database file is opened read-only. Return FALSE
1902** if the database is (in theory) writable.
1903*/
1904int sqlitepager_isreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00001905 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00001906}
1907
1908/*
drhd9b02572001-04-15 00:37:09 +00001909** This routine is used for testing and analysis only.
1910*/
1911int *sqlitepager_stats(Pager *pPager){
1912 static int a[9];
1913 a[0] = pPager->nRef;
1914 a[1] = pPager->nPage;
1915 a[2] = pPager->mxPage;
1916 a[3] = pPager->dbSize;
1917 a[4] = pPager->state;
1918 a[5] = pPager->errMask;
1919 a[6] = pPager->nHit;
1920 a[7] = pPager->nMiss;
1921 a[8] = pPager->nOvfl;
1922 return a;
1923}
drhdd793422001-06-28 01:54:48 +00001924
drhfa86c412002-02-02 15:01:15 +00001925/*
1926** Set the checkpoint.
1927**
1928** This routine should be called with the transaction journal already
1929** open. A new checkpoint journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00001930** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00001931*/
1932int sqlitepager_ckpt_begin(Pager *pPager){
1933 int rc;
1934 char zTemp[SQLITE_TEMPNAME_SIZE];
drhda47d772002-12-02 04:25:19 +00001935 if( !pPager->journalOpen ){
1936 pPager->ckptAutoopen = 1;
1937 return SQLITE_OK;
1938 }
drhfa86c412002-02-02 15:01:15 +00001939 assert( pPager->journalOpen );
drh0f892532002-05-30 12:27:03 +00001940 assert( !pPager->ckptInUse );
drhfa86c412002-02-02 15:01:15 +00001941 pPager->aInCkpt = sqliteMalloc( pPager->dbSize/8 + 1 );
1942 if( pPager->aInCkpt==0 ){
1943 sqliteOsReadLock(&pPager->fd);
1944 return SQLITE_NOMEM;
1945 }
drh968af522003-02-11 14:55:40 +00001946#ifndef NDEBUG
drhfa86c412002-02-02 15:01:15 +00001947 rc = sqliteOsFileSize(&pPager->jfd, &pPager->ckptJSize);
1948 if( rc ) goto ckpt_begin_failed;
drh968af522003-02-11 14:55:40 +00001949 assert( pPager->ckptJSize ==
1950 pPager->nRec*JOURNAL_PG_SZ(journal_format)+JOURNAL_HDR_SZ(journal_format) );
1951#endif
1952 pPager->ckptJSize = pPager->nRec*JOURNAL_PG_SZ(journal_format)
1953 + JOURNAL_HDR_SZ(journal_format);
drh663fc632002-02-02 18:49:19 +00001954 pPager->ckptSize = pPager->dbSize;
drh0f892532002-05-30 12:27:03 +00001955 if( !pPager->ckptOpen ){
1956 rc = sqlitepager_opentemp(zTemp, &pPager->cpfd);
1957 if( rc ) goto ckpt_begin_failed;
1958 pPager->ckptOpen = 1;
drh9bd47a92003-01-07 14:46:08 +00001959 pPager->ckptNRec = 0;
drh0f892532002-05-30 12:27:03 +00001960 }
1961 pPager->ckptInUse = 1;
drhfa86c412002-02-02 15:01:15 +00001962 return SQLITE_OK;
1963
1964ckpt_begin_failed:
1965 if( pPager->aInCkpt ){
1966 sqliteFree(pPager->aInCkpt);
1967 pPager->aInCkpt = 0;
1968 }
1969 return rc;
1970}
1971
1972/*
1973** Commit a checkpoint.
1974*/
1975int sqlitepager_ckpt_commit(Pager *pPager){
drh0f892532002-05-30 12:27:03 +00001976 if( pPager->ckptInUse ){
drh03eb96a2002-11-10 23:32:56 +00001977 PgHdr *pPg, *pNext;
drh96ddd6d2002-09-05 19:10:33 +00001978 sqliteOsSeek(&pPager->cpfd, 0);
drh9bd47a92003-01-07 14:46:08 +00001979 /* sqliteOsTruncate(&pPager->cpfd, 0); */
1980 pPager->ckptNRec = 0;
drh0f892532002-05-30 12:27:03 +00001981 pPager->ckptInUse = 0;
drh663fc632002-02-02 18:49:19 +00001982 sqliteFree( pPager->aInCkpt );
1983 pPager->aInCkpt = 0;
drh03eb96a2002-11-10 23:32:56 +00001984 for(pPg=pPager->pCkpt; pPg; pPg=pNext){
1985 pNext = pPg->pNextCkpt;
1986 assert( pPg->inCkpt );
drh663fc632002-02-02 18:49:19 +00001987 pPg->inCkpt = 0;
drh03eb96a2002-11-10 23:32:56 +00001988 pPg->pPrevCkpt = pPg->pNextCkpt = 0;
drh663fc632002-02-02 18:49:19 +00001989 }
drh03eb96a2002-11-10 23:32:56 +00001990 pPager->pCkpt = 0;
drh663fc632002-02-02 18:49:19 +00001991 }
drhda47d772002-12-02 04:25:19 +00001992 pPager->ckptAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00001993 return SQLITE_OK;
1994}
1995
1996/*
1997** Rollback a checkpoint.
1998*/
1999int sqlitepager_ckpt_rollback(Pager *pPager){
2000 int rc;
drh0f892532002-05-30 12:27:03 +00002001 if( pPager->ckptInUse ){
drh663fc632002-02-02 18:49:19 +00002002 rc = pager_ckpt_playback(pPager);
2003 sqlitepager_ckpt_commit(pPager);
2004 }else{
2005 rc = SQLITE_OK;
2006 }
drhda47d772002-12-02 04:25:19 +00002007 pPager->ckptAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002008 return rc;
2009}
2010
drh74587e52002-08-13 00:01:16 +00002011#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00002012/*
2013** Print a listing of all referenced pages and their ref count.
2014*/
2015void sqlitepager_refdump(Pager *pPager){
2016 PgHdr *pPg;
2017 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2018 if( pPg->nRef<=0 ) continue;
2019 printf("PAGE %3d addr=0x%08x nRef=%d\n",
2020 pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
2021 }
2022}
2023#endif