blob: a8aa1467a66ba6cc24aa8b1e4086507522e39f71 [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**
drh968af522003-02-11 14:55:40 +000021** @(#) $Id: pager.c,v 1.73 2003/02/11 14:55:41 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 */
drh968af522003-02-11 14:55:40 +0000104 /* Pager.nExtra bytes of local data follow the page data and checksum */
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() */
drhdb48ee02003-01-16 13:42:43 +0000138#ifndef NDEBUG
139 off_t syncJSize; /* Size of journal at last fsync() call */
140#endif
drh968af522003-02-11 14:55:40 +0000141 int nRec; /* Number of pages written to the journal */
142 u32 cksumInit; /* Quasi-random value added to every checksum */
drh9bd47a92003-01-07 14:46:08 +0000143 int ckptNRec; /* Number of records in the checkpoint journal */
drh7e3b0a02001-04-28 16:52:40 +0000144 int nExtra; /* Add this many bytes to each in-memory page */
drh72f82862001-05-24 21:06:34 +0000145 void (*xDestructor)(void*); /* Call this routine when freeing pages */
drhed7c8552001-04-11 14:29:21 +0000146 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000147 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000148 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000149 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
drh603240c2002-03-05 01:11:12 +0000150 u8 journalOpen; /* True if journal file descriptors is valid */
drhdb48ee02003-01-16 13:42:43 +0000151 u8 journalStarted; /* True if initial magic of journal is synced */
drhda47d772002-12-02 04:25:19 +0000152 u8 useJournal; /* Do not use a rollback journal on this file */
drh603240c2002-03-05 01:11:12 +0000153 u8 ckptOpen; /* True if the checkpoint journal is open */
drh0f892532002-05-30 12:27:03 +0000154 u8 ckptInUse; /* True we are in a checkpoint */
drhda47d772002-12-02 04:25:19 +0000155 u8 ckptAutoopen; /* Open ckpt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000156 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000157 u8 fullSync; /* Do extra syncs of the journal for robustness */
drh603240c2002-03-05 01:11:12 +0000158 u8 state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */
159 u8 errMask; /* One of several kinds of errors */
160 u8 tempFile; /* zFilename is a temporary file */
161 u8 readOnly; /* True for a read-only database */
162 u8 needSync; /* True if an fsync() is needed on the journal */
drha1680452002-04-18 01:56:57 +0000163 u8 dirtyFile; /* True if database file has changed in any way */
drh193a6b42002-07-07 16:52:46 +0000164 u8 alwaysRollback; /* Disable dont_rollback() for all pages */
drh603240c2002-03-05 01:11:12 +0000165 u8 *aInJournal; /* One bit for each page in the database file */
166 u8 *aInCkpt; /* One bit for each page in the database */
drhed7c8552001-04-11 14:29:21 +0000167 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000168 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000169 PgHdr *pAll; /* List of all pages */
drh03eb96a2002-11-10 23:32:56 +0000170 PgHdr *pCkpt; /* List of pages in the checkpoint journal */
drhed7c8552001-04-11 14:29:21 +0000171 PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */
drhd9b02572001-04-15 00:37:09 +0000172};
173
174/*
175** These are bits that can be set in Pager.errMask.
176*/
177#define PAGER_ERR_FULL 0x01 /* a write() failed */
178#define PAGER_ERR_MEM 0x02 /* malloc() failed */
179#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
180#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
drh81a20f22001-10-12 17:30:04 +0000181#define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */
drhd9b02572001-04-15 00:37:09 +0000182
183/*
184** The journal file contains page records in the following
185** format.
drh968af522003-02-11 14:55:40 +0000186**
187** Actually, this structure is the complete page record for pager
188** formats less than 3. Beginning with format 3, this record is surrounded
189** by two checksums.
drhd9b02572001-04-15 00:37:09 +0000190*/
191typedef struct PageRecord PageRecord;
192struct PageRecord {
193 Pgno pgno; /* The page number */
194 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
195};
196
197/*
drh5e00f6c2001-09-13 13:46:56 +0000198** Journal files begin with the following magic string. The data
199** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000200**
drh968af522003-02-11 14:55:40 +0000201** There are three journal formats (so far). The 1st journal format writes
202** 32-bit integers in the byte-order of the host machine. New
203** formats writes integers as big-endian. All new journals use the
drh94f33312002-08-12 12:29:56 +0000204** new format, but we have to be able to read an older journal in order
drh968af522003-02-11 14:55:40 +0000205** to rollback journals created by older versions of the library.
206**
207** The 3rd journal format (added for 2.8.0) adds additional sanity
208** checking information to the journal. If the power fails while the
209** journal is being written, semi-random garbage data might appear in
210** the journal file after power is restored. If an attempt is then made
211** to roll the journal back, the database could be corrupted. The additional
212** sanity checking data is an attempt to discover the garbage in the
213** journal and ignore it.
214**
215** The sanity checking information for the 3rd journal format consists
216** of a 32-bit checksum on each page of data. The checksum covers both
217** the page number and the SQLITE_PAGE_SIZE bytes of data for the page.
218** This cksum is initialized to a 32-bit random value that appears in the
219** journal file right after the header. The random initializer is important,
220** because garbage data that appears at the end of a journal is likely
221** data that was once in other files that have now been deleted. If the
222** garbage data came from an obsolete journal file, the checksums might
223** be correct. But by initializing the checksum to random value which
224** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000225*/
drh968af522003-02-11 14:55:40 +0000226static const unsigned char aJournalMagic1[] = {
drhd9b02572001-04-15 00:37:09 +0000227 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000228};
drh968af522003-02-11 14:55:40 +0000229static const unsigned char aJournalMagic2[] = {
drh94f33312002-08-12 12:29:56 +0000230 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5,
231};
drh968af522003-02-11 14:55:40 +0000232static const unsigned char aJournalMagic3[] = {
233 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd6,
234};
235#define JOURNAL_FORMAT_1 1
236#define JOURNAL_FORMAT_2 2
237#define JOURNAL_FORMAT_3 3
drh94f33312002-08-12 12:29:56 +0000238
239/*
drh968af522003-02-11 14:55:40 +0000240** The following integer determines what format to use when creating
241** new primary journal files. By default we always use format 3.
242** When testing, we can set this value to older journal formats in order to
243** make sure that newer versions of the library are able to rollback older
244** journal files.
245**
246** Note that checkpoint journals always use format 2 and omit the header.
drh94f33312002-08-12 12:29:56 +0000247*/
248#ifdef SQLITE_TEST
drh968af522003-02-11 14:55:40 +0000249int journal_format = 3;
drh74587e52002-08-13 00:01:16 +0000250#else
drh968af522003-02-11 14:55:40 +0000251# define journal_format 3
drh94f33312002-08-12 12:29:56 +0000252#endif
drhed7c8552001-04-11 14:29:21 +0000253
254/*
drh968af522003-02-11 14:55:40 +0000255** The size of the header and of each page in the journal varies according
256** to which journal format is being used. The following macros figure out
257** the sizes based on format numbers.
258*/
259#define JOURNAL_HDR_SZ(X) \
260 (sizeof(aJournalMagic1) + sizeof(Pgno) + ((X)>=3)*2*sizeof(u32))
261#define JOURNAL_PG_SZ(X) \
262 (SQLITE_PAGE_SIZE + sizeof(Pgno) + ((X)>=3)*sizeof(u32))
263
264/*
drhdd793422001-06-28 01:54:48 +0000265** Enable reference count tracking here:
266*/
drh74587e52002-08-13 00:01:16 +0000267#ifdef SQLITE_TEST
drh5e00f6c2001-09-13 13:46:56 +0000268 int pager_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000269 static void pager_refinfo(PgHdr *p){
270 static int cnt = 0;
271 if( !pager_refinfo_enable ) return;
272 printf(
273 "REFCNT: %4d addr=0x%08x nRef=%d\n",
274 p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
275 );
276 cnt++; /* Something to set a breakpoint on */
277 }
278# define REFINFO(X) pager_refinfo(X)
279#else
280# define REFINFO(X)
281#endif
282
283/*
drh94f33312002-08-12 12:29:56 +0000284** Read a 32-bit integer from the given file descriptor
285*/
drh968af522003-02-11 14:55:40 +0000286static int read32bits(int format, OsFile *fd, u32 *pRes){
drh94f33312002-08-12 12:29:56 +0000287 u32 res;
288 int rc;
289 rc = sqliteOsRead(fd, &res, sizeof(res));
drh968af522003-02-11 14:55:40 +0000290 if( rc==SQLITE_OK && format>JOURNAL_FORMAT_1 ){
drh94f33312002-08-12 12:29:56 +0000291 unsigned char ac[4];
292 memcpy(ac, &res, 4);
293 res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
294 }
295 *pRes = res;
296 return rc;
297}
298
299/*
300** Write a 32-bit integer into the given file descriptor. Writing
301** is always done using the new journal format.
302*/
303static int write32bits(OsFile *fd, u32 val){
304 unsigned char ac[4];
drh968af522003-02-11 14:55:40 +0000305 if( journal_format<=1 ){
drh94f33312002-08-12 12:29:56 +0000306 return sqliteOsWrite(fd, &val, 4);
307 }
drh94f33312002-08-12 12:29:56 +0000308 ac[0] = (val>>24) & 0xff;
309 ac[1] = (val>>16) & 0xff;
310 ac[2] = (val>>8) & 0xff;
311 ac[3] = val & 0xff;
312 return sqliteOsWrite(fd, ac, 4);
313}
314
drh2554f8b2003-01-22 01:26:44 +0000315/*
316** Write a 32-bit integer into a page header right before the
317** page data. This will overwrite the PgHdr.pDirty pointer.
318*/
drh968af522003-02-11 14:55:40 +0000319static void store32bits(u32 val, PgHdr *p, int offset){
drh2554f8b2003-01-22 01:26:44 +0000320 unsigned char *ac;
drh968af522003-02-11 14:55:40 +0000321 ac = &((char*)PGHDR_TO_DATA(p))[offset];
322 if( journal_format<=1 ){
drh2554f8b2003-01-22 01:26:44 +0000323 memcpy(ac, &val, 4);
324 }else{
325 ac[0] = (val>>24) & 0xff;
326 ac[1] = (val>>16) & 0xff;
327 ac[2] = (val>>8) & 0xff;
328 ac[3] = val & 0xff;
329 }
330}
331
drh94f33312002-08-12 12:29:56 +0000332
333/*
drhd9b02572001-04-15 00:37:09 +0000334** Convert the bits in the pPager->errMask into an approprate
335** return code.
336*/
337static int pager_errcode(Pager *pPager){
338 int rc = SQLITE_OK;
339 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
drh81a20f22001-10-12 17:30:04 +0000340 if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR;
drhd9b02572001-04-15 00:37:09 +0000341 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
342 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
343 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
344 return rc;
drhed7c8552001-04-11 14:29:21 +0000345}
346
347/*
drh03eb96a2002-11-10 23:32:56 +0000348** Add or remove a page from the list of all pages that are in the
349** checkpoint journal.
350**
351** The Pager keeps a separate list of pages that are currently in
352** the checkpoint journal. This helps the sqlitepager_ckpt_commit()
353** routine run MUCH faster for the common case where there are many
354** pages in memory but only a few are in the checkpoint journal.
355*/
356static void page_add_to_ckpt_list(PgHdr *pPg){
357 Pager *pPager = pPg->pPager;
358 if( pPg->inCkpt ) return;
359 assert( pPg->pPrevCkpt==0 && pPg->pNextCkpt==0 );
360 pPg->pPrevCkpt = 0;
361 if( pPager->pCkpt ){
362 pPager->pCkpt->pPrevCkpt = pPg;
363 }
364 pPg->pNextCkpt = pPager->pCkpt;
365 pPager->pCkpt = pPg;
366 pPg->inCkpt = 1;
367}
368static void page_remove_from_ckpt_list(PgHdr *pPg){
369 if( !pPg->inCkpt ) return;
370 if( pPg->pPrevCkpt ){
371 assert( pPg->pPrevCkpt->pNextCkpt==pPg );
372 pPg->pPrevCkpt->pNextCkpt = pPg->pNextCkpt;
373 }else{
374 assert( pPg->pPager->pCkpt==pPg );
375 pPg->pPager->pCkpt = pPg->pNextCkpt;
376 }
377 if( pPg->pNextCkpt ){
378 assert( pPg->pNextCkpt->pPrevCkpt==pPg );
379 pPg->pNextCkpt->pPrevCkpt = pPg->pPrevCkpt;
380 }
381 pPg->pNextCkpt = 0;
382 pPg->pPrevCkpt = 0;
383 pPg->inCkpt = 0;
384}
385
386/*
drhed7c8552001-04-11 14:29:21 +0000387** Find a page in the hash table given its page number. Return
388** a pointer to the page or NULL if not found.
389*/
drhd9b02572001-04-15 00:37:09 +0000390static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh836faa42003-01-11 13:30:57 +0000391 PgHdr *p = pPager->aHash[pager_hash(pgno)];
drhed7c8552001-04-11 14:29:21 +0000392 while( p && p->pgno!=pgno ){
393 p = p->pNextHash;
394 }
395 return p;
396}
397
398/*
399** Unlock the database and clear the in-memory cache. This routine
400** sets the state of the pager back to what it was when it was first
401** opened. Any outstanding pages are invalidated and subsequent attempts
402** to access those pages will likely result in a coredump.
403*/
drhd9b02572001-04-15 00:37:09 +0000404static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000405 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000406 for(pPg=pPager->pAll; pPg; pPg=pNext){
407 pNext = pPg->pNextAll;
408 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000409 }
410 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000411 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +0000412 pPager->pLast = 0;
413 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000414 memset(pPager->aHash, 0, sizeof(pPager->aHash));
415 pPager->nPage = 0;
drhfa86c412002-02-02 15:01:15 +0000416 if( pPager->state>=SQLITE_WRITELOCK ){
drhd9b02572001-04-15 00:37:09 +0000417 sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000418 }
drha7fcb052001-12-14 15:09:55 +0000419 sqliteOsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000420 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000421 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000422 pPager->nRef = 0;
drh8cfbf082001-09-19 13:22:39 +0000423 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000424}
425
426/*
427** When this routine is called, the pager has the journal file open and
428** a write lock on the database. This routine releases the database
429** write lock and acquires a read lock in its place. The journal file
430** is deleted and closed.
drhed7c8552001-04-11 14:29:21 +0000431*/
drhd9b02572001-04-15 00:37:09 +0000432static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000433 int rc;
drhd9b02572001-04-15 00:37:09 +0000434 PgHdr *pPg;
drhfa86c412002-02-02 15:01:15 +0000435 if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK;
drh663fc632002-02-02 18:49:19 +0000436 sqlitepager_ckpt_commit(pPager);
drh0f892532002-05-30 12:27:03 +0000437 if( pPager->ckptOpen ){
438 sqliteOsClose(&pPager->cpfd);
439 pPager->ckptOpen = 0;
440 }
drhda47d772002-12-02 04:25:19 +0000441 if( pPager->journalOpen ){
442 sqliteOsClose(&pPager->jfd);
443 pPager->journalOpen = 0;
444 sqliteOsDelete(pPager->zJournal);
445 sqliteFree( pPager->aInJournal );
446 pPager->aInJournal = 0;
447 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
448 pPg->inJournal = 0;
449 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +0000450 pPg->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000451 }
452 }else{
453 assert( pPager->dirtyFile==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +0000454 }
drhda47d772002-12-02 04:25:19 +0000455 rc = sqliteOsReadLock(&pPager->fd);
drh8e298f92002-07-06 16:28:47 +0000456 if( rc==SQLITE_OK ){
457 pPager->state = SQLITE_READLOCK;
458 }else{
459 /* This can only happen if a process does a BEGIN, then forks and the
460 ** child process does the COMMIT. Because of the semantics of unix
461 ** file locking, the unlock will fail.
462 */
463 pPager->state = SQLITE_UNLOCK;
464 }
drhed7c8552001-04-11 14:29:21 +0000465 return rc;
466}
467
drhed7c8552001-04-11 14:29:21 +0000468/*
drh968af522003-02-11 14:55:40 +0000469** Compute and return a checksum for the page of data.
470*/
471static u32 pager_cksum(Pager *pPager, Pgno pgno, const char *aData){
472 u32 cksum = pPager->cksumInit + pgno;
473 /* const u8 *a = (const u8*)aData;
474 int i;
475 for(i=0; i<SQLITE_PAGE_SIZE; i++){ cksum += a[i]; } */
476 /* fprintf(stderr,"CKSUM for %p(%08x) page %d: %08x\n", pPager, pPager->cksumInit, pgno, cksum); */
477 return cksum;
478}
479
480/*
drhfa86c412002-02-02 15:01:15 +0000481** Read a single page from the journal file opened on file descriptor
482** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +0000483**
484** There are three different journal formats. The format parameter determines
485** which format is used by the journal that is played back.
drhfa86c412002-02-02 15:01:15 +0000486*/
drh968af522003-02-11 14:55:40 +0000487static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int format){
drhfa86c412002-02-02 15:01:15 +0000488 int rc;
489 PgHdr *pPg; /* An existing page in the cache */
490 PageRecord pgRec;
drh968af522003-02-11 14:55:40 +0000491 u32 cksum;
drhfa86c412002-02-02 15:01:15 +0000492
drh968af522003-02-11 14:55:40 +0000493 rc = read32bits(format, jfd, &pgRec.pgno);
494 if( rc!=SQLITE_OK ) return SQLITE_DONE;
drh94f33312002-08-12 12:29:56 +0000495 rc = sqliteOsRead(jfd, &pgRec.aData, sizeof(pgRec.aData));
drh968af522003-02-11 14:55:40 +0000496 if( rc!=SQLITE_OK ) return SQLITE_DONE;
drhfa86c412002-02-02 15:01:15 +0000497
drh968af522003-02-11 14:55:40 +0000498 /* Sanity checking on the page. This is more important that I originally
499 ** thought. If a power failure occurs while the journal is being written,
500 ** it could cause invalid data to be written into the journal. We need to
501 ** detect this invalid data (with high probability) and ignore it.
502 */
503 if( pgRec.pgno==0 ){
504 return SQLITE_DONE;
505 }
506 if( pgRec.pgno>pPager->dbSize ){
507 return SQLITE_OK;
508 }
509 if( format>=JOURNAL_FORMAT_3 ){
510 rc = read32bits(format, jfd, &cksum);
511 if( rc ) return SQLITE_DONE;
512 if( pager_cksum(pPager, pgRec.pgno, pgRec.aData)!=cksum ){
513 return SQLITE_DONE;
514 }
515 }
drhfa86c412002-02-02 15:01:15 +0000516
517 /* Playback the page. Update the in-memory copy of the page
518 ** at the same time, if there is one.
519 */
520 pPg = pager_lookup(pPager, pgRec.pgno);
drhdb48ee02003-01-16 13:42:43 +0000521 if( pPg==0 || pPg->needSync==0 ){
522 TRACE2("PLAYBACK %d\n", pgRec.pgno);
523 sqliteOsSeek(&pPager->fd, (pgRec.pgno-1)*(off_t)SQLITE_PAGE_SIZE);
524 rc = sqliteOsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
525 }
drhfa86c412002-02-02 15:01:15 +0000526 if( pPg ){
drh3a840692003-01-29 22:58:26 +0000527 if( pPg->nRef==0 ||
528 memcmp(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE)==0
529 ){
530 /* Do not update the data on this page if the page is in use
531 ** and the page has never been modified. This avoids resetting
532 ** the "extra" data. That in turn avoids invalidating BTree cursors
533 ** in trees that have never been modified. The end result is that
534 ** you can have a SELECT going on in one table and ROLLBACK changes
535 ** to a different table and the SELECT is unaffected by the ROLLBACK.
536 */
537 memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
538 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
539 }
drhdb48ee02003-01-16 13:42:43 +0000540 pPg->dirty = 0;
541 pPg->needSync = 0;
drhfa86c412002-02-02 15:01:15 +0000542 }
543 return rc;
544}
545
546/*
drhed7c8552001-04-11 14:29:21 +0000547** Playback the journal and thus restore the database file to
548** the state it was in before we started making changes.
549**
drhd9b02572001-04-15 00:37:09 +0000550** The journal file format is as follows: There is an initial
551** file-type string for sanity checking. Then there is a single
552** Pgno number which is the number of pages in the database before
553** changes were made. The database is truncated to this size.
drh306dc212001-05-21 13:45:10 +0000554** Next come zero or more page records where each page record
555** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See
556** the PageRecord structure for details.
drhed7c8552001-04-11 14:29:21 +0000557**
drhd9b02572001-04-15 00:37:09 +0000558** If the file opened as the journal file is not a well-formed
559** journal file (as determined by looking at the magic number
560** at the beginning) then this routine returns SQLITE_PROTOCOL.
561** If any other errors occur during playback, the database will
562** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in
563** pPager->errMask and SQLITE_CORRUPT is returned. If it all
564** works, then this routine returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000565*/
drhd9b02572001-04-15 00:37:09 +0000566static int pager_playback(Pager *pPager){
drh968af522003-02-11 14:55:40 +0000567 off_t szJ; /* Size of the journal file in bytes */
568 int nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +0000569 int i; /* Loop counter */
570 Pgno mxPg = 0; /* Size of the original file in pages */
drh968af522003-02-11 14:55:40 +0000571 int format; /* Format of the journal file. */
572 unsigned char aMagic[sizeof(aJournalMagic1)];
drhed7c8552001-04-11 14:29:21 +0000573 int rc;
574
drhc3a64ba2001-11-22 00:01:27 +0000575 /* Figure out how many records are in the journal. Abort early if
576 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +0000577 */
drh8cfbf082001-09-19 13:22:39 +0000578 assert( pPager->journalOpen );
drha7fcb052001-12-14 15:09:55 +0000579 sqliteOsSeek(&pPager->jfd, 0);
drh968af522003-02-11 14:55:40 +0000580 rc = sqliteOsFileSize(&pPager->jfd, &szJ);
drhc3a64ba2001-11-22 00:01:27 +0000581 if( rc!=SQLITE_OK ){
582 goto end_playback;
583 }
drh968af522003-02-11 14:55:40 +0000584 if( szJ < sizeof(aMagic)+sizeof(Pgno) ){
drhc3a64ba2001-11-22 00:01:27 +0000585 goto end_playback;
586 }
587
588 /* Read the beginning of the journal and truncate the
589 ** database file back to its original size.
590 */
drha7fcb052001-12-14 15:09:55 +0000591 rc = sqliteOsRead(&pPager->jfd, aMagic, sizeof(aMagic));
drh94f33312002-08-12 12:29:56 +0000592 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000593 rc = SQLITE_PROTOCOL;
594 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000595 }
drh968af522003-02-11 14:55:40 +0000596 if( memcmp(aMagic, aJournalMagic3, sizeof(aMagic))==0 ){
597 format = JOURNAL_FORMAT_3;
598 }else if( memcmp(aMagic, aJournalMagic2, sizeof(aMagic))==0 ){
599 format = JOURNAL_FORMAT_2;
600 }else if( memcmp(aMagic, aJournalMagic1, sizeof(aMagic))==0 ){
601 format = JOURNAL_FORMAT_1;
drh94f33312002-08-12 12:29:56 +0000602 }else{
603 rc = SQLITE_PROTOCOL;
604 goto end_playback;
605 }
drh968af522003-02-11 14:55:40 +0000606 if( format>=JOURNAL_FORMAT_3 ){
607 rc = read32bits(format, &pPager->jfd, &nRec);
608 if( rc ) goto end_playback;
609 rc = read32bits(format, &pPager->jfd, &pPager->cksumInit);
610 if( rc ) goto end_playback;
611 if( nRec==0xffffffff ){
612 nRec = (szJ - JOURNAL_HDR_SZ(3))/JOURNAL_PG_SZ(3);
613 }
614 }else{
615 nRec = (szJ - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord);
616 }
617 rc = read32bits(format, &pPager->jfd, &mxPg);
drhd9b02572001-04-15 00:37:09 +0000618 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000619 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000620 }
drh28be87c2002-11-05 23:03:02 +0000621 rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg);
drh81a20f22001-10-12 17:30:04 +0000622 if( rc!=SQLITE_OK ){
623 goto end_playback;
624 }
drhd9b02572001-04-15 00:37:09 +0000625 pPager->dbSize = mxPg;
626
drhfa86c412002-02-02 15:01:15 +0000627 /* Copy original pages out of the journal and back into the database file.
drhed7c8552001-04-11 14:29:21 +0000628 */
drh968af522003-02-11 14:55:40 +0000629 for(i=0; i<nRec; i++){
630 rc = pager_playback_one_page(pPager, &pPager->jfd, format);
631 if( rc!=SQLITE_OK ){
632 if( rc==SQLITE_DONE ){
633fprintf(stderr,"Playback complete after %d of %d records\n", i, nRec);
634 rc = SQLITE_OK;
635 }
636 break;
637 }
drhed7c8552001-04-11 14:29:21 +0000638 }
drh81a20f22001-10-12 17:30:04 +0000639
drhdb48ee02003-01-16 13:42:43 +0000640
drh81a20f22001-10-12 17:30:04 +0000641end_playback:
drhdb48ee02003-01-16 13:42:43 +0000642#if !defined(NDEBUG) && defined(SQLITE_TEST)
643 /* For pages that were never written into the journal, restore the
644 ** memory copy from the original database file.
645 **
646 ** This is code is used during testing only. It is necessary to
647 ** compensate for the sqliteOsTruncate() call inside
648 ** sqlitepager_rollback().
649 */
650 if( rc==SQLITE_OK ){
651 PgHdr *pPg;
652 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3a840692003-01-29 22:58:26 +0000653 char zBuf[SQLITE_PAGE_SIZE];
drhdb48ee02003-01-16 13:42:43 +0000654 if( (int)pPg->pgno <= pPager->origDbSize ){
655 sqliteOsSeek(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)(pPg->pgno-1));
drh3a840692003-01-29 22:58:26 +0000656 rc = sqliteOsRead(&pPager->fd, zBuf, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000657 if( rc ) break;
658 }else{
drh3a840692003-01-29 22:58:26 +0000659 memset(zBuf, 0, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000660 }
drh3a840692003-01-29 22:58:26 +0000661 if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE) ){
662 memcpy(PGHDR_TO_DATA(pPg), zBuf, SQLITE_PAGE_SIZE);
663 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
664 }
drhdb48ee02003-01-16 13:42:43 +0000665 pPg->needSync = 0;
666 pPg->dirty = 0;
667 }
668 }
669#endif
drhd9b02572001-04-15 00:37:09 +0000670 if( rc!=SQLITE_OK ){
671 pager_unwritelock(pPager);
672 pPager->errMask |= PAGER_ERR_CORRUPT;
673 rc = SQLITE_CORRUPT;
674 }else{
675 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000676 }
drhd9b02572001-04-15 00:37:09 +0000677 return rc;
drhed7c8552001-04-11 14:29:21 +0000678}
679
680/*
drhfa86c412002-02-02 15:01:15 +0000681** Playback the checkpoint journal.
682**
683** This is similar to playing back the transaction journal but with
684** a few extra twists.
685**
drh663fc632002-02-02 18:49:19 +0000686** (1) The number of pages in the database file at the start of
687** the checkpoint is stored in pPager->ckptSize, not in the
688** journal file itself.
drhfa86c412002-02-02 15:01:15 +0000689**
690** (2) In addition to playing back the checkpoint journal, also
691** playback all pages of the transaction journal beginning
692** at offset pPager->ckptJSize.
693*/
694static int pager_ckpt_playback(Pager *pPager){
drh968af522003-02-11 14:55:40 +0000695 off_t szJ; /* Size of the full journal */
696 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +0000697 int i; /* Loop counter */
698 int rc;
699
700 /* Truncate the database back to its original size.
701 */
drh28be87c2002-11-05 23:03:02 +0000702 rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)pPager->ckptSize);
drhfa86c412002-02-02 15:01:15 +0000703 pPager->dbSize = pPager->ckptSize;
704
705 /* Figure out how many records are in the checkpoint journal.
706 */
drh0f892532002-05-30 12:27:03 +0000707 assert( pPager->ckptInUse && pPager->journalOpen );
drhfa86c412002-02-02 15:01:15 +0000708 sqliteOsSeek(&pPager->cpfd, 0);
drh9bd47a92003-01-07 14:46:08 +0000709 nRec = pPager->ckptNRec;
drhfa86c412002-02-02 15:01:15 +0000710
711 /* Copy original pages out of the checkpoint journal and back into the
drh968af522003-02-11 14:55:40 +0000712 ** database file. Note that the checkpoint journal always uses format
713 ** 2 instead of format 3 since it does not need to be concerned with
714 ** power failures corrupting the journal and can thus omit the checksums.
drhfa86c412002-02-02 15:01:15 +0000715 */
716 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000717 rc = pager_playback_one_page(pPager, &pPager->cpfd, 2);
718 assert( rc!=SQLITE_DONE );
drhfa86c412002-02-02 15:01:15 +0000719 if( rc!=SQLITE_OK ) goto end_ckpt_playback;
720 }
721
722 /* Figure out how many pages need to be copied out of the transaction
723 ** journal.
724 */
725 rc = sqliteOsSeek(&pPager->jfd, pPager->ckptJSize);
726 if( rc!=SQLITE_OK ){
727 goto end_ckpt_playback;
728 }
drh968af522003-02-11 14:55:40 +0000729 rc = sqliteOsFileSize(&pPager->jfd, &szJ);
drhfa86c412002-02-02 15:01:15 +0000730 if( rc!=SQLITE_OK ){
731 goto end_ckpt_playback;
732 }
drh968af522003-02-11 14:55:40 +0000733 nRec = (szJ - pPager->ckptJSize)/JOURNAL_PG_SZ(journal_format);
drhfa86c412002-02-02 15:01:15 +0000734 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000735 rc = pager_playback_one_page(pPager, &pPager->jfd, journal_format);
736 if( rc!=SQLITE_OK ){
737 assert( rc!=SQLITE_DONE );
738 goto end_ckpt_playback;
739 }
drhfa86c412002-02-02 15:01:15 +0000740 }
741
drhfa86c412002-02-02 15:01:15 +0000742end_ckpt_playback:
drhfa86c412002-02-02 15:01:15 +0000743 if( rc!=SQLITE_OK ){
drhfa86c412002-02-02 15:01:15 +0000744 pPager->errMask |= PAGER_ERR_CORRUPT;
745 rc = SQLITE_CORRUPT;
drhfa86c412002-02-02 15:01:15 +0000746 }
747 return rc;
748}
749
750/*
drhf57b14a2001-09-14 18:54:08 +0000751** Change the maximum number of in-memory pages that are allowed.
drhcd61c282002-03-06 22:01:34 +0000752**
753** The maximum number is the absolute value of the mxPage parameter.
754** If mxPage is negative, the noSync flag is also set. noSync bypasses
755** calls to sqliteOsSync(). The pager runs much faster with noSync on,
756** but if the operating system crashes or there is an abrupt power
757** failure, the database file might be left in an inconsistent and
758** unrepairable state.
drhf57b14a2001-09-14 18:54:08 +0000759*/
760void sqlitepager_set_cachesize(Pager *pPager, int mxPage){
drh603240c2002-03-05 01:11:12 +0000761 if( mxPage>=0 ){
drha1680452002-04-18 01:56:57 +0000762 pPager->noSync = pPager->tempFile;
drh603240c2002-03-05 01:11:12 +0000763 }else{
764 pPager->noSync = 1;
765 mxPage = -mxPage;
766 }
drhf57b14a2001-09-14 18:54:08 +0000767 if( mxPage>10 ){
768 pPager->mxPage = mxPage;
769 }
770}
771
772/*
drhfa86c412002-02-02 15:01:15 +0000773** Open a temporary file. Write the name of the file into zName
774** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write
775** the file descriptor into *fd. Return SQLITE_OK on success or some
776** other error code if we fail.
777**
778** The OS will automatically delete the temporary file when it is
779** closed.
780*/
781static int sqlitepager_opentemp(char *zFile, OsFile *fd){
782 int cnt = 8;
783 int rc;
784 do{
785 cnt--;
786 sqliteOsTempFileName(zFile);
787 rc = sqliteOsOpenExclusive(zFile, fd, 1);
788 }while( cnt>0 && rc!=SQLITE_OK );
789 return rc;
790}
791
792/*
drhed7c8552001-04-11 14:29:21 +0000793** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +0000794** The file to be cached need not exist. The file is not locked until
drhd9b02572001-04-15 00:37:09 +0000795** the first call to sqlitepager_get() and is only held open until the
796** last page is released using sqlitepager_unref().
drh382c0242001-10-06 16:33:02 +0000797**
drh6446c4d2001-12-15 14:22:18 +0000798** If zFilename is NULL then a randomly-named temporary file is created
799** and used as the file to be cached. The file will be deleted
800** automatically when it is closed.
drhed7c8552001-04-11 14:29:21 +0000801*/
drh7e3b0a02001-04-28 16:52:40 +0000802int sqlitepager_open(
803 Pager **ppPager, /* Return the Pager structure here */
804 const char *zFilename, /* Name of the database file to open */
805 int mxPage, /* Max number of in-memory cache pages */
drhda47d772002-12-02 04:25:19 +0000806 int nExtra, /* Extra bytes append to each in-memory page */
807 int useJournal /* TRUE to use a rollback journal on this file */
drh7e3b0a02001-04-28 16:52:40 +0000808){
drhed7c8552001-04-11 14:29:21 +0000809 Pager *pPager;
drh3e7a6092002-12-07 21:45:14 +0000810 char *zFullPathname;
drhed7c8552001-04-11 14:29:21 +0000811 int nameLen;
drh8cfbf082001-09-19 13:22:39 +0000812 OsFile fd;
813 int rc;
drh5e00f6c2001-09-13 13:46:56 +0000814 int tempFile;
815 int readOnly = 0;
drh8cfbf082001-09-19 13:22:39 +0000816 char zTemp[SQLITE_TEMPNAME_SIZE];
drhed7c8552001-04-11 14:29:21 +0000817
drhd9b02572001-04-15 00:37:09 +0000818 *ppPager = 0;
819 if( sqlite_malloc_failed ){
820 return SQLITE_NOMEM;
821 }
drh5e00f6c2001-09-13 13:46:56 +0000822 if( zFilename ){
drh3e7a6092002-12-07 21:45:14 +0000823 zFullPathname = sqliteOsFullPathname(zFilename);
824 rc = sqliteOsOpenReadWrite(zFullPathname, &fd, &readOnly);
drh5e00f6c2001-09-13 13:46:56 +0000825 tempFile = 0;
826 }else{
drhfa86c412002-02-02 15:01:15 +0000827 rc = sqlitepager_opentemp(zTemp, &fd);
drh5e00f6c2001-09-13 13:46:56 +0000828 zFilename = zTemp;
drh3e7a6092002-12-07 21:45:14 +0000829 zFullPathname = sqliteOsFullPathname(zFilename);
drh5e00f6c2001-09-13 13:46:56 +0000830 tempFile = 1;
831 }
drh3e7a6092002-12-07 21:45:14 +0000832 if( sqlite_malloc_failed ){
833 return SQLITE_NOMEM;
834 }
drh8cfbf082001-09-19 13:22:39 +0000835 if( rc!=SQLITE_OK ){
drh3e7a6092002-12-07 21:45:14 +0000836 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000837 return SQLITE_CANTOPEN;
838 }
drh3e7a6092002-12-07 21:45:14 +0000839 nameLen = strlen(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000840 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 );
drhd9b02572001-04-15 00:37:09 +0000841 if( pPager==0 ){
drha7fcb052001-12-14 15:09:55 +0000842 sqliteOsClose(&fd);
drh3e7a6092002-12-07 21:45:14 +0000843 sqliteFree(zFullPathname);
drhd9b02572001-04-15 00:37:09 +0000844 return SQLITE_NOMEM;
845 }
drhdb48ee02003-01-16 13:42:43 +0000846 SET_PAGER(pPager);
drhed7c8552001-04-11 14:29:21 +0000847 pPager->zFilename = (char*)&pPager[1];
848 pPager->zJournal = &pPager->zFilename[nameLen+1];
drh3e7a6092002-12-07 21:45:14 +0000849 strcpy(pPager->zFilename, zFullPathname);
850 strcpy(pPager->zJournal, zFullPathname);
851 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000852 strcpy(&pPager->zJournal[nameLen], "-journal");
853 pPager->fd = fd;
drh8cfbf082001-09-19 13:22:39 +0000854 pPager->journalOpen = 0;
drhda47d772002-12-02 04:25:19 +0000855 pPager->useJournal = useJournal;
drhfa86c412002-02-02 15:01:15 +0000856 pPager->ckptOpen = 0;
drh0f892532002-05-30 12:27:03 +0000857 pPager->ckptInUse = 0;
drhed7c8552001-04-11 14:29:21 +0000858 pPager->nRef = 0;
859 pPager->dbSize = -1;
drhfa86c412002-02-02 15:01:15 +0000860 pPager->ckptSize = 0;
861 pPager->ckptJSize = 0;
drhed7c8552001-04-11 14:29:21 +0000862 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +0000863 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +0000864 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000865 pPager->errMask = 0;
drh5e00f6c2001-09-13 13:46:56 +0000866 pPager->tempFile = tempFile;
867 pPager->readOnly = readOnly;
drhf57b14a2001-09-14 18:54:08 +0000868 pPager->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000869 pPager->noSync = pPager->tempFile || !useJournal;
drhed7c8552001-04-11 14:29:21 +0000870 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000871 pPager->pFirstSynced = 0;
drhed7c8552001-04-11 14:29:21 +0000872 pPager->pLast = 0;
drh7c717f72001-06-24 20:39:41 +0000873 pPager->nExtra = nExtra;
drhed7c8552001-04-11 14:29:21 +0000874 memset(pPager->aHash, 0, sizeof(pPager->aHash));
875 *ppPager = pPager;
876 return SQLITE_OK;
877}
878
879/*
drh72f82862001-05-24 21:06:34 +0000880** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +0000881** when the reference count on each page reaches zero. The destructor can
882** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +0000883**
884** The destructor is not called as a result sqlitepager_close().
885** Destructors are only called by sqlitepager_unref().
886*/
887void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){
888 pPager->xDestructor = xDesc;
889}
890
891/*
drh5e00f6c2001-09-13 13:46:56 +0000892** Return the total number of pages in the disk file associated with
893** pPager.
drhed7c8552001-04-11 14:29:21 +0000894*/
drhd9b02572001-04-15 00:37:09 +0000895int sqlitepager_pagecount(Pager *pPager){
drh28be87c2002-11-05 23:03:02 +0000896 off_t n;
drhd9b02572001-04-15 00:37:09 +0000897 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +0000898 if( pPager->dbSize>=0 ){
899 return pPager->dbSize;
900 }
drha7fcb052001-12-14 15:09:55 +0000901 if( sqliteOsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000902 pPager->errMask |= PAGER_ERR_DISK;
drh8cfbf082001-09-19 13:22:39 +0000903 return 0;
drhed7c8552001-04-11 14:29:21 +0000904 }
drh8cfbf082001-09-19 13:22:39 +0000905 n /= SQLITE_PAGE_SIZE;
drhd9b02572001-04-15 00:37:09 +0000906 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +0000907 pPager->dbSize = n;
908 }
909 return n;
910}
911
912/*
913** Shutdown the page cache. Free all memory and close all files.
914**
915** If a transaction was in progress when this routine is called, that
916** transaction is rolled back. All outstanding pages are invalidated
917** and their memory is freed. Any attempt to use a page associated
918** with this page cache after this function returns will likely
919** result in a coredump.
920*/
drhd9b02572001-04-15 00:37:09 +0000921int sqlitepager_close(Pager *pPager){
922 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +0000923 switch( pPager->state ){
924 case SQLITE_WRITELOCK: {
drhd9b02572001-04-15 00:37:09 +0000925 sqlitepager_rollback(pPager);
drha7fcb052001-12-14 15:09:55 +0000926 sqliteOsUnlock(&pPager->fd);
drh8cfbf082001-09-19 13:22:39 +0000927 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000928 break;
929 }
930 case SQLITE_READLOCK: {
drha7fcb052001-12-14 15:09:55 +0000931 sqliteOsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000932 break;
933 }
934 default: {
935 /* Do nothing */
936 break;
937 }
938 }
drhd9b02572001-04-15 00:37:09 +0000939 for(pPg=pPager->pAll; pPg; pPg=pNext){
940 pNext = pPg->pNextAll;
941 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000942 }
drha7fcb052001-12-14 15:09:55 +0000943 sqliteOsClose(&pPager->fd);
drh8cfbf082001-09-19 13:22:39 +0000944 assert( pPager->journalOpen==0 );
drh0f892532002-05-30 12:27:03 +0000945 /* Temp files are automatically deleted by the OS
946 ** if( pPager->tempFile ){
947 ** sqliteOsDelete(pPager->zFilename);
948 ** }
949 */
drhdb48ee02003-01-16 13:42:43 +0000950 CLR_PAGER(pPager);
drhed7c8552001-04-11 14:29:21 +0000951 sqliteFree(pPager);
952 return SQLITE_OK;
953}
954
955/*
drh5e00f6c2001-09-13 13:46:56 +0000956** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +0000957*/
drhd9b02572001-04-15 00:37:09 +0000958Pgno sqlitepager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +0000959 PgHdr *p = DATA_TO_PGHDR(pData);
960 return p->pgno;
961}
962
963/*
drh7e3b0a02001-04-28 16:52:40 +0000964** Increment the reference count for a page. If the page is
965** currently on the freelist (the reference count is zero) then
966** remove it from the freelist.
967*/
drh836faa42003-01-11 13:30:57 +0000968#define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
969static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +0000970 if( pPg->nRef==0 ){
971 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +0000972 if( pPg==pPg->pPager->pFirstSynced ){
973 PgHdr *p = pPg->pNextFree;
974 while( p && p->needSync ){ p = p->pNextFree; }
975 pPg->pPager->pFirstSynced = p;
976 }
drh7e3b0a02001-04-28 16:52:40 +0000977 if( pPg->pPrevFree ){
978 pPg->pPrevFree->pNextFree = pPg->pNextFree;
979 }else{
980 pPg->pPager->pFirst = pPg->pNextFree;
981 }
982 if( pPg->pNextFree ){
983 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
984 }else{
985 pPg->pPager->pLast = pPg->pPrevFree;
986 }
987 pPg->pPager->nRef++;
988 }
989 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +0000990 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +0000991}
992
993/*
994** Increment the reference count for a page. The input pointer is
995** a reference to the page data.
996*/
997int sqlitepager_ref(void *pData){
998 PgHdr *pPg = DATA_TO_PGHDR(pData);
999 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +00001000 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001001}
1002
1003/*
drhb19a2bc2001-09-16 00:13:26 +00001004** Sync the journal and then write all free dirty pages to the database
1005** file.
1006**
1007** Writing all free dirty pages to the database after the sync is a
1008** non-obvious optimization. fsync() is an expensive operation so we
drhaaab5722002-02-19 13:39:21 +00001009** want to minimize the number ot times it is called. After an fsync() call,
drh6446c4d2001-12-15 14:22:18 +00001010** we are free to write dirty pages back to the database. It is best
1011** to go ahead and write as many dirty pages as possible to minimize
1012** the risk of having to do another fsync() later on. Writing dirty
1013** free pages in this way was observed to make database operations go
1014** up to 10 times faster.
drhfa86c412002-02-02 15:01:15 +00001015**
1016** If we are writing to temporary database, there is no need to preserve
1017** the integrity of the journal file, so we can save time and skip the
1018** fsync().
drh50e5dad2001-09-15 00:57:28 +00001019*/
1020static int syncAllPages(Pager *pPager){
1021 PgHdr *pPg;
1022 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00001023
1024 /* Sync the journal before modifying the main database
1025 ** (assuming there is a journal and it needs to be synced.)
1026 */
drh50e5dad2001-09-15 00:57:28 +00001027 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00001028 if( !pPager->tempFile ){
drh968af522003-02-11 14:55:40 +00001029 off_t szJ;
drhdb48ee02003-01-16 13:42:43 +00001030 assert( pPager->journalOpen );
1031 assert( !pPager->noSync );
drh968af522003-02-11 14:55:40 +00001032#ifndef NDEBUG
1033 {
1034 off_t hdrSz, pgSz;
1035 hdrSz = JOURNAL_HDR_SZ(journal_format);
1036 pgSz = JOURNAL_PG_SZ(journal_format);
1037 rc = sqliteOsFileSize(&pPager->jfd, &pPager->syncJSize);
1038 if( rc!=0 ) return rc;
1039 assert( pPager->nRec*pgSz+hdrSz==pPager->syncJSize );
1040 }
1041#endif
1042 if( pPager->fullSync ){
1043 TRACE1("SYNC\n");
1044 rc = sqliteOsSync(&pPager->jfd);
1045 if( rc!=0 ) return rc;
1046 }
1047 sqliteOsSeek(&pPager->jfd, sizeof(aJournalMagic1));
1048 write32bits(&pPager->jfd, pPager->nRec);
1049 szJ = JOURNAL_HDR_SZ(journal_format) +
1050 pPager->nRec*JOURNAL_PG_SZ(journal_format);
1051 sqliteOsSeek(&pPager->jfd, szJ);
drhdb48ee02003-01-16 13:42:43 +00001052 TRACE1("SYNC\n");
drhfa86c412002-02-02 15:01:15 +00001053 rc = sqliteOsSync(&pPager->jfd);
1054 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00001055 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00001056 }
drh50e5dad2001-09-15 00:57:28 +00001057 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00001058
1059 /* Erase the needSync flag from every page.
1060 */
1061 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1062 pPg->needSync = 0;
1063 }
1064 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00001065 }
drh03eb96a2002-11-10 23:32:56 +00001066
drh341eae82003-01-21 02:39:36 +00001067#ifndef NDEBUG
1068 /* If the Pager.needSync flag is clear then the PgHdr.needSync
1069 ** flag must also be clear for all pages. Verify that this
1070 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00001071 */
drh341eae82003-01-21 02:39:36 +00001072 else{
1073 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1074 assert( pPg->needSync==0 );
1075 }
1076 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00001077 }
drh341eae82003-01-21 02:39:36 +00001078#endif
drhdb48ee02003-01-16 13:42:43 +00001079
drh81a20f22001-10-12 17:30:04 +00001080 return rc;
drh50e5dad2001-09-15 00:57:28 +00001081}
1082
1083/*
drh2554f8b2003-01-22 01:26:44 +00001084** Given a list of pages (connected by the PgHdr.pDirty pointer) write
1085** every one of those pages out to the database file and mark them all
1086** as clean.
1087*/
1088static int pager_write_pagelist(PgHdr *pList){
1089 Pager *pPager;
1090 int rc;
1091
1092 if( pList==0 ) return SQLITE_OK;
1093 pPager = pList->pPager;
1094 while( pList ){
1095 assert( pList->dirty );
1096 sqliteOsSeek(&pPager->fd, (pList->pgno-1)*(off_t)SQLITE_PAGE_SIZE);
1097 rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pList), SQLITE_PAGE_SIZE);
1098 if( rc ) return rc;
1099 pList->dirty = 0;
1100 pList = pList->pDirty;
1101 }
1102 return SQLITE_OK;
1103}
1104
1105/*
1106** Collect every dirty page into a dirty list and
1107** return a pointer to the head of that list. All pages are
1108** collected even if they are still in use.
1109*/
1110static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
1111 PgHdr *p, *pList;
1112 pList = 0;
1113 for(p=pPager->pAll; p; p=p->pNextAll){
1114 if( p->dirty ){
1115 p->pDirty = pList;
1116 pList = p;
1117 }
1118 }
1119 return pList;
1120}
1121
1122/*
drhd9b02572001-04-15 00:37:09 +00001123** Acquire a page.
1124**
drh58a11682001-11-10 13:51:08 +00001125** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00001126** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00001127**
drh306dc212001-05-21 13:45:10 +00001128** A _get works for any page number greater than 0. If the database
1129** file is smaller than the requested page, then no actual disk
1130** read occurs and the memory image of the page is initialized to
1131** all zeros. The extra data appended to a page is always initialized
1132** to zeros the first time a page is loaded into memory.
1133**
drhd9b02572001-04-15 00:37:09 +00001134** The acquisition might fail for several reasons. In all cases,
1135** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00001136**
1137** See also sqlitepager_lookup(). Both this routine and _lookup() attempt
1138** to find a page in the in-memory cache first. If the page is not already
drh5e00f6c2001-09-13 13:46:56 +00001139** in memory, this routine goes to disk to read it in whereas _lookup()
drh7e3b0a02001-04-28 16:52:40 +00001140** just returns 0. This routine acquires a read-lock the first time it
1141** has to go to disk, and could also playback an old journal if necessary.
1142** Since _lookup() never goes to disk, it never has to deal with locks
1143** or journal files.
drhed7c8552001-04-11 14:29:21 +00001144*/
drhd9b02572001-04-15 00:37:09 +00001145int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +00001146 PgHdr *pPg;
drh8766c342002-11-09 00:33:15 +00001147 int rc;
drhed7c8552001-04-11 14:29:21 +00001148
drhd9b02572001-04-15 00:37:09 +00001149 /* Make sure we have not hit any critical errors.
1150 */
drh836faa42003-01-11 13:30:57 +00001151 assert( pPager!=0 );
1152 assert( pgno!=0 );
drhd9b02572001-04-15 00:37:09 +00001153 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1154 return pager_errcode(pPager);
1155 }
1156
drhed7c8552001-04-11 14:29:21 +00001157 /* If this is the first page accessed, then get a read lock
1158 ** on the database file.
1159 */
1160 if( pPager->nRef==0 ){
drh8766c342002-11-09 00:33:15 +00001161 rc = sqliteOsReadLock(&pPager->fd);
1162 if( rc!=SQLITE_OK ){
drhed7c8552001-04-11 14:29:21 +00001163 *ppPage = 0;
drh8766c342002-11-09 00:33:15 +00001164 return rc;
drhed7c8552001-04-11 14:29:21 +00001165 }
drhd9b02572001-04-15 00:37:09 +00001166 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +00001167
1168 /* If a journal file exists, try to play it back.
1169 */
drhda47d772002-12-02 04:25:19 +00001170 if( pPager->useJournal && sqliteOsFileExists(pPager->zJournal) ){
drhf57b3392001-10-08 13:22:32 +00001171 int rc, dummy;
drhed7c8552001-04-11 14:29:21 +00001172
drha7fcb052001-12-14 15:09:55 +00001173 /* Get a write lock on the database
1174 */
1175 rc = sqliteOsWriteLock(&pPager->fd);
1176 if( rc!=SQLITE_OK ){
drh8766c342002-11-09 00:33:15 +00001177 if( sqliteOsUnlock(&pPager->fd)!=SQLITE_OK ){
1178 /* This should never happen! */
1179 rc = SQLITE_INTERNAL;
1180 }
drha7fcb052001-12-14 15:09:55 +00001181 *ppPage = 0;
drh8766c342002-11-09 00:33:15 +00001182 return rc;
drha7fcb052001-12-14 15:09:55 +00001183 }
1184 pPager->state = SQLITE_WRITELOCK;
1185
drhed7c8552001-04-11 14:29:21 +00001186 /* Open the journal for exclusive access. Return SQLITE_BUSY if
drhf57b3392001-10-08 13:22:32 +00001187 ** we cannot get exclusive access to the journal file.
1188 **
1189 ** Even though we will only be reading from the journal, not writing,
1190 ** we have to open the journal for writing in order to obtain an
1191 ** exclusive access lock.
drhed7c8552001-04-11 14:29:21 +00001192 */
drhf57b3392001-10-08 13:22:32 +00001193 rc = sqliteOsOpenReadWrite(pPager->zJournal, &pPager->jfd, &dummy);
drha7fcb052001-12-14 15:09:55 +00001194 if( rc!=SQLITE_OK ){
1195 rc = sqliteOsUnlock(&pPager->fd);
1196 assert( rc==SQLITE_OK );
drhed7c8552001-04-11 14:29:21 +00001197 *ppPage = 0;
1198 return SQLITE_BUSY;
1199 }
drha7fcb052001-12-14 15:09:55 +00001200 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001201 pPager->journalStarted = 0;
drhed7c8552001-04-11 14:29:21 +00001202
1203 /* Playback and delete the journal. Drop the database write
1204 ** lock and reacquire the read lock.
1205 */
drhd9b02572001-04-15 00:37:09 +00001206 rc = pager_playback(pPager);
1207 if( rc!=SQLITE_OK ){
1208 return rc;
1209 }
drhed7c8552001-04-11 14:29:21 +00001210 }
1211 pPg = 0;
1212 }else{
1213 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +00001214 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00001215 }
1216 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001217 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +00001218 int h;
drh7e3b0a02001-04-28 16:52:40 +00001219 pPager->nMiss++;
drhed7c8552001-04-11 14:29:21 +00001220 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){
1221 /* Create a new page */
drh968af522003-02-11 14:55:40 +00001222 pPg = sqliteMallocRaw( sizeof(*pPg) + SQLITE_PAGE_SIZE
1223 + sizeof(u32) + pPager->nExtra );
drhd9b02572001-04-15 00:37:09 +00001224 if( pPg==0 ){
1225 *ppPage = 0;
1226 pager_unwritelock(pPager);
1227 pPager->errMask |= PAGER_ERR_MEM;
1228 return SQLITE_NOMEM;
1229 }
drh8c1238a2003-01-02 14:43:55 +00001230 memset(pPg, 0, sizeof(*pPg));
drhed7c8552001-04-11 14:29:21 +00001231 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +00001232 pPg->pNextAll = pPager->pAll;
1233 if( pPager->pAll ){
1234 pPager->pAll->pPrevAll = pPg;
1235 }
1236 pPg->pPrevAll = 0;
drhd79caeb2001-04-15 02:27:24 +00001237 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +00001238 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +00001239 }else{
drhdb48ee02003-01-16 13:42:43 +00001240 /* Find a page to recycle. Try to locate a page that does not
1241 ** require us to do an fsync() on the journal.
1242 */
drh341eae82003-01-21 02:39:36 +00001243 pPg = pPager->pFirstSynced;
drhb19a2bc2001-09-16 00:13:26 +00001244
drhdb48ee02003-01-16 13:42:43 +00001245 /* If we could not find a page that does not require an fsync()
1246 ** on the journal file then fsync the journal file. This is a
1247 ** very slow operation, so we work hard to avoid it. But sometimes
1248 ** it can't be helped.
drhb19a2bc2001-09-16 00:13:26 +00001249 */
drh603240c2002-03-05 01:11:12 +00001250 if( pPg==0 ){
drh50e5dad2001-09-15 00:57:28 +00001251 int rc = syncAllPages(pPager);
1252 if( rc!=0 ){
1253 sqlitepager_rollback(pPager);
1254 *ppPage = 0;
1255 return SQLITE_IOERR;
1256 }
1257 pPg = pPager->pFirst;
1258 }
drhd9b02572001-04-15 00:37:09 +00001259 assert( pPg->nRef==0 );
drhdb48ee02003-01-16 13:42:43 +00001260
1261 /* Write the page to the database file if it is dirty.
1262 */
1263 if( pPg->dirty ){
1264 assert( pPg->needSync==0 );
drh2554f8b2003-01-22 01:26:44 +00001265 pPg->pDirty = 0;
1266 rc = pager_write_pagelist( pPg );
drhdb48ee02003-01-16 13:42:43 +00001267 if( rc!=SQLITE_OK ){
1268 sqlitepager_rollback(pPager);
1269 *ppPage = 0;
1270 return SQLITE_IOERR;
1271 }
drhdb48ee02003-01-16 13:42:43 +00001272 }
drh50e5dad2001-09-15 00:57:28 +00001273 assert( pPg->dirty==0 );
drhd9b02572001-04-15 00:37:09 +00001274
drhdb48ee02003-01-16 13:42:43 +00001275 /* If the page we are recycling is marked as alwaysRollback, then
drh193a6b42002-07-07 16:52:46 +00001276 ** set the global alwaysRollback flag, thus disabling the
1277 ** sqlite_dont_rollback() optimization for the rest of this transaction.
1278 ** It is necessary to do this because the page marked alwaysRollback
1279 ** might be reloaded at a later time but at that point we won't remember
1280 ** that is was marked alwaysRollback. This means that all pages must
1281 ** be marked as alwaysRollback from here on out.
1282 */
1283 if( pPg->alwaysRollback ){
1284 pPager->alwaysRollback = 1;
1285 }
1286
drhd9b02572001-04-15 00:37:09 +00001287 /* Unlink the old page from the free list and the hash table
1288 */
drh341eae82003-01-21 02:39:36 +00001289 if( pPg==pPager->pFirstSynced ){
1290 PgHdr *p = pPg->pNextFree;
1291 while( p && p->needSync ){ p = p->pNextFree; }
1292 pPager->pFirstSynced = p;
1293 }
drh6019e162001-07-02 17:51:45 +00001294 if( pPg->pPrevFree ){
1295 pPg->pPrevFree->pNextFree = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +00001296 }else{
drh6019e162001-07-02 17:51:45 +00001297 assert( pPager->pFirst==pPg );
1298 pPager->pFirst = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +00001299 }
drh6019e162001-07-02 17:51:45 +00001300 if( pPg->pNextFree ){
1301 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1302 }else{
1303 assert( pPager->pLast==pPg );
1304 pPager->pLast = pPg->pPrevFree;
1305 }
1306 pPg->pNextFree = pPg->pPrevFree = 0;
drhed7c8552001-04-11 14:29:21 +00001307 if( pPg->pNextHash ){
1308 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
1309 }
1310 if( pPg->pPrevHash ){
1311 pPg->pPrevHash->pNextHash = pPg->pNextHash;
1312 }else{
drhd9b02572001-04-15 00:37:09 +00001313 h = pager_hash(pPg->pgno);
drhed7c8552001-04-11 14:29:21 +00001314 assert( pPager->aHash[h]==pPg );
1315 pPager->aHash[h] = pPg->pNextHash;
1316 }
drh6019e162001-07-02 17:51:45 +00001317 pPg->pNextHash = pPg->pPrevHash = 0;
drhd9b02572001-04-15 00:37:09 +00001318 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +00001319 }
1320 pPg->pgno = pgno;
drh1ab43002002-01-14 09:28:19 +00001321 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
drhed6c8672003-01-12 18:02:16 +00001322 sqliteCheckMemory(pPager->aInJournal, pgno/8);
drhdb48ee02003-01-16 13:42:43 +00001323 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00001324 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00001325 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001326 }else{
1327 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00001328 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001329 }
drh03eb96a2002-11-10 23:32:56 +00001330 if( pPager->aInCkpt && (int)pgno<=pPager->ckptSize
1331 && (pPager->aInCkpt[pgno/8] & (1<<(pgno&7)))!=0 ){
1332 page_add_to_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001333 }else{
drh03eb96a2002-11-10 23:32:56 +00001334 page_remove_from_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001335 }
drhed7c8552001-04-11 14:29:21 +00001336 pPg->dirty = 0;
1337 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00001338 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001339 pPager->nRef++;
1340 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +00001341 pPg->pNextHash = pPager->aHash[h];
1342 pPager->aHash[h] = pPg;
1343 if( pPg->pNextHash ){
1344 assert( pPg->pNextHash->pPrevHash==0 );
1345 pPg->pNextHash->pPrevHash = pPg;
1346 }
drh306dc212001-05-21 13:45:10 +00001347 if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager);
drh1ab43002002-01-14 09:28:19 +00001348 if( pPager->dbSize<(int)pgno ){
drh306dc212001-05-21 13:45:10 +00001349 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
1350 }else{
drh81a20f22001-10-12 17:30:04 +00001351 int rc;
drhd0d006e2002-12-01 02:00:57 +00001352 sqliteOsSeek(&pPager->fd, (pgno-1)*(off_t)SQLITE_PAGE_SIZE);
drha7fcb052001-12-14 15:09:55 +00001353 rc = sqliteOsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drh81a20f22001-10-12 17:30:04 +00001354 if( rc!=SQLITE_OK ){
drh28be87c2002-11-05 23:03:02 +00001355 off_t fileSize;
drh4e371ee2002-09-05 16:08:27 +00001356 if( sqliteOsFileSize(&pPager->fd,&fileSize)!=SQLITE_OK
1357 || fileSize>=pgno*SQLITE_PAGE_SIZE ){
1358 return rc;
1359 }else{
1360 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
1361 }
drh81a20f22001-10-12 17:30:04 +00001362 }
drh306dc212001-05-21 13:45:10 +00001363 }
drh7e3b0a02001-04-28 16:52:40 +00001364 if( pPager->nExtra>0 ){
1365 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
1366 }
drhed7c8552001-04-11 14:29:21 +00001367 }else{
drhd9b02572001-04-15 00:37:09 +00001368 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +00001369 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +00001370 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00001371 }
1372 *ppPage = PGHDR_TO_DATA(pPg);
1373 return SQLITE_OK;
1374}
1375
1376/*
drh7e3b0a02001-04-28 16:52:40 +00001377** Acquire a page if it is already in the in-memory cache. Do
1378** not read the page from disk. Return a pointer to the page,
1379** or 0 if the page is not in cache.
1380**
1381** See also sqlitepager_get(). The difference between this routine
1382** and sqlitepager_get() is that _get() will go to the disk and read
1383** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00001384** returns NULL if the page is not in cache or if a disk I/O error
1385** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00001386*/
1387void *sqlitepager_lookup(Pager *pPager, Pgno pgno){
1388 PgHdr *pPg;
1389
drh836faa42003-01-11 13:30:57 +00001390 assert( pPager!=0 );
1391 assert( pgno!=0 );
drh7e3b0a02001-04-28 16:52:40 +00001392 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1393 return 0;
1394 }
drh836faa42003-01-11 13:30:57 +00001395 /* if( pPager->nRef==0 ){
1396 ** return 0;
1397 ** }
1398 */
drh7e3b0a02001-04-28 16:52:40 +00001399 pPg = pager_lookup(pPager, pgno);
1400 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +00001401 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +00001402 return PGHDR_TO_DATA(pPg);
1403}
1404
1405/*
drhed7c8552001-04-11 14:29:21 +00001406** Release a page.
1407**
1408** If the number of references to the page drop to zero, then the
1409** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00001410** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00001411** removed.
1412*/
drhd9b02572001-04-15 00:37:09 +00001413int sqlitepager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +00001414 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001415
1416 /* Decrement the reference count for this page
1417 */
drhed7c8552001-04-11 14:29:21 +00001418 pPg = DATA_TO_PGHDR(pData);
1419 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +00001420 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00001421 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001422
drh72f82862001-05-24 21:06:34 +00001423 /* When the number of references to a page reach 0, call the
1424 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00001425 */
drhed7c8552001-04-11 14:29:21 +00001426 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00001427 Pager *pPager;
1428 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00001429 pPg->pNextFree = 0;
1430 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00001431 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00001432 if( pPg->pPrevFree ){
1433 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00001434 }else{
1435 pPager->pFirst = pPg;
1436 }
drh341eae82003-01-21 02:39:36 +00001437 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
1438 pPager->pFirstSynced = pPg;
1439 }
drh72f82862001-05-24 21:06:34 +00001440 if( pPager->xDestructor ){
1441 pPager->xDestructor(pData);
1442 }
drhd9b02572001-04-15 00:37:09 +00001443
1444 /* When all pages reach the freelist, drop the read lock from
1445 ** the database file.
1446 */
1447 pPager->nRef--;
1448 assert( pPager->nRef>=0 );
1449 if( pPager->nRef==0 ){
1450 pager_reset(pPager);
1451 }
drhed7c8552001-04-11 14:29:21 +00001452 }
drhd9b02572001-04-15 00:37:09 +00001453 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00001454}
1455
1456/*
drhda47d772002-12-02 04:25:19 +00001457** Create a journal file for pPager. There should already be a write
1458** lock on the database file when this routine is called.
1459**
1460** Return SQLITE_OK if everything. Return an error code and release the
1461** write lock if anything goes wrong.
1462*/
1463static int pager_open_journal(Pager *pPager){
1464 int rc;
1465 assert( pPager->state==SQLITE_WRITELOCK );
1466 assert( pPager->journalOpen==0 );
1467 assert( pPager->useJournal );
1468 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
1469 if( pPager->aInJournal==0 ){
1470 sqliteOsReadLock(&pPager->fd);
1471 pPager->state = SQLITE_READLOCK;
1472 return SQLITE_NOMEM;
1473 }
1474 rc = sqliteOsOpenExclusive(pPager->zJournal, &pPager->jfd,pPager->tempFile);
1475 if( rc!=SQLITE_OK ){
1476 sqliteFree(pPager->aInJournal);
1477 pPager->aInJournal = 0;
1478 sqliteOsReadLock(&pPager->fd);
1479 pPager->state = SQLITE_READLOCK;
1480 return SQLITE_CANTOPEN;
1481 }
1482 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001483 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00001484 pPager->needSync = 0;
1485 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00001486 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001487 sqlitepager_pagecount(pPager);
1488 pPager->origDbSize = pPager->dbSize;
drh968af522003-02-11 14:55:40 +00001489 if( journal_format==JOURNAL_FORMAT_3 ){
1490 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic3, sizeof(aJournalMagic3));
1491 if( rc==SQLITE_OK ){
1492 rc = write32bits(&pPager->jfd, pPager->tempFile ? 0xffffffff : 0);
1493 }
1494 if( rc==SQLITE_OK ){
1495 pPager->cksumInit = (u32)sqliteRandomInteger();
1496 rc = write32bits(&pPager->jfd, pPager->cksumInit);
1497 }
1498 }else if( journal_format==JOURNAL_FORMAT_2 ){
1499 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic2, sizeof(aJournalMagic2));
drhda47d772002-12-02 04:25:19 +00001500 }else{
drh968af522003-02-11 14:55:40 +00001501 assert( journal_format==JOURNAL_FORMAT_1 );
1502 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic1, sizeof(aJournalMagic1));
drhda47d772002-12-02 04:25:19 +00001503 }
1504 if( rc==SQLITE_OK ){
1505 rc = write32bits(&pPager->jfd, pPager->dbSize);
1506 }
1507 if( pPager->ckptAutoopen && rc==SQLITE_OK ){
1508 rc = sqlitepager_ckpt_begin(pPager);
1509 }
1510 if( rc!=SQLITE_OK ){
1511 rc = pager_unwritelock(pPager);
1512 if( rc==SQLITE_OK ){
1513 rc = SQLITE_FULL;
1514 }
1515 }
drhdb48ee02003-01-16 13:42:43 +00001516#ifndef NDEBUG
1517 pPager->syncJSize = 0;
1518#endif
drhda47d772002-12-02 04:25:19 +00001519 return rc;
1520}
1521
1522/*
drh4b845d72002-03-05 12:41:19 +00001523** Acquire a write-lock on the database. The lock is removed when
1524** the any of the following happen:
1525**
1526** * sqlitepager_commit() is called.
1527** * sqlitepager_rollback() is called.
1528** * sqlitepager_close() is called.
1529** * sqlitepager_unref() is called to on every outstanding page.
1530**
1531** The parameter to this routine is a pointer to any open page of the
1532** database file. Nothing changes about the page - it is used merely
1533** to acquire a pointer to the Pager structure and as proof that there
1534** is already a read-lock on the database.
1535**
drhda47d772002-12-02 04:25:19 +00001536** A journal file is opened if this is not a temporary file. For
1537** temporary files, the opening of the journal file is deferred until
1538** there is an actual need to write to the journal.
1539**
drh4b845d72002-03-05 12:41:19 +00001540** If the database is already write-locked, this routine is a no-op.
1541*/
1542int sqlitepager_begin(void *pData){
1543 PgHdr *pPg = DATA_TO_PGHDR(pData);
1544 Pager *pPager = pPg->pPager;
1545 int rc = SQLITE_OK;
1546 assert( pPg->nRef>0 );
1547 assert( pPager->state!=SQLITE_UNLOCK );
1548 if( pPager->state==SQLITE_READLOCK ){
1549 assert( pPager->aInJournal==0 );
1550 rc = sqliteOsWriteLock(&pPager->fd);
1551 if( rc!=SQLITE_OK ){
1552 return rc;
1553 }
drh4b845d72002-03-05 12:41:19 +00001554 pPager->state = SQLITE_WRITELOCK;
drhda47d772002-12-02 04:25:19 +00001555 pPager->dirtyFile = 0;
drhdb48ee02003-01-16 13:42:43 +00001556 TRACE1("TRANSACTION\n");
drhda47d772002-12-02 04:25:19 +00001557 if( pPager->useJournal && !pPager->tempFile ){
1558 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00001559 }
1560 }
1561 return rc;
1562}
1563
1564/*
drhed7c8552001-04-11 14:29:21 +00001565** Mark a data page as writeable. The page is written into the journal
1566** if it is not there already. This routine must be called before making
1567** changes to a page.
1568**
1569** The first time this routine is called, the pager creates a new
1570** journal and acquires a write lock on the database. If the write
1571** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00001572** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00001573** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00001574**
1575** If the journal file could not be written because the disk is full,
1576** then this routine returns SQLITE_FULL and does an immediate rollback.
1577** All subsequent write attempts also return SQLITE_FULL until there
1578** is a call to sqlitepager_commit() or sqlitepager_rollback() to
1579** reset.
drhed7c8552001-04-11 14:29:21 +00001580*/
drhd9b02572001-04-15 00:37:09 +00001581int sqlitepager_write(void *pData){
drh69688d52001-04-14 16:38:23 +00001582 PgHdr *pPg = DATA_TO_PGHDR(pData);
1583 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00001584 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00001585
drh6446c4d2001-12-15 14:22:18 +00001586 /* Check for errors
1587 */
drhd9b02572001-04-15 00:37:09 +00001588 if( pPager->errMask ){
1589 return pager_errcode(pPager);
1590 }
drh5e00f6c2001-09-13 13:46:56 +00001591 if( pPager->readOnly ){
1592 return SQLITE_PERM;
1593 }
drh6446c4d2001-12-15 14:22:18 +00001594
1595 /* Mark the page as dirty. If the page has already been written
1596 ** to the journal then we can return right away.
1597 */
drhd9b02572001-04-15 00:37:09 +00001598 pPg->dirty = 1;
drh0f892532002-05-30 12:27:03 +00001599 if( pPg->inJournal && (pPg->inCkpt || pPager->ckptInUse==0) ){
drha1680452002-04-18 01:56:57 +00001600 pPager->dirtyFile = 1;
drhfa86c412002-02-02 15:01:15 +00001601 return SQLITE_OK;
1602 }
drh6446c4d2001-12-15 14:22:18 +00001603
1604 /* If we get this far, it means that the page needs to be
drhfa86c412002-02-02 15:01:15 +00001605 ** written to the transaction journal or the ckeckpoint journal
1606 ** or both.
1607 **
1608 ** First check to see that the transaction journal exists and
1609 ** create it if it does not.
drh6446c4d2001-12-15 14:22:18 +00001610 */
drhd9b02572001-04-15 00:37:09 +00001611 assert( pPager->state!=SQLITE_UNLOCK );
drh4b845d72002-03-05 12:41:19 +00001612 rc = sqlitepager_begin(pData);
drhda47d772002-12-02 04:25:19 +00001613 if( rc!=SQLITE_OK ){
1614 return rc;
1615 }
drhd9b02572001-04-15 00:37:09 +00001616 assert( pPager->state==SQLITE_WRITELOCK );
drhda47d772002-12-02 04:25:19 +00001617 if( !pPager->journalOpen && pPager->useJournal ){
1618 rc = pager_open_journal(pPager);
1619 if( rc!=SQLITE_OK ) return rc;
1620 }
1621 assert( pPager->journalOpen || !pPager->useJournal );
1622 pPager->dirtyFile = 1;
drh6446c4d2001-12-15 14:22:18 +00001623
drhfa86c412002-02-02 15:01:15 +00001624 /* The transaction journal now exists and we have a write lock on the
1625 ** main database file. Write the current page to the transaction
1626 ** journal if it is not there already.
drh6446c4d2001-12-15 14:22:18 +00001627 */
drhdb48ee02003-01-16 13:42:43 +00001628 if( !pPg->inJournal && pPager->useJournal ){
1629 if( (int)pPg->pgno <= pPager->origDbSize ){
drh968af522003-02-11 14:55:40 +00001630 int szPg;
1631 u32 saved;
1632 if( journal_format>=JOURNAL_FORMAT_3 ){
1633 u32 cksum = pager_cksum(pPager, pPg->pgno, pData);
1634 saved = *(u32*)PGHDR_TO_EXTRA(pPg);
1635 store32bits(cksum, pPg, SQLITE_PAGE_SIZE);
1636 szPg = SQLITE_PAGE_SIZE+8;
1637 }else{
1638 szPg = SQLITE_PAGE_SIZE+4;
1639 }
1640 store32bits(pPg->pgno, pPg, -4);
1641 rc = sqliteOsWrite(&pPager->jfd, &((char*)pData)[-4], szPg);
1642 if( journal_format>=JOURNAL_FORMAT_3 ){
1643 *(u32*)PGHDR_TO_EXTRA(pPg) = saved;
1644 }
1645 pPager->nRec++;
drhdb48ee02003-01-16 13:42:43 +00001646 if( rc!=SQLITE_OK ){
1647 sqlitepager_rollback(pPager);
1648 pPager->errMask |= PAGER_ERR_FULL;
1649 return rc;
1650 }
1651 assert( pPager->aInJournal!=0 );
1652 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1653 pPg->needSync = !pPager->noSync;
1654 pPg->inJournal = 1;
1655 if( pPager->ckptInUse ){
1656 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1657 page_add_to_ckpt_list(pPg);
1658 }
1659 TRACE3("JOURNAL %d %d\n", pPg->pgno, pPg->needSync);
1660 }else{
1661 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
1662 TRACE3("APPEND %d %d\n", pPg->pgno, pPg->needSync);
drhd9b02572001-04-15 00:37:09 +00001663 }
drhdb48ee02003-01-16 13:42:43 +00001664 if( pPg->needSync ){
1665 pPager->needSync = 1;
drhfa86c412002-02-02 15:01:15 +00001666 }
drh69688d52001-04-14 16:38:23 +00001667 }
drh6446c4d2001-12-15 14:22:18 +00001668
drhfa86c412002-02-02 15:01:15 +00001669 /* If the checkpoint journal is open and the page is not in it,
drh968af522003-02-11 14:55:40 +00001670 ** then write the current page to the checkpoint journal. Note that
1671 ** the checkpoint journal always uses the simplier format 2 that lacks
1672 ** checksums. The header is also omitted from the checkpoint journal.
drh6446c4d2001-12-15 14:22:18 +00001673 */
drh0f892532002-05-30 12:27:03 +00001674 if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
drh1e336b42002-02-14 12:50:33 +00001675 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drh968af522003-02-11 14:55:40 +00001676 store32bits(pPg->pgno, pPg, -4);
drh2554f8b2003-01-22 01:26:44 +00001677 rc = sqliteOsWrite(&pPager->cpfd, &((char*)pData)[-4], SQLITE_PAGE_SIZE+4);
drhfa86c412002-02-02 15:01:15 +00001678 if( rc!=SQLITE_OK ){
1679 sqlitepager_rollback(pPager);
1680 pPager->errMask |= PAGER_ERR_FULL;
1681 return rc;
1682 }
drh9bd47a92003-01-07 14:46:08 +00001683 pPager->ckptNRec++;
drhfa86c412002-02-02 15:01:15 +00001684 assert( pPager->aInCkpt!=0 );
1685 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001686 page_add_to_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001687 }
1688
1689 /* Update the database size and return.
1690 */
drh1ab43002002-01-14 09:28:19 +00001691 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00001692 pPager->dbSize = pPg->pgno;
1693 }
drh69688d52001-04-14 16:38:23 +00001694 return rc;
drhed7c8552001-04-11 14:29:21 +00001695}
1696
1697/*
drhaacc5432002-01-06 17:07:40 +00001698** Return TRUE if the page given in the argument was previously passed
drh6019e162001-07-02 17:51:45 +00001699** to sqlitepager_write(). In other words, return TRUE if it is ok
1700** to change the content of the page.
1701*/
1702int sqlitepager_iswriteable(void *pData){
1703 PgHdr *pPg = DATA_TO_PGHDR(pData);
1704 return pPg->dirty;
1705}
1706
1707/*
drh30e58752002-03-02 20:41:57 +00001708** A call to this routine tells the pager that it is not necessary to
1709** write the information on page "pgno" back to the disk, even though
1710** that page might be marked as dirty.
1711**
1712** The overlying software layer calls this routine when all of the data
1713** on the given page is unused. The pager marks the page as clean so
1714** that it does not get written to disk.
1715**
1716** Tests show that this optimization, together with the
1717** sqlitepager_dont_rollback() below, more than double the speed
1718** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00001719**
1720** When this routine is called, set the alwaysRollback flag to true.
1721** Subsequent calls to sqlitepager_dont_rollback() for the same page
1722** will thereafter be ignored. This is necessary to avoid a problem
1723** where a page with data is added to the freelist during one part of
1724** a transaction then removed from the freelist during a later part
1725** of the same transaction and reused for some other purpose. When it
1726** is first added to the freelist, this routine is called. When reused,
1727** the dont_rollback() routine is called. But because the page contains
1728** critical data, we still need to be sure it gets rolled back in spite
1729** of the dont_rollback() call.
drh30e58752002-03-02 20:41:57 +00001730*/
1731void sqlitepager_dont_write(Pager *pPager, Pgno pgno){
1732 PgHdr *pPg;
drh8e298f92002-07-06 16:28:47 +00001733
drh30e58752002-03-02 20:41:57 +00001734 pPg = pager_lookup(pPager, pgno);
drh8e298f92002-07-06 16:28:47 +00001735 pPg->alwaysRollback = 1;
drh30e58752002-03-02 20:41:57 +00001736 if( pPg && pPg->dirty ){
drh8124a302002-06-25 14:43:57 +00001737 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
1738 /* If this pages is the last page in the file and the file has grown
1739 ** during the current transaction, then do NOT mark the page as clean.
1740 ** When the database file grows, we must make sure that the last page
1741 ** gets written at least once so that the disk file will be the correct
1742 ** size. If you do not write this page and the size of the file
1743 ** on the disk ends up being too small, that can lead to database
1744 ** corruption during the next transaction.
1745 */
1746 }else{
drhdb48ee02003-01-16 13:42:43 +00001747 TRACE2("DONT_WRITE %d\n", pgno);
drh8124a302002-06-25 14:43:57 +00001748 pPg->dirty = 0;
1749 }
drh30e58752002-03-02 20:41:57 +00001750 }
1751}
1752
1753/*
1754** A call to this routine tells the pager that if a rollback occurs,
1755** it is not necessary to restore the data on the given page. This
1756** means that the pager does not have to record the given page in the
1757** rollback journal.
1758*/
1759void sqlitepager_dont_rollback(void *pData){
1760 PgHdr *pPg = DATA_TO_PGHDR(pData);
1761 Pager *pPager = pPg->pPager;
1762
1763 if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return;
drh193a6b42002-07-07 16:52:46 +00001764 if( pPg->alwaysRollback || pPager->alwaysRollback ) return;
drh30e58752002-03-02 20:41:57 +00001765 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
1766 assert( pPager->aInJournal!=0 );
1767 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1768 pPg->inJournal = 1;
drh0f892532002-05-30 12:27:03 +00001769 if( pPager->ckptInUse ){
drh30e58752002-03-02 20:41:57 +00001770 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001771 page_add_to_ckpt_list(pPg);
drh30e58752002-03-02 20:41:57 +00001772 }
drhdb48ee02003-01-16 13:42:43 +00001773 TRACE2("DONT_ROLLBACK %d\n", pPg->pgno);
drh30e58752002-03-02 20:41:57 +00001774 }
drh0f892532002-05-30 12:27:03 +00001775 if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
drh30e58752002-03-02 20:41:57 +00001776 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
1777 assert( pPager->aInCkpt!=0 );
1778 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001779 page_add_to_ckpt_list(pPg);
drh30e58752002-03-02 20:41:57 +00001780 }
1781}
1782
1783/*
drhed7c8552001-04-11 14:29:21 +00001784** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00001785**
1786** If the commit fails for any reason, a rollback attempt is made
1787** and an error code is returned. If the commit worked, SQLITE_OK
1788** is returned.
drhed7c8552001-04-11 14:29:21 +00001789*/
drhd9b02572001-04-15 00:37:09 +00001790int sqlitepager_commit(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00001791 int rc;
drhed7c8552001-04-11 14:29:21 +00001792 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001793
1794 if( pPager->errMask==PAGER_ERR_FULL ){
1795 rc = sqlitepager_rollback(pPager);
drh4e371ee2002-09-05 16:08:27 +00001796 if( rc==SQLITE_OK ){
1797 rc = SQLITE_FULL;
1798 }
drhd9b02572001-04-15 00:37:09 +00001799 return rc;
1800 }
1801 if( pPager->errMask!=0 ){
1802 rc = pager_errcode(pPager);
1803 return rc;
1804 }
1805 if( pPager->state!=SQLITE_WRITELOCK ){
1806 return SQLITE_ERROR;
1807 }
drhdb48ee02003-01-16 13:42:43 +00001808 TRACE1("COMMIT\n");
drha1680452002-04-18 01:56:57 +00001809 if( pPager->dirtyFile==0 ){
1810 /* Exit early (without doing the time-consuming sqliteOsSync() calls)
1811 ** if there have been no changes to the database file. */
drh341eae82003-01-21 02:39:36 +00001812 assert( pPager->needSync==0 );
drha1680452002-04-18 01:56:57 +00001813 rc = pager_unwritelock(pPager);
1814 pPager->dbSize = -1;
1815 return rc;
1816 }
drhda47d772002-12-02 04:25:19 +00001817 assert( pPager->journalOpen );
drha7fcb052001-12-14 15:09:55 +00001818 if( pPager->needSync && sqliteOsSync(&pPager->jfd)!=SQLITE_OK ){
drhd9b02572001-04-15 00:37:09 +00001819 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +00001820 }
drh2554f8b2003-01-22 01:26:44 +00001821 pPg = pager_get_all_dirty_pages(pPager);
1822 if( pPg ){
1823 rc = pager_write_pagelist(pPg);
1824 if( rc || (!pPager->noSync && sqliteOsSync(&pPager->fd)!=SQLITE_OK) ){
1825 goto commit_abort;
1826 }
drh603240c2002-03-05 01:11:12 +00001827 }
drhd9b02572001-04-15 00:37:09 +00001828 rc = pager_unwritelock(pPager);
1829 pPager->dbSize = -1;
1830 return rc;
1831
1832 /* Jump here if anything goes wrong during the commit process.
1833 */
1834commit_abort:
1835 rc = sqlitepager_rollback(pPager);
1836 if( rc==SQLITE_OK ){
1837 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +00001838 }
drhed7c8552001-04-11 14:29:21 +00001839 return rc;
1840}
1841
1842/*
1843** Rollback all changes. The database falls back to read-only mode.
1844** All in-memory cache pages revert to their original data contents.
1845** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00001846**
1847** This routine cannot fail unless some other process is not following
1848** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
1849** process is writing trash into the journal file (SQLITE_CORRUPT) or
1850** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
1851** codes are returned for all these occasions. Otherwise,
1852** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00001853*/
drhd9b02572001-04-15 00:37:09 +00001854int sqlitepager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001855 int rc;
drhdb48ee02003-01-16 13:42:43 +00001856 TRACE1("ROLLBACK\n");
drhda47d772002-12-02 04:25:19 +00001857 if( !pPager->dirtyFile || !pPager->journalOpen ){
1858 rc = pager_unwritelock(pPager);
1859 pPager->dbSize = -1;
1860 return rc;
1861 }
drhdb48ee02003-01-16 13:42:43 +00001862
1863#if defined(SQLITE_TEST) && !defined(NDEBUG)
1864 /* Truncate the journal to the size it was at the conclusion of the
1865 ** last sqliteOsSync() call. This is really an error check. If the
1866 ** rollback still works, it means that the rollback would have also
1867 ** worked if it had occurred after an OS crash or unexpected power
1868 ** loss.
1869 */
drha218b6a2003-01-25 15:43:22 +00001870 if( !pPager->noSync ){
drh968af522003-02-11 14:55:40 +00001871 int m = JOURNAL_HDR_SZ(journal_format);
drha218b6a2003-01-25 15:43:22 +00001872 assert( !pPager->tempFile );
drh968af522003-02-11 14:55:40 +00001873 if( pPager->syncJSize<m ){
1874 pPager->syncJSize = m;
drha218b6a2003-01-25 15:43:22 +00001875 }
1876 TRACE2("TRUNCATE JOURNAL %lld\n", pPager->syncJSize);
1877 rc = sqliteOsTruncate(&pPager->jfd, pPager->syncJSize);
1878 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00001879 pPager->nRec = 0;
drhdb48ee02003-01-16 13:42:43 +00001880 }
drhdb48ee02003-01-16 13:42:43 +00001881#endif
1882
drhd9b02572001-04-15 00:37:09 +00001883 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
drh4b845d72002-03-05 12:41:19 +00001884 if( pPager->state>=SQLITE_WRITELOCK ){
1885 pager_playback(pPager);
1886 }
drhd9b02572001-04-15 00:37:09 +00001887 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00001888 }
drhd9b02572001-04-15 00:37:09 +00001889 if( pPager->state!=SQLITE_WRITELOCK ){
1890 return SQLITE_OK;
1891 }
1892 rc = pager_playback(pPager);
1893 if( rc!=SQLITE_OK ){
1894 rc = SQLITE_CORRUPT;
1895 pPager->errMask |= PAGER_ERR_CORRUPT;
1896 }
1897 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00001898 return rc;
drh98808ba2001-10-18 12:34:46 +00001899}
drhd9b02572001-04-15 00:37:09 +00001900
1901/*
drh5e00f6c2001-09-13 13:46:56 +00001902** Return TRUE if the database file is opened read-only. Return FALSE
1903** if the database is (in theory) writable.
1904*/
1905int sqlitepager_isreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00001906 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00001907}
1908
1909/*
drhd9b02572001-04-15 00:37:09 +00001910** This routine is used for testing and analysis only.
1911*/
1912int *sqlitepager_stats(Pager *pPager){
1913 static int a[9];
1914 a[0] = pPager->nRef;
1915 a[1] = pPager->nPage;
1916 a[2] = pPager->mxPage;
1917 a[3] = pPager->dbSize;
1918 a[4] = pPager->state;
1919 a[5] = pPager->errMask;
1920 a[6] = pPager->nHit;
1921 a[7] = pPager->nMiss;
1922 a[8] = pPager->nOvfl;
1923 return a;
1924}
drhdd793422001-06-28 01:54:48 +00001925
drhfa86c412002-02-02 15:01:15 +00001926/*
1927** Set the checkpoint.
1928**
1929** This routine should be called with the transaction journal already
1930** open. A new checkpoint journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00001931** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00001932*/
1933int sqlitepager_ckpt_begin(Pager *pPager){
1934 int rc;
1935 char zTemp[SQLITE_TEMPNAME_SIZE];
drhda47d772002-12-02 04:25:19 +00001936 if( !pPager->journalOpen ){
1937 pPager->ckptAutoopen = 1;
1938 return SQLITE_OK;
1939 }
drhfa86c412002-02-02 15:01:15 +00001940 assert( pPager->journalOpen );
drh0f892532002-05-30 12:27:03 +00001941 assert( !pPager->ckptInUse );
drhfa86c412002-02-02 15:01:15 +00001942 pPager->aInCkpt = sqliteMalloc( pPager->dbSize/8 + 1 );
1943 if( pPager->aInCkpt==0 ){
1944 sqliteOsReadLock(&pPager->fd);
1945 return SQLITE_NOMEM;
1946 }
drh968af522003-02-11 14:55:40 +00001947#ifndef NDEBUG
drhfa86c412002-02-02 15:01:15 +00001948 rc = sqliteOsFileSize(&pPager->jfd, &pPager->ckptJSize);
1949 if( rc ) goto ckpt_begin_failed;
drh968af522003-02-11 14:55:40 +00001950 assert( pPager->ckptJSize ==
1951 pPager->nRec*JOURNAL_PG_SZ(journal_format)+JOURNAL_HDR_SZ(journal_format) );
1952#endif
1953 pPager->ckptJSize = pPager->nRec*JOURNAL_PG_SZ(journal_format)
1954 + JOURNAL_HDR_SZ(journal_format);
drh663fc632002-02-02 18:49:19 +00001955 pPager->ckptSize = pPager->dbSize;
drh0f892532002-05-30 12:27:03 +00001956 if( !pPager->ckptOpen ){
1957 rc = sqlitepager_opentemp(zTemp, &pPager->cpfd);
1958 if( rc ) goto ckpt_begin_failed;
1959 pPager->ckptOpen = 1;
drh9bd47a92003-01-07 14:46:08 +00001960 pPager->ckptNRec = 0;
drh0f892532002-05-30 12:27:03 +00001961 }
1962 pPager->ckptInUse = 1;
drhfa86c412002-02-02 15:01:15 +00001963 return SQLITE_OK;
1964
1965ckpt_begin_failed:
1966 if( pPager->aInCkpt ){
1967 sqliteFree(pPager->aInCkpt);
1968 pPager->aInCkpt = 0;
1969 }
1970 return rc;
1971}
1972
1973/*
1974** Commit a checkpoint.
1975*/
1976int sqlitepager_ckpt_commit(Pager *pPager){
drh0f892532002-05-30 12:27:03 +00001977 if( pPager->ckptInUse ){
drh03eb96a2002-11-10 23:32:56 +00001978 PgHdr *pPg, *pNext;
drh96ddd6d2002-09-05 19:10:33 +00001979 sqliteOsSeek(&pPager->cpfd, 0);
drh9bd47a92003-01-07 14:46:08 +00001980 /* sqliteOsTruncate(&pPager->cpfd, 0); */
1981 pPager->ckptNRec = 0;
drh0f892532002-05-30 12:27:03 +00001982 pPager->ckptInUse = 0;
drh663fc632002-02-02 18:49:19 +00001983 sqliteFree( pPager->aInCkpt );
1984 pPager->aInCkpt = 0;
drh03eb96a2002-11-10 23:32:56 +00001985 for(pPg=pPager->pCkpt; pPg; pPg=pNext){
1986 pNext = pPg->pNextCkpt;
1987 assert( pPg->inCkpt );
drh663fc632002-02-02 18:49:19 +00001988 pPg->inCkpt = 0;
drh03eb96a2002-11-10 23:32:56 +00001989 pPg->pPrevCkpt = pPg->pNextCkpt = 0;
drh663fc632002-02-02 18:49:19 +00001990 }
drh03eb96a2002-11-10 23:32:56 +00001991 pPager->pCkpt = 0;
drh663fc632002-02-02 18:49:19 +00001992 }
drhda47d772002-12-02 04:25:19 +00001993 pPager->ckptAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00001994 return SQLITE_OK;
1995}
1996
1997/*
1998** Rollback a checkpoint.
1999*/
2000int sqlitepager_ckpt_rollback(Pager *pPager){
2001 int rc;
drh0f892532002-05-30 12:27:03 +00002002 if( pPager->ckptInUse ){
drh663fc632002-02-02 18:49:19 +00002003 rc = pager_ckpt_playback(pPager);
2004 sqlitepager_ckpt_commit(pPager);
2005 }else{
2006 rc = SQLITE_OK;
2007 }
drhda47d772002-12-02 04:25:19 +00002008 pPager->ckptAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002009 return rc;
2010}
2011
drh74587e52002-08-13 00:01:16 +00002012#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00002013/*
2014** Print a listing of all referenced pages and their ref count.
2015*/
2016void sqlitepager_refdump(Pager *pPager){
2017 PgHdr *pPg;
2018 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2019 if( pPg->nRef<=0 ) continue;
2020 printf("PAGE %3d addr=0x%08x nRef=%d\n",
2021 pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
2022 }
2023}
2024#endif