xref: /titanic_50/usr/src/lib/libsqlite/src/pager.c (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292 /*
2*c5c4113dSnw141292  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*c5c4113dSnw141292  * Use is subject to license terms.
4*c5c4113dSnw141292  */
5*c5c4113dSnw141292 
6*c5c4113dSnw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*c5c4113dSnw141292 
8*c5c4113dSnw141292 /*
9*c5c4113dSnw141292 ** 2001 September 15
10*c5c4113dSnw141292 **
11*c5c4113dSnw141292 ** The author disclaims copyright to this source code.  In place of
12*c5c4113dSnw141292 ** a legal notice, here is a blessing:
13*c5c4113dSnw141292 **
14*c5c4113dSnw141292 **    May you do good and not evil.
15*c5c4113dSnw141292 **    May you find forgiveness for yourself and forgive others.
16*c5c4113dSnw141292 **    May you share freely, never taking more than you give.
17*c5c4113dSnw141292 **
18*c5c4113dSnw141292 *************************************************************************
19*c5c4113dSnw141292 ** This is the implementation of the page cache subsystem or "pager".
20*c5c4113dSnw141292 **
21*c5c4113dSnw141292 ** The pager is used to access a database disk file.  It implements
22*c5c4113dSnw141292 ** atomic commit and rollback through the use of a journal file that
23*c5c4113dSnw141292 ** is separate from the database file.  The pager also implements file
24*c5c4113dSnw141292 ** locking to prevent two processes from writing the same database
25*c5c4113dSnw141292 ** file simultaneously, or one process from reading the database while
26*c5c4113dSnw141292 ** another is writing.
27*c5c4113dSnw141292 **
28*c5c4113dSnw141292 ** @(#) $Id: pager.c,v 1.101 2004/02/25 02:20:41 drh Exp $
29*c5c4113dSnw141292 */
30*c5c4113dSnw141292 #include "os.h"         /* Must be first to enable large file support */
31*c5c4113dSnw141292 #include "sqliteInt.h"
32*c5c4113dSnw141292 #include "pager.h"
33*c5c4113dSnw141292 #include <assert.h>
34*c5c4113dSnw141292 #include <string.h>
35*c5c4113dSnw141292 
36*c5c4113dSnw141292 /*
37*c5c4113dSnw141292 ** Macros for troubleshooting.  Normally turned off
38*c5c4113dSnw141292 */
39*c5c4113dSnw141292 #if 0
40*c5c4113dSnw141292 static Pager *mainPager = 0;
41*c5c4113dSnw141292 #define SET_PAGER(X)  if( mainPager==0 ) mainPager = (X)
42*c5c4113dSnw141292 #define CLR_PAGER(X)  if( mainPager==(X) ) mainPager = 0
43*c5c4113dSnw141292 #define TRACE1(X)     if( pPager==mainPager ) fprintf(stderr,X)
44*c5c4113dSnw141292 #define TRACE2(X,Y)   if( pPager==mainPager ) fprintf(stderr,X,Y)
45*c5c4113dSnw141292 #define TRACE3(X,Y,Z) if( pPager==mainPager ) fprintf(stderr,X,Y,Z)
46*c5c4113dSnw141292 #else
47*c5c4113dSnw141292 #define SET_PAGER(X)
48*c5c4113dSnw141292 #define CLR_PAGER(X)
49*c5c4113dSnw141292 #define TRACE1(X)
50*c5c4113dSnw141292 #define TRACE2(X,Y)
51*c5c4113dSnw141292 #define TRACE3(X,Y,Z)
52*c5c4113dSnw141292 #endif
53*c5c4113dSnw141292 
54*c5c4113dSnw141292 
55*c5c4113dSnw141292 /*
56*c5c4113dSnw141292 ** The page cache as a whole is always in one of the following
57*c5c4113dSnw141292 ** states:
58*c5c4113dSnw141292 **
59*c5c4113dSnw141292 **   SQLITE_UNLOCK       The page cache is not currently reading or
60*c5c4113dSnw141292 **                       writing the database file.  There is no
61*c5c4113dSnw141292 **                       data held in memory.  This is the initial
62*c5c4113dSnw141292 **                       state.
63*c5c4113dSnw141292 **
64*c5c4113dSnw141292 **   SQLITE_READLOCK     The page cache is reading the database.
65*c5c4113dSnw141292 **                       Writing is not permitted.  There can be
66*c5c4113dSnw141292 **                       multiple readers accessing the same database
67*c5c4113dSnw141292 **                       file at the same time.
68*c5c4113dSnw141292 **
69*c5c4113dSnw141292 **   SQLITE_WRITELOCK    The page cache is writing the database.
70*c5c4113dSnw141292 **                       Access is exclusive.  No other processes or
71*c5c4113dSnw141292 **                       threads can be reading or writing while one
72*c5c4113dSnw141292 **                       process is writing.
73*c5c4113dSnw141292 **
74*c5c4113dSnw141292 ** The page cache comes up in SQLITE_UNLOCK.  The first time a
75*c5c4113dSnw141292 ** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK.
76*c5c4113dSnw141292 ** After all pages have been released using sqlite_page_unref(),
77*c5c4113dSnw141292 ** the state transitions back to SQLITE_UNLOCK.  The first time
78*c5c4113dSnw141292 ** that sqlite_page_write() is called, the state transitions to
79*c5c4113dSnw141292 ** SQLITE_WRITELOCK.  (Note that sqlite_page_write() can only be
80*c5c4113dSnw141292 ** called on an outstanding page which means that the pager must
81*c5c4113dSnw141292 ** be in SQLITE_READLOCK before it transitions to SQLITE_WRITELOCK.)
82*c5c4113dSnw141292 ** The sqlite_page_rollback() and sqlite_page_commit() functions
83*c5c4113dSnw141292 ** transition the state from SQLITE_WRITELOCK back to SQLITE_READLOCK.
84*c5c4113dSnw141292 */
85*c5c4113dSnw141292 #define SQLITE_UNLOCK      0
86*c5c4113dSnw141292 #define SQLITE_READLOCK    1
87*c5c4113dSnw141292 #define SQLITE_WRITELOCK   2
88*c5c4113dSnw141292 
89*c5c4113dSnw141292 
90*c5c4113dSnw141292 /*
91*c5c4113dSnw141292 ** Each in-memory image of a page begins with the following header.
92*c5c4113dSnw141292 ** This header is only visible to this pager module.  The client
93*c5c4113dSnw141292 ** code that calls pager sees only the data that follows the header.
94*c5c4113dSnw141292 **
95*c5c4113dSnw141292 ** Client code should call sqlitepager_write() on a page prior to making
96*c5c4113dSnw141292 ** any modifications to that page.  The first time sqlitepager_write()
97*c5c4113dSnw141292 ** is called, the original page contents are written into the rollback
98*c5c4113dSnw141292 ** journal and PgHdr.inJournal and PgHdr.needSync are set.  Later, once
99*c5c4113dSnw141292 ** the journal page has made it onto the disk surface, PgHdr.needSync
100*c5c4113dSnw141292 ** is cleared.  The modified page cannot be written back into the original
101*c5c4113dSnw141292 ** database file until the journal pages has been synced to disk and the
102*c5c4113dSnw141292 ** PgHdr.needSync has been cleared.
103*c5c4113dSnw141292 **
104*c5c4113dSnw141292 ** The PgHdr.dirty flag is set when sqlitepager_write() is called and
105*c5c4113dSnw141292 ** is cleared again when the page content is written back to the original
106*c5c4113dSnw141292 ** database file.
107*c5c4113dSnw141292 */
108*c5c4113dSnw141292 typedef struct PgHdr PgHdr;
109*c5c4113dSnw141292 struct PgHdr {
110*c5c4113dSnw141292   Pager *pPager;                 /* The pager to which this page belongs */
111*c5c4113dSnw141292   Pgno pgno;                     /* The page number for this page */
112*c5c4113dSnw141292   PgHdr *pNextHash, *pPrevHash;  /* Hash collision chain for PgHdr.pgno */
113*c5c4113dSnw141292   int nRef;                      /* Number of users of this page */
114*c5c4113dSnw141292   PgHdr *pNextFree, *pPrevFree;  /* Freelist of pages where nRef==0 */
115*c5c4113dSnw141292   PgHdr *pNextAll, *pPrevAll;    /* A list of all pages */
116*c5c4113dSnw141292   PgHdr *pNextCkpt, *pPrevCkpt;  /* List of pages in the checkpoint journal */
117*c5c4113dSnw141292   u8 inJournal;                  /* TRUE if has been written to journal */
118*c5c4113dSnw141292   u8 inCkpt;                     /* TRUE if written to the checkpoint journal */
119*c5c4113dSnw141292   u8 dirty;                      /* TRUE if we need to write back changes */
120*c5c4113dSnw141292   u8 needSync;                   /* Sync journal before writing this page */
121*c5c4113dSnw141292   u8 alwaysRollback;             /* Disable dont_rollback() for this page */
122*c5c4113dSnw141292   PgHdr *pDirty;                 /* Dirty pages sorted by PgHdr.pgno */
123*c5c4113dSnw141292   /* SQLITE_PAGE_SIZE bytes of page data follow this header */
124*c5c4113dSnw141292   /* Pager.nExtra bytes of local data follow the page data */
125*c5c4113dSnw141292 };
126*c5c4113dSnw141292 
127*c5c4113dSnw141292 
128*c5c4113dSnw141292 /*
129*c5c4113dSnw141292 ** A macro used for invoking the codec if there is one
130*c5c4113dSnw141292 */
131*c5c4113dSnw141292 #ifdef SQLITE_HAS_CODEC
132*c5c4113dSnw141292 # define CODEC(P,D,N,X) if( P->xCodec ){ P->xCodec(P->pCodecArg,D,N,X); }
133*c5c4113dSnw141292 #else
134*c5c4113dSnw141292 # define CODEC(P,D,N,X)
135*c5c4113dSnw141292 #endif
136*c5c4113dSnw141292 
137*c5c4113dSnw141292 /*
138*c5c4113dSnw141292 ** Convert a pointer to a PgHdr into a pointer to its data
139*c5c4113dSnw141292 ** and back again.
140*c5c4113dSnw141292 */
141*c5c4113dSnw141292 #define PGHDR_TO_DATA(P)  ((void*)(&(P)[1]))
142*c5c4113dSnw141292 #define DATA_TO_PGHDR(D)  (&((PgHdr*)(D))[-1])
143*c5c4113dSnw141292 #define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE])
144*c5c4113dSnw141292 
145*c5c4113dSnw141292 /*
146*c5c4113dSnw141292 ** How big to make the hash table used for locating in-memory pages
147*c5c4113dSnw141292 ** by page number.
148*c5c4113dSnw141292 */
149*c5c4113dSnw141292 #define N_PG_HASH 2048
150*c5c4113dSnw141292 
151*c5c4113dSnw141292 /*
152*c5c4113dSnw141292 ** Hash a page number
153*c5c4113dSnw141292 */
154*c5c4113dSnw141292 #define pager_hash(PN)  ((PN)&(N_PG_HASH-1))
155*c5c4113dSnw141292 
156*c5c4113dSnw141292 /*
157*c5c4113dSnw141292 ** A open page cache is an instance of the following structure.
158*c5c4113dSnw141292 */
159*c5c4113dSnw141292 struct Pager {
160*c5c4113dSnw141292   char *zFilename;            /* Name of the database file */
161*c5c4113dSnw141292   char *zJournal;             /* Name of the journal file */
162*c5c4113dSnw141292   char *zDirectory;           /* Directory hold database and journal files */
163*c5c4113dSnw141292   OsFile fd, jfd;             /* File descriptors for database and journal */
164*c5c4113dSnw141292   OsFile cpfd;                /* File descriptor for the checkpoint journal */
165*c5c4113dSnw141292   int dbSize;                 /* Number of pages in the file */
166*c5c4113dSnw141292   int origDbSize;             /* dbSize before the current change */
167*c5c4113dSnw141292   int ckptSize;               /* Size of database (in pages) at ckpt_begin() */
168*c5c4113dSnw141292   off_t ckptJSize;            /* Size of journal at ckpt_begin() */
169*c5c4113dSnw141292   int nRec;                   /* Number of pages written to the journal */
170*c5c4113dSnw141292   u32 cksumInit;              /* Quasi-random value added to every checksum */
171*c5c4113dSnw141292   int ckptNRec;               /* Number of records in the checkpoint journal */
172*c5c4113dSnw141292   int nExtra;                 /* Add this many bytes to each in-memory page */
173*c5c4113dSnw141292   void (*xDestructor)(void*); /* Call this routine when freeing pages */
174*c5c4113dSnw141292   int nPage;                  /* Total number of in-memory pages */
175*c5c4113dSnw141292   int nRef;                   /* Number of in-memory pages with PgHdr.nRef>0 */
176*c5c4113dSnw141292   int mxPage;                 /* Maximum number of pages to hold in cache */
177*c5c4113dSnw141292   int nHit, nMiss, nOvfl;     /* Cache hits, missing, and LRU overflows */
178*c5c4113dSnw141292   void (*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
179*c5c4113dSnw141292   void *pCodecArg;            /* First argument to xCodec() */
180*c5c4113dSnw141292   u8 journalOpen;             /* True if journal file descriptors is valid */
181*c5c4113dSnw141292   u8 journalStarted;          /* True if header of journal is synced */
182*c5c4113dSnw141292   u8 useJournal;              /* Use a rollback journal on this file */
183*c5c4113dSnw141292   u8 ckptOpen;                /* True if the checkpoint journal is open */
184*c5c4113dSnw141292   u8 ckptInUse;               /* True we are in a checkpoint */
185*c5c4113dSnw141292   u8 ckptAutoopen;            /* Open ckpt journal when main journal is opened*/
186*c5c4113dSnw141292   u8 noSync;                  /* Do not sync the journal if true */
187*c5c4113dSnw141292   u8 fullSync;                /* Do extra syncs of the journal for robustness */
188*c5c4113dSnw141292   u8 state;                   /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */
189*c5c4113dSnw141292   u8 errMask;                 /* One of several kinds of errors */
190*c5c4113dSnw141292   u8 tempFile;                /* zFilename is a temporary file */
191*c5c4113dSnw141292   u8 readOnly;                /* True for a read-only database */
192*c5c4113dSnw141292   u8 needSync;                /* True if an fsync() is needed on the journal */
193*c5c4113dSnw141292   u8 dirtyFile;               /* True if database file has changed in any way */
194*c5c4113dSnw141292   u8 alwaysRollback;          /* Disable dont_rollback() for all pages */
195*c5c4113dSnw141292   u8 *aInJournal;             /* One bit for each page in the database file */
196*c5c4113dSnw141292   u8 *aInCkpt;                /* One bit for each page in the database */
197*c5c4113dSnw141292   PgHdr *pFirst, *pLast;      /* List of free pages */
198*c5c4113dSnw141292   PgHdr *pFirstSynced;        /* First free page with PgHdr.needSync==0 */
199*c5c4113dSnw141292   PgHdr *pAll;                /* List of all pages */
200*c5c4113dSnw141292   PgHdr *pCkpt;               /* List of pages in the checkpoint journal */
201*c5c4113dSnw141292   PgHdr *aHash[N_PG_HASH];    /* Hash table to map page number of PgHdr */
202*c5c4113dSnw141292 };
203*c5c4113dSnw141292 
204*c5c4113dSnw141292 /*
205*c5c4113dSnw141292 ** These are bits that can be set in Pager.errMask.
206*c5c4113dSnw141292 */
207*c5c4113dSnw141292 #define PAGER_ERR_FULL     0x01  /* a write() failed */
208*c5c4113dSnw141292 #define PAGER_ERR_MEM      0x02  /* malloc() failed */
209*c5c4113dSnw141292 #define PAGER_ERR_LOCK     0x04  /* error in the locking protocol */
210*c5c4113dSnw141292 #define PAGER_ERR_CORRUPT  0x08  /* database or journal corruption */
211*c5c4113dSnw141292 #define PAGER_ERR_DISK     0x10  /* general disk I/O error - bad hard drive? */
212*c5c4113dSnw141292 
213*c5c4113dSnw141292 /*
214*c5c4113dSnw141292 ** The journal file contains page records in the following
215*c5c4113dSnw141292 ** format.
216*c5c4113dSnw141292 **
217*c5c4113dSnw141292 ** Actually, this structure is the complete page record for pager
218*c5c4113dSnw141292 ** formats less than 3.  Beginning with format 3, this record is surrounded
219*c5c4113dSnw141292 ** by two checksums.
220*c5c4113dSnw141292 */
221*c5c4113dSnw141292 typedef struct PageRecord PageRecord;
222*c5c4113dSnw141292 struct PageRecord {
223*c5c4113dSnw141292   Pgno pgno;                      /* The page number */
224*c5c4113dSnw141292   char aData[SQLITE_PAGE_SIZE];   /* Original data for page pgno */
225*c5c4113dSnw141292 };
226*c5c4113dSnw141292 
227*c5c4113dSnw141292 /*
228*c5c4113dSnw141292 ** Journal files begin with the following magic string.  The data
229*c5c4113dSnw141292 ** was obtained from /dev/random.  It is used only as a sanity check.
230*c5c4113dSnw141292 **
231*c5c4113dSnw141292 ** There are three journal formats (so far). The 1st journal format writes
232*c5c4113dSnw141292 ** 32-bit integers in the byte-order of the host machine.  New
233*c5c4113dSnw141292 ** formats writes integers as big-endian.  All new journals use the
234*c5c4113dSnw141292 ** new format, but we have to be able to read an older journal in order
235*c5c4113dSnw141292 ** to rollback journals created by older versions of the library.
236*c5c4113dSnw141292 **
237*c5c4113dSnw141292 ** The 3rd journal format (added for 2.8.0) adds additional sanity
238*c5c4113dSnw141292 ** checking information to the journal.  If the power fails while the
239*c5c4113dSnw141292 ** journal is being written, semi-random garbage data might appear in
240*c5c4113dSnw141292 ** the journal file after power is restored.  If an attempt is then made
241*c5c4113dSnw141292 ** to roll the journal back, the database could be corrupted.  The additional
242*c5c4113dSnw141292 ** sanity checking data is an attempt to discover the garbage in the
243*c5c4113dSnw141292 ** journal and ignore it.
244*c5c4113dSnw141292 **
245*c5c4113dSnw141292 ** The sanity checking information for the 3rd journal format consists
246*c5c4113dSnw141292 ** of a 32-bit checksum on each page of data.  The checksum covers both
247*c5c4113dSnw141292 ** the page number and the SQLITE_PAGE_SIZE bytes of data for the page.
248*c5c4113dSnw141292 ** This cksum is initialized to a 32-bit random value that appears in the
249*c5c4113dSnw141292 ** journal file right after the header.  The random initializer is important,
250*c5c4113dSnw141292 ** because garbage data that appears at the end of a journal is likely
251*c5c4113dSnw141292 ** data that was once in other files that have now been deleted.  If the
252*c5c4113dSnw141292 ** garbage data came from an obsolete journal file, the checksums might
253*c5c4113dSnw141292 ** be correct.  But by initializing the checksum to random value which
254*c5c4113dSnw141292 ** is different for every journal, we minimize that risk.
255*c5c4113dSnw141292 */
256*c5c4113dSnw141292 static const unsigned char aJournalMagic1[] = {
257*c5c4113dSnw141292   0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
258*c5c4113dSnw141292 };
259*c5c4113dSnw141292 static const unsigned char aJournalMagic2[] = {
260*c5c4113dSnw141292   0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5,
261*c5c4113dSnw141292 };
262*c5c4113dSnw141292 static const unsigned char aJournalMagic3[] = {
263*c5c4113dSnw141292   0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd6,
264*c5c4113dSnw141292 };
265*c5c4113dSnw141292 #define JOURNAL_FORMAT_1 1
266*c5c4113dSnw141292 #define JOURNAL_FORMAT_2 2
267*c5c4113dSnw141292 #define JOURNAL_FORMAT_3 3
268*c5c4113dSnw141292 
269*c5c4113dSnw141292 /*
270*c5c4113dSnw141292 ** The following integer determines what format to use when creating
271*c5c4113dSnw141292 ** new primary journal files.  By default we always use format 3.
272*c5c4113dSnw141292 ** When testing, we can set this value to older journal formats in order to
273*c5c4113dSnw141292 ** make sure that newer versions of the library are able to rollback older
274*c5c4113dSnw141292 ** journal files.
275*c5c4113dSnw141292 **
276*c5c4113dSnw141292 ** Note that checkpoint journals always use format 2 and omit the header.
277*c5c4113dSnw141292 */
278*c5c4113dSnw141292 #ifdef SQLITE_TEST
279*c5c4113dSnw141292 int journal_format = 3;
280*c5c4113dSnw141292 #else
281*c5c4113dSnw141292 # define journal_format 3
282*c5c4113dSnw141292 #endif
283*c5c4113dSnw141292 
284*c5c4113dSnw141292 /*
285*c5c4113dSnw141292 ** The size of the header and of each page in the journal varies according
286*c5c4113dSnw141292 ** to which journal format is being used.  The following macros figure out
287*c5c4113dSnw141292 ** the sizes based on format numbers.
288*c5c4113dSnw141292 */
289*c5c4113dSnw141292 #define JOURNAL_HDR_SZ(X) \
290*c5c4113dSnw141292    (sizeof(aJournalMagic1) + sizeof(Pgno) + ((X)>=3)*2*sizeof(u32))
291*c5c4113dSnw141292 #define JOURNAL_PG_SZ(X) \
292*c5c4113dSnw141292    (SQLITE_PAGE_SIZE + sizeof(Pgno) + ((X)>=3)*sizeof(u32))
293*c5c4113dSnw141292 
294*c5c4113dSnw141292 /*
295*c5c4113dSnw141292 ** Enable reference count tracking here:
296*c5c4113dSnw141292 */
297*c5c4113dSnw141292 #ifdef SQLITE_TEST
298*c5c4113dSnw141292   int pager_refinfo_enable = 0;
pager_refinfo(PgHdr * p)299*c5c4113dSnw141292   static void pager_refinfo(PgHdr *p){
300*c5c4113dSnw141292     static int cnt = 0;
301*c5c4113dSnw141292     if( !pager_refinfo_enable ) return;
302*c5c4113dSnw141292     printf(
303*c5c4113dSnw141292        "REFCNT: %4d addr=0x%08x nRef=%d\n",
304*c5c4113dSnw141292        p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
305*c5c4113dSnw141292     );
306*c5c4113dSnw141292     cnt++;   /* Something to set a breakpoint on */
307*c5c4113dSnw141292   }
308*c5c4113dSnw141292 # define REFINFO(X)  pager_refinfo(X)
309*c5c4113dSnw141292 #else
310*c5c4113dSnw141292 # define REFINFO(X)
311*c5c4113dSnw141292 #endif
312*c5c4113dSnw141292 
313*c5c4113dSnw141292 /*
314*c5c4113dSnw141292 ** Read a 32-bit integer from the given file descriptor.  Store the integer
315*c5c4113dSnw141292 ** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
316*c5c4113dSnw141292 ** error code is something goes wrong.
317*c5c4113dSnw141292 **
318*c5c4113dSnw141292 ** If the journal format is 2 or 3, read a big-endian integer.  If the
319*c5c4113dSnw141292 ** journal format is 1, read an integer in the native byte-order of the
320*c5c4113dSnw141292 ** host machine.
321*c5c4113dSnw141292 */
read32bits(int format,OsFile * fd,u32 * pRes)322*c5c4113dSnw141292 static int read32bits(int format, OsFile *fd, u32 *pRes){
323*c5c4113dSnw141292   u32 res;
324*c5c4113dSnw141292   int rc;
325*c5c4113dSnw141292   rc = sqliteOsRead(fd, &res, sizeof(res));
326*c5c4113dSnw141292   if( rc==SQLITE_OK && format>JOURNAL_FORMAT_1 ){
327*c5c4113dSnw141292     unsigned char ac[4];
328*c5c4113dSnw141292     memcpy(ac, &res, 4);
329*c5c4113dSnw141292     res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
330*c5c4113dSnw141292   }
331*c5c4113dSnw141292   *pRes = res;
332*c5c4113dSnw141292   return rc;
333*c5c4113dSnw141292 }
334*c5c4113dSnw141292 
335*c5c4113dSnw141292 /*
336*c5c4113dSnw141292 ** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
337*c5c4113dSnw141292 ** on success or an error code is something goes wrong.
338*c5c4113dSnw141292 **
339*c5c4113dSnw141292 ** If the journal format is 2 or 3, write the integer as 4 big-endian
340*c5c4113dSnw141292 ** bytes.  If the journal format is 1, write the integer in the native
341*c5c4113dSnw141292 ** byte order.  In normal operation, only formats 2 and 3 are used.
342*c5c4113dSnw141292 ** Journal format 1 is only used for testing.
343*c5c4113dSnw141292 */
write32bits(OsFile * fd,u32 val)344*c5c4113dSnw141292 static int write32bits(OsFile *fd, u32 val){
345*c5c4113dSnw141292   unsigned char ac[4];
346*c5c4113dSnw141292   if( journal_format<=1 ){
347*c5c4113dSnw141292     return sqliteOsWrite(fd, &val, 4);
348*c5c4113dSnw141292   }
349*c5c4113dSnw141292   ac[0] = (val>>24) & 0xff;
350*c5c4113dSnw141292   ac[1] = (val>>16) & 0xff;
351*c5c4113dSnw141292   ac[2] = (val>>8) & 0xff;
352*c5c4113dSnw141292   ac[3] = val & 0xff;
353*c5c4113dSnw141292   return sqliteOsWrite(fd, ac, 4);
354*c5c4113dSnw141292 }
355*c5c4113dSnw141292 
356*c5c4113dSnw141292 /*
357*c5c4113dSnw141292 ** Write a 32-bit integer into a page header right before the
358*c5c4113dSnw141292 ** page data.  This will overwrite the PgHdr.pDirty pointer.
359*c5c4113dSnw141292 **
360*c5c4113dSnw141292 ** The integer is big-endian for formats 2 and 3 and native byte order
361*c5c4113dSnw141292 ** for journal format 1.
362*c5c4113dSnw141292 */
store32bits(u32 val,PgHdr * p,int offset)363*c5c4113dSnw141292 static void store32bits(u32 val, PgHdr *p, int offset){
364*c5c4113dSnw141292   unsigned char *ac;
365*c5c4113dSnw141292   ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset];
366*c5c4113dSnw141292   if( journal_format<=1 ){
367*c5c4113dSnw141292     memcpy(ac, &val, 4);
368*c5c4113dSnw141292   }else{
369*c5c4113dSnw141292     ac[0] = (val>>24) & 0xff;
370*c5c4113dSnw141292     ac[1] = (val>>16) & 0xff;
371*c5c4113dSnw141292     ac[2] = (val>>8) & 0xff;
372*c5c4113dSnw141292     ac[3] = val & 0xff;
373*c5c4113dSnw141292   }
374*c5c4113dSnw141292 }
375*c5c4113dSnw141292 
376*c5c4113dSnw141292 
377*c5c4113dSnw141292 /*
378*c5c4113dSnw141292 ** Convert the bits in the pPager->errMask into an approprate
379*c5c4113dSnw141292 ** return code.
380*c5c4113dSnw141292 */
pager_errcode(Pager * pPager)381*c5c4113dSnw141292 static int pager_errcode(Pager *pPager){
382*c5c4113dSnw141292   int rc = SQLITE_OK;
383*c5c4113dSnw141292   if( pPager->errMask & PAGER_ERR_LOCK )    rc = SQLITE_PROTOCOL;
384*c5c4113dSnw141292   if( pPager->errMask & PAGER_ERR_DISK )    rc = SQLITE_IOERR;
385*c5c4113dSnw141292   if( pPager->errMask & PAGER_ERR_FULL )    rc = SQLITE_FULL;
386*c5c4113dSnw141292   if( pPager->errMask & PAGER_ERR_MEM )     rc = SQLITE_NOMEM;
387*c5c4113dSnw141292   if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
388*c5c4113dSnw141292   return rc;
389*c5c4113dSnw141292 }
390*c5c4113dSnw141292 
391*c5c4113dSnw141292 /*
392*c5c4113dSnw141292 ** Add or remove a page from the list of all pages that are in the
393*c5c4113dSnw141292 ** checkpoint journal.
394*c5c4113dSnw141292 **
395*c5c4113dSnw141292 ** The Pager keeps a separate list of pages that are currently in
396*c5c4113dSnw141292 ** the checkpoint journal.  This helps the sqlitepager_ckpt_commit()
397*c5c4113dSnw141292 ** routine run MUCH faster for the common case where there are many
398*c5c4113dSnw141292 ** pages in memory but only a few are in the checkpoint journal.
399*c5c4113dSnw141292 */
page_add_to_ckpt_list(PgHdr * pPg)400*c5c4113dSnw141292 static void page_add_to_ckpt_list(PgHdr *pPg){
401*c5c4113dSnw141292   Pager *pPager = pPg->pPager;
402*c5c4113dSnw141292   if( pPg->inCkpt ) return;
403*c5c4113dSnw141292   assert( pPg->pPrevCkpt==0 && pPg->pNextCkpt==0 );
404*c5c4113dSnw141292   pPg->pPrevCkpt = 0;
405*c5c4113dSnw141292   if( pPager->pCkpt ){
406*c5c4113dSnw141292     pPager->pCkpt->pPrevCkpt = pPg;
407*c5c4113dSnw141292   }
408*c5c4113dSnw141292   pPg->pNextCkpt = pPager->pCkpt;
409*c5c4113dSnw141292   pPager->pCkpt = pPg;
410*c5c4113dSnw141292   pPg->inCkpt = 1;
411*c5c4113dSnw141292 }
page_remove_from_ckpt_list(PgHdr * pPg)412*c5c4113dSnw141292 static void page_remove_from_ckpt_list(PgHdr *pPg){
413*c5c4113dSnw141292   if( !pPg->inCkpt ) return;
414*c5c4113dSnw141292   if( pPg->pPrevCkpt ){
415*c5c4113dSnw141292     assert( pPg->pPrevCkpt->pNextCkpt==pPg );
416*c5c4113dSnw141292     pPg->pPrevCkpt->pNextCkpt = pPg->pNextCkpt;
417*c5c4113dSnw141292   }else{
418*c5c4113dSnw141292     assert( pPg->pPager->pCkpt==pPg );
419*c5c4113dSnw141292     pPg->pPager->pCkpt = pPg->pNextCkpt;
420*c5c4113dSnw141292   }
421*c5c4113dSnw141292   if( pPg->pNextCkpt ){
422*c5c4113dSnw141292     assert( pPg->pNextCkpt->pPrevCkpt==pPg );
423*c5c4113dSnw141292     pPg->pNextCkpt->pPrevCkpt = pPg->pPrevCkpt;
424*c5c4113dSnw141292   }
425*c5c4113dSnw141292   pPg->pNextCkpt = 0;
426*c5c4113dSnw141292   pPg->pPrevCkpt = 0;
427*c5c4113dSnw141292   pPg->inCkpt = 0;
428*c5c4113dSnw141292 }
429*c5c4113dSnw141292 
430*c5c4113dSnw141292 /*
431*c5c4113dSnw141292 ** Find a page in the hash table given its page number.  Return
432*c5c4113dSnw141292 ** a pointer to the page or NULL if not found.
433*c5c4113dSnw141292 */
pager_lookup(Pager * pPager,Pgno pgno)434*c5c4113dSnw141292 static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
435*c5c4113dSnw141292   PgHdr *p = pPager->aHash[pager_hash(pgno)];
436*c5c4113dSnw141292   while( p && p->pgno!=pgno ){
437*c5c4113dSnw141292     p = p->pNextHash;
438*c5c4113dSnw141292   }
439*c5c4113dSnw141292   return p;
440*c5c4113dSnw141292 }
441*c5c4113dSnw141292 
442*c5c4113dSnw141292 /*
443*c5c4113dSnw141292 ** Unlock the database and clear the in-memory cache.  This routine
444*c5c4113dSnw141292 ** sets the state of the pager back to what it was when it was first
445*c5c4113dSnw141292 ** opened.  Any outstanding pages are invalidated and subsequent attempts
446*c5c4113dSnw141292 ** to access those pages will likely result in a coredump.
447*c5c4113dSnw141292 */
pager_reset(Pager * pPager)448*c5c4113dSnw141292 static void pager_reset(Pager *pPager){
449*c5c4113dSnw141292   PgHdr *pPg, *pNext;
450*c5c4113dSnw141292   for(pPg=pPager->pAll; pPg; pPg=pNext){
451*c5c4113dSnw141292     pNext = pPg->pNextAll;
452*c5c4113dSnw141292     sqliteFree(pPg);
453*c5c4113dSnw141292   }
454*c5c4113dSnw141292   pPager->pFirst = 0;
455*c5c4113dSnw141292   pPager->pFirstSynced = 0;
456*c5c4113dSnw141292   pPager->pLast = 0;
457*c5c4113dSnw141292   pPager->pAll = 0;
458*c5c4113dSnw141292   memset(pPager->aHash, 0, sizeof(pPager->aHash));
459*c5c4113dSnw141292   pPager->nPage = 0;
460*c5c4113dSnw141292   if( pPager->state>=SQLITE_WRITELOCK ){
461*c5c4113dSnw141292     sqlitepager_rollback(pPager);
462*c5c4113dSnw141292   }
463*c5c4113dSnw141292   sqliteOsUnlock(&pPager->fd);
464*c5c4113dSnw141292   pPager->state = SQLITE_UNLOCK;
465*c5c4113dSnw141292   pPager->dbSize = -1;
466*c5c4113dSnw141292   pPager->nRef = 0;
467*c5c4113dSnw141292   assert( pPager->journalOpen==0 );
468*c5c4113dSnw141292 }
469*c5c4113dSnw141292 
470*c5c4113dSnw141292 /*
471*c5c4113dSnw141292 ** When this routine is called, the pager has the journal file open and
472*c5c4113dSnw141292 ** a write lock on the database.  This routine releases the database
473*c5c4113dSnw141292 ** write lock and acquires a read lock in its place.  The journal file
474*c5c4113dSnw141292 ** is deleted and closed.
475*c5c4113dSnw141292 **
476*c5c4113dSnw141292 ** TODO: Consider keeping the journal file open for temporary databases.
477*c5c4113dSnw141292 ** This might give a performance improvement on windows where opening
478*c5c4113dSnw141292 ** a file is an expensive operation.
479*c5c4113dSnw141292 */
pager_unwritelock(Pager * pPager)480*c5c4113dSnw141292 static int pager_unwritelock(Pager *pPager){
481*c5c4113dSnw141292   int rc;
482*c5c4113dSnw141292   PgHdr *pPg;
483*c5c4113dSnw141292   if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK;
484*c5c4113dSnw141292   sqlitepager_ckpt_commit(pPager);
485*c5c4113dSnw141292   if( pPager->ckptOpen ){
486*c5c4113dSnw141292     sqliteOsClose(&pPager->cpfd);
487*c5c4113dSnw141292     pPager->ckptOpen = 0;
488*c5c4113dSnw141292   }
489*c5c4113dSnw141292   if( pPager->journalOpen ){
490*c5c4113dSnw141292     sqliteOsClose(&pPager->jfd);
491*c5c4113dSnw141292     pPager->journalOpen = 0;
492*c5c4113dSnw141292     sqliteOsDelete(pPager->zJournal);
493*c5c4113dSnw141292     sqliteFree( pPager->aInJournal );
494*c5c4113dSnw141292     pPager->aInJournal = 0;
495*c5c4113dSnw141292     for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
496*c5c4113dSnw141292       pPg->inJournal = 0;
497*c5c4113dSnw141292       pPg->dirty = 0;
498*c5c4113dSnw141292       pPg->needSync = 0;
499*c5c4113dSnw141292     }
500*c5c4113dSnw141292   }else{
501*c5c4113dSnw141292     assert( pPager->dirtyFile==0 || pPager->useJournal==0 );
502*c5c4113dSnw141292   }
503*c5c4113dSnw141292   rc = sqliteOsReadLock(&pPager->fd);
504*c5c4113dSnw141292   if( rc==SQLITE_OK ){
505*c5c4113dSnw141292     pPager->state = SQLITE_READLOCK;
506*c5c4113dSnw141292   }else{
507*c5c4113dSnw141292     /* This can only happen if a process does a BEGIN, then forks and the
508*c5c4113dSnw141292     ** child process does the COMMIT.  Because of the semantics of unix
509*c5c4113dSnw141292     ** file locking, the unlock will fail.
510*c5c4113dSnw141292     */
511*c5c4113dSnw141292     pPager->state = SQLITE_UNLOCK;
512*c5c4113dSnw141292   }
513*c5c4113dSnw141292   return rc;
514*c5c4113dSnw141292 }
515*c5c4113dSnw141292 
516*c5c4113dSnw141292 /*
517*c5c4113dSnw141292 ** Compute and return a checksum for the page of data.
518*c5c4113dSnw141292 **
519*c5c4113dSnw141292 ** This is not a real checksum.  It is really just the sum of the
520*c5c4113dSnw141292 ** random initial value and the page number.  We considered do a checksum
521*c5c4113dSnw141292 ** of the database, but that was found to be too slow.
522*c5c4113dSnw141292 */
pager_cksum(Pager * pPager,Pgno pgno,const char * aData)523*c5c4113dSnw141292 static u32 pager_cksum(Pager *pPager, Pgno pgno, const char *aData){
524*c5c4113dSnw141292   u32 cksum = pPager->cksumInit + pgno;
525*c5c4113dSnw141292   return cksum;
526*c5c4113dSnw141292 }
527*c5c4113dSnw141292 
528*c5c4113dSnw141292 /*
529*c5c4113dSnw141292 ** Read a single page from the journal file opened on file descriptor
530*c5c4113dSnw141292 ** jfd.  Playback this one page.
531*c5c4113dSnw141292 **
532*c5c4113dSnw141292 ** There are three different journal formats.  The format parameter determines
533*c5c4113dSnw141292 ** which format is used by the journal that is played back.
534*c5c4113dSnw141292 */
pager_playback_one_page(Pager * pPager,OsFile * jfd,int format)535*c5c4113dSnw141292 static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int format){
536*c5c4113dSnw141292   int rc;
537*c5c4113dSnw141292   PgHdr *pPg;              /* An existing page in the cache */
538*c5c4113dSnw141292   PageRecord pgRec;
539*c5c4113dSnw141292   u32 cksum;
540*c5c4113dSnw141292 
541*c5c4113dSnw141292   rc = read32bits(format, jfd, &pgRec.pgno);
542*c5c4113dSnw141292   if( rc!=SQLITE_OK ) return rc;
543*c5c4113dSnw141292   rc = sqliteOsRead(jfd, &pgRec.aData, sizeof(pgRec.aData));
544*c5c4113dSnw141292   if( rc!=SQLITE_OK ) return rc;
545*c5c4113dSnw141292 
546*c5c4113dSnw141292   /* Sanity checking on the page.  This is more important that I originally
547*c5c4113dSnw141292   ** thought.  If a power failure occurs while the journal is being written,
548*c5c4113dSnw141292   ** it could cause invalid data to be written into the journal.  We need to
549*c5c4113dSnw141292   ** detect this invalid data (with high probability) and ignore it.
550*c5c4113dSnw141292   */
551*c5c4113dSnw141292   if( pgRec.pgno==0 ){
552*c5c4113dSnw141292     return SQLITE_DONE;
553*c5c4113dSnw141292   }
554*c5c4113dSnw141292   if( pgRec.pgno>(unsigned)pPager->dbSize ){
555*c5c4113dSnw141292     return SQLITE_OK;
556*c5c4113dSnw141292   }
557*c5c4113dSnw141292   if( format>=JOURNAL_FORMAT_3 ){
558*c5c4113dSnw141292     rc = read32bits(format, jfd, &cksum);
559*c5c4113dSnw141292     if( rc ) return rc;
560*c5c4113dSnw141292     if( pager_cksum(pPager, pgRec.pgno, pgRec.aData)!=cksum ){
561*c5c4113dSnw141292       return SQLITE_DONE;
562*c5c4113dSnw141292     }
563*c5c4113dSnw141292   }
564*c5c4113dSnw141292 
565*c5c4113dSnw141292   /* Playback the page.  Update the in-memory copy of the page
566*c5c4113dSnw141292   ** at the same time, if there is one.
567*c5c4113dSnw141292   */
568*c5c4113dSnw141292   pPg = pager_lookup(pPager, pgRec.pgno);
569*c5c4113dSnw141292   TRACE2("PLAYBACK %d\n", pgRec.pgno);
570*c5c4113dSnw141292   sqliteOsSeek(&pPager->fd, (pgRec.pgno-1)*(off_t)SQLITE_PAGE_SIZE);
571*c5c4113dSnw141292   rc = sqliteOsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
572*c5c4113dSnw141292   if( pPg ){
573*c5c4113dSnw141292     /* No page should ever be rolled back that is in use, except for page
574*c5c4113dSnw141292     ** 1 which is held in use in order to keep the lock on the database
575*c5c4113dSnw141292     ** active.  However, such a page may be rolled back as a result of an
576*c5c4113dSnw141292     ** internal error resulting in an automatic call to
577*c5c4113dSnw141292     ** sqlitepager_rollback(), so we can't assert() it.
578*c5c4113dSnw141292     */
579*c5c4113dSnw141292     /* assert( pPg->nRef==0 || pPg->pgno==1 ) */
580*c5c4113dSnw141292     memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
581*c5c4113dSnw141292     memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
582*c5c4113dSnw141292     pPg->dirty = 0;
583*c5c4113dSnw141292     pPg->needSync = 0;
584*c5c4113dSnw141292     CODEC(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
585*c5c4113dSnw141292   }
586*c5c4113dSnw141292   return rc;
587*c5c4113dSnw141292 }
588*c5c4113dSnw141292 
589*c5c4113dSnw141292 /*
590*c5c4113dSnw141292 ** Playback the journal and thus restore the database file to
591*c5c4113dSnw141292 ** the state it was in before we started making changes.
592*c5c4113dSnw141292 **
593*c5c4113dSnw141292 ** The journal file format is as follows:
594*c5c4113dSnw141292 **
595*c5c4113dSnw141292 **    *  8 byte prefix.  One of the aJournalMagic123 vectors defined
596*c5c4113dSnw141292 **       above.  The format of the journal file is determined by which
597*c5c4113dSnw141292 **       of the three prefix vectors is seen.
598*c5c4113dSnw141292 **    *  4 byte big-endian integer which is the number of valid page records
599*c5c4113dSnw141292 **       in the journal.  If this value is 0xffffffff, then compute the
600*c5c4113dSnw141292 **       number of page records from the journal size.  This field appears
601*c5c4113dSnw141292 **       in format 3 only.
602*c5c4113dSnw141292 **    *  4 byte big-endian integer which is the initial value for the
603*c5c4113dSnw141292 **       sanity checksum.  This field appears in format 3 only.
604*c5c4113dSnw141292 **    *  4 byte integer which is the number of pages to truncate the
605*c5c4113dSnw141292 **       database to during a rollback.
606*c5c4113dSnw141292 **    *  Zero or more pages instances, each as follows:
607*c5c4113dSnw141292 **        +  4 byte page number.
608*c5c4113dSnw141292 **        +  SQLITE_PAGE_SIZE bytes of data.
609*c5c4113dSnw141292 **        +  4 byte checksum (format 3 only)
610*c5c4113dSnw141292 **
611*c5c4113dSnw141292 ** When we speak of the journal header, we mean the first 4 bullets above.
612*c5c4113dSnw141292 ** Each entry in the journal is an instance of the 5th bullet.  Note that
613*c5c4113dSnw141292 ** bullets 2 and 3 only appear in format-3 journals.
614*c5c4113dSnw141292 **
615*c5c4113dSnw141292 ** Call the value from the second bullet "nRec".  nRec is the number of
616*c5c4113dSnw141292 ** valid page entries in the journal.  In most cases, you can compute the
617*c5c4113dSnw141292 ** value of nRec from the size of the journal file.  But if a power
618*c5c4113dSnw141292 ** failure occurred while the journal was being written, it could be the
619*c5c4113dSnw141292 ** case that the size of the journal file had already been increased but
620*c5c4113dSnw141292 ** the extra entries had not yet made it safely to disk.  In such a case,
621*c5c4113dSnw141292 ** the value of nRec computed from the file size would be too large.  For
622*c5c4113dSnw141292 ** that reason, we always use the nRec value in the header.
623*c5c4113dSnw141292 **
624*c5c4113dSnw141292 ** If the nRec value is 0xffffffff it means that nRec should be computed
625*c5c4113dSnw141292 ** from the file size.  This value is used when the user selects the
626*c5c4113dSnw141292 ** no-sync option for the journal.  A power failure could lead to corruption
627*c5c4113dSnw141292 ** in this case.  But for things like temporary table (which will be
628*c5c4113dSnw141292 ** deleted when the power is restored) we don't care.
629*c5c4113dSnw141292 **
630*c5c4113dSnw141292 ** Journal formats 1 and 2 do not have an nRec value in the header so we
631*c5c4113dSnw141292 ** have to compute nRec from the file size.  This has risks (as described
632*c5c4113dSnw141292 ** above) which is why all persistent tables have been changed to use
633*c5c4113dSnw141292 ** format 3.
634*c5c4113dSnw141292 **
635*c5c4113dSnw141292 ** If the file opened as the journal file is not a well-formed
636*c5c4113dSnw141292 ** journal file then the database will likely already be
637*c5c4113dSnw141292 ** corrupted, so the PAGER_ERR_CORRUPT bit is set in pPager->errMask
638*c5c4113dSnw141292 ** and SQLITE_CORRUPT is returned.  If it all works, then this routine
639*c5c4113dSnw141292 ** returns SQLITE_OK.
640*c5c4113dSnw141292 */
pager_playback(Pager * pPager,int useJournalSize)641*c5c4113dSnw141292 static int pager_playback(Pager *pPager, int useJournalSize){
642*c5c4113dSnw141292   off_t szJ;               /* Size of the journal file in bytes */
643*c5c4113dSnw141292   int nRec;                /* Number of Records in the journal */
644*c5c4113dSnw141292   int i;                   /* Loop counter */
645*c5c4113dSnw141292   Pgno mxPg = 0;           /* Size of the original file in pages */
646*c5c4113dSnw141292   int format;              /* Format of the journal file. */
647*c5c4113dSnw141292   unsigned char aMagic[sizeof(aJournalMagic1)];
648*c5c4113dSnw141292   int rc;
649*c5c4113dSnw141292 
650*c5c4113dSnw141292   /* Figure out how many records are in the journal.  Abort early if
651*c5c4113dSnw141292   ** the journal is empty.
652*c5c4113dSnw141292   */
653*c5c4113dSnw141292   assert( pPager->journalOpen );
654*c5c4113dSnw141292   sqliteOsSeek(&pPager->jfd, 0);
655*c5c4113dSnw141292   rc = sqliteOsFileSize(&pPager->jfd, &szJ);
656*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
657*c5c4113dSnw141292     goto end_playback;
658*c5c4113dSnw141292   }
659*c5c4113dSnw141292 
660*c5c4113dSnw141292   /* If the journal file is too small to contain a complete header,
661*c5c4113dSnw141292   ** it must mean that the process that created the journal was just
662*c5c4113dSnw141292   ** beginning to write the journal file when it died.  In that case,
663*c5c4113dSnw141292   ** the database file should have still been completely unchanged.
664*c5c4113dSnw141292   ** Nothing needs to be rolled back.  We can safely ignore this journal.
665*c5c4113dSnw141292   */
666*c5c4113dSnw141292   if( szJ < sizeof(aMagic)+sizeof(Pgno) ){
667*c5c4113dSnw141292     goto end_playback;
668*c5c4113dSnw141292   }
669*c5c4113dSnw141292 
670*c5c4113dSnw141292   /* Read the beginning of the journal and truncate the
671*c5c4113dSnw141292   ** database file back to its original size.
672*c5c4113dSnw141292   */
673*c5c4113dSnw141292   rc = sqliteOsRead(&pPager->jfd, aMagic, sizeof(aMagic));
674*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
675*c5c4113dSnw141292     rc = SQLITE_PROTOCOL;
676*c5c4113dSnw141292     goto end_playback;
677*c5c4113dSnw141292   }
678*c5c4113dSnw141292   if( memcmp(aMagic, aJournalMagic3, sizeof(aMagic))==0 ){
679*c5c4113dSnw141292     format = JOURNAL_FORMAT_3;
680*c5c4113dSnw141292   }else if( memcmp(aMagic, aJournalMagic2, sizeof(aMagic))==0 ){
681*c5c4113dSnw141292     format = JOURNAL_FORMAT_2;
682*c5c4113dSnw141292   }else if( memcmp(aMagic, aJournalMagic1, sizeof(aMagic))==0 ){
683*c5c4113dSnw141292     format = JOURNAL_FORMAT_1;
684*c5c4113dSnw141292   }else{
685*c5c4113dSnw141292     rc = SQLITE_PROTOCOL;
686*c5c4113dSnw141292     goto end_playback;
687*c5c4113dSnw141292   }
688*c5c4113dSnw141292   if( format>=JOURNAL_FORMAT_3 ){
689*c5c4113dSnw141292     if( szJ < sizeof(aMagic) + 3*sizeof(u32) ){
690*c5c4113dSnw141292       /* Ignore the journal if it is too small to contain a complete
691*c5c4113dSnw141292       ** header.  We already did this test once above, but at the prior
692*c5c4113dSnw141292       ** test, we did not know the journal format and so we had to assume
693*c5c4113dSnw141292       ** the smallest possible header.  Now we know the header is bigger
694*c5c4113dSnw141292       ** than the minimum so we test again.
695*c5c4113dSnw141292       */
696*c5c4113dSnw141292       goto end_playback;
697*c5c4113dSnw141292     }
698*c5c4113dSnw141292     rc = read32bits(format, &pPager->jfd, (u32*)&nRec);
699*c5c4113dSnw141292     if( rc ) goto end_playback;
700*c5c4113dSnw141292     rc = read32bits(format, &pPager->jfd, &pPager->cksumInit);
701*c5c4113dSnw141292     if( rc ) goto end_playback;
702*c5c4113dSnw141292     if( nRec==0xffffffff || useJournalSize ){
703*c5c4113dSnw141292       nRec = (szJ - JOURNAL_HDR_SZ(3))/JOURNAL_PG_SZ(3);
704*c5c4113dSnw141292     }
705*c5c4113dSnw141292   }else{
706*c5c4113dSnw141292     nRec = (szJ - JOURNAL_HDR_SZ(2))/JOURNAL_PG_SZ(2);
707*c5c4113dSnw141292     assert( nRec*JOURNAL_PG_SZ(2)+JOURNAL_HDR_SZ(2)==szJ );
708*c5c4113dSnw141292   }
709*c5c4113dSnw141292   rc = read32bits(format, &pPager->jfd, &mxPg);
710*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
711*c5c4113dSnw141292     goto end_playback;
712*c5c4113dSnw141292   }
713*c5c4113dSnw141292   assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg );
714*c5c4113dSnw141292   rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg);
715*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
716*c5c4113dSnw141292     goto end_playback;
717*c5c4113dSnw141292   }
718*c5c4113dSnw141292   pPager->dbSize = mxPg;
719*c5c4113dSnw141292 
720*c5c4113dSnw141292   /* Copy original pages out of the journal and back into the database file.
721*c5c4113dSnw141292   */
722*c5c4113dSnw141292   for(i=0; i<nRec; i++){
723*c5c4113dSnw141292     rc = pager_playback_one_page(pPager, &pPager->jfd, format);
724*c5c4113dSnw141292     if( rc!=SQLITE_OK ){
725*c5c4113dSnw141292       if( rc==SQLITE_DONE ){
726*c5c4113dSnw141292         rc = SQLITE_OK;
727*c5c4113dSnw141292       }
728*c5c4113dSnw141292       break;
729*c5c4113dSnw141292     }
730*c5c4113dSnw141292   }
731*c5c4113dSnw141292 
732*c5c4113dSnw141292   /* Pages that have been written to the journal but never synced
733*c5c4113dSnw141292   ** where not restored by the loop above.  We have to restore those
734*c5c4113dSnw141292   ** pages by reading them back from the original database.
735*c5c4113dSnw141292   */
736*c5c4113dSnw141292   if( rc==SQLITE_OK ){
737*c5c4113dSnw141292     PgHdr *pPg;
738*c5c4113dSnw141292     for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
739*c5c4113dSnw141292       char zBuf[SQLITE_PAGE_SIZE];
740*c5c4113dSnw141292       if( !pPg->dirty ) continue;
741*c5c4113dSnw141292       if( (int)pPg->pgno <= pPager->origDbSize ){
742*c5c4113dSnw141292         sqliteOsSeek(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)(pPg->pgno-1));
743*c5c4113dSnw141292         rc = sqliteOsRead(&pPager->fd, zBuf, SQLITE_PAGE_SIZE);
744*c5c4113dSnw141292         TRACE2("REFETCH %d\n", pPg->pgno);
745*c5c4113dSnw141292         CODEC(pPager, zBuf, pPg->pgno, 2);
746*c5c4113dSnw141292         if( rc ) break;
747*c5c4113dSnw141292       }else{
748*c5c4113dSnw141292         memset(zBuf, 0, SQLITE_PAGE_SIZE);
749*c5c4113dSnw141292       }
750*c5c4113dSnw141292       if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE) ){
751*c5c4113dSnw141292         memcpy(PGHDR_TO_DATA(pPg), zBuf, SQLITE_PAGE_SIZE);
752*c5c4113dSnw141292         memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
753*c5c4113dSnw141292       }
754*c5c4113dSnw141292       pPg->needSync = 0;
755*c5c4113dSnw141292       pPg->dirty = 0;
756*c5c4113dSnw141292     }
757*c5c4113dSnw141292   }
758*c5c4113dSnw141292 
759*c5c4113dSnw141292 end_playback:
760*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
761*c5c4113dSnw141292     pager_unwritelock(pPager);
762*c5c4113dSnw141292     pPager->errMask |= PAGER_ERR_CORRUPT;
763*c5c4113dSnw141292     rc = SQLITE_CORRUPT;
764*c5c4113dSnw141292   }else{
765*c5c4113dSnw141292     rc = pager_unwritelock(pPager);
766*c5c4113dSnw141292   }
767*c5c4113dSnw141292   return rc;
768*c5c4113dSnw141292 }
769*c5c4113dSnw141292 
770*c5c4113dSnw141292 /*
771*c5c4113dSnw141292 ** Playback the checkpoint journal.
772*c5c4113dSnw141292 **
773*c5c4113dSnw141292 ** This is similar to playing back the transaction journal but with
774*c5c4113dSnw141292 ** a few extra twists.
775*c5c4113dSnw141292 **
776*c5c4113dSnw141292 **    (1)  The number of pages in the database file at the start of
777*c5c4113dSnw141292 **         the checkpoint is stored in pPager->ckptSize, not in the
778*c5c4113dSnw141292 **         journal file itself.
779*c5c4113dSnw141292 **
780*c5c4113dSnw141292 **    (2)  In addition to playing back the checkpoint journal, also
781*c5c4113dSnw141292 **         playback all pages of the transaction journal beginning
782*c5c4113dSnw141292 **         at offset pPager->ckptJSize.
783*c5c4113dSnw141292 */
pager_ckpt_playback(Pager * pPager)784*c5c4113dSnw141292 static int pager_ckpt_playback(Pager *pPager){
785*c5c4113dSnw141292   off_t szJ;               /* Size of the full journal */
786*c5c4113dSnw141292   int nRec;                /* Number of Records */
787*c5c4113dSnw141292   int i;                   /* Loop counter */
788*c5c4113dSnw141292   int rc;
789*c5c4113dSnw141292 
790*c5c4113dSnw141292   /* Truncate the database back to its original size.
791*c5c4113dSnw141292   */
792*c5c4113dSnw141292   rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)pPager->ckptSize);
793*c5c4113dSnw141292   pPager->dbSize = pPager->ckptSize;
794*c5c4113dSnw141292 
795*c5c4113dSnw141292   /* Figure out how many records are in the checkpoint journal.
796*c5c4113dSnw141292   */
797*c5c4113dSnw141292   assert( pPager->ckptInUse && pPager->journalOpen );
798*c5c4113dSnw141292   sqliteOsSeek(&pPager->cpfd, 0);
799*c5c4113dSnw141292   nRec = pPager->ckptNRec;
800*c5c4113dSnw141292 
801*c5c4113dSnw141292   /* Copy original pages out of the checkpoint journal and back into the
802*c5c4113dSnw141292   ** database file.  Note that the checkpoint journal always uses format
803*c5c4113dSnw141292   ** 2 instead of format 3 since it does not need to be concerned with
804*c5c4113dSnw141292   ** power failures corrupting the journal and can thus omit the checksums.
805*c5c4113dSnw141292   */
806*c5c4113dSnw141292   for(i=nRec-1; i>=0; i--){
807*c5c4113dSnw141292     rc = pager_playback_one_page(pPager, &pPager->cpfd, 2);
808*c5c4113dSnw141292     assert( rc!=SQLITE_DONE );
809*c5c4113dSnw141292     if( rc!=SQLITE_OK ) goto end_ckpt_playback;
810*c5c4113dSnw141292   }
811*c5c4113dSnw141292 
812*c5c4113dSnw141292   /* Figure out how many pages need to be copied out of the transaction
813*c5c4113dSnw141292   ** journal.
814*c5c4113dSnw141292   */
815*c5c4113dSnw141292   rc = sqliteOsSeek(&pPager->jfd, pPager->ckptJSize);
816*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
817*c5c4113dSnw141292     goto end_ckpt_playback;
818*c5c4113dSnw141292   }
819*c5c4113dSnw141292   rc = sqliteOsFileSize(&pPager->jfd, &szJ);
820*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
821*c5c4113dSnw141292     goto end_ckpt_playback;
822*c5c4113dSnw141292   }
823*c5c4113dSnw141292   nRec = (szJ - pPager->ckptJSize)/JOURNAL_PG_SZ(journal_format);
824*c5c4113dSnw141292   for(i=nRec-1; i>=0; i--){
825*c5c4113dSnw141292     rc = pager_playback_one_page(pPager, &pPager->jfd, journal_format);
826*c5c4113dSnw141292     if( rc!=SQLITE_OK ){
827*c5c4113dSnw141292       assert( rc!=SQLITE_DONE );
828*c5c4113dSnw141292       goto end_ckpt_playback;
829*c5c4113dSnw141292     }
830*c5c4113dSnw141292   }
831*c5c4113dSnw141292 
832*c5c4113dSnw141292 end_ckpt_playback:
833*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
834*c5c4113dSnw141292     pPager->errMask |= PAGER_ERR_CORRUPT;
835*c5c4113dSnw141292     rc = SQLITE_CORRUPT;
836*c5c4113dSnw141292   }
837*c5c4113dSnw141292   return rc;
838*c5c4113dSnw141292 }
839*c5c4113dSnw141292 
840*c5c4113dSnw141292 /*
841*c5c4113dSnw141292 ** Change the maximum number of in-memory pages that are allowed.
842*c5c4113dSnw141292 **
843*c5c4113dSnw141292 ** The maximum number is the absolute value of the mxPage parameter.
844*c5c4113dSnw141292 ** If mxPage is negative, the noSync flag is also set.  noSync bypasses
845*c5c4113dSnw141292 ** calls to sqliteOsSync().  The pager runs much faster with noSync on,
846*c5c4113dSnw141292 ** but if the operating system crashes or there is an abrupt power
847*c5c4113dSnw141292 ** failure, the database file might be left in an inconsistent and
848*c5c4113dSnw141292 ** unrepairable state.
849*c5c4113dSnw141292 */
sqlitepager_set_cachesize(Pager * pPager,int mxPage)850*c5c4113dSnw141292 void sqlitepager_set_cachesize(Pager *pPager, int mxPage){
851*c5c4113dSnw141292   if( mxPage>=0 ){
852*c5c4113dSnw141292     pPager->noSync = pPager->tempFile;
853*c5c4113dSnw141292     if( pPager->noSync==0 ) pPager->needSync = 0;
854*c5c4113dSnw141292   }else{
855*c5c4113dSnw141292     pPager->noSync = 1;
856*c5c4113dSnw141292     mxPage = -mxPage;
857*c5c4113dSnw141292   }
858*c5c4113dSnw141292   if( mxPage>10 ){
859*c5c4113dSnw141292     pPager->mxPage = mxPage;
860*c5c4113dSnw141292   }
861*c5c4113dSnw141292 }
862*c5c4113dSnw141292 
863*c5c4113dSnw141292 /*
864*c5c4113dSnw141292 ** Adjust the robustness of the database to damage due to OS crashes
865*c5c4113dSnw141292 ** or power failures by changing the number of syncs()s when writing
866*c5c4113dSnw141292 ** the rollback journal.  There are three levels:
867*c5c4113dSnw141292 **
868*c5c4113dSnw141292 **    OFF       sqliteOsSync() is never called.  This is the default
869*c5c4113dSnw141292 **              for temporary and transient files.
870*c5c4113dSnw141292 **
871*c5c4113dSnw141292 **    NORMAL    The journal is synced once before writes begin on the
872*c5c4113dSnw141292 **              database.  This is normally adequate protection, but
873*c5c4113dSnw141292 **              it is theoretically possible, though very unlikely,
874*c5c4113dSnw141292 **              that an inopertune power failure could leave the journal
875*c5c4113dSnw141292 **              in a state which would cause damage to the database
876*c5c4113dSnw141292 **              when it is rolled back.
877*c5c4113dSnw141292 **
878*c5c4113dSnw141292 **    FULL      The journal is synced twice before writes begin on the
879*c5c4113dSnw141292 **              database (with some additional information - the nRec field
880*c5c4113dSnw141292 **              of the journal header - being written in between the two
881*c5c4113dSnw141292 **              syncs).  If we assume that writing a
882*c5c4113dSnw141292 **              single disk sector is atomic, then this mode provides
883*c5c4113dSnw141292 **              assurance that the journal will not be corrupted to the
884*c5c4113dSnw141292 **              point of causing damage to the database during rollback.
885*c5c4113dSnw141292 **
886*c5c4113dSnw141292 ** Numeric values associated with these states are OFF==1, NORMAL=2,
887*c5c4113dSnw141292 ** and FULL=3.
888*c5c4113dSnw141292 */
sqlitepager_set_safety_level(Pager * pPager,int level)889*c5c4113dSnw141292 void sqlitepager_set_safety_level(Pager *pPager, int level){
890*c5c4113dSnw141292   pPager->noSync =  level==1 || pPager->tempFile;
891*c5c4113dSnw141292   pPager->fullSync = level==3 && !pPager->tempFile;
892*c5c4113dSnw141292   if( pPager->noSync==0 ) pPager->needSync = 0;
893*c5c4113dSnw141292 }
894*c5c4113dSnw141292 
895*c5c4113dSnw141292 /*
896*c5c4113dSnw141292 ** Open a temporary file.  Write the name of the file into zName
897*c5c4113dSnw141292 ** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.)  Write
898*c5c4113dSnw141292 ** the file descriptor into *fd.  Return SQLITE_OK on success or some
899*c5c4113dSnw141292 ** other error code if we fail.
900*c5c4113dSnw141292 **
901*c5c4113dSnw141292 ** The OS will automatically delete the temporary file when it is
902*c5c4113dSnw141292 ** closed.
903*c5c4113dSnw141292 */
sqlitepager_opentemp(char * zFile,OsFile * fd)904*c5c4113dSnw141292 static int sqlitepager_opentemp(char *zFile, OsFile *fd){
905*c5c4113dSnw141292   int cnt = 8;
906*c5c4113dSnw141292   int rc;
907*c5c4113dSnw141292   do{
908*c5c4113dSnw141292     cnt--;
909*c5c4113dSnw141292     sqliteOsTempFileName(zFile);
910*c5c4113dSnw141292     rc = sqliteOsOpenExclusive(zFile, fd, 1);
911*c5c4113dSnw141292   }while( cnt>0 && rc!=SQLITE_OK );
912*c5c4113dSnw141292   return rc;
913*c5c4113dSnw141292 }
914*c5c4113dSnw141292 
915*c5c4113dSnw141292 /*
916*c5c4113dSnw141292 ** Create a new page cache and put a pointer to the page cache in *ppPager.
917*c5c4113dSnw141292 ** The file to be cached need not exist.  The file is not locked until
918*c5c4113dSnw141292 ** the first call to sqlitepager_get() and is only held open until the
919*c5c4113dSnw141292 ** last page is released using sqlitepager_unref().
920*c5c4113dSnw141292 **
921*c5c4113dSnw141292 ** If zFilename is NULL then a randomly-named temporary file is created
922*c5c4113dSnw141292 ** and used as the file to be cached.  The file will be deleted
923*c5c4113dSnw141292 ** automatically when it is closed.
924*c5c4113dSnw141292 */
sqlitepager_open(Pager ** ppPager,const char * zFilename,int mxPage,int nExtra,int useJournal)925*c5c4113dSnw141292 int sqlitepager_open(
926*c5c4113dSnw141292   Pager **ppPager,         /* Return the Pager structure here */
927*c5c4113dSnw141292   const char *zFilename,   /* Name of the database file to open */
928*c5c4113dSnw141292   int mxPage,              /* Max number of in-memory cache pages */
929*c5c4113dSnw141292   int nExtra,              /* Extra bytes append to each in-memory page */
930*c5c4113dSnw141292   int useJournal           /* TRUE to use a rollback journal on this file */
931*c5c4113dSnw141292 ){
932*c5c4113dSnw141292   Pager *pPager;
933*c5c4113dSnw141292   char *zFullPathname;
934*c5c4113dSnw141292   int nameLen;
935*c5c4113dSnw141292   OsFile fd;
936*c5c4113dSnw141292   int rc, i;
937*c5c4113dSnw141292   int tempFile;
938*c5c4113dSnw141292   int readOnly = 0;
939*c5c4113dSnw141292   char zTemp[SQLITE_TEMPNAME_SIZE];
940*c5c4113dSnw141292 
941*c5c4113dSnw141292   *ppPager = 0;
942*c5c4113dSnw141292   if( sqlite_malloc_failed ){
943*c5c4113dSnw141292     return SQLITE_NOMEM;
944*c5c4113dSnw141292   }
945*c5c4113dSnw141292   if( zFilename && zFilename[0] ){
946*c5c4113dSnw141292     zFullPathname = sqliteOsFullPathname(zFilename);
947*c5c4113dSnw141292     rc = sqliteOsOpenReadWrite(zFullPathname, &fd, &readOnly);
948*c5c4113dSnw141292     tempFile = 0;
949*c5c4113dSnw141292   }else{
950*c5c4113dSnw141292     rc = sqlitepager_opentemp(zTemp, &fd);
951*c5c4113dSnw141292     zFilename = zTemp;
952*c5c4113dSnw141292     zFullPathname = sqliteOsFullPathname(zFilename);
953*c5c4113dSnw141292     tempFile = 1;
954*c5c4113dSnw141292   }
955*c5c4113dSnw141292   if( sqlite_malloc_failed ){
956*c5c4113dSnw141292     return SQLITE_NOMEM;
957*c5c4113dSnw141292   }
958*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
959*c5c4113dSnw141292     sqliteFree(zFullPathname);
960*c5c4113dSnw141292     return SQLITE_CANTOPEN;
961*c5c4113dSnw141292   }
962*c5c4113dSnw141292   nameLen = strlen(zFullPathname);
963*c5c4113dSnw141292   pPager = sqliteMalloc( sizeof(*pPager) + nameLen*3 + 30 );
964*c5c4113dSnw141292   if( pPager==0 ){
965*c5c4113dSnw141292     sqliteOsClose(&fd);
966*c5c4113dSnw141292     sqliteFree(zFullPathname);
967*c5c4113dSnw141292     return SQLITE_NOMEM;
968*c5c4113dSnw141292   }
969*c5c4113dSnw141292   SET_PAGER(pPager);
970*c5c4113dSnw141292   pPager->zFilename = (char*)&pPager[1];
971*c5c4113dSnw141292   pPager->zDirectory = &pPager->zFilename[nameLen+1];
972*c5c4113dSnw141292   pPager->zJournal = &pPager->zDirectory[nameLen+1];
973*c5c4113dSnw141292   strcpy(pPager->zFilename, zFullPathname);
974*c5c4113dSnw141292   strcpy(pPager->zDirectory, zFullPathname);
975*c5c4113dSnw141292   for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){}
976*c5c4113dSnw141292   if( i>0 ) pPager->zDirectory[i-1] = 0;
977*c5c4113dSnw141292   strcpy(pPager->zJournal, zFullPathname);
978*c5c4113dSnw141292   sqliteFree(zFullPathname);
979*c5c4113dSnw141292   strcpy(&pPager->zJournal[nameLen], "-journal");
980*c5c4113dSnw141292   pPager->fd = fd;
981*c5c4113dSnw141292   pPager->journalOpen = 0;
982*c5c4113dSnw141292   pPager->useJournal = useJournal;
983*c5c4113dSnw141292   pPager->ckptOpen = 0;
984*c5c4113dSnw141292   pPager->ckptInUse = 0;
985*c5c4113dSnw141292   pPager->nRef = 0;
986*c5c4113dSnw141292   pPager->dbSize = -1;
987*c5c4113dSnw141292   pPager->ckptSize = 0;
988*c5c4113dSnw141292   pPager->ckptJSize = 0;
989*c5c4113dSnw141292   pPager->nPage = 0;
990*c5c4113dSnw141292   pPager->mxPage = mxPage>5 ? mxPage : 10;
991*c5c4113dSnw141292   pPager->state = SQLITE_UNLOCK;
992*c5c4113dSnw141292   pPager->errMask = 0;
993*c5c4113dSnw141292   pPager->tempFile = tempFile;
994*c5c4113dSnw141292   pPager->readOnly = readOnly;
995*c5c4113dSnw141292   pPager->needSync = 0;
996*c5c4113dSnw141292   pPager->noSync = pPager->tempFile || !useJournal;
997*c5c4113dSnw141292   pPager->pFirst = 0;
998*c5c4113dSnw141292   pPager->pFirstSynced = 0;
999*c5c4113dSnw141292   pPager->pLast = 0;
1000*c5c4113dSnw141292   pPager->nExtra = nExtra;
1001*c5c4113dSnw141292   memset(pPager->aHash, 0, sizeof(pPager->aHash));
1002*c5c4113dSnw141292   *ppPager = pPager;
1003*c5c4113dSnw141292   return SQLITE_OK;
1004*c5c4113dSnw141292 }
1005*c5c4113dSnw141292 
1006*c5c4113dSnw141292 /*
1007*c5c4113dSnw141292 ** Set the destructor for this pager.  If not NULL, the destructor is called
1008*c5c4113dSnw141292 ** when the reference count on each page reaches zero.  The destructor can
1009*c5c4113dSnw141292 ** be used to clean up information in the extra segment appended to each page.
1010*c5c4113dSnw141292 **
1011*c5c4113dSnw141292 ** The destructor is not called as a result sqlitepager_close().
1012*c5c4113dSnw141292 ** Destructors are only called by sqlitepager_unref().
1013*c5c4113dSnw141292 */
sqlitepager_set_destructor(Pager * pPager,void (* xDesc)(void *))1014*c5c4113dSnw141292 void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){
1015*c5c4113dSnw141292   pPager->xDestructor = xDesc;
1016*c5c4113dSnw141292 }
1017*c5c4113dSnw141292 
1018*c5c4113dSnw141292 /*
1019*c5c4113dSnw141292 ** Return the total number of pages in the disk file associated with
1020*c5c4113dSnw141292 ** pPager.
1021*c5c4113dSnw141292 */
sqlitepager_pagecount(Pager * pPager)1022*c5c4113dSnw141292 int sqlitepager_pagecount(Pager *pPager){
1023*c5c4113dSnw141292   off_t n;
1024*c5c4113dSnw141292   assert( pPager!=0 );
1025*c5c4113dSnw141292   if( pPager->dbSize>=0 ){
1026*c5c4113dSnw141292     return pPager->dbSize;
1027*c5c4113dSnw141292   }
1028*c5c4113dSnw141292   if( sqliteOsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
1029*c5c4113dSnw141292     pPager->errMask |= PAGER_ERR_DISK;
1030*c5c4113dSnw141292     return 0;
1031*c5c4113dSnw141292   }
1032*c5c4113dSnw141292   n /= SQLITE_PAGE_SIZE;
1033*c5c4113dSnw141292   if( pPager->state!=SQLITE_UNLOCK ){
1034*c5c4113dSnw141292     pPager->dbSize = n;
1035*c5c4113dSnw141292   }
1036*c5c4113dSnw141292   return n;
1037*c5c4113dSnw141292 }
1038*c5c4113dSnw141292 
1039*c5c4113dSnw141292 /*
1040*c5c4113dSnw141292 ** Forward declaration
1041*c5c4113dSnw141292 */
1042*c5c4113dSnw141292 static int syncJournal(Pager*);
1043*c5c4113dSnw141292 
1044*c5c4113dSnw141292 /*
1045*c5c4113dSnw141292 ** Truncate the file to the number of pages specified.
1046*c5c4113dSnw141292 */
sqlitepager_truncate(Pager * pPager,Pgno nPage)1047*c5c4113dSnw141292 int sqlitepager_truncate(Pager *pPager, Pgno nPage){
1048*c5c4113dSnw141292   int rc;
1049*c5c4113dSnw141292   if( pPager->dbSize<0 ){
1050*c5c4113dSnw141292     sqlitepager_pagecount(pPager);
1051*c5c4113dSnw141292   }
1052*c5c4113dSnw141292   if( pPager->errMask!=0 ){
1053*c5c4113dSnw141292     rc = pager_errcode(pPager);
1054*c5c4113dSnw141292     return rc;
1055*c5c4113dSnw141292   }
1056*c5c4113dSnw141292   if( nPage>=(unsigned)pPager->dbSize ){
1057*c5c4113dSnw141292     return SQLITE_OK;
1058*c5c4113dSnw141292   }
1059*c5c4113dSnw141292   syncJournal(pPager);
1060*c5c4113dSnw141292   rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)nPage);
1061*c5c4113dSnw141292   if( rc==SQLITE_OK ){
1062*c5c4113dSnw141292     pPager->dbSize = nPage;
1063*c5c4113dSnw141292   }
1064*c5c4113dSnw141292   return rc;
1065*c5c4113dSnw141292 }
1066*c5c4113dSnw141292 
1067*c5c4113dSnw141292 /*
1068*c5c4113dSnw141292 ** Shutdown the page cache.  Free all memory and close all files.
1069*c5c4113dSnw141292 **
1070*c5c4113dSnw141292 ** If a transaction was in progress when this routine is called, that
1071*c5c4113dSnw141292 ** transaction is rolled back.  All outstanding pages are invalidated
1072*c5c4113dSnw141292 ** and their memory is freed.  Any attempt to use a page associated
1073*c5c4113dSnw141292 ** with this page cache after this function returns will likely
1074*c5c4113dSnw141292 ** result in a coredump.
1075*c5c4113dSnw141292 */
sqlitepager_close(Pager * pPager)1076*c5c4113dSnw141292 int sqlitepager_close(Pager *pPager){
1077*c5c4113dSnw141292   PgHdr *pPg, *pNext;
1078*c5c4113dSnw141292   switch( pPager->state ){
1079*c5c4113dSnw141292     case SQLITE_WRITELOCK: {
1080*c5c4113dSnw141292       sqlitepager_rollback(pPager);
1081*c5c4113dSnw141292       sqliteOsUnlock(&pPager->fd);
1082*c5c4113dSnw141292       assert( pPager->journalOpen==0 );
1083*c5c4113dSnw141292       break;
1084*c5c4113dSnw141292     }
1085*c5c4113dSnw141292     case SQLITE_READLOCK: {
1086*c5c4113dSnw141292       sqliteOsUnlock(&pPager->fd);
1087*c5c4113dSnw141292       break;
1088*c5c4113dSnw141292     }
1089*c5c4113dSnw141292     default: {
1090*c5c4113dSnw141292       /* Do nothing */
1091*c5c4113dSnw141292       break;
1092*c5c4113dSnw141292     }
1093*c5c4113dSnw141292   }
1094*c5c4113dSnw141292   for(pPg=pPager->pAll; pPg; pPg=pNext){
1095*c5c4113dSnw141292     pNext = pPg->pNextAll;
1096*c5c4113dSnw141292     sqliteFree(pPg);
1097*c5c4113dSnw141292   }
1098*c5c4113dSnw141292   sqliteOsClose(&pPager->fd);
1099*c5c4113dSnw141292   assert( pPager->journalOpen==0 );
1100*c5c4113dSnw141292   /* Temp files are automatically deleted by the OS
1101*c5c4113dSnw141292   ** if( pPager->tempFile ){
1102*c5c4113dSnw141292   **   sqliteOsDelete(pPager->zFilename);
1103*c5c4113dSnw141292   ** }
1104*c5c4113dSnw141292   */
1105*c5c4113dSnw141292   CLR_PAGER(pPager);
1106*c5c4113dSnw141292   if( pPager->zFilename!=(char*)&pPager[1] ){
1107*c5c4113dSnw141292     assert( 0 );  /* Cannot happen */
1108*c5c4113dSnw141292     sqliteFree(pPager->zFilename);
1109*c5c4113dSnw141292     sqliteFree(pPager->zJournal);
1110*c5c4113dSnw141292     sqliteFree(pPager->zDirectory);
1111*c5c4113dSnw141292   }
1112*c5c4113dSnw141292   sqliteFree(pPager);
1113*c5c4113dSnw141292   return SQLITE_OK;
1114*c5c4113dSnw141292 }
1115*c5c4113dSnw141292 
1116*c5c4113dSnw141292 /*
1117*c5c4113dSnw141292 ** Return the page number for the given page data.
1118*c5c4113dSnw141292 */
sqlitepager_pagenumber(void * pData)1119*c5c4113dSnw141292 Pgno sqlitepager_pagenumber(void *pData){
1120*c5c4113dSnw141292   PgHdr *p = DATA_TO_PGHDR(pData);
1121*c5c4113dSnw141292   return p->pgno;
1122*c5c4113dSnw141292 }
1123*c5c4113dSnw141292 
1124*c5c4113dSnw141292 /*
1125*c5c4113dSnw141292 ** Increment the reference count for a page.  If the page is
1126*c5c4113dSnw141292 ** currently on the freelist (the reference count is zero) then
1127*c5c4113dSnw141292 ** remove it from the freelist.
1128*c5c4113dSnw141292 */
1129*c5c4113dSnw141292 #define page_ref(P)   ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
_page_ref(PgHdr * pPg)1130*c5c4113dSnw141292 static void _page_ref(PgHdr *pPg){
1131*c5c4113dSnw141292   if( pPg->nRef==0 ){
1132*c5c4113dSnw141292     /* The page is currently on the freelist.  Remove it. */
1133*c5c4113dSnw141292     if( pPg==pPg->pPager->pFirstSynced ){
1134*c5c4113dSnw141292       PgHdr *p = pPg->pNextFree;
1135*c5c4113dSnw141292       while( p && p->needSync ){ p = p->pNextFree; }
1136*c5c4113dSnw141292       pPg->pPager->pFirstSynced = p;
1137*c5c4113dSnw141292     }
1138*c5c4113dSnw141292     if( pPg->pPrevFree ){
1139*c5c4113dSnw141292       pPg->pPrevFree->pNextFree = pPg->pNextFree;
1140*c5c4113dSnw141292     }else{
1141*c5c4113dSnw141292       pPg->pPager->pFirst = pPg->pNextFree;
1142*c5c4113dSnw141292     }
1143*c5c4113dSnw141292     if( pPg->pNextFree ){
1144*c5c4113dSnw141292       pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1145*c5c4113dSnw141292     }else{
1146*c5c4113dSnw141292       pPg->pPager->pLast = pPg->pPrevFree;
1147*c5c4113dSnw141292     }
1148*c5c4113dSnw141292     pPg->pPager->nRef++;
1149*c5c4113dSnw141292   }
1150*c5c4113dSnw141292   pPg->nRef++;
1151*c5c4113dSnw141292   REFINFO(pPg);
1152*c5c4113dSnw141292 }
1153*c5c4113dSnw141292 
1154*c5c4113dSnw141292 /*
1155*c5c4113dSnw141292 ** Increment the reference count for a page.  The input pointer is
1156*c5c4113dSnw141292 ** a reference to the page data.
1157*c5c4113dSnw141292 */
sqlitepager_ref(void * pData)1158*c5c4113dSnw141292 int sqlitepager_ref(void *pData){
1159*c5c4113dSnw141292   PgHdr *pPg = DATA_TO_PGHDR(pData);
1160*c5c4113dSnw141292   page_ref(pPg);
1161*c5c4113dSnw141292   return SQLITE_OK;
1162*c5c4113dSnw141292 }
1163*c5c4113dSnw141292 
1164*c5c4113dSnw141292 /*
1165*c5c4113dSnw141292 ** Sync the journal.  In other words, make sure all the pages that have
1166*c5c4113dSnw141292 ** been written to the journal have actually reached the surface of the
1167*c5c4113dSnw141292 ** disk.  It is not safe to modify the original database file until after
1168*c5c4113dSnw141292 ** the journal has been synced.  If the original database is modified before
1169*c5c4113dSnw141292 ** the journal is synced and a power failure occurs, the unsynced journal
1170*c5c4113dSnw141292 ** data would be lost and we would be unable to completely rollback the
1171*c5c4113dSnw141292 ** database changes.  Database corruption would occur.
1172*c5c4113dSnw141292 **
1173*c5c4113dSnw141292 ** This routine also updates the nRec field in the header of the journal.
1174*c5c4113dSnw141292 ** (See comments on the pager_playback() routine for additional information.)
1175*c5c4113dSnw141292 ** If the sync mode is FULL, two syncs will occur.  First the whole journal
1176*c5c4113dSnw141292 ** is synced, then the nRec field is updated, then a second sync occurs.
1177*c5c4113dSnw141292 **
1178*c5c4113dSnw141292 ** For temporary databases, we do not care if we are able to rollback
1179*c5c4113dSnw141292 ** after a power failure, so sync occurs.
1180*c5c4113dSnw141292 **
1181*c5c4113dSnw141292 ** This routine clears the needSync field of every page current held in
1182*c5c4113dSnw141292 ** memory.
1183*c5c4113dSnw141292 */
syncJournal(Pager * pPager)1184*c5c4113dSnw141292 static int syncJournal(Pager *pPager){
1185*c5c4113dSnw141292   PgHdr *pPg;
1186*c5c4113dSnw141292   int rc = SQLITE_OK;
1187*c5c4113dSnw141292 
1188*c5c4113dSnw141292   /* Sync the journal before modifying the main database
1189*c5c4113dSnw141292   ** (assuming there is a journal and it needs to be synced.)
1190*c5c4113dSnw141292   */
1191*c5c4113dSnw141292   if( pPager->needSync ){
1192*c5c4113dSnw141292     if( !pPager->tempFile ){
1193*c5c4113dSnw141292       assert( pPager->journalOpen );
1194*c5c4113dSnw141292       /* assert( !pPager->noSync ); // noSync might be set if synchronous
1195*c5c4113dSnw141292       ** was turned off after the transaction was started.  Ticket #615 */
1196*c5c4113dSnw141292 #ifndef NDEBUG
1197*c5c4113dSnw141292       {
1198*c5c4113dSnw141292         /* Make sure the pPager->nRec counter we are keeping agrees
1199*c5c4113dSnw141292         ** with the nRec computed from the size of the journal file.
1200*c5c4113dSnw141292         */
1201*c5c4113dSnw141292         off_t hdrSz, pgSz, jSz;
1202*c5c4113dSnw141292         hdrSz = JOURNAL_HDR_SZ(journal_format);
1203*c5c4113dSnw141292         pgSz = JOURNAL_PG_SZ(journal_format);
1204*c5c4113dSnw141292         rc = sqliteOsFileSize(&pPager->jfd, &jSz);
1205*c5c4113dSnw141292         if( rc!=0 ) return rc;
1206*c5c4113dSnw141292         assert( pPager->nRec*pgSz+hdrSz==jSz );
1207*c5c4113dSnw141292       }
1208*c5c4113dSnw141292 #endif
1209*c5c4113dSnw141292       if( journal_format>=3 ){
1210*c5c4113dSnw141292         /* Write the nRec value into the journal file header */
1211*c5c4113dSnw141292         off_t szJ;
1212*c5c4113dSnw141292         if( pPager->fullSync ){
1213*c5c4113dSnw141292           TRACE1("SYNC\n");
1214*c5c4113dSnw141292           rc = sqliteOsSync(&pPager->jfd);
1215*c5c4113dSnw141292           if( rc!=0 ) return rc;
1216*c5c4113dSnw141292         }
1217*c5c4113dSnw141292         sqliteOsSeek(&pPager->jfd, sizeof(aJournalMagic1));
1218*c5c4113dSnw141292         rc = write32bits(&pPager->jfd, pPager->nRec);
1219*c5c4113dSnw141292         if( rc ) return rc;
1220*c5c4113dSnw141292         szJ = JOURNAL_HDR_SZ(journal_format) +
1221*c5c4113dSnw141292                  pPager->nRec*JOURNAL_PG_SZ(journal_format);
1222*c5c4113dSnw141292         sqliteOsSeek(&pPager->jfd, szJ);
1223*c5c4113dSnw141292       }
1224*c5c4113dSnw141292       TRACE1("SYNC\n");
1225*c5c4113dSnw141292       rc = sqliteOsSync(&pPager->jfd);
1226*c5c4113dSnw141292       if( rc!=0 ) return rc;
1227*c5c4113dSnw141292       pPager->journalStarted = 1;
1228*c5c4113dSnw141292     }
1229*c5c4113dSnw141292     pPager->needSync = 0;
1230*c5c4113dSnw141292 
1231*c5c4113dSnw141292     /* Erase the needSync flag from every page.
1232*c5c4113dSnw141292     */
1233*c5c4113dSnw141292     for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1234*c5c4113dSnw141292       pPg->needSync = 0;
1235*c5c4113dSnw141292     }
1236*c5c4113dSnw141292     pPager->pFirstSynced = pPager->pFirst;
1237*c5c4113dSnw141292   }
1238*c5c4113dSnw141292 
1239*c5c4113dSnw141292 #ifndef NDEBUG
1240*c5c4113dSnw141292   /* If the Pager.needSync flag is clear then the PgHdr.needSync
1241*c5c4113dSnw141292   ** flag must also be clear for all pages.  Verify that this
1242*c5c4113dSnw141292   ** invariant is true.
1243*c5c4113dSnw141292   */
1244*c5c4113dSnw141292   else{
1245*c5c4113dSnw141292     for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1246*c5c4113dSnw141292       assert( pPg->needSync==0 );
1247*c5c4113dSnw141292     }
1248*c5c4113dSnw141292     assert( pPager->pFirstSynced==pPager->pFirst );
1249*c5c4113dSnw141292   }
1250*c5c4113dSnw141292 #endif
1251*c5c4113dSnw141292 
1252*c5c4113dSnw141292   return rc;
1253*c5c4113dSnw141292 }
1254*c5c4113dSnw141292 
1255*c5c4113dSnw141292 /*
1256*c5c4113dSnw141292 ** Given a list of pages (connected by the PgHdr.pDirty pointer) write
1257*c5c4113dSnw141292 ** every one of those pages out to the database file and mark them all
1258*c5c4113dSnw141292 ** as clean.
1259*c5c4113dSnw141292 */
pager_write_pagelist(PgHdr * pList)1260*c5c4113dSnw141292 static int pager_write_pagelist(PgHdr *pList){
1261*c5c4113dSnw141292   Pager *pPager;
1262*c5c4113dSnw141292   int rc;
1263*c5c4113dSnw141292 
1264*c5c4113dSnw141292   if( pList==0 ) return SQLITE_OK;
1265*c5c4113dSnw141292   pPager = pList->pPager;
1266*c5c4113dSnw141292   while( pList ){
1267*c5c4113dSnw141292     assert( pList->dirty );
1268*c5c4113dSnw141292     sqliteOsSeek(&pPager->fd, (pList->pgno-1)*(off_t)SQLITE_PAGE_SIZE);
1269*c5c4113dSnw141292     CODEC(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
1270*c5c4113dSnw141292     TRACE2("STORE %d\n", pList->pgno);
1271*c5c4113dSnw141292     rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pList), SQLITE_PAGE_SIZE);
1272*c5c4113dSnw141292     CODEC(pPager, PGHDR_TO_DATA(pList), pList->pgno, 0);
1273*c5c4113dSnw141292     if( rc ) return rc;
1274*c5c4113dSnw141292     pList->dirty = 0;
1275*c5c4113dSnw141292     pList = pList->pDirty;
1276*c5c4113dSnw141292   }
1277*c5c4113dSnw141292   return SQLITE_OK;
1278*c5c4113dSnw141292 }
1279*c5c4113dSnw141292 
1280*c5c4113dSnw141292 /*
1281*c5c4113dSnw141292 ** Collect every dirty page into a dirty list and
1282*c5c4113dSnw141292 ** return a pointer to the head of that list.  All pages are
1283*c5c4113dSnw141292 ** collected even if they are still in use.
1284*c5c4113dSnw141292 */
pager_get_all_dirty_pages(Pager * pPager)1285*c5c4113dSnw141292 static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
1286*c5c4113dSnw141292   PgHdr *p, *pList;
1287*c5c4113dSnw141292   pList = 0;
1288*c5c4113dSnw141292   for(p=pPager->pAll; p; p=p->pNextAll){
1289*c5c4113dSnw141292     if( p->dirty ){
1290*c5c4113dSnw141292       p->pDirty = pList;
1291*c5c4113dSnw141292       pList = p;
1292*c5c4113dSnw141292     }
1293*c5c4113dSnw141292   }
1294*c5c4113dSnw141292   return pList;
1295*c5c4113dSnw141292 }
1296*c5c4113dSnw141292 
1297*c5c4113dSnw141292 /*
1298*c5c4113dSnw141292 ** Acquire a page.
1299*c5c4113dSnw141292 **
1300*c5c4113dSnw141292 ** A read lock on the disk file is obtained when the first page is acquired.
1301*c5c4113dSnw141292 ** This read lock is dropped when the last page is released.
1302*c5c4113dSnw141292 **
1303*c5c4113dSnw141292 ** A _get works for any page number greater than 0.  If the database
1304*c5c4113dSnw141292 ** file is smaller than the requested page, then no actual disk
1305*c5c4113dSnw141292 ** read occurs and the memory image of the page is initialized to
1306*c5c4113dSnw141292 ** all zeros.  The extra data appended to a page is always initialized
1307*c5c4113dSnw141292 ** to zeros the first time a page is loaded into memory.
1308*c5c4113dSnw141292 **
1309*c5c4113dSnw141292 ** The acquisition might fail for several reasons.  In all cases,
1310*c5c4113dSnw141292 ** an appropriate error code is returned and *ppPage is set to NULL.
1311*c5c4113dSnw141292 **
1312*c5c4113dSnw141292 ** See also sqlitepager_lookup().  Both this routine and _lookup() attempt
1313*c5c4113dSnw141292 ** to find a page in the in-memory cache first.  If the page is not already
1314*c5c4113dSnw141292 ** in memory, this routine goes to disk to read it in whereas _lookup()
1315*c5c4113dSnw141292 ** just returns 0.  This routine acquires a read-lock the first time it
1316*c5c4113dSnw141292 ** has to go to disk, and could also playback an old journal if necessary.
1317*c5c4113dSnw141292 ** Since _lookup() never goes to disk, it never has to deal with locks
1318*c5c4113dSnw141292 ** or journal files.
1319*c5c4113dSnw141292 */
sqlitepager_get(Pager * pPager,Pgno pgno,void ** ppPage)1320*c5c4113dSnw141292 int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
1321*c5c4113dSnw141292   PgHdr *pPg;
1322*c5c4113dSnw141292   int rc;
1323*c5c4113dSnw141292 
1324*c5c4113dSnw141292   /* Make sure we have not hit any critical errors.
1325*c5c4113dSnw141292   */
1326*c5c4113dSnw141292   assert( pPager!=0 );
1327*c5c4113dSnw141292   assert( pgno!=0 );
1328*c5c4113dSnw141292   *ppPage = 0;
1329*c5c4113dSnw141292   if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1330*c5c4113dSnw141292     return pager_errcode(pPager);
1331*c5c4113dSnw141292   }
1332*c5c4113dSnw141292 
1333*c5c4113dSnw141292   /* If this is the first page accessed, then get a read lock
1334*c5c4113dSnw141292   ** on the database file.
1335*c5c4113dSnw141292   */
1336*c5c4113dSnw141292   if( pPager->nRef==0 ){
1337*c5c4113dSnw141292     rc = sqliteOsReadLock(&pPager->fd);
1338*c5c4113dSnw141292     if( rc!=SQLITE_OK ){
1339*c5c4113dSnw141292       return rc;
1340*c5c4113dSnw141292     }
1341*c5c4113dSnw141292     pPager->state = SQLITE_READLOCK;
1342*c5c4113dSnw141292 
1343*c5c4113dSnw141292     /* If a journal file exists, try to play it back.
1344*c5c4113dSnw141292     */
1345*c5c4113dSnw141292     if( pPager->useJournal && sqliteOsFileExists(pPager->zJournal) ){
1346*c5c4113dSnw141292        int rc;
1347*c5c4113dSnw141292 
1348*c5c4113dSnw141292        /* Get a write lock on the database
1349*c5c4113dSnw141292        */
1350*c5c4113dSnw141292        rc = sqliteOsWriteLock(&pPager->fd);
1351*c5c4113dSnw141292        if( rc!=SQLITE_OK ){
1352*c5c4113dSnw141292          if( sqliteOsUnlock(&pPager->fd)!=SQLITE_OK ){
1353*c5c4113dSnw141292            /* This should never happen! */
1354*c5c4113dSnw141292            rc = SQLITE_INTERNAL;
1355*c5c4113dSnw141292          }
1356*c5c4113dSnw141292          return rc;
1357*c5c4113dSnw141292        }
1358*c5c4113dSnw141292        pPager->state = SQLITE_WRITELOCK;
1359*c5c4113dSnw141292 
1360*c5c4113dSnw141292        /* Open the journal for reading only.  Return SQLITE_BUSY if
1361*c5c4113dSnw141292        ** we are unable to open the journal file.
1362*c5c4113dSnw141292        **
1363*c5c4113dSnw141292        ** The journal file does not need to be locked itself.  The
1364*c5c4113dSnw141292        ** journal file is never open unless the main database file holds
1365*c5c4113dSnw141292        ** a write lock, so there is never any chance of two or more
1366*c5c4113dSnw141292        ** processes opening the journal at the same time.
1367*c5c4113dSnw141292        */
1368*c5c4113dSnw141292        rc = sqliteOsOpenReadOnly(pPager->zJournal, &pPager->jfd);
1369*c5c4113dSnw141292        if( rc!=SQLITE_OK ){
1370*c5c4113dSnw141292          rc = sqliteOsUnlock(&pPager->fd);
1371*c5c4113dSnw141292          assert( rc==SQLITE_OK );
1372*c5c4113dSnw141292          return SQLITE_BUSY;
1373*c5c4113dSnw141292        }
1374*c5c4113dSnw141292        pPager->journalOpen = 1;
1375*c5c4113dSnw141292        pPager->journalStarted = 0;
1376*c5c4113dSnw141292 
1377*c5c4113dSnw141292        /* Playback and delete the journal.  Drop the database write
1378*c5c4113dSnw141292        ** lock and reacquire the read lock.
1379*c5c4113dSnw141292        */
1380*c5c4113dSnw141292        rc = pager_playback(pPager, 0);
1381*c5c4113dSnw141292        if( rc!=SQLITE_OK ){
1382*c5c4113dSnw141292          return rc;
1383*c5c4113dSnw141292        }
1384*c5c4113dSnw141292     }
1385*c5c4113dSnw141292     pPg = 0;
1386*c5c4113dSnw141292   }else{
1387*c5c4113dSnw141292     /* Search for page in cache */
1388*c5c4113dSnw141292     pPg = pager_lookup(pPager, pgno);
1389*c5c4113dSnw141292   }
1390*c5c4113dSnw141292   if( pPg==0 ){
1391*c5c4113dSnw141292     /* The requested page is not in the page cache. */
1392*c5c4113dSnw141292     int h;
1393*c5c4113dSnw141292     pPager->nMiss++;
1394*c5c4113dSnw141292     if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){
1395*c5c4113dSnw141292       /* Create a new page */
1396*c5c4113dSnw141292       pPg = sqliteMallocRaw( sizeof(*pPg) + SQLITE_PAGE_SIZE
1397*c5c4113dSnw141292                               + sizeof(u32) + pPager->nExtra );
1398*c5c4113dSnw141292       if( pPg==0 ){
1399*c5c4113dSnw141292         pager_unwritelock(pPager);
1400*c5c4113dSnw141292         pPager->errMask |= PAGER_ERR_MEM;
1401*c5c4113dSnw141292         return SQLITE_NOMEM;
1402*c5c4113dSnw141292       }
1403*c5c4113dSnw141292       memset(pPg, 0, sizeof(*pPg));
1404*c5c4113dSnw141292       pPg->pPager = pPager;
1405*c5c4113dSnw141292       pPg->pNextAll = pPager->pAll;
1406*c5c4113dSnw141292       if( pPager->pAll ){
1407*c5c4113dSnw141292         pPager->pAll->pPrevAll = pPg;
1408*c5c4113dSnw141292       }
1409*c5c4113dSnw141292       pPg->pPrevAll = 0;
1410*c5c4113dSnw141292       pPager->pAll = pPg;
1411*c5c4113dSnw141292       pPager->nPage++;
1412*c5c4113dSnw141292     }else{
1413*c5c4113dSnw141292       /* Find a page to recycle.  Try to locate a page that does not
1414*c5c4113dSnw141292       ** require us to do an fsync() on the journal.
1415*c5c4113dSnw141292       */
1416*c5c4113dSnw141292       pPg = pPager->pFirstSynced;
1417*c5c4113dSnw141292 
1418*c5c4113dSnw141292       /* If we could not find a page that does not require an fsync()
1419*c5c4113dSnw141292       ** on the journal file then fsync the journal file.  This is a
1420*c5c4113dSnw141292       ** very slow operation, so we work hard to avoid it.  But sometimes
1421*c5c4113dSnw141292       ** it can't be helped.
1422*c5c4113dSnw141292       */
1423*c5c4113dSnw141292       if( pPg==0 ){
1424*c5c4113dSnw141292         int rc = syncJournal(pPager);
1425*c5c4113dSnw141292         if( rc!=0 ){
1426*c5c4113dSnw141292           sqlitepager_rollback(pPager);
1427*c5c4113dSnw141292           return SQLITE_IOERR;
1428*c5c4113dSnw141292         }
1429*c5c4113dSnw141292         pPg = pPager->pFirst;
1430*c5c4113dSnw141292       }
1431*c5c4113dSnw141292       assert( pPg->nRef==0 );
1432*c5c4113dSnw141292 
1433*c5c4113dSnw141292       /* Write the page to the database file if it is dirty.
1434*c5c4113dSnw141292       */
1435*c5c4113dSnw141292       if( pPg->dirty ){
1436*c5c4113dSnw141292         assert( pPg->needSync==0 );
1437*c5c4113dSnw141292         pPg->pDirty = 0;
1438*c5c4113dSnw141292         rc = pager_write_pagelist( pPg );
1439*c5c4113dSnw141292         if( rc!=SQLITE_OK ){
1440*c5c4113dSnw141292           sqlitepager_rollback(pPager);
1441*c5c4113dSnw141292           return SQLITE_IOERR;
1442*c5c4113dSnw141292         }
1443*c5c4113dSnw141292       }
1444*c5c4113dSnw141292       assert( pPg->dirty==0 );
1445*c5c4113dSnw141292 
1446*c5c4113dSnw141292       /* If the page we are recycling is marked as alwaysRollback, then
1447*c5c4113dSnw141292       ** set the global alwaysRollback flag, thus disabling the
1448*c5c4113dSnw141292       ** sqlite_dont_rollback() optimization for the rest of this transaction.
1449*c5c4113dSnw141292       ** It is necessary to do this because the page marked alwaysRollback
1450*c5c4113dSnw141292       ** might be reloaded at a later time but at that point we won't remember
1451*c5c4113dSnw141292       ** that is was marked alwaysRollback.  This means that all pages must
1452*c5c4113dSnw141292       ** be marked as alwaysRollback from here on out.
1453*c5c4113dSnw141292       */
1454*c5c4113dSnw141292       if( pPg->alwaysRollback ){
1455*c5c4113dSnw141292         pPager->alwaysRollback = 1;
1456*c5c4113dSnw141292       }
1457*c5c4113dSnw141292 
1458*c5c4113dSnw141292       /* Unlink the old page from the free list and the hash table
1459*c5c4113dSnw141292       */
1460*c5c4113dSnw141292       if( pPg==pPager->pFirstSynced ){
1461*c5c4113dSnw141292         PgHdr *p = pPg->pNextFree;
1462*c5c4113dSnw141292         while( p && p->needSync ){ p = p->pNextFree; }
1463*c5c4113dSnw141292         pPager->pFirstSynced = p;
1464*c5c4113dSnw141292       }
1465*c5c4113dSnw141292       if( pPg->pPrevFree ){
1466*c5c4113dSnw141292         pPg->pPrevFree->pNextFree = pPg->pNextFree;
1467*c5c4113dSnw141292       }else{
1468*c5c4113dSnw141292         assert( pPager->pFirst==pPg );
1469*c5c4113dSnw141292         pPager->pFirst = pPg->pNextFree;
1470*c5c4113dSnw141292       }
1471*c5c4113dSnw141292       if( pPg->pNextFree ){
1472*c5c4113dSnw141292         pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1473*c5c4113dSnw141292       }else{
1474*c5c4113dSnw141292         assert( pPager->pLast==pPg );
1475*c5c4113dSnw141292         pPager->pLast = pPg->pPrevFree;
1476*c5c4113dSnw141292       }
1477*c5c4113dSnw141292       pPg->pNextFree = pPg->pPrevFree = 0;
1478*c5c4113dSnw141292       if( pPg->pNextHash ){
1479*c5c4113dSnw141292         pPg->pNextHash->pPrevHash = pPg->pPrevHash;
1480*c5c4113dSnw141292       }
1481*c5c4113dSnw141292       if( pPg->pPrevHash ){
1482*c5c4113dSnw141292         pPg->pPrevHash->pNextHash = pPg->pNextHash;
1483*c5c4113dSnw141292       }else{
1484*c5c4113dSnw141292         h = pager_hash(pPg->pgno);
1485*c5c4113dSnw141292         assert( pPager->aHash[h]==pPg );
1486*c5c4113dSnw141292         pPager->aHash[h] = pPg->pNextHash;
1487*c5c4113dSnw141292       }
1488*c5c4113dSnw141292       pPg->pNextHash = pPg->pPrevHash = 0;
1489*c5c4113dSnw141292       pPager->nOvfl++;
1490*c5c4113dSnw141292     }
1491*c5c4113dSnw141292     pPg->pgno = pgno;
1492*c5c4113dSnw141292     if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
1493*c5c4113dSnw141292       sqliteCheckMemory(pPager->aInJournal, pgno/8);
1494*c5c4113dSnw141292       assert( pPager->journalOpen );
1495*c5c4113dSnw141292       pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
1496*c5c4113dSnw141292       pPg->needSync = 0;
1497*c5c4113dSnw141292     }else{
1498*c5c4113dSnw141292       pPg->inJournal = 0;
1499*c5c4113dSnw141292       pPg->needSync = 0;
1500*c5c4113dSnw141292     }
1501*c5c4113dSnw141292     if( pPager->aInCkpt && (int)pgno<=pPager->ckptSize
1502*c5c4113dSnw141292              && (pPager->aInCkpt[pgno/8] & (1<<(pgno&7)))!=0 ){
1503*c5c4113dSnw141292       page_add_to_ckpt_list(pPg);
1504*c5c4113dSnw141292     }else{
1505*c5c4113dSnw141292       page_remove_from_ckpt_list(pPg);
1506*c5c4113dSnw141292     }
1507*c5c4113dSnw141292     pPg->dirty = 0;
1508*c5c4113dSnw141292     pPg->nRef = 1;
1509*c5c4113dSnw141292     REFINFO(pPg);
1510*c5c4113dSnw141292     pPager->nRef++;
1511*c5c4113dSnw141292     h = pager_hash(pgno);
1512*c5c4113dSnw141292     pPg->pNextHash = pPager->aHash[h];
1513*c5c4113dSnw141292     pPager->aHash[h] = pPg;
1514*c5c4113dSnw141292     if( pPg->pNextHash ){
1515*c5c4113dSnw141292       assert( pPg->pNextHash->pPrevHash==0 );
1516*c5c4113dSnw141292       pPg->pNextHash->pPrevHash = pPg;
1517*c5c4113dSnw141292     }
1518*c5c4113dSnw141292     if( pPager->nExtra>0 ){
1519*c5c4113dSnw141292       memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
1520*c5c4113dSnw141292     }
1521*c5c4113dSnw141292     if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager);
1522*c5c4113dSnw141292     if( pPager->errMask!=0 ){
1523*c5c4113dSnw141292       sqlitepager_unref(PGHDR_TO_DATA(pPg));
1524*c5c4113dSnw141292       rc = pager_errcode(pPager);
1525*c5c4113dSnw141292       return rc;
1526*c5c4113dSnw141292     }
1527*c5c4113dSnw141292     if( pPager->dbSize<(int)pgno ){
1528*c5c4113dSnw141292       memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
1529*c5c4113dSnw141292     }else{
1530*c5c4113dSnw141292       int rc;
1531*c5c4113dSnw141292       sqliteOsSeek(&pPager->fd, (pgno-1)*(off_t)SQLITE_PAGE_SIZE);
1532*c5c4113dSnw141292       rc = sqliteOsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
1533*c5c4113dSnw141292       TRACE2("FETCH %d\n", pPg->pgno);
1534*c5c4113dSnw141292       CODEC(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
1535*c5c4113dSnw141292       if( rc!=SQLITE_OK ){
1536*c5c4113dSnw141292         off_t fileSize;
1537*c5c4113dSnw141292         if( sqliteOsFileSize(&pPager->fd,&fileSize)!=SQLITE_OK
1538*c5c4113dSnw141292                || fileSize>=pgno*SQLITE_PAGE_SIZE ){
1539*c5c4113dSnw141292           sqlitepager_unref(PGHDR_TO_DATA(pPg));
1540*c5c4113dSnw141292           return rc;
1541*c5c4113dSnw141292         }else{
1542*c5c4113dSnw141292           memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
1543*c5c4113dSnw141292         }
1544*c5c4113dSnw141292       }
1545*c5c4113dSnw141292     }
1546*c5c4113dSnw141292   }else{
1547*c5c4113dSnw141292     /* The requested page is in the page cache. */
1548*c5c4113dSnw141292     pPager->nHit++;
1549*c5c4113dSnw141292     page_ref(pPg);
1550*c5c4113dSnw141292   }
1551*c5c4113dSnw141292   *ppPage = PGHDR_TO_DATA(pPg);
1552*c5c4113dSnw141292   return SQLITE_OK;
1553*c5c4113dSnw141292 }
1554*c5c4113dSnw141292 
1555*c5c4113dSnw141292 /*
1556*c5c4113dSnw141292 ** Acquire a page if it is already in the in-memory cache.  Do
1557*c5c4113dSnw141292 ** not read the page from disk.  Return a pointer to the page,
1558*c5c4113dSnw141292 ** or 0 if the page is not in cache.
1559*c5c4113dSnw141292 **
1560*c5c4113dSnw141292 ** See also sqlitepager_get().  The difference between this routine
1561*c5c4113dSnw141292 ** and sqlitepager_get() is that _get() will go to the disk and read
1562*c5c4113dSnw141292 ** in the page if the page is not already in cache.  This routine
1563*c5c4113dSnw141292 ** returns NULL if the page is not in cache or if a disk I/O error
1564*c5c4113dSnw141292 ** has ever happened.
1565*c5c4113dSnw141292 */
sqlitepager_lookup(Pager * pPager,Pgno pgno)1566*c5c4113dSnw141292 void *sqlitepager_lookup(Pager *pPager, Pgno pgno){
1567*c5c4113dSnw141292   PgHdr *pPg;
1568*c5c4113dSnw141292 
1569*c5c4113dSnw141292   assert( pPager!=0 );
1570*c5c4113dSnw141292   assert( pgno!=0 );
1571*c5c4113dSnw141292   if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1572*c5c4113dSnw141292     return 0;
1573*c5c4113dSnw141292   }
1574*c5c4113dSnw141292   /* if( pPager->nRef==0 ){
1575*c5c4113dSnw141292   **  return 0;
1576*c5c4113dSnw141292   ** }
1577*c5c4113dSnw141292   */
1578*c5c4113dSnw141292   pPg = pager_lookup(pPager, pgno);
1579*c5c4113dSnw141292   if( pPg==0 ) return 0;
1580*c5c4113dSnw141292   page_ref(pPg);
1581*c5c4113dSnw141292   return PGHDR_TO_DATA(pPg);
1582*c5c4113dSnw141292 }
1583*c5c4113dSnw141292 
1584*c5c4113dSnw141292 /*
1585*c5c4113dSnw141292 ** Release a page.
1586*c5c4113dSnw141292 **
1587*c5c4113dSnw141292 ** If the number of references to the page drop to zero, then the
1588*c5c4113dSnw141292 ** page is added to the LRU list.  When all references to all pages
1589*c5c4113dSnw141292 ** are released, a rollback occurs and the lock on the database is
1590*c5c4113dSnw141292 ** removed.
1591*c5c4113dSnw141292 */
sqlitepager_unref(void * pData)1592*c5c4113dSnw141292 int sqlitepager_unref(void *pData){
1593*c5c4113dSnw141292   PgHdr *pPg;
1594*c5c4113dSnw141292 
1595*c5c4113dSnw141292   /* Decrement the reference count for this page
1596*c5c4113dSnw141292   */
1597*c5c4113dSnw141292   pPg = DATA_TO_PGHDR(pData);
1598*c5c4113dSnw141292   assert( pPg->nRef>0 );
1599*c5c4113dSnw141292   pPg->nRef--;
1600*c5c4113dSnw141292   REFINFO(pPg);
1601*c5c4113dSnw141292 
1602*c5c4113dSnw141292   /* When the number of references to a page reach 0, call the
1603*c5c4113dSnw141292   ** destructor and add the page to the freelist.
1604*c5c4113dSnw141292   */
1605*c5c4113dSnw141292   if( pPg->nRef==0 ){
1606*c5c4113dSnw141292     Pager *pPager;
1607*c5c4113dSnw141292     pPager = pPg->pPager;
1608*c5c4113dSnw141292     pPg->pNextFree = 0;
1609*c5c4113dSnw141292     pPg->pPrevFree = pPager->pLast;
1610*c5c4113dSnw141292     pPager->pLast = pPg;
1611*c5c4113dSnw141292     if( pPg->pPrevFree ){
1612*c5c4113dSnw141292       pPg->pPrevFree->pNextFree = pPg;
1613*c5c4113dSnw141292     }else{
1614*c5c4113dSnw141292       pPager->pFirst = pPg;
1615*c5c4113dSnw141292     }
1616*c5c4113dSnw141292     if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
1617*c5c4113dSnw141292       pPager->pFirstSynced = pPg;
1618*c5c4113dSnw141292     }
1619*c5c4113dSnw141292     if( pPager->xDestructor ){
1620*c5c4113dSnw141292       pPager->xDestructor(pData);
1621*c5c4113dSnw141292     }
1622*c5c4113dSnw141292 
1623*c5c4113dSnw141292     /* When all pages reach the freelist, drop the read lock from
1624*c5c4113dSnw141292     ** the database file.
1625*c5c4113dSnw141292     */
1626*c5c4113dSnw141292     pPager->nRef--;
1627*c5c4113dSnw141292     assert( pPager->nRef>=0 );
1628*c5c4113dSnw141292     if( pPager->nRef==0 ){
1629*c5c4113dSnw141292       pager_reset(pPager);
1630*c5c4113dSnw141292     }
1631*c5c4113dSnw141292   }
1632*c5c4113dSnw141292   return SQLITE_OK;
1633*c5c4113dSnw141292 }
1634*c5c4113dSnw141292 
1635*c5c4113dSnw141292 /*
1636*c5c4113dSnw141292 ** Create a journal file for pPager.  There should already be a write
1637*c5c4113dSnw141292 ** lock on the database file when this routine is called.
1638*c5c4113dSnw141292 **
1639*c5c4113dSnw141292 ** Return SQLITE_OK if everything.  Return an error code and release the
1640*c5c4113dSnw141292 ** write lock if anything goes wrong.
1641*c5c4113dSnw141292 */
pager_open_journal(Pager * pPager)1642*c5c4113dSnw141292 static int pager_open_journal(Pager *pPager){
1643*c5c4113dSnw141292   int rc;
1644*c5c4113dSnw141292   assert( pPager->state==SQLITE_WRITELOCK );
1645*c5c4113dSnw141292   assert( pPager->journalOpen==0 );
1646*c5c4113dSnw141292   assert( pPager->useJournal );
1647*c5c4113dSnw141292   sqlitepager_pagecount(pPager);
1648*c5c4113dSnw141292   pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
1649*c5c4113dSnw141292   if( pPager->aInJournal==0 ){
1650*c5c4113dSnw141292     sqliteOsReadLock(&pPager->fd);
1651*c5c4113dSnw141292     pPager->state = SQLITE_READLOCK;
1652*c5c4113dSnw141292     return SQLITE_NOMEM;
1653*c5c4113dSnw141292   }
1654*c5c4113dSnw141292   rc = sqliteOsOpenExclusive(pPager->zJournal, &pPager->jfd,pPager->tempFile);
1655*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
1656*c5c4113dSnw141292     sqliteFree(pPager->aInJournal);
1657*c5c4113dSnw141292     pPager->aInJournal = 0;
1658*c5c4113dSnw141292     sqliteOsReadLock(&pPager->fd);
1659*c5c4113dSnw141292     pPager->state = SQLITE_READLOCK;
1660*c5c4113dSnw141292     return SQLITE_CANTOPEN;
1661*c5c4113dSnw141292   }
1662*c5c4113dSnw141292   sqliteOsOpenDirectory(pPager->zDirectory, &pPager->jfd);
1663*c5c4113dSnw141292   pPager->journalOpen = 1;
1664*c5c4113dSnw141292   pPager->journalStarted = 0;
1665*c5c4113dSnw141292   pPager->needSync = 0;
1666*c5c4113dSnw141292   pPager->alwaysRollback = 0;
1667*c5c4113dSnw141292   pPager->nRec = 0;
1668*c5c4113dSnw141292   if( pPager->errMask!=0 ){
1669*c5c4113dSnw141292     rc = pager_errcode(pPager);
1670*c5c4113dSnw141292     return rc;
1671*c5c4113dSnw141292   }
1672*c5c4113dSnw141292   pPager->origDbSize = pPager->dbSize;
1673*c5c4113dSnw141292   if( journal_format==JOURNAL_FORMAT_3 ){
1674*c5c4113dSnw141292     rc = sqliteOsWrite(&pPager->jfd, aJournalMagic3, sizeof(aJournalMagic3));
1675*c5c4113dSnw141292     if( rc==SQLITE_OK ){
1676*c5c4113dSnw141292       rc = write32bits(&pPager->jfd, pPager->noSync ? 0xffffffff : 0);
1677*c5c4113dSnw141292     }
1678*c5c4113dSnw141292     if( rc==SQLITE_OK ){
1679*c5c4113dSnw141292       sqliteRandomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
1680*c5c4113dSnw141292       rc = write32bits(&pPager->jfd, pPager->cksumInit);
1681*c5c4113dSnw141292     }
1682*c5c4113dSnw141292   }else if( journal_format==JOURNAL_FORMAT_2 ){
1683*c5c4113dSnw141292     rc = sqliteOsWrite(&pPager->jfd, aJournalMagic2, sizeof(aJournalMagic2));
1684*c5c4113dSnw141292   }else{
1685*c5c4113dSnw141292     assert( journal_format==JOURNAL_FORMAT_1 );
1686*c5c4113dSnw141292     rc = sqliteOsWrite(&pPager->jfd, aJournalMagic1, sizeof(aJournalMagic1));
1687*c5c4113dSnw141292   }
1688*c5c4113dSnw141292   if( rc==SQLITE_OK ){
1689*c5c4113dSnw141292     rc = write32bits(&pPager->jfd, pPager->dbSize);
1690*c5c4113dSnw141292   }
1691*c5c4113dSnw141292   if( pPager->ckptAutoopen && rc==SQLITE_OK ){
1692*c5c4113dSnw141292     rc = sqlitepager_ckpt_begin(pPager);
1693*c5c4113dSnw141292   }
1694*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
1695*c5c4113dSnw141292     rc = pager_unwritelock(pPager);
1696*c5c4113dSnw141292     if( rc==SQLITE_OK ){
1697*c5c4113dSnw141292       rc = SQLITE_FULL;
1698*c5c4113dSnw141292     }
1699*c5c4113dSnw141292   }
1700*c5c4113dSnw141292   return rc;
1701*c5c4113dSnw141292 }
1702*c5c4113dSnw141292 
1703*c5c4113dSnw141292 /*
1704*c5c4113dSnw141292 ** Acquire a write-lock on the database.  The lock is removed when
1705*c5c4113dSnw141292 ** the any of the following happen:
1706*c5c4113dSnw141292 **
1707*c5c4113dSnw141292 **   *  sqlitepager_commit() is called.
1708*c5c4113dSnw141292 **   *  sqlitepager_rollback() is called.
1709*c5c4113dSnw141292 **   *  sqlitepager_close() is called.
1710*c5c4113dSnw141292 **   *  sqlitepager_unref() is called to on every outstanding page.
1711*c5c4113dSnw141292 **
1712*c5c4113dSnw141292 ** The parameter to this routine is a pointer to any open page of the
1713*c5c4113dSnw141292 ** database file.  Nothing changes about the page - it is used merely
1714*c5c4113dSnw141292 ** to acquire a pointer to the Pager structure and as proof that there
1715*c5c4113dSnw141292 ** is already a read-lock on the database.
1716*c5c4113dSnw141292 **
1717*c5c4113dSnw141292 ** A journal file is opened if this is not a temporary file.  For
1718*c5c4113dSnw141292 ** temporary files, the opening of the journal file is deferred until
1719*c5c4113dSnw141292 ** there is an actual need to write to the journal.
1720*c5c4113dSnw141292 **
1721*c5c4113dSnw141292 ** If the database is already write-locked, this routine is a no-op.
1722*c5c4113dSnw141292 */
sqlitepager_begin(void * pData)1723*c5c4113dSnw141292 int sqlitepager_begin(void *pData){
1724*c5c4113dSnw141292   PgHdr *pPg = DATA_TO_PGHDR(pData);
1725*c5c4113dSnw141292   Pager *pPager = pPg->pPager;
1726*c5c4113dSnw141292   int rc = SQLITE_OK;
1727*c5c4113dSnw141292   assert( pPg->nRef>0 );
1728*c5c4113dSnw141292   assert( pPager->state!=SQLITE_UNLOCK );
1729*c5c4113dSnw141292   if( pPager->state==SQLITE_READLOCK ){
1730*c5c4113dSnw141292     assert( pPager->aInJournal==0 );
1731*c5c4113dSnw141292     rc = sqliteOsWriteLock(&pPager->fd);
1732*c5c4113dSnw141292     if( rc!=SQLITE_OK ){
1733*c5c4113dSnw141292       return rc;
1734*c5c4113dSnw141292     }
1735*c5c4113dSnw141292     pPager->state = SQLITE_WRITELOCK;
1736*c5c4113dSnw141292     pPager->dirtyFile = 0;
1737*c5c4113dSnw141292     TRACE1("TRANSACTION\n");
1738*c5c4113dSnw141292     if( pPager->useJournal && !pPager->tempFile ){
1739*c5c4113dSnw141292       rc = pager_open_journal(pPager);
1740*c5c4113dSnw141292     }
1741*c5c4113dSnw141292   }
1742*c5c4113dSnw141292   return rc;
1743*c5c4113dSnw141292 }
1744*c5c4113dSnw141292 
1745*c5c4113dSnw141292 /*
1746*c5c4113dSnw141292 ** Mark a data page as writeable.  The page is written into the journal
1747*c5c4113dSnw141292 ** if it is not there already.  This routine must be called before making
1748*c5c4113dSnw141292 ** changes to a page.
1749*c5c4113dSnw141292 **
1750*c5c4113dSnw141292 ** The first time this routine is called, the pager creates a new
1751*c5c4113dSnw141292 ** journal and acquires a write lock on the database.  If the write
1752*c5c4113dSnw141292 ** lock could not be acquired, this routine returns SQLITE_BUSY.  The
1753*c5c4113dSnw141292 ** calling routine must check for that return value and be careful not to
1754*c5c4113dSnw141292 ** change any page data until this routine returns SQLITE_OK.
1755*c5c4113dSnw141292 **
1756*c5c4113dSnw141292 ** If the journal file could not be written because the disk is full,
1757*c5c4113dSnw141292 ** then this routine returns SQLITE_FULL and does an immediate rollback.
1758*c5c4113dSnw141292 ** All subsequent write attempts also return SQLITE_FULL until there
1759*c5c4113dSnw141292 ** is a call to sqlitepager_commit() or sqlitepager_rollback() to
1760*c5c4113dSnw141292 ** reset.
1761*c5c4113dSnw141292 */
sqlitepager_write(void * pData)1762*c5c4113dSnw141292 int sqlitepager_write(void *pData){
1763*c5c4113dSnw141292   PgHdr *pPg = DATA_TO_PGHDR(pData);
1764*c5c4113dSnw141292   Pager *pPager = pPg->pPager;
1765*c5c4113dSnw141292   int rc = SQLITE_OK;
1766*c5c4113dSnw141292 
1767*c5c4113dSnw141292   /* Check for errors
1768*c5c4113dSnw141292   */
1769*c5c4113dSnw141292   if( pPager->errMask ){
1770*c5c4113dSnw141292     return pager_errcode(pPager);
1771*c5c4113dSnw141292   }
1772*c5c4113dSnw141292   if( pPager->readOnly ){
1773*c5c4113dSnw141292     return SQLITE_PERM;
1774*c5c4113dSnw141292   }
1775*c5c4113dSnw141292 
1776*c5c4113dSnw141292   /* Mark the page as dirty.  If the page has already been written
1777*c5c4113dSnw141292   ** to the journal then we can return right away.
1778*c5c4113dSnw141292   */
1779*c5c4113dSnw141292   pPg->dirty = 1;
1780*c5c4113dSnw141292   if( pPg->inJournal && (pPg->inCkpt || pPager->ckptInUse==0) ){
1781*c5c4113dSnw141292     pPager->dirtyFile = 1;
1782*c5c4113dSnw141292     return SQLITE_OK;
1783*c5c4113dSnw141292   }
1784*c5c4113dSnw141292 
1785*c5c4113dSnw141292   /* If we get this far, it means that the page needs to be
1786*c5c4113dSnw141292   ** written to the transaction journal or the ckeckpoint journal
1787*c5c4113dSnw141292   ** or both.
1788*c5c4113dSnw141292   **
1789*c5c4113dSnw141292   ** First check to see that the transaction journal exists and
1790*c5c4113dSnw141292   ** create it if it does not.
1791*c5c4113dSnw141292   */
1792*c5c4113dSnw141292   assert( pPager->state!=SQLITE_UNLOCK );
1793*c5c4113dSnw141292   rc = sqlitepager_begin(pData);
1794*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
1795*c5c4113dSnw141292     return rc;
1796*c5c4113dSnw141292   }
1797*c5c4113dSnw141292   assert( pPager->state==SQLITE_WRITELOCK );
1798*c5c4113dSnw141292   if( !pPager->journalOpen && pPager->useJournal ){
1799*c5c4113dSnw141292     rc = pager_open_journal(pPager);
1800*c5c4113dSnw141292     if( rc!=SQLITE_OK ) return rc;
1801*c5c4113dSnw141292   }
1802*c5c4113dSnw141292   assert( pPager->journalOpen || !pPager->useJournal );
1803*c5c4113dSnw141292   pPager->dirtyFile = 1;
1804*c5c4113dSnw141292 
1805*c5c4113dSnw141292   /* The transaction journal now exists and we have a write lock on the
1806*c5c4113dSnw141292   ** main database file.  Write the current page to the transaction
1807*c5c4113dSnw141292   ** journal if it is not there already.
1808*c5c4113dSnw141292   */
1809*c5c4113dSnw141292   if( !pPg->inJournal && pPager->useJournal ){
1810*c5c4113dSnw141292     if( (int)pPg->pgno <= pPager->origDbSize ){
1811*c5c4113dSnw141292       int szPg;
1812*c5c4113dSnw141292       u32 saved;
1813*c5c4113dSnw141292       if( journal_format>=JOURNAL_FORMAT_3 ){
1814*c5c4113dSnw141292         u32 cksum = pager_cksum(pPager, pPg->pgno, pData);
1815*c5c4113dSnw141292         saved = *(u32*)PGHDR_TO_EXTRA(pPg);
1816*c5c4113dSnw141292         store32bits(cksum, pPg, SQLITE_PAGE_SIZE);
1817*c5c4113dSnw141292         szPg = SQLITE_PAGE_SIZE+8;
1818*c5c4113dSnw141292       }else{
1819*c5c4113dSnw141292         szPg = SQLITE_PAGE_SIZE+4;
1820*c5c4113dSnw141292       }
1821*c5c4113dSnw141292       store32bits(pPg->pgno, pPg, -4);
1822*c5c4113dSnw141292       CODEC(pPager, pData, pPg->pgno, 7);
1823*c5c4113dSnw141292       rc = sqliteOsWrite(&pPager->jfd, &((char*)pData)[-4], szPg);
1824*c5c4113dSnw141292       TRACE3("JOURNAL %d %d\n", pPg->pgno, pPg->needSync);
1825*c5c4113dSnw141292       CODEC(pPager, pData, pPg->pgno, 0);
1826*c5c4113dSnw141292       if( journal_format>=JOURNAL_FORMAT_3 ){
1827*c5c4113dSnw141292         *(u32*)PGHDR_TO_EXTRA(pPg) = saved;
1828*c5c4113dSnw141292       }
1829*c5c4113dSnw141292       if( rc!=SQLITE_OK ){
1830*c5c4113dSnw141292         sqlitepager_rollback(pPager);
1831*c5c4113dSnw141292         pPager->errMask |= PAGER_ERR_FULL;
1832*c5c4113dSnw141292         return rc;
1833*c5c4113dSnw141292       }
1834*c5c4113dSnw141292       pPager->nRec++;
1835*c5c4113dSnw141292       assert( pPager->aInJournal!=0 );
1836*c5c4113dSnw141292       pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1837*c5c4113dSnw141292       pPg->needSync = !pPager->noSync;
1838*c5c4113dSnw141292       pPg->inJournal = 1;
1839*c5c4113dSnw141292       if( pPager->ckptInUse ){
1840*c5c4113dSnw141292         pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1841*c5c4113dSnw141292         page_add_to_ckpt_list(pPg);
1842*c5c4113dSnw141292       }
1843*c5c4113dSnw141292     }else{
1844*c5c4113dSnw141292       pPg->needSync = !pPager->journalStarted && !pPager->noSync;
1845*c5c4113dSnw141292       TRACE3("APPEND %d %d\n", pPg->pgno, pPg->needSync);
1846*c5c4113dSnw141292     }
1847*c5c4113dSnw141292     if( pPg->needSync ){
1848*c5c4113dSnw141292       pPager->needSync = 1;
1849*c5c4113dSnw141292     }
1850*c5c4113dSnw141292   }
1851*c5c4113dSnw141292 
1852*c5c4113dSnw141292   /* If the checkpoint journal is open and the page is not in it,
1853*c5c4113dSnw141292   ** then write the current page to the checkpoint journal.  Note that
1854*c5c4113dSnw141292   ** the checkpoint journal always uses the simplier format 2 that lacks
1855*c5c4113dSnw141292   ** checksums.  The header is also omitted from the checkpoint journal.
1856*c5c4113dSnw141292   */
1857*c5c4113dSnw141292   if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
1858*c5c4113dSnw141292     assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
1859*c5c4113dSnw141292     store32bits(pPg->pgno, pPg, -4);
1860*c5c4113dSnw141292     CODEC(pPager, pData, pPg->pgno, 7);
1861*c5c4113dSnw141292     rc = sqliteOsWrite(&pPager->cpfd, &((char*)pData)[-4], SQLITE_PAGE_SIZE+4);
1862*c5c4113dSnw141292     TRACE2("CKPT-JOURNAL %d\n", pPg->pgno);
1863*c5c4113dSnw141292     CODEC(pPager, pData, pPg->pgno, 0);
1864*c5c4113dSnw141292     if( rc!=SQLITE_OK ){
1865*c5c4113dSnw141292       sqlitepager_rollback(pPager);
1866*c5c4113dSnw141292       pPager->errMask |= PAGER_ERR_FULL;
1867*c5c4113dSnw141292       return rc;
1868*c5c4113dSnw141292     }
1869*c5c4113dSnw141292     pPager->ckptNRec++;
1870*c5c4113dSnw141292     assert( pPager->aInCkpt!=0 );
1871*c5c4113dSnw141292     pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1872*c5c4113dSnw141292     page_add_to_ckpt_list(pPg);
1873*c5c4113dSnw141292   }
1874*c5c4113dSnw141292 
1875*c5c4113dSnw141292   /* Update the database size and return.
1876*c5c4113dSnw141292   */
1877*c5c4113dSnw141292   if( pPager->dbSize<(int)pPg->pgno ){
1878*c5c4113dSnw141292     pPager->dbSize = pPg->pgno;
1879*c5c4113dSnw141292   }
1880*c5c4113dSnw141292   return rc;
1881*c5c4113dSnw141292 }
1882*c5c4113dSnw141292 
1883*c5c4113dSnw141292 /*
1884*c5c4113dSnw141292 ** Return TRUE if the page given in the argument was previously passed
1885*c5c4113dSnw141292 ** to sqlitepager_write().  In other words, return TRUE if it is ok
1886*c5c4113dSnw141292 ** to change the content of the page.
1887*c5c4113dSnw141292 */
sqlitepager_iswriteable(void * pData)1888*c5c4113dSnw141292 int sqlitepager_iswriteable(void *pData){
1889*c5c4113dSnw141292   PgHdr *pPg = DATA_TO_PGHDR(pData);
1890*c5c4113dSnw141292   return pPg->dirty;
1891*c5c4113dSnw141292 }
1892*c5c4113dSnw141292 
1893*c5c4113dSnw141292 /*
1894*c5c4113dSnw141292 ** Replace the content of a single page with the information in the third
1895*c5c4113dSnw141292 ** argument.
1896*c5c4113dSnw141292 */
sqlitepager_overwrite(Pager * pPager,Pgno pgno,void * pData)1897*c5c4113dSnw141292 int sqlitepager_overwrite(Pager *pPager, Pgno pgno, void *pData){
1898*c5c4113dSnw141292   void *pPage;
1899*c5c4113dSnw141292   int rc;
1900*c5c4113dSnw141292 
1901*c5c4113dSnw141292   rc = sqlitepager_get(pPager, pgno, &pPage);
1902*c5c4113dSnw141292   if( rc==SQLITE_OK ){
1903*c5c4113dSnw141292     rc = sqlitepager_write(pPage);
1904*c5c4113dSnw141292     if( rc==SQLITE_OK ){
1905*c5c4113dSnw141292       memcpy(pPage, pData, SQLITE_PAGE_SIZE);
1906*c5c4113dSnw141292     }
1907*c5c4113dSnw141292     sqlitepager_unref(pPage);
1908*c5c4113dSnw141292   }
1909*c5c4113dSnw141292   return rc;
1910*c5c4113dSnw141292 }
1911*c5c4113dSnw141292 
1912*c5c4113dSnw141292 /*
1913*c5c4113dSnw141292 ** A call to this routine tells the pager that it is not necessary to
1914*c5c4113dSnw141292 ** write the information on page "pgno" back to the disk, even though
1915*c5c4113dSnw141292 ** that page might be marked as dirty.
1916*c5c4113dSnw141292 **
1917*c5c4113dSnw141292 ** The overlying software layer calls this routine when all of the data
1918*c5c4113dSnw141292 ** on the given page is unused.  The pager marks the page as clean so
1919*c5c4113dSnw141292 ** that it does not get written to disk.
1920*c5c4113dSnw141292 **
1921*c5c4113dSnw141292 ** Tests show that this optimization, together with the
1922*c5c4113dSnw141292 ** sqlitepager_dont_rollback() below, more than double the speed
1923*c5c4113dSnw141292 ** of large INSERT operations and quadruple the speed of large DELETEs.
1924*c5c4113dSnw141292 **
1925*c5c4113dSnw141292 ** When this routine is called, set the alwaysRollback flag to true.
1926*c5c4113dSnw141292 ** Subsequent calls to sqlitepager_dont_rollback() for the same page
1927*c5c4113dSnw141292 ** will thereafter be ignored.  This is necessary to avoid a problem
1928*c5c4113dSnw141292 ** where a page with data is added to the freelist during one part of
1929*c5c4113dSnw141292 ** a transaction then removed from the freelist during a later part
1930*c5c4113dSnw141292 ** of the same transaction and reused for some other purpose.  When it
1931*c5c4113dSnw141292 ** is first added to the freelist, this routine is called.  When reused,
1932*c5c4113dSnw141292 ** the dont_rollback() routine is called.  But because the page contains
1933*c5c4113dSnw141292 ** critical data, we still need to be sure it gets rolled back in spite
1934*c5c4113dSnw141292 ** of the dont_rollback() call.
1935*c5c4113dSnw141292 */
sqlitepager_dont_write(Pager * pPager,Pgno pgno)1936*c5c4113dSnw141292 void sqlitepager_dont_write(Pager *pPager, Pgno pgno){
1937*c5c4113dSnw141292   PgHdr *pPg;
1938*c5c4113dSnw141292 
1939*c5c4113dSnw141292   pPg = pager_lookup(pPager, pgno);
1940*c5c4113dSnw141292   pPg->alwaysRollback = 1;
1941*c5c4113dSnw141292   if( pPg && pPg->dirty ){
1942*c5c4113dSnw141292     if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
1943*c5c4113dSnw141292       /* If this pages is the last page in the file and the file has grown
1944*c5c4113dSnw141292       ** during the current transaction, then do NOT mark the page as clean.
1945*c5c4113dSnw141292       ** When the database file grows, we must make sure that the last page
1946*c5c4113dSnw141292       ** gets written at least once so that the disk file will be the correct
1947*c5c4113dSnw141292       ** size. If you do not write this page and the size of the file
1948*c5c4113dSnw141292       ** on the disk ends up being too small, that can lead to database
1949*c5c4113dSnw141292       ** corruption during the next transaction.
1950*c5c4113dSnw141292       */
1951*c5c4113dSnw141292     }else{
1952*c5c4113dSnw141292       TRACE2("DONT_WRITE %d\n", pgno);
1953*c5c4113dSnw141292       pPg->dirty = 0;
1954*c5c4113dSnw141292     }
1955*c5c4113dSnw141292   }
1956*c5c4113dSnw141292 }
1957*c5c4113dSnw141292 
1958*c5c4113dSnw141292 /*
1959*c5c4113dSnw141292 ** A call to this routine tells the pager that if a rollback occurs,
1960*c5c4113dSnw141292 ** it is not necessary to restore the data on the given page.  This
1961*c5c4113dSnw141292 ** means that the pager does not have to record the given page in the
1962*c5c4113dSnw141292 ** rollback journal.
1963*c5c4113dSnw141292 */
sqlitepager_dont_rollback(void * pData)1964*c5c4113dSnw141292 void sqlitepager_dont_rollback(void *pData){
1965*c5c4113dSnw141292   PgHdr *pPg = DATA_TO_PGHDR(pData);
1966*c5c4113dSnw141292   Pager *pPager = pPg->pPager;
1967*c5c4113dSnw141292 
1968*c5c4113dSnw141292   if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return;
1969*c5c4113dSnw141292   if( pPg->alwaysRollback || pPager->alwaysRollback ) return;
1970*c5c4113dSnw141292   if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
1971*c5c4113dSnw141292     assert( pPager->aInJournal!=0 );
1972*c5c4113dSnw141292     pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1973*c5c4113dSnw141292     pPg->inJournal = 1;
1974*c5c4113dSnw141292     if( pPager->ckptInUse ){
1975*c5c4113dSnw141292       pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1976*c5c4113dSnw141292       page_add_to_ckpt_list(pPg);
1977*c5c4113dSnw141292     }
1978*c5c4113dSnw141292     TRACE2("DONT_ROLLBACK %d\n", pPg->pgno);
1979*c5c4113dSnw141292   }
1980*c5c4113dSnw141292   if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
1981*c5c4113dSnw141292     assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
1982*c5c4113dSnw141292     assert( pPager->aInCkpt!=0 );
1983*c5c4113dSnw141292     pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1984*c5c4113dSnw141292     page_add_to_ckpt_list(pPg);
1985*c5c4113dSnw141292   }
1986*c5c4113dSnw141292 }
1987*c5c4113dSnw141292 
1988*c5c4113dSnw141292 /*
1989*c5c4113dSnw141292 ** Commit all changes to the database and release the write lock.
1990*c5c4113dSnw141292 **
1991*c5c4113dSnw141292 ** If the commit fails for any reason, a rollback attempt is made
1992*c5c4113dSnw141292 ** and an error code is returned.  If the commit worked, SQLITE_OK
1993*c5c4113dSnw141292 ** is returned.
1994*c5c4113dSnw141292 */
sqlitepager_commit(Pager * pPager)1995*c5c4113dSnw141292 int sqlitepager_commit(Pager *pPager){
1996*c5c4113dSnw141292   int rc;
1997*c5c4113dSnw141292   PgHdr *pPg;
1998*c5c4113dSnw141292 
1999*c5c4113dSnw141292   if( pPager->errMask==PAGER_ERR_FULL ){
2000*c5c4113dSnw141292     rc = sqlitepager_rollback(pPager);
2001*c5c4113dSnw141292     if( rc==SQLITE_OK ){
2002*c5c4113dSnw141292       rc = SQLITE_FULL;
2003*c5c4113dSnw141292     }
2004*c5c4113dSnw141292     return rc;
2005*c5c4113dSnw141292   }
2006*c5c4113dSnw141292   if( pPager->errMask!=0 ){
2007*c5c4113dSnw141292     rc = pager_errcode(pPager);
2008*c5c4113dSnw141292     return rc;
2009*c5c4113dSnw141292   }
2010*c5c4113dSnw141292   if( pPager->state!=SQLITE_WRITELOCK ){
2011*c5c4113dSnw141292     return SQLITE_ERROR;
2012*c5c4113dSnw141292   }
2013*c5c4113dSnw141292   TRACE1("COMMIT\n");
2014*c5c4113dSnw141292   if( pPager->dirtyFile==0 ){
2015*c5c4113dSnw141292     /* Exit early (without doing the time-consuming sqliteOsSync() calls)
2016*c5c4113dSnw141292     ** if there have been no changes to the database file. */
2017*c5c4113dSnw141292     assert( pPager->needSync==0 );
2018*c5c4113dSnw141292     rc = pager_unwritelock(pPager);
2019*c5c4113dSnw141292     pPager->dbSize = -1;
2020*c5c4113dSnw141292     return rc;
2021*c5c4113dSnw141292   }
2022*c5c4113dSnw141292   assert( pPager->journalOpen );
2023*c5c4113dSnw141292   rc = syncJournal(pPager);
2024*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
2025*c5c4113dSnw141292     goto commit_abort;
2026*c5c4113dSnw141292   }
2027*c5c4113dSnw141292   pPg = pager_get_all_dirty_pages(pPager);
2028*c5c4113dSnw141292   if( pPg ){
2029*c5c4113dSnw141292     rc = pager_write_pagelist(pPg);
2030*c5c4113dSnw141292     if( rc || (!pPager->noSync && sqliteOsSync(&pPager->fd)!=SQLITE_OK) ){
2031*c5c4113dSnw141292       goto commit_abort;
2032*c5c4113dSnw141292     }
2033*c5c4113dSnw141292   }
2034*c5c4113dSnw141292   rc = pager_unwritelock(pPager);
2035*c5c4113dSnw141292   pPager->dbSize = -1;
2036*c5c4113dSnw141292   return rc;
2037*c5c4113dSnw141292 
2038*c5c4113dSnw141292   /* Jump here if anything goes wrong during the commit process.
2039*c5c4113dSnw141292   */
2040*c5c4113dSnw141292 commit_abort:
2041*c5c4113dSnw141292   rc = sqlitepager_rollback(pPager);
2042*c5c4113dSnw141292   if( rc==SQLITE_OK ){
2043*c5c4113dSnw141292     rc = SQLITE_FULL;
2044*c5c4113dSnw141292   }
2045*c5c4113dSnw141292   return rc;
2046*c5c4113dSnw141292 }
2047*c5c4113dSnw141292 
2048*c5c4113dSnw141292 /*
2049*c5c4113dSnw141292 ** Rollback all changes.  The database falls back to read-only mode.
2050*c5c4113dSnw141292 ** All in-memory cache pages revert to their original data contents.
2051*c5c4113dSnw141292 ** The journal is deleted.
2052*c5c4113dSnw141292 **
2053*c5c4113dSnw141292 ** This routine cannot fail unless some other process is not following
2054*c5c4113dSnw141292 ** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
2055*c5c4113dSnw141292 ** process is writing trash into the journal file (SQLITE_CORRUPT) or
2056*c5c4113dSnw141292 ** unless a prior malloc() failed (SQLITE_NOMEM).  Appropriate error
2057*c5c4113dSnw141292 ** codes are returned for all these occasions.  Otherwise,
2058*c5c4113dSnw141292 ** SQLITE_OK is returned.
2059*c5c4113dSnw141292 */
sqlitepager_rollback(Pager * pPager)2060*c5c4113dSnw141292 int sqlitepager_rollback(Pager *pPager){
2061*c5c4113dSnw141292   int rc;
2062*c5c4113dSnw141292   TRACE1("ROLLBACK\n");
2063*c5c4113dSnw141292   if( !pPager->dirtyFile || !pPager->journalOpen ){
2064*c5c4113dSnw141292     rc = pager_unwritelock(pPager);
2065*c5c4113dSnw141292     pPager->dbSize = -1;
2066*c5c4113dSnw141292     return rc;
2067*c5c4113dSnw141292   }
2068*c5c4113dSnw141292 
2069*c5c4113dSnw141292   if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
2070*c5c4113dSnw141292     if( pPager->state>=SQLITE_WRITELOCK ){
2071*c5c4113dSnw141292       pager_playback(pPager, 1);
2072*c5c4113dSnw141292     }
2073*c5c4113dSnw141292     return pager_errcode(pPager);
2074*c5c4113dSnw141292   }
2075*c5c4113dSnw141292   if( pPager->state!=SQLITE_WRITELOCK ){
2076*c5c4113dSnw141292     return SQLITE_OK;
2077*c5c4113dSnw141292   }
2078*c5c4113dSnw141292   rc = pager_playback(pPager, 1);
2079*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
2080*c5c4113dSnw141292     rc = SQLITE_CORRUPT;
2081*c5c4113dSnw141292     pPager->errMask |= PAGER_ERR_CORRUPT;
2082*c5c4113dSnw141292   }
2083*c5c4113dSnw141292   pPager->dbSize = -1;
2084*c5c4113dSnw141292   return rc;
2085*c5c4113dSnw141292 }
2086*c5c4113dSnw141292 
2087*c5c4113dSnw141292 /*
2088*c5c4113dSnw141292 ** Return TRUE if the database file is opened read-only.  Return FALSE
2089*c5c4113dSnw141292 ** if the database is (in theory) writable.
2090*c5c4113dSnw141292 */
sqlitepager_isreadonly(Pager * pPager)2091*c5c4113dSnw141292 int sqlitepager_isreadonly(Pager *pPager){
2092*c5c4113dSnw141292   return pPager->readOnly;
2093*c5c4113dSnw141292 }
2094*c5c4113dSnw141292 
2095*c5c4113dSnw141292 /*
2096*c5c4113dSnw141292 ** This routine is used for testing and analysis only.
2097*c5c4113dSnw141292 */
sqlitepager_stats(Pager * pPager)2098*c5c4113dSnw141292 int *sqlitepager_stats(Pager *pPager){
2099*c5c4113dSnw141292   static int a[9];
2100*c5c4113dSnw141292   a[0] = pPager->nRef;
2101*c5c4113dSnw141292   a[1] = pPager->nPage;
2102*c5c4113dSnw141292   a[2] = pPager->mxPage;
2103*c5c4113dSnw141292   a[3] = pPager->dbSize;
2104*c5c4113dSnw141292   a[4] = pPager->state;
2105*c5c4113dSnw141292   a[5] = pPager->errMask;
2106*c5c4113dSnw141292   a[6] = pPager->nHit;
2107*c5c4113dSnw141292   a[7] = pPager->nMiss;
2108*c5c4113dSnw141292   a[8] = pPager->nOvfl;
2109*c5c4113dSnw141292   return a;
2110*c5c4113dSnw141292 }
2111*c5c4113dSnw141292 
2112*c5c4113dSnw141292 /*
2113*c5c4113dSnw141292 ** Set the checkpoint.
2114*c5c4113dSnw141292 **
2115*c5c4113dSnw141292 ** This routine should be called with the transaction journal already
2116*c5c4113dSnw141292 ** open.  A new checkpoint journal is created that can be used to rollback
2117*c5c4113dSnw141292 ** changes of a single SQL command within a larger transaction.
2118*c5c4113dSnw141292 */
sqlitepager_ckpt_begin(Pager * pPager)2119*c5c4113dSnw141292 int sqlitepager_ckpt_begin(Pager *pPager){
2120*c5c4113dSnw141292   int rc;
2121*c5c4113dSnw141292   char zTemp[SQLITE_TEMPNAME_SIZE];
2122*c5c4113dSnw141292   if( !pPager->journalOpen ){
2123*c5c4113dSnw141292     pPager->ckptAutoopen = 1;
2124*c5c4113dSnw141292     return SQLITE_OK;
2125*c5c4113dSnw141292   }
2126*c5c4113dSnw141292   assert( pPager->journalOpen );
2127*c5c4113dSnw141292   assert( !pPager->ckptInUse );
2128*c5c4113dSnw141292   pPager->aInCkpt = sqliteMalloc( pPager->dbSize/8 + 1 );
2129*c5c4113dSnw141292   if( pPager->aInCkpt==0 ){
2130*c5c4113dSnw141292     sqliteOsReadLock(&pPager->fd);
2131*c5c4113dSnw141292     return SQLITE_NOMEM;
2132*c5c4113dSnw141292   }
2133*c5c4113dSnw141292 #ifndef NDEBUG
2134*c5c4113dSnw141292   rc = sqliteOsFileSize(&pPager->jfd, &pPager->ckptJSize);
2135*c5c4113dSnw141292   if( rc ) goto ckpt_begin_failed;
2136*c5c4113dSnw141292   assert( pPager->ckptJSize ==
2137*c5c4113dSnw141292     pPager->nRec*JOURNAL_PG_SZ(journal_format)+JOURNAL_HDR_SZ(journal_format) );
2138*c5c4113dSnw141292 #endif
2139*c5c4113dSnw141292   pPager->ckptJSize = pPager->nRec*JOURNAL_PG_SZ(journal_format)
2140*c5c4113dSnw141292                          + JOURNAL_HDR_SZ(journal_format);
2141*c5c4113dSnw141292   pPager->ckptSize = pPager->dbSize;
2142*c5c4113dSnw141292   if( !pPager->ckptOpen ){
2143*c5c4113dSnw141292     rc = sqlitepager_opentemp(zTemp, &pPager->cpfd);
2144*c5c4113dSnw141292     if( rc ) goto ckpt_begin_failed;
2145*c5c4113dSnw141292     pPager->ckptOpen = 1;
2146*c5c4113dSnw141292     pPager->ckptNRec = 0;
2147*c5c4113dSnw141292   }
2148*c5c4113dSnw141292   pPager->ckptInUse = 1;
2149*c5c4113dSnw141292   return SQLITE_OK;
2150*c5c4113dSnw141292 
2151*c5c4113dSnw141292 ckpt_begin_failed:
2152*c5c4113dSnw141292   if( pPager->aInCkpt ){
2153*c5c4113dSnw141292     sqliteFree(pPager->aInCkpt);
2154*c5c4113dSnw141292     pPager->aInCkpt = 0;
2155*c5c4113dSnw141292   }
2156*c5c4113dSnw141292   return rc;
2157*c5c4113dSnw141292 }
2158*c5c4113dSnw141292 
2159*c5c4113dSnw141292 /*
2160*c5c4113dSnw141292 ** Commit a checkpoint.
2161*c5c4113dSnw141292 */
sqlitepager_ckpt_commit(Pager * pPager)2162*c5c4113dSnw141292 int sqlitepager_ckpt_commit(Pager *pPager){
2163*c5c4113dSnw141292   if( pPager->ckptInUse ){
2164*c5c4113dSnw141292     PgHdr *pPg, *pNext;
2165*c5c4113dSnw141292     sqliteOsSeek(&pPager->cpfd, 0);
2166*c5c4113dSnw141292     /* sqliteOsTruncate(&pPager->cpfd, 0); */
2167*c5c4113dSnw141292     pPager->ckptNRec = 0;
2168*c5c4113dSnw141292     pPager->ckptInUse = 0;
2169*c5c4113dSnw141292     sqliteFree( pPager->aInCkpt );
2170*c5c4113dSnw141292     pPager->aInCkpt = 0;
2171*c5c4113dSnw141292     for(pPg=pPager->pCkpt; pPg; pPg=pNext){
2172*c5c4113dSnw141292       pNext = pPg->pNextCkpt;
2173*c5c4113dSnw141292       assert( pPg->inCkpt );
2174*c5c4113dSnw141292       pPg->inCkpt = 0;
2175*c5c4113dSnw141292       pPg->pPrevCkpt = pPg->pNextCkpt = 0;
2176*c5c4113dSnw141292     }
2177*c5c4113dSnw141292     pPager->pCkpt = 0;
2178*c5c4113dSnw141292   }
2179*c5c4113dSnw141292   pPager->ckptAutoopen = 0;
2180*c5c4113dSnw141292   return SQLITE_OK;
2181*c5c4113dSnw141292 }
2182*c5c4113dSnw141292 
2183*c5c4113dSnw141292 /*
2184*c5c4113dSnw141292 ** Rollback a checkpoint.
2185*c5c4113dSnw141292 */
sqlitepager_ckpt_rollback(Pager * pPager)2186*c5c4113dSnw141292 int sqlitepager_ckpt_rollback(Pager *pPager){
2187*c5c4113dSnw141292   int rc;
2188*c5c4113dSnw141292   if( pPager->ckptInUse ){
2189*c5c4113dSnw141292     rc = pager_ckpt_playback(pPager);
2190*c5c4113dSnw141292     sqlitepager_ckpt_commit(pPager);
2191*c5c4113dSnw141292   }else{
2192*c5c4113dSnw141292     rc = SQLITE_OK;
2193*c5c4113dSnw141292   }
2194*c5c4113dSnw141292   pPager->ckptAutoopen = 0;
2195*c5c4113dSnw141292   return rc;
2196*c5c4113dSnw141292 }
2197*c5c4113dSnw141292 
2198*c5c4113dSnw141292 /*
2199*c5c4113dSnw141292 ** Return the full pathname of the database file.
2200*c5c4113dSnw141292 */
sqlitepager_filename(Pager * pPager)2201*c5c4113dSnw141292 const char *sqlitepager_filename(Pager *pPager){
2202*c5c4113dSnw141292   return pPager->zFilename;
2203*c5c4113dSnw141292 }
2204*c5c4113dSnw141292 
2205*c5c4113dSnw141292 /*
2206*c5c4113dSnw141292 ** Set the codec for this pager
2207*c5c4113dSnw141292 */
sqlitepager_set_codec(Pager * pPager,void (* xCodec)(void *,void *,Pgno,int),void * pCodecArg)2208*c5c4113dSnw141292 void sqlitepager_set_codec(
2209*c5c4113dSnw141292   Pager *pPager,
2210*c5c4113dSnw141292   void (*xCodec)(void*,void*,Pgno,int),
2211*c5c4113dSnw141292   void *pCodecArg
2212*c5c4113dSnw141292 ){
2213*c5c4113dSnw141292   pPager->xCodec = xCodec;
2214*c5c4113dSnw141292   pPager->pCodecArg = pCodecArg;
2215*c5c4113dSnw141292 }
2216*c5c4113dSnw141292 
2217*c5c4113dSnw141292 #ifdef SQLITE_TEST
2218*c5c4113dSnw141292 /*
2219*c5c4113dSnw141292 ** Print a listing of all referenced pages and their ref count.
2220*c5c4113dSnw141292 */
sqlitepager_refdump(Pager * pPager)2221*c5c4113dSnw141292 void sqlitepager_refdump(Pager *pPager){
2222*c5c4113dSnw141292   PgHdr *pPg;
2223*c5c4113dSnw141292   for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2224*c5c4113dSnw141292     if( pPg->nRef<=0 ) continue;
2225*c5c4113dSnw141292     printf("PAGE %3d addr=0x%08x nRef=%d\n",
2226*c5c4113dSnw141292        pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
2227*c5c4113dSnw141292   }
2228*c5c4113dSnw141292 }
2229*c5c4113dSnw141292 #endif
2230