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