drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 12 | ** This is the implementation of the page cache subsystem or "pager". |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 13 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 14 | ** The pager is used to access a database disk file. It implements |
| 15 | ** atomic commit and rollback through the use of a journal file that |
| 16 | ** is separate from the database file. The pager also implements file |
| 17 | ** locking to prevent two processes from writing the same database |
| 18 | ** file simultaneously, or one process from reading the database while |
| 19 | ** another is writing. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 20 | ** |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame^] | 21 | ** @(#) $Id: pager.c,v 1.31 2001/11/22 00:01:27 drh Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 22 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 23 | #include "sqliteInt.h" |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 24 | #include "pager.h" |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 25 | #include "os.h" |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 26 | #include <assert.h> |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 27 | #include <string.h> |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | ** The page cache as a whole is always in one of the following |
| 31 | ** states: |
| 32 | ** |
| 33 | ** SQLITE_UNLOCK The page cache is not currently reading or |
| 34 | ** writing the database file. There is no |
| 35 | ** data held in memory. This is the initial |
| 36 | ** state. |
| 37 | ** |
| 38 | ** SQLITE_READLOCK The page cache is reading the database. |
| 39 | ** Writing is not permitted. There can be |
| 40 | ** multiple readers accessing the same database |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 41 | ** file at the same time. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 42 | ** |
| 43 | ** SQLITE_WRITELOCK The page cache is writing the database. |
| 44 | ** Access is exclusive. No other processes or |
| 45 | ** threads can be reading or writing while one |
| 46 | ** process is writing. |
| 47 | ** |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 48 | ** The page cache comes up in SQLITE_UNLOCK. The first time a |
| 49 | ** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 50 | ** After all pages have been released using sqlite_page_unref(), |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 51 | ** the state transitions back to SQLITE_UNLOCK. The first time |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 52 | ** that sqlite_page_write() is called, the state transitions to |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 53 | ** SQLITE_WRITELOCK. (Note that sqlite_page_write() can only be |
| 54 | ** called on an outstanding page which means that the pager must |
| 55 | ** be in SQLITE_READLOCK before it transitions to SQLITE_WRITELOCK.) |
| 56 | ** The sqlite_page_rollback() and sqlite_page_commit() functions |
| 57 | ** transition the state from SQLITE_WRITELOCK back to SQLITE_READLOCK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 58 | */ |
| 59 | #define SQLITE_UNLOCK 0 |
| 60 | #define SQLITE_READLOCK 1 |
| 61 | #define SQLITE_WRITELOCK 2 |
| 62 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 63 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 64 | /* |
| 65 | ** Each in-memory image of a page begins with the following header. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 66 | ** This header is only visible to this pager module. The client |
| 67 | ** code that calls pager sees only the data that follows the header. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 68 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 69 | typedef struct PgHdr PgHdr; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 70 | struct PgHdr { |
| 71 | Pager *pPager; /* The pager to which this page belongs */ |
| 72 | Pgno pgno; /* The page number for this page */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 73 | PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 74 | int nRef; /* Number of users of this page */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 75 | PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */ |
| 76 | PgHdr *pNextAll, *pPrevAll; /* A list of all pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 77 | char inJournal; /* TRUE if has been written to journal */ |
| 78 | char dirty; /* TRUE if we need to write back changes */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 79 | /* SQLITE_PAGE_SIZE bytes of page data follow this header */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 80 | /* Pager.nExtra bytes of local data follow the page data */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | /* |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 84 | ** Convert a pointer to a PgHdr into a pointer to its data |
| 85 | ** and back again. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 86 | */ |
| 87 | #define PGHDR_TO_DATA(P) ((void*)(&(P)[1])) |
| 88 | #define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1]) |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 89 | #define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE]) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 90 | |
| 91 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 92 | ** How big to make the hash table used for locating in-memory pages |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 93 | ** by page number. Knuth says this should be a prime number. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 94 | */ |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 95 | #define N_PG_HASH 373 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | ** A open page cache is an instance of the following structure. |
| 99 | */ |
| 100 | struct Pager { |
| 101 | char *zFilename; /* Name of the database file */ |
| 102 | char *zJournal; /* Name of the journal file */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 103 | OsFile fd, jfd; /* File descriptors for database and journal */ |
| 104 | int journalOpen; /* True if journal file descriptors is valid */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 105 | int dbSize; /* Number of pages in the file */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 106 | int origDbSize; /* dbSize before the current change */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 107 | int nExtra; /* Add this many bytes to each in-memory page */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 108 | void (*xDestructor)(void*); /* Call this routine when freeing pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 109 | int nPage; /* Total number of in-memory pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 110 | int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 111 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 112 | int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */ |
| 113 | unsigned char state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */ |
| 114 | unsigned char errMask; /* One of several kinds of errors */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 115 | unsigned char tempFile; /* zFilename is a temporary file */ |
| 116 | unsigned char readOnly; /* True for a read-only database */ |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 117 | unsigned char needSync; /* True if an fsync() is needed on the journal */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 118 | unsigned char *aInJournal; /* One bit for each page in the database file */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 119 | PgHdr *pFirst, *pLast; /* List of free pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 120 | PgHdr *pAll; /* List of all pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 121 | PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | /* |
| 125 | ** These are bits that can be set in Pager.errMask. |
| 126 | */ |
| 127 | #define PAGER_ERR_FULL 0x01 /* a write() failed */ |
| 128 | #define PAGER_ERR_MEM 0x02 /* malloc() failed */ |
| 129 | #define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */ |
| 130 | #define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 131 | #define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | ** The journal file contains page records in the following |
| 135 | ** format. |
| 136 | */ |
| 137 | typedef struct PageRecord PageRecord; |
| 138 | struct PageRecord { |
| 139 | Pgno pgno; /* The page number */ |
| 140 | char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */ |
| 141 | }; |
| 142 | |
| 143 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 144 | ** Journal files begin with the following magic string. The data |
| 145 | ** was obtained from /dev/random. It is used only as a sanity check. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 146 | */ |
| 147 | static const unsigned char aJournalMagic[] = { |
| 148 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4, |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | /* |
| 152 | ** Hash a page number |
| 153 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 154 | #define pager_hash(PN) ((PN)%N_PG_HASH) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 155 | |
| 156 | /* |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 157 | ** Enable reference count tracking here: |
| 158 | */ |
| 159 | #if SQLITE_TEST |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 160 | int pager_refinfo_enable = 0; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 161 | static void pager_refinfo(PgHdr *p){ |
| 162 | static int cnt = 0; |
| 163 | if( !pager_refinfo_enable ) return; |
| 164 | printf( |
| 165 | "REFCNT: %4d addr=0x%08x nRef=%d\n", |
| 166 | p->pgno, (int)PGHDR_TO_DATA(p), p->nRef |
| 167 | ); |
| 168 | cnt++; /* Something to set a breakpoint on */ |
| 169 | } |
| 170 | # define REFINFO(X) pager_refinfo(X) |
| 171 | #else |
| 172 | # define REFINFO(X) |
| 173 | #endif |
| 174 | |
| 175 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 176 | ** Convert the bits in the pPager->errMask into an approprate |
| 177 | ** return code. |
| 178 | */ |
| 179 | static int pager_errcode(Pager *pPager){ |
| 180 | int rc = SQLITE_OK; |
| 181 | if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 182 | if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 183 | if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL; |
| 184 | if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM; |
| 185 | if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT; |
| 186 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
| 190 | ** Find a page in the hash table given its page number. Return |
| 191 | ** a pointer to the page or NULL if not found. |
| 192 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 193 | static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 194 | PgHdr *p = pPager->aHash[pgno % N_PG_HASH]; |
| 195 | while( p && p->pgno!=pgno ){ |
| 196 | p = p->pNextHash; |
| 197 | } |
| 198 | return p; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | ** Unlock the database and clear the in-memory cache. This routine |
| 203 | ** sets the state of the pager back to what it was when it was first |
| 204 | ** opened. Any outstanding pages are invalidated and subsequent attempts |
| 205 | ** to access those pages will likely result in a coredump. |
| 206 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 207 | static void pager_reset(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 208 | PgHdr *pPg, *pNext; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 209 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 210 | pNext = pPg->pNextAll; |
| 211 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 212 | } |
| 213 | pPager->pFirst = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 214 | pPager->pLast = 0; |
| 215 | pPager->pAll = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 216 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 217 | pPager->nPage = 0; |
| 218 | if( pPager->state==SQLITE_WRITELOCK ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 219 | sqlitepager_rollback(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 220 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 221 | sqliteOsUnlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 222 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 223 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 224 | pPager->nRef = 0; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 225 | assert( pPager->journalOpen==0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
| 229 | ** When this routine is called, the pager has the journal file open and |
| 230 | ** a write lock on the database. This routine releases the database |
| 231 | ** write lock and acquires a read lock in its place. The journal file |
| 232 | ** is deleted and closed. |
| 233 | ** |
| 234 | ** We have to release the write lock before acquiring the read lock, |
| 235 | ** so there is a race condition where another process can get the lock |
| 236 | ** while we are not holding it. But, no other process should do this |
| 237 | ** because we are also holding a lock on the journal, and no process |
| 238 | ** should get a write lock on the database without first getting a lock |
| 239 | ** on the journal. So this routine should never fail. But it can fail |
| 240 | ** if another process is not playing by the rules. If it does fail, |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 241 | ** all in-memory cache pages are invalidated, the PAGER_ERR_LOCK bit |
| 242 | ** is set in pPager->errMask, and this routine returns SQLITE_PROTOCOL. |
| 243 | ** SQLITE_OK is returned on success. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 244 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 245 | static int pager_unwritelock(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 246 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 247 | PgHdr *pPg; |
| 248 | if( pPager->state!=SQLITE_WRITELOCK ) return SQLITE_OK; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 249 | sqliteOsUnlock(pPager->fd); |
| 250 | rc = sqliteOsLock(pPager->fd, 0); |
| 251 | sqliteOsClose(pPager->jfd); |
| 252 | pPager->journalOpen = 0; |
| 253 | sqliteOsDelete(pPager->zJournal); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 254 | sqliteFree( pPager->aInJournal ); |
| 255 | pPager->aInJournal = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 256 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 257 | pPg->inJournal = 0; |
| 258 | pPg->dirty = 0; |
| 259 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 260 | if( rc!=SQLITE_OK ){ |
| 261 | pPager->state = SQLITE_UNLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 262 | rc = SQLITE_PROTOCOL; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 263 | pPager->errMask |= PAGER_ERR_LOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 264 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 265 | rc = SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 266 | pPager->state = SQLITE_READLOCK; |
| 267 | } |
| 268 | return rc; |
| 269 | } |
| 270 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 271 | /* |
| 272 | ** Playback the journal and thus restore the database file to |
| 273 | ** the state it was in before we started making changes. |
| 274 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 275 | ** The journal file format is as follows: There is an initial |
| 276 | ** file-type string for sanity checking. Then there is a single |
| 277 | ** Pgno number which is the number of pages in the database before |
| 278 | ** changes were made. The database is truncated to this size. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 279 | ** Next come zero or more page records where each page record |
| 280 | ** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See |
| 281 | ** the PageRecord structure for details. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 282 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 283 | ** For playback, the pages have to be read from the journal in |
| 284 | ** reverse order and put back into the original database file. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 285 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 286 | ** If the file opened as the journal file is not a well-formed |
| 287 | ** journal file (as determined by looking at the magic number |
| 288 | ** at the beginning) then this routine returns SQLITE_PROTOCOL. |
| 289 | ** If any other errors occur during playback, the database will |
| 290 | ** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in |
| 291 | ** pPager->errMask and SQLITE_CORRUPT is returned. If it all |
| 292 | ** works, then this routine returns SQLITE_OK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 293 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 294 | static int pager_playback(Pager *pPager){ |
| 295 | int nRec; /* Number of Records */ |
| 296 | int i; /* Loop counter */ |
| 297 | Pgno mxPg = 0; /* Size of the original file in pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 298 | PgHdr *pPg; /* An existing page in the cache */ |
| 299 | PageRecord pgRec; |
| 300 | unsigned char aMagic[sizeof(aJournalMagic)]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 301 | int rc; |
| 302 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame^] | 303 | /* Figure out how many records are in the journal. Abort early if |
| 304 | ** the journal is empty. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 305 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 306 | assert( pPager->journalOpen ); |
| 307 | sqliteOsSeek(pPager->jfd, 0); |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame^] | 308 | rc = sqliteOsFileSize(pPager->jfd, &nRec); |
| 309 | if( rc!=SQLITE_OK ){ |
| 310 | goto end_playback; |
| 311 | } |
| 312 | nRec = (nRec - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord); |
| 313 | if( nRec<=0 ){ |
| 314 | goto end_playback; |
| 315 | } |
| 316 | |
| 317 | /* Read the beginning of the journal and truncate the |
| 318 | ** database file back to its original size. |
| 319 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 320 | rc = sqliteOsRead(pPager->jfd, aMagic, sizeof(aMagic)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 321 | if( rc!=SQLITE_OK || memcmp(aMagic,aJournalMagic,sizeof(aMagic))!=0 ){ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 322 | rc = SQLITE_PROTOCOL; |
| 323 | goto end_playback; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 324 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 325 | rc = sqliteOsRead(pPager->jfd, &mxPg, sizeof(mxPg)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 326 | if( rc!=SQLITE_OK ){ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 327 | goto end_playback; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 328 | } |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 329 | rc = sqliteOsTruncate(pPager->fd, mxPg*SQLITE_PAGE_SIZE); |
| 330 | if( rc!=SQLITE_OK ){ |
| 331 | goto end_playback; |
| 332 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 333 | pPager->dbSize = mxPg; |
| 334 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 335 | /* Process segments beginning with the last and working backwards |
| 336 | ** to the first. |
| 337 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 338 | for(i=nRec-1; i>=0; i--){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 339 | /* Seek to the beginning of the segment */ |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 340 | int ofst; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 341 | ofst = i*sizeof(PageRecord) + sizeof(aMagic) + sizeof(Pgno); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 342 | rc = sqliteOsSeek(pPager->jfd, ofst); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 343 | if( rc!=SQLITE_OK ) break; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 344 | rc = sqliteOsRead(pPager->jfd, &pgRec, sizeof(pgRec)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 345 | if( rc!=SQLITE_OK ) break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 346 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 347 | /* Sanity checking on the page */ |
| 348 | if( pgRec.pgno>mxPg || pgRec.pgno==0 ){ |
| 349 | rc = SQLITE_CORRUPT; |
| 350 | break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 351 | } |
| 352 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 353 | /* Playback the page. Update the in-memory copy of the page |
| 354 | ** at the same time, if there is one. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 355 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 356 | pPg = pager_lookup(pPager, pgRec.pgno); |
| 357 | if( pPg ){ |
| 358 | memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 359 | memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 360 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 361 | rc = sqliteOsSeek(pPager->fd, (pgRec.pgno-1)*SQLITE_PAGE_SIZE); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 362 | if( rc!=SQLITE_OK ) break; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 363 | rc = sqliteOsWrite(pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 364 | if( rc!=SQLITE_OK ) break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 365 | } |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 366 | |
| 367 | end_playback: |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 368 | if( rc!=SQLITE_OK ){ |
| 369 | pager_unwritelock(pPager); |
| 370 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 371 | rc = SQLITE_CORRUPT; |
| 372 | }else{ |
| 373 | rc = pager_unwritelock(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 374 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 375 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 379 | ** Change the maximum number of in-memory pages that are allowed. |
| 380 | */ |
| 381 | void sqlitepager_set_cachesize(Pager *pPager, int mxPage){ |
| 382 | if( mxPage>10 ){ |
| 383 | pPager->mxPage = mxPage; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 388 | ** Create a new page cache and put a pointer to the page cache in *ppPager. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 389 | ** The file to be cached need not exist. The file is not locked until |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 390 | ** the first call to sqlitepager_get() and is only held open until the |
| 391 | ** last page is released using sqlitepager_unref(). |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 392 | ** |
| 393 | ** If zFilename is NULL then a random temporary file is created and used |
| 394 | ** as the file to be cached. The file will be deleted automatically when |
| 395 | ** it is closed. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 396 | */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 397 | int sqlitepager_open( |
| 398 | Pager **ppPager, /* Return the Pager structure here */ |
| 399 | const char *zFilename, /* Name of the database file to open */ |
| 400 | int mxPage, /* Max number of in-memory cache pages */ |
| 401 | int nExtra /* Extra bytes append to each in-memory page */ |
| 402 | ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 403 | Pager *pPager; |
| 404 | int nameLen; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 405 | OsFile fd; |
| 406 | int rc; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 407 | int tempFile; |
| 408 | int readOnly = 0; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 409 | char zTemp[SQLITE_TEMPNAME_SIZE]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 410 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 411 | *ppPager = 0; |
| 412 | if( sqlite_malloc_failed ){ |
| 413 | return SQLITE_NOMEM; |
| 414 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 415 | if( zFilename ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 416 | rc = sqliteOsOpenReadWrite(zFilename, &fd, &readOnly); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 417 | tempFile = 0; |
| 418 | }else{ |
| 419 | int cnt = 8; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 420 | sqliteOsTempFileName(zTemp); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 421 | do{ |
| 422 | cnt--; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 423 | sqliteOsTempFileName(zTemp); |
| 424 | rc = sqliteOsOpenExclusive(zTemp, &fd); |
| 425 | }while( cnt>0 && rc!=SQLITE_OK ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 426 | zFilename = zTemp; |
| 427 | tempFile = 1; |
| 428 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 429 | if( rc!=SQLITE_OK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 430 | return SQLITE_CANTOPEN; |
| 431 | } |
| 432 | nameLen = strlen(zFilename); |
| 433 | pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 434 | if( pPager==0 ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 435 | sqliteOsClose(fd); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 436 | return SQLITE_NOMEM; |
| 437 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 438 | pPager->zFilename = (char*)&pPager[1]; |
| 439 | pPager->zJournal = &pPager->zFilename[nameLen+1]; |
| 440 | strcpy(pPager->zFilename, zFilename); |
| 441 | strcpy(pPager->zJournal, zFilename); |
| 442 | strcpy(&pPager->zJournal[nameLen], "-journal"); |
| 443 | pPager->fd = fd; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 444 | pPager->journalOpen = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 445 | pPager->nRef = 0; |
| 446 | pPager->dbSize = -1; |
| 447 | pPager->nPage = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 448 | pPager->mxPage = mxPage>5 ? mxPage : 10; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 449 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 450 | pPager->errMask = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 451 | pPager->tempFile = tempFile; |
| 452 | pPager->readOnly = readOnly; |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 453 | pPager->needSync = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 454 | pPager->pFirst = 0; |
| 455 | pPager->pLast = 0; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 456 | pPager->nExtra = nExtra; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 457 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 458 | *ppPager = pPager; |
| 459 | return SQLITE_OK; |
| 460 | } |
| 461 | |
| 462 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 463 | ** Set the destructor for this pager. If not NULL, the destructor is called |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 464 | ** when the reference count on each page reaches zero. The destructor can |
| 465 | ** be used to clean up information in the extra segment appended to each page. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 466 | ** |
| 467 | ** The destructor is not called as a result sqlitepager_close(). |
| 468 | ** Destructors are only called by sqlitepager_unref(). |
| 469 | */ |
| 470 | void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){ |
| 471 | pPager->xDestructor = xDesc; |
| 472 | } |
| 473 | |
| 474 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 475 | ** Return the total number of pages in the disk file associated with |
| 476 | ** pPager. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 477 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 478 | int sqlitepager_pagecount(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 479 | int n; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 480 | assert( pPager!=0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 481 | if( pPager->dbSize>=0 ){ |
| 482 | return pPager->dbSize; |
| 483 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 484 | if( sqliteOsFileSize(pPager->fd, &n)!=SQLITE_OK ){ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 485 | pPager->errMask |= PAGER_ERR_DISK; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 486 | return 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 487 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 488 | n /= SQLITE_PAGE_SIZE; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 489 | if( pPager->state!=SQLITE_UNLOCK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 490 | pPager->dbSize = n; |
| 491 | } |
| 492 | return n; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | ** Shutdown the page cache. Free all memory and close all files. |
| 497 | ** |
| 498 | ** If a transaction was in progress when this routine is called, that |
| 499 | ** transaction is rolled back. All outstanding pages are invalidated |
| 500 | ** and their memory is freed. Any attempt to use a page associated |
| 501 | ** with this page cache after this function returns will likely |
| 502 | ** result in a coredump. |
| 503 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 504 | int sqlitepager_close(Pager *pPager){ |
| 505 | PgHdr *pPg, *pNext; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 506 | switch( pPager->state ){ |
| 507 | case SQLITE_WRITELOCK: { |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 508 | sqlitepager_rollback(pPager); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 509 | sqliteOsUnlock(pPager->fd); |
| 510 | assert( pPager->journalOpen==0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 511 | break; |
| 512 | } |
| 513 | case SQLITE_READLOCK: { |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 514 | sqliteOsUnlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 515 | break; |
| 516 | } |
| 517 | default: { |
| 518 | /* Do nothing */ |
| 519 | break; |
| 520 | } |
| 521 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 522 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 523 | pNext = pPg->pNextAll; |
| 524 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 525 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 526 | sqliteOsClose(pPager->fd); |
| 527 | assert( pPager->journalOpen==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 528 | if( pPager->tempFile ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 529 | sqliteOsDelete(pPager->zFilename); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 530 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 531 | sqliteFree(pPager); |
| 532 | return SQLITE_OK; |
| 533 | } |
| 534 | |
| 535 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 536 | ** Return the page number for the given page data. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 537 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 538 | Pgno sqlitepager_pagenumber(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 539 | PgHdr *p = DATA_TO_PGHDR(pData); |
| 540 | return p->pgno; |
| 541 | } |
| 542 | |
| 543 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 544 | ** Increment the reference count for a page. If the page is |
| 545 | ** currently on the freelist (the reference count is zero) then |
| 546 | ** remove it from the freelist. |
| 547 | */ |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 548 | static void page_ref(PgHdr *pPg){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 549 | if( pPg->nRef==0 ){ |
| 550 | /* The page is currently on the freelist. Remove it. */ |
| 551 | if( pPg->pPrevFree ){ |
| 552 | pPg->pPrevFree->pNextFree = pPg->pNextFree; |
| 553 | }else{ |
| 554 | pPg->pPager->pFirst = pPg->pNextFree; |
| 555 | } |
| 556 | if( pPg->pNextFree ){ |
| 557 | pPg->pNextFree->pPrevFree = pPg->pPrevFree; |
| 558 | }else{ |
| 559 | pPg->pPager->pLast = pPg->pPrevFree; |
| 560 | } |
| 561 | pPg->pPager->nRef++; |
| 562 | } |
| 563 | pPg->nRef++; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 564 | REFINFO(pPg); |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /* |
| 568 | ** Increment the reference count for a page. The input pointer is |
| 569 | ** a reference to the page data. |
| 570 | */ |
| 571 | int sqlitepager_ref(void *pData){ |
| 572 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 573 | page_ref(pPg); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 574 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 578 | ** Sync the journal and then write all free dirty pages to the database |
| 579 | ** file. |
| 580 | ** |
| 581 | ** Writing all free dirty pages to the database after the sync is a |
| 582 | ** non-obvious optimization. fsync() is an expensive operation so we |
| 583 | ** want to minimize the number that occur. So after an fsync() is forced |
| 584 | ** and we are free to write dirty pages back to the database, it is best |
| 585 | ** to go ahead and do as much of that as possible to minimize the chance |
| 586 | ** of having to do another fsync() later on. Writing dirty free pages |
| 587 | ** in this way make database operations go up to 10 times faster. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 588 | */ |
| 589 | static int syncAllPages(Pager *pPager){ |
| 590 | PgHdr *pPg; |
| 591 | int rc = SQLITE_OK; |
| 592 | if( pPager->needSync ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 593 | rc = sqliteOsSync(pPager->jfd); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 594 | if( rc!=0 ) return rc; |
| 595 | pPager->needSync = 0; |
| 596 | } |
| 597 | for(pPg=pPager->pFirst; pPg; pPg=pPg->pNextFree){ |
| 598 | if( pPg->dirty ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 599 | sqliteOsSeek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE); |
| 600 | rc = sqliteOsWrite(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 601 | if( rc!=SQLITE_OK ) break; |
| 602 | pPg->dirty = 0; |
| 603 | } |
| 604 | } |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 605 | return rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 609 | ** Acquire a page. |
| 610 | ** |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 611 | ** A read lock on the disk file is obtained when the first page is acquired. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 612 | ** This read lock is dropped when the last page is released. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 613 | ** |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 614 | ** A _get works for any page number greater than 0. If the database |
| 615 | ** file is smaller than the requested page, then no actual disk |
| 616 | ** read occurs and the memory image of the page is initialized to |
| 617 | ** all zeros. The extra data appended to a page is always initialized |
| 618 | ** to zeros the first time a page is loaded into memory. |
| 619 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 620 | ** The acquisition might fail for several reasons. In all cases, |
| 621 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 622 | ** |
| 623 | ** See also sqlitepager_lookup(). Both this routine and _lookup() attempt |
| 624 | ** to find a page in the in-memory cache first. If the page is not already |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 625 | ** in memory, this routine goes to disk to read it in whereas _lookup() |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 626 | ** just returns 0. This routine acquires a read-lock the first time it |
| 627 | ** has to go to disk, and could also playback an old journal if necessary. |
| 628 | ** Since _lookup() never goes to disk, it never has to deal with locks |
| 629 | ** or journal files. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 630 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 631 | int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 632 | PgHdr *pPg; |
| 633 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 634 | /* Make sure we have not hit any critical errors. |
| 635 | */ |
| 636 | if( pPager==0 || pgno==0 ){ |
| 637 | return SQLITE_ERROR; |
| 638 | } |
| 639 | if( pPager->errMask & ~(PAGER_ERR_FULL) ){ |
| 640 | return pager_errcode(pPager); |
| 641 | } |
| 642 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 643 | /* If this is the first page accessed, then get a read lock |
| 644 | ** on the database file. |
| 645 | */ |
| 646 | if( pPager->nRef==0 ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 647 | if( sqliteOsLock(pPager->fd, 0)!=SQLITE_OK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 648 | *ppPage = 0; |
| 649 | return SQLITE_BUSY; |
| 650 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 651 | pPager->state = SQLITE_READLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 652 | |
| 653 | /* If a journal file exists, try to play it back. |
| 654 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 655 | if( sqliteOsFileExists(pPager->zJournal) ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 656 | int rc, dummy; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 657 | |
| 658 | /* Open the journal for exclusive access. Return SQLITE_BUSY if |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 659 | ** we cannot get exclusive access to the journal file. |
| 660 | ** |
| 661 | ** Even though we will only be reading from the journal, not writing, |
| 662 | ** we have to open the journal for writing in order to obtain an |
| 663 | ** exclusive access lock. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 664 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 665 | rc = sqliteOsOpenReadWrite(pPager->zJournal, &pPager->jfd, &dummy); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 666 | if( rc==SQLITE_OK ){ |
| 667 | pPager->journalOpen = 1; |
| 668 | } |
| 669 | if( rc!=SQLITE_OK || sqliteOsLock(pPager->jfd, 1)!=SQLITE_OK ){ |
| 670 | if( pPager->journalOpen ){ |
| 671 | sqliteOsClose(pPager->jfd); |
| 672 | pPager->journalOpen = 0; |
| 673 | } |
| 674 | sqliteOsUnlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 675 | *ppPage = 0; |
| 676 | return SQLITE_BUSY; |
| 677 | } |
| 678 | |
| 679 | /* Get a write lock on the database */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 680 | sqliteOsUnlock(pPager->fd); |
| 681 | if( sqliteOsLock(pPager->fd, 1)!=SQLITE_OK ){ |
| 682 | sqliteOsClose(pPager->jfd); |
| 683 | pPager->journalOpen = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 684 | *ppPage = 0; |
| 685 | return SQLITE_PROTOCOL; |
| 686 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 687 | pPager->state = SQLITE_WRITELOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 688 | |
| 689 | /* Playback and delete the journal. Drop the database write |
| 690 | ** lock and reacquire the read lock. |
| 691 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 692 | rc = pager_playback(pPager); |
| 693 | if( rc!=SQLITE_OK ){ |
| 694 | return rc; |
| 695 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 696 | } |
| 697 | pPg = 0; |
| 698 | }else{ |
| 699 | /* Search for page in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 700 | pPg = pager_lookup(pPager, pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 701 | } |
| 702 | if( pPg==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 703 | /* The requested page is not in the page cache. */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 704 | int h; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 705 | pPager->nMiss++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 706 | if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){ |
| 707 | /* Create a new page */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 708 | pPg = sqliteMalloc( sizeof(*pPg) + SQLITE_PAGE_SIZE + pPager->nExtra ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 709 | if( pPg==0 ){ |
| 710 | *ppPage = 0; |
| 711 | pager_unwritelock(pPager); |
| 712 | pPager->errMask |= PAGER_ERR_MEM; |
| 713 | return SQLITE_NOMEM; |
| 714 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 715 | pPg->pPager = pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 716 | pPg->pNextAll = pPager->pAll; |
| 717 | if( pPager->pAll ){ |
| 718 | pPager->pAll->pPrevAll = pPg; |
| 719 | } |
| 720 | pPg->pPrevAll = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 721 | pPager->pAll = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 722 | pPager->nPage++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 723 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 724 | /* Recycle an older page. First locate the page to be recycled. |
| 725 | ** Try to find one that is not dirty and is near the head of |
| 726 | ** of the free list */ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 727 | int cnt = pPager->mxPage/2; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 728 | pPg = pPager->pFirst; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 729 | while( pPg->dirty && 0<cnt-- && pPg->pNextFree ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 730 | pPg = pPg->pNextFree; |
| 731 | } |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 732 | |
| 733 | /* If we could not find a page that has not been used recently |
| 734 | ** and which is not dirty, then sync the journal and write all |
| 735 | ** dirty free pages into the database file, thus making them |
| 736 | ** clean pages and available for recycling. |
| 737 | ** |
| 738 | ** We have to sync the journal before writing a page to the main |
| 739 | ** database. But syncing is a very slow operation. So after a |
| 740 | ** sync, it is best to write everything we can back to the main |
| 741 | ** database to minimize the risk of having to sync again in the |
| 742 | ** near future. That is way we write all dirty pages after a |
| 743 | ** sync. |
| 744 | */ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 745 | if( pPg==0 || pPg->dirty ){ |
| 746 | int rc = syncAllPages(pPager); |
| 747 | if( rc!=0 ){ |
| 748 | sqlitepager_rollback(pPager); |
| 749 | *ppPage = 0; |
| 750 | return SQLITE_IOERR; |
| 751 | } |
| 752 | pPg = pPager->pFirst; |
| 753 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 754 | assert( pPg->nRef==0 ); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 755 | assert( pPg->dirty==0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 756 | |
| 757 | /* Unlink the old page from the free list and the hash table |
| 758 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 759 | if( pPg->pPrevFree ){ |
| 760 | pPg->pPrevFree->pNextFree = pPg->pNextFree; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 761 | }else{ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 762 | assert( pPager->pFirst==pPg ); |
| 763 | pPager->pFirst = pPg->pNextFree; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 764 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 765 | if( pPg->pNextFree ){ |
| 766 | pPg->pNextFree->pPrevFree = pPg->pPrevFree; |
| 767 | }else{ |
| 768 | assert( pPager->pLast==pPg ); |
| 769 | pPager->pLast = pPg->pPrevFree; |
| 770 | } |
| 771 | pPg->pNextFree = pPg->pPrevFree = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 772 | if( pPg->pNextHash ){ |
| 773 | pPg->pNextHash->pPrevHash = pPg->pPrevHash; |
| 774 | } |
| 775 | if( pPg->pPrevHash ){ |
| 776 | pPg->pPrevHash->pNextHash = pPg->pNextHash; |
| 777 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 778 | h = pager_hash(pPg->pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 779 | assert( pPager->aHash[h]==pPg ); |
| 780 | pPager->aHash[h] = pPg->pNextHash; |
| 781 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 782 | pPg->pNextHash = pPg->pPrevHash = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 783 | pPager->nOvfl++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 784 | } |
| 785 | pPg->pgno = pgno; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 786 | if( pPager->aInJournal && pgno<=pPager->origDbSize ){ |
| 787 | pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; |
| 788 | }else{ |
| 789 | pPg->inJournal = 0; |
| 790 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 791 | pPg->dirty = 0; |
| 792 | pPg->nRef = 1; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 793 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 794 | pPager->nRef++; |
| 795 | h = pager_hash(pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 796 | pPg->pNextHash = pPager->aHash[h]; |
| 797 | pPager->aHash[h] = pPg; |
| 798 | if( pPg->pNextHash ){ |
| 799 | assert( pPg->pNextHash->pPrevHash==0 ); |
| 800 | pPg->pNextHash->pPrevHash = pPg; |
| 801 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 802 | if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager); |
| 803 | if( pPager->dbSize<pgno ){ |
| 804 | memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE); |
| 805 | }else{ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 806 | int rc; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 807 | sqliteOsSeek(pPager->fd, (pgno-1)*SQLITE_PAGE_SIZE); |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 808 | rc = sqliteOsRead(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
| 809 | if( rc!=SQLITE_OK ){ |
| 810 | return rc; |
| 811 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 812 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 813 | if( pPager->nExtra>0 ){ |
| 814 | memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra); |
| 815 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 816 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 817 | /* The requested page is in the page cache. */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 818 | pPager->nHit++; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 819 | page_ref(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 820 | } |
| 821 | *ppPage = PGHDR_TO_DATA(pPg); |
| 822 | return SQLITE_OK; |
| 823 | } |
| 824 | |
| 825 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 826 | ** Acquire a page if it is already in the in-memory cache. Do |
| 827 | ** not read the page from disk. Return a pointer to the page, |
| 828 | ** or 0 if the page is not in cache. |
| 829 | ** |
| 830 | ** See also sqlitepager_get(). The difference between this routine |
| 831 | ** and sqlitepager_get() is that _get() will go to the disk and read |
| 832 | ** in the page if the page is not already in cache. This routine |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 833 | ** returns NULL if the page is not in cache or if a disk I/O error |
| 834 | ** has ever happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 835 | */ |
| 836 | void *sqlitepager_lookup(Pager *pPager, Pgno pgno){ |
| 837 | PgHdr *pPg; |
| 838 | |
| 839 | /* Make sure we have not hit any critical errors. |
| 840 | */ |
| 841 | if( pPager==0 || pgno==0 ){ |
| 842 | return 0; |
| 843 | } |
| 844 | if( pPager->errMask & ~(PAGER_ERR_FULL) ){ |
| 845 | return 0; |
| 846 | } |
| 847 | if( pPager->nRef==0 ){ |
| 848 | return 0; |
| 849 | } |
| 850 | pPg = pager_lookup(pPager, pgno); |
| 851 | if( pPg==0 ) return 0; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 852 | page_ref(pPg); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 853 | return PGHDR_TO_DATA(pPg); |
| 854 | } |
| 855 | |
| 856 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 857 | ** Release a page. |
| 858 | ** |
| 859 | ** If the number of references to the page drop to zero, then the |
| 860 | ** page is added to the LRU list. When all references to all pages |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 861 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 862 | ** removed. |
| 863 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 864 | int sqlitepager_unref(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 865 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 866 | |
| 867 | /* Decrement the reference count for this page |
| 868 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 869 | pPg = DATA_TO_PGHDR(pData); |
| 870 | assert( pPg->nRef>0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 871 | pPg->nRef--; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 872 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 873 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 874 | /* When the number of references to a page reach 0, call the |
| 875 | ** destructor and add the page to the freelist. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 876 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 877 | if( pPg->nRef==0 ){ |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 878 | Pager *pPager; |
| 879 | pPager = pPg->pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 880 | pPg->pNextFree = 0; |
| 881 | pPg->pPrevFree = pPager->pLast; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 882 | pPager->pLast = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 883 | if( pPg->pPrevFree ){ |
| 884 | pPg->pPrevFree->pNextFree = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 885 | }else{ |
| 886 | pPager->pFirst = pPg; |
| 887 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 888 | if( pPager->xDestructor ){ |
| 889 | pPager->xDestructor(pData); |
| 890 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 891 | |
| 892 | /* When all pages reach the freelist, drop the read lock from |
| 893 | ** the database file. |
| 894 | */ |
| 895 | pPager->nRef--; |
| 896 | assert( pPager->nRef>=0 ); |
| 897 | if( pPager->nRef==0 ){ |
| 898 | pager_reset(pPager); |
| 899 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 900 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 901 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /* |
| 905 | ** Mark a data page as writeable. The page is written into the journal |
| 906 | ** if it is not there already. This routine must be called before making |
| 907 | ** changes to a page. |
| 908 | ** |
| 909 | ** The first time this routine is called, the pager creates a new |
| 910 | ** journal and acquires a write lock on the database. If the write |
| 911 | ** lock could not be acquired, this routine returns SQLITE_BUSY. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 912 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 913 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 914 | ** |
| 915 | ** If the journal file could not be written because the disk is full, |
| 916 | ** then this routine returns SQLITE_FULL and does an immediate rollback. |
| 917 | ** All subsequent write attempts also return SQLITE_FULL until there |
| 918 | ** is a call to sqlitepager_commit() or sqlitepager_rollback() to |
| 919 | ** reset. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 920 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 921 | int sqlitepager_write(void *pData){ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 922 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 923 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 924 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 925 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 926 | if( pPager->errMask ){ |
| 927 | return pager_errcode(pPager); |
| 928 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 929 | if( pPager->readOnly ){ |
| 930 | return SQLITE_PERM; |
| 931 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 932 | pPg->dirty = 1; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 933 | if( pPg->inJournal ){ return SQLITE_OK; } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 934 | assert( pPager->state!=SQLITE_UNLOCK ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 935 | if( pPager->state==SQLITE_READLOCK ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 936 | assert( pPager->aInJournal==0 ); |
| 937 | pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 ); |
| 938 | if( pPager->aInJournal==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 939 | sqliteFree(pPager->aInJournal); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 940 | return SQLITE_NOMEM; |
| 941 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 942 | rc = sqliteOsOpenExclusive(pPager->zJournal, &pPager->jfd); |
| 943 | if( rc!=SQLITE_OK ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 944 | sqliteFree(pPager->aInJournal); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 945 | return SQLITE_CANTOPEN; |
| 946 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 947 | pPager->journalOpen = 1; |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 948 | pPager->needSync = 0; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 949 | if( sqliteOsLock(pPager->jfd, 1)!=SQLITE_OK ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 950 | sqliteFree(pPager->aInJournal); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 951 | sqliteOsClose(pPager->jfd); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 952 | sqliteOsDelete(pPager->zJournal); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 953 | pPager->journalOpen = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 954 | return SQLITE_BUSY; |
| 955 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 956 | sqliteOsUnlock(pPager->fd); |
| 957 | if( sqliteOsLock(pPager->fd, 1)!=SQLITE_OK ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 958 | sqliteFree(pPager->aInJournal); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 959 | sqliteOsClose(pPager->jfd); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 960 | sqliteOsDelete(pPager->zJournal); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 961 | pPager->journalOpen = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 962 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 963 | pPager->errMask |= PAGER_ERR_LOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 964 | return SQLITE_PROTOCOL; |
| 965 | } |
| 966 | pPager->state = SQLITE_WRITELOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 967 | sqlitepager_pagecount(pPager); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 968 | pPager->origDbSize = pPager->dbSize; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 969 | rc = sqliteOsWrite(pPager->jfd, aJournalMagic, sizeof(aJournalMagic)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 970 | if( rc==SQLITE_OK ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 971 | rc = sqliteOsWrite(pPager->jfd, &pPager->dbSize, sizeof(Pgno)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 972 | } |
| 973 | if( rc!=SQLITE_OK ){ |
| 974 | rc = pager_unwritelock(pPager); |
| 975 | if( rc==SQLITE_OK ) rc = SQLITE_FULL; |
| 976 | return rc; |
| 977 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 978 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 979 | assert( pPager->state==SQLITE_WRITELOCK ); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 980 | assert( pPager->journalOpen ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 981 | if( pPg->pgno <= pPager->origDbSize ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 982 | rc = sqliteOsWrite(pPager->jfd, &pPg->pgno, sizeof(Pgno)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 983 | if( rc==SQLITE_OK ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 984 | rc = sqliteOsWrite(pPager->jfd, pData, SQLITE_PAGE_SIZE); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 985 | } |
| 986 | if( rc!=SQLITE_OK ){ |
| 987 | sqlitepager_rollback(pPager); |
| 988 | pPager->errMask |= PAGER_ERR_FULL; |
| 989 | return rc; |
| 990 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 991 | assert( pPager->aInJournal!=0 ); |
| 992 | pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 993 | pPager->needSync = 1; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 994 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 995 | pPg->inJournal = 1; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 996 | if( pPager->dbSize<pPg->pgno ){ |
| 997 | pPager->dbSize = pPg->pgno; |
| 998 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 999 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1003 | ** Return TRUE if the page given in the argument was previous passed |
| 1004 | ** to sqlitepager_write(). In other words, return TRUE if it is ok |
| 1005 | ** to change the content of the page. |
| 1006 | */ |
| 1007 | int sqlitepager_iswriteable(void *pData){ |
| 1008 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 1009 | return pPg->dirty; |
| 1010 | } |
| 1011 | |
| 1012 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1013 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1014 | ** |
| 1015 | ** If the commit fails for any reason, a rollback attempt is made |
| 1016 | ** and an error code is returned. If the commit worked, SQLITE_OK |
| 1017 | ** is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1018 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1019 | int sqlitepager_commit(Pager *pPager){ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1020 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1021 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1022 | |
| 1023 | if( pPager->errMask==PAGER_ERR_FULL ){ |
| 1024 | rc = sqlitepager_rollback(pPager); |
| 1025 | if( rc==SQLITE_OK ) rc = SQLITE_FULL; |
| 1026 | return rc; |
| 1027 | } |
| 1028 | if( pPager->errMask!=0 ){ |
| 1029 | rc = pager_errcode(pPager); |
| 1030 | return rc; |
| 1031 | } |
| 1032 | if( pPager->state!=SQLITE_WRITELOCK ){ |
| 1033 | return SQLITE_ERROR; |
| 1034 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1035 | assert( pPager->journalOpen ); |
| 1036 | if( pPager->needSync && sqliteOsSync(pPager->jfd)!=SQLITE_OK ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1037 | goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1038 | } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1039 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1040 | if( pPg->dirty==0 ) continue; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1041 | rc = sqliteOsSeek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE); |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1042 | if( rc!=SQLITE_OK ) goto commit_abort; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1043 | rc = sqliteOsWrite(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1044 | if( rc!=SQLITE_OK ) goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1045 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1046 | if( sqliteOsSync(pPager->fd)!=SQLITE_OK ) goto commit_abort; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1047 | rc = pager_unwritelock(pPager); |
| 1048 | pPager->dbSize = -1; |
| 1049 | return rc; |
| 1050 | |
| 1051 | /* Jump here if anything goes wrong during the commit process. |
| 1052 | */ |
| 1053 | commit_abort: |
| 1054 | rc = sqlitepager_rollback(pPager); |
| 1055 | if( rc==SQLITE_OK ){ |
| 1056 | rc = SQLITE_FULL; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1057 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1058 | return rc; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | ** Rollback all changes. The database falls back to read-only mode. |
| 1063 | ** All in-memory cache pages revert to their original data contents. |
| 1064 | ** The journal is deleted. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1065 | ** |
| 1066 | ** This routine cannot fail unless some other process is not following |
| 1067 | ** the correct locking protocol (SQLITE_PROTOCOL) or unless some other |
| 1068 | ** process is writing trash into the journal file (SQLITE_CORRUPT) or |
| 1069 | ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error |
| 1070 | ** codes are returned for all these occasions. Otherwise, |
| 1071 | ** SQLITE_OK is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1072 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1073 | int sqlitepager_rollback(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1074 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1075 | if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){ |
| 1076 | return pager_errcode(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1077 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1078 | if( pPager->state!=SQLITE_WRITELOCK ){ |
| 1079 | return SQLITE_OK; |
| 1080 | } |
| 1081 | rc = pager_playback(pPager); |
| 1082 | if( rc!=SQLITE_OK ){ |
| 1083 | rc = SQLITE_CORRUPT; |
| 1084 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 1085 | } |
| 1086 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1087 | return rc; |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 1088 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1089 | |
| 1090 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1091 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 1092 | ** if the database is (in theory) writable. |
| 1093 | */ |
| 1094 | int sqlitepager_isreadonly(Pager *pPager){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1095 | return pPager->readOnly; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1099 | ** This routine is used for testing and analysis only. |
| 1100 | */ |
| 1101 | int *sqlitepager_stats(Pager *pPager){ |
| 1102 | static int a[9]; |
| 1103 | a[0] = pPager->nRef; |
| 1104 | a[1] = pPager->nPage; |
| 1105 | a[2] = pPager->mxPage; |
| 1106 | a[3] = pPager->dbSize; |
| 1107 | a[4] = pPager->state; |
| 1108 | a[5] = pPager->errMask; |
| 1109 | a[6] = pPager->nHit; |
| 1110 | a[7] = pPager->nMiss; |
| 1111 | a[8] = pPager->nOvfl; |
| 1112 | return a; |
| 1113 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1114 | |
| 1115 | #if SQLITE_TEST |
| 1116 | /* |
| 1117 | ** Print a listing of all referenced pages and their ref count. |
| 1118 | */ |
| 1119 | void sqlitepager_refdump(Pager *pPager){ |
| 1120 | PgHdr *pPg; |
| 1121 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1122 | if( pPg->nRef<=0 ) continue; |
| 1123 | printf("PAGE %3d addr=0x%08x nRef=%d\n", |
| 1124 | pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef); |
| 1125 | } |
| 1126 | } |
| 1127 | #endif |