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