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