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