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 | 5f2d46b | 2009-01-13 16:03:44 +0000 | [diff] [blame^] | 21 | ** @(#) $Id: pager.c,v 1.549 2009/01/13 16:03:44 danielk1977 Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 22 | */ |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 23 | #ifndef SQLITE_OMIT_DISKIO |
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 | |
| 26 | /* |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 27 | ** Macros for troubleshooting. Normally turned off |
| 28 | */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 29 | #if 0 |
danielk1977 | f2c31ad | 2009-01-06 13:40:08 +0000 | [diff] [blame] | 30 | int sqlite3PagerTrace=1; /* True to enable tracing */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 31 | #define sqlite3DebugPrintf printf |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 32 | #define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; } |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 33 | #else |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 34 | #define PAGERTRACE(X) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 37 | /* |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 38 | ** The following two macros are used within the PAGERTRACE() macros above |
drh | d86959f | 2005-11-26 03:51:18 +0000 | [diff] [blame] | 39 | ** to print out file-descriptors. |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 40 | ** |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 41 | ** PAGERID() takes a pointer to a Pager struct as its argument. The |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 42 | ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 43 | ** struct as its argument. |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 44 | */ |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 45 | #define PAGERID(p) ((int)(p->fd)) |
| 46 | #define FILEHANDLEID(fd) ((int)fd) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 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 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 52 | ** PAGER_UNLOCK The page cache is not currently reading or |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 53 | ** writing the database file. There is no |
| 54 | ** data held in memory. This is the initial |
| 55 | ** state. |
| 56 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 57 | ** PAGER_SHARED The page cache is reading the database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 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 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 62 | ** PAGER_RESERVED This process has reserved the database for writing |
| 63 | ** but has not yet made any changes. Only one process |
| 64 | ** at a time can reserve the database. The original |
| 65 | ** database file has not been modified so other |
| 66 | ** processes may still be reading the on-disk |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 67 | ** database file. |
| 68 | ** |
| 69 | ** PAGER_EXCLUSIVE The page cache is writing the database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 70 | ** Access is exclusive. No other processes or |
| 71 | ** threads can be reading or writing while one |
| 72 | ** process is writing. |
| 73 | ** |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 74 | ** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE |
| 75 | ** after all dirty pages have been written to the |
| 76 | ** database file and the file has been synced to |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 77 | ** disk. All that remains to do is to remove or |
| 78 | ** truncate the journal file and the transaction |
| 79 | ** will be committed. |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 80 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 81 | ** The page cache comes up in PAGER_UNLOCK. The first time a |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 82 | ** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 83 | ** After all pages have been released using sqlite_page_unref(), |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 84 | ** the state transitions back to PAGER_UNLOCK. The first time |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 85 | ** that sqlite3PagerWrite() is called, the state transitions to |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 86 | ** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 87 | ** called on an outstanding page which means that the pager must |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 88 | ** be in PAGER_SHARED before it transitions to PAGER_RESERVED.) |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 89 | ** PAGER_RESERVED means that there is an open rollback journal. |
| 90 | ** The transition to PAGER_EXCLUSIVE occurs before any changes |
| 91 | ** are made to the database file, though writes to the rollback |
| 92 | ** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback() |
| 93 | ** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED, |
| 94 | ** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 95 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 96 | #define PAGER_UNLOCK 0 |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 97 | #define PAGER_SHARED 1 /* same as SHARED_LOCK */ |
| 98 | #define PAGER_RESERVED 2 /* same as RESERVED_LOCK */ |
| 99 | #define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */ |
| 100 | #define PAGER_SYNCED 5 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 101 | |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 102 | /* |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 103 | ** This macro rounds values up so that if the value is an address it |
| 104 | ** is guaranteed to be an address that is aligned to an 8-byte boundary. |
| 105 | */ |
| 106 | #define FORCE_ALIGNMENT(X) (((X)+7)&~7) |
| 107 | |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 108 | /* |
| 109 | ** A macro used for invoking the codec if there is one |
| 110 | */ |
| 111 | #ifdef SQLITE_HAS_CODEC |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 112 | # define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); } |
| 113 | # define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D)) |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 114 | #else |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 115 | # define CODEC1(P,D,N,X) /* NO-OP */ |
| 116 | # define CODEC2(P,D,N,X) ((char*)D) |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 117 | #endif |
| 118 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 119 | /* |
danielk1977 | 7cbd589 | 2009-01-10 16:15:09 +0000 | [diff] [blame] | 120 | ** The maximum allowed sector size. 16MB. If the xSectorsize() method |
| 121 | ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead. |
| 122 | ** This could conceivably cause corruption following a power failure on |
| 123 | ** such a system. This is currently an undocumented limit. |
| 124 | */ |
| 125 | #define MAX_SECTOR_SIZE 0x0100000 |
| 126 | |
| 127 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 128 | ** An instance of the following structure is allocated for each active |
| 129 | ** savepoint and statement transaction in the system. All such structures |
| 130 | ** are stored in the Pager.aSavepoint[] array, which is allocated and |
| 131 | ** resized using sqlite3Realloc(). |
| 132 | ** |
| 133 | ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is |
| 134 | ** set to 0. If a journal-header is written into the main journal while |
| 135 | ** the savepoint is active, then iHdrOffset is set to the byte offset |
| 136 | ** immediately following the last journal record written into the main |
| 137 | ** journal before the journal-header. This is required during savepoint |
| 138 | ** rollback (see pagerPlaybackSavepoint()). |
| 139 | */ |
| 140 | typedef struct PagerSavepoint PagerSavepoint; |
| 141 | struct PagerSavepoint { |
| 142 | i64 iOffset; /* Starting offset in main journal */ |
| 143 | i64 iHdrOffset; /* See above */ |
| 144 | Bitvec *pInSavepoint; /* Set of pages in this savepoint */ |
| 145 | Pgno nOrig; /* Original number of pages in file */ |
| 146 | Pgno iSubRec; /* Index of first record in sub-journal */ |
| 147 | }; |
| 148 | |
| 149 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 150 | ** A open page cache is an instance of the following structure. |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 151 | ** |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 152 | ** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 153 | ** or SQLITE_FULL. Once one of the first three errors occurs, it persists |
| 154 | ** and is returned as the result of every major pager API call. The |
| 155 | ** SQLITE_FULL return code is slightly different. It persists only until the |
| 156 | ** next successful rollback is performed on the pager cache. Also, |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 157 | ** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup() |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 158 | ** APIs, they may still be used successfully. |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 159 | ** |
| 160 | ** Managing the size of the database file in pages is a little complicated. |
| 161 | ** The variable Pager.dbSize contains the number of pages that the database |
| 162 | ** image currently contains. As the database image grows or shrinks this |
| 163 | ** variable is updated. The variable Pager.dbFileSize contains the number |
| 164 | ** of pages in the database file. This may be different from Pager.dbSize |
| 165 | ** if some pages have been appended to the database image but not yet written |
| 166 | ** out from the cache to the actual file on disk. Or if the image has been |
| 167 | ** truncated by an incremental-vacuum operation. The Pager.dbOrigSize variable |
| 168 | ** contains the number of pages in the database image when the current |
| 169 | ** transaction was opened. The contents of all three of these variables is |
| 170 | ** only guaranteed to be correct if the boolean Pager.dbSizeValid is true. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 171 | */ |
| 172 | struct Pager { |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 173 | sqlite3_vfs *pVfs; /* OS functions to use for IO */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 174 | u8 journalOpen; /* True if journal file descriptors is valid */ |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 175 | u8 journalStarted; /* True if header of journal is synced */ |
| 176 | u8 useJournal; /* Use a rollback journal on this file */ |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 177 | u8 noReadlock; /* Do not bother to obtain readlocks */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 178 | u8 noSync; /* Do not sync the journal if true */ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 179 | u8 fullSync; /* Do extra syncs of the journal for robustness */ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 180 | u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 181 | u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 182 | u8 tempFile; /* zFilename is a temporary file */ |
| 183 | u8 readOnly; /* True for a read-only database */ |
| 184 | u8 needSync; /* True if an fsync() is needed on the journal */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 185 | u8 dirtyCache; /* True if cached pages have changed */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 186 | u8 memDb; /* True to inhibit all file I/O */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 187 | u8 setMaster; /* True if a m-j name has been written to jrnl */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 188 | u8 doNotSync; /* Boolean. While true, do not spill the cache */ |
| 189 | u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 190 | u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */ |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 191 | u8 dbModified; /* True if there are any changes to the Db */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 192 | u8 changeCountDone; /* Set after incrementing the change-counter */ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 193 | u8 dbSizeValid; /* Set when dbSize is correct */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 194 | Pgno dbSize; /* Number of pages in the database */ |
| 195 | Pgno dbOrigSize; /* dbSize before the current transaction */ |
| 196 | Pgno dbFileSize; /* Number of pages in the database file */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 197 | u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 198 | int errCode; /* One of several kinds of errors */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 199 | int nRec; /* Number of pages written to the journal */ |
| 200 | u32 cksumInit; /* Quasi-random value added to every checksum */ |
| 201 | int stmtNRec; /* Number of records in stmt subjournal */ |
| 202 | int nExtra; /* Add this many bytes to each in-memory page */ |
| 203 | int pageSize; /* Number of bytes in a page */ |
| 204 | int nPage; /* Total number of in-memory pages */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 205 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 206 | Pgno mxPgno; /* Maximum allowed size of the database */ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 207 | Bitvec *pInJournal; /* One bit for each page in the database file */ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 208 | Bitvec *pAlwaysRollback; /* One bit for each page marked always-rollback */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 209 | char *zFilename; /* Name of the database file */ |
| 210 | char *zJournal; /* Name of the journal file */ |
| 211 | char *zDirectory; /* Directory hold database and journal files */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 212 | sqlite3_file *fd, *jfd; /* File descriptors for database and journal */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 213 | sqlite3_file *sjfd; /* File descriptor for the sub-journal*/ |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 214 | int (*xBusyHandler)(void*); /* Function to call when busy */ |
| 215 | void *pBusyHandlerArg; /* Context argument for xBusyHandler */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 216 | i64 journalOff; /* Current byte offset in the journal file */ |
| 217 | i64 journalHdr; /* Byte offset to previous journal header */ |
drh | 98c5835 | 2008-11-07 00:24:53 +0000 | [diff] [blame] | 218 | u32 sectorSize; /* Assumed sector size during rollback */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 219 | #ifdef SQLITE_TEST |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 220 | int nHit, nMiss; /* Cache hits and missing */ |
| 221 | int nRead, nWrite; /* Database pages read/written */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 222 | #endif |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 223 | void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 224 | #ifdef SQLITE_HAS_CODEC |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 225 | void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 226 | void *pCodecArg; /* First argument to xCodec() */ |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 227 | #endif |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 228 | char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 229 | char dbFileVers[16]; /* Changes whenever database file changes */ |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 230 | i64 journalSizeLimit; /* Size limit for persistent journal files */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 231 | PCache *pPCache; /* Pointer to page cache object */ |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 232 | PagerSavepoint *aSavepoint; /* Array of active savepoints */ |
| 233 | int nSavepoint; /* Number of elements in aSavepoint[] */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | /* |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 237 | ** The following global variables hold counters used for |
| 238 | ** testing purposes only. These variables do not exist in |
| 239 | ** a non-testing build. These variables are not thread-safe. |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 240 | */ |
| 241 | #ifdef SQLITE_TEST |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 242 | int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */ |
| 243 | int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */ |
| 244 | int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 245 | # define PAGER_INCR(v) v++ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 246 | #else |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 247 | # define PAGER_INCR(v) |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 248 | #endif |
| 249 | |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 250 | |
| 251 | |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 252 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 253 | ** Journal files begin with the following magic string. The data |
| 254 | ** was obtained from /dev/random. It is used only as a sanity check. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 255 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 256 | ** Since version 2.8.0, the journal format contains additional sanity |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 257 | ** checking information. If the power fails while the journal is being |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 258 | ** written, semi-random garbage data might appear in the journal |
| 259 | ** file after power is restored. If an attempt is then made |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 260 | ** to roll the journal back, the database could be corrupted. The additional |
| 261 | ** sanity checking data is an attempt to discover the garbage in the |
| 262 | ** journal and ignore it. |
| 263 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 264 | ** The sanity checking information for the new journal format consists |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 265 | ** of a 32-bit checksum on each page of data. The checksum covers both |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 266 | ** the page number and the pPager->pageSize bytes of data for the page. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 267 | ** This cksum is initialized to a 32-bit random value that appears in the |
| 268 | ** journal file right after the header. The random initializer is important, |
| 269 | ** because garbage data that appears at the end of a journal is likely |
| 270 | ** data that was once in other files that have now been deleted. If the |
| 271 | ** garbage data came from an obsolete journal file, the checksums might |
| 272 | ** be correct. But by initializing the checksum to random value which |
| 273 | ** is different for every journal, we minimize that risk. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 274 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 275 | static const unsigned char aJournalMagic[] = { |
| 276 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7, |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | /* |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 280 | ** The size of the header and of each page in the journal is determined |
| 281 | ** by the following macros. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 282 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 283 | #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8) |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 284 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 285 | /* |
| 286 | ** The journal header size for this pager. In the future, this could be |
| 287 | ** set to some value read from the disk controller. The important |
| 288 | ** characteristic is that it is the same size as a disk sector. |
| 289 | */ |
| 290 | #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize) |
| 291 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 292 | /* |
| 293 | ** The macro MEMDB is true if we are dealing with an in-memory database. |
| 294 | ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set, |
| 295 | ** the value of MEMDB will be a constant and the compiler will optimize |
| 296 | ** out code that would never execute. |
| 297 | */ |
| 298 | #ifdef SQLITE_OMIT_MEMORYDB |
| 299 | # define MEMDB 0 |
| 300 | #else |
| 301 | # define MEMDB pPager->memDb |
| 302 | #endif |
| 303 | |
| 304 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 305 | ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is |
| 306 | ** reserved for working around a windows/posix incompatibility). It is |
| 307 | ** used in the journal to signify that the remainder of the journal file |
| 308 | ** is devoted to storing a master journal name - there are no more pages to |
| 309 | ** roll back. See comments for function writeMasterJournal() for details. |
| 310 | */ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 311 | /* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 312 | #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1)) |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 313 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 314 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 315 | ** The maximum legal page number is (2^31 - 1). |
| 316 | */ |
| 317 | #define PAGER_MAX_PGNO 2147483647 |
| 318 | |
| 319 | /* |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 320 | ** Return true if it is necessary to write page *pPg into the sub-journal. |
| 321 | ** A page needs to be written into the sub-journal if there exists one |
| 322 | ** or more open savepoints for which: |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 323 | ** |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 324 | ** * The page-number is less than or equal to PagerSavepoint.nOrig, and |
| 325 | ** * The bit corresponding to the page-number is not set in |
| 326 | ** PagerSavepoint.pInSavepoint. |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 327 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 328 | static int subjRequiresPage(PgHdr *pPg){ |
| 329 | Pgno pgno = pPg->pgno; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 330 | Pager *pPager = pPg->pPager; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 331 | int i; |
| 332 | for(i=0; i<pPager->nSavepoint; i++){ |
| 333 | PagerSavepoint *p = &pPager->aSavepoint[i]; |
| 334 | if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){ |
| 335 | return 1; |
| 336 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 337 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 338 | return 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 339 | } |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 340 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 341 | /* |
| 342 | ** Return true if the page is already in the journal file. |
| 343 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 344 | static int pageInJournal(PgHdr *pPg){ |
| 345 | return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno); |
| 346 | } |
| 347 | |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 348 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 349 | ** Read a 32-bit integer from the given file descriptor. Store the integer |
| 350 | ** that is read in *pRes. Return SQLITE_OK if everything worked, or an |
| 351 | ** error code is something goes wrong. |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 352 | ** |
| 353 | ** All values are stored on disk as big-endian. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 354 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 355 | static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){ |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 356 | unsigned char ac[4]; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 357 | int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset); |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 358 | if( rc==SQLITE_OK ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 359 | *pRes = sqlite3Get4byte(ac); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 360 | } |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 361 | return rc; |
| 362 | } |
| 363 | |
| 364 | /* |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 365 | ** Write a 32-bit integer into a string buffer in big-endian byte order. |
| 366 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 367 | #define put32bits(A,B) sqlite3Put4byte((u8*)A,B) |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 368 | |
| 369 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 370 | ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK |
| 371 | ** on success or an error code is something goes wrong. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 372 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 373 | static int write32bits(sqlite3_file *fd, i64 offset, u32 val){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 374 | char ac[4]; |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 375 | put32bits(ac, val); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 376 | return sqlite3OsWrite(fd, ac, 4, offset); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 377 | } |
| 378 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 379 | /* |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 380 | ** If file pFd is open, call sqlite3OsUnlock() on it. |
| 381 | */ |
| 382 | static int osUnlock(sqlite3_file *pFd, int eLock){ |
| 383 | if( !pFd->pMethods ){ |
| 384 | return SQLITE_OK; |
| 385 | } |
| 386 | return sqlite3OsUnlock(pFd, eLock); |
| 387 | } |
| 388 | |
| 389 | /* |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 390 | ** This function determines whether or not the atomic-write optimization |
| 391 | ** can be used with this pager. The optimization can be used if: |
| 392 | ** |
| 393 | ** (a) the value returned by OsDeviceCharacteristics() indicates that |
| 394 | ** a database page may be written atomically, and |
| 395 | ** (b) the value returned by OsSectorSize() is less than or equal |
| 396 | ** to the page size. |
| 397 | ** |
| 398 | ** If the optimization cannot be used, 0 is returned. If it can be used, |
| 399 | ** then the value returned is the size of the journal file when it |
| 400 | ** contains rollback data for exactly one page. |
| 401 | */ |
| 402 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 403 | static int jrnlBufferSize(Pager *pPager){ |
| 404 | int dc; /* Device characteristics */ |
| 405 | int nSector; /* Sector size */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 406 | int szPage; /* Page size */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 407 | sqlite3_file *fd = pPager->fd; |
| 408 | |
| 409 | if( fd->pMethods ){ |
| 410 | dc = sqlite3OsDeviceCharacteristics(fd); |
danielk1977 | 7cbd589 | 2009-01-10 16:15:09 +0000 | [diff] [blame] | 411 | nSector = pPager->sectorSize; |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 412 | szPage = pPager->pageSize; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 416 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 417 | |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 418 | if( !fd->pMethods || |
| 419 | (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 420 | return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); |
| 421 | } |
| 422 | return 0; |
| 423 | } |
| 424 | #endif |
| 425 | |
| 426 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 427 | ** This function should be called when an error occurs within the pager |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 428 | ** code. The first argument is a pointer to the pager structure, the |
| 429 | ** second the error-code about to be returned by a pager API function. |
| 430 | ** The value returned is a copy of the second argument to this function. |
| 431 | ** |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 432 | ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 433 | ** the error becomes persistent. Until the persisten error is cleared, |
| 434 | ** subsequent API calls on this Pager will immediately return the same |
| 435 | ** error code. |
| 436 | ** |
| 437 | ** A persistent error indicates that the contents of the pager-cache |
| 438 | ** cannot be trusted. This state can be cleared by completely discarding |
| 439 | ** the contents of the pager-cache. If a transaction was active when |
| 440 | ** the persistent error occured, then the rollback journal may need |
| 441 | ** to be replayed. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 442 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 443 | static void pager_unlock(Pager *pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 444 | static int pager_error(Pager *pPager, int rc){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 445 | int rc2 = rc & 0xff; |
drh | 34f5621 | 2007-08-10 23:56:35 +0000 | [diff] [blame] | 446 | assert( |
| 447 | pPager->errCode==SQLITE_FULL || |
| 448 | pPager->errCode==SQLITE_OK || |
| 449 | (pPager->errCode & 0xff)==SQLITE_IOERR |
| 450 | ); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 451 | if( |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 452 | rc2==SQLITE_FULL || |
| 453 | rc2==SQLITE_IOERR || |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 454 | rc2==SQLITE_CORRUPT |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 455 | ){ |
| 456 | pPager->errCode = rc; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 457 | if( pPager->state==PAGER_UNLOCK |
| 458 | && sqlite3PcacheRefCount(pPager->pPCache)==0 |
| 459 | ){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 460 | /* If the pager is already unlocked, call pager_unlock() now to |
| 461 | ** clear the error state and ensure that the pager-cache is |
| 462 | ** completely empty. |
| 463 | */ |
| 464 | pager_unlock(pPager); |
| 465 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 466 | } |
| 467 | return rc; |
| 468 | } |
| 469 | |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 470 | /* |
| 471 | ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking |
| 472 | ** on the cache using a hash function. This is used for testing |
| 473 | ** and debugging only. |
| 474 | */ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 475 | #ifdef SQLITE_CHECK_PAGES |
| 476 | /* |
| 477 | ** Return a 32-bit hash of the page data for pPage. |
| 478 | */ |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 479 | static u32 pager_datahash(int nByte, unsigned char *pData){ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 480 | u32 hash = 0; |
| 481 | int i; |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 482 | for(i=0; i<nByte; i++){ |
| 483 | hash = (hash*1039) + pData[i]; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 484 | } |
| 485 | return hash; |
| 486 | } |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 487 | static u32 pager_pagehash(PgHdr *pPage){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 488 | return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData); |
| 489 | } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 490 | static void pager_set_pagehash(PgHdr *pPage){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 491 | pPage->pageHash = pager_pagehash(pPage); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 492 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 493 | |
| 494 | /* |
| 495 | ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES |
| 496 | ** is defined, and NDEBUG is not defined, an assert() statement checks |
| 497 | ** that the page is either dirty or still matches the calculated page-hash. |
| 498 | */ |
| 499 | #define CHECK_PAGE(x) checkPage(x) |
| 500 | static void checkPage(PgHdr *pPg){ |
| 501 | Pager *pPager = pPg->pPager; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 502 | assert( !pPg->pageHash || pPager->errCode |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 503 | || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) ); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | #else |
drh | 8ffa817 | 2007-06-18 17:25:17 +0000 | [diff] [blame] | 507 | #define pager_datahash(X,Y) 0 |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 508 | #define pager_pagehash(X) 0 |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 509 | #define CHECK_PAGE(x) |
drh | 41d3027 | 2008-08-20 21:47:45 +0000 | [diff] [blame] | 510 | #endif /* SQLITE_CHECK_PAGES */ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 511 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 512 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 513 | ** When this is called the journal file for pager pPager must be open. |
| 514 | ** The master journal file name is read from the end of the file and |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 515 | ** written into memory supplied by the caller. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 516 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 517 | ** zMaster must point to a buffer of at least nMaster bytes allocated by |
| 518 | ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is |
| 519 | ** enough space to write the master journal name). If the master journal |
| 520 | ** name in the journal is longer than nMaster bytes (including a |
| 521 | ** nul-terminator), then this is handled as if no master journal name |
| 522 | ** were present in the journal. |
| 523 | ** |
| 524 | ** If no master journal file name is present zMaster[0] is set to 0 and |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 525 | ** SQLITE_OK returned. |
| 526 | */ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 527 | static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 528 | int rc; |
| 529 | u32 len; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 530 | i64 szJ; |
danielk1977 | c3e8f5e | 2004-06-28 01:16:46 +0000 | [diff] [blame] | 531 | u32 cksum; |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 532 | u32 u; /* Unsigned loop counter */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 533 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
| 534 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 535 | zMaster[0] = '\0'; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 536 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 537 | rc = sqlite3OsFileSize(pJrnl, &szJ); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 538 | if( rc!=SQLITE_OK || szJ<16 ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 539 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 540 | rc = read32bits(pJrnl, szJ-16, &len); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 541 | if( rc!=SQLITE_OK ) return rc; |
| 542 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 543 | if( len>=nMaster ){ |
| 544 | return SQLITE_OK; |
| 545 | } |
| 546 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 547 | rc = read32bits(pJrnl, szJ-12, &cksum); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 548 | if( rc!=SQLITE_OK ) return rc; |
| 549 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 550 | rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 551 | if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc; |
| 552 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 553 | rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 554 | if( rc!=SQLITE_OK ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 555 | return rc; |
| 556 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 557 | zMaster[len] = '\0'; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 558 | |
| 559 | /* See if the checksum matches the master journal name */ |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 560 | for(u=0; u<len; u++){ |
| 561 | cksum -= zMaster[u]; |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 562 | } |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 563 | if( cksum ){ |
| 564 | /* If the checksum doesn't add up, then one or more of the disk sectors |
| 565 | ** containing the master journal filename is corrupted. This means |
| 566 | ** definitely roll back, so just return SQLITE_OK and report a (nul) |
| 567 | ** master-journal filename. |
| 568 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 569 | zMaster[0] = '\0'; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 570 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 571 | |
| 572 | return SQLITE_OK; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | ** Seek the journal file descriptor to the next sector boundary where a |
| 577 | ** journal header may be read or written. Pager.journalOff is updated with |
| 578 | ** the new seek offset. |
| 579 | ** |
| 580 | ** i.e for a sector size of 512: |
| 581 | ** |
| 582 | ** Input Offset Output Offset |
| 583 | ** --------------------------------------- |
| 584 | ** 0 0 |
| 585 | ** 512 512 |
| 586 | ** 100 512 |
| 587 | ** 2000 2048 |
| 588 | ** |
| 589 | */ |
danielk1977 | 112f752 | 2009-01-08 17:50:45 +0000 | [diff] [blame] | 590 | static i64 journalHdrOffset(Pager *pPager){ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 591 | i64 offset = 0; |
| 592 | i64 c = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 593 | if( c ){ |
| 594 | offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager); |
| 595 | } |
| 596 | assert( offset%JOURNAL_HDR_SZ(pPager)==0 ); |
| 597 | assert( offset>=c ); |
| 598 | assert( (offset-c)<JOURNAL_HDR_SZ(pPager) ); |
danielk1977 | 112f752 | 2009-01-08 17:50:45 +0000 | [diff] [blame] | 599 | return offset; |
| 600 | } |
| 601 | static void seekJournalHdr(Pager *pPager){ |
| 602 | pPager->journalOff = journalHdrOffset(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
drh | f3a8762 | 2008-04-17 14:16:42 +0000 | [diff] [blame] | 606 | ** Write zeros over the header of the journal file. This has the |
| 607 | ** effect of invalidating the journal file and committing the |
| 608 | ** transaction. |
| 609 | */ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 610 | static int zeroJournalHdr(Pager *pPager, int doTruncate){ |
| 611 | int rc = SQLITE_OK; |
danielk1977 | 55a25a1 | 2008-09-11 10:29:15 +0000 | [diff] [blame] | 612 | static const char zeroHdr[28] = {0}; |
drh | f3a8762 | 2008-04-17 14:16:42 +0000 | [diff] [blame] | 613 | |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 614 | if( pPager->journalOff ){ |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 615 | i64 iLimit = pPager->journalSizeLimit; |
| 616 | |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 617 | IOTRACE(("JZEROHDR %p\n", pPager)) |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 618 | if( doTruncate || iLimit==0 ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 619 | rc = sqlite3OsTruncate(pPager->jfd, 0); |
| 620 | }else{ |
| 621 | rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0); |
| 622 | } |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 623 | if( rc==SQLITE_OK && !pPager->noSync ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 624 | rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags); |
| 625 | } |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 626 | |
| 627 | /* At this point the transaction is committed but the write lock |
| 628 | ** is still held on the file. If there is a size limit configured for |
| 629 | ** the persistent journal and the journal file currently consumes more |
| 630 | ** space than that limit allows for, truncate it now. There is no need |
| 631 | ** to sync the file following this operation. |
| 632 | */ |
| 633 | if( rc==SQLITE_OK && iLimit>0 ){ |
| 634 | i64 sz; |
| 635 | rc = sqlite3OsFileSize(pPager->jfd, &sz); |
| 636 | if( rc==SQLITE_OK && sz>iLimit ){ |
| 637 | rc = sqlite3OsTruncate(pPager->jfd, iLimit); |
| 638 | } |
| 639 | } |
drh | a06ecba | 2008-04-22 17:15:17 +0000 | [diff] [blame] | 640 | } |
drh | f3a8762 | 2008-04-17 14:16:42 +0000 | [diff] [blame] | 641 | return rc; |
| 642 | } |
| 643 | |
| 644 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 645 | ** The journal file must be open when this routine is called. A journal |
| 646 | ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the |
| 647 | ** current location. |
| 648 | ** |
| 649 | ** The format for the journal header is as follows: |
| 650 | ** - 8 bytes: Magic identifying journal format. |
| 651 | ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on. |
| 652 | ** - 4 bytes: Random number used for page hash. |
| 653 | ** - 4 bytes: Initial database page count. |
| 654 | ** - 4 bytes: Sector size used by the process that wrote this journal. |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 655 | ** - 4 bytes: Database page size. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 656 | ** |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 657 | ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 658 | */ |
| 659 | static int writeJournalHdr(Pager *pPager){ |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 660 | int rc = SQLITE_OK; |
| 661 | char *zHeader = pPager->pTmpSpace; |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 662 | u32 nHeader = pPager->pageSize; |
| 663 | u32 nWrite; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 664 | int ii; |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 665 | |
| 666 | if( nHeader>JOURNAL_HDR_SZ(pPager) ){ |
| 667 | nHeader = JOURNAL_HDR_SZ(pPager); |
| 668 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 669 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 670 | /* If there are active savepoints and any of them were created since the |
| 671 | ** most recent journal header was written, update the PagerSavepoint.iHdrOff |
| 672 | ** fields now. |
| 673 | */ |
| 674 | for(ii=0; ii<pPager->nSavepoint; ii++){ |
| 675 | if( pPager->aSavepoint[ii].iHdrOffset==0 ){ |
| 676 | pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff; |
| 677 | } |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 678 | } |
| 679 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 680 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 681 | pPager->journalHdr = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 682 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 683 | memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 684 | |
| 685 | /* |
| 686 | ** Write the nRec Field - the number of page records that follow this |
| 687 | ** journal header. Normally, zero is written to this value at this time. |
| 688 | ** After the records are added to the journal (and the journal synced, |
| 689 | ** if in full-sync mode), the zero is overwritten with the true number |
| 690 | ** of records (see syncJournal()). |
| 691 | ** |
| 692 | ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When |
| 693 | ** reading the journal this value tells SQLite to assume that the |
| 694 | ** rest of the journal file contains valid page records. This assumption |
| 695 | ** is dangerous, as if a failure occured whilst writing to the journal |
| 696 | ** file it may contain some garbage data. There are two scenarios |
| 697 | ** where this risk can be ignored: |
| 698 | ** |
| 699 | ** * When the pager is in no-sync mode. Corruption can follow a |
| 700 | ** power failure in this case anyway. |
| 701 | ** |
| 702 | ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees |
| 703 | ** that garbage data is never appended to the journal file. |
| 704 | */ |
| 705 | assert(pPager->fd->pMethods||pPager->noSync); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 706 | if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY) |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 707 | || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) |
| 708 | ){ |
| 709 | put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); |
| 710 | }else{ |
| 711 | put32bits(&zHeader[sizeof(aJournalMagic)], 0); |
| 712 | } |
| 713 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 714 | /* The random check-hash initialiser */ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 715 | sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 716 | put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit); |
| 717 | /* The initial database size */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 718 | put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize); |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 719 | /* The assumed sector size for this process */ |
| 720 | put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize); |
drh | 08609ce | 2008-11-29 22:49:23 +0000 | [diff] [blame] | 721 | |
| 722 | /* Initializing the tail of the buffer is not necessary. Everything |
| 723 | ** works find if the following memset() is omitted. But initializing |
| 724 | ** the memory prevents valgrind from complaining, so we are willing to |
| 725 | ** take the performance hit. |
| 726 | */ |
| 727 | memset(&zHeader[sizeof(aJournalMagic)+16], 0, |
| 728 | nHeader-(sizeof(aJournalMagic)+16)); |
| 729 | |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 730 | if( pPager->journalHdr==0 ){ |
| 731 | /* The page size */ |
| 732 | put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize); |
| 733 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 734 | |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 735 | for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){ |
| 736 | IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader)) |
| 737 | rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff); |
| 738 | pPager->journalOff += nHeader; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 739 | } |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 740 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 741 | return rc; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | ** The journal file must be open when this is called. A journal header file |
| 746 | ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 747 | ** file. The current location in the journal file is given by |
| 748 | ** pPager->journalOff. See comments above function writeJournalHdr() for |
| 749 | ** a description of the journal header format. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 750 | ** |
| 751 | ** If the header is read successfully, *nRec is set to the number of |
| 752 | ** page records following this header and *dbSize is set to the size of the |
| 753 | ** database before the transaction began, in pages. Also, pPager->cksumInit |
| 754 | ** is set to the value read from the journal header. SQLITE_OK is returned |
| 755 | ** in this case. |
| 756 | ** |
| 757 | ** If the journal header file appears to be corrupted, SQLITE_DONE is |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 758 | ** returned and *nRec and *dbSize are undefined. If JOURNAL_HDR_SZ bytes |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 759 | ** cannot be read from the journal file an error code is returned. |
| 760 | */ |
| 761 | static int readJournalHdr( |
| 762 | Pager *pPager, |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 763 | i64 journalSize, |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 764 | u32 *pNRec, |
| 765 | u32 *pDbSize |
| 766 | ){ |
| 767 | int rc; |
| 768 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 769 | i64 jrnlOff; |
danielk1977 | 7cbd589 | 2009-01-10 16:15:09 +0000 | [diff] [blame] | 770 | u32 iPageSize; |
| 771 | u32 iSectorSize; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 772 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 773 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 774 | if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){ |
| 775 | return SQLITE_DONE; |
| 776 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 777 | jrnlOff = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 778 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 779 | rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 780 | if( rc ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 781 | jrnlOff += sizeof(aMagic); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 782 | |
| 783 | if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){ |
| 784 | return SQLITE_DONE; |
| 785 | } |
| 786 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 787 | rc = read32bits(pPager->jfd, jrnlOff, pNRec); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 788 | if( rc ) return rc; |
| 789 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 790 | rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 791 | if( rc ) return rc; |
| 792 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 793 | rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 794 | if( rc ) return rc; |
| 795 | |
danielk1977 | 7cbd589 | 2009-01-10 16:15:09 +0000 | [diff] [blame] | 796 | if( pPager->journalOff==0 ){ |
| 797 | rc = read32bits(pPager->jfd, jrnlOff+16, &iPageSize); |
| 798 | if( rc ) return rc; |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 799 | |
danielk1977 | 7cbd589 | 2009-01-10 16:15:09 +0000 | [diff] [blame] | 800 | if( iPageSize<512 |
| 801 | || iPageSize>SQLITE_MAX_PAGE_SIZE |
| 802 | || ((iPageSize-1)&iPageSize)!=0 |
| 803 | ){ |
| 804 | /* If the page-size in the journal-header is invalid, then the process |
| 805 | ** that wrote the journal-header must have crashed before the header |
| 806 | ** was synced. In this case stop reading the journal file here. |
| 807 | */ |
| 808 | rc = SQLITE_DONE; |
| 809 | }else{ |
| 810 | u16 pagesize = (u16)iPageSize; |
| 811 | rc = sqlite3PagerSetPagesize(pPager, &pagesize); |
| 812 | assert( rc!=SQLITE_OK || pagesize==(u16)iPageSize ); |
| 813 | } |
| 814 | if( rc ) return rc; |
| 815 | |
| 816 | /* Update the assumed sector-size to match the value used by |
| 817 | ** the process that created this journal. If this journal was |
| 818 | ** created by a process other than this one, then this routine |
| 819 | ** is being called from within pager_playback(). The local value |
| 820 | ** of Pager.sectorSize is restored at the end of that routine. |
| 821 | */ |
| 822 | rc = read32bits(pPager->jfd, jrnlOff+12, &iSectorSize); |
| 823 | if( rc ) return rc; |
| 824 | if( (iSectorSize&(iSectorSize-1)) |
| 825 | || iSectorSize<512 |
| 826 | || iSectorSize>MAX_SECTOR_SIZE |
| 827 | ){ |
| 828 | return SQLITE_DONE; |
| 829 | } |
| 830 | pPager->sectorSize = iSectorSize; |
drh | 98c5835 | 2008-11-07 00:24:53 +0000 | [diff] [blame] | 831 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 832 | |
| 833 | pPager->journalOff += JOURNAL_HDR_SZ(pPager); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 834 | return SQLITE_OK; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | |
| 838 | /* |
| 839 | ** Write the supplied master journal name into the journal file for pager |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 840 | ** pPager at the current location. The master journal name must be the last |
| 841 | ** thing written to a journal file. If the pager is in full-sync mode, the |
| 842 | ** journal file descriptor is advanced to the next sector boundary before |
| 843 | ** anything is written. The format is: |
| 844 | ** |
| 845 | ** + 4 bytes: PAGER_MJ_PGNO. |
| 846 | ** + N bytes: length of master journal name. |
| 847 | ** + 4 bytes: N |
| 848 | ** + 4 bytes: Master journal name checksum. |
| 849 | ** + 8 bytes: aJournalMagic[]. |
| 850 | ** |
| 851 | ** The master journal page checksum is the sum of the bytes in the master |
| 852 | ** journal name. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 853 | ** |
| 854 | ** If zMaster is a NULL pointer (occurs for a single database transaction), |
| 855 | ** this call is a no-op. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 856 | */ |
| 857 | static int writeMasterJournal(Pager *pPager, const char *zMaster){ |
| 858 | int rc; |
| 859 | int len; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 860 | int i; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 861 | i64 jrnlOff; |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 862 | i64 jrnlSize; |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 863 | u32 cksum = 0; |
| 864 | char zBuf[sizeof(aJournalMagic)+2*4]; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 865 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 866 | if( !zMaster || pPager->setMaster ) return SQLITE_OK; |
| 867 | if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ) return SQLITE_OK; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 868 | pPager->setMaster = 1; |
| 869 | |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 870 | len = sqlite3Strlen30(zMaster); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 871 | for(i=0; i<len; i++){ |
| 872 | cksum += zMaster[i]; |
| 873 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 874 | |
| 875 | /* If in full-sync mode, advance to the next disk sector before writing |
| 876 | ** the master journal name. This is in case the previous page written to |
| 877 | ** the journal has already been synced. |
| 878 | */ |
| 879 | if( pPager->fullSync ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 880 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 881 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 882 | jrnlOff = pPager->journalOff; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 883 | pPager->journalOff += (len+20); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 884 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 885 | rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager)); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 886 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 887 | jrnlOff += 4; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 888 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 889 | rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 890 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 891 | jrnlOff += len; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 892 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 893 | put32bits(zBuf, len); |
| 894 | put32bits(&zBuf[4], cksum); |
| 895 | memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic)); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 896 | rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 897 | jrnlOff += 8+sizeof(aJournalMagic); |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 898 | pPager->needSync = !pPager->noSync; |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 899 | |
| 900 | /* If the pager is in peristent-journal mode, then the physical |
| 901 | ** journal-file may extend past the end of the master-journal name |
| 902 | ** and 8 bytes of magic data just written to the file. This is |
| 903 | ** dangerous because the code to rollback a hot-journal file |
| 904 | ** will not be able to find the master-journal name to determine |
| 905 | ** whether or not the journal is hot. |
| 906 | ** |
| 907 | ** Easiest thing to do in this scenario is to truncate the journal |
| 908 | ** file to the required size. |
| 909 | */ |
| 910 | if( (rc==SQLITE_OK) |
| 911 | && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK |
| 912 | && jrnlSize>jrnlOff |
| 913 | ){ |
| 914 | rc = sqlite3OsTruncate(pPager->jfd, jrnlOff); |
| 915 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 916 | return rc; |
| 917 | } |
| 918 | |
| 919 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 920 | ** Find a page in the hash table given its page number. Return |
| 921 | ** a pointer to the page or NULL if not found. |
| 922 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 923 | static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 924 | PgHdr *p; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 925 | sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 926 | return p; |
| 927 | } |
| 928 | |
| 929 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 930 | ** Clear the in-memory cache. This routine |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 931 | ** sets the state of the pager back to what it was when it was first |
| 932 | ** opened. Any outstanding pages are invalidated and subsequent attempts |
| 933 | ** to access those pages will likely result in a coredump. |
| 934 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 935 | static void pager_reset(Pager *pPager){ |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 936 | if( pPager->errCode ) return; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 937 | sqlite3PcacheClear(pPager->pPCache); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 938 | } |
| 939 | |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 940 | /* |
| 941 | ** Free all structures in the Pager.aSavepoint[] array and set both |
| 942 | ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal |
| 943 | ** if it is open and the pager is not in exclusive mode. |
| 944 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 945 | static void releaseAllSavepoint(Pager *pPager){ |
| 946 | int ii; |
| 947 | for(ii=0; ii<pPager->nSavepoint; ii++){ |
| 948 | sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); |
| 949 | } |
| 950 | if( !pPager->exclusiveMode ){ |
| 951 | sqlite3OsClose(pPager->sjfd); |
| 952 | } |
| 953 | sqlite3_free(pPager->aSavepoint); |
| 954 | pPager->aSavepoint = 0; |
| 955 | pPager->nSavepoint = 0; |
danielk1977 | 67ddef6 | 2008-12-23 19:15:56 +0000 | [diff] [blame] | 956 | pPager->stmtNRec = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 957 | } |
| 958 | |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 959 | /* |
| 960 | ** Set the bit number pgno in the PagerSavepoint.pInSavepoint bitvecs of |
| 961 | ** all open savepoints. |
| 962 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 963 | static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){ |
drh | 7539b6b | 2009-01-02 21:39:39 +0000 | [diff] [blame] | 964 | int ii; /* Loop counter */ |
| 965 | int rc = SQLITE_OK; /* Result code */ |
| 966 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 967 | for(ii=0; ii<pPager->nSavepoint; ii++){ |
| 968 | PagerSavepoint *p = &pPager->aSavepoint[ii]; |
| 969 | if( pgno<=p->nOrig ){ |
drh | 7539b6b | 2009-01-02 21:39:39 +0000 | [diff] [blame] | 970 | rc |= sqlite3BitvecSet(p->pInSavepoint, pgno); |
| 971 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
drh | 7539b6b | 2009-01-02 21:39:39 +0000 | [diff] [blame] | 974 | return rc; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 975 | } |
| 976 | |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 977 | /* |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 978 | ** Unlock the database file. |
| 979 | ** |
| 980 | ** If the pager is currently in error state, discard the contents of |
| 981 | ** the cache and reset the Pager structure internal state. If there is |
| 982 | ** an open journal-file, then the next time a shared-lock is obtained |
| 983 | ** on the pager file (by this or any other process), it will be |
| 984 | ** treated as a hot-journal and rolled back. |
| 985 | */ |
| 986 | static void pager_unlock(Pager *pPager){ |
| 987 | if( !pPager->exclusiveMode ){ |
danielk1977 | 5f2d46b | 2009-01-13 16:03:44 +0000 | [diff] [blame^] | 988 | int rc; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 989 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 990 | /* Always close the journal file when dropping the database lock. |
| 991 | ** Otherwise, another connection with journal_mode=delete might |
| 992 | ** delete the file out from under us. |
| 993 | */ |
| 994 | if( pPager->journalOpen ){ |
| 995 | sqlite3OsClose(pPager->jfd); |
| 996 | pPager->journalOpen = 0; |
| 997 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 998 | pPager->pInJournal = 0; |
| 999 | sqlite3BitvecDestroy(pPager->pAlwaysRollback); |
| 1000 | pPager->pAlwaysRollback = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
danielk1977 | 5f2d46b | 2009-01-13 16:03:44 +0000 | [diff] [blame^] | 1003 | rc = osUnlock(pPager->fd, NO_LOCK); |
| 1004 | if( rc ) pPager->errCode = rc; |
| 1005 | pPager->dbSizeValid = 0; |
| 1006 | IOTRACE(("UNLOCK %p\n", pPager)) |
| 1007 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1008 | /* If Pager.errCode is set, the contents of the pager cache cannot be |
| 1009 | ** trusted. Now that the pager file is unlocked, the contents of the |
| 1010 | ** cache can be discarded and the error code safely cleared. |
| 1011 | */ |
| 1012 | if( pPager->errCode ){ |
| 1013 | if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK; |
| 1014 | pager_reset(pPager); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1015 | releaseAllSavepoint(pPager); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1016 | pPager->journalOff = 0; |
| 1017 | pPager->journalStarted = 0; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 1018 | pPager->dbOrigSize = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1019 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1020 | |
| 1021 | pPager->state = PAGER_UNLOCK; |
| 1022 | pPager->changeCountDone = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | ** Execute a rollback if a transaction is active and unlock the |
| 1028 | ** database file. If the pager has already entered the error state, |
| 1029 | ** do not attempt the rollback. |
| 1030 | */ |
| 1031 | static void pagerUnlockAndRollback(Pager *p){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1032 | if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){ |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 1033 | sqlite3BeginBenignMalloc(); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1034 | sqlite3PagerRollback(p); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 1035 | sqlite3EndBenignMalloc(); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1036 | } |
| 1037 | pager_unlock(p); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1041 | ** This routine ends a transaction. A transaction is ended by either |
| 1042 | ** a COMMIT or a ROLLBACK. |
| 1043 | ** |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1044 | ** When this routine is called, the pager has the journal file open and |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1045 | ** a RESERVED or EXCLUSIVE lock on the database. This routine will release |
| 1046 | ** the database lock and acquires a SHARED lock in its place if that is |
| 1047 | ** the appropriate thing to do. Release locks usually is appropriate, |
| 1048 | ** unless we are in exclusive access mode or unless this is a |
| 1049 | ** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation. |
| 1050 | ** |
| 1051 | ** The journal file is either deleted or truncated. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1052 | ** |
| 1053 | ** TODO: Consider keeping the journal file open for temporary databases. |
| 1054 | ** This might give a performance improvement on windows where opening |
| 1055 | ** a file is an expensive operation. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1056 | */ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 1057 | static int pager_end_transaction(Pager *pPager, int hasMaster){ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1058 | int rc = SQLITE_OK; |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1059 | int rc2 = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1060 | if( pPager->state<PAGER_RESERVED ){ |
| 1061 | return SQLITE_OK; |
| 1062 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1063 | releaseAllSavepoint(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1064 | if( pPager->journalOpen ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1065 | if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ |
| 1066 | int isMemoryJournal = sqlite3IsMemJournal(pPager->jfd); |
| 1067 | sqlite3OsClose(pPager->jfd); |
| 1068 | pPager->journalOpen = 0; |
| 1069 | if( !isMemoryJournal ){ |
| 1070 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
| 1071 | } |
| 1072 | }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE |
drh | 0433588 | 2008-09-26 21:08:08 +0000 | [diff] [blame] | 1073 | && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){ |
| 1074 | pPager->journalOff = 0; |
| 1075 | pPager->journalStarted = 0; |
| 1076 | }else if( pPager->exclusiveMode |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1077 | || pPager->journalMode==PAGER_JOURNALMODE_PERSIST |
| 1078 | ){ |
| 1079 | rc = zeroJournalHdr(pPager, hasMaster); |
| 1080 | pager_error(pPager, rc); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1081 | pPager->journalOff = 0; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1082 | pPager->journalStarted = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1083 | }else{ |
drh | 0433588 | 2008-09-26 21:08:08 +0000 | [diff] [blame] | 1084 | assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || rc ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1085 | sqlite3OsClose(pPager->jfd); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1086 | pPager->journalOpen = 0; |
danielk1977 | 0f01fda | 2008-06-06 16:14:02 +0000 | [diff] [blame] | 1087 | if( rc==SQLITE_OK && !pPager->tempFile ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1088 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
danielk1977 | 7152de8 | 2007-03-29 17:28:14 +0000 | [diff] [blame] | 1089 | } |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1090 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1091 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 1092 | pPager->pInJournal = 0; |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 1093 | sqlite3BitvecDestroy(pPager->pAlwaysRollback); |
| 1094 | pPager->pAlwaysRollback = 0; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1095 | #ifdef SQLITE_CHECK_PAGES |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1096 | sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1097 | #endif |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1098 | sqlite3PcacheCleanAll(pPager->pPCache); |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1099 | pPager->dirtyCache = 0; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1100 | pPager->nRec = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1101 | }else{ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1102 | assert( pPager->pInJournal==0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1103 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1104 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1105 | if( !pPager->exclusiveMode ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1106 | rc2 = osUnlock(pPager->fd, SHARED_LOCK); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1107 | pPager->state = PAGER_SHARED; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1108 | }else if( pPager->state==PAGER_SYNCED ){ |
| 1109 | pPager->state = PAGER_EXCLUSIVE; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1110 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 1111 | pPager->dbOrigSize = 0; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1112 | pPager->setMaster = 0; |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 1113 | pPager->needSync = 0; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1114 | /* lruListSetFirstSynced(pPager); */ |
danielk1977 | f90b726 | 2009-01-07 15:18:20 +0000 | [diff] [blame] | 1115 | sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1116 | if( !MEMDB ){ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 1117 | pPager->dbSizeValid = 0; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1118 | } |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 1119 | pPager->dbModified = 0; |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1120 | |
| 1121 | return (rc==SQLITE_OK?rc2:rc); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1124 | /* |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1125 | ** Compute and return a checksum for the page of data. |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1126 | ** |
| 1127 | ** This is not a real checksum. It is really just the sum of the |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1128 | ** random initial value and the page number. We experimented with |
| 1129 | ** a checksum of the entire data, but that was found to be too slow. |
| 1130 | ** |
| 1131 | ** Note that the page number is stored at the beginning of data and |
| 1132 | ** the checksum is stored at the end. This is important. If journal |
| 1133 | ** corruption occurs due to a power failure, the most likely scenario |
| 1134 | ** is that one end or the other of the record will be changed. It is |
| 1135 | ** much less likely that the two ends of the journal record will be |
| 1136 | ** correct and the middle be corrupt. Thus, this "checksum" scheme, |
| 1137 | ** though fast and simple, catches the mostly likely kind of corruption. |
| 1138 | ** |
| 1139 | ** FIX ME: Consider adding every 200th (or so) byte of the data to the |
| 1140 | ** checksum. That way if a single page spans 3 or more disk sectors and |
| 1141 | ** only the middle sector is corrupt, we will still have a reasonable |
| 1142 | ** chance of failing the checksum and thus detecting the problem. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1143 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1144 | static u32 pager_cksum(Pager *pPager, const u8 *aData){ |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1145 | u32 cksum = pPager->cksumInit; |
| 1146 | int i = pPager->pageSize-200; |
| 1147 | while( i>0 ){ |
| 1148 | cksum += aData[i]; |
| 1149 | i -= 200; |
| 1150 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1151 | return cksum; |
| 1152 | } |
| 1153 | |
| 1154 | /* |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1155 | ** Read a single page from either the journal file (if isMainJrnl==1) or |
| 1156 | ** from the sub-journal (if isMainJrnl==0) and playback that page. |
| 1157 | ** The page begins at offset *pOffset into the file. The *pOffset |
| 1158 | ** value is increased to the start of the next page in the journal. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1159 | ** |
drh | c13148f | 2008-08-27 18:03:20 +0000 | [diff] [blame] | 1160 | ** The isMainJrnl flag is true if this is the main rollback journal and |
| 1161 | ** false for the statement journal. The main rollback journal uses |
| 1162 | ** checksums - the statement journal does not. |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1163 | ** |
| 1164 | ** If pDone is not NULL, then it is a record of pages that have already |
| 1165 | ** been played back. If the page at *pOffset has already been played back |
| 1166 | ** (if the corresponding pDone bit is set) then skip the playback. |
| 1167 | ** Make sure the pDone bit corresponding to the *pOffset page is set |
| 1168 | ** prior to returning. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1169 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1170 | static int pager_playback_one_page( |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1171 | Pager *pPager, /* The pager being played back */ |
| 1172 | int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */ |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1173 | i64 *pOffset, /* Offset of record to playback */ |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1174 | int isSavepnt, /* True for a savepoint rollback */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1175 | Bitvec *pDone /* Bitvec of pages already played back */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1176 | ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1177 | int rc; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1178 | PgHdr *pPg; /* An existing page in the cache */ |
| 1179 | Pgno pgno; /* The page number of a page in journal */ |
| 1180 | u32 cksum; /* Checksum used for sanity checking */ |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1181 | u8 *aData; /* Temporary storage for the page */ |
| 1182 | sqlite3_file *jfd; /* The file descriptor for the journal file */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1183 | |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1184 | assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */ |
| 1185 | assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */ |
| 1186 | assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */ |
| 1187 | assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */ |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1188 | |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1189 | aData = (u8*)pPager->pTmpSpace; |
| 1190 | assert( aData ); /* Temp storage must have already been allocated */ |
| 1191 | |
| 1192 | jfd = isMainJrnl ? pPager->jfd : pPager->sjfd; |
| 1193 | |
| 1194 | rc = read32bits(jfd, *pOffset, &pgno); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1195 | if( rc!=SQLITE_OK ) return rc; |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1196 | rc = sqlite3OsRead(jfd, aData, pPager->pageSize, (*pOffset)+4); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1197 | if( rc!=SQLITE_OK ) return rc; |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1198 | *pOffset += pPager->pageSize + 4 + isMainJrnl*4; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1199 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1200 | /* Sanity checking on the page. This is more important that I originally |
| 1201 | ** thought. If a power failure occurs while the journal is being written, |
| 1202 | ** it could cause invalid data to be written into the journal. We need to |
| 1203 | ** detect this invalid data (with high probability) and ignore it. |
| 1204 | */ |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1205 | if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1206 | return SQLITE_DONE; |
| 1207 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1208 | if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1209 | return SQLITE_OK; |
| 1210 | } |
drh | c13148f | 2008-08-27 18:03:20 +0000 | [diff] [blame] | 1211 | if( isMainJrnl ){ |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1212 | rc = read32bits(jfd, (*pOffset)-4, &cksum); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1213 | if( rc ) return rc; |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1214 | if( !isSavepnt && pager_cksum(pPager, aData)!=cksum ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1215 | return SQLITE_DONE; |
| 1216 | } |
| 1217 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1218 | if( pDone && (rc = sqlite3BitvecSet(pDone, pgno)) ){ |
| 1219 | return rc; |
| 1220 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1221 | |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 1222 | assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE ); |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1223 | |
| 1224 | /* If the pager is in RESERVED state, then there must be a copy of this |
| 1225 | ** page in the pager cache. In this case just update the pager cache, |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 1226 | ** not the database file. The page is left marked dirty in this case. |
| 1227 | ** |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1228 | ** An exception to the above rule: If the database is in no-sync mode |
| 1229 | ** and a page is moved during an incremental vacuum then the page may |
danielk1977 | 369f3a0 | 2007-05-24 09:41:20 +0000 | [diff] [blame] | 1230 | ** not be in the pager cache. Later: if a malloc() or IO error occurs |
| 1231 | ** during a Movepage() call, then the page may not be in the cache |
| 1232 | ** either. So the condition described in the above paragraph is not |
| 1233 | ** assert()able. |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1234 | ** |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1235 | ** If in EXCLUSIVE state, then we update the pager cache if it exists |
| 1236 | ** and the main file. The page is then marked not dirty. |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1237 | ** |
| 1238 | ** Ticket #1171: The statement journal might contain page content that is |
| 1239 | ** different from the page content at the start of the transaction. |
| 1240 | ** This occurs when a page is changed prior to the start of a statement |
| 1241 | ** then changed again within the statement. When rolling back such a |
| 1242 | ** statement we must not write to the original database unless we know |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 1243 | ** for certain that original page contents are synced into the main rollback |
| 1244 | ** journal. Otherwise, a power loss might leave modified data in the |
| 1245 | ** database file without an entry in the rollback journal that can |
| 1246 | ** restore the database to its original form. Two conditions must be |
| 1247 | ** met before writing to the database files. (1) the database must be |
| 1248 | ** locked. (2) we know that the original page content is fully synced |
| 1249 | ** in the main journal either because the page is not in cache or else |
| 1250 | ** the page is marked as needSync==0. |
drh | 4c02a23 | 2008-04-14 23:13:45 +0000 | [diff] [blame] | 1251 | ** |
| 1252 | ** 2008-04-14: When attempting to vacuum a corrupt database file, it |
| 1253 | ** is possible to fail a statement on a database that does not yet exist. |
| 1254 | ** Do not attempt to write if database file has never been opened. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1255 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1256 | pPg = pager_lookup(pPager, pgno); |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 1257 | PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1258 | PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData), |
| 1259 | (isMainJrnl?"main-journal":"sub-journal") |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 1260 | )); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1261 | if( (pPager->state>=PAGER_EXCLUSIVE) |
| 1262 | && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC)) |
| 1263 | && (pPager->fd->pMethods) |
| 1264 | ){ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1265 | i64 ofst = (pgno-1)*(i64)pPager->pageSize; |
| 1266 | rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 1267 | if( pgno>pPager->dbFileSize ){ |
| 1268 | pPager->dbFileSize = pgno; |
| 1269 | } |
danielk1977 | f2c31ad | 2009-01-06 13:40:08 +0000 | [diff] [blame] | 1270 | }else if( !isMainJrnl && pPg==0 ){ |
| 1271 | /* If this is a rollback of a savepoint and data was not written to |
| 1272 | ** the database and the page is not in-memory, there is a potential |
| 1273 | ** problem. When the page is next fetched by the b-tree layer, it |
| 1274 | ** will be read from the database file, which may or may not be |
| 1275 | ** current. |
| 1276 | ** |
| 1277 | ** There are a couple of different ways this can happen. All are quite |
danielk1977 | 401b65e | 2009-01-06 14:34:34 +0000 | [diff] [blame] | 1278 | ** obscure. When running in synchronous mode, this can only happen |
danielk1977 | f2c31ad | 2009-01-06 13:40:08 +0000 | [diff] [blame] | 1279 | ** if the page is on the free-list at the start of the transaction, then |
| 1280 | ** populated, then moved using sqlite3PagerMovepage(). |
| 1281 | ** |
| 1282 | ** The solution is to add an in-memory page to the cache containing |
| 1283 | ** the data just read from the sub-journal. Mark the page as dirty |
| 1284 | ** and if the pager requires a journal-sync, then mark the page as |
| 1285 | ** requiring a journal-sync before it is written. |
| 1286 | */ |
| 1287 | assert( isSavepnt ); |
| 1288 | if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1)) ){ |
| 1289 | return rc; |
| 1290 | } |
| 1291 | pPg->flags &= ~PGHDR_NEED_READ; |
| 1292 | sqlite3PcacheMakeDirty(pPg); |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1293 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1294 | if( pPg ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1295 | /* No page should ever be explicitly rolled back that is in use, except |
| 1296 | ** for page 1 which is held in use in order to keep the lock on the |
| 1297 | ** database active. However such a page may be rolled back as a result |
| 1298 | ** of an internal error resulting in an automatic call to |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1299 | ** sqlite3PagerRollback(). |
drh | acf4ac9 | 2003-12-17 23:57:34 +0000 | [diff] [blame] | 1300 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1301 | void *pData; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1302 | pData = pPg->pData; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1303 | memcpy(pData, aData, pPager->pageSize); |
danielk1977 | 9038bb6 | 2007-04-09 11:20:54 +0000 | [diff] [blame] | 1304 | if( pPager->xReiniter ){ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1305 | pPager->xReiniter(pPg); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1306 | } |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1307 | if( isMainJrnl && (!isSavepnt || pPager->journalOff<=pPager->journalHdr) ){ |
danielk1977 | 488af09 | 2008-12-19 16:31:11 +0000 | [diff] [blame] | 1308 | /* If the contents of this page were just restored from the main |
| 1309 | ** journal file, then its content must be as they were when the |
| 1310 | ** transaction was first opened. In this case we can mark the page |
| 1311 | ** as clean, since there will be no need to write it out to the. |
| 1312 | ** |
| 1313 | ** There is one exception to this rule. If the page is being rolled |
| 1314 | ** back as part of a savepoint (or statement) rollback from an |
| 1315 | ** unsynced portion of the main journal file, then it is not safe |
| 1316 | ** to mark the page as clean. This is because marking the page as |
| 1317 | ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is |
| 1318 | ** already in the journal file (recorded in Pager.pInJournal) and |
| 1319 | ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to |
| 1320 | ** again within this transaction, it will be marked as dirty but |
| 1321 | ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially |
| 1322 | ** be written out into the database file before its journal file |
| 1323 | ** segment is synced. If a crash occurs during or following this, |
| 1324 | ** database corruption may ensue. |
| 1325 | */ |
drh | c047b9f | 2008-11-21 03:23:43 +0000 | [diff] [blame] | 1326 | sqlite3PcacheMakeClean(pPg); |
| 1327 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1328 | #ifdef SQLITE_CHECK_PAGES |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1329 | pPg->pageHash = pager_pagehash(pPg); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1330 | #endif |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1331 | /* If this was page 1, then restore the value of Pager.dbFileVers. |
| 1332 | ** Do this before any decoding. */ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1333 | if( pgno==1 ){ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1334 | memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1335 | } |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1336 | |
| 1337 | /* Decode the page just read from disk */ |
| 1338 | CODEC1(pPager, pData, pPg->pgno, 3); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1339 | sqlite3PcacheRelease(pPg); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1340 | } |
| 1341 | return rc; |
| 1342 | } |
| 1343 | |
drh | ee03d62 | 2009-01-07 15:33:45 +0000 | [diff] [blame] | 1344 | #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST) |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1345 | /* |
| 1346 | ** This routine looks ahead into the main journal file and determines |
| 1347 | ** whether or not the next record (the record that begins at file |
| 1348 | ** offset pPager->journalOff) is a well-formed page record consisting |
| 1349 | ** of a valid page number, pPage->pageSize bytes of content, followed |
| 1350 | ** by a valid checksum. |
| 1351 | ** |
| 1352 | ** The pager never needs to know this in order to do its job. This |
| 1353 | ** routine is only used from with assert() and testcase() macros. |
| 1354 | */ |
| 1355 | static int pagerNextJournalPageIsValid(Pager *pPager){ |
| 1356 | Pgno pgno; /* The page number of the page */ |
| 1357 | u32 cksum; /* The page checksum */ |
| 1358 | int rc; /* Return code from read operations */ |
| 1359 | sqlite3_file *fd; /* The file descriptor from which we are reading */ |
| 1360 | u8 *aData; /* Content of the page */ |
| 1361 | |
| 1362 | /* Read the page number header */ |
| 1363 | fd = pPager->jfd; |
| 1364 | rc = read32bits(fd, pPager->journalOff, &pgno); |
| 1365 | if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/ |
| 1366 | if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ return 0; } /*NO_TEST*/ |
| 1367 | if( pgno>(Pgno)pPager->dbSize ){ return 0; } /*NO_TEST*/ |
| 1368 | |
| 1369 | /* Read the checksum */ |
| 1370 | rc = read32bits(fd, pPager->journalOff+pPager->pageSize+4, &cksum); |
| 1371 | if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/ |
| 1372 | |
| 1373 | /* Read the data and verify the checksum */ |
| 1374 | aData = (u8*)pPager->pTmpSpace; |
| 1375 | rc = sqlite3OsRead(fd, aData, pPager->pageSize, pPager->journalOff+4); |
| 1376 | if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/ |
| 1377 | if( pager_cksum(pPager, aData)!=cksum ){ return 0; } /*NO_TEST*/ |
| 1378 | |
| 1379 | /* Reach this point only if the page is valid */ |
| 1380 | return 1; |
| 1381 | } |
| 1382 | #endif /* !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST) */ |
| 1383 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1384 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1385 | ** Parameter zMaster is the name of a master journal file. A single journal |
| 1386 | ** file that referred to the master journal file has just been rolled back. |
| 1387 | ** This routine checks if it is possible to delete the master journal file, |
| 1388 | ** and does so if it is. |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1389 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1390 | ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not |
| 1391 | ** available for use within this function. |
| 1392 | ** |
| 1393 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1394 | ** The master journal file contains the names of all child journals. |
| 1395 | ** To tell if a master journal can be deleted, check to each of the |
| 1396 | ** children. If all children are either missing or do not refer to |
| 1397 | ** a different master journal, then this master journal can be deleted. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1398 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1399 | static int pager_delmaster(Pager *pPager, const char *zMaster){ |
| 1400 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1401 | int rc; |
| 1402 | int master_open = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1403 | sqlite3_file *pMaster; |
| 1404 | sqlite3_file *pJournal; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1405 | char *zMasterJournal = 0; /* Contents of master journal file */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1406 | i64 nMasterJournal; /* Size of master journal file */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1407 | |
| 1408 | /* Open the master journal file exclusively in case some other process |
| 1409 | ** is running this routine also. Not that it makes too much difference. |
| 1410 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1411 | pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1412 | pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1413 | if( !pMaster ){ |
| 1414 | rc = SQLITE_NOMEM; |
| 1415 | }else{ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1416 | int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); |
| 1417 | rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1418 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1419 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1420 | master_open = 1; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1421 | |
| 1422 | rc = sqlite3OsFileSize(pMaster, &nMasterJournal); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1423 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1424 | |
| 1425 | if( nMasterJournal>0 ){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1426 | char *zJournal; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1427 | char *zMasterPtr = 0; |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1428 | int nMasterPtr = pPager->pVfs->mxPathname+1; |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1429 | |
| 1430 | /* Load the entire master journal file into space obtained from |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1431 | ** sqlite3_malloc() and pointed to by zMasterJournal. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1432 | */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1433 | zMasterJournal = (char *)sqlite3Malloc((int)nMasterJournal + nMasterPtr); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1434 | if( !zMasterJournal ){ |
| 1435 | rc = SQLITE_NOMEM; |
| 1436 | goto delmaster_out; |
| 1437 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1438 | zMasterPtr = &zMasterJournal[nMasterJournal]; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1439 | rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1440 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1441 | |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1442 | zJournal = zMasterJournal; |
| 1443 | while( (zJournal-zMasterJournal)<nMasterJournal ){ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1444 | int exists; |
| 1445 | rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 1446 | if( rc!=SQLITE_OK ){ |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 1447 | goto delmaster_out; |
| 1448 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1449 | if( exists ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1450 | /* One of the journals pointed to by the master journal exists. |
| 1451 | ** Open it and check if it points at the master journal. If |
| 1452 | ** so, return without deleting the master journal file. |
| 1453 | */ |
drh | 3b7b78b | 2004-11-24 01:16:43 +0000 | [diff] [blame] | 1454 | int c; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1455 | int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL); |
| 1456 | rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1457 | if( rc!=SQLITE_OK ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1458 | goto delmaster_out; |
| 1459 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1460 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1461 | rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1462 | sqlite3OsClose(pJournal); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 1463 | if( rc!=SQLITE_OK ){ |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 1464 | goto delmaster_out; |
| 1465 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1466 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1467 | c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0; |
drh | 3b7b78b | 2004-11-24 01:16:43 +0000 | [diff] [blame] | 1468 | if( c ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1469 | /* We have a match. Do not delete the master journal file. */ |
| 1470 | goto delmaster_out; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1471 | } |
| 1472 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1473 | zJournal += (sqlite3Strlen30(zJournal)+1); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1477 | rc = sqlite3OsDelete(pVfs, zMaster, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1478 | |
| 1479 | delmaster_out: |
| 1480 | if( zMasterJournal ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1481 | sqlite3_free(zMasterJournal); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1482 | } |
| 1483 | if( master_open ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1484 | sqlite3OsClose(pMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1485 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1486 | sqlite3_free(pMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1487 | return rc; |
| 1488 | } |
| 1489 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1490 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1491 | /* |
danielk1977 | f90b726 | 2009-01-07 15:18:20 +0000 | [diff] [blame] | 1492 | ** If the main database file is open and an exclusive lock is held, |
| 1493 | ** truncate the main file of the given pager to the specified number |
| 1494 | ** of pages. |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1495 | ** |
danielk1977 | f90b726 | 2009-01-07 15:18:20 +0000 | [diff] [blame] | 1496 | ** It might might be the case that the file on disk is smaller than nPage. |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1497 | ** This can happen, for example, if we are in the middle of a transaction |
| 1498 | ** which has extended the file size and the new pages are still all held |
| 1499 | ** in cache, then an INSERT or UPDATE does a statement rollback. Some |
| 1500 | ** operating system implementations can get confused if you try to |
| 1501 | ** truncate a file to some size that is larger than it currently is, |
danielk1977 | 06e11af | 2008-05-06 18:13:26 +0000 | [diff] [blame] | 1502 | ** so detect this case and write a single zero byte to the end of the new |
| 1503 | ** file instead. |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1504 | */ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 1505 | static int pager_truncate(Pager *pPager, Pgno nPage){ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1506 | int rc = SQLITE_OK; |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1507 | if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){ |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1508 | i64 currentSize, newSize; |
| 1509 | rc = sqlite3OsFileSize(pPager->fd, ¤tSize); |
| 1510 | newSize = pPager->pageSize*(i64)nPage; |
danielk1977 | 06e11af | 2008-05-06 18:13:26 +0000 | [diff] [blame] | 1511 | if( rc==SQLITE_OK && currentSize!=newSize ){ |
| 1512 | if( currentSize>newSize ){ |
| 1513 | rc = sqlite3OsTruncate(pPager->fd, newSize); |
| 1514 | }else{ |
| 1515 | rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1); |
| 1516 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 1517 | if( rc==SQLITE_OK ){ |
| 1518 | pPager->dbFileSize = nPage; |
| 1519 | } |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1520 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1521 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1522 | return rc; |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | /* |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1526 | ** Set the sectorSize for the given pager. |
| 1527 | ** |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1528 | ** The sector size is at least as big as the sector size reported |
danielk1977 | 7cbd589 | 2009-01-10 16:15:09 +0000 | [diff] [blame] | 1529 | ** by sqlite3OsSectorSize(). The minimum sector size is 512. |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1530 | */ |
| 1531 | static void setSectorSize(Pager *pPager){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1532 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 1533 | if( !pPager->tempFile ){ |
| 1534 | /* Sector size doesn't matter for temporary files. Also, the file |
| 1535 | ** may not have been opened yet, in whcih case the OsSectorSize() |
| 1536 | ** call will segfault. |
| 1537 | */ |
| 1538 | pPager->sectorSize = sqlite3OsSectorSize(pPager->fd); |
| 1539 | } |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1540 | if( pPager->sectorSize<512 ){ |
| 1541 | pPager->sectorSize = 512; |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1542 | } |
danielk1977 | 7cbd589 | 2009-01-10 16:15:09 +0000 | [diff] [blame] | 1543 | if( pPager->sectorSize>MAX_SECTOR_SIZE ){ |
| 1544 | pPager->sectorSize = MAX_SECTOR_SIZE; |
| 1545 | } |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1549 | ** Playback the journal and thus restore the database file to |
| 1550 | ** the state it was in before we started making changes. |
| 1551 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1552 | ** The journal file format is as follows: |
| 1553 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1554 | ** (1) 8 byte prefix. A copy of aJournalMagic[]. |
| 1555 | ** (2) 4 byte big-endian integer which is the number of valid page records |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1556 | ** in the journal. If this value is 0xffffffff, then compute the |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1557 | ** number of page records from the journal size. |
| 1558 | ** (3) 4 byte big-endian integer which is the initial value for the |
| 1559 | ** sanity checksum. |
| 1560 | ** (4) 4 byte integer which is the number of pages to truncate the |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1561 | ** database to during a rollback. |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1562 | ** (5) 4 byte big-endian integer which is the sector size. The header |
| 1563 | ** is this many bytes in size. |
| 1564 | ** (6) 4 byte big-endian integer which is the page case. |
| 1565 | ** (7) 4 byte integer which is the number of bytes in the master journal |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1566 | ** name. The value may be zero (indicate that there is no master |
| 1567 | ** journal.) |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1568 | ** (8) N bytes of the master journal name. The name will be nul-terminated |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1569 | ** and might be shorter than the value read from (5). If the first byte |
| 1570 | ** of the name is \000 then there is no master journal. The master |
| 1571 | ** journal name is stored in UTF-8. |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1572 | ** (9) Zero or more pages instances, each as follows: |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1573 | ** + 4 byte page number. |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1574 | ** + pPager->pageSize bytes of data. |
| 1575 | ** + 4 byte checksum |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1576 | ** |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1577 | ** When we speak of the journal header, we mean the first 8 items above. |
| 1578 | ** Each entry in the journal is an instance of the 9th item. |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1579 | ** |
| 1580 | ** Call the value from the second bullet "nRec". nRec is the number of |
| 1581 | ** valid page entries in the journal. In most cases, you can compute the |
| 1582 | ** value of nRec from the size of the journal file. But if a power |
| 1583 | ** failure occurred while the journal was being written, it could be the |
| 1584 | ** case that the size of the journal file had already been increased but |
| 1585 | ** the extra entries had not yet made it safely to disk. In such a case, |
| 1586 | ** the value of nRec computed from the file size would be too large. For |
| 1587 | ** that reason, we always use the nRec value in the header. |
| 1588 | ** |
| 1589 | ** If the nRec value is 0xffffffff it means that nRec should be computed |
| 1590 | ** from the file size. This value is used when the user selects the |
| 1591 | ** no-sync option for the journal. A power failure could lead to corruption |
| 1592 | ** in this case. But for things like temporary table (which will be |
| 1593 | ** deleted when the power is restored) we don't care. |
| 1594 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1595 | ** If the file opened as the journal file is not a well-formed |
danielk1977 | ece80f1 | 2004-06-23 01:05:26 +0000 | [diff] [blame] | 1596 | ** journal file then all pages up to the first corrupted page are rolled |
| 1597 | ** back (or no pages if the journal header is corrupted). The journal file |
| 1598 | ** is then deleted and SQLITE_OK returned, just as if no corruption had |
| 1599 | ** been encountered. |
| 1600 | ** |
| 1601 | ** If an I/O or malloc() error occurs, the journal-file is not deleted |
| 1602 | ** and an error code is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1603 | */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1604 | static int pager_playback(Pager *pPager, int isHot){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1605 | sqlite3_vfs *pVfs = pPager->pVfs; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1606 | i64 szJ; /* Size of the journal file in bytes */ |
danielk1977 | c3e8f5e | 2004-06-28 01:16:46 +0000 | [diff] [blame] | 1607 | u32 nRec; /* Number of Records in the journal */ |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 1608 | u32 u; /* Unsigned loop counter */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1609 | Pgno mxPg = 0; /* Size of the original file in pages */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1610 | int rc; /* Result code of a subroutine */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1611 | int res = 1; /* Value returned by sqlite3OsAccess() */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1612 | char *zMaster = 0; /* Name of master journal file if any */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1613 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1614 | /* Figure out how many records are in the journal. Abort early if |
| 1615 | ** the journal is empty. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1616 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1617 | assert( pPager->journalOpen ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1618 | rc = sqlite3OsFileSize(pPager->jfd, &szJ); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1619 | if( rc!=SQLITE_OK || szJ==0 ){ |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1620 | goto end_playback; |
| 1621 | } |
drh | 240c579 | 2004-02-08 00:40:52 +0000 | [diff] [blame] | 1622 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1623 | /* Read the master journal name from the journal, if it is present. |
| 1624 | ** If a master journal file name is specified, but the file is not |
| 1625 | ** present on disk, then the journal is not hot and does not need to be |
| 1626 | ** played back. |
drh | 240c579 | 2004-02-08 00:40:52 +0000 | [diff] [blame] | 1627 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1628 | zMaster = pPager->pTmpSpace; |
| 1629 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1630 | if( rc==SQLITE_OK && zMaster[0] ){ |
| 1631 | rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1632 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1633 | zMaster = 0; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1634 | if( rc!=SQLITE_OK || !res ){ |
danielk1977 | ce98bba | 2008-04-03 10:13:01 +0000 | [diff] [blame] | 1635 | goto end_playback; |
| 1636 | } |
| 1637 | pPager->journalOff = 0; |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1638 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1639 | /* This loop terminates either when the readJournalHdr() call returns |
| 1640 | ** SQLITE_DONE or an IO error occurs. */ |
| 1641 | while( 1 ){ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1642 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1643 | /* Read the next journal header from the journal file. If there are |
| 1644 | ** not enough bytes left in the journal file for a complete header, or |
| 1645 | ** it is corrupted, then a process must of failed while writing it. |
| 1646 | ** This indicates nothing more needs to be rolled back. |
| 1647 | */ |
| 1648 | rc = readJournalHdr(pPager, szJ, &nRec, &mxPg); |
| 1649 | if( rc!=SQLITE_OK ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1650 | if( rc==SQLITE_DONE ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1651 | rc = SQLITE_OK; |
| 1652 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1653 | goto end_playback; |
| 1654 | } |
| 1655 | |
| 1656 | /* If nRec is 0xffffffff, then this journal was created by a process |
| 1657 | ** working in no-sync mode. This means that the rest of the journal |
| 1658 | ** file consists of pages, there are no more journal headers. Compute |
| 1659 | ** the value of nRec based on this assumption. |
| 1660 | */ |
| 1661 | if( nRec==0xffffffff ){ |
| 1662 | assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1663 | nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager)); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1666 | /* If nRec is 0 and this rollback is of a transaction created by this |
drh | 8940f4e | 2007-08-11 00:26:20 +0000 | [diff] [blame] | 1667 | ** process and if this is the final header in the journal, then it means |
| 1668 | ** that this part of the journal was being filled but has not yet been |
| 1669 | ** synced to disk. Compute the number of pages based on the remaining |
| 1670 | ** size of the file. |
| 1671 | ** |
| 1672 | ** The third term of the test was added to fix ticket #2565. |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1673 | ** When rolling back a hot journal, nRec==0 always means that the next |
| 1674 | ** chunk of the journal contains zero pages to be rolled back. But |
| 1675 | ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in |
| 1676 | ** the journal, it means that the journal might contain additional |
| 1677 | ** pages that need to be rolled back and that the number of pages |
drh | ee03d62 | 2009-01-07 15:33:45 +0000 | [diff] [blame] | 1678 | ** should be computed based on the journal file size. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1679 | */ |
drh | 4fd18c4 | 2009-01-08 15:24:01 +0000 | [diff] [blame] | 1680 | testcase( nRec==0 && !isHot |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1681 | && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)!=pPager->journalOff |
| 1682 | && ((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager))>0 |
drh | 4fd18c4 | 2009-01-08 15:24:01 +0000 | [diff] [blame] | 1683 | && pagerNextJournalPageIsValid(pPager) |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1684 | ); |
drh | 8940f4e | 2007-08-11 00:26:20 +0000 | [diff] [blame] | 1685 | if( nRec==0 && !isHot && |
| 1686 | pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1687 | nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager)); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1690 | /* If this is the first header read from the journal, truncate the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 1691 | ** database file back to its original size. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1692 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1693 | if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){ |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1694 | rc = pager_truncate(pPager, mxPg); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1695 | if( rc!=SQLITE_OK ){ |
| 1696 | goto end_playback; |
| 1697 | } |
danielk1977 | f90b726 | 2009-01-07 15:18:20 +0000 | [diff] [blame] | 1698 | pPager->dbSize = mxPg; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1701 | /* Copy original pages out of the journal and back into the database file. |
| 1702 | */ |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 1703 | for(u=0; u<nRec; u++){ |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1704 | rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 0, 0); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1705 | if( rc!=SQLITE_OK ){ |
| 1706 | if( rc==SQLITE_DONE ){ |
| 1707 | rc = SQLITE_OK; |
| 1708 | pPager->journalOff = szJ; |
| 1709 | break; |
| 1710 | }else{ |
drh | a9625ea | 2008-09-03 00:08:29 +0000 | [diff] [blame] | 1711 | /* If we are unable to rollback, then the database is probably |
| 1712 | ** going to end up being corrupt. It is corrupt to us, anyhow. |
| 1713 | ** Perhaps the next process to come along can fix it.... |
| 1714 | */ |
danielk1977 | 98c2190 | 2008-09-23 16:41:29 +0000 | [diff] [blame] | 1715 | rc = SQLITE_CORRUPT_BKPT; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1716 | goto end_playback; |
| 1717 | } |
| 1718 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1719 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1720 | } |
drh | 580eeaf | 2006-02-24 03:09:37 +0000 | [diff] [blame] | 1721 | /*NOTREACHED*/ |
| 1722 | assert( 0 ); |
drh | 4a0681e | 2003-02-13 01:58:20 +0000 | [diff] [blame] | 1723 | |
| 1724 | end_playback: |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 1725 | if( rc==SQLITE_OK ){ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1726 | zMaster = pPager->pTmpSpace; |
| 1727 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
| 1728 | } |
| 1729 | if( rc==SQLITE_OK ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 1730 | rc = pager_end_transaction(pPager, zMaster[0]!='\0'); |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 1731 | } |
danielk1977 | c56774e | 2008-10-07 11:51:20 +0000 | [diff] [blame] | 1732 | if( rc==SQLITE_OK && zMaster[0] && res ){ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1733 | /* If there was a master journal and this routine will return success, |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1734 | ** see if it is possible to delete the master journal. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1735 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1736 | rc = pager_delmaster(pPager, zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1737 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1738 | |
| 1739 | /* The Pager.sectorSize variable may have been updated while rolling |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 1740 | ** back a journal created by a process with a different sector size |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1741 | ** value. Reset it to the correct value for this process. |
| 1742 | */ |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1743 | setSectorSize(pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1744 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | /* |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1748 | ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback |
| 1749 | ** the entire master journal file. |
| 1750 | ** |
| 1751 | ** The case pSavepoint==NULL occurs when a ROLLBACK TO command is invoked |
| 1752 | ** on a SAVEPOINT that is a transaction savepoint. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1753 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1754 | static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){ |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1755 | i64 szJ; /* Effective size of the main journal */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1756 | i64 iHdrOff; /* End of first segment of main-journal records */ |
| 1757 | Pgno ii; /* Loop counter */ |
danielk1977 | f2c31ad | 2009-01-06 13:40:08 +0000 | [diff] [blame] | 1758 | int rc = SQLITE_OK; /* Return code */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1759 | Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1760 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1761 | /* Allocate a bitvec to use to store the set of pages rolled back */ |
| 1762 | if( pSavepoint ){ |
| 1763 | pDone = sqlite3BitvecCreate(pSavepoint->nOrig); |
| 1764 | if( !pDone ){ |
| 1765 | return SQLITE_NOMEM; |
| 1766 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1767 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1768 | |
| 1769 | /* Truncate the database back to the size it was before the |
| 1770 | ** savepoint being reverted was opened. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1771 | */ |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1772 | pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize; |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 1773 | assert( pPager->state>=PAGER_SHARED ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1774 | |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1775 | /* Use pPager->journalOff as the effective size of the main rollback |
| 1776 | ** journal. The actual file might be larger than this in |
| 1777 | ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything |
| 1778 | ** past pPager->journalOff is off-limits to us. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1779 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1780 | szJ = pPager->journalOff; |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1781 | |
| 1782 | /* Begin by rolling back records from the main journal starting at |
| 1783 | ** PagerSavepoint.iOffset and continuing to the next journal header. |
| 1784 | ** There might be records in the main journal that have a page number |
| 1785 | ** greater than the current database size (pPager->dbSize) but those |
| 1786 | ** will be skipped automatically. Pages are added to pDone as they |
| 1787 | ** are played back. |
| 1788 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1789 | if( pSavepoint ){ |
| 1790 | iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ; |
| 1791 | pPager->journalOff = pSavepoint->iOffset; |
| 1792 | while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){ |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1793 | rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1794 | assert( rc!=SQLITE_DONE ); |
| 1795 | } |
| 1796 | }else{ |
| 1797 | pPager->journalOff = 0; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1798 | } |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1799 | |
| 1800 | /* Continue rolling back records out of the main journal starting at |
| 1801 | ** the first journal header seen and continuing until the effective end |
| 1802 | ** of the main journal file. Continue to skip out-of-range pages and |
| 1803 | ** continue adding pages rolled back to pDone. |
| 1804 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1805 | while( rc==SQLITE_OK && pPager->journalOff<szJ ){ |
danielk1977 | c81806f | 2009-01-01 15:20:37 +0000 | [diff] [blame] | 1806 | u32 nJRec = 0; /* Number of Journal Records */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1807 | u32 dummy; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1808 | rc = readJournalHdr(pPager, szJ, &nJRec, &dummy); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1809 | assert( rc!=SQLITE_DONE ); |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1810 | |
| 1811 | /* |
| 1812 | ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff" |
| 1813 | ** test is related to ticket #2565. See the discussion in the |
| 1814 | ** pager_playback() function for additional information. |
| 1815 | */ |
drh | ee03d62 | 2009-01-07 15:33:45 +0000 | [diff] [blame] | 1816 | assert( !(nJRec==0 |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1817 | && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)!=pPager->journalOff |
| 1818 | && ((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager))>0 |
drh | ee03d62 | 2009-01-07 15:33:45 +0000 | [diff] [blame] | 1819 | && pagerNextJournalPageIsValid(pPager)) |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1820 | ); |
| 1821 | if( nJRec==0 |
| 1822 | && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff |
| 1823 | ){ |
| 1824 | nJRec = (szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager); |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1825 | } |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 1826 | for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){ |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1827 | rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1828 | assert( rc!=SQLITE_DONE ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | assert( rc!=SQLITE_OK || pPager->journalOff==szJ ); |
| 1832 | |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1833 | /* Finally, rollback pages from the sub-journal. Page that were |
| 1834 | ** previously rolled back out of the main journal (and are hence in pDone) |
| 1835 | ** will be skipped. Out-of-range pages are also skipped. |
| 1836 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1837 | if( pSavepoint ){ |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1838 | i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize); |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame] | 1839 | for(ii=pSavepoint->iSubRec; rc==SQLITE_OK&&ii<(u32)pPager->stmtNRec; ii++){ |
drh | d6e5e09 | 2009-01-07 02:03:35 +0000 | [diff] [blame] | 1840 | assert( offset == ii*(4+pPager->pageSize) ); |
| 1841 | rc = pager_playback_one_page(pPager, 0, &offset, 1, pDone); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1842 | assert( rc!=SQLITE_DONE ); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1843 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1844 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1845 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1846 | sqlite3BitvecDestroy(pDone); |
| 1847 | if( rc==SQLITE_OK ){ |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1848 | pPager->journalOff = szJ; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1849 | } |
| 1850 | return rc; |
| 1851 | } |
| 1852 | |
| 1853 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1854 | ** Change the maximum number of in-memory pages that are allowed. |
| 1855 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1856 | void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1857 | sqlite3PcacheSetCachesize(pPager->pPCache, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1861 | ** Adjust the robustness of the database to damage due to OS crashes |
| 1862 | ** or power failures by changing the number of syncs()s when writing |
| 1863 | ** the rollback journal. There are three levels: |
| 1864 | ** |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1865 | ** OFF sqlite3OsSync() is never called. This is the default |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1866 | ** for temporary and transient files. |
| 1867 | ** |
| 1868 | ** NORMAL The journal is synced once before writes begin on the |
| 1869 | ** database. This is normally adequate protection, but |
| 1870 | ** it is theoretically possible, though very unlikely, |
| 1871 | ** that an inopertune power failure could leave the journal |
| 1872 | ** in a state which would cause damage to the database |
| 1873 | ** when it is rolled back. |
| 1874 | ** |
| 1875 | ** FULL The journal is synced twice before writes begin on the |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1876 | ** database (with some additional information - the nRec field |
| 1877 | ** of the journal header - being written in between the two |
| 1878 | ** syncs). If we assume that writing a |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1879 | ** single disk sector is atomic, then this mode provides |
| 1880 | ** assurance that the journal will not be corrupted to the |
| 1881 | ** point of causing damage to the database during rollback. |
| 1882 | ** |
| 1883 | ** Numeric values associated with these states are OFF==1, NORMAL=2, |
| 1884 | ** and FULL=3. |
| 1885 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1886 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1887 | void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1888 | pPager->noSync = (level==1 || pPager->tempFile) ?1:0; |
| 1889 | pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1890 | pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1891 | if( pPager->noSync ) pPager->needSync = 0; |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1892 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1893 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1894 | |
| 1895 | /* |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1896 | ** The following global variable is incremented whenever the library |
| 1897 | ** attempts to open a temporary file. This information is used for |
| 1898 | ** testing and analysis only. |
| 1899 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1900 | #ifdef SQLITE_TEST |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1901 | int sqlite3_opentemp_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1902 | #endif |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1903 | |
| 1904 | /* |
drh | 3f56e6e | 2007-03-15 01:16:47 +0000 | [diff] [blame] | 1905 | ** Open a temporary file. |
| 1906 | ** |
| 1907 | ** Write the file descriptor into *fd. Return SQLITE_OK on success or some |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1908 | ** other error code if we fail. The OS will automatically delete the temporary |
| 1909 | ** file when it is closed. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1910 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1911 | static int sqlite3PagerOpentemp( |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1912 | Pager *pPager, /* The pager object */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1913 | sqlite3_file *pFile, /* Write the file descriptor here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1914 | int vfsFlags /* Flags passed through to the VFS */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1915 | ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1916 | int rc; |
drh | 3f56e6e | 2007-03-15 01:16:47 +0000 | [diff] [blame] | 1917 | |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1918 | #ifdef SQLITE_TEST |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1919 | sqlite3_opentemp_count++; /* Used for testing and analysis only */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1920 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1921 | |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1922 | vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
| 1923 | SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1924 | rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0); |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 1925 | assert( rc!=SQLITE_OK || pFile->pMethods ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1926 | return rc; |
| 1927 | } |
| 1928 | |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 1929 | static int pagerStress(void *,PgHdr *); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1930 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1931 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1932 | ** 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] | 1933 | ** The file to be cached need not exist. The file is not locked until |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1934 | ** the first call to sqlite3PagerGet() and is only held open until the |
| 1935 | ** last page is released using sqlite3PagerUnref(). |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1936 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1937 | ** If zFilename is NULL then a randomly-named temporary file is created |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1938 | ** and used as the file to be cached. The file will be deleted |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1939 | ** automatically when it is closed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1940 | ** |
| 1941 | ** If zFilename is ":memory:" then all information is held in cache. |
| 1942 | ** It is never written to disk. This can be used to implement an |
| 1943 | ** in-memory database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1944 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1945 | int sqlite3PagerOpen( |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 1946 | sqlite3_vfs *pVfs, /* The virtual file system to use */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1947 | Pager **ppPager, /* Return the Pager structure here */ |
| 1948 | const char *zFilename, /* Name of the database file to open */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1949 | int nExtra, /* Extra bytes append to each in-memory page */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1950 | int flags, /* flags controlling this file */ |
| 1951 | int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1952 | ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1953 | u8 *pPtr; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1954 | Pager *pPager = 0; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1955 | int rc = SQLITE_OK; |
| 1956 | int i; |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 1957 | int tempFile = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1958 | int memDb = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1959 | int readOnly = 0; |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 1960 | int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; |
| 1961 | int noReadlock = (flags & PAGER_NO_READLOCK)!=0; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1962 | int journalFileSize; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1963 | int pcacheSize = sqlite3PcacheSize(); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 1964 | int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1965 | char *zPathname = 0; |
| 1966 | int nPathname = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1967 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1968 | if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){ |
| 1969 | journalFileSize = sqlite3JournalSize(pVfs); |
| 1970 | }else{ |
| 1971 | journalFileSize = sqlite3MemJournalSize(); |
| 1972 | } |
| 1973 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 1974 | /* The default return is a NULL pointer */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1975 | *ppPager = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1976 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1977 | /* Compute and store the full pathname in an allocated buffer pointed |
| 1978 | ** to by zPathname, length nPathname. Or, if this is a temporary file, |
| 1979 | ** leave both nPathname and zPathname set to 0. |
| 1980 | */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1981 | if( zFilename && zFilename[0] ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1982 | nPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1983 | zPathname = sqlite3Malloc(nPathname*2); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1984 | if( zPathname==0 ){ |
| 1985 | return SQLITE_NOMEM; |
| 1986 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1987 | #ifndef SQLITE_OMIT_MEMORYDB |
| 1988 | if( strcmp(zFilename,":memory:")==0 ){ |
| 1989 | memDb = 1; |
| 1990 | zPathname[0] = 0; |
| 1991 | }else |
| 1992 | #endif |
| 1993 | { |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1994 | rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1995 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1996 | if( rc!=SQLITE_OK ){ |
| 1997 | sqlite3_free(zPathname); |
| 1998 | return rc; |
| 1999 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2000 | nPathname = sqlite3Strlen30(zPathname); |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2001 | } |
drh | 99b90c3 | 2008-03-17 13:50:58 +0000 | [diff] [blame] | 2002 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2003 | /* Allocate memory for the pager structure */ |
| 2004 | pPager = sqlite3MallocZero( |
| 2005 | sizeof(*pPager) + /* Pager structure */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2006 | pcacheSize + /* PCache object */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 2007 | journalFileSize + /* The journal file structure */ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2008 | pVfs->szOsFile + /* The main db file */ |
| 2009 | journalFileSize * 2 + /* The two journal files */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2010 | 3*nPathname + 40 /* zFilename, zDirectory, zJournal */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2011 | ); |
| 2012 | if( !pPager ){ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2013 | sqlite3_free(zPathname); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2014 | return SQLITE_NOMEM; |
| 2015 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2016 | pPager->pPCache = (PCache *)&pPager[1]; |
| 2017 | pPtr = ((u8 *)&pPager[1]) + pcacheSize; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2018 | pPager->vfsFlags = vfsFlags; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2019 | pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0]; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2020 | pPager->sjfd = (sqlite3_file*)&pPtr[pVfs->szOsFile]; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2021 | pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile+journalFileSize]; |
| 2022 | pPager->zFilename = (char*)&pPtr[pVfs->szOsFile+2*journalFileSize]; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2023 | pPager->zDirectory = &pPager->zFilename[nPathname+1]; |
| 2024 | pPager->zJournal = &pPager->zDirectory[nPathname+1]; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2025 | pPager->pVfs = pVfs; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2026 | if( zPathname ){ |
| 2027 | memcpy(pPager->zFilename, zPathname, nPathname+1); |
| 2028 | sqlite3_free(zPathname); |
| 2029 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2030 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2031 | /* Open the pager file. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2032 | */ |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2033 | if( zFilename && zFilename[0] && !memDb ){ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 2034 | if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2035 | rc = SQLITE_CANTOPEN; |
| 2036 | }else{ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2037 | int fout = 0; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2038 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, |
| 2039 | pPager->vfsFlags, &fout); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2040 | readOnly = (fout&SQLITE_OPEN_READONLY); |
| 2041 | |
| 2042 | /* If the file was successfully opened for read/write access, |
| 2043 | ** choose a default page size in case we have to create the |
| 2044 | ** database file. The default page size is the maximum of: |
| 2045 | ** |
| 2046 | ** + SQLITE_DEFAULT_PAGE_SIZE, |
| 2047 | ** + The value returned by sqlite3OsSectorSize() |
| 2048 | ** + The largest page size that can be written atomically. |
| 2049 | */ |
| 2050 | if( rc==SQLITE_OK && !readOnly ){ |
danielk1977 | 7cbd589 | 2009-01-10 16:15:09 +0000 | [diff] [blame] | 2051 | setSectorSize(pPager); |
| 2052 | if( szPageDflt<pPager->sectorSize ){ |
| 2053 | szPageDflt = pPager->sectorSize; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2054 | } |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2055 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2056 | { |
| 2057 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| 2058 | int ii; |
| 2059 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 2060 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 2061 | assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 2062 | for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){ |
| 2063 | if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii; |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2064 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2065 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2066 | #endif |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 2067 | if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ |
| 2068 | szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2069 | } |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 2070 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2071 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2072 | }else{ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2073 | /* If a temporary file is requested, it is not opened immediately. |
| 2074 | ** In this case we accept the default page size and delay actually |
| 2075 | ** opening the file until the first call to OsWrite(). |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2076 | ** |
| 2077 | ** This branch is also run for an in-memory database. An in-memory |
| 2078 | ** database is the same as a temp-file that is never written out to |
| 2079 | ** disk and uses an in-memory rollback journal. |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2080 | */ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2081 | tempFile = 1; |
| 2082 | pPager->state = PAGER_EXCLUSIVE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2083 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2084 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2085 | if( pPager && rc==SQLITE_OK ){ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 2086 | pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt); |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 2087 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2088 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2089 | /* If an error occured in either of the blocks above. |
| 2090 | ** Free the Pager structure and close the file. |
| 2091 | ** Since the pager is not allocated there is no need to set |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2092 | ** any Pager.errMask variables. |
| 2093 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2094 | if( !pPager || !pPager->pTmpSpace ){ |
| 2095 | sqlite3OsClose(pPager->fd); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2096 | sqlite3_free(pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2097 | return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2098 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2099 | nExtra = FORCE_ALIGNMENT(nExtra); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2100 | sqlite3PcacheOpen(szPageDflt, nExtra, !memDb, |
drh | 41d3027 | 2008-08-20 21:47:45 +0000 | [diff] [blame] | 2101 | !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2102 | |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 2103 | PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename)); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2104 | IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename)) |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2105 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2106 | /* Fill in Pager.zDirectory[] */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2107 | memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2108 | for(i=sqlite3Strlen30(pPager->zDirectory); |
| 2109 | i>0 && pPager->zDirectory[i-1]!='/'; i--){} |
drh | a76c82e | 2003-07-27 18:59:42 +0000 | [diff] [blame] | 2110 | if( i>0 ) pPager->zDirectory[i-1] = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2111 | |
drh | 99b90c3 | 2008-03-17 13:50:58 +0000 | [diff] [blame] | 2112 | /* Fill in Pager.zJournal[] */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2113 | if( zPathname ){ |
| 2114 | memcpy(pPager->zJournal, pPager->zFilename, nPathname); |
| 2115 | memcpy(&pPager->zJournal[nPathname], "-journal", 9); |
| 2116 | }else{ |
| 2117 | pPager->zJournal = 0; |
| 2118 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2119 | |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2120 | /* pPager->journalOpen = 0; */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2121 | pPager->useJournal = (u8)useJournal; |
| 2122 | pPager->noReadlock = (noReadlock && readOnly) ?1:0; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2123 | /* pPager->stmtOpen = 0; */ |
| 2124 | /* pPager->stmtInUse = 0; */ |
| 2125 | /* pPager->nRef = 0; */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2126 | pPager->dbSizeValid = (u8)memDb; |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 2127 | pPager->pageSize = szPageDflt; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2128 | /* pPager->stmtSize = 0; */ |
| 2129 | /* pPager->stmtJSize = 0; */ |
| 2130 | /* pPager->nPage = 0; */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2131 | pPager->mxPage = 100; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2132 | pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2133 | /* pPager->state = PAGER_UNLOCK; */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2134 | assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) ); |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2135 | /* pPager->errMask = 0; */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2136 | pPager->tempFile = (u8)tempFile; |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 2137 | assert( tempFile==PAGER_LOCKINGMODE_NORMAL |
| 2138 | || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 2139 | assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2140 | pPager->exclusiveMode = (u8)tempFile; |
| 2141 | pPager->memDb = (u8)memDb; |
| 2142 | pPager->readOnly = (u8)readOnly; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2143 | /* pPager->needSync = 0; */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2144 | pPager->noSync = (pPager->tempFile || !useJournal) ?1:0; |
| 2145 | pPager->fullSync = pPager->noSync ?0:1; |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2146 | pPager->sync_flags = SQLITE_SYNC_NORMAL; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2147 | /* pPager->pFirst = 0; */ |
| 2148 | /* pPager->pFirstSynced = 0; */ |
| 2149 | /* pPager->pLast = 0; */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2150 | pPager->nExtra = nExtra; |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 2151 | pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2152 | assert(pPager->fd->pMethods||tempFile); |
| 2153 | setSectorSize(pPager); |
| 2154 | if( memDb ){ |
| 2155 | pPager->journalMode = PAGER_JOURNALMODE_MEMORY; |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 2156 | } |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2157 | /* pPager->xBusyHandler = 0; */ |
| 2158 | /* pPager->pBusyHandlerArg = 0; */ |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2159 | /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2160 | *ppPager = pPager; |
| 2161 | return SQLITE_OK; |
| 2162 | } |
| 2163 | |
| 2164 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2165 | ** Set the busy handler function. |
| 2166 | */ |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2167 | void sqlite3PagerSetBusyhandler( |
| 2168 | Pager *pPager, |
| 2169 | int (*xBusyHandler)(void *), |
| 2170 | void *pBusyHandlerArg |
| 2171 | ){ |
| 2172 | pPager->xBusyHandler = xBusyHandler; |
| 2173 | pPager->pBusyHandlerArg = pBusyHandlerArg; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2177 | ** Set the reinitializer for this pager. If not NULL, the reinitializer |
| 2178 | ** is called when the content of a page in cache is restored to its original |
| 2179 | ** value as a result of a rollback. The callback gives higher-level code |
| 2180 | ** an opportunity to restore the EXTRA section to agree with the restored |
| 2181 | ** page data. |
| 2182 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 2183 | void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2184 | pPager->xReiniter = xReinit; |
| 2185 | } |
| 2186 | |
| 2187 | /* |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2188 | ** Set the page size to *pPageSize. If the suggest new page size is |
| 2189 | ** inappropriate, then an alternative page size is set to that |
| 2190 | ** value before returning. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2191 | */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2192 | int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){ |
danielk1977 | 1357d9f | 2008-09-16 05:12:24 +0000 | [diff] [blame] | 2193 | int rc = pPager->errCode; |
| 2194 | if( rc==SQLITE_OK ){ |
| 2195 | u16 pageSize = *pPageSize; |
| 2196 | assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) ); |
| 2197 | if( pageSize && pageSize!=pPager->pageSize |
| 2198 | && (pPager->memDb==0 || pPager->dbSize==0) |
| 2199 | && sqlite3PcacheRefCount(pPager->pPCache)==0 |
| 2200 | ){ |
| 2201 | char *pNew = (char *)sqlite3PageMalloc(pageSize); |
| 2202 | if( !pNew ){ |
| 2203 | rc = SQLITE_NOMEM; |
| 2204 | }else{ |
| 2205 | pager_reset(pPager); |
| 2206 | pPager->pageSize = pageSize; |
| 2207 | if( !pPager->memDb ) setSectorSize(pPager); |
| 2208 | sqlite3PageFree(pPager->pTmpSpace); |
| 2209 | pPager->pTmpSpace = pNew; |
| 2210 | sqlite3PcacheSetPageSize(pPager->pPCache, pageSize); |
| 2211 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2212 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2213 | *pPageSize = (u16)pPager->pageSize; |
drh | 1c7880e | 2005-05-20 20:01:55 +0000 | [diff] [blame] | 2214 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2215 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | /* |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 2219 | ** Return a pointer to the "temporary page" buffer held internally |
| 2220 | ** by the pager. This is a buffer that is big enough to hold the |
| 2221 | ** entire content of a database page. This buffer is used internally |
| 2222 | ** during rollback and will be overwritten whenever a rollback |
| 2223 | ** occurs. But other modules are free to use it too, as long as |
| 2224 | ** no rollbacks are happening. |
| 2225 | */ |
| 2226 | void *sqlite3PagerTempSpace(Pager *pPager){ |
| 2227 | return pPager->pTmpSpace; |
| 2228 | } |
| 2229 | |
| 2230 | /* |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2231 | ** Attempt to set the maximum database page count if mxPage is positive. |
| 2232 | ** Make no changes if mxPage is zero or negative. And never reduce the |
| 2233 | ** maximum page count below the current size of the database. |
| 2234 | ** |
| 2235 | ** Regardless of mxPage, return the current maximum page count. |
| 2236 | */ |
| 2237 | int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ |
| 2238 | if( mxPage>0 ){ |
| 2239 | pPager->mxPgno = mxPage; |
| 2240 | } |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2241 | sqlite3PagerPagecount(pPager, 0); |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2242 | return pPager->mxPgno; |
| 2243 | } |
| 2244 | |
| 2245 | /* |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2246 | ** The following set of routines are used to disable the simulated |
| 2247 | ** I/O error mechanism. These routines are used to avoid simulated |
| 2248 | ** errors in places where we do not care about errors. |
| 2249 | ** |
| 2250 | ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops |
| 2251 | ** and generate no code. |
| 2252 | */ |
| 2253 | #ifdef SQLITE_TEST |
| 2254 | extern int sqlite3_io_error_pending; |
| 2255 | extern int sqlite3_io_error_hit; |
| 2256 | static int saved_cnt; |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2257 | void disable_simulated_io_errors(void){ |
| 2258 | saved_cnt = sqlite3_io_error_pending; |
| 2259 | sqlite3_io_error_pending = -1; |
| 2260 | } |
| 2261 | void enable_simulated_io_errors(void){ |
| 2262 | sqlite3_io_error_pending = saved_cnt; |
| 2263 | } |
| 2264 | #else |
drh | 152410f | 2005-11-05 15:11:22 +0000 | [diff] [blame] | 2265 | # define disable_simulated_io_errors() |
| 2266 | # define enable_simulated_io_errors() |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2267 | #endif |
| 2268 | |
| 2269 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2270 | ** Read the first N bytes from the beginning of the file into memory |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2271 | ** that pDest points to. |
| 2272 | ** |
| 2273 | ** No error checking is done. The rational for this is that this function |
| 2274 | ** may be called even if the file does not exist or contain a header. In |
| 2275 | ** these cases sqlite3OsRead() will return an error, to which the correct |
| 2276 | ** response is to zero the memory at pDest and continue. A real IO error |
| 2277 | ** will presumably recur and be picked up later (Todo: Think about this). |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2278 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2279 | int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){ |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2280 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2281 | memset(pDest, 0, N); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2282 | assert(pPager->fd->pMethods||pPager->tempFile); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2283 | if( pPager->fd->pMethods ){ |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2284 | IOTRACE(("DBHDR %p 0 %d\n", pPager, N)) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2285 | rc = sqlite3OsRead(pPager->fd, pDest, N, 0); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2286 | if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 2287 | rc = SQLITE_OK; |
| 2288 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2289 | } |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2290 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2294 | ** Return the total number of pages in the disk file associated with |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2295 | ** pPager. |
| 2296 | ** |
| 2297 | ** If the PENDING_BYTE lies on the page directly after the end of the |
| 2298 | ** file, then consider this page part of the file too. For example, if |
| 2299 | ** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the |
| 2300 | ** file is 4096 bytes, 5 is returned instead of 4. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2301 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2302 | int sqlite3PagerPagecount(Pager *pPager, int *pnPage){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2303 | i64 n = 0; |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 2304 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2305 | assert( pPager!=0 ); |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 2306 | if( pPager->errCode ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2307 | rc = pPager->errCode; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2308 | return rc; |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 2309 | } |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 2310 | if( pPager->dbSizeValid ){ |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2311 | n = pPager->dbSize; |
| 2312 | } else { |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2313 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 2314 | if( (pPager->fd->pMethods) |
| 2315 | && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){ |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 2316 | pager_error(pPager, rc); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2317 | return rc; |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2318 | } |
| 2319 | if( n>0 && n<pPager->pageSize ){ |
| 2320 | n = 1; |
| 2321 | }else{ |
| 2322 | n /= pPager->pageSize; |
| 2323 | } |
| 2324 | if( pPager->state!=PAGER_UNLOCK ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2325 | pPager->dbSize = (Pgno)n; |
| 2326 | pPager->dbFileSize = (Pgno)n; |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 2327 | pPager->dbSizeValid = 1; |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2328 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2329 | } |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2330 | if( n==(PENDING_BYTE/pPager->pageSize) ){ |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 2331 | n++; |
| 2332 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2333 | if( n>pPager->mxPgno ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2334 | pPager->mxPgno = (Pgno)n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2335 | } |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2336 | if( pnPage ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2337 | *pnPage = (int)n; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2338 | } |
| 2339 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2343 | ** Forward declaration |
| 2344 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2345 | static int syncJournal(Pager*); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2346 | |
| 2347 | /* |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2348 | ** Try to obtain a lock on a file. Invoke the busy callback if the lock |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 2349 | ** is currently not available. Repeat until the busy callback returns |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2350 | ** false or until the lock succeeds. |
| 2351 | ** |
| 2352 | ** Return SQLITE_OK on success and an error code if we cannot obtain |
| 2353 | ** the lock. |
| 2354 | */ |
| 2355 | static int pager_wait_on_lock(Pager *pPager, int locktype){ |
| 2356 | int rc; |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2357 | |
| 2358 | /* The OS lock values must be the same as the Pager lock values */ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2359 | assert( PAGER_SHARED==SHARED_LOCK ); |
| 2360 | assert( PAGER_RESERVED==RESERVED_LOCK ); |
| 2361 | assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2362 | |
| 2363 | /* If the file is currently unlocked then the size must be unknown */ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 2364 | assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2365 | |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2366 | if( pPager->state>=locktype ){ |
| 2367 | rc = SQLITE_OK; |
| 2368 | }else{ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2369 | do { |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2370 | rc = sqlite3OsLock(pPager->fd, locktype); |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2371 | }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) ); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2372 | if( rc==SQLITE_OK ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2373 | pPager->state = (u8)locktype; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2374 | IOTRACE(("LOCK %p %d\n", pPager, locktype)) |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
| 2377 | return rc; |
| 2378 | } |
| 2379 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2380 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2381 | /* |
danielk1977 | f90b726 | 2009-01-07 15:18:20 +0000 | [diff] [blame] | 2382 | ** Truncate the in-memory database file image to nPage pages. This |
| 2383 | ** function does not actually modify the database file on disk. It |
| 2384 | ** just sets the internal state of the pager object so that the |
| 2385 | ** truncation will be done when the current transaction is committed. |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2386 | */ |
| 2387 | void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ |
| 2388 | assert( pPager->dbSizeValid ); |
| 2389 | assert( pPager->dbSize>=nPage ); |
| 2390 | pPager->dbSize = nPage; |
| 2391 | } |
| 2392 | |
| 2393 | /* |
| 2394 | ** Return the current size of the database file image in pages. This |
| 2395 | ** function differs from sqlite3PagerPagecount() in two ways: |
| 2396 | ** |
| 2397 | ** a) It may only be called when at least one reference to a database |
| 2398 | ** page is held. This guarantees that the database size is already |
| 2399 | ** known and a call to sqlite3OsFileSize() is not required. |
| 2400 | ** |
| 2401 | ** b) The return value is not adjusted for the locking page. |
| 2402 | */ |
| 2403 | Pgno sqlite3PagerImageSize(Pager *pPager){ |
| 2404 | assert( pPager->dbSizeValid ); |
| 2405 | return pPager->dbSize; |
| 2406 | } |
| 2407 | #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
| 2408 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2409 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2410 | ** Shutdown the page cache. Free all memory and close all files. |
| 2411 | ** |
| 2412 | ** If a transaction was in progress when this routine is called, that |
| 2413 | ** transaction is rolled back. All outstanding pages are invalidated |
| 2414 | ** and their memory is freed. Any attempt to use a page associated |
| 2415 | ** with this page cache after this function returns will likely |
| 2416 | ** result in a coredump. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2417 | ** |
| 2418 | ** This function always succeeds. If a transaction is active an attempt |
| 2419 | ** is made to roll it back. If an error occurs during the rollback |
| 2420 | ** a hot journal may be left in the filesystem but no error is returned |
| 2421 | ** to the caller. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2422 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2423 | int sqlite3PagerClose(Pager *pPager){ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 2424 | |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2425 | disable_simulated_io_errors(); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 2426 | sqlite3BeginBenignMalloc(); |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 2427 | pPager->errCode = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 2428 | pPager->exclusiveMode = 0; |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2429 | pager_reset(pPager); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2430 | if( !MEMDB ){ |
danielk1977 | f2c31ad | 2009-01-06 13:40:08 +0000 | [diff] [blame] | 2431 | /* Set Pager.journalHdr to -1 for the benefit of the pager_playback() |
| 2432 | ** call which may be made from within pagerUnlockAndRollback(). If it |
| 2433 | ** is not -1, then the unsynced portion of an open journal file may |
| 2434 | ** be played back into the database. If a power failure occurs while |
| 2435 | ** this is happening, the database may become corrupt. |
| 2436 | */ |
| 2437 | pPager->journalHdr = -1; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2438 | pagerUnlockAndRollback(pPager); |
| 2439 | } |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2440 | enable_simulated_io_errors(); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 2441 | sqlite3EndBenignMalloc(); |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 2442 | PAGERTRACE(("CLOSE %d\n", PAGERID(pPager))); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2443 | IOTRACE(("CLOSE %p\n", pPager)) |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2444 | if( pPager->journalOpen ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2445 | sqlite3OsClose(pPager->jfd); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2446 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 2447 | sqlite3BitvecDestroy(pPager->pInJournal); |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 2448 | sqlite3BitvecDestroy(pPager->pAlwaysRollback); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2449 | releaseAllSavepoint(pPager); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2450 | sqlite3OsClose(pPager->fd); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 2451 | /* Temp files are automatically deleted by the OS |
| 2452 | ** if( pPager->tempFile ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 2453 | ** sqlite3OsDelete(pPager->zFilename); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 2454 | ** } |
| 2455 | */ |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 2456 | |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 2457 | sqlite3PageFree(pPager->pTmpSpace); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2458 | sqlite3PcacheClose(pPager->pPCache); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2459 | sqlite3_free(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2460 | return SQLITE_OK; |
| 2461 | } |
| 2462 | |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 2463 | #if !defined(NDEBUG) || defined(SQLITE_TEST) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2464 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2465 | ** Return the page number for the given page data. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2466 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2467 | Pgno sqlite3PagerPagenumber(DbPage *p){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2468 | return p->pgno; |
| 2469 | } |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 2470 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2471 | |
| 2472 | /* |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2473 | ** Increment the reference count for a page. The input pointer is |
| 2474 | ** a reference to the page data. |
| 2475 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2476 | int sqlite3PagerRef(DbPage *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2477 | sqlite3PcacheRef(pPg); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2478 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2482 | ** Sync the journal. In other words, make sure all the pages that have |
| 2483 | ** been written to the journal have actually reached the surface of the |
| 2484 | ** disk. It is not safe to modify the original database file until after |
| 2485 | ** the journal has been synced. If the original database is modified before |
| 2486 | ** the journal is synced and a power failure occurs, the unsynced journal |
| 2487 | ** data would be lost and we would be unable to completely rollback the |
| 2488 | ** database changes. Database corruption would occur. |
| 2489 | ** |
| 2490 | ** This routine also updates the nRec field in the header of the journal. |
| 2491 | ** (See comments on the pager_playback() routine for additional information.) |
| 2492 | ** If the sync mode is FULL, two syncs will occur. First the whole journal |
| 2493 | ** is synced, then the nRec field is updated, then a second sync occurs. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2494 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2495 | ** For temporary databases, we do not care if we are able to rollback |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2496 | ** after a power failure, so no sync occurs. |
| 2497 | ** |
| 2498 | ** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which |
| 2499 | ** the database is stored, then OsSync() is never called on the journal |
| 2500 | ** file. In this case all that is required is to update the nRec field in |
| 2501 | ** the journal header. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2502 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2503 | ** This routine clears the needSync field of every page current held in |
| 2504 | ** memory. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2505 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2506 | static int syncJournal(Pager *pPager){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2507 | int rc = SQLITE_OK; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2508 | |
| 2509 | /* Sync the journal before modifying the main database |
| 2510 | ** (assuming there is a journal and it needs to be synced.) |
| 2511 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2512 | if( pPager->needSync ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2513 | assert( !pPager->tempFile ); |
| 2514 | if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2515 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2516 | assert( pPager->journalOpen ); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2517 | |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2518 | if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
danielk1977 | 112f752 | 2009-01-08 17:50:45 +0000 | [diff] [blame] | 2519 | i64 jrnlOff = journalHdrOffset(pPager); |
| 2520 | u8 zMagic[8]; |
| 2521 | |
| 2522 | /* This block deals with an obscure problem. If the last connection |
| 2523 | ** that wrote to this database was operating in persistent-journal |
| 2524 | ** mode, then the journal file may at this point actually be larger |
| 2525 | ** than Pager.journalOff bytes. If the next thing in the journal |
| 2526 | ** file happens to be a journal-header (written as part of the |
| 2527 | ** previous connections transaction), and a crash or power-failure |
| 2528 | ** occurs after nRec is updated but before this connection writes |
| 2529 | ** anything else to the journal file (or commits/rolls back its |
| 2530 | ** transaction), then SQLite may become confused when doing the |
| 2531 | ** hot-journal rollback following recovery. It may roll back all |
| 2532 | ** of this connections data, then proceed to rolling back the old, |
| 2533 | ** out-of-date data that follows it. Database corruption. |
| 2534 | ** |
| 2535 | ** To work around this, if the journal file does appear to contain |
| 2536 | ** a valid header following Pager.journalOff, then write a 0x00 |
| 2537 | ** byte to the start of it to prevent it from being recognized. |
| 2538 | */ |
| 2539 | rc = sqlite3OsRead(pPager->jfd, zMagic, 8, jrnlOff); |
| 2540 | if( rc==SQLITE_OK && 0==memcmp(zMagic, aJournalMagic, 8) ){ |
| 2541 | static const u8 zerobyte = 0; |
| 2542 | rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, jrnlOff); |
| 2543 | } |
| 2544 | if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ |
| 2545 | return rc; |
| 2546 | } |
| 2547 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2548 | /* Write the nRec value into the journal file header. If in |
| 2549 | ** full-synchronous mode, sync the journal first. This ensures that |
| 2550 | ** all data has really hit the disk before nRec is updated to mark |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2551 | ** it as a candidate for rollback. |
| 2552 | ** |
| 2553 | ** This is not required if the persistent media supports the |
| 2554 | ** SAFE_APPEND property. Because in this case it is not possible |
| 2555 | ** for garbage data to be appended to the file, the nRec field |
| 2556 | ** is populated with 0xFFFFFFFF when the journal header is written |
| 2557 | ** and never needs to be updated. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2558 | */ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2559 | if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 2560 | PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager))); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2561 | IOTRACE(("JSYNC %p\n", pPager)) |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2562 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags); |
drh | d8d66e8 | 2003-02-12 02:10:15 +0000 | [diff] [blame] | 2563 | if( rc!=0 ) return rc; |
| 2564 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2565 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2566 | jrnlOff = pPager->journalHdr + sizeof(aJournalMagic); |
| 2567 | IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4)); |
| 2568 | rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec); |
drh | b4746b9 | 2005-09-09 01:32:06 +0000 | [diff] [blame] | 2569 | if( rc ) return rc; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2570 | } |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2571 | if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 2572 | PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager))); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2573 | IOTRACE(("JSYNC %p\n", pPager)) |
| 2574 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| |
| 2575 | (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) |
| 2576 | ); |
| 2577 | if( rc!=0 ) return rc; |
| 2578 | } |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2579 | pPager->journalStarted = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2580 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2581 | pPager->needSync = 0; |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2582 | |
| 2583 | /* Erase the needSync flag from every page. |
| 2584 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 2585 | sqlite3PcacheClearSyncFlags(pPager->pPCache); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2586 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2587 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2588 | return rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | /* |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2592 | ** Given a list of pages (connected by the PgHdr.pDirty pointer) write |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2593 | ** every one of those pages out to the database file. No calls are made |
| 2594 | ** to the page-cache to mark the pages as clean. It is the responsibility |
| 2595 | ** of the caller to use PcacheCleanAll() or PcacheMakeClean() to mark |
| 2596 | ** the pages as clean. |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2597 | */ |
| 2598 | static int pager_write_pagelist(PgHdr *pList){ |
| 2599 | Pager *pPager; |
| 2600 | int rc; |
| 2601 | |
| 2602 | if( pList==0 ) return SQLITE_OK; |
| 2603 | pPager = pList->pPager; |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2604 | |
| 2605 | /* At this point there may be either a RESERVED or EXCLUSIVE lock on the |
| 2606 | ** database file. If there is already an EXCLUSIVE lock, the following |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2607 | ** calls to sqlite3OsLock() are no-ops. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2608 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2609 | ** Moving the lock from RESERVED to EXCLUSIVE actually involves going |
| 2610 | ** through an intermediate state PENDING. A PENDING lock prevents new |
| 2611 | ** readers from attaching to the database but is unsufficient for us to |
| 2612 | ** write. The idea of a PENDING lock is to prevent new readers from |
| 2613 | ** coming in while we wait for existing readers to clear. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2614 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2615 | ** While the pager is in the RESERVED state, the original database file |
| 2616 | ** is unchanged and we can rollback without having to playback the |
| 2617 | ** journal into the original database file. Once we transition to |
| 2618 | ** EXCLUSIVE, it means the database file has been changed and any rollback |
| 2619 | ** will require a journal playback. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2620 | */ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2621 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2622 | if( rc!=SQLITE_OK ){ |
| 2623 | return rc; |
| 2624 | } |
| 2625 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2626 | while( pList ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2627 | |
| 2628 | /* If the file has not yet been opened, open it now. */ |
| 2629 | if( !pPager->fd->pMethods ){ |
| 2630 | assert(pPager->tempFile); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2631 | rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2632 | if( rc ) return rc; |
| 2633 | } |
| 2634 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2635 | /* If there are dirty pages in the page cache with page numbers greater |
danielk1977 | f90b726 | 2009-01-07 15:18:20 +0000 | [diff] [blame] | 2636 | ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2637 | ** make the file smaller (presumably by auto-vacuum code). Do not write |
| 2638 | ** any such pages to the file. |
| 2639 | */ |
danielk1977 | 33e3216 | 2008-08-23 18:53:08 +0000 | [diff] [blame] | 2640 | if( pList->pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2641 | i64 offset = (pList->pgno-1)*(i64)pPager->pageSize; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2642 | char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6); |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2643 | |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 2644 | PAGERTRACE(("STORE %d page %d hash(%08x)\n", |
| 2645 | PAGERID(pPager), pList->pgno, pager_pagehash(pList))); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2646 | IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno)); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2647 | rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2648 | PAGER_INCR(sqlite3_pager_writedb_count); |
| 2649 | PAGER_INCR(pPager->nWrite); |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 2650 | if( pList->pgno==1 ){ |
| 2651 | memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); |
| 2652 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2653 | if( pList->pgno>pPager->dbFileSize ){ |
| 2654 | pPager->dbFileSize = pList->pgno; |
| 2655 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2656 | } |
| 2657 | #ifndef NDEBUG |
| 2658 | else{ |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 2659 | PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno)); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2660 | } |
| 2661 | #endif |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2662 | if( rc ) return rc; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 2663 | #ifdef SQLITE_CHECK_PAGES |
| 2664 | pList->pageHash = pager_pagehash(pList); |
| 2665 | #endif |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2666 | pList = pList->pDirty; |
| 2667 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2668 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2669 | return SQLITE_OK; |
| 2670 | } |
| 2671 | |
| 2672 | /* |
danielk1977 | f2c31ad | 2009-01-06 13:40:08 +0000 | [diff] [blame] | 2673 | ** Add the page to the sub-journal. It is the callers responsibility to |
| 2674 | ** use subjRequiresPage() to check that it is really required before |
| 2675 | ** calling this function. |
| 2676 | */ |
| 2677 | static int subjournalPage(PgHdr *pPg){ |
| 2678 | int rc; |
| 2679 | void *pData = pPg->pData; |
| 2680 | Pager *pPager = pPg->pPager; |
| 2681 | i64 offset = pPager->stmtNRec*(4+pPager->pageSize); |
| 2682 | char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7); |
| 2683 | |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 2684 | PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno)); |
danielk1977 | f2c31ad | 2009-01-06 13:40:08 +0000 | [diff] [blame] | 2685 | |
| 2686 | assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); |
| 2687 | rc = write32bits(pPager->sjfd, offset, pPg->pgno); |
| 2688 | if( rc==SQLITE_OK ){ |
| 2689 | rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4); |
| 2690 | } |
| 2691 | if( rc==SQLITE_OK ){ |
| 2692 | pPager->stmtNRec++; |
| 2693 | assert( pPager->nSavepoint>0 ); |
| 2694 | rc = addToSavepointBitvecs(pPager, pPg->pgno); |
| 2695 | } |
| 2696 | return rc; |
| 2697 | } |
| 2698 | |
| 2699 | |
| 2700 | /* |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2701 | ** This function is called by the pcache layer when it has reached some |
| 2702 | ** soft memory limit. The argument is a pointer to a purgeable Pager |
| 2703 | ** object. This function attempts to make a single dirty page that has no |
| 2704 | ** outstanding references (if one exists) clean so that it can be recycled |
| 2705 | ** by the pcache layer. |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2706 | */ |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2707 | static int pagerStress(void *p, PgHdr *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2708 | Pager *pPager = (Pager *)p; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2709 | int rc = SQLITE_OK; |
drh | 8f2e9a1 | 2008-02-26 14:46:04 +0000 | [diff] [blame] | 2710 | |
danielk1977 | 8c20014 | 2008-08-25 07:12:28 +0000 | [diff] [blame] | 2711 | if( pPager->doNotSync ){ |
| 2712 | return SQLITE_OK; |
| 2713 | } |
| 2714 | |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2715 | assert( pPg->flags&PGHDR_DIRTY ); |
| 2716 | if( pPager->errCode==SQLITE_OK ){ |
| 2717 | if( pPg->flags&PGHDR_NEED_SYNC ){ |
| 2718 | rc = syncJournal(pPager); |
| 2719 | if( rc==SQLITE_OK && pPager->fullSync && |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2720 | !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) && |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2721 | !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) |
| 2722 | ){ |
| 2723 | pPager->nRec = 0; |
| 2724 | rc = writeJournalHdr(pPager); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2725 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2726 | } |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2727 | if( rc==SQLITE_OK ){ |
| 2728 | pPg->pDirty = 0; |
danielk1977 | f2c31ad | 2009-01-06 13:40:08 +0000 | [diff] [blame] | 2729 | if( pPg->pgno>pPager->dbSize && subjRequiresPage(pPg) ){ |
| 2730 | rc = subjournalPage(pPg); |
| 2731 | } |
| 2732 | if( rc==SQLITE_OK ){ |
| 2733 | rc = pager_write_pagelist(pPg); |
| 2734 | } |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2735 | } |
| 2736 | if( rc!=SQLITE_OK ){ |
| 2737 | pager_error(pPager, rc); |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | if( rc==SQLITE_OK ){ |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 2742 | PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno)); |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2743 | sqlite3PcacheMakeClean(pPg); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2744 | } |
| 2745 | return rc; |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2748 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2749 | /* |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 2750 | ** Return 1 if there is a hot journal on the given pager. |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 2751 | ** A hot journal is one that needs to be played back. |
| 2752 | ** |
| 2753 | ** If the current size of the database file is 0 but a journal file |
| 2754 | ** exists, that is probably an old journal left over from a prior |
| 2755 | ** database with the same name. Just delete the journal. |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 2756 | ** |
| 2757 | ** Return negative if unable to determine the status of the journal. |
drh | 82ed1e5 | 2008-04-25 12:25:42 +0000 | [diff] [blame] | 2758 | ** |
| 2759 | ** This routine does not open the journal file to examine its |
| 2760 | ** content. Hence, the journal might contain the name of a master |
| 2761 | ** journal file that has been deleted, and hence not be hot. Or |
| 2762 | ** the header of the journal might be zeroed out. This routine |
| 2763 | ** does not discover these cases of a non-hot journal - if the |
| 2764 | ** journal file exists and is not empty this routine assumes it |
| 2765 | ** is hot. The pager_playback() routine will discover that the |
| 2766 | ** journal file is not really hot and will no-op. |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 2767 | */ |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2768 | static int hasHotJournal(Pager *pPager, int *pExists){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2769 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2770 | int rc = SQLITE_OK; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2771 | int exists = 0; |
| 2772 | int locked = 0; |
drh | 0a846f9 | 2008-08-25 17:23:29 +0000 | [diff] [blame] | 2773 | assert( pPager!=0 ); |
| 2774 | assert( pPager->useJournal ); |
| 2775 | assert( pPager->fd->pMethods ); |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2776 | *pExists = 0; |
drh | 0a846f9 | 2008-08-25 17:23:29 +0000 | [diff] [blame] | 2777 | rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 2778 | if( rc==SQLITE_OK && exists ){ |
| 2779 | rc = sqlite3OsCheckReservedLock(pPager->fd, &locked); |
| 2780 | } |
| 2781 | if( rc==SQLITE_OK && exists && !locked ){ |
| 2782 | int nPage; |
| 2783 | rc = sqlite3PagerPagecount(pPager, &nPage); |
| 2784 | if( rc==SQLITE_OK ){ |
| 2785 | if( nPage==0 ){ |
| 2786 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
| 2787 | }else{ |
| 2788 | *pExists = 1; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 2789 | } |
| 2790 | } |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 2791 | } |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2792 | return rc; |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2796 | ** Read the content of page pPg out of the database file. |
| 2797 | */ |
| 2798 | static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){ |
| 2799 | int rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2800 | i64 offset; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2801 | assert( MEMDB==0 ); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2802 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 2803 | if( !pPager->fd->pMethods ){ |
| 2804 | return SQLITE_IOERR_SHORT_READ; |
| 2805 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2806 | offset = (pgno-1)*(i64)pPager->pageSize; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2807 | rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2808 | PAGER_INCR(sqlite3_pager_readdb_count); |
| 2809 | PAGER_INCR(pPager->nRead); |
| 2810 | IOTRACE(("PGIN %p %d\n", pPager, pgno)); |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 2811 | if( pgno==1 ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2812 | memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24], |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 2813 | sizeof(pPager->dbFileVers)); |
| 2814 | } |
danielk1977 | 3084952 | 2008-08-21 04:35:18 +0000 | [diff] [blame] | 2815 | CODEC1(pPager, pPg->pData, pPg->pgno, 3); |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 2816 | PAGERTRACE(("FETCH %d page %d hash(%08x)\n", |
| 2817 | PAGERID(pPager), pPg->pgno, pager_pagehash(pPg))); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2818 | return rc; |
| 2819 | } |
| 2820 | |
| 2821 | |
| 2822 | /* |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2823 | ** This function is called to obtain the shared lock required before |
| 2824 | ** data may be read from the pager cache. If the shared lock has already |
| 2825 | ** been obtained, this function is a no-op. |
danielk1977 | 393f068 | 2007-03-31 10:00:48 +0000 | [diff] [blame] | 2826 | ** |
| 2827 | ** Immediately after obtaining the shared lock (if required), this function |
| 2828 | ** checks for a hot-journal file. If one is found, an emergency rollback |
| 2829 | ** is performed immediately. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2830 | */ |
| 2831 | static int pagerSharedLock(Pager *pPager){ |
| 2832 | int rc = SQLITE_OK; |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2833 | int isErrorReset = 0; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2834 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2835 | /* If this database is opened for exclusive access, has no outstanding |
| 2836 | ** page references and is in an error-state, now is the chance to clear |
| 2837 | ** the error. Discard the contents of the pager-cache and treat any |
| 2838 | ** open journal file as a hot-journal. |
| 2839 | */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2840 | if( !MEMDB && pPager->exclusiveMode |
| 2841 | && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode |
| 2842 | ){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2843 | if( pPager->journalOpen ){ |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2844 | isErrorReset = 1; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2845 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2846 | pPager->errCode = SQLITE_OK; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 2847 | pager_reset(pPager); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | /* If the pager is still in an error state, do not proceed. The error |
| 2851 | ** state will be cleared at some point in the future when all page |
| 2852 | ** references are dropped and the cache can be discarded. |
| 2853 | */ |
| 2854 | if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
| 2855 | return pPager->errCode; |
| 2856 | } |
| 2857 | |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2858 | if( pPager->state==PAGER_UNLOCK || isErrorReset ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2859 | sqlite3_vfs *pVfs = pPager->pVfs; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2860 | int isHotJournal = 0; |
shane | 049fc21 | 2008-10-22 16:26:47 +0000 | [diff] [blame] | 2861 | assert( !MEMDB ); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2862 | assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); |
| 2863 | if( !pPager->noReadlock ){ |
| 2864 | rc = pager_wait_on_lock(pPager, SHARED_LOCK); |
| 2865 | if( rc!=SQLITE_OK ){ |
| 2866 | assert( pPager->state==PAGER_UNLOCK ); |
| 2867 | return pager_error(pPager, rc); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2868 | } |
danielk1977 | 0371f1b | 2009-01-09 17:11:04 +0000 | [diff] [blame] | 2869 | }else if( pPager->state==PAGER_UNLOCK ){ |
| 2870 | pPager->state = PAGER_SHARED; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2871 | } |
danielk1977 | 0371f1b | 2009-01-09 17:11:04 +0000 | [diff] [blame] | 2872 | assert( pPager->state>=SHARED_LOCK ); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2873 | |
| 2874 | /* If a journal file exists, and there is no RESERVED lock on the |
| 2875 | ** database file, then it either needs to be played back or deleted. |
| 2876 | */ |
| 2877 | if( !isErrorReset ){ |
| 2878 | rc = hasHotJournal(pPager, &isHotJournal); |
| 2879 | if( rc!=SQLITE_OK ){ |
| 2880 | goto failed; |
| 2881 | } |
| 2882 | } |
| 2883 | if( isErrorReset || isHotJournal ){ |
| 2884 | /* Get an EXCLUSIVE lock on the database file. At this point it is |
| 2885 | ** important that a RESERVED lock is not obtained on the way to the |
| 2886 | ** EXCLUSIVE lock. If it were, another process might open the |
| 2887 | ** database file, detect the RESERVED lock, and conclude that the |
| 2888 | ** database is safe to read while this process is still rolling it |
| 2889 | ** back. |
| 2890 | ** |
| 2891 | ** Because the intermediate RESERVED lock is not requested, the |
| 2892 | ** second process will get to this point in the code and fail to |
| 2893 | ** obtain its own EXCLUSIVE lock on the database file. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2894 | */ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2895 | if( pPager->state<EXCLUSIVE_LOCK ){ |
| 2896 | rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2897 | if( rc!=SQLITE_OK ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 2898 | rc = pager_error(pPager, rc); |
| 2899 | goto failed; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2900 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2901 | pPager->state = PAGER_EXCLUSIVE; |
| 2902 | } |
| 2903 | |
| 2904 | /* Open the journal for read/write access. This is because in |
| 2905 | ** exclusive-access mode the file descriptor will be kept open and |
| 2906 | ** possibly used for a transaction later on. On some systems, the |
| 2907 | ** OsTruncate() call used in exclusive-access mode also requires |
| 2908 | ** a read/write file handle. |
| 2909 | */ |
| 2910 | if( !isErrorReset && pPager->journalOpen==0 ){ |
| 2911 | int res; |
| 2912 | rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res); |
| 2913 | if( rc==SQLITE_OK ){ |
| 2914 | if( res ){ |
| 2915 | int fout = 0; |
| 2916 | int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL; |
| 2917 | assert( !pPager->tempFile ); |
| 2918 | rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout); |
| 2919 | assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); |
danielk1977 | 281d8bd | 2008-10-29 07:01:56 +0000 | [diff] [blame] | 2920 | if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){ |
| 2921 | rc = SQLITE_CANTOPEN; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2922 | sqlite3OsClose(pPager->jfd); |
| 2923 | } |
| 2924 | }else{ |
| 2925 | /* If the journal does not exist, that means some other process |
| 2926 | ** has already rolled it back */ |
| 2927 | rc = SQLITE_BUSY; |
| 2928 | } |
| 2929 | } |
| 2930 | } |
| 2931 | if( rc!=SQLITE_OK ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2932 | goto failed; |
| 2933 | } |
| 2934 | pPager->journalOpen = 1; |
| 2935 | pPager->journalStarted = 0; |
| 2936 | pPager->journalOff = 0; |
| 2937 | pPager->setMaster = 0; |
| 2938 | pPager->journalHdr = 0; |
| 2939 | |
| 2940 | /* Playback and delete the journal. Drop the database write |
danielk1977 | 112f752 | 2009-01-08 17:50:45 +0000 | [diff] [blame] | 2941 | ** lock and reacquire the read lock. Purge the cache before |
| 2942 | ** playing back the hot-journal so that we don't end up with |
danielk1977 | ad0ea22 | 2009-01-08 18:04:13 +0000 | [diff] [blame] | 2943 | ** an inconsistent cache. |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2944 | */ |
danielk1977 | 112f752 | 2009-01-08 17:50:45 +0000 | [diff] [blame] | 2945 | sqlite3PcacheClear(pPager->pPCache); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2946 | rc = pager_playback(pPager, 1); |
| 2947 | if( rc!=SQLITE_OK ){ |
| 2948 | rc = pager_error(pPager, rc); |
| 2949 | goto failed; |
| 2950 | } |
| 2951 | assert(pPager->state==PAGER_SHARED || |
| 2952 | (pPager->exclusiveMode && pPager->state>PAGER_SHARED) |
| 2953 | ); |
| 2954 | } |
| 2955 | |
| 2956 | if( sqlite3PcachePagecount(pPager->pPCache)>0 ){ |
| 2957 | /* The shared-lock has just been acquired on the database file |
| 2958 | ** and there are already pages in the cache (from a previous |
| 2959 | ** read or write transaction). Check to see if the database |
| 2960 | ** has been modified. If the database has changed, flush the |
| 2961 | ** cache. |
| 2962 | ** |
| 2963 | ** Database changes is detected by looking at 15 bytes beginning |
| 2964 | ** at offset 24 into the file. The first 4 of these 16 bytes are |
| 2965 | ** a 32-bit counter that is incremented with each change. The |
| 2966 | ** other bytes change randomly with each file change when |
| 2967 | ** a codec is in use. |
| 2968 | ** |
| 2969 | ** There is a vanishingly small chance that a change will not be |
| 2970 | ** detected. The chance of an undetected change is so small that |
| 2971 | ** it can be neglected. |
| 2972 | */ |
| 2973 | char dbFileVers[sizeof(pPager->dbFileVers)]; |
| 2974 | sqlite3PagerPagecount(pPager, 0); |
| 2975 | |
| 2976 | if( pPager->errCode ){ |
| 2977 | rc = pPager->errCode; |
| 2978 | goto failed; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 2981 | assert( pPager->dbSizeValid ); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2982 | if( pPager->dbSize>0 ){ |
| 2983 | IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); |
| 2984 | rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); |
| 2985 | if( rc!=SQLITE_OK ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 2986 | goto failed; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2987 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2988 | }else{ |
| 2989 | memset(dbFileVers, 0, sizeof(dbFileVers)); |
| 2990 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2991 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2992 | if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ |
| 2993 | pager_reset(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2994 | } |
| 2995 | } |
danielk1977 | 0371f1b | 2009-01-09 17:11:04 +0000 | [diff] [blame] | 2996 | assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 2999 | failed: |
| 3000 | if( rc!=SQLITE_OK ){ |
| 3001 | /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */ |
| 3002 | pager_unlock(pPager); |
| 3003 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3004 | return rc; |
| 3005 | } |
| 3006 | |
| 3007 | /* |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3008 | ** Make sure we have the content for a page. If the page was |
| 3009 | ** previously acquired with noContent==1, then the content was |
| 3010 | ** just initialized to zeros instead of being read from disk. |
| 3011 | ** But now we need the real data off of disk. So make sure we |
| 3012 | ** have it. Read it in if we do not have it already. |
| 3013 | */ |
| 3014 | static int pager_get_content(PgHdr *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3015 | if( pPg->flags&PGHDR_NEED_READ ){ |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3016 | int rc = readDbPage(pPg->pPager, pPg, pPg->pgno); |
| 3017 | if( rc==SQLITE_OK ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3018 | pPg->flags &= ~PGHDR_NEED_READ; |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3019 | }else{ |
| 3020 | return rc; |
| 3021 | } |
| 3022 | } |
| 3023 | return SQLITE_OK; |
| 3024 | } |
| 3025 | |
| 3026 | /* |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3027 | ** If the reference count has reached zero, and the pager is not in the |
| 3028 | ** middle of a write transaction or opened in exclusive mode, unlock it. |
| 3029 | */ |
| 3030 | static void pagerUnlockIfUnused(Pager *pPager){ |
| 3031 | if( (sqlite3PcacheRefCount(pPager->pPCache)==0) |
| 3032 | && (!pPager->exclusiveMode || pPager->journalOff>0) |
| 3033 | ){ |
| 3034 | pagerUnlockAndRollback(pPager); |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | /* |
| 3039 | ** Drop a page from the cache using sqlite3PcacheDrop(). |
| 3040 | ** |
| 3041 | ** If this means there are now no pages with references to them, a rollback |
| 3042 | ** occurs and the lock on the database is removed. |
| 3043 | */ |
| 3044 | static void pagerDropPage(DbPage *pPg){ |
| 3045 | Pager *pPager = pPg->pPager; |
| 3046 | sqlite3PcacheDrop(pPg); |
| 3047 | pagerUnlockIfUnused(pPager); |
| 3048 | } |
| 3049 | |
| 3050 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3051 | ** Acquire a page. |
| 3052 | ** |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 3053 | ** 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] | 3054 | ** This read lock is dropped when the last page is released. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3055 | ** |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3056 | ** This routine works for any page number greater than 0. If the database |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3057 | ** file is smaller than the requested page, then no actual disk |
| 3058 | ** read occurs and the memory image of the page is initialized to |
| 3059 | ** all zeros. The extra data appended to a page is always initialized |
| 3060 | ** to zeros the first time a page is loaded into memory. |
| 3061 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3062 | ** The acquisition might fail for several reasons. In all cases, |
| 3063 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3064 | ** |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3065 | ** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3066 | ** to find a page in the in-memory cache first. If the page is not already |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3067 | ** in memory, this routine goes to disk to read it in whereas Lookup() |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3068 | ** just returns 0. This routine acquires a read-lock the first time it |
| 3069 | ** has to go to disk, and could also playback an old journal if necessary. |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3070 | ** Since Lookup() never goes to disk, it never has to deal with locks |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3071 | ** or journal files. |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 3072 | ** |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3073 | ** If noContent is false, the page contents are actually read from disk. |
| 3074 | ** If noContent is true, it means that we do not care about the contents |
| 3075 | ** of the page at this time, so do not do a disk read. Just fill in the |
| 3076 | ** page content with zeros. But mark the fact that we have not read the |
| 3077 | ** content by setting the PgHdr.needRead flag. Later on, if |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3078 | ** sqlite3PagerWrite() is called on this page or if this routine is |
| 3079 | ** called again with noContent==0, that means that the content is needed |
| 3080 | ** and the disk read should occur at that point. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3081 | */ |
danielk1977 | 65e0ff3 | 2008-09-19 09:14:43 +0000 | [diff] [blame] | 3082 | int sqlite3PagerAcquire( |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3083 | Pager *pPager, /* The pager open on the database file */ |
| 3084 | Pgno pgno, /* Page number to fetch */ |
| 3085 | DbPage **ppPage, /* Write a pointer to the page here */ |
| 3086 | int noContent /* Do not bother reading content from disk if true */ |
| 3087 | ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3088 | PgHdr *pPg = 0; |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3089 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3090 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3091 | assert( pPager->state==PAGER_UNLOCK |
| 3092 | || sqlite3PcacheRefCount(pPager->pPCache)>0 |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3093 | || pgno==1 |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3094 | ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3095 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 3096 | /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page |
| 3097 | ** number greater than this, or zero, is requested. |
| 3098 | */ |
drh | 267cb32 | 2005-09-16 17:16:52 +0000 | [diff] [blame] | 3099 | if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3100 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3103 | /* Make sure we have not hit any critical errors. |
| 3104 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 3105 | assert( pPager!=0 ); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3106 | *ppPage = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3107 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3108 | /* If this is the first page accessed, then get a SHARED lock |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3109 | ** on the database file. pagerSharedLock() is a no-op if |
| 3110 | ** a database lock is already held. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3111 | */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3112 | rc = pagerSharedLock(pPager); |
| 3113 | if( rc!=SQLITE_OK ){ |
| 3114 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3115 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3116 | assert( pPager->state!=PAGER_UNLOCK ); |
| 3117 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3118 | rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg); |
| 3119 | if( rc!=SQLITE_OK ){ |
| 3120 | return rc; |
| 3121 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3122 | if( pPg->pPager==0 ){ |
| 3123 | /* The pager cache has created a new page. Its content needs to |
| 3124 | ** be initialized. |
| 3125 | */ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3126 | int nMax; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3127 | PAGER_INCR(pPager->nMiss); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3128 | pPg->pPager = pPager; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3129 | memset(pPg->pExtra, 0, pPager->nExtra); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3130 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3131 | rc = sqlite3PagerPagecount(pPager, &nMax); |
| 3132 | if( rc!=SQLITE_OK ){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3133 | sqlite3PagerUnref(pPg); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3134 | return rc; |
| 3135 | } |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3136 | |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3137 | if( nMax<(int)pgno || MEMDB || noContent ){ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 3138 | if( pgno>pPager->mxPgno ){ |
danielk1977 | de3bea7 | 2007-05-09 15:56:39 +0000 | [diff] [blame] | 3139 | sqlite3PagerUnref(pPg); |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 3140 | return SQLITE_FULL; |
| 3141 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3142 | memset(pPg->pData, 0, pPager->pageSize); |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3143 | if( noContent ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3144 | pPg->flags |= PGHDR_NEED_READ; |
| 3145 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3146 | IOTRACE(("ZERO %p %d\n", pPager, pgno)); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3147 | }else{ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3148 | rc = readDbPage(pPager, pPg, pgno); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 3149 | if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3150 | /* sqlite3PagerUnref(pPg); */ |
| 3151 | pagerDropPage(pPg); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 3152 | return rc; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 3153 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3154 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3155 | #ifdef SQLITE_CHECK_PAGES |
| 3156 | pPg->pageHash = pager_pagehash(pPg); |
| 3157 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3158 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3159 | /* The requested page is in the page cache. */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3160 | assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3161 | PAGER_INCR(pPager->nHit); |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3162 | if( !noContent ){ |
| 3163 | rc = pager_get_content(pPg); |
| 3164 | if( rc ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3165 | sqlite3PagerUnref(pPg); |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3166 | return rc; |
| 3167 | } |
| 3168 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3169 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3170 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3171 | *ppPage = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3172 | return SQLITE_OK; |
| 3173 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3174 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3175 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3176 | ** Acquire a page if it is already in the in-memory cache. Do |
| 3177 | ** not read the page from disk. Return a pointer to the page, |
| 3178 | ** or 0 if the page is not in cache. |
| 3179 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3180 | ** See also sqlite3PagerGet(). The difference between this routine |
| 3181 | ** and sqlite3PagerGet() is that _get() will go to the disk and read |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3182 | ** in the page if the page is not already in cache. This routine |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3183 | ** returns NULL if the page is not in cache or if a disk I/O error |
| 3184 | ** has ever happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3185 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3186 | DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3187 | PgHdr *pPg = 0; |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 3188 | assert( pPager!=0 ); |
| 3189 | assert( pgno!=0 ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3190 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3191 | if( (pPager->state!=PAGER_UNLOCK) |
| 3192 | && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL) |
| 3193 | ){ |
| 3194 | sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3195 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3196 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3197 | return pPg; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3201 | ** Release a page. |
| 3202 | ** |
| 3203 | ** If the number of references to the page drop to zero, then the |
| 3204 | ** page is added to the LRU list. When all references to all pages |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3205 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3206 | ** removed. |
| 3207 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3208 | int sqlite3PagerUnref(DbPage *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3209 | if( pPg ){ |
| 3210 | Pager *pPager = pPg->pPager; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3211 | sqlite3PcacheRelease(pPg); |
| 3212 | pagerUnlockIfUnused(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3213 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3214 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
danielk1977 | 9153d85 | 2009-01-07 10:52:56 +0000 | [diff] [blame] | 3217 | /* |
| 3218 | ** If the main journal file has already been opened, ensure that the |
| 3219 | ** sub-journal file is open too. If the main journal is not open, |
| 3220 | ** this function is a no-op. |
| 3221 | ** |
| 3222 | ** SQLITE_OK is returned if everything goes according to plan. An |
| 3223 | ** SQLITE_IOERR_XXX error code is returned if the call to |
| 3224 | ** sqlite3OsOpen() fails. |
| 3225 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3226 | static int openSubJournal(Pager *pPager){ |
| 3227 | int rc = SQLITE_OK; |
| 3228 | if( pPager->journalOpen && !pPager->sjfd->pMethods ){ |
| 3229 | if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ |
| 3230 | sqlite3MemJournalOpen(pPager->sjfd); |
| 3231 | }else{ |
| 3232 | rc = sqlite3PagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL); |
| 3233 | } |
| 3234 | } |
| 3235 | return rc; |
| 3236 | } |
| 3237 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3238 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3239 | ** Create a journal file for pPager. There should already be a RESERVED |
| 3240 | ** or EXCLUSIVE lock on the database file when this routine is called. |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3241 | ** |
| 3242 | ** Return SQLITE_OK if everything. Return an error code and release the |
| 3243 | ** write lock if anything goes wrong. |
| 3244 | */ |
| 3245 | static int pager_open_journal(Pager *pPager){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3246 | sqlite3_vfs *pVfs = pPager->pVfs; |
| 3247 | int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE); |
| 3248 | |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3249 | int rc; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3250 | assert( pPager->state>=PAGER_RESERVED ); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3251 | assert( pPager->useJournal ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3252 | assert( pPager->pInJournal==0 ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3253 | sqlite3PagerPagecount(pPager, 0); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3254 | pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3255 | if( pPager->pInJournal==0 ){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3256 | rc = SQLITE_NOMEM; |
| 3257 | goto failed_to_open_journal; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3258 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3259 | |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3260 | if( pPager->journalOpen==0 ){ |
| 3261 | if( pPager->tempFile ){ |
| 3262 | flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); |
| 3263 | }else{ |
| 3264 | flags |= (SQLITE_OPEN_MAIN_JOURNAL); |
drh | 600e46a | 2007-03-28 01:59:33 +0000 | [diff] [blame] | 3265 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3266 | if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ |
| 3267 | sqlite3MemJournalOpen(pPager->jfd); |
| 3268 | rc = SQLITE_OK; |
| 3269 | }else{ |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3270 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3271 | rc = sqlite3JournalOpen( |
| 3272 | pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) |
| 3273 | ); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3274 | #else |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3275 | rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3276 | #endif |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3277 | } |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3278 | assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); |
| 3279 | pPager->journalOff = 0; |
| 3280 | pPager->setMaster = 0; |
| 3281 | pPager->journalHdr = 0; |
| 3282 | if( rc!=SQLITE_OK ){ |
| 3283 | if( rc==SQLITE_NOMEM ){ |
| 3284 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
| 3285 | } |
| 3286 | goto failed_to_open_journal; |
| 3287 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3288 | } |
| 3289 | pPager->journalOpen = 1; |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 3290 | pPager->journalStarted = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3291 | pPager->needSync = 0; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 3292 | pPager->nRec = 0; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3293 | if( pPager->errCode ){ |
| 3294 | rc = pPager->errCode; |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 3295 | goto failed_to_open_journal; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3296 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3297 | pPager->dbOrigSize = pPager->dbSize; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 3298 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 3299 | rc = writeJournalHdr(pPager); |
| 3300 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3301 | if( pPager->nSavepoint && rc==SQLITE_OK ){ |
| 3302 | rc = openSubJournal(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3303 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3304 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 3305 | rc = pager_end_transaction(pPager, 0); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3306 | if( rc==SQLITE_OK ){ |
| 3307 | rc = SQLITE_FULL; |
| 3308 | } |
| 3309 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3310 | return rc; |
| 3311 | |
| 3312 | failed_to_open_journal: |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3313 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 3314 | pPager->pInJournal = 0; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3315 | return rc; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | /* |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3319 | ** Acquire a write-lock on the database. The lock is removed when |
| 3320 | ** the any of the following happen: |
| 3321 | ** |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3322 | ** * sqlite3PagerCommitPhaseTwo() is called. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3323 | ** * sqlite3PagerRollback() is called. |
| 3324 | ** * sqlite3PagerClose() is called. |
| 3325 | ** * sqlite3PagerUnref() is called to on every outstanding page. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3326 | ** |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3327 | ** The first parameter to this routine is a pointer to any open page of the |
| 3328 | ** database file. Nothing changes about the page - it is used merely to |
| 3329 | ** acquire a pointer to the Pager structure and as proof that there is |
| 3330 | ** already a read-lock on the database. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3331 | ** |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3332 | ** The second parameter indicates how much space in bytes to reserve for a |
| 3333 | ** master journal file-name at the start of the journal when it is created. |
| 3334 | ** |
| 3335 | ** A journal file is opened if this is not a temporary file. For temporary |
| 3336 | ** files, the opening of the journal file is deferred until there is an |
| 3337 | ** actual need to write to the journal. |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3338 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3339 | ** If the database is already reserved for writing, this routine is a no-op. |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 3340 | ** |
| 3341 | ** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file |
| 3342 | ** immediately instead of waiting until we try to flush the cache. The |
| 3343 | ** exFlag is ignored if a transaction is already active. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3344 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3345 | int sqlite3PagerBegin(DbPage *pPg, int exFlag){ |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3346 | Pager *pPager = pPg->pPager; |
| 3347 | int rc = SQLITE_OK; |
| 3348 | assert( pPg->nRef>0 ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3349 | assert( pPager->state!=PAGER_UNLOCK ); |
| 3350 | if( pPager->state==PAGER_SHARED ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3351 | assert( pPager->pInJournal==0 ); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3352 | assert( !MEMDB ); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3353 | rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK); |
| 3354 | if( rc==SQLITE_OK ){ |
| 3355 | pPager->state = PAGER_RESERVED; |
| 3356 | if( exFlag ){ |
| 3357 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 3358 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3359 | } |
| 3360 | if( rc!=SQLITE_OK ){ |
| 3361 | return rc; |
| 3362 | } |
| 3363 | pPager->dirtyCache = 0; |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 3364 | PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager))); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3365 | if( pPager->useJournal && !pPager->tempFile |
| 3366 | && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
| 3367 | rc = pager_open_journal(pPager); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3368 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3369 | }else if( pPager->journalOpen && pPager->journalOff==0 ){ |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3370 | /* This happens when the pager was in exclusive-access mode the last |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3371 | ** time a (read or write) transaction was successfully concluded |
| 3372 | ** by this connection. Instead of deleting the journal file it was |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3373 | ** kept open and either was truncated to 0 bytes or its header was |
| 3374 | ** overwritten with zeros. |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3375 | */ |
| 3376 | assert( pPager->nRec==0 ); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3377 | assert( pPager->dbOrigSize==0 ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3378 | assert( pPager->pInJournal==0 ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3379 | sqlite3PagerPagecount(pPager, 0); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3380 | pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3381 | if( !pPager->pInJournal ){ |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3382 | rc = SQLITE_NOMEM; |
| 3383 | }else{ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3384 | pPager->dbOrigSize = pPager->dbSize; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3385 | rc = writeJournalHdr(pPager); |
| 3386 | } |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3387 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3388 | assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK ); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3389 | return rc; |
| 3390 | } |
| 3391 | |
| 3392 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3393 | ** Mark a data page as writeable. The page is written into the journal |
| 3394 | ** if it is not there already. This routine must be called before making |
| 3395 | ** changes to a page. |
| 3396 | ** |
| 3397 | ** The first time this routine is called, the pager creates a new |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3398 | ** journal and acquires a RESERVED lock on the database. If the RESERVED |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3399 | ** lock could not be acquired, this routine returns SQLITE_BUSY. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3400 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3401 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3402 | ** |
| 3403 | ** If the journal file could not be written because the disk is full, |
| 3404 | ** then this routine returns SQLITE_FULL and does an immediate rollback. |
| 3405 | ** All subsequent write attempts also return SQLITE_FULL until there |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3406 | ** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3407 | ** reset. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3408 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3409 | static int pager_write(PgHdr *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3410 | void *pData = pPg->pData; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 3411 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 3412 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 3413 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3414 | /* Check for errors |
| 3415 | */ |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3416 | if( pPager->errCode ){ |
| 3417 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3418 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3419 | if( pPager->readOnly ){ |
| 3420 | return SQLITE_PERM; |
| 3421 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3422 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 3423 | assert( !pPager->setMaster ); |
| 3424 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3425 | CHECK_PAGE(pPg); |
| 3426 | |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3427 | /* If this page was previously acquired with noContent==1, that means |
| 3428 | ** we didn't really read in the content of the page. This can happen |
| 3429 | ** (for example) when the page is being moved to the freelist. But |
| 3430 | ** now we are (perhaps) moving the page off of the freelist for |
| 3431 | ** reuse and we need to know its original content so that content |
| 3432 | ** can be stored in the rollback journal. So do the read at this |
| 3433 | ** time. |
| 3434 | */ |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3435 | rc = pager_get_content(pPg); |
| 3436 | if( rc ){ |
| 3437 | return rc; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3440 | /* Mark the page as dirty. If the page has already been written |
| 3441 | ** to the journal then we can return right away. |
| 3442 | */ |
drh | c047b9f | 2008-11-21 03:23:43 +0000 | [diff] [blame] | 3443 | sqlite3PcacheMakeDirty(pPg); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3444 | if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3445 | pPager->dirtyCache = 1; |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3446 | pPager->dbModified = 1; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3447 | }else{ |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3448 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3449 | /* If we get this far, it means that the page needs to be |
| 3450 | ** written to the transaction journal or the ckeckpoint journal |
| 3451 | ** or both. |
| 3452 | ** |
| 3453 | ** First check to see that the transaction journal exists and |
| 3454 | ** create it if it does not. |
| 3455 | */ |
| 3456 | assert( pPager->state!=PAGER_UNLOCK ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3457 | rc = sqlite3PagerBegin(pPg, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3458 | if( rc!=SQLITE_OK ){ |
| 3459 | return rc; |
| 3460 | } |
| 3461 | assert( pPager->state>=PAGER_RESERVED ); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3462 | if( !pPager->journalOpen && pPager->useJournal |
| 3463 | && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3464 | rc = pager_open_journal(pPager); |
| 3465 | if( rc!=SQLITE_OK ) return rc; |
| 3466 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3467 | pPager->dirtyCache = 1; |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3468 | pPager->dbModified = 1; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3469 | |
| 3470 | /* The transaction journal now exists and we have a RESERVED or an |
| 3471 | ** EXCLUSIVE lock on the main database file. Write the current page to |
| 3472 | ** the transaction journal if it is not there already. |
| 3473 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 3474 | if( !pageInJournal(pPg) && pPager->journalOpen ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3475 | if( pPg->pgno<=pPager->dbOrigSize ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3476 | u32 cksum; |
| 3477 | char *pData2; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3478 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3479 | /* We should never write to the journal file the page that |
| 3480 | ** contains the database locks. The following assert verifies |
| 3481 | ** that we do not. */ |
| 3482 | assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
| 3483 | pData2 = CODEC2(pPager, pData, pPg->pgno, 7); |
| 3484 | cksum = pager_cksum(pPager, (u8*)pData2); |
| 3485 | rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno); |
| 3486 | if( rc==SQLITE_OK ){ |
| 3487 | rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, |
| 3488 | pPager->journalOff + 4); |
| 3489 | pPager->journalOff += pPager->pageSize+4; |
| 3490 | } |
| 3491 | if( rc==SQLITE_OK ){ |
| 3492 | rc = write32bits(pPager->jfd, pPager->journalOff, cksum); |
| 3493 | pPager->journalOff += 4; |
| 3494 | } |
| 3495 | IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, |
| 3496 | pPager->journalOff, pPager->pageSize)); |
| 3497 | PAGER_INCR(sqlite3_pager_writej_count); |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 3498 | PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n", |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3499 | PAGERID(pPager), pPg->pgno, |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 3500 | ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg))); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 3501 | |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 3502 | /* Even if an IO or diskfull error occurred while journalling the |
| 3503 | ** page in the block above, set the need-sync flag for the page. |
| 3504 | ** Otherwise, when the transaction is rolled back, the logic in |
| 3505 | ** playback_one_page() will think that the page needs to be restored |
| 3506 | ** in the database file. And if an IO error occurs while doing so, |
| 3507 | ** then corruption may follow. |
| 3508 | */ |
| 3509 | if( !pPager->noSync ){ |
| 3510 | pPg->flags |= PGHDR_NEED_SYNC; |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 3511 | pPager->needSync = 1; |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3514 | /* An error has occured writing to the journal file. The |
| 3515 | ** transaction will be rolled back by the layer above. |
| 3516 | */ |
| 3517 | if( rc!=SQLITE_OK ){ |
| 3518 | return rc; |
| 3519 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 3520 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3521 | pPager->nRec++; |
| 3522 | assert( pPager->pInJournal!=0 ); |
drh | 7539b6b | 2009-01-02 21:39:39 +0000 | [diff] [blame] | 3523 | rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
| 3524 | testcase( rc==SQLITE_NOMEM ); |
| 3525 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 3526 | rc |= addToSavepointBitvecs(pPager, pPg->pgno); |
| 3527 | if( rc!=SQLITE_OK ){ |
| 3528 | assert( rc==SQLITE_NOMEM ); |
| 3529 | return rc; |
| 3530 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3531 | }else{ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3532 | if( !pPager->journalStarted && !pPager->noSync ){ |
| 3533 | pPg->flags |= PGHDR_NEED_SYNC; |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 3534 | pPager->needSync = 1; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3535 | } |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 3536 | PAGERTRACE(("APPEND %d page %d needSync=%d\n", |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3537 | PAGERID(pPager), pPg->pgno, |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 3538 | ((pPg->flags&PGHDR_NEED_SYNC)?1:0))); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3539 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | /* If the statement journal is open and the page is not in it, |
| 3543 | ** then write the current page to the statement journal. Note that |
| 3544 | ** the statement journal format differs from the standard journal format |
| 3545 | ** in that it omits the checksums and the header. |
| 3546 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3547 | if( subjRequiresPage(pPg) ){ |
danielk1977 | f2c31ad | 2009-01-06 13:40:08 +0000 | [diff] [blame] | 3548 | rc = subjournalPage(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3549 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
| 3552 | /* Update the database size and return. |
| 3553 | */ |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 3554 | assert( pPager->state>=PAGER_SHARED ); |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 3555 | if( pPager->dbSize<pPg->pgno ){ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3556 | pPager->dbSize = pPg->pgno; |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 3557 | if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){ |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 3558 | pPager->dbSize++; |
| 3559 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3560 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 3561 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | /* |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3565 | ** This function is used to mark a data-page as writable. It uses |
| 3566 | ** pager_write() to open a journal file (if it is not already open) |
| 3567 | ** and write the page *pData to the journal. |
| 3568 | ** |
| 3569 | ** The difference between this function and pager_write() is that this |
| 3570 | ** function also deals with the special case where 2 or more pages |
| 3571 | ** fit on a single disk sector. In this case all co-resident pages |
| 3572 | ** must have been written to the journal file before returning. |
| 3573 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3574 | int sqlite3PagerWrite(DbPage *pDbPage){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3575 | int rc = SQLITE_OK; |
| 3576 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3577 | PgHdr *pPg = pDbPage; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3578 | Pager *pPager = pPg->pPager; |
| 3579 | Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); |
| 3580 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3581 | if( nPagePerSector>1 ){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3582 | Pgno nPageCount; /* Total number of pages in database file */ |
| 3583 | Pgno pg1; /* First page of the sector pPg is located on. */ |
| 3584 | int nPage; /* Number of pages starting at pg1 to journal */ |
| 3585 | int ii; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3586 | int needSync = 0; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3587 | |
| 3588 | /* Set the doNotSync flag to 1. This is because we cannot allow a journal |
| 3589 | ** header to be written between the pages journaled by this function. |
| 3590 | */ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3591 | assert( !MEMDB ); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3592 | assert( pPager->doNotSync==0 ); |
| 3593 | pPager->doNotSync = 1; |
| 3594 | |
| 3595 | /* This trick assumes that both the page-size and sector-size are |
| 3596 | ** an integer power of 2. It sets variable pg1 to the identifier |
| 3597 | ** of the first page of the sector pPg is located on. |
| 3598 | */ |
| 3599 | pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; |
| 3600 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3601 | sqlite3PagerPagecount(pPager, (int *)&nPageCount); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3602 | if( pPg->pgno>nPageCount ){ |
| 3603 | nPage = (pPg->pgno - pg1)+1; |
| 3604 | }else if( (pg1+nPagePerSector-1)>nPageCount ){ |
| 3605 | nPage = nPageCount+1-pg1; |
| 3606 | }else{ |
| 3607 | nPage = nPagePerSector; |
| 3608 | } |
| 3609 | assert(nPage>0); |
| 3610 | assert(pg1<=pPg->pgno); |
| 3611 | assert((pg1+nPage)>pPg->pgno); |
| 3612 | |
| 3613 | for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ |
| 3614 | Pgno pg = pg1+ii; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3615 | PgHdr *pPage; |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3616 | if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3617 | if( pg!=PAGER_MJ_PGNO(pPager) ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3618 | rc = sqlite3PagerGet(pPager, pg, &pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3619 | if( rc==SQLITE_OK ){ |
| 3620 | rc = pager_write(pPage); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3621 | if( pPage->flags&PGHDR_NEED_SYNC ){ |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3622 | needSync = 1; |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 3623 | assert(pPager->needSync); |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3624 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3625 | sqlite3PagerUnref(pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3626 | } |
| 3627 | } |
drh | c81945e | 2008-03-10 14:12:53 +0000 | [diff] [blame] | 3628 | }else if( (pPage = pager_lookup(pPager, pg))!=0 ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3629 | if( pPage->flags&PGHDR_NEED_SYNC ){ |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3630 | needSync = 1; |
| 3631 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3632 | sqlite3PagerUnref(pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3633 | } |
| 3634 | } |
| 3635 | |
drh | ee03d62 | 2009-01-07 15:33:45 +0000 | [diff] [blame] | 3636 | /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3637 | ** starting at pg1, then it needs to be set for all of them. Because |
| 3638 | ** writing to any of these nPage pages may damage the others, the |
| 3639 | ** journal file must contain sync()ed copies of all of them |
| 3640 | ** before any of them can be written out to the database file. |
| 3641 | */ |
| 3642 | if( needSync ){ |
drh | b3df2e1 | 2008-09-17 20:06:26 +0000 | [diff] [blame] | 3643 | assert( !MEMDB && pPager->noSync==0 ); |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3644 | for(ii=0; ii<nPage && needSync; ii++){ |
| 3645 | PgHdr *pPage = pager_lookup(pPager, pg1+ii); |
drh | ee03d62 | 2009-01-07 15:33:45 +0000 | [diff] [blame] | 3646 | if( pPage ){ |
drh | ee03d62 | 2009-01-07 15:33:45 +0000 | [diff] [blame] | 3647 | pPage->flags |= PGHDR_NEED_SYNC; |
| 3648 | sqlite3PagerUnref(pPage); |
| 3649 | } |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3650 | } |
| 3651 | assert(pPager->needSync); |
| 3652 | } |
| 3653 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3654 | assert( pPager->doNotSync==1 ); |
| 3655 | pPager->doNotSync = 0; |
| 3656 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3657 | rc = pager_write(pDbPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3658 | } |
| 3659 | return rc; |
| 3660 | } |
| 3661 | |
| 3662 | /* |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 3663 | ** Return TRUE if the page given in the argument was previously passed |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3664 | ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3665 | ** to change the content of the page. |
| 3666 | */ |
danielk1977 | 7d3a666 | 2006-01-23 16:21:05 +0000 | [diff] [blame] | 3667 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3668 | int sqlite3PagerIswriteable(DbPage *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3669 | return pPg->flags&PGHDR_DIRTY; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3670 | } |
danielk1977 | 7d3a666 | 2006-01-23 16:21:05 +0000 | [diff] [blame] | 3671 | #endif |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3672 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3673 | /* |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3674 | ** A call to this routine tells the pager that it is not necessary to |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3675 | ** write the information on page pPg back to the disk, even though |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3676 | ** that page might be marked as dirty. This happens, for example, when |
| 3677 | ** the page has been added as a leaf of the freelist and so its |
| 3678 | ** content no longer matters. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3679 | ** |
| 3680 | ** The overlying software layer calls this routine when all of the data |
| 3681 | ** on the given page is unused. The pager marks the page as clean so |
| 3682 | ** that it does not get written to disk. |
| 3683 | ** |
| 3684 | ** Tests show that this optimization, together with the |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3685 | ** sqlite3PagerDontRollback() below, more than double the speed |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3686 | ** of large INSERT operations and quadruple the speed of large DELETEs. |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 3687 | ** |
danielk1977 | 0298393 | 2009-01-10 17:57:48 +0000 | [diff] [blame] | 3688 | ** When this routine is called, set the bit corresponding to pDbPage in |
| 3689 | ** the Pager.pAlwaysRollback bitvec. Subsequent calls to |
| 3690 | ** sqlite3PagerDontRollback() for the same page will thereafter be ignored. |
| 3691 | ** This is necessary to avoid a problem where a page with data is added to |
| 3692 | ** the freelist during one part of a transaction then removed from the |
| 3693 | ** freelist during a later part of the same transaction and reused for some |
| 3694 | ** other purpose. When it is first added to the freelist, this routine is |
| 3695 | ** called. When reused, the sqlite3PagerDontRollback() routine is called. |
| 3696 | ** But because the page contains critical data, we still need to be sure it |
| 3697 | ** gets rolled back in spite of the sqlite3PagerDontRollback() call. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3698 | */ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3699 | int sqlite3PagerDontWrite(DbPage *pDbPage){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3700 | PgHdr *pPg = pDbPage; |
| 3701 | Pager *pPager = pPg->pPager; |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3702 | int rc; |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 3703 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3704 | if( pPg->pgno>pPager->dbOrigSize ){ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3705 | return SQLITE_OK; |
| 3706 | } |
| 3707 | if( pPager->pAlwaysRollback==0 ){ |
| 3708 | assert( pPager->pInJournal ); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3709 | pPager->pAlwaysRollback = sqlite3BitvecCreate(pPager->dbOrigSize); |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3710 | if( !pPager->pAlwaysRollback ){ |
| 3711 | return SQLITE_NOMEM; |
| 3712 | } |
| 3713 | } |
| 3714 | rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno); |
| 3715 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3716 | if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 3717 | assert( pPager->state>=PAGER_SHARED ); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3718 | if( pPager->dbSize==pPg->pgno && pPager->dbOrigSize<pPager->dbSize ){ |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 3719 | /* If this pages is the last page in the file and the file has grown |
| 3720 | ** during the current transaction, then do NOT mark the page as clean. |
| 3721 | ** When the database file grows, we must make sure that the last page |
| 3722 | ** gets written at least once so that the disk file will be the correct |
| 3723 | ** size. If you do not write this page and the size of the file |
| 3724 | ** on the disk ends up being too small, that can lead to database |
| 3725 | ** corruption during the next transaction. |
| 3726 | */ |
| 3727 | }else{ |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 3728 | PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager))); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3729 | IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) |
danielk1977 | 33e3216 | 2008-08-23 18:53:08 +0000 | [diff] [blame] | 3730 | pPg->flags |= PGHDR_DONT_WRITE; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3731 | #ifdef SQLITE_CHECK_PAGES |
| 3732 | pPg->pageHash = pager_pagehash(pPg); |
| 3733 | #endif |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 3734 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3735 | } |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3736 | return rc; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | /* |
| 3740 | ** A call to this routine tells the pager that if a rollback occurs, |
| 3741 | ** it is not necessary to restore the data on the given page. This |
| 3742 | ** means that the pager does not have to record the given page in the |
| 3743 | ** rollback journal. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3744 | ** |
| 3745 | ** If we have not yet actually read the content of this page (if |
| 3746 | ** the PgHdr.needRead flag is set) then this routine acts as a promise |
| 3747 | ** that we will never need to read the page content in the future. |
| 3748 | ** so the needRead flag can be cleared at this point. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3749 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3750 | void sqlite3PagerDontRollback(DbPage *pPg){ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3751 | Pager *pPager = pPg->pPager; |
drh | 7539b6b | 2009-01-02 21:39:39 +0000 | [diff] [blame] | 3752 | TESTONLY( int rc; ) /* Return value from sqlite3BitvecSet() */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3753 | |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3754 | assert( pPager->state>=PAGER_RESERVED ); |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3755 | |
| 3756 | /* If the journal file is not open, or DontWrite() has been called on |
danielk1977 | 0298393 | 2009-01-10 17:57:48 +0000 | [diff] [blame] | 3757 | ** this page (DontWrite() sets the Pager.pAlwaysRollback bit), then this |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3758 | ** function is a no-op. |
| 3759 | */ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3760 | if( pPager->journalOpen==0 |
| 3761 | || sqlite3BitvecTest(pPager->pAlwaysRollback, pPg->pgno) |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3762 | || pPg->pgno>pPager->dbOrigSize |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3763 | ){ |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 3764 | return; |
| 3765 | } |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3766 | |
drh | c5d0bd9 | 2008-04-14 01:00:57 +0000 | [diff] [blame] | 3767 | #ifdef SQLITE_SECURE_DELETE |
drh | 1feb7dd | 2008-11-19 18:30:29 +0000 | [diff] [blame] | 3768 | if( sqlite3BitvecTest(pPager->pInJournal, pPg->pgno)!=0 |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3769 | || pPg->pgno>pPager->dbOrigSize ){ |
drh | c5d0bd9 | 2008-04-14 01:00:57 +0000 | [diff] [blame] | 3770 | return; |
| 3771 | } |
| 3772 | #endif |
| 3773 | |
| 3774 | /* If SECURE_DELETE is disabled, then there is no way that this |
| 3775 | ** routine can be called on a page for which sqlite3PagerDontWrite() |
| 3776 | ** has not been previously called during the same transaction. |
| 3777 | ** And if DontWrite() has previously been called, the following |
| 3778 | ** conditions must be met. |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 3779 | ** |
| 3780 | ** (Later:) Not true. If the database is corrupted by having duplicate |
| 3781 | ** pages on the freelist (ex: corrupt9.test) then the following is not |
| 3782 | ** necessarily true: |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3783 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3784 | /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->dbOrigSize ); */ |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3785 | |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3786 | assert( pPager->pInJournal!=0 ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3787 | pPg->flags &= ~PGHDR_NEED_READ; |
drh | 7539b6b | 2009-01-02 21:39:39 +0000 | [diff] [blame] | 3788 | |
| 3789 | /* Failure to set the bits in the InJournal bit-vectors is benign. |
| 3790 | ** It merely means that we might do some extra work to journal a page |
drh | a8a71ba | 2009-01-03 12:55:17 +0000 | [diff] [blame] | 3791 | ** that does not need to be journaled. Nevertheless, be sure to test the |
drh | 7539b6b | 2009-01-02 21:39:39 +0000 | [diff] [blame] | 3792 | ** case where a malloc error occurs while trying to set a bit in a |
| 3793 | ** bit vector. |
| 3794 | */ |
| 3795 | sqlite3BeginBenignMalloc(); |
| 3796 | TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
| 3797 | testcase( rc==SQLITE_NOMEM ); |
| 3798 | TESTONLY( rc = ) addToSavepointBitvecs(pPager, pPg->pgno); |
| 3799 | testcase( rc==SQLITE_NOMEM ); |
| 3800 | sqlite3EndBenignMalloc(); |
| 3801 | |
| 3802 | |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 3803 | PAGERTRACE(("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager))); |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3804 | IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno)) |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 3807 | |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3808 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3809 | ** This routine is called to increment the database file change-counter, |
| 3810 | ** stored at byte 24 of the pager file. |
| 3811 | */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3812 | static int pager_incr_changecounter(Pager *pPager, int isDirect){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3813 | PgHdr *pPgHdr; |
| 3814 | u32 change_counter; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3815 | int rc = SQLITE_OK; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3816 | |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 3817 | #ifndef SQLITE_ENABLE_ATOMIC_WRITE |
| 3818 | assert( isDirect==0 ); /* isDirect is only true for atomic writes */ |
| 3819 | #endif |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 3820 | if( !pPager->changeCountDone && pPager->dbSize>0 ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3821 | /* Open page 1 of the file for writing. */ |
| 3822 | rc = sqlite3PagerGet(pPager, 1, &pPgHdr); |
| 3823 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3824 | |
| 3825 | if( !isDirect ){ |
| 3826 | rc = sqlite3PagerWrite(pPgHdr); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3827 | if( rc!=SQLITE_OK ){ |
| 3828 | sqlite3PagerUnref(pPgHdr); |
| 3829 | return rc; |
| 3830 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3833 | /* Increment the value just read and write it back to byte 24. */ |
drh | b100391 | 2007-07-20 00:33:36 +0000 | [diff] [blame] | 3834 | change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3835 | change_counter++; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3836 | put32bits(((char*)pPgHdr->pData)+24, change_counter); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3837 | |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 3838 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3839 | if( isDirect && pPager->fd->pMethods ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3840 | const void *zBuf = pPgHdr->pData; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3841 | assert( pPager->dbFileSize>0 ); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3842 | rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); |
| 3843 | } |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 3844 | #endif |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3845 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3846 | /* Release the page reference. */ |
| 3847 | sqlite3PagerUnref(pPgHdr); |
| 3848 | pPager->changeCountDone = 1; |
| 3849 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3850 | return rc; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | /* |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3854 | ** Sync the pager file to disk. |
| 3855 | */ |
| 3856 | int sqlite3PagerSync(Pager *pPager){ |
| 3857 | int rc; |
drh | 7426f86 | 2008-08-26 21:07:26 +0000 | [diff] [blame] | 3858 | if( MEMDB ){ |
| 3859 | rc = SQLITE_OK; |
| 3860 | }else{ |
| 3861 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
| 3862 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3863 | return rc; |
| 3864 | } |
| 3865 | |
| 3866 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3867 | ** Sync the database file for the pager pPager. zMaster points to the name |
| 3868 | ** of a master journal file that should be written into the individual |
| 3869 | ** journal file. zMaster may be NULL, which is interpreted as no master |
| 3870 | ** journal (a single database transaction). |
| 3871 | ** |
| 3872 | ** This routine ensures that the journal is synced, all dirty pages written |
| 3873 | ** to the database file and the database file synced. The only thing that |
| 3874 | ** remains to commit the transaction is to delete the journal file (or |
| 3875 | ** master journal file if specified). |
| 3876 | ** |
| 3877 | ** Note that if zMaster==NULL, this does not overwrite a previous value |
| 3878 | ** passed to an sqlite3PagerCommitPhaseOne() call. |
| 3879 | ** |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3880 | ** If the final parameter - noSync - is true, then the database file itself |
| 3881 | ** is not synced. The caller must call sqlite3PagerSync() directly to |
| 3882 | ** sync the database file before calling CommitPhaseTwo() to delete the |
| 3883 | ** journal file in this case. |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3884 | */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3885 | int sqlite3PagerCommitPhaseOne( |
| 3886 | Pager *pPager, |
| 3887 | const char *zMaster, |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3888 | int noSync |
| 3889 | ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3890 | int rc = SQLITE_OK; |
| 3891 | |
danielk1977 | dad31b5 | 2008-05-15 11:08:07 +0000 | [diff] [blame] | 3892 | if( pPager->errCode ){ |
| 3893 | return pPager->errCode; |
| 3894 | } |
| 3895 | |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3896 | /* If no changes have been made, we can leave the transaction early. |
| 3897 | */ |
| 3898 | if( pPager->dbModified==0 && |
| 3899 | (pPager->journalMode!=PAGER_JOURNALMODE_DELETE || |
| 3900 | pPager->exclusiveMode!=0) ){ |
| 3901 | assert( pPager->dirtyCache==0 || pPager->journalOpen==0 ); |
| 3902 | return SQLITE_OK; |
| 3903 | } |
| 3904 | |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 3905 | PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", |
| 3906 | pPager->zFilename, zMaster, pPager->dbSize)); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3907 | |
| 3908 | /* If this is an in-memory db, or no pages have been written to, or this |
| 3909 | ** function has already been called, it is a no-op. |
| 3910 | */ |
| 3911 | if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){ |
| 3912 | PgHdr *pPg; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3913 | |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3914 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 3915 | /* The atomic-write optimization can be used if all of the |
| 3916 | ** following are true: |
| 3917 | ** |
| 3918 | ** + The file-system supports the atomic-write property for |
| 3919 | ** blocks of size page-size, and |
| 3920 | ** + This commit is not part of a multi-file transaction, and |
| 3921 | ** + Exactly one page has been modified and store in the journal file. |
| 3922 | ** |
| 3923 | ** If the optimization can be used, then the journal file will never |
| 3924 | ** be created for this transaction. |
| 3925 | */ |
danielk1977 | 4d60af9 | 2008-09-08 15:35:06 +0000 | [diff] [blame] | 3926 | int useAtomicWrite; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3927 | pPg = sqlite3PcacheDirtyList(pPager->pPCache); |
danielk1977 | 4d60af9 | 2008-09-08 15:35:06 +0000 | [diff] [blame] | 3928 | useAtomicWrite = ( |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3929 | !zMaster && |
danielk1977 | 700b9c5 | 2008-04-24 12:37:40 +0000 | [diff] [blame] | 3930 | pPager->journalOpen && |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3931 | pPager->journalOff==jrnlBufferSize(pPager) && |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3932 | pPager->dbSize>=pPager->dbFileSize && |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3933 | (pPg==0 || pPg->pDirty==0) |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3934 | ); |
danielk1977 | 700b9c5 | 2008-04-24 12:37:40 +0000 | [diff] [blame] | 3935 | assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF ); |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3936 | if( useAtomicWrite ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3937 | /* Update the nRec field in the journal file. */ |
| 3938 | int offset = pPager->journalHdr + sizeof(aJournalMagic); |
| 3939 | assert(pPager->nRec==1); |
| 3940 | rc = write32bits(pPager->jfd, offset, pPager->nRec); |
| 3941 | |
| 3942 | /* Update the db file change counter. The following call will modify |
| 3943 | ** the in-memory representation of page 1 to include the updated |
| 3944 | ** change counter and then write page 1 directly to the database |
| 3945 | ** file. Because of the atomic-write property of the host file-system, |
| 3946 | ** this is safe. |
| 3947 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3948 | if( rc==SQLITE_OK ){ |
| 3949 | rc = pager_incr_changecounter(pPager, 1); |
| 3950 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3951 | }else{ |
| 3952 | rc = sqlite3JournalCreate(pPager->jfd); |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3955 | if( !useAtomicWrite && rc==SQLITE_OK ) |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3956 | #endif |
| 3957 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3958 | /* If a master journal file name has already been written to the |
| 3959 | ** journal file, then no sync is required. This happens when it is |
| 3960 | ** written, then the process fails to upgrade from a RESERVED to an |
| 3961 | ** EXCLUSIVE lock. The next time the process tries to commit the |
| 3962 | ** transaction the m-j name will have already been written. |
| 3963 | */ |
| 3964 | if( !pPager->setMaster ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3965 | rc = pager_incr_changecounter(pPager, 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3966 | if( rc!=SQLITE_OK ) goto sync_exit; |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 3967 | if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3968 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3969 | if( pPager->dbSize<pPager->dbOrigSize ){ |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 3970 | /* If this transaction has made the database smaller, then all pages |
| 3971 | ** being discarded by the truncation must be written to the journal |
| 3972 | ** file. |
| 3973 | */ |
| 3974 | Pgno i; |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 3975 | Pgno iSkip = PAGER_MJ_PGNO(pPager); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3976 | Pgno dbSize = pPager->dbSize; |
danielk1977 | f9bce3c | 2009-01-06 15:20:58 +0000 | [diff] [blame] | 3977 | pPager->dbSize = pPager->dbOrigSize; |
danielk1977 | f70c1fe | 2009-01-07 18:08:48 +0000 | [diff] [blame] | 3978 | for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){ |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 3979 | if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){ |
| 3980 | rc = sqlite3PagerGet(pPager, i, &pPg); |
| 3981 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 3982 | rc = sqlite3PagerWrite(pPg); |
| 3983 | sqlite3PagerUnref(pPg); |
| 3984 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 3985 | } |
| 3986 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3987 | pPager->dbSize = dbSize; |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 3988 | } |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3989 | #endif |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 3990 | rc = writeMasterJournal(pPager, zMaster); |
| 3991 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 3992 | rc = syncJournal(pPager); |
| 3993 | } |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3994 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3995 | if( rc!=SQLITE_OK ) goto sync_exit; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3996 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3997 | /* Write all dirty pages to the database file */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3998 | pPg = sqlite3PcacheDirtyList(pPager->pPCache); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3999 | rc = pager_write_pagelist(pPg); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4000 | if( rc!=SQLITE_OK ){ |
drh | 04c3a46 | 2008-02-26 16:16:45 +0000 | [diff] [blame] | 4001 | assert( rc!=SQLITE_IOERR_BLOCKED ); |
| 4002 | /* The error might have left the dirty list all fouled up here, |
| 4003 | ** but that does not matter because if the if the dirty list did |
| 4004 | ** get corrupted, then the transaction will roll back and |
| 4005 | ** discard the dirty list. There is an assert in |
| 4006 | ** pager_get_all_dirty_pages() that verifies that no attempt |
| 4007 | ** is made to use an invalid dirty list. |
| 4008 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4009 | goto sync_exit; |
| 4010 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4011 | sqlite3PcacheCleanAll(pPager->pPCache); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4012 | |
danielk1977 | f90b726 | 2009-01-07 15:18:20 +0000 | [diff] [blame] | 4013 | if( pPager->dbSize<pPager->dbFileSize ){ |
| 4014 | assert( pPager->state>=PAGER_EXCLUSIVE ); |
| 4015 | rc = pager_truncate(pPager, pPager->dbSize); |
| 4016 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4017 | } |
| 4018 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4019 | /* Sync the database file. */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 4020 | if( !pPager->noSync && !noSync ){ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 4021 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4022 | } |
| 4023 | IOTRACE(("DBSYNC %p\n", pPager)) |
| 4024 | |
| 4025 | pPager->state = PAGER_SYNCED; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | sync_exit: |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 4029 | if( rc==SQLITE_IOERR_BLOCKED ){ |
| 4030 | /* pager_incr_changecounter() may attempt to obtain an exclusive |
| 4031 | * lock to spill the cache and return IOERR_BLOCKED. But since |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 4032 | * there is no chance the cache is inconsistent, it is |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 4033 | * better to return SQLITE_BUSY. |
| 4034 | */ |
| 4035 | rc = SQLITE_BUSY; |
| 4036 | } |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4037 | return rc; |
| 4038 | } |
| 4039 | |
| 4040 | |
| 4041 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4042 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4043 | ** |
| 4044 | ** If the commit fails for any reason, a rollback attempt is made |
| 4045 | ** and an error code is returned. If the commit worked, SQLITE_OK |
| 4046 | ** is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4047 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4048 | int sqlite3PagerCommitPhaseTwo(Pager *pPager){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4049 | int rc = SQLITE_OK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4050 | |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4051 | if( pPager->errCode ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 4052 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4053 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4054 | if( pPager->state<PAGER_RESERVED ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4055 | return SQLITE_ERROR; |
| 4056 | } |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 4057 | if( pPager->dbModified==0 && |
| 4058 | (pPager->journalMode!=PAGER_JOURNALMODE_DELETE || |
| 4059 | pPager->exclusiveMode!=0) ){ |
| 4060 | assert( pPager->dirtyCache==0 || pPager->journalOpen==0 ); |
| 4061 | return SQLITE_OK; |
| 4062 | } |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 4063 | PAGERTRACE(("COMMIT %d\n", PAGERID(pPager))); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4064 | assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dirtyCache ); |
| 4065 | rc = pager_end_transaction(pPager, pPager->setMaster); |
| 4066 | rc = pager_error(pPager, rc); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4067 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4071 | ** Rollback all changes. The database falls back to PAGER_SHARED mode. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4072 | ** All in-memory cache pages revert to their original data contents. |
| 4073 | ** The journal is deleted. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4074 | ** |
| 4075 | ** This routine cannot fail unless some other process is not following |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 4076 | ** the correct locking protocol or unless some other |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4077 | ** process is writing trash into the journal file (SQLITE_CORRUPT) or |
| 4078 | ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error |
| 4079 | ** codes are returned for all these occasions. Otherwise, |
| 4080 | ** SQLITE_OK is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4081 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4082 | int sqlite3PagerRollback(Pager *pPager){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4083 | int rc = SQLITE_OK; |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 4084 | PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager))); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4085 | if( !pPager->dirtyCache || !pPager->journalOpen ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 4086 | rc = pager_end_transaction(pPager, pPager->setMaster); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4087 | }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4088 | if( pPager->state>=PAGER_EXCLUSIVE ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 4089 | pager_playback(pPager, 0); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4090 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4091 | rc = pPager->errCode; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4092 | }else{ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4093 | if( pPager->state==PAGER_RESERVED ){ |
| 4094 | int rc2; |
| 4095 | rc = pager_playback(pPager, 0); |
| 4096 | rc2 = pager_end_transaction(pPager, pPager->setMaster); |
| 4097 | if( rc==SQLITE_OK ){ |
| 4098 | rc = rc2; |
| 4099 | } |
| 4100 | }else{ |
| 4101 | rc = pager_playback(pPager, 0); |
| 4102 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4103 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4104 | if( !MEMDB ){ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 4105 | pPager->dbSizeValid = 0; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4106 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4107 | |
| 4108 | /* If an error occurs during a ROLLBACK, we can no longer trust the pager |
| 4109 | ** cache. So call pager_error() on the way out to make any error |
| 4110 | ** persistent. |
| 4111 | */ |
| 4112 | rc = pager_error(pPager, rc); |
| 4113 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4114 | return rc; |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 4115 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4116 | |
| 4117 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4118 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 4119 | ** if the database is (in theory) writable. |
| 4120 | */ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4121 | u8 sqlite3PagerIsreadonly(Pager *pPager){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 4122 | return pPager->readOnly; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | /* |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4126 | ** Return the number of references to the pager. |
| 4127 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4128 | int sqlite3PagerRefcount(Pager *pPager){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4129 | return sqlite3PcacheRefCount(pPager->pPCache); |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4132 | /* |
| 4133 | ** Return the number of references to the specified page. |
| 4134 | */ |
| 4135 | int sqlite3PagerPageRefcount(DbPage *pPage){ |
| 4136 | return sqlite3PcachePageRefcount(pPage); |
| 4137 | } |
| 4138 | |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4139 | #ifdef SQLITE_TEST |
| 4140 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4141 | ** This routine is used for testing and analysis only. |
| 4142 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4143 | int *sqlite3PagerStats(Pager *pPager){ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 4144 | static int a[11]; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4145 | a[0] = sqlite3PcacheRefCount(pPager->pPCache); |
| 4146 | a[1] = sqlite3PcachePagecount(pPager->pPCache); |
| 4147 | a[2] = sqlite3PcacheGetCachesize(pPager->pPCache); |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 4148 | a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4149 | a[4] = pPager->state; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4150 | a[5] = pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4151 | a[6] = pPager->nHit; |
| 4152 | a[7] = pPager->nMiss; |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4153 | a[8] = 0; /* Used to be pPager->nOvfl */ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 4154 | a[9] = pPager->nRead; |
| 4155 | a[10] = pPager->nWrite; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4156 | return a; |
| 4157 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4158 | int sqlite3PagerIsMemdb(Pager *pPager){ |
| 4159 | return MEMDB; |
| 4160 | } |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4161 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4162 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4163 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4164 | ** Ensure that there are at least nSavepoint savepoints open. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4165 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4166 | int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ |
| 4167 | int rc = SQLITE_OK; |
| 4168 | |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 4169 | if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4170 | int ii; |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame] | 4171 | PagerSavepoint *aNew; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4172 | |
drh | c0731c9 | 2009-01-11 17:00:02 +0000 | [diff] [blame] | 4173 | /* Either there is no active journal or the sub-journal is open or |
| 4174 | ** the journal is always stored in memory */ |
| 4175 | assert( pPager->nSavepoint==0 || pPager->sjfd->pMethods || |
| 4176 | pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4177 | |
| 4178 | /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM |
| 4179 | ** if the allocation fails. Otherwise, zero the new portion in case a |
| 4180 | ** malloc failure occurs while populating it in the for(...) loop below. |
| 4181 | */ |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame] | 4182 | aNew = (PagerSavepoint *)sqlite3Realloc( |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4183 | pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint |
| 4184 | ); |
| 4185 | if( !aNew ){ |
| 4186 | return SQLITE_NOMEM; |
| 4187 | } |
| 4188 | memset(&aNew[pPager->nSavepoint], 0, |
| 4189 | (nSavepoint - pPager->nSavepoint) * sizeof(PagerSavepoint) |
| 4190 | ); |
| 4191 | pPager->aSavepoint = aNew; |
| 4192 | ii = pPager->nSavepoint; |
| 4193 | pPager->nSavepoint = nSavepoint; |
| 4194 | |
| 4195 | /* Populate the PagerSavepoint structures just allocated. */ |
| 4196 | for(/* no-op */; ii<nSavepoint; ii++){ |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 4197 | assert( pPager->dbSizeValid ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4198 | aNew[ii].nOrig = pPager->dbSize; |
danielk1977 | 67ddef6 | 2008-12-23 19:15:56 +0000 | [diff] [blame] | 4199 | if( pPager->journalOpen && pPager->journalOff>0 ){ |
| 4200 | aNew[ii].iOffset = pPager->journalOff; |
| 4201 | }else{ |
| 4202 | aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); |
| 4203 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4204 | aNew[ii].iSubRec = pPager->stmtNRec; |
| 4205 | aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); |
| 4206 | if( !aNew[ii].pInSavepoint ){ |
| 4207 | return SQLITE_NOMEM; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4208 | } |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 4209 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4210 | |
| 4211 | /* Open the sub-journal, if it is not already opened. */ |
| 4212 | rc = openSubJournal(pPager); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 4213 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4214 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4215 | return rc; |
| 4216 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4217 | |
| 4218 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4219 | ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE. |
| 4220 | ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with |
| 4221 | ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes |
| 4222 | ** that have occured since savepoint iSavepoint was created. |
| 4223 | ** |
| 4224 | ** In either case, all savepoints with an index greater than iSavepoint |
| 4225 | ** are destroyed. |
| 4226 | ** |
| 4227 | ** If there are less than (iSavepoint+1) active savepoints when this |
| 4228 | ** function is called it is a no-op. |
| 4229 | */ |
| 4230 | int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ |
| 4231 | int rc = SQLITE_OK; |
| 4232 | |
| 4233 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 4234 | |
| 4235 | if( iSavepoint<pPager->nSavepoint ){ |
| 4236 | int ii; |
| 4237 | int nNew = iSavepoint + (op==SAVEPOINT_ROLLBACK); |
| 4238 | for(ii=nNew; ii<pPager->nSavepoint; ii++){ |
| 4239 | sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); |
| 4240 | } |
| 4241 | pPager->nSavepoint = nNew; |
| 4242 | |
danielk1977 | 8a7adb0 | 2008-12-22 11:43:35 +0000 | [diff] [blame] | 4243 | if( op==SAVEPOINT_ROLLBACK && pPager->jfd->pMethods ){ |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 4244 | PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1]; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4245 | rc = pagerPlaybackSavepoint(pPager, pSavepoint); |
| 4246 | assert(rc!=SQLITE_DONE); |
| 4247 | } |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 4248 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4249 | /* If this is a release of the outermost savepoint, truncate |
| 4250 | ** the sub-journal. */ |
| 4251 | if( nNew==0 && op==SAVEPOINT_RELEASE && pPager->sjfd->pMethods ){ |
| 4252 | assert( rc==SQLITE_OK ); |
| 4253 | rc = sqlite3OsTruncate(pPager->sjfd, 0); |
| 4254 | pPager->stmtNRec = 0; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4255 | } |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 4256 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4257 | return rc; |
| 4258 | } |
| 4259 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4260 | /* |
| 4261 | ** Return the full pathname of the database file. |
| 4262 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4263 | const char *sqlite3PagerFilename(Pager *pPager){ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4264 | return pPager->zFilename; |
| 4265 | } |
| 4266 | |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4267 | /* |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 4268 | ** Return the VFS structure for the pager. |
| 4269 | */ |
| 4270 | const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ |
| 4271 | return pPager->pVfs; |
| 4272 | } |
| 4273 | |
| 4274 | /* |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 4275 | ** Return the file handle for the database file associated |
| 4276 | ** with the pager. This might return NULL if the file has |
| 4277 | ** not yet been opened. |
| 4278 | */ |
| 4279 | sqlite3_file *sqlite3PagerFile(Pager *pPager){ |
| 4280 | return pPager->fd; |
| 4281 | } |
| 4282 | |
| 4283 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4284 | ** Return the directory of the database file. |
| 4285 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4286 | const char *sqlite3PagerDirname(Pager *pPager){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4287 | return pPager->zDirectory; |
| 4288 | } |
| 4289 | |
| 4290 | /* |
| 4291 | ** Return the full pathname of the journal file. |
| 4292 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4293 | const char *sqlite3PagerJournalname(Pager *pPager){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4294 | return pPager->zJournal; |
| 4295 | } |
| 4296 | |
| 4297 | /* |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4298 | ** Return true if fsync() calls are disabled for this pager. Return FALSE |
| 4299 | ** if fsync()s are executed normally. |
| 4300 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4301 | int sqlite3PagerNosync(Pager *pPager){ |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4302 | return pPager->noSync; |
| 4303 | } |
| 4304 | |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4305 | #ifdef SQLITE_HAS_CODEC |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4306 | /* |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4307 | ** Set the codec for this pager |
| 4308 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4309 | void sqlite3PagerSetCodec( |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4310 | Pager *pPager, |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4311 | void *(*xCodec)(void*,void*,Pgno,int), |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4312 | void *pCodecArg |
| 4313 | ){ |
| 4314 | pPager->xCodec = xCodec; |
| 4315 | pPager->pCodecArg = pCodecArg; |
| 4316 | } |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4317 | #endif |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4318 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4319 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4320 | /* |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4321 | ** Move the page pPg to location pgno in the file. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4322 | ** |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4323 | ** There must be no references to the page previously located at |
| 4324 | ** pgno (which we call pPgOld) though that page is allowed to be |
drh | b3df2e1 | 2008-09-17 20:06:26 +0000 | [diff] [blame] | 4325 | ** in cache. If the page previously located at pgno is not already |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4326 | ** in the rollback journal, it is not put there by by this routine. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4327 | ** |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4328 | ** References to the page pPg remain valid. Updating any |
| 4329 | ** meta-data associated with pPg (i.e. data stored in the nExtra bytes |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4330 | ** allocated along with the page) is the responsibility of the caller. |
| 4331 | ** |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 4332 | ** A transaction must be active when this routine is called. It used to be |
| 4333 | ** required that a statement transaction was not active, but this restriction |
| 4334 | ** has been removed (CREATE INDEX needs to move a page when a statement |
| 4335 | ** transaction is active). |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 4336 | ** |
| 4337 | ** If the fourth argument, isCommit, is non-zero, then this page is being |
| 4338 | ** moved as part of a database reorganization just before the transaction |
| 4339 | ** is being committed. In this case, it is guaranteed that the database page |
| 4340 | ** pPg refers to will not be written to again within this transaction. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4341 | */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 4342 | int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){ |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4343 | PgHdr *pPgOld; /* The page being overwritten. */ |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4344 | Pgno needSyncPgno = 0; |
danielk1977 | 1fab7b6 | 2009-01-07 10:35:18 +0000 | [diff] [blame] | 4345 | int rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4346 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4347 | assert( pPg->nRef>0 ); |
| 4348 | |
danielk1977 | 1fab7b6 | 2009-01-07 10:35:18 +0000 | [diff] [blame] | 4349 | /* If the page being moved is dirty and has not been saved by the latest |
| 4350 | ** savepoint, then save the current contents of the page into the |
| 4351 | ** sub-journal now. This is required to handle the following scenario: |
| 4352 | ** |
| 4353 | ** BEGIN; |
| 4354 | ** <journal page X, then modify it in memory> |
| 4355 | ** SAVEPOINT one; |
| 4356 | ** <Move page X to location Y> |
| 4357 | ** ROLLBACK TO one; |
| 4358 | ** |
| 4359 | ** If page X were not written to the sub-journal here, it would not |
| 4360 | ** be possible to restore its contents when the "ROLLBACK TO one" |
| 4361 | ** statement were processed. |
| 4362 | */ |
| 4363 | if( pPg->flags&PGHDR_DIRTY |
| 4364 | && subjRequiresPage(pPg) |
| 4365 | && SQLITE_OK!=(rc = subjournalPage(pPg)) |
| 4366 | ){ |
| 4367 | return rc; |
| 4368 | } |
| 4369 | |
drh | 30d5370 | 2009-01-06 15:58:57 +0000 | [diff] [blame] | 4370 | PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", |
| 4371 | PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno)); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 4372 | IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno)) |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 4373 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 4374 | pager_get_content(pPg); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 4375 | |
| 4376 | /* If the journal needs to be sync()ed before page pPg->pgno can |
| 4377 | ** be written to, store pPg->pgno in local variable needSyncPgno. |
| 4378 | ** |
| 4379 | ** If the isCommit flag is set, there is no need to remember that |
| 4380 | ** the journal needs to be sync()ed before database page pPg->pgno |
| 4381 | ** can be written to. The caller has already promised not to write to it. |
| 4382 | */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4383 | if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){ |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4384 | needSyncPgno = pPg->pgno; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 4385 | assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4386 | assert( pPg->flags&PGHDR_DIRTY ); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4387 | assert( pPager->needSync ); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 4390 | /* If the cache contains a page with page-number pgno, remove it |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 4391 | ** from its hash chain. Also, if the PgHdr.needSync was set for |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4392 | ** page pgno before the 'move' operation, it needs to be retained |
| 4393 | ** for the page moved there. |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4394 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 4395 | pPg->flags &= ~PGHDR_NEED_SYNC; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4396 | pPgOld = pager_lookup(pPager, pgno); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4397 | assert( !pPgOld || pPgOld->nRef==1 ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4398 | if( pPgOld ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4399 | pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4400 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4401 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4402 | sqlite3PcacheMove(pPg, pgno); |
| 4403 | if( pPgOld ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 4404 | sqlite3PcacheDrop(pPgOld); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4405 | } |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4406 | |
drh | c047b9f | 2008-11-21 03:23:43 +0000 | [diff] [blame] | 4407 | sqlite3PcacheMakeDirty(pPg); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4408 | pPager->dirtyCache = 1; |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 4409 | pPager->dbModified = 1; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4410 | |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4411 | if( needSyncPgno ){ |
| 4412 | /* If needSyncPgno is non-zero, then the journal file needs to be |
| 4413 | ** sync()ed before any data is written to database file page needSyncPgno. |
| 4414 | ** Currently, no such page exists in the page-cache and the |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 4415 | ** "is journaled" bitvec flag has been set. This needs to be remedied by |
| 4416 | ** loading the page into the pager-cache and setting the PgHdr.needSync |
| 4417 | ** flag. |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4418 | ** |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 4419 | ** If the attempt to load the page into the page-cache fails, (due |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4420 | ** to a malloc() or IO failure), clear the bit in the pInJournal[] |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 4421 | ** array. Otherwise, if the page is loaded and written again in |
| 4422 | ** this transaction, it may be written to the database file before |
| 4423 | ** it is synced into the journal file. This way, it may end up in |
| 4424 | ** the journal file twice, but that is not a problem. |
| 4425 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4426 | ** The sqlite3PagerGet() call may cause the journal to sync. So make |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4427 | ** sure the Pager.needSync flag is set too. |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4428 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4429 | PgHdr *pPgHdr; |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4430 | assert( pPager->needSync ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4431 | rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4432 | if( rc!=SQLITE_OK ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 4433 | if( pPager->pInJournal && needSyncPgno<=pPager->dbOrigSize ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4434 | sqlite3BitvecClear(pPager->pInJournal, needSyncPgno); |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 4435 | } |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4436 | return rc; |
| 4437 | } |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4438 | pPager->needSync = 1; |
drh | b3df2e1 | 2008-09-17 20:06:26 +0000 | [diff] [blame] | 4439 | assert( pPager->noSync==0 && !MEMDB ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4440 | pPgHdr->flags |= PGHDR_NEED_SYNC; |
drh | c047b9f | 2008-11-21 03:23:43 +0000 | [diff] [blame] | 4441 | sqlite3PcacheMakeDirty(pPgHdr); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4442 | sqlite3PagerUnref(pPgHdr); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4445 | return SQLITE_OK; |
| 4446 | } |
| 4447 | #endif |
| 4448 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4449 | /* |
| 4450 | ** Return a pointer to the data for the specified page. |
| 4451 | */ |
| 4452 | void *sqlite3PagerGetData(DbPage *pPg){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4453 | assert( pPg->nRef>0 || pPg->pPager->memDb ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4454 | return pPg->pData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | /* |
| 4458 | ** Return a pointer to the Pager.nExtra bytes of "extra" space |
| 4459 | ** allocated along with the specified page. |
| 4460 | */ |
| 4461 | void *sqlite3PagerGetExtra(DbPage *pPg){ |
| 4462 | Pager *pPager = pPg->pPager; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4463 | return (pPager?pPg->pExtra:0); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 4466 | /* |
| 4467 | ** Get/set the locking-mode for this pager. Parameter eMode must be one |
| 4468 | ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or |
| 4469 | ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then |
| 4470 | ** the locking-mode is set to the value specified. |
| 4471 | ** |
| 4472 | ** The returned value is either PAGER_LOCKINGMODE_NORMAL or |
| 4473 | ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated) |
| 4474 | ** locking-mode. |
| 4475 | */ |
| 4476 | int sqlite3PagerLockingMode(Pager *pPager, int eMode){ |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 4477 | assert( eMode==PAGER_LOCKINGMODE_QUERY |
| 4478 | || eMode==PAGER_LOCKINGMODE_NORMAL |
| 4479 | || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 4480 | assert( PAGER_LOCKINGMODE_QUERY<0 ); |
| 4481 | assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 ); |
| 4482 | if( eMode>=0 && !pPager->tempFile ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4483 | pPager->exclusiveMode = (u8)eMode; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 4484 | } |
| 4485 | return (int)pPager->exclusiveMode; |
| 4486 | } |
| 4487 | |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 4488 | /* |
drh | 0433588 | 2008-09-26 21:08:08 +0000 | [diff] [blame] | 4489 | ** Get/set the journal-mode for this pager. Parameter eMode must be one of: |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 4490 | ** |
drh | 0433588 | 2008-09-26 21:08:08 +0000 | [diff] [blame] | 4491 | ** PAGER_JOURNALMODE_QUERY |
| 4492 | ** PAGER_JOURNALMODE_DELETE |
| 4493 | ** PAGER_JOURNALMODE_TRUNCATE |
| 4494 | ** PAGER_JOURNALMODE_PERSIST |
| 4495 | ** PAGER_JOURNALMODE_OFF |
| 4496 | ** |
| 4497 | ** If the parameter is not _QUERY, then the journal-mode is set to the |
| 4498 | ** value specified. |
| 4499 | ** |
| 4500 | ** The returned indicate the current (possibly updated) |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 4501 | ** journal-mode. |
| 4502 | */ |
| 4503 | int sqlite3PagerJournalMode(Pager *pPager, int eMode){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4504 | if( !MEMDB ){ |
| 4505 | assert( eMode==PAGER_JOURNALMODE_QUERY |
| 4506 | || eMode==PAGER_JOURNALMODE_DELETE |
| 4507 | || eMode==PAGER_JOURNALMODE_TRUNCATE |
| 4508 | || eMode==PAGER_JOURNALMODE_PERSIST |
| 4509 | || eMode==PAGER_JOURNALMODE_OFF |
| 4510 | || eMode==PAGER_JOURNALMODE_MEMORY ); |
| 4511 | assert( PAGER_JOURNALMODE_QUERY<0 ); |
| 4512 | if( eMode>=0 ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4513 | pPager->journalMode = (u8)eMode; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4514 | }else{ |
| 4515 | assert( eMode==PAGER_JOURNALMODE_QUERY ); |
| 4516 | } |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 4517 | } |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 4518 | return (int)pPager->journalMode; |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 4521 | /* |
| 4522 | ** Get/set the size-limit used for persistent journal files. |
| 4523 | */ |
| 4524 | i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){ |
| 4525 | if( iLimit>=-1 ){ |
| 4526 | pPager->journalSizeLimit = iLimit; |
| 4527 | } |
| 4528 | return pPager->journalSizeLimit; |
| 4529 | } |
| 4530 | |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 4531 | #endif /* SQLITE_OMIT_DISKIO */ |