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