blob: 589a885f000d0540ebad0f60a14d8cc4faf4f7aa [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**
drh240c5792004-02-08 00:40:52 +000021** @(#) $Id: pager.c,v 1.93 2004/02/08 00:40:52 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 */
drha76c82e2003-07-27 18:59:42 +0000132 char *zDirectory; /* Directory hold database and journal files */
drh8cfbf082001-09-19 13:22:39 +0000133 OsFile fd, jfd; /* File descriptors for database and journal */
drhfa86c412002-02-02 15:01:15 +0000134 OsFile cpfd; /* File descriptor for the checkpoint journal */
drhed7c8552001-04-11 14:29:21 +0000135 int dbSize; /* Number of pages in the file */
drh69688d52001-04-14 16:38:23 +0000136 int origDbSize; /* dbSize before the current change */
drh28be87c2002-11-05 23:03:02 +0000137 int ckptSize; /* Size of database (in pages) at ckpt_begin() */
138 off_t ckptJSize; /* Size of journal at ckpt_begin() */
drh968af522003-02-11 14:55:40 +0000139 int nRec; /* Number of pages written to the journal */
140 u32 cksumInit; /* Quasi-random value added to every checksum */
drh9bd47a92003-01-07 14:46:08 +0000141 int ckptNRec; /* Number of records in the checkpoint journal */
drh7e3b0a02001-04-28 16:52:40 +0000142 int nExtra; /* Add this many bytes to each in-memory page */
drh72f82862001-05-24 21:06:34 +0000143 void (*xDestructor)(void*); /* Call this routine when freeing pages */
drhed7c8552001-04-11 14:29:21 +0000144 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000145 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000146 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000147 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
drh603240c2002-03-05 01:11:12 +0000148 u8 journalOpen; /* True if journal file descriptors is valid */
drhdb48ee02003-01-16 13:42:43 +0000149 u8 journalStarted; /* True if initial magic of journal is synced */
drhda47d772002-12-02 04:25:19 +0000150 u8 useJournal; /* Do not use a rollback journal on this file */
drh603240c2002-03-05 01:11:12 +0000151 u8 ckptOpen; /* True if the checkpoint journal is open */
drh0f892532002-05-30 12:27:03 +0000152 u8 ckptInUse; /* True we are in a checkpoint */
drhda47d772002-12-02 04:25:19 +0000153 u8 ckptAutoopen; /* Open ckpt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000154 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000155 u8 fullSync; /* Do extra syncs of the journal for robustness */
drh603240c2002-03-05 01:11:12 +0000156 u8 state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */
157 u8 errMask; /* One of several kinds of errors */
158 u8 tempFile; /* zFilename is a temporary file */
159 u8 readOnly; /* True for a read-only database */
160 u8 needSync; /* True if an fsync() is needed on the journal */
drha1680452002-04-18 01:56:57 +0000161 u8 dirtyFile; /* True if database file has changed in any way */
drh193a6b42002-07-07 16:52:46 +0000162 u8 alwaysRollback; /* Disable dont_rollback() for all pages */
drh603240c2002-03-05 01:11:12 +0000163 u8 *aInJournal; /* One bit for each page in the database file */
164 u8 *aInCkpt; /* One bit for each page in the database */
drhed7c8552001-04-11 14:29:21 +0000165 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000166 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000167 PgHdr *pAll; /* List of all pages */
drh03eb96a2002-11-10 23:32:56 +0000168 PgHdr *pCkpt; /* List of pages in the checkpoint journal */
drhed7c8552001-04-11 14:29:21 +0000169 PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */
drhd9b02572001-04-15 00:37:09 +0000170};
171
172/*
173** These are bits that can be set in Pager.errMask.
174*/
175#define PAGER_ERR_FULL 0x01 /* a write() failed */
176#define PAGER_ERR_MEM 0x02 /* malloc() failed */
177#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
178#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
drh81a20f22001-10-12 17:30:04 +0000179#define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */
drhd9b02572001-04-15 00:37:09 +0000180
181/*
182** The journal file contains page records in the following
183** format.
drh968af522003-02-11 14:55:40 +0000184**
185** Actually, this structure is the complete page record for pager
186** formats less than 3. Beginning with format 3, this record is surrounded
187** by two checksums.
drhd9b02572001-04-15 00:37:09 +0000188*/
189typedef struct PageRecord PageRecord;
190struct PageRecord {
191 Pgno pgno; /* The page number */
192 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
193};
194
195/*
drh5e00f6c2001-09-13 13:46:56 +0000196** Journal files begin with the following magic string. The data
197** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000198**
drh968af522003-02-11 14:55:40 +0000199** There are three journal formats (so far). The 1st journal format writes
200** 32-bit integers in the byte-order of the host machine. New
201** formats writes integers as big-endian. All new journals use the
drh94f33312002-08-12 12:29:56 +0000202** new format, but we have to be able to read an older journal in order
drh968af522003-02-11 14:55:40 +0000203** to rollback journals created by older versions of the library.
204**
205** The 3rd journal format (added for 2.8.0) adds additional sanity
206** checking information to the journal. If the power fails while the
207** journal is being written, semi-random garbage data might appear in
208** the journal file after power is restored. If an attempt is then made
209** to roll the journal back, the database could be corrupted. The additional
210** sanity checking data is an attempt to discover the garbage in the
211** journal and ignore it.
212**
213** The sanity checking information for the 3rd journal format consists
214** of a 32-bit checksum on each page of data. The checksum covers both
215** the page number and the SQLITE_PAGE_SIZE bytes of data for the page.
216** This cksum is initialized to a 32-bit random value that appears in the
217** journal file right after the header. The random initializer is important,
218** because garbage data that appears at the end of a journal is likely
219** data that was once in other files that have now been deleted. If the
220** garbage data came from an obsolete journal file, the checksums might
221** be correct. But by initializing the checksum to random value which
222** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000223*/
drh968af522003-02-11 14:55:40 +0000224static const unsigned char aJournalMagic1[] = {
drhd9b02572001-04-15 00:37:09 +0000225 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000226};
drh968af522003-02-11 14:55:40 +0000227static const unsigned char aJournalMagic2[] = {
drh94f33312002-08-12 12:29:56 +0000228 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5,
229};
drh968af522003-02-11 14:55:40 +0000230static const unsigned char aJournalMagic3[] = {
231 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd6,
232};
233#define JOURNAL_FORMAT_1 1
234#define JOURNAL_FORMAT_2 2
235#define JOURNAL_FORMAT_3 3
drh94f33312002-08-12 12:29:56 +0000236
237/*
drh968af522003-02-11 14:55:40 +0000238** The following integer determines what format to use when creating
239** new primary journal files. By default we always use format 3.
240** When testing, we can set this value to older journal formats in order to
241** make sure that newer versions of the library are able to rollback older
242** journal files.
243**
244** Note that checkpoint journals always use format 2 and omit the header.
drh94f33312002-08-12 12:29:56 +0000245*/
246#ifdef SQLITE_TEST
drh968af522003-02-11 14:55:40 +0000247int journal_format = 3;
drh74587e52002-08-13 00:01:16 +0000248#else
drh968af522003-02-11 14:55:40 +0000249# define journal_format 3
drh94f33312002-08-12 12:29:56 +0000250#endif
drhed7c8552001-04-11 14:29:21 +0000251
252/*
drh968af522003-02-11 14:55:40 +0000253** The size of the header and of each page in the journal varies according
254** to which journal format is being used. The following macros figure out
255** the sizes based on format numbers.
256*/
257#define JOURNAL_HDR_SZ(X) \
258 (sizeof(aJournalMagic1) + sizeof(Pgno) + ((X)>=3)*2*sizeof(u32))
259#define JOURNAL_PG_SZ(X) \
260 (SQLITE_PAGE_SIZE + sizeof(Pgno) + ((X)>=3)*sizeof(u32))
261
262/*
drhdd793422001-06-28 01:54:48 +0000263** Enable reference count tracking here:
264*/
drh74587e52002-08-13 00:01:16 +0000265#ifdef SQLITE_TEST
drh5e00f6c2001-09-13 13:46:56 +0000266 int pager_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000267 static void pager_refinfo(PgHdr *p){
268 static int cnt = 0;
269 if( !pager_refinfo_enable ) return;
270 printf(
271 "REFCNT: %4d addr=0x%08x nRef=%d\n",
272 p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
273 );
274 cnt++; /* Something to set a breakpoint on */
275 }
276# define REFINFO(X) pager_refinfo(X)
277#else
278# define REFINFO(X)
279#endif
280
281/*
drh94f33312002-08-12 12:29:56 +0000282** Read a 32-bit integer from the given file descriptor
283*/
drh968af522003-02-11 14:55:40 +0000284static int read32bits(int format, OsFile *fd, u32 *pRes){
drh94f33312002-08-12 12:29:56 +0000285 u32 res;
286 int rc;
287 rc = sqliteOsRead(fd, &res, sizeof(res));
drh968af522003-02-11 14:55:40 +0000288 if( rc==SQLITE_OK && format>JOURNAL_FORMAT_1 ){
drh94f33312002-08-12 12:29:56 +0000289 unsigned char ac[4];
290 memcpy(ac, &res, 4);
291 res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
292 }
293 *pRes = res;
294 return rc;
295}
296
297/*
298** Write a 32-bit integer into the given file descriptor. Writing
299** is always done using the new journal format.
300*/
301static int write32bits(OsFile *fd, u32 val){
302 unsigned char ac[4];
drh968af522003-02-11 14:55:40 +0000303 if( journal_format<=1 ){
drh94f33312002-08-12 12:29:56 +0000304 return sqliteOsWrite(fd, &val, 4);
305 }
drh94f33312002-08-12 12:29:56 +0000306 ac[0] = (val>>24) & 0xff;
307 ac[1] = (val>>16) & 0xff;
308 ac[2] = (val>>8) & 0xff;
309 ac[3] = val & 0xff;
310 return sqliteOsWrite(fd, ac, 4);
311}
312
drh2554f8b2003-01-22 01:26:44 +0000313/*
314** Write a 32-bit integer into a page header right before the
315** page data. This will overwrite the PgHdr.pDirty pointer.
316*/
drh968af522003-02-11 14:55:40 +0000317static void store32bits(u32 val, PgHdr *p, int offset){
drh2554f8b2003-01-22 01:26:44 +0000318 unsigned char *ac;
drhec1bd0b2003-08-26 11:41:27 +0000319 ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset];
drh968af522003-02-11 14:55:40 +0000320 if( journal_format<=1 ){
drh2554f8b2003-01-22 01:26:44 +0000321 memcpy(ac, &val, 4);
322 }else{
323 ac[0] = (val>>24) & 0xff;
324 ac[1] = (val>>16) & 0xff;
325 ac[2] = (val>>8) & 0xff;
326 ac[3] = val & 0xff;
327 }
328}
329
drh94f33312002-08-12 12:29:56 +0000330
331/*
drhd9b02572001-04-15 00:37:09 +0000332** Convert the bits in the pPager->errMask into an approprate
333** return code.
334*/
335static int pager_errcode(Pager *pPager){
336 int rc = SQLITE_OK;
337 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
drh81a20f22001-10-12 17:30:04 +0000338 if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR;
drhd9b02572001-04-15 00:37:09 +0000339 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
340 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
341 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
342 return rc;
drhed7c8552001-04-11 14:29:21 +0000343}
344
345/*
drh03eb96a2002-11-10 23:32:56 +0000346** Add or remove a page from the list of all pages that are in the
347** checkpoint journal.
348**
349** The Pager keeps a separate list of pages that are currently in
350** the checkpoint journal. This helps the sqlitepager_ckpt_commit()
351** routine run MUCH faster for the common case where there are many
352** pages in memory but only a few are in the checkpoint journal.
353*/
354static void page_add_to_ckpt_list(PgHdr *pPg){
355 Pager *pPager = pPg->pPager;
356 if( pPg->inCkpt ) return;
357 assert( pPg->pPrevCkpt==0 && pPg->pNextCkpt==0 );
358 pPg->pPrevCkpt = 0;
359 if( pPager->pCkpt ){
360 pPager->pCkpt->pPrevCkpt = pPg;
361 }
362 pPg->pNextCkpt = pPager->pCkpt;
363 pPager->pCkpt = pPg;
364 pPg->inCkpt = 1;
365}
366static void page_remove_from_ckpt_list(PgHdr *pPg){
367 if( !pPg->inCkpt ) return;
368 if( pPg->pPrevCkpt ){
369 assert( pPg->pPrevCkpt->pNextCkpt==pPg );
370 pPg->pPrevCkpt->pNextCkpt = pPg->pNextCkpt;
371 }else{
372 assert( pPg->pPager->pCkpt==pPg );
373 pPg->pPager->pCkpt = pPg->pNextCkpt;
374 }
375 if( pPg->pNextCkpt ){
376 assert( pPg->pNextCkpt->pPrevCkpt==pPg );
377 pPg->pNextCkpt->pPrevCkpt = pPg->pPrevCkpt;
378 }
379 pPg->pNextCkpt = 0;
380 pPg->pPrevCkpt = 0;
381 pPg->inCkpt = 0;
382}
383
384/*
drhed7c8552001-04-11 14:29:21 +0000385** Find a page in the hash table given its page number. Return
386** a pointer to the page or NULL if not found.
387*/
drhd9b02572001-04-15 00:37:09 +0000388static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh836faa42003-01-11 13:30:57 +0000389 PgHdr *p = pPager->aHash[pager_hash(pgno)];
drhed7c8552001-04-11 14:29:21 +0000390 while( p && p->pgno!=pgno ){
391 p = p->pNextHash;
392 }
393 return p;
394}
395
396/*
397** Unlock the database and clear the in-memory cache. This routine
398** sets the state of the pager back to what it was when it was first
399** opened. Any outstanding pages are invalidated and subsequent attempts
400** to access those pages will likely result in a coredump.
401*/
drhd9b02572001-04-15 00:37:09 +0000402static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000403 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000404 for(pPg=pPager->pAll; pPg; pPg=pNext){
405 pNext = pPg->pNextAll;
406 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000407 }
408 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000409 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +0000410 pPager->pLast = 0;
411 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000412 memset(pPager->aHash, 0, sizeof(pPager->aHash));
413 pPager->nPage = 0;
drhfa86c412002-02-02 15:01:15 +0000414 if( pPager->state>=SQLITE_WRITELOCK ){
drhd9b02572001-04-15 00:37:09 +0000415 sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000416 }
drha7fcb052001-12-14 15:09:55 +0000417 sqliteOsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000418 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000419 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000420 pPager->nRef = 0;
drh8cfbf082001-09-19 13:22:39 +0000421 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000422}
423
424/*
425** When this routine is called, the pager has the journal file open and
426** a write lock on the database. This routine releases the database
427** write lock and acquires a read lock in its place. The journal file
428** is deleted and closed.
drh50457892003-09-06 01:10:47 +0000429**
430** TODO: Consider keeping the journal file open for temporary databases.
431** This might give a performance improvement on windows where opening
432** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +0000433*/
drhd9b02572001-04-15 00:37:09 +0000434static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000435 int rc;
drhd9b02572001-04-15 00:37:09 +0000436 PgHdr *pPg;
drhfa86c412002-02-02 15:01:15 +0000437 if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK;
drh663fc632002-02-02 18:49:19 +0000438 sqlitepager_ckpt_commit(pPager);
drh0f892532002-05-30 12:27:03 +0000439 if( pPager->ckptOpen ){
440 sqliteOsClose(&pPager->cpfd);
441 pPager->ckptOpen = 0;
442 }
drhda47d772002-12-02 04:25:19 +0000443 if( pPager->journalOpen ){
444 sqliteOsClose(&pPager->jfd);
445 pPager->journalOpen = 0;
446 sqliteOsDelete(pPager->zJournal);
447 sqliteFree( pPager->aInJournal );
448 pPager->aInJournal = 0;
449 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
450 pPg->inJournal = 0;
451 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +0000452 pPg->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000453 }
454 }else{
455 assert( pPager->dirtyFile==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +0000456 }
drhda47d772002-12-02 04:25:19 +0000457 rc = sqliteOsReadLock(&pPager->fd);
drh8e298f92002-07-06 16:28:47 +0000458 if( rc==SQLITE_OK ){
459 pPager->state = SQLITE_READLOCK;
460 }else{
461 /* This can only happen if a process does a BEGIN, then forks and the
462 ** child process does the COMMIT. Because of the semantics of unix
463 ** file locking, the unlock will fail.
464 */
465 pPager->state = SQLITE_UNLOCK;
466 }
drhed7c8552001-04-11 14:29:21 +0000467 return rc;
468}
469
drhed7c8552001-04-11 14:29:21 +0000470/*
drh968af522003-02-11 14:55:40 +0000471** Compute and return a checksum for the page of data.
472*/
473static u32 pager_cksum(Pager *pPager, Pgno pgno, const char *aData){
474 u32 cksum = pPager->cksumInit + pgno;
drh968af522003-02-11 14:55:40 +0000475 return cksum;
476}
477
478/*
drhfa86c412002-02-02 15:01:15 +0000479** Read a single page from the journal file opened on file descriptor
480** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +0000481**
482** There are three different journal formats. The format parameter determines
483** which format is used by the journal that is played back.
drhfa86c412002-02-02 15:01:15 +0000484*/
drh968af522003-02-11 14:55:40 +0000485static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int format){
drhfa86c412002-02-02 15:01:15 +0000486 int rc;
487 PgHdr *pPg; /* An existing page in the cache */
488 PageRecord pgRec;
drh968af522003-02-11 14:55:40 +0000489 u32 cksum;
drhfa86c412002-02-02 15:01:15 +0000490
drh968af522003-02-11 14:55:40 +0000491 rc = read32bits(format, jfd, &pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000492 if( rc!=SQLITE_OK ) return rc;
drh94f33312002-08-12 12:29:56 +0000493 rc = sqliteOsRead(jfd, &pgRec.aData, sizeof(pgRec.aData));
drh99ee3602003-02-16 19:13:36 +0000494 if( rc!=SQLITE_OK ) return rc;
drhfa86c412002-02-02 15:01:15 +0000495
drh968af522003-02-11 14:55:40 +0000496 /* Sanity checking on the page. This is more important that I originally
497 ** thought. If a power failure occurs while the journal is being written,
498 ** it could cause invalid data to be written into the journal. We need to
499 ** detect this invalid data (with high probability) and ignore it.
500 */
501 if( pgRec.pgno==0 ){
502 return SQLITE_DONE;
503 }
drh7d02cb72003-06-04 16:24:39 +0000504 if( pgRec.pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +0000505 return SQLITE_OK;
506 }
507 if( format>=JOURNAL_FORMAT_3 ){
508 rc = read32bits(format, jfd, &cksum);
drh99ee3602003-02-16 19:13:36 +0000509 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +0000510 if( pager_cksum(pPager, pgRec.pgno, pgRec.aData)!=cksum ){
511 return SQLITE_DONE;
512 }
513 }
drhfa86c412002-02-02 15:01:15 +0000514
515 /* Playback the page. Update the in-memory copy of the page
516 ** at the same time, if there is one.
517 */
518 pPg = pager_lookup(pPager, pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000519 TRACE2("PLAYBACK %d\n", pgRec.pgno);
520 sqliteOsSeek(&pPager->fd, (pgRec.pgno-1)*(off_t)SQLITE_PAGE_SIZE);
521 rc = sqliteOsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
drhfa86c412002-02-02 15:01:15 +0000522 if( pPg ){
drhacf4ac92003-12-17 23:57:34 +0000523 /* No page should ever be rolled back that is in use, except for page
524 ** 1 which is held in use in order to keep the lock on the database
525 ** active.
526 */
527 assert( pPg->nRef==0 || pPg->pgno==1 );
528 memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
529 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
drhdb48ee02003-01-16 13:42:43 +0000530 pPg->dirty = 0;
531 pPg->needSync = 0;
drhfa86c412002-02-02 15:01:15 +0000532 }
533 return rc;
534}
535
536/*
drhed7c8552001-04-11 14:29:21 +0000537** Playback the journal and thus restore the database file to
538** the state it was in before we started making changes.
539**
drhd9b02572001-04-15 00:37:09 +0000540** The journal file format is as follows: There is an initial
541** file-type string for sanity checking. Then there is a single
542** Pgno number which is the number of pages in the database before
543** changes were made. The database is truncated to this size.
drh306dc212001-05-21 13:45:10 +0000544** Next come zero or more page records where each page record
545** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See
546** the PageRecord structure for details.
drhed7c8552001-04-11 14:29:21 +0000547**
drhd9b02572001-04-15 00:37:09 +0000548** If the file opened as the journal file is not a well-formed
549** journal file (as determined by looking at the magic number
550** at the beginning) then this routine returns SQLITE_PROTOCOL.
551** If any other errors occur during playback, the database will
552** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in
553** pPager->errMask and SQLITE_CORRUPT is returned. If it all
554** works, then this routine returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000555*/
drh99ee3602003-02-16 19:13:36 +0000556static int pager_playback(Pager *pPager, int useJournalSize){
drh968af522003-02-11 14:55:40 +0000557 off_t szJ; /* Size of the journal file in bytes */
558 int nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +0000559 int i; /* Loop counter */
560 Pgno mxPg = 0; /* Size of the original file in pages */
drh968af522003-02-11 14:55:40 +0000561 int format; /* Format of the journal file. */
562 unsigned char aMagic[sizeof(aJournalMagic1)];
drhed7c8552001-04-11 14:29:21 +0000563 int rc;
564
drhc3a64ba2001-11-22 00:01:27 +0000565 /* Figure out how many records are in the journal. Abort early if
566 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +0000567 */
drh8cfbf082001-09-19 13:22:39 +0000568 assert( pPager->journalOpen );
drha7fcb052001-12-14 15:09:55 +0000569 sqliteOsSeek(&pPager->jfd, 0);
drh968af522003-02-11 14:55:40 +0000570 rc = sqliteOsFileSize(&pPager->jfd, &szJ);
drhc3a64ba2001-11-22 00:01:27 +0000571 if( rc!=SQLITE_OK ){
572 goto end_playback;
573 }
drh240c5792004-02-08 00:40:52 +0000574
575 /* If the journal file is too small to contain a complete header,
576 ** then ignore the journal completely.
577 */
drh968af522003-02-11 14:55:40 +0000578 if( szJ < sizeof(aMagic)+sizeof(Pgno) ){
drhc3a64ba2001-11-22 00:01:27 +0000579 goto end_playback;
580 }
581
582 /* Read the beginning of the journal and truncate the
583 ** database file back to its original size.
584 */
drha7fcb052001-12-14 15:09:55 +0000585 rc = sqliteOsRead(&pPager->jfd, aMagic, sizeof(aMagic));
drh94f33312002-08-12 12:29:56 +0000586 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000587 rc = SQLITE_PROTOCOL;
588 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000589 }
drh968af522003-02-11 14:55:40 +0000590 if( memcmp(aMagic, aJournalMagic3, sizeof(aMagic))==0 ){
591 format = JOURNAL_FORMAT_3;
592 }else if( memcmp(aMagic, aJournalMagic2, sizeof(aMagic))==0 ){
593 format = JOURNAL_FORMAT_2;
594 }else if( memcmp(aMagic, aJournalMagic1, sizeof(aMagic))==0 ){
595 format = JOURNAL_FORMAT_1;
drh94f33312002-08-12 12:29:56 +0000596 }else{
597 rc = SQLITE_PROTOCOL;
598 goto end_playback;
599 }
drh968af522003-02-11 14:55:40 +0000600 if( format>=JOURNAL_FORMAT_3 ){
drh240c5792004-02-08 00:40:52 +0000601 if( szJ < sizeof(aMagic) + 3*sizeof(u32) ){
602 /* Ignore the journal if it is too small to contain a complete
603 ** header. We already did this test once above, but at the prior
604 ** test, we did not know the journal format and so we had to assume
605 ** the smallest possible header. Now we know the header is bigger
606 ** than that so we test again.
607 */
608 goto end_playback;
609 }
drh133cdf62004-01-07 02:52:07 +0000610 rc = read32bits(format, &pPager->jfd, (u32*)&nRec);
drh968af522003-02-11 14:55:40 +0000611 if( rc ) goto end_playback;
612 rc = read32bits(format, &pPager->jfd, &pPager->cksumInit);
613 if( rc ) goto end_playback;
drh99ee3602003-02-16 19:13:36 +0000614 if( nRec==0xffffffff || useJournalSize ){
drh968af522003-02-11 14:55:40 +0000615 nRec = (szJ - JOURNAL_HDR_SZ(3))/JOURNAL_PG_SZ(3);
616 }
617 }else{
drhd8d66e82003-02-12 02:10:15 +0000618 nRec = (szJ - JOURNAL_HDR_SZ(2))/JOURNAL_PG_SZ(2);
619 assert( nRec*JOURNAL_PG_SZ(2)+JOURNAL_HDR_SZ(2)==szJ );
drh968af522003-02-11 14:55:40 +0000620 }
621 rc = read32bits(format, &pPager->jfd, &mxPg);
drhd9b02572001-04-15 00:37:09 +0000622 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000623 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000624 }
drhd8d66e82003-02-12 02:10:15 +0000625 assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg );
drh28be87c2002-11-05 23:03:02 +0000626 rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg);
drh81a20f22001-10-12 17:30:04 +0000627 if( rc!=SQLITE_OK ){
628 goto end_playback;
629 }
drhd9b02572001-04-15 00:37:09 +0000630 pPager->dbSize = mxPg;
631
drhfa86c412002-02-02 15:01:15 +0000632 /* Copy original pages out of the journal and back into the database file.
drhed7c8552001-04-11 14:29:21 +0000633 */
drh968af522003-02-11 14:55:40 +0000634 for(i=0; i<nRec; i++){
635 rc = pager_playback_one_page(pPager, &pPager->jfd, format);
636 if( rc!=SQLITE_OK ){
637 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +0000638 rc = SQLITE_OK;
639 }
640 break;
641 }
drhed7c8552001-04-11 14:29:21 +0000642 }
drh81a20f22001-10-12 17:30:04 +0000643
drh4a0681e2003-02-13 01:58:20 +0000644 /* Pages that have been written to the journal but never synced
645 ** where not restored by the loop above. We have to restore those
drh240c5792004-02-08 00:40:52 +0000646 ** pages by reading them back from the original database.
drhdb48ee02003-01-16 13:42:43 +0000647 */
648 if( rc==SQLITE_OK ){
649 PgHdr *pPg;
650 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3a840692003-01-29 22:58:26 +0000651 char zBuf[SQLITE_PAGE_SIZE];
drh4a0681e2003-02-13 01:58:20 +0000652 if( !pPg->dirty ) continue;
drhdb48ee02003-01-16 13:42:43 +0000653 if( (int)pPg->pgno <= pPager->origDbSize ){
654 sqliteOsSeek(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)(pPg->pgno-1));
drh3a840692003-01-29 22:58:26 +0000655 rc = sqliteOsRead(&pPager->fd, zBuf, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000656 if( rc ) break;
657 }else{
drh3a840692003-01-29 22:58:26 +0000658 memset(zBuf, 0, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000659 }
drh3a840692003-01-29 22:58:26 +0000660 if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE) ){
661 memcpy(PGHDR_TO_DATA(pPg), zBuf, SQLITE_PAGE_SIZE);
662 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
663 }
drhdb48ee02003-01-16 13:42:43 +0000664 pPg->needSync = 0;
665 pPg->dirty = 0;
666 }
667 }
drh4a0681e2003-02-13 01:58:20 +0000668
669end_playback:
drhd9b02572001-04-15 00:37:09 +0000670 if( rc!=SQLITE_OK ){
671 pager_unwritelock(pPager);
672 pPager->errMask |= PAGER_ERR_CORRUPT;
673 rc = SQLITE_CORRUPT;
674 }else{
675 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000676 }
drhd9b02572001-04-15 00:37:09 +0000677 return rc;
drhed7c8552001-04-11 14:29:21 +0000678}
679
680/*
drhfa86c412002-02-02 15:01:15 +0000681** Playback the checkpoint journal.
682**
683** This is similar to playing back the transaction journal but with
684** a few extra twists.
685**
drh663fc632002-02-02 18:49:19 +0000686** (1) The number of pages in the database file at the start of
687** the checkpoint is stored in pPager->ckptSize, not in the
688** journal file itself.
drhfa86c412002-02-02 15:01:15 +0000689**
690** (2) In addition to playing back the checkpoint journal, also
691** playback all pages of the transaction journal beginning
692** at offset pPager->ckptJSize.
693*/
694static int pager_ckpt_playback(Pager *pPager){
drh968af522003-02-11 14:55:40 +0000695 off_t szJ; /* Size of the full journal */
696 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +0000697 int i; /* Loop counter */
698 int rc;
699
700 /* Truncate the database back to its original size.
701 */
drh28be87c2002-11-05 23:03:02 +0000702 rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)pPager->ckptSize);
drhfa86c412002-02-02 15:01:15 +0000703 pPager->dbSize = pPager->ckptSize;
704
705 /* Figure out how many records are in the checkpoint journal.
706 */
drh0f892532002-05-30 12:27:03 +0000707 assert( pPager->ckptInUse && pPager->journalOpen );
drhfa86c412002-02-02 15:01:15 +0000708 sqliteOsSeek(&pPager->cpfd, 0);
drh9bd47a92003-01-07 14:46:08 +0000709 nRec = pPager->ckptNRec;
drhfa86c412002-02-02 15:01:15 +0000710
711 /* Copy original pages out of the checkpoint journal and back into the
drh968af522003-02-11 14:55:40 +0000712 ** database file. Note that the checkpoint journal always uses format
713 ** 2 instead of format 3 since it does not need to be concerned with
714 ** power failures corrupting the journal and can thus omit the checksums.
drhfa86c412002-02-02 15:01:15 +0000715 */
716 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000717 rc = pager_playback_one_page(pPager, &pPager->cpfd, 2);
718 assert( rc!=SQLITE_DONE );
drhfa86c412002-02-02 15:01:15 +0000719 if( rc!=SQLITE_OK ) goto end_ckpt_playback;
720 }
721
722 /* Figure out how many pages need to be copied out of the transaction
723 ** journal.
724 */
725 rc = sqliteOsSeek(&pPager->jfd, pPager->ckptJSize);
726 if( rc!=SQLITE_OK ){
727 goto end_ckpt_playback;
728 }
drh968af522003-02-11 14:55:40 +0000729 rc = sqliteOsFileSize(&pPager->jfd, &szJ);
drhfa86c412002-02-02 15:01:15 +0000730 if( rc!=SQLITE_OK ){
731 goto end_ckpt_playback;
732 }
drh968af522003-02-11 14:55:40 +0000733 nRec = (szJ - pPager->ckptJSize)/JOURNAL_PG_SZ(journal_format);
drhfa86c412002-02-02 15:01:15 +0000734 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000735 rc = pager_playback_one_page(pPager, &pPager->jfd, journal_format);
736 if( rc!=SQLITE_OK ){
737 assert( rc!=SQLITE_DONE );
738 goto end_ckpt_playback;
739 }
drhfa86c412002-02-02 15:01:15 +0000740 }
741
drhfa86c412002-02-02 15:01:15 +0000742end_ckpt_playback:
drhfa86c412002-02-02 15:01:15 +0000743 if( rc!=SQLITE_OK ){
drhfa86c412002-02-02 15:01:15 +0000744 pPager->errMask |= PAGER_ERR_CORRUPT;
745 rc = SQLITE_CORRUPT;
drhfa86c412002-02-02 15:01:15 +0000746 }
747 return rc;
748}
749
750/*
drhf57b14a2001-09-14 18:54:08 +0000751** Change the maximum number of in-memory pages that are allowed.
drhcd61c282002-03-06 22:01:34 +0000752**
753** The maximum number is the absolute value of the mxPage parameter.
754** If mxPage is negative, the noSync flag is also set. noSync bypasses
755** calls to sqliteOsSync(). The pager runs much faster with noSync on,
756** but if the operating system crashes or there is an abrupt power
757** failure, the database file might be left in an inconsistent and
758** unrepairable state.
drhf57b14a2001-09-14 18:54:08 +0000759*/
760void sqlitepager_set_cachesize(Pager *pPager, int mxPage){
drh603240c2002-03-05 01:11:12 +0000761 if( mxPage>=0 ){
drha1680452002-04-18 01:56:57 +0000762 pPager->noSync = pPager->tempFile;
drh603240c2002-03-05 01:11:12 +0000763 }else{
764 pPager->noSync = 1;
765 mxPage = -mxPage;
766 }
drhf57b14a2001-09-14 18:54:08 +0000767 if( mxPage>10 ){
768 pPager->mxPage = mxPage;
769 }
770}
771
772/*
drh973b6e32003-02-12 14:09:42 +0000773** Adjust the robustness of the database to damage due to OS crashes
774** or power failures by changing the number of syncs()s when writing
775** the rollback journal. There are three levels:
776**
777** OFF sqliteOsSync() is never called. This is the default
778** for temporary and transient files.
779**
780** NORMAL The journal is synced once before writes begin on the
781** database. This is normally adequate protection, but
782** it is theoretically possible, though very unlikely,
783** that an inopertune power failure could leave the journal
784** in a state which would cause damage to the database
785** when it is rolled back.
786**
787** FULL The journal is synced twice before writes begin on the
788** database (with some additional information being written
789** in between the two syncs. If we assume that writing a
790** single disk sector is atomic, then this mode provides
791** assurance that the journal will not be corrupted to the
792** point of causing damage to the database during rollback.
793**
794** Numeric values associated with these states are OFF==1, NORMAL=2,
795** and FULL=3.
796*/
797void sqlitepager_set_safety_level(Pager *pPager, int level){
798 pPager->noSync = level==1 || pPager->tempFile;
799 pPager->fullSync = level==3 && !pPager->tempFile;
800}
801
802/*
drhfa86c412002-02-02 15:01:15 +0000803** Open a temporary file. Write the name of the file into zName
804** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write
805** the file descriptor into *fd. Return SQLITE_OK on success or some
806** other error code if we fail.
807**
808** The OS will automatically delete the temporary file when it is
809** closed.
810*/
811static int sqlitepager_opentemp(char *zFile, OsFile *fd){
812 int cnt = 8;
813 int rc;
814 do{
815 cnt--;
816 sqliteOsTempFileName(zFile);
817 rc = sqliteOsOpenExclusive(zFile, fd, 1);
818 }while( cnt>0 && rc!=SQLITE_OK );
819 return rc;
820}
821
822/*
drhed7c8552001-04-11 14:29:21 +0000823** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +0000824** The file to be cached need not exist. The file is not locked until
drhd9b02572001-04-15 00:37:09 +0000825** the first call to sqlitepager_get() and is only held open until the
826** last page is released using sqlitepager_unref().
drh382c0242001-10-06 16:33:02 +0000827**
drh6446c4d2001-12-15 14:22:18 +0000828** If zFilename is NULL then a randomly-named temporary file is created
829** and used as the file to be cached. The file will be deleted
830** automatically when it is closed.
drhed7c8552001-04-11 14:29:21 +0000831*/
drh7e3b0a02001-04-28 16:52:40 +0000832int sqlitepager_open(
833 Pager **ppPager, /* Return the Pager structure here */
834 const char *zFilename, /* Name of the database file to open */
835 int mxPage, /* Max number of in-memory cache pages */
drhda47d772002-12-02 04:25:19 +0000836 int nExtra, /* Extra bytes append to each in-memory page */
837 int useJournal /* TRUE to use a rollback journal on this file */
drh7e3b0a02001-04-28 16:52:40 +0000838){
drhed7c8552001-04-11 14:29:21 +0000839 Pager *pPager;
drh3e7a6092002-12-07 21:45:14 +0000840 char *zFullPathname;
drhed7c8552001-04-11 14:29:21 +0000841 int nameLen;
drh8cfbf082001-09-19 13:22:39 +0000842 OsFile fd;
drha76c82e2003-07-27 18:59:42 +0000843 int rc, i;
drh5e00f6c2001-09-13 13:46:56 +0000844 int tempFile;
845 int readOnly = 0;
drh8cfbf082001-09-19 13:22:39 +0000846 char zTemp[SQLITE_TEMPNAME_SIZE];
drhed7c8552001-04-11 14:29:21 +0000847
drhd9b02572001-04-15 00:37:09 +0000848 *ppPager = 0;
849 if( sqlite_malloc_failed ){
850 return SQLITE_NOMEM;
851 }
drh901afd42003-08-26 11:25:58 +0000852 if( zFilename && zFilename[0] ){
drh3e7a6092002-12-07 21:45:14 +0000853 zFullPathname = sqliteOsFullPathname(zFilename);
854 rc = sqliteOsOpenReadWrite(zFullPathname, &fd, &readOnly);
drh5e00f6c2001-09-13 13:46:56 +0000855 tempFile = 0;
856 }else{
drhfa86c412002-02-02 15:01:15 +0000857 rc = sqlitepager_opentemp(zTemp, &fd);
drh5e00f6c2001-09-13 13:46:56 +0000858 zFilename = zTemp;
drh3e7a6092002-12-07 21:45:14 +0000859 zFullPathname = sqliteOsFullPathname(zFilename);
drh5e00f6c2001-09-13 13:46:56 +0000860 tempFile = 1;
861 }
drh3e7a6092002-12-07 21:45:14 +0000862 if( sqlite_malloc_failed ){
863 return SQLITE_NOMEM;
864 }
drh8cfbf082001-09-19 13:22:39 +0000865 if( rc!=SQLITE_OK ){
drh3e7a6092002-12-07 21:45:14 +0000866 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000867 return SQLITE_CANTOPEN;
868 }
drh3e7a6092002-12-07 21:45:14 +0000869 nameLen = strlen(zFullPathname);
drha76c82e2003-07-27 18:59:42 +0000870 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*3 + 30 );
drhd9b02572001-04-15 00:37:09 +0000871 if( pPager==0 ){
drha7fcb052001-12-14 15:09:55 +0000872 sqliteOsClose(&fd);
drh3e7a6092002-12-07 21:45:14 +0000873 sqliteFree(zFullPathname);
drhd9b02572001-04-15 00:37:09 +0000874 return SQLITE_NOMEM;
875 }
drhdb48ee02003-01-16 13:42:43 +0000876 SET_PAGER(pPager);
drhed7c8552001-04-11 14:29:21 +0000877 pPager->zFilename = (char*)&pPager[1];
drha76c82e2003-07-27 18:59:42 +0000878 pPager->zDirectory = &pPager->zFilename[nameLen+1];
879 pPager->zJournal = &pPager->zDirectory[nameLen+1];
drh3e7a6092002-12-07 21:45:14 +0000880 strcpy(pPager->zFilename, zFullPathname);
drha76c82e2003-07-27 18:59:42 +0000881 strcpy(pPager->zDirectory, zFullPathname);
882 for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){}
883 if( i>0 ) pPager->zDirectory[i-1] = 0;
drh3e7a6092002-12-07 21:45:14 +0000884 strcpy(pPager->zJournal, zFullPathname);
885 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000886 strcpy(&pPager->zJournal[nameLen], "-journal");
887 pPager->fd = fd;
drh8cfbf082001-09-19 13:22:39 +0000888 pPager->journalOpen = 0;
drhda47d772002-12-02 04:25:19 +0000889 pPager->useJournal = useJournal;
drhfa86c412002-02-02 15:01:15 +0000890 pPager->ckptOpen = 0;
drh0f892532002-05-30 12:27:03 +0000891 pPager->ckptInUse = 0;
drhed7c8552001-04-11 14:29:21 +0000892 pPager->nRef = 0;
893 pPager->dbSize = -1;
drhfa86c412002-02-02 15:01:15 +0000894 pPager->ckptSize = 0;
895 pPager->ckptJSize = 0;
drhed7c8552001-04-11 14:29:21 +0000896 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +0000897 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +0000898 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000899 pPager->errMask = 0;
drh5e00f6c2001-09-13 13:46:56 +0000900 pPager->tempFile = tempFile;
901 pPager->readOnly = readOnly;
drhf57b14a2001-09-14 18:54:08 +0000902 pPager->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000903 pPager->noSync = pPager->tempFile || !useJournal;
drhed7c8552001-04-11 14:29:21 +0000904 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000905 pPager->pFirstSynced = 0;
drhed7c8552001-04-11 14:29:21 +0000906 pPager->pLast = 0;
drh7c717f72001-06-24 20:39:41 +0000907 pPager->nExtra = nExtra;
drhed7c8552001-04-11 14:29:21 +0000908 memset(pPager->aHash, 0, sizeof(pPager->aHash));
909 *ppPager = pPager;
910 return SQLITE_OK;
911}
912
913/*
drh72f82862001-05-24 21:06:34 +0000914** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +0000915** when the reference count on each page reaches zero. The destructor can
916** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +0000917**
918** The destructor is not called as a result sqlitepager_close().
919** Destructors are only called by sqlitepager_unref().
920*/
921void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){
922 pPager->xDestructor = xDesc;
923}
924
925/*
drh5e00f6c2001-09-13 13:46:56 +0000926** Return the total number of pages in the disk file associated with
927** pPager.
drhed7c8552001-04-11 14:29:21 +0000928*/
drhd9b02572001-04-15 00:37:09 +0000929int sqlitepager_pagecount(Pager *pPager){
drh28be87c2002-11-05 23:03:02 +0000930 off_t n;
drhd9b02572001-04-15 00:37:09 +0000931 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +0000932 if( pPager->dbSize>=0 ){
933 return pPager->dbSize;
934 }
drha7fcb052001-12-14 15:09:55 +0000935 if( sqliteOsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000936 pPager->errMask |= PAGER_ERR_DISK;
drh8cfbf082001-09-19 13:22:39 +0000937 return 0;
drhed7c8552001-04-11 14:29:21 +0000938 }
drh8cfbf082001-09-19 13:22:39 +0000939 n /= SQLITE_PAGE_SIZE;
drhd9b02572001-04-15 00:37:09 +0000940 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +0000941 pPager->dbSize = n;
942 }
943 return n;
944}
945
946/*
drhf7c57532003-04-25 13:22:51 +0000947** Forward declaration
948*/
949static int syncAllPages(Pager*);
950
951/*
952** Truncate the file to the number of pages specified.
953*/
954int sqlitepager_truncate(Pager *pPager, Pgno nPage){
955 int rc;
drh2e6d11b2003-04-25 15:37:57 +0000956 if( pPager->dbSize<0 ){
957 sqlitepager_pagecount(pPager);
958 }
959 if( pPager->errMask!=0 ){
960 rc = pager_errcode(pPager);
961 return rc;
962 }
drh7d02cb72003-06-04 16:24:39 +0000963 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +0000964 return SQLITE_OK;
965 }
966 syncAllPages(pPager);
967 rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)nPage);
968 if( rc==SQLITE_OK ){
969 pPager->dbSize = nPage;
970 }
971 return rc;
972}
973
974/*
drhed7c8552001-04-11 14:29:21 +0000975** Shutdown the page cache. Free all memory and close all files.
976**
977** If a transaction was in progress when this routine is called, that
978** transaction is rolled back. All outstanding pages are invalidated
979** and their memory is freed. Any attempt to use a page associated
980** with this page cache after this function returns will likely
981** result in a coredump.
982*/
drhd9b02572001-04-15 00:37:09 +0000983int sqlitepager_close(Pager *pPager){
984 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +0000985 switch( pPager->state ){
986 case SQLITE_WRITELOCK: {
drhd9b02572001-04-15 00:37:09 +0000987 sqlitepager_rollback(pPager);
drha7fcb052001-12-14 15:09:55 +0000988 sqliteOsUnlock(&pPager->fd);
drh8cfbf082001-09-19 13:22:39 +0000989 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000990 break;
991 }
992 case SQLITE_READLOCK: {
drha7fcb052001-12-14 15:09:55 +0000993 sqliteOsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000994 break;
995 }
996 default: {
997 /* Do nothing */
998 break;
999 }
1000 }
drhd9b02572001-04-15 00:37:09 +00001001 for(pPg=pPager->pAll; pPg; pPg=pNext){
1002 pNext = pPg->pNextAll;
1003 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +00001004 }
drha7fcb052001-12-14 15:09:55 +00001005 sqliteOsClose(&pPager->fd);
drh8cfbf082001-09-19 13:22:39 +00001006 assert( pPager->journalOpen==0 );
drh0f892532002-05-30 12:27:03 +00001007 /* Temp files are automatically deleted by the OS
1008 ** if( pPager->tempFile ){
1009 ** sqliteOsDelete(pPager->zFilename);
1010 ** }
1011 */
drhdb48ee02003-01-16 13:42:43 +00001012 CLR_PAGER(pPager);
drh73509ee2003-04-06 20:44:45 +00001013 if( pPager->zFilename!=(char*)&pPager[1] ){
drha76c82e2003-07-27 18:59:42 +00001014 assert( 0 ); /* Cannot happen */
drh73509ee2003-04-06 20:44:45 +00001015 sqliteFree(pPager->zFilename);
1016 sqliteFree(pPager->zJournal);
drha76c82e2003-07-27 18:59:42 +00001017 sqliteFree(pPager->zDirectory);
drh73509ee2003-04-06 20:44:45 +00001018 }
drhed7c8552001-04-11 14:29:21 +00001019 sqliteFree(pPager);
1020 return SQLITE_OK;
1021}
1022
1023/*
drh5e00f6c2001-09-13 13:46:56 +00001024** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00001025*/
drhd9b02572001-04-15 00:37:09 +00001026Pgno sqlitepager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +00001027 PgHdr *p = DATA_TO_PGHDR(pData);
1028 return p->pgno;
1029}
1030
1031/*
drh7e3b0a02001-04-28 16:52:40 +00001032** Increment the reference count for a page. If the page is
1033** currently on the freelist (the reference count is zero) then
1034** remove it from the freelist.
1035*/
drh836faa42003-01-11 13:30:57 +00001036#define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
1037static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00001038 if( pPg->nRef==0 ){
1039 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +00001040 if( pPg==pPg->pPager->pFirstSynced ){
1041 PgHdr *p = pPg->pNextFree;
1042 while( p && p->needSync ){ p = p->pNextFree; }
1043 pPg->pPager->pFirstSynced = p;
1044 }
drh7e3b0a02001-04-28 16:52:40 +00001045 if( pPg->pPrevFree ){
1046 pPg->pPrevFree->pNextFree = pPg->pNextFree;
1047 }else{
1048 pPg->pPager->pFirst = pPg->pNextFree;
1049 }
1050 if( pPg->pNextFree ){
1051 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1052 }else{
1053 pPg->pPager->pLast = pPg->pPrevFree;
1054 }
1055 pPg->pPager->nRef++;
1056 }
1057 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00001058 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00001059}
1060
1061/*
1062** Increment the reference count for a page. The input pointer is
1063** a reference to the page data.
1064*/
1065int sqlitepager_ref(void *pData){
1066 PgHdr *pPg = DATA_TO_PGHDR(pData);
1067 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +00001068 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001069}
1070
1071/*
drhb19a2bc2001-09-16 00:13:26 +00001072** Sync the journal and then write all free dirty pages to the database
1073** file.
1074**
1075** Writing all free dirty pages to the database after the sync is a
1076** non-obvious optimization. fsync() is an expensive operation so we
drhaaab5722002-02-19 13:39:21 +00001077** want to minimize the number ot times it is called. After an fsync() call,
drh6446c4d2001-12-15 14:22:18 +00001078** we are free to write dirty pages back to the database. It is best
1079** to go ahead and write as many dirty pages as possible to minimize
1080** the risk of having to do another fsync() later on. Writing dirty
1081** free pages in this way was observed to make database operations go
1082** up to 10 times faster.
drhfa86c412002-02-02 15:01:15 +00001083**
1084** If we are writing to temporary database, there is no need to preserve
1085** the integrity of the journal file, so we can save time and skip the
1086** fsync().
drh50e5dad2001-09-15 00:57:28 +00001087*/
1088static int syncAllPages(Pager *pPager){
1089 PgHdr *pPg;
1090 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00001091
1092 /* Sync the journal before modifying the main database
1093 ** (assuming there is a journal and it needs to be synced.)
1094 */
drh50e5dad2001-09-15 00:57:28 +00001095 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00001096 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00001097 assert( pPager->journalOpen );
1098 assert( !pPager->noSync );
drh968af522003-02-11 14:55:40 +00001099#ifndef NDEBUG
1100 {
drh4a0681e2003-02-13 01:58:20 +00001101 off_t hdrSz, pgSz, jSz;
drh968af522003-02-11 14:55:40 +00001102 hdrSz = JOURNAL_HDR_SZ(journal_format);
1103 pgSz = JOURNAL_PG_SZ(journal_format);
drh4a0681e2003-02-13 01:58:20 +00001104 rc = sqliteOsFileSize(&pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00001105 if( rc!=0 ) return rc;
drh4a0681e2003-02-13 01:58:20 +00001106 assert( pPager->nRec*pgSz+hdrSz==jSz );
drh968af522003-02-11 14:55:40 +00001107 }
1108#endif
drhd8d66e82003-02-12 02:10:15 +00001109 if( journal_format>=3 ){
1110 off_t szJ;
1111 if( pPager->fullSync ){
1112 TRACE1("SYNC\n");
1113 rc = sqliteOsSync(&pPager->jfd);
1114 if( rc!=0 ) return rc;
1115 }
1116 sqliteOsSeek(&pPager->jfd, sizeof(aJournalMagic1));
drh99ee3602003-02-16 19:13:36 +00001117 rc = write32bits(&pPager->jfd, pPager->nRec);
1118 if( rc ) return rc;
drhd8d66e82003-02-12 02:10:15 +00001119 szJ = JOURNAL_HDR_SZ(journal_format) +
1120 pPager->nRec*JOURNAL_PG_SZ(journal_format);
1121 sqliteOsSeek(&pPager->jfd, szJ);
drh968af522003-02-11 14:55:40 +00001122 }
drhdb48ee02003-01-16 13:42:43 +00001123 TRACE1("SYNC\n");
drhfa86c412002-02-02 15:01:15 +00001124 rc = sqliteOsSync(&pPager->jfd);
1125 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00001126 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00001127 }
drh50e5dad2001-09-15 00:57:28 +00001128 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00001129
1130 /* Erase the needSync flag from every page.
1131 */
1132 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1133 pPg->needSync = 0;
1134 }
1135 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00001136 }
drh03eb96a2002-11-10 23:32:56 +00001137
drh341eae82003-01-21 02:39:36 +00001138#ifndef NDEBUG
1139 /* If the Pager.needSync flag is clear then the PgHdr.needSync
1140 ** flag must also be clear for all pages. Verify that this
1141 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00001142 */
drh341eae82003-01-21 02:39:36 +00001143 else{
1144 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1145 assert( pPg->needSync==0 );
1146 }
1147 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00001148 }
drh341eae82003-01-21 02:39:36 +00001149#endif
drhdb48ee02003-01-16 13:42:43 +00001150
drh81a20f22001-10-12 17:30:04 +00001151 return rc;
drh50e5dad2001-09-15 00:57:28 +00001152}
1153
1154/*
drh2554f8b2003-01-22 01:26:44 +00001155** Given a list of pages (connected by the PgHdr.pDirty pointer) write
1156** every one of those pages out to the database file and mark them all
1157** as clean.
1158*/
1159static int pager_write_pagelist(PgHdr *pList){
1160 Pager *pPager;
1161 int rc;
1162
1163 if( pList==0 ) return SQLITE_OK;
1164 pPager = pList->pPager;
1165 while( pList ){
1166 assert( pList->dirty );
1167 sqliteOsSeek(&pPager->fd, (pList->pgno-1)*(off_t)SQLITE_PAGE_SIZE);
1168 rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pList), SQLITE_PAGE_SIZE);
1169 if( rc ) return rc;
1170 pList->dirty = 0;
1171 pList = pList->pDirty;
1172 }
1173 return SQLITE_OK;
1174}
1175
1176/*
1177** Collect every dirty page into a dirty list and
1178** return a pointer to the head of that list. All pages are
1179** collected even if they are still in use.
1180*/
1181static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
1182 PgHdr *p, *pList;
1183 pList = 0;
1184 for(p=pPager->pAll; p; p=p->pNextAll){
1185 if( p->dirty ){
1186 p->pDirty = pList;
1187 pList = p;
1188 }
1189 }
1190 return pList;
1191}
1192
1193/*
drhd9b02572001-04-15 00:37:09 +00001194** Acquire a page.
1195**
drh58a11682001-11-10 13:51:08 +00001196** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00001197** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00001198**
drh306dc212001-05-21 13:45:10 +00001199** A _get works for any page number greater than 0. If the database
1200** file is smaller than the requested page, then no actual disk
1201** read occurs and the memory image of the page is initialized to
1202** all zeros. The extra data appended to a page is always initialized
1203** to zeros the first time a page is loaded into memory.
1204**
drhd9b02572001-04-15 00:37:09 +00001205** The acquisition might fail for several reasons. In all cases,
1206** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00001207**
1208** See also sqlitepager_lookup(). Both this routine and _lookup() attempt
1209** to find a page in the in-memory cache first. If the page is not already
drh5e00f6c2001-09-13 13:46:56 +00001210** in memory, this routine goes to disk to read it in whereas _lookup()
drh7e3b0a02001-04-28 16:52:40 +00001211** just returns 0. This routine acquires a read-lock the first time it
1212** has to go to disk, and could also playback an old journal if necessary.
1213** Since _lookup() never goes to disk, it never has to deal with locks
1214** or journal files.
drhed7c8552001-04-11 14:29:21 +00001215*/
drhd9b02572001-04-15 00:37:09 +00001216int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +00001217 PgHdr *pPg;
drh8766c342002-11-09 00:33:15 +00001218 int rc;
drhed7c8552001-04-11 14:29:21 +00001219
drhd9b02572001-04-15 00:37:09 +00001220 /* Make sure we have not hit any critical errors.
1221 */
drh836faa42003-01-11 13:30:57 +00001222 assert( pPager!=0 );
1223 assert( pgno!=0 );
drh2e6d11b2003-04-25 15:37:57 +00001224 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00001225 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1226 return pager_errcode(pPager);
1227 }
1228
drhed7c8552001-04-11 14:29:21 +00001229 /* If this is the first page accessed, then get a read lock
1230 ** on the database file.
1231 */
1232 if( pPager->nRef==0 ){
drh8766c342002-11-09 00:33:15 +00001233 rc = sqliteOsReadLock(&pPager->fd);
1234 if( rc!=SQLITE_OK ){
drh8766c342002-11-09 00:33:15 +00001235 return rc;
drhed7c8552001-04-11 14:29:21 +00001236 }
drhd9b02572001-04-15 00:37:09 +00001237 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +00001238
1239 /* If a journal file exists, try to play it back.
1240 */
drhda47d772002-12-02 04:25:19 +00001241 if( pPager->useJournal && sqliteOsFileExists(pPager->zJournal) ){
drhe2227f02003-06-14 11:42:57 +00001242 int rc;
drhed7c8552001-04-11 14:29:21 +00001243
drha7fcb052001-12-14 15:09:55 +00001244 /* Get a write lock on the database
1245 */
1246 rc = sqliteOsWriteLock(&pPager->fd);
1247 if( rc!=SQLITE_OK ){
drh8766c342002-11-09 00:33:15 +00001248 if( sqliteOsUnlock(&pPager->fd)!=SQLITE_OK ){
1249 /* This should never happen! */
1250 rc = SQLITE_INTERNAL;
1251 }
drh8766c342002-11-09 00:33:15 +00001252 return rc;
drha7fcb052001-12-14 15:09:55 +00001253 }
1254 pPager->state = SQLITE_WRITELOCK;
1255
drhe2227f02003-06-14 11:42:57 +00001256 /* Open the journal for reading only. Return SQLITE_BUSY if
1257 ** we are unable to open the journal file.
drhf57b3392001-10-08 13:22:32 +00001258 **
drhe2227f02003-06-14 11:42:57 +00001259 ** The journal file does not need to be locked itself. The
1260 ** journal file is never open unless the main database file holds
1261 ** a write lock, so there is never any chance of two or more
1262 ** processes opening the journal at the same time.
drhed7c8552001-04-11 14:29:21 +00001263 */
drhe2227f02003-06-14 11:42:57 +00001264 rc = sqliteOsOpenReadOnly(pPager->zJournal, &pPager->jfd);
drha7fcb052001-12-14 15:09:55 +00001265 if( rc!=SQLITE_OK ){
1266 rc = sqliteOsUnlock(&pPager->fd);
1267 assert( rc==SQLITE_OK );
drhed7c8552001-04-11 14:29:21 +00001268 return SQLITE_BUSY;
1269 }
drha7fcb052001-12-14 15:09:55 +00001270 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001271 pPager->journalStarted = 0;
drhed7c8552001-04-11 14:29:21 +00001272
1273 /* Playback and delete the journal. Drop the database write
1274 ** lock and reacquire the read lock.
1275 */
drh99ee3602003-02-16 19:13:36 +00001276 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00001277 if( rc!=SQLITE_OK ){
1278 return rc;
1279 }
drhed7c8552001-04-11 14:29:21 +00001280 }
1281 pPg = 0;
1282 }else{
1283 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +00001284 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00001285 }
1286 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001287 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +00001288 int h;
drh7e3b0a02001-04-28 16:52:40 +00001289 pPager->nMiss++;
drhed7c8552001-04-11 14:29:21 +00001290 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){
1291 /* Create a new page */
drh968af522003-02-11 14:55:40 +00001292 pPg = sqliteMallocRaw( sizeof(*pPg) + SQLITE_PAGE_SIZE
1293 + sizeof(u32) + pPager->nExtra );
drhd9b02572001-04-15 00:37:09 +00001294 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001295 pager_unwritelock(pPager);
1296 pPager->errMask |= PAGER_ERR_MEM;
1297 return SQLITE_NOMEM;
1298 }
drh8c1238a2003-01-02 14:43:55 +00001299 memset(pPg, 0, sizeof(*pPg));
drhed7c8552001-04-11 14:29:21 +00001300 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +00001301 pPg->pNextAll = pPager->pAll;
1302 if( pPager->pAll ){
1303 pPager->pAll->pPrevAll = pPg;
1304 }
1305 pPg->pPrevAll = 0;
drhd79caeb2001-04-15 02:27:24 +00001306 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +00001307 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +00001308 }else{
drhdb48ee02003-01-16 13:42:43 +00001309 /* Find a page to recycle. Try to locate a page that does not
1310 ** require us to do an fsync() on the journal.
1311 */
drh341eae82003-01-21 02:39:36 +00001312 pPg = pPager->pFirstSynced;
drhb19a2bc2001-09-16 00:13:26 +00001313
drhdb48ee02003-01-16 13:42:43 +00001314 /* If we could not find a page that does not require an fsync()
1315 ** on the journal file then fsync the journal file. This is a
1316 ** very slow operation, so we work hard to avoid it. But sometimes
1317 ** it can't be helped.
drhb19a2bc2001-09-16 00:13:26 +00001318 */
drh603240c2002-03-05 01:11:12 +00001319 if( pPg==0 ){
drh50e5dad2001-09-15 00:57:28 +00001320 int rc = syncAllPages(pPager);
1321 if( rc!=0 ){
1322 sqlitepager_rollback(pPager);
drh50e5dad2001-09-15 00:57:28 +00001323 return SQLITE_IOERR;
1324 }
1325 pPg = pPager->pFirst;
1326 }
drhd9b02572001-04-15 00:37:09 +00001327 assert( pPg->nRef==0 );
drhdb48ee02003-01-16 13:42:43 +00001328
1329 /* Write the page to the database file if it is dirty.
1330 */
1331 if( pPg->dirty ){
1332 assert( pPg->needSync==0 );
drh2554f8b2003-01-22 01:26:44 +00001333 pPg->pDirty = 0;
1334 rc = pager_write_pagelist( pPg );
drhdb48ee02003-01-16 13:42:43 +00001335 if( rc!=SQLITE_OK ){
1336 sqlitepager_rollback(pPager);
drhdb48ee02003-01-16 13:42:43 +00001337 return SQLITE_IOERR;
1338 }
drhdb48ee02003-01-16 13:42:43 +00001339 }
drh50e5dad2001-09-15 00:57:28 +00001340 assert( pPg->dirty==0 );
drhd9b02572001-04-15 00:37:09 +00001341
drhdb48ee02003-01-16 13:42:43 +00001342 /* If the page we are recycling is marked as alwaysRollback, then
drh193a6b42002-07-07 16:52:46 +00001343 ** set the global alwaysRollback flag, thus disabling the
1344 ** sqlite_dont_rollback() optimization for the rest of this transaction.
1345 ** It is necessary to do this because the page marked alwaysRollback
1346 ** might be reloaded at a later time but at that point we won't remember
1347 ** that is was marked alwaysRollback. This means that all pages must
1348 ** be marked as alwaysRollback from here on out.
1349 */
1350 if( pPg->alwaysRollback ){
1351 pPager->alwaysRollback = 1;
1352 }
1353
drhd9b02572001-04-15 00:37:09 +00001354 /* Unlink the old page from the free list and the hash table
1355 */
drh341eae82003-01-21 02:39:36 +00001356 if( pPg==pPager->pFirstSynced ){
1357 PgHdr *p = pPg->pNextFree;
1358 while( p && p->needSync ){ p = p->pNextFree; }
1359 pPager->pFirstSynced = p;
1360 }
drh6019e162001-07-02 17:51:45 +00001361 if( pPg->pPrevFree ){
1362 pPg->pPrevFree->pNextFree = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +00001363 }else{
drh6019e162001-07-02 17:51:45 +00001364 assert( pPager->pFirst==pPg );
1365 pPager->pFirst = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +00001366 }
drh6019e162001-07-02 17:51:45 +00001367 if( pPg->pNextFree ){
1368 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1369 }else{
1370 assert( pPager->pLast==pPg );
1371 pPager->pLast = pPg->pPrevFree;
1372 }
1373 pPg->pNextFree = pPg->pPrevFree = 0;
drhed7c8552001-04-11 14:29:21 +00001374 if( pPg->pNextHash ){
1375 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
1376 }
1377 if( pPg->pPrevHash ){
1378 pPg->pPrevHash->pNextHash = pPg->pNextHash;
1379 }else{
drhd9b02572001-04-15 00:37:09 +00001380 h = pager_hash(pPg->pgno);
drhed7c8552001-04-11 14:29:21 +00001381 assert( pPager->aHash[h]==pPg );
1382 pPager->aHash[h] = pPg->pNextHash;
1383 }
drh6019e162001-07-02 17:51:45 +00001384 pPg->pNextHash = pPg->pPrevHash = 0;
drhd9b02572001-04-15 00:37:09 +00001385 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +00001386 }
1387 pPg->pgno = pgno;
drh1ab43002002-01-14 09:28:19 +00001388 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
drhed6c8672003-01-12 18:02:16 +00001389 sqliteCheckMemory(pPager->aInJournal, pgno/8);
drhdb48ee02003-01-16 13:42:43 +00001390 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00001391 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00001392 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001393 }else{
1394 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00001395 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001396 }
drh03eb96a2002-11-10 23:32:56 +00001397 if( pPager->aInCkpt && (int)pgno<=pPager->ckptSize
1398 && (pPager->aInCkpt[pgno/8] & (1<<(pgno&7)))!=0 ){
1399 page_add_to_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001400 }else{
drh03eb96a2002-11-10 23:32:56 +00001401 page_remove_from_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001402 }
drhed7c8552001-04-11 14:29:21 +00001403 pPg->dirty = 0;
1404 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00001405 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001406 pPager->nRef++;
1407 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +00001408 pPg->pNextHash = pPager->aHash[h];
1409 pPager->aHash[h] = pPg;
1410 if( pPg->pNextHash ){
1411 assert( pPg->pNextHash->pPrevHash==0 );
1412 pPg->pNextHash->pPrevHash = pPg;
1413 }
drh2e6d11b2003-04-25 15:37:57 +00001414 if( pPager->nExtra>0 ){
1415 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
1416 }
drh306dc212001-05-21 13:45:10 +00001417 if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager);
drh2e6d11b2003-04-25 15:37:57 +00001418 if( pPager->errMask!=0 ){
1419 sqlitepager_unref(PGHDR_TO_DATA(pPg));
1420 rc = pager_errcode(pPager);
1421 return rc;
1422 }
drh1ab43002002-01-14 09:28:19 +00001423 if( pPager->dbSize<(int)pgno ){
drh306dc212001-05-21 13:45:10 +00001424 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
1425 }else{
drh81a20f22001-10-12 17:30:04 +00001426 int rc;
drhd0d006e2002-12-01 02:00:57 +00001427 sqliteOsSeek(&pPager->fd, (pgno-1)*(off_t)SQLITE_PAGE_SIZE);
drha7fcb052001-12-14 15:09:55 +00001428 rc = sqliteOsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drh81a20f22001-10-12 17:30:04 +00001429 if( rc!=SQLITE_OK ){
drh28be87c2002-11-05 23:03:02 +00001430 off_t fileSize;
drh4e371ee2002-09-05 16:08:27 +00001431 if( sqliteOsFileSize(&pPager->fd,&fileSize)!=SQLITE_OK
1432 || fileSize>=pgno*SQLITE_PAGE_SIZE ){
drh2e6d11b2003-04-25 15:37:57 +00001433 sqlitepager_unref(PGHDR_TO_DATA(pPg));
drh4e371ee2002-09-05 16:08:27 +00001434 return rc;
1435 }else{
1436 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
1437 }
drh81a20f22001-10-12 17:30:04 +00001438 }
drh306dc212001-05-21 13:45:10 +00001439 }
drhed7c8552001-04-11 14:29:21 +00001440 }else{
drhd9b02572001-04-15 00:37:09 +00001441 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +00001442 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +00001443 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00001444 }
1445 *ppPage = PGHDR_TO_DATA(pPg);
1446 return SQLITE_OK;
1447}
1448
1449/*
drh7e3b0a02001-04-28 16:52:40 +00001450** Acquire a page if it is already in the in-memory cache. Do
1451** not read the page from disk. Return a pointer to the page,
1452** or 0 if the page is not in cache.
1453**
1454** See also sqlitepager_get(). The difference between this routine
1455** and sqlitepager_get() is that _get() will go to the disk and read
1456** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00001457** returns NULL if the page is not in cache or if a disk I/O error
1458** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00001459*/
1460void *sqlitepager_lookup(Pager *pPager, Pgno pgno){
1461 PgHdr *pPg;
1462
drh836faa42003-01-11 13:30:57 +00001463 assert( pPager!=0 );
1464 assert( pgno!=0 );
drh7e3b0a02001-04-28 16:52:40 +00001465 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1466 return 0;
1467 }
drh836faa42003-01-11 13:30:57 +00001468 /* if( pPager->nRef==0 ){
1469 ** return 0;
1470 ** }
1471 */
drh7e3b0a02001-04-28 16:52:40 +00001472 pPg = pager_lookup(pPager, pgno);
1473 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +00001474 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +00001475 return PGHDR_TO_DATA(pPg);
1476}
1477
1478/*
drhed7c8552001-04-11 14:29:21 +00001479** Release a page.
1480**
1481** If the number of references to the page drop to zero, then the
1482** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00001483** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00001484** removed.
1485*/
drhd9b02572001-04-15 00:37:09 +00001486int sqlitepager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +00001487 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001488
1489 /* Decrement the reference count for this page
1490 */
drhed7c8552001-04-11 14:29:21 +00001491 pPg = DATA_TO_PGHDR(pData);
1492 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +00001493 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00001494 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001495
drh72f82862001-05-24 21:06:34 +00001496 /* When the number of references to a page reach 0, call the
1497 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00001498 */
drhed7c8552001-04-11 14:29:21 +00001499 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00001500 Pager *pPager;
1501 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00001502 pPg->pNextFree = 0;
1503 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00001504 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00001505 if( pPg->pPrevFree ){
1506 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00001507 }else{
1508 pPager->pFirst = pPg;
1509 }
drh341eae82003-01-21 02:39:36 +00001510 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
1511 pPager->pFirstSynced = pPg;
1512 }
drh72f82862001-05-24 21:06:34 +00001513 if( pPager->xDestructor ){
1514 pPager->xDestructor(pData);
1515 }
drhd9b02572001-04-15 00:37:09 +00001516
1517 /* When all pages reach the freelist, drop the read lock from
1518 ** the database file.
1519 */
1520 pPager->nRef--;
1521 assert( pPager->nRef>=0 );
1522 if( pPager->nRef==0 ){
1523 pager_reset(pPager);
1524 }
drhed7c8552001-04-11 14:29:21 +00001525 }
drhd9b02572001-04-15 00:37:09 +00001526 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00001527}
1528
1529/*
drhda47d772002-12-02 04:25:19 +00001530** Create a journal file for pPager. There should already be a write
1531** lock on the database file when this routine is called.
1532**
1533** Return SQLITE_OK if everything. Return an error code and release the
1534** write lock if anything goes wrong.
1535*/
1536static int pager_open_journal(Pager *pPager){
1537 int rc;
1538 assert( pPager->state==SQLITE_WRITELOCK );
1539 assert( pPager->journalOpen==0 );
1540 assert( pPager->useJournal );
drh3e4c8522003-07-07 10:47:10 +00001541 sqlitepager_pagecount(pPager);
drhda47d772002-12-02 04:25:19 +00001542 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
1543 if( pPager->aInJournal==0 ){
1544 sqliteOsReadLock(&pPager->fd);
1545 pPager->state = SQLITE_READLOCK;
1546 return SQLITE_NOMEM;
1547 }
1548 rc = sqliteOsOpenExclusive(pPager->zJournal, &pPager->jfd,pPager->tempFile);
1549 if( rc!=SQLITE_OK ){
1550 sqliteFree(pPager->aInJournal);
1551 pPager->aInJournal = 0;
1552 sqliteOsReadLock(&pPager->fd);
1553 pPager->state = SQLITE_READLOCK;
1554 return SQLITE_CANTOPEN;
1555 }
drha76c82e2003-07-27 18:59:42 +00001556 sqliteOsOpenDirectory(pPager->zDirectory, &pPager->jfd);
drhda47d772002-12-02 04:25:19 +00001557 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001558 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00001559 pPager->needSync = 0;
1560 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00001561 pPager->nRec = 0;
drh2e6d11b2003-04-25 15:37:57 +00001562 if( pPager->errMask!=0 ){
1563 rc = pager_errcode(pPager);
1564 return rc;
1565 }
drhda47d772002-12-02 04:25:19 +00001566 pPager->origDbSize = pPager->dbSize;
drh968af522003-02-11 14:55:40 +00001567 if( journal_format==JOURNAL_FORMAT_3 ){
1568 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic3, sizeof(aJournalMagic3));
1569 if( rc==SQLITE_OK ){
drh4303fee2003-02-15 23:09:17 +00001570 rc = write32bits(&pPager->jfd, pPager->noSync ? 0xffffffff : 0);
drh968af522003-02-11 14:55:40 +00001571 }
1572 if( rc==SQLITE_OK ){
1573 pPager->cksumInit = (u32)sqliteRandomInteger();
1574 rc = write32bits(&pPager->jfd, pPager->cksumInit);
1575 }
1576 }else if( journal_format==JOURNAL_FORMAT_2 ){
1577 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic2, sizeof(aJournalMagic2));
drhda47d772002-12-02 04:25:19 +00001578 }else{
drh968af522003-02-11 14:55:40 +00001579 assert( journal_format==JOURNAL_FORMAT_1 );
1580 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic1, sizeof(aJournalMagic1));
drhda47d772002-12-02 04:25:19 +00001581 }
1582 if( rc==SQLITE_OK ){
1583 rc = write32bits(&pPager->jfd, pPager->dbSize);
1584 }
1585 if( pPager->ckptAutoopen && rc==SQLITE_OK ){
1586 rc = sqlitepager_ckpt_begin(pPager);
1587 }
1588 if( rc!=SQLITE_OK ){
1589 rc = pager_unwritelock(pPager);
1590 if( rc==SQLITE_OK ){
1591 rc = SQLITE_FULL;
1592 }
1593 }
1594 return rc;
1595}
1596
1597/*
drh4b845d72002-03-05 12:41:19 +00001598** Acquire a write-lock on the database. The lock is removed when
1599** the any of the following happen:
1600**
1601** * sqlitepager_commit() is called.
1602** * sqlitepager_rollback() is called.
1603** * sqlitepager_close() is called.
1604** * sqlitepager_unref() is called to on every outstanding page.
1605**
1606** The parameter to this routine is a pointer to any open page of the
1607** database file. Nothing changes about the page - it is used merely
1608** to acquire a pointer to the Pager structure and as proof that there
1609** is already a read-lock on the database.
1610**
drhda47d772002-12-02 04:25:19 +00001611** A journal file is opened if this is not a temporary file. For
1612** temporary files, the opening of the journal file is deferred until
1613** there is an actual need to write to the journal.
1614**
drh4b845d72002-03-05 12:41:19 +00001615** If the database is already write-locked, this routine is a no-op.
1616*/
1617int sqlitepager_begin(void *pData){
1618 PgHdr *pPg = DATA_TO_PGHDR(pData);
1619 Pager *pPager = pPg->pPager;
1620 int rc = SQLITE_OK;
1621 assert( pPg->nRef>0 );
1622 assert( pPager->state!=SQLITE_UNLOCK );
1623 if( pPager->state==SQLITE_READLOCK ){
1624 assert( pPager->aInJournal==0 );
1625 rc = sqliteOsWriteLock(&pPager->fd);
1626 if( rc!=SQLITE_OK ){
1627 return rc;
1628 }
drh4b845d72002-03-05 12:41:19 +00001629 pPager->state = SQLITE_WRITELOCK;
drhda47d772002-12-02 04:25:19 +00001630 pPager->dirtyFile = 0;
drhdb48ee02003-01-16 13:42:43 +00001631 TRACE1("TRANSACTION\n");
drhda47d772002-12-02 04:25:19 +00001632 if( pPager->useJournal && !pPager->tempFile ){
1633 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00001634 }
1635 }
1636 return rc;
1637}
1638
1639/*
drhed7c8552001-04-11 14:29:21 +00001640** Mark a data page as writeable. The page is written into the journal
1641** if it is not there already. This routine must be called before making
1642** changes to a page.
1643**
1644** The first time this routine is called, the pager creates a new
1645** journal and acquires a write lock on the database. If the write
1646** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00001647** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00001648** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00001649**
1650** If the journal file could not be written because the disk is full,
1651** then this routine returns SQLITE_FULL and does an immediate rollback.
1652** All subsequent write attempts also return SQLITE_FULL until there
1653** is a call to sqlitepager_commit() or sqlitepager_rollback() to
1654** reset.
drhed7c8552001-04-11 14:29:21 +00001655*/
drhd9b02572001-04-15 00:37:09 +00001656int sqlitepager_write(void *pData){
drh69688d52001-04-14 16:38:23 +00001657 PgHdr *pPg = DATA_TO_PGHDR(pData);
1658 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00001659 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00001660
drh6446c4d2001-12-15 14:22:18 +00001661 /* Check for errors
1662 */
drhd9b02572001-04-15 00:37:09 +00001663 if( pPager->errMask ){
1664 return pager_errcode(pPager);
1665 }
drh5e00f6c2001-09-13 13:46:56 +00001666 if( pPager->readOnly ){
1667 return SQLITE_PERM;
1668 }
drh6446c4d2001-12-15 14:22:18 +00001669
1670 /* Mark the page as dirty. If the page has already been written
1671 ** to the journal then we can return right away.
1672 */
drhd9b02572001-04-15 00:37:09 +00001673 pPg->dirty = 1;
drh0f892532002-05-30 12:27:03 +00001674 if( pPg->inJournal && (pPg->inCkpt || pPager->ckptInUse==0) ){
drha1680452002-04-18 01:56:57 +00001675 pPager->dirtyFile = 1;
drhfa86c412002-02-02 15:01:15 +00001676 return SQLITE_OK;
1677 }
drh6446c4d2001-12-15 14:22:18 +00001678
1679 /* If we get this far, it means that the page needs to be
drhfa86c412002-02-02 15:01:15 +00001680 ** written to the transaction journal or the ckeckpoint journal
1681 ** or both.
1682 **
1683 ** First check to see that the transaction journal exists and
1684 ** create it if it does not.
drh6446c4d2001-12-15 14:22:18 +00001685 */
drhd9b02572001-04-15 00:37:09 +00001686 assert( pPager->state!=SQLITE_UNLOCK );
drh4b845d72002-03-05 12:41:19 +00001687 rc = sqlitepager_begin(pData);
drhda47d772002-12-02 04:25:19 +00001688 if( rc!=SQLITE_OK ){
1689 return rc;
1690 }
drhd9b02572001-04-15 00:37:09 +00001691 assert( pPager->state==SQLITE_WRITELOCK );
drhda47d772002-12-02 04:25:19 +00001692 if( !pPager->journalOpen && pPager->useJournal ){
1693 rc = pager_open_journal(pPager);
1694 if( rc!=SQLITE_OK ) return rc;
1695 }
1696 assert( pPager->journalOpen || !pPager->useJournal );
1697 pPager->dirtyFile = 1;
drh6446c4d2001-12-15 14:22:18 +00001698
drhfa86c412002-02-02 15:01:15 +00001699 /* The transaction journal now exists and we have a write lock on the
1700 ** main database file. Write the current page to the transaction
1701 ** journal if it is not there already.
drh6446c4d2001-12-15 14:22:18 +00001702 */
drhdb48ee02003-01-16 13:42:43 +00001703 if( !pPg->inJournal && pPager->useJournal ){
1704 if( (int)pPg->pgno <= pPager->origDbSize ){
drh968af522003-02-11 14:55:40 +00001705 int szPg;
1706 u32 saved;
1707 if( journal_format>=JOURNAL_FORMAT_3 ){
1708 u32 cksum = pager_cksum(pPager, pPg->pgno, pData);
1709 saved = *(u32*)PGHDR_TO_EXTRA(pPg);
1710 store32bits(cksum, pPg, SQLITE_PAGE_SIZE);
1711 szPg = SQLITE_PAGE_SIZE+8;
1712 }else{
1713 szPg = SQLITE_PAGE_SIZE+4;
1714 }
1715 store32bits(pPg->pgno, pPg, -4);
1716 rc = sqliteOsWrite(&pPager->jfd, &((char*)pData)[-4], szPg);
1717 if( journal_format>=JOURNAL_FORMAT_3 ){
1718 *(u32*)PGHDR_TO_EXTRA(pPg) = saved;
1719 }
drhdb48ee02003-01-16 13:42:43 +00001720 if( rc!=SQLITE_OK ){
1721 sqlitepager_rollback(pPager);
1722 pPager->errMask |= PAGER_ERR_FULL;
1723 return rc;
1724 }
drh99ee3602003-02-16 19:13:36 +00001725 pPager->nRec++;
drhdb48ee02003-01-16 13:42:43 +00001726 assert( pPager->aInJournal!=0 );
1727 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1728 pPg->needSync = !pPager->noSync;
1729 pPg->inJournal = 1;
1730 if( pPager->ckptInUse ){
1731 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1732 page_add_to_ckpt_list(pPg);
1733 }
1734 TRACE3("JOURNAL %d %d\n", pPg->pgno, pPg->needSync);
1735 }else{
1736 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
1737 TRACE3("APPEND %d %d\n", pPg->pgno, pPg->needSync);
drhd9b02572001-04-15 00:37:09 +00001738 }
drhdb48ee02003-01-16 13:42:43 +00001739 if( pPg->needSync ){
1740 pPager->needSync = 1;
drhfa86c412002-02-02 15:01:15 +00001741 }
drh69688d52001-04-14 16:38:23 +00001742 }
drh6446c4d2001-12-15 14:22:18 +00001743
drhfa86c412002-02-02 15:01:15 +00001744 /* If the checkpoint journal is open and the page is not in it,
drh968af522003-02-11 14:55:40 +00001745 ** then write the current page to the checkpoint journal. Note that
1746 ** the checkpoint journal always uses the simplier format 2 that lacks
1747 ** checksums. The header is also omitted from the checkpoint journal.
drh6446c4d2001-12-15 14:22:18 +00001748 */
drh0f892532002-05-30 12:27:03 +00001749 if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
drh1e336b42002-02-14 12:50:33 +00001750 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drh968af522003-02-11 14:55:40 +00001751 store32bits(pPg->pgno, pPg, -4);
drh2554f8b2003-01-22 01:26:44 +00001752 rc = sqliteOsWrite(&pPager->cpfd, &((char*)pData)[-4], SQLITE_PAGE_SIZE+4);
drhfa86c412002-02-02 15:01:15 +00001753 if( rc!=SQLITE_OK ){
1754 sqlitepager_rollback(pPager);
1755 pPager->errMask |= PAGER_ERR_FULL;
1756 return rc;
1757 }
drh9bd47a92003-01-07 14:46:08 +00001758 pPager->ckptNRec++;
drhfa86c412002-02-02 15:01:15 +00001759 assert( pPager->aInCkpt!=0 );
1760 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001761 page_add_to_ckpt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001762 }
1763
1764 /* Update the database size and return.
1765 */
drh1ab43002002-01-14 09:28:19 +00001766 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00001767 pPager->dbSize = pPg->pgno;
1768 }
drh69688d52001-04-14 16:38:23 +00001769 return rc;
drhed7c8552001-04-11 14:29:21 +00001770}
1771
1772/*
drhaacc5432002-01-06 17:07:40 +00001773** Return TRUE if the page given in the argument was previously passed
drh6019e162001-07-02 17:51:45 +00001774** to sqlitepager_write(). In other words, return TRUE if it is ok
1775** to change the content of the page.
1776*/
1777int sqlitepager_iswriteable(void *pData){
1778 PgHdr *pPg = DATA_TO_PGHDR(pData);
1779 return pPg->dirty;
1780}
1781
1782/*
drh001bbcb2003-03-19 03:14:00 +00001783** Replace the content of a single page with the information in the third
1784** argument.
1785*/
1786int sqlitepager_overwrite(Pager *pPager, Pgno pgno, void *pData){
1787 void *pPage;
1788 int rc;
1789
1790 rc = sqlitepager_get(pPager, pgno, &pPage);
1791 if( rc==SQLITE_OK ){
1792 rc = sqlitepager_write(pPage);
1793 if( rc==SQLITE_OK ){
1794 memcpy(pPage, pData, SQLITE_PAGE_SIZE);
1795 }
1796 sqlitepager_unref(pPage);
1797 }
1798 return rc;
1799}
1800
1801/*
drh30e58752002-03-02 20:41:57 +00001802** A call to this routine tells the pager that it is not necessary to
1803** write the information on page "pgno" back to the disk, even though
1804** that page might be marked as dirty.
1805**
1806** The overlying software layer calls this routine when all of the data
1807** on the given page is unused. The pager marks the page as clean so
1808** that it does not get written to disk.
1809**
1810** Tests show that this optimization, together with the
1811** sqlitepager_dont_rollback() below, more than double the speed
1812** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00001813**
1814** When this routine is called, set the alwaysRollback flag to true.
1815** Subsequent calls to sqlitepager_dont_rollback() for the same page
1816** will thereafter be ignored. This is necessary to avoid a problem
1817** where a page with data is added to the freelist during one part of
1818** a transaction then removed from the freelist during a later part
1819** of the same transaction and reused for some other purpose. When it
1820** is first added to the freelist, this routine is called. When reused,
1821** the dont_rollback() routine is called. But because the page contains
1822** critical data, we still need to be sure it gets rolled back in spite
1823** of the dont_rollback() call.
drh30e58752002-03-02 20:41:57 +00001824*/
1825void sqlitepager_dont_write(Pager *pPager, Pgno pgno){
1826 PgHdr *pPg;
drh8e298f92002-07-06 16:28:47 +00001827
drh30e58752002-03-02 20:41:57 +00001828 pPg = pager_lookup(pPager, pgno);
drh8e298f92002-07-06 16:28:47 +00001829 pPg->alwaysRollback = 1;
drh30e58752002-03-02 20:41:57 +00001830 if( pPg && pPg->dirty ){
drh8124a302002-06-25 14:43:57 +00001831 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
1832 /* If this pages is the last page in the file and the file has grown
1833 ** during the current transaction, then do NOT mark the page as clean.
1834 ** When the database file grows, we must make sure that the last page
1835 ** gets written at least once so that the disk file will be the correct
1836 ** size. If you do not write this page and the size of the file
1837 ** on the disk ends up being too small, that can lead to database
1838 ** corruption during the next transaction.
1839 */
1840 }else{
drhdb48ee02003-01-16 13:42:43 +00001841 TRACE2("DONT_WRITE %d\n", pgno);
drh8124a302002-06-25 14:43:57 +00001842 pPg->dirty = 0;
1843 }
drh30e58752002-03-02 20:41:57 +00001844 }
1845}
1846
1847/*
1848** A call to this routine tells the pager that if a rollback occurs,
1849** it is not necessary to restore the data on the given page. This
1850** means that the pager does not have to record the given page in the
1851** rollback journal.
1852*/
1853void sqlitepager_dont_rollback(void *pData){
1854 PgHdr *pPg = DATA_TO_PGHDR(pData);
1855 Pager *pPager = pPg->pPager;
1856
1857 if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return;
drh193a6b42002-07-07 16:52:46 +00001858 if( pPg->alwaysRollback || pPager->alwaysRollback ) return;
drh30e58752002-03-02 20:41:57 +00001859 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
1860 assert( pPager->aInJournal!=0 );
1861 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1862 pPg->inJournal = 1;
drh0f892532002-05-30 12:27:03 +00001863 if( pPager->ckptInUse ){
drh30e58752002-03-02 20:41:57 +00001864 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001865 page_add_to_ckpt_list(pPg);
drh30e58752002-03-02 20:41:57 +00001866 }
drhdb48ee02003-01-16 13:42:43 +00001867 TRACE2("DONT_ROLLBACK %d\n", pPg->pgno);
drh30e58752002-03-02 20:41:57 +00001868 }
drh0f892532002-05-30 12:27:03 +00001869 if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
drh30e58752002-03-02 20:41:57 +00001870 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
1871 assert( pPager->aInCkpt!=0 );
1872 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh03eb96a2002-11-10 23:32:56 +00001873 page_add_to_ckpt_list(pPg);
drh30e58752002-03-02 20:41:57 +00001874 }
1875}
1876
1877/*
drhed7c8552001-04-11 14:29:21 +00001878** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00001879**
1880** If the commit fails for any reason, a rollback attempt is made
1881** and an error code is returned. If the commit worked, SQLITE_OK
1882** is returned.
drhed7c8552001-04-11 14:29:21 +00001883*/
drhd9b02572001-04-15 00:37:09 +00001884int sqlitepager_commit(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00001885 int rc;
drhed7c8552001-04-11 14:29:21 +00001886 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001887
1888 if( pPager->errMask==PAGER_ERR_FULL ){
1889 rc = sqlitepager_rollback(pPager);
drh4e371ee2002-09-05 16:08:27 +00001890 if( rc==SQLITE_OK ){
1891 rc = SQLITE_FULL;
1892 }
drhd9b02572001-04-15 00:37:09 +00001893 return rc;
1894 }
1895 if( pPager->errMask!=0 ){
1896 rc = pager_errcode(pPager);
1897 return rc;
1898 }
1899 if( pPager->state!=SQLITE_WRITELOCK ){
1900 return SQLITE_ERROR;
1901 }
drhdb48ee02003-01-16 13:42:43 +00001902 TRACE1("COMMIT\n");
drha1680452002-04-18 01:56:57 +00001903 if( pPager->dirtyFile==0 ){
1904 /* Exit early (without doing the time-consuming sqliteOsSync() calls)
1905 ** if there have been no changes to the database file. */
drh341eae82003-01-21 02:39:36 +00001906 assert( pPager->needSync==0 );
drha1680452002-04-18 01:56:57 +00001907 rc = pager_unwritelock(pPager);
1908 pPager->dbSize = -1;
1909 return rc;
1910 }
drhda47d772002-12-02 04:25:19 +00001911 assert( pPager->journalOpen );
drh240c5792004-02-08 00:40:52 +00001912 rc = syncAllPages(pPager);
1913 if( rc!=SQLITE_OK ){
drhd9b02572001-04-15 00:37:09 +00001914 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +00001915 }
drh2554f8b2003-01-22 01:26:44 +00001916 pPg = pager_get_all_dirty_pages(pPager);
1917 if( pPg ){
1918 rc = pager_write_pagelist(pPg);
1919 if( rc || (!pPager->noSync && sqliteOsSync(&pPager->fd)!=SQLITE_OK) ){
1920 goto commit_abort;
1921 }
drh603240c2002-03-05 01:11:12 +00001922 }
drhd9b02572001-04-15 00:37:09 +00001923 rc = pager_unwritelock(pPager);
1924 pPager->dbSize = -1;
1925 return rc;
1926
1927 /* Jump here if anything goes wrong during the commit process.
1928 */
1929commit_abort:
1930 rc = sqlitepager_rollback(pPager);
1931 if( rc==SQLITE_OK ){
1932 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +00001933 }
drhed7c8552001-04-11 14:29:21 +00001934 return rc;
1935}
1936
1937/*
1938** Rollback all changes. The database falls back to read-only mode.
1939** All in-memory cache pages revert to their original data contents.
1940** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00001941**
1942** This routine cannot fail unless some other process is not following
1943** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
1944** process is writing trash into the journal file (SQLITE_CORRUPT) or
1945** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
1946** codes are returned for all these occasions. Otherwise,
1947** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00001948*/
drhd9b02572001-04-15 00:37:09 +00001949int sqlitepager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001950 int rc;
drhdb48ee02003-01-16 13:42:43 +00001951 TRACE1("ROLLBACK\n");
drhda47d772002-12-02 04:25:19 +00001952 if( !pPager->dirtyFile || !pPager->journalOpen ){
1953 rc = pager_unwritelock(pPager);
1954 pPager->dbSize = -1;
1955 return rc;
1956 }
drhdb48ee02003-01-16 13:42:43 +00001957
drhd9b02572001-04-15 00:37:09 +00001958 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
drh4b845d72002-03-05 12:41:19 +00001959 if( pPager->state>=SQLITE_WRITELOCK ){
drh99ee3602003-02-16 19:13:36 +00001960 pager_playback(pPager, 1);
drh4b845d72002-03-05 12:41:19 +00001961 }
drhd9b02572001-04-15 00:37:09 +00001962 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00001963 }
drhd9b02572001-04-15 00:37:09 +00001964 if( pPager->state!=SQLITE_WRITELOCK ){
1965 return SQLITE_OK;
1966 }
drh99ee3602003-02-16 19:13:36 +00001967 rc = pager_playback(pPager, 1);
drhd9b02572001-04-15 00:37:09 +00001968 if( rc!=SQLITE_OK ){
1969 rc = SQLITE_CORRUPT;
1970 pPager->errMask |= PAGER_ERR_CORRUPT;
1971 }
1972 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00001973 return rc;
drh98808ba2001-10-18 12:34:46 +00001974}
drhd9b02572001-04-15 00:37:09 +00001975
1976/*
drh5e00f6c2001-09-13 13:46:56 +00001977** Return TRUE if the database file is opened read-only. Return FALSE
1978** if the database is (in theory) writable.
1979*/
1980int sqlitepager_isreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00001981 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00001982}
1983
1984/*
drhd9b02572001-04-15 00:37:09 +00001985** This routine is used for testing and analysis only.
1986*/
1987int *sqlitepager_stats(Pager *pPager){
1988 static int a[9];
1989 a[0] = pPager->nRef;
1990 a[1] = pPager->nPage;
1991 a[2] = pPager->mxPage;
1992 a[3] = pPager->dbSize;
1993 a[4] = pPager->state;
1994 a[5] = pPager->errMask;
1995 a[6] = pPager->nHit;
1996 a[7] = pPager->nMiss;
1997 a[8] = pPager->nOvfl;
1998 return a;
1999}
drhdd793422001-06-28 01:54:48 +00002000
drhfa86c412002-02-02 15:01:15 +00002001/*
2002** Set the checkpoint.
2003**
2004** This routine should be called with the transaction journal already
2005** open. A new checkpoint journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00002006** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00002007*/
2008int sqlitepager_ckpt_begin(Pager *pPager){
2009 int rc;
2010 char zTemp[SQLITE_TEMPNAME_SIZE];
drhda47d772002-12-02 04:25:19 +00002011 if( !pPager->journalOpen ){
2012 pPager->ckptAutoopen = 1;
2013 return SQLITE_OK;
2014 }
drhfa86c412002-02-02 15:01:15 +00002015 assert( pPager->journalOpen );
drh0f892532002-05-30 12:27:03 +00002016 assert( !pPager->ckptInUse );
drhfa86c412002-02-02 15:01:15 +00002017 pPager->aInCkpt = sqliteMalloc( pPager->dbSize/8 + 1 );
2018 if( pPager->aInCkpt==0 ){
2019 sqliteOsReadLock(&pPager->fd);
2020 return SQLITE_NOMEM;
2021 }
drh968af522003-02-11 14:55:40 +00002022#ifndef NDEBUG
drhfa86c412002-02-02 15:01:15 +00002023 rc = sqliteOsFileSize(&pPager->jfd, &pPager->ckptJSize);
2024 if( rc ) goto ckpt_begin_failed;
drh968af522003-02-11 14:55:40 +00002025 assert( pPager->ckptJSize ==
2026 pPager->nRec*JOURNAL_PG_SZ(journal_format)+JOURNAL_HDR_SZ(journal_format) );
2027#endif
2028 pPager->ckptJSize = pPager->nRec*JOURNAL_PG_SZ(journal_format)
2029 + JOURNAL_HDR_SZ(journal_format);
drh663fc632002-02-02 18:49:19 +00002030 pPager->ckptSize = pPager->dbSize;
drh0f892532002-05-30 12:27:03 +00002031 if( !pPager->ckptOpen ){
2032 rc = sqlitepager_opentemp(zTemp, &pPager->cpfd);
2033 if( rc ) goto ckpt_begin_failed;
2034 pPager->ckptOpen = 1;
drh9bd47a92003-01-07 14:46:08 +00002035 pPager->ckptNRec = 0;
drh0f892532002-05-30 12:27:03 +00002036 }
2037 pPager->ckptInUse = 1;
drhfa86c412002-02-02 15:01:15 +00002038 return SQLITE_OK;
2039
2040ckpt_begin_failed:
2041 if( pPager->aInCkpt ){
2042 sqliteFree(pPager->aInCkpt);
2043 pPager->aInCkpt = 0;
2044 }
2045 return rc;
2046}
2047
2048/*
2049** Commit a checkpoint.
2050*/
2051int sqlitepager_ckpt_commit(Pager *pPager){
drh0f892532002-05-30 12:27:03 +00002052 if( pPager->ckptInUse ){
drh03eb96a2002-11-10 23:32:56 +00002053 PgHdr *pPg, *pNext;
drh96ddd6d2002-09-05 19:10:33 +00002054 sqliteOsSeek(&pPager->cpfd, 0);
drh9bd47a92003-01-07 14:46:08 +00002055 /* sqliteOsTruncate(&pPager->cpfd, 0); */
2056 pPager->ckptNRec = 0;
drh0f892532002-05-30 12:27:03 +00002057 pPager->ckptInUse = 0;
drh663fc632002-02-02 18:49:19 +00002058 sqliteFree( pPager->aInCkpt );
2059 pPager->aInCkpt = 0;
drh03eb96a2002-11-10 23:32:56 +00002060 for(pPg=pPager->pCkpt; pPg; pPg=pNext){
2061 pNext = pPg->pNextCkpt;
2062 assert( pPg->inCkpt );
drh663fc632002-02-02 18:49:19 +00002063 pPg->inCkpt = 0;
drh03eb96a2002-11-10 23:32:56 +00002064 pPg->pPrevCkpt = pPg->pNextCkpt = 0;
drh663fc632002-02-02 18:49:19 +00002065 }
drh03eb96a2002-11-10 23:32:56 +00002066 pPager->pCkpt = 0;
drh663fc632002-02-02 18:49:19 +00002067 }
drhda47d772002-12-02 04:25:19 +00002068 pPager->ckptAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002069 return SQLITE_OK;
2070}
2071
2072/*
2073** Rollback a checkpoint.
2074*/
2075int sqlitepager_ckpt_rollback(Pager *pPager){
2076 int rc;
drh0f892532002-05-30 12:27:03 +00002077 if( pPager->ckptInUse ){
drh663fc632002-02-02 18:49:19 +00002078 rc = pager_ckpt_playback(pPager);
2079 sqlitepager_ckpt_commit(pPager);
2080 }else{
2081 rc = SQLITE_OK;
2082 }
drhda47d772002-12-02 04:25:19 +00002083 pPager->ckptAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002084 return rc;
2085}
2086
drh73509ee2003-04-06 20:44:45 +00002087/*
2088** Return the full pathname of the database file.
2089*/
2090const char *sqlitepager_filename(Pager *pPager){
2091 return pPager->zFilename;
2092}
2093
drh74587e52002-08-13 00:01:16 +00002094#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00002095/*
2096** Print a listing of all referenced pages and their ref count.
2097*/
2098void sqlitepager_refdump(Pager *pPager){
2099 PgHdr *pPg;
2100 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2101 if( pPg->nRef<=0 ) continue;
2102 printf("PAGE %3d addr=0x%08x nRef=%d\n",
2103 pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
2104 }
2105}
2106#endif