extensions/sqlite/sqlite-source/pager.c
changeset 1952 b7aa2b626807
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sqlite/sqlite-source/pager.c	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,5104 @@
     1.4 +/*
     1.5 +** 2001 September 15
     1.6 +**
     1.7 +** The author disclaims copyright to this source code.  In place of
     1.8 +** a legal notice, here is a blessing:
     1.9 +**
    1.10 +**    May you do good and not evil.
    1.11 +**    May you find forgiveness for yourself and forgive others.
    1.12 +**    May you share freely, never taking more than you give.
    1.13 +**
    1.14 +*************************************************************************
    1.15 +** This is the implementation of the page cache subsystem or "pager".
    1.16 +** 
    1.17 +** The pager is used to access a database disk file.  It implements
    1.18 +** atomic commit and rollback through the use of a journal file that
    1.19 +** is separate from the database file.  The pager also implements file
    1.20 +** locking to prevent two processes from writing the same database
    1.21 +** file simultaneously, or one process from reading the database while
    1.22 +** another is writing.
    1.23 +**
    1.24 +** @(#) $Id$
    1.25 +*/
    1.26 +#ifndef SQLITE_OMIT_DISKIO
    1.27 +#include "sqliteInt.h"
    1.28 +#include <assert.h>
    1.29 +#include <string.h>
    1.30 +
    1.31 +/*
    1.32 +** Macros for troubleshooting.  Normally turned off
    1.33 +*/
    1.34 +#if 0
    1.35 +#define sqlite3DebugPrintf printf
    1.36 +#define PAGERTRACE1(X)       sqlite3DebugPrintf(X)
    1.37 +#define PAGERTRACE2(X,Y)     sqlite3DebugPrintf(X,Y)
    1.38 +#define PAGERTRACE3(X,Y,Z)   sqlite3DebugPrintf(X,Y,Z)
    1.39 +#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
    1.40 +#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
    1.41 +#else
    1.42 +#define PAGERTRACE1(X)
    1.43 +#define PAGERTRACE2(X,Y)
    1.44 +#define PAGERTRACE3(X,Y,Z)
    1.45 +#define PAGERTRACE4(X,Y,Z,W)
    1.46 +#define PAGERTRACE5(X,Y,Z,W,V)
    1.47 +#endif
    1.48 +
    1.49 +/*
    1.50 +** The following two macros are used within the PAGERTRACEX() macros above
    1.51 +** to print out file-descriptors. 
    1.52 +**
    1.53 +** PAGERID() takes a pointer to a Pager struct as it's argument. The
    1.54 +** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
    1.55 +** struct as it's argument.
    1.56 +*/
    1.57 +#define PAGERID(p) ((int)(p->fd))
    1.58 +#define FILEHANDLEID(fd) ((int)fd)
    1.59 +
    1.60 +/*
    1.61 +** The page cache as a whole is always in one of the following
    1.62 +** states:
    1.63 +**
    1.64 +**   PAGER_UNLOCK        The page cache is not currently reading or 
    1.65 +**                       writing the database file.  There is no
    1.66 +**                       data held in memory.  This is the initial
    1.67 +**                       state.
    1.68 +**
    1.69 +**   PAGER_SHARED        The page cache is reading the database.
    1.70 +**                       Writing is not permitted.  There can be
    1.71 +**                       multiple readers accessing the same database
    1.72 +**                       file at the same time.
    1.73 +**
    1.74 +**   PAGER_RESERVED      This process has reserved the database for writing
    1.75 +**                       but has not yet made any changes.  Only one process
    1.76 +**                       at a time can reserve the database.  The original
    1.77 +**                       database file has not been modified so other
    1.78 +**                       processes may still be reading the on-disk
    1.79 +**                       database file.
    1.80 +**
    1.81 +**   PAGER_EXCLUSIVE     The page cache is writing the database.
    1.82 +**                       Access is exclusive.  No other processes or
    1.83 +**                       threads can be reading or writing while one
    1.84 +**                       process is writing.
    1.85 +**
    1.86 +**   PAGER_SYNCED        The pager moves to this state from PAGER_EXCLUSIVE
    1.87 +**                       after all dirty pages have been written to the
    1.88 +**                       database file and the file has been synced to
    1.89 +**                       disk. All that remains to do is to remove or
    1.90 +**                       truncate the journal file and the transaction 
    1.91 +**                       will be committed.
    1.92 +**
    1.93 +** The page cache comes up in PAGER_UNLOCK.  The first time a
    1.94 +** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
    1.95 +** After all pages have been released using sqlite_page_unref(),
    1.96 +** the state transitions back to PAGER_UNLOCK.  The first time
    1.97 +** that sqlite3PagerWrite() is called, the state transitions to
    1.98 +** PAGER_RESERVED.  (Note that sqlite3PagerWrite() can only be
    1.99 +** called on an outstanding page which means that the pager must
   1.100 +** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
   1.101 +** PAGER_RESERVED means that there is an open rollback journal.
   1.102 +** The transition to PAGER_EXCLUSIVE occurs before any changes
   1.103 +** are made to the database file, though writes to the rollback
   1.104 +** journal occurs with just PAGER_RESERVED.  After an sqlite3PagerRollback()
   1.105 +** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
   1.106 +** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
   1.107 +*/
   1.108 +#define PAGER_UNLOCK      0
   1.109 +#define PAGER_SHARED      1   /* same as SHARED_LOCK */
   1.110 +#define PAGER_RESERVED    2   /* same as RESERVED_LOCK */
   1.111 +#define PAGER_EXCLUSIVE   4   /* same as EXCLUSIVE_LOCK */
   1.112 +#define PAGER_SYNCED      5
   1.113 +
   1.114 +/*
   1.115 +** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
   1.116 +** then failed attempts to get a reserved lock will invoke the busy callback.
   1.117 +** This is off by default.  To see why, consider the following scenario:
   1.118 +** 
   1.119 +** Suppose thread A already has a shared lock and wants a reserved lock.
   1.120 +** Thread B already has a reserved lock and wants an exclusive lock.  If
   1.121 +** both threads are using their busy callbacks, it might be a long time
   1.122 +** be for one of the threads give up and allows the other to proceed.
   1.123 +** But if the thread trying to get the reserved lock gives up quickly
   1.124 +** (if it never invokes its busy callback) then the contention will be
   1.125 +** resolved quickly.
   1.126 +*/
   1.127 +#ifndef SQLITE_BUSY_RESERVED_LOCK
   1.128 +# define SQLITE_BUSY_RESERVED_LOCK 0
   1.129 +#endif
   1.130 +
   1.131 +/*
   1.132 +** This macro rounds values up so that if the value is an address it
   1.133 +** is guaranteed to be an address that is aligned to an 8-byte boundary.
   1.134 +*/
   1.135 +#define FORCE_ALIGNMENT(X)   (((X)+7)&~7)
   1.136 +
   1.137 +typedef struct PgHdr PgHdr;
   1.138 +
   1.139 +/*
   1.140 +** Each pager stores all currently unreferenced pages in a list sorted
   1.141 +** in least-recently-used (LRU) order (i.e. the first item on the list has 
   1.142 +** not been referenced in a long time, the last item has been recently
   1.143 +** used). An instance of this structure is included as part of each
   1.144 +** pager structure for this purpose (variable Pager.lru).
   1.145 +**
   1.146 +** Additionally, if memory-management is enabled, all unreferenced pages 
   1.147 +** are stored in a global LRU list (global variable sqlite3LruPageList).
   1.148 +**
   1.149 +** In both cases, the PagerLruList.pFirstSynced variable points to
   1.150 +** the first page in the corresponding list that does not require an
   1.151 +** fsync() operation before it's memory can be reclaimed. If no such
   1.152 +** page exists, PagerLruList.pFirstSynced is set to NULL.
   1.153 +*/
   1.154 +typedef struct PagerLruList PagerLruList;
   1.155 +struct PagerLruList {
   1.156 +  PgHdr *pFirst;         /* First page in LRU list */
   1.157 +  PgHdr *pLast;          /* Last page in LRU list (the most recently used) */
   1.158 +  PgHdr *pFirstSynced;   /* First page in list with PgHdr.needSync==0 */
   1.159 +};
   1.160 +
   1.161 +/*
   1.162 +** The following structure contains the next and previous pointers used
   1.163 +** to link a PgHdr structure into a PagerLruList linked list. 
   1.164 +*/
   1.165 +typedef struct PagerLruLink PagerLruLink;
   1.166 +struct PagerLruLink {
   1.167 +  PgHdr *pNext;
   1.168 +  PgHdr *pPrev;
   1.169 +};
   1.170 +
   1.171 +/*
   1.172 +** Each in-memory image of a page begins with the following header.
   1.173 +** This header is only visible to this pager module.  The client
   1.174 +** code that calls pager sees only the data that follows the header.
   1.175 +**
   1.176 +** Client code should call sqlite3PagerWrite() on a page prior to making
   1.177 +** any modifications to that page.  The first time sqlite3PagerWrite()
   1.178 +** is called, the original page contents are written into the rollback
   1.179 +** journal and PgHdr.inJournal and PgHdr.needSync are set.  Later, once
   1.180 +** the journal page has made it onto the disk surface, PgHdr.needSync
   1.181 +** is cleared.  The modified page cannot be written back into the original
   1.182 +** database file until the journal pages has been synced to disk and the
   1.183 +** PgHdr.needSync has been cleared.
   1.184 +**
   1.185 +** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
   1.186 +** is cleared again when the page content is written back to the original
   1.187 +** database file.
   1.188 +**
   1.189 +** Details of important structure elements:
   1.190 +**
   1.191 +** needSync
   1.192 +**
   1.193 +**     If this is true, this means that it is not safe to write the page
   1.194 +**     content to the database because the original content needed
   1.195 +**     for rollback has not by synced to the main rollback journal.
   1.196 +**     The original content may have been written to the rollback journal
   1.197 +**     but it has not yet been synced.  So we cannot write to the database
   1.198 +**     file because power failure might cause the page in the journal file
   1.199 +**     to never reach the disk.  It is as if the write to the journal file
   1.200 +**     does not occur until the journal file is synced.
   1.201 +**     
   1.202 +**     This flag is false if the page content exactly matches what
   1.203 +**     currently exists in the database file.  The needSync flag is also
   1.204 +**     false if the original content has been written to the main rollback
   1.205 +**     journal and synced.  If the page represents a new page that has
   1.206 +**     been added onto the end of the database during the current
   1.207 +**     transaction, the needSync flag is true until the original database
   1.208 +**     size in the journal header has been synced to disk.
   1.209 +**
   1.210 +** inJournal
   1.211 +**
   1.212 +**     This is true if the original page has been written into the main
   1.213 +**     rollback journal.  This is always false for new pages added to
   1.214 +**     the end of the database file during the current transaction.
   1.215 +**     And this flag says nothing about whether or not the journal
   1.216 +**     has been synced to disk.  For pages that are in the original
   1.217 +**     database file, the following expression should always be true:
   1.218 +**
   1.219 +**       inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0
   1.220 +**
   1.221 +**     The pPager->aInJournal[] array is only valid for the original
   1.222 +**     pages of the database, not new pages that are added to the end
   1.223 +**     of the database, so obviously the above expression cannot be
   1.224 +**     valid for new pages.  For new pages inJournal is always 0.
   1.225 +**
   1.226 +** dirty
   1.227 +**
   1.228 +**     When true, this means that the content of the page has been
   1.229 +**     modified and needs to be written back to the database file.
   1.230 +**     If false, it means that either the content of the page is
   1.231 +**     unchanged or else the content is unimportant and we do not
   1.232 +**     care whether or not it is preserved.
   1.233 +**
   1.234 +** alwaysRollback
   1.235 +**
   1.236 +**     This means that the sqlite3PagerDontRollback() API should be
   1.237 +**     ignored for this page.  The DontRollback() API attempts to say
   1.238 +**     that the content of the page on disk is unimportant (it is an
   1.239 +**     unused page on the freelist) so that it is unnecessary to 
   1.240 +**     rollback changes to this page because the content of the page
   1.241 +**     can change without changing the meaning of the database.  This
   1.242 +**     flag overrides any DontRollback() attempt.  This flag is set
   1.243 +**     when a page that originally contained valid data is added to
   1.244 +**     the freelist.  Later in the same transaction, this page might
   1.245 +**     be pulled from the freelist and reused for something different
   1.246 +**     and at that point the DontRollback() API will be called because
   1.247 +**     pages taken from the freelist do not need to be protected by
   1.248 +**     the rollback journal.  But this flag says that the page was
   1.249 +**     not originally part of the freelist so that it still needs to
   1.250 +**     be rolled back in spite of any subsequent DontRollback() calls.
   1.251 +**
   1.252 +** needRead 
   1.253 +**
   1.254 +**     This flag means (when true) that the content of the page has
   1.255 +**     not yet been loaded from disk.  The in-memory content is just
   1.256 +**     garbage.  (Actually, we zero the content, but you should not
   1.257 +**     make any assumptions about the content nevertheless.)  If the
   1.258 +**     content is needed in the future, it should be read from the
   1.259 +**     original database file.
   1.260 +*/
   1.261 +struct PgHdr {
   1.262 +  Pager *pPager;                 /* The pager to which this page belongs */
   1.263 +  Pgno pgno;                     /* The page number for this page */
   1.264 +  PgHdr *pNextHash, *pPrevHash;  /* Hash collision chain for PgHdr.pgno */
   1.265 +  PagerLruLink free;             /* Next and previous free pages */
   1.266 +  PgHdr *pNextAll;               /* A list of all pages */
   1.267 +  u8 inJournal;                  /* TRUE if has been written to journal */
   1.268 +  u8 dirty;                      /* TRUE if we need to write back changes */
   1.269 +  u8 needSync;                   /* Sync journal before writing this page */
   1.270 +  u8 alwaysRollback;             /* Disable DontRollback() for this page */
   1.271 +  u8 needRead;                   /* Read content if PagerWrite() is called */
   1.272 +  short int nRef;                /* Number of users of this page */
   1.273 +  PgHdr *pDirty, *pPrevDirty;    /* Dirty pages */
   1.274 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.275 +  PagerLruLink gfree;            /* Global list of nRef==0 pages */
   1.276 +#endif
   1.277 +#ifdef SQLITE_CHECK_PAGES
   1.278 +  u32 pageHash;
   1.279 +#endif
   1.280 +  void *pData;                   /* Page data */
   1.281 +  /* Pager.nExtra bytes of local data appended to this header */
   1.282 +};
   1.283 +
   1.284 +/*
   1.285 +** For an in-memory only database, some extra information is recorded about
   1.286 +** each page so that changes can be rolled back.  (Journal files are not
   1.287 +** used for in-memory databases.)  The following information is added to
   1.288 +** the end of every EXTRA block for in-memory databases.
   1.289 +**
   1.290 +** This information could have been added directly to the PgHdr structure.
   1.291 +** But then it would take up an extra 8 bytes of storage on every PgHdr
   1.292 +** even for disk-based databases.  Splitting it out saves 8 bytes.  This
   1.293 +** is only a savings of 0.8% but those percentages add up.
   1.294 +*/
   1.295 +typedef struct PgHistory PgHistory;
   1.296 +struct PgHistory {
   1.297 +  u8 *pOrig;     /* Original page text.  Restore to this on a full rollback */
   1.298 +  u8 *pStmt;     /* Text as it was at the beginning of the current statement */
   1.299 +  PgHdr *pNextStmt, *pPrevStmt;  /* List of pages in the statement journal */
   1.300 +  u8 inStmt;                     /* TRUE if in the statement subjournal */
   1.301 +};
   1.302 +
   1.303 +/*
   1.304 +** A macro used for invoking the codec if there is one
   1.305 +*/
   1.306 +#ifdef SQLITE_HAS_CODEC
   1.307 +# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
   1.308 +# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
   1.309 +#else
   1.310 +# define CODEC1(P,D,N,X) /* NO-OP */
   1.311 +# define CODEC2(P,D,N,X) ((char*)D)
   1.312 +#endif
   1.313 +
   1.314 +/*
   1.315 +** Convert a pointer to a PgHdr into a pointer to its data
   1.316 +** and back again.
   1.317 +*/
   1.318 +#define PGHDR_TO_DATA(P)    ((P)->pData)
   1.319 +#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
   1.320 +#define PGHDR_TO_HIST(P,PGR)  \
   1.321 +            ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
   1.322 +
   1.323 +/*
   1.324 +** A open page cache is an instance of the following structure.
   1.325 +**
   1.326 +** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
   1.327 +** or SQLITE_FULL. Once one of the first three errors occurs, it persists
   1.328 +** and is returned as the result of every major pager API call.  The
   1.329 +** SQLITE_FULL return code is slightly different. It persists only until the
   1.330 +** next successful rollback is performed on the pager cache. Also,
   1.331 +** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
   1.332 +** APIs, they may still be used successfully.
   1.333 +*/
   1.334 +struct Pager {
   1.335 +  sqlite3_vfs *pVfs;          /* OS functions to use for IO */
   1.336 +  u8 journalOpen;             /* True if journal file descriptors is valid */
   1.337 +  u8 journalStarted;          /* True if header of journal is synced */
   1.338 +  u8 useJournal;              /* Use a rollback journal on this file */
   1.339 +  u8 noReadlock;              /* Do not bother to obtain readlocks */
   1.340 +  u8 stmtOpen;                /* True if the statement subjournal is open */
   1.341 +  u8 stmtInUse;               /* True we are in a statement subtransaction */
   1.342 +  u8 stmtAutoopen;            /* Open stmt journal when main journal is opened*/
   1.343 +  u8 noSync;                  /* Do not sync the journal if true */
   1.344 +  u8 fullSync;                /* Do extra syncs of the journal for robustness */
   1.345 +  u8 sync_flags;              /* One of SYNC_NORMAL or SYNC_FULL */
   1.346 +  u8 state;                   /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
   1.347 +  u8 tempFile;                /* zFilename is a temporary file */
   1.348 +  u8 readOnly;                /* True for a read-only database */
   1.349 +  u8 needSync;                /* True if an fsync() is needed on the journal */
   1.350 +  u8 dirtyCache;              /* True if cached pages have changed */
   1.351 +  u8 alwaysRollback;          /* Disable DontRollback() for all pages */
   1.352 +  u8 memDb;                   /* True to inhibit all file I/O */
   1.353 +  u8 setMaster;               /* True if a m-j name has been written to jrnl */
   1.354 +  u8 doNotSync;               /* Boolean. While true, do not spill the cache */
   1.355 +  u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
   1.356 +  u8 changeCountDone;         /* Set after incrementing the change-counter */
   1.357 +  u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
   1.358 +  int errCode;                /* One of several kinds of errors */
   1.359 +  int dbSize;                 /* Number of pages in the file */
   1.360 +  int origDbSize;             /* dbSize before the current change */
   1.361 +  int stmtSize;               /* Size of database (in pages) at stmt_begin() */
   1.362 +  int nRec;                   /* Number of pages written to the journal */
   1.363 +  u32 cksumInit;              /* Quasi-random value added to every checksum */
   1.364 +  int stmtNRec;               /* Number of records in stmt subjournal */
   1.365 +  int nExtra;                 /* Add this many bytes to each in-memory page */
   1.366 +  int pageSize;               /* Number of bytes in a page */
   1.367 +  int nPage;                  /* Total number of in-memory pages */
   1.368 +  int nRef;                   /* Number of in-memory pages with PgHdr.nRef>0 */
   1.369 +  int mxPage;                 /* Maximum number of pages to hold in cache */
   1.370 +  Pgno mxPgno;                /* Maximum allowed size of the database */
   1.371 +  u8 *aInJournal;             /* One bit for each page in the database file */
   1.372 +  u8 *aInStmt;                /* One bit for each page in the database */
   1.373 +  char *zFilename;            /* Name of the database file */
   1.374 +  char *zJournal;             /* Name of the journal file */
   1.375 +  char *zDirectory;           /* Directory hold database and journal files */
   1.376 +  char *zStmtJrnl;            /* Name of the statement journal file */
   1.377 +  sqlite3_file *fd, *jfd;     /* File descriptors for database and journal */
   1.378 +  sqlite3_file *stfd;         /* File descriptor for the statement subjournal*/
   1.379 +  BusyHandler *pBusyHandler;  /* Pointer to sqlite.busyHandler */
   1.380 +  PagerLruList lru;           /* LRU list of free pages */
   1.381 +  PgHdr *pAll;                /* List of all pages */
   1.382 +  PgHdr *pStmt;               /* List of pages in the statement subjournal */
   1.383 +  PgHdr *pDirty;              /* List of all dirty pages */
   1.384 +  i64 journalOff;             /* Current byte offset in the journal file */
   1.385 +  i64 journalHdr;             /* Byte offset to previous journal header */
   1.386 +  i64 stmtHdrOff;             /* First journal header written this statement */
   1.387 +  i64 stmtCksum;              /* cksumInit when statement was started */
   1.388 +  i64 stmtJSize;              /* Size of journal at stmt_begin() */
   1.389 +  int sectorSize;             /* Assumed sector size during rollback */
   1.390 +#ifdef SQLITE_TEST
   1.391 +  int nHit, nMiss;            /* Cache hits and missing */
   1.392 +  int nRead, nWrite;          /* Database pages read/written */
   1.393 +#endif
   1.394 +  void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
   1.395 +  void (*xReiniter)(DbPage*,int);   /* Call this routine when reloading pages */
   1.396 +#ifdef SQLITE_HAS_CODEC
   1.397 +  void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
   1.398 +  void *pCodecArg;            /* First argument to xCodec() */
   1.399 +#endif
   1.400 +  int nHash;                  /* Size of the pager hash table */
   1.401 +  PgHdr **aHash;              /* Hash table to map page number to PgHdr */
   1.402 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.403 +  Pager *pNext;               /* Doubly linked list of pagers on which */
   1.404 +  Pager *pPrev;               /* sqlite3_release_memory() will work */
   1.405 +  int iInUseMM;               /* Non-zero if unavailable to MM */
   1.406 +  int iInUseDB;               /* Non-zero if in sqlite3_release_memory() */
   1.407 +#endif
   1.408 +  char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
   1.409 +  char dbFileVers[16];        /* Changes whenever database file changes */
   1.410 +};
   1.411 +
   1.412 +/*
   1.413 +** The following global variables hold counters used for
   1.414 +** testing purposes only.  These variables do not exist in
   1.415 +** a non-testing build.  These variables are not thread-safe.
   1.416 +*/
   1.417 +#ifdef SQLITE_TEST
   1.418 +int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
   1.419 +int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
   1.420 +int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
   1.421 +int sqlite3_pager_pgfree_count = 0;    /* Number of cache pages freed */
   1.422 +# define PAGER_INCR(v)  v++
   1.423 +#else
   1.424 +# define PAGER_INCR(v)
   1.425 +#endif
   1.426 +
   1.427 +/*
   1.428 +** The following variable points to the head of a double-linked list
   1.429 +** of all pagers that are eligible for page stealing by the
   1.430 +** sqlite3_release_memory() interface.  Access to this list is
   1.431 +** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
   1.432 +*/
   1.433 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.434 +static Pager *sqlite3PagerList = 0;
   1.435 +static PagerLruList sqlite3LruPageList = {0, 0, 0};
   1.436 +#endif
   1.437 +
   1.438 +
   1.439 +/*
   1.440 +** Journal files begin with the following magic string.  The data
   1.441 +** was obtained from /dev/random.  It is used only as a sanity check.
   1.442 +**
   1.443 +** Since version 2.8.0, the journal format contains additional sanity
   1.444 +** checking information.  If the power fails while the journal is begin
   1.445 +** written, semi-random garbage data might appear in the journal
   1.446 +** file after power is restored.  If an attempt is then made
   1.447 +** to roll the journal back, the database could be corrupted.  The additional
   1.448 +** sanity checking data is an attempt to discover the garbage in the
   1.449 +** journal and ignore it.
   1.450 +**
   1.451 +** The sanity checking information for the new journal format consists
   1.452 +** of a 32-bit checksum on each page of data.  The checksum covers both
   1.453 +** the page number and the pPager->pageSize bytes of data for the page.
   1.454 +** This cksum is initialized to a 32-bit random value that appears in the
   1.455 +** journal file right after the header.  The random initializer is important,
   1.456 +** because garbage data that appears at the end of a journal is likely
   1.457 +** data that was once in other files that have now been deleted.  If the
   1.458 +** garbage data came from an obsolete journal file, the checksums might
   1.459 +** be correct.  But by initializing the checksum to random value which
   1.460 +** is different for every journal, we minimize that risk.
   1.461 +*/
   1.462 +static const unsigned char aJournalMagic[] = {
   1.463 +  0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
   1.464 +};
   1.465 +
   1.466 +/*
   1.467 +** The size of the header and of each page in the journal is determined
   1.468 +** by the following macros.
   1.469 +*/
   1.470 +#define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
   1.471 +
   1.472 +/*
   1.473 +** The journal header size for this pager. In the future, this could be
   1.474 +** set to some value read from the disk controller. The important
   1.475 +** characteristic is that it is the same size as a disk sector.
   1.476 +*/
   1.477 +#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
   1.478 +
   1.479 +/*
   1.480 +** The macro MEMDB is true if we are dealing with an in-memory database.
   1.481 +** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
   1.482 +** the value of MEMDB will be a constant and the compiler will optimize
   1.483 +** out code that would never execute.
   1.484 +*/
   1.485 +#ifdef SQLITE_OMIT_MEMORYDB
   1.486 +# define MEMDB 0
   1.487 +#else
   1.488 +# define MEMDB pPager->memDb
   1.489 +#endif
   1.490 +
   1.491 +/*
   1.492 +** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
   1.493 +** reserved for working around a windows/posix incompatibility). It is
   1.494 +** used in the journal to signify that the remainder of the journal file 
   1.495 +** is devoted to storing a master journal name - there are no more pages to
   1.496 +** roll back. See comments for function writeMasterJournal() for details.
   1.497 +*/
   1.498 +/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
   1.499 +#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
   1.500 +
   1.501 +/*
   1.502 +** The maximum legal page number is (2^31 - 1).
   1.503 +*/
   1.504 +#define PAGER_MAX_PGNO 2147483647
   1.505 +
   1.506 +/*
   1.507 +** The pagerEnter() and pagerLeave() routines acquire and release
   1.508 +** a mutex on each pager.  The mutex is recursive.
   1.509 +**
   1.510 +** This is a special-purpose mutex.  It only provides mutual exclusion
   1.511 +** between the Btree and the Memory Management sqlite3_release_memory()
   1.512 +** function.  It does not prevent, for example, two Btrees from accessing
   1.513 +** the same pager at the same time.  Other general-purpose mutexes in
   1.514 +** the btree layer handle that chore.
   1.515 +*/
   1.516 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.517 +  static void pagerEnter(Pager *p){
   1.518 +    p->iInUseDB++;
   1.519 +    if( p->iInUseMM && p->iInUseDB==1 ){
   1.520 +      sqlite3_mutex *mutex;
   1.521 +      mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
   1.522 +      p->iInUseDB = 0;
   1.523 +      sqlite3_mutex_enter(mutex);
   1.524 +      p->iInUseDB = 1;
   1.525 +      sqlite3_mutex_leave(mutex);
   1.526 +    }
   1.527 +    assert( p->iInUseMM==0 );
   1.528 +  }
   1.529 +  static void pagerLeave(Pager *p){
   1.530 +    p->iInUseDB--;
   1.531 +    assert( p->iInUseDB>=0 );
   1.532 +  }
   1.533 +#else
   1.534 +# define pagerEnter(X)
   1.535 +# define pagerLeave(X)
   1.536 +#endif
   1.537 +
   1.538 +/*
   1.539 +** Enable reference count tracking (for debugging) here:
   1.540 +*/
   1.541 +#ifdef SQLITE_DEBUG
   1.542 +  int pager3_refinfo_enable = 0;
   1.543 +  static void pager_refinfo(PgHdr *p){
   1.544 +    static int cnt = 0;
   1.545 +    if( !pager3_refinfo_enable ) return;
   1.546 +    sqlite3DebugPrintf(
   1.547 +       "REFCNT: %4d addr=%p nRef=%-3d total=%d\n",
   1.548 +       p->pgno, PGHDR_TO_DATA(p), p->nRef, p->pPager->nRef
   1.549 +    );
   1.550 +    cnt++;   /* Something to set a breakpoint on */
   1.551 +  }
   1.552 +# define REFINFO(X)  pager_refinfo(X)
   1.553 +#else
   1.554 +# define REFINFO(X)
   1.555 +#endif
   1.556 +
   1.557 +/*
   1.558 +** Add page pPg to the end of the linked list managed by structure
   1.559 +** pList (pPg becomes the last entry in the list - the most recently 
   1.560 +** used). Argument pLink should point to either pPg->free or pPg->gfree,
   1.561 +** depending on whether pPg is being added to the pager-specific or
   1.562 +** global LRU list.
   1.563 +*/
   1.564 +static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
   1.565 +  pLink->pNext = 0;
   1.566 +  pLink->pPrev = pList->pLast;
   1.567 +
   1.568 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.569 +  assert(pLink==&pPg->free || pLink==&pPg->gfree);
   1.570 +  assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
   1.571 +#endif
   1.572 +
   1.573 +  if( pList->pLast ){
   1.574 +    int iOff = (char *)pLink - (char *)pPg;
   1.575 +    PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
   1.576 +    pLastLink->pNext = pPg;
   1.577 +  }else{
   1.578 +    assert(!pList->pFirst);
   1.579 +    pList->pFirst = pPg;
   1.580 +  }
   1.581 +
   1.582 +  pList->pLast = pPg;
   1.583 +  if( !pList->pFirstSynced && pPg->needSync==0 ){
   1.584 +    pList->pFirstSynced = pPg;
   1.585 +  }
   1.586 +}
   1.587 +
   1.588 +/*
   1.589 +** Remove pPg from the list managed by the structure pointed to by pList.
   1.590 +**
   1.591 +** Argument pLink should point to either pPg->free or pPg->gfree, depending 
   1.592 +** on whether pPg is being added to the pager-specific or global LRU list.
   1.593 +*/
   1.594 +static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
   1.595 +  int iOff = (char *)pLink - (char *)pPg;
   1.596 +
   1.597 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.598 +  assert(pLink==&pPg->free || pLink==&pPg->gfree);
   1.599 +  assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
   1.600 +#endif
   1.601 +
   1.602 +  if( pPg==pList->pFirst ){
   1.603 +    pList->pFirst = pLink->pNext;
   1.604 +  }
   1.605 +  if( pPg==pList->pLast ){
   1.606 +    pList->pLast = pLink->pPrev;
   1.607 +  }
   1.608 +  if( pLink->pPrev ){
   1.609 +    PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
   1.610 +    pPrevLink->pNext = pLink->pNext;
   1.611 +  }
   1.612 +  if( pLink->pNext ){
   1.613 +    PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
   1.614 +    pNextLink->pPrev = pLink->pPrev;
   1.615 +  }
   1.616 +  if( pPg==pList->pFirstSynced ){
   1.617 +    PgHdr *p = pLink->pNext;
   1.618 +    while( p && p->needSync ){
   1.619 +      PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
   1.620 +      p = pL->pNext;
   1.621 +    }
   1.622 +    pList->pFirstSynced = p;
   1.623 +  }
   1.624 +
   1.625 +  pLink->pNext = pLink->pPrev = 0;
   1.626 +}
   1.627 +
   1.628 +/* 
   1.629 +** Add page pPg to the list of free pages for the pager. If 
   1.630 +** memory-management is enabled, also add the page to the global 
   1.631 +** list of free pages.
   1.632 +*/
   1.633 +static void lruListAdd(PgHdr *pPg){
   1.634 +  listAdd(&pPg->pPager->lru, &pPg->free, pPg);
   1.635 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.636 +  if( !pPg->pPager->memDb ){
   1.637 +    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
   1.638 +    listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
   1.639 +    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
   1.640 +  }
   1.641 +#endif
   1.642 +}
   1.643 +
   1.644 +/* 
   1.645 +** Remove page pPg from the list of free pages for the associated pager.
   1.646 +** If memory-management is enabled, also remove pPg from the global list
   1.647 +** of free pages.
   1.648 +*/
   1.649 +static void lruListRemove(PgHdr *pPg){
   1.650 +  listRemove(&pPg->pPager->lru, &pPg->free, pPg);
   1.651 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.652 +  if( !pPg->pPager->memDb ){
   1.653 +    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
   1.654 +    listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
   1.655 +    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
   1.656 +  }
   1.657 +#endif
   1.658 +}
   1.659 +
   1.660 +/* 
   1.661 +** This function is called just after the needSync flag has been cleared
   1.662 +** from all pages managed by pPager (usually because the journal file
   1.663 +** has just been synced). It updates the pPager->lru.pFirstSynced variable
   1.664 +** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
   1.665 +** variable also.
   1.666 +*/
   1.667 +static void lruListSetFirstSynced(Pager *pPager){
   1.668 +  pPager->lru.pFirstSynced = pPager->lru.pFirst;
   1.669 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.670 +  if( !pPager->memDb ){
   1.671 +    PgHdr *p;
   1.672 +    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
   1.673 +    for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
   1.674 +    assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
   1.675 +    sqlite3LruPageList.pFirstSynced = p;
   1.676 +    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
   1.677 +  }
   1.678 +#endif
   1.679 +}
   1.680 +
   1.681 +/*
   1.682 +** Return true if page *pPg has already been written to the statement
   1.683 +** journal (or statement snapshot has been created, if *pPg is part
   1.684 +** of an in-memory database).
   1.685 +*/
   1.686 +static int pageInStatement(PgHdr *pPg){
   1.687 +  Pager *pPager = pPg->pPager;
   1.688 +  if( MEMDB ){
   1.689 +    return PGHDR_TO_HIST(pPg, pPager)->inStmt;
   1.690 +  }else{
   1.691 +    Pgno pgno = pPg->pgno;
   1.692 +    u8 *a = pPager->aInStmt;
   1.693 +    return (a && (int)pgno<=pPager->stmtSize && (a[pgno/8] & (1<<(pgno&7))));
   1.694 +  }
   1.695 +}
   1.696 +
   1.697 +/*
   1.698 +** Change the size of the pager hash table to N.  N must be a power
   1.699 +** of two.
   1.700 +*/
   1.701 +static void pager_resize_hash_table(Pager *pPager, int N){
   1.702 +  PgHdr **aHash, *pPg;
   1.703 +  assert( N>0 && (N&(N-1))==0 );
   1.704 +  pagerLeave(pPager);
   1.705 +  sqlite3MallocBenignFailure((int)pPager->aHash);
   1.706 +  aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
   1.707 +  pagerEnter(pPager);
   1.708 +  if( aHash==0 ){
   1.709 +    /* Failure to rehash is not an error.  It is only a performance hit. */
   1.710 +    return;
   1.711 +  }
   1.712 +  sqlite3_free(pPager->aHash);
   1.713 +  pPager->nHash = N;
   1.714 +  pPager->aHash = aHash;
   1.715 +  for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
   1.716 +    int h;
   1.717 +    if( pPg->pgno==0 ){
   1.718 +      assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
   1.719 +      continue;
   1.720 +    }
   1.721 +    h = pPg->pgno & (N-1);
   1.722 +    pPg->pNextHash = aHash[h];
   1.723 +    if( aHash[h] ){
   1.724 +      aHash[h]->pPrevHash = pPg;
   1.725 +    }
   1.726 +    aHash[h] = pPg;
   1.727 +    pPg->pPrevHash = 0;
   1.728 +  }
   1.729 +}
   1.730 +
   1.731 +/*
   1.732 +** Read a 32-bit integer from the given file descriptor.  Store the integer
   1.733 +** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
   1.734 +** error code is something goes wrong.
   1.735 +**
   1.736 +** All values are stored on disk as big-endian.
   1.737 +*/
   1.738 +static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
   1.739 +  unsigned char ac[4];
   1.740 +  int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
   1.741 +  if( rc==SQLITE_OK ){
   1.742 +    *pRes = sqlite3Get4byte(ac);
   1.743 +  }
   1.744 +  return rc;
   1.745 +}
   1.746 +
   1.747 +/*
   1.748 +** Write a 32-bit integer into a string buffer in big-endian byte order.
   1.749 +*/
   1.750 +#define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
   1.751 +
   1.752 +/*
   1.753 +** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
   1.754 +** on success or an error code is something goes wrong.
   1.755 +*/
   1.756 +static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
   1.757 +  char ac[4];
   1.758 +  put32bits(ac, val);
   1.759 +  return sqlite3OsWrite(fd, ac, 4, offset);
   1.760 +}
   1.761 +
   1.762 +/*
   1.763 +** If file pFd is open, call sqlite3OsUnlock() on it.
   1.764 +*/
   1.765 +static int osUnlock(sqlite3_file *pFd, int eLock){
   1.766 +  if( !pFd->pMethods ){
   1.767 +    return SQLITE_OK;
   1.768 +  }
   1.769 +  return sqlite3OsUnlock(pFd, eLock);
   1.770 +}
   1.771 +
   1.772 +/*
   1.773 +** This function determines whether or not the atomic-write optimization
   1.774 +** can be used with this pager. The optimization can be used if:
   1.775 +**
   1.776 +**  (a) the value returned by OsDeviceCharacteristics() indicates that
   1.777 +**      a database page may be written atomically, and
   1.778 +**  (b) the value returned by OsSectorSize() is less than or equal
   1.779 +**      to the page size.
   1.780 +**
   1.781 +** If the optimization cannot be used, 0 is returned. If it can be used,
   1.782 +** then the value returned is the size of the journal file when it
   1.783 +** contains rollback data for exactly one page.
   1.784 +*/
   1.785 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
   1.786 +static int jrnlBufferSize(Pager *pPager){
   1.787 +  int dc;           /* Device characteristics */
   1.788 +  int nSector;      /* Sector size */
   1.789 +  int nPage;        /* Page size */
   1.790 +  sqlite3_file *fd = pPager->fd;
   1.791 +
   1.792 +  if( fd->pMethods ){
   1.793 +    dc = sqlite3OsDeviceCharacteristics(fd);
   1.794 +    nSector = sqlite3OsSectorSize(fd);
   1.795 +    nPage = pPager->pageSize;
   1.796 +  }
   1.797 +
   1.798 +  assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
   1.799 +  assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
   1.800 +
   1.801 +  if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
   1.802 +    return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
   1.803 +  }
   1.804 +  return 0;
   1.805 +}
   1.806 +#endif
   1.807 +
   1.808 +/*
   1.809 +** This function should be called when an error occurs within the pager
   1.810 +** code. The first argument is a pointer to the pager structure, the
   1.811 +** second the error-code about to be returned by a pager API function. 
   1.812 +** The value returned is a copy of the second argument to this function. 
   1.813 +**
   1.814 +** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
   1.815 +** the error becomes persistent. Until the persisten error is cleared,
   1.816 +** subsequent API calls on this Pager will immediately return the same 
   1.817 +** error code.
   1.818 +**
   1.819 +** A persistent error indicates that the contents of the pager-cache 
   1.820 +** cannot be trusted. This state can be cleared by completely discarding 
   1.821 +** the contents of the pager-cache. If a transaction was active when
   1.822 +** the persistent error occured, then the rollback journal may need
   1.823 +** to be replayed.
   1.824 +*/
   1.825 +static void pager_unlock(Pager *pPager);
   1.826 +static int pager_error(Pager *pPager, int rc){
   1.827 +  int rc2 = rc & 0xff;
   1.828 +  assert(
   1.829 +       pPager->errCode==SQLITE_FULL ||
   1.830 +       pPager->errCode==SQLITE_OK ||
   1.831 +       (pPager->errCode & 0xff)==SQLITE_IOERR
   1.832 +  );
   1.833 +  if(
   1.834 +    rc2==SQLITE_FULL ||
   1.835 +    rc2==SQLITE_IOERR ||
   1.836 +    rc2==SQLITE_CORRUPT
   1.837 +  ){
   1.838 +    pPager->errCode = rc;
   1.839 +    if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
   1.840 +      /* If the pager is already unlocked, call pager_unlock() now to
   1.841 +      ** clear the error state and ensure that the pager-cache is 
   1.842 +      ** completely empty.
   1.843 +      */
   1.844 +      pager_unlock(pPager);
   1.845 +    }
   1.846 +  }
   1.847 +  return rc;
   1.848 +}
   1.849 +
   1.850 +/*
   1.851 +** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
   1.852 +** on the cache using a hash function.  This is used for testing
   1.853 +** and debugging only.
   1.854 +*/
   1.855 +#ifdef SQLITE_CHECK_PAGES
   1.856 +/*
   1.857 +** Return a 32-bit hash of the page data for pPage.
   1.858 +*/
   1.859 +static u32 pager_datahash(int nByte, unsigned char *pData){
   1.860 +  u32 hash = 0;
   1.861 +  int i;
   1.862 +  for(i=0; i<nByte; i++){
   1.863 +    hash = (hash*1039) + pData[i];
   1.864 +  }
   1.865 +  return hash;
   1.866 +}
   1.867 +static u32 pager_pagehash(PgHdr *pPage){
   1.868 +  return pager_datahash(pPage->pPager->pageSize, 
   1.869 +                        (unsigned char *)PGHDR_TO_DATA(pPage));
   1.870 +}
   1.871 +
   1.872 +/*
   1.873 +** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
   1.874 +** is defined, and NDEBUG is not defined, an assert() statement checks
   1.875 +** that the page is either dirty or still matches the calculated page-hash.
   1.876 +*/
   1.877 +#define CHECK_PAGE(x) checkPage(x)
   1.878 +static void checkPage(PgHdr *pPg){
   1.879 +  Pager *pPager = pPg->pPager;
   1.880 +  assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty || 
   1.881 +      pPg->pageHash==pager_pagehash(pPg) );
   1.882 +}
   1.883 +
   1.884 +#else
   1.885 +#define pager_datahash(X,Y)  0
   1.886 +#define pager_pagehash(X)  0
   1.887 +#define CHECK_PAGE(x)
   1.888 +#endif
   1.889 +
   1.890 +/*
   1.891 +** When this is called the journal file for pager pPager must be open.
   1.892 +** The master journal file name is read from the end of the file and 
   1.893 +** written into memory supplied by the caller. 
   1.894 +**
   1.895 +** zMaster must point to a buffer of at least nMaster bytes allocated by
   1.896 +** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
   1.897 +** enough space to write the master journal name). If the master journal
   1.898 +** name in the journal is longer than nMaster bytes (including a
   1.899 +** nul-terminator), then this is handled as if no master journal name
   1.900 +** were present in the journal.
   1.901 +**
   1.902 +** If no master journal file name is present zMaster[0] is set to 0 and
   1.903 +** SQLITE_OK returned.
   1.904 +*/
   1.905 +static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
   1.906 +  int rc;
   1.907 +  u32 len;
   1.908 +  i64 szJ;
   1.909 +  u32 cksum;
   1.910 +  int i;
   1.911 +  unsigned char aMagic[8]; /* A buffer to hold the magic header */
   1.912 +
   1.913 +  zMaster[0] = '\0';
   1.914 +
   1.915 +  rc = sqlite3OsFileSize(pJrnl, &szJ);
   1.916 +  if( rc!=SQLITE_OK || szJ<16 ) return rc;
   1.917 +
   1.918 +  rc = read32bits(pJrnl, szJ-16, &len);
   1.919 +  if( rc!=SQLITE_OK ) return rc;
   1.920 +
   1.921 +  if( len>=nMaster ){
   1.922 +    return SQLITE_OK;
   1.923 +  }
   1.924 +
   1.925 +  rc = read32bits(pJrnl, szJ-12, &cksum);
   1.926 +  if( rc!=SQLITE_OK ) return rc;
   1.927 +
   1.928 +  rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
   1.929 +  if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
   1.930 +
   1.931 +  rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
   1.932 +  if( rc!=SQLITE_OK ){
   1.933 +    return rc;
   1.934 +  }
   1.935 +  zMaster[len] = '\0';
   1.936 +
   1.937 +  /* See if the checksum matches the master journal name */
   1.938 +  for(i=0; i<len; i++){
   1.939 +    cksum -= zMaster[i];
   1.940 +   }
   1.941 +  if( cksum ){
   1.942 +    /* If the checksum doesn't add up, then one or more of the disk sectors
   1.943 +    ** containing the master journal filename is corrupted. This means
   1.944 +    ** definitely roll back, so just return SQLITE_OK and report a (nul)
   1.945 +    ** master-journal filename.
   1.946 +    */
   1.947 +    zMaster[0] = '\0';
   1.948 +  }
   1.949 +   
   1.950 +  return SQLITE_OK;
   1.951 +}
   1.952 +
   1.953 +/*
   1.954 +** Seek the journal file descriptor to the next sector boundary where a
   1.955 +** journal header may be read or written. Pager.journalOff is updated with
   1.956 +** the new seek offset.
   1.957 +**
   1.958 +** i.e for a sector size of 512:
   1.959 +**
   1.960 +** Input Offset              Output Offset
   1.961 +** ---------------------------------------
   1.962 +** 0                         0
   1.963 +** 512                       512
   1.964 +** 100                       512
   1.965 +** 2000                      2048
   1.966 +** 
   1.967 +*/
   1.968 +static void seekJournalHdr(Pager *pPager){
   1.969 +  i64 offset = 0;
   1.970 +  i64 c = pPager->journalOff;
   1.971 +  if( c ){
   1.972 +    offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
   1.973 +  }
   1.974 +  assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
   1.975 +  assert( offset>=c );
   1.976 +  assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
   1.977 +  pPager->journalOff = offset;
   1.978 +}
   1.979 +
   1.980 +/*
   1.981 +** The journal file must be open when this routine is called. A journal
   1.982 +** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
   1.983 +** current location.
   1.984 +**
   1.985 +** The format for the journal header is as follows:
   1.986 +** - 8 bytes: Magic identifying journal format.
   1.987 +** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
   1.988 +** - 4 bytes: Random number used for page hash.
   1.989 +** - 4 bytes: Initial database page count.
   1.990 +** - 4 bytes: Sector size used by the process that wrote this journal.
   1.991 +** 
   1.992 +** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
   1.993 +*/
   1.994 +static int writeJournalHdr(Pager *pPager){
   1.995 +  char zHeader[sizeof(aJournalMagic)+16];
   1.996 +  int rc;
   1.997 +
   1.998 +  if( pPager->stmtHdrOff==0 ){
   1.999 +    pPager->stmtHdrOff = pPager->journalOff;
  1.1000 +  }
  1.1001 +
  1.1002 +  seekJournalHdr(pPager);
  1.1003 +  pPager->journalHdr = pPager->journalOff;
  1.1004 +
  1.1005 +  memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
  1.1006 +
  1.1007 +  /* 
  1.1008 +  ** Write the nRec Field - the number of page records that follow this
  1.1009 +  ** journal header. Normally, zero is written to this value at this time.
  1.1010 +  ** After the records are added to the journal (and the journal synced, 
  1.1011 +  ** if in full-sync mode), the zero is overwritten with the true number
  1.1012 +  ** of records (see syncJournal()).
  1.1013 +  **
  1.1014 +  ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
  1.1015 +  ** reading the journal this value tells SQLite to assume that the
  1.1016 +  ** rest of the journal file contains valid page records. This assumption
  1.1017 +  ** is dangerous, as if a failure occured whilst writing to the journal
  1.1018 +  ** file it may contain some garbage data. There are two scenarios
  1.1019 +  ** where this risk can be ignored:
  1.1020 +  **
  1.1021 +  **   * When the pager is in no-sync mode. Corruption can follow a
  1.1022 +  **     power failure in this case anyway.
  1.1023 +  **
  1.1024 +  **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
  1.1025 +  **     that garbage data is never appended to the journal file.
  1.1026 +  */
  1.1027 +  assert(pPager->fd->pMethods||pPager->noSync);
  1.1028 +  if( (pPager->noSync) 
  1.1029 +   || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 
  1.1030 +  ){
  1.1031 +    put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
  1.1032 +  }else{
  1.1033 +    put32bits(&zHeader[sizeof(aJournalMagic)], 0);
  1.1034 +  }
  1.1035 +
  1.1036 +  /* The random check-hash initialiser */ 
  1.1037 +  sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
  1.1038 +  put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
  1.1039 +  /* The initial database size */
  1.1040 +  put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
  1.1041 +  /* The assumed sector size for this process */
  1.1042 +  put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
  1.1043 +  IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
  1.1044 +  rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
  1.1045 +  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
  1.1046 +
  1.1047 +  /* The journal header has been written successfully. Seek the journal
  1.1048 +  ** file descriptor to the end of the journal header sector.
  1.1049 +  */
  1.1050 +  if( rc==SQLITE_OK ){
  1.1051 +    IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
  1.1052 +    rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
  1.1053 +  }
  1.1054 +  return rc;
  1.1055 +}
  1.1056 +
  1.1057 +/*
  1.1058 +** The journal file must be open when this is called. A journal header file
  1.1059 +** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
  1.1060 +** file. See comments above function writeJournalHdr() for a description of
  1.1061 +** the journal header format.
  1.1062 +**
  1.1063 +** If the header is read successfully, *nRec is set to the number of
  1.1064 +** page records following this header and *dbSize is set to the size of the
  1.1065 +** database before the transaction began, in pages. Also, pPager->cksumInit
  1.1066 +** is set to the value read from the journal header. SQLITE_OK is returned
  1.1067 +** in this case.
  1.1068 +**
  1.1069 +** If the journal header file appears to be corrupted, SQLITE_DONE is
  1.1070 +** returned and *nRec and *dbSize are not set.  If JOURNAL_HDR_SZ bytes
  1.1071 +** cannot be read from the journal file an error code is returned.
  1.1072 +*/
  1.1073 +static int readJournalHdr(
  1.1074 +  Pager *pPager, 
  1.1075 +  i64 journalSize,
  1.1076 +  u32 *pNRec, 
  1.1077 +  u32 *pDbSize
  1.1078 +){
  1.1079 +  int rc;
  1.1080 +  unsigned char aMagic[8]; /* A buffer to hold the magic header */
  1.1081 +  i64 jrnlOff;
  1.1082 +
  1.1083 +  seekJournalHdr(pPager);
  1.1084 +  if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
  1.1085 +    return SQLITE_DONE;
  1.1086 +  }
  1.1087 +  jrnlOff = pPager->journalOff;
  1.1088 +
  1.1089 +  rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
  1.1090 +  if( rc ) return rc;
  1.1091 +  jrnlOff += sizeof(aMagic);
  1.1092 +
  1.1093 +  if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
  1.1094 +    return SQLITE_DONE;
  1.1095 +  }
  1.1096 +
  1.1097 +  rc = read32bits(pPager->jfd, jrnlOff, pNRec);
  1.1098 +  if( rc ) return rc;
  1.1099 +
  1.1100 +  rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
  1.1101 +  if( rc ) return rc;
  1.1102 +
  1.1103 +  rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
  1.1104 +  if( rc ) return rc;
  1.1105 +
  1.1106 +  /* Update the assumed sector-size to match the value used by 
  1.1107 +  ** the process that created this journal. If this journal was
  1.1108 +  ** created by a process other than this one, then this routine
  1.1109 +  ** is being called from within pager_playback(). The local value
  1.1110 +  ** of Pager.sectorSize is restored at the end of that routine.
  1.1111 +  */
  1.1112 +  rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
  1.1113 +  if( rc ) return rc;
  1.1114 +
  1.1115 +  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
  1.1116 +  return SQLITE_OK;
  1.1117 +}
  1.1118 +
  1.1119 +
  1.1120 +/*
  1.1121 +** Write the supplied master journal name into the journal file for pager
  1.1122 +** pPager at the current location. The master journal name must be the last
  1.1123 +** thing written to a journal file. If the pager is in full-sync mode, the
  1.1124 +** journal file descriptor is advanced to the next sector boundary before
  1.1125 +** anything is written. The format is:
  1.1126 +**
  1.1127 +** + 4 bytes: PAGER_MJ_PGNO.
  1.1128 +** + N bytes: length of master journal name.
  1.1129 +** + 4 bytes: N
  1.1130 +** + 4 bytes: Master journal name checksum.
  1.1131 +** + 8 bytes: aJournalMagic[].
  1.1132 +**
  1.1133 +** The master journal page checksum is the sum of the bytes in the master
  1.1134 +** journal name.
  1.1135 +**
  1.1136 +** If zMaster is a NULL pointer (occurs for a single database transaction), 
  1.1137 +** this call is a no-op.
  1.1138 +*/
  1.1139 +static int writeMasterJournal(Pager *pPager, const char *zMaster){
  1.1140 +  int rc;
  1.1141 +  int len; 
  1.1142 +  int i; 
  1.1143 +  i64 jrnlOff;
  1.1144 +  u32 cksum = 0;
  1.1145 +  char zBuf[sizeof(aJournalMagic)+2*4];
  1.1146 +
  1.1147 +  if( !zMaster || pPager->setMaster) return SQLITE_OK;
  1.1148 +  pPager->setMaster = 1;
  1.1149 +
  1.1150 +  len = strlen(zMaster);
  1.1151 +  for(i=0; i<len; i++){
  1.1152 +    cksum += zMaster[i];
  1.1153 +  }
  1.1154 +
  1.1155 +  /* If in full-sync mode, advance to the next disk sector before writing
  1.1156 +  ** the master journal name. This is in case the previous page written to
  1.1157 +  ** the journal has already been synced.
  1.1158 +  */
  1.1159 +  if( pPager->fullSync ){
  1.1160 +    seekJournalHdr(pPager);
  1.1161 +  }
  1.1162 +  jrnlOff = pPager->journalOff;
  1.1163 +  pPager->journalOff += (len+20);
  1.1164 +
  1.1165 +  rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
  1.1166 +  if( rc!=SQLITE_OK ) return rc;
  1.1167 +  jrnlOff += 4;
  1.1168 +
  1.1169 +  rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
  1.1170 +  if( rc!=SQLITE_OK ) return rc;
  1.1171 +  jrnlOff += len;
  1.1172 +
  1.1173 +  put32bits(zBuf, len);
  1.1174 +  put32bits(&zBuf[4], cksum);
  1.1175 +  memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
  1.1176 +  rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
  1.1177 +  pPager->needSync = !pPager->noSync;
  1.1178 +  return rc;
  1.1179 +}
  1.1180 +
  1.1181 +/*
  1.1182 +** Add or remove a page from the list of all pages that are in the
  1.1183 +** statement journal.
  1.1184 +**
  1.1185 +** The Pager keeps a separate list of pages that are currently in
  1.1186 +** the statement journal.  This helps the sqlite3PagerStmtCommit()
  1.1187 +** routine run MUCH faster for the common case where there are many
  1.1188 +** pages in memory but only a few are in the statement journal.
  1.1189 +*/
  1.1190 +static void page_add_to_stmt_list(PgHdr *pPg){
  1.1191 +  Pager *pPager = pPg->pPager;
  1.1192 +  PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  1.1193 +  assert( MEMDB );
  1.1194 +  if( !pHist->inStmt ){
  1.1195 +    assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
  1.1196 +    if( pPager->pStmt ){
  1.1197 +      PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
  1.1198 +    }
  1.1199 +    pHist->pNextStmt = pPager->pStmt;
  1.1200 +    pPager->pStmt = pPg;
  1.1201 +    pHist->inStmt = 1;
  1.1202 +  }
  1.1203 +}
  1.1204 +
  1.1205 +/*
  1.1206 +** Find a page in the hash table given its page number.  Return
  1.1207 +** a pointer to the page or NULL if not found.
  1.1208 +*/
  1.1209 +static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
  1.1210 +  PgHdr *p;
  1.1211 +  if( pPager->aHash==0 ) return 0;
  1.1212 +  p = pPager->aHash[pgno & (pPager->nHash-1)];
  1.1213 +  while( p && p->pgno!=pgno ){
  1.1214 +    p = p->pNextHash;
  1.1215 +  }
  1.1216 +  return p;
  1.1217 +}
  1.1218 +
  1.1219 +/*
  1.1220 +** Clear the in-memory cache.  This routine
  1.1221 +** sets the state of the pager back to what it was when it was first
  1.1222 +** opened.  Any outstanding pages are invalidated and subsequent attempts
  1.1223 +** to access those pages will likely result in a coredump.
  1.1224 +*/
  1.1225 +static void pager_reset(Pager *pPager){
  1.1226 +  PgHdr *pPg, *pNext;
  1.1227 +  if( pPager->errCode ) return;
  1.1228 +  for(pPg=pPager->pAll; pPg; pPg=pNext){
  1.1229 +    IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
  1.1230 +    PAGER_INCR(sqlite3_pager_pgfree_count);
  1.1231 +    pNext = pPg->pNextAll;
  1.1232 +    lruListRemove(pPg);
  1.1233 +    sqlite3_free(pPg->pData);
  1.1234 +    sqlite3_free(pPg);
  1.1235 +  }
  1.1236 +  assert(pPager->lru.pFirst==0);
  1.1237 +  assert(pPager->lru.pFirstSynced==0);
  1.1238 +  assert(pPager->lru.pLast==0);
  1.1239 +  pPager->pStmt = 0;
  1.1240 +  pPager->pAll = 0;
  1.1241 +  pPager->pDirty = 0;
  1.1242 +  pPager->nHash = 0;
  1.1243 +  sqlite3_free(pPager->aHash);
  1.1244 +  pPager->nPage = 0;
  1.1245 +  pPager->aHash = 0;
  1.1246 +  pPager->nRef = 0;
  1.1247 +}
  1.1248 +
  1.1249 +/*
  1.1250 +** Unlock the database file. 
  1.1251 +**
  1.1252 +** If the pager is currently in error state, discard the contents of 
  1.1253 +** the cache and reset the Pager structure internal state. If there is
  1.1254 +** an open journal-file, then the next time a shared-lock is obtained
  1.1255 +** on the pager file (by this or any other process), it will be
  1.1256 +** treated as a hot-journal and rolled back.
  1.1257 +*/
  1.1258 +static void pager_unlock(Pager *pPager){
  1.1259 +  if( !pPager->exclusiveMode ){
  1.1260 +    if( !MEMDB ){
  1.1261 +      if( pPager->fd->pMethods ){
  1.1262 +        osUnlock(pPager->fd, NO_LOCK);
  1.1263 +      }
  1.1264 +      pPager->dbSize = -1;
  1.1265 +      IOTRACE(("UNLOCK %p\n", pPager))
  1.1266 +
  1.1267 +      /* If Pager.errCode is set, the contents of the pager cache cannot be
  1.1268 +      ** trusted. Now that the pager file is unlocked, the contents of the
  1.1269 +      ** cache can be discarded and the error code safely cleared.
  1.1270 +      */
  1.1271 +      if( pPager->errCode ){
  1.1272 +        pPager->errCode = SQLITE_OK;
  1.1273 +        pager_reset(pPager);
  1.1274 +        if( pPager->stmtOpen ){
  1.1275 +          sqlite3OsClose(pPager->stfd);
  1.1276 +          sqlite3_free(pPager->aInStmt);
  1.1277 +          pPager->aInStmt = 0;
  1.1278 +        }
  1.1279 +        if( pPager->journalOpen ){
  1.1280 +          sqlite3OsClose(pPager->jfd);
  1.1281 +          pPager->journalOpen = 0;
  1.1282 +          sqlite3_free(pPager->aInJournal);
  1.1283 +          pPager->aInJournal = 0;
  1.1284 +        }
  1.1285 +        pPager->stmtOpen = 0;
  1.1286 +        pPager->stmtInUse = 0;
  1.1287 +        pPager->journalOff = 0;
  1.1288 +        pPager->journalStarted = 0;
  1.1289 +        pPager->stmtAutoopen = 0;
  1.1290 +        pPager->origDbSize = 0;
  1.1291 +      }
  1.1292 +    }
  1.1293 +
  1.1294 +    if( !MEMDB || pPager->errCode==SQLITE_OK ){
  1.1295 +      pPager->state = PAGER_UNLOCK;
  1.1296 +      pPager->changeCountDone = 0;
  1.1297 +    }
  1.1298 +  }
  1.1299 +}
  1.1300 +
  1.1301 +/*
  1.1302 +** Execute a rollback if a transaction is active and unlock the 
  1.1303 +** database file. If the pager has already entered the error state, 
  1.1304 +** do not attempt the rollback.
  1.1305 +*/
  1.1306 +static void pagerUnlockAndRollback(Pager *p){
  1.1307 +  assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
  1.1308 +  if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
  1.1309 +    sqlite3PagerRollback(p);
  1.1310 +  }
  1.1311 +  pager_unlock(p);
  1.1312 +  assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
  1.1313 +  assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
  1.1314 +}
  1.1315 +
  1.1316 +/*
  1.1317 +** This routine ends a transaction.  A transaction is ended by either
  1.1318 +** a COMMIT or a ROLLBACK.
  1.1319 +**
  1.1320 +** When this routine is called, the pager has the journal file open and
  1.1321 +** a RESERVED or EXCLUSIVE lock on the database.  This routine will release
  1.1322 +** the database lock and acquires a SHARED lock in its place if that is
  1.1323 +** the appropriate thing to do.  Release locks usually is appropriate,
  1.1324 +** unless we are in exclusive access mode or unless this is a 
  1.1325 +** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
  1.1326 +**
  1.1327 +** The journal file is either deleted or truncated.
  1.1328 +**
  1.1329 +** TODO: Consider keeping the journal file open for temporary databases.
  1.1330 +** This might give a performance improvement on windows where opening
  1.1331 +** a file is an expensive operation.
  1.1332 +*/
  1.1333 +static int pager_end_transaction(Pager *pPager){
  1.1334 +  PgHdr *pPg;
  1.1335 +  int rc = SQLITE_OK;
  1.1336 +  int rc2 = SQLITE_OK;
  1.1337 +  assert( !MEMDB );
  1.1338 +  if( pPager->state<PAGER_RESERVED ){
  1.1339 +    return SQLITE_OK;
  1.1340 +  }
  1.1341 +  sqlite3PagerStmtCommit(pPager);
  1.1342 +  if( pPager->stmtOpen && !pPager->exclusiveMode ){
  1.1343 +    sqlite3OsClose(pPager->stfd);
  1.1344 +    pPager->stmtOpen = 0;
  1.1345 +  }
  1.1346 +  if( pPager->journalOpen ){
  1.1347 +    if( pPager->exclusiveMode 
  1.1348 +          && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
  1.1349 +      pPager->journalOff = 0;
  1.1350 +      pPager->journalStarted = 0;
  1.1351 +    }else{
  1.1352 +      sqlite3OsClose(pPager->jfd);
  1.1353 +      pPager->journalOpen = 0;
  1.1354 +      if( rc==SQLITE_OK ){
  1.1355 +        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
  1.1356 +      }
  1.1357 +    }
  1.1358 +    sqlite3_free( pPager->aInJournal );
  1.1359 +    pPager->aInJournal = 0;
  1.1360 +    for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
  1.1361 +      pPg->inJournal = 0;
  1.1362 +      pPg->dirty = 0;
  1.1363 +      pPg->needSync = 0;
  1.1364 +      pPg->alwaysRollback = 0;
  1.1365 +#ifdef SQLITE_CHECK_PAGES
  1.1366 +      pPg->pageHash = pager_pagehash(pPg);
  1.1367 +#endif
  1.1368 +    }
  1.1369 +    pPager->pDirty = 0;
  1.1370 +    pPager->dirtyCache = 0;
  1.1371 +    pPager->nRec = 0;
  1.1372 +  }else{
  1.1373 +    assert( pPager->aInJournal==0 );
  1.1374 +    assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
  1.1375 +  }
  1.1376 +
  1.1377 +  if( !pPager->exclusiveMode ){
  1.1378 +    rc2 = osUnlock(pPager->fd, SHARED_LOCK);
  1.1379 +    pPager->state = PAGER_SHARED;
  1.1380 +  }else if( pPager->state==PAGER_SYNCED ){
  1.1381 +    pPager->state = PAGER_EXCLUSIVE;
  1.1382 +  }
  1.1383 +  pPager->origDbSize = 0;
  1.1384 +  pPager->setMaster = 0;
  1.1385 +  pPager->needSync = 0;
  1.1386 +  lruListSetFirstSynced(pPager);
  1.1387 +  pPager->dbSize = -1;
  1.1388 +
  1.1389 +  return (rc==SQLITE_OK?rc2:rc);
  1.1390 +}
  1.1391 +
  1.1392 +/*
  1.1393 +** Compute and return a checksum for the page of data.
  1.1394 +**
  1.1395 +** This is not a real checksum.  It is really just the sum of the 
  1.1396 +** random initial value and the page number.  We experimented with
  1.1397 +** a checksum of the entire data, but that was found to be too slow.
  1.1398 +**
  1.1399 +** Note that the page number is stored at the beginning of data and
  1.1400 +** the checksum is stored at the end.  This is important.  If journal
  1.1401 +** corruption occurs due to a power failure, the most likely scenario
  1.1402 +** is that one end or the other of the record will be changed.  It is
  1.1403 +** much less likely that the two ends of the journal record will be
  1.1404 +** correct and the middle be corrupt.  Thus, this "checksum" scheme,
  1.1405 +** though fast and simple, catches the mostly likely kind of corruption.
  1.1406 +**
  1.1407 +** FIX ME:  Consider adding every 200th (or so) byte of the data to the
  1.1408 +** checksum.  That way if a single page spans 3 or more disk sectors and
  1.1409 +** only the middle sector is corrupt, we will still have a reasonable
  1.1410 +** chance of failing the checksum and thus detecting the problem.
  1.1411 +*/
  1.1412 +static u32 pager_cksum(Pager *pPager, const u8 *aData){
  1.1413 +  u32 cksum = pPager->cksumInit;
  1.1414 +  int i = pPager->pageSize-200;
  1.1415 +  while( i>0 ){
  1.1416 +    cksum += aData[i];
  1.1417 +    i -= 200;
  1.1418 +  }
  1.1419 +  return cksum;
  1.1420 +}
  1.1421 +
  1.1422 +/* Forward declaration */
  1.1423 +static void makeClean(PgHdr*);
  1.1424 +
  1.1425 +/*
  1.1426 +** Read a single page from the journal file opened on file descriptor
  1.1427 +** jfd.  Playback this one page.
  1.1428 +**
  1.1429 +** If useCksum==0 it means this journal does not use checksums.  Checksums
  1.1430 +** are not used in statement journals because statement journals do not
  1.1431 +** need to survive power failures.
  1.1432 +*/
  1.1433 +static int pager_playback_one_page(
  1.1434 +  Pager *pPager, 
  1.1435 +  sqlite3_file *jfd,
  1.1436 +  i64 offset,
  1.1437 +  int useCksum
  1.1438 +){
  1.1439 +  int rc;
  1.1440 +  PgHdr *pPg;                   /* An existing page in the cache */
  1.1441 +  Pgno pgno;                    /* The page number of a page in journal */
  1.1442 +  u32 cksum;                    /* Checksum used for sanity checking */
  1.1443 +  u8 *aData = (u8 *)pPager->pTmpSpace;   /* Temp storage for a page */
  1.1444 +
  1.1445 +  /* useCksum should be true for the main journal and false for
  1.1446 +  ** statement journals.  Verify that this is always the case
  1.1447 +  */
  1.1448 +  assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
  1.1449 +  assert( aData );
  1.1450 +
  1.1451 +  rc = read32bits(jfd, offset, &pgno);
  1.1452 +  if( rc!=SQLITE_OK ) return rc;
  1.1453 +  rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
  1.1454 +  if( rc!=SQLITE_OK ) return rc;
  1.1455 +  pPager->journalOff += pPager->pageSize + 4;
  1.1456 +
  1.1457 +  /* Sanity checking on the page.  This is more important that I originally
  1.1458 +  ** thought.  If a power failure occurs while the journal is being written,
  1.1459 +  ** it could cause invalid data to be written into the journal.  We need to
  1.1460 +  ** detect this invalid data (with high probability) and ignore it.
  1.1461 +  */
  1.1462 +  if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
  1.1463 +    return SQLITE_DONE;
  1.1464 +  }
  1.1465 +  if( pgno>(unsigned)pPager->dbSize ){
  1.1466 +    return SQLITE_OK;
  1.1467 +  }
  1.1468 +  if( useCksum ){
  1.1469 +    rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
  1.1470 +    if( rc ) return rc;
  1.1471 +    pPager->journalOff += 4;
  1.1472 +    if( pager_cksum(pPager, aData)!=cksum ){
  1.1473 +      return SQLITE_DONE;
  1.1474 +    }
  1.1475 +  }
  1.1476 +
  1.1477 +  assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
  1.1478 +
  1.1479 +  /* If the pager is in RESERVED state, then there must be a copy of this
  1.1480 +  ** page in the pager cache. In this case just update the pager cache,
  1.1481 +  ** not the database file. The page is left marked dirty in this case.
  1.1482 +  **
  1.1483 +  ** An exception to the above rule: If the database is in no-sync mode
  1.1484 +  ** and a page is moved during an incremental vacuum then the page may
  1.1485 +  ** not be in the pager cache. Later: if a malloc() or IO error occurs
  1.1486 +  ** during a Movepage() call, then the page may not be in the cache
  1.1487 +  ** either. So the condition described in the above paragraph is not
  1.1488 +  ** assert()able.
  1.1489 +  **
  1.1490 +  ** If in EXCLUSIVE state, then we update the pager cache if it exists
  1.1491 +  ** and the main file. The page is then marked not dirty.
  1.1492 +  **
  1.1493 +  ** Ticket #1171:  The statement journal might contain page content that is
  1.1494 +  ** different from the page content at the start of the transaction.
  1.1495 +  ** This occurs when a page is changed prior to the start of a statement
  1.1496 +  ** then changed again within the statement.  When rolling back such a
  1.1497 +  ** statement we must not write to the original database unless we know
  1.1498 +  ** for certain that original page contents are synced into the main rollback
  1.1499 +  ** journal.  Otherwise, a power loss might leave modified data in the
  1.1500 +  ** database file without an entry in the rollback journal that can
  1.1501 +  ** restore the database to its original form.  Two conditions must be
  1.1502 +  ** met before writing to the database files. (1) the database must be
  1.1503 +  ** locked.  (2) we know that the original page content is fully synced
  1.1504 +  ** in the main journal either because the page is not in cache or else
  1.1505 +  ** the page is marked as needSync==0.
  1.1506 +  */
  1.1507 +  pPg = pager_lookup(pPager, pgno);
  1.1508 +  PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
  1.1509 +               PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
  1.1510 +  if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
  1.1511 +    i64 offset = (pgno-1)*(i64)pPager->pageSize;
  1.1512 +    rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
  1.1513 +    if( pPg ){
  1.1514 +      makeClean(pPg);
  1.1515 +    }
  1.1516 +  }
  1.1517 +  if( pPg ){
  1.1518 +    /* No page should ever be explicitly rolled back that is in use, except
  1.1519 +    ** for page 1 which is held in use in order to keep the lock on the
  1.1520 +    ** database active. However such a page may be rolled back as a result
  1.1521 +    ** of an internal error resulting in an automatic call to
  1.1522 +    ** sqlite3PagerRollback().
  1.1523 +    */
  1.1524 +    void *pData;
  1.1525 +    /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
  1.1526 +    pData = PGHDR_TO_DATA(pPg);
  1.1527 +    memcpy(pData, aData, pPager->pageSize);
  1.1528 +    if( pPager->xReiniter ){
  1.1529 +      pPager->xReiniter(pPg, pPager->pageSize);
  1.1530 +    }
  1.1531 +#ifdef SQLITE_CHECK_PAGES
  1.1532 +    pPg->pageHash = pager_pagehash(pPg);
  1.1533 +#endif
  1.1534 +    /* If this was page 1, then restore the value of Pager.dbFileVers.
  1.1535 +    ** Do this before any decoding. */
  1.1536 +    if( pgno==1 ){
  1.1537 +      memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
  1.1538 +    }
  1.1539 +
  1.1540 +    /* Decode the page just read from disk */
  1.1541 +    CODEC1(pPager, pData, pPg->pgno, 3);
  1.1542 +  }
  1.1543 +  return rc;
  1.1544 +}
  1.1545 +
  1.1546 +/*
  1.1547 +** Parameter zMaster is the name of a master journal file. A single journal
  1.1548 +** file that referred to the master journal file has just been rolled back.
  1.1549 +** This routine checks if it is possible to delete the master journal file,
  1.1550 +** and does so if it is.
  1.1551 +**
  1.1552 +** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not 
  1.1553 +** available for use within this function.
  1.1554 +**
  1.1555 +**
  1.1556 +** The master journal file contains the names of all child journals.
  1.1557 +** To tell if a master journal can be deleted, check to each of the
  1.1558 +** children.  If all children are either missing or do not refer to
  1.1559 +** a different master journal, then this master journal can be deleted.
  1.1560 +*/
  1.1561 +static int pager_delmaster(Pager *pPager, const char *zMaster){
  1.1562 +  sqlite3_vfs *pVfs = pPager->pVfs;
  1.1563 +  int rc;
  1.1564 +  int master_open = 0;
  1.1565 +  sqlite3_file *pMaster;
  1.1566 +  sqlite3_file *pJournal;
  1.1567 +  char *zMasterJournal = 0; /* Contents of master journal file */
  1.1568 +  i64 nMasterJournal;       /* Size of master journal file */
  1.1569 +
  1.1570 +  /* Open the master journal file exclusively in case some other process
  1.1571 +  ** is running this routine also. Not that it makes too much difference.
  1.1572 +  */
  1.1573 +  pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
  1.1574 +  pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
  1.1575 +  if( !pMaster ){
  1.1576 +    rc = SQLITE_NOMEM;
  1.1577 +  }else{
  1.1578 +    int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
  1.1579 +    rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
  1.1580 +  }
  1.1581 +  if( rc!=SQLITE_OK ) goto delmaster_out;
  1.1582 +  master_open = 1;
  1.1583 +
  1.1584 +  rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
  1.1585 +  if( rc!=SQLITE_OK ) goto delmaster_out;
  1.1586 +
  1.1587 +  if( nMasterJournal>0 ){
  1.1588 +    char *zJournal;
  1.1589 +    char *zMasterPtr = 0;
  1.1590 +    int nMasterPtr = pPager->pVfs->mxPathname+1;
  1.1591 +
  1.1592 +    /* Load the entire master journal file into space obtained from
  1.1593 +    ** sqlite3_malloc() and pointed to by zMasterJournal. 
  1.1594 +    */
  1.1595 +    zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
  1.1596 +    if( !zMasterJournal ){
  1.1597 +      rc = SQLITE_NOMEM;
  1.1598 +      goto delmaster_out;
  1.1599 +    }
  1.1600 +    zMasterPtr = &zMasterJournal[nMasterJournal];
  1.1601 +    rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
  1.1602 +    if( rc!=SQLITE_OK ) goto delmaster_out;
  1.1603 +
  1.1604 +    zJournal = zMasterJournal;
  1.1605 +    while( (zJournal-zMasterJournal)<nMasterJournal ){
  1.1606 +      if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
  1.1607 +        /* One of the journals pointed to by the master journal exists.
  1.1608 +        ** Open it and check if it points at the master journal. If
  1.1609 +        ** so, return without deleting the master journal file.
  1.1610 +        */
  1.1611 +        int c;
  1.1612 +        int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
  1.1613 +        rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
  1.1614 +        if( rc!=SQLITE_OK ){
  1.1615 +          goto delmaster_out;
  1.1616 +        }
  1.1617 +
  1.1618 +        rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
  1.1619 +        sqlite3OsClose(pJournal);
  1.1620 +        if( rc!=SQLITE_OK ){
  1.1621 +          goto delmaster_out;
  1.1622 +        }
  1.1623 +
  1.1624 +        c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
  1.1625 +        if( c ){
  1.1626 +          /* We have a match. Do not delete the master journal file. */
  1.1627 +          goto delmaster_out;
  1.1628 +        }
  1.1629 +      }
  1.1630 +      zJournal += (strlen(zJournal)+1);
  1.1631 +    }
  1.1632 +  }
  1.1633 +  
  1.1634 +  rc = sqlite3OsDelete(pVfs, zMaster, 0);
  1.1635 +
  1.1636 +delmaster_out:
  1.1637 +  if( zMasterJournal ){
  1.1638 +    sqlite3_free(zMasterJournal);
  1.1639 +  }  
  1.1640 +  if( master_open ){
  1.1641 +    sqlite3OsClose(pMaster);
  1.1642 +  }
  1.1643 +  sqlite3_free(pMaster);
  1.1644 +  return rc;
  1.1645 +}
  1.1646 +
  1.1647 +
  1.1648 +static void pager_truncate_cache(Pager *pPager);
  1.1649 +
  1.1650 +/*
  1.1651 +** Truncate the main file of the given pager to the number of pages
  1.1652 +** indicated. Also truncate the cached representation of the file.
  1.1653 +*/
  1.1654 +static int pager_truncate(Pager *pPager, int nPage){
  1.1655 +  int rc = SQLITE_OK;
  1.1656 +  if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
  1.1657 +    rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
  1.1658 +  }
  1.1659 +  if( rc==SQLITE_OK ){
  1.1660 +    pPager->dbSize = nPage;
  1.1661 +    pager_truncate_cache(pPager);
  1.1662 +  }
  1.1663 +  return rc;
  1.1664 +}
  1.1665 +
  1.1666 +/*
  1.1667 +** Set the sectorSize for the given pager.
  1.1668 +**
  1.1669 +** The sector size is the larger of the sector size reported
  1.1670 +** by sqlite3OsSectorSize() and the pageSize.
  1.1671 +*/
  1.1672 +static void setSectorSize(Pager *pPager){
  1.1673 +  assert(pPager->fd->pMethods||pPager->tempFile);
  1.1674 +  if( !pPager->tempFile ){
  1.1675 +    /* Sector size doesn't matter for temporary files. Also, the file
  1.1676 +    ** may not have been opened yet, in whcih case the OsSectorSize()
  1.1677 +    ** call will segfault.
  1.1678 +    */
  1.1679 +    pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
  1.1680 +  }
  1.1681 +  if( pPager->sectorSize<pPager->pageSize ){
  1.1682 +    pPager->sectorSize = pPager->pageSize;
  1.1683 +  }
  1.1684 +}
  1.1685 +
  1.1686 +/*
  1.1687 +** Playback the journal and thus restore the database file to
  1.1688 +** the state it was in before we started making changes.  
  1.1689 +**
  1.1690 +** The journal file format is as follows: 
  1.1691 +**
  1.1692 +**  (1)  8 byte prefix.  A copy of aJournalMagic[].
  1.1693 +**  (2)  4 byte big-endian integer which is the number of valid page records
  1.1694 +**       in the journal.  If this value is 0xffffffff, then compute the
  1.1695 +**       number of page records from the journal size.
  1.1696 +**  (3)  4 byte big-endian integer which is the initial value for the 
  1.1697 +**       sanity checksum.
  1.1698 +**  (4)  4 byte integer which is the number of pages to truncate the
  1.1699 +**       database to during a rollback.
  1.1700 +**  (5)  4 byte integer which is the number of bytes in the master journal
  1.1701 +**       name.  The value may be zero (indicate that there is no master
  1.1702 +**       journal.)
  1.1703 +**  (6)  N bytes of the master journal name.  The name will be nul-terminated
  1.1704 +**       and might be shorter than the value read from (5).  If the first byte
  1.1705 +**       of the name is \000 then there is no master journal.  The master
  1.1706 +**       journal name is stored in UTF-8.
  1.1707 +**  (7)  Zero or more pages instances, each as follows:
  1.1708 +**        +  4 byte page number.
  1.1709 +**        +  pPager->pageSize bytes of data.
  1.1710 +**        +  4 byte checksum
  1.1711 +**
  1.1712 +** When we speak of the journal header, we mean the first 6 items above.
  1.1713 +** Each entry in the journal is an instance of the 7th item.
  1.1714 +**
  1.1715 +** Call the value from the second bullet "nRec".  nRec is the number of
  1.1716 +** valid page entries in the journal.  In most cases, you can compute the
  1.1717 +** value of nRec from the size of the journal file.  But if a power
  1.1718 +** failure occurred while the journal was being written, it could be the
  1.1719 +** case that the size of the journal file had already been increased but
  1.1720 +** the extra entries had not yet made it safely to disk.  In such a case,
  1.1721 +** the value of nRec computed from the file size would be too large.  For
  1.1722 +** that reason, we always use the nRec value in the header.
  1.1723 +**
  1.1724 +** If the nRec value is 0xffffffff it means that nRec should be computed
  1.1725 +** from the file size.  This value is used when the user selects the
  1.1726 +** no-sync option for the journal.  A power failure could lead to corruption
  1.1727 +** in this case.  But for things like temporary table (which will be
  1.1728 +** deleted when the power is restored) we don't care.  
  1.1729 +**
  1.1730 +** If the file opened as the journal file is not a well-formed
  1.1731 +** journal file then all pages up to the first corrupted page are rolled
  1.1732 +** back (or no pages if the journal header is corrupted). The journal file
  1.1733 +** is then deleted and SQLITE_OK returned, just as if no corruption had
  1.1734 +** been encountered.
  1.1735 +**
  1.1736 +** If an I/O or malloc() error occurs, the journal-file is not deleted
  1.1737 +** and an error code is returned.
  1.1738 +*/
  1.1739 +static int pager_playback(Pager *pPager, int isHot){
  1.1740 +  sqlite3_vfs *pVfs = pPager->pVfs;
  1.1741 +  i64 szJ;                 /* Size of the journal file in bytes */
  1.1742 +  u32 nRec;                /* Number of Records in the journal */
  1.1743 +  int i;                   /* Loop counter */
  1.1744 +  Pgno mxPg = 0;           /* Size of the original file in pages */
  1.1745 +  int rc;                  /* Result code of a subroutine */
  1.1746 +  char *zMaster = 0;       /* Name of master journal file if any */
  1.1747 +
  1.1748 +  /* Figure out how many records are in the journal.  Abort early if
  1.1749 +  ** the journal is empty.
  1.1750 +  */
  1.1751 +  assert( pPager->journalOpen );
  1.1752 +  rc = sqlite3OsFileSize(pPager->jfd, &szJ);
  1.1753 +  if( rc!=SQLITE_OK || szJ==0 ){
  1.1754 +    goto end_playback;
  1.1755 +  }
  1.1756 +
  1.1757 +  /* Read the master journal name from the journal, if it is present.
  1.1758 +  ** If a master journal file name is specified, but the file is not
  1.1759 +  ** present on disk, then the journal is not hot and does not need to be
  1.1760 +  ** played back.
  1.1761 +  */
  1.1762 +  zMaster = pPager->pTmpSpace;
  1.1763 +  rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
  1.1764 +  assert( rc!=SQLITE_DONE );
  1.1765 +  if( rc!=SQLITE_OK 
  1.1766 +   || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS)) 
  1.1767 +  ){
  1.1768 +    zMaster = 0;
  1.1769 +    if( rc==SQLITE_DONE ) rc = SQLITE_OK;
  1.1770 +    goto end_playback;
  1.1771 +  }
  1.1772 +  pPager->journalOff = 0;
  1.1773 +  zMaster = 0;
  1.1774 +
  1.1775 +  /* This loop terminates either when the readJournalHdr() call returns
  1.1776 +  ** SQLITE_DONE or an IO error occurs. */
  1.1777 +  while( 1 ){
  1.1778 +
  1.1779 +    /* Read the next journal header from the journal file.  If there are
  1.1780 +    ** not enough bytes left in the journal file for a complete header, or
  1.1781 +    ** it is corrupted, then a process must of failed while writing it.
  1.1782 +    ** This indicates nothing more needs to be rolled back.
  1.1783 +    */
  1.1784 +    rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
  1.1785 +    if( rc!=SQLITE_OK ){ 
  1.1786 +      if( rc==SQLITE_DONE ){
  1.1787 +        rc = SQLITE_OK;
  1.1788 +      }
  1.1789 +      goto end_playback;
  1.1790 +    }
  1.1791 +
  1.1792 +    /* If nRec is 0xffffffff, then this journal was created by a process
  1.1793 +    ** working in no-sync mode. This means that the rest of the journal
  1.1794 +    ** file consists of pages, there are no more journal headers. Compute
  1.1795 +    ** the value of nRec based on this assumption.
  1.1796 +    */
  1.1797 +    if( nRec==0xffffffff ){
  1.1798 +      assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
  1.1799 +      nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
  1.1800 +    }
  1.1801 +
  1.1802 +    /* If nRec is 0 and this rollback is of a transaction created by this
  1.1803 +    ** process and if this is the final header in the journal, then it means
  1.1804 +    ** that this part of the journal was being filled but has not yet been
  1.1805 +    ** synced to disk.  Compute the number of pages based on the remaining
  1.1806 +    ** size of the file.
  1.1807 +    **
  1.1808 +    ** The third term of the test was added to fix ticket #2565.
  1.1809 +    */
  1.1810 +    if( nRec==0 && !isHot &&
  1.1811 +        pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
  1.1812 +      nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
  1.1813 +    }
  1.1814 +
  1.1815 +    /* If this is the first header read from the journal, truncate the
  1.1816 +    ** database file back to it's original size.
  1.1817 +    */
  1.1818 +    if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
  1.1819 +      rc = pager_truncate(pPager, mxPg);
  1.1820 +      if( rc!=SQLITE_OK ){
  1.1821 +        goto end_playback;
  1.1822 +      }
  1.1823 +    }
  1.1824 +
  1.1825 +    /* Copy original pages out of the journal and back into the database file.
  1.1826 +    */
  1.1827 +    for(i=0; i<nRec; i++){
  1.1828 +      rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
  1.1829 +      if( rc!=SQLITE_OK ){
  1.1830 +        if( rc==SQLITE_DONE ){
  1.1831 +          rc = SQLITE_OK;
  1.1832 +          pPager->journalOff = szJ;
  1.1833 +          break;
  1.1834 +        }else{
  1.1835 +          goto end_playback;
  1.1836 +        }
  1.1837 +      }
  1.1838 +    }
  1.1839 +  }
  1.1840 +  /*NOTREACHED*/
  1.1841 +  assert( 0 );
  1.1842 +
  1.1843 +end_playback:
  1.1844 +  if( rc==SQLITE_OK ){
  1.1845 +    zMaster = pPager->pTmpSpace;
  1.1846 +    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
  1.1847 +  }
  1.1848 +  if( rc==SQLITE_OK ){
  1.1849 +    rc = pager_end_transaction(pPager);
  1.1850 +  }
  1.1851 +  if( rc==SQLITE_OK && zMaster[0] ){
  1.1852 +    /* If there was a master journal and this routine will return success,
  1.1853 +    ** see if it is possible to delete the master journal.
  1.1854 +    */
  1.1855 +    rc = pager_delmaster(pPager, zMaster);
  1.1856 +  }
  1.1857 +
  1.1858 +  /* The Pager.sectorSize variable may have been updated while rolling
  1.1859 +  ** back a journal created by a process with a different sector size
  1.1860 +  ** value. Reset it to the correct value for this process.
  1.1861 +  */
  1.1862 +  setSectorSize(pPager);
  1.1863 +  return rc;
  1.1864 +}
  1.1865 +
  1.1866 +/*
  1.1867 +** Playback the statement journal.
  1.1868 +**
  1.1869 +** This is similar to playing back the transaction journal but with
  1.1870 +** a few extra twists.
  1.1871 +**
  1.1872 +**    (1)  The number of pages in the database file at the start of
  1.1873 +**         the statement is stored in pPager->stmtSize, not in the
  1.1874 +**         journal file itself.
  1.1875 +**
  1.1876 +**    (2)  In addition to playing back the statement journal, also
  1.1877 +**         playback all pages of the transaction journal beginning
  1.1878 +**         at offset pPager->stmtJSize.
  1.1879 +*/
  1.1880 +static int pager_stmt_playback(Pager *pPager){
  1.1881 +  i64 szJ;                 /* Size of the full journal */
  1.1882 +  i64 hdrOff;
  1.1883 +  int nRec;                /* Number of Records */
  1.1884 +  int i;                   /* Loop counter */
  1.1885 +  int rc;
  1.1886 +
  1.1887 +  szJ = pPager->journalOff;
  1.1888 +#ifndef NDEBUG 
  1.1889 +  {
  1.1890 +    i64 os_szJ;
  1.1891 +    rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
  1.1892 +    if( rc!=SQLITE_OK ) return rc;
  1.1893 +    assert( szJ==os_szJ );
  1.1894 +  }
  1.1895 +#endif
  1.1896 +
  1.1897 +  /* Set hdrOff to be the offset just after the end of the last journal
  1.1898 +  ** page written before the first journal-header for this statement
  1.1899 +  ** transaction was written, or the end of the file if no journal
  1.1900 +  ** header was written.
  1.1901 +  */
  1.1902 +  hdrOff = pPager->stmtHdrOff;
  1.1903 +  assert( pPager->fullSync || !hdrOff );
  1.1904 +  if( !hdrOff ){
  1.1905 +    hdrOff = szJ;
  1.1906 +  }
  1.1907 +  
  1.1908 +  /* Truncate the database back to its original size.
  1.1909 +  */
  1.1910 +  rc = pager_truncate(pPager, pPager->stmtSize);
  1.1911 +  assert( pPager->state>=PAGER_SHARED );
  1.1912 +
  1.1913 +  /* Figure out how many records are in the statement journal.
  1.1914 +  */
  1.1915 +  assert( pPager->stmtInUse && pPager->journalOpen );
  1.1916 +  nRec = pPager->stmtNRec;
  1.1917 +  
  1.1918 +  /* Copy original pages out of the statement journal and back into the
  1.1919 +  ** database file.  Note that the statement journal omits checksums from
  1.1920 +  ** each record since power-failure recovery is not important to statement
  1.1921 +  ** journals.
  1.1922 +  */
  1.1923 +  for(i=0; i<nRec; i++){
  1.1924 +    i64 offset = i*(4+pPager->pageSize);
  1.1925 +    rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
  1.1926 +    assert( rc!=SQLITE_DONE );
  1.1927 +    if( rc!=SQLITE_OK ) goto end_stmt_playback;
  1.1928 +  }
  1.1929 +
  1.1930 +  /* Now roll some pages back from the transaction journal. Pager.stmtJSize
  1.1931 +  ** was the size of the journal file when this statement was started, so
  1.1932 +  ** everything after that needs to be rolled back, either into the
  1.1933 +  ** database, the memory cache, or both.
  1.1934 +  **
  1.1935 +  ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
  1.1936 +  ** of the first journal header written during this statement transaction.
  1.1937 +  */
  1.1938 +  pPager->journalOff = pPager->stmtJSize;
  1.1939 +  pPager->cksumInit = pPager->stmtCksum;
  1.1940 +  while( pPager->journalOff < hdrOff ){
  1.1941 +    rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
  1.1942 +    assert( rc!=SQLITE_DONE );
  1.1943 +    if( rc!=SQLITE_OK ) goto end_stmt_playback;
  1.1944 +  }
  1.1945 +
  1.1946 +  while( pPager->journalOff < szJ ){
  1.1947 +    u32 nJRec;         /* Number of Journal Records */
  1.1948 +    u32 dummy;
  1.1949 +    rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
  1.1950 +    if( rc!=SQLITE_OK ){
  1.1951 +      assert( rc!=SQLITE_DONE );
  1.1952 +      goto end_stmt_playback;
  1.1953 +    }
  1.1954 +    if( nJRec==0 ){
  1.1955 +      nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
  1.1956 +    }
  1.1957 +    for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
  1.1958 +      rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
  1.1959 +      assert( rc!=SQLITE_DONE );
  1.1960 +      if( rc!=SQLITE_OK ) goto end_stmt_playback;
  1.1961 +    }
  1.1962 +  }
  1.1963 +
  1.1964 +  pPager->journalOff = szJ;
  1.1965 +  
  1.1966 +end_stmt_playback:
  1.1967 +  if( rc==SQLITE_OK) {
  1.1968 +    pPager->journalOff = szJ;
  1.1969 +    /* pager_reload_cache(pPager); */
  1.1970 +  }
  1.1971 +  return rc;
  1.1972 +}
  1.1973 +
  1.1974 +/*
  1.1975 +** Change the maximum number of in-memory pages that are allowed.
  1.1976 +*/
  1.1977 +void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
  1.1978 +  if( mxPage>10 ){
  1.1979 +    pPager->mxPage = mxPage;
  1.1980 +  }else{
  1.1981 +    pPager->mxPage = 10;
  1.1982 +  }
  1.1983 +}
  1.1984 +
  1.1985 +/*
  1.1986 +** Adjust the robustness of the database to damage due to OS crashes
  1.1987 +** or power failures by changing the number of syncs()s when writing
  1.1988 +** the rollback journal.  There are three levels:
  1.1989 +**
  1.1990 +**    OFF       sqlite3OsSync() is never called.  This is the default
  1.1991 +**              for temporary and transient files.
  1.1992 +**
  1.1993 +**    NORMAL    The journal is synced once before writes begin on the
  1.1994 +**              database.  This is normally adequate protection, but
  1.1995 +**              it is theoretically possible, though very unlikely,
  1.1996 +**              that an inopertune power failure could leave the journal
  1.1997 +**              in a state which would cause damage to the database
  1.1998 +**              when it is rolled back.
  1.1999 +**
  1.2000 +**    FULL      The journal is synced twice before writes begin on the
  1.2001 +**              database (with some additional information - the nRec field
  1.2002 +**              of the journal header - being written in between the two
  1.2003 +**              syncs).  If we assume that writing a
  1.2004 +**              single disk sector is atomic, then this mode provides
  1.2005 +**              assurance that the journal will not be corrupted to the
  1.2006 +**              point of causing damage to the database during rollback.
  1.2007 +**
  1.2008 +** Numeric values associated with these states are OFF==1, NORMAL=2,
  1.2009 +** and FULL=3.
  1.2010 +*/
  1.2011 +#ifndef SQLITE_OMIT_PAGER_PRAGMAS
  1.2012 +void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
  1.2013 +  pPager->noSync =  level==1 || pPager->tempFile;
  1.2014 +  pPager->fullSync = level==3 && !pPager->tempFile;
  1.2015 +  pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
  1.2016 +  if( pPager->noSync ) pPager->needSync = 0;
  1.2017 +}
  1.2018 +#endif
  1.2019 +
  1.2020 +/*
  1.2021 +** The following global variable is incremented whenever the library
  1.2022 +** attempts to open a temporary file.  This information is used for
  1.2023 +** testing and analysis only.  
  1.2024 +*/
  1.2025 +#ifdef SQLITE_TEST
  1.2026 +int sqlite3_opentemp_count = 0;
  1.2027 +#endif
  1.2028 +
  1.2029 +/*
  1.2030 +** Open a temporary file. 
  1.2031 +**
  1.2032 +** Write the file descriptor into *fd.  Return SQLITE_OK on success or some
  1.2033 +** other error code if we fail. The OS will automatically delete the temporary
  1.2034 +** file when it is closed.
  1.2035 +*/
  1.2036 +static int sqlite3PagerOpentemp(
  1.2037 +  sqlite3_vfs *pVfs,    /* The virtual file system layer */
  1.2038 +  sqlite3_file *pFile,  /* Write the file descriptor here */
  1.2039 +  char *zFilename,      /* Name of the file.  Might be NULL */
  1.2040 +  int vfsFlags          /* Flags passed through to the VFS */
  1.2041 +){
  1.2042 +  int rc;
  1.2043 +  assert( zFilename!=0 );
  1.2044 +
  1.2045 +#ifdef SQLITE_TEST
  1.2046 +  sqlite3_opentemp_count++;  /* Used for testing and analysis only */
  1.2047 +#endif
  1.2048 +
  1.2049 +  vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
  1.2050 +            SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
  1.2051 +  rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
  1.2052 +  assert( rc!=SQLITE_OK || pFile->pMethods );
  1.2053 +  return rc;
  1.2054 +}
  1.2055 +
  1.2056 +/*
  1.2057 +** Create a new page cache and put a pointer to the page cache in *ppPager.
  1.2058 +** The file to be cached need not exist.  The file is not locked until
  1.2059 +** the first call to sqlite3PagerGet() and is only held open until the
  1.2060 +** last page is released using sqlite3PagerUnref().
  1.2061 +**
  1.2062 +** If zFilename is NULL then a randomly-named temporary file is created
  1.2063 +** and used as the file to be cached.  The file will be deleted
  1.2064 +** automatically when it is closed.
  1.2065 +**
  1.2066 +** If zFilename is ":memory:" then all information is held in cache.
  1.2067 +** It is never written to disk.  This can be used to implement an
  1.2068 +** in-memory database.
  1.2069 +*/
  1.2070 +int sqlite3PagerOpen(
  1.2071 +  sqlite3_vfs *pVfs,       /* The virtual file system to use */
  1.2072 +  Pager **ppPager,         /* Return the Pager structure here */
  1.2073 +  const char *zFilename,   /* Name of the database file to open */
  1.2074 +  int nExtra,              /* Extra bytes append to each in-memory page */
  1.2075 +  int flags,               /* flags controlling this file */
  1.2076 +  int vfsFlags             /* flags passed through to sqlite3_vfs.xOpen() */
  1.2077 +){
  1.2078 +  u8 *pPtr;
  1.2079 +  Pager *pPager = 0;
  1.2080 +  int rc = SQLITE_OK;
  1.2081 +  int i;
  1.2082 +  int tempFile = 0;
  1.2083 +  int memDb = 0;
  1.2084 +  int readOnly = 0;
  1.2085 +  int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
  1.2086 +  int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
  1.2087 +  int journalFileSize = sqlite3JournalSize(pVfs);
  1.2088 +  int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
  1.2089 +  char *zPathname;
  1.2090 +  int nPathname;
  1.2091 +
  1.2092 +  /* The default return is a NULL pointer */
  1.2093 +  *ppPager = 0;
  1.2094 +
  1.2095 +  /* Compute the full pathname */
  1.2096 +  nPathname = pVfs->mxPathname+1;
  1.2097 +  zPathname = sqlite3_malloc(nPathname);
  1.2098 +  if( zPathname==0 ){
  1.2099 +    return SQLITE_NOMEM;
  1.2100 +  }
  1.2101 +  if( zFilename && zFilename[0] ){
  1.2102 +#ifndef SQLITE_OMIT_MEMORYDB
  1.2103 +    if( strcmp(zFilename,":memory:")==0 ){
  1.2104 +      memDb = 1;
  1.2105 +      zPathname[0] = 0;
  1.2106 +    }else
  1.2107 +#endif
  1.2108 +    {
  1.2109 +      rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
  1.2110 +    }
  1.2111 +  }else{
  1.2112 +    rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
  1.2113 +  }
  1.2114 +  if( rc!=SQLITE_OK ){
  1.2115 +    sqlite3_free(zPathname);
  1.2116 +    return rc;
  1.2117 +  }
  1.2118 +  nPathname = strlen(zPathname);
  1.2119 +
  1.2120 +  /* Allocate memory for the pager structure */
  1.2121 +  pPager = sqlite3MallocZero(
  1.2122 +    sizeof(*pPager) +           /* Pager structure */
  1.2123 +    journalFileSize +           /* The journal file structure */ 
  1.2124 +    pVfs->szOsFile * 2 +        /* The db and stmt journal files */ 
  1.2125 +    4*nPathname + 40            /* zFilename, zDirectory, zJournal, zStmtJrnl */
  1.2126 +  );
  1.2127 +  if( !pPager ){
  1.2128 +    sqlite3_free(zPathname);
  1.2129 +    return SQLITE_NOMEM;
  1.2130 +  }
  1.2131 +  pPtr = (u8 *)&pPager[1];
  1.2132 +  pPager->vfsFlags = vfsFlags;
  1.2133 +  pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
  1.2134 +  pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
  1.2135 +  pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
  1.2136 +  pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
  1.2137 +  pPager->zDirectory = &pPager->zFilename[nPathname+1];
  1.2138 +  pPager->zJournal = &pPager->zDirectory[nPathname+1];
  1.2139 +  pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
  1.2140 +  pPager->pVfs = pVfs;
  1.2141 +  memcpy(pPager->zFilename, zPathname, nPathname+1);
  1.2142 +  sqlite3_free(zPathname);
  1.2143 +
  1.2144 +  /* Open the pager file.
  1.2145 +  */
  1.2146 +  if( zFilename && zFilename[0] && !memDb ){
  1.2147 +    if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
  1.2148 +      rc = SQLITE_CANTOPEN;
  1.2149 +    }else{
  1.2150 +      int fout = 0;
  1.2151 +      rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
  1.2152 +                         pPager->vfsFlags, &fout);
  1.2153 +      readOnly = (fout&SQLITE_OPEN_READONLY);
  1.2154 +
  1.2155 +      /* If the file was successfully opened for read/write access,
  1.2156 +      ** choose a default page size in case we have to create the
  1.2157 +      ** database file. The default page size is the maximum of:
  1.2158 +      **
  1.2159 +      **    + SQLITE_DEFAULT_PAGE_SIZE,
  1.2160 +      **    + The value returned by sqlite3OsSectorSize()
  1.2161 +      **    + The largest page size that can be written atomically.
  1.2162 +      */
  1.2163 +      if( rc==SQLITE_OK && !readOnly ){
  1.2164 +        int iSectorSize = sqlite3OsSectorSize(pPager->fd);
  1.2165 +        if( nDefaultPage<iSectorSize ){
  1.2166 +          nDefaultPage = iSectorSize;
  1.2167 +        }
  1.2168 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
  1.2169 +        {
  1.2170 +          int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
  1.2171 +          int ii;
  1.2172 +          assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
  1.2173 +          assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
  1.2174 +          assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
  1.2175 +          for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
  1.2176 +            if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
  1.2177 +          }
  1.2178 +        }
  1.2179 +#endif
  1.2180 +        if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
  1.2181 +          nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
  1.2182 +        }
  1.2183 +      }
  1.2184 +    }
  1.2185 +  }else if( !memDb ){
  1.2186 +    /* If a temporary file is requested, it is not opened immediately.
  1.2187 +    ** In this case we accept the default page size and delay actually
  1.2188 +    ** opening the file until the first call to OsWrite().
  1.2189 +    */ 
  1.2190 +    tempFile = 1;
  1.2191 +    pPager->state = PAGER_EXCLUSIVE;
  1.2192 +  }
  1.2193 +
  1.2194 +  if( pPager && rc==SQLITE_OK ){
  1.2195 +    pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
  1.2196 +  }
  1.2197 +
  1.2198 +  /* If an error occured in either of the blocks above.
  1.2199 +  ** Free the Pager structure and close the file.
  1.2200 +  ** Since the pager is not allocated there is no need to set 
  1.2201 +  ** any Pager.errMask variables.
  1.2202 +  */
  1.2203 +  if( !pPager || !pPager->pTmpSpace ){
  1.2204 +    sqlite3OsClose(pPager->fd);
  1.2205 +    sqlite3_free(pPager);
  1.2206 +    return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
  1.2207 +  }
  1.2208 +
  1.2209 +  PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
  1.2210 +  IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
  1.2211 +
  1.2212 +  /* Fill in Pager.zDirectory[] */
  1.2213 +  memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
  1.2214 +  for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
  1.2215 +  if( i>0 ) pPager->zDirectory[i-1] = 0;
  1.2216 +
  1.2217 +  /* Fill in Pager.zJournal[] and Pager.zStmtJrnl[] */
  1.2218 +  memcpy(pPager->zJournal, pPager->zFilename, nPathname);
  1.2219 +  memcpy(&pPager->zJournal[nPathname], "-journal", 9);
  1.2220 +  memcpy(pPager->zStmtJrnl, pPager->zFilename, nPathname);
  1.2221 +  memcpy(&pPager->zStmtJrnl[nPathname], "-stmtjrnl", 10);
  1.2222 +
  1.2223 +  /* pPager->journalOpen = 0; */
  1.2224 +  pPager->useJournal = useJournal && !memDb;
  1.2225 +  pPager->noReadlock = noReadlock && readOnly;
  1.2226 +  /* pPager->stmtOpen = 0; */
  1.2227 +  /* pPager->stmtInUse = 0; */
  1.2228 +  /* pPager->nRef = 0; */
  1.2229 +  pPager->dbSize = memDb-1;
  1.2230 +  pPager->pageSize = nDefaultPage;
  1.2231 +  /* pPager->stmtSize = 0; */
  1.2232 +  /* pPager->stmtJSize = 0; */
  1.2233 +  /* pPager->nPage = 0; */
  1.2234 +  pPager->mxPage = 100;
  1.2235 +  pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
  1.2236 +  /* pPager->state = PAGER_UNLOCK; */
  1.2237 +  assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
  1.2238 +  /* pPager->errMask = 0; */
  1.2239 +  pPager->tempFile = tempFile;
  1.2240 +  assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
  1.2241 +          || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
  1.2242 +  assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
  1.2243 +  pPager->exclusiveMode = tempFile; 
  1.2244 +  pPager->memDb = memDb;
  1.2245 +  pPager->readOnly = readOnly;
  1.2246 +  /* pPager->needSync = 0; */
  1.2247 +  pPager->noSync = pPager->tempFile || !useJournal;
  1.2248 +  pPager->fullSync = (pPager->noSync?0:1);
  1.2249 +  pPager->sync_flags = SQLITE_SYNC_NORMAL;
  1.2250 +  /* pPager->pFirst = 0; */
  1.2251 +  /* pPager->pFirstSynced = 0; */
  1.2252 +  /* pPager->pLast = 0; */
  1.2253 +  pPager->nExtra = FORCE_ALIGNMENT(nExtra);
  1.2254 +  assert(pPager->fd->pMethods||memDb||tempFile);
  1.2255 +  if( !memDb ){
  1.2256 +    setSectorSize(pPager);
  1.2257 +  }
  1.2258 +  /* pPager->pBusyHandler = 0; */
  1.2259 +  /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
  1.2260 +  *ppPager = pPager;
  1.2261 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  1.2262 +  pPager->iInUseMM = 0;
  1.2263 +  pPager->iInUseDB = 0;
  1.2264 +  if( !memDb ){
  1.2265 +    sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
  1.2266 +    sqlite3_mutex_enter(mutex);
  1.2267 +    pPager->pNext = sqlite3PagerList;
  1.2268 +    if( sqlite3PagerList ){
  1.2269 +      assert( sqlite3PagerList->pPrev==0 );
  1.2270 +      sqlite3PagerList->pPrev = pPager;
  1.2271 +    }
  1.2272 +    pPager->pPrev = 0;
  1.2273 +    sqlite3PagerList = pPager;
  1.2274 +    sqlite3_mutex_leave(mutex);
  1.2275 +  }
  1.2276 +#endif
  1.2277 +  return SQLITE_OK;
  1.2278 +}
  1.2279 +
  1.2280 +/*
  1.2281 +** Set the busy handler function.
  1.2282 +*/
  1.2283 +void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
  1.2284 +  pPager->pBusyHandler = pBusyHandler;
  1.2285 +}
  1.2286 +
  1.2287 +/*
  1.2288 +** Set the destructor for this pager.  If not NULL, the destructor is called
  1.2289 +** when the reference count on each page reaches zero.  The destructor can
  1.2290 +** be used to clean up information in the extra segment appended to each page.
  1.2291 +**
  1.2292 +** The destructor is not called as a result sqlite3PagerClose().  
  1.2293 +** Destructors are only called by sqlite3PagerUnref().
  1.2294 +*/
  1.2295 +void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
  1.2296 +  pPager->xDestructor = xDesc;
  1.2297 +}
  1.2298 +
  1.2299 +/*
  1.2300 +** Set the reinitializer for this pager.  If not NULL, the reinitializer
  1.2301 +** is called when the content of a page in cache is restored to its original
  1.2302 +** value as a result of a rollback.  The callback gives higher-level code
  1.2303 +** an opportunity to restore the EXTRA section to agree with the restored
  1.2304 +** page data.
  1.2305 +*/
  1.2306 +void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
  1.2307 +  pPager->xReiniter = xReinit;
  1.2308 +}
  1.2309 +
  1.2310 +/*
  1.2311 +** Set the page size to *pPageSize. If the suggest new page size is
  1.2312 +** inappropriate, then an alternative page size is set to that
  1.2313 +** value before returning.
  1.2314 +*/
  1.2315 +int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
  1.2316 +  int rc = SQLITE_OK;
  1.2317 +  u16 pageSize = *pPageSize;
  1.2318 +  assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
  1.2319 +  if( pageSize && pageSize!=pPager->pageSize 
  1.2320 +   && !pPager->memDb && pPager->nRef==0 
  1.2321 +  ){
  1.2322 +    char *pNew = (char *)sqlite3_malloc(pageSize);
  1.2323 +    if( !pNew ){
  1.2324 +      rc = SQLITE_NOMEM;
  1.2325 +    }else{
  1.2326 +      pagerEnter(pPager);
  1.2327 +      pager_reset(pPager);
  1.2328 +      pPager->pageSize = pageSize;
  1.2329 +      setSectorSize(pPager);
  1.2330 +      sqlite3_free(pPager->pTmpSpace);
  1.2331 +      pPager->pTmpSpace = pNew;
  1.2332 +      pagerLeave(pPager);
  1.2333 +    }
  1.2334 +  }
  1.2335 +  *pPageSize = pPager->pageSize;
  1.2336 +  return rc;
  1.2337 +}
  1.2338 +
  1.2339 +/*
  1.2340 +** Attempt to set the maximum database page count if mxPage is positive. 
  1.2341 +** Make no changes if mxPage is zero or negative.  And never reduce the
  1.2342 +** maximum page count below the current size of the database.
  1.2343 +**
  1.2344 +** Regardless of mxPage, return the current maximum page count.
  1.2345 +*/
  1.2346 +int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
  1.2347 +  if( mxPage>0 ){
  1.2348 +    pPager->mxPgno = mxPage;
  1.2349 +  }
  1.2350 +  sqlite3PagerPagecount(pPager);
  1.2351 +  return pPager->mxPgno;
  1.2352 +}
  1.2353 +
  1.2354 +/*
  1.2355 +** The following set of routines are used to disable the simulated
  1.2356 +** I/O error mechanism.  These routines are used to avoid simulated
  1.2357 +** errors in places where we do not care about errors.
  1.2358 +**
  1.2359 +** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
  1.2360 +** and generate no code.
  1.2361 +*/
  1.2362 +#ifdef SQLITE_TEST
  1.2363 +extern int sqlite3_io_error_pending;
  1.2364 +extern int sqlite3_io_error_hit;
  1.2365 +static int saved_cnt;
  1.2366 +void disable_simulated_io_errors(void){
  1.2367 +  saved_cnt = sqlite3_io_error_pending;
  1.2368 +  sqlite3_io_error_pending = -1;
  1.2369 +}
  1.2370 +void enable_simulated_io_errors(void){
  1.2371 +  sqlite3_io_error_pending = saved_cnt;
  1.2372 +}
  1.2373 +#else
  1.2374 +# define disable_simulated_io_errors()
  1.2375 +# define enable_simulated_io_errors()
  1.2376 +#endif
  1.2377 +
  1.2378 +/*
  1.2379 +** Read the first N bytes from the beginning of the file into memory
  1.2380 +** that pDest points to. 
  1.2381 +**
  1.2382 +** No error checking is done. The rational for this is that this function 
  1.2383 +** may be called even if the file does not exist or contain a header. In 
  1.2384 +** these cases sqlite3OsRead() will return an error, to which the correct 
  1.2385 +** response is to zero the memory at pDest and continue.  A real IO error 
  1.2386 +** will presumably recur and be picked up later (Todo: Think about this).
  1.2387 +*/
  1.2388 +int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
  1.2389 +  int rc = SQLITE_OK;
  1.2390 +  memset(pDest, 0, N);
  1.2391 +  assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
  1.2392 +  if( pPager->fd->pMethods ){
  1.2393 +    IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
  1.2394 +    rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
  1.2395 +    if( rc==SQLITE_IOERR_SHORT_READ ){
  1.2396 +      rc = SQLITE_OK;
  1.2397 +    }
  1.2398 +  }
  1.2399 +  return rc;
  1.2400 +}
  1.2401 +
  1.2402 +/*
  1.2403 +** Return the total number of pages in the disk file associated with
  1.2404 +** pPager. 
  1.2405 +**
  1.2406 +** If the PENDING_BYTE lies on the page directly after the end of the
  1.2407 +** file, then consider this page part of the file too. For example, if
  1.2408 +** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
  1.2409 +** file is 4096 bytes, 5 is returned instead of 4.
  1.2410 +*/
  1.2411 +int sqlite3PagerPagecount(Pager *pPager){
  1.2412 +  i64 n = 0;
  1.2413 +  int rc;
  1.2414 +  assert( pPager!=0 );
  1.2415 +  if( pPager->errCode ){
  1.2416 +    return 0;
  1.2417 +  }
  1.2418 +  if( pPager->dbSize>=0 ){
  1.2419 +    n = pPager->dbSize;
  1.2420 +  } else {
  1.2421 +    assert(pPager->fd->pMethods||pPager->tempFile);
  1.2422 +    if( (pPager->fd->pMethods)
  1.2423 +     && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
  1.2424 +      pPager->nRef++;
  1.2425 +      pager_error(pPager, rc);
  1.2426 +      pPager->nRef--;
  1.2427 +      return 0;
  1.2428 +    }
  1.2429 +    if( n>0 && n<pPager->pageSize ){
  1.2430 +      n = 1;
  1.2431 +    }else{
  1.2432 +      n /= pPager->pageSize;
  1.2433 +    }
  1.2434 +    if( pPager->state!=PAGER_UNLOCK ){
  1.2435 +      pPager->dbSize = n;
  1.2436 +    }
  1.2437 +  }
  1.2438 +  if( n==(PENDING_BYTE/pPager->pageSize) ){
  1.2439 +    n++;
  1.2440 +  }
  1.2441 +  if( n>pPager->mxPgno ){
  1.2442 +    pPager->mxPgno = n;
  1.2443 +  }
  1.2444 +  return n;
  1.2445 +}
  1.2446 +
  1.2447 +
  1.2448 +#ifndef SQLITE_OMIT_MEMORYDB
  1.2449 +/*
  1.2450 +** Clear a PgHistory block
  1.2451 +*/
  1.2452 +static void clearHistory(PgHistory *pHist){
  1.2453 +  sqlite3_free(pHist->pOrig);
  1.2454 +  sqlite3_free(pHist->pStmt);
  1.2455 +  pHist->pOrig = 0;
  1.2456 +  pHist->pStmt = 0;
  1.2457 +}
  1.2458 +#else
  1.2459 +#define clearHistory(x)
  1.2460 +#endif
  1.2461 +
  1.2462 +/*
  1.2463 +** Forward declaration
  1.2464 +*/
  1.2465 +static int syncJournal(Pager*);
  1.2466 +
  1.2467 +/*
  1.2468 +** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate
  1.2469 +** that the page is not part of any hash chain. This is required because the
  1.2470 +** sqlite3PagerMovepage() routine can leave a page in the 
  1.2471 +** pNextFree/pPrevFree list that is not a part of any hash-chain.
  1.2472 +*/
  1.2473 +static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
  1.2474 +  if( pPg->pgno==0 ){
  1.2475 +    assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
  1.2476 +    return;
  1.2477 +  }
  1.2478 +  if( pPg->pNextHash ){
  1.2479 +    pPg->pNextHash->pPrevHash = pPg->pPrevHash;
  1.2480 +  }
  1.2481 +  if( pPg->pPrevHash ){
  1.2482 +    assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
  1.2483 +    pPg->pPrevHash->pNextHash = pPg->pNextHash;
  1.2484 +  }else{
  1.2485 +    int h = pPg->pgno & (pPager->nHash-1);
  1.2486 +    pPager->aHash[h] = pPg->pNextHash;
  1.2487 +  }
  1.2488 +  if( MEMDB ){
  1.2489 +    clearHistory(PGHDR_TO_HIST(pPg, pPager));
  1.2490 +  }
  1.2491 +  pPg->pgno = 0;
  1.2492 +  pPg->pNextHash = pPg->pPrevHash = 0;
  1.2493 +}
  1.2494 +
  1.2495 +/*
  1.2496 +** Unlink a page from the free list (the list of all pages where nRef==0)
  1.2497 +** and from its hash collision chain.
  1.2498 +*/
  1.2499 +static void unlinkPage(PgHdr *pPg){
  1.2500 +  Pager *pPager = pPg->pPager;
  1.2501 +
  1.2502 +  /* Unlink from free page list */
  1.2503 +  lruListRemove(pPg);
  1.2504 +
  1.2505 +  /* Unlink from the pgno hash table */
  1.2506 +  unlinkHashChain(pPager, pPg);
  1.2507 +}
  1.2508 +
  1.2509 +/*
  1.2510 +** This routine is used to truncate the cache when a database
  1.2511 +** is truncated.  Drop from the cache all pages whose pgno is
  1.2512 +** larger than pPager->dbSize and is unreferenced.
  1.2513 +**
  1.2514 +** Referenced pages larger than pPager->dbSize are zeroed.
  1.2515 +**
  1.2516 +** Actually, at the point this routine is called, it would be
  1.2517 +** an error to have a referenced page.  But rather than delete
  1.2518 +** that page and guarantee a subsequent segfault, it seems better
  1.2519 +** to zero it and hope that we error out sanely.
  1.2520 +*/
  1.2521 +static void pager_truncate_cache(Pager *pPager){
  1.2522 +  PgHdr *pPg;
  1.2523 +  PgHdr **ppPg;
  1.2524 +  int dbSize = pPager->dbSize;
  1.2525 +
  1.2526 +  ppPg = &pPager->pAll;
  1.2527 +  while( (pPg = *ppPg)!=0 ){
  1.2528 +    if( pPg->pgno<=dbSize ){
  1.2529 +      ppPg = &pPg->pNextAll;
  1.2530 +    }else if( pPg->nRef>0 ){
  1.2531 +      memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
  1.2532 +      ppPg = &pPg->pNextAll;
  1.2533 +    }else{
  1.2534 +      *ppPg = pPg->pNextAll;
  1.2535 +      IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
  1.2536 +      PAGER_INCR(sqlite3_pager_pgfree_count);
  1.2537 +      unlinkPage(pPg);
  1.2538 +      makeClean(pPg);
  1.2539 +      sqlite3_free(pPg->pData);
  1.2540 +      sqlite3_free(pPg);
  1.2541 +      pPager->nPage--;
  1.2542 +    }
  1.2543 +  }
  1.2544 +}
  1.2545 +
  1.2546 +/*
  1.2547 +** Try to obtain a lock on a file.  Invoke the busy callback if the lock
  1.2548 +** is currently not available.  Repeat until the busy callback returns
  1.2549 +** false or until the lock succeeds.
  1.2550 +**
  1.2551 +** Return SQLITE_OK on success and an error code if we cannot obtain
  1.2552 +** the lock.
  1.2553 +*/
  1.2554 +static int pager_wait_on_lock(Pager *pPager, int locktype){
  1.2555 +  int rc;
  1.2556 +
  1.2557 +  /* The OS lock values must be the same as the Pager lock values */
  1.2558 +  assert( PAGER_SHARED==SHARED_LOCK );
  1.2559 +  assert( PAGER_RESERVED==RESERVED_LOCK );
  1.2560 +  assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
  1.2561 +
  1.2562 +  /* If the file is currently unlocked then the size must be unknown */
  1.2563 +  assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
  1.2564 +
  1.2565 +  if( pPager->state>=locktype ){
  1.2566 +    rc = SQLITE_OK;
  1.2567 +  }else{
  1.2568 +    do {
  1.2569 +      rc = sqlite3OsLock(pPager->fd, locktype);
  1.2570 +    }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
  1.2571 +    if( rc==SQLITE_OK ){
  1.2572 +      pPager->state = locktype;
  1.2573 +      IOTRACE(("LOCK %p %d\n", pPager, locktype))
  1.2574 +    }
  1.2575 +  }
  1.2576 +  return rc;
  1.2577 +}
  1.2578 +
  1.2579 +/*
  1.2580 +** Truncate the file to the number of pages specified.
  1.2581 +*/
  1.2582 +int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
  1.2583 +  int rc;
  1.2584 +  assert( pPager->state>=PAGER_SHARED || MEMDB );
  1.2585 +  sqlite3PagerPagecount(pPager);
  1.2586 +  if( pPager->errCode ){
  1.2587 +    rc = pPager->errCode;
  1.2588 +    return rc;
  1.2589 +  }
  1.2590 +  if( nPage>=(unsigned)pPager->dbSize ){
  1.2591 +    return SQLITE_OK;
  1.2592 +  }
  1.2593 +  if( MEMDB ){
  1.2594 +    pPager->dbSize = nPage;
  1.2595 +    pager_truncate_cache(pPager);
  1.2596 +    return SQLITE_OK;
  1.2597 +  }
  1.2598 +  pagerEnter(pPager);
  1.2599 +  rc = syncJournal(pPager);
  1.2600 +  pagerLeave(pPager);
  1.2601 +  if( rc!=SQLITE_OK ){
  1.2602 +    return rc;
  1.2603 +  }
  1.2604 +
  1.2605 +  /* Get an exclusive lock on the database before truncating. */
  1.2606 +  pagerEnter(pPager);
  1.2607 +  rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
  1.2608 +  pagerLeave(pPager);
  1.2609 +  if( rc!=SQLITE_OK ){
  1.2610 +    return rc;
  1.2611 +  }
  1.2612 +
  1.2613 +  rc = pager_truncate(pPager, nPage);
  1.2614 +  return rc;
  1.2615 +}
  1.2616 +
  1.2617 +/*
  1.2618 +** Shutdown the page cache.  Free all memory and close all files.
  1.2619 +**
  1.2620 +** If a transaction was in progress when this routine is called, that
  1.2621 +** transaction is rolled back.  All outstanding pages are invalidated
  1.2622 +** and their memory is freed.  Any attempt to use a page associated
  1.2623 +** with this page cache after this function returns will likely
  1.2624 +** result in a coredump.
  1.2625 +**
  1.2626 +** This function always succeeds. If a transaction is active an attempt
  1.2627 +** is made to roll it back. If an error occurs during the rollback 
  1.2628 +** a hot journal may be left in the filesystem but no error is returned
  1.2629 +** to the caller.
  1.2630 +*/
  1.2631 +int sqlite3PagerClose(Pager *pPager){
  1.2632 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  1.2633 +  if( !MEMDB ){
  1.2634 +    sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
  1.2635 +    sqlite3_mutex_enter(mutex);
  1.2636 +    if( pPager->pPrev ){
  1.2637 +      pPager->pPrev->pNext = pPager->pNext;
  1.2638 +    }else{
  1.2639 +      sqlite3PagerList = pPager->pNext;
  1.2640 +    }
  1.2641 +    if( pPager->pNext ){
  1.2642 +      pPager->pNext->pPrev = pPager->pPrev;
  1.2643 +    }
  1.2644 +    sqlite3_mutex_leave(mutex);
  1.2645 +  }
  1.2646 +#endif
  1.2647 +
  1.2648 +  disable_simulated_io_errors();
  1.2649 +  pPager->errCode = 0;
  1.2650 +  pPager->exclusiveMode = 0;
  1.2651 +  pager_reset(pPager);
  1.2652 +  pagerUnlockAndRollback(pPager);
  1.2653 +  enable_simulated_io_errors();
  1.2654 +  PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
  1.2655 +  IOTRACE(("CLOSE %p\n", pPager))
  1.2656 +  assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
  1.2657 +  if( pPager->journalOpen ){
  1.2658 +    sqlite3OsClose(pPager->jfd);
  1.2659 +  }
  1.2660 +  sqlite3_free(pPager->aInJournal);
  1.2661 +  if( pPager->stmtOpen ){
  1.2662 +    sqlite3OsClose(pPager->stfd);
  1.2663 +  }
  1.2664 +  sqlite3OsClose(pPager->fd);
  1.2665 +  /* Temp files are automatically deleted by the OS
  1.2666 +  ** if( pPager->tempFile ){
  1.2667 +  **   sqlite3OsDelete(pPager->zFilename);
  1.2668 +  ** }
  1.2669 +  */
  1.2670 +
  1.2671 +  sqlite3_free(pPager->aHash);
  1.2672 +  sqlite3_free(pPager->pTmpSpace);
  1.2673 +  sqlite3_free(pPager);
  1.2674 +  return SQLITE_OK;
  1.2675 +}
  1.2676 +
  1.2677 +#if !defined(NDEBUG) || defined(SQLITE_TEST)
  1.2678 +/*
  1.2679 +** Return the page number for the given page data.
  1.2680 +*/
  1.2681 +Pgno sqlite3PagerPagenumber(DbPage *p){
  1.2682 +  return p->pgno;
  1.2683 +}
  1.2684 +#endif
  1.2685 +
  1.2686 +/*
  1.2687 +** The page_ref() function increments the reference count for a page.
  1.2688 +** If the page is currently on the freelist (the reference count is zero) then
  1.2689 +** remove it from the freelist.
  1.2690 +**
  1.2691 +** For non-test systems, page_ref() is a macro that calls _page_ref()
  1.2692 +** online of the reference count is zero.  For test systems, page_ref()
  1.2693 +** is a real function so that we can set breakpoints and trace it.
  1.2694 +*/
  1.2695 +static void _page_ref(PgHdr *pPg){
  1.2696 +  if( pPg->nRef==0 ){
  1.2697 +    /* The page is currently on the freelist.  Remove it. */
  1.2698 +    lruListRemove(pPg);
  1.2699 +    pPg->pPager->nRef++;
  1.2700 +  }
  1.2701 +  pPg->nRef++;
  1.2702 +  REFINFO(pPg);
  1.2703 +}
  1.2704 +#ifdef SQLITE_DEBUG
  1.2705 +  static void page_ref(PgHdr *pPg){
  1.2706 +    if( pPg->nRef==0 ){
  1.2707 +      _page_ref(pPg);
  1.2708 +    }else{
  1.2709 +      pPg->nRef++;
  1.2710 +      REFINFO(pPg);
  1.2711 +    }
  1.2712 +  }
  1.2713 +#else
  1.2714 +# define page_ref(P)   ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
  1.2715 +#endif
  1.2716 +
  1.2717 +/*
  1.2718 +** Increment the reference count for a page.  The input pointer is
  1.2719 +** a reference to the page data.
  1.2720 +*/
  1.2721 +int sqlite3PagerRef(DbPage *pPg){
  1.2722 +  pagerEnter(pPg->pPager);
  1.2723 +  page_ref(pPg);
  1.2724 +  pagerLeave(pPg->pPager);
  1.2725 +  return SQLITE_OK;
  1.2726 +}
  1.2727 +
  1.2728 +/*
  1.2729 +** Sync the journal.  In other words, make sure all the pages that have
  1.2730 +** been written to the journal have actually reached the surface of the
  1.2731 +** disk.  It is not safe to modify the original database file until after
  1.2732 +** the journal has been synced.  If the original database is modified before
  1.2733 +** the journal is synced and a power failure occurs, the unsynced journal
  1.2734 +** data would be lost and we would be unable to completely rollback the
  1.2735 +** database changes.  Database corruption would occur.
  1.2736 +** 
  1.2737 +** This routine also updates the nRec field in the header of the journal.
  1.2738 +** (See comments on the pager_playback() routine for additional information.)
  1.2739 +** If the sync mode is FULL, two syncs will occur.  First the whole journal
  1.2740 +** is synced, then the nRec field is updated, then a second sync occurs.
  1.2741 +**
  1.2742 +** For temporary databases, we do not care if we are able to rollback
  1.2743 +** after a power failure, so no sync occurs.
  1.2744 +**
  1.2745 +** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
  1.2746 +** the database is stored, then OsSync() is never called on the journal
  1.2747 +** file. In this case all that is required is to update the nRec field in
  1.2748 +** the journal header.
  1.2749 +**
  1.2750 +** This routine clears the needSync field of every page current held in
  1.2751 +** memory.
  1.2752 +*/
  1.2753 +static int syncJournal(Pager *pPager){
  1.2754 +  PgHdr *pPg;
  1.2755 +  int rc = SQLITE_OK;
  1.2756 +
  1.2757 +
  1.2758 +  /* Sync the journal before modifying the main database
  1.2759 +  ** (assuming there is a journal and it needs to be synced.)
  1.2760 +  */
  1.2761 +  if( pPager->needSync ){
  1.2762 +    if( !pPager->tempFile ){
  1.2763 +      int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
  1.2764 +      assert( pPager->journalOpen );
  1.2765 +
  1.2766 +      /* assert( !pPager->noSync ); // noSync might be set if synchronous
  1.2767 +      ** was turned off after the transaction was started.  Ticket #615 */
  1.2768 +#ifndef NDEBUG
  1.2769 +      {
  1.2770 +        /* Make sure the pPager->nRec counter we are keeping agrees
  1.2771 +        ** with the nRec computed from the size of the journal file.
  1.2772 +        */
  1.2773 +        i64 jSz;
  1.2774 +        rc = sqlite3OsFileSize(pPager->jfd, &jSz);
  1.2775 +        if( rc!=0 ) return rc;
  1.2776 +        assert( pPager->journalOff==jSz );
  1.2777 +      }
  1.2778 +#endif
  1.2779 +      if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
  1.2780 +        /* Write the nRec value into the journal file header. If in
  1.2781 +        ** full-synchronous mode, sync the journal first. This ensures that
  1.2782 +        ** all data has really hit the disk before nRec is updated to mark
  1.2783 +        ** it as a candidate for rollback.
  1.2784 +        **
  1.2785 +        ** This is not required if the persistent media supports the
  1.2786 +        ** SAFE_APPEND property. Because in this case it is not possible 
  1.2787 +        ** for garbage data to be appended to the file, the nRec field
  1.2788 +        ** is populated with 0xFFFFFFFF when the journal header is written
  1.2789 +        ** and never needs to be updated.
  1.2790 +        */
  1.2791 +        i64 jrnlOff;
  1.2792 +        if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
  1.2793 +          PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
  1.2794 +          IOTRACE(("JSYNC %p\n", pPager))
  1.2795 +          rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
  1.2796 +          if( rc!=0 ) return rc;
  1.2797 +        }
  1.2798 +
  1.2799 +        jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
  1.2800 +        IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
  1.2801 +        rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
  1.2802 +        if( rc ) return rc;
  1.2803 +      }
  1.2804 +      if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
  1.2805 +        PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
  1.2806 +        IOTRACE(("JSYNC %p\n", pPager))
  1.2807 +        rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| 
  1.2808 +          (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
  1.2809 +        );
  1.2810 +        if( rc!=0 ) return rc;
  1.2811 +      }
  1.2812 +      pPager->journalStarted = 1;
  1.2813 +    }
  1.2814 +    pPager->needSync = 0;
  1.2815 +
  1.2816 +    /* Erase the needSync flag from every page.
  1.2817 +    */
  1.2818 +    for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
  1.2819 +      pPg->needSync = 0;
  1.2820 +    }
  1.2821 +    lruListSetFirstSynced(pPager);
  1.2822 +  }
  1.2823 +
  1.2824 +#ifndef NDEBUG
  1.2825 +  /* If the Pager.needSync flag is clear then the PgHdr.needSync
  1.2826 +  ** flag must also be clear for all pages.  Verify that this
  1.2827 +  ** invariant is true.
  1.2828 +  */
  1.2829 +  else{
  1.2830 +    for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
  1.2831 +      assert( pPg->needSync==0 );
  1.2832 +    }
  1.2833 +    assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
  1.2834 +  }
  1.2835 +#endif
  1.2836 +
  1.2837 +  return rc;
  1.2838 +}
  1.2839 +
  1.2840 +/*
  1.2841 +** Merge two lists of pages connected by pDirty and in pgno order.
  1.2842 +** Do not both fixing the pPrevDirty pointers.
  1.2843 +*/
  1.2844 +static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
  1.2845 +  PgHdr result, *pTail;
  1.2846 +  pTail = &result;
  1.2847 +  while( pA && pB ){
  1.2848 +    if( pA->pgno<pB->pgno ){
  1.2849 +      pTail->pDirty = pA;
  1.2850 +      pTail = pA;
  1.2851 +      pA = pA->pDirty;
  1.2852 +    }else{
  1.2853 +      pTail->pDirty = pB;
  1.2854 +      pTail = pB;
  1.2855 +      pB = pB->pDirty;
  1.2856 +    }
  1.2857 +  }
  1.2858 +  if( pA ){
  1.2859 +    pTail->pDirty = pA;
  1.2860 +  }else if( pB ){
  1.2861 +    pTail->pDirty = pB;
  1.2862 +  }else{
  1.2863 +    pTail->pDirty = 0;
  1.2864 +  }
  1.2865 +  return result.pDirty;
  1.2866 +}
  1.2867 +
  1.2868 +/*
  1.2869 +** Sort the list of pages in accending order by pgno.  Pages are
  1.2870 +** connected by pDirty pointers.  The pPrevDirty pointers are
  1.2871 +** corrupted by this sort.
  1.2872 +*/
  1.2873 +#define N_SORT_BUCKET_ALLOC 25
  1.2874 +#define N_SORT_BUCKET       25
  1.2875 +#ifdef SQLITE_TEST
  1.2876 +  int sqlite3_pager_n_sort_bucket = 0;
  1.2877 +  #undef N_SORT_BUCKET
  1.2878 +  #define N_SORT_BUCKET \
  1.2879 +   (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
  1.2880 +#endif
  1.2881 +static PgHdr *sort_pagelist(PgHdr *pIn){
  1.2882 +  PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
  1.2883 +  int i;
  1.2884 +  memset(a, 0, sizeof(a));
  1.2885 +  while( pIn ){
  1.2886 +    p = pIn;
  1.2887 +    pIn = p->pDirty;
  1.2888 +    p->pDirty = 0;
  1.2889 +    for(i=0; i<N_SORT_BUCKET-1; i++){
  1.2890 +      if( a[i]==0 ){
  1.2891 +        a[i] = p;
  1.2892 +        break;
  1.2893 +      }else{
  1.2894 +        p = merge_pagelist(a[i], p);
  1.2895 +        a[i] = 0;
  1.2896 +      }
  1.2897 +    }
  1.2898 +    if( i==N_SORT_BUCKET-1 ){
  1.2899 +      /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET) 
  1.2900 +      ** elements in the input list. This is possible, but impractical.
  1.2901 +      ** Testing this line is the point of global variable
  1.2902 +      ** sqlite3_pager_n_sort_bucket.
  1.2903 +      */
  1.2904 +      a[i] = merge_pagelist(a[i], p);
  1.2905 +    }
  1.2906 +  }
  1.2907 +  p = a[0];
  1.2908 +  for(i=1; i<N_SORT_BUCKET; i++){
  1.2909 +    p = merge_pagelist(p, a[i]);
  1.2910 +  }
  1.2911 +  return p;
  1.2912 +}
  1.2913 +
  1.2914 +/*
  1.2915 +** Given a list of pages (connected by the PgHdr.pDirty pointer) write
  1.2916 +** every one of those pages out to the database file and mark them all
  1.2917 +** as clean.
  1.2918 +*/
  1.2919 +static int pager_write_pagelist(PgHdr *pList){
  1.2920 +  Pager *pPager;
  1.2921 +  PgHdr *p;
  1.2922 +  int rc;
  1.2923 +
  1.2924 +  if( pList==0 ) return SQLITE_OK;
  1.2925 +  pPager = pList->pPager;
  1.2926 +
  1.2927 +  /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
  1.2928 +  ** database file. If there is already an EXCLUSIVE lock, the following
  1.2929 +  ** calls to sqlite3OsLock() are no-ops.
  1.2930 +  **
  1.2931 +  ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
  1.2932 +  ** through an intermediate state PENDING.   A PENDING lock prevents new
  1.2933 +  ** readers from attaching to the database but is unsufficient for us to
  1.2934 +  ** write.  The idea of a PENDING lock is to prevent new readers from
  1.2935 +  ** coming in while we wait for existing readers to clear.
  1.2936 +  **
  1.2937 +  ** While the pager is in the RESERVED state, the original database file
  1.2938 +  ** is unchanged and we can rollback without having to playback the
  1.2939 +  ** journal into the original database file.  Once we transition to
  1.2940 +  ** EXCLUSIVE, it means the database file has been changed and any rollback
  1.2941 +  ** will require a journal playback.
  1.2942 +  */
  1.2943 +  rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
  1.2944 +  if( rc!=SQLITE_OK ){
  1.2945 +    return rc;
  1.2946 +  }
  1.2947 +
  1.2948 +  pList = sort_pagelist(pList);
  1.2949 +  for(p=pList; p; p=p->pDirty){
  1.2950 +    assert( p->dirty );
  1.2951 +    p->dirty = 0;
  1.2952 +  }
  1.2953 +  while( pList ){
  1.2954 +
  1.2955 +    /* If the file has not yet been opened, open it now. */
  1.2956 +    if( !pPager->fd->pMethods ){
  1.2957 +      assert(pPager->tempFile);
  1.2958 +      rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
  1.2959 +                                pPager->vfsFlags);
  1.2960 +      if( rc ) return rc;
  1.2961 +    }
  1.2962 +
  1.2963 +    /* If there are dirty pages in the page cache with page numbers greater
  1.2964 +    ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
  1.2965 +    ** make the file smaller (presumably by auto-vacuum code). Do not write
  1.2966 +    ** any such pages to the file.
  1.2967 +    */
  1.2968 +    if( pList->pgno<=pPager->dbSize ){
  1.2969 +      i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
  1.2970 +      char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
  1.2971 +      PAGERTRACE4("STORE %d page %d hash(%08x)\n",
  1.2972 +                   PAGERID(pPager), pList->pgno, pager_pagehash(pList));
  1.2973 +      IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
  1.2974 +      rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
  1.2975 +      PAGER_INCR(sqlite3_pager_writedb_count);
  1.2976 +      PAGER_INCR(pPager->nWrite);
  1.2977 +      if( pList->pgno==1 ){
  1.2978 +        memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
  1.2979 +      }
  1.2980 +    }
  1.2981 +#ifndef NDEBUG
  1.2982 +    else{
  1.2983 +      PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
  1.2984 +    }
  1.2985 +#endif
  1.2986 +    if( rc ) return rc;
  1.2987 +#ifdef SQLITE_CHECK_PAGES
  1.2988 +    pList->pageHash = pager_pagehash(pList);
  1.2989 +#endif
  1.2990 +    pList = pList->pDirty;
  1.2991 +  }
  1.2992 +  return SQLITE_OK;
  1.2993 +}
  1.2994 +
  1.2995 +/*
  1.2996 +** Collect every dirty page into a dirty list and
  1.2997 +** return a pointer to the head of that list.  All pages are
  1.2998 +** collected even if they are still in use.
  1.2999 +*/
  1.3000 +static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
  1.3001 +  return pPager->pDirty;
  1.3002 +}
  1.3003 +
  1.3004 +/*
  1.3005 +** Return TRUE if there is a hot journal on the given pager.
  1.3006 +** A hot journal is one that needs to be played back.
  1.3007 +**
  1.3008 +** If the current size of the database file is 0 but a journal file
  1.3009 +** exists, that is probably an old journal left over from a prior
  1.3010 +** database with the same name.  Just delete the journal.
  1.3011 +*/
  1.3012 +static int hasHotJournal(Pager *pPager){
  1.3013 +  sqlite3_vfs *pVfs = pPager->pVfs;
  1.3014 +  if( !pPager->useJournal ) return 0;
  1.3015 +  if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
  1.3016 +    return 0;
  1.3017 +  }
  1.3018 +  if( sqlite3OsCheckReservedLock(pPager->fd) ){
  1.3019 +    return 0;
  1.3020 +  }
  1.3021 +  if( sqlite3PagerPagecount(pPager)==0 ){
  1.3022 +    sqlite3OsDelete(pVfs, pPager->zJournal, 0);
  1.3023 +    return 0;
  1.3024 +  }else{
  1.3025 +    return 1;
  1.3026 +  }
  1.3027 +}
  1.3028 +
  1.3029 +/*
  1.3030 +** Try to find a page in the cache that can be recycled. 
  1.3031 +**
  1.3032 +** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It 
  1.3033 +** does not set the pPager->errCode variable.
  1.3034 +*/
  1.3035 +static int pager_recycle(Pager *pPager, PgHdr **ppPg){
  1.3036 +  PgHdr *pPg;
  1.3037 +  *ppPg = 0;
  1.3038 +
  1.3039 +  /* It is illegal to call this function unless the pager object
  1.3040 +  ** pointed to by pPager has at least one free page (page with nRef==0).
  1.3041 +  */ 
  1.3042 +  assert(!MEMDB);
  1.3043 +  assert(pPager->lru.pFirst);
  1.3044 +
  1.3045 +  /* Find a page to recycle.  Try to locate a page that does not
  1.3046 +  ** require us to do an fsync() on the journal.
  1.3047 +  */
  1.3048 +  pPg = pPager->lru.pFirstSynced;
  1.3049 +
  1.3050 +  /* If we could not find a page that does not require an fsync()
  1.3051 +  ** on the journal file then fsync the journal file.  This is a
  1.3052 +  ** very slow operation, so we work hard to avoid it.  But sometimes
  1.3053 +  ** it can't be helped.
  1.3054 +  */
  1.3055 +  if( pPg==0 && pPager->lru.pFirst){
  1.3056 +    int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
  1.3057 +    int rc = syncJournal(pPager);
  1.3058 +    if( rc!=0 ){
  1.3059 +      return rc;
  1.3060 +    }
  1.3061 +    if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
  1.3062 +      /* If in full-sync mode, write a new journal header into the
  1.3063 +      ** journal file. This is done to avoid ever modifying a journal
  1.3064 +      ** header that is involved in the rollback of pages that have
  1.3065 +      ** already been written to the database (in case the header is
  1.3066 +      ** trashed when the nRec field is updated).
  1.3067 +      */
  1.3068 +      pPager->nRec = 0;
  1.3069 +      assert( pPager->journalOff > 0 );
  1.3070 +      assert( pPager->doNotSync==0 );
  1.3071 +      rc = writeJournalHdr(pPager);
  1.3072 +      if( rc!=0 ){
  1.3073 +        return rc;
  1.3074 +      }
  1.3075 +    }
  1.3076 +    pPg = pPager->lru.pFirst;
  1.3077 +  }
  1.3078 +
  1.3079 +  assert( pPg->nRef==0 );
  1.3080 +
  1.3081 +  /* Write the page to the database file if it is dirty.
  1.3082 +  */
  1.3083 +  if( pPg->dirty ){
  1.3084 +    int rc;
  1.3085 +    assert( pPg->needSync==0 );
  1.3086 +    makeClean(pPg);
  1.3087 +    pPg->dirty = 1;
  1.3088 +    pPg->pDirty = 0;
  1.3089 +    rc = pager_write_pagelist( pPg );
  1.3090 +    pPg->dirty = 0;
  1.3091 +    if( rc!=SQLITE_OK ){
  1.3092 +      return rc;
  1.3093 +    }
  1.3094 +  }
  1.3095 +  assert( pPg->dirty==0 );
  1.3096 +
  1.3097 +  /* If the page we are recycling is marked as alwaysRollback, then
  1.3098 +  ** set the global alwaysRollback flag, thus disabling the
  1.3099 +  ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
  1.3100 +  ** It is necessary to do this because the page marked alwaysRollback
  1.3101 +  ** might be reloaded at a later time but at that point we won't remember
  1.3102 +  ** that is was marked alwaysRollback.  This means that all pages must
  1.3103 +  ** be marked as alwaysRollback from here on out.
  1.3104 +  */
  1.3105 +  if( pPg->alwaysRollback ){
  1.3106 +    IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
  1.3107 +    pPager->alwaysRollback = 1;
  1.3108 +  }
  1.3109 +
  1.3110 +  /* Unlink the old page from the free list and the hash table
  1.3111 +  */
  1.3112 +  unlinkPage(pPg);
  1.3113 +  assert( pPg->pgno==0 );
  1.3114 +
  1.3115 +  *ppPg = pPg;
  1.3116 +  return SQLITE_OK;
  1.3117 +}
  1.3118 +
  1.3119 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  1.3120 +/*
  1.3121 +** This function is called to free superfluous dynamically allocated memory
  1.3122 +** held by the pager system. Memory in use by any SQLite pager allocated
  1.3123 +** by the current thread may be sqlite3_free()ed.
  1.3124 +**
  1.3125 +** nReq is the number of bytes of memory required. Once this much has
  1.3126 +** been released, the function returns. The return value is the total number 
  1.3127 +** of bytes of memory released.
  1.3128 +*/
  1.3129 +int sqlite3PagerReleaseMemory(int nReq){
  1.3130 +  int nReleased = 0;          /* Bytes of memory released so far */
  1.3131 +  sqlite3_mutex *mutex;       /* The MEM2 mutex */
  1.3132 +  Pager *pPager;              /* For looping over pagers */
  1.3133 +  int rc = SQLITE_OK;
  1.3134 +
  1.3135 +  /* Acquire the memory-management mutex
  1.3136 +  */
  1.3137 +  mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
  1.3138 +  sqlite3_mutex_enter(mutex);
  1.3139 +
  1.3140 +  /* Signal all database connections that memory management wants
  1.3141 +  ** to have access to the pagers.
  1.3142 +  */
  1.3143 +  for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
  1.3144 +     pPager->iInUseMM = 1;
  1.3145 +  }
  1.3146 +
  1.3147 +  while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
  1.3148 +    PgHdr *pPg;
  1.3149 +    PgHdr *pRecycled;
  1.3150 + 
  1.3151 +    /* Try to find a page to recycle that does not require a sync(). If
  1.3152 +    ** this is not possible, find one that does require a sync().
  1.3153 +    */
  1.3154 +    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
  1.3155 +    pPg = sqlite3LruPageList.pFirstSynced;
  1.3156 +    while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
  1.3157 +      pPg = pPg->gfree.pNext;
  1.3158 +    }
  1.3159 +    if( !pPg ){
  1.3160 +      pPg = sqlite3LruPageList.pFirst;
  1.3161 +      while( pPg && pPg->pPager->iInUseDB ){
  1.3162 +        pPg = pPg->gfree.pNext;
  1.3163 +      }
  1.3164 +    }
  1.3165 +    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
  1.3166 +
  1.3167 +    /* If pPg==0, then the block above has failed to find a page to
  1.3168 +    ** recycle. In this case return early - no further memory will
  1.3169 +    ** be released.
  1.3170 +    */
  1.3171 +    if( !pPg ) break;
  1.3172 +
  1.3173 +    pPager = pPg->pPager;
  1.3174 +    assert(!pPg->needSync || pPg==pPager->lru.pFirst);
  1.3175 +    assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
  1.3176 +  
  1.3177 +    rc = pager_recycle(pPager, &pRecycled);
  1.3178 +    assert(pRecycled==pPg || rc!=SQLITE_OK);
  1.3179 +    if( rc==SQLITE_OK ){
  1.3180 +      /* We've found a page to free. At this point the page has been 
  1.3181 +      ** removed from the page hash-table, free-list and synced-list 
  1.3182 +      ** (pFirstSynced). It is still in the all pages (pAll) list. 
  1.3183 +      ** Remove it from this list before freeing.
  1.3184 +      **
  1.3185 +      ** Todo: Check the Pager.pStmt list to make sure this is Ok. It 
  1.3186 +      ** probably is though.
  1.3187 +      */
  1.3188 +      PgHdr *pTmp;
  1.3189 +      assert( pPg );
  1.3190 +      if( pPg==pPager->pAll ){
  1.3191 +         pPager->pAll = pPg->pNextAll;
  1.3192 +      }else{
  1.3193 +        for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
  1.3194 +        pTmp->pNextAll = pPg->pNextAll;
  1.3195 +      }
  1.3196 +      nReleased += (
  1.3197 +          sizeof(*pPg) + pPager->pageSize
  1.3198 +          + sizeof(u32) + pPager->nExtra
  1.3199 +          + MEMDB*sizeof(PgHistory) 
  1.3200 +      );
  1.3201 +      IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
  1.3202 +      PAGER_INCR(sqlite3_pager_pgfree_count);
  1.3203 +      sqlite3_free(pPg->pData);
  1.3204 +      sqlite3_free(pPg);
  1.3205 +      pPager->nPage--;
  1.3206 +    }else{
  1.3207 +      /* An error occured whilst writing to the database file or 
  1.3208 +      ** journal in pager_recycle(). The error is not returned to the 
  1.3209 +      ** caller of this function. Instead, set the Pager.errCode variable.
  1.3210 +      ** The error will be returned to the user (or users, in the case 
  1.3211 +      ** of a shared pager cache) of the pager for which the error occured.
  1.3212 +      */
  1.3213 +      assert(
  1.3214 +          (rc&0xff)==SQLITE_IOERR ||
  1.3215 +          rc==SQLITE_FULL ||
  1.3216 +          rc==SQLITE_BUSY
  1.3217 +      );
  1.3218 +      assert( pPager->state>=PAGER_RESERVED );
  1.3219 +      pager_error(pPager, rc);
  1.3220 +    }
  1.3221 +  }
  1.3222 +
  1.3223 +  /* Clear the memory management flags and release the mutex
  1.3224 +  */
  1.3225 +  for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
  1.3226 +     pPager->iInUseMM = 0;
  1.3227 +  }
  1.3228 +  sqlite3_mutex_leave(mutex);
  1.3229 +
  1.3230 +  /* Return the number of bytes released
  1.3231 +  */
  1.3232 +  return nReleased;
  1.3233 +}
  1.3234 +#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
  1.3235 +
  1.3236 +/*
  1.3237 +** Read the content of page pPg out of the database file.
  1.3238 +*/
  1.3239 +static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
  1.3240 +  int rc;
  1.3241 +  i64 offset;
  1.3242 +  assert( MEMDB==0 );
  1.3243 +  assert(pPager->fd->pMethods||pPager->tempFile);
  1.3244 +  if( !pPager->fd->pMethods ){
  1.3245 +    return SQLITE_IOERR_SHORT_READ;
  1.3246 +  }
  1.3247 +  offset = (pgno-1)*(i64)pPager->pageSize;
  1.3248 +  rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
  1.3249 +  PAGER_INCR(sqlite3_pager_readdb_count);
  1.3250 +  PAGER_INCR(pPager->nRead);
  1.3251 +  IOTRACE(("PGIN %p %d\n", pPager, pgno));
  1.3252 +  if( pgno==1 ){
  1.3253 +    memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
  1.3254 +                                              sizeof(pPager->dbFileVers));
  1.3255 +  }
  1.3256 +  CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
  1.3257 +  PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
  1.3258 +               PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
  1.3259 +  return rc;
  1.3260 +}
  1.3261 +
  1.3262 +
  1.3263 +/*
  1.3264 +** This function is called to obtain the shared lock required before
  1.3265 +** data may be read from the pager cache. If the shared lock has already
  1.3266 +** been obtained, this function is a no-op.
  1.3267 +**
  1.3268 +** Immediately after obtaining the shared lock (if required), this function
  1.3269 +** checks for a hot-journal file. If one is found, an emergency rollback
  1.3270 +** is performed immediately.
  1.3271 +*/
  1.3272 +static int pagerSharedLock(Pager *pPager){
  1.3273 +  int rc = SQLITE_OK;
  1.3274 +  int isHot = 0;
  1.3275 +
  1.3276 +  /* If this database is opened for exclusive access, has no outstanding 
  1.3277 +  ** page references and is in an error-state, now is the chance to clear
  1.3278 +  ** the error. Discard the contents of the pager-cache and treat any
  1.3279 +  ** open journal file as a hot-journal.
  1.3280 +  */
  1.3281 +  if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
  1.3282 +    if( pPager->journalOpen ){
  1.3283 +      isHot = 1;
  1.3284 +    }
  1.3285 +    pager_reset(pPager);
  1.3286 +    pPager->errCode = SQLITE_OK;
  1.3287 +  }
  1.3288 +
  1.3289 +  /* If the pager is still in an error state, do not proceed. The error 
  1.3290 +  ** state will be cleared at some point in the future when all page 
  1.3291 +  ** references are dropped and the cache can be discarded.
  1.3292 +  */
  1.3293 +  if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
  1.3294 +    return pPager->errCode;
  1.3295 +  }
  1.3296 +
  1.3297 +  if( pPager->state==PAGER_UNLOCK || isHot ){
  1.3298 +    sqlite3_vfs *pVfs = pPager->pVfs;
  1.3299 +    if( !MEMDB ){
  1.3300 +      assert( pPager->nRef==0 );
  1.3301 +      if( !pPager->noReadlock ){
  1.3302 +        rc = pager_wait_on_lock(pPager, SHARED_LOCK);
  1.3303 +        if( rc!=SQLITE_OK ){
  1.3304 +          return pager_error(pPager, rc);
  1.3305 +        }
  1.3306 +        assert( pPager->state>=SHARED_LOCK );
  1.3307 +      }
  1.3308 +  
  1.3309 +      /* If a journal file exists, and there is no RESERVED lock on the
  1.3310 +      ** database file, then it either needs to be played back or deleted.
  1.3311 +      */
  1.3312 +      if( hasHotJournal(pPager) || isHot ){
  1.3313 +        /* Get an EXCLUSIVE lock on the database file. At this point it is
  1.3314 +        ** important that a RESERVED lock is not obtained on the way to the
  1.3315 +        ** EXCLUSIVE lock. If it were, another process might open the
  1.3316 +        ** database file, detect the RESERVED lock, and conclude that the
  1.3317 +        ** database is safe to read while this process is still rolling it 
  1.3318 +        ** back.
  1.3319 +        ** 
  1.3320 +        ** Because the intermediate RESERVED lock is not requested, the
  1.3321 +        ** second process will get to this point in the code and fail to
  1.3322 +        ** obtain it's own EXCLUSIVE lock on the database file.
  1.3323 +        */
  1.3324 +        if( pPager->state<EXCLUSIVE_LOCK ){
  1.3325 +          rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
  1.3326 +          if( rc!=SQLITE_OK ){
  1.3327 +            pager_unlock(pPager);
  1.3328 +            return pager_error(pPager, rc);
  1.3329 +          }
  1.3330 +          pPager->state = PAGER_EXCLUSIVE;
  1.3331 +        }
  1.3332 + 
  1.3333 +        /* Open the journal for reading only.  Return SQLITE_BUSY if
  1.3334 +        ** we are unable to open the journal file. 
  1.3335 +        **
  1.3336 +        ** The journal file does not need to be locked itself.  The
  1.3337 +        ** journal file is never open unless the main database file holds
  1.3338 +        ** a write lock, so there is never any chance of two or more
  1.3339 +        ** processes opening the journal at the same time.
  1.3340 +        **
  1.3341 +        ** Open the journal for read/write access. This is because in 
  1.3342 +        ** exclusive-access mode the file descriptor will be kept open and
  1.3343 +        ** possibly used for a transaction later on. On some systems, the
  1.3344 +        ** OsTruncate() call used in exclusive-access mode also requires
  1.3345 +        ** a read/write file handle.
  1.3346 +        */
  1.3347 +        if( !isHot ){
  1.3348 +          rc = SQLITE_BUSY;
  1.3349 +          if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
  1.3350 +            int fout = 0;
  1.3351 +            int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
  1.3352 +            assert( !pPager->tempFile );
  1.3353 +            rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
  1.3354 +            assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
  1.3355 +            if( fout&SQLITE_OPEN_READONLY ){
  1.3356 +              rc = SQLITE_BUSY;
  1.3357 +              sqlite3OsClose(pPager->jfd);
  1.3358 +            }
  1.3359 +          }
  1.3360 +        }
  1.3361 +        if( rc!=SQLITE_OK ){
  1.3362 +          pager_unlock(pPager);
  1.3363 +          return ((rc==SQLITE_NOMEM||rc==SQLITE_IOERR_NOMEM)?rc:SQLITE_BUSY);
  1.3364 +        }
  1.3365 +        pPager->journalOpen = 1;
  1.3366 +        pPager->journalStarted = 0;
  1.3367 +        pPager->journalOff = 0;
  1.3368 +        pPager->setMaster = 0;
  1.3369 +        pPager->journalHdr = 0;
  1.3370 + 
  1.3371 +        /* Playback and delete the journal.  Drop the database write
  1.3372 +        ** lock and reacquire the read lock.
  1.3373 +        */
  1.3374 +        rc = pager_playback(pPager, 1);
  1.3375 +        if( rc!=SQLITE_OK ){
  1.3376 +          return pager_error(pPager, rc);
  1.3377 +        }
  1.3378 +        assert(pPager->state==PAGER_SHARED || 
  1.3379 +            (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
  1.3380 +        );
  1.3381 +      }
  1.3382 +
  1.3383 +      if( pPager->pAll ){
  1.3384 +        /* The shared-lock has just been acquired on the database file
  1.3385 +        ** and there are already pages in the cache (from a previous
  1.3386 +        ** read or write transaction).  Check to see if the database
  1.3387 +        ** has been modified.  If the database has changed, flush the
  1.3388 +        ** cache.
  1.3389 +        **
  1.3390 +        ** Database changes is detected by looking at 15 bytes beginning
  1.3391 +        ** at offset 24 into the file.  The first 4 of these 16 bytes are
  1.3392 +        ** a 32-bit counter that is incremented with each change.  The
  1.3393 +        ** other bytes change randomly with each file change when
  1.3394 +        ** a codec is in use.
  1.3395 +        ** 
  1.3396 +        ** There is a vanishingly small chance that a change will not be 
  1.3397 +        ** detected.  The chance of an undetected change is so small that
  1.3398 +        ** it can be neglected.
  1.3399 +        */
  1.3400 +        char dbFileVers[sizeof(pPager->dbFileVers)];
  1.3401 +        sqlite3PagerPagecount(pPager);
  1.3402 +
  1.3403 +        if( pPager->errCode ){
  1.3404 +          return pPager->errCode;
  1.3405 +        }
  1.3406 +
  1.3407 +        if( pPager->dbSize>0 ){
  1.3408 +          IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
  1.3409 +          rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
  1.3410 +          if( rc!=SQLITE_OK ){
  1.3411 +            return rc;
  1.3412 +          }
  1.3413 +        }else{
  1.3414 +          memset(dbFileVers, 0, sizeof(dbFileVers));
  1.3415 +        }
  1.3416 +
  1.3417 +        if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
  1.3418 +          pager_reset(pPager);
  1.3419 +        }
  1.3420 +      }
  1.3421 +    }
  1.3422 +    assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
  1.3423 +    if( pPager->state==PAGER_UNLOCK ){
  1.3424 +      pPager->state = PAGER_SHARED;
  1.3425 +    }
  1.3426 +  }
  1.3427 +
  1.3428 +  return rc;
  1.3429 +}
  1.3430 +
  1.3431 +/*
  1.3432 +** Allocate a PgHdr object.   Either create a new one or reuse
  1.3433 +** an existing one that is not otherwise in use.
  1.3434 +**
  1.3435 +** A new PgHdr structure is created if any of the following are
  1.3436 +** true:
  1.3437 +**
  1.3438 +**     (1)  We have not exceeded our maximum allocated cache size
  1.3439 +**          as set by the "PRAGMA cache_size" command.
  1.3440 +**
  1.3441 +**     (2)  There are no unused PgHdr objects available at this time.
  1.3442 +**
  1.3443 +**     (3)  This is an in-memory database.
  1.3444 +**
  1.3445 +**     (4)  There are no PgHdr objects that do not require a journal
  1.3446 +**          file sync and a sync of the journal file is currently
  1.3447 +**          prohibited.
  1.3448 +**
  1.3449 +** Otherwise, reuse an existing PgHdr.  In other words, reuse an
  1.3450 +** existing PgHdr if all of the following are true:
  1.3451 +**
  1.3452 +**     (1)  We have reached or exceeded the maximum cache size
  1.3453 +**          allowed by "PRAGMA cache_size".
  1.3454 +**
  1.3455 +**     (2)  There is a PgHdr available with PgHdr->nRef==0
  1.3456 +**
  1.3457 +**     (3)  We are not in an in-memory database
  1.3458 +**
  1.3459 +**     (4)  Either there is an available PgHdr that does not need
  1.3460 +**          to be synced to disk or else disk syncing is currently
  1.3461 +**          allowed.
  1.3462 +*/
  1.3463 +static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
  1.3464 +  int rc = SQLITE_OK;
  1.3465 +  PgHdr *pPg;
  1.3466 +  void *pData;
  1.3467 +
  1.3468 +  /* Create a new PgHdr if any of the four conditions defined 
  1.3469 +  ** above are met: */
  1.3470 +  if( pPager->nPage<pPager->mxPage
  1.3471 +   || pPager->lru.pFirst==0 
  1.3472 +   || MEMDB
  1.3473 +   || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
  1.3474 +  ){
  1.3475 +    if( pPager->nPage>=pPager->nHash ){
  1.3476 +      pager_resize_hash_table(pPager,
  1.3477 +         pPager->nHash<256 ? 256 : pPager->nHash*2);
  1.3478 +      if( pPager->nHash==0 ){
  1.3479 +        rc = SQLITE_NOMEM;
  1.3480 +        goto pager_allocate_out;
  1.3481 +      }
  1.3482 +    }
  1.3483 +    pagerLeave(pPager);
  1.3484 +    pPg = sqlite3_malloc( sizeof(*pPg) + sizeof(u32) + pPager->nExtra
  1.3485 +                            + MEMDB*sizeof(PgHistory) );
  1.3486 +    if( pPg ){
  1.3487 +      pData = sqlite3_malloc( pPager->pageSize );
  1.3488 +      if( pData==0 ){
  1.3489 +        sqlite3_free(pPg);
  1.3490 +        pPg = 0;
  1.3491 +      }
  1.3492 +    }
  1.3493 +    pagerEnter(pPager);
  1.3494 +    if( pPg==0 ){
  1.3495 +      rc = SQLITE_NOMEM;
  1.3496 +      goto pager_allocate_out;
  1.3497 +    }
  1.3498 +    memset(pPg, 0, sizeof(*pPg));
  1.3499 +    if( MEMDB ){
  1.3500 +      memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
  1.3501 +    }
  1.3502 +    pPg->pData = pData;
  1.3503 +    pPg->pPager = pPager;
  1.3504 +    pPg->pNextAll = pPager->pAll;
  1.3505 +    pPager->pAll = pPg;
  1.3506 +    pPager->nPage++;
  1.3507 +  }else{
  1.3508 +    /* Recycle an existing page with a zero ref-count. */
  1.3509 +    rc = pager_recycle(pPager, &pPg);
  1.3510 +    if( rc==SQLITE_BUSY ){
  1.3511 +      rc = SQLITE_IOERR_BLOCKED;
  1.3512 +    }
  1.3513 +    if( rc!=SQLITE_OK ){
  1.3514 +      goto pager_allocate_out;
  1.3515 +    }
  1.3516 +    assert( pPager->state>=SHARED_LOCK );
  1.3517 +    assert(pPg);
  1.3518 +  }
  1.3519 +  *ppPg = pPg;
  1.3520 +
  1.3521 +pager_allocate_out:
  1.3522 +  return rc;
  1.3523 +}
  1.3524 +
  1.3525 +/*
  1.3526 +** Make sure we have the content for a page.  If the page was
  1.3527 +** previously acquired with noContent==1, then the content was
  1.3528 +** just initialized to zeros instead of being read from disk.
  1.3529 +** But now we need the real data off of disk.  So make sure we
  1.3530 +** have it.  Read it in if we do not have it already.
  1.3531 +*/
  1.3532 +static int pager_get_content(PgHdr *pPg){
  1.3533 +  if( pPg->needRead ){
  1.3534 +    int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
  1.3535 +    if( rc==SQLITE_OK ){
  1.3536 +      pPg->needRead = 0;
  1.3537 +    }else{
  1.3538 +      return rc;
  1.3539 +    }
  1.3540 +  }
  1.3541 +  return SQLITE_OK;
  1.3542 +}
  1.3543 +
  1.3544 +/*
  1.3545 +** Acquire a page.
  1.3546 +**
  1.3547 +** A read lock on the disk file is obtained when the first page is acquired. 
  1.3548 +** This read lock is dropped when the last page is released.
  1.3549 +**
  1.3550 +** This routine works for any page number greater than 0.  If the database
  1.3551 +** file is smaller than the requested page, then no actual disk
  1.3552 +** read occurs and the memory image of the page is initialized to
  1.3553 +** all zeros.  The extra data appended to a page is always initialized
  1.3554 +** to zeros the first time a page is loaded into memory.
  1.3555 +**
  1.3556 +** The acquisition might fail for several reasons.  In all cases,
  1.3557 +** an appropriate error code is returned and *ppPage is set to NULL.
  1.3558 +**
  1.3559 +** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
  1.3560 +** to find a page in the in-memory cache first.  If the page is not already
  1.3561 +** in memory, this routine goes to disk to read it in whereas Lookup()
  1.3562 +** just returns 0.  This routine acquires a read-lock the first time it
  1.3563 +** has to go to disk, and could also playback an old journal if necessary.
  1.3564 +** Since Lookup() never goes to disk, it never has to deal with locks
  1.3565 +** or journal files.
  1.3566 +**
  1.3567 +** If noContent is false, the page contents are actually read from disk.
  1.3568 +** If noContent is true, it means that we do not care about the contents
  1.3569 +** of the page at this time, so do not do a disk read.  Just fill in the
  1.3570 +** page content with zeros.  But mark the fact that we have not read the
  1.3571 +** content by setting the PgHdr.needRead flag.  Later on, if 
  1.3572 +** sqlite3PagerWrite() is called on this page or if this routine is
  1.3573 +** called again with noContent==0, that means that the content is needed
  1.3574 +** and the disk read should occur at that point.
  1.3575 +*/
  1.3576 +static int pagerAcquire(
  1.3577 +  Pager *pPager,      /* The pager open on the database file */
  1.3578 +  Pgno pgno,          /* Page number to fetch */
  1.3579 +  DbPage **ppPage,    /* Write a pointer to the page here */
  1.3580 +  int noContent       /* Do not bother reading content from disk if true */
  1.3581 +){
  1.3582 +  PgHdr *pPg;
  1.3583 +  int rc;
  1.3584 +
  1.3585 +  assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
  1.3586 +
  1.3587 +  /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
  1.3588 +  ** number greater than this, or zero, is requested.
  1.3589 +  */
  1.3590 +  if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
  1.3591 +    return SQLITE_CORRUPT_BKPT;
  1.3592 +  }
  1.3593 +
  1.3594 +  /* Make sure we have not hit any critical errors.
  1.3595 +  */ 
  1.3596 +  assert( pPager!=0 );
  1.3597 +  *ppPage = 0;
  1.3598 +
  1.3599 +  /* If this is the first page accessed, then get a SHARED lock
  1.3600 +  ** on the database file. pagerSharedLock() is a no-op if 
  1.3601 +  ** a database lock is already held.
  1.3602 +  */
  1.3603 +  rc = pagerSharedLock(pPager);
  1.3604 +  if( rc!=SQLITE_OK ){
  1.3605 +    return rc;
  1.3606 +  }
  1.3607 +  assert( pPager->state!=PAGER_UNLOCK );
  1.3608 +
  1.3609 +  pPg = pager_lookup(pPager, pgno);
  1.3610 +  if( pPg==0 ){
  1.3611 +    /* The requested page is not in the page cache. */
  1.3612 +    int nMax;
  1.3613 +    int h;
  1.3614 +    PAGER_INCR(pPager->nMiss);
  1.3615 +    rc = pagerAllocatePage(pPager, &pPg);
  1.3616 +    if( rc!=SQLITE_OK ){
  1.3617 +      return rc;
  1.3618 +    }
  1.3619 +
  1.3620 +    pPg->pgno = pgno;
  1.3621 +    assert( !MEMDB || pgno>pPager->stmtSize );
  1.3622 +    if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
  1.3623 +#if 0
  1.3624 +      sqlite3CheckMemory(pPager->aInJournal, pgno/8);
  1.3625 +#endif
  1.3626 +      assert( pPager->journalOpen );
  1.3627 +      pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
  1.3628 +      pPg->needSync = 0;
  1.3629 +    }else{
  1.3630 +      pPg->inJournal = 0;
  1.3631 +      pPg->needSync = 0;
  1.3632 +    }
  1.3633 +
  1.3634 +    makeClean(pPg);
  1.3635 +    pPg->nRef = 1;
  1.3636 +    REFINFO(pPg);
  1.3637 +
  1.3638 +    pPager->nRef++;
  1.3639 +    if( pPager->nExtra>0 ){
  1.3640 +      memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
  1.3641 +    }
  1.3642 +    nMax = sqlite3PagerPagecount(pPager);
  1.3643 +    if( pPager->errCode ){
  1.3644 +      rc = pPager->errCode;
  1.3645 +      sqlite3PagerUnref(pPg);
  1.3646 +      return rc;
  1.3647 +    }
  1.3648 +
  1.3649 +    /* Populate the page with data, either by reading from the database
  1.3650 +    ** file, or by setting the entire page to zero.
  1.3651 +    */
  1.3652 +    if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
  1.3653 +      if( pgno>pPager->mxPgno ){
  1.3654 +        sqlite3PagerUnref(pPg);
  1.3655 +        return SQLITE_FULL;
  1.3656 +      }
  1.3657 +      memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
  1.3658 +      pPg->needRead = noContent && !pPager->alwaysRollback;
  1.3659 +      IOTRACE(("ZERO %p %d\n", pPager, pgno));
  1.3660 +    }else{
  1.3661 +      rc = readDbPage(pPager, pPg, pgno);
  1.3662 +      if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
  1.3663 +        pPg->pgno = 0;
  1.3664 +        sqlite3PagerUnref(pPg);
  1.3665 +        return rc;
  1.3666 +      }
  1.3667 +      pPg->needRead = 0;
  1.3668 +    }
  1.3669 +
  1.3670 +    /* Link the page into the page hash table */
  1.3671 +    h = pgno & (pPager->nHash-1);
  1.3672 +    assert( pgno!=0 );
  1.3673 +    pPg->pNextHash = pPager->aHash[h];
  1.3674 +    pPager->aHash[h] = pPg;
  1.3675 +    if( pPg->pNextHash ){
  1.3676 +      assert( pPg->pNextHash->pPrevHash==0 );
  1.3677 +      pPg->pNextHash->pPrevHash = pPg;
  1.3678 +    }
  1.3679 +
  1.3680 +#ifdef SQLITE_CHECK_PAGES
  1.3681 +    pPg->pageHash = pager_pagehash(pPg);
  1.3682 +#endif
  1.3683 +  }else{
  1.3684 +    /* The requested page is in the page cache. */
  1.3685 +    assert(pPager->nRef>0 || pgno==1);
  1.3686 +    PAGER_INCR(pPager->nHit);
  1.3687 +    if( !noContent ){
  1.3688 +      rc = pager_get_content(pPg);
  1.3689 +      if( rc ){
  1.3690 +        return rc;
  1.3691 +      }
  1.3692 +    }
  1.3693 +    page_ref(pPg);
  1.3694 +  }
  1.3695 +  *ppPage = pPg;
  1.3696 +  return SQLITE_OK;
  1.3697 +}
  1.3698 +int sqlite3PagerAcquire(
  1.3699 +  Pager *pPager,      /* The pager open on the database file */
  1.3700 +  Pgno pgno,          /* Page number to fetch */
  1.3701 +  DbPage **ppPage,    /* Write a pointer to the page here */
  1.3702 +  int noContent       /* Do not bother reading content from disk if true */
  1.3703 +){
  1.3704 +  int rc;
  1.3705 +  pagerEnter(pPager);
  1.3706 +  rc = pagerAcquire(pPager, pgno, ppPage, noContent);
  1.3707 +  pagerLeave(pPager);
  1.3708 +  return rc;
  1.3709 +}
  1.3710 +
  1.3711 +
  1.3712 +/*
  1.3713 +** Acquire a page if it is already in the in-memory cache.  Do
  1.3714 +** not read the page from disk.  Return a pointer to the page,
  1.3715 +** or 0 if the page is not in cache.
  1.3716 +**
  1.3717 +** See also sqlite3PagerGet().  The difference between this routine
  1.3718 +** and sqlite3PagerGet() is that _get() will go to the disk and read
  1.3719 +** in the page if the page is not already in cache.  This routine
  1.3720 +** returns NULL if the page is not in cache or if a disk I/O error 
  1.3721 +** has ever happened.
  1.3722 +*/
  1.3723 +DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
  1.3724 +  PgHdr *pPg = 0;
  1.3725 +
  1.3726 +  assert( pPager!=0 );
  1.3727 +  assert( pgno!=0 );
  1.3728 +
  1.3729 +  pagerEnter(pPager);
  1.3730 +  if( pPager->state==PAGER_UNLOCK ){
  1.3731 +    assert( !pPager->pAll || pPager->exclusiveMode );
  1.3732 +  }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
  1.3733 +    /* Do nothing */
  1.3734 +  }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
  1.3735 +    page_ref(pPg);
  1.3736 +  }
  1.3737 +  pagerLeave(pPager);
  1.3738 +  return pPg;
  1.3739 +}
  1.3740 +
  1.3741 +/*
  1.3742 +** Release a page.
  1.3743 +**
  1.3744 +** If the number of references to the page drop to zero, then the
  1.3745 +** page is added to the LRU list.  When all references to all pages
  1.3746 +** are released, a rollback occurs and the lock on the database is
  1.3747 +** removed.
  1.3748 +*/
  1.3749 +int sqlite3PagerUnref(DbPage *pPg){
  1.3750 +  Pager *pPager = pPg->pPager;
  1.3751 +
  1.3752 +  /* Decrement the reference count for this page
  1.3753 +  */
  1.3754 +  assert( pPg->nRef>0 );
  1.3755 +  pagerEnter(pPg->pPager);
  1.3756 +  pPg->nRef--;
  1.3757 +  REFINFO(pPg);
  1.3758 +
  1.3759 +  CHECK_PAGE(pPg);
  1.3760 +
  1.3761 +  /* When the number of references to a page reach 0, call the
  1.3762 +  ** destructor and add the page to the freelist.
  1.3763 +  */
  1.3764 +  if( pPg->nRef==0 ){
  1.3765 +
  1.3766 +    lruListAdd(pPg);
  1.3767 +    if( pPager->xDestructor ){
  1.3768 +      pPager->xDestructor(pPg, pPager->pageSize);
  1.3769 +    }
  1.3770 +  
  1.3771 +    /* When all pages reach the freelist, drop the read lock from
  1.3772 +    ** the database file.
  1.3773 +    */
  1.3774 +    pPager->nRef--;
  1.3775 +    assert( pPager->nRef>=0 );
  1.3776 +    if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
  1.3777 +      pagerUnlockAndRollback(pPager);
  1.3778 +    }
  1.3779 +  }
  1.3780 +  pagerLeave(pPager);
  1.3781 +  return SQLITE_OK;
  1.3782 +}
  1.3783 +
  1.3784 +/*
  1.3785 +** Create a journal file for pPager.  There should already be a RESERVED
  1.3786 +** or EXCLUSIVE lock on the database file when this routine is called.
  1.3787 +**
  1.3788 +** Return SQLITE_OK if everything.  Return an error code and release the
  1.3789 +** write lock if anything goes wrong.
  1.3790 +*/
  1.3791 +static int pager_open_journal(Pager *pPager){
  1.3792 +  sqlite3_vfs *pVfs = pPager->pVfs;
  1.3793 +  int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
  1.3794 +
  1.3795 +  int rc;
  1.3796 +  assert( !MEMDB );
  1.3797 +  assert( pPager->state>=PAGER_RESERVED );
  1.3798 +  assert( pPager->journalOpen==0 );
  1.3799 +  assert( pPager->useJournal );
  1.3800 +  assert( pPager->aInJournal==0 );
  1.3801 +  sqlite3PagerPagecount(pPager);
  1.3802 +  pagerLeave(pPager);
  1.3803 +  pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
  1.3804 +  pagerEnter(pPager);
  1.3805 +  if( pPager->aInJournal==0 ){
  1.3806 +    rc = SQLITE_NOMEM;
  1.3807 +    goto failed_to_open_journal;
  1.3808 +  }
  1.3809 +
  1.3810 +  if( pPager->tempFile ){
  1.3811 +    flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
  1.3812 +  }else{
  1.3813 +    flags |= (SQLITE_OPEN_MAIN_JOURNAL);
  1.3814 +  }
  1.3815 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
  1.3816 +  rc = sqlite3JournalOpen(
  1.3817 +      pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
  1.3818 +  );
  1.3819 +#else
  1.3820 +  rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
  1.3821 +#endif
  1.3822 +  assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
  1.3823 +  pPager->journalOff = 0;
  1.3824 +  pPager->setMaster = 0;
  1.3825 +  pPager->journalHdr = 0;
  1.3826 +  if( rc!=SQLITE_OK ){
  1.3827 +    if( rc==SQLITE_NOMEM ){
  1.3828 +      sqlite3OsDelete(pVfs, pPager->zJournal, 0);
  1.3829 +    }
  1.3830 +    goto failed_to_open_journal;
  1.3831 +  }
  1.3832 +  pPager->journalOpen = 1;
  1.3833 +  pPager->journalStarted = 0;
  1.3834 +  pPager->needSync = 0;
  1.3835 +  pPager->alwaysRollback = 0;
  1.3836 +  pPager->nRec = 0;
  1.3837 +  if( pPager->errCode ){
  1.3838 +    rc = pPager->errCode;
  1.3839 +    goto failed_to_open_journal;
  1.3840 +  }
  1.3841 +  pPager->origDbSize = pPager->dbSize;
  1.3842 +
  1.3843 +  rc = writeJournalHdr(pPager);
  1.3844 +
  1.3845 +  if( pPager->stmtAutoopen && rc==SQLITE_OK ){
  1.3846 +    rc = sqlite3PagerStmtBegin(pPager);
  1.3847 +  }
  1.3848 +  if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
  1.3849 +    rc = pager_end_transaction(pPager);
  1.3850 +    if( rc==SQLITE_OK ){
  1.3851 +      rc = SQLITE_FULL;
  1.3852 +    }
  1.3853 +  }
  1.3854 +  return rc;
  1.3855 +
  1.3856 +failed_to_open_journal:
  1.3857 +  sqlite3_free(pPager->aInJournal);
  1.3858 +  pPager->aInJournal = 0;
  1.3859 +  return rc;
  1.3860 +}
  1.3861 +
  1.3862 +/*
  1.3863 +** Acquire a write-lock on the database.  The lock is removed when
  1.3864 +** the any of the following happen:
  1.3865 +**
  1.3866 +**   *  sqlite3PagerCommitPhaseTwo() is called.
  1.3867 +**   *  sqlite3PagerRollback() is called.
  1.3868 +**   *  sqlite3PagerClose() is called.
  1.3869 +**   *  sqlite3PagerUnref() is called to on every outstanding page.
  1.3870 +**
  1.3871 +** The first parameter to this routine is a pointer to any open page of the
  1.3872 +** database file.  Nothing changes about the page - it is used merely to
  1.3873 +** acquire a pointer to the Pager structure and as proof that there is
  1.3874 +** already a read-lock on the database.
  1.3875 +**
  1.3876 +** The second parameter indicates how much space in bytes to reserve for a
  1.3877 +** master journal file-name at the start of the journal when it is created.
  1.3878 +**
  1.3879 +** A journal file is opened if this is not a temporary file.  For temporary
  1.3880 +** files, the opening of the journal file is deferred until there is an
  1.3881 +** actual need to write to the journal.
  1.3882 +**
  1.3883 +** If the database is already reserved for writing, this routine is a no-op.
  1.3884 +**
  1.3885 +** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
  1.3886 +** immediately instead of waiting until we try to flush the cache.  The
  1.3887 +** exFlag is ignored if a transaction is already active.
  1.3888 +*/
  1.3889 +int sqlite3PagerBegin(DbPage *pPg, int exFlag){
  1.3890 +  Pager *pPager = pPg->pPager;
  1.3891 +  int rc = SQLITE_OK;
  1.3892 +  pagerEnter(pPager);
  1.3893 +  assert( pPg->nRef>0 );
  1.3894 +  assert( pPager->state!=PAGER_UNLOCK );
  1.3895 +  if( pPager->state==PAGER_SHARED ){
  1.3896 +    assert( pPager->aInJournal==0 );
  1.3897 +    if( MEMDB ){
  1.3898 +      pPager->state = PAGER_EXCLUSIVE;
  1.3899 +      pPager->origDbSize = pPager->dbSize;
  1.3900 +    }else{
  1.3901 +      rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
  1.3902 +      if( rc==SQLITE_OK ){
  1.3903 +        pPager->state = PAGER_RESERVED;
  1.3904 +        if( exFlag ){
  1.3905 +          rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
  1.3906 +        }
  1.3907 +      }
  1.3908 +      if( rc!=SQLITE_OK ){
  1.3909 +        pagerLeave(pPager);
  1.3910 +        return rc;
  1.3911 +      }
  1.3912 +      pPager->dirtyCache = 0;
  1.3913 +      PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
  1.3914 +      if( pPager->useJournal && !pPager->tempFile ){
  1.3915 +        rc = pager_open_journal(pPager);
  1.3916 +      }
  1.3917 +    }
  1.3918 +  }else if( pPager->journalOpen && pPager->journalOff==0 ){
  1.3919 +    /* This happens when the pager was in exclusive-access mode last
  1.3920 +    ** time a (read or write) transaction was successfully concluded
  1.3921 +    ** by this connection. Instead of deleting the journal file it was 
  1.3922 +    ** kept open and truncated to 0 bytes.
  1.3923 +    */
  1.3924 +    assert( pPager->nRec==0 );
  1.3925 +    assert( pPager->origDbSize==0 );
  1.3926 +    assert( pPager->aInJournal==0 );
  1.3927 +    sqlite3PagerPagecount(pPager);
  1.3928 +    pagerLeave(pPager);
  1.3929 +    pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
  1.3930 +    pagerEnter(pPager);
  1.3931 +    if( !pPager->aInJournal ){
  1.3932 +      rc = SQLITE_NOMEM;
  1.3933 +    }else{
  1.3934 +      pPager->origDbSize = pPager->dbSize;
  1.3935 +      rc = writeJournalHdr(pPager);
  1.3936 +    }
  1.3937 +  }
  1.3938 +  assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
  1.3939 +  pagerLeave(pPager);
  1.3940 +  return rc;
  1.3941 +}
  1.3942 +
  1.3943 +/*
  1.3944 +** Make a page dirty.  Set its dirty flag and add it to the dirty
  1.3945 +** page list.
  1.3946 +*/
  1.3947 +static void makeDirty(PgHdr *pPg){
  1.3948 +  if( pPg->dirty==0 ){
  1.3949 +    Pager *pPager = pPg->pPager;
  1.3950 +    pPg->dirty = 1;
  1.3951 +    pPg->pDirty = pPager->pDirty;
  1.3952 +    if( pPager->pDirty ){
  1.3953 +      pPager->pDirty->pPrevDirty = pPg;
  1.3954 +    }
  1.3955 +    pPg->pPrevDirty = 0;
  1.3956 +    pPager->pDirty = pPg;
  1.3957 +  }
  1.3958 +}
  1.3959 +
  1.3960 +/*
  1.3961 +** Make a page clean.  Clear its dirty bit and remove it from the
  1.3962 +** dirty page list.
  1.3963 +*/
  1.3964 +static void makeClean(PgHdr *pPg){
  1.3965 +  if( pPg->dirty ){
  1.3966 +    pPg->dirty = 0;
  1.3967 +    if( pPg->pDirty ){
  1.3968 +      assert( pPg->pDirty->pPrevDirty==pPg );
  1.3969 +      pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
  1.3970 +    }
  1.3971 +    if( pPg->pPrevDirty ){
  1.3972 +      assert( pPg->pPrevDirty->pDirty==pPg );
  1.3973 +      pPg->pPrevDirty->pDirty = pPg->pDirty;
  1.3974 +    }else{
  1.3975 +      assert( pPg->pPager->pDirty==pPg );
  1.3976 +      pPg->pPager->pDirty = pPg->pDirty;
  1.3977 +    }
  1.3978 +  }
  1.3979 +}
  1.3980 +
  1.3981 +
  1.3982 +/*
  1.3983 +** Mark a data page as writeable.  The page is written into the journal 
  1.3984 +** if it is not there already.  This routine must be called before making
  1.3985 +** changes to a page.
  1.3986 +**
  1.3987 +** The first time this routine is called, the pager creates a new
  1.3988 +** journal and acquires a RESERVED lock on the database.  If the RESERVED
  1.3989 +** lock could not be acquired, this routine returns SQLITE_BUSY.  The
  1.3990 +** calling routine must check for that return value and be careful not to
  1.3991 +** change any page data until this routine returns SQLITE_OK.
  1.3992 +**
  1.3993 +** If the journal file could not be written because the disk is full,
  1.3994 +** then this routine returns SQLITE_FULL and does an immediate rollback.
  1.3995 +** All subsequent write attempts also return SQLITE_FULL until there
  1.3996 +** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
  1.3997 +** reset.
  1.3998 +*/
  1.3999 +static int pager_write(PgHdr *pPg){
  1.4000 +  void *pData = PGHDR_TO_DATA(pPg);
  1.4001 +  Pager *pPager = pPg->pPager;
  1.4002 +  int rc = SQLITE_OK;
  1.4003 +
  1.4004 +  /* Check for errors
  1.4005 +  */
  1.4006 +  if( pPager->errCode ){ 
  1.4007 +    return pPager->errCode;
  1.4008 +  }
  1.4009 +  if( pPager->readOnly ){
  1.4010 +    return SQLITE_PERM;
  1.4011 +  }
  1.4012 +
  1.4013 +  assert( !pPager->setMaster );
  1.4014 +
  1.4015 +  CHECK_PAGE(pPg);
  1.4016 +
  1.4017 +  /* If this page was previously acquired with noContent==1, that means
  1.4018 +  ** we didn't really read in the content of the page.  This can happen
  1.4019 +  ** (for example) when the page is being moved to the freelist.  But
  1.4020 +  ** now we are (perhaps) moving the page off of the freelist for
  1.4021 +  ** reuse and we need to know its original content so that content
  1.4022 +  ** can be stored in the rollback journal.  So do the read at this
  1.4023 +  ** time.
  1.4024 +  */
  1.4025 +  rc = pager_get_content(pPg);
  1.4026 +  if( rc ){
  1.4027 +    return rc;
  1.4028 +  }
  1.4029 +
  1.4030 +  /* Mark the page as dirty.  If the page has already been written
  1.4031 +  ** to the journal then we can return right away.
  1.4032 +  */
  1.4033 +  makeDirty(pPg);
  1.4034 +  if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
  1.4035 +    pPager->dirtyCache = 1;
  1.4036 +  }else{
  1.4037 +
  1.4038 +    /* If we get this far, it means that the page needs to be
  1.4039 +    ** written to the transaction journal or the ckeckpoint journal
  1.4040 +    ** or both.
  1.4041 +    **
  1.4042 +    ** First check to see that the transaction journal exists and
  1.4043 +    ** create it if it does not.
  1.4044 +    */
  1.4045 +    assert( pPager->state!=PAGER_UNLOCK );
  1.4046 +    rc = sqlite3PagerBegin(pPg, 0);
  1.4047 +    if( rc!=SQLITE_OK ){
  1.4048 +      return rc;
  1.4049 +    }
  1.4050 +    assert( pPager->state>=PAGER_RESERVED );
  1.4051 +    if( !pPager->journalOpen && pPager->useJournal ){
  1.4052 +      rc = pager_open_journal(pPager);
  1.4053 +      if( rc!=SQLITE_OK ) return rc;
  1.4054 +    }
  1.4055 +    assert( pPager->journalOpen || !pPager->useJournal );
  1.4056 +    pPager->dirtyCache = 1;
  1.4057 +  
  1.4058 +    /* The transaction journal now exists and we have a RESERVED or an
  1.4059 +    ** EXCLUSIVE lock on the main database file.  Write the current page to
  1.4060 +    ** the transaction journal if it is not there already.
  1.4061 +    */
  1.4062 +    if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
  1.4063 +      if( (int)pPg->pgno <= pPager->origDbSize ){
  1.4064 +        if( MEMDB ){
  1.4065 +          PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  1.4066 +          PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
  1.4067 +          assert( pHist->pOrig==0 );
  1.4068 +          pHist->pOrig = sqlite3_malloc( pPager->pageSize );
  1.4069 +          if( pHist->pOrig ){
  1.4070 +            memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
  1.4071 +          }
  1.4072 +        }else{
  1.4073 +          u32 cksum;
  1.4074 +          char *pData2;
  1.4075 +
  1.4076 +          /* We should never write to the journal file the page that
  1.4077 +          ** contains the database locks.  The following assert verifies
  1.4078 +          ** that we do not. */
  1.4079 +          assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
  1.4080 +          pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
  1.4081 +          cksum = pager_cksum(pPager, (u8*)pData2);
  1.4082 +          rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
  1.4083 +          if( rc==SQLITE_OK ){
  1.4084 +            rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
  1.4085 +                                pPager->journalOff + 4);
  1.4086 +            pPager->journalOff += pPager->pageSize+4;
  1.4087 +          }
  1.4088 +          if( rc==SQLITE_OK ){
  1.4089 +            rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
  1.4090 +            pPager->journalOff += 4;
  1.4091 +          }
  1.4092 +          IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
  1.4093 +                   pPager->journalOff, pPager->pageSize));
  1.4094 +          PAGER_INCR(sqlite3_pager_writej_count);
  1.4095 +          PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
  1.4096 +               PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
  1.4097 +
  1.4098 +          /* An error has occured writing to the journal file. The 
  1.4099 +          ** transaction will be rolled back by the layer above.
  1.4100 +          */
  1.4101 +          if( rc!=SQLITE_OK ){
  1.4102 +            return rc;
  1.4103 +          }
  1.4104 +
  1.4105 +          pPager->nRec++;
  1.4106 +          assert( pPager->aInJournal!=0 );
  1.4107 +          pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  1.4108 +          pPg->needSync = !pPager->noSync;
  1.4109 +          if( pPager->stmtInUse ){
  1.4110 +            pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  1.4111 +          }
  1.4112 +        }
  1.4113 +      }else{
  1.4114 +        pPg->needSync = !pPager->journalStarted && !pPager->noSync;
  1.4115 +        PAGERTRACE4("APPEND %d page %d needSync=%d\n",
  1.4116 +                PAGERID(pPager), pPg->pgno, pPg->needSync);
  1.4117 +      }
  1.4118 +      if( pPg->needSync ){
  1.4119 +        pPager->needSync = 1;
  1.4120 +      }
  1.4121 +      pPg->inJournal = 1;
  1.4122 +    }
  1.4123 +  
  1.4124 +    /* If the statement journal is open and the page is not in it,
  1.4125 +    ** then write the current page to the statement journal.  Note that
  1.4126 +    ** the statement journal format differs from the standard journal format
  1.4127 +    ** in that it omits the checksums and the header.
  1.4128 +    */
  1.4129 +    if( pPager->stmtInUse 
  1.4130 +     && !pageInStatement(pPg) 
  1.4131 +     && (int)pPg->pgno<=pPager->stmtSize 
  1.4132 +    ){
  1.4133 +      assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
  1.4134 +      if( MEMDB ){
  1.4135 +        PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  1.4136 +        assert( pHist->pStmt==0 );
  1.4137 +        pHist->pStmt = sqlite3_malloc( pPager->pageSize );
  1.4138 +        if( pHist->pStmt ){
  1.4139 +          memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
  1.4140 +        }
  1.4141 +        PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
  1.4142 +        page_add_to_stmt_list(pPg);
  1.4143 +      }else{
  1.4144 +        i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
  1.4145 +        char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
  1.4146 +        rc = write32bits(pPager->stfd, offset, pPg->pgno);
  1.4147 +        if( rc==SQLITE_OK ){
  1.4148 +          rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
  1.4149 +        }
  1.4150 +        PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
  1.4151 +        if( rc!=SQLITE_OK ){
  1.4152 +          return rc;
  1.4153 +        }
  1.4154 +        pPager->stmtNRec++;
  1.4155 +        assert( pPager->aInStmt!=0 );
  1.4156 +        pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  1.4157 +      }
  1.4158 +    }
  1.4159 +  }
  1.4160 +
  1.4161 +  /* Update the database size and return.
  1.4162 +  */
  1.4163 +  assert( pPager->state>=PAGER_SHARED );
  1.4164 +  if( pPager->dbSize<(int)pPg->pgno ){
  1.4165 +    pPager->dbSize = pPg->pgno;
  1.4166 +    if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
  1.4167 +      pPager->dbSize++;
  1.4168 +    }
  1.4169 +  }
  1.4170 +  return rc;
  1.4171 +}
  1.4172 +
  1.4173 +/*
  1.4174 +** This function is used to mark a data-page as writable. It uses 
  1.4175 +** pager_write() to open a journal file (if it is not already open)
  1.4176 +** and write the page *pData to the journal.
  1.4177 +**
  1.4178 +** The difference between this function and pager_write() is that this
  1.4179 +** function also deals with the special case where 2 or more pages
  1.4180 +** fit on a single disk sector. In this case all co-resident pages
  1.4181 +** must have been written to the journal file before returning.
  1.4182 +*/
  1.4183 +int sqlite3PagerWrite(DbPage *pDbPage){
  1.4184 +  int rc = SQLITE_OK;
  1.4185 +
  1.4186 +  PgHdr *pPg = pDbPage;
  1.4187 +  Pager *pPager = pPg->pPager;
  1.4188 +  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
  1.4189 +
  1.4190 +  pagerEnter(pPager);
  1.4191 +  if( !MEMDB && nPagePerSector>1 ){
  1.4192 +    Pgno nPageCount;          /* Total number of pages in database file */
  1.4193 +    Pgno pg1;                 /* First page of the sector pPg is located on. */
  1.4194 +    int nPage;                /* Number of pages starting at pg1 to journal */
  1.4195 +    int ii;
  1.4196 +    int needSync = 0;
  1.4197 +
  1.4198 +    /* Set the doNotSync flag to 1. This is because we cannot allow a journal
  1.4199 +    ** header to be written between the pages journaled by this function.
  1.4200 +    */
  1.4201 +    assert( pPager->doNotSync==0 );
  1.4202 +    pPager->doNotSync = 1;
  1.4203 +
  1.4204 +    /* This trick assumes that both the page-size and sector-size are
  1.4205 +    ** an integer power of 2. It sets variable pg1 to the identifier
  1.4206 +    ** of the first page of the sector pPg is located on.
  1.4207 +    */
  1.4208 +    pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
  1.4209 +
  1.4210 +    nPageCount = sqlite3PagerPagecount(pPager);
  1.4211 +    if( pPg->pgno>nPageCount ){
  1.4212 +      nPage = (pPg->pgno - pg1)+1;
  1.4213 +    }else if( (pg1+nPagePerSector-1)>nPageCount ){
  1.4214 +      nPage = nPageCount+1-pg1;
  1.4215 +    }else{
  1.4216 +      nPage = nPagePerSector;
  1.4217 +    }
  1.4218 +    assert(nPage>0);
  1.4219 +    assert(pg1<=pPg->pgno);
  1.4220 +    assert((pg1+nPage)>pPg->pgno);
  1.4221 +
  1.4222 +    for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
  1.4223 +      Pgno pg = pg1+ii;
  1.4224 +      PgHdr *pPage;
  1.4225 +      if( !pPager->aInJournal || pg==pPg->pgno || 
  1.4226 +          pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
  1.4227 +      ) {
  1.4228 +        if( pg!=PAGER_MJ_PGNO(pPager) ){
  1.4229 +          rc = sqlite3PagerGet(pPager, pg, &pPage);
  1.4230 +          if( rc==SQLITE_OK ){
  1.4231 +            rc = pager_write(pPage);
  1.4232 +            if( pPage->needSync ){
  1.4233 +              needSync = 1;
  1.4234 +            }
  1.4235 +            sqlite3PagerUnref(pPage);
  1.4236 +          }
  1.4237 +        }
  1.4238 +      }else if( (pPage = pager_lookup(pPager, pg)) ){
  1.4239 +        if( pPage->needSync ){
  1.4240 +          needSync = 1;
  1.4241 +        }
  1.4242 +      }
  1.4243 +    }
  1.4244 +
  1.4245 +    /* If the PgHdr.needSync flag is set for any of the nPage pages 
  1.4246 +    ** starting at pg1, then it needs to be set for all of them. Because
  1.4247 +    ** writing to any of these nPage pages may damage the others, the
  1.4248 +    ** journal file must contain sync()ed copies of all of them
  1.4249 +    ** before any of them can be written out to the database file.
  1.4250 +    */
  1.4251 +    if( needSync ){
  1.4252 +      for(ii=0; ii<nPage && needSync; ii++){
  1.4253 +        PgHdr *pPage = pager_lookup(pPager, pg1+ii);
  1.4254 +        if( pPage ) pPage->needSync = 1;
  1.4255 +      }
  1.4256 +      assert(pPager->needSync);
  1.4257 +    }
  1.4258 +
  1.4259 +    assert( pPager->doNotSync==1 );
  1.4260 +    pPager->doNotSync = 0;
  1.4261 +  }else{
  1.4262 +    rc = pager_write(pDbPage);
  1.4263 +  }
  1.4264 +  pagerLeave(pPager);
  1.4265 +  return rc;
  1.4266 +}
  1.4267 +
  1.4268 +/*
  1.4269 +** Return TRUE if the page given in the argument was previously passed
  1.4270 +** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
  1.4271 +** to change the content of the page.
  1.4272 +*/
  1.4273 +#ifndef NDEBUG
  1.4274 +int sqlite3PagerIswriteable(DbPage *pPg){
  1.4275 +  return pPg->dirty;
  1.4276 +}
  1.4277 +#endif
  1.4278 +
  1.4279 +#ifndef SQLITE_OMIT_VACUUM
  1.4280 +/*
  1.4281 +** Replace the content of a single page with the information in the third
  1.4282 +** argument.
  1.4283 +*/
  1.4284 +int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
  1.4285 +  PgHdr *pPg;
  1.4286 +  int rc;
  1.4287 +
  1.4288 +  pagerEnter(pPager);
  1.4289 +  rc = sqlite3PagerGet(pPager, pgno, &pPg);
  1.4290 +  if( rc==SQLITE_OK ){
  1.4291 +    rc = sqlite3PagerWrite(pPg);
  1.4292 +    if( rc==SQLITE_OK ){
  1.4293 +      memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
  1.4294 +    }
  1.4295 +    sqlite3PagerUnref(pPg);
  1.4296 +  }
  1.4297 +  pagerLeave(pPager);
  1.4298 +  return rc;
  1.4299 +}
  1.4300 +#endif
  1.4301 +
  1.4302 +/*
  1.4303 +** A call to this routine tells the pager that it is not necessary to
  1.4304 +** write the information on page pPg back to the disk, even though
  1.4305 +** that page might be marked as dirty.
  1.4306 +**
  1.4307 +** The overlying software layer calls this routine when all of the data
  1.4308 +** on the given page is unused.  The pager marks the page as clean so
  1.4309 +** that it does not get written to disk.
  1.4310 +**
  1.4311 +** Tests show that this optimization, together with the
  1.4312 +** sqlite3PagerDontRollback() below, more than double the speed
  1.4313 +** of large INSERT operations and quadruple the speed of large DELETEs.
  1.4314 +**
  1.4315 +** When this routine is called, set the alwaysRollback flag to true.
  1.4316 +** Subsequent calls to sqlite3PagerDontRollback() for the same page
  1.4317 +** will thereafter be ignored.  This is necessary to avoid a problem
  1.4318 +** where a page with data is added to the freelist during one part of
  1.4319 +** a transaction then removed from the freelist during a later part
  1.4320 +** of the same transaction and reused for some other purpose.  When it
  1.4321 +** is first added to the freelist, this routine is called.  When reused,
  1.4322 +** the sqlite3PagerDontRollback() routine is called.  But because the
  1.4323 +** page contains critical data, we still need to be sure it gets
  1.4324 +** rolled back in spite of the sqlite3PagerDontRollback() call.
  1.4325 +*/
  1.4326 +void sqlite3PagerDontWrite(DbPage *pDbPage){
  1.4327 +  PgHdr *pPg = pDbPage;
  1.4328 +  Pager *pPager = pPg->pPager;
  1.4329 +
  1.4330 +  if( MEMDB ) return;
  1.4331 +  pagerEnter(pPager);
  1.4332 +  pPg->alwaysRollback = 1;
  1.4333 +  if( pPg->dirty && !pPager->stmtInUse ){
  1.4334 +    assert( pPager->state>=PAGER_SHARED );
  1.4335 +    if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
  1.4336 +      /* If this pages is the last page in the file and the file has grown
  1.4337 +      ** during the current transaction, then do NOT mark the page as clean.
  1.4338 +      ** When the database file grows, we must make sure that the last page
  1.4339 +      ** gets written at least once so that the disk file will be the correct
  1.4340 +      ** size. If you do not write this page and the size of the file
  1.4341 +      ** on the disk ends up being too small, that can lead to database
  1.4342 +      ** corruption during the next transaction.
  1.4343 +      */
  1.4344 +    }else{
  1.4345 +      PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
  1.4346 +      IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
  1.4347 +      makeClean(pPg);
  1.4348 +#ifdef SQLITE_CHECK_PAGES
  1.4349 +      pPg->pageHash = pager_pagehash(pPg);
  1.4350 +#endif
  1.4351 +    }
  1.4352 +  }
  1.4353 +  pagerLeave(pPager);
  1.4354 +}
  1.4355 +
  1.4356 +/*
  1.4357 +** A call to this routine tells the pager that if a rollback occurs,
  1.4358 +** it is not necessary to restore the data on the given page.  This
  1.4359 +** means that the pager does not have to record the given page in the
  1.4360 +** rollback journal.
  1.4361 +**
  1.4362 +** If we have not yet actually read the content of this page (if
  1.4363 +** the PgHdr.needRead flag is set) then this routine acts as a promise
  1.4364 +** that we will never need to read the page content in the future.
  1.4365 +** so the needRead flag can be cleared at this point.
  1.4366 +*/
  1.4367 +void sqlite3PagerDontRollback(DbPage *pPg){
  1.4368 +  Pager *pPager = pPg->pPager;
  1.4369 +
  1.4370 +  pagerEnter(pPager);
  1.4371 +  assert( pPager->state>=PAGER_RESERVED );
  1.4372 +  if( pPager->journalOpen==0 ) return;
  1.4373 +  if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
  1.4374 +  if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
  1.4375 +    assert( pPager->aInJournal!=0 );
  1.4376 +    pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  1.4377 +    pPg->inJournal = 1;
  1.4378 +    pPg->needRead = 0;
  1.4379 +    if( pPager->stmtInUse ){
  1.4380 +      pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  1.4381 +    }
  1.4382 +    PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
  1.4383 +    IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
  1.4384 +  }
  1.4385 +  if( pPager->stmtInUse 
  1.4386 +   && !pageInStatement(pPg) 
  1.4387 +   && (int)pPg->pgno<=pPager->stmtSize 
  1.4388 +  ){
  1.4389 +    assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
  1.4390 +    assert( pPager->aInStmt!=0 );
  1.4391 +    pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  1.4392 +  }
  1.4393 +  pagerLeave(pPager);
  1.4394 +}
  1.4395 +
  1.4396 +
  1.4397 +/*
  1.4398 +** This routine is called to increment the database file change-counter,
  1.4399 +** stored at byte 24 of the pager file.
  1.4400 +*/
  1.4401 +static int pager_incr_changecounter(Pager *pPager, int isDirect){
  1.4402 +  PgHdr *pPgHdr;
  1.4403 +  u32 change_counter;
  1.4404 +  int rc = SQLITE_OK;
  1.4405 +
  1.4406 +  if( !pPager->changeCountDone ){
  1.4407 +    /* Open page 1 of the file for writing. */
  1.4408 +    rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
  1.4409 +    if( rc!=SQLITE_OK ) return rc;
  1.4410 +
  1.4411 +    if( !isDirect ){
  1.4412 +      rc = sqlite3PagerWrite(pPgHdr);
  1.4413 +      if( rc!=SQLITE_OK ){
  1.4414 +        sqlite3PagerUnref(pPgHdr);
  1.4415 +        return rc;
  1.4416 +      }
  1.4417 +    }
  1.4418 +
  1.4419 +    /* Increment the value just read and write it back to byte 24. */
  1.4420 +    change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
  1.4421 +    change_counter++;
  1.4422 +    put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
  1.4423 +
  1.4424 +    if( isDirect && pPager->fd->pMethods ){
  1.4425 +      const void *zBuf = PGHDR_TO_DATA(pPgHdr);
  1.4426 +      rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
  1.4427 +    }
  1.4428 +
  1.4429 +    /* Release the page reference. */
  1.4430 +    sqlite3PagerUnref(pPgHdr);
  1.4431 +    pPager->changeCountDone = 1;
  1.4432 +  }
  1.4433 +  return rc;
  1.4434 +}
  1.4435 +
  1.4436 +/*
  1.4437 +** Sync the database file for the pager pPager. zMaster points to the name
  1.4438 +** of a master journal file that should be written into the individual
  1.4439 +** journal file. zMaster may be NULL, which is interpreted as no master
  1.4440 +** journal (a single database transaction).
  1.4441 +**
  1.4442 +** This routine ensures that the journal is synced, all dirty pages written
  1.4443 +** to the database file and the database file synced. The only thing that
  1.4444 +** remains to commit the transaction is to delete the journal file (or
  1.4445 +** master journal file if specified).
  1.4446 +**
  1.4447 +** Note that if zMaster==NULL, this does not overwrite a previous value
  1.4448 +** passed to an sqlite3PagerCommitPhaseOne() call.
  1.4449 +**
  1.4450 +** If parameter nTrunc is non-zero, then the pager file is truncated to
  1.4451 +** nTrunc pages (this is used by auto-vacuum databases).
  1.4452 +*/
  1.4453 +int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
  1.4454 +  int rc = SQLITE_OK;
  1.4455 +
  1.4456 +  PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n", 
  1.4457 +      pPager->zFilename, zMaster, nTrunc);
  1.4458 +  pagerEnter(pPager);
  1.4459 +
  1.4460 +  /* If this is an in-memory db, or no pages have been written to, or this
  1.4461 +  ** function has already been called, it is a no-op.
  1.4462 +  */
  1.4463 +  if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
  1.4464 +    PgHdr *pPg;
  1.4465 +
  1.4466 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
  1.4467 +    /* The atomic-write optimization can be used if all of the
  1.4468 +    ** following are true:
  1.4469 +    **
  1.4470 +    **    + The file-system supports the atomic-write property for
  1.4471 +    **      blocks of size page-size, and
  1.4472 +    **    + This commit is not part of a multi-file transaction, and
  1.4473 +    **    + Exactly one page has been modified and store in the journal file.
  1.4474 +    **
  1.4475 +    ** If the optimization can be used, then the journal file will never
  1.4476 +    ** be created for this transaction.
  1.4477 +    */
  1.4478 +    int useAtomicWrite = (
  1.4479 +        !zMaster && 
  1.4480 +        pPager->journalOff==jrnlBufferSize(pPager) && 
  1.4481 +        nTrunc==0 && 
  1.4482 +        (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
  1.4483 +    );
  1.4484 +    if( useAtomicWrite ){
  1.4485 +      /* Update the nRec field in the journal file. */
  1.4486 +      int offset = pPager->journalHdr + sizeof(aJournalMagic);
  1.4487 +      assert(pPager->nRec==1);
  1.4488 +      rc = write32bits(pPager->jfd, offset, pPager->nRec);
  1.4489 +
  1.4490 +      /* Update the db file change counter. The following call will modify
  1.4491 +      ** the in-memory representation of page 1 to include the updated
  1.4492 +      ** change counter and then write page 1 directly to the database
  1.4493 +      ** file. Because of the atomic-write property of the host file-system, 
  1.4494 +      ** this is safe.
  1.4495 +      */
  1.4496 +      if( rc==SQLITE_OK ){
  1.4497 +        rc = pager_incr_changecounter(pPager, 1);
  1.4498 +      }
  1.4499 +    }else{
  1.4500 +      rc = sqlite3JournalCreate(pPager->jfd);
  1.4501 +    }
  1.4502 +
  1.4503 +    if( !useAtomicWrite && rc==SQLITE_OK )
  1.4504 +#endif
  1.4505 +
  1.4506 +    /* If a master journal file name has already been written to the
  1.4507 +    ** journal file, then no sync is required. This happens when it is
  1.4508 +    ** written, then the process fails to upgrade from a RESERVED to an
  1.4509 +    ** EXCLUSIVE lock. The next time the process tries to commit the
  1.4510 +    ** transaction the m-j name will have already been written.
  1.4511 +    */
  1.4512 +    if( !pPager->setMaster ){
  1.4513 +      assert( pPager->journalOpen );
  1.4514 +      rc = pager_incr_changecounter(pPager, 0);
  1.4515 +      if( rc!=SQLITE_OK ) goto sync_exit;
  1.4516 +#ifndef SQLITE_OMIT_AUTOVACUUM
  1.4517 +      if( nTrunc!=0 ){
  1.4518 +        /* If this transaction has made the database smaller, then all pages
  1.4519 +        ** being discarded by the truncation must be written to the journal
  1.4520 +        ** file.
  1.4521 +        */
  1.4522 +        Pgno i;
  1.4523 +        int iSkip = PAGER_MJ_PGNO(pPager);
  1.4524 +        for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
  1.4525 +          if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){
  1.4526 +            rc = sqlite3PagerGet(pPager, i, &pPg);
  1.4527 +            if( rc!=SQLITE_OK ) goto sync_exit;
  1.4528 +            rc = sqlite3PagerWrite(pPg);
  1.4529 +            sqlite3PagerUnref(pPg);
  1.4530 +            if( rc!=SQLITE_OK ) goto sync_exit;
  1.4531 +          }
  1.4532 +        } 
  1.4533 +      }
  1.4534 +#endif
  1.4535 +      rc = writeMasterJournal(pPager, zMaster);
  1.4536 +      if( rc!=SQLITE_OK ) goto sync_exit;
  1.4537 +      rc = syncJournal(pPager);
  1.4538 +    }
  1.4539 +    if( rc!=SQLITE_OK ) goto sync_exit;
  1.4540 +
  1.4541 +#ifndef SQLITE_OMIT_AUTOVACUUM
  1.4542 +    if( nTrunc!=0 ){
  1.4543 +      rc = sqlite3PagerTruncate(pPager, nTrunc);
  1.4544 +      if( rc!=SQLITE_OK ) goto sync_exit;
  1.4545 +    }
  1.4546 +#endif
  1.4547 +
  1.4548 +    /* Write all dirty pages to the database file */
  1.4549 +    pPg = pager_get_all_dirty_pages(pPager);
  1.4550 +    rc = pager_write_pagelist(pPg);
  1.4551 +    if( rc!=SQLITE_OK ){
  1.4552 +      while( pPg && !pPg->dirty ){ pPg = pPg->pDirty; }
  1.4553 +      pPager->pDirty = pPg;
  1.4554 +      goto sync_exit;
  1.4555 +    }
  1.4556 +    pPager->pDirty = 0;
  1.4557 +
  1.4558 +    /* Sync the database file. */
  1.4559 +    if( !pPager->noSync ){
  1.4560 +      rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
  1.4561 +    }
  1.4562 +    IOTRACE(("DBSYNC %p\n", pPager))
  1.4563 +
  1.4564 +    pPager->state = PAGER_SYNCED;
  1.4565 +  }else if( MEMDB && nTrunc!=0 ){
  1.4566 +    rc = sqlite3PagerTruncate(pPager, nTrunc);
  1.4567 +  }
  1.4568 +
  1.4569 +sync_exit:
  1.4570 +  if( rc==SQLITE_IOERR_BLOCKED ){
  1.4571 +    /* pager_incr_changecounter() may attempt to obtain an exclusive
  1.4572 +     * lock to spill the cache and return IOERR_BLOCKED. But since 
  1.4573 +     * there is no chance the cache is inconsistent, it's
  1.4574 +     * better to return SQLITE_BUSY.
  1.4575 +     */
  1.4576 +    rc = SQLITE_BUSY;
  1.4577 +  }
  1.4578 +  pagerLeave(pPager);
  1.4579 +  return rc;
  1.4580 +}
  1.4581 +
  1.4582 +
  1.4583 +/*
  1.4584 +** Commit all changes to the database and release the write lock.
  1.4585 +**
  1.4586 +** If the commit fails for any reason, a rollback attempt is made
  1.4587 +** and an error code is returned.  If the commit worked, SQLITE_OK
  1.4588 +** is returned.
  1.4589 +*/
  1.4590 +int sqlite3PagerCommitPhaseTwo(Pager *pPager){
  1.4591 +  int rc;
  1.4592 +  PgHdr *pPg;
  1.4593 +
  1.4594 +  if( pPager->errCode ){
  1.4595 +    return pPager->errCode;
  1.4596 +  }
  1.4597 +  if( pPager->state<PAGER_RESERVED ){
  1.4598 +    return SQLITE_ERROR;
  1.4599 +  }
  1.4600 +  pagerEnter(pPager);
  1.4601 +  PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
  1.4602 +  if( MEMDB ){
  1.4603 +    pPg = pager_get_all_dirty_pages(pPager);
  1.4604 +    while( pPg ){
  1.4605 +      PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  1.4606 +      clearHistory(pHist);
  1.4607 +      pPg->dirty = 0;
  1.4608 +      pPg->inJournal = 0;
  1.4609 +      pHist->inStmt = 0;
  1.4610 +      pPg->needSync = 0;
  1.4611 +      pHist->pPrevStmt = pHist->pNextStmt = 0;
  1.4612 +      pPg = pPg->pDirty;
  1.4613 +    }
  1.4614 +    pPager->pDirty = 0;
  1.4615 +#ifndef NDEBUG
  1.4616 +    for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
  1.4617 +      PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  1.4618 +      assert( !pPg->alwaysRollback );
  1.4619 +      assert( !pHist->pOrig );
  1.4620 +      assert( !pHist->pStmt );
  1.4621 +    }
  1.4622 +#endif
  1.4623 +    pPager->pStmt = 0;
  1.4624 +    pPager->state = PAGER_SHARED;
  1.4625 +    return SQLITE_OK;
  1.4626 +  }
  1.4627 +  assert( pPager->journalOpen || !pPager->dirtyCache );
  1.4628 +  assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
  1.4629 +  rc = pager_end_transaction(pPager);
  1.4630 +  rc = pager_error(pPager, rc);
  1.4631 +  pagerLeave(pPager);
  1.4632 +  return rc;
  1.4633 +}
  1.4634 +
  1.4635 +/*
  1.4636 +** Rollback all changes.  The database falls back to PAGER_SHARED mode.
  1.4637 +** All in-memory cache pages revert to their original data contents.
  1.4638 +** The journal is deleted.
  1.4639 +**
  1.4640 +** This routine cannot fail unless some other process is not following
  1.4641 +** the correct locking protocol or unless some other
  1.4642 +** process is writing trash into the journal file (SQLITE_CORRUPT) or
  1.4643 +** unless a prior malloc() failed (SQLITE_NOMEM).  Appropriate error
  1.4644 +** codes are returned for all these occasions.  Otherwise,
  1.4645 +** SQLITE_OK is returned.
  1.4646 +*/
  1.4647 +int sqlite3PagerRollback(Pager *pPager){
  1.4648 +  int rc;
  1.4649 +  PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
  1.4650 +  if( MEMDB ){
  1.4651 +    PgHdr *p;
  1.4652 +    for(p=pPager->pAll; p; p=p->pNextAll){
  1.4653 +      PgHistory *pHist;
  1.4654 +      assert( !p->alwaysRollback );
  1.4655 +      if( !p->dirty ){
  1.4656 +        assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
  1.4657 +        assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
  1.4658 +        continue;
  1.4659 +      }
  1.4660 +
  1.4661 +      pHist = PGHDR_TO_HIST(p, pPager);
  1.4662 +      if( pHist->pOrig ){
  1.4663 +        memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
  1.4664 +        PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
  1.4665 +      }else{
  1.4666 +        PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
  1.4667 +      }
  1.4668 +      clearHistory(pHist);
  1.4669 +      p->dirty = 0;
  1.4670 +      p->inJournal = 0;
  1.4671 +      pHist->inStmt = 0;
  1.4672 +      pHist->pPrevStmt = pHist->pNextStmt = 0;
  1.4673 +      if( pPager->xReiniter ){
  1.4674 +        pPager->xReiniter(p, pPager->pageSize);
  1.4675 +      }
  1.4676 +    }
  1.4677 +    pPager->pDirty = 0;
  1.4678 +    pPager->pStmt = 0;
  1.4679 +    pPager->dbSize = pPager->origDbSize;
  1.4680 +    pager_truncate_cache(pPager);
  1.4681 +    pPager->stmtInUse = 0;
  1.4682 +    pPager->state = PAGER_SHARED;
  1.4683 +    return SQLITE_OK;
  1.4684 +  }
  1.4685 +
  1.4686 +  pagerEnter(pPager);
  1.4687 +  if( !pPager->dirtyCache || !pPager->journalOpen ){
  1.4688 +    rc = pager_end_transaction(pPager);
  1.4689 +    pagerLeave(pPager);
  1.4690 +    return rc;
  1.4691 +  }
  1.4692 +
  1.4693 +  if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
  1.4694 +    if( pPager->state>=PAGER_EXCLUSIVE ){
  1.4695 +      pager_playback(pPager, 0);
  1.4696 +    }
  1.4697 +    pagerLeave(pPager);
  1.4698 +    return pPager->errCode;
  1.4699 +  }
  1.4700 +  if( pPager->state==PAGER_RESERVED ){
  1.4701 +    int rc2;
  1.4702 +    rc = pager_playback(pPager, 0);
  1.4703 +    rc2 = pager_end_transaction(pPager);
  1.4704 +    if( rc==SQLITE_OK ){
  1.4705 +      rc = rc2;
  1.4706 +    }
  1.4707 +  }else{
  1.4708 +    rc = pager_playback(pPager, 0);
  1.4709 +  }
  1.4710 +  /* pager_reset(pPager); */
  1.4711 +  pPager->dbSize = -1;
  1.4712 +
  1.4713 +  /* If an error occurs during a ROLLBACK, we can no longer trust the pager
  1.4714 +  ** cache. So call pager_error() on the way out to make any error 
  1.4715 +  ** persistent.
  1.4716 +  */
  1.4717 +  rc = pager_error(pPager, rc);
  1.4718 +  pagerLeave(pPager);
  1.4719 +  return rc;
  1.4720 +}
  1.4721 +
  1.4722 +/*
  1.4723 +** Return TRUE if the database file is opened read-only.  Return FALSE
  1.4724 +** if the database is (in theory) writable.
  1.4725 +*/
  1.4726 +int sqlite3PagerIsreadonly(Pager *pPager){
  1.4727 +  return pPager->readOnly;
  1.4728 +}
  1.4729 +
  1.4730 +/*
  1.4731 +** Return the number of references to the pager.
  1.4732 +*/
  1.4733 +int sqlite3PagerRefcount(Pager *pPager){
  1.4734 +  return pPager->nRef;
  1.4735 +}
  1.4736 +
  1.4737 +#ifdef SQLITE_TEST
  1.4738 +/*
  1.4739 +** This routine is used for testing and analysis only.
  1.4740 +*/
  1.4741 +int *sqlite3PagerStats(Pager *pPager){
  1.4742 +  static int a[11];
  1.4743 +  a[0] = pPager->nRef;
  1.4744 +  a[1] = pPager->nPage;
  1.4745 +  a[2] = pPager->mxPage;
  1.4746 +  a[3] = pPager->dbSize;
  1.4747 +  a[4] = pPager->state;
  1.4748 +  a[5] = pPager->errCode;
  1.4749 +  a[6] = pPager->nHit;
  1.4750 +  a[7] = pPager->nMiss;
  1.4751 +  a[8] = 0;  /* Used to be pPager->nOvfl */
  1.4752 +  a[9] = pPager->nRead;
  1.4753 +  a[10] = pPager->nWrite;
  1.4754 +  return a;
  1.4755 +}
  1.4756 +#endif
  1.4757 +
  1.4758 +/*
  1.4759 +** Set the statement rollback point.
  1.4760 +**
  1.4761 +** This routine should be called with the transaction journal already
  1.4762 +** open.  A new statement journal is created that can be used to rollback
  1.4763 +** changes of a single SQL command within a larger transaction.
  1.4764 +*/
  1.4765 +static int pagerStmtBegin(Pager *pPager){
  1.4766 +  int rc;
  1.4767 +  assert( !pPager->stmtInUse );
  1.4768 +  assert( pPager->state>=PAGER_SHARED );
  1.4769 +  assert( pPager->dbSize>=0 );
  1.4770 +  PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
  1.4771 +  if( MEMDB ){
  1.4772 +    pPager->stmtInUse = 1;
  1.4773 +    pPager->stmtSize = pPager->dbSize;
  1.4774 +    return SQLITE_OK;
  1.4775 +  }
  1.4776 +  if( !pPager->journalOpen ){
  1.4777 +    pPager->stmtAutoopen = 1;
  1.4778 +    return SQLITE_OK;
  1.4779 +  }
  1.4780 +  assert( pPager->journalOpen );
  1.4781 +  pagerLeave(pPager);
  1.4782 +  assert( pPager->aInStmt==0 );
  1.4783 +  pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 );
  1.4784 +  pagerEnter(pPager);
  1.4785 +  if( pPager->aInStmt==0 ){
  1.4786 +    /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
  1.4787 +    return SQLITE_NOMEM;
  1.4788 +  }
  1.4789 +#ifndef NDEBUG
  1.4790 +  rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
  1.4791 +  if( rc ) goto stmt_begin_failed;
  1.4792 +  assert( pPager->stmtJSize == pPager->journalOff );
  1.4793 +#endif
  1.4794 +  pPager->stmtJSize = pPager->journalOff;
  1.4795 +  pPager->stmtSize = pPager->dbSize;
  1.4796 +  pPager->stmtHdrOff = 0;
  1.4797 +  pPager->stmtCksum = pPager->cksumInit;
  1.4798 +  if( !pPager->stmtOpen ){
  1.4799 +    rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
  1.4800 +                              SQLITE_OPEN_SUBJOURNAL);
  1.4801 +    if( rc ){
  1.4802 +      goto stmt_begin_failed;
  1.4803 +    }
  1.4804 +    pPager->stmtOpen = 1;
  1.4805 +    pPager->stmtNRec = 0;
  1.4806 +  }
  1.4807 +  pPager->stmtInUse = 1;
  1.4808 +  return SQLITE_OK;
  1.4809 + 
  1.4810 +stmt_begin_failed:
  1.4811 +  if( pPager->aInStmt ){
  1.4812 +    sqlite3_free(pPager->aInStmt);
  1.4813 +    pPager->aInStmt = 0;
  1.4814 +  }
  1.4815 +  return rc;
  1.4816 +}
  1.4817 +int sqlite3PagerStmtBegin(Pager *pPager){
  1.4818 +  int rc;
  1.4819 +  pagerEnter(pPager);
  1.4820 +  rc = pagerStmtBegin(pPager);
  1.4821 +  pagerLeave(pPager);
  1.4822 +  return rc;
  1.4823 +}
  1.4824 +
  1.4825 +/*
  1.4826 +** Commit a statement.
  1.4827 +*/
  1.4828 +int sqlite3PagerStmtCommit(Pager *pPager){
  1.4829 +  pagerEnter(pPager);
  1.4830 +  if( pPager->stmtInUse ){
  1.4831 +    PgHdr *pPg, *pNext;
  1.4832 +    PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
  1.4833 +    if( !MEMDB ){
  1.4834 +      /* sqlite3OsTruncate(pPager->stfd, 0); */
  1.4835 +      sqlite3_free( pPager->aInStmt );
  1.4836 +      pPager->aInStmt = 0;
  1.4837 +    }else{
  1.4838 +      for(pPg=pPager->pStmt; pPg; pPg=pNext){
  1.4839 +        PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  1.4840 +        pNext = pHist->pNextStmt;
  1.4841 +        assert( pHist->inStmt );
  1.4842 +        pHist->inStmt = 0;
  1.4843 +        pHist->pPrevStmt = pHist->pNextStmt = 0;
  1.4844 +        sqlite3_free(pHist->pStmt);
  1.4845 +        pHist->pStmt = 0;
  1.4846 +      }
  1.4847 +    }
  1.4848 +    pPager->stmtNRec = 0;
  1.4849 +    pPager->stmtInUse = 0;
  1.4850 +    pPager->pStmt = 0;
  1.4851 +  }
  1.4852 +  pPager->stmtAutoopen = 0;
  1.4853 +  pagerLeave(pPager);
  1.4854 +  return SQLITE_OK;
  1.4855 +}
  1.4856 +
  1.4857 +/*
  1.4858 +** Rollback a statement.
  1.4859 +*/
  1.4860 +int sqlite3PagerStmtRollback(Pager *pPager){
  1.4861 +  int rc;
  1.4862 +  pagerEnter(pPager);
  1.4863 +  if( pPager->stmtInUse ){
  1.4864 +    PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
  1.4865 +    if( MEMDB ){
  1.4866 +      PgHdr *pPg;
  1.4867 +      PgHistory *pHist;
  1.4868 +      for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
  1.4869 +        pHist = PGHDR_TO_HIST(pPg, pPager);
  1.4870 +        if( pHist->pStmt ){
  1.4871 +          memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
  1.4872 +          sqlite3_free(pHist->pStmt);
  1.4873 +          pHist->pStmt = 0;
  1.4874 +        }
  1.4875 +      }
  1.4876 +      pPager->dbSize = pPager->stmtSize;
  1.4877 +      pager_truncate_cache(pPager);
  1.4878 +      rc = SQLITE_OK;
  1.4879 +    }else{
  1.4880 +      rc = pager_stmt_playback(pPager);
  1.4881 +    }
  1.4882 +    sqlite3PagerStmtCommit(pPager);
  1.4883 +  }else{
  1.4884 +    rc = SQLITE_OK;
  1.4885 +  }
  1.4886 +  pPager->stmtAutoopen = 0;
  1.4887 +  pagerLeave(pPager);
  1.4888 +  return rc;
  1.4889 +}
  1.4890 +
  1.4891 +/*
  1.4892 +** Return the full pathname of the database file.
  1.4893 +*/
  1.4894 +const char *sqlite3PagerFilename(Pager *pPager){
  1.4895 +  return pPager->zFilename;
  1.4896 +}
  1.4897 +
  1.4898 +/*
  1.4899 +** Return the VFS structure for the pager.
  1.4900 +*/
  1.4901 +const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
  1.4902 +  return pPager->pVfs;
  1.4903 +}
  1.4904 +
  1.4905 +/*
  1.4906 +** Return the file handle for the database file associated
  1.4907 +** with the pager.  This might return NULL if the file has
  1.4908 +** not yet been opened.
  1.4909 +*/
  1.4910 +sqlite3_file *sqlite3PagerFile(Pager *pPager){
  1.4911 +  return pPager->fd;
  1.4912 +}
  1.4913 +
  1.4914 +/*
  1.4915 +** Return the directory of the database file.
  1.4916 +*/
  1.4917 +const char *sqlite3PagerDirname(Pager *pPager){
  1.4918 +  return pPager->zDirectory;
  1.4919 +}
  1.4920 +
  1.4921 +/*
  1.4922 +** Return the full pathname of the journal file.
  1.4923 +*/
  1.4924 +const char *sqlite3PagerJournalname(Pager *pPager){
  1.4925 +  return pPager->zJournal;
  1.4926 +}
  1.4927 +
  1.4928 +/*
  1.4929 +** Return true if fsync() calls are disabled for this pager.  Return FALSE
  1.4930 +** if fsync()s are executed normally.
  1.4931 +*/
  1.4932 +int sqlite3PagerNosync(Pager *pPager){
  1.4933 +  return pPager->noSync;
  1.4934 +}
  1.4935 +
  1.4936 +#ifdef SQLITE_HAS_CODEC
  1.4937 +/*
  1.4938 +** Set the codec for this pager
  1.4939 +*/
  1.4940 +void sqlite3PagerSetCodec(
  1.4941 +  Pager *pPager,
  1.4942 +  void *(*xCodec)(void*,void*,Pgno,int),
  1.4943 +  void *pCodecArg
  1.4944 +){
  1.4945 +  pPager->xCodec = xCodec;
  1.4946 +  pPager->pCodecArg = pCodecArg;
  1.4947 +}
  1.4948 +#endif
  1.4949 +
  1.4950 +#ifndef SQLITE_OMIT_AUTOVACUUM
  1.4951 +/*
  1.4952 +** Move the page pPg to location pgno in the file. 
  1.4953 +**
  1.4954 +** There must be no references to the page previously located at
  1.4955 +** pgno (which we call pPgOld) though that page is allowed to be
  1.4956 +** in cache.  If the page previous located at pgno is not already
  1.4957 +** in the rollback journal, it is not put there by by this routine.
  1.4958 +**
  1.4959 +** References to the page pPg remain valid. Updating any
  1.4960 +** meta-data associated with pPg (i.e. data stored in the nExtra bytes
  1.4961 +** allocated along with the page) is the responsibility of the caller.
  1.4962 +**
  1.4963 +** A transaction must be active when this routine is called. It used to be
  1.4964 +** required that a statement transaction was not active, but this restriction
  1.4965 +** has been removed (CREATE INDEX needs to move a page when a statement
  1.4966 +** transaction is active).
  1.4967 +*/
  1.4968 +int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
  1.4969 +  PgHdr *pPgOld;  /* The page being overwritten. */
  1.4970 +  int h;
  1.4971 +  Pgno needSyncPgno = 0;
  1.4972 +
  1.4973 +  pagerEnter(pPager);
  1.4974 +  assert( pPg->nRef>0 );
  1.4975 +
  1.4976 +  PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n", 
  1.4977 +      PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
  1.4978 +  IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
  1.4979 +
  1.4980 +  pager_get_content(pPg);
  1.4981 +  if( pPg->needSync ){
  1.4982 +    needSyncPgno = pPg->pgno;
  1.4983 +    assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
  1.4984 +    assert( pPg->dirty );
  1.4985 +    assert( pPager->needSync );
  1.4986 +  }
  1.4987 +
  1.4988 +  /* Unlink pPg from it's hash-chain */
  1.4989 +  unlinkHashChain(pPager, pPg);
  1.4990 +
  1.4991 +  /* If the cache contains a page with page-number pgno, remove it
  1.4992 +  ** from it's hash chain. Also, if the PgHdr.needSync was set for 
  1.4993 +  ** page pgno before the 'move' operation, it needs to be retained 
  1.4994 +  ** for the page moved there.
  1.4995 +  */
  1.4996 +  pPg->needSync = 0;
  1.4997 +  pPgOld = pager_lookup(pPager, pgno);
  1.4998 +  if( pPgOld ){
  1.4999 +    assert( pPgOld->nRef==0 );
  1.5000 +    unlinkHashChain(pPager, pPgOld);
  1.5001 +    makeClean(pPgOld);
  1.5002 +    pPg->needSync = pPgOld->needSync;
  1.5003 +  }else{
  1.5004 +    pPg->needSync = 0;
  1.5005 +  }
  1.5006 +  if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
  1.5007 +    pPg->inJournal =  (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
  1.5008 +  }else{
  1.5009 +    pPg->inJournal = 0;
  1.5010 +    assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize );
  1.5011 +  }
  1.5012 +
  1.5013 +  /* Change the page number for pPg and insert it into the new hash-chain. */
  1.5014 +  assert( pgno!=0 );
  1.5015 +  pPg->pgno = pgno;
  1.5016 +  h = pgno & (pPager->nHash-1);
  1.5017 +  if( pPager->aHash[h] ){
  1.5018 +    assert( pPager->aHash[h]->pPrevHash==0 );
  1.5019 +    pPager->aHash[h]->pPrevHash = pPg;
  1.5020 +  }
  1.5021 +  pPg->pNextHash = pPager->aHash[h];
  1.5022 +  pPager->aHash[h] = pPg;
  1.5023 +  pPg->pPrevHash = 0;
  1.5024 +
  1.5025 +  makeDirty(pPg);
  1.5026 +  pPager->dirtyCache = 1;
  1.5027 +
  1.5028 +  if( needSyncPgno ){
  1.5029 +    /* If needSyncPgno is non-zero, then the journal file needs to be 
  1.5030 +    ** sync()ed before any data is written to database file page needSyncPgno.
  1.5031 +    ** Currently, no such page exists in the page-cache and the 
  1.5032 +    ** Pager.aInJournal bit has been set. This needs to be remedied by loading
  1.5033 +    ** the page into the pager-cache and setting the PgHdr.needSync flag.
  1.5034 +    **
  1.5035 +    ** The sqlite3PagerGet() call may cause the journal to sync. So make
  1.5036 +    ** sure the Pager.needSync flag is set too.
  1.5037 +    */
  1.5038 +    int rc;
  1.5039 +    PgHdr *pPgHdr;
  1.5040 +    assert( pPager->needSync );
  1.5041 +    rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
  1.5042 +    if( rc!=SQLITE_OK ) return rc;
  1.5043 +    pPager->needSync = 1;
  1.5044 +    pPgHdr->needSync = 1;
  1.5045 +    pPgHdr->inJournal = 1;
  1.5046 +    makeDirty(pPgHdr);
  1.5047 +    sqlite3PagerUnref(pPgHdr);
  1.5048 +  }
  1.5049 +
  1.5050 +  pagerLeave(pPager);
  1.5051 +  return SQLITE_OK;
  1.5052 +}
  1.5053 +#endif
  1.5054 +
  1.5055 +/*
  1.5056 +** Return a pointer to the data for the specified page.
  1.5057 +*/
  1.5058 +void *sqlite3PagerGetData(DbPage *pPg){
  1.5059 +  return PGHDR_TO_DATA(pPg);
  1.5060 +}
  1.5061 +
  1.5062 +/*
  1.5063 +** Return a pointer to the Pager.nExtra bytes of "extra" space 
  1.5064 +** allocated along with the specified page.
  1.5065 +*/
  1.5066 +void *sqlite3PagerGetExtra(DbPage *pPg){
  1.5067 +  Pager *pPager = pPg->pPager;
  1.5068 +  return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
  1.5069 +}
  1.5070 +
  1.5071 +/*
  1.5072 +** Get/set the locking-mode for this pager. Parameter eMode must be one
  1.5073 +** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or 
  1.5074 +** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
  1.5075 +** the locking-mode is set to the value specified.
  1.5076 +**
  1.5077 +** The returned value is either PAGER_LOCKINGMODE_NORMAL or
  1.5078 +** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
  1.5079 +** locking-mode.
  1.5080 +*/
  1.5081 +int sqlite3PagerLockingMode(Pager *pPager, int eMode){
  1.5082 +  assert( eMode==PAGER_LOCKINGMODE_QUERY
  1.5083 +            || eMode==PAGER_LOCKINGMODE_NORMAL
  1.5084 +            || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
  1.5085 +  assert( PAGER_LOCKINGMODE_QUERY<0 );
  1.5086 +  assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
  1.5087 +  if( eMode>=0 && !pPager->tempFile ){
  1.5088 +    pPager->exclusiveMode = eMode;
  1.5089 +  }
  1.5090 +  return (int)pPager->exclusiveMode;
  1.5091 +}
  1.5092 +
  1.5093 +#ifdef SQLITE_DEBUG
  1.5094 +/*
  1.5095 +** Print a listing of all referenced pages and their ref count.
  1.5096 +*/
  1.5097 +void sqlite3PagerRefdump(Pager *pPager){
  1.5098 +  PgHdr *pPg;
  1.5099 +  for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
  1.5100 +    if( pPg->nRef<=0 ) continue;
  1.5101 +    sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n", 
  1.5102 +       pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
  1.5103 +  }
  1.5104 +}
  1.5105 +#endif
  1.5106 +
  1.5107 +#endif /* SQLITE_OMIT_DISKIO */