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