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