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