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