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