blob: 37948df6c87550dad6e5339bf901be42a435b71a [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**
drh73509ee2003-04-06 20:44:45 +000021** @(#) $Id: pager.c,v 1.80 2003/04/06 20:44:45 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh829e8022002-11-06 14:08:11 +000023#include "os.h" /* Must be first to enable large file support */
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include "pager.h"
drhed7c8552001-04-11 14:29:21 +000026#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000027#include <string.h>
drhed7c8552001-04-11 14:29:21 +000028
29/*
drhdb48ee02003-01-16 13:42:43 +000030** Macros for troubleshooting. Normally turned off
31*/
32#if 0
33static Pager *mainPager = 0;
34#define SET_PAGER(X) if( mainPager==0 ) mainPager = (X)
35#define CLR_PAGER(X) if( mainPager==(X) ) mainPager = 0
36#define TRACE1(X) if( pPager==mainPager ) fprintf(stderr,X)
37#define TRACE2(X,Y) if( pPager==mainPager ) fprintf(stderr,X,Y)
38#define TRACE3(X,Y,Z) if( pPager==mainPager ) fprintf(stderr,X,Y,Z)
39#else
40#define SET_PAGER(X)
41#define CLR_PAGER(X)
42#define TRACE1(X)
43#define TRACE2(X,Y)
44#define TRACE3(X,Y,Z)
45#endif
46
47
48/*
drhed7c8552001-04-11 14:29:21 +000049** The page cache as a whole is always in one of the following
50** states:
51**
52** SQLITE_UNLOCK The page cache is not currently reading or
53** writing the database file. There is no
54** data held in memory. This is the initial
55** state.
56**
57** SQLITE_READLOCK The page cache is reading the database.
58** Writing is not permitted. There can be
59** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000060** file at the same time.
drhed7c8552001-04-11 14:29:21 +000061**
62** SQLITE_WRITELOCK The page cache is writing the database.
63** Access is exclusive. No other processes or
64** threads can be reading or writing while one
65** process is writing.
66**
drh306dc212001-05-21 13:45:10 +000067** The page cache comes up in SQLITE_UNLOCK. The first time a
68** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000069** After all pages have been released using sqlite_page_unref(),
drh306dc212001-05-21 13:45:10 +000070** the state transitions back to SQLITE_UNLOCK. The first time
drhed7c8552001-04-11 14:29:21 +000071** that sqlite_page_write() is called, the state transitions to
drh306dc212001-05-21 13:45:10 +000072** SQLITE_WRITELOCK. (Note that sqlite_page_write() can only be
73** called on an outstanding page which means that the pager must
74** be in SQLITE_READLOCK before it transitions to SQLITE_WRITELOCK.)
75** The sqlite_page_rollback() and sqlite_page_commit() functions
76** transition the state from SQLITE_WRITELOCK back to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000077*/
78#define SQLITE_UNLOCK 0
79#define SQLITE_READLOCK 1
80#define SQLITE_WRITELOCK 2
81
drhd9b02572001-04-15 00:37:09 +000082
drhed7c8552001-04-11 14:29:21 +000083/*
84** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +000085** This header is only visible to this pager module. The client
86** code that calls pager sees only the data that follows the header.
drhed7c8552001-04-11 14:29:21 +000087*/
drhd9b02572001-04-15 00:37:09 +000088typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +000089struct PgHdr {
90 Pager *pPager; /* The pager to which this page belongs */
91 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +000092 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhed7c8552001-04-11 14:29:21 +000093 int nRef; /* Number of users of this page */
drhd9b02572001-04-15 00:37:09 +000094 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
95 PgHdr *pNextAll, *pPrevAll; /* A list of all pages */
drh03eb96a2002-11-10 23:32:56 +000096 PgHdr *pNextCkpt, *pPrevCkpt; /* List of pages in the checkpoint journal */
drh193a6b42002-07-07 16:52:46 +000097 u8 inJournal; /* TRUE if has been written to journal */
98 u8 inCkpt; /* TRUE if written to the checkpoint journal */
99 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000100 u8 needSync; /* Sync journal before writing this page */
drh193a6b42002-07-07 16:52:46 +0000101 u8 alwaysRollback; /* Disable dont_rollback() for this page */
drh2554f8b2003-01-22 01:26:44 +0000102 PgHdr *pDirty; /* Dirty pages sorted by PgHdr.pgno */
drh69688d52001-04-14 16:38:23 +0000103 /* SQLITE_PAGE_SIZE bytes of page data follow this header */
drh973b6e32003-02-12 14:09:42 +0000104 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +0000105};
106
107/*
drh69688d52001-04-14 16:38:23 +0000108** Convert a pointer to a PgHdr into a pointer to its data
109** and back again.
drhed7c8552001-04-11 14:29:21 +0000110*/
111#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
112#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drh7e3b0a02001-04-28 16:52:40 +0000113#define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE])
drhed7c8552001-04-11 14:29:21 +0000114
115/*
drhed7c8552001-04-11 14:29:21 +0000116** How big to make the hash table used for locating in-memory pages
drh836faa42003-01-11 13:30:57 +0000117** by page number.
drhed7c8552001-04-11 14:29:21 +0000118*/
drh836faa42003-01-11 13:30:57 +0000119#define N_PG_HASH 2048
120
121/*
122** Hash a page number
123*/
124#define pager_hash(PN) ((PN)&(N_PG_HASH-1))
drhed7c8552001-04-11 14:29:21 +0000125
126/*
127** A open page cache is an instance of the following structure.
128*/
129struct Pager {
130 char *zFilename; /* Name of the database file */
131 char *zJournal; /* Name of the journal file */
drh8cfbf082001-09-19 13:22:39 +0000132 OsFile fd, jfd; /* File descriptors for database and journal */
drhfa86c412002-02-02 15:01:15 +0000133 OsFile cpfd; /* File descriptor for the checkpoint journal */
drhed7c8552001-04-11 14:29:21 +0000134 int dbSize; /* Number of pages in the file */
drh69688d52001-04-14 16:38:23 +0000135 int origDbSize; /* dbSize before the current change */
drh28be87c2002-11-05 23:03:02 +0000136 int ckptSize; /* Size of database (in pages) at ckpt_begin() */
137 off_t ckptJSize; /* Size of journal at ckpt_begin() */
drh968af522003-02-11 14:55:40 +0000138 int nRec; /* Number of pages written to the journal */
139 u32 cksumInit; /* Quasi-random value added to every checksum */
drh9bd47a92003-01-07 14:46:08 +0000140 int ckptNRec; /* Number of records in the checkpoint journal */
drh7e3b0a02001-04-28 16:52:40 +0000141 int nExtra; /* Add this many bytes to each in-memory page */
drh72f82862001-05-24 21:06:34 +0000142 void (*xDestructor)(void*); /* Call this routine when freeing pages */
drhed7c8552001-04-11 14:29:21 +0000143 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000144 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000145 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000146 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
drh603240c2002-03-05 01:11:12 +0000147 u8 journalOpen; /* True if journal file descriptors is valid */
drhdb48ee02003-01-16 13:42:43 +0000148 u8 journalStarted; /* True if initial magic of journal is synced */
drhda47d772002-12-02 04:25:19 +0000149 u8 useJournal; /* Do not use a rollback journal on this file */
drh603240c2002-03-05 01:11:12 +0000150 u8 ckptOpen; /* True if the checkpoint journal is open */
drh0f892532002-05-30 12:27:03 +0000151 u8 ckptInUse; /* True we are in a checkpoint */
drhda47d772002-12-02 04:25:19 +0000152 u8 ckptAutoopen; /* Open ckpt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000153 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000154 u8 fullSync; /* Do extra syncs of the journal for robustness */
drh603240c2002-03-05 01:11:12 +0000155 u8 state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */
156 u8 errMask; /* One of several kinds of errors */
157 u8 tempFile; /* zFilename is a temporary file */
158 u8 readOnly; /* True for a read-only database */
159 u8 needSync; /* True if an fsync() is needed on the journal */
drha1680452002-04-18 01:56:57 +0000160 u8 dirtyFile; /* True if database file has changed in any way */
drh193a6b42002-07-07 16:52:46 +0000161 u8 alwaysRollback; /* Disable dont_rollback() for all pages */
drh603240c2002-03-05 01:11:12 +0000162 u8 *aInJournal; /* One bit for each page in the database file */
163 u8 *aInCkpt; /* One bit for each page in the database */
drhed7c8552001-04-11 14:29:21 +0000164 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000165 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000166 PgHdr *pAll; /* List of all pages */
drh03eb96a2002-11-10 23:32:56 +0000167 PgHdr *pCkpt; /* List of pages in the checkpoint journal */
drhed7c8552001-04-11 14:29:21 +0000168 PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */
drhd9b02572001-04-15 00:37:09 +0000169};
170
171/*
172** These are bits that can be set in Pager.errMask.
173*/
174#define PAGER_ERR_FULL 0x01 /* a write() failed */
175#define PAGER_ERR_MEM 0x02 /* malloc() failed */
176#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
177#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
drh81a20f22001-10-12 17:30:04 +0000178#define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */
drhd9b02572001-04-15 00:37:09 +0000179
180/*
181** The journal file contains page records in the following
182** format.
drh968af522003-02-11 14:55:40 +0000183**
184** Actually, this structure is the complete page record for pager
185** formats less than 3. Beginning with format 3, this record is surrounded
186** by two checksums.
drhd9b02572001-04-15 00:37:09 +0000187*/
188typedef struct PageRecord PageRecord;
189struct PageRecord {
190 Pgno pgno; /* The page number */
191 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
192};
193
194/*
drh5e00f6c2001-09-13 13:46:56 +0000195** Journal files begin with the following magic string. The data
196** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000197**
drh968af522003-02-11 14:55:40 +0000198** There are three journal formats (so far). The 1st journal format writes
199** 32-bit integers in the byte-order of the host machine. New
200** formats writes integers as big-endian. All new journals use the
drh94f33312002-08-12 12:29:56 +0000201** new format, but we have to be able to read an older journal in order
drh968af522003-02-11 14:55:40 +0000202** to rollback journals created by older versions of the library.
203**
204** The 3rd journal format (added for 2.8.0) adds additional sanity
205** checking information to the journal. If the power fails while the
206** journal is being written, semi-random garbage data might appear in
207** the journal file after power is restored. If an attempt is then made
208** to roll the journal back, the database could be corrupted. The additional
209** sanity checking data is an attempt to discover the garbage in the
210** journal and ignore it.
211**
212** The sanity checking information for the 3rd journal format consists
213** of a 32-bit checksum on each page of data. The checksum covers both
214** the page number and the SQLITE_PAGE_SIZE bytes of data for the page.
215** This cksum is initialized to a 32-bit random value that appears in the
216** journal file right after the header. The random initializer is important,
217** because garbage data that appears at the end of a journal is likely
218** data that was once in other files that have now been deleted. If the
219** garbage data came from an obsolete journal file, the checksums might
220** be correct. But by initializing the checksum to random value which
221** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000222*/
drh968af522003-02-11 14:55:40 +0000223static const unsigned char aJournalMagic1[] = {
drhd9b02572001-04-15 00:37:09 +0000224 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000225};
drh968af522003-02-11 14:55:40 +0000226static const unsigned char aJournalMagic2[] = {
drh94f33312002-08-12 12:29:56 +0000227 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5,
228};
drh968af522003-02-11 14:55:40 +0000229static const unsigned char aJournalMagic3[] = {
230 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd6,
231};
232#define JOURNAL_FORMAT_1 1
233#define JOURNAL_FORMAT_2 2
234#define JOURNAL_FORMAT_3 3
drh94f33312002-08-12 12:29:56 +0000235
236/*
drh968af522003-02-11 14:55:40 +0000237** The following integer determines what format to use when creating
238** new primary journal files. By default we always use format 3.
239** When testing, we can set this value to older journal formats in order to
240** make sure that newer versions of the library are able to rollback older
241** journal files.
242**
243** Note that checkpoint journals always use format 2 and omit the header.
drh94f33312002-08-12 12:29:56 +0000244*/
245#ifdef SQLITE_TEST
drh968af522003-02-11 14:55:40 +0000246int journal_format = 3;
drh74587e52002-08-13 00:01:16 +0000247#else
drh968af522003-02-11 14:55:40 +0000248# define journal_format 3
drh94f33312002-08-12 12:29:56 +0000249#endif
drhed7c8552001-04-11 14:29:21 +0000250
251/*
drh968af522003-02-11 14:55:40 +0000252** The size of the header and of each page in the journal varies according
253** to which journal format is being used. The following macros figure out
254** the sizes based on format numbers.
255*/
256#define JOURNAL_HDR_SZ(X) \
257 (sizeof(aJournalMagic1) + sizeof(Pgno) + ((X)>=3)*2*sizeof(u32))
258#define JOURNAL_PG_SZ(X) \
259 (SQLITE_PAGE_SIZE + sizeof(Pgno) + ((X)>=3)*sizeof(u32))
260
261/*
drhdd793422001-06-28 01:54:48 +0000262** Enable reference count tracking here:
263*/
drh74587e52002-08-13 00:01:16 +0000264#ifdef SQLITE_TEST
drh5e00f6c2001-09-13 13:46:56 +0000265 int pager_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000266 static void pager_refinfo(PgHdr *p){
267 static int cnt = 0;
268 if( !pager_refinfo_enable ) return;
269 printf(
270 "REFCNT: %4d addr=0x%08x nRef=%d\n",
271 p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
272 );
273 cnt++; /* Something to set a breakpoint on */
274 }
275# define REFINFO(X) pager_refinfo(X)
276#else
277# define REFINFO(X)
278#endif
279
280/*
drh94f33312002-08-12 12:29:56 +0000281** Read a 32-bit integer from the given file descriptor
282*/
drh968af522003-02-11 14:55:40 +0000283static int read32bits(int format, OsFile *fd, u32 *pRes){
drh94f33312002-08-12 12:29:56 +0000284 u32 res;
285 int rc;
286 rc = sqliteOsRead(fd, &res, sizeof(res));
drh968af522003-02-11 14:55:40 +0000287 if( rc==SQLITE_OK && format>JOURNAL_FORMAT_1 ){
drh94f33312002-08-12 12:29:56 +0000288 unsigned char ac[4];
289 memcpy(ac, &res, 4);
290 res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
291 }
292 *pRes = res;
293 return rc;
294}
295
296/*
297** Write a 32-bit integer into the given file descriptor. Writing
298** is always done using the new journal format.
299*/
300static int write32bits(OsFile *fd, u32 val){
301 unsigned char ac[4];
drh968af522003-02-11 14:55:40 +0000302 if( journal_format<=1 ){
drh94f33312002-08-12 12:29:56 +0000303 return sqliteOsWrite(fd, &val, 4);
304 }
drh94f33312002-08-12 12:29:56 +0000305 ac[0] = (val>>24) & 0xff;
306 ac[1] = (val>>16) & 0xff;
307 ac[2] = (val>>8) & 0xff;
308 ac[3] = val & 0xff;
309 return sqliteOsWrite(fd, ac, 4);
310}
311
drh2554f8b2003-01-22 01:26:44 +0000312/*
313** Write a 32-bit integer into a page header right before the
314** page data. This will overwrite the PgHdr.pDirty pointer.
315*/
drh968af522003-02-11 14:55:40 +0000316static void store32bits(u32 val, PgHdr *p, int offset){
drh2554f8b2003-01-22 01:26:44 +0000317 unsigned char *ac;
drh968af522003-02-11 14:55:40 +0000318 ac = &((char*)PGHDR_TO_DATA(p))[offset];
319 if( journal_format<=1 ){
drh2554f8b2003-01-22 01:26:44 +0000320 memcpy(ac, &val, 4);
321 }else{
322 ac[0] = (val>>24) & 0xff;
323 ac[1] = (val>>16) & 0xff;
324 ac[2] = (val>>8) & 0xff;
325 ac[3] = val & 0xff;
326 }
327}
328
drh94f33312002-08-12 12:29:56 +0000329
330/*
drhd9b02572001-04-15 00:37:09 +0000331** Convert the bits in the pPager->errMask into an approprate
332** return code.
333*/
334static int pager_errcode(Pager *pPager){
335 int rc = SQLITE_OK;
336 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
drh81a20f22001-10-12 17:30:04 +0000337 if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR;
drhd9b02572001-04-15 00:37:09 +0000338 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
339 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
340 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
341 return rc;
drhed7c8552001-04-11 14:29:21 +0000342}
343
344/*
drh03eb96a2002-11-10 23:32:56 +0000345** Add or remove a page from the list of all pages that are in the
346** checkpoint journal.
347**
348** The Pager keeps a separate list of pages that are currently in
349** the checkpoint journal. This helps the sqlitepager_ckpt_commit()
350** routine run MUCH faster for the common case where there are many
351** pages in memory but only a few are in the checkpoint journal.
352*/
353static void page_add_to_ckpt_list(PgHdr *pPg){
354 Pager *pPager = pPg->pPager;
355 if( pPg->inCkpt ) return;
356 assert( pPg->pPrevCkpt==0 && pPg->pNextCkpt==0 );
357 pPg->pPrevCkpt = 0;
358 if( pPager->pCkpt ){
359 pPager->pCkpt->pPrevCkpt = pPg;
360 }
361 pPg->pNextCkpt = pPager->pCkpt;
362 pPager->pCkpt = pPg;
363 pPg->inCkpt = 1;
364}
365static void page_remove_from_ckpt_list(PgHdr *pPg){
366 if( !pPg->inCkpt ) return;
367 if( pPg->pPrevCkpt ){
368 assert( pPg->pPrevCkpt->pNextCkpt==pPg );
369 pPg->pPrevCkpt->pNextCkpt = pPg->pNextCkpt;
370 }else{
371 assert( pPg->pPager->pCkpt==pPg );
372 pPg->pPager->pCkpt = pPg->pNextCkpt;
373 }
374 if( pPg->pNextCkpt ){
375 assert( pPg->pNextCkpt->pPrevCkpt==pPg );
376 pPg->pNextCkpt->pPrevCkpt = pPg->pPrevCkpt;
377 }
378 pPg->pNextCkpt = 0;
379 pPg->pPrevCkpt = 0;
380 pPg->inCkpt = 0;
381}
382
383/*
drhed7c8552001-04-11 14:29:21 +0000384** Find a page in the hash table given its page number. Return
385** a pointer to the page or NULL if not found.
386*/
drhd9b02572001-04-15 00:37:09 +0000387static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh836faa42003-01-11 13:30:57 +0000388 PgHdr *p = pPager->aHash[pager_hash(pgno)];
drhed7c8552001-04-11 14:29:21 +0000389 while( p && p->pgno!=pgno ){
390 p = p->pNextHash;
391 }
392 return p;
393}
394
395/*
396** Unlock the database and clear the in-memory cache. This routine
397** sets the state of the pager back to what it was when it was first
398** opened. Any outstanding pages are invalidated and subsequent attempts
399** to access those pages will likely result in a coredump.
400*/
drhd9b02572001-04-15 00:37:09 +0000401static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000402 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000403 for(pPg=pPager->pAll; pPg; pPg=pNext){
404 pNext = pPg->pNextAll;
405 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000406 }
407 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000408 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +0000409 pPager->pLast = 0;
410 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000411 memset(pPager->aHash, 0, sizeof(pPager->aHash));
412 pPager->nPage = 0;
drhfa86c412002-02-02 15:01:15 +0000413 if( pPager->state>=SQLITE_WRITELOCK ){
drhd9b02572001-04-15 00:37:09 +0000414 sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000415 }
drha7fcb052001-12-14 15:09:55 +0000416 sqliteOsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000417 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000418 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000419 pPager->nRef = 0;
drh8cfbf082001-09-19 13:22:39 +0000420 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000421}
422
423/*
424** When this routine is called, the pager has the journal file open and
425** a write lock on the database. This routine releases the database
426** write lock and acquires a read lock in its place. The journal file
427** is deleted and closed.
drhed7c8552001-04-11 14:29:21 +0000428*/
drhd9b02572001-04-15 00:37:09 +0000429static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000430 int rc;
drhd9b02572001-04-15 00:37:09 +0000431 PgHdr *pPg;
drhfa86c412002-02-02 15:01:15 +0000432 if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK;
drh663fc632002-02-02 18:49:19 +0000433 sqlitepager_ckpt_commit(pPager);
drh0f892532002-05-30 12:27:03 +0000434 if( pPager->ckptOpen ){
435 sqliteOsClose(&pPager->cpfd);
436 pPager->ckptOpen = 0;
437 }
drhda47d772002-12-02 04:25:19 +0000438 if( pPager->journalOpen ){
439 sqliteOsClose(&pPager->jfd);
440 pPager->journalOpen = 0;
441 sqliteOsDelete(pPager->zJournal);
442 sqliteFree( pPager->aInJournal );
443 pPager->aInJournal = 0;
444 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
445 pPg->inJournal = 0;
446 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +0000447 pPg->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000448 }
449 }else{
450 assert( pPager->dirtyFile==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +0000451 }
drhda47d772002-12-02 04:25:19 +0000452 rc = sqliteOsReadLock(&pPager->fd);
drh8e298f92002-07-06 16:28:47 +0000453 if( rc==SQLITE_OK ){
454 pPager->state = SQLITE_READLOCK;
455 }else{
456 /* This can only happen if a process does a BEGIN, then forks and the
457 ** child process does the COMMIT. Because of the semantics of unix
458 ** file locking, the unlock will fail.
459 */
460 pPager->state = SQLITE_UNLOCK;
461 }
drhed7c8552001-04-11 14:29:21 +0000462 return rc;
463}
464
drhed7c8552001-04-11 14:29:21 +0000465/*
drh968af522003-02-11 14:55:40 +0000466** Compute and return a checksum for the page of data.
467*/
468static u32 pager_cksum(Pager *pPager, Pgno pgno, const char *aData){
469 u32 cksum = pPager->cksumInit + pgno;
drh968af522003-02-11 14:55:40 +0000470 return cksum;
471}
472
473/*
drhfa86c412002-02-02 15:01:15 +0000474** Read a single page from the journal file opened on file descriptor
475** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +0000476**
477** There are three different journal formats. The format parameter determines
478** which format is used by the journal that is played back.
drhfa86c412002-02-02 15:01:15 +0000479*/
drh968af522003-02-11 14:55:40 +0000480static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int format){
drhfa86c412002-02-02 15:01:15 +0000481 int rc;
482 PgHdr *pPg; /* An existing page in the cache */
483 PageRecord pgRec;
drh968af522003-02-11 14:55:40 +0000484 u32 cksum;
drhfa86c412002-02-02 15:01:15 +0000485
drh968af522003-02-11 14:55:40 +0000486 rc = read32bits(format, jfd, &pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000487 if( rc!=SQLITE_OK ) return rc;
drh94f33312002-08-12 12:29:56 +0000488 rc = sqliteOsRead(jfd, &pgRec.aData, sizeof(pgRec.aData));
drh99ee3602003-02-16 19:13:36 +0000489 if( rc!=SQLITE_OK ) return rc;
drhfa86c412002-02-02 15:01:15 +0000490
drh968af522003-02-11 14:55:40 +0000491 /* Sanity checking on the page. This is more important that I originally
492 ** thought. If a power failure occurs while the journal is being written,
493 ** it could cause invalid data to be written into the journal. We need to
494 ** detect this invalid data (with high probability) and ignore it.
495 */
496 if( pgRec.pgno==0 ){
497 return SQLITE_DONE;
498 }
499 if( pgRec.pgno>pPager->dbSize ){
500 return SQLITE_OK;
501 }
502 if( format>=JOURNAL_FORMAT_3 ){
503 rc = read32bits(format, jfd, &cksum);
drh99ee3602003-02-16 19:13:36 +0000504 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +0000505 if( pager_cksum(pPager, pgRec.pgno, pgRec.aData)!=cksum ){
506 return SQLITE_DONE;
507 }
508 }
drhfa86c412002-02-02 15:01:15 +0000509
510 /* Playback the page. Update the in-memory copy of the page
511 ** at the same time, if there is one.
512 */
513 pPg = pager_lookup(pPager, pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000514 TRACE2("PLAYBACK %d\n", pgRec.pgno);
515 sqliteOsSeek(&pPager->fd, (pgRec.pgno-1)*(off_t)SQLITE_PAGE_SIZE);
516 rc = sqliteOsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
drhfa86c412002-02-02 15:01:15 +0000517 if( pPg ){
drh3a840692003-01-29 22:58:26 +0000518 if( pPg->nRef==0 ||
519 memcmp(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE)==0
520 ){
521 /* Do not update the data on this page if the page is in use
522 ** and the page has never been modified. This avoids resetting
523 ** the "extra" data. That in turn avoids invalidating BTree cursors
524 ** in trees that have never been modified. The end result is that
525 ** you can have a SELECT going on in one table and ROLLBACK changes
526 ** to a different table and the SELECT is unaffected by the ROLLBACK.
527 */
528 memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
529 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
530 }
drhdb48ee02003-01-16 13:42:43 +0000531 pPg->dirty = 0;
532 pPg->needSync = 0;
drhfa86c412002-02-02 15:01:15 +0000533 }
534 return rc;
535}
536
537/*
drhed7c8552001-04-11 14:29:21 +0000538** Playback the journal and thus restore the database file to
539** the state it was in before we started making changes.
540**
drhd9b02572001-04-15 00:37:09 +0000541** The journal file format is as follows: There is an initial
542** file-type string for sanity checking. Then there is a single
543** Pgno number which is the number of pages in the database before
544** changes were made. The database is truncated to this size.
drh306dc212001-05-21 13:45:10 +0000545** Next come zero or more page records where each page record
546** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See
547** the PageRecord structure for details.
drhed7c8552001-04-11 14:29:21 +0000548**
drhd9b02572001-04-15 00:37:09 +0000549** If the file opened as the journal file is not a well-formed
550** journal file (as determined by looking at the magic number
551** at the beginning) then this routine returns SQLITE_PROTOCOL.
552** If any other errors occur during playback, the database will
553** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in
554** pPager->errMask and SQLITE_CORRUPT is returned. If it all
555** works, then this routine returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000556*/
drh99ee3602003-02-16 19:13:36 +0000557static int pager_playback(Pager *pPager, int useJournalSize){
drh968af522003-02-11 14:55:40 +0000558 off_t szJ; /* Size of the journal file in bytes */
559 int nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +0000560 int i; /* Loop counter */
561 Pgno mxPg = 0; /* Size of the original file in pages */
drh968af522003-02-11 14:55:40 +0000562 int format; /* Format of the journal file. */
563 unsigned char aMagic[sizeof(aJournalMagic1)];
drhed7c8552001-04-11 14:29:21 +0000564 int rc;
565
drhc3a64ba2001-11-22 00:01:27 +0000566 /* Figure out how many records are in the journal. Abort early if
567 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +0000568 */
drh8cfbf082001-09-19 13:22:39 +0000569 assert( pPager->journalOpen );
drha7fcb052001-12-14 15:09:55 +0000570 sqliteOsSeek(&pPager->jfd, 0);
drh968af522003-02-11 14:55:40 +0000571 rc = sqliteOsFileSize(&pPager->jfd, &szJ);
drhc3a64ba2001-11-22 00:01:27 +0000572 if( rc!=SQLITE_OK ){
573 goto end_playback;
574 }
drh968af522003-02-11 14:55:40 +0000575 if( szJ < sizeof(aMagic)+sizeof(Pgno) ){
drhc3a64ba2001-11-22 00:01:27 +0000576 goto end_playback;
577 }
578
579 /* Read the beginning of the journal and truncate the
580 ** database file back to its original size.
581 */
drha7fcb052001-12-14 15:09:55 +0000582 rc = sqliteOsRead(&pPager->jfd, aMagic, sizeof(aMagic));
drh94f33312002-08-12 12:29:56 +0000583 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000584 rc = SQLITE_PROTOCOL;
585 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000586 }
drh968af522003-02-11 14:55:40 +0000587 if( memcmp(aMagic, aJournalMagic3, sizeof(aMagic))==0 ){
588 format = JOURNAL_FORMAT_3;
589 }else if( memcmp(aMagic, aJournalMagic2, sizeof(aMagic))==0 ){
590 format = JOURNAL_FORMAT_2;
591 }else if( memcmp(aMagic, aJournalMagic1, sizeof(aMagic))==0 ){
592 format = JOURNAL_FORMAT_1;
drh94f33312002-08-12 12:29:56 +0000593 }else{
594 rc = SQLITE_PROTOCOL;
595 goto end_playback;
596 }
drh968af522003-02-11 14:55:40 +0000597 if( format>=JOURNAL_FORMAT_3 ){
598 rc = read32bits(format, &pPager->jfd, &nRec);
599 if( rc ) goto end_playback;
600 rc = read32bits(format, &pPager->jfd, &pPager->cksumInit);
601 if( rc ) goto end_playback;
drh99ee3602003-02-16 19:13:36 +0000602 if( nRec==0xffffffff || useJournalSize ){
drh968af522003-02-11 14:55:40 +0000603 nRec = (szJ - JOURNAL_HDR_SZ(3))/JOURNAL_PG_SZ(3);
604 }
605 }else{
drhd8d66e82003-02-12 02:10:15 +0000606 nRec = (szJ - JOURNAL_HDR_SZ(2))/JOURNAL_PG_SZ(2);
607 assert( nRec*JOURNAL_PG_SZ(2)+JOURNAL_HDR_SZ(2)==szJ );
drh968af522003-02-11 14:55:40 +0000608 }
609 rc = read32bits(format, &pPager->jfd, &mxPg);
drhd9b02572001-04-15 00:37:09 +0000610 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000611 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000612 }
drhd8d66e82003-02-12 02:10:15 +0000613 assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg );
drh28be87c2002-11-05 23:03:02 +0000614 rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg);
drh81a20f22001-10-12 17:30:04 +0000615 if( rc!=SQLITE_OK ){
616 goto end_playback;
617 }
drhd9b02572001-04-15 00:37:09 +0000618 pPager->dbSize = mxPg;
619
drhfa86c412002-02-02 15:01:15 +0000620 /* Copy original pages out of the journal and back into the database file.
drhed7c8552001-04-11 14:29:21 +0000621 */
drh968af522003-02-11 14:55:40 +0000622 for(i=0; i<nRec; i++){
623 rc = pager_playback_one_page(pPager, &pPager->jfd, format);
624 if( rc!=SQLITE_OK ){
625 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +0000626 rc = SQLITE_OK;
627 }
628 break;
629 }
drhed7c8552001-04-11 14:29:21 +0000630 }
drh81a20f22001-10-12 17:30:04 +0000631
drh4a0681e2003-02-13 01:58:20 +0000632 /* Pages that have been written to the journal but never synced
633 ** where not restored by the loop above. We have to restore those
634 ** pages by reading the back from the original database.
drhdb48ee02003-01-16 13:42:43 +0000635 */
636 if( rc==SQLITE_OK ){
637 PgHdr *pPg;
638 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3a840692003-01-29 22:58:26 +0000639 char zBuf[SQLITE_PAGE_SIZE];
drh4a0681e2003-02-13 01:58:20 +0000640 if( !pPg->dirty ) continue;
drhdb48ee02003-01-16 13:42:43 +0000641 if( (int)pPg->pgno <= pPager->origDbSize ){
642 sqliteOsSeek(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)(pPg->pgno-1));
drh3a840692003-01-29 22:58:26 +0000643 rc = sqliteOsRead(&pPager->fd, zBuf, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000644 if( rc ) break;
645 }else{
drh3a840692003-01-29 22:58:26 +0000646 memset(zBuf, 0, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000647 }
drh3a840692003-01-29 22:58:26 +0000648 if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE) ){
649 memcpy(PGHDR_TO_DATA(pPg), zBuf, SQLITE_PAGE_SIZE);
650 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
651 }
drhdb48ee02003-01-16 13:42:43 +0000652 pPg->needSync = 0;
653 pPg->dirty = 0;
654 }
655 }
drh4a0681e2003-02-13 01:58:20 +0000656
657end_playback:
drhd9b02572001-04-15 00:37:09 +0000658 if( rc!=SQLITE_OK ){
659 pager_unwritelock(pPager);
660 pPager->errMask |= PAGER_ERR_CORRUPT;
661 rc = SQLITE_CORRUPT;
662 }else{
663 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000664 }
drhd9b02572001-04-15 00:37:09 +0000665 return rc;
drhed7c8552001-04-11 14:29:21 +0000666}
667
668/*
drhfa86c412002-02-02 15:01:15 +0000669** Playback the checkpoint journal.
670**
671** This is similar to playing back the transaction journal but with
672** a few extra twists.
673**
drh663fc632002-02-02 18:49:19 +0000674** (1) The number of pages in the database file at the start of
675** the checkpoint is stored in pPager->ckptSize, not in the
676** journal file itself.
drhfa86c412002-02-02 15:01:15 +0000677**
678** (2) In addition to playing back the checkpoint journal, also
679** playback all pages of the transaction journal beginning
680** at offset pPager->ckptJSize.
681*/
682static int pager_ckpt_playback(Pager *pPager){
drh968af522003-02-11 14:55:40 +0000683 off_t szJ; /* Size of the full journal */
684 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +0000685 int i; /* Loop counter */
686 int rc;
687
688 /* Truncate the database back to its original size.
689 */
drh28be87c2002-11-05 23:03:02 +0000690 rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)pPager->ckptSize);
drhfa86c412002-02-02 15:01:15 +0000691 pPager->dbSize = pPager->ckptSize;
692
693 /* Figure out how many records are in the checkpoint journal.
694 */
drh0f892532002-05-30 12:27:03 +0000695 assert( pPager->ckptInUse && pPager->journalOpen );
drhfa86c412002-02-02 15:01:15 +0000696 sqliteOsSeek(&pPager->cpfd, 0);
drh9bd47a92003-01-07 14:46:08 +0000697 nRec = pPager->ckptNRec;
drhfa86c412002-02-02 15:01:15 +0000698
699 /* Copy original pages out of the checkpoint journal and back into the
drh968af522003-02-11 14:55:40 +0000700 ** database file. Note that the checkpoint journal always uses format
701 ** 2 instead of format 3 since it does not need to be concerned with
702 ** power failures corrupting the journal and can thus omit the checksums.
drhfa86c412002-02-02 15:01:15 +0000703 */
704 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000705 rc = pager_playback_one_page(pPager, &pPager->cpfd, 2);
706 assert( rc!=SQLITE_DONE );
drhfa86c412002-02-02 15:01:15 +0000707 if( rc!=SQLITE_OK ) goto end_ckpt_playback;
708 }
709
710 /* Figure out how many pages need to be copied out of the transaction
711 ** journal.
712 */
713 rc = sqliteOsSeek(&pPager->jfd, pPager->ckptJSize);
714 if( rc!=SQLITE_OK ){
715 goto end_ckpt_playback;
716 }
drh968af522003-02-11 14:55:40 +0000717 rc = sqliteOsFileSize(&pPager->jfd, &szJ);
drhfa86c412002-02-02 15:01:15 +0000718 if( rc!=SQLITE_OK ){
719 goto end_ckpt_playback;
720 }
drh968af522003-02-11 14:55:40 +0000721 nRec = (szJ - pPager->ckptJSize)/JOURNAL_PG_SZ(journal_format);
drhfa86c412002-02-02 15:01:15 +0000722 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000723 rc = pager_playback_one_page(pPager, &pPager->jfd, journal_format);
724 if( rc!=SQLITE_OK ){
725 assert( rc!=SQLITE_DONE );
726 goto end_ckpt_playback;
727 }
drhfa86c412002-02-02 15:01:15 +0000728 }
729
drhfa86c412002-02-02 15:01:15 +0000730end_ckpt_playback:
drhfa86c412002-02-02 15:01:15 +0000731 if( rc!=SQLITE_OK ){
drhfa86c412002-02-02 15:01:15 +0000732 pPager->errMask |= PAGER_ERR_CORRUPT;
733 rc = SQLITE_CORRUPT;
drhfa86c412002-02-02 15:01:15 +0000734 }
735 return rc;
736}
737
738/*
drhf57b14a2001-09-14 18:54:08 +0000739** Change the maximum number of in-memory pages that are allowed.
drhcd61c282002-03-06 22:01:34 +0000740**
741** The maximum number is the absolute value of the mxPage parameter.
742** If mxPage is negative, the noSync flag is also set. noSync bypasses
743** calls to sqliteOsSync(). The pager runs much faster with noSync on,
744** but if the operating system crashes or there is an abrupt power
745** failure, the database file might be left in an inconsistent and
746** unrepairable state.
drhf57b14a2001-09-14 18:54:08 +0000747*/
748void sqlitepager_set_cachesize(Pager *pPager, int mxPage){
drh603240c2002-03-05 01:11:12 +0000749 if( mxPage>=0 ){
drha1680452002-04-18 01:56:57 +0000750 pPager->noSync = pPager->tempFile;
drh603240c2002-03-05 01:11:12 +0000751 }else{
752 pPager->noSync = 1;
753 mxPage = -mxPage;
754 }
drhf57b14a2001-09-14 18:54:08 +0000755 if( mxPage>10 ){
756 pPager->mxPage = mxPage;
757 }
758}
759
760/*
drh973b6e32003-02-12 14:09:42 +0000761** Adjust the robustness of the database to damage due to OS crashes
762** or power failures by changing the number of syncs()s when writing
763** the rollback journal. There are three levels:
764**
765** OFF sqliteOsSync() is never called. This is the default
766** for temporary and transient files.
767**
768** NORMAL The journal is synced once before writes begin on the
769** database. This is normally adequate protection, but
770** it is theoretically possible, though very unlikely,
771** that an inopertune power failure could leave the journal
772** in a state which would cause damage to the database
773** when it is rolled back.
774**
775** FULL The journal is synced twice before writes begin on the
776** database (with some additional information being written
777** in between the two syncs. If we assume that writing a
778** single disk sector is atomic, then this mode provides
779** assurance that the journal will not be corrupted to the
780** point of causing damage to the database during rollback.
781**
782** Numeric values associated with these states are OFF==1, NORMAL=2,
783** and FULL=3.
784*/
785void sqlitepager_set_safety_level(Pager *pPager, int level){
786 pPager->noSync = level==1 || pPager->tempFile;
787 pPager->fullSync = level==3 && !pPager->tempFile;
788}
789
790/*
drhfa86c412002-02-02 15:01:15 +0000791** Open a temporary file. Write the name of the file into zName
792** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write
793** the file descriptor into *fd. Return SQLITE_OK on success or some
794** other error code if we fail.
795**
796** The OS will automatically delete the temporary file when it is
797** closed.
798*/
799static int sqlitepager_opentemp(char *zFile, OsFile *fd){
800 int cnt = 8;
801 int rc;
802 do{
803 cnt--;
804 sqliteOsTempFileName(zFile);
805 rc = sqliteOsOpenExclusive(zFile, fd, 1);
806 }while( cnt>0 && rc!=SQLITE_OK );
807 return rc;
808}
809
810/*
drhed7c8552001-04-11 14:29:21 +0000811** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +0000812** The file to be cached need not exist. The file is not locked until
drhd9b02572001-04-15 00:37:09 +0000813** the first call to sqlitepager_get() and is only held open until the
814** last page is released using sqlitepager_unref().
drh382c0242001-10-06 16:33:02 +0000815**
drh6446c4d2001-12-15 14:22:18 +0000816** If zFilename is NULL then a randomly-named temporary file is created
817** and used as the file to be cached. The file will be deleted
818** automatically when it is closed.
drhed7c8552001-04-11 14:29:21 +0000819*/
drh7e3b0a02001-04-28 16:52:40 +0000820int sqlitepager_open(
821 Pager **ppPager, /* Return the Pager structure here */
822 const char *zFilename, /* Name of the database file to open */
823 int mxPage, /* Max number of in-memory cache pages */
drhda47d772002-12-02 04:25:19 +0000824 int nExtra, /* Extra bytes append to each in-memory page */
825 int useJournal /* TRUE to use a rollback journal on this file */
drh7e3b0a02001-04-28 16:52:40 +0000826){
drhed7c8552001-04-11 14:29:21 +0000827 Pager *pPager;
drh3e7a6092002-12-07 21:45:14 +0000828 char *zFullPathname;
drhed7c8552001-04-11 14:29:21 +0000829 int nameLen;
drh8cfbf082001-09-19 13:22:39 +0000830 OsFile fd;
831 int rc;
drh5e00f6c2001-09-13 13:46:56 +0000832 int tempFile;
833 int readOnly = 0;
drh8cfbf082001-09-19 13:22:39 +0000834 char zTemp[SQLITE_TEMPNAME_SIZE];
drhed7c8552001-04-11 14:29:21 +0000835
drhd9b02572001-04-15 00:37:09 +0000836 *ppPager = 0;
837 if( sqlite_malloc_failed ){
838 return SQLITE_NOMEM;
839 }
drh5e00f6c2001-09-13 13:46:56 +0000840 if( zFilename ){
drh3e7a6092002-12-07 21:45:14 +0000841 zFullPathname = sqliteOsFullPathname(zFilename);
842 rc = sqliteOsOpenReadWrite(zFullPathname, &fd, &readOnly);
drh5e00f6c2001-09-13 13:46:56 +0000843 tempFile = 0;
844 }else{
drhfa86c412002-02-02 15:01:15 +0000845 rc = sqlitepager_opentemp(zTemp, &fd);
drh5e00f6c2001-09-13 13:46:56 +0000846 zFilename = zTemp;
drh3e7a6092002-12-07 21:45:14 +0000847 zFullPathname = sqliteOsFullPathname(zFilename);
drh5e00f6c2001-09-13 13:46:56 +0000848 tempFile = 1;
849 }
drh3e7a6092002-12-07 21:45:14 +0000850 if( sqlite_malloc_failed ){
851 return SQLITE_NOMEM;
852 }
drh8cfbf082001-09-19 13:22:39 +0000853 if( rc!=SQLITE_OK ){
drh3e7a6092002-12-07 21:45:14 +0000854 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000855 return SQLITE_CANTOPEN;
856 }
drh3e7a6092002-12-07 21:45:14 +0000857 nameLen = strlen(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000858 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 );
drhd9b02572001-04-15 00:37:09 +0000859 if( pPager==0 ){
drha7fcb052001-12-14 15:09:55 +0000860 sqliteOsClose(&fd);
drh3e7a6092002-12-07 21:45:14 +0000861 sqliteFree(zFullPathname);
drhd9b02572001-04-15 00:37:09 +0000862 return SQLITE_NOMEM;
863 }
drhdb48ee02003-01-16 13:42:43 +0000864 SET_PAGER(pPager);
drhed7c8552001-04-11 14:29:21 +0000865 pPager->zFilename = (char*)&pPager[1];
866 pPager->zJournal = &pPager->zFilename[nameLen+1];
drh3e7a6092002-12-07 21:45:14 +0000867 strcpy(pPager->zFilename, zFullPathname);
868 strcpy(pPager->zJournal, zFullPathname);
869 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000870 strcpy(&pPager->zJournal[nameLen], "-journal");
871 pPager->fd = fd;
drh8cfbf082001-09-19 13:22:39 +0000872 pPager->journalOpen = 0;
drhda47d772002-12-02 04:25:19 +0000873 pPager->useJournal = useJournal;
drhfa86c412002-02-02 15:01:15 +0000874 pPager->ckptOpen = 0;
drh0f892532002-05-30 12:27:03 +0000875 pPager->ckptInUse = 0;
drhed7c8552001-04-11 14:29:21 +0000876 pPager->nRef = 0;
877 pPager->dbSize = -1;
drhfa86c412002-02-02 15:01:15 +0000878 pPager->ckptSize = 0;
879 pPager->ckptJSize = 0;
drhed7c8552001-04-11 14:29:21 +0000880 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +0000881 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +0000882 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000883 pPager->errMask = 0;
drh5e00f6c2001-09-13 13:46:56 +0000884 pPager->tempFile = tempFile;
885 pPager->readOnly = readOnly;
drhf57b14a2001-09-14 18:54:08 +0000886 pPager->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000887 pPager->noSync = pPager->tempFile || !useJournal;
drhed7c8552001-04-11 14:29:21 +0000888 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000889 pPager->pFirstSynced = 0;
drhed7c8552001-04-11 14:29:21 +0000890 pPager->pLast = 0;
drh7c717f72001-06-24 20:39:41 +0000891 pPager->nExtra = nExtra;
drhed7c8552001-04-11 14:29:21 +0000892 memset(pPager->aHash, 0, sizeof(pPager->aHash));
893 *ppPager = pPager;
894 return SQLITE_OK;
895}
896
897/*
drh72f82862001-05-24 21:06:34 +0000898** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +0000899** when the reference count on each page reaches zero. The destructor can
900** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +0000901**
902** The destructor is not called as a result sqlitepager_close().
903** Destructors are only called by sqlitepager_unref().
904*/
905void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){
906 pPager->xDestructor = xDesc;
907}
908
909/*
drh5e00f6c2001-09-13 13:46:56 +0000910** Return the total number of pages in the disk file associated with
911** pPager.
drhed7c8552001-04-11 14:29:21 +0000912*/
drhd9b02572001-04-15 00:37:09 +0000913int sqlitepager_pagecount(Pager *pPager){
drh28be87c2002-11-05 23:03:02 +0000914 off_t n;
drhd9b02572001-04-15 00:37:09 +0000915 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +0000916 if( pPager->dbSize>=0 ){
917 return pPager->dbSize;
918 }
drha7fcb052001-12-14 15:09:55 +0000919 if( sqliteOsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000920 pPager->errMask |= PAGER_ERR_DISK;
drh8cfbf082001-09-19 13:22:39 +0000921 return 0;
drhed7c8552001-04-11 14:29:21 +0000922 }
drh8cfbf082001-09-19 13:22:39 +0000923 n /= SQLITE_PAGE_SIZE;
drhd9b02572001-04-15 00:37:09 +0000924 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +0000925 pPager->dbSize = n;
926 }
927 return n;
928}
929
930/*
931** Shutdown the page cache. Free all memory and close all files.
932**
933** If a transaction was in progress when this routine is called, that
934** transaction is rolled back. All outstanding pages are invalidated
935** and their memory is freed. Any attempt to use a page associated
936** with this page cache after this function returns will likely
937** result in a coredump.
938*/
drhd9b02572001-04-15 00:37:09 +0000939int sqlitepager_close(Pager *pPager){
940 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +0000941 switch( pPager->state ){
942 case SQLITE_WRITELOCK: {
drhd9b02572001-04-15 00:37:09 +0000943 sqlitepager_rollback(pPager);
drha7fcb052001-12-14 15:09:55 +0000944 sqliteOsUnlock(&pPager->fd);
drh8cfbf082001-09-19 13:22:39 +0000945 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000946 break;
947 }
948 case SQLITE_READLOCK: {
drha7fcb052001-12-14 15:09:55 +0000949 sqliteOsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000950 break;
951 }
952 default: {
953 /* Do nothing */
954 break;
955 }
956 }
drhd9b02572001-04-15 00:37:09 +0000957 for(pPg=pPager->pAll; pPg; pPg=pNext){
958 pNext = pPg->pNextAll;
959 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000960 }
drha7fcb052001-12-14 15:09:55 +0000961 sqliteOsClose(&pPager->fd);
drh8cfbf082001-09-19 13:22:39 +0000962 assert( pPager->journalOpen==0 );
drh0f892532002-05-30 12:27:03 +0000963 /* Temp files are automatically deleted by the OS
964 ** if( pPager->tempFile ){
965 ** sqliteOsDelete(pPager->zFilename);
966 ** }
967 */
drhdb48ee02003-01-16 13:42:43 +0000968 CLR_PAGER(pPager);
drh73509ee2003-04-06 20:44:45 +0000969 if( pPager->zFilename!=(char*)&pPager[1] ){
970 sqliteFree(pPager->zFilename);
971 sqliteFree(pPager->zJournal);
972 }
drhed7c8552001-04-11 14:29:21 +0000973 sqliteFree(pPager);
974 return SQLITE_OK;
975}
976
977/*
drh5e00f6c2001-09-13 13:46:56 +0000978** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +0000979*/
drhd9b02572001-04-15 00:37:09 +0000980Pgno sqlitepager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +0000981 PgHdr *p = DATA_TO_PGHDR(pData);
982 return p->pgno;
983}
984
985/*
drh7e3b0a02001-04-28 16:52:40 +0000986** Increment the reference count for a page. If the page is
987** currently on the freelist (the reference count is zero) then
988** remove it from the freelist.
989*/
drh836faa42003-01-11 13:30:57 +0000990#define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
991static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +0000992 if( pPg->nRef==0 ){
993 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +0000994 if( pPg==pPg->pPager->pFirstSynced ){
995 PgHdr *p = pPg->pNextFree;
996 while( p && p->needSync ){ p = p->pNextFree; }
997 pPg->pPager->pFirstSynced = p;
998 }
drh7e3b0a02001-04-28 16:52:40 +0000999 if( pPg->pPrevFree ){
1000 pPg->pPrevFree->pNextFree = pPg->pNextFree;
1001 }else{
1002 pPg->pPager->pFirst = pPg->pNextFree;
1003 }
1004 if( pPg->pNextFree ){
1005 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1006 }else{
1007 pPg->pPager->pLast = pPg->pPrevFree;
1008 }
1009 pPg->pPager->nRef++;
1010 }
1011 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00001012 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00001013}
1014
1015/*
1016** Increment the reference count for a page. The input pointer is
1017** a reference to the page data.
1018*/
1019int sqlitepager_ref(void *pData){
1020 PgHdr *pPg = DATA_TO_PGHDR(pData);
1021 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +00001022 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001023}
1024
1025/*
drhb19a2bc2001-09-16 00:13:26 +00001026** Sync the journal and then write all free dirty pages to the database
1027** file.
1028**
1029** Writing all free dirty pages to the database after the sync is a
1030** non-obvious optimization. fsync() is an expensive operation so we
drhaaab5722002-02-19 13:39:21 +00001031** want to minimize the number ot times it is called. After an fsync() call,
drh6446c4d2001-12-15 14:22:18 +00001032** we are free to write dirty pages back to the database. It is best
1033** to go ahead and write as many dirty pages as possible to minimize
1034** the risk of having to do another fsync() later on. Writing dirty
1035** free pages in this way was observed to make database operations go
1036** up to 10 times faster.
drhfa86c412002-02-02 15:01:15 +00001037**
1038** If we are writing to temporary database, there is no need to preserve
1039** the integrity of the journal file, so we can save time and skip the
1040** fsync().
drh50e5dad2001-09-15 00:57:28 +00001041*/
1042static int syncAllPages(Pager *pPager){
1043 PgHdr *pPg;
1044 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00001045
1046 /* Sync the journal before modifying the main database
1047 ** (assuming there is a journal and it needs to be synced.)
1048 */
drh50e5dad2001-09-15 00:57:28 +00001049 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00001050 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00001051 assert( pPager->journalOpen );
1052 assert( !pPager->noSync );
drh968af522003-02-11 14:55:40 +00001053#ifndef NDEBUG
1054 {
drh4a0681e2003-02-13 01:58:20 +00001055 off_t hdrSz, pgSz, jSz;
drh968af522003-02-11 14:55:40 +00001056 hdrSz = JOURNAL_HDR_SZ(journal_format);
1057 pgSz = JOURNAL_PG_SZ(journal_format);
drh4a0681e2003-02-13 01:58:20 +00001058 rc = sqliteOsFileSize(&pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00001059 if( rc!=0 ) return rc;
drh4a0681e2003-02-13 01:58:20 +00001060 assert( pPager->nRec*pgSz+hdrSz==jSz );
drh968af522003-02-11 14:55:40 +00001061 }
1062#endif
drhd8d66e82003-02-12 02:10:15 +00001063 if( journal_format>=3 ){
1064 off_t szJ;
1065 if( pPager->fullSync ){
1066 TRACE1("SYNC\n");
1067 rc = sqliteOsSync(&pPager->jfd);
1068 if( rc!=0 ) return rc;
1069 }
1070 sqliteOsSeek(&pPager->jfd, sizeof(aJournalMagic1));
drh99ee3602003-02-16 19:13:36 +00001071 rc = write32bits(&pPager->jfd, pPager->nRec);
1072 if( rc ) return rc;
drhd8d66e82003-02-12 02:10:15 +00001073 szJ = JOURNAL_HDR_SZ(journal_format) +
1074 pPager->nRec*JOURNAL_PG_SZ(journal_format);
1075 sqliteOsSeek(&pPager->jfd, szJ);
drh968af522003-02-11 14:55:40 +00001076 }
drhdb48ee02003-01-16 13:42:43 +00001077 TRACE1("SYNC\n");
drhfa86c412002-02-02 15:01:15 +00001078 rc = sqliteOsSync(&pPager->jfd);
1079 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00001080 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00001081 }
drh50e5dad2001-09-15 00:57:28 +00001082 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00001083
1084 /* Erase the needSync flag from every page.
1085 */
1086 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1087 pPg->needSync = 0;
1088 }
1089 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00001090 }
drh03eb96a2002-11-10 23:32:56 +00001091
drh341eae82003-01-21 02:39:36 +00001092#ifndef NDEBUG
1093 /* If the Pager.needSync flag is clear then the PgHdr.needSync
1094 ** flag must also be clear for all pages. Verify that this
1095 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00001096 */
drh341eae82003-01-21 02:39:36 +00001097 else{
1098 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1099 assert( pPg->needSync==0 );
1100 }
1101 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00001102 }
drh341eae82003-01-21 02:39:36 +00001103#endif
drhdb48ee02003-01-16 13:42:43 +00001104
drh81a20f22001-10-12 17:30:04 +00001105 return rc;
drh50e5dad2001-09-15 00:57:28 +00001106}
1107
1108/*
drh2554f8b2003-01-22 01:26:44 +00001109** Given a list of pages (connected by the PgHdr.pDirty pointer) write
1110** every one of those pages out to the database file and mark them all
1111** as clean.
1112*/
1113static int pager_write_pagelist(PgHdr *pList){
1114 Pager *pPager;
1115 int rc;
1116
1117 if( pList==0 ) return SQLITE_OK;
1118 pPager = pList->pPager;
1119 while( pList ){
1120 assert( pList->dirty );
1121 sqliteOsSeek(&pPager->fd, (pList->pgno-1)*(off_t)SQLITE_PAGE_SIZE);
1122 rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pList), SQLITE_PAGE_SIZE);
1123 if( rc ) return rc;
1124 pList->dirty = 0;
1125 pList = pList->pDirty;
1126 }
1127 return SQLITE_OK;
1128}
1129
1130/*
1131** Collect every dirty page into a dirty list and
1132** return a pointer to the head of that list. All pages are
1133** collected even if they are still in use.
1134*/
1135static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
1136 PgHdr *p, *pList;
1137 pList = 0;
1138 for(p=pPager->pAll; p; p=p->pNextAll){
1139 if( p->dirty ){
1140 p->pDirty = pList;
1141 pList = p;
1142 }
1143 }
1144 return pList;
1145}
1146
1147/*
drhd9b02572001-04-15 00:37:09 +00001148** Acquire a page.
1149**
drh58a11682001-11-10 13:51:08 +00001150** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00001151** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00001152**
drh306dc212001-05-21 13:45:10 +00001153** A _get works for any page number greater than 0. If the database
1154** file is smaller than the requested page, then no actual disk
1155** read occurs and the memory image of the page is initialized to
1156** all zeros. The extra data appended to a page is always initialized
1157** to zeros the first time a page is loaded into memory.
1158**
drhd9b02572001-04-15 00:37:09 +00001159** The acquisition might fail for several reasons. In all cases,
1160** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00001161**
1162** See also sqlitepager_lookup(). Both this routine and _lookup() attempt
1163** to find a page in the in-memory cache first. If the page is not already
drh5e00f6c2001-09-13 13:46:56 +00001164** in memory, this routine goes to disk to read it in whereas _lookup()
drh7e3b0a02001-04-28 16:52:40 +00001165** just returns 0. This routine acquires a read-lock the first time it
1166** has to go to disk, and could also playback an old journal if necessary.
1167** Since _lookup() never goes to disk, it never has to deal with locks
1168** or journal files.
drhed7c8552001-04-11 14:29:21 +00001169*/
drhd9b02572001-04-15 00:37:09 +00001170int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +00001171 PgHdr *pPg;
drh8766c342002-11-09 00:33:15 +00001172 int rc;
drhed7c8552001-04-11 14:29:21 +00001173
drhd9b02572001-04-15 00:37:09 +00001174 /* Make sure we have not hit any critical errors.
1175 */
drh836faa42003-01-11 13:30:57 +00001176 assert( pPager!=0 );
1177 assert( pgno!=0 );
drhd9b02572001-04-15 00:37:09 +00001178 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1179 return pager_errcode(pPager);
1180 }
1181
drhed7c8552001-04-11 14:29:21 +00001182 /* If this is the first page accessed, then get a read lock
1183 ** on the database file.
1184 */
1185 if( pPager->nRef==0 ){
drh8766c342002-11-09 00:33:15 +00001186 rc = sqliteOsReadLock(&pPager->fd);
1187 if( rc!=SQLITE_OK ){
drhed7c8552001-04-11 14:29:21 +00001188 *ppPage = 0;
drh8766c342002-11-09 00:33:15 +00001189 return rc;
drhed7c8552001-04-11 14:29:21 +00001190 }
drhd9b02572001-04-15 00:37:09 +00001191 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +00001192
1193 /* If a journal file exists, try to play it back.
1194 */
drhda47d772002-12-02 04:25:19 +00001195 if( pPager->useJournal && sqliteOsFileExists(pPager->zJournal) ){
drhf57b3392001-10-08 13:22:32 +00001196 int rc, dummy;
drhed7c8552001-04-11 14:29:21 +00001197
drha7fcb052001-12-14 15:09:55 +00001198 /* Get a write lock on the database
1199 */
1200 rc = sqliteOsWriteLock(&pPager->fd);
1201 if( rc!=SQLITE_OK ){
drh8766c342002-11-09 00:33:15 +00001202 if( sqliteOsUnlock(&pPager->fd)!=SQLITE_OK ){
1203 /* This should never happen! */
1204 rc = SQLITE_INTERNAL;
1205 }
drha7fcb052001-12-14 15:09:55 +00001206 *ppPage = 0;
drh8766c342002-11-09 00:33:15 +00001207 return rc;
drha7fcb052001-12-14 15:09:55 +00001208 }
1209 pPager->state = SQLITE_WRITELOCK;
1210
drhed7c8552001-04-11 14:29:21 +00001211 /* Open the journal for exclusive access. Return SQLITE_BUSY if
drhf57b3392001-10-08 13:22:32 +00001212 ** we cannot get exclusive access to the journal file.
1213 **
1214 ** Even though we will only be reading from the journal, not writing,
1215 ** we have to open the journal for writing in order to obtain an
1216 ** exclusive access lock.
drhed7c8552001-04-11 14:29:21 +00001217 */
drhf57b3392001-10-08 13:22:32 +00001218 rc = sqliteOsOpenReadWrite(pPager->zJournal, &pPager->jfd, &dummy);
drha7fcb052001-12-14 15:09:55 +00001219 if( rc!=SQLITE_OK ){
1220 rc = sqliteOsUnlock(&pPager->fd);
1221 assert( rc==SQLITE_OK );
drhed7c8552001-04-11 14:29:21 +00001222 *ppPage = 0;
1223 return SQLITE_BUSY;
1224 }
drha7fcb052001-12-14 15:09:55 +00001225 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001226 pPager->journalStarted = 0;
drhed7c8552001-04-11 14:29:21 +00001227
1228 /* Playback and delete the journal. Drop the database write
1229 ** lock and reacquire the read lock.
1230 */
drh99ee3602003-02-16 19:13:36 +00001231 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00001232 if( rc!=SQLITE_OK ){
1233 return rc;
1234 }
drhed7c8552001-04-11 14:29:21 +00001235 }
1236 pPg = 0;
1237 }else{
1238 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +00001239 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00001240 }
1241 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001242 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +00001243 int h;
drh7e3b0a02001-04-28 16:52:40 +00001244 pPager->nMiss++;
drhed7c8552001-04-11 14:29:21 +00001245 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){
1246 /* Create a new page */
drh968af522003-02-11 14:55:40 +00001247 pPg = sqliteMallocRaw( sizeof(*pPg) + SQLITE_PAGE_SIZE
1248 + sizeof(u32) + pPager->nExtra );
drhd9b02572001-04-15 00:37:09 +00001249 if( pPg==0 ){
1250 *ppPage = 0;
1251 pager_unwritelock(pPager);
1252 pPager->errMask |= PAGER_ERR_MEM;
1253 return SQLITE_NOMEM;
1254 }
drh8c1238a2003-01-02 14:43:55 +00001255 memset(pPg, 0, sizeof(*pPg));
drhed7c8552001-04-11 14:29:21 +00001256 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +00001257 pPg->pNextAll = pPager->pAll;
1258 if( pPager->pAll ){
1259 pPager->pAll->pPrevAll = pPg;
1260 }
1261 pPg->pPrevAll = 0;
drhd79caeb2001-04-15 02:27:24 +00001262 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +00001263 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +00001264 }else{
drhdb48ee02003-01-16 13:42:43 +00001265 /* Find a page to recycle. Try to locate a page that does not
1266 ** require us to do an fsync() on the journal.
1267 */
drh341eae82003-01-21 02:39:36 +00001268 pPg = pPager->pFirstSynced;
drhb19a2bc2001-09-16 00:13:26 +00001269
drhdb48ee02003-01-16 13:42:43 +00001270 /* If we could not find a page that does not require an fsync()
1271 ** on the journal file then fsync the journal file. This is a
1272 ** very slow operation, so we work hard to avoid it. But sometimes
1273 ** it can't be helped.
drhb19a2bc2001-09-16 00:13:26 +00001274 */
drh603240c2002-03-05 01:11:12 +00001275 if( pPg==0 ){
drh50e5dad2001-09-15 00:57:28 +00001276 int rc = syncAllPages(pPager);
1277 if( rc!=0 ){
1278 sqlitepager_rollback(pPager);
1279 *ppPage = 0;
1280 return SQLITE_IOERR;
1281 }
1282 pPg = pPager->pFirst;
1283 }
drhd9b02572001-04-15 00:37:09 +00001284 assert( pPg->nRef==0 );
drhdb48ee02003-01-16 13:42:43 +00001285
1286 /* Write the page to the database file if it is dirty.
1287 */
1288 if( pPg->dirty ){
1289 assert( pPg->needSync==0 );
drh2554f8b2003-01-22 01:26:44 +00001290 pPg->pDirty = 0;
1291 rc = pager_write_pagelist( pPg );
drhdb48ee02003-01-16 13:42:43 +00001292 if( rc!=SQLITE_OK ){
1293 sqlitepager_rollback(pPager);
1294 *ppPage = 0;
1295 return SQLITE_IOERR;
1296 }
drhdb48ee02003-01-16 13:42:43 +00001297 }
drh50e5dad2001-09-15 00:57:28 +00001298 assert( pPg->dirty==0 );
drhd9b02572001-04-15 00:37:09 +00001299
drhdb48ee02003-01-16 13:42:43 +00001300 /* If the page we are recycling is marked as alwaysRollback, then
drh193a6b42002-07-07 16:52:46 +00001301 ** set the global alwaysRollback flag, thus disabling the
1302 ** sqlite_dont_rollback() optimization for the rest of this transaction.
1303 ** It is necessary to do this because the page marked alwaysRollback
1304 ** might be reloaded at a later time but at that point we won't remember
1305 ** that is was marked alwaysRollback. This means that all pages must
1306 ** be marked as alwaysRollback from here on out.
1307 */
1308 if( pPg->alwaysRollback ){
1309 pPager->alwaysRollback = 1;
1310 }
1311
drhd9b02572001-04-15 00:37:09 +00001312 /* Unlink the old page from the free list and the hash table
1313 */
drh341eae82003-01-21 02:39:36 +00001314 if( pPg==pPager->pFirstSynced ){
1315 PgHdr *p = pPg->pNextFree;
1316 while( p && p->needSync ){ p = p->pNextFree; }
1317 pPager->pFirstSynced = p;
1318 }
drh6019e162001-07-02 17:51:45 +00001319 if( pPg->pPrevFree ){
1320 pPg->pPrevFree->pNextFree = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +00001321 }else{
drh6019e162001-07-02 17:51:45 +00001322 assert( pPager->pFirst==pPg );
1323 pPager->pFirst = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +00001324 }
drh6019e162001-07-02 17:51:45 +00001325 if( pPg->pNextFree ){
1326 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1327 }else{
1328 assert( pPager->pLast==pPg );
1329 pPager->pLast = pPg->pPrevFree;
1330 }
1331 pPg->pNextFree = pPg->pPrevFree = 0;
drhed7c8552001-04-11 14:29:21 +00001332 if( pPg->pNextHash ){
1333 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
1334 }
1335 if( pPg->pPrevHash ){
1336 pPg->pPrevHash->pNextHash = pPg->pNextHash;
1337 }else{
drhd9b02572001-04-15 00:37:09 +00001338 h = pager_hash(pPg->pgno);
drhed7c8552001-04-11 14:29:21 +00001339 assert( pPager->aHash[h]==pPg );
1340 pPager->aHash[h] = pPg->pNextHash;
1341 }
drh6019e162001-07-02 17:51:45 +00001342 pPg->pNextHash = pPg->pPrevHash = 0;
drhd9b02572001-04-15 00:37:09 +00001343 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +00001344 }
1345 pPg->pgno = pgno;
drh1ab43002002-01-14 09:28:19 +00001346 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
drhed6c8672003-01-12 18:02:16 +00001347 sqliteCheckMemory(pPager->aInJournal, pgno/8);
drhdb48ee02003-01-16 13:42:43 +00001348 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00001349 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00001350 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001351 }else{
1352 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00001353 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001354 }
drh03eb96a2002-11-10 23:32:56 +00001355 if( pPager->aInCkpt && (int)pgno<=pPager->ckptSize
1356 && (pPager->aInCkpt[pgno/8] & (1<<(pgno&7)))!=0 ){
1357 page_add_to_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001358 }else{
drh03eb96a2002-11-10 23:32:56 +00001359 page_remove_from_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001360 }
drhed7c8552001-04-11 14:29:21 +00001361 pPg->dirty = 0;
1362 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00001363 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001364 pPager->nRef++;
1365 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +00001366 pPg->pNextHash = pPager->aHash[h];
1367 pPager->aHash[h] = pPg;
1368 if( pPg->pNextHash ){
1369 assert( pPg->pNextHash->pPrevHash==0 );
1370 pPg->pNextHash->pPrevHash = pPg;
1371 }
drh306dc212001-05-21 13:45:10 +00001372 if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager);
drh1ab43002002-01-14 09:28:19 +00001373 if( pPager->dbSize<(int)pgno ){
drh306dc212001-05-21 13:45:10 +00001374 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
1375 }else{
drh81a20f22001-10-12 17:30:04 +00001376 int rc;
drhd0d006e2002-12-01 02:00:57 +00001377 sqliteOsSeek(&pPager->fd, (pgno-1)*(off_t)SQLITE_PAGE_SIZE);
drha7fcb052001-12-14 15:09:55 +00001378 rc = sqliteOsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drh81a20f22001-10-12 17:30:04 +00001379 if( rc!=SQLITE_OK ){
drh28be87c2002-11-05 23:03:02 +00001380 off_t fileSize;
drh4e371ee2002-09-05 16:08:27 +00001381 if( sqliteOsFileSize(&pPager->fd,&fileSize)!=SQLITE_OK
1382 || fileSize>=pgno*SQLITE_PAGE_SIZE ){
1383 return rc;
1384 }else{
1385 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
1386 }
drh81a20f22001-10-12 17:30:04 +00001387 }
drh306dc212001-05-21 13:45:10 +00001388 }
drh7e3b0a02001-04-28 16:52:40 +00001389 if( pPager->nExtra>0 ){
1390 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
1391 }
drhed7c8552001-04-11 14:29:21 +00001392 }else{
drhd9b02572001-04-15 00:37:09 +00001393 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +00001394 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +00001395 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00001396 }
1397 *ppPage = PGHDR_TO_DATA(pPg);
1398 return SQLITE_OK;
1399}
1400
1401/*
drh7e3b0a02001-04-28 16:52:40 +00001402** Acquire a page if it is already in the in-memory cache. Do
1403** not read the page from disk. Return a pointer to the page,
1404** or 0 if the page is not in cache.
1405**
1406** See also sqlitepager_get(). The difference between this routine
1407** and sqlitepager_get() is that _get() will go to the disk and read
1408** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00001409** returns NULL if the page is not in cache or if a disk I/O error
1410** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00001411*/
1412void *sqlitepager_lookup(Pager *pPager, Pgno pgno){
1413 PgHdr *pPg;
1414
drh836faa42003-01-11 13:30:57 +00001415 assert( pPager!=0 );
1416 assert( pgno!=0 );
drh7e3b0a02001-04-28 16:52:40 +00001417 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1418 return 0;
1419 }
drh836faa42003-01-11 13:30:57 +00001420 /* if( pPager->nRef==0 ){
1421 ** return 0;
1422 ** }
1423 */
drh7e3b0a02001-04-28 16:52:40 +00001424 pPg = pager_lookup(pPager, pgno);
1425 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +00001426 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +00001427 return PGHDR_TO_DATA(pPg);
1428}
1429
1430/*
drhed7c8552001-04-11 14:29:21 +00001431** Release a page.
1432**
1433** If the number of references to the page drop to zero, then the
1434** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00001435** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00001436** removed.
1437*/
drhd9b02572001-04-15 00:37:09 +00001438int sqlitepager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +00001439 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001440
1441 /* Decrement the reference count for this page
1442 */
drhed7c8552001-04-11 14:29:21 +00001443 pPg = DATA_TO_PGHDR(pData);
1444 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +00001445 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00001446 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001447
drh72f82862001-05-24 21:06:34 +00001448 /* When the number of references to a page reach 0, call the
1449 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00001450 */
drhed7c8552001-04-11 14:29:21 +00001451 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00001452 Pager *pPager;
1453 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00001454 pPg->pNextFree = 0;
1455 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00001456 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00001457 if( pPg->pPrevFree ){
1458 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00001459 }else{
1460 pPager->pFirst = pPg;
1461 }
drh341eae82003-01-21 02:39:36 +00001462 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
1463 pPager->pFirstSynced = pPg;
1464 }
drh72f82862001-05-24 21:06:34 +00001465 if( pPager->xDestructor ){
1466 pPager->xDestructor(pData);
1467 }
drhd9b02572001-04-15 00:37:09 +00001468
1469 /* When all pages reach the freelist, drop the read lock from
1470 ** the database file.
1471 */
1472 pPager->nRef--;
1473 assert( pPager->nRef>=0 );
1474 if( pPager->nRef==0 ){
1475 pager_reset(pPager);
1476 }
drhed7c8552001-04-11 14:29:21 +00001477 }
drhd9b02572001-04-15 00:37:09 +00001478 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00001479}
1480
1481/*
drhda47d772002-12-02 04:25:19 +00001482** Create a journal file for pPager. There should already be a write
1483** lock on the database file when this routine is called.
1484**
1485** Return SQLITE_OK if everything. Return an error code and release the
1486** write lock if anything goes wrong.
1487*/
1488static int pager_open_journal(Pager *pPager){
1489 int rc;
1490 assert( pPager->state==SQLITE_WRITELOCK );
1491 assert( pPager->journalOpen==0 );
1492 assert( pPager->useJournal );
1493 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
1494 if( pPager->aInJournal==0 ){
1495 sqliteOsReadLock(&pPager->fd);
1496 pPager->state = SQLITE_READLOCK;
1497 return SQLITE_NOMEM;
1498 }
1499 rc = sqliteOsOpenExclusive(pPager->zJournal, &pPager->jfd,pPager->tempFile);
1500 if( rc!=SQLITE_OK ){
1501 sqliteFree(pPager->aInJournal);
1502 pPager->aInJournal = 0;
1503 sqliteOsReadLock(&pPager->fd);
1504 pPager->state = SQLITE_READLOCK;
1505 return SQLITE_CANTOPEN;
1506 }
1507 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001508 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00001509 pPager->needSync = 0;
1510 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00001511 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001512 sqlitepager_pagecount(pPager);
1513 pPager->origDbSize = pPager->dbSize;
drh968af522003-02-11 14:55:40 +00001514 if( journal_format==JOURNAL_FORMAT_3 ){
1515 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic3, sizeof(aJournalMagic3));
1516 if( rc==SQLITE_OK ){
drh4303fee2003-02-15 23:09:17 +00001517 rc = write32bits(&pPager->jfd, pPager->noSync ? 0xffffffff : 0);
drh968af522003-02-11 14:55:40 +00001518 }
1519 if( rc==SQLITE_OK ){
1520 pPager->cksumInit = (u32)sqliteRandomInteger();
1521 rc = write32bits(&pPager->jfd, pPager->cksumInit);
1522 }
1523 }else if( journal_format==JOURNAL_FORMAT_2 ){
1524 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic2, sizeof(aJournalMagic2));
drhda47d772002-12-02 04:25:19 +00001525 }else{
drh968af522003-02-11 14:55:40 +00001526 assert( journal_format==JOURNAL_FORMAT_1 );
1527 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic1, sizeof(aJournalMagic1));
drhda47d772002-12-02 04:25:19 +00001528 }
1529 if( rc==SQLITE_OK ){
1530 rc = write32bits(&pPager->jfd, pPager->dbSize);
1531 }
1532 if( pPager->ckptAutoopen && rc==SQLITE_OK ){
1533 rc = sqlitepager_ckpt_begin(pPager);
1534 }
1535 if( rc!=SQLITE_OK ){
1536 rc = pager_unwritelock(pPager);
1537 if( rc==SQLITE_OK ){
1538 rc = SQLITE_FULL;
1539 }
1540 }
1541 return rc;
1542}
1543
1544/*
drh4b845d72002-03-05 12:41:19 +00001545** Acquire a write-lock on the database. The lock is removed when
1546** the any of the following happen:
1547**
1548** * sqlitepager_commit() is called.
1549** * sqlitepager_rollback() is called.
1550** * sqlitepager_close() is called.
1551** * sqlitepager_unref() is called to on every outstanding page.
1552**
1553** The parameter to this routine is a pointer to any open page of the
1554** database file. Nothing changes about the page - it is used merely
1555** to acquire a pointer to the Pager structure and as proof that there
1556** is already a read-lock on the database.
1557**
drhda47d772002-12-02 04:25:19 +00001558** A journal file is opened if this is not a temporary file. For
1559** temporary files, the opening of the journal file is deferred until
1560** there is an actual need to write to the journal.
1561**
drh4b845d72002-03-05 12:41:19 +00001562** If the database is already write-locked, this routine is a no-op.
1563*/
1564int sqlitepager_begin(void *pData){
1565 PgHdr *pPg = DATA_TO_PGHDR(pData);
1566 Pager *pPager = pPg->pPager;
1567 int rc = SQLITE_OK;
1568 assert( pPg->nRef>0 );
1569 assert( pPager->state!=SQLITE_UNLOCK );
1570 if( pPager->state==SQLITE_READLOCK ){
1571 assert( pPager->aInJournal==0 );
1572 rc = sqliteOsWriteLock(&pPager->fd);
1573 if( rc!=SQLITE_OK ){
1574 return rc;
1575 }
drh4b845d72002-03-05 12:41:19 +00001576 pPager->state = SQLITE_WRITELOCK;
drhda47d772002-12-02 04:25:19 +00001577 pPager->dirtyFile = 0;
drhdb48ee02003-01-16 13:42:43 +00001578 TRACE1("TRANSACTION\n");
drhda47d772002-12-02 04:25:19 +00001579 if( pPager->useJournal && !pPager->tempFile ){
1580 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00001581 }
1582 }
1583 return rc;
1584}
1585
1586/*
drhed7c8552001-04-11 14:29:21 +00001587** Mark a data page as writeable. The page is written into the journal
1588** if it is not there already. This routine must be called before making
1589** changes to a page.
1590**
1591** The first time this routine is called, the pager creates a new
1592** journal and acquires a write lock on the database. If the write
1593** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00001594** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00001595** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00001596**
1597** If the journal file could not be written because the disk is full,
1598** then this routine returns SQLITE_FULL and does an immediate rollback.
1599** All subsequent write attempts also return SQLITE_FULL until there
1600** is a call to sqlitepager_commit() or sqlitepager_rollback() to
1601** reset.
drhed7c8552001-04-11 14:29:21 +00001602*/
drhd9b02572001-04-15 00:37:09 +00001603int sqlitepager_write(void *pData){
drh69688d52001-04-14 16:38:23 +00001604 PgHdr *pPg = DATA_TO_PGHDR(pData);
1605 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00001606 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00001607
drh6446c4d2001-12-15 14:22:18 +00001608 /* Check for errors
1609 */
drhd9b02572001-04-15 00:37:09 +00001610 if( pPager->errMask ){
1611 return pager_errcode(pPager);
1612 }
drh5e00f6c2001-09-13 13:46:56 +00001613 if( pPager->readOnly ){
1614 return SQLITE_PERM;
1615 }
drh6446c4d2001-12-15 14:22:18 +00001616
1617 /* Mark the page as dirty. If the page has already been written
1618 ** to the journal then we can return right away.
1619 */
drhd9b02572001-04-15 00:37:09 +00001620 pPg->dirty = 1;
drh0f892532002-05-30 12:27:03 +00001621 if( pPg->inJournal && (pPg->inCkpt || pPager->ckptInUse==0) ){
drha1680452002-04-18 01:56:57 +00001622 pPager->dirtyFile = 1;
drhfa86c412002-02-02 15:01:15 +00001623 return SQLITE_OK;
1624 }
drh6446c4d2001-12-15 14:22:18 +00001625
1626 /* If we get this far, it means that the page needs to be
drhfa86c412002-02-02 15:01:15 +00001627 ** written to the transaction journal or the ckeckpoint journal
1628 ** or both.
1629 **
1630 ** First check to see that the transaction journal exists and
1631 ** create it if it does not.
drh6446c4d2001-12-15 14:22:18 +00001632 */
drhd9b02572001-04-15 00:37:09 +00001633 assert( pPager->state!=SQLITE_UNLOCK );
drh4b845d72002-03-05 12:41:19 +00001634 rc = sqlitepager_begin(pData);
drhda47d772002-12-02 04:25:19 +00001635 if( rc!=SQLITE_OK ){
1636 return rc;
1637 }
drhd9b02572001-04-15 00:37:09 +00001638 assert( pPager->state==SQLITE_WRITELOCK );
drhda47d772002-12-02 04:25:19 +00001639 if( !pPager->journalOpen && pPager->useJournal ){
1640 rc = pager_open_journal(pPager);
1641 if( rc!=SQLITE_OK ) return rc;
1642 }
1643 assert( pPager->journalOpen || !pPager->useJournal );
1644 pPager->dirtyFile = 1;
drh6446c4d2001-12-15 14:22:18 +00001645
drhfa86c412002-02-02 15:01:15 +00001646 /* The transaction journal now exists and we have a write lock on the
1647 ** main database file. Write the current page to the transaction
1648 ** journal if it is not there already.
drh6446c4d2001-12-15 14:22:18 +00001649 */
drhdb48ee02003-01-16 13:42:43 +00001650 if( !pPg->inJournal && pPager->useJournal ){
1651 if( (int)pPg->pgno <= pPager->origDbSize ){
drh968af522003-02-11 14:55:40 +00001652 int szPg;
1653 u32 saved;
1654 if( journal_format>=JOURNAL_FORMAT_3 ){
1655 u32 cksum = pager_cksum(pPager, pPg->pgno, pData);
1656 saved = *(u32*)PGHDR_TO_EXTRA(pPg);
1657 store32bits(cksum, pPg, SQLITE_PAGE_SIZE);
1658 szPg = SQLITE_PAGE_SIZE+8;
1659 }else{
1660 szPg = SQLITE_PAGE_SIZE+4;
1661 }
1662 store32bits(pPg->pgno, pPg, -4);
1663 rc = sqliteOsWrite(&pPager->jfd, &((char*)pData)[-4], szPg);
1664 if( journal_format>=JOURNAL_FORMAT_3 ){
1665 *(u32*)PGHDR_TO_EXTRA(pPg) = saved;
1666 }
drhdb48ee02003-01-16 13:42:43 +00001667 if( rc!=SQLITE_OK ){
1668 sqlitepager_rollback(pPager);
1669 pPager->errMask |= PAGER_ERR_FULL;
1670 return rc;
1671 }
drh99ee3602003-02-16 19:13:36 +00001672 pPager->nRec++;
drhdb48ee02003-01-16 13:42:43 +00001673 assert( pPager->aInJournal!=0 );
1674 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1675 pPg->needSync = !pPager->noSync;
1676 pPg->inJournal = 1;
1677 if( pPager->ckptInUse ){
1678 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1679 page_add_to_ckpt_list(pPg);
1680 }
1681 TRACE3("JOURNAL %d %d\n", pPg->pgno, pPg->needSync);
1682 }else{
1683 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
1684 TRACE3("APPEND %d %d\n", pPg->pgno, pPg->needSync);
drhd9b02572001-04-15 00:37:09 +00001685 }
drhdb48ee02003-01-16 13:42:43 +00001686 if( pPg->needSync ){
1687 pPager->needSync = 1;
drhfa86c412002-02-02 15:01:15 +00001688 }
drh69688d52001-04-14 16:38:23 +00001689 }
drh6446c4d2001-12-15 14:22:18 +00001690
drhfa86c412002-02-02 15:01:15 +00001691 /* If the checkpoint journal is open and the page is not in it,
drh968af522003-02-11 14:55:40 +00001692 ** then write the current page to the checkpoint journal. Note that
1693 ** the checkpoint journal always uses the simplier format 2 that lacks
1694 ** checksums. The header is also omitted from the checkpoint journal.
drh6446c4d2001-12-15 14:22:18 +00001695 */
drh0f892532002-05-30 12:27:03 +00001696 if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
drh1e336b42002-02-14 12:50:33 +00001697 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drh968af522003-02-11 14:55:40 +00001698 store32bits(pPg->pgno, pPg, -4);
drh2554f8b2003-01-22 01:26:44 +00001699 rc = sqliteOsWrite(&pPager->cpfd, &((char*)pData)[-4], SQLITE_PAGE_SIZE+4);
drhfa86c412002-02-02 15:01:15 +00001700 if( rc!=SQLITE_OK ){
1701 sqlitepager_rollback(pPager);
1702 pPager->errMask |= PAGER_ERR_FULL;
1703 return rc;
1704 }
drh9bd47a92003-01-07 14:46:08 +00001705 pPager->ckptNRec++;
drhfa86c412002-02-02 15:01:15 +00001706 assert( pPager->aInCkpt!=0 );
1707 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001708 page_add_to_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001709 }
1710
1711 /* Update the database size and return.
1712 */
drh1ab43002002-01-14 09:28:19 +00001713 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00001714 pPager->dbSize = pPg->pgno;
1715 }
drh69688d52001-04-14 16:38:23 +00001716 return rc;
drhed7c8552001-04-11 14:29:21 +00001717}
1718
1719/*
drhaacc5432002-01-06 17:07:40 +00001720** Return TRUE if the page given in the argument was previously passed
drh6019e162001-07-02 17:51:45 +00001721** to sqlitepager_write(). In other words, return TRUE if it is ok
1722** to change the content of the page.
1723*/
1724int sqlitepager_iswriteable(void *pData){
1725 PgHdr *pPg = DATA_TO_PGHDR(pData);
1726 return pPg->dirty;
1727}
1728
1729/*
drh001bbcb2003-03-19 03:14:00 +00001730** Replace the content of a single page with the information in the third
1731** argument.
1732*/
1733int sqlitepager_overwrite(Pager *pPager, Pgno pgno, void *pData){
1734 void *pPage;
1735 int rc;
1736
1737 rc = sqlitepager_get(pPager, pgno, &pPage);
1738 if( rc==SQLITE_OK ){
1739 rc = sqlitepager_write(pPage);
1740 if( rc==SQLITE_OK ){
1741 memcpy(pPage, pData, SQLITE_PAGE_SIZE);
1742 }
1743 sqlitepager_unref(pPage);
1744 }
1745 return rc;
1746}
1747
1748/*
drh30e58752002-03-02 20:41:57 +00001749** A call to this routine tells the pager that it is not necessary to
1750** write the information on page "pgno" back to the disk, even though
1751** that page might be marked as dirty.
1752**
1753** The overlying software layer calls this routine when all of the data
1754** on the given page is unused. The pager marks the page as clean so
1755** that it does not get written to disk.
1756**
1757** Tests show that this optimization, together with the
1758** sqlitepager_dont_rollback() below, more than double the speed
1759** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00001760**
1761** When this routine is called, set the alwaysRollback flag to true.
1762** Subsequent calls to sqlitepager_dont_rollback() for the same page
1763** will thereafter be ignored. This is necessary to avoid a problem
1764** where a page with data is added to the freelist during one part of
1765** a transaction then removed from the freelist during a later part
1766** of the same transaction and reused for some other purpose. When it
1767** is first added to the freelist, this routine is called. When reused,
1768** the dont_rollback() routine is called. But because the page contains
1769** critical data, we still need to be sure it gets rolled back in spite
1770** of the dont_rollback() call.
drh30e58752002-03-02 20:41:57 +00001771*/
1772void sqlitepager_dont_write(Pager *pPager, Pgno pgno){
1773 PgHdr *pPg;
drh8e298f92002-07-06 16:28:47 +00001774
drh30e58752002-03-02 20:41:57 +00001775 pPg = pager_lookup(pPager, pgno);
drh8e298f92002-07-06 16:28:47 +00001776 pPg->alwaysRollback = 1;
drh30e58752002-03-02 20:41:57 +00001777 if( pPg && pPg->dirty ){
drh8124a302002-06-25 14:43:57 +00001778 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
1779 /* If this pages is the last page in the file and the file has grown
1780 ** during the current transaction, then do NOT mark the page as clean.
1781 ** When the database file grows, we must make sure that the last page
1782 ** gets written at least once so that the disk file will be the correct
1783 ** size. If you do not write this page and the size of the file
1784 ** on the disk ends up being too small, that can lead to database
1785 ** corruption during the next transaction.
1786 */
1787 }else{
drhdb48ee02003-01-16 13:42:43 +00001788 TRACE2("DONT_WRITE %d\n", pgno);
drh8124a302002-06-25 14:43:57 +00001789 pPg->dirty = 0;
1790 }
drh30e58752002-03-02 20:41:57 +00001791 }
1792}
1793
1794/*
1795** A call to this routine tells the pager that if a rollback occurs,
1796** it is not necessary to restore the data on the given page. This
1797** means that the pager does not have to record the given page in the
1798** rollback journal.
1799*/
1800void sqlitepager_dont_rollback(void *pData){
1801 PgHdr *pPg = DATA_TO_PGHDR(pData);
1802 Pager *pPager = pPg->pPager;
1803
1804 if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return;
drh193a6b42002-07-07 16:52:46 +00001805 if( pPg->alwaysRollback || pPager->alwaysRollback ) return;
drh30e58752002-03-02 20:41:57 +00001806 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
1807 assert( pPager->aInJournal!=0 );
1808 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1809 pPg->inJournal = 1;
drh0f892532002-05-30 12:27:03 +00001810 if( pPager->ckptInUse ){
drh30e58752002-03-02 20:41:57 +00001811 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001812 page_add_to_ckpt_list(pPg);
drh30e58752002-03-02 20:41:57 +00001813 }
drhdb48ee02003-01-16 13:42:43 +00001814 TRACE2("DONT_ROLLBACK %d\n", pPg->pgno);
drh30e58752002-03-02 20:41:57 +00001815 }
drh0f892532002-05-30 12:27:03 +00001816 if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
drh30e58752002-03-02 20:41:57 +00001817 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
1818 assert( pPager->aInCkpt!=0 );
1819 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001820 page_add_to_ckpt_list(pPg);
drh30e58752002-03-02 20:41:57 +00001821 }
1822}
1823
1824/*
drhed7c8552001-04-11 14:29:21 +00001825** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00001826**
1827** If the commit fails for any reason, a rollback attempt is made
1828** and an error code is returned. If the commit worked, SQLITE_OK
1829** is returned.
drhed7c8552001-04-11 14:29:21 +00001830*/
drhd9b02572001-04-15 00:37:09 +00001831int sqlitepager_commit(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00001832 int rc;
drhed7c8552001-04-11 14:29:21 +00001833 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001834
1835 if( pPager->errMask==PAGER_ERR_FULL ){
1836 rc = sqlitepager_rollback(pPager);
drh4e371ee2002-09-05 16:08:27 +00001837 if( rc==SQLITE_OK ){
1838 rc = SQLITE_FULL;
1839 }
drhd9b02572001-04-15 00:37:09 +00001840 return rc;
1841 }
1842 if( pPager->errMask!=0 ){
1843 rc = pager_errcode(pPager);
1844 return rc;
1845 }
1846 if( pPager->state!=SQLITE_WRITELOCK ){
1847 return SQLITE_ERROR;
1848 }
drhdb48ee02003-01-16 13:42:43 +00001849 TRACE1("COMMIT\n");
drha1680452002-04-18 01:56:57 +00001850 if( pPager->dirtyFile==0 ){
1851 /* Exit early (without doing the time-consuming sqliteOsSync() calls)
1852 ** if there have been no changes to the database file. */
drh341eae82003-01-21 02:39:36 +00001853 assert( pPager->needSync==0 );
drha1680452002-04-18 01:56:57 +00001854 rc = pager_unwritelock(pPager);
1855 pPager->dbSize = -1;
1856 return rc;
1857 }
drhda47d772002-12-02 04:25:19 +00001858 assert( pPager->journalOpen );
drha7fcb052001-12-14 15:09:55 +00001859 if( pPager->needSync && sqliteOsSync(&pPager->jfd)!=SQLITE_OK ){
drhd9b02572001-04-15 00:37:09 +00001860 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +00001861 }
drh2554f8b2003-01-22 01:26:44 +00001862 pPg = pager_get_all_dirty_pages(pPager);
1863 if( pPg ){
1864 rc = pager_write_pagelist(pPg);
1865 if( rc || (!pPager->noSync && sqliteOsSync(&pPager->fd)!=SQLITE_OK) ){
1866 goto commit_abort;
1867 }
drh603240c2002-03-05 01:11:12 +00001868 }
drhd9b02572001-04-15 00:37:09 +00001869 rc = pager_unwritelock(pPager);
1870 pPager->dbSize = -1;
1871 return rc;
1872
1873 /* Jump here if anything goes wrong during the commit process.
1874 */
1875commit_abort:
1876 rc = sqlitepager_rollback(pPager);
1877 if( rc==SQLITE_OK ){
1878 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +00001879 }
drhed7c8552001-04-11 14:29:21 +00001880 return rc;
1881}
1882
1883/*
1884** Rollback all changes. The database falls back to read-only mode.
1885** All in-memory cache pages revert to their original data contents.
1886** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00001887**
1888** This routine cannot fail unless some other process is not following
1889** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
1890** process is writing trash into the journal file (SQLITE_CORRUPT) or
1891** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
1892** codes are returned for all these occasions. Otherwise,
1893** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00001894*/
drhd9b02572001-04-15 00:37:09 +00001895int sqlitepager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001896 int rc;
drhdb48ee02003-01-16 13:42:43 +00001897 TRACE1("ROLLBACK\n");
drhda47d772002-12-02 04:25:19 +00001898 if( !pPager->dirtyFile || !pPager->journalOpen ){
1899 rc = pager_unwritelock(pPager);
1900 pPager->dbSize = -1;
1901 return rc;
1902 }
drhdb48ee02003-01-16 13:42:43 +00001903
drhd9b02572001-04-15 00:37:09 +00001904 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
drh4b845d72002-03-05 12:41:19 +00001905 if( pPager->state>=SQLITE_WRITELOCK ){
drh99ee3602003-02-16 19:13:36 +00001906 pager_playback(pPager, 1);
drh4b845d72002-03-05 12:41:19 +00001907 }
drhd9b02572001-04-15 00:37:09 +00001908 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00001909 }
drhd9b02572001-04-15 00:37:09 +00001910 if( pPager->state!=SQLITE_WRITELOCK ){
1911 return SQLITE_OK;
1912 }
drh99ee3602003-02-16 19:13:36 +00001913 rc = pager_playback(pPager, 1);
drhd9b02572001-04-15 00:37:09 +00001914 if( rc!=SQLITE_OK ){
1915 rc = SQLITE_CORRUPT;
1916 pPager->errMask |= PAGER_ERR_CORRUPT;
1917 }
1918 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00001919 return rc;
drh98808ba2001-10-18 12:34:46 +00001920}
drhd9b02572001-04-15 00:37:09 +00001921
1922/*
drh5e00f6c2001-09-13 13:46:56 +00001923** Return TRUE if the database file is opened read-only. Return FALSE
1924** if the database is (in theory) writable.
1925*/
1926int sqlitepager_isreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00001927 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00001928}
1929
1930/*
drhd9b02572001-04-15 00:37:09 +00001931** This routine is used for testing and analysis only.
1932*/
1933int *sqlitepager_stats(Pager *pPager){
1934 static int a[9];
1935 a[0] = pPager->nRef;
1936 a[1] = pPager->nPage;
1937 a[2] = pPager->mxPage;
1938 a[3] = pPager->dbSize;
1939 a[4] = pPager->state;
1940 a[5] = pPager->errMask;
1941 a[6] = pPager->nHit;
1942 a[7] = pPager->nMiss;
1943 a[8] = pPager->nOvfl;
1944 return a;
1945}
drhdd793422001-06-28 01:54:48 +00001946
drhfa86c412002-02-02 15:01:15 +00001947/*
1948** Set the checkpoint.
1949**
1950** This routine should be called with the transaction journal already
1951** open. A new checkpoint journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00001952** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00001953*/
1954int sqlitepager_ckpt_begin(Pager *pPager){
1955 int rc;
1956 char zTemp[SQLITE_TEMPNAME_SIZE];
drhda47d772002-12-02 04:25:19 +00001957 if( !pPager->journalOpen ){
1958 pPager->ckptAutoopen = 1;
1959 return SQLITE_OK;
1960 }
drhfa86c412002-02-02 15:01:15 +00001961 assert( pPager->journalOpen );
drh0f892532002-05-30 12:27:03 +00001962 assert( !pPager->ckptInUse );
drhfa86c412002-02-02 15:01:15 +00001963 pPager->aInCkpt = sqliteMalloc( pPager->dbSize/8 + 1 );
1964 if( pPager->aInCkpt==0 ){
1965 sqliteOsReadLock(&pPager->fd);
1966 return SQLITE_NOMEM;
1967 }
drh968af522003-02-11 14:55:40 +00001968#ifndef NDEBUG
drhfa86c412002-02-02 15:01:15 +00001969 rc = sqliteOsFileSize(&pPager->jfd, &pPager->ckptJSize);
1970 if( rc ) goto ckpt_begin_failed;
drh968af522003-02-11 14:55:40 +00001971 assert( pPager->ckptJSize ==
1972 pPager->nRec*JOURNAL_PG_SZ(journal_format)+JOURNAL_HDR_SZ(journal_format) );
1973#endif
1974 pPager->ckptJSize = pPager->nRec*JOURNAL_PG_SZ(journal_format)
1975 + JOURNAL_HDR_SZ(journal_format);
drh663fc632002-02-02 18:49:19 +00001976 pPager->ckptSize = pPager->dbSize;
drh0f892532002-05-30 12:27:03 +00001977 if( !pPager->ckptOpen ){
1978 rc = sqlitepager_opentemp(zTemp, &pPager->cpfd);
1979 if( rc ) goto ckpt_begin_failed;
1980 pPager->ckptOpen = 1;
drh9bd47a92003-01-07 14:46:08 +00001981 pPager->ckptNRec = 0;
drh0f892532002-05-30 12:27:03 +00001982 }
1983 pPager->ckptInUse = 1;
drhfa86c412002-02-02 15:01:15 +00001984 return SQLITE_OK;
1985
1986ckpt_begin_failed:
1987 if( pPager->aInCkpt ){
1988 sqliteFree(pPager->aInCkpt);
1989 pPager->aInCkpt = 0;
1990 }
1991 return rc;
1992}
1993
1994/*
1995** Commit a checkpoint.
1996*/
1997int sqlitepager_ckpt_commit(Pager *pPager){
drh0f892532002-05-30 12:27:03 +00001998 if( pPager->ckptInUse ){
drh03eb96a2002-11-10 23:32:56 +00001999 PgHdr *pPg, *pNext;
drh96ddd6d2002-09-05 19:10:33 +00002000 sqliteOsSeek(&pPager->cpfd, 0);
drh9bd47a92003-01-07 14:46:08 +00002001 /* sqliteOsTruncate(&pPager->cpfd, 0); */
2002 pPager->ckptNRec = 0;
drh0f892532002-05-30 12:27:03 +00002003 pPager->ckptInUse = 0;
drh663fc632002-02-02 18:49:19 +00002004 sqliteFree( pPager->aInCkpt );
2005 pPager->aInCkpt = 0;
drh03eb96a2002-11-10 23:32:56 +00002006 for(pPg=pPager->pCkpt; pPg; pPg=pNext){
2007 pNext = pPg->pNextCkpt;
2008 assert( pPg->inCkpt );
drh663fc632002-02-02 18:49:19 +00002009 pPg->inCkpt = 0;
drh03eb96a2002-11-10 23:32:56 +00002010 pPg->pPrevCkpt = pPg->pNextCkpt = 0;
drh663fc632002-02-02 18:49:19 +00002011 }
drh03eb96a2002-11-10 23:32:56 +00002012 pPager->pCkpt = 0;
drh663fc632002-02-02 18:49:19 +00002013 }
drhda47d772002-12-02 04:25:19 +00002014 pPager->ckptAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002015 return SQLITE_OK;
2016}
2017
2018/*
2019** Rollback a checkpoint.
2020*/
2021int sqlitepager_ckpt_rollback(Pager *pPager){
2022 int rc;
drh0f892532002-05-30 12:27:03 +00002023 if( pPager->ckptInUse ){
drh663fc632002-02-02 18:49:19 +00002024 rc = pager_ckpt_playback(pPager);
2025 sqlitepager_ckpt_commit(pPager);
2026 }else{
2027 rc = SQLITE_OK;
2028 }
drhda47d772002-12-02 04:25:19 +00002029 pPager->ckptAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002030 return rc;
2031}
2032
drh73509ee2003-04-06 20:44:45 +00002033/*
2034** Return the full pathname of the database file.
2035*/
2036const char *sqlitepager_filename(Pager *pPager){
2037 return pPager->zFilename;
2038}
2039
2040/*
2041** Rename the database file
2042*/
2043int sqlitepager_rename(Pager *pPager, const char *zNewName){
2044 char *zNew;
2045 char *zJournal;
2046 int nName;
2047 int rc;
2048
2049 nName = strlen(zNewName);
2050 zNew = sqliteMalloc( nName*2 + 30 );
2051 if( zNew==0 ){
2052 return SQLITE_NOMEM;
2053 }
2054 memcpy(zNew, zNewName, nName+1);
2055 zJournal = &zNew[nName+1];
2056 memcpy(zJournal, zNew, nName);
2057 strcpy(&zJournal[nName], "-journal");
2058 if( pPager->journalOpen ){
2059 rc = sqliteOsRename(pPager->zJournal, zJournal);
2060 if( rc ){
2061 sqliteFree(zNew);
2062 return rc;
2063 }
2064 }
2065 rc = sqliteOsRename(pPager->zFilename, zNew);
2066 if( rc ){
2067 sqliteFree(zNew);
2068 return rc;
2069 }
2070 if( pPager->zFilename!=(char*)&pPager[1] ){
2071 sqliteFree(pPager->zFilename);
2072 }
2073 pPager->zFilename = zNew;
2074 return SQLITE_OK;
2075}
2076
drh74587e52002-08-13 00:01:16 +00002077#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00002078/*
2079** Print a listing of all referenced pages and their ref count.
2080*/
2081void sqlitepager_refdump(Pager *pPager){
2082 PgHdr *pPg;
2083 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2084 if( pPg->nRef<=0 ) continue;
2085 printf("PAGE %3d addr=0x%08x nRef=%d\n",
2086 pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
2087 }
2088}
2089#endif