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