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 | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame^] | 21 | ** @(#) $Id: pager.c,v 1.66 2003/01/11 13:30:58 drh Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 22 | */ |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 23 | #include "os.h" /* Must be first to enable large file support */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 24 | #include "sqliteInt.h" |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 25 | #include "pager.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 | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 77 | PgHdr *pNextCkpt, *pPrevCkpt; /* List of pages in the checkpoint journal */ |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 78 | u8 inJournal; /* TRUE if has been written to journal */ |
| 79 | u8 inCkpt; /* TRUE if written to the checkpoint journal */ |
| 80 | u8 dirty; /* TRUE if we need to write back changes */ |
| 81 | u8 alwaysRollback; /* Disable dont_rollback() for this page */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 82 | /* SQLITE_PAGE_SIZE bytes of page data follow this header */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 83 | /* Pager.nExtra bytes of local data follow the page data */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 87 | ** Convert a pointer to a PgHdr into a pointer to its data |
| 88 | ** and back again. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 89 | */ |
| 90 | #define PGHDR_TO_DATA(P) ((void*)(&(P)[1])) |
| 91 | #define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1]) |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 92 | #define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE]) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 93 | |
| 94 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 95 | ** How big to make the hash table used for locating in-memory pages |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame^] | 96 | ** by page number. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 97 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame^] | 98 | #define N_PG_HASH 2048 |
| 99 | |
| 100 | /* |
| 101 | ** Hash a page number |
| 102 | */ |
| 103 | #define pager_hash(PN) ((PN)&(N_PG_HASH-1)) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | ** A open page cache is an instance of the following structure. |
| 107 | */ |
| 108 | struct Pager { |
| 109 | char *zFilename; /* Name of the database file */ |
| 110 | char *zJournal; /* Name of the journal file */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 111 | OsFile fd, jfd; /* File descriptors for database and journal */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 112 | OsFile cpfd; /* File descriptor for the checkpoint journal */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 113 | int dbSize; /* Number of pages in the file */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 114 | int origDbSize; /* dbSize before the current change */ |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 115 | int ckptSize; /* Size of database (in pages) at ckpt_begin() */ |
| 116 | off_t ckptJSize; /* Size of journal at ckpt_begin() */ |
drh | 9bd47a9 | 2003-01-07 14:46:08 +0000 | [diff] [blame] | 117 | int ckptNRec; /* Number of records in the checkpoint journal */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 118 | int nExtra; /* Add this many bytes to each in-memory page */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 119 | void (*xDestructor)(void*); /* Call this routine when freeing pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 120 | int nPage; /* Total number of in-memory pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 121 | int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 122 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 123 | int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 124 | u8 journalOpen; /* True if journal file descriptors is valid */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 125 | u8 useJournal; /* Do not use a rollback journal on this file */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 126 | u8 ckptOpen; /* True if the checkpoint journal is open */ |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 127 | u8 ckptInUse; /* True we are in a checkpoint */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 128 | u8 ckptAutoopen; /* Open ckpt journal when main journal is opened*/ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 129 | u8 noSync; /* Do not sync the journal if true */ |
| 130 | u8 state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */ |
| 131 | u8 errMask; /* One of several kinds of errors */ |
| 132 | u8 tempFile; /* zFilename is a temporary file */ |
| 133 | u8 readOnly; /* True for a read-only database */ |
| 134 | u8 needSync; /* True if an fsync() is needed on the journal */ |
drh | a168045 | 2002-04-18 01:56:57 +0000 | [diff] [blame] | 135 | u8 dirtyFile; /* True if database file has changed in any way */ |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 136 | u8 alwaysRollback; /* Disable dont_rollback() for all pages */ |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 137 | u8 journalFormat; /* Version number of the journal file */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 138 | u8 *aInJournal; /* One bit for each page in the database file */ |
| 139 | u8 *aInCkpt; /* One bit for each page in the database */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 140 | PgHdr *pFirst, *pLast; /* List of free pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 141 | PgHdr *pAll; /* List of all pages */ |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 142 | PgHdr *pCkpt; /* List of pages in the checkpoint journal */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 143 | PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /* |
| 147 | ** These are bits that can be set in Pager.errMask. |
| 148 | */ |
| 149 | #define PAGER_ERR_FULL 0x01 /* a write() failed */ |
| 150 | #define PAGER_ERR_MEM 0x02 /* malloc() failed */ |
| 151 | #define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */ |
| 152 | #define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 153 | #define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | ** The journal file contains page records in the following |
| 157 | ** format. |
| 158 | */ |
| 159 | typedef struct PageRecord PageRecord; |
| 160 | struct PageRecord { |
| 161 | Pgno pgno; /* The page number */ |
| 162 | char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */ |
| 163 | }; |
| 164 | |
| 165 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 166 | ** Journal files begin with the following magic string. The data |
| 167 | ** was obtained from /dev/random. It is used only as a sanity check. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 168 | ** |
| 169 | ** There are two journal formats. The older journal format writes |
| 170 | ** 32-bit integers in the byte-order of the host machine. The new |
| 171 | ** format writes integers as big-endian. All new journals use the |
| 172 | ** new format, but we have to be able to read an older journal in order |
| 173 | ** to roll it back. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 174 | */ |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 175 | static const unsigned char aOldJournalMagic[] = { |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 176 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4, |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 177 | }; |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 178 | static const unsigned char aJournalMagic[] = { |
| 179 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5, |
| 180 | }; |
| 181 | #define SQLITE_NEW_JOURNAL_FORMAT 1 |
| 182 | #define SQLITE_OLD_JOURNAL_FORMAT 0 |
| 183 | |
| 184 | /* |
| 185 | ** The following integer, if set, causes journals to be written in the |
| 186 | ** old format. This is used for testing purposes only - to make sure |
| 187 | ** the code is able to rollback an old journal. |
| 188 | */ |
| 189 | #ifdef SQLITE_TEST |
| 190 | int pager_old_format = 0; |
drh | 74587e5 | 2002-08-13 00:01:16 +0000 | [diff] [blame] | 191 | #else |
| 192 | # define pager_old_format 0 |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 193 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 194 | |
| 195 | /* |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 196 | ** Enable reference count tracking here: |
| 197 | */ |
drh | 74587e5 | 2002-08-13 00:01:16 +0000 | [diff] [blame] | 198 | #ifdef SQLITE_TEST |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 199 | int pager_refinfo_enable = 0; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 200 | static void pager_refinfo(PgHdr *p){ |
| 201 | static int cnt = 0; |
| 202 | if( !pager_refinfo_enable ) return; |
| 203 | printf( |
| 204 | "REFCNT: %4d addr=0x%08x nRef=%d\n", |
| 205 | p->pgno, (int)PGHDR_TO_DATA(p), p->nRef |
| 206 | ); |
| 207 | cnt++; /* Something to set a breakpoint on */ |
| 208 | } |
| 209 | # define REFINFO(X) pager_refinfo(X) |
| 210 | #else |
| 211 | # define REFINFO(X) |
| 212 | #endif |
| 213 | |
| 214 | /* |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 215 | ** Read a 32-bit integer from the given file descriptor |
| 216 | */ |
| 217 | static int read32bits(Pager *pPager, OsFile *fd, u32 *pRes){ |
| 218 | u32 res; |
| 219 | int rc; |
| 220 | rc = sqliteOsRead(fd, &res, sizeof(res)); |
| 221 | if( rc==SQLITE_OK && pPager->journalFormat==SQLITE_NEW_JOURNAL_FORMAT ){ |
| 222 | unsigned char ac[4]; |
| 223 | memcpy(ac, &res, 4); |
| 224 | res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3]; |
| 225 | } |
| 226 | *pRes = res; |
| 227 | return rc; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | ** Write a 32-bit integer into the given file descriptor. Writing |
| 232 | ** is always done using the new journal format. |
| 233 | */ |
| 234 | static int write32bits(OsFile *fd, u32 val){ |
| 235 | unsigned char ac[4]; |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 236 | if( pager_old_format ){ |
| 237 | return sqliteOsWrite(fd, &val, 4); |
| 238 | } |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 239 | ac[0] = (val>>24) & 0xff; |
| 240 | ac[1] = (val>>16) & 0xff; |
| 241 | ac[2] = (val>>8) & 0xff; |
| 242 | ac[3] = val & 0xff; |
| 243 | return sqliteOsWrite(fd, ac, 4); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 248 | ** Convert the bits in the pPager->errMask into an approprate |
| 249 | ** return code. |
| 250 | */ |
| 251 | static int pager_errcode(Pager *pPager){ |
| 252 | int rc = SQLITE_OK; |
| 253 | if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 254 | if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 255 | if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL; |
| 256 | if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM; |
| 257 | if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT; |
| 258 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 262 | ** Add or remove a page from the list of all pages that are in the |
| 263 | ** checkpoint journal. |
| 264 | ** |
| 265 | ** The Pager keeps a separate list of pages that are currently in |
| 266 | ** the checkpoint journal. This helps the sqlitepager_ckpt_commit() |
| 267 | ** routine run MUCH faster for the common case where there are many |
| 268 | ** pages in memory but only a few are in the checkpoint journal. |
| 269 | */ |
| 270 | static void page_add_to_ckpt_list(PgHdr *pPg){ |
| 271 | Pager *pPager = pPg->pPager; |
| 272 | if( pPg->inCkpt ) return; |
| 273 | assert( pPg->pPrevCkpt==0 && pPg->pNextCkpt==0 ); |
| 274 | pPg->pPrevCkpt = 0; |
| 275 | if( pPager->pCkpt ){ |
| 276 | pPager->pCkpt->pPrevCkpt = pPg; |
| 277 | } |
| 278 | pPg->pNextCkpt = pPager->pCkpt; |
| 279 | pPager->pCkpt = pPg; |
| 280 | pPg->inCkpt = 1; |
| 281 | } |
| 282 | static void page_remove_from_ckpt_list(PgHdr *pPg){ |
| 283 | if( !pPg->inCkpt ) return; |
| 284 | if( pPg->pPrevCkpt ){ |
| 285 | assert( pPg->pPrevCkpt->pNextCkpt==pPg ); |
| 286 | pPg->pPrevCkpt->pNextCkpt = pPg->pNextCkpt; |
| 287 | }else{ |
| 288 | assert( pPg->pPager->pCkpt==pPg ); |
| 289 | pPg->pPager->pCkpt = pPg->pNextCkpt; |
| 290 | } |
| 291 | if( pPg->pNextCkpt ){ |
| 292 | assert( pPg->pNextCkpt->pPrevCkpt==pPg ); |
| 293 | pPg->pNextCkpt->pPrevCkpt = pPg->pPrevCkpt; |
| 294 | } |
| 295 | pPg->pNextCkpt = 0; |
| 296 | pPg->pPrevCkpt = 0; |
| 297 | pPg->inCkpt = 0; |
| 298 | } |
| 299 | |
| 300 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 301 | ** Find a page in the hash table given its page number. Return |
| 302 | ** a pointer to the page or NULL if not found. |
| 303 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 304 | static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame^] | 305 | PgHdr *p = pPager->aHash[pager_hash(pgno)]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 306 | while( p && p->pgno!=pgno ){ |
| 307 | p = p->pNextHash; |
| 308 | } |
| 309 | return p; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | ** Unlock the database and clear the in-memory cache. This routine |
| 314 | ** sets the state of the pager back to what it was when it was first |
| 315 | ** opened. Any outstanding pages are invalidated and subsequent attempts |
| 316 | ** to access those pages will likely result in a coredump. |
| 317 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 318 | static void pager_reset(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 319 | PgHdr *pPg, *pNext; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 320 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 321 | pNext = pPg->pNextAll; |
| 322 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 323 | } |
| 324 | pPager->pFirst = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 325 | pPager->pLast = 0; |
| 326 | pPager->pAll = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 327 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 328 | pPager->nPage = 0; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 329 | if( pPager->state>=SQLITE_WRITELOCK ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 330 | sqlitepager_rollback(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 331 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 332 | sqliteOsUnlock(&pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 333 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 334 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 335 | pPager->nRef = 0; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 336 | assert( pPager->journalOpen==0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* |
| 340 | ** When this routine is called, the pager has the journal file open and |
| 341 | ** a write lock on the database. This routine releases the database |
| 342 | ** write lock and acquires a read lock in its place. The journal file |
| 343 | ** is deleted and closed. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 344 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 345 | static int pager_unwritelock(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 346 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 347 | PgHdr *pPg; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 348 | if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 349 | sqlitepager_ckpt_commit(pPager); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 350 | if( pPager->ckptOpen ){ |
| 351 | sqliteOsClose(&pPager->cpfd); |
| 352 | pPager->ckptOpen = 0; |
| 353 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 354 | if( pPager->journalOpen ){ |
| 355 | sqliteOsClose(&pPager->jfd); |
| 356 | pPager->journalOpen = 0; |
| 357 | sqliteOsDelete(pPager->zJournal); |
| 358 | sqliteFree( pPager->aInJournal ); |
| 359 | pPager->aInJournal = 0; |
| 360 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 361 | pPg->inJournal = 0; |
| 362 | pPg->dirty = 0; |
| 363 | } |
| 364 | }else{ |
| 365 | assert( pPager->dirtyFile==0 || pPager->useJournal==0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 366 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 367 | rc = sqliteOsReadLock(&pPager->fd); |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 368 | if( rc==SQLITE_OK ){ |
| 369 | pPager->state = SQLITE_READLOCK; |
| 370 | }else{ |
| 371 | /* This can only happen if a process does a BEGIN, then forks and the |
| 372 | ** child process does the COMMIT. Because of the semantics of unix |
| 373 | ** file locking, the unlock will fail. |
| 374 | */ |
| 375 | pPager->state = SQLITE_UNLOCK; |
| 376 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 377 | return rc; |
| 378 | } |
| 379 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 380 | /* |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 381 | ** Read a single page from the journal file opened on file descriptor |
| 382 | ** jfd. Playback this one page. |
| 383 | */ |
| 384 | static int pager_playback_one_page(Pager *pPager, OsFile *jfd){ |
| 385 | int rc; |
| 386 | PgHdr *pPg; /* An existing page in the cache */ |
| 387 | PageRecord pgRec; |
| 388 | |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 389 | rc = read32bits(pPager, jfd, &pgRec.pgno); |
| 390 | if( rc!=SQLITE_OK ) return rc; |
| 391 | rc = sqliteOsRead(jfd, &pgRec.aData, sizeof(pgRec.aData)); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 392 | if( rc!=SQLITE_OK ) return rc; |
| 393 | |
| 394 | /* Sanity checking on the page */ |
| 395 | if( pgRec.pgno>pPager->dbSize || pgRec.pgno==0 ) return SQLITE_CORRUPT; |
| 396 | |
| 397 | /* Playback the page. Update the in-memory copy of the page |
| 398 | ** at the same time, if there is one. |
| 399 | */ |
| 400 | pPg = pager_lookup(pPager, pgRec.pgno); |
| 401 | if( pPg ){ |
| 402 | memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE); |
| 403 | memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra); |
| 404 | } |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 405 | rc = sqliteOsSeek(&pPager->fd, (pgRec.pgno-1)*(off_t)SQLITE_PAGE_SIZE); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 406 | if( rc==SQLITE_OK ){ |
| 407 | rc = sqliteOsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE); |
| 408 | } |
| 409 | return rc; |
| 410 | } |
| 411 | |
| 412 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 413 | ** Playback the journal and thus restore the database file to |
| 414 | ** the state it was in before we started making changes. |
| 415 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 416 | ** The journal file format is as follows: There is an initial |
| 417 | ** file-type string for sanity checking. Then there is a single |
| 418 | ** Pgno number which is the number of pages in the database before |
| 419 | ** changes were made. The database is truncated to this size. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 420 | ** Next come zero or more page records where each page record |
| 421 | ** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See |
| 422 | ** the PageRecord structure for details. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 423 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 424 | ** If the file opened as the journal file is not a well-formed |
| 425 | ** journal file (as determined by looking at the magic number |
| 426 | ** at the beginning) then this routine returns SQLITE_PROTOCOL. |
| 427 | ** If any other errors occur during playback, the database will |
| 428 | ** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in |
| 429 | ** pPager->errMask and SQLITE_CORRUPT is returned. If it all |
| 430 | ** works, then this routine returns SQLITE_OK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 431 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 432 | static int pager_playback(Pager *pPager){ |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 433 | off_t nRec; /* Number of Records */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 434 | int i; /* Loop counter */ |
| 435 | Pgno mxPg = 0; /* Size of the original file in pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 436 | unsigned char aMagic[sizeof(aJournalMagic)]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 437 | int rc; |
| 438 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 439 | /* Figure out how many records are in the journal. Abort early if |
| 440 | ** the journal is empty. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 441 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 442 | assert( pPager->journalOpen ); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 443 | sqliteOsSeek(&pPager->jfd, 0); |
| 444 | rc = sqliteOsFileSize(&pPager->jfd, &nRec); |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 445 | if( rc!=SQLITE_OK ){ |
| 446 | goto end_playback; |
| 447 | } |
drh | 2c79995 | 2003-01-03 02:04:27 +0000 | [diff] [blame] | 448 | if( nRec < sizeof(aMagic)+sizeof(Pgno) ){ |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 449 | goto end_playback; |
| 450 | } |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 451 | nRec = (nRec - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord); |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 452 | |
| 453 | /* Read the beginning of the journal and truncate the |
| 454 | ** database file back to its original size. |
| 455 | */ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 456 | rc = sqliteOsRead(&pPager->jfd, aMagic, sizeof(aMagic)); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 457 | if( rc!=SQLITE_OK ){ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 458 | rc = SQLITE_PROTOCOL; |
| 459 | goto end_playback; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 460 | } |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 461 | if( memcmp(aMagic, aOldJournalMagic, sizeof(aMagic))==0 ){ |
| 462 | pPager->journalFormat = SQLITE_OLD_JOURNAL_FORMAT; |
| 463 | }else if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))==0 ){ |
| 464 | pPager->journalFormat = SQLITE_NEW_JOURNAL_FORMAT; |
| 465 | }else{ |
| 466 | rc = SQLITE_PROTOCOL; |
| 467 | goto end_playback; |
| 468 | } |
| 469 | rc = read32bits(pPager, &pPager->jfd, &mxPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 470 | if( rc!=SQLITE_OK ){ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 471 | goto end_playback; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 472 | } |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 473 | rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg); |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 474 | if( rc!=SQLITE_OK ){ |
| 475 | goto end_playback; |
| 476 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 477 | pPager->dbSize = mxPg; |
| 478 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 479 | /* Copy original pages out of the journal and back into the database file. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 480 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 481 | for(i=nRec-1; i>=0; i--){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 482 | rc = pager_playback_one_page(pPager, &pPager->jfd); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 483 | if( rc!=SQLITE_OK ) break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 484 | } |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 485 | |
| 486 | end_playback: |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 487 | if( rc!=SQLITE_OK ){ |
| 488 | pager_unwritelock(pPager); |
| 489 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 490 | rc = SQLITE_CORRUPT; |
| 491 | }else{ |
| 492 | rc = pager_unwritelock(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 493 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 494 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 498 | ** Playback the checkpoint journal. |
| 499 | ** |
| 500 | ** This is similar to playing back the transaction journal but with |
| 501 | ** a few extra twists. |
| 502 | ** |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 503 | ** (1) The number of pages in the database file at the start of |
| 504 | ** the checkpoint is stored in pPager->ckptSize, not in the |
| 505 | ** journal file itself. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 506 | ** |
| 507 | ** (2) In addition to playing back the checkpoint journal, also |
| 508 | ** playback all pages of the transaction journal beginning |
| 509 | ** at offset pPager->ckptJSize. |
| 510 | */ |
| 511 | static int pager_ckpt_playback(Pager *pPager){ |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 512 | off_t nRec; /* Number of Records */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 513 | int i; /* Loop counter */ |
| 514 | int rc; |
| 515 | |
| 516 | /* Truncate the database back to its original size. |
| 517 | */ |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 518 | rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)pPager->ckptSize); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 519 | pPager->dbSize = pPager->ckptSize; |
| 520 | |
| 521 | /* Figure out how many records are in the checkpoint journal. |
| 522 | */ |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 523 | assert( pPager->ckptInUse && pPager->journalOpen ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 524 | sqliteOsSeek(&pPager->cpfd, 0); |
drh | 9bd47a9 | 2003-01-07 14:46:08 +0000 | [diff] [blame] | 525 | nRec = pPager->ckptNRec; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 526 | |
| 527 | /* Copy original pages out of the checkpoint journal and back into the |
| 528 | ** database file. |
| 529 | */ |
drh | 74587e5 | 2002-08-13 00:01:16 +0000 | [diff] [blame] | 530 | if( pager_old_format ){ |
| 531 | pPager->journalFormat = SQLITE_OLD_JOURNAL_FORMAT; |
| 532 | }else{ |
| 533 | pPager->journalFormat = SQLITE_NEW_JOURNAL_FORMAT; |
| 534 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 535 | for(i=nRec-1; i>=0; i--){ |
| 536 | rc = pager_playback_one_page(pPager, &pPager->cpfd); |
| 537 | if( rc!=SQLITE_OK ) goto end_ckpt_playback; |
| 538 | } |
| 539 | |
| 540 | /* Figure out how many pages need to be copied out of the transaction |
| 541 | ** journal. |
| 542 | */ |
| 543 | rc = sqliteOsSeek(&pPager->jfd, pPager->ckptJSize); |
| 544 | if( rc!=SQLITE_OK ){ |
| 545 | goto end_ckpt_playback; |
| 546 | } |
| 547 | rc = sqliteOsFileSize(&pPager->jfd, &nRec); |
| 548 | if( rc!=SQLITE_OK ){ |
| 549 | goto end_ckpt_playback; |
| 550 | } |
| 551 | nRec = (nRec - pPager->ckptJSize)/sizeof(PageRecord); |
| 552 | for(i=nRec-1; i>=0; i--){ |
| 553 | rc = pager_playback_one_page(pPager, &pPager->jfd); |
| 554 | if( rc!=SQLITE_OK ) goto end_ckpt_playback; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | end_ckpt_playback: |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 559 | if( rc!=SQLITE_OK ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 560 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 561 | rc = SQLITE_CORRUPT; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 562 | } |
| 563 | return rc; |
| 564 | } |
| 565 | |
| 566 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 567 | ** Change the maximum number of in-memory pages that are allowed. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 568 | ** |
| 569 | ** The maximum number is the absolute value of the mxPage parameter. |
| 570 | ** If mxPage is negative, the noSync flag is also set. noSync bypasses |
| 571 | ** calls to sqliteOsSync(). The pager runs much faster with noSync on, |
| 572 | ** but if the operating system crashes or there is an abrupt power |
| 573 | ** failure, the database file might be left in an inconsistent and |
| 574 | ** unrepairable state. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 575 | */ |
| 576 | void sqlitepager_set_cachesize(Pager *pPager, int mxPage){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 577 | if( mxPage>=0 ){ |
drh | a168045 | 2002-04-18 01:56:57 +0000 | [diff] [blame] | 578 | pPager->noSync = pPager->tempFile; |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 579 | }else{ |
| 580 | pPager->noSync = 1; |
| 581 | mxPage = -mxPage; |
| 582 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 583 | if( mxPage>10 ){ |
| 584 | pPager->mxPage = mxPage; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /* |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 589 | ** Open a temporary file. Write the name of the file into zName |
| 590 | ** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write |
| 591 | ** the file descriptor into *fd. Return SQLITE_OK on success or some |
| 592 | ** other error code if we fail. |
| 593 | ** |
| 594 | ** The OS will automatically delete the temporary file when it is |
| 595 | ** closed. |
| 596 | */ |
| 597 | static int sqlitepager_opentemp(char *zFile, OsFile *fd){ |
| 598 | int cnt = 8; |
| 599 | int rc; |
| 600 | do{ |
| 601 | cnt--; |
| 602 | sqliteOsTempFileName(zFile); |
| 603 | rc = sqliteOsOpenExclusive(zFile, fd, 1); |
| 604 | }while( cnt>0 && rc!=SQLITE_OK ); |
| 605 | return rc; |
| 606 | } |
| 607 | |
| 608 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 609 | ** 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] | 610 | ** 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] | 611 | ** the first call to sqlitepager_get() and is only held open until the |
| 612 | ** last page is released using sqlitepager_unref(). |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 613 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 614 | ** If zFilename is NULL then a randomly-named temporary file is created |
| 615 | ** and used as the file to be cached. The file will be deleted |
| 616 | ** automatically when it is closed. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 617 | */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 618 | int sqlitepager_open( |
| 619 | Pager **ppPager, /* Return the Pager structure here */ |
| 620 | const char *zFilename, /* Name of the database file to open */ |
| 621 | int mxPage, /* Max number of in-memory cache pages */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 622 | int nExtra, /* Extra bytes append to each in-memory page */ |
| 623 | int useJournal /* TRUE to use a rollback journal on this file */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 624 | ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 625 | Pager *pPager; |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 626 | char *zFullPathname; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 627 | int nameLen; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 628 | OsFile fd; |
| 629 | int rc; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 630 | int tempFile; |
| 631 | int readOnly = 0; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 632 | char zTemp[SQLITE_TEMPNAME_SIZE]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 633 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 634 | *ppPager = 0; |
| 635 | if( sqlite_malloc_failed ){ |
| 636 | return SQLITE_NOMEM; |
| 637 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 638 | if( zFilename ){ |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 639 | zFullPathname = sqliteOsFullPathname(zFilename); |
| 640 | rc = sqliteOsOpenReadWrite(zFullPathname, &fd, &readOnly); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 641 | tempFile = 0; |
| 642 | }else{ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 643 | rc = sqlitepager_opentemp(zTemp, &fd); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 644 | zFilename = zTemp; |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 645 | zFullPathname = sqliteOsFullPathname(zFilename); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 646 | tempFile = 1; |
| 647 | } |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 648 | if( sqlite_malloc_failed ){ |
| 649 | return SQLITE_NOMEM; |
| 650 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 651 | if( rc!=SQLITE_OK ){ |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 652 | sqliteFree(zFullPathname); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 653 | return SQLITE_CANTOPEN; |
| 654 | } |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 655 | nameLen = strlen(zFullPathname); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 656 | pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 657 | if( pPager==0 ){ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 658 | sqliteOsClose(&fd); |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 659 | sqliteFree(zFullPathname); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 660 | return SQLITE_NOMEM; |
| 661 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 662 | pPager->zFilename = (char*)&pPager[1]; |
| 663 | pPager->zJournal = &pPager->zFilename[nameLen+1]; |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 664 | strcpy(pPager->zFilename, zFullPathname); |
| 665 | strcpy(pPager->zJournal, zFullPathname); |
| 666 | sqliteFree(zFullPathname); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 667 | strcpy(&pPager->zJournal[nameLen], "-journal"); |
| 668 | pPager->fd = fd; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 669 | pPager->journalOpen = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 670 | pPager->useJournal = useJournal; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 671 | pPager->ckptOpen = 0; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 672 | pPager->ckptInUse = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 673 | pPager->nRef = 0; |
| 674 | pPager->dbSize = -1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 675 | pPager->ckptSize = 0; |
| 676 | pPager->ckptJSize = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 677 | pPager->nPage = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 678 | pPager->mxPage = mxPage>5 ? mxPage : 10; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 679 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 680 | pPager->errMask = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 681 | pPager->tempFile = tempFile; |
| 682 | pPager->readOnly = readOnly; |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 683 | pPager->needSync = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 684 | pPager->noSync = pPager->tempFile || !useJournal; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 685 | pPager->pFirst = 0; |
| 686 | pPager->pLast = 0; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 687 | pPager->nExtra = nExtra; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 688 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 689 | *ppPager = pPager; |
| 690 | return SQLITE_OK; |
| 691 | } |
| 692 | |
| 693 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 694 | ** Set the destructor for this pager. If not NULL, the destructor is called |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 695 | ** when the reference count on each page reaches zero. The destructor can |
| 696 | ** 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] | 697 | ** |
| 698 | ** The destructor is not called as a result sqlitepager_close(). |
| 699 | ** Destructors are only called by sqlitepager_unref(). |
| 700 | */ |
| 701 | void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){ |
| 702 | pPager->xDestructor = xDesc; |
| 703 | } |
| 704 | |
| 705 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 706 | ** Return the total number of pages in the disk file associated with |
| 707 | ** pPager. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 708 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 709 | int sqlitepager_pagecount(Pager *pPager){ |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 710 | off_t n; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 711 | assert( pPager!=0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 712 | if( pPager->dbSize>=0 ){ |
| 713 | return pPager->dbSize; |
| 714 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 715 | if( sqliteOsFileSize(&pPager->fd, &n)!=SQLITE_OK ){ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 716 | pPager->errMask |= PAGER_ERR_DISK; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 717 | return 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 718 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 719 | n /= SQLITE_PAGE_SIZE; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 720 | if( pPager->state!=SQLITE_UNLOCK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 721 | pPager->dbSize = n; |
| 722 | } |
| 723 | return n; |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | ** Shutdown the page cache. Free all memory and close all files. |
| 728 | ** |
| 729 | ** If a transaction was in progress when this routine is called, that |
| 730 | ** transaction is rolled back. All outstanding pages are invalidated |
| 731 | ** and their memory is freed. Any attempt to use a page associated |
| 732 | ** with this page cache after this function returns will likely |
| 733 | ** result in a coredump. |
| 734 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 735 | int sqlitepager_close(Pager *pPager){ |
| 736 | PgHdr *pPg, *pNext; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 737 | switch( pPager->state ){ |
| 738 | case SQLITE_WRITELOCK: { |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 739 | sqlitepager_rollback(pPager); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 740 | sqliteOsUnlock(&pPager->fd); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 741 | assert( pPager->journalOpen==0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 742 | break; |
| 743 | } |
| 744 | case SQLITE_READLOCK: { |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 745 | sqliteOsUnlock(&pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 746 | break; |
| 747 | } |
| 748 | default: { |
| 749 | /* Do nothing */ |
| 750 | break; |
| 751 | } |
| 752 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 753 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 754 | pNext = pPg->pNextAll; |
| 755 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 756 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 757 | sqliteOsClose(&pPager->fd); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 758 | assert( pPager->journalOpen==0 ); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 759 | /* Temp files are automatically deleted by the OS |
| 760 | ** if( pPager->tempFile ){ |
| 761 | ** sqliteOsDelete(pPager->zFilename); |
| 762 | ** } |
| 763 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 764 | sqliteFree(pPager); |
| 765 | return SQLITE_OK; |
| 766 | } |
| 767 | |
| 768 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 769 | ** Return the page number for the given page data. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 770 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 771 | Pgno sqlitepager_pagenumber(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 772 | PgHdr *p = DATA_TO_PGHDR(pData); |
| 773 | return p->pgno; |
| 774 | } |
| 775 | |
| 776 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 777 | ** Increment the reference count for a page. If the page is |
| 778 | ** currently on the freelist (the reference count is zero) then |
| 779 | ** remove it from the freelist. |
| 780 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame^] | 781 | #define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++) |
| 782 | static void _page_ref(PgHdr *pPg){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 783 | if( pPg->nRef==0 ){ |
| 784 | /* The page is currently on the freelist. Remove it. */ |
| 785 | if( pPg->pPrevFree ){ |
| 786 | pPg->pPrevFree->pNextFree = pPg->pNextFree; |
| 787 | }else{ |
| 788 | pPg->pPager->pFirst = pPg->pNextFree; |
| 789 | } |
| 790 | if( pPg->pNextFree ){ |
| 791 | pPg->pNextFree->pPrevFree = pPg->pPrevFree; |
| 792 | }else{ |
| 793 | pPg->pPager->pLast = pPg->pPrevFree; |
| 794 | } |
| 795 | pPg->pPager->nRef++; |
| 796 | } |
| 797 | pPg->nRef++; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 798 | REFINFO(pPg); |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* |
| 802 | ** Increment the reference count for a page. The input pointer is |
| 803 | ** a reference to the page data. |
| 804 | */ |
| 805 | int sqlitepager_ref(void *pData){ |
| 806 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 807 | page_ref(pPg); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 808 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 812 | ** Sync the journal and then write all free dirty pages to the database |
| 813 | ** file. |
| 814 | ** |
| 815 | ** Writing all free dirty pages to the database after the sync is a |
| 816 | ** non-obvious optimization. fsync() is an expensive operation so we |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 817 | ** want to minimize the number ot times it is called. After an fsync() call, |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 818 | ** we are free to write dirty pages back to the database. It is best |
| 819 | ** to go ahead and write as many dirty pages as possible to minimize |
| 820 | ** the risk of having to do another fsync() later on. Writing dirty |
| 821 | ** free pages in this way was observed to make database operations go |
| 822 | ** up to 10 times faster. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 823 | ** |
| 824 | ** If we are writing to temporary database, there is no need to preserve |
| 825 | ** the integrity of the journal file, so we can save time and skip the |
| 826 | ** fsync(). |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 827 | */ |
| 828 | static int syncAllPages(Pager *pPager){ |
| 829 | PgHdr *pPg; |
drh | 80eb790 | 2002-12-28 01:06:30 +0000 | [diff] [blame] | 830 | Pgno lastPgno = 0; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 831 | int rc = SQLITE_OK; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 832 | |
| 833 | /* Sync the journal before modifying the main database |
| 834 | ** (assuming there is a journal and it needs to be synced.) |
| 835 | */ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 836 | if( pPager->needSync ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 837 | if( !pPager->tempFile ){ |
| 838 | rc = sqliteOsSync(&pPager->jfd); |
| 839 | if( rc!=0 ) return rc; |
| 840 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 841 | pPager->needSync = 0; |
| 842 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 843 | |
drh | 4d9ef4c | 2002-11-11 01:04:47 +0000 | [diff] [blame] | 844 | /* Write all dirty free pages to the disk in the order that they |
| 845 | ** appear on the disk. We have experimented with sorting the pages |
| 846 | ** by page numbers so that they are written in order, but that does |
| 847 | ** not appear to improve performance. |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 848 | */ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 849 | for(pPg=pPager->pFirst; pPg; pPg=pPg->pNextFree){ |
| 850 | if( pPg->dirty ){ |
drh | 4d9ef4c | 2002-11-11 01:04:47 +0000 | [diff] [blame] | 851 | if( lastPgno==0 || pPg->pgno!=lastPgno+1 ){ |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 852 | sqliteOsSeek(&pPager->fd, (pPg->pgno-1)*(off_t)SQLITE_PAGE_SIZE); |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 853 | } |
drh | 4d9ef4c | 2002-11-11 01:04:47 +0000 | [diff] [blame] | 854 | rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
| 855 | if( rc!=SQLITE_OK ) break; |
| 856 | pPg->dirty = 0; |
| 857 | lastPgno = pPg->pgno; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 858 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 859 | } |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 860 | return rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 864 | ** Acquire a page. |
| 865 | ** |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 866 | ** 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] | 867 | ** This read lock is dropped when the last page is released. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 868 | ** |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 869 | ** A _get works for any page number greater than 0. If the database |
| 870 | ** file is smaller than the requested page, then no actual disk |
| 871 | ** read occurs and the memory image of the page is initialized to |
| 872 | ** all zeros. The extra data appended to a page is always initialized |
| 873 | ** to zeros the first time a page is loaded into memory. |
| 874 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 875 | ** The acquisition might fail for several reasons. In all cases, |
| 876 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 877 | ** |
| 878 | ** See also sqlitepager_lookup(). Both this routine and _lookup() attempt |
| 879 | ** 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] | 880 | ** in memory, this routine goes to disk to read it in whereas _lookup() |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 881 | ** just returns 0. This routine acquires a read-lock the first time it |
| 882 | ** has to go to disk, and could also playback an old journal if necessary. |
| 883 | ** Since _lookup() never goes to disk, it never has to deal with locks |
| 884 | ** or journal files. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 885 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 886 | int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 887 | PgHdr *pPg; |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 888 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 889 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 890 | /* Make sure we have not hit any critical errors. |
| 891 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame^] | 892 | assert( pPager!=0 ); |
| 893 | assert( pgno!=0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 894 | if( pPager->errMask & ~(PAGER_ERR_FULL) ){ |
| 895 | return pager_errcode(pPager); |
| 896 | } |
| 897 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 898 | /* If this is the first page accessed, then get a read lock |
| 899 | ** on the database file. |
| 900 | */ |
| 901 | if( pPager->nRef==0 ){ |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 902 | rc = sqliteOsReadLock(&pPager->fd); |
| 903 | if( rc!=SQLITE_OK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 904 | *ppPage = 0; |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 905 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 906 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 907 | pPager->state = SQLITE_READLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 908 | |
| 909 | /* If a journal file exists, try to play it back. |
| 910 | */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 911 | if( pPager->useJournal && sqliteOsFileExists(pPager->zJournal) ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 912 | int rc, dummy; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 913 | |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 914 | /* Get a write lock on the database |
| 915 | */ |
| 916 | rc = sqliteOsWriteLock(&pPager->fd); |
| 917 | if( rc!=SQLITE_OK ){ |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 918 | if( sqliteOsUnlock(&pPager->fd)!=SQLITE_OK ){ |
| 919 | /* This should never happen! */ |
| 920 | rc = SQLITE_INTERNAL; |
| 921 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 922 | *ppPage = 0; |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 923 | return rc; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 924 | } |
| 925 | pPager->state = SQLITE_WRITELOCK; |
| 926 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 927 | /* Open the journal for exclusive access. Return SQLITE_BUSY if |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 928 | ** we cannot get exclusive access to the journal file. |
| 929 | ** |
| 930 | ** Even though we will only be reading from the journal, not writing, |
| 931 | ** we have to open the journal for writing in order to obtain an |
| 932 | ** exclusive access lock. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 933 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 934 | rc = sqliteOsOpenReadWrite(pPager->zJournal, &pPager->jfd, &dummy); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 935 | if( rc!=SQLITE_OK ){ |
| 936 | rc = sqliteOsUnlock(&pPager->fd); |
| 937 | assert( rc==SQLITE_OK ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 938 | *ppPage = 0; |
| 939 | return SQLITE_BUSY; |
| 940 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 941 | pPager->journalOpen = 1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 942 | |
| 943 | /* Playback and delete the journal. Drop the database write |
| 944 | ** lock and reacquire the read lock. |
| 945 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 946 | rc = pager_playback(pPager); |
| 947 | if( rc!=SQLITE_OK ){ |
| 948 | return rc; |
| 949 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 950 | } |
| 951 | pPg = 0; |
| 952 | }else{ |
| 953 | /* Search for page in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 954 | pPg = pager_lookup(pPager, pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 955 | } |
| 956 | if( pPg==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 957 | /* The requested page is not in the page cache. */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 958 | int h; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 959 | pPager->nMiss++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 960 | if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){ |
| 961 | /* Create a new page */ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 962 | pPg = sqliteMallocRaw( sizeof(*pPg) + SQLITE_PAGE_SIZE + pPager->nExtra ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 963 | if( pPg==0 ){ |
| 964 | *ppPage = 0; |
| 965 | pager_unwritelock(pPager); |
| 966 | pPager->errMask |= PAGER_ERR_MEM; |
| 967 | return SQLITE_NOMEM; |
| 968 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 969 | memset(pPg, 0, sizeof(*pPg)); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 970 | pPg->pPager = pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 971 | pPg->pNextAll = pPager->pAll; |
| 972 | if( pPager->pAll ){ |
| 973 | pPager->pAll->pPrevAll = pPg; |
| 974 | } |
| 975 | pPg->pPrevAll = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 976 | pPager->pAll = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 977 | pPager->nPage++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 978 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 979 | /* Recycle an older page. First locate the page to be recycled. |
| 980 | ** Try to find one that is not dirty and is near the head of |
| 981 | ** of the free list */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 982 | pPg = pPager->pFirst; |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 983 | while( pPg && pPg->dirty ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 984 | pPg = pPg->pNextFree; |
| 985 | } |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 986 | |
| 987 | /* If we could not find a page that has not been used recently |
| 988 | ** and which is not dirty, then sync the journal and write all |
| 989 | ** dirty free pages into the database file, thus making them |
| 990 | ** clean pages and available for recycling. |
| 991 | ** |
| 992 | ** We have to sync the journal before writing a page to the main |
| 993 | ** database. But syncing is a very slow operation. So after a |
| 994 | ** sync, it is best to write everything we can back to the main |
| 995 | ** database to minimize the risk of having to sync again in the |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 996 | ** near future. That is why we write all dirty pages after a |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 997 | ** sync. |
| 998 | */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 999 | if( pPg==0 ){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1000 | int rc = syncAllPages(pPager); |
| 1001 | if( rc!=0 ){ |
| 1002 | sqlitepager_rollback(pPager); |
| 1003 | *ppPage = 0; |
| 1004 | return SQLITE_IOERR; |
| 1005 | } |
| 1006 | pPg = pPager->pFirst; |
| 1007 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1008 | assert( pPg->nRef==0 ); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1009 | assert( pPg->dirty==0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1010 | |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 1011 | /* If the page we are recyclying is marked as alwaysRollback, then |
| 1012 | ** set the global alwaysRollback flag, thus disabling the |
| 1013 | ** sqlite_dont_rollback() optimization for the rest of this transaction. |
| 1014 | ** It is necessary to do this because the page marked alwaysRollback |
| 1015 | ** might be reloaded at a later time but at that point we won't remember |
| 1016 | ** that is was marked alwaysRollback. This means that all pages must |
| 1017 | ** be marked as alwaysRollback from here on out. |
| 1018 | */ |
| 1019 | if( pPg->alwaysRollback ){ |
| 1020 | pPager->alwaysRollback = 1; |
| 1021 | } |
| 1022 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1023 | /* Unlink the old page from the free list and the hash table |
| 1024 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1025 | if( pPg->pPrevFree ){ |
| 1026 | pPg->pPrevFree->pNextFree = pPg->pNextFree; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1027 | }else{ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1028 | assert( pPager->pFirst==pPg ); |
| 1029 | pPager->pFirst = pPg->pNextFree; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1030 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1031 | if( pPg->pNextFree ){ |
| 1032 | pPg->pNextFree->pPrevFree = pPg->pPrevFree; |
| 1033 | }else{ |
| 1034 | assert( pPager->pLast==pPg ); |
| 1035 | pPager->pLast = pPg->pPrevFree; |
| 1036 | } |
| 1037 | pPg->pNextFree = pPg->pPrevFree = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1038 | if( pPg->pNextHash ){ |
| 1039 | pPg->pNextHash->pPrevHash = pPg->pPrevHash; |
| 1040 | } |
| 1041 | if( pPg->pPrevHash ){ |
| 1042 | pPg->pPrevHash->pNextHash = pPg->pNextHash; |
| 1043 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1044 | h = pager_hash(pPg->pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1045 | assert( pPager->aHash[h]==pPg ); |
| 1046 | pPager->aHash[h] = pPg->pNextHash; |
| 1047 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1048 | pPg->pNextHash = pPg->pPrevHash = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1049 | pPager->nOvfl++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1050 | } |
| 1051 | pPg->pgno = pgno; |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 1052 | if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1053 | pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; |
| 1054 | }else{ |
| 1055 | pPg->inJournal = 0; |
| 1056 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1057 | if( pPager->aInCkpt && (int)pgno<=pPager->ckptSize |
| 1058 | && (pPager->aInCkpt[pgno/8] & (1<<(pgno&7)))!=0 ){ |
| 1059 | page_add_to_ckpt_list(pPg); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1060 | }else{ |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1061 | page_remove_from_ckpt_list(pPg); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1062 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1063 | pPg->dirty = 0; |
| 1064 | pPg->nRef = 1; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1065 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1066 | pPager->nRef++; |
| 1067 | h = pager_hash(pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1068 | pPg->pNextHash = pPager->aHash[h]; |
| 1069 | pPager->aHash[h] = pPg; |
| 1070 | if( pPg->pNextHash ){ |
| 1071 | assert( pPg->pNextHash->pPrevHash==0 ); |
| 1072 | pPg->pNextHash->pPrevHash = pPg; |
| 1073 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1074 | if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager); |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 1075 | if( pPager->dbSize<(int)pgno ){ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1076 | memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE); |
| 1077 | }else{ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 1078 | int rc; |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 1079 | sqliteOsSeek(&pPager->fd, (pgno-1)*(off_t)SQLITE_PAGE_SIZE); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1080 | rc = sqliteOsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 1081 | if( rc!=SQLITE_OK ){ |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 1082 | off_t fileSize; |
drh | 4e371ee | 2002-09-05 16:08:27 +0000 | [diff] [blame] | 1083 | if( sqliteOsFileSize(&pPager->fd,&fileSize)!=SQLITE_OK |
| 1084 | || fileSize>=pgno*SQLITE_PAGE_SIZE ){ |
| 1085 | return rc; |
| 1086 | }else{ |
| 1087 | memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE); |
| 1088 | } |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 1089 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1090 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1091 | if( pPager->nExtra>0 ){ |
| 1092 | memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra); |
| 1093 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1094 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1095 | /* The requested page is in the page cache. */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1096 | pPager->nHit++; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 1097 | page_ref(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1098 | } |
| 1099 | *ppPage = PGHDR_TO_DATA(pPg); |
| 1100 | return SQLITE_OK; |
| 1101 | } |
| 1102 | |
| 1103 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1104 | ** Acquire a page if it is already in the in-memory cache. Do |
| 1105 | ** not read the page from disk. Return a pointer to the page, |
| 1106 | ** or 0 if the page is not in cache. |
| 1107 | ** |
| 1108 | ** See also sqlitepager_get(). The difference between this routine |
| 1109 | ** and sqlitepager_get() is that _get() will go to the disk and read |
| 1110 | ** in the page if the page is not already in cache. This routine |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1111 | ** returns NULL if the page is not in cache or if a disk I/O error |
| 1112 | ** has ever happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1113 | */ |
| 1114 | void *sqlitepager_lookup(Pager *pPager, Pgno pgno){ |
| 1115 | PgHdr *pPg; |
| 1116 | |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame^] | 1117 | assert( pPager!=0 ); |
| 1118 | assert( pgno!=0 ); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1119 | if( pPager->errMask & ~(PAGER_ERR_FULL) ){ |
| 1120 | return 0; |
| 1121 | } |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame^] | 1122 | /* if( pPager->nRef==0 ){ |
| 1123 | ** return 0; |
| 1124 | ** } |
| 1125 | */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1126 | pPg = pager_lookup(pPager, pgno); |
| 1127 | if( pPg==0 ) return 0; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 1128 | page_ref(pPg); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1129 | return PGHDR_TO_DATA(pPg); |
| 1130 | } |
| 1131 | |
| 1132 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1133 | ** Release a page. |
| 1134 | ** |
| 1135 | ** If the number of references to the page drop to zero, then the |
| 1136 | ** page is added to the LRU list. When all references to all pages |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1137 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1138 | ** removed. |
| 1139 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1140 | int sqlitepager_unref(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1141 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1142 | |
| 1143 | /* Decrement the reference count for this page |
| 1144 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1145 | pPg = DATA_TO_PGHDR(pData); |
| 1146 | assert( pPg->nRef>0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1147 | pPg->nRef--; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1148 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1149 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1150 | /* When the number of references to a page reach 0, call the |
| 1151 | ** destructor and add the page to the freelist. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1152 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1153 | if( pPg->nRef==0 ){ |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 1154 | Pager *pPager; |
| 1155 | pPager = pPg->pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1156 | pPg->pNextFree = 0; |
| 1157 | pPg->pPrevFree = pPager->pLast; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1158 | pPager->pLast = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1159 | if( pPg->pPrevFree ){ |
| 1160 | pPg->pPrevFree->pNextFree = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1161 | }else{ |
| 1162 | pPager->pFirst = pPg; |
| 1163 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1164 | if( pPager->xDestructor ){ |
| 1165 | pPager->xDestructor(pData); |
| 1166 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* When all pages reach the freelist, drop the read lock from |
| 1169 | ** the database file. |
| 1170 | */ |
| 1171 | pPager->nRef--; |
| 1172 | assert( pPager->nRef>=0 ); |
| 1173 | if( pPager->nRef==0 ){ |
| 1174 | pager_reset(pPager); |
| 1175 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1176 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1177 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1181 | ** Create a journal file for pPager. There should already be a write |
| 1182 | ** lock on the database file when this routine is called. |
| 1183 | ** |
| 1184 | ** Return SQLITE_OK if everything. Return an error code and release the |
| 1185 | ** write lock if anything goes wrong. |
| 1186 | */ |
| 1187 | static int pager_open_journal(Pager *pPager){ |
| 1188 | int rc; |
| 1189 | assert( pPager->state==SQLITE_WRITELOCK ); |
| 1190 | assert( pPager->journalOpen==0 ); |
| 1191 | assert( pPager->useJournal ); |
| 1192 | pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 ); |
| 1193 | if( pPager->aInJournal==0 ){ |
| 1194 | sqliteOsReadLock(&pPager->fd); |
| 1195 | pPager->state = SQLITE_READLOCK; |
| 1196 | return SQLITE_NOMEM; |
| 1197 | } |
| 1198 | rc = sqliteOsOpenExclusive(pPager->zJournal, &pPager->jfd,pPager->tempFile); |
| 1199 | if( rc!=SQLITE_OK ){ |
| 1200 | sqliteFree(pPager->aInJournal); |
| 1201 | pPager->aInJournal = 0; |
| 1202 | sqliteOsReadLock(&pPager->fd); |
| 1203 | pPager->state = SQLITE_READLOCK; |
| 1204 | return SQLITE_CANTOPEN; |
| 1205 | } |
| 1206 | pPager->journalOpen = 1; |
| 1207 | pPager->needSync = 0; |
| 1208 | pPager->alwaysRollback = 0; |
| 1209 | sqlitepager_pagecount(pPager); |
| 1210 | pPager->origDbSize = pPager->dbSize; |
| 1211 | if( pager_old_format ){ |
| 1212 | rc = sqliteOsWrite(&pPager->jfd, aOldJournalMagic, |
| 1213 | sizeof(aOldJournalMagic)); |
| 1214 | }else{ |
| 1215 | rc = sqliteOsWrite(&pPager->jfd, aJournalMagic, sizeof(aJournalMagic)); |
| 1216 | } |
| 1217 | if( rc==SQLITE_OK ){ |
| 1218 | rc = write32bits(&pPager->jfd, pPager->dbSize); |
| 1219 | } |
| 1220 | if( pPager->ckptAutoopen && rc==SQLITE_OK ){ |
| 1221 | rc = sqlitepager_ckpt_begin(pPager); |
| 1222 | } |
| 1223 | if( rc!=SQLITE_OK ){ |
| 1224 | rc = pager_unwritelock(pPager); |
| 1225 | if( rc==SQLITE_OK ){ |
| 1226 | rc = SQLITE_FULL; |
| 1227 | } |
| 1228 | } |
| 1229 | return rc; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 1233 | ** Acquire a write-lock on the database. The lock is removed when |
| 1234 | ** the any of the following happen: |
| 1235 | ** |
| 1236 | ** * sqlitepager_commit() is called. |
| 1237 | ** * sqlitepager_rollback() is called. |
| 1238 | ** * sqlitepager_close() is called. |
| 1239 | ** * sqlitepager_unref() is called to on every outstanding page. |
| 1240 | ** |
| 1241 | ** The parameter to this routine is a pointer to any open page of the |
| 1242 | ** database file. Nothing changes about the page - it is used merely |
| 1243 | ** to acquire a pointer to the Pager structure and as proof that there |
| 1244 | ** is already a read-lock on the database. |
| 1245 | ** |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1246 | ** A journal file is opened if this is not a temporary file. For |
| 1247 | ** temporary files, the opening of the journal file is deferred until |
| 1248 | ** there is an actual need to write to the journal. |
| 1249 | ** |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 1250 | ** If the database is already write-locked, this routine is a no-op. |
| 1251 | */ |
| 1252 | int sqlitepager_begin(void *pData){ |
| 1253 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 1254 | Pager *pPager = pPg->pPager; |
| 1255 | int rc = SQLITE_OK; |
| 1256 | assert( pPg->nRef>0 ); |
| 1257 | assert( pPager->state!=SQLITE_UNLOCK ); |
| 1258 | if( pPager->state==SQLITE_READLOCK ){ |
| 1259 | assert( pPager->aInJournal==0 ); |
| 1260 | rc = sqliteOsWriteLock(&pPager->fd); |
| 1261 | if( rc!=SQLITE_OK ){ |
| 1262 | return rc; |
| 1263 | } |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 1264 | pPager->state = SQLITE_WRITELOCK; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1265 | pPager->dirtyFile = 0; |
| 1266 | if( pPager->useJournal && !pPager->tempFile ){ |
| 1267 | rc = pager_open_journal(pPager); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | return rc; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1274 | ** Mark a data page as writeable. The page is written into the journal |
| 1275 | ** if it is not there already. This routine must be called before making |
| 1276 | ** changes to a page. |
| 1277 | ** |
| 1278 | ** The first time this routine is called, the pager creates a new |
| 1279 | ** journal and acquires a write lock on the database. If the write |
| 1280 | ** lock could not be acquired, this routine returns SQLITE_BUSY. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1281 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1282 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1283 | ** |
| 1284 | ** If the journal file could not be written because the disk is full, |
| 1285 | ** then this routine returns SQLITE_FULL and does an immediate rollback. |
| 1286 | ** All subsequent write attempts also return SQLITE_FULL until there |
| 1287 | ** is a call to sqlitepager_commit() or sqlitepager_rollback() to |
| 1288 | ** reset. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1289 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1290 | int sqlitepager_write(void *pData){ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1291 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 1292 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 1293 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1294 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1295 | /* Check for errors |
| 1296 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1297 | if( pPager->errMask ){ |
| 1298 | return pager_errcode(pPager); |
| 1299 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1300 | if( pPager->readOnly ){ |
| 1301 | return SQLITE_PERM; |
| 1302 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1303 | |
| 1304 | /* Mark the page as dirty. If the page has already been written |
| 1305 | ** to the journal then we can return right away. |
| 1306 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1307 | pPg->dirty = 1; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1308 | if( pPg->inJournal && (pPg->inCkpt || pPager->ckptInUse==0) ){ |
drh | a168045 | 2002-04-18 01:56:57 +0000 | [diff] [blame] | 1309 | pPager->dirtyFile = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1310 | return SQLITE_OK; |
| 1311 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1312 | |
| 1313 | /* If we get this far, it means that the page needs to be |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1314 | ** written to the transaction journal or the ckeckpoint journal |
| 1315 | ** or both. |
| 1316 | ** |
| 1317 | ** First check to see that the transaction journal exists and |
| 1318 | ** create it if it does not. |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1319 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1320 | assert( pPager->state!=SQLITE_UNLOCK ); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 1321 | rc = sqlitepager_begin(pData); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1322 | if( rc!=SQLITE_OK ){ |
| 1323 | return rc; |
| 1324 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1325 | assert( pPager->state==SQLITE_WRITELOCK ); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1326 | if( !pPager->journalOpen && pPager->useJournal ){ |
| 1327 | rc = pager_open_journal(pPager); |
| 1328 | if( rc!=SQLITE_OK ) return rc; |
| 1329 | } |
| 1330 | assert( pPager->journalOpen || !pPager->useJournal ); |
| 1331 | pPager->dirtyFile = 1; |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1332 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1333 | /* The transaction journal now exists and we have a write lock on the |
| 1334 | ** main database file. Write the current page to the transaction |
| 1335 | ** journal if it is not there already. |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1336 | */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1337 | if( !pPg->inJournal && pPager->useJournal |
| 1338 | && (int)pPg->pgno <= pPager->origDbSize ){ |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 1339 | rc = write32bits(&pPager->jfd, pPg->pgno); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1340 | if( rc==SQLITE_OK ){ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1341 | rc = sqliteOsWrite(&pPager->jfd, pData, SQLITE_PAGE_SIZE); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1342 | } |
| 1343 | if( rc!=SQLITE_OK ){ |
| 1344 | sqlitepager_rollback(pPager); |
| 1345 | pPager->errMask |= PAGER_ERR_FULL; |
| 1346 | return rc; |
| 1347 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1348 | assert( pPager->aInJournal!=0 ); |
| 1349 | pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 1350 | pPager->needSync = !pPager->noSync; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1351 | pPg->inJournal = 1; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1352 | if( pPager->ckptInUse ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1353 | pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1354 | page_add_to_ckpt_list(pPg); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1355 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1356 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1357 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1358 | /* If the checkpoint journal is open and the page is not in it, |
| 1359 | ** then write the current page to the checkpoint journal. |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1360 | */ |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1361 | if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){ |
drh | 1e336b4 | 2002-02-14 12:50:33 +0000 | [diff] [blame] | 1362 | assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 1363 | rc = write32bits(&pPager->cpfd, pPg->pgno); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1364 | if( rc==SQLITE_OK ){ |
| 1365 | rc = sqliteOsWrite(&pPager->cpfd, pData, SQLITE_PAGE_SIZE); |
| 1366 | } |
| 1367 | if( rc!=SQLITE_OK ){ |
| 1368 | sqlitepager_rollback(pPager); |
| 1369 | pPager->errMask |= PAGER_ERR_FULL; |
| 1370 | return rc; |
| 1371 | } |
drh | 9bd47a9 | 2003-01-07 14:46:08 +0000 | [diff] [blame] | 1372 | pPager->ckptNRec++; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1373 | assert( pPager->aInCkpt!=0 ); |
| 1374 | pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1375 | page_add_to_ckpt_list(pPg); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | /* Update the database size and return. |
| 1379 | */ |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 1380 | if( pPager->dbSize<(int)pPg->pgno ){ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1381 | pPager->dbSize = pPg->pgno; |
| 1382 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1383 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /* |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 1387 | ** Return TRUE if the page given in the argument was previously passed |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1388 | ** to sqlitepager_write(). In other words, return TRUE if it is ok |
| 1389 | ** to change the content of the page. |
| 1390 | */ |
| 1391 | int sqlitepager_iswriteable(void *pData){ |
| 1392 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 1393 | return pPg->dirty; |
| 1394 | } |
| 1395 | |
| 1396 | /* |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1397 | ** A call to this routine tells the pager that it is not necessary to |
| 1398 | ** write the information on page "pgno" back to the disk, even though |
| 1399 | ** that page might be marked as dirty. |
| 1400 | ** |
| 1401 | ** The overlying software layer calls this routine when all of the data |
| 1402 | ** on the given page is unused. The pager marks the page as clean so |
| 1403 | ** that it does not get written to disk. |
| 1404 | ** |
| 1405 | ** Tests show that this optimization, together with the |
| 1406 | ** sqlitepager_dont_rollback() below, more than double the speed |
| 1407 | ** of large INSERT operations and quadruple the speed of large DELETEs. |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 1408 | ** |
| 1409 | ** When this routine is called, set the alwaysRollback flag to true. |
| 1410 | ** Subsequent calls to sqlitepager_dont_rollback() for the same page |
| 1411 | ** will thereafter be ignored. This is necessary to avoid a problem |
| 1412 | ** where a page with data is added to the freelist during one part of |
| 1413 | ** a transaction then removed from the freelist during a later part |
| 1414 | ** of the same transaction and reused for some other purpose. When it |
| 1415 | ** is first added to the freelist, this routine is called. When reused, |
| 1416 | ** the dont_rollback() routine is called. But because the page contains |
| 1417 | ** critical data, we still need to be sure it gets rolled back in spite |
| 1418 | ** of the dont_rollback() call. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1419 | */ |
| 1420 | void sqlitepager_dont_write(Pager *pPager, Pgno pgno){ |
| 1421 | PgHdr *pPg; |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 1422 | |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1423 | pPg = pager_lookup(pPager, pgno); |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 1424 | pPg->alwaysRollback = 1; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1425 | if( pPg && pPg->dirty ){ |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 1426 | if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){ |
| 1427 | /* If this pages is the last page in the file and the file has grown |
| 1428 | ** during the current transaction, then do NOT mark the page as clean. |
| 1429 | ** When the database file grows, we must make sure that the last page |
| 1430 | ** gets written at least once so that the disk file will be the correct |
| 1431 | ** size. If you do not write this page and the size of the file |
| 1432 | ** on the disk ends up being too small, that can lead to database |
| 1433 | ** corruption during the next transaction. |
| 1434 | */ |
| 1435 | }else{ |
| 1436 | pPg->dirty = 0; |
| 1437 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | /* |
| 1442 | ** A call to this routine tells the pager that if a rollback occurs, |
| 1443 | ** it is not necessary to restore the data on the given page. This |
| 1444 | ** means that the pager does not have to record the given page in the |
| 1445 | ** rollback journal. |
| 1446 | */ |
| 1447 | void sqlitepager_dont_rollback(void *pData){ |
| 1448 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 1449 | Pager *pPager = pPg->pPager; |
| 1450 | |
| 1451 | if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 1452 | if( pPg->alwaysRollback || pPager->alwaysRollback ) return; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1453 | if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){ |
| 1454 | assert( pPager->aInJournal!=0 ); |
| 1455 | pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
| 1456 | pPg->inJournal = 1; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1457 | if( pPager->ckptInUse ){ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1458 | pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1459 | page_add_to_ckpt_list(pPg); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1460 | } |
| 1461 | } |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1462 | if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1463 | assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); |
| 1464 | assert( pPager->aInCkpt!=0 ); |
| 1465 | pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1466 | page_add_to_ckpt_list(pPg); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1471 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1472 | ** |
| 1473 | ** If the commit fails for any reason, a rollback attempt is made |
| 1474 | ** and an error code is returned. If the commit worked, SQLITE_OK |
| 1475 | ** is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1476 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1477 | int sqlitepager_commit(Pager *pPager){ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1478 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1479 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1480 | |
| 1481 | if( pPager->errMask==PAGER_ERR_FULL ){ |
| 1482 | rc = sqlitepager_rollback(pPager); |
drh | 4e371ee | 2002-09-05 16:08:27 +0000 | [diff] [blame] | 1483 | if( rc==SQLITE_OK ){ |
| 1484 | rc = SQLITE_FULL; |
| 1485 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1486 | return rc; |
| 1487 | } |
| 1488 | if( pPager->errMask!=0 ){ |
| 1489 | rc = pager_errcode(pPager); |
| 1490 | return rc; |
| 1491 | } |
| 1492 | if( pPager->state!=SQLITE_WRITELOCK ){ |
| 1493 | return SQLITE_ERROR; |
| 1494 | } |
drh | a168045 | 2002-04-18 01:56:57 +0000 | [diff] [blame] | 1495 | if( pPager->dirtyFile==0 ){ |
| 1496 | /* Exit early (without doing the time-consuming sqliteOsSync() calls) |
| 1497 | ** if there have been no changes to the database file. */ |
| 1498 | rc = pager_unwritelock(pPager); |
| 1499 | pPager->dbSize = -1; |
| 1500 | return rc; |
| 1501 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1502 | assert( pPager->journalOpen ); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1503 | if( pPager->needSync && sqliteOsSync(&pPager->jfd)!=SQLITE_OK ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1504 | goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1505 | } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1506 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1507 | if( pPg->dirty==0 ) continue; |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 1508 | rc = sqliteOsSeek(&pPager->fd, (pPg->pgno-1)*(off_t)SQLITE_PAGE_SIZE); |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1509 | if( rc!=SQLITE_OK ) goto commit_abort; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1510 | rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1511 | if( rc!=SQLITE_OK ) goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1512 | } |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 1513 | if( !pPager->noSync && sqliteOsSync(&pPager->fd)!=SQLITE_OK ){ |
| 1514 | goto commit_abort; |
| 1515 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1516 | rc = pager_unwritelock(pPager); |
| 1517 | pPager->dbSize = -1; |
| 1518 | return rc; |
| 1519 | |
| 1520 | /* Jump here if anything goes wrong during the commit process. |
| 1521 | */ |
| 1522 | commit_abort: |
| 1523 | rc = sqlitepager_rollback(pPager); |
| 1524 | if( rc==SQLITE_OK ){ |
| 1525 | rc = SQLITE_FULL; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1526 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1527 | return rc; |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | ** Rollback all changes. The database falls back to read-only mode. |
| 1532 | ** All in-memory cache pages revert to their original data contents. |
| 1533 | ** The journal is deleted. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1534 | ** |
| 1535 | ** This routine cannot fail unless some other process is not following |
| 1536 | ** the correct locking protocol (SQLITE_PROTOCOL) or unless some other |
| 1537 | ** process is writing trash into the journal file (SQLITE_CORRUPT) or |
| 1538 | ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error |
| 1539 | ** codes are returned for all these occasions. Otherwise, |
| 1540 | ** SQLITE_OK is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1541 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1542 | int sqlitepager_rollback(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1543 | int rc; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1544 | if( !pPager->dirtyFile || !pPager->journalOpen ){ |
| 1545 | rc = pager_unwritelock(pPager); |
| 1546 | pPager->dbSize = -1; |
| 1547 | return rc; |
| 1548 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1549 | if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){ |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 1550 | if( pPager->state>=SQLITE_WRITELOCK ){ |
| 1551 | pager_playback(pPager); |
| 1552 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1553 | return pager_errcode(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1554 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1555 | if( pPager->state!=SQLITE_WRITELOCK ){ |
| 1556 | return SQLITE_OK; |
| 1557 | } |
| 1558 | rc = pager_playback(pPager); |
| 1559 | if( rc!=SQLITE_OK ){ |
| 1560 | rc = SQLITE_CORRUPT; |
| 1561 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 1562 | } |
| 1563 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1564 | return rc; |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 1565 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1566 | |
| 1567 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1568 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 1569 | ** if the database is (in theory) writable. |
| 1570 | */ |
| 1571 | int sqlitepager_isreadonly(Pager *pPager){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1572 | return pPager->readOnly; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1576 | ** This routine is used for testing and analysis only. |
| 1577 | */ |
| 1578 | int *sqlitepager_stats(Pager *pPager){ |
| 1579 | static int a[9]; |
| 1580 | a[0] = pPager->nRef; |
| 1581 | a[1] = pPager->nPage; |
| 1582 | a[2] = pPager->mxPage; |
| 1583 | a[3] = pPager->dbSize; |
| 1584 | a[4] = pPager->state; |
| 1585 | a[5] = pPager->errMask; |
| 1586 | a[6] = pPager->nHit; |
| 1587 | a[7] = pPager->nMiss; |
| 1588 | a[8] = pPager->nOvfl; |
| 1589 | return a; |
| 1590 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1591 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1592 | /* |
| 1593 | ** Set the checkpoint. |
| 1594 | ** |
| 1595 | ** This routine should be called with the transaction journal already |
| 1596 | ** open. A new checkpoint journal is created that can be used to rollback |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 1597 | ** changes of a single SQL command within a larger transaction. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1598 | */ |
| 1599 | int sqlitepager_ckpt_begin(Pager *pPager){ |
| 1600 | int rc; |
| 1601 | char zTemp[SQLITE_TEMPNAME_SIZE]; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1602 | if( !pPager->journalOpen ){ |
| 1603 | pPager->ckptAutoopen = 1; |
| 1604 | return SQLITE_OK; |
| 1605 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1606 | assert( pPager->journalOpen ); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1607 | assert( !pPager->ckptInUse ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1608 | pPager->aInCkpt = sqliteMalloc( pPager->dbSize/8 + 1 ); |
| 1609 | if( pPager->aInCkpt==0 ){ |
| 1610 | sqliteOsReadLock(&pPager->fd); |
| 1611 | return SQLITE_NOMEM; |
| 1612 | } |
| 1613 | rc = sqliteOsFileSize(&pPager->jfd, &pPager->ckptJSize); |
| 1614 | if( rc ) goto ckpt_begin_failed; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1615 | pPager->ckptSize = pPager->dbSize; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1616 | if( !pPager->ckptOpen ){ |
| 1617 | rc = sqlitepager_opentemp(zTemp, &pPager->cpfd); |
| 1618 | if( rc ) goto ckpt_begin_failed; |
| 1619 | pPager->ckptOpen = 1; |
drh | 9bd47a9 | 2003-01-07 14:46:08 +0000 | [diff] [blame] | 1620 | pPager->ckptNRec = 0; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1621 | } |
| 1622 | pPager->ckptInUse = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1623 | return SQLITE_OK; |
| 1624 | |
| 1625 | ckpt_begin_failed: |
| 1626 | if( pPager->aInCkpt ){ |
| 1627 | sqliteFree(pPager->aInCkpt); |
| 1628 | pPager->aInCkpt = 0; |
| 1629 | } |
| 1630 | return rc; |
| 1631 | } |
| 1632 | |
| 1633 | /* |
| 1634 | ** Commit a checkpoint. |
| 1635 | */ |
| 1636 | int sqlitepager_ckpt_commit(Pager *pPager){ |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1637 | if( pPager->ckptInUse ){ |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1638 | PgHdr *pPg, *pNext; |
drh | 96ddd6d | 2002-09-05 19:10:33 +0000 | [diff] [blame] | 1639 | sqliteOsSeek(&pPager->cpfd, 0); |
drh | 9bd47a9 | 2003-01-07 14:46:08 +0000 | [diff] [blame] | 1640 | /* sqliteOsTruncate(&pPager->cpfd, 0); */ |
| 1641 | pPager->ckptNRec = 0; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1642 | pPager->ckptInUse = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1643 | sqliteFree( pPager->aInCkpt ); |
| 1644 | pPager->aInCkpt = 0; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1645 | for(pPg=pPager->pCkpt; pPg; pPg=pNext){ |
| 1646 | pNext = pPg->pNextCkpt; |
| 1647 | assert( pPg->inCkpt ); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1648 | pPg->inCkpt = 0; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1649 | pPg->pPrevCkpt = pPg->pNextCkpt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1650 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1651 | pPager->pCkpt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1652 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1653 | pPager->ckptAutoopen = 0; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1654 | return SQLITE_OK; |
| 1655 | } |
| 1656 | |
| 1657 | /* |
| 1658 | ** Rollback a checkpoint. |
| 1659 | */ |
| 1660 | int sqlitepager_ckpt_rollback(Pager *pPager){ |
| 1661 | int rc; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1662 | if( pPager->ckptInUse ){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1663 | rc = pager_ckpt_playback(pPager); |
| 1664 | sqlitepager_ckpt_commit(pPager); |
| 1665 | }else{ |
| 1666 | rc = SQLITE_OK; |
| 1667 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1668 | pPager->ckptAutoopen = 0; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1669 | return rc; |
| 1670 | } |
| 1671 | |
drh | 74587e5 | 2002-08-13 00:01:16 +0000 | [diff] [blame] | 1672 | #ifdef SQLITE_TEST |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1673 | /* |
| 1674 | ** Print a listing of all referenced pages and their ref count. |
| 1675 | */ |
| 1676 | void sqlitepager_refdump(Pager *pPager){ |
| 1677 | PgHdr *pPg; |
| 1678 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1679 | if( pPg->nRef<=0 ) continue; |
| 1680 | printf("PAGE %3d addr=0x%08x nRef=%d\n", |
| 1681 | pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef); |
| 1682 | } |
| 1683 | } |
| 1684 | #endif |