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