diff options
Diffstat (limited to 'ext/pdo_sqlite/sqlite/src/pager.c')
| -rw-r--r-- | ext/pdo_sqlite/sqlite/src/pager.c | 3572 |
1 files changed, 2386 insertions, 1186 deletions
diff --git a/ext/pdo_sqlite/sqlite/src/pager.c b/ext/pdo_sqlite/sqlite/src/pager.c index 7f4b6f952f..ef7683db89 100644 --- a/ext/pdo_sqlite/sqlite/src/pager.c +++ b/ext/pdo_sqlite/sqlite/src/pager.c @@ -22,8 +22,6 @@ */ #ifndef SQLITE_OMIT_DISKIO #include "sqliteInt.h" -#include "os.h" -#include "pager.h" #include <assert.h> #include <string.h> @@ -31,26 +29,27 @@ ** Macros for troubleshooting. Normally turned off */ #if 0 -#define TRACE1(X) sqlite3DebugPrintf(X) -#define TRACE2(X,Y) sqlite3DebugPrintf(X,Y) -#define TRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z) -#define TRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W) -#define TRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V) +#define sqlite3DebugPrintf printf +#define PAGERTRACE1(X) sqlite3DebugPrintf(X) +#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y) +#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z) +#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W) +#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V) #else -#define TRACE1(X) -#define TRACE2(X,Y) -#define TRACE3(X,Y,Z) -#define TRACE4(X,Y,Z,W) -#define TRACE5(X,Y,Z,W,V) +#define PAGERTRACE1(X) +#define PAGERTRACE2(X,Y) +#define PAGERTRACE3(X,Y,Z) +#define PAGERTRACE4(X,Y,Z,W) +#define PAGERTRACE5(X,Y,Z,W,V) #endif /* -** The following two macros are used within the TRACEX() macros above +** The following two macros are used within the PAGERTRACEX() macros above ** to print out file-descriptors. ** -** PAGERID() takes a pointer to a Pager struct as it's argument. The -** associated file-descriptor is returned. FILEHANDLEID() takes an OsFile -** struct as it's argument. +** PAGERID() takes a pointer to a Pager struct as its argument. The +** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file +** struct as its argument. */ #define PAGERID(p) ((int)(p->fd)) #define FILEHANDLEID(fd) ((int)fd) @@ -84,21 +83,24 @@ ** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE ** after all dirty pages have been written to the ** database file and the file has been synced to -** disk. All that remains to do is to remove the -** journal file and the transaction will be -** committed. +** disk. All that remains to do is to remove or +** truncate the journal file and the transaction +** will be committed. ** ** The page cache comes up in PAGER_UNLOCK. The first time a -** sqlite3pager_get() occurs, the state transitions to PAGER_SHARED. +** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED. ** After all pages have been released using sqlite_page_unref(), ** the state transitions back to PAGER_UNLOCK. The first time -** that sqlite3pager_write() is called, the state transitions to -** PAGER_RESERVED. (Note that sqlite_page_write() can only be +** that sqlite3PagerWrite() is called, the state transitions to +** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be ** called on an outstanding page which means that the pager must ** be in PAGER_SHARED before it transitions to PAGER_RESERVED.) -** The transition to PAGER_EXCLUSIVE occurs when before any changes -** are made to the database file. After an sqlite3pager_rollback() -** or sqlite_pager_commit(), the state goes back to PAGER_SHARED. +** PAGER_RESERVED means that there is an open rollback journal. +** The transition to PAGER_EXCLUSIVE occurs before any changes +** are made to the database file, though writes to the rollback +** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback() +** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED, +** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode. */ #define PAGER_UNLOCK 0 #define PAGER_SHARED 1 /* same as SHARED_LOCK */ @@ -129,13 +131,47 @@ */ #define FORCE_ALIGNMENT(X) (((X)+7)&~7) +typedef struct PgHdr PgHdr; + +/* +** Each pager stores all currently unreferenced pages in a list sorted +** in least-recently-used (LRU) order (i.e. the first item on the list has +** not been referenced in a long time, the last item has been recently +** used). An instance of this structure is included as part of each +** pager structure for this purpose (variable Pager.lru). +** +** Additionally, if memory-management is enabled, all unreferenced pages +** are stored in a global LRU list (global variable sqlite3LruPageList). +** +** In both cases, the PagerLruList.pFirstSynced variable points to +** the first page in the corresponding list that does not require an +** fsync() operation before its memory can be reclaimed. If no such +** page exists, PagerLruList.pFirstSynced is set to NULL. +*/ +typedef struct PagerLruList PagerLruList; +struct PagerLruList { + PgHdr *pFirst; /* First page in LRU list */ + PgHdr *pLast; /* Last page in LRU list (the most recently used) */ + PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0 */ +}; + +/* +** The following structure contains the next and previous pointers used +** to link a PgHdr structure into a PagerLruList linked list. +*/ +typedef struct PagerLruLink PagerLruLink; +struct PagerLruLink { + PgHdr *pNext; + PgHdr *pPrev; +}; + /* ** Each in-memory image of a page begins with the following header. ** This header is only visible to this pager module. The client ** code that calls pager sees only the data that follows the header. ** -** Client code should call sqlite3pager_write() on a page prior to making -** any modifications to that page. The first time sqlite3pager_write() +** Client code should call sqlite3PagerWrite() on a page prior to making +** any modifications to that page. The first time sqlite3PagerWrite() ** is called, the original page contents are written into the rollback ** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once ** the journal page has made it onto the disk surface, PgHdr.needSync @@ -143,31 +179,103 @@ ** database file until the journal pages has been synced to disk and the ** PgHdr.needSync has been cleared. ** -** The PgHdr.dirty flag is set when sqlite3pager_write() is called and +** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and ** is cleared again when the page content is written back to the original ** database file. +** +** Details of important structure elements: +** +** needSync +** +** If this is true, this means that it is not safe to write the page +** content to the database because the original content needed +** for rollback has not by synced to the main rollback journal. +** The original content may have been written to the rollback journal +** but it has not yet been synced. So we cannot write to the database +** file because power failure might cause the page in the journal file +** to never reach the disk. It is as if the write to the journal file +** does not occur until the journal file is synced. +** +** This flag is false if the page content exactly matches what +** currently exists in the database file. The needSync flag is also +** false if the original content has been written to the main rollback +** journal and synced. If the page represents a new page that has +** been added onto the end of the database during the current +** transaction, the needSync flag is true until the original database +** size in the journal header has been synced to disk. +** +** inJournal +** +** This is true if the original page has been written into the main +** rollback journal. This is always false for new pages added to +** the end of the database file during the current transaction. +** And this flag says nothing about whether or not the journal +** has been synced to disk. For pages that are in the original +** database file, the following expression should always be true: +** +** inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0 +** +** The pPager->aInJournal[] array is only valid for the original +** pages of the database, not new pages that are added to the end +** of the database, so obviously the above expression cannot be +** valid for new pages. For new pages inJournal is always 0. +** +** dirty +** +** When true, this means that the content of the page has been +** modified and needs to be written back to the database file. +** If false, it means that either the content of the page is +** unchanged or else the content is unimportant and we do not +** care whether or not it is preserved. +** +** alwaysRollback +** +** This means that the sqlite3PagerDontRollback() API should be +** ignored for this page. The DontRollback() API attempts to say +** that the content of the page on disk is unimportant (it is an +** unused page on the freelist) so that it is unnecessary to +** rollback changes to this page because the content of the page +** can change without changing the meaning of the database. This +** flag overrides any DontRollback() attempt. This flag is set +** when a page that originally contained valid data is added to +** the freelist. Later in the same transaction, this page might +** be pulled from the freelist and reused for something different +** and at that point the DontRollback() API will be called because +** pages taken from the freelist do not need to be protected by +** the rollback journal. But this flag says that the page was +** not originally part of the freelist so that it still needs to +** be rolled back in spite of any subsequent DontRollback() calls. +** +** needRead +** +** This flag means (when true) that the content of the page has +** not yet been loaded from disk. The in-memory content is just +** garbage. (Actually, we zero the content, but you should not +** make any assumptions about the content nevertheless.) If the +** content is needed in the future, it should be read from the +** original database file. */ -typedef struct PgHdr PgHdr; struct PgHdr { Pager *pPager; /* The pager to which this page belongs */ Pgno pgno; /* The page number for this page */ PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */ - PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */ + PagerLruLink free; /* Next and previous free pages */ PgHdr *pNextAll; /* A list of all pages */ - PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */ u8 inJournal; /* TRUE if has been written to journal */ - u8 inStmt; /* TRUE if in the statement subjournal */ u8 dirty; /* TRUE if we need to write back changes */ u8 needSync; /* Sync journal before writing this page */ - u8 alwaysRollback; /* Disable dont_rollback() for this page */ + u8 alwaysRollback; /* Disable DontRollback() for this page */ + u8 needRead; /* Read content if PagerWrite() is called */ short int nRef; /* Number of users of this page */ PgHdr *pDirty, *pPrevDirty; /* Dirty pages */ - u32 notUsed; /* Buffer space */ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + PagerLruLink gfree; /* Global list of nRef==0 pages */ +#endif #ifdef SQLITE_CHECK_PAGES u32 pageHash; #endif - /* pPager->pageSize bytes of page data follow this header */ - /* Pager.nExtra bytes of local data follow the page data */ + void *pData; /* Page data */ + /* Pager.nExtra bytes of local data appended to this header */ }; /* @@ -185,6 +293,8 @@ typedef struct PgHistory PgHistory; struct PgHistory { u8 *pOrig; /* Original page text. Restore to this on a full rollback */ u8 *pStmt; /* Text as it was at the beginning of the current statement */ + PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */ + u8 inStmt; /* TRUE if in the statement subjournal */ }; /* @@ -202,24 +312,24 @@ struct PgHistory { ** Convert a pointer to a PgHdr into a pointer to its data ** and back again. */ -#define PGHDR_TO_DATA(P) ((void*)(&(P)[1])) -#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1]) -#define PGHDR_TO_EXTRA(G,P) ((void*)&((char*)(&(G)[1]))[(P)->pageSize]) +#define PGHDR_TO_DATA(P) ((P)->pData) +#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1])) #define PGHDR_TO_HIST(P,PGR) \ - ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra]) + ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra]) /* ** A open page cache is an instance of the following structure. ** -** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, SQLITE_PROTOCOL +** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or ** or SQLITE_FULL. Once one of the first three errors occurs, it persists ** and is returned as the result of every major pager API call. The ** SQLITE_FULL return code is slightly different. It persists only until the ** next successful rollback is performed on the pager cache. Also, -** SQLITE_FULL does not affect the sqlite3pager_get() and sqlite3pager_lookup() +** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup() ** APIs, they may still be used successfully. */ struct Pager { + sqlite3_vfs *pVfs; /* OS functions to use for IO */ u8 journalOpen; /* True if journal file descriptors is valid */ u8 journalStarted; /* True if header of journal is synced */ u8 useJournal; /* Use a rollback journal on this file */ @@ -229,16 +339,20 @@ struct Pager { u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/ u8 noSync; /* Do not sync the journal if true */ u8 fullSync; /* Do extra syncs of the journal for robustness */ - u8 full_fsync; /* Use F_FULLFSYNC when available */ + u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */ u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */ - u8 errCode; /* One of several kinds of errors */ u8 tempFile; /* zFilename is a temporary file */ u8 readOnly; /* True for a read-only database */ u8 needSync; /* True if an fsync() is needed on the journal */ u8 dirtyCache; /* True if cached pages have changed */ - u8 alwaysRollback; /* Disable dont_rollback() for all pages */ + u8 alwaysRollback; /* Disable DontRollback() for all pages */ u8 memDb; /* True to inhibit all file I/O */ u8 setMaster; /* True if a m-j name has been written to jrnl */ + u8 doNotSync; /* Boolean. While true, do not spill the cache */ + u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ + u8 changeCountDone; /* Set after incrementing the change-counter */ + u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ + int errCode; /* One of several kinds of errors */ int dbSize; /* Number of pages in the file */ int origDbSize; /* dbSize before the current change */ int stmtSize; /* Size of database (in pages) at stmt_begin() */ @@ -248,19 +362,19 @@ struct Pager { int nExtra; /* Add this many bytes to each in-memory page */ int pageSize; /* Number of bytes in a page */ int nPage; /* Total number of in-memory pages */ - int nMaxPage; /* High water mark of nPage */ int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ int mxPage; /* Maximum number of pages to hold in cache */ + Pgno mxPgno; /* Maximum allowed size of the database */ u8 *aInJournal; /* One bit for each page in the database file */ u8 *aInStmt; /* One bit for each page in the database */ char *zFilename; /* Name of the database file */ char *zJournal; /* Name of the journal file */ char *zDirectory; /* Directory hold database and journal files */ - OsFile *fd, *jfd; /* File descriptors for database and journal */ - OsFile *stfd; /* File descriptor for the statement subjournal*/ + char *zStmtJrnl; /* Name of the statement journal file */ + sqlite3_file *fd, *jfd; /* File descriptors for database and journal */ + sqlite3_file *stfd; /* File descriptor for the statement subjournal*/ BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */ - PgHdr *pFirst, *pLast; /* List of free pages */ - PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */ + PagerLruList lru; /* LRU list of free pages */ PgHdr *pAll; /* List of all pages */ PgHdr *pStmt; /* List of pages in the statement subjournal */ PgHdr *pDirty; /* List of all dirty pages */ @@ -271,31 +385,55 @@ struct Pager { i64 stmtJSize; /* Size of journal at stmt_begin() */ int sectorSize; /* Assumed sector size during rollback */ #ifdef SQLITE_TEST - int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */ - int nRead,nWrite; /* Database pages read/written */ + int nHit, nMiss; /* Cache hits and missing */ + int nRead, nWrite; /* Database pages read/written */ #endif - void (*xDestructor)(void*,int); /* Call this routine when freeing pages */ - void (*xReiniter)(void*,int); /* Call this routine when reloading pages */ + void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */ + void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */ +#ifdef SQLITE_HAS_CODEC void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ void *pCodecArg; /* First argument to xCodec() */ +#endif int nHash; /* Size of the pager hash table */ PgHdr **aHash; /* Hash table to map page number to PgHdr */ #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - Pager *pNext; /* Linked list of pagers in this thread */ + Pager *pNext; /* Doubly linked list of pagers on which */ + Pager *pPrev; /* sqlite3_release_memory() will work */ + int iInUseMM; /* Non-zero if unavailable to MM */ + int iInUseDB; /* Non-zero if in sqlite3_release_memory() */ #endif + char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ + char dbFileVers[16]; /* Changes whenever database file changes */ }; /* -** If SQLITE_TEST is defined then increment the variable given in -** the argument +** The following global variables hold counters used for +** testing purposes only. These variables do not exist in +** a non-testing build. These variables are not thread-safe. */ #ifdef SQLITE_TEST -# define TEST_INCR(x) x++ +int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */ +int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */ +int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */ +int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */ +# define PAGER_INCR(v) v++ #else -# define TEST_INCR(x) +# define PAGER_INCR(v) #endif /* +** The following variable points to the head of a double-linked list +** of all pagers that are eligible for page stealing by the +** sqlite3_release_memory() interface. Access to this list is +** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex. +*/ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +static Pager *sqlite3PagerList = 0; +static PagerLruList sqlite3LruPageList = {0, 0, 0}; +#endif + + +/* ** Journal files begin with the following magic string. The data ** was obtained from /dev/random. It is used only as a sanity check. ** @@ -348,11 +486,6 @@ static const unsigned char aJournalMagic[] = { #endif /* -** The default size of a disk sector -*/ -#define PAGER_SECTOR_SIZE 512 - -/* ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is ** reserved for working around a windows/posix incompatibility). It is ** used in the journal to signify that the remainder of the journal file @@ -368,24 +501,176 @@ static const unsigned char aJournalMagic[] = { #define PAGER_MAX_PGNO 2147483647 /* -** Enable reference count tracking (for debugging) here: +** The pagerEnter() and pagerLeave() routines acquire and release +** a mutex on each pager. The mutex is recursive. +** +** This is a special-purpose mutex. It only provides mutual exclusion +** between the Btree and the Memory Management sqlite3_release_memory() +** function. It does not prevent, for example, two Btrees from accessing +** the same pager at the same time. Other general-purpose mutexes in +** the btree layer handle that chore. */ -#ifdef SQLITE_TEST - int pager3_refinfo_enable = 0; - static void pager_refinfo(PgHdr *p){ - static int cnt = 0; - if( !pager3_refinfo_enable ) return; - sqlite3DebugPrintf( - "REFCNT: %4d addr=%p nRef=%d\n", - p->pgno, PGHDR_TO_DATA(p), p->nRef - ); - cnt++; /* Something to set a breakpoint on */ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + static void pagerEnter(Pager *p){ + p->iInUseDB++; + if( p->iInUseMM && p->iInUseDB==1 ){ + sqlite3_mutex *mutex; + mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); + p->iInUseDB = 0; + sqlite3_mutex_enter(mutex); + p->iInUseDB = 1; + sqlite3_mutex_leave(mutex); + } + assert( p->iInUseMM==0 ); + } + static void pagerLeave(Pager *p){ + p->iInUseDB--; + assert( p->iInUseDB>=0 ); } -# define REFINFO(X) pager_refinfo(X) #else -# define REFINFO(X) +# define pagerEnter(X) +# define pagerLeave(X) +#endif + +/* +** Add page pPg to the end of the linked list managed by structure +** pList (pPg becomes the last entry in the list - the most recently +** used). Argument pLink should point to either pPg->free or pPg->gfree, +** depending on whether pPg is being added to the pager-specific or +** global LRU list. +*/ +static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){ + pLink->pNext = 0; + pLink->pPrev = pList->pLast; + +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + assert(pLink==&pPg->free || pLink==&pPg->gfree); + assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList); #endif + if( pList->pLast ){ + int iOff = (char *)pLink - (char *)pPg; + PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]); + pLastLink->pNext = pPg; + }else{ + assert(!pList->pFirst); + pList->pFirst = pPg; + } + + pList->pLast = pPg; + if( !pList->pFirstSynced && pPg->needSync==0 ){ + pList->pFirstSynced = pPg; + } +} + +/* +** Remove pPg from the list managed by the structure pointed to by pList. +** +** Argument pLink should point to either pPg->free or pPg->gfree, depending +** on whether pPg is being added to the pager-specific or global LRU list. +*/ +static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){ + int iOff = (char *)pLink - (char *)pPg; + +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + assert(pLink==&pPg->free || pLink==&pPg->gfree); + assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList); +#endif + + if( pPg==pList->pFirst ){ + pList->pFirst = pLink->pNext; + } + if( pPg==pList->pLast ){ + pList->pLast = pLink->pPrev; + } + if( pLink->pPrev ){ + PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]); + pPrevLink->pNext = pLink->pNext; + } + if( pLink->pNext ){ + PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]); + pNextLink->pPrev = pLink->pPrev; + } + if( pPg==pList->pFirstSynced ){ + PgHdr *p = pLink->pNext; + while( p && p->needSync ){ + PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]); + p = pL->pNext; + } + pList->pFirstSynced = p; + } + + pLink->pNext = pLink->pPrev = 0; +} + +/* +** Add page pPg to the list of free pages for the pager. If +** memory-management is enabled, also add the page to the global +** list of free pages. +*/ +static void lruListAdd(PgHdr *pPg){ + listAdd(&pPg->pPager->lru, &pPg->free, pPg); +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + if( !pPg->pPager->memDb ){ + sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); + listAdd(&sqlite3LruPageList, &pPg->gfree, pPg); + sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); + } +#endif +} + +/* +** Remove page pPg from the list of free pages for the associated pager. +** If memory-management is enabled, also remove pPg from the global list +** of free pages. +*/ +static void lruListRemove(PgHdr *pPg){ + listRemove(&pPg->pPager->lru, &pPg->free, pPg); +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + if( !pPg->pPager->memDb ){ + sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); + listRemove(&sqlite3LruPageList, &pPg->gfree, pPg); + sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); + } +#endif +} + +/* +** This function is called just after the needSync flag has been cleared +** from all pages managed by pPager (usually because the journal file +** has just been synced). It updates the pPager->lru.pFirstSynced variable +** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced +** variable also. +*/ +static void lruListSetFirstSynced(Pager *pPager){ + pPager->lru.pFirstSynced = pPager->lru.pFirst; +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + if( !pPager->memDb ){ + PgHdr *p; + sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); + for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext); + assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced); + sqlite3LruPageList.pFirstSynced = p; + sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); + } +#endif +} + +/* +** Return true if page *pPg has already been written to the statement +** journal (or statement snapshot has been created, if *pPg is part +** of an in-memory database). +*/ +static int pageInStatement(PgHdr *pPg){ + Pager *pPager = pPg->pPager; + if( MEMDB ){ + return PGHDR_TO_HIST(pPg, pPager)->inStmt; + }else{ + Pgno pgno = pPg->pgno; + u8 *a = pPager->aInStmt; + return (a && (int)pgno<=pPager->stmtSize && (a[pgno/8] & (1<<(pgno&7)))); + } +} /* ** Change the size of the pager hash table to N. N must be a power @@ -394,12 +679,16 @@ static const unsigned char aJournalMagic[] = { static void pager_resize_hash_table(Pager *pPager, int N){ PgHdr **aHash, *pPg; assert( N>0 && (N&(N-1))==0 ); - aHash = sqliteMalloc( sizeof(aHash[0])*N ); + pagerLeave(pPager); + sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pPager->aHash!=0); + aHash = sqlite3MallocZero( sizeof(aHash[0])*N ); + sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0); + pagerEnter(pPager); if( aHash==0 ){ /* Failure to rehash is not an error. It is only a performance hit. */ return; } - sqliteFree(pPager->aHash); + sqlite3_free(pPager->aHash); pPager->nHash = N; pPager->aHash = aHash; for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ @@ -425,11 +714,11 @@ static void pager_resize_hash_table(Pager *pPager, int N){ ** ** All values are stored on disk as big-endian. */ -static int read32bits(OsFile *fd, u32 *pRes){ +static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){ unsigned char ac[4]; - int rc = sqlite3OsRead(fd, ac, sizeof(ac)); + int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset); if( rc==SQLITE_OK ){ - *pRes = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3]; + *pRes = sqlite3Get4byte(ac); } return rc; } @@ -437,33 +726,63 @@ static int read32bits(OsFile *fd, u32 *pRes){ /* ** Write a 32-bit integer into a string buffer in big-endian byte order. */ -static void put32bits(char *ac, u32 val){ - ac[0] = (val>>24) & 0xff; - ac[1] = (val>>16) & 0xff; - ac[2] = (val>>8) & 0xff; - ac[3] = val & 0xff; -} +#define put32bits(A,B) sqlite3Put4byte((u8*)A,B) /* ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK ** on success or an error code is something goes wrong. */ -static int write32bits(OsFile *fd, u32 val){ +static int write32bits(sqlite3_file *fd, i64 offset, u32 val){ char ac[4]; put32bits(ac, val); - return sqlite3OsWrite(fd, ac, 4); + return sqlite3OsWrite(fd, ac, 4, offset); } /* -** Read a 32-bit integer at offset 'offset' from the page identified by -** page header 'p'. +** If file pFd is open, call sqlite3OsUnlock() on it. */ -static u32 retrieve32bits(PgHdr *p, int offset){ - unsigned char *ac; - ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset]; - return (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3]; +static int osUnlock(sqlite3_file *pFd, int eLock){ + if( !pFd->pMethods ){ + return SQLITE_OK; + } + return sqlite3OsUnlock(pFd, eLock); } +/* +** This function determines whether or not the atomic-write optimization +** can be used with this pager. The optimization can be used if: +** +** (a) the value returned by OsDeviceCharacteristics() indicates that +** a database page may be written atomically, and +** (b) the value returned by OsSectorSize() is less than or equal +** to the page size. +** +** If the optimization cannot be used, 0 is returned. If it can be used, +** then the value returned is the size of the journal file when it +** contains rollback data for exactly one page. +*/ +#ifdef SQLITE_ENABLE_ATOMIC_WRITE +static int jrnlBufferSize(Pager *pPager){ + int dc; /* Device characteristics */ + int nSector; /* Sector size */ + int nPage; /* Page size */ + sqlite3_file *fd = pPager->fd; + + if( fd->pMethods ){ + dc = sqlite3OsDeviceCharacteristics(fd); + nSector = sqlite3OsSectorSize(fd); + nPage = pPager->pageSize; + } + + assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); + assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); + + if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){ + return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); + } + return 0; +} +#endif /* ** This function should be called when an error occurs within the pager @@ -471,36 +790,63 @@ static u32 retrieve32bits(PgHdr *p, int offset){ ** second the error-code about to be returned by a pager API function. ** The value returned is a copy of the second argument to this function. ** -** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT or SQLITE_PROTOCOL, -** the error becomes persistent. All subsequent API calls on this Pager -** will immediately return the same error code. +** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL +** the error becomes persistent. Until the persisten error is cleared, +** subsequent API calls on this Pager will immediately return the same +** error code. +** +** A persistent error indicates that the contents of the pager-cache +** cannot be trusted. This state can be cleared by completely discarding +** the contents of the pager-cache. If a transaction was active when +** the persistent error occured, then the rollback journal may need +** to be replayed. */ +static void pager_unlock(Pager *pPager); static int pager_error(Pager *pPager, int rc){ - assert( pPager->errCode==SQLITE_FULL || pPager->errCode==SQLITE_OK ); - if( - rc==SQLITE_FULL || - rc==SQLITE_IOERR || - rc==SQLITE_CORRUPT || - rc==SQLITE_PROTOCOL + int rc2 = rc & 0xff; + assert( + pPager->errCode==SQLITE_FULL || + pPager->errCode==SQLITE_OK || + (pPager->errCode & 0xff)==SQLITE_IOERR + ); + if( + rc2==SQLITE_FULL || + rc2==SQLITE_IOERR || + rc2==SQLITE_CORRUPT ){ pPager->errCode = rc; + if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){ + /* If the pager is already unlocked, call pager_unlock() now to + ** clear the error state and ensure that the pager-cache is + ** completely empty. + */ + pager_unlock(pPager); + } } return rc; } +/* +** If SQLITE_CHECK_PAGES is defined then we do some sanity checking +** on the cache using a hash function. This is used for testing +** and debugging only. +*/ #ifdef SQLITE_CHECK_PAGES /* ** Return a 32-bit hash of the page data for pPage. */ -static u32 pager_pagehash(PgHdr *pPage){ +static u32 pager_datahash(int nByte, unsigned char *pData){ u32 hash = 0; int i; - unsigned char *pData = (unsigned char *)PGHDR_TO_DATA(pPage); - for(i=0; i<pPage->pPager->pageSize; i++){ - hash = (hash+i)^pData[i]; + for(i=0; i<nByte; i++){ + hash = (hash*1039) + pData[i]; } return hash; } +static u32 pager_pagehash(PgHdr *pPage){ + return pager_datahash(pPage->pPager->pageSize, + (unsigned char *)PGHDR_TO_DATA(pPage)); +} /* ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES @@ -515,20 +861,27 @@ static void checkPage(PgHdr *pPg){ } #else +#define pager_datahash(X,Y) 0 +#define pager_pagehash(X) 0 #define CHECK_PAGE(x) #endif /* ** When this is called the journal file for pager pPager must be open. ** The master journal file name is read from the end of the file and -** written into memory obtained from sqliteMalloc(). *pzMaster is -** set to point at the memory and SQLITE_OK returned. The caller must -** sqliteFree() *pzMaster. +** written into memory supplied by the caller. +** +** zMaster must point to a buffer of at least nMaster bytes allocated by +** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is +** enough space to write the master journal name). If the master journal +** name in the journal is longer than nMaster bytes (including a +** nul-terminator), then this is handled as if no master journal name +** were present in the journal. ** -** If no master journal file name is present *pzMaster is set to 0 and +** If no master journal file name is present zMaster[0] is set to 0 and ** SQLITE_OK returned. */ -static int readMasterJournal(OsFile *pJrnl, char **pzMaster){ +static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){ int rc; u32 len; i64 szJ; @@ -536,51 +889,41 @@ static int readMasterJournal(OsFile *pJrnl, char **pzMaster){ int i; unsigned char aMagic[8]; /* A buffer to hold the magic header */ - *pzMaster = 0; + zMaster[0] = '\0'; rc = sqlite3OsFileSize(pJrnl, &szJ); if( rc!=SQLITE_OK || szJ<16 ) return rc; - rc = sqlite3OsSeek(pJrnl, szJ-16); - if( rc!=SQLITE_OK ) return rc; - - rc = read32bits(pJrnl, &len); + rc = read32bits(pJrnl, szJ-16, &len); if( rc!=SQLITE_OK ) return rc; - rc = read32bits(pJrnl, &cksum); + if( len>=nMaster ){ + return SQLITE_OK; + } + + rc = read32bits(pJrnl, szJ-12, &cksum); if( rc!=SQLITE_OK ) return rc; - rc = sqlite3OsRead(pJrnl, aMagic, 8); + rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8); if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc; - rc = sqlite3OsSeek(pJrnl, szJ-16-len); - if( rc!=SQLITE_OK ) return rc; - - *pzMaster = (char *)sqliteMalloc(len+1); - if( !*pzMaster ){ - return SQLITE_NOMEM; - } - rc = sqlite3OsRead(pJrnl, *pzMaster, len); + rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len); if( rc!=SQLITE_OK ){ - sqliteFree(*pzMaster); - *pzMaster = 0; return rc; } + zMaster[len] = '\0'; /* See if the checksum matches the master journal name */ for(i=0; i<len; i++){ - cksum -= (*pzMaster)[i]; - } + cksum -= zMaster[i]; + } if( cksum ){ /* If the checksum doesn't add up, then one or more of the disk sectors ** containing the master journal filename is corrupted. This means ** definitely roll back, so just return SQLITE_OK and report a (nul) ** master-journal filename. */ - sqliteFree(*pzMaster); - *pzMaster = 0; - }else{ - (*pzMaster)[len] = '\0'; + zMaster[0] = '\0'; } return SQLITE_OK; @@ -601,7 +944,7 @@ static int readMasterJournal(OsFile *pJrnl, char **pzMaster){ ** 2000 2048 ** */ -static int seekJournalHdr(Pager *pPager){ +static void seekJournalHdr(Pager *pPager){ i64 offset = 0; i64 c = pPager->journalOff; if( c ){ @@ -611,7 +954,6 @@ static int seekJournalHdr(Pager *pPager){ assert( offset>=c ); assert( (offset-c)<JOURNAL_HDR_SZ(pPager) ); pPager->journalOff = offset; - return sqlite3OsSeek(pPager->jfd, pPager->journalOff); } /* @@ -630,27 +972,46 @@ static int seekJournalHdr(Pager *pPager){ */ static int writeJournalHdr(Pager *pPager){ char zHeader[sizeof(aJournalMagic)+16]; + int rc; - int rc = seekJournalHdr(pPager); - if( rc ) return rc; - - pPager->journalHdr = pPager->journalOff; if( pPager->stmtHdrOff==0 ){ - pPager->stmtHdrOff = pPager->journalHdr; + pPager->stmtHdrOff = pPager->journalOff; } - pPager->journalOff += JOURNAL_HDR_SZ(pPager); - /* FIX ME: + seekJournalHdr(pPager); + pPager->journalHdr = pPager->journalOff; + + memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); + + /* + ** Write the nRec Field - the number of page records that follow this + ** journal header. Normally, zero is written to this value at this time. + ** After the records are added to the journal (and the journal synced, + ** if in full-sync mode), the zero is overwritten with the true number + ** of records (see syncJournal()). + ** + ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When + ** reading the journal this value tells SQLite to assume that the + ** rest of the journal file contains valid page records. This assumption + ** is dangerous, as if a failure occured whilst writing to the journal + ** file it may contain some garbage data. There are two scenarios + ** where this risk can be ignored: ** - ** Possibly for a pager not in no-sync mode, the journal magic should not - ** be written until nRec is filled in as part of next syncJournal(). + ** * When the pager is in no-sync mode. Corruption can follow a + ** power failure in this case anyway. ** - ** Actually maybe the whole journal header should be delayed until that - ** point. Think about this. + ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees + ** that garbage data is never appended to the journal file. */ - memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); - /* The nRec Field. 0xFFFFFFFF for no-sync journals. */ - put32bits(&zHeader[sizeof(aJournalMagic)], pPager->noSync ? 0xffffffff : 0); + assert(pPager->fd->pMethods||pPager->noSync); + if( (pPager->noSync) + || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) + ){ + put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); + }else{ + put32bits(&zHeader[sizeof(aJournalMagic)], 0); + } + /* The random check-hash initialiser */ sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit); @@ -658,16 +1019,16 @@ static int writeJournalHdr(Pager *pPager){ put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize); /* The assumed sector size for this process */ put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize); - rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader)); + IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader))) + rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff); + pPager->journalOff += JOURNAL_HDR_SZ(pPager); /* The journal header has been written successfully. Seek the journal ** file descriptor to the end of the journal header sector. */ if( rc==SQLITE_OK ){ - rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff-1); - if( rc==SQLITE_OK ){ - rc = sqlite3OsWrite(pPager->jfd, "\000", 1); - } + IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1)) + rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1); } return rc; } @@ -696,28 +1057,29 @@ static int readJournalHdr( ){ int rc; unsigned char aMagic[8]; /* A buffer to hold the magic header */ + i64 jrnlOff; - rc = seekJournalHdr(pPager); - if( rc ) return rc; - + seekJournalHdr(pPager); if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){ return SQLITE_DONE; } + jrnlOff = pPager->journalOff; - rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic)); + rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff); if( rc ) return rc; + jrnlOff += sizeof(aMagic); if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){ return SQLITE_DONE; } - rc = read32bits(pPager->jfd, pNRec); + rc = read32bits(pPager->jfd, jrnlOff, pNRec); if( rc ) return rc; - rc = read32bits(pPager->jfd, &pPager->cksumInit); + rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit); if( rc ) return rc; - rc = read32bits(pPager->jfd, pDbSize); + rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize); if( rc ) return rc; /* Update the assumed sector-size to match the value used by @@ -726,12 +1088,11 @@ static int readJournalHdr( ** is being called from within pager_playback(). The local value ** of Pager.sectorSize is restored at the end of that routine. */ - rc = read32bits(pPager->jfd, (u32 *)&pPager->sectorSize); + rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize); if( rc ) return rc; pPager->journalOff += JOURNAL_HDR_SZ(pPager); - rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff); - return rc; + return SQLITE_OK; } @@ -758,6 +1119,7 @@ static int writeMasterJournal(Pager *pPager, const char *zMaster){ int rc; int len; int i; + i64 jrnlOff; u32 cksum = 0; char zBuf[sizeof(aJournalMagic)+2*4]; @@ -774,21 +1136,23 @@ static int writeMasterJournal(Pager *pPager, const char *zMaster){ ** the journal has already been synced. */ if( pPager->fullSync ){ - rc = seekJournalHdr(pPager); - if( rc!=SQLITE_OK ) return rc; + seekJournalHdr(pPager); } + jrnlOff = pPager->journalOff; pPager->journalOff += (len+20); - rc = write32bits(pPager->jfd, PAGER_MJ_PGNO(pPager)); + rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager)); if( rc!=SQLITE_OK ) return rc; + jrnlOff += 4; - rc = sqlite3OsWrite(pPager->jfd, zMaster, len); + rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff); if( rc!=SQLITE_OK ) return rc; + jrnlOff += len; put32bits(zBuf, len); put32bits(&zBuf[4], cksum); memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic)); - rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic)); + rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff); pPager->needSync = !pPager->noSync; return rc; } @@ -798,38 +1162,23 @@ static int writeMasterJournal(Pager *pPager, const char *zMaster){ ** statement journal. ** ** The Pager keeps a separate list of pages that are currently in -** the statement journal. This helps the sqlite3pager_stmt_commit() +** the statement journal. This helps the sqlite3PagerStmtCommit() ** routine run MUCH faster for the common case where there are many ** pages in memory but only a few are in the statement journal. */ static void page_add_to_stmt_list(PgHdr *pPg){ Pager *pPager = pPg->pPager; - if( pPg->inStmt ) return; - assert( pPg->pPrevStmt==0 && pPg->pNextStmt==0 ); - pPg->pPrevStmt = 0; - if( pPager->pStmt ){ - pPager->pStmt->pPrevStmt = pPg; - } - pPg->pNextStmt = pPager->pStmt; - pPager->pStmt = pPg; - pPg->inStmt = 1; -} -static void page_remove_from_stmt_list(PgHdr *pPg){ - if( !pPg->inStmt ) return; - if( pPg->pPrevStmt ){ - assert( pPg->pPrevStmt->pNextStmt==pPg ); - pPg->pPrevStmt->pNextStmt = pPg->pNextStmt; - }else{ - assert( pPg->pPager->pStmt==pPg ); - pPg->pPager->pStmt = pPg->pNextStmt; - } - if( pPg->pNextStmt ){ - assert( pPg->pNextStmt->pPrevStmt==pPg ); - pPg->pNextStmt->pPrevStmt = pPg->pPrevStmt; + PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); + assert( MEMDB ); + if( !pHist->inStmt ){ + assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 ); + if( pPager->pStmt ){ + PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg; + } + pHist->pNextStmt = pPager->pStmt; + pPager->pStmt = pPg; + pHist->inStmt = 1; } - pPg->pNextStmt = 0; - pPg->pPrevStmt = 0; - pPg->inStmt = 0; } /* @@ -847,7 +1196,7 @@ static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ } /* -** Unlock the database and clear the in-memory cache. This routine +** Clear the in-memory cache. This routine ** sets the state of the pager back to what it was when it was first ** opened. Any outstanding pages are invalidated and subsequent attempts ** to access those pages will likely result in a coredump. @@ -856,59 +1205,141 @@ static void pager_reset(Pager *pPager){ PgHdr *pPg, *pNext; if( pPager->errCode ) return; for(pPg=pPager->pAll; pPg; pPg=pNext){ + IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno)); + PAGER_INCR(sqlite3_pager_pgfree_count); pNext = pPg->pNextAll; - sqliteFree(pPg); + lruListRemove(pPg); + sqlite3_free(pPg); } - pPager->pFirst = 0; - pPager->pFirstSynced = 0; - pPager->pLast = 0; + assert(pPager->lru.pFirst==0); + assert(pPager->lru.pFirstSynced==0); + assert(pPager->lru.pLast==0); + pPager->pStmt = 0; pPager->pAll = 0; + pPager->pDirty = 0; pPager->nHash = 0; - sqliteFree(pPager->aHash); + sqlite3_free(pPager->aHash); pPager->nPage = 0; pPager->aHash = 0; - if( pPager->state>=PAGER_RESERVED ){ - sqlite3pager_rollback(pPager); - } - sqlite3OsUnlock(pPager->fd, NO_LOCK); - pPager->state = PAGER_UNLOCK; - pPager->dbSize = -1; pPager->nRef = 0; - assert( pPager->journalOpen==0 ); } /* +** Unlock the database file. +** +** If the pager is currently in error state, discard the contents of +** the cache and reset the Pager structure internal state. If there is +** an open journal-file, then the next time a shared-lock is obtained +** on the pager file (by this or any other process), it will be +** treated as a hot-journal and rolled back. +*/ +static void pager_unlock(Pager *pPager){ + if( !pPager->exclusiveMode ){ + if( !MEMDB ){ + if( pPager->fd->pMethods ){ + osUnlock(pPager->fd, NO_LOCK); + } + pPager->dbSize = -1; + IOTRACE(("UNLOCK %p\n", pPager)) + + /* If Pager.errCode is set, the contents of the pager cache cannot be + ** trusted. Now that the pager file is unlocked, the contents of the + ** cache can be discarded and the error code safely cleared. + */ + if( pPager->errCode ){ + pPager->errCode = SQLITE_OK; + pager_reset(pPager); + if( pPager->stmtOpen ){ + sqlite3OsClose(pPager->stfd); + sqlite3_free(pPager->aInStmt); + pPager->aInStmt = 0; + } + if( pPager->journalOpen ){ + sqlite3OsClose(pPager->jfd); + pPager->journalOpen = 0; + sqlite3_free(pPager->aInJournal); + pPager->aInJournal = 0; + } + pPager->stmtOpen = 0; + pPager->stmtInUse = 0; + pPager->journalOff = 0; + pPager->journalStarted = 0; + pPager->stmtAutoopen = 0; + pPager->origDbSize = 0; + } + } + + if( !MEMDB || pPager->errCode==SQLITE_OK ){ + pPager->state = PAGER_UNLOCK; + pPager->changeCountDone = 0; + } + } +} + +/* +** Execute a rollback if a transaction is active and unlock the +** database file. If the pager has already entered the error state, +** do not attempt the rollback. +*/ +static void pagerUnlockAndRollback(Pager *p){ + assert( p->state>=PAGER_RESERVED || p->journalOpen==0 ); + if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){ + sqlite3PagerRollback(p); + } + pager_unlock(p); + assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) ); + assert( p->errCode || !p->stmtOpen || p->exclusiveMode ); +} + +/* +** This routine ends a transaction. A transaction is ended by either +** a COMMIT or a ROLLBACK. +** ** When this routine is called, the pager has the journal file open and -** a RESERVED or EXCLUSIVE lock on the database. This routine releases -** the database lock and acquires a SHARED lock in its place. The journal -** file is deleted and closed. +** a RESERVED or EXCLUSIVE lock on the database. This routine will release +** the database lock and acquires a SHARED lock in its place if that is +** the appropriate thing to do. Release locks usually is appropriate, +** unless we are in exclusive access mode or unless this is a +** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation. +** +** The journal file is either deleted or truncated. ** ** TODO: Consider keeping the journal file open for temporary databases. ** This might give a performance improvement on windows where opening ** a file is an expensive operation. */ -static int pager_unwritelock(Pager *pPager){ +static int pager_end_transaction(Pager *pPager){ PgHdr *pPg; - int rc; + int rc = SQLITE_OK; + int rc2 = SQLITE_OK; assert( !MEMDB ); if( pPager->state<PAGER_RESERVED ){ return SQLITE_OK; } - sqlite3pager_stmt_commit(pPager); - if( pPager->stmtOpen ){ - sqlite3OsClose(&pPager->stfd); + sqlite3PagerStmtCommit(pPager); + if( pPager->stmtOpen && !pPager->exclusiveMode ){ + sqlite3OsClose(pPager->stfd); pPager->stmtOpen = 0; } if( pPager->journalOpen ){ - sqlite3OsClose(&pPager->jfd); - pPager->journalOpen = 0; - sqlite3OsDelete(pPager->zJournal); - sqliteFree( pPager->aInJournal ); + if( pPager->exclusiveMode + && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){; + pPager->journalOff = 0; + pPager->journalStarted = 0; + }else{ + sqlite3OsClose(pPager->jfd); + pPager->journalOpen = 0; + if( rc==SQLITE_OK ){ + rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); + } + } + sqlite3_free( pPager->aInJournal ); pPager->aInJournal = 0; for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ pPg->inJournal = 0; pPg->dirty = 0; pPg->needSync = 0; + pPg->alwaysRollback = 0; #ifdef SQLITE_CHECK_PAGES pPg->pageHash = pager_pagehash(pPg); #endif @@ -920,13 +1351,20 @@ static int pager_unwritelock(Pager *pPager){ assert( pPager->aInJournal==0 ); assert( pPager->dirtyCache==0 || pPager->useJournal==0 ); } - rc = sqlite3OsUnlock(pPager->fd, SHARED_LOCK); - pPager->state = PAGER_SHARED; + + if( !pPager->exclusiveMode ){ + rc2 = osUnlock(pPager->fd, SHARED_LOCK); + pPager->state = PAGER_SHARED; + }else if( pPager->state==PAGER_SYNCED ){ + pPager->state = PAGER_EXCLUSIVE; + } pPager->origDbSize = 0; pPager->setMaster = 0; pPager->needSync = 0; - pPager->pFirstSynced = pPager->pFirst; - return rc; + lruListSetFirstSynced(pPager); + pPager->dbSize = -1; + + return (rc==SQLITE_OK?rc2:rc); } /* @@ -970,22 +1408,27 @@ static void makeClean(PgHdr*); ** are not used in statement journals because statement journals do not ** need to survive power failures. */ -static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int useCksum){ +static int pager_playback_one_page( + Pager *pPager, + sqlite3_file *jfd, + i64 offset, + int useCksum +){ int rc; PgHdr *pPg; /* An existing page in the cache */ Pgno pgno; /* The page number of a page in journal */ u32 cksum; /* Checksum used for sanity checking */ - u8 aData[SQLITE_MAX_PAGE_SIZE]; /* Temp storage for a page */ + u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */ /* useCksum should be true for the main journal and false for ** statement journals. Verify that this is always the case */ assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) ); + assert( aData ); - - rc = read32bits(jfd, &pgno); + rc = read32bits(jfd, offset, &pgno); if( rc!=SQLITE_OK ) return rc; - rc = sqlite3OsRead(jfd, &aData, pPager->pageSize); + rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4); if( rc!=SQLITE_OK ) return rc; pPager->journalOff += pPager->pageSize + 4; @@ -1001,7 +1444,7 @@ static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int useCksum){ return SQLITE_OK; } if( useCksum ){ - rc = read32bits(jfd, &cksum); + rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum); if( rc ) return rc; pPager->journalOff += 4; if( pager_cksum(pPager, aData)!=cksum ){ @@ -1015,6 +1458,13 @@ static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int useCksum){ ** page in the pager cache. In this case just update the pager cache, ** not the database file. The page is left marked dirty in this case. ** + ** An exception to the above rule: If the database is in no-sync mode + ** and a page is moved during an incremental vacuum then the page may + ** not be in the pager cache. Later: if a malloc() or IO error occurs + ** during a Movepage() call, then the page may not be in the cache + ** either. So the condition described in the above paragraph is not + ** assert()able. + ** ** If in EXCLUSIVE state, then we update the pager cache if it exists ** and the main file. The page is then marked not dirty. ** @@ -1023,22 +1473,21 @@ static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int useCksum){ ** This occurs when a page is changed prior to the start of a statement ** then changed again within the statement. When rolling back such a ** statement we must not write to the original database unless we know - ** for certain that original page contents are in the main rollback - ** journal. Otherwise, if a full ROLLBACK occurs after the statement - ** rollback the full ROLLBACK will not restore the page to its original - ** content. Two conditions must be met before writing to the database - ** files. (1) the database must be locked. (2) we know that the original - ** page content is in the main journal either because the page is not in - ** cache or else it is marked as needSync==0. + ** for certain that original page contents are synced into the main rollback + ** journal. Otherwise, a power loss might leave modified data in the + ** database file without an entry in the rollback journal that can + ** restore the database to its original form. Two conditions must be + ** met before writing to the database files. (1) the database must be + ** locked. (2) we know that the original page content is fully synced + ** in the main journal either because the page is not in cache or else + ** the page is marked as needSync==0. */ pPg = pager_lookup(pPager, pgno); - assert( pPager->state>=PAGER_EXCLUSIVE || pPg!=0 ); - TRACE3("PLAYBACK %d page %d\n", PAGERID(pPager), pgno); + PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n", + PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData)); if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){ - rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize); - if( rc==SQLITE_OK ){ - rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize); - } + i64 offset = (pgno-1)*(i64)pPager->pageSize; + rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset); if( pPg ){ makeClean(pPg); } @@ -1048,18 +1497,25 @@ static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int useCksum){ ** for page 1 which is held in use in order to keep the lock on the ** database active. However such a page may be rolled back as a result ** of an internal error resulting in an automatic call to - ** sqlite3pager_rollback(). + ** sqlite3PagerRollback(). */ void *pData; /* assert( pPg->nRef==0 || pPg->pgno==1 ); */ pData = PGHDR_TO_DATA(pPg); memcpy(pData, aData, pPager->pageSize); - if( pPager->xDestructor ){ /*** FIX ME: Should this be xReinit? ***/ - pPager->xDestructor(pData, pPager->pageSize); + if( pPager->xReiniter ){ + pPager->xReiniter(pPg, pPager->pageSize); } #ifdef SQLITE_CHECK_PAGES pPg->pageHash = pager_pagehash(pPg); #endif + /* If this was page 1, then restore the value of Pager.dbFileVers. + ** Do this before any decoding. */ + if( pgno==1 ){ + memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); + } + + /* Decode the page just read from disk */ CODEC1(pPager, pData, pPg->pgno, 3); } return rc; @@ -1071,65 +1527,79 @@ static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int useCksum){ ** This routine checks if it is possible to delete the master journal file, ** and does so if it is. ** +** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not +** available for use within this function. +** +** ** The master journal file contains the names of all child journals. ** To tell if a master journal can be deleted, check to each of the ** children. If all children are either missing or do not refer to ** a different master journal, then this master journal can be deleted. */ -static int pager_delmaster(const char *zMaster){ +static int pager_delmaster(Pager *pPager, const char *zMaster){ + sqlite3_vfs *pVfs = pPager->pVfs; int rc; int master_open = 0; - OsFile *master = 0; + sqlite3_file *pMaster; + sqlite3_file *pJournal; char *zMasterJournal = 0; /* Contents of master journal file */ i64 nMasterJournal; /* Size of master journal file */ /* Open the master journal file exclusively in case some other process ** is running this routine also. Not that it makes too much difference. */ - rc = sqlite3OsOpenReadOnly(zMaster, &master); + pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2); + pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); + if( !pMaster ){ + rc = SQLITE_NOMEM; + }else{ + int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); + rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); + } if( rc!=SQLITE_OK ) goto delmaster_out; master_open = 1; - rc = sqlite3OsFileSize(master, &nMasterJournal); + + rc = sqlite3OsFileSize(pMaster, &nMasterJournal); if( rc!=SQLITE_OK ) goto delmaster_out; if( nMasterJournal>0 ){ char *zJournal; char *zMasterPtr = 0; + int nMasterPtr = pPager->pVfs->mxPathname+1; /* Load the entire master journal file into space obtained from - ** sqliteMalloc() and pointed to by zMasterJournal. + ** sqlite3_malloc() and pointed to by zMasterJournal. */ - zMasterJournal = (char *)sqliteMalloc(nMasterJournal); + zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr); if( !zMasterJournal ){ rc = SQLITE_NOMEM; goto delmaster_out; } - rc = sqlite3OsRead(master, zMasterJournal, nMasterJournal); + zMasterPtr = &zMasterJournal[nMasterJournal]; + rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0); if( rc!=SQLITE_OK ) goto delmaster_out; zJournal = zMasterJournal; while( (zJournal-zMasterJournal)<nMasterJournal ){ - if( sqlite3OsFileExists(zJournal) ){ + if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){ /* One of the journals pointed to by the master journal exists. ** Open it and check if it points at the master journal. If ** so, return without deleting the master journal file. */ - OsFile *journal = 0; int c; - - rc = sqlite3OsOpenReadOnly(zJournal, &journal); + int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL); + rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); if( rc!=SQLITE_OK ){ goto delmaster_out; } - rc = readMasterJournal(journal, &zMasterPtr); - sqlite3OsClose(&journal); + rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr); + sqlite3OsClose(pJournal); if( rc!=SQLITE_OK ){ goto delmaster_out; } - c = zMasterPtr!=0 && strcmp(zMasterPtr, zMaster)==0; - sqliteFree(zMasterPtr); + c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0; if( c ){ /* We have a match. Do not delete the master journal file. */ goto delmaster_out; @@ -1139,69 +1609,69 @@ static int pager_delmaster(const char *zMaster){ } } - sqlite3OsDelete(zMaster); + rc = sqlite3OsDelete(pVfs, zMaster, 0); delmaster_out: if( zMasterJournal ){ - sqliteFree(zMasterJournal); + sqlite3_free(zMasterJournal); } if( master_open ){ - sqlite3OsClose(&master); + sqlite3OsClose(pMaster); } + sqlite3_free(pMaster); return rc; } + +static void pager_truncate_cache(Pager *pPager); + /* -** Make every page in the cache agree with what is on disk. In other words, -** reread the disk to reset the state of the cache. +** Truncate the main file of the given pager to the number of pages +** indicated. Also truncate the cached representation of the file. ** -** This routine is called after a rollback in which some of the dirty cache -** pages had never been written out to disk. We need to roll back the -** cache content and the easiest way to do that is to reread the old content -** back from the disk. +** Might might be the case that the file on disk is smaller than nPage. +** This can happen, for example, if we are in the middle of a transaction +** which has extended the file size and the new pages are still all held +** in cache, then an INSERT or UPDATE does a statement rollback. Some +** operating system implementations can get confused if you try to +** truncate a file to some size that is larger than it currently is, +** so detect this case and do not do the truncation. */ -static int pager_reload_cache(Pager *pPager){ - PgHdr *pPg; +static int pager_truncate(Pager *pPager, int nPage){ int rc = SQLITE_OK; - for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ - char zBuf[SQLITE_MAX_PAGE_SIZE]; - if( !pPg->dirty ) continue; - if( (int)pPg->pgno <= pPager->origDbSize ){ - rc = sqlite3OsSeek(pPager->fd, pPager->pageSize*(i64)(pPg->pgno-1)); - if( rc==SQLITE_OK ){ - rc = sqlite3OsRead(pPager->fd, zBuf, pPager->pageSize); - } - TRACE3("REFETCH %d page %d\n", PAGERID(pPager), pPg->pgno); - if( rc ) break; - CODEC1(pPager, zBuf, pPg->pgno, 2); - }else{ - memset(zBuf, 0, pPager->pageSize); + if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){ + i64 currentSize, newSize; + rc = sqlite3OsFileSize(pPager->fd, ¤tSize); + newSize = pPager->pageSize*(i64)nPage; + if( rc==SQLITE_OK && currentSize>newSize ){ + rc = sqlite3OsTruncate(pPager->fd, newSize); } - if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), pPager->pageSize) ){ - memcpy(PGHDR_TO_DATA(pPg), zBuf, pPager->pageSize); - if( pPager->xReiniter ){ - pPager->xReiniter(PGHDR_TO_DATA(pPg), pPager->pageSize); - }else{ - memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra); - } - } - pPg->needSync = 0; - pPg->dirty = 0; -#ifdef SQLITE_CHECK_PAGES - pPg->pageHash = pager_pagehash(pPg); -#endif } - pPager->pDirty = 0; + if( rc==SQLITE_OK ){ + pPager->dbSize = nPage; + pager_truncate_cache(pPager); + } return rc; } /* -** Truncate the main file of the given pager to the number of pages -** indicated. +** Set the sectorSize for the given pager. +** +** The sector size is the larger of the sector size reported +** by sqlite3OsSectorSize() and the pageSize. */ -static int pager_truncate(Pager *pPager, int nPage){ - assert( pPager->state>=PAGER_EXCLUSIVE ); - return sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage); +static void setSectorSize(Pager *pPager){ + assert(pPager->fd->pMethods||pPager->tempFile); + if( !pPager->tempFile ){ + /* Sector size doesn't matter for temporary files. Also, the file + ** may not have been opened yet, in whcih case the OsSectorSize() + ** call will segfault. + */ + pPager->sectorSize = sqlite3OsSectorSize(pPager->fd); + } + if( pPager->sectorSize<pPager->pageSize ){ + pPager->sectorSize = pPager->pageSize; + } } /* @@ -1257,7 +1727,8 @@ static int pager_truncate(Pager *pPager, int nPage){ ** If an I/O or malloc() error occurs, the journal-file is not deleted ** and an error code is returned. */ -static int pager_playback(Pager *pPager){ +static int pager_playback(Pager *pPager, int isHot){ + sqlite3_vfs *pVfs = pPager->pVfs; i64 szJ; /* Size of the journal file in bytes */ u32 nRec; /* Number of Records in the journal */ int i; /* Loop counter */ @@ -1270,7 +1741,7 @@ static int pager_playback(Pager *pPager){ */ assert( pPager->journalOpen ); rc = sqlite3OsFileSize(pPager->jfd, &szJ); - if( rc!=SQLITE_OK ){ + if( rc!=SQLITE_OK || szJ==0 ){ goto end_playback; } @@ -1279,16 +1750,18 @@ static int pager_playback(Pager *pPager){ ** present on disk, then the journal is not hot and does not need to be ** played back. */ - rc = readMasterJournal(pPager->jfd, &zMaster); + zMaster = pPager->pTmpSpace; + rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); assert( rc!=SQLITE_DONE ); - if( rc!=SQLITE_OK || (zMaster && !sqlite3OsFileExists(zMaster)) ){ - sqliteFree(zMaster); + if( rc!=SQLITE_OK + || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS)) + ){ zMaster = 0; if( rc==SQLITE_DONE ) rc = SQLITE_OK; goto end_playback; } - sqlite3OsSeek(pPager->jfd, 0); pPager->journalOff = 0; + zMaster = 0; /* This loop terminates either when the readJournalHdr() call returns ** SQLITE_DONE or an IO error occurs. */ @@ -1317,23 +1790,33 @@ static int pager_playback(Pager *pPager){ nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager); } + /* If nRec is 0 and this rollback is of a transaction created by this + ** process and if this is the final header in the journal, then it means + ** that this part of the journal was being filled but has not yet been + ** synced to disk. Compute the number of pages based on the remaining + ** size of the file. + ** + ** The third term of the test was added to fix ticket #2565. + */ + if( nRec==0 && !isHot && + pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){ + nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager); + } + /* If this is the first header read from the journal, truncate the - ** database file back to it's original size. + ** database file back to its original size. */ - if( pPager->state>=PAGER_EXCLUSIVE && - pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){ - assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg ); + if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){ rc = pager_truncate(pPager, mxPg); if( rc!=SQLITE_OK ){ goto end_playback; } - pPager->dbSize = mxPg; } /* Copy original pages out of the journal and back into the database file. */ for(i=0; i<nRec; i++){ - rc = pager_playback_one_page(pPager, pPager->jfd, 1); + rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); if( rc!=SQLITE_OK ){ if( rc==SQLITE_DONE ){ rc = SQLITE_OK; @@ -1350,23 +1833,24 @@ static int pager_playback(Pager *pPager){ end_playback: if( rc==SQLITE_OK ){ - rc = pager_unwritelock(pPager); + zMaster = pPager->pTmpSpace; + rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); } - if( zMaster ){ - /* If there was a master journal and this routine will return true, + if( rc==SQLITE_OK ){ + rc = pager_end_transaction(pPager); + } + if( rc==SQLITE_OK && zMaster[0] ){ + /* If there was a master journal and this routine will return success, ** see if it is possible to delete the master journal. */ - if( rc==SQLITE_OK ){ - rc = pager_delmaster(zMaster); - } - sqliteFree(zMaster); + rc = pager_delmaster(pPager, zMaster); } /* The Pager.sectorSize variable may have been updated while rolling - ** back a journal created by a process with a different PAGER_SECTOR_SIZE + ** back a journal created by a process with a different sector size ** value. Reset it to the correct value for this process. */ - pPager->sectorSize = PAGER_SECTOR_SIZE; + setSectorSize(pPager); return rc; } @@ -1401,8 +1885,9 @@ static int pager_stmt_playback(Pager *pPager){ } #endif - /* Set hdrOff to be the offset to the first journal header written - ** this statement transaction, or the end of the file if no journal + /* Set hdrOff to be the offset just after the end of the last journal + ** page written before the first journal-header for this statement + ** transaction was written, or the end of the file if no journal ** header was written. */ hdrOff = pPager->stmtHdrOff; @@ -1413,15 +1898,12 @@ static int pager_stmt_playback(Pager *pPager){ /* Truncate the database back to its original size. */ - if( pPager->state>=PAGER_EXCLUSIVE ){ - rc = pager_truncate(pPager, pPager->stmtSize); - } - pPager->dbSize = pPager->stmtSize; + rc = pager_truncate(pPager, pPager->stmtSize); + assert( pPager->state>=PAGER_SHARED ); /* Figure out how many records are in the statement journal. */ assert( pPager->stmtInUse && pPager->journalOpen ); - sqlite3OsSeek(pPager->stfd, 0); nRec = pPager->stmtNRec; /* Copy original pages out of the statement journal and back into the @@ -1429,8 +1911,9 @@ static int pager_stmt_playback(Pager *pPager){ ** each record since power-failure recovery is not important to statement ** journals. */ - for(i=nRec-1; i>=0; i--){ - rc = pager_playback_one_page(pPager, pPager->stfd, 0); + for(i=0; i<nRec; i++){ + i64 offset = i*(4+pPager->pageSize); + rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0); assert( rc!=SQLITE_DONE ); if( rc!=SQLITE_OK ) goto end_stmt_playback; } @@ -1443,15 +1926,10 @@ static int pager_stmt_playback(Pager *pPager){ ** If it is not zero, then Pager.stmtHdrOff is the offset to the start ** of the first journal header written during this statement transaction. */ - rc = sqlite3OsSeek(pPager->jfd, pPager->stmtJSize); - if( rc!=SQLITE_OK ){ - goto end_stmt_playback; - } pPager->journalOff = pPager->stmtJSize; pPager->cksumInit = pPager->stmtCksum; - assert( JOURNAL_HDR_SZ(pPager)<(pPager->pageSize+8) ); - while( pPager->journalOff <= (hdrOff-(pPager->pageSize+8)) ){ - rc = pager_playback_one_page(pPager, pPager->jfd, 1); + while( pPager->journalOff < hdrOff ){ + rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); assert( rc!=SQLITE_DONE ); if( rc!=SQLITE_OK ) goto end_stmt_playback; } @@ -1468,7 +1946,7 @@ static int pager_stmt_playback(Pager *pPager){ nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8); } for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){ - rc = pager_playback_one_page(pPager, pPager->jfd, 1); + rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); assert( rc!=SQLITE_DONE ); if( rc!=SQLITE_OK ) goto end_stmt_playback; } @@ -1487,7 +1965,7 @@ end_stmt_playback: /* ** Change the maximum number of in-memory pages that are allowed. */ -void sqlite3pager_set_cachesize(Pager *pPager, int mxPage){ +void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){ if( mxPage>10 ){ pPager->mxPage = mxPage; }else{ @@ -1522,10 +2000,10 @@ void sqlite3pager_set_cachesize(Pager *pPager, int mxPage){ ** and FULL=3. */ #ifndef SQLITE_OMIT_PAGER_PRAGMAS -void sqlite3pager_set_safety_level(Pager *pPager, int level, int full_fsync){ +void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){ pPager->noSync = level==1 || pPager->tempFile; pPager->fullSync = level==3 && !pPager->tempFile; - pPager->full_fsync = full_fsync; + pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL); if( pPager->noSync ) pPager->needSync = 0; } #endif @@ -1540,33 +2018,37 @@ int sqlite3_opentemp_count = 0; #endif /* -** Open a temporary file. Write the name of the file into zFile -** (zFile must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write -** the file descriptor into *fd. Return SQLITE_OK on success or some -** other error code if we fail. +** Open a temporary file. ** -** The OS will automatically delete the temporary file when it is -** closed. +** Write the file descriptor into *fd. Return SQLITE_OK on success or some +** other error code if we fail. The OS will automatically delete the temporary +** file when it is closed. */ -static int sqlite3pager_opentemp(char *zFile, OsFile **pFd){ - int cnt = 8; +static int sqlite3PagerOpentemp( + sqlite3_vfs *pVfs, /* The virtual file system layer */ + sqlite3_file *pFile, /* Write the file descriptor here */ + char *zFilename, /* Name of the file. Might be NULL */ + int vfsFlags /* Flags passed through to the VFS */ +){ int rc; + assert( zFilename!=0 ); + #ifdef SQLITE_TEST sqlite3_opentemp_count++; /* Used for testing and analysis only */ #endif - do{ - cnt--; - sqlite3OsTempFileName(zFile); - rc = sqlite3OsOpenExclusive(zFile, pFd, 1); - }while( cnt>0 && rc!=SQLITE_OK && rc!=SQLITE_NOMEM ); + + vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | + SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE; + rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0); + assert( rc!=SQLITE_OK || pFile->pMethods ); return rc; } /* ** Create a new page cache and put a pointer to the page cache in *ppPager. ** The file to be cached need not exist. The file is not locked until -** the first call to sqlite3pager_get() and is only held open until the -** last page is released using sqlite3pager_unref(). +** the first call to sqlite3PagerGet() and is only held open until the +** last page is released using sqlite3PagerUnref(). ** ** If zFilename is NULL then a randomly-named temporary file is created ** and used as the file to be cached. The file will be deleted @@ -1576,16 +2058,16 @@ static int sqlite3pager_opentemp(char *zFile, OsFile **pFd){ ** It is never written to disk. This can be used to implement an ** in-memory database. */ -int sqlite3pager_open( +int sqlite3PagerOpen( + sqlite3_vfs *pVfs, /* The virtual file system to use */ Pager **ppPager, /* Return the Pager structure here */ const char *zFilename, /* Name of the database file to open */ int nExtra, /* Extra bytes append to each in-memory page */ - int flags /* flags controlling this file */ + int flags, /* flags controlling this file */ + int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */ ){ + u8 *pPtr; Pager *pPager = 0; - char *zFullPathname = 0; - int nameLen; /* Compiler is wrong. This is always initialized before use */ - OsFile *fd; int rc = SQLITE_OK; int i; int tempFile = 0; @@ -1593,88 +2075,142 @@ int sqlite3pager_open( int readOnly = 0; int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; int noReadlock = (flags & PAGER_NO_READLOCK)!=0; - char zTemp[SQLITE_TEMPNAME_SIZE]; -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to - ** malloc() must have already been made by this thread before it gets - ** to this point. This means the ThreadData must have been allocated already - ** so that ThreadData.nAlloc can be set. It would be nice to assert - ** that ThreadData.nAlloc is non-zero, but alas this breaks test cases - ** written to invoke the pager directly. - */ - ThreadData *pTsd = sqlite3ThreadData(); - assert( pTsd ); -#endif + int journalFileSize = sqlite3JournalSize(pVfs); + int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE; + char *zPathname; + int nPathname; - /* If malloc() has already failed return SQLITE_NOMEM. Before even - ** testing for this, set *ppPager to NULL so the caller knows the pager - ** structure was never allocated. - */ + /* The default return is a NULL pointer */ *ppPager = 0; - if( sqlite3MallocFailed() ){ + + /* Compute the full pathname */ + nPathname = pVfs->mxPathname+1; + zPathname = sqlite3_malloc(nPathname); + if( zPathname==0 ){ return SQLITE_NOMEM; } - memset(&fd, 0, sizeof(fd)); - - /* Open the pager file and set zFullPathname to point at malloc()ed - ** memory containing the complete filename (i.e. including the directory). - */ if( zFilename && zFilename[0] ){ #ifndef SQLITE_OMIT_MEMORYDB if( strcmp(zFilename,":memory:")==0 ){ memDb = 1; - zFullPathname = sqliteStrDup(""); + zPathname[0] = 0; }else #endif { - zFullPathname = sqlite3OsFullPathname(zFilename); - if( zFullPathname ){ - rc = sqlite3OsOpenReadWrite(zFullPathname, &fd, &readOnly); - } + rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); } }else{ - rc = sqlite3pager_opentemp(zTemp, &fd); - zFilename = zTemp; - zFullPathname = sqlite3OsFullPathname(zFilename); - if( rc==SQLITE_OK ){ - tempFile = 1; + rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname); + } + if( rc!=SQLITE_OK ){ + sqlite3_free(zPathname); + return rc; + } + nPathname = strlen(zPathname); + + /* Allocate memory for the pager structure */ + pPager = sqlite3MallocZero( + sizeof(*pPager) + /* Pager structure */ + journalFileSize + /* The journal file structure */ + pVfs->szOsFile * 2 + /* The db and stmt journal files */ + 4*nPathname + 40 /* zFilename, zDirectory, zJournal, zStmtJrnl */ + ); + if( !pPager ){ + sqlite3_free(zPathname); + return SQLITE_NOMEM; + } + pPtr = (u8 *)&pPager[1]; + pPager->vfsFlags = vfsFlags; + pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0]; + pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1]; + pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2]; + pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize]; + pPager->zDirectory = &pPager->zFilename[nPathname+1]; + pPager->zJournal = &pPager->zDirectory[nPathname+1]; + pPager->zStmtJrnl = &pPager->zJournal[nPathname+10]; + pPager->pVfs = pVfs; + memcpy(pPager->zFilename, zPathname, nPathname+1); + sqlite3_free(zPathname); + + /* Open the pager file. + */ + if( zFilename && zFilename[0] && !memDb ){ + if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){ + rc = SQLITE_CANTOPEN; + }else{ + int fout = 0; + rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, + pPager->vfsFlags, &fout); + readOnly = (fout&SQLITE_OPEN_READONLY); + + /* If the file was successfully opened for read/write access, + ** choose a default page size in case we have to create the + ** database file. The default page size is the maximum of: + ** + ** + SQLITE_DEFAULT_PAGE_SIZE, + ** + The value returned by sqlite3OsSectorSize() + ** + The largest page size that can be written atomically. + */ + if( rc==SQLITE_OK && !readOnly ){ + int iSectorSize = sqlite3OsSectorSize(pPager->fd); + if( nDefaultPage<iSectorSize ){ + nDefaultPage = iSectorSize; + } +#ifdef SQLITE_ENABLE_ATOMIC_WRITE + { + int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); + int ii; + assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); + assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); + assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536); + for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){ + if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii; + } + } +#endif + if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ + nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE; + } + } } + }else if( !memDb ){ + /* If a temporary file is requested, it is not opened immediately. + ** In this case we accept the default page size and delay actually + ** opening the file until the first call to OsWrite(). + */ + tempFile = 1; + pPager->state = PAGER_EXCLUSIVE; } - /* Allocate the Pager structure. As part of the same allocation, allocate - ** space for the full paths of the file, directory and journal - ** (Pager.zFilename, Pager.zDirectory and Pager.zJournal). - */ - if( zFullPathname ){ - nameLen = strlen(zFullPathname); - pPager = sqliteMalloc( sizeof(*pPager) + nameLen*3 + 30 ); + if( pPager && rc==SQLITE_OK ){ + pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage); } - /* If an error occured in either of the blocks above, free the memory - ** pointed to by zFullPathname, free the Pager structure and close the - ** file. Since the pager is not allocated there is no need to set + /* If an error occured in either of the blocks above. + ** Free the Pager structure and close the file. + ** Since the pager is not allocated there is no need to set ** any Pager.errMask variables. */ - if( !pPager || !zFullPathname || rc!=SQLITE_OK ){ - sqlite3OsClose(&fd); - sqliteFree(zFullPathname); - sqliteFree(pPager); + if( !pPager || !pPager->pTmpSpace ){ + sqlite3OsClose(pPager->fd); + sqlite3_free(pPager); return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc); } - TRACE3("OPEN %d %s\n", FILEHANDLEID(fd), zFullPathname); - pPager->zFilename = (char*)&pPager[1]; - pPager->zDirectory = &pPager->zFilename[nameLen+1]; - pPager->zJournal = &pPager->zDirectory[nameLen+1]; - strcpy(pPager->zFilename, zFullPathname); - strcpy(pPager->zDirectory, zFullPathname); + PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename); + IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename)) - for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){} + /* Fill in Pager.zDirectory[] */ + memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1); + for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){} if( i>0 ) pPager->zDirectory[i-1] = 0; - strcpy(pPager->zJournal, zFullPathname); - sqliteFree(zFullPathname); - strcpy(&pPager->zJournal[nameLen], "-journal"); - pPager->fd = fd; + + /* Fill in Pager.zJournal[] and Pager.zStmtJrnl[] */ + memcpy(pPager->zJournal, pPager->zFilename, nPathname); + memcpy(&pPager->zJournal[nPathname], "-journal", 9); + memcpy(pPager->zStmtJrnl, pPager->zFilename, nPathname); + memcpy(&pPager->zStmtJrnl[nPathname], "-stmtjrnl", 10); + /* pPager->journalOpen = 0; */ pPager->useJournal = useJournal && !memDb; pPager->noReadlock = noReadlock && readOnly; @@ -1682,32 +2218,52 @@ int sqlite3pager_open( /* pPager->stmtInUse = 0; */ /* pPager->nRef = 0; */ pPager->dbSize = memDb-1; - pPager->pageSize = SQLITE_DEFAULT_PAGE_SIZE; + pPager->pageSize = nDefaultPage; /* pPager->stmtSize = 0; */ /* pPager->stmtJSize = 0; */ /* pPager->nPage = 0; */ - /* pPager->nMaxPage = 0; */ pPager->mxPage = 100; - assert( PAGER_UNLOCK==0 ); + pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; /* pPager->state = PAGER_UNLOCK; */ + assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) ); /* pPager->errMask = 0; */ pPager->tempFile = tempFile; + assert( tempFile==PAGER_LOCKINGMODE_NORMAL + || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); + assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); + pPager->exclusiveMode = tempFile; pPager->memDb = memDb; pPager->readOnly = readOnly; /* pPager->needSync = 0; */ pPager->noSync = pPager->tempFile || !useJournal; pPager->fullSync = (pPager->noSync?0:1); + pPager->sync_flags = SQLITE_SYNC_NORMAL; /* pPager->pFirst = 0; */ /* pPager->pFirstSynced = 0; */ /* pPager->pLast = 0; */ pPager->nExtra = FORCE_ALIGNMENT(nExtra); - pPager->sectorSize = PAGER_SECTOR_SIZE; + assert(pPager->fd->pMethods||memDb||tempFile); + if( !memDb ){ + setSectorSize(pPager); + } /* pPager->pBusyHandler = 0; */ /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ *ppPager = pPager; #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - pPager->pNext = pTsd->pPager; - pTsd->pPager = pPager; + pPager->iInUseMM = 0; + pPager->iInUseDB = 0; + if( !memDb ){ + sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); + sqlite3_mutex_enter(mutex); + pPager->pNext = sqlite3PagerList; + if( sqlite3PagerList ){ + assert( sqlite3PagerList->pPrev==0 ); + sqlite3PagerList->pPrev = pPager; + } + pPager->pPrev = 0; + sqlite3PagerList = pPager; + sqlite3_mutex_leave(mutex); + } #endif return SQLITE_OK; } @@ -1715,7 +2271,7 @@ int sqlite3pager_open( /* ** Set the busy handler function. */ -void sqlite3pager_set_busyhandler(Pager *pPager, BusyHandler *pBusyHandler){ +void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){ pPager->pBusyHandler = pBusyHandler; } @@ -1724,10 +2280,10 @@ void sqlite3pager_set_busyhandler(Pager *pPager, BusyHandler *pBusyHandler){ ** when the reference count on each page reaches zero. The destructor can ** be used to clean up information in the extra segment appended to each page. ** -** The destructor is not called as a result sqlite3pager_close(). -** Destructors are only called by sqlite3pager_unref(). +** The destructor is not called as a result sqlite3PagerClose(). +** Destructors are only called by sqlite3PagerUnref(). */ -void sqlite3pager_set_destructor(Pager *pPager, void (*xDesc)(void*,int)){ +void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){ pPager->xDestructor = xDesc; } @@ -1738,21 +2294,64 @@ void sqlite3pager_set_destructor(Pager *pPager, void (*xDesc)(void*,int)){ ** an opportunity to restore the EXTRA section to agree with the restored ** page data. */ -void sqlite3pager_set_reiniter(Pager *pPager, void (*xReinit)(void*,int)){ +void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){ pPager->xReiniter = xReinit; } /* -** Set the page size. Return the new size. If the suggest new page -** size is inappropriate, then an alternative page size is selected -** and returned. +** Set the page size to *pPageSize. If the suggest new page size is +** inappropriate, then an alternative page size is set to that +** value before returning. */ -int sqlite3pager_set_pagesize(Pager *pPager, int pageSize){ - assert( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE ); - if( !pPager->memDb ){ - pPager->pageSize = pageSize; +int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){ + int rc = SQLITE_OK; + u16 pageSize = *pPageSize; + assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) ); + if( pageSize && pageSize!=pPager->pageSize + && !pPager->memDb && pPager->nRef==0 + ){ + char *pNew = (char *)sqlite3_malloc(pageSize); + if( !pNew ){ + rc = SQLITE_NOMEM; + }else{ + pagerEnter(pPager); + pager_reset(pPager); + pPager->pageSize = pageSize; + setSectorSize(pPager); + sqlite3_free(pPager->pTmpSpace); + pPager->pTmpSpace = pNew; + pagerLeave(pPager); + } } - return pPager->pageSize; + *pPageSize = pPager->pageSize; + return rc; +} + +/* +** Return a pointer to the "temporary page" buffer held internally +** by the pager. This is a buffer that is big enough to hold the +** entire content of a database page. This buffer is used internally +** during rollback and will be overwritten whenever a rollback +** occurs. But other modules are free to use it too, as long as +** no rollbacks are happening. +*/ +void *sqlite3PagerTempSpace(Pager *pPager){ + return pPager->pTmpSpace; +} + +/* +** Attempt to set the maximum database page count if mxPage is positive. +** Make no changes if mxPage is zero or negative. And never reduce the +** maximum page count below the current size of the database. +** +** Regardless of mxPage, return the current maximum page count. +*/ +int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ + if( mxPage>0 ){ + pPager->mxPgno = mxPage; + } + sqlite3PagerPagecount(pPager); + return pPager->mxPgno; } /* @@ -1767,9 +2366,6 @@ int sqlite3pager_set_pagesize(Pager *pPager, int pageSize){ extern int sqlite3_io_error_pending; extern int sqlite3_io_error_hit; static int saved_cnt; -void clear_simulated_io_error(){ - sqlite3_io_error_hit = 0; -} void disable_simulated_io_errors(void){ saved_cnt = sqlite3_io_error_pending; sqlite3_io_error_pending = -1; @@ -1778,7 +2374,6 @@ void enable_simulated_io_errors(void){ sqlite3_io_error_pending = saved_cnt; } #else -# define clear_simulated_io_error() # define disable_simulated_io_errors() # define enable_simulated_io_errors() #endif @@ -1793,14 +2388,18 @@ void enable_simulated_io_errors(void){ ** response is to zero the memory at pDest and continue. A real IO error ** will presumably recur and be picked up later (Todo: Think about this). */ -void sqlite3pager_read_fileheader(Pager *pPager, int N, unsigned char *pDest){ +int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){ + int rc = SQLITE_OK; memset(pDest, 0, N); - if( MEMDB==0 ){ - disable_simulated_io_errors(); - sqlite3OsSeek(pPager->fd, 0); - sqlite3OsRead(pPager->fd, pDest, N); - enable_simulated_io_errors(); + assert(MEMDB||pPager->fd->pMethods||pPager->tempFile); + if( pPager->fd->pMethods ){ + IOTRACE(("DBHDR %p 0 %d\n", pPager, N)) + rc = sqlite3OsRead(pPager->fd, pDest, N, 0); + if( rc==SQLITE_IOERR_SHORT_READ ){ + rc = SQLITE_OK; + } } + return rc; } /* @@ -1812,14 +2411,22 @@ void sqlite3pager_read_fileheader(Pager *pPager, int N, unsigned char *pDest){ ** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the ** file is 4096 bytes, 5 is returned instead of 4. */ -int sqlite3pager_pagecount(Pager *pPager){ - i64 n; +int sqlite3PagerPagecount(Pager *pPager){ + i64 n = 0; + int rc; assert( pPager!=0 ); + if( pPager->errCode ){ + return 0; + } if( pPager->dbSize>=0 ){ n = pPager->dbSize; } else { - if( sqlite3OsFileSize(pPager->fd, &n)!=SQLITE_OK ){ - pager_error(pPager, SQLITE_IOERR); + assert(pPager->fd->pMethods||pPager->tempFile); + if( (pPager->fd->pMethods) + && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){ + pPager->nRef++; + pager_error(pPager, rc); + pPager->nRef--; return 0; } if( n>0 && n<pPager->pageSize ){ @@ -1834,6 +2441,9 @@ int sqlite3pager_pagecount(Pager *pPager){ if( n==(PENDING_BYTE/pPager->pageSize) ){ n++; } + if( n>pPager->mxPgno ){ + pPager->mxPgno = n; + } return n; } @@ -1843,8 +2453,8 @@ int sqlite3pager_pagecount(Pager *pPager){ ** Clear a PgHistory block */ static void clearHistory(PgHistory *pHist){ - sqliteFree(pHist->pOrig); - sqliteFree(pHist->pStmt); + sqlite3_free(pHist->pOrig); + sqlite3_free(pHist->pStmt); pHist->pOrig = 0; pHist->pStmt = 0; } @@ -1858,9 +2468,9 @@ static void clearHistory(PgHistory *pHist){ static int syncJournal(Pager*); /* -** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate +** Unlink pPg from its hash chain. Also set the page number to 0 to indicate ** that the page is not part of any hash chain. This is required because the -** sqlite3pager_movepage() routine can leave a page in the +** sqlite3PagerMovepage() routine can leave a page in the ** pNextFree/pPrevFree list that is not a part of any hash-chain. */ static void unlinkHashChain(Pager *pPager, PgHdr *pPg){ @@ -1892,39 +2502,26 @@ static void unlinkHashChain(Pager *pPager, PgHdr *pPg){ static void unlinkPage(PgHdr *pPg){ Pager *pPager = pPg->pPager; - /* Keep the pFirstSynced pointer pointing at the first synchronized page */ - if( pPg==pPager->pFirstSynced ){ - PgHdr *p = pPg->pNextFree; - while( p && p->needSync ){ p = p->pNextFree; } - pPager->pFirstSynced = p; - } - - /* Unlink from the freelist */ - if( pPg->pPrevFree ){ - pPg->pPrevFree->pNextFree = pPg->pNextFree; - }else{ - assert( pPager->pFirst==pPg ); - pPager->pFirst = pPg->pNextFree; - } - if( pPg->pNextFree ){ - pPg->pNextFree->pPrevFree = pPg->pPrevFree; - }else{ - assert( pPager->pLast==pPg ); - pPager->pLast = pPg->pPrevFree; - } - pPg->pNextFree = pPg->pPrevFree = 0; + /* Unlink from free page list */ + lruListRemove(pPg); /* Unlink from the pgno hash table */ unlinkHashChain(pPager, pPg); } -#ifndef SQLITE_OMIT_MEMORYDB /* -** This routine is used to truncate an in-memory database. Delete -** all pages whose pgno is larger than pPager->dbSize and is unreferenced. +** This routine is used to truncate the cache when a database +** is truncated. Drop from the cache all pages whose pgno is +** larger than pPager->dbSize and is unreferenced. +** ** Referenced pages larger than pPager->dbSize are zeroed. +** +** Actually, at the point this routine is called, it would be +** an error to have a referenced page. But rather than delete +** that page and guarantee a subsequent segfault, it seems better +** to zero it and hope that we error out sanely. */ -static void memoryTruncate(Pager *pPager){ +static void pager_truncate_cache(Pager *pPager){ PgHdr *pPg; PgHdr **ppPg; int dbSize = pPager->dbSize; @@ -1938,16 +2535,15 @@ static void memoryTruncate(Pager *pPager){ ppPg = &pPg->pNextAll; }else{ *ppPg = pPg->pNextAll; + IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno)); + PAGER_INCR(sqlite3_pager_pgfree_count); unlinkPage(pPg); makeClean(pPg); - sqliteFree(pPg); + sqlite3_free(pPg); pPager->nPage--; } } } -#else -#define memoryTruncate(p) -#endif /* ** Try to obtain a lock on a file. Invoke the busy callback if the lock @@ -1959,9 +2555,15 @@ static void memoryTruncate(Pager *pPager){ */ static int pager_wait_on_lock(Pager *pPager, int locktype){ int rc; + + /* The OS lock values must be the same as the Pager lock values */ assert( PAGER_SHARED==SHARED_LOCK ); assert( PAGER_RESERVED==RESERVED_LOCK ); assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK ); + + /* If the file is currently unlocked then the size must be unknown */ + assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB ); + if( pPager->state>=locktype ){ rc = SQLITE_OK; }else{ @@ -1970,6 +2572,7 @@ static int pager_wait_on_lock(Pager *pPager, int locktype){ }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) ); if( rc==SQLITE_OK ){ pPager->state = locktype; + IOTRACE(("LOCK %p %d\n", pPager, locktype)) } } return rc; @@ -1978,9 +2581,10 @@ static int pager_wait_on_lock(Pager *pPager, int locktype){ /* ** Truncate the file to the number of pages specified. */ -int sqlite3pager_truncate(Pager *pPager, Pgno nPage){ +int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){ int rc; - sqlite3pager_pagecount(pPager); + assert( pPager->state>=PAGER_SHARED || MEMDB ); + sqlite3PagerPagecount(pPager); if( pPager->errCode ){ rc = pPager->errCode; return rc; @@ -1990,24 +2594,25 @@ int sqlite3pager_truncate(Pager *pPager, Pgno nPage){ } if( MEMDB ){ pPager->dbSize = nPage; - memoryTruncate(pPager); + pager_truncate_cache(pPager); return SQLITE_OK; } + pagerEnter(pPager); rc = syncJournal(pPager); + pagerLeave(pPager); if( rc!=SQLITE_OK ){ return rc; } /* Get an exclusive lock on the database before truncating. */ + pagerEnter(pPager); rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); + pagerLeave(pPager); if( rc!=SQLITE_OK ){ return rc; } rc = pager_truncate(pPager, nPage); - if( rc==SQLITE_OK ){ - pPager->dbSize = nPage; - } return rc; } @@ -2025,99 +2630,60 @@ int sqlite3pager_truncate(Pager *pPager, Pgno nPage){ ** a hot journal may be left in the filesystem but no error is returned ** to the caller. */ -int sqlite3pager_close(Pager *pPager){ - PgHdr *pPg, *pNext; +int sqlite3PagerClose(Pager *pPager){ #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to - ** malloc() must have already been made by this thread before it gets - ** to this point. This means the ThreadData must have been allocated already - ** so that ThreadData.nAlloc can be set. - */ - ThreadData *pTsd = sqlite3ThreadData(); - assert( pPager ); - assert( pTsd && pTsd->nAlloc ); -#endif - - switch( pPager->state ){ - case PAGER_RESERVED: - case PAGER_SYNCED: - case PAGER_EXCLUSIVE: { - /* We ignore any IO errors that occur during the rollback - ** operation. So disable IO error simulation so that testing - ** works more easily. - */ - disable_simulated_io_errors(); - sqlite3pager_rollback(pPager); - enable_simulated_io_errors(); - if( !MEMDB ){ - sqlite3OsUnlock(pPager->fd, NO_LOCK); - } - assert( pPager->errCode || pPager->journalOpen==0 ); - break; - } - case PAGER_SHARED: { - if( !MEMDB ){ - sqlite3OsUnlock(pPager->fd, NO_LOCK); - } - break; + if( !MEMDB ){ + sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); + sqlite3_mutex_enter(mutex); + if( pPager->pPrev ){ + pPager->pPrev->pNext = pPager->pNext; + }else{ + sqlite3PagerList = pPager->pNext; } - default: { - /* Do nothing */ - break; + if( pPager->pNext ){ + pPager->pNext->pPrev = pPager->pPrev; } + sqlite3_mutex_leave(mutex); } - for(pPg=pPager->pAll; pPg; pPg=pNext){ -#ifndef NDEBUG - if( MEMDB ){ - PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); - assert( !pPg->alwaysRollback ); - assert( !pHist->pOrig ); - assert( !pHist->pStmt ); - } #endif - pNext = pPg->pNextAll; - sqliteFree(pPg); - } - TRACE2("CLOSE %d\n", PAGERID(pPager)); + + disable_simulated_io_errors(); + pPager->errCode = 0; + pPager->exclusiveMode = 0; + pager_reset(pPager); + pagerUnlockAndRollback(pPager); + enable_simulated_io_errors(); + PAGERTRACE2("CLOSE %d\n", PAGERID(pPager)); + IOTRACE(("CLOSE %p\n", pPager)) assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) ); if( pPager->journalOpen ){ - sqlite3OsClose(&pPager->jfd); + sqlite3OsClose(pPager->jfd); } - sqliteFree(pPager->aInJournal); + sqlite3_free(pPager->aInJournal); if( pPager->stmtOpen ){ - sqlite3OsClose(&pPager->stfd); + sqlite3OsClose(pPager->stfd); } - sqlite3OsClose(&pPager->fd); + sqlite3OsClose(pPager->fd); /* Temp files are automatically deleted by the OS ** if( pPager->tempFile ){ ** sqlite3OsDelete(pPager->zFilename); ** } */ -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - /* Remove the pager from the linked list of pagers starting at - ** ThreadData.pPager if memory-management is enabled. - */ - if( pPager==pTsd->pPager ){ - pTsd->pPager = pPager->pNext; - }else{ - Pager *pTmp; - for(pTmp = pTsd->pPager; pTmp->pNext!=pPager; pTmp=pTmp->pNext){} - pTmp->pNext = pPager->pNext; - } -#endif - sqliteFree(pPager->aHash); - sqliteFree(pPager); + sqlite3_free(pPager->aHash); + sqlite3_free(pPager->pTmpSpace); + sqlite3_free(pPager); return SQLITE_OK; } +#if !defined(NDEBUG) || defined(SQLITE_TEST) /* ** Return the page number for the given page data. */ -Pgno sqlite3pager_pagenumber(void *pData){ - PgHdr *p = DATA_TO_PGHDR(pData); +Pgno sqlite3PagerPagenumber(DbPage *p){ return p->pgno; } +#endif /* ** The page_ref() function increments the reference count for a page. @@ -2131,25 +2697,10 @@ Pgno sqlite3pager_pagenumber(void *pData){ static void _page_ref(PgHdr *pPg){ if( pPg->nRef==0 ){ /* The page is currently on the freelist. Remove it. */ - if( pPg==pPg->pPager->pFirstSynced ){ - PgHdr *p = pPg->pNextFree; - while( p && p->needSync ){ p = p->pNextFree; } - pPg->pPager->pFirstSynced = p; - } - if( pPg->pPrevFree ){ - pPg->pPrevFree->pNextFree = pPg->pNextFree; - }else{ - pPg->pPager->pFirst = pPg->pNextFree; - } - if( pPg->pNextFree ){ - pPg->pNextFree->pPrevFree = pPg->pPrevFree; - }else{ - pPg->pPager->pLast = pPg->pPrevFree; - } + lruListRemove(pPg); pPg->pPager->nRef++; } pPg->nRef++; - REFINFO(pPg); } #ifdef SQLITE_DEBUG static void page_ref(PgHdr *pPg){ @@ -2157,7 +2708,6 @@ static void _page_ref(PgHdr *pPg){ _page_ref(pPg); }else{ pPg->nRef++; - REFINFO(pPg); } } #else @@ -2168,9 +2718,10 @@ static void _page_ref(PgHdr *pPg){ ** Increment the reference count for a page. The input pointer is ** a reference to the page data. */ -int sqlite3pager_ref(void *pData){ - PgHdr *pPg = DATA_TO_PGHDR(pData); +int sqlite3PagerRef(DbPage *pPg){ + pagerEnter(pPg->pPager); page_ref(pPg); + pagerLeave(pPg->pPager); return SQLITE_OK; } @@ -2189,7 +2740,12 @@ int sqlite3pager_ref(void *pData){ ** is synced, then the nRec field is updated, then a second sync occurs. ** ** For temporary databases, we do not care if we are able to rollback -** after a power failure, so sync occurs. +** after a power failure, so no sync occurs. +** +** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which +** the database is stored, then OsSync() is never called on the journal +** file. In this case all that is required is to update the nRec field in +** the journal header. ** ** This routine clears the needSync field of every page current held in ** memory. @@ -2198,12 +2754,15 @@ static int syncJournal(Pager *pPager){ PgHdr *pPg; int rc = SQLITE_OK; + /* Sync the journal before modifying the main database ** (assuming there is a journal and it needs to be synced.) */ if( pPager->needSync ){ if( !pPager->tempFile ){ + int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); assert( pPager->journalOpen ); + /* assert( !pPager->noSync ); // noSync might be set if synchronous ** was turned off after the transaction was started. Ticket #615 */ #ifndef NDEBUG @@ -2217,29 +2776,39 @@ static int syncJournal(Pager *pPager){ assert( pPager->journalOff==jSz ); } #endif - { + if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ /* Write the nRec value into the journal file header. If in ** full-synchronous mode, sync the journal first. This ensures that ** all data has really hit the disk before nRec is updated to mark - ** it as a candidate for rollback. + ** it as a candidate for rollback. + ** + ** This is not required if the persistent media supports the + ** SAFE_APPEND property. Because in this case it is not possible + ** for garbage data to be appended to the file, the nRec field + ** is populated with 0xFFFFFFFF when the journal header is written + ** and never needs to be updated. */ - if( pPager->fullSync ){ - TRACE2("SYNC journal of %d\n", PAGERID(pPager)); - rc = sqlite3OsSync(pPager->jfd, 0); + i64 jrnlOff; + if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ + PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); + IOTRACE(("JSYNC %p\n", pPager)) + rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags); if( rc!=0 ) return rc; } - rc = sqlite3OsSeek(pPager->jfd, - pPager->journalHdr + sizeof(aJournalMagic)); - if( rc ) return rc; - rc = write32bits(pPager->jfd, pPager->nRec); - if( rc ) return rc; - rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff); + jrnlOff = pPager->journalHdr + sizeof(aJournalMagic); + IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4)); + rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec); if( rc ) return rc; } - TRACE2("SYNC journal of %d\n", PAGERID(pPager)); - rc = sqlite3OsSync(pPager->jfd, pPager->full_fsync); - if( rc!=0 ) return rc; + if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ + PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); + IOTRACE(("JSYNC %p\n", pPager)) + rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| + (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) + ); + if( rc!=0 ) return rc; + } pPager->journalStarted = 1; } pPager->needSync = 0; @@ -2249,7 +2818,7 @@ static int syncJournal(Pager *pPager){ for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ pPg->needSync = 0; } - pPager->pFirstSynced = pPager->pFirst; + lruListSetFirstSynced(pPager); } #ifndef NDEBUG @@ -2261,7 +2830,7 @@ static int syncJournal(Pager *pPager){ for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ assert( pPg->needSync==0 ); } - assert( pPager->pFirstSynced==pPager->pFirst ); + assert( pPager->lru.pFirstSynced==pPager->lru.pFirst ); } #endif @@ -2301,9 +2870,16 @@ static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){ ** connected by pDirty pointers. The pPrevDirty pointers are ** corrupted by this sort. */ -#define N_SORT_BUCKET 25 +#define N_SORT_BUCKET_ALLOC 25 +#define N_SORT_BUCKET 25 +#ifdef SQLITE_TEST + int sqlite3_pager_n_sort_bucket = 0; + #undef N_SORT_BUCKET + #define N_SORT_BUCKET \ + (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC) +#endif static PgHdr *sort_pagelist(PgHdr *pIn){ - PgHdr *a[N_SORT_BUCKET], *p; + PgHdr *a[N_SORT_BUCKET_ALLOC], *p; int i; memset(a, 0, sizeof(a)); while( pIn ){ @@ -2320,6 +2896,11 @@ static PgHdr *sort_pagelist(PgHdr *pIn){ } } if( i==N_SORT_BUCKET-1 ){ + /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET) + ** elements in the input list. This is possible, but impractical. + ** Testing this line is the point of global variable + ** sqlite3_pager_n_sort_bucket. + */ a[i] = merge_pagelist(a[i], p); } } @@ -2337,6 +2918,7 @@ static PgHdr *sort_pagelist(PgHdr *pIn){ */ static int pager_write_pagelist(PgHdr *pList){ Pager *pPager; + PgHdr *p; int rc; if( pList==0 ) return SQLITE_OK; @@ -2364,28 +2946,44 @@ static int pager_write_pagelist(PgHdr *pList){ } pList = sort_pagelist(pList); + for(p=pList; p; p=p->pDirty){ + assert( p->dirty ); + p->dirty = 0; + } while( pList ){ - assert( pList->dirty ); - rc = sqlite3OsSeek(pPager->fd, (pList->pgno-1)*(i64)pPager->pageSize); - if( rc ) return rc; + + /* If the file has not yet been opened, open it now. */ + if( !pPager->fd->pMethods ){ + assert(pPager->tempFile); + rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename, + pPager->vfsFlags); + if( rc ) return rc; + } + /* If there are dirty pages in the page cache with page numbers greater - ** than Pager.dbSize, this means sqlite3pager_truncate() was called to + ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to ** make the file smaller (presumably by auto-vacuum code). Do not write ** any such pages to the file. */ if( pList->pgno<=pPager->dbSize ){ + i64 offset = (pList->pgno-1)*(i64)pPager->pageSize; char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6); - TRACE3("STORE %d page %d\n", PAGERID(pPager), pList->pgno); - rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize); - TEST_INCR(pPager->nWrite); + PAGERTRACE4("STORE %d page %d hash(%08x)\n", + PAGERID(pPager), pList->pgno, pager_pagehash(pList)); + IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno)); + rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); + PAGER_INCR(sqlite3_pager_writedb_count); + PAGER_INCR(pPager->nWrite); + if( pList->pgno==1 ){ + memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); + } } #ifndef NDEBUG else{ - TRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno); + PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno); } #endif if( rc ) return rc; - pList->dirty = 0; #ifdef SQLITE_CHECK_PAGES pList->pageHash = pager_pagehash(pList); #endif @@ -2412,11 +3010,17 @@ static PgHdr *pager_get_all_dirty_pages(Pager *pPager){ ** database with the same name. Just delete the journal. */ static int hasHotJournal(Pager *pPager){ + sqlite3_vfs *pVfs = pPager->pVfs; if( !pPager->useJournal ) return 0; - if( !sqlite3OsFileExists(pPager->zJournal) ) return 0; - if( sqlite3OsCheckReservedLock(pPager->fd) ) return 0; - if( sqlite3pager_pagecount(pPager)==0 ){ - sqlite3OsDelete(pPager->zJournal); + if( !pPager->fd->pMethods ) return 0; + if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){ + return 0; + } + if( sqlite3OsCheckReservedLock(pPager->fd) ){ + return 0; + } + if( sqlite3PagerPagecount(pPager)==0 ){ + sqlite3OsDelete(pVfs, pPager->zJournal, 0); return 0; }else{ return 1; @@ -2429,26 +3033,33 @@ static int hasHotJournal(Pager *pPager){ ** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It ** does not set the pPager->errCode variable. */ -static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){ +static int pager_recycle(Pager *pPager, PgHdr **ppPg){ PgHdr *pPg; *ppPg = 0; + /* It is illegal to call this function unless the pager object + ** pointed to by pPager has at least one free page (page with nRef==0). + */ + assert(!MEMDB); + assert(pPager->lru.pFirst); + /* Find a page to recycle. Try to locate a page that does not ** require us to do an fsync() on the journal. */ - pPg = pPager->pFirstSynced; + pPg = pPager->lru.pFirstSynced; /* If we could not find a page that does not require an fsync() ** on the journal file then fsync the journal file. This is a ** very slow operation, so we work hard to avoid it. But sometimes ** it can't be helped. */ - if( pPg==0 && pPager->pFirst && syncOk && !MEMDB){ + if( pPg==0 && pPager->lru.pFirst){ + int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); int rc = syncJournal(pPager); if( rc!=0 ){ return rc; } - if( pPager->fullSync ){ + if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ /* If in full-sync mode, write a new journal header into the ** journal file. This is done to avoid ever modifying a journal ** header that is involved in the rollback of pages that have @@ -2457,15 +3068,13 @@ static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){ */ pPager->nRec = 0; assert( pPager->journalOff > 0 ); + assert( pPager->doNotSync==0 ); rc = writeJournalHdr(pPager); if( rc!=0 ){ return rc; } } - pPg = pPager->pFirst; - } - if( pPg==0 ){ - return SQLITE_OK; + pPg = pPager->lru.pFirst; } assert( pPg->nRef==0 ); @@ -2479,6 +3088,7 @@ static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){ pPg->dirty = 1; pPg->pDirty = 0; rc = pager_write_pagelist( pPg ); + pPg->dirty = 0; if( rc!=SQLITE_OK ){ return rc; } @@ -2487,107 +3097,444 @@ static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){ /* If the page we are recycling is marked as alwaysRollback, then ** set the global alwaysRollback flag, thus disabling the - ** sqlite_dont_rollback() optimization for the rest of this transaction. + ** sqlite3PagerDontRollback() optimization for the rest of this transaction. ** It is necessary to do this because the page marked alwaysRollback ** might be reloaded at a later time but at that point we won't remember ** that is was marked alwaysRollback. This means that all pages must ** be marked as alwaysRollback from here on out. */ if( pPg->alwaysRollback ){ + IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager)) pPager->alwaysRollback = 1; } /* Unlink the old page from the free list and the hash table */ unlinkPage(pPg); - TEST_INCR(pPager->nOvfl); + assert( pPg->pgno==0 ); *ppPg = pPg; return SQLITE_OK; } +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT /* ** This function is called to free superfluous dynamically allocated memory ** held by the pager system. Memory in use by any SQLite pager allocated -** by the current thread may be sqliteFree()ed. +** by the current thread may be sqlite3_free()ed. ** ** nReq is the number of bytes of memory required. Once this much has -** been released, the function returns. A negative value for nReq means -** free as much memory as possible. The return value is the total number +** been released, the function returns. The return value is the total number ** of bytes of memory released. */ -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT -int sqlite3pager_release_memory(int nReq){ - const ThreadData *pTsdro = sqlite3ThreadDataReadOnly(); - Pager *p; - int nReleased = 0; - int i; +int sqlite3PagerReleaseMemory(int nReq){ + int nReleased = 0; /* Bytes of memory released so far */ + sqlite3_mutex *mutex; /* The MEM2 mutex */ + Pager *pPager; /* For looping over pagers */ + BusyHandler *savedBusy; /* Saved copy of the busy handler */ + int rc = SQLITE_OK; - /* If the the global mutex is held, this subroutine becomes a - ** o-op; zero bytes of memory are freed. This is because - ** some of the code invoked by this function may also - ** try to obtain the mutex, resulting in a deadlock. + /* Acquire the memory-management mutex */ - if( sqlite3OsInMutex(0) ){ - return 0; + mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); + sqlite3_mutex_enter(mutex); + + /* Signal all database connections that memory management wants + ** to have access to the pagers. + */ + for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){ + pPager->iInUseMM = 1; + } + + while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){ + PgHdr *pPg; + PgHdr *pRecycled; + + /* Try to find a page to recycle that does not require a sync(). If + ** this is not possible, find one that does require a sync(). + */ + sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); + pPg = sqlite3LruPageList.pFirstSynced; + while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){ + pPg = pPg->gfree.pNext; + } + if( !pPg ){ + pPg = sqlite3LruPageList.pFirst; + while( pPg && pPg->pPager->iInUseDB ){ + pPg = pPg->gfree.pNext; + } + } + sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); + + /* If pPg==0, then the block above has failed to find a page to + ** recycle. In this case return early - no further memory will + ** be released. + */ + if( !pPg ) break; + + pPager = pPg->pPager; + assert(!pPg->needSync || pPg==pPager->lru.pFirst); + assert(pPg->needSync || pPg==pPager->lru.pFirstSynced); + + savedBusy = pPager->pBusyHandler; + pPager->pBusyHandler = 0; + rc = pager_recycle(pPager, &pRecycled); + pPager->pBusyHandler = savedBusy; + assert(pRecycled==pPg || rc!=SQLITE_OK); + if( rc==SQLITE_OK ){ + /* We've found a page to free. At this point the page has been + ** removed from the page hash-table, free-list and synced-list + ** (pFirstSynced). It is still in the all pages (pAll) list. + ** Remove it from this list before freeing. + ** + ** Todo: Check the Pager.pStmt list to make sure this is Ok. It + ** probably is though. + */ + PgHdr *pTmp; + assert( pPg ); + if( pPg==pPager->pAll ){ + pPager->pAll = pPg->pNextAll; + }else{ + for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){} + pTmp->pNextAll = pPg->pNextAll; + } + nReleased += ( + sizeof(*pPg) + pPager->pageSize + + sizeof(u32) + pPager->nExtra + + MEMDB*sizeof(PgHistory) + ); + IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno)); + PAGER_INCR(sqlite3_pager_pgfree_count); + sqlite3_free(pPg); + pPager->nPage--; + }else{ + /* An error occured whilst writing to the database file or + ** journal in pager_recycle(). The error is not returned to the + ** caller of this function. Instead, set the Pager.errCode variable. + ** The error will be returned to the user (or users, in the case + ** of a shared pager cache) of the pager for which the error occured. + */ + assert( + (rc&0xff)==SQLITE_IOERR || + rc==SQLITE_FULL || + rc==SQLITE_BUSY + ); + assert( pPager->state>=PAGER_RESERVED ); + pager_error(pPager, rc); + } } - /* Outermost loop runs for at most two iterations. First iteration we - ** try to find memory that can be released without calling fsync(). Second - ** iteration (which only runs if the first failed to free nReq bytes of - ** memory) is permitted to call fsync(). This is of course much more - ** expensive. + /* Clear the memory management flags and release the mutex */ - for(i=0; i<=1; i++){ + for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){ + pPager->iInUseMM = 0; + } + sqlite3_mutex_leave(mutex); - /* Loop through all the SQLite pagers opened by the current thread. */ - for(p=pTsdro->pPager; p && (nReq<0 || nReleased<nReq); p=p->pNext){ - PgHdr *pPg; - int rc; + /* Return the number of bytes released + */ + return nReleased; +} +#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ + +/* +** Read the content of page pPg out of the database file. +*/ +static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){ + int rc; + i64 offset; + assert( MEMDB==0 ); + assert(pPager->fd->pMethods||pPager->tempFile); + if( !pPager->fd->pMethods ){ + return SQLITE_IOERR_SHORT_READ; + } + offset = (pgno-1)*(i64)pPager->pageSize; + rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset); + PAGER_INCR(sqlite3_pager_readdb_count); + PAGER_INCR(pPager->nRead); + IOTRACE(("PGIN %p %d\n", pPager, pgno)); + if( pgno==1 ){ + memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24], + sizeof(pPager->dbFileVers)); + } + CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3); + PAGERTRACE4("FETCH %d page %d hash(%08x)\n", + PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)); + return rc; +} - /* For each pager, try to free as many pages as possible (without - ** calling fsync() if this is the first iteration of the outermost - ** loop). + +/* +** This function is called to obtain the shared lock required before +** data may be read from the pager cache. If the shared lock has already +** been obtained, this function is a no-op. +** +** Immediately after obtaining the shared lock (if required), this function +** checks for a hot-journal file. If one is found, an emergency rollback +** is performed immediately. +*/ +static int pagerSharedLock(Pager *pPager){ + int rc = SQLITE_OK; + int isHot = 0; + + /* If this database is opened for exclusive access, has no outstanding + ** page references and is in an error-state, now is the chance to clear + ** the error. Discard the contents of the pager-cache and treat any + ** open journal file as a hot-journal. + */ + if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){ + if( pPager->journalOpen ){ + isHot = 1; + } + pager_reset(pPager); + pPager->errCode = SQLITE_OK; + } + + /* If the pager is still in an error state, do not proceed. The error + ** state will be cleared at some point in the future when all page + ** references are dropped and the cache can be discarded. + */ + if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ + return pPager->errCode; + } + + if( pPager->state==PAGER_UNLOCK || isHot ){ + sqlite3_vfs *pVfs = pPager->pVfs; + if( !MEMDB ){ + assert( pPager->nRef==0 ); + if( !pPager->noReadlock ){ + rc = pager_wait_on_lock(pPager, SHARED_LOCK); + if( rc!=SQLITE_OK ){ + return pager_error(pPager, rc); + } + assert( pPager->state>=SHARED_LOCK ); + } + + /* If a journal file exists, and there is no RESERVED lock on the + ** database file, then it either needs to be played back or deleted. */ - while( SQLITE_OK==(rc = pager_recycle(p, i, &pPg)) && pPg) { - /* We've found a page to free. At this point the page has been - ** removed from the page hash-table, free-list and synced-list - ** (pFirstSynced). It is still in the all pages (pAll) list. - ** Remove it from this list before freeing. + if( hasHotJournal(pPager) || isHot ){ + /* Get an EXCLUSIVE lock on the database file. At this point it is + ** important that a RESERVED lock is not obtained on the way to the + ** EXCLUSIVE lock. If it were, another process might open the + ** database file, detect the RESERVED lock, and conclude that the + ** database is safe to read while this process is still rolling it + ** back. + ** + ** Because the intermediate RESERVED lock is not requested, the + ** second process will get to this point in the code and fail to + ** obtain its own EXCLUSIVE lock on the database file. + */ + if( pPager->state<EXCLUSIVE_LOCK ){ + rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK); + if( rc!=SQLITE_OK ){ + pager_unlock(pPager); + return pager_error(pPager, rc); + } + pPager->state = PAGER_EXCLUSIVE; + } + + /* Open the journal for reading only. Return SQLITE_BUSY if + ** we are unable to open the journal file. + ** + ** The journal file does not need to be locked itself. The + ** journal file is never open unless the main database file holds + ** a write lock, so there is never any chance of two or more + ** processes opening the journal at the same time. ** - ** Todo: Check the Pager.pStmt list to make sure this is Ok. It - ** probably is though. + ** Open the journal for read/write access. This is because in + ** exclusive-access mode the file descriptor will be kept open and + ** possibly used for a transaction later on. On some systems, the + ** OsTruncate() call used in exclusive-access mode also requires + ** a read/write file handle. */ - PgHdr *pTmp; - assert( pPg ); - page_remove_from_stmt_list(pPg); - if( pPg==p->pAll ){ - p->pAll = pPg->pNextAll; - }else{ - for( pTmp=p->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){} - pTmp->pNextAll = pPg->pNextAll; + if( !isHot ){ + rc = SQLITE_BUSY; + if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){ + int fout = 0; + int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL; + assert( !pPager->tempFile ); + rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout); + assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); + if( fout&SQLITE_OPEN_READONLY ){ + rc = SQLITE_BUSY; + sqlite3OsClose(pPager->jfd); + } + } + } + if( rc!=SQLITE_OK ){ + pager_unlock(pPager); + return ((rc==SQLITE_NOMEM||rc==SQLITE_IOERR_NOMEM)?rc:SQLITE_BUSY); + } + pPager->journalOpen = 1; + pPager->journalStarted = 0; + pPager->journalOff = 0; + pPager->setMaster = 0; + pPager->journalHdr = 0; + + /* Playback and delete the journal. Drop the database write + ** lock and reacquire the read lock. + */ + rc = pager_playback(pPager, 1); + if( rc!=SQLITE_OK ){ + return pager_error(pPager, rc); } - nReleased += sqliteAllocSize(pPg); - sqliteFree(pPg); + assert(pPager->state==PAGER_SHARED || + (pPager->exclusiveMode && pPager->state>PAGER_SHARED) + ); } - if( rc!=SQLITE_OK ){ - /* An error occured whilst writing to the database file or - ** journal in pager_recycle(). The error is not returned to the - ** caller of this function. Instead, set the Pager.errCode variable. - ** The error will be returned to the user (or users, in the case - ** of a shared pager cache) of the pager for which the error occured. + if( pPager->pAll ){ + /* The shared-lock has just been acquired on the database file + ** and there are already pages in the cache (from a previous + ** read or write transaction). Check to see if the database + ** has been modified. If the database has changed, flush the + ** cache. + ** + ** Database changes is detected by looking at 15 bytes beginning + ** at offset 24 into the file. The first 4 of these 16 bytes are + ** a 32-bit counter that is incremented with each change. The + ** other bytes change randomly with each file change when + ** a codec is in use. + ** + ** There is a vanishingly small chance that a change will not be + ** detected. The chance of an undetected change is so small that + ** it can be neglected. */ - assert( rc==SQLITE_IOERR || rc==SQLITE_FULL ); - assert( p->state>=PAGER_RESERVED ); - pager_error(p, rc); + char dbFileVers[sizeof(pPager->dbFileVers)]; + sqlite3PagerPagecount(pPager); + + if( pPager->errCode ){ + return pPager->errCode; + } + + if( pPager->dbSize>0 ){ + IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); + rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); + if( rc!=SQLITE_OK ){ + return rc; + } + }else{ + memset(dbFileVers, 0, sizeof(dbFileVers)); + } + + if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ + pager_reset(pPager); + } } } + assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED ); + if( pPager->state==PAGER_UNLOCK ){ + pPager->state = PAGER_SHARED; + } } - return nReleased; + return rc; +} + +/* +** Allocate a PgHdr object. Either create a new one or reuse +** an existing one that is not otherwise in use. +** +** A new PgHdr structure is created if any of the following are +** true: +** +** (1) We have not exceeded our maximum allocated cache size +** as set by the "PRAGMA cache_size" command. +** +** (2) There are no unused PgHdr objects available at this time. +** +** (3) This is an in-memory database. +** +** (4) There are no PgHdr objects that do not require a journal +** file sync and a sync of the journal file is currently +** prohibited. +** +** Otherwise, reuse an existing PgHdr. In other words, reuse an +** existing PgHdr if all of the following are true: +** +** (1) We have reached or exceeded the maximum cache size +** allowed by "PRAGMA cache_size". +** +** (2) There is a PgHdr available with PgHdr->nRef==0 +** +** (3) We are not in an in-memory database +** +** (4) Either there is an available PgHdr that does not need +** to be synced to disk or else disk syncing is currently +** allowed. +*/ +static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){ + int rc = SQLITE_OK; + PgHdr *pPg; + int nByteHdr; + + /* Create a new PgHdr if any of the four conditions defined + ** above are met: */ + if( pPager->nPage<pPager->mxPage + || pPager->lru.pFirst==0 + || MEMDB + || (pPager->lru.pFirstSynced==0 && pPager->doNotSync) + ){ + if( pPager->nPage>=pPager->nHash ){ + pager_resize_hash_table(pPager, + pPager->nHash<256 ? 256 : pPager->nHash*2); + if( pPager->nHash==0 ){ + rc = SQLITE_NOMEM; + goto pager_allocate_out; + } + } + pagerLeave(pPager); + nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra + + MEMDB*sizeof(PgHistory); + pPg = sqlite3_malloc( nByteHdr + pPager->pageSize ); + pagerEnter(pPager); + if( pPg==0 ){ + rc = SQLITE_NOMEM; + goto pager_allocate_out; + } + memset(pPg, 0, nByteHdr); + pPg->pData = (void*)(nByteHdr + (char*)pPg); + pPg->pPager = pPager; + pPg->pNextAll = pPager->pAll; + pPager->pAll = pPg; + pPager->nPage++; + }else{ + /* Recycle an existing page with a zero ref-count. */ + rc = pager_recycle(pPager, &pPg); + if( rc==SQLITE_BUSY ){ + rc = SQLITE_IOERR_BLOCKED; + } + if( rc!=SQLITE_OK ){ + goto pager_allocate_out; + } + assert( pPager->state>=SHARED_LOCK ); + assert(pPg); + } + *ppPg = pPg; + +pager_allocate_out: + return rc; +} + +/* +** Make sure we have the content for a page. If the page was +** previously acquired with noContent==1, then the content was +** just initialized to zeros instead of being read from disk. +** But now we need the real data off of disk. So make sure we +** have it. Read it in if we do not have it already. +*/ +static int pager_get_content(PgHdr *pPg){ + if( pPg->needRead ){ + int rc = readDbPage(pPg->pPager, pPg, pPg->pgno); + if( rc==SQLITE_OK ){ + pPg->needRead = 0; + }else{ + return rc; + } + } + return SQLITE_OK; } -#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ /* ** Acquire a page. @@ -2595,7 +3542,7 @@ int sqlite3pager_release_memory(int nReq){ ** A read lock on the disk file is obtained when the first page is acquired. ** This read lock is dropped when the last page is released. ** -** A _get works for any page number greater than 0. If the database +** This routine works for any page number greater than 0. If the database ** file is smaller than the requested page, then no actual disk ** read occurs and the memory image of the page is initialized to ** all zeros. The extra data appended to a page is always initialized @@ -2604,18 +3551,34 @@ int sqlite3pager_release_memory(int nReq){ ** The acquisition might fail for several reasons. In all cases, ** an appropriate error code is returned and *ppPage is set to NULL. ** -** See also sqlite3pager_lookup(). Both this routine and _lookup() attempt +** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt ** to find a page in the in-memory cache first. If the page is not already -** in memory, this routine goes to disk to read it in whereas _lookup() +** in memory, this routine goes to disk to read it in whereas Lookup() ** just returns 0. This routine acquires a read-lock the first time it ** has to go to disk, and could also playback an old journal if necessary. -** Since _lookup() never goes to disk, it never has to deal with locks +** Since Lookup() never goes to disk, it never has to deal with locks ** or journal files. +** +** If noContent is false, the page contents are actually read from disk. +** If noContent is true, it means that we do not care about the contents +** of the page at this time, so do not do a disk read. Just fill in the +** page content with zeros. But mark the fact that we have not read the +** content by setting the PgHdr.needRead flag. Later on, if +** sqlite3PagerWrite() is called on this page or if this routine is +** called again with noContent==0, that means that the content is needed +** and the disk read should occur at that point. */ -int sqlite3pager_get(Pager *pPager, Pgno pgno, void **ppPage){ +static int pagerAcquire( + Pager *pPager, /* The pager open on the database file */ + Pgno pgno, /* Page number to fetch */ + DbPage **ppPage, /* Write a pointer to the page here */ + int noContent /* Do not bother reading content from disk if true */ +){ PgHdr *pPg; int rc; + assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 ); + /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page ** number greater than this, or zero, is requested. */ @@ -2627,121 +3590,34 @@ int sqlite3pager_get(Pager *pPager, Pgno pgno, void **ppPage){ */ assert( pPager!=0 ); *ppPage = 0; - if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ - return pPager->errCode; - } /* If this is the first page accessed, then get a SHARED lock - ** on the database file. + ** on the database file. pagerSharedLock() is a no-op if + ** a database lock is already held. */ - if( pPager->nRef==0 && !MEMDB ){ - if( !pPager->noReadlock ){ - rc = pager_wait_on_lock(pPager, SHARED_LOCK); - if( rc!=SQLITE_OK ){ - return pager_error(pPager, rc); - } - } - - /* If a journal file exists, and there is no RESERVED lock on the - ** database file, then it either needs to be played back or deleted. - */ - if( hasHotJournal(pPager) ){ - /* Get an EXCLUSIVE lock on the database file. At this point it is - ** important that a RESERVED lock is not obtained on the way to the - ** EXCLUSIVE lock. If it were, another process might open the - ** database file, detect the RESERVED lock, and conclude that the - ** database is safe to read while this process is still rolling it - ** back. - ** - ** Because the intermediate RESERVED lock is not requested, the - ** second process will get to this point in the code and fail to - ** obtain it's own EXCLUSIVE lock on the database file. - */ - rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK); - if( rc!=SQLITE_OK ){ - sqlite3OsUnlock(pPager->fd, NO_LOCK); - pPager->state = PAGER_UNLOCK; - return pager_error(pPager, rc); - } - pPager->state = PAGER_EXCLUSIVE; - - /* Open the journal for reading only. Return SQLITE_BUSY if - ** we are unable to open the journal file. - ** - ** The journal file does not need to be locked itself. The - ** journal file is never open unless the main database file holds - ** a write lock, so there is never any chance of two or more - ** processes opening the journal at the same time. - */ - rc = sqlite3OsOpenReadOnly(pPager->zJournal, &pPager->jfd); - if( rc!=SQLITE_OK ){ - sqlite3OsUnlock(pPager->fd, NO_LOCK); - pPager->state = PAGER_UNLOCK; - return SQLITE_BUSY; - } - pPager->journalOpen = 1; - pPager->journalStarted = 0; - pPager->journalOff = 0; - pPager->setMaster = 0; - pPager->journalHdr = 0; - - /* Playback and delete the journal. Drop the database write - ** lock and reacquire the read lock. - */ - rc = pager_playback(pPager); - if( rc!=SQLITE_OK ){ - return pager_error(pPager, rc); - } - } - pPg = 0; - }else{ - /* Search for page in cache */ - pPg = pager_lookup(pPager, pgno); - if( MEMDB && pPager->state==PAGER_UNLOCK ){ - pPager->state = PAGER_SHARED; - } + rc = pagerSharedLock(pPager); + if( rc!=SQLITE_OK ){ + return rc; } + assert( pPager->state!=PAGER_UNLOCK ); + + pPg = pager_lookup(pPager, pgno); if( pPg==0 ){ /* The requested page is not in the page cache. */ + int nMax; int h; - TEST_INCR(pPager->nMiss); - if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 || MEMDB ){ - /* Create a new page */ - if( pPager->nPage>=pPager->nHash ){ - pager_resize_hash_table(pPager, - pPager->nHash<256 ? 256 : pPager->nHash*2); - if( pPager->nHash==0 ){ - return SQLITE_NOMEM; - } - } - pPg = sqliteMallocRaw( sizeof(*pPg) + pPager->pageSize - + sizeof(u32) + pPager->nExtra - + MEMDB*sizeof(PgHistory) ); - if( pPg==0 ){ - return SQLITE_NOMEM; - } - memset(pPg, 0, sizeof(*pPg)); - if( MEMDB ){ - memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory)); - } - pPg->pPager = pPager; - pPg->pNextAll = pPager->pAll; - pPager->pAll = pPg; - pPager->nPage++; - if( pPager->nPage>pPager->nMaxPage ){ - assert( pPager->nMaxPage==(pPager->nPage-1) ); - pPager->nMaxPage++; - } - }else{ - rc = pager_recycle(pPager, 1, &pPg); - if( rc!=SQLITE_OK ){ - return rc; - } - assert(pPg) ; + PAGER_INCR(pPager->nMiss); + rc = pagerAllocatePage(pPager, &pPg); + if( rc!=SQLITE_OK ){ + return rc; } + pPg->pgno = pgno; + assert( !MEMDB || pgno>pPager->stmtSize ); if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){ +#if 0 sqlite3CheckMemory(pPager->aInJournal, pgno/8); +#endif assert( pPager->journalOpen ); pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; pPg->needSync = 0; @@ -2749,56 +3625,40 @@ int sqlite3pager_get(Pager *pPager, Pgno pgno, void **ppPage){ pPg->inJournal = 0; pPg->needSync = 0; } - if( pPager->aInStmt && (int)pgno<=pPager->stmtSize - && (pPager->aInStmt[pgno/8] & (1<<(pgno&7)))!=0 ){ - page_add_to_stmt_list(pPg); - }else{ - page_remove_from_stmt_list(pPg); - } + makeClean(pPg); pPg->nRef = 1; - REFINFO(pPg); pPager->nRef++; if( pPager->nExtra>0 ){ memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra); } + nMax = sqlite3PagerPagecount(pPager); if( pPager->errCode ){ - sqlite3pager_unref(PGHDR_TO_DATA(pPg)); rc = pPager->errCode; + sqlite3PagerUnref(pPg); return rc; } /* Populate the page with data, either by reading from the database ** file, or by setting the entire page to zero. */ - if( sqlite3pager_pagecount(pPager)<(int)pgno || MEMDB ){ + if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){ + if( pgno>pPager->mxPgno ){ + sqlite3PagerUnref(pPg); + return SQLITE_FULL; + } memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize); + pPg->needRead = noContent && !pPager->alwaysRollback; + IOTRACE(("ZERO %p %d\n", pPager, pgno)); }else{ - assert( MEMDB==0 ); - rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize); - if( rc==SQLITE_OK ){ - rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), - pPager->pageSize); - } - TRACE3("FETCH %d page %d\n", PAGERID(pPager), pPg->pgno); - CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3); - if( rc!=SQLITE_OK ){ - i64 fileSize; - int rc2 = sqlite3OsFileSize(pPager->fd, &fileSize); - if( rc2!=SQLITE_OK || fileSize>=pgno*pPager->pageSize ){ - /* An IO error occured in one of the the sqlite3OsSeek() or - ** sqlite3OsRead() calls above. */ - pPg->pgno = 0; - sqlite3pager_unref(PGHDR_TO_DATA(pPg)); - return rc; - }else{ - clear_simulated_io_error(); - memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize); - } - }else{ - TEST_INCR(pPager->nRead); + rc = readDbPage(pPager, pPg, pgno); + if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ + pPg->pgno = 0; + sqlite3PagerUnref(pPg); + return rc; } + pPg->needRead = 0; } /* Link the page into the page hash table */ @@ -2816,36 +3676,60 @@ int sqlite3pager_get(Pager *pPager, Pgno pgno, void **ppPage){ #endif }else{ /* The requested page is in the page cache. */ - TEST_INCR(pPager->nHit); + assert(pPager->nRef>0 || pgno==1); + PAGER_INCR(pPager->nHit); + if( !noContent ){ + rc = pager_get_content(pPg); + if( rc ){ + return rc; + } + } page_ref(pPg); } - *ppPage = PGHDR_TO_DATA(pPg); + *ppPage = pPg; return SQLITE_OK; } +int sqlite3PagerAcquire( + Pager *pPager, /* The pager open on the database file */ + Pgno pgno, /* Page number to fetch */ + DbPage **ppPage, /* Write a pointer to the page here */ + int noContent /* Do not bother reading content from disk if true */ +){ + int rc; + pagerEnter(pPager); + rc = pagerAcquire(pPager, pgno, ppPage, noContent); + pagerLeave(pPager); + return rc; +} + /* ** Acquire a page if it is already in the in-memory cache. Do ** not read the page from disk. Return a pointer to the page, ** or 0 if the page is not in cache. ** -** See also sqlite3pager_get(). The difference between this routine -** and sqlite3pager_get() is that _get() will go to the disk and read +** See also sqlite3PagerGet(). The difference between this routine +** and sqlite3PagerGet() is that _get() will go to the disk and read ** in the page if the page is not already in cache. This routine ** returns NULL if the page is not in cache or if a disk I/O error ** has ever happened. */ -void *sqlite3pager_lookup(Pager *pPager, Pgno pgno){ - PgHdr *pPg; +DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ + PgHdr *pPg = 0; assert( pPager!=0 ); assert( pgno!=0 ); - if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ - return 0; + + pagerEnter(pPager); + if( pPager->state==PAGER_UNLOCK ){ + assert( !pPager->pAll || pPager->exclusiveMode ); + }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ + /* Do nothing */ + }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){ + page_ref(pPg); } - pPg = pager_lookup(pPager, pgno); - if( pPg==0 ) return 0; - page_ref(pPg); - return PGHDR_TO_DATA(pPg); + pagerLeave(pPager); + return pPg; } /* @@ -2856,15 +3740,14 @@ void *sqlite3pager_lookup(Pager *pPager, Pgno pgno){ ** are released, a rollback occurs and the lock on the database is ** removed. */ -int sqlite3pager_unref(void *pData){ - PgHdr *pPg; +int sqlite3PagerUnref(DbPage *pPg){ + Pager *pPager = pPg->pPager; /* Decrement the reference count for this page */ - pPg = DATA_TO_PGHDR(pData); assert( pPg->nRef>0 ); + pagerEnter(pPg->pPager); pPg->nRef--; - REFINFO(pPg); CHECK_PAGE(pPg); @@ -2872,21 +3755,10 @@ int sqlite3pager_unref(void *pData){ ** destructor and add the page to the freelist. */ if( pPg->nRef==0 ){ - Pager *pPager; - pPager = pPg->pPager; - pPg->pNextFree = 0; - pPg->pPrevFree = pPager->pLast; - pPager->pLast = pPg; - if( pPg->pPrevFree ){ - pPg->pPrevFree->pNextFree = pPg; - }else{ - pPager->pFirst = pPg; - } - if( pPg->needSync==0 && pPager->pFirstSynced==0 ){ - pPager->pFirstSynced = pPg; - } + + lruListAdd(pPg); if( pPager->xDestructor ){ - pPager->xDestructor(pData, pPager->pageSize); + pPager->xDestructor(pPg, pPager->pageSize); } /* When all pages reach the freelist, drop the read lock from @@ -2894,10 +3766,11 @@ int sqlite3pager_unref(void *pData){ */ pPager->nRef--; assert( pPager->nRef>=0 ); - if( pPager->nRef==0 && !MEMDB ){ - pager_reset(pPager); + if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){ + pagerUnlockAndRollback(pPager); } } + pagerLeave(pPager); return SQLITE_OK; } @@ -2909,29 +3782,46 @@ int sqlite3pager_unref(void *pData){ ** write lock if anything goes wrong. */ static int pager_open_journal(Pager *pPager){ + sqlite3_vfs *pVfs = pPager->pVfs; + int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE); + int rc; assert( !MEMDB ); assert( pPager->state>=PAGER_RESERVED ); assert( pPager->journalOpen==0 ); assert( pPager->useJournal ); assert( pPager->aInJournal==0 ); - sqlite3pager_pagecount(pPager); - pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 ); + sqlite3PagerPagecount(pPager); + pagerLeave(pPager); + pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 ); + pagerEnter(pPager); if( pPager->aInJournal==0 ){ rc = SQLITE_NOMEM; goto failed_to_open_journal; } - rc = sqlite3OsOpenExclusive(pPager->zJournal, &pPager->jfd, - pPager->tempFile); + + if( pPager->tempFile ){ + flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); + }else{ + flags |= (SQLITE_OPEN_MAIN_JOURNAL); + } +#ifdef SQLITE_ENABLE_ATOMIC_WRITE + rc = sqlite3JournalOpen( + pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) + ); +#else + rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); +#endif + assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); pPager->journalOff = 0; pPager->setMaster = 0; pPager->journalHdr = 0; if( rc!=SQLITE_OK ){ + if( rc==SQLITE_NOMEM ){ + sqlite3OsDelete(pVfs, pPager->zJournal, 0); + } goto failed_to_open_journal; } - sqlite3OsSetFullSync(pPager->jfd, pPager->full_fsync); - sqlite3OsSetFullSync(pPager->fd, pPager->full_fsync); - sqlite3OsOpenDirectory(pPager->jfd, pPager->zDirectory); pPager->journalOpen = 1; pPager->journalStarted = 0; pPager->needSync = 0; @@ -2946,10 +3836,10 @@ static int pager_open_journal(Pager *pPager){ rc = writeJournalHdr(pPager); if( pPager->stmtAutoopen && rc==SQLITE_OK ){ - rc = sqlite3pager_stmt_begin(pPager); + rc = sqlite3PagerStmtBegin(pPager); } - if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){ - rc = pager_unwritelock(pPager); + if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){ + rc = pager_end_transaction(pPager); if( rc==SQLITE_OK ){ rc = SQLITE_FULL; } @@ -2957,19 +3847,8 @@ static int pager_open_journal(Pager *pPager){ return rc; failed_to_open_journal: - sqliteFree(pPager->aInJournal); + sqlite3_free(pPager->aInJournal); pPager->aInJournal = 0; - if( rc==SQLITE_NOMEM ){ - /* If this was a malloc() failure, then we will not be closing the pager - ** file. So delete any journal file we may have just created. Otherwise, - ** the system will get confused, we have a read-lock on the file and a - ** mysterious journal has appeared in the filesystem. - */ - sqlite3OsDelete(pPager->zJournal); - }else{ - sqlite3OsUnlock(pPager->fd, NO_LOCK); - pPager->state = PAGER_UNLOCK; - } return rc; } @@ -2977,10 +3856,10 @@ failed_to_open_journal: ** Acquire a write-lock on the database. The lock is removed when ** the any of the following happen: ** -** * sqlite3pager_commit() is called. -** * sqlite3pager_rollback() is called. -** * sqlite3pager_close() is called. -** * sqlite3pager_unref() is called to on every outstanding page. +** * sqlite3PagerCommitPhaseTwo() is called. +** * sqlite3PagerRollback() is called. +** * sqlite3PagerClose() is called. +** * sqlite3PagerUnref() is called to on every outstanding page. ** ** The first parameter to this routine is a pointer to any open page of the ** database file. Nothing changes about the page - it is used merely to @@ -3000,10 +3879,10 @@ failed_to_open_journal: ** immediately instead of waiting until we try to flush the cache. The ** exFlag is ignored if a transaction is already active. */ -int sqlite3pager_begin(void *pData, int exFlag){ - PgHdr *pPg = DATA_TO_PGHDR(pData); +int sqlite3PagerBegin(DbPage *pPg, int exFlag){ Pager *pPager = pPg->pPager; int rc = SQLITE_OK; + pagerEnter(pPager); assert( pPg->nRef>0 ); assert( pPager->state!=PAGER_UNLOCK ); if( pPager->state==PAGER_SHARED ){ @@ -3020,15 +3899,37 @@ int sqlite3pager_begin(void *pData, int exFlag){ } } if( rc!=SQLITE_OK ){ + pagerLeave(pPager); return rc; } pPager->dirtyCache = 0; - TRACE2("TRANSACTION %d\n", PAGERID(pPager)); + PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager)); if( pPager->useJournal && !pPager->tempFile ){ rc = pager_open_journal(pPager); } } + }else if( pPager->journalOpen && pPager->journalOff==0 ){ + /* This happens when the pager was in exclusive-access mode last + ** time a (read or write) transaction was successfully concluded + ** by this connection. Instead of deleting the journal file it was + ** kept open and truncated to 0 bytes. + */ + assert( pPager->nRec==0 ); + assert( pPager->origDbSize==0 ); + assert( pPager->aInJournal==0 ); + sqlite3PagerPagecount(pPager); + pagerLeave(pPager); + pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 ); + pagerEnter(pPager); + if( !pPager->aInJournal ){ + rc = SQLITE_NOMEM; + }else{ + pPager->origDbSize = pPager->dbSize; + rc = writeJournalHdr(pPager); + } } + assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK ); + pagerLeave(pPager); return rc; } @@ -3057,11 +3958,14 @@ static void makeClean(PgHdr *pPg){ if( pPg->dirty ){ pPg->dirty = 0; if( pPg->pDirty ){ + assert( pPg->pDirty->pPrevDirty==pPg ); pPg->pDirty->pPrevDirty = pPg->pPrevDirty; } if( pPg->pPrevDirty ){ + assert( pPg->pPrevDirty->pDirty==pPg ); pPg->pPrevDirty->pDirty = pPg->pDirty; }else{ + assert( pPg->pPager->pDirty==pPg ); pPg->pPager->pDirty = pPg->pDirty; } } @@ -3082,11 +3986,11 @@ static void makeClean(PgHdr *pPg){ ** If the journal file could not be written because the disk is full, ** then this routine returns SQLITE_FULL and does an immediate rollback. ** All subsequent write attempts also return SQLITE_FULL until there -** is a call to sqlite3pager_commit() or sqlite3pager_rollback() to +** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to ** reset. */ -int sqlite3pager_write(void *pData){ - PgHdr *pPg = DATA_TO_PGHDR(pData); +static int pager_write(PgHdr *pPg){ + void *pData = PGHDR_TO_DATA(pPg); Pager *pPager = pPg->pPager; int rc = SQLITE_OK; @@ -3103,11 +4007,24 @@ int sqlite3pager_write(void *pData){ CHECK_PAGE(pPg); + /* If this page was previously acquired with noContent==1, that means + ** we didn't really read in the content of the page. This can happen + ** (for example) when the page is being moved to the freelist. But + ** now we are (perhaps) moving the page off of the freelist for + ** reuse and we need to know its original content so that content + ** can be stored in the rollback journal. So do the read at this + ** time. + */ + rc = pager_get_content(pPg); + if( rc ){ + return rc; + } + /* Mark the page as dirty. If the page has already been written ** to the journal then we can return right away. */ makeDirty(pPg); - if( pPg->inJournal && (pPg->inStmt || pPager->stmtInUse==0) ){ + if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){ pPager->dirtyCache = 1; }else{ @@ -3119,7 +4036,7 @@ int sqlite3pager_write(void *pData){ ** create it if it does not. */ assert( pPager->state!=PAGER_UNLOCK ); - rc = sqlite3pager_begin(pData, 0); + rc = sqlite3PagerBegin(pPg, 0); if( rc!=SQLITE_OK ){ return rc; } @@ -3137,37 +4054,42 @@ int sqlite3pager_write(void *pData){ */ if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){ if( (int)pPg->pgno <= pPager->origDbSize ){ - int szPg; if( MEMDB ){ PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); - TRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); + PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); assert( pHist->pOrig==0 ); - pHist->pOrig = sqliteMallocRaw( pPager->pageSize ); - if( pHist->pOrig ){ - memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize); + pHist->pOrig = sqlite3_malloc( pPager->pageSize ); + if( !pHist->pOrig ){ + return SQLITE_NOMEM; } + memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize); }else{ - u32 cksum, saved; - char *pData2, *pEnd; + u32 cksum; + char *pData2; + /* We should never write to the journal file the page that ** contains the database locks. The following assert verifies ** that we do not. */ assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); pData2 = CODEC2(pPager, pData, pPg->pgno, 7); cksum = pager_cksum(pPager, (u8*)pData2); - pEnd = pData2 + pPager->pageSize; - pData2 -= 4; - saved = *(u32*)pEnd; - put32bits(pEnd, cksum); - szPg = pPager->pageSize+8; - put32bits(pData2, pPg->pgno); - rc = sqlite3OsWrite(pPager->jfd, pData2, szPg); - pPager->journalOff += szPg; - TRACE4("JOURNAL %d page %d needSync=%d\n", - PAGERID(pPager), pPg->pgno, pPg->needSync); - *(u32*)pEnd = saved; - - /* An error has occured writing to the journal file. The + rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno); + if( rc==SQLITE_OK ){ + rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, + pPager->journalOff + 4); + pPager->journalOff += pPager->pageSize+4; + } + if( rc==SQLITE_OK ){ + rc = write32bits(pPager->jfd, pPager->journalOff, cksum); + pPager->journalOff += 4; + } + IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, + pPager->journalOff, pPager->pageSize)); + PAGER_INCR(sqlite3_pager_writej_count); + PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n", + PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg)); + + /* An error has occured writing to the journal file. The ** transaction will be rolled back by the layer above. */ if( rc!=SQLITE_OK ){ @@ -3180,12 +4102,11 @@ int sqlite3pager_write(void *pData){ pPg->needSync = !pPager->noSync; if( pPager->stmtInUse ){ pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); - page_add_to_stmt_list(pPg); } } }else{ pPg->needSync = !pPager->journalStarted && !pPager->noSync; - TRACE4("APPEND %d page %d needSync=%d\n", + PAGERTRACE4("APPEND %d page %d needSync=%d\n", PAGERID(pPager), pPg->pgno, pPg->needSync); } if( pPg->needSync ){ @@ -3199,21 +4120,28 @@ int sqlite3pager_write(void *pData){ ** the statement journal format differs from the standard journal format ** in that it omits the checksums and the header. */ - if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){ + if( pPager->stmtInUse + && !pageInStatement(pPg) + && (int)pPg->pgno<=pPager->stmtSize + ){ assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); if( MEMDB ){ PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); assert( pHist->pStmt==0 ); - pHist->pStmt = sqliteMallocRaw( pPager->pageSize ); + pHist->pStmt = sqlite3_malloc( pPager->pageSize ); if( pHist->pStmt ){ memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize); } - TRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); + PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); + page_add_to_stmt_list(pPg); }else{ - char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4; - put32bits(pData2, pPg->pgno); - rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4); - TRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); + i64 offset = pPager->stmtNRec*(4+pPager->pageSize); + char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7); + rc = write32bits(pPager->stfd, offset, pPg->pgno); + if( rc==SQLITE_OK ){ + rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4); + } + PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); if( rc!=SQLITE_OK ){ return rc; } @@ -3221,12 +4149,12 @@ int sqlite3pager_write(void *pData){ assert( pPager->aInStmt!=0 ); pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); } - page_add_to_stmt_list(pPg); } } /* Update the database size and return. */ + assert( pPager->state>=PAGER_SHARED ); if( pPager->dbSize<(int)pPg->pgno ){ pPager->dbSize = pPg->pgno; if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){ @@ -3237,13 +4165,107 @@ int sqlite3pager_write(void *pData){ } /* +** This function is used to mark a data-page as writable. It uses +** pager_write() to open a journal file (if it is not already open) +** and write the page *pData to the journal. +** +** The difference between this function and pager_write() is that this +** function also deals with the special case where 2 or more pages +** fit on a single disk sector. In this case all co-resident pages +** must have been written to the journal file before returning. +*/ +int sqlite3PagerWrite(DbPage *pDbPage){ + int rc = SQLITE_OK; + + PgHdr *pPg = pDbPage; + Pager *pPager = pPg->pPager; + Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); + + pagerEnter(pPager); + if( !MEMDB && nPagePerSector>1 ){ + Pgno nPageCount; /* Total number of pages in database file */ + Pgno pg1; /* First page of the sector pPg is located on. */ + int nPage; /* Number of pages starting at pg1 to journal */ + int ii; + int needSync = 0; + + /* Set the doNotSync flag to 1. This is because we cannot allow a journal + ** header to be written between the pages journaled by this function. + */ + assert( pPager->doNotSync==0 ); + pPager->doNotSync = 1; + + /* This trick assumes that both the page-size and sector-size are + ** an integer power of 2. It sets variable pg1 to the identifier + ** of the first page of the sector pPg is located on. + */ + pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; + + nPageCount = sqlite3PagerPagecount(pPager); + if( pPg->pgno>nPageCount ){ + nPage = (pPg->pgno - pg1)+1; + }else if( (pg1+nPagePerSector-1)>nPageCount ){ + nPage = nPageCount+1-pg1; + }else{ + nPage = nPagePerSector; + } + assert(nPage>0); + assert(pg1<=pPg->pgno); + assert((pg1+nPage)>pPg->pgno); + + for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ + Pgno pg = pg1+ii; + PgHdr *pPage; + if( !pPager->aInJournal || pg==pPg->pgno || + pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7))) + ) { + if( pg!=PAGER_MJ_PGNO(pPager) ){ + rc = sqlite3PagerGet(pPager, pg, &pPage); + if( rc==SQLITE_OK ){ + rc = pager_write(pPage); + if( pPage->needSync ){ + needSync = 1; + } + sqlite3PagerUnref(pPage); + } + } + }else if( (pPage = pager_lookup(pPager, pg)) ){ + if( pPage->needSync ){ + needSync = 1; + } + } + } + + /* If the PgHdr.needSync flag is set for any of the nPage pages + ** starting at pg1, then it needs to be set for all of them. Because + ** writing to any of these nPage pages may damage the others, the + ** journal file must contain sync()ed copies of all of them + ** before any of them can be written out to the database file. + */ + if( needSync ){ + for(ii=0; ii<nPage && needSync; ii++){ + PgHdr *pPage = pager_lookup(pPager, pg1+ii); + if( pPage ) pPage->needSync = 1; + } + assert(pPager->needSync); + } + + assert( pPager->doNotSync==1 ); + pPager->doNotSync = 0; + }else{ + rc = pager_write(pDbPage); + } + pagerLeave(pPager); + return rc; +} + +/* ** Return TRUE if the page given in the argument was previously passed -** to sqlite3pager_write(). In other words, return TRUE if it is ok +** to sqlite3PagerWrite(). In other words, return TRUE if it is ok ** to change the content of the page. */ #ifndef NDEBUG -int sqlite3pager_iswriteable(void *pData){ - PgHdr *pPg = DATA_TO_PGHDR(pData); +int sqlite3PagerIswriteable(DbPage *pPg){ return pPg->dirty; } #endif @@ -3253,25 +4275,27 @@ int sqlite3pager_iswriteable(void *pData){ ** Replace the content of a single page with the information in the third ** argument. */ -int sqlite3pager_overwrite(Pager *pPager, Pgno pgno, void *pData){ - void *pPage; +int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){ + PgHdr *pPg; int rc; - rc = sqlite3pager_get(pPager, pgno, &pPage); + pagerEnter(pPager); + rc = sqlite3PagerGet(pPager, pgno, &pPg); if( rc==SQLITE_OK ){ - rc = sqlite3pager_write(pPage); + rc = sqlite3PagerWrite(pPg); if( rc==SQLITE_OK ){ - memcpy(pPage, pData, pPager->pageSize); + memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize); } - sqlite3pager_unref(pPage); + sqlite3PagerUnref(pPg); } + pagerLeave(pPager); return rc; } #endif /* ** A call to this routine tells the pager that it is not necessary to -** write the information on page "pgno" back to the disk, even though +** write the information on page pPg back to the disk, even though ** that page might be marked as dirty. ** ** The overlying software layer calls this routine when all of the data @@ -3279,29 +4303,29 @@ int sqlite3pager_overwrite(Pager *pPager, Pgno pgno, void *pData){ ** that it does not get written to disk. ** ** Tests show that this optimization, together with the -** sqlite3pager_dont_rollback() below, more than double the speed +** sqlite3PagerDontRollback() below, more than double the speed ** of large INSERT operations and quadruple the speed of large DELETEs. ** ** When this routine is called, set the alwaysRollback flag to true. -** Subsequent calls to sqlite3pager_dont_rollback() for the same page +** Subsequent calls to sqlite3PagerDontRollback() for the same page ** will thereafter be ignored. This is necessary to avoid a problem ** where a page with data is added to the freelist during one part of ** a transaction then removed from the freelist during a later part ** of the same transaction and reused for some other purpose. When it ** is first added to the freelist, this routine is called. When reused, -** the dont_rollback() routine is called. But because the page contains -** critical data, we still need to be sure it gets rolled back in spite -** of the dont_rollback() call. +** the sqlite3PagerDontRollback() routine is called. But because the +** page contains critical data, we still need to be sure it gets +** rolled back in spite of the sqlite3PagerDontRollback() call. */ -void sqlite3pager_dont_write(Pager *pPager, Pgno pgno){ - PgHdr *pPg; +void sqlite3PagerDontWrite(DbPage *pDbPage){ + PgHdr *pPg = pDbPage; + Pager *pPager = pPg->pPager; if( MEMDB ) return; - - pPg = pager_lookup(pPager, pgno); - assert( pPg!=0 ); /* We never call _dont_write unless the page is in mem */ + pagerEnter(pPager); pPg->alwaysRollback = 1; if( pPg->dirty && !pPager->stmtInUse ){ + assert( pPager->state>=PAGER_SHARED ); if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){ /* If this pages is the last page in the file and the file has grown ** during the current transaction, then do NOT mark the page as clean. @@ -3312,13 +4336,15 @@ void sqlite3pager_dont_write(Pager *pPager, Pgno pgno){ ** corruption during the next transaction. */ }else{ - TRACE3("DONT_WRITE page %d of %d\n", pgno, PAGERID(pPager)); + PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)); + IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) makeClean(pPg); #ifdef SQLITE_CHECK_PAGES pPg->pageHash = pager_pagehash(pPg); #endif } } + pagerLeave(pPager); } /* @@ -3326,29 +4352,243 @@ void sqlite3pager_dont_write(Pager *pPager, Pgno pgno){ ** it is not necessary to restore the data on the given page. This ** means that the pager does not have to record the given page in the ** rollback journal. +** +** If we have not yet actually read the content of this page (if +** the PgHdr.needRead flag is set) then this routine acts as a promise +** that we will never need to read the page content in the future. +** so the needRead flag can be cleared at this point. +** +** This routine is only called from a single place in the sqlite btree +** code (when a leaf is removed from the free-list). This allows the +** following assumptions to be made about pPg: +** +** 1. PagerDontWrite() has been called on the page, OR +** PagerWrite() has not yet been called on the page. +** +** 2. The page existed when the transaction was started. +** +** Details: DontRollback() (this routine) is only called when a leaf is +** removed from the free list. DontWrite() is called whenever a page +** becomes a free-list leaf. */ -void sqlite3pager_dont_rollback(void *pData){ - PgHdr *pPg = DATA_TO_PGHDR(pData); +void sqlite3PagerDontRollback(DbPage *pPg){ Pager *pPager = pPg->pPager; - if( pPager->state!=PAGER_EXCLUSIVE || pPager->journalOpen==0 ) return; - if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return; - if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){ - assert( pPager->aInJournal!=0 ); - pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); - pPg->inJournal = 1; - if( pPager->stmtInUse ){ - pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); - page_add_to_stmt_list(pPg); - } - TRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)); - } - if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){ - assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); - assert( pPager->aInStmt!=0 ); + pagerEnter(pPager); + assert( pPager->state>=PAGER_RESERVED ); + + /* If the journal file is not open, or DontWrite() has been called on + ** this page (DontWrite() sets the alwaysRollback flag), then this + ** function is a no-op. + */ + if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){ + pagerLeave(pPager); + return; + } + assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */ + + /* Check that PagerWrite() has not yet been called on this page, and + ** that the page existed when the transaction started. + */ + assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); + + assert( pPager->aInJournal!=0 ); + pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); + pPg->inJournal = 1; + pPg->needRead = 0; + if( pPager->stmtInUse ){ + assert( pPager->stmtSize <= pPager->origDbSize ); pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); - page_add_to_stmt_list(pPg); } + PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)); + IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno)) + pagerLeave(pPager); +} + + +/* +** This routine is called to increment the database file change-counter, +** stored at byte 24 of the pager file. +*/ +static int pager_incr_changecounter(Pager *pPager, int isDirect){ + PgHdr *pPgHdr; + u32 change_counter; + int rc = SQLITE_OK; + + if( !pPager->changeCountDone ){ + /* Open page 1 of the file for writing. */ + rc = sqlite3PagerGet(pPager, 1, &pPgHdr); + if( rc!=SQLITE_OK ) return rc; + + if( !isDirect ){ + rc = sqlite3PagerWrite(pPgHdr); + if( rc!=SQLITE_OK ){ + sqlite3PagerUnref(pPgHdr); + return rc; + } + } + + /* Increment the value just read and write it back to byte 24. */ + change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers); + change_counter++; + put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter); + + if( isDirect && pPager->fd->pMethods ){ + const void *zBuf = PGHDR_TO_DATA(pPgHdr); + rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); + } + + /* Release the page reference. */ + sqlite3PagerUnref(pPgHdr); + pPager->changeCountDone = 1; + } + return rc; +} + +/* +** Sync the database file for the pager pPager. zMaster points to the name +** of a master journal file that should be written into the individual +** journal file. zMaster may be NULL, which is interpreted as no master +** journal (a single database transaction). +** +** This routine ensures that the journal is synced, all dirty pages written +** to the database file and the database file synced. The only thing that +** remains to commit the transaction is to delete the journal file (or +** master journal file if specified). +** +** Note that if zMaster==NULL, this does not overwrite a previous value +** passed to an sqlite3PagerCommitPhaseOne() call. +** +** If parameter nTrunc is non-zero, then the pager file is truncated to +** nTrunc pages (this is used by auto-vacuum databases). +*/ +int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){ + int rc = SQLITE_OK; + + PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n", + pPager->zFilename, zMaster, nTrunc); + pagerEnter(pPager); + + /* If this is an in-memory db, or no pages have been written to, or this + ** function has already been called, it is a no-op. + */ + if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){ + PgHdr *pPg; + +#ifdef SQLITE_ENABLE_ATOMIC_WRITE + /* The atomic-write optimization can be used if all of the + ** following are true: + ** + ** + The file-system supports the atomic-write property for + ** blocks of size page-size, and + ** + This commit is not part of a multi-file transaction, and + ** + Exactly one page has been modified and store in the journal file. + ** + ** If the optimization can be used, then the journal file will never + ** be created for this transaction. + */ + int useAtomicWrite = ( + !zMaster && + pPager->journalOff==jrnlBufferSize(pPager) && + nTrunc==0 && + (0==pPager->pDirty || 0==pPager->pDirty->pDirty) + ); + if( useAtomicWrite ){ + /* Update the nRec field in the journal file. */ + int offset = pPager->journalHdr + sizeof(aJournalMagic); + assert(pPager->nRec==1); + rc = write32bits(pPager->jfd, offset, pPager->nRec); + + /* Update the db file change counter. The following call will modify + ** the in-memory representation of page 1 to include the updated + ** change counter and then write page 1 directly to the database + ** file. Because of the atomic-write property of the host file-system, + ** this is safe. + */ + if( rc==SQLITE_OK ){ + rc = pager_incr_changecounter(pPager, 1); + } + }else{ + rc = sqlite3JournalCreate(pPager->jfd); + } + + if( !useAtomicWrite && rc==SQLITE_OK ) +#endif + + /* If a master journal file name has already been written to the + ** journal file, then no sync is required. This happens when it is + ** written, then the process fails to upgrade from a RESERVED to an + ** EXCLUSIVE lock. The next time the process tries to commit the + ** transaction the m-j name will have already been written. + */ + if( !pPager->setMaster ){ + assert( pPager->journalOpen ); + rc = pager_incr_changecounter(pPager, 0); + if( rc!=SQLITE_OK ) goto sync_exit; +#ifndef SQLITE_OMIT_AUTOVACUUM + if( nTrunc!=0 ){ + /* If this transaction has made the database smaller, then all pages + ** being discarded by the truncation must be written to the journal + ** file. + */ + Pgno i; + int iSkip = PAGER_MJ_PGNO(pPager); + for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){ + if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){ + rc = sqlite3PagerGet(pPager, i, &pPg); + if( rc!=SQLITE_OK ) goto sync_exit; + rc = sqlite3PagerWrite(pPg); + sqlite3PagerUnref(pPg); + if( rc!=SQLITE_OK ) goto sync_exit; + } + } + } +#endif + rc = writeMasterJournal(pPager, zMaster); + if( rc!=SQLITE_OK ) goto sync_exit; + rc = syncJournal(pPager); + } + if( rc!=SQLITE_OK ) goto sync_exit; + +#ifndef SQLITE_OMIT_AUTOVACUUM + if( nTrunc!=0 ){ + rc = sqlite3PagerTruncate(pPager, nTrunc); + if( rc!=SQLITE_OK ) goto sync_exit; + } +#endif + + /* Write all dirty pages to the database file */ + pPg = pager_get_all_dirty_pages(pPager); + rc = pager_write_pagelist(pPg); + if( rc!=SQLITE_OK ){ + while( pPg && !pPg->dirty ){ pPg = pPg->pDirty; } + pPager->pDirty = pPg; + goto sync_exit; + } + pPager->pDirty = 0; + + /* Sync the database file. */ + if( !pPager->noSync ){ + rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); + } + IOTRACE(("DBSYNC %p\n", pPager)) + + pPager->state = PAGER_SYNCED; + }else if( MEMDB && nTrunc!=0 ){ + rc = sqlite3PagerTruncate(pPager, nTrunc); + } + +sync_exit: + if( rc==SQLITE_IOERR_BLOCKED ){ + /* pager_incr_changecounter() may attempt to obtain an exclusive + * lock to spill the cache and return IOERR_BLOCKED. But since + * there is no chance the cache is inconsistent, it is + * better to return SQLITE_BUSY. + */ + rc = SQLITE_BUSY; + } + pagerLeave(pPager); + return rc; } @@ -3359,7 +4599,7 @@ void sqlite3pager_dont_rollback(void *pData){ ** and an error code is returned. If the commit worked, SQLITE_OK ** is returned. */ -int sqlite3pager_commit(Pager *pPager){ +int sqlite3PagerCommitPhaseTwo(Pager *pPager){ int rc; PgHdr *pPg; @@ -3369,16 +4609,18 @@ int sqlite3pager_commit(Pager *pPager){ if( pPager->state<PAGER_RESERVED ){ return SQLITE_ERROR; } - TRACE2("COMMIT %d\n", PAGERID(pPager)); + pagerEnter(pPager); + PAGERTRACE2("COMMIT %d\n", PAGERID(pPager)); if( MEMDB ){ pPg = pager_get_all_dirty_pages(pPager); while( pPg ){ - clearHistory(PGHDR_TO_HIST(pPg, pPager)); + PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); + clearHistory(pHist); pPg->dirty = 0; pPg->inJournal = 0; - pPg->inStmt = 0; + pHist->inStmt = 0; pPg->needSync = 0; - pPg->pPrevStmt = pPg->pNextStmt = 0; + pHist->pPrevStmt = pHist->pNextStmt = 0; pPg = pPg->pDirty; } pPager->pDirty = 0; @@ -3392,22 +4634,14 @@ int sqlite3pager_commit(Pager *pPager){ #endif pPager->pStmt = 0; pPager->state = PAGER_SHARED; + pagerLeave(pPager); return SQLITE_OK; } - if( pPager->dirtyCache==0 ){ - /* Exit early (without doing the time-consuming sqlite3OsSync() calls) - ** if there have been no changes to the database file. */ - assert( pPager->needSync==0 ); - rc = pager_unwritelock(pPager); - pPager->dbSize = -1; - return rc; - } - assert( pPager->journalOpen ); - rc = sqlite3pager_sync(pPager, 0, 0); - if( rc==SQLITE_OK ){ - rc = pager_unwritelock(pPager); - pPager->dbSize = -1; - } + assert( pPager->journalOpen || !pPager->dirtyCache ); + assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache ); + rc = pager_end_transaction(pPager); + rc = pager_error(pPager, rc); + pagerLeave(pPager); return rc; } @@ -3417,15 +4651,15 @@ int sqlite3pager_commit(Pager *pPager){ ** The journal is deleted. ** ** This routine cannot fail unless some other process is not following -** the correct locking protocol (SQLITE_PROTOCOL) or unless some other +** the correct locking protocol or unless some other ** process is writing trash into the journal file (SQLITE_CORRUPT) or ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error ** codes are returned for all these occasions. Otherwise, ** SQLITE_OK is returned. */ -int sqlite3pager_rollback(Pager *pPager){ +int sqlite3PagerRollback(Pager *pPager){ int rc; - TRACE2("ROLLBACK %d\n", PAGERID(pPager)); + PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager)); if( MEMDB ){ PgHdr *p; for(p=pPager->pAll; p; p=p->pNextAll){ @@ -3440,71 +4674,76 @@ int sqlite3pager_rollback(Pager *pPager){ pHist = PGHDR_TO_HIST(p, pPager); if( pHist->pOrig ){ memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize); - TRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager)); + PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager)); }else{ - TRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager)); + PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager)); } clearHistory(pHist); p->dirty = 0; p->inJournal = 0; - p->inStmt = 0; - p->pPrevStmt = p->pNextStmt = 0; + pHist->inStmt = 0; + pHist->pPrevStmt = pHist->pNextStmt = 0; if( pPager->xReiniter ){ - pPager->xReiniter(PGHDR_TO_DATA(p), pPager->pageSize); + pPager->xReiniter(p, pPager->pageSize); } } pPager->pDirty = 0; pPager->pStmt = 0; pPager->dbSize = pPager->origDbSize; - memoryTruncate(pPager); + pager_truncate_cache(pPager); pPager->stmtInUse = 0; pPager->state = PAGER_SHARED; return SQLITE_OK; } + pagerEnter(pPager); if( !pPager->dirtyCache || !pPager->journalOpen ){ - rc = pager_unwritelock(pPager); - pPager->dbSize = -1; + rc = pager_end_transaction(pPager); + pagerLeave(pPager); return rc; } if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ if( pPager->state>=PAGER_EXCLUSIVE ){ - pager_playback(pPager); + pager_playback(pPager, 0); } + pagerLeave(pPager); return pPager->errCode; } if( pPager->state==PAGER_RESERVED ){ int rc2; - rc = pager_reload_cache(pPager); - rc2 = pager_unwritelock(pPager); + rc = pager_playback(pPager, 0); + rc2 = pager_end_transaction(pPager); if( rc==SQLITE_OK ){ rc = rc2; } }else{ - rc = pager_playback(pPager); + rc = pager_playback(pPager, 0); } + /* pager_reset(pPager); */ pPager->dbSize = -1; /* If an error occurs during a ROLLBACK, we can no longer trust the pager ** cache. So call pager_error() on the way out to make any error ** persistent. */ - return pager_error(pPager, rc); + rc = pager_error(pPager, rc); + pagerLeave(pPager); + return rc; } /* ** Return TRUE if the database file is opened read-only. Return FALSE ** if the database is (in theory) writable. */ -int sqlite3pager_isreadonly(Pager *pPager){ +int sqlite3PagerIsreadonly(Pager *pPager){ return pPager->readOnly; } /* ** Return the number of references to the pager. */ -int sqlite3pager_refcount(Pager *pPager){ +int sqlite3PagerRefcount(Pager *pPager){ return pPager->nRef; } @@ -3512,7 +4751,7 @@ int sqlite3pager_refcount(Pager *pPager){ /* ** This routine is used for testing and analysis only. */ -int *sqlite3pager_stats(Pager *pPager){ +int *sqlite3PagerStats(Pager *pPager){ static int a[11]; a[0] = pPager->nRef; a[1] = pPager->nPage; @@ -3522,7 +4761,7 @@ int *sqlite3pager_stats(Pager *pPager){ a[5] = pPager->errCode; a[6] = pPager->nHit; a[7] = pPager->nMiss; - a[8] = pPager->nOvfl; + a[8] = 0; /* Used to be pPager->nOvfl */ a[9] = pPager->nRead; a[10] = pPager->nWrite; return a; @@ -3536,12 +4775,12 @@ int *sqlite3pager_stats(Pager *pPager){ ** open. A new statement journal is created that can be used to rollback ** changes of a single SQL command within a larger transaction. */ -int sqlite3pager_stmt_begin(Pager *pPager){ +static int pagerStmtBegin(Pager *pPager){ int rc; - char zTemp[SQLITE_TEMPNAME_SIZE]; assert( !pPager->stmtInUse ); + assert( pPager->state>=PAGER_SHARED ); assert( pPager->dbSize>=0 ); - TRACE2("STMT-BEGIN %d\n", PAGERID(pPager)); + PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager)); if( MEMDB ){ pPager->stmtInUse = 1; pPager->stmtSize = pPager->dbSize; @@ -3552,7 +4791,10 @@ int sqlite3pager_stmt_begin(Pager *pPager){ return SQLITE_OK; } assert( pPager->journalOpen ); - pPager->aInStmt = sqliteMalloc( pPager->dbSize/8 + 1 ); + pagerLeave(pPager); + assert( pPager->aInStmt==0 ); + pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 ); + pagerEnter(pPager); if( pPager->aInStmt==0 ){ /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */ return SQLITE_NOMEM; @@ -3567,8 +4809,11 @@ int sqlite3pager_stmt_begin(Pager *pPager){ pPager->stmtHdrOff = 0; pPager->stmtCksum = pPager->cksumInit; if( !pPager->stmtOpen ){ - rc = sqlite3pager_opentemp(zTemp, &pPager->stfd); - if( rc ) goto stmt_begin_failed; + rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl, + SQLITE_OPEN_SUBJOURNAL); + if( rc ){ + goto stmt_begin_failed; + } pPager->stmtOpen = 1; pPager->stmtNRec = 0; } @@ -3577,33 +4822,39 @@ int sqlite3pager_stmt_begin(Pager *pPager){ stmt_begin_failed: if( pPager->aInStmt ){ - sqliteFree(pPager->aInStmt); + sqlite3_free(pPager->aInStmt); pPager->aInStmt = 0; } return rc; } +int sqlite3PagerStmtBegin(Pager *pPager){ + int rc; + pagerEnter(pPager); + rc = pagerStmtBegin(pPager); + pagerLeave(pPager); + return rc; +} /* ** Commit a statement. */ -int sqlite3pager_stmt_commit(Pager *pPager){ +int sqlite3PagerStmtCommit(Pager *pPager){ + pagerEnter(pPager); if( pPager->stmtInUse ){ PgHdr *pPg, *pNext; - TRACE2("STMT-COMMIT %d\n", PAGERID(pPager)); + PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager)); if( !MEMDB ){ - sqlite3OsSeek(pPager->stfd, 0); /* sqlite3OsTruncate(pPager->stfd, 0); */ - sqliteFree( pPager->aInStmt ); + sqlite3_free( pPager->aInStmt ); pPager->aInStmt = 0; - } - for(pPg=pPager->pStmt; pPg; pPg=pNext){ - pNext = pPg->pNextStmt; - assert( pPg->inStmt ); - pPg->inStmt = 0; - pPg->pPrevStmt = pPg->pNextStmt = 0; - if( MEMDB ){ + }else{ + for(pPg=pPager->pStmt; pPg; pPg=pNext){ PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); - sqliteFree(pHist->pStmt); + pNext = pHist->pNextStmt; + assert( pHist->inStmt ); + pHist->inStmt = 0; + pHist->pPrevStmt = pHist->pNextStmt = 0; + sqlite3_free(pHist->pStmt); pHist->pStmt = 0; } } @@ -3612,58 +4863,78 @@ int sqlite3pager_stmt_commit(Pager *pPager){ pPager->pStmt = 0; } pPager->stmtAutoopen = 0; + pagerLeave(pPager); return SQLITE_OK; } /* ** Rollback a statement. */ -int sqlite3pager_stmt_rollback(Pager *pPager){ +int sqlite3PagerStmtRollback(Pager *pPager){ int rc; + pagerEnter(pPager); if( pPager->stmtInUse ){ - TRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager)); + PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager)); if( MEMDB ){ PgHdr *pPg; - for(pPg=pPager->pStmt; pPg; pPg=pPg->pNextStmt){ - PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); + PgHistory *pHist; + for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){ + pHist = PGHDR_TO_HIST(pPg, pPager); if( pHist->pStmt ){ memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize); - sqliteFree(pHist->pStmt); + sqlite3_free(pHist->pStmt); pHist->pStmt = 0; } } pPager->dbSize = pPager->stmtSize; - memoryTruncate(pPager); + pager_truncate_cache(pPager); rc = SQLITE_OK; }else{ rc = pager_stmt_playback(pPager); } - sqlite3pager_stmt_commit(pPager); + sqlite3PagerStmtCommit(pPager); }else{ rc = SQLITE_OK; } pPager->stmtAutoopen = 0; + pagerLeave(pPager); return rc; } /* ** Return the full pathname of the database file. */ -const char *sqlite3pager_filename(Pager *pPager){ +const char *sqlite3PagerFilename(Pager *pPager){ return pPager->zFilename; } /* +** Return the VFS structure for the pager. +*/ +const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ + return pPager->pVfs; +} + +/* +** Return the file handle for the database file associated +** with the pager. This might return NULL if the file has +** not yet been opened. +*/ +sqlite3_file *sqlite3PagerFile(Pager *pPager){ + return pPager->fd; +} + +/* ** Return the directory of the database file. */ -const char *sqlite3pager_dirname(Pager *pPager){ +const char *sqlite3PagerDirname(Pager *pPager){ return pPager->zDirectory; } /* ** Return the full pathname of the journal file. */ -const char *sqlite3pager_journalname(Pager *pPager){ +const char *sqlite3PagerJournalname(Pager *pPager){ return pPager->zJournal; } @@ -3671,14 +4942,15 @@ const char *sqlite3pager_journalname(Pager *pPager){ ** Return true if fsync() calls are disabled for this pager. Return FALSE ** if fsync()s are executed normally. */ -int sqlite3pager_nosync(Pager *pPager){ +int sqlite3PagerNosync(Pager *pPager){ return pPager->noSync; } +#ifdef SQLITE_HAS_CODEC /* ** Set the codec for this pager */ -void sqlite3pager_set_codec( +void sqlite3PagerSetCodec( Pager *pPager, void *(*xCodec)(void*,void*,Pgno,int), void *pCodecArg @@ -3686,138 +4958,19 @@ void sqlite3pager_set_codec( pPager->xCodec = xCodec; pPager->pCodecArg = pCodecArg; } - -/* -** This routine is called to increment the database file change-counter, -** stored at byte 24 of the pager file. -*/ -static int pager_incr_changecounter(Pager *pPager){ - void *pPage; - PgHdr *pPgHdr; - u32 change_counter; - int rc; - - /* Open page 1 of the file for writing. */ - rc = sqlite3pager_get(pPager, 1, &pPage); - if( rc!=SQLITE_OK ) return rc; - rc = sqlite3pager_write(pPage); - if( rc!=SQLITE_OK ) return rc; - - /* Read the current value at byte 24. */ - pPgHdr = DATA_TO_PGHDR(pPage); - change_counter = retrieve32bits(pPgHdr, 24); - - /* Increment the value just read and write it back to byte 24. */ - change_counter++; - put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter); - - /* Release the page reference. */ - sqlite3pager_unref(pPage); - return SQLITE_OK; -} - -/* -** Sync the database file for the pager pPager. zMaster points to the name -** of a master journal file that should be written into the individual -** journal file. zMaster may be NULL, which is interpreted as no master -** journal (a single database transaction). -** -** This routine ensures that the journal is synced, all dirty pages written -** to the database file and the database file synced. The only thing that -** remains to commit the transaction is to delete the journal file (or -** master journal file if specified). -** -** Note that if zMaster==NULL, this does not overwrite a previous value -** passed to an sqlite3pager_sync() call. -** -** If parameter nTrunc is non-zero, then the pager file is truncated to -** nTrunc pages (this is used by auto-vacuum databases). -*/ -int sqlite3pager_sync(Pager *pPager, const char *zMaster, Pgno nTrunc){ - int rc = SQLITE_OK; - - TRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n", - pPager->zFilename, zMaster, nTrunc); - - /* If this is an in-memory db, or no pages have been written to, or this - ** function has already been called, it is a no-op. - */ - if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){ - PgHdr *pPg; - assert( pPager->journalOpen ); - - /* If a master journal file name has already been written to the - ** journal file, then no sync is required. This happens when it is - ** written, then the process fails to upgrade from a RESERVED to an - ** EXCLUSIVE lock. The next time the process tries to commit the - ** transaction the m-j name will have already been written. - */ - if( !pPager->setMaster ){ - rc = pager_incr_changecounter(pPager); - if( rc!=SQLITE_OK ) goto sync_exit; -#ifndef SQLITE_OMIT_AUTOVACUUM - if( nTrunc!=0 ){ - /* If this transaction has made the database smaller, then all pages - ** being discarded by the truncation must be written to the journal - ** file. - */ - Pgno i; - void *pPage; - int iSkip = PAGER_MJ_PGNO(pPager); - for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){ - if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){ - rc = sqlite3pager_get(pPager, i, &pPage); - if( rc!=SQLITE_OK ) goto sync_exit; - rc = sqlite3pager_write(pPage); - sqlite3pager_unref(pPage); - if( rc!=SQLITE_OK ) goto sync_exit; - } - } - } -#endif - rc = writeMasterJournal(pPager, zMaster); - if( rc!=SQLITE_OK ) goto sync_exit; - rc = syncJournal(pPager); - if( rc!=SQLITE_OK ) goto sync_exit; - } - -#ifndef SQLITE_OMIT_AUTOVACUUM - if( nTrunc!=0 ){ - rc = sqlite3pager_truncate(pPager, nTrunc); - if( rc!=SQLITE_OK ) goto sync_exit; - } #endif - /* Write all dirty pages to the database file */ - pPg = pager_get_all_dirty_pages(pPager); - rc = pager_write_pagelist(pPg); - if( rc!=SQLITE_OK ) goto sync_exit; - - /* Sync the database file. */ - if( !pPager->noSync ){ - rc = sqlite3OsSync(pPager->fd, 0); - } - - pPager->state = PAGER_SYNCED; - }else if( MEMDB && nTrunc!=0 ){ - rc = sqlite3pager_truncate(pPager, nTrunc); - } - -sync_exit: - return rc; -} - #ifndef SQLITE_OMIT_AUTOVACUUM /* -** Move the page identified by pData to location pgno in the file. +** Move the page pPg to location pgno in the file. ** -** There must be no references to the current page pgno. If current page -** pgno is not already in the rollback journal, it is not written there by -** by this routine. The same applies to the page pData refers to on entry to -** this routine. +** There must be no references to the page previously located at +** pgno (which we call pPgOld) though that page is allowed to be +** in cache. If the page previous located at pgno is not already +** in the rollback journal, it is not put there by by this routine. ** -** References to the page refered to by pData remain valid. Updating any -** meta-data associated with page pData (i.e. data stored in the nExtra bytes +** References to the page pPg remain valid. Updating any +** meta-data associated with pPg (i.e. data stored in the nExtra bytes ** allocated along with the page) is the responsibility of the caller. ** ** A transaction must be active when this routine is called. It used to be @@ -3825,43 +4978,49 @@ sync_exit: ** has been removed (CREATE INDEX needs to move a page when a statement ** transaction is active). */ -int sqlite3pager_movepage(Pager *pPager, void *pData, Pgno pgno){ - PgHdr *pPg = DATA_TO_PGHDR(pData); - PgHdr *pPgOld; +int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){ + PgHdr *pPgOld; /* The page being overwritten. */ int h; Pgno needSyncPgno = 0; + pagerEnter(pPager); assert( pPg->nRef>0 ); - TRACE5("MOVE %d page %d (needSync=%d) moves to %d\n", + PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n", PAGERID(pPager), pPg->pgno, pPg->needSync, pgno); + IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno)) + pager_get_content(pPg); if( pPg->needSync ){ needSyncPgno = pPg->pgno; - assert( pPg->inJournal ); + assert( pPg->inJournal || (int)pgno>pPager->origDbSize ); assert( pPg->dirty ); assert( pPager->needSync ); } - /* Unlink pPg from it's hash-chain */ + /* Unlink pPg from its hash-chain */ unlinkHashChain(pPager, pPg); /* If the cache contains a page with page-number pgno, remove it - ** from it's hash chain. Also, if the PgHdr.needSync was set for + ** from its hash chain. Also, if the PgHdr.needSync was set for ** page pgno before the 'move' operation, it needs to be retained ** for the page moved there. */ + pPg->needSync = 0; pPgOld = pager_lookup(pPager, pgno); if( pPgOld ){ assert( pPgOld->nRef==0 ); unlinkHashChain(pPager, pPgOld); makeClean(pPgOld); - if( pPgOld->needSync ){ - assert( pPgOld->inJournal ); - pPg->inJournal = 1; - pPg->needSync = 1; - assert( pPager->needSync ); - } + pPg->needSync = pPgOld->needSync; + }else{ + pPg->needSync = 0; + } + if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){ + pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; + }else{ + pPg->inJournal = 0; + assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize ); } /* Change the page number for pPg and insert it into the new hash-chain. */ @@ -3886,41 +5045,82 @@ int sqlite3pager_movepage(Pager *pPager, void *pData, Pgno pgno){ ** Pager.aInJournal bit has been set. This needs to be remedied by loading ** the page into the pager-cache and setting the PgHdr.needSync flag. ** - ** The sqlite3pager_get() call may cause the journal to sync. So make + ** If the attempt to load the page into the page-cache fails, (due + ** to a malloc() or IO failure), clear the bit in the aInJournal[] + ** array. Otherwise, if the page is loaded and written again in + ** this transaction, it may be written to the database file before + ** it is synced into the journal file. This way, it may end up in + ** the journal file twice, but that is not a problem. + ** + ** The sqlite3PagerGet() call may cause the journal to sync. So make ** sure the Pager.needSync flag is set too. */ int rc; - void *pNeedSync; + PgHdr *pPgHdr; assert( pPager->needSync ); - rc = sqlite3pager_get(pPager, needSyncPgno, &pNeedSync); - if( rc!=SQLITE_OK ) return rc; + rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); + if( rc!=SQLITE_OK ){ + if( pPager->aInJournal && (int)needSyncPgno<=pPager->origDbSize ){ + pPager->aInJournal[needSyncPgno/8] &= ~(1<<(needSyncPgno&7)); + } + pagerLeave(pPager); + return rc; + } pPager->needSync = 1; - DATA_TO_PGHDR(pNeedSync)->needSync = 1; - DATA_TO_PGHDR(pNeedSync)->inJournal = 1; - makeDirty(DATA_TO_PGHDR(pNeedSync)); - sqlite3pager_unref(pNeedSync); + pPgHdr->needSync = 1; + pPgHdr->inJournal = 1; + makeDirty(pPgHdr); + sqlite3PagerUnref(pPgHdr); } + pagerLeave(pPager); return SQLITE_OK; } #endif -#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) /* -** Return the current state of the file lock for the given pager. -** The return value is one of NO_LOCK, SHARED_LOCK, RESERVED_LOCK, -** PENDING_LOCK, or EXCLUSIVE_LOCK. +** Return a pointer to the data for the specified page. */ -int sqlite3pager_lockstate(Pager *pPager){ - return sqlite3OsLockState(pPager->fd); +void *sqlite3PagerGetData(DbPage *pPg){ + return PGHDR_TO_DATA(pPg); } -#endif -#ifdef SQLITE_DEBUG +/* +** Return a pointer to the Pager.nExtra bytes of "extra" space +** allocated along with the specified page. +*/ +void *sqlite3PagerGetExtra(DbPage *pPg){ + Pager *pPager = pPg->pPager; + return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0); +} + +/* +** Get/set the locking-mode for this pager. Parameter eMode must be one +** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or +** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then +** the locking-mode is set to the value specified. +** +** The returned value is either PAGER_LOCKINGMODE_NORMAL or +** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated) +** locking-mode. +*/ +int sqlite3PagerLockingMode(Pager *pPager, int eMode){ + assert( eMode==PAGER_LOCKINGMODE_QUERY + || eMode==PAGER_LOCKINGMODE_NORMAL + || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); + assert( PAGER_LOCKINGMODE_QUERY<0 ); + assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 ); + if( eMode>=0 && !pPager->tempFile ){ + pPager->exclusiveMode = eMode; + } + return (int)pPager->exclusiveMode; +} + +#ifdef SQLITE_TEST /* ** Print a listing of all referenced pages and their ref count. */ -void sqlite3pager_refdump(Pager *pPager){ +void sqlite3PagerRefdump(Pager *pPager){ PgHdr *pPg; for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ if( pPg->nRef<=0 ) continue; |
