monotone

monotone Mtn Source Tree

Root/sqlite/pager.c

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

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status