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