drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 12 | ** This is the implementation of the page cache subsystem or "pager". |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 13 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 14 | ** The pager is used to access a database disk file. It implements |
| 15 | ** atomic commit and rollback through the use of a journal file that |
| 16 | ** is separate from the database file. The pager also implements file |
| 17 | ** locking to prevent two processes from writing the same database |
| 18 | ** file simultaneously, or one process from reading the database while |
| 19 | ** another is writing. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 20 | ** |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame^] | 21 | ** @(#) $Id: pager.c,v 1.526 2009/01/02 18:10:42 drh 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 |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 30 | #define sqlite3DebugPrintf printf |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 31 | #define PAGERTRACE1(X) sqlite3DebugPrintf(X) |
| 32 | #define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y) |
| 33 | #define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z) |
| 34 | #define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W) |
| 35 | #define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 36 | #else |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 37 | #define PAGERTRACE1(X) |
| 38 | #define PAGERTRACE2(X,Y) |
| 39 | #define PAGERTRACE3(X,Y,Z) |
| 40 | #define PAGERTRACE4(X,Y,Z,W) |
| 41 | #define PAGERTRACE5(X,Y,Z,W,V) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 44 | /* |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 45 | ** The following two macros are used within the PAGERTRACEX() macros above |
drh | d86959f | 2005-11-26 03:51:18 +0000 | [diff] [blame] | 46 | ** to print out file-descriptors. |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 47 | ** |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 48 | ** PAGERID() takes a pointer to a Pager struct as its argument. The |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 49 | ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 50 | ** struct as its argument. |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 51 | */ |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 52 | #define PAGERID(p) ((int)(p->fd)) |
| 53 | #define FILEHANDLEID(fd) ((int)fd) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 54 | |
| 55 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 56 | ** The page cache as a whole is always in one of the following |
| 57 | ** states: |
| 58 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 59 | ** PAGER_UNLOCK The page cache is not currently reading or |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 60 | ** writing the database file. There is no |
| 61 | ** data held in memory. This is the initial |
| 62 | ** state. |
| 63 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 64 | ** PAGER_SHARED The page cache is reading the database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 65 | ** Writing is not permitted. There can be |
| 66 | ** multiple readers accessing the same database |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 67 | ** file at the same time. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 68 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 69 | ** PAGER_RESERVED This process has reserved the database for writing |
| 70 | ** but has not yet made any changes. Only one process |
| 71 | ** at a time can reserve the database. The original |
| 72 | ** database file has not been modified so other |
| 73 | ** processes may still be reading the on-disk |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 74 | ** database file. |
| 75 | ** |
| 76 | ** PAGER_EXCLUSIVE The page cache is writing the database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 77 | ** Access is exclusive. No other processes or |
| 78 | ** threads can be reading or writing while one |
| 79 | ** process is writing. |
| 80 | ** |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 81 | ** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE |
| 82 | ** after all dirty pages have been written to the |
| 83 | ** database file and the file has been synced to |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 84 | ** disk. All that remains to do is to remove or |
| 85 | ** truncate the journal file and the transaction |
| 86 | ** will be committed. |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 87 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 88 | ** The page cache comes up in PAGER_UNLOCK. The first time a |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 89 | ** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 90 | ** After all pages have been released using sqlite_page_unref(), |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 91 | ** the state transitions back to PAGER_UNLOCK. The first time |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 92 | ** that sqlite3PagerWrite() is called, the state transitions to |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 93 | ** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 94 | ** called on an outstanding page which means that the pager must |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 95 | ** be in PAGER_SHARED before it transitions to PAGER_RESERVED.) |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 96 | ** PAGER_RESERVED means that there is an open rollback journal. |
| 97 | ** The transition to PAGER_EXCLUSIVE occurs before any changes |
| 98 | ** are made to the database file, though writes to the rollback |
| 99 | ** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback() |
| 100 | ** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED, |
| 101 | ** 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] | 102 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 103 | #define PAGER_UNLOCK 0 |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 104 | #define PAGER_SHARED 1 /* same as SHARED_LOCK */ |
| 105 | #define PAGER_RESERVED 2 /* same as RESERVED_LOCK */ |
| 106 | #define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */ |
| 107 | #define PAGER_SYNCED 5 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 108 | |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 109 | /* |
| 110 | ** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time, |
| 111 | ** then failed attempts to get a reserved lock will invoke the busy callback. |
| 112 | ** This is off by default. To see why, consider the following scenario: |
| 113 | ** |
| 114 | ** Suppose thread A already has a shared lock and wants a reserved lock. |
| 115 | ** Thread B already has a reserved lock and wants an exclusive lock. If |
| 116 | ** both threads are using their busy callbacks, it might be a long time |
| 117 | ** be for one of the threads give up and allows the other to proceed. |
| 118 | ** But if the thread trying to get the reserved lock gives up quickly |
| 119 | ** (if it never invokes its busy callback) then the contention will be |
| 120 | ** resolved quickly. |
| 121 | */ |
| 122 | #ifndef SQLITE_BUSY_RESERVED_LOCK |
| 123 | # define SQLITE_BUSY_RESERVED_LOCK 0 |
| 124 | #endif |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 125 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 126 | /* |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 127 | ** This macro rounds values up so that if the value is an address it |
| 128 | ** is guaranteed to be an address that is aligned to an 8-byte boundary. |
| 129 | */ |
| 130 | #define FORCE_ALIGNMENT(X) (((X)+7)&~7) |
| 131 | |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 132 | /* |
| 133 | ** A macro used for invoking the codec if there is one |
| 134 | */ |
| 135 | #ifdef SQLITE_HAS_CODEC |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 136 | # define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); } |
| 137 | # 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] | 138 | #else |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 139 | # define CODEC1(P,D,N,X) /* NO-OP */ |
| 140 | # define CODEC2(P,D,N,X) ((char*)D) |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 143 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 144 | ** An instance of the following structure is allocated for each active |
| 145 | ** savepoint and statement transaction in the system. All such structures |
| 146 | ** are stored in the Pager.aSavepoint[] array, which is allocated and |
| 147 | ** resized using sqlite3Realloc(). |
| 148 | ** |
| 149 | ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is |
| 150 | ** set to 0. If a journal-header is written into the main journal while |
| 151 | ** the savepoint is active, then iHdrOffset is set to the byte offset |
| 152 | ** immediately following the last journal record written into the main |
| 153 | ** journal before the journal-header. This is required during savepoint |
| 154 | ** rollback (see pagerPlaybackSavepoint()). |
| 155 | */ |
| 156 | typedef struct PagerSavepoint PagerSavepoint; |
| 157 | struct PagerSavepoint { |
| 158 | i64 iOffset; /* Starting offset in main journal */ |
| 159 | i64 iHdrOffset; /* See above */ |
| 160 | Bitvec *pInSavepoint; /* Set of pages in this savepoint */ |
| 161 | Pgno nOrig; /* Original number of pages in file */ |
| 162 | Pgno iSubRec; /* Index of first record in sub-journal */ |
| 163 | }; |
| 164 | |
| 165 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 166 | ** A open page cache is an instance of the following structure. |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 167 | ** |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 168 | ** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 169 | ** or SQLITE_FULL. Once one of the first three errors occurs, it persists |
| 170 | ** and is returned as the result of every major pager API call. The |
| 171 | ** SQLITE_FULL return code is slightly different. It persists only until the |
| 172 | ** next successful rollback is performed on the pager cache. Also, |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 173 | ** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup() |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 174 | ** APIs, they may still be used successfully. |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 175 | ** |
| 176 | ** Managing the size of the database file in pages is a little complicated. |
| 177 | ** The variable Pager.dbSize contains the number of pages that the database |
| 178 | ** image currently contains. As the database image grows or shrinks this |
| 179 | ** variable is updated. The variable Pager.dbFileSize contains the number |
| 180 | ** of pages in the database file. This may be different from Pager.dbSize |
| 181 | ** if some pages have been appended to the database image but not yet written |
| 182 | ** out from the cache to the actual file on disk. Or if the image has been |
| 183 | ** truncated by an incremental-vacuum operation. The Pager.dbOrigSize variable |
| 184 | ** contains the number of pages in the database image when the current |
| 185 | ** transaction was opened. The contents of all three of these variables is |
| 186 | ** only guaranteed to be correct if the boolean Pager.dbSizeValid is true. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 187 | */ |
| 188 | struct Pager { |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 189 | sqlite3_vfs *pVfs; /* OS functions to use for IO */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 190 | u8 journalOpen; /* True if journal file descriptors is valid */ |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 191 | u8 journalStarted; /* True if header of journal is synced */ |
| 192 | u8 useJournal; /* Use a rollback journal on this file */ |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 193 | u8 noReadlock; /* Do not bother to obtain readlocks */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 194 | u8 noSync; /* Do not sync the journal if true */ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 195 | u8 fullSync; /* Do extra syncs of the journal for robustness */ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 196 | u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 197 | u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 198 | u8 tempFile; /* zFilename is a temporary file */ |
| 199 | u8 readOnly; /* True for a read-only database */ |
| 200 | u8 needSync; /* True if an fsync() is needed on the journal */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 201 | u8 dirtyCache; /* True if cached pages have changed */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 202 | u8 alwaysRollback; /* Disable DontRollback() for all pages */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 203 | u8 memDb; /* True to inhibit all file I/O */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 204 | u8 setMaster; /* True if a m-j name has been written to jrnl */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 205 | u8 doNotSync; /* Boolean. While true, do not spill the cache */ |
| 206 | u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 207 | u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */ |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 208 | u8 dbModified; /* True if there are any changes to the Db */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 209 | u8 changeCountDone; /* Set after incrementing the change-counter */ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 210 | u8 dbSizeValid; /* Set when dbSize is correct */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 211 | Pgno dbSize; /* Number of pages in the database */ |
| 212 | Pgno dbOrigSize; /* dbSize before the current transaction */ |
| 213 | Pgno dbFileSize; /* Number of pages in the database file */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 214 | u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 215 | int errCode; /* One of several kinds of errors */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 216 | int nRec; /* Number of pages written to the journal */ |
| 217 | u32 cksumInit; /* Quasi-random value added to every checksum */ |
| 218 | int stmtNRec; /* Number of records in stmt subjournal */ |
| 219 | int nExtra; /* Add this many bytes to each in-memory page */ |
| 220 | int pageSize; /* Number of bytes in a page */ |
| 221 | int nPage; /* Total number of in-memory pages */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 222 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 223 | Pgno mxPgno; /* Maximum allowed size of the database */ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 224 | Bitvec *pInJournal; /* One bit for each page in the database file */ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 225 | Bitvec *pAlwaysRollback; /* One bit for each page marked always-rollback */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 226 | char *zFilename; /* Name of the database file */ |
| 227 | char *zJournal; /* Name of the journal file */ |
| 228 | char *zDirectory; /* Directory hold database and journal files */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 229 | sqlite3_file *fd, *jfd; /* File descriptors for database and journal */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 230 | sqlite3_file *sjfd; /* File descriptor for the sub-journal*/ |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 231 | int (*xBusyHandler)(void*); /* Function to call when busy */ |
| 232 | void *pBusyHandlerArg; /* Context argument for xBusyHandler */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 233 | i64 journalOff; /* Current byte offset in the journal file */ |
| 234 | i64 journalHdr; /* Byte offset to previous journal header */ |
drh | 98c5835 | 2008-11-07 00:24:53 +0000 | [diff] [blame] | 235 | u32 sectorSize; /* Assumed sector size during rollback */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 236 | #ifdef SQLITE_TEST |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 237 | int nHit, nMiss; /* Cache hits and missing */ |
| 238 | int nRead, nWrite; /* Database pages read/written */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 239 | #endif |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 240 | void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 241 | #ifdef SQLITE_HAS_CODEC |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 242 | void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 243 | void *pCodecArg; /* First argument to xCodec() */ |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 244 | #endif |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 245 | char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 246 | char dbFileVers[16]; /* Changes whenever database file changes */ |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 247 | i64 journalSizeLimit; /* Size limit for persistent journal files */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 248 | PCache *pPCache; /* Pointer to page cache object */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 249 | |
| 250 | PagerSavepoint *aSavepoint; |
| 251 | int nSavepoint; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | /* |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 255 | ** The following global variables hold counters used for |
| 256 | ** testing purposes only. These variables do not exist in |
| 257 | ** a non-testing build. These variables are not thread-safe. |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 258 | */ |
| 259 | #ifdef SQLITE_TEST |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 260 | int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */ |
| 261 | int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */ |
| 262 | int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 263 | # define PAGER_INCR(v) v++ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 264 | #else |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 265 | # define PAGER_INCR(v) |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 266 | #endif |
| 267 | |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 268 | |
| 269 | |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 270 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 271 | ** Journal files begin with the following magic string. The data |
| 272 | ** was obtained from /dev/random. It is used only as a sanity check. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 273 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 274 | ** Since version 2.8.0, the journal format contains additional sanity |
| 275 | ** checking information. If the power fails while the journal is begin |
| 276 | ** written, semi-random garbage data might appear in the journal |
| 277 | ** file after power is restored. If an attempt is then made |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 278 | ** to roll the journal back, the database could be corrupted. The additional |
| 279 | ** sanity checking data is an attempt to discover the garbage in the |
| 280 | ** journal and ignore it. |
| 281 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 282 | ** The sanity checking information for the new journal format consists |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 283 | ** 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] | 284 | ** the page number and the pPager->pageSize bytes of data for the page. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 285 | ** This cksum is initialized to a 32-bit random value that appears in the |
| 286 | ** journal file right after the header. The random initializer is important, |
| 287 | ** because garbage data that appears at the end of a journal is likely |
| 288 | ** data that was once in other files that have now been deleted. If the |
| 289 | ** garbage data came from an obsolete journal file, the checksums might |
| 290 | ** be correct. But by initializing the checksum to random value which |
| 291 | ** is different for every journal, we minimize that risk. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 292 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 293 | static const unsigned char aJournalMagic[] = { |
| 294 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7, |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | /* |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 298 | ** The size of the header and of each page in the journal is determined |
| 299 | ** by the following macros. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 300 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 301 | #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8) |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 302 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 303 | /* |
| 304 | ** The journal header size for this pager. In the future, this could be |
| 305 | ** set to some value read from the disk controller. The important |
| 306 | ** characteristic is that it is the same size as a disk sector. |
| 307 | */ |
| 308 | #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize) |
| 309 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 310 | /* |
| 311 | ** The macro MEMDB is true if we are dealing with an in-memory database. |
| 312 | ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set, |
| 313 | ** the value of MEMDB will be a constant and the compiler will optimize |
| 314 | ** out code that would never execute. |
| 315 | */ |
| 316 | #ifdef SQLITE_OMIT_MEMORYDB |
| 317 | # define MEMDB 0 |
| 318 | #else |
| 319 | # define MEMDB pPager->memDb |
| 320 | #endif |
| 321 | |
| 322 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 323 | ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is |
| 324 | ** reserved for working around a windows/posix incompatibility). It is |
| 325 | ** used in the journal to signify that the remainder of the journal file |
| 326 | ** is devoted to storing a master journal name - there are no more pages to |
| 327 | ** roll back. See comments for function writeMasterJournal() for details. |
| 328 | */ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 329 | /* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 330 | #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1)) |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 331 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 332 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 333 | ** The maximum legal page number is (2^31 - 1). |
| 334 | */ |
| 335 | #define PAGER_MAX_PGNO 2147483647 |
| 336 | |
| 337 | /* |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 338 | ** Return true if it is necessary to write page *pPg into the sub-journal. |
| 339 | ** A page needs to be written into the sub-journal if there exists one |
| 340 | ** or more open savepoints for which: |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 341 | ** |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 342 | ** * The page-number is less than or equal to PagerSavepoint.nOrig, and |
| 343 | ** * The bit corresponding to the page-number is not set in |
| 344 | ** PagerSavepoint.pInSavepoint. |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 345 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 346 | static int subjRequiresPage(PgHdr *pPg){ |
| 347 | Pgno pgno = pPg->pgno; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 348 | Pager *pPager = pPg->pPager; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 349 | int i; |
| 350 | for(i=0; i<pPager->nSavepoint; i++){ |
| 351 | PagerSavepoint *p = &pPager->aSavepoint[i]; |
| 352 | if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){ |
| 353 | return 1; |
| 354 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 355 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 356 | return 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 357 | } |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 358 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 359 | /* |
| 360 | ** Return true if the page is already in the journal file. |
| 361 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 362 | static int pageInJournal(PgHdr *pPg){ |
| 363 | return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno); |
| 364 | } |
| 365 | |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 366 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 367 | ** Read a 32-bit integer from the given file descriptor. Store the integer |
| 368 | ** that is read in *pRes. Return SQLITE_OK if everything worked, or an |
| 369 | ** error code is something goes wrong. |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 370 | ** |
| 371 | ** All values are stored on disk as big-endian. |
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 read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){ |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 374 | unsigned char ac[4]; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 375 | int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset); |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 376 | if( rc==SQLITE_OK ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 377 | *pRes = sqlite3Get4byte(ac); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 378 | } |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 379 | return rc; |
| 380 | } |
| 381 | |
| 382 | /* |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 383 | ** Write a 32-bit integer into a string buffer in big-endian byte order. |
| 384 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 385 | #define put32bits(A,B) sqlite3Put4byte((u8*)A,B) |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 386 | |
| 387 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 388 | ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK |
| 389 | ** on success or an error code is something goes wrong. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 390 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 391 | static int write32bits(sqlite3_file *fd, i64 offset, u32 val){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 392 | char ac[4]; |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 393 | put32bits(ac, val); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 394 | return sqlite3OsWrite(fd, ac, 4, offset); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 395 | } |
| 396 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 397 | /* |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 398 | ** If file pFd is open, call sqlite3OsUnlock() on it. |
| 399 | */ |
| 400 | static int osUnlock(sqlite3_file *pFd, int eLock){ |
| 401 | if( !pFd->pMethods ){ |
| 402 | return SQLITE_OK; |
| 403 | } |
| 404 | return sqlite3OsUnlock(pFd, eLock); |
| 405 | } |
| 406 | |
| 407 | /* |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 408 | ** This function determines whether or not the atomic-write optimization |
| 409 | ** can be used with this pager. The optimization can be used if: |
| 410 | ** |
| 411 | ** (a) the value returned by OsDeviceCharacteristics() indicates that |
| 412 | ** a database page may be written atomically, and |
| 413 | ** (b) the value returned by OsSectorSize() is less than or equal |
| 414 | ** to the page size. |
| 415 | ** |
| 416 | ** If the optimization cannot be used, 0 is returned. If it can be used, |
| 417 | ** then the value returned is the size of the journal file when it |
| 418 | ** contains rollback data for exactly one page. |
| 419 | */ |
| 420 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 421 | static int jrnlBufferSize(Pager *pPager){ |
| 422 | int dc; /* Device characteristics */ |
| 423 | int nSector; /* Sector size */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 424 | int szPage; /* Page size */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 425 | sqlite3_file *fd = pPager->fd; |
| 426 | |
| 427 | if( fd->pMethods ){ |
| 428 | dc = sqlite3OsDeviceCharacteristics(fd); |
| 429 | nSector = sqlite3OsSectorSize(fd); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 430 | szPage = pPager->pageSize; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 434 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 435 | |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 436 | if( !fd->pMethods || |
| 437 | (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 438 | return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | #endif |
| 443 | |
| 444 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 445 | ** This function should be called when an error occurs within the pager |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 446 | ** code. The first argument is a pointer to the pager structure, the |
| 447 | ** second the error-code about to be returned by a pager API function. |
| 448 | ** The value returned is a copy of the second argument to this function. |
| 449 | ** |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 450 | ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 451 | ** the error becomes persistent. Until the persisten error is cleared, |
| 452 | ** subsequent API calls on this Pager will immediately return the same |
| 453 | ** error code. |
| 454 | ** |
| 455 | ** A persistent error indicates that the contents of the pager-cache |
| 456 | ** cannot be trusted. This state can be cleared by completely discarding |
| 457 | ** the contents of the pager-cache. If a transaction was active when |
| 458 | ** the persistent error occured, then the rollback journal may need |
| 459 | ** to be replayed. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 460 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 461 | static void pager_unlock(Pager *pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 462 | static int pager_error(Pager *pPager, int rc){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 463 | int rc2 = rc & 0xff; |
drh | 34f5621 | 2007-08-10 23:56:35 +0000 | [diff] [blame] | 464 | assert( |
| 465 | pPager->errCode==SQLITE_FULL || |
| 466 | pPager->errCode==SQLITE_OK || |
| 467 | (pPager->errCode & 0xff)==SQLITE_IOERR |
| 468 | ); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 469 | if( |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 470 | rc2==SQLITE_FULL || |
| 471 | rc2==SQLITE_IOERR || |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 472 | rc2==SQLITE_CORRUPT |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 473 | ){ |
| 474 | pPager->errCode = rc; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 475 | if( pPager->state==PAGER_UNLOCK |
| 476 | && sqlite3PcacheRefCount(pPager->pPCache)==0 |
| 477 | ){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 478 | /* If the pager is already unlocked, call pager_unlock() now to |
| 479 | ** clear the error state and ensure that the pager-cache is |
| 480 | ** completely empty. |
| 481 | */ |
| 482 | pager_unlock(pPager); |
| 483 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 484 | } |
| 485 | return rc; |
| 486 | } |
| 487 | |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 488 | /* |
| 489 | ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking |
| 490 | ** on the cache using a hash function. This is used for testing |
| 491 | ** and debugging only. |
| 492 | */ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 493 | #ifdef SQLITE_CHECK_PAGES |
| 494 | /* |
| 495 | ** Return a 32-bit hash of the page data for pPage. |
| 496 | */ |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 497 | static u32 pager_datahash(int nByte, unsigned char *pData){ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 498 | u32 hash = 0; |
| 499 | int i; |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 500 | for(i=0; i<nByte; i++){ |
| 501 | hash = (hash*1039) + pData[i]; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 502 | } |
| 503 | return hash; |
| 504 | } |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 505 | static u32 pager_pagehash(PgHdr *pPage){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 506 | return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData); |
| 507 | } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 508 | static void pager_set_pagehash(PgHdr *pPage){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 509 | pPage->pageHash = pager_pagehash(pPage); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 510 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 511 | |
| 512 | /* |
| 513 | ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES |
| 514 | ** is defined, and NDEBUG is not defined, an assert() statement checks |
| 515 | ** that the page is either dirty or still matches the calculated page-hash. |
| 516 | */ |
| 517 | #define CHECK_PAGE(x) checkPage(x) |
| 518 | static void checkPage(PgHdr *pPg){ |
| 519 | Pager *pPager = pPg->pPager; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 520 | assert( !pPg->pageHash || pPager->errCode |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 521 | || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) ); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | #else |
drh | 8ffa817 | 2007-06-18 17:25:17 +0000 | [diff] [blame] | 525 | #define pager_datahash(X,Y) 0 |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 526 | #define pager_pagehash(X) 0 |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 527 | #define CHECK_PAGE(x) |
drh | 41d3027 | 2008-08-20 21:47:45 +0000 | [diff] [blame] | 528 | #endif /* SQLITE_CHECK_PAGES */ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 529 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 530 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 531 | ** When this is called the journal file for pager pPager must be open. |
| 532 | ** 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] | 533 | ** written into memory supplied by the caller. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 534 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 535 | ** zMaster must point to a buffer of at least nMaster bytes allocated by |
| 536 | ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is |
| 537 | ** enough space to write the master journal name). If the master journal |
| 538 | ** name in the journal is longer than nMaster bytes (including a |
| 539 | ** nul-terminator), then this is handled as if no master journal name |
| 540 | ** were present in the journal. |
| 541 | ** |
| 542 | ** 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] | 543 | ** SQLITE_OK returned. |
| 544 | */ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 545 | static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 546 | int rc; |
| 547 | u32 len; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 548 | i64 szJ; |
danielk1977 | c3e8f5e | 2004-06-28 01:16:46 +0000 | [diff] [blame] | 549 | u32 cksum; |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 550 | u32 u; /* Unsigned loop counter */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 551 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
| 552 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 553 | zMaster[0] = '\0'; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 554 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 555 | rc = sqlite3OsFileSize(pJrnl, &szJ); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 556 | if( rc!=SQLITE_OK || szJ<16 ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 557 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 558 | rc = read32bits(pJrnl, szJ-16, &len); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 559 | if( rc!=SQLITE_OK ) return rc; |
| 560 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 561 | if( len>=nMaster ){ |
| 562 | return SQLITE_OK; |
| 563 | } |
| 564 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 565 | rc = read32bits(pJrnl, szJ-12, &cksum); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 566 | if( rc!=SQLITE_OK ) return rc; |
| 567 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 568 | rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 569 | if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc; |
| 570 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 571 | rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 572 | if( rc!=SQLITE_OK ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 573 | return rc; |
| 574 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 575 | zMaster[len] = '\0'; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 576 | |
| 577 | /* See if the checksum matches the master journal name */ |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 578 | for(u=0; u<len; u++){ |
| 579 | cksum -= zMaster[u]; |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 580 | } |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 581 | if( cksum ){ |
| 582 | /* If the checksum doesn't add up, then one or more of the disk sectors |
| 583 | ** containing the master journal filename is corrupted. This means |
| 584 | ** definitely roll back, so just return SQLITE_OK and report a (nul) |
| 585 | ** master-journal filename. |
| 586 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 587 | zMaster[0] = '\0'; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 588 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 589 | |
| 590 | return SQLITE_OK; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | ** Seek the journal file descriptor to the next sector boundary where a |
| 595 | ** journal header may be read or written. Pager.journalOff is updated with |
| 596 | ** the new seek offset. |
| 597 | ** |
| 598 | ** i.e for a sector size of 512: |
| 599 | ** |
| 600 | ** Input Offset Output Offset |
| 601 | ** --------------------------------------- |
| 602 | ** 0 0 |
| 603 | ** 512 512 |
| 604 | ** 100 512 |
| 605 | ** 2000 2048 |
| 606 | ** |
| 607 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 608 | static void seekJournalHdr(Pager *pPager){ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 609 | i64 offset = 0; |
| 610 | i64 c = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 611 | if( c ){ |
| 612 | offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager); |
| 613 | } |
| 614 | assert( offset%JOURNAL_HDR_SZ(pPager)==0 ); |
| 615 | assert( offset>=c ); |
| 616 | assert( (offset-c)<JOURNAL_HDR_SZ(pPager) ); |
| 617 | pPager->journalOff = offset; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
drh | f3a8762 | 2008-04-17 14:16:42 +0000 | [diff] [blame] | 621 | ** Write zeros over the header of the journal file. This has the |
| 622 | ** effect of invalidating the journal file and committing the |
| 623 | ** transaction. |
| 624 | */ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 625 | static int zeroJournalHdr(Pager *pPager, int doTruncate){ |
| 626 | int rc = SQLITE_OK; |
danielk1977 | 55a25a1 | 2008-09-11 10:29:15 +0000 | [diff] [blame] | 627 | static const char zeroHdr[28] = {0}; |
drh | f3a8762 | 2008-04-17 14:16:42 +0000 | [diff] [blame] | 628 | |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 629 | if( pPager->journalOff ){ |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 630 | i64 iLimit = pPager->journalSizeLimit; |
| 631 | |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 632 | IOTRACE(("JZEROHDR %p\n", pPager)) |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 633 | if( doTruncate || iLimit==0 ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 634 | rc = sqlite3OsTruncate(pPager->jfd, 0); |
| 635 | }else{ |
| 636 | rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0); |
| 637 | } |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 638 | if( rc==SQLITE_OK && !pPager->noSync ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 639 | rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags); |
| 640 | } |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 641 | |
| 642 | /* At this point the transaction is committed but the write lock |
| 643 | ** is still held on the file. If there is a size limit configured for |
| 644 | ** the persistent journal and the journal file currently consumes more |
| 645 | ** space than that limit allows for, truncate it now. There is no need |
| 646 | ** to sync the file following this operation. |
| 647 | */ |
| 648 | if( rc==SQLITE_OK && iLimit>0 ){ |
| 649 | i64 sz; |
| 650 | rc = sqlite3OsFileSize(pPager->jfd, &sz); |
| 651 | if( rc==SQLITE_OK && sz>iLimit ){ |
| 652 | rc = sqlite3OsTruncate(pPager->jfd, iLimit); |
| 653 | } |
| 654 | } |
drh | a06ecba | 2008-04-22 17:15:17 +0000 | [diff] [blame] | 655 | } |
drh | f3a8762 | 2008-04-17 14:16:42 +0000 | [diff] [blame] | 656 | return rc; |
| 657 | } |
| 658 | |
| 659 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 660 | ** The journal file must be open when this routine is called. A journal |
| 661 | ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the |
| 662 | ** current location. |
| 663 | ** |
| 664 | ** The format for the journal header is as follows: |
| 665 | ** - 8 bytes: Magic identifying journal format. |
| 666 | ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on. |
| 667 | ** - 4 bytes: Random number used for page hash. |
| 668 | ** - 4 bytes: Initial database page count. |
| 669 | ** - 4 bytes: Sector size used by the process that wrote this journal. |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 670 | ** - 4 bytes: Database page size. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 671 | ** |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 672 | ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 673 | */ |
| 674 | static int writeJournalHdr(Pager *pPager){ |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 675 | int rc = SQLITE_OK; |
| 676 | char *zHeader = pPager->pTmpSpace; |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 677 | u32 nHeader = pPager->pageSize; |
| 678 | u32 nWrite; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 679 | int ii; |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 680 | |
| 681 | if( nHeader>JOURNAL_HDR_SZ(pPager) ){ |
| 682 | nHeader = JOURNAL_HDR_SZ(pPager); |
| 683 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 684 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 685 | /* If there are active savepoints and any of them were created since the |
| 686 | ** most recent journal header was written, update the PagerSavepoint.iHdrOff |
| 687 | ** fields now. |
| 688 | */ |
| 689 | for(ii=0; ii<pPager->nSavepoint; ii++){ |
| 690 | if( pPager->aSavepoint[ii].iHdrOffset==0 ){ |
| 691 | pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff; |
| 692 | } |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 693 | } |
| 694 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 695 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 696 | pPager->journalHdr = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 697 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 698 | memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 699 | |
| 700 | /* |
| 701 | ** Write the nRec Field - the number of page records that follow this |
| 702 | ** journal header. Normally, zero is written to this value at this time. |
| 703 | ** After the records are added to the journal (and the journal synced, |
| 704 | ** if in full-sync mode), the zero is overwritten with the true number |
| 705 | ** of records (see syncJournal()). |
| 706 | ** |
| 707 | ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When |
| 708 | ** reading the journal this value tells SQLite to assume that the |
| 709 | ** rest of the journal file contains valid page records. This assumption |
| 710 | ** is dangerous, as if a failure occured whilst writing to the journal |
| 711 | ** file it may contain some garbage data. There are two scenarios |
| 712 | ** where this risk can be ignored: |
| 713 | ** |
| 714 | ** * When the pager is in no-sync mode. Corruption can follow a |
| 715 | ** power failure in this case anyway. |
| 716 | ** |
| 717 | ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees |
| 718 | ** that garbage data is never appended to the journal file. |
| 719 | */ |
| 720 | assert(pPager->fd->pMethods||pPager->noSync); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 721 | if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY) |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 722 | || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) |
| 723 | ){ |
| 724 | put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); |
| 725 | }else{ |
| 726 | put32bits(&zHeader[sizeof(aJournalMagic)], 0); |
| 727 | } |
| 728 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 729 | /* The random check-hash initialiser */ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 730 | sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 731 | put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit); |
| 732 | /* The initial database size */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 733 | put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize); |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 734 | /* The assumed sector size for this process */ |
| 735 | put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize); |
drh | 08609ce | 2008-11-29 22:49:23 +0000 | [diff] [blame] | 736 | |
| 737 | /* Initializing the tail of the buffer is not necessary. Everything |
| 738 | ** works find if the following memset() is omitted. But initializing |
| 739 | ** the memory prevents valgrind from complaining, so we are willing to |
| 740 | ** take the performance hit. |
| 741 | */ |
| 742 | memset(&zHeader[sizeof(aJournalMagic)+16], 0, |
| 743 | nHeader-(sizeof(aJournalMagic)+16)); |
| 744 | |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 745 | if( pPager->journalHdr==0 ){ |
| 746 | /* The page size */ |
| 747 | put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize); |
| 748 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 749 | |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 750 | for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){ |
| 751 | IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader)) |
| 752 | rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff); |
| 753 | pPager->journalOff += nHeader; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 754 | } |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 755 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 756 | return rc; |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | ** The journal file must be open when this is called. A journal header file |
| 761 | ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal |
| 762 | ** file. See comments above function writeJournalHdr() for a description of |
| 763 | ** the journal header format. |
| 764 | ** |
| 765 | ** If the header is read successfully, *nRec is set to the number of |
| 766 | ** page records following this header and *dbSize is set to the size of the |
| 767 | ** database before the transaction began, in pages. Also, pPager->cksumInit |
| 768 | ** is set to the value read from the journal header. SQLITE_OK is returned |
| 769 | ** in this case. |
| 770 | ** |
| 771 | ** If the journal header file appears to be corrupted, SQLITE_DONE is |
| 772 | ** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes |
| 773 | ** cannot be read from the journal file an error code is returned. |
| 774 | */ |
| 775 | static int readJournalHdr( |
| 776 | Pager *pPager, |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 777 | i64 journalSize, |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 778 | u32 *pNRec, |
| 779 | u32 *pDbSize |
| 780 | ){ |
| 781 | int rc; |
| 782 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 783 | i64 jrnlOff; |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 784 | int iPageSize; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 785 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 786 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 787 | if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){ |
| 788 | return SQLITE_DONE; |
| 789 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 790 | jrnlOff = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 791 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 792 | rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 793 | if( rc ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 794 | jrnlOff += sizeof(aMagic); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 795 | |
| 796 | if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){ |
| 797 | return SQLITE_DONE; |
| 798 | } |
| 799 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 800 | rc = read32bits(pPager->jfd, jrnlOff, pNRec); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 801 | if( rc ) return rc; |
| 802 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 803 | rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 804 | if( rc ) return rc; |
| 805 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 806 | rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 807 | if( rc ) return rc; |
| 808 | |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 809 | rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize); |
| 810 | if( rc==SQLITE_OK |
| 811 | && iPageSize>=512 |
| 812 | && iPageSize<=SQLITE_MAX_PAGE_SIZE |
| 813 | && ((iPageSize-1)&iPageSize)==0 |
| 814 | ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 815 | u16 pagesize = (u16)iPageSize; |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 816 | rc = sqlite3PagerSetPagesize(pPager, &pagesize); |
| 817 | } |
| 818 | if( rc ) return rc; |
| 819 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 820 | /* Update the assumed sector-size to match the value used by |
| 821 | ** the process that created this journal. If this journal was |
| 822 | ** created by a process other than this one, then this routine |
| 823 | ** is being called from within pager_playback(). The local value |
| 824 | ** of Pager.sectorSize is restored at the end of that routine. |
| 825 | */ |
drh | 98c5835 | 2008-11-07 00:24:53 +0000 | [diff] [blame] | 826 | rc = read32bits(pPager->jfd, jrnlOff+12, &pPager->sectorSize); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 827 | if( rc ) return rc; |
drh | 98c5835 | 2008-11-07 00:24:53 +0000 | [diff] [blame] | 828 | if( (pPager->sectorSize & (pPager->sectorSize-1))!=0 |
| 829 | || pPager->sectorSize>0x1000000 ){ |
| 830 | return SQLITE_DONE; |
| 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){ |
| 964 | int ii; |
| 965 | for(ii=0; ii<pPager->nSavepoint; ii++){ |
| 966 | PagerSavepoint *p = &pPager->aSavepoint[ii]; |
| 967 | if( pgno<=p->nOrig ){ |
| 968 | /* TODO: malloc() failure handling */ |
| 969 | sqlite3BitvecSet(p->pInSavepoint, pgno); |
| 970 | } |
| 971 | } |
| 972 | return SQLITE_OK; |
| 973 | } |
| 974 | |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 975 | /* |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 976 | ** Unlock the database file. |
| 977 | ** |
| 978 | ** If the pager is currently in error state, discard the contents of |
| 979 | ** the cache and reset the Pager structure internal state. If there is |
| 980 | ** an open journal-file, then the next time a shared-lock is obtained |
| 981 | ** on the pager file (by this or any other process), it will be |
| 982 | ** treated as a hot-journal and rolled back. |
| 983 | */ |
| 984 | static void pager_unlock(Pager *pPager){ |
| 985 | if( !pPager->exclusiveMode ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 986 | int rc = osUnlock(pPager->fd, NO_LOCK); |
| 987 | if( rc ) pPager->errCode = rc; |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 988 | pPager->dbSizeValid = 0; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 989 | IOTRACE(("UNLOCK %p\n", pPager)) |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 990 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 991 | /* Always close the journal file when dropping the database lock. |
| 992 | ** Otherwise, another connection with journal_mode=delete might |
| 993 | ** delete the file out from under us. |
| 994 | */ |
| 995 | if( pPager->journalOpen ){ |
| 996 | sqlite3OsClose(pPager->jfd); |
| 997 | pPager->journalOpen = 0; |
| 998 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 999 | pPager->pInJournal = 0; |
| 1000 | sqlite3BitvecDestroy(pPager->pAlwaysRollback); |
| 1001 | pPager->pAlwaysRollback = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1004 | /* If Pager.errCode is set, the contents of the pager cache cannot be |
| 1005 | ** trusted. Now that the pager file is unlocked, the contents of the |
| 1006 | ** cache can be discarded and the error code safely cleared. |
| 1007 | */ |
| 1008 | if( pPager->errCode ){ |
| 1009 | if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK; |
| 1010 | pager_reset(pPager); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1011 | releaseAllSavepoint(pPager); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1012 | pPager->journalOff = 0; |
| 1013 | pPager->journalStarted = 0; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 1014 | pPager->dbOrigSize = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1015 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1016 | |
| 1017 | pPager->state = PAGER_UNLOCK; |
| 1018 | pPager->changeCountDone = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | ** Execute a rollback if a transaction is active and unlock the |
| 1024 | ** database file. If the pager has already entered the error state, |
| 1025 | ** do not attempt the rollback. |
| 1026 | */ |
| 1027 | static void pagerUnlockAndRollback(Pager *p){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1028 | if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){ |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 1029 | sqlite3BeginBenignMalloc(); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1030 | sqlite3PagerRollback(p); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 1031 | sqlite3EndBenignMalloc(); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1032 | } |
| 1033 | pager_unlock(p); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1037 | ** This routine ends a transaction. A transaction is ended by either |
| 1038 | ** a COMMIT or a ROLLBACK. |
| 1039 | ** |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1040 | ** When this routine is called, the pager has the journal file open and |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1041 | ** a RESERVED or EXCLUSIVE lock on the database. This routine will release |
| 1042 | ** the database lock and acquires a SHARED lock in its place if that is |
| 1043 | ** the appropriate thing to do. Release locks usually is appropriate, |
| 1044 | ** unless we are in exclusive access mode or unless this is a |
| 1045 | ** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation. |
| 1046 | ** |
| 1047 | ** The journal file is either deleted or truncated. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1048 | ** |
| 1049 | ** TODO: Consider keeping the journal file open for temporary databases. |
| 1050 | ** This might give a performance improvement on windows where opening |
| 1051 | ** a file is an expensive operation. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1052 | */ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 1053 | static int pager_end_transaction(Pager *pPager, int hasMaster){ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1054 | int rc = SQLITE_OK; |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1055 | int rc2 = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1056 | if( pPager->state<PAGER_RESERVED ){ |
| 1057 | return SQLITE_OK; |
| 1058 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1059 | releaseAllSavepoint(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1060 | if( pPager->journalOpen ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1061 | if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ |
| 1062 | int isMemoryJournal = sqlite3IsMemJournal(pPager->jfd); |
| 1063 | sqlite3OsClose(pPager->jfd); |
| 1064 | pPager->journalOpen = 0; |
| 1065 | if( !isMemoryJournal ){ |
| 1066 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
| 1067 | } |
| 1068 | }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE |
drh | 0433588 | 2008-09-26 21:08:08 +0000 | [diff] [blame] | 1069 | && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){ |
| 1070 | pPager->journalOff = 0; |
| 1071 | pPager->journalStarted = 0; |
| 1072 | }else if( pPager->exclusiveMode |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1073 | || pPager->journalMode==PAGER_JOURNALMODE_PERSIST |
| 1074 | ){ |
| 1075 | rc = zeroJournalHdr(pPager, hasMaster); |
| 1076 | pager_error(pPager, rc); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1077 | pPager->journalOff = 0; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1078 | pPager->journalStarted = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1079 | }else{ |
drh | 0433588 | 2008-09-26 21:08:08 +0000 | [diff] [blame] | 1080 | assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || rc ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1081 | sqlite3OsClose(pPager->jfd); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1082 | pPager->journalOpen = 0; |
danielk1977 | 0f01fda | 2008-06-06 16:14:02 +0000 | [diff] [blame] | 1083 | if( rc==SQLITE_OK && !pPager->tempFile ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1084 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
danielk1977 | 7152de8 | 2007-03-29 17:28:14 +0000 | [diff] [blame] | 1085 | } |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1086 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1087 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 1088 | pPager->pInJournal = 0; |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 1089 | sqlite3BitvecDestroy(pPager->pAlwaysRollback); |
| 1090 | pPager->pAlwaysRollback = 0; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1091 | #ifdef SQLITE_CHECK_PAGES |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1092 | sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1093 | #endif |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1094 | sqlite3PcacheCleanAll(pPager->pPCache); |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1095 | pPager->dirtyCache = 0; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1096 | pPager->nRec = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1097 | }else{ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1098 | assert( pPager->pInJournal==0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1099 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1100 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1101 | if( !pPager->exclusiveMode ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1102 | rc2 = osUnlock(pPager->fd, SHARED_LOCK); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1103 | pPager->state = PAGER_SHARED; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1104 | }else if( pPager->state==PAGER_SYNCED ){ |
| 1105 | pPager->state = PAGER_EXCLUSIVE; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1106 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 1107 | pPager->dbOrigSize = 0; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1108 | pPager->setMaster = 0; |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 1109 | pPager->needSync = 0; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1110 | /* lruListSetFirstSynced(pPager); */ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1111 | if( !MEMDB ){ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 1112 | pPager->dbSizeValid = 0; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1113 | } |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 1114 | pPager->dbModified = 0; |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1115 | |
| 1116 | return (rc==SQLITE_OK?rc2:rc); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1119 | /* |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1120 | ** Compute and return a checksum for the page of data. |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1121 | ** |
| 1122 | ** 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] | 1123 | ** random initial value and the page number. We experimented with |
| 1124 | ** a checksum of the entire data, but that was found to be too slow. |
| 1125 | ** |
| 1126 | ** Note that the page number is stored at the beginning of data and |
| 1127 | ** the checksum is stored at the end. This is important. If journal |
| 1128 | ** corruption occurs due to a power failure, the most likely scenario |
| 1129 | ** is that one end or the other of the record will be changed. It is |
| 1130 | ** much less likely that the two ends of the journal record will be |
| 1131 | ** correct and the middle be corrupt. Thus, this "checksum" scheme, |
| 1132 | ** though fast and simple, catches the mostly likely kind of corruption. |
| 1133 | ** |
| 1134 | ** FIX ME: Consider adding every 200th (or so) byte of the data to the |
| 1135 | ** checksum. That way if a single page spans 3 or more disk sectors and |
| 1136 | ** only the middle sector is corrupt, we will still have a reasonable |
| 1137 | ** chance of failing the checksum and thus detecting the problem. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1138 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1139 | static u32 pager_cksum(Pager *pPager, const u8 *aData){ |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1140 | u32 cksum = pPager->cksumInit; |
| 1141 | int i = pPager->pageSize-200; |
| 1142 | while( i>0 ){ |
| 1143 | cksum += aData[i]; |
| 1144 | i -= 200; |
| 1145 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1146 | return cksum; |
| 1147 | } |
| 1148 | |
| 1149 | /* |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1150 | ** Read a single page from the journal file opened on file descriptor |
| 1151 | ** jfd. Playback this one page. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1152 | ** |
drh | c13148f | 2008-08-27 18:03:20 +0000 | [diff] [blame] | 1153 | ** The isMainJrnl flag is true if this is the main rollback journal and |
| 1154 | ** false for the statement journal. The main rollback journal uses |
| 1155 | ** checksums - the statement journal does not. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1156 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1157 | static int pager_playback_one_page( |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1158 | Pager *pPager, /* The pager being played back */ |
| 1159 | int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */ |
| 1160 | i64 offset, /* Offset of record to playback */ |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1161 | int isSavepnt, /* True for a savepoint rollback */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1162 | Bitvec *pDone /* Bitvec of pages already played back */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1163 | ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1164 | int rc; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1165 | PgHdr *pPg; /* An existing page in the cache */ |
| 1166 | Pgno pgno; /* The page number of a page in journal */ |
| 1167 | u32 cksum; /* Checksum used for sanity checking */ |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 1168 | u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1169 | sqlite3_file *jfd = (isMainJrnl ? pPager->jfd : pPager->sjfd); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1170 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1171 | /* The temp storage must be allocated at this point */ |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 1172 | assert( aData ); |
danielk1977 | 488af09 | 2008-12-19 16:31:11 +0000 | [diff] [blame] | 1173 | assert( isMainJrnl || pDone ); |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1174 | assert( isSavepnt || pDone==0 ); |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1175 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1176 | rc = read32bits(jfd, offset, &pgno); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1177 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1178 | rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1179 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1180 | pPager->journalOff += pPager->pageSize + 4; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1181 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1182 | /* Sanity checking on the page. This is more important that I originally |
| 1183 | ** thought. If a power failure occurs while the journal is being written, |
| 1184 | ** it could cause invalid data to be written into the journal. We need to |
| 1185 | ** detect this invalid data (with high probability) and ignore it. |
| 1186 | */ |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1187 | if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1188 | return SQLITE_DONE; |
| 1189 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1190 | if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1191 | return SQLITE_OK; |
| 1192 | } |
drh | c13148f | 2008-08-27 18:03:20 +0000 | [diff] [blame] | 1193 | if( isMainJrnl ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1194 | rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1195 | if( rc ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1196 | pPager->journalOff += 4; |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1197 | if( !isSavepnt && pager_cksum(pPager, aData)!=cksum ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1198 | return SQLITE_DONE; |
| 1199 | } |
| 1200 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1201 | if( pDone && (rc = sqlite3BitvecSet(pDone, pgno)) ){ |
| 1202 | return rc; |
| 1203 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1204 | |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 1205 | assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE ); |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1206 | |
| 1207 | /* If the pager is in RESERVED state, then there must be a copy of this |
| 1208 | ** page in the pager cache. In this case just update the pager cache, |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 1209 | ** not the database file. The page is left marked dirty in this case. |
| 1210 | ** |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1211 | ** An exception to the above rule: If the database is in no-sync mode |
| 1212 | ** and a page is moved during an incremental vacuum then the page may |
danielk1977 | 369f3a0 | 2007-05-24 09:41:20 +0000 | [diff] [blame] | 1213 | ** not be in the pager cache. Later: if a malloc() or IO error occurs |
| 1214 | ** during a Movepage() call, then the page may not be in the cache |
| 1215 | ** either. So the condition described in the above paragraph is not |
| 1216 | ** assert()able. |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1217 | ** |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1218 | ** If in EXCLUSIVE state, then we update the pager cache if it exists |
| 1219 | ** and the main file. The page is then marked not dirty. |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1220 | ** |
| 1221 | ** Ticket #1171: The statement journal might contain page content that is |
| 1222 | ** different from the page content at the start of the transaction. |
| 1223 | ** This occurs when a page is changed prior to the start of a statement |
| 1224 | ** then changed again within the statement. When rolling back such a |
| 1225 | ** statement we must not write to the original database unless we know |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 1226 | ** for certain that original page contents are synced into the main rollback |
| 1227 | ** journal. Otherwise, a power loss might leave modified data in the |
| 1228 | ** database file without an entry in the rollback journal that can |
| 1229 | ** restore the database to its original form. Two conditions must be |
| 1230 | ** met before writing to the database files. (1) the database must be |
| 1231 | ** locked. (2) we know that the original page content is fully synced |
| 1232 | ** in the main journal either because the page is not in cache or else |
| 1233 | ** the page is marked as needSync==0. |
drh | 4c02a23 | 2008-04-14 23:13:45 +0000 | [diff] [blame] | 1234 | ** |
| 1235 | ** 2008-04-14: When attempting to vacuum a corrupt database file, it |
| 1236 | ** is possible to fail a statement on a database that does not yet exist. |
| 1237 | ** Do not attempt to write if database file has never been opened. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1238 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1239 | pPg = pager_lookup(pPager, pgno); |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1240 | PAGERTRACE5("PLAYBACK %d page %d hash(%08x) %s\n", |
| 1241 | PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData), |
| 1242 | (isMainJrnl?"main-journal":"sub-journal") |
| 1243 | ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1244 | if( (pPager->state>=PAGER_EXCLUSIVE) |
| 1245 | && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC)) |
| 1246 | && (pPager->fd->pMethods) |
| 1247 | ){ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1248 | i64 ofst = (pgno-1)*(i64)pPager->pageSize; |
| 1249 | rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 1250 | if( pgno>pPager->dbFileSize ){ |
| 1251 | pPager->dbFileSize = pgno; |
| 1252 | } |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1253 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1254 | if( pPg ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1255 | /* No page should ever be explicitly rolled back that is in use, except |
| 1256 | ** for page 1 which is held in use in order to keep the lock on the |
| 1257 | ** database active. However such a page may be rolled back as a result |
| 1258 | ** of an internal error resulting in an automatic call to |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1259 | ** sqlite3PagerRollback(). |
drh | acf4ac9 | 2003-12-17 23:57:34 +0000 | [diff] [blame] | 1260 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1261 | void *pData; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1262 | pData = pPg->pData; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1263 | memcpy(pData, aData, pPager->pageSize); |
danielk1977 | 9038bb6 | 2007-04-09 11:20:54 +0000 | [diff] [blame] | 1264 | if( pPager->xReiniter ){ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1265 | pPager->xReiniter(pPg); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1266 | } |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1267 | if( isMainJrnl && (!isSavepnt || pPager->journalOff<=pPager->journalHdr) ){ |
danielk1977 | 488af09 | 2008-12-19 16:31:11 +0000 | [diff] [blame] | 1268 | /* If the contents of this page were just restored from the main |
| 1269 | ** journal file, then its content must be as they were when the |
| 1270 | ** transaction was first opened. In this case we can mark the page |
| 1271 | ** as clean, since there will be no need to write it out to the. |
| 1272 | ** |
| 1273 | ** There is one exception to this rule. If the page is being rolled |
| 1274 | ** back as part of a savepoint (or statement) rollback from an |
| 1275 | ** unsynced portion of the main journal file, then it is not safe |
| 1276 | ** to mark the page as clean. This is because marking the page as |
| 1277 | ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is |
| 1278 | ** already in the journal file (recorded in Pager.pInJournal) and |
| 1279 | ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to |
| 1280 | ** again within this transaction, it will be marked as dirty but |
| 1281 | ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially |
| 1282 | ** be written out into the database file before its journal file |
| 1283 | ** segment is synced. If a crash occurs during or following this, |
| 1284 | ** database corruption may ensue. |
| 1285 | */ |
drh | c047b9f | 2008-11-21 03:23:43 +0000 | [diff] [blame] | 1286 | sqlite3PcacheMakeClean(pPg); |
| 1287 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1288 | #ifdef SQLITE_CHECK_PAGES |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1289 | pPg->pageHash = pager_pagehash(pPg); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1290 | #endif |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1291 | /* If this was page 1, then restore the value of Pager.dbFileVers. |
| 1292 | ** Do this before any decoding. */ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1293 | if( pgno==1 ){ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1294 | memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1295 | } |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1296 | |
| 1297 | /* Decode the page just read from disk */ |
| 1298 | CODEC1(pPager, pData, pPg->pgno, 3); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1299 | sqlite3PcacheRelease(pPg); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1300 | } |
| 1301 | return rc; |
| 1302 | } |
| 1303 | |
| 1304 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1305 | ** Parameter zMaster is the name of a master journal file. A single journal |
| 1306 | ** file that referred to the master journal file has just been rolled back. |
| 1307 | ** This routine checks if it is possible to delete the master journal file, |
| 1308 | ** and does so if it is. |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1309 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1310 | ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not |
| 1311 | ** available for use within this function. |
| 1312 | ** |
| 1313 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1314 | ** The master journal file contains the names of all child journals. |
| 1315 | ** To tell if a master journal can be deleted, check to each of the |
| 1316 | ** children. If all children are either missing or do not refer to |
| 1317 | ** a different master journal, then this master journal can be deleted. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1318 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1319 | static int pager_delmaster(Pager *pPager, const char *zMaster){ |
| 1320 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1321 | int rc; |
| 1322 | int master_open = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1323 | sqlite3_file *pMaster; |
| 1324 | sqlite3_file *pJournal; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1325 | char *zMasterJournal = 0; /* Contents of master journal file */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1326 | i64 nMasterJournal; /* Size of master journal file */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1327 | |
| 1328 | /* Open the master journal file exclusively in case some other process |
| 1329 | ** is running this routine also. Not that it makes too much difference. |
| 1330 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1331 | pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1332 | pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1333 | if( !pMaster ){ |
| 1334 | rc = SQLITE_NOMEM; |
| 1335 | }else{ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1336 | int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); |
| 1337 | rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1338 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1339 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1340 | master_open = 1; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1341 | |
| 1342 | rc = sqlite3OsFileSize(pMaster, &nMasterJournal); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1343 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1344 | |
| 1345 | if( nMasterJournal>0 ){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1346 | char *zJournal; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1347 | char *zMasterPtr = 0; |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1348 | int nMasterPtr = pPager->pVfs->mxPathname+1; |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1349 | |
| 1350 | /* Load the entire master journal file into space obtained from |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1351 | ** sqlite3_malloc() and pointed to by zMasterJournal. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1352 | */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1353 | zMasterJournal = (char *)sqlite3Malloc((int)nMasterJournal + nMasterPtr); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1354 | if( !zMasterJournal ){ |
| 1355 | rc = SQLITE_NOMEM; |
| 1356 | goto delmaster_out; |
| 1357 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1358 | zMasterPtr = &zMasterJournal[nMasterJournal]; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1359 | rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1360 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1361 | |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1362 | zJournal = zMasterJournal; |
| 1363 | while( (zJournal-zMasterJournal)<nMasterJournal ){ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1364 | int exists; |
| 1365 | rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 1366 | if( rc!=SQLITE_OK ){ |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 1367 | goto delmaster_out; |
| 1368 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1369 | if( exists ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1370 | /* One of the journals pointed to by the master journal exists. |
| 1371 | ** Open it and check if it points at the master journal. If |
| 1372 | ** so, return without deleting the master journal file. |
| 1373 | */ |
drh | 3b7b78b | 2004-11-24 01:16:43 +0000 | [diff] [blame] | 1374 | int c; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1375 | int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL); |
| 1376 | rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1377 | if( rc!=SQLITE_OK ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1378 | goto delmaster_out; |
| 1379 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1380 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1381 | rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1382 | sqlite3OsClose(pJournal); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 1383 | if( rc!=SQLITE_OK ){ |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 1384 | goto delmaster_out; |
| 1385 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1386 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1387 | c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0; |
drh | 3b7b78b | 2004-11-24 01:16:43 +0000 | [diff] [blame] | 1388 | if( c ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1389 | /* We have a match. Do not delete the master journal file. */ |
| 1390 | goto delmaster_out; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1391 | } |
| 1392 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1393 | zJournal += (sqlite3Strlen30(zJournal)+1); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1397 | rc = sqlite3OsDelete(pVfs, zMaster, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1398 | |
| 1399 | delmaster_out: |
| 1400 | if( zMasterJournal ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1401 | sqlite3_free(zMasterJournal); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1402 | } |
| 1403 | if( master_open ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1404 | sqlite3OsClose(pMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1405 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1406 | sqlite3_free(pMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1407 | return rc; |
| 1408 | } |
| 1409 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1410 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1411 | static void pager_truncate_cache(Pager *pPager); |
| 1412 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1413 | /* |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1414 | ** Truncate the main file of the given pager to the number of pages |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1415 | ** indicated. Also truncate the cached representation of the file. |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1416 | ** |
| 1417 | ** Might might be the case that the file on disk is smaller than nPage. |
| 1418 | ** This can happen, for example, if we are in the middle of a transaction |
| 1419 | ** which has extended the file size and the new pages are still all held |
| 1420 | ** in cache, then an INSERT or UPDATE does a statement rollback. Some |
| 1421 | ** operating system implementations can get confused if you try to |
| 1422 | ** truncate a file to some size that is larger than it currently is, |
danielk1977 | 06e11af | 2008-05-06 18:13:26 +0000 | [diff] [blame] | 1423 | ** so detect this case and write a single zero byte to the end of the new |
| 1424 | ** file instead. |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1425 | */ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 1426 | static int pager_truncate(Pager *pPager, Pgno nPage){ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1427 | int rc = SQLITE_OK; |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1428 | if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){ |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1429 | i64 currentSize, newSize; |
| 1430 | rc = sqlite3OsFileSize(pPager->fd, ¤tSize); |
| 1431 | newSize = pPager->pageSize*(i64)nPage; |
danielk1977 | 06e11af | 2008-05-06 18:13:26 +0000 | [diff] [blame] | 1432 | if( rc==SQLITE_OK && currentSize!=newSize ){ |
| 1433 | if( currentSize>newSize ){ |
| 1434 | rc = sqlite3OsTruncate(pPager->fd, newSize); |
| 1435 | }else{ |
| 1436 | rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1); |
| 1437 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 1438 | if( rc==SQLITE_OK ){ |
| 1439 | pPager->dbFileSize = nPage; |
| 1440 | } |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1441 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1442 | } |
| 1443 | if( rc==SQLITE_OK ){ |
| 1444 | pPager->dbSize = nPage; |
| 1445 | pager_truncate_cache(pPager); |
| 1446 | } |
| 1447 | return rc; |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | /* |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1451 | ** Set the sectorSize for the given pager. |
| 1452 | ** |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1453 | ** The sector size is at least as big as the sector size reported |
| 1454 | ** by sqlite3OsSectorSize(). The minimum sector size is 512. |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1455 | */ |
| 1456 | static void setSectorSize(Pager *pPager){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1457 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 1458 | if( !pPager->tempFile ){ |
| 1459 | /* Sector size doesn't matter for temporary files. Also, the file |
| 1460 | ** may not have been opened yet, in whcih case the OsSectorSize() |
| 1461 | ** call will segfault. |
| 1462 | */ |
| 1463 | pPager->sectorSize = sqlite3OsSectorSize(pPager->fd); |
| 1464 | } |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1465 | if( pPager->sectorSize<512 ){ |
| 1466 | pPager->sectorSize = 512; |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1471 | ** Playback the journal and thus restore the database file to |
| 1472 | ** the state it was in before we started making changes. |
| 1473 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1474 | ** The journal file format is as follows: |
| 1475 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1476 | ** (1) 8 byte prefix. A copy of aJournalMagic[]. |
| 1477 | ** (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] | 1478 | ** in the journal. If this value is 0xffffffff, then compute the |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1479 | ** number of page records from the journal size. |
| 1480 | ** (3) 4 byte big-endian integer which is the initial value for the |
| 1481 | ** sanity checksum. |
| 1482 | ** (4) 4 byte integer which is the number of pages to truncate the |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1483 | ** database to during a rollback. |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1484 | ** (5) 4 byte big-endian integer which is the sector size. The header |
| 1485 | ** is this many bytes in size. |
| 1486 | ** (6) 4 byte big-endian integer which is the page case. |
| 1487 | ** (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] | 1488 | ** name. The value may be zero (indicate that there is no master |
| 1489 | ** journal.) |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1490 | ** (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] | 1491 | ** and might be shorter than the value read from (5). If the first byte |
| 1492 | ** of the name is \000 then there is no master journal. The master |
| 1493 | ** journal name is stored in UTF-8. |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1494 | ** (9) Zero or more pages instances, each as follows: |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1495 | ** + 4 byte page number. |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1496 | ** + pPager->pageSize bytes of data. |
| 1497 | ** + 4 byte checksum |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1498 | ** |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1499 | ** When we speak of the journal header, we mean the first 8 items above. |
| 1500 | ** Each entry in the journal is an instance of the 9th item. |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1501 | ** |
| 1502 | ** Call the value from the second bullet "nRec". nRec is the number of |
| 1503 | ** valid page entries in the journal. In most cases, you can compute the |
| 1504 | ** value of nRec from the size of the journal file. But if a power |
| 1505 | ** failure occurred while the journal was being written, it could be the |
| 1506 | ** case that the size of the journal file had already been increased but |
| 1507 | ** the extra entries had not yet made it safely to disk. In such a case, |
| 1508 | ** the value of nRec computed from the file size would be too large. For |
| 1509 | ** that reason, we always use the nRec value in the header. |
| 1510 | ** |
| 1511 | ** If the nRec value is 0xffffffff it means that nRec should be computed |
| 1512 | ** from the file size. This value is used when the user selects the |
| 1513 | ** no-sync option for the journal. A power failure could lead to corruption |
| 1514 | ** in this case. But for things like temporary table (which will be |
| 1515 | ** deleted when the power is restored) we don't care. |
| 1516 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1517 | ** If the file opened as the journal file is not a well-formed |
danielk1977 | ece80f1 | 2004-06-23 01:05:26 +0000 | [diff] [blame] | 1518 | ** journal file then all pages up to the first corrupted page are rolled |
| 1519 | ** back (or no pages if the journal header is corrupted). The journal file |
| 1520 | ** is then deleted and SQLITE_OK returned, just as if no corruption had |
| 1521 | ** been encountered. |
| 1522 | ** |
| 1523 | ** If an I/O or malloc() error occurs, the journal-file is not deleted |
| 1524 | ** and an error code is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1525 | */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1526 | static int pager_playback(Pager *pPager, int isHot){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1527 | sqlite3_vfs *pVfs = pPager->pVfs; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1528 | i64 szJ; /* Size of the journal file in bytes */ |
danielk1977 | c3e8f5e | 2004-06-28 01:16:46 +0000 | [diff] [blame] | 1529 | u32 nRec; /* Number of Records in the journal */ |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 1530 | u32 u; /* Unsigned loop counter */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1531 | Pgno mxPg = 0; /* Size of the original file in pages */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1532 | int rc; /* Result code of a subroutine */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1533 | int res = 1; /* Value returned by sqlite3OsAccess() */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1534 | char *zMaster = 0; /* Name of master journal file if any */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1535 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1536 | /* Figure out how many records are in the journal. Abort early if |
| 1537 | ** the journal is empty. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1538 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1539 | assert( pPager->journalOpen ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1540 | rc = sqlite3OsFileSize(pPager->jfd, &szJ); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1541 | if( rc!=SQLITE_OK || szJ==0 ){ |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1542 | goto end_playback; |
| 1543 | } |
drh | 240c579 | 2004-02-08 00:40:52 +0000 | [diff] [blame] | 1544 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1545 | /* Read the master journal name from the journal, if it is present. |
| 1546 | ** If a master journal file name is specified, but the file is not |
| 1547 | ** present on disk, then the journal is not hot and does not need to be |
| 1548 | ** played back. |
drh | 240c579 | 2004-02-08 00:40:52 +0000 | [diff] [blame] | 1549 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1550 | zMaster = pPager->pTmpSpace; |
| 1551 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1552 | if( rc==SQLITE_OK && zMaster[0] ){ |
| 1553 | rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1554 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1555 | zMaster = 0; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1556 | if( rc!=SQLITE_OK || !res ){ |
danielk1977 | ce98bba | 2008-04-03 10:13:01 +0000 | [diff] [blame] | 1557 | goto end_playback; |
| 1558 | } |
| 1559 | pPager->journalOff = 0; |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1560 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1561 | /* This loop terminates either when the readJournalHdr() call returns |
| 1562 | ** SQLITE_DONE or an IO error occurs. */ |
| 1563 | while( 1 ){ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1564 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1565 | /* Read the next journal header from the journal file. If there are |
| 1566 | ** not enough bytes left in the journal file for a complete header, or |
| 1567 | ** it is corrupted, then a process must of failed while writing it. |
| 1568 | ** This indicates nothing more needs to be rolled back. |
| 1569 | */ |
| 1570 | rc = readJournalHdr(pPager, szJ, &nRec, &mxPg); |
| 1571 | if( rc!=SQLITE_OK ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1572 | if( rc==SQLITE_DONE ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1573 | rc = SQLITE_OK; |
| 1574 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1575 | goto end_playback; |
| 1576 | } |
| 1577 | |
| 1578 | /* If nRec is 0xffffffff, then this journal was created by a process |
| 1579 | ** working in no-sync mode. This means that the rest of the journal |
| 1580 | ** file consists of pages, there are no more journal headers. Compute |
| 1581 | ** the value of nRec based on this assumption. |
| 1582 | */ |
| 1583 | if( nRec==0xffffffff ){ |
| 1584 | assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1585 | nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager)); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1588 | /* 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] | 1589 | ** process and if this is the final header in the journal, then it means |
| 1590 | ** that this part of the journal was being filled but has not yet been |
| 1591 | ** synced to disk. Compute the number of pages based on the remaining |
| 1592 | ** size of the file. |
| 1593 | ** |
| 1594 | ** The third term of the test was added to fix ticket #2565. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1595 | */ |
drh | 8940f4e | 2007-08-11 00:26:20 +0000 | [diff] [blame] | 1596 | if( nRec==0 && !isHot && |
| 1597 | pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1598 | nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager)); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1601 | /* If this is the first header read from the journal, truncate the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 1602 | ** database file back to its original size. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1603 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1604 | if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){ |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1605 | rc = pager_truncate(pPager, mxPg); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1606 | if( rc!=SQLITE_OK ){ |
| 1607 | goto end_playback; |
| 1608 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1611 | /* Copy original pages out of the journal and back into the database file. |
| 1612 | */ |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 1613 | for(u=0; u<nRec; u++){ |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1614 | rc = pager_playback_one_page(pPager, 1, pPager->journalOff, 0, 0); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1615 | if( rc!=SQLITE_OK ){ |
| 1616 | if( rc==SQLITE_DONE ){ |
| 1617 | rc = SQLITE_OK; |
| 1618 | pPager->journalOff = szJ; |
| 1619 | break; |
| 1620 | }else{ |
drh | a9625ea | 2008-09-03 00:08:29 +0000 | [diff] [blame] | 1621 | /* If we are unable to rollback, then the database is probably |
| 1622 | ** going to end up being corrupt. It is corrupt to us, anyhow. |
| 1623 | ** Perhaps the next process to come along can fix it.... |
| 1624 | */ |
danielk1977 | 98c2190 | 2008-09-23 16:41:29 +0000 | [diff] [blame] | 1625 | rc = SQLITE_CORRUPT_BKPT; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1626 | goto end_playback; |
| 1627 | } |
| 1628 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1629 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1630 | } |
drh | 580eeaf | 2006-02-24 03:09:37 +0000 | [diff] [blame] | 1631 | /*NOTREACHED*/ |
| 1632 | assert( 0 ); |
drh | 4a0681e | 2003-02-13 01:58:20 +0000 | [diff] [blame] | 1633 | |
| 1634 | end_playback: |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 1635 | if( rc==SQLITE_OK ){ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1636 | zMaster = pPager->pTmpSpace; |
| 1637 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
| 1638 | } |
| 1639 | if( rc==SQLITE_OK ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 1640 | rc = pager_end_transaction(pPager, zMaster[0]!='\0'); |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 1641 | } |
danielk1977 | c56774e | 2008-10-07 11:51:20 +0000 | [diff] [blame] | 1642 | if( rc==SQLITE_OK && zMaster[0] && res ){ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1643 | /* If there was a master journal and this routine will return success, |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1644 | ** see if it is possible to delete the master journal. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1645 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1646 | rc = pager_delmaster(pPager, zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1647 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1648 | |
| 1649 | /* The Pager.sectorSize variable may have been updated while rolling |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 1650 | ** back a journal created by a process with a different sector size |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1651 | ** value. Reset it to the correct value for this process. |
| 1652 | */ |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1653 | setSectorSize(pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1654 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1658 | ** Playback a savepoint. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1659 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1660 | static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1661 | i64 szJ; /* Size of the full journal */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1662 | i64 iHdrOff; /* End of first segment of main-journal records */ |
| 1663 | Pgno ii; /* Loop counter */ |
| 1664 | int rc; /* Return code */ |
| 1665 | Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1666 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1667 | /* Allocate a bitvec to use to store the set of pages rolled back */ |
| 1668 | if( pSavepoint ){ |
| 1669 | pDone = sqlite3BitvecCreate(pSavepoint->nOrig); |
| 1670 | if( !pDone ){ |
| 1671 | return SQLITE_NOMEM; |
| 1672 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1673 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1674 | |
| 1675 | /* Truncate the database back to the size it was before the |
| 1676 | ** savepoint being reverted was opened. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1677 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 1678 | rc = pager_truncate(pPager, pSavepoint?pSavepoint->nOrig:pPager->dbOrigSize); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 1679 | assert( pPager->state>=PAGER_SHARED ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1680 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1681 | /* Now roll back all main journal file records that occur after byte |
| 1682 | ** byte offset PagerSavepoint.iOffset that have a page number less than |
| 1683 | ** or equal to PagerSavepoint.nOrig. As each record is played back, |
| 1684 | ** the corresponding bit in bitvec PagerSavepoint.pInSavepoint is |
| 1685 | ** cleared. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1686 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1687 | szJ = pPager->journalOff; |
| 1688 | if( pSavepoint ){ |
| 1689 | iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ; |
| 1690 | pPager->journalOff = pSavepoint->iOffset; |
| 1691 | while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){ |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1692 | rc = pager_playback_one_page(pPager, 1, pPager->journalOff, 1, pDone); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1693 | assert( rc!=SQLITE_DONE ); |
| 1694 | } |
| 1695 | }else{ |
| 1696 | pPager->journalOff = 0; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1697 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1698 | while( rc==SQLITE_OK && pPager->journalOff<szJ ){ |
danielk1977 | c81806f | 2009-01-01 15:20:37 +0000 | [diff] [blame] | 1699 | u32 nJRec = 0; /* Number of Journal Records */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1700 | u32 dummy; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1701 | rc = readJournalHdr(pPager, szJ, &nJRec, &dummy); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1702 | assert( rc!=SQLITE_DONE ); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1703 | if( nJRec==0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1704 | nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8); |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1705 | } |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 1706 | for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){ |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1707 | rc = pager_playback_one_page(pPager, 1, pPager->journalOff, 1, pDone); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1708 | assert( rc!=SQLITE_DONE ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | assert( rc!=SQLITE_OK || pPager->journalOff==szJ ); |
| 1712 | |
| 1713 | /* Now roll back pages from the sub-journal. */ |
| 1714 | if( pSavepoint ){ |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame^] | 1715 | for(ii=pSavepoint->iSubRec; rc==SQLITE_OK&&ii<(u32)pPager->stmtNRec; ii++){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1716 | i64 offset = ii*(4+pPager->pageSize); |
danielk1977 | ecfef98 | 2008-12-20 08:39:57 +0000 | [diff] [blame] | 1717 | rc = pager_playback_one_page(pPager, 0, offset, 1, pDone); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1718 | assert( rc!=SQLITE_DONE ); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1719 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1720 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1721 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1722 | sqlite3BitvecDestroy(pDone); |
| 1723 | if( rc==SQLITE_OK ){ |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1724 | pPager->journalOff = szJ; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1725 | } |
| 1726 | return rc; |
| 1727 | } |
| 1728 | |
| 1729 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1730 | ** Change the maximum number of in-memory pages that are allowed. |
| 1731 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1732 | void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1733 | sqlite3PcacheSetCachesize(pPager->pPCache, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1737 | ** Adjust the robustness of the database to damage due to OS crashes |
| 1738 | ** or power failures by changing the number of syncs()s when writing |
| 1739 | ** the rollback journal. There are three levels: |
| 1740 | ** |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1741 | ** OFF sqlite3OsSync() is never called. This is the default |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1742 | ** for temporary and transient files. |
| 1743 | ** |
| 1744 | ** NORMAL The journal is synced once before writes begin on the |
| 1745 | ** database. This is normally adequate protection, but |
| 1746 | ** it is theoretically possible, though very unlikely, |
| 1747 | ** that an inopertune power failure could leave the journal |
| 1748 | ** in a state which would cause damage to the database |
| 1749 | ** when it is rolled back. |
| 1750 | ** |
| 1751 | ** FULL The journal is synced twice before writes begin on the |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1752 | ** database (with some additional information - the nRec field |
| 1753 | ** of the journal header - being written in between the two |
| 1754 | ** syncs). If we assume that writing a |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1755 | ** single disk sector is atomic, then this mode provides |
| 1756 | ** assurance that the journal will not be corrupted to the |
| 1757 | ** point of causing damage to the database during rollback. |
| 1758 | ** |
| 1759 | ** Numeric values associated with these states are OFF==1, NORMAL=2, |
| 1760 | ** and FULL=3. |
| 1761 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1762 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1763 | void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1764 | pPager->noSync = (level==1 || pPager->tempFile) ?1:0; |
| 1765 | pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1766 | pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1767 | if( pPager->noSync ) pPager->needSync = 0; |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1768 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1769 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1770 | |
| 1771 | /* |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1772 | ** The following global variable is incremented whenever the library |
| 1773 | ** attempts to open a temporary file. This information is used for |
| 1774 | ** testing and analysis only. |
| 1775 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1776 | #ifdef SQLITE_TEST |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1777 | int sqlite3_opentemp_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1778 | #endif |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1779 | |
| 1780 | /* |
drh | 3f56e6e | 2007-03-15 01:16:47 +0000 | [diff] [blame] | 1781 | ** Open a temporary file. |
| 1782 | ** |
| 1783 | ** Write the file descriptor into *fd. Return SQLITE_OK on success or some |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1784 | ** other error code if we fail. The OS will automatically delete the temporary |
| 1785 | ** file when it is closed. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1786 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1787 | static int sqlite3PagerOpentemp( |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1788 | Pager *pPager, /* The pager object */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1789 | sqlite3_file *pFile, /* Write the file descriptor here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1790 | int vfsFlags /* Flags passed through to the VFS */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1791 | ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1792 | int rc; |
drh | 3f56e6e | 2007-03-15 01:16:47 +0000 | [diff] [blame] | 1793 | |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1794 | #ifdef SQLITE_TEST |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1795 | sqlite3_opentemp_count++; /* Used for testing and analysis only */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1796 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1797 | |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1798 | vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
| 1799 | SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1800 | rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0); |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 1801 | assert( rc!=SQLITE_OK || pFile->pMethods ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1802 | return rc; |
| 1803 | } |
| 1804 | |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 1805 | static int pagerStress(void *,PgHdr *); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1806 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1807 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1808 | ** 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] | 1809 | ** 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] | 1810 | ** the first call to sqlite3PagerGet() and is only held open until the |
| 1811 | ** last page is released using sqlite3PagerUnref(). |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1812 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1813 | ** If zFilename is NULL then a randomly-named temporary file is created |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1814 | ** and used as the file to be cached. The file will be deleted |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1815 | ** automatically when it is closed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1816 | ** |
| 1817 | ** If zFilename is ":memory:" then all information is held in cache. |
| 1818 | ** It is never written to disk. This can be used to implement an |
| 1819 | ** in-memory database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1820 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1821 | int sqlite3PagerOpen( |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 1822 | sqlite3_vfs *pVfs, /* The virtual file system to use */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1823 | Pager **ppPager, /* Return the Pager structure here */ |
| 1824 | const char *zFilename, /* Name of the database file to open */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1825 | int nExtra, /* Extra bytes append to each in-memory page */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1826 | int flags, /* flags controlling this file */ |
| 1827 | int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1828 | ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1829 | u8 *pPtr; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1830 | Pager *pPager = 0; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1831 | int rc = SQLITE_OK; |
| 1832 | int i; |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 1833 | int tempFile = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1834 | int memDb = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1835 | int readOnly = 0; |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 1836 | int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; |
| 1837 | int noReadlock = (flags & PAGER_NO_READLOCK)!=0; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1838 | int journalFileSize; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1839 | int pcacheSize = sqlite3PcacheSize(); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 1840 | int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1841 | char *zPathname = 0; |
| 1842 | int nPathname = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1843 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1844 | if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){ |
| 1845 | journalFileSize = sqlite3JournalSize(pVfs); |
| 1846 | }else{ |
| 1847 | journalFileSize = sqlite3MemJournalSize(); |
| 1848 | } |
| 1849 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 1850 | /* The default return is a NULL pointer */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1851 | *ppPager = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1852 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1853 | /* Compute and store the full pathname in an allocated buffer pointed |
| 1854 | ** to by zPathname, length nPathname. Or, if this is a temporary file, |
| 1855 | ** leave both nPathname and zPathname set to 0. |
| 1856 | */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1857 | if( zFilename && zFilename[0] ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1858 | nPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1859 | zPathname = sqlite3Malloc(nPathname*2); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1860 | if( zPathname==0 ){ |
| 1861 | return SQLITE_NOMEM; |
| 1862 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1863 | #ifndef SQLITE_OMIT_MEMORYDB |
| 1864 | if( strcmp(zFilename,":memory:")==0 ){ |
| 1865 | memDb = 1; |
| 1866 | zPathname[0] = 0; |
| 1867 | }else |
| 1868 | #endif |
| 1869 | { |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1870 | rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1871 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1872 | if( rc!=SQLITE_OK ){ |
| 1873 | sqlite3_free(zPathname); |
| 1874 | return rc; |
| 1875 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1876 | nPathname = sqlite3Strlen30(zPathname); |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 1877 | } |
drh | 99b90c3 | 2008-03-17 13:50:58 +0000 | [diff] [blame] | 1878 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1879 | /* Allocate memory for the pager structure */ |
| 1880 | pPager = sqlite3MallocZero( |
| 1881 | sizeof(*pPager) + /* Pager structure */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1882 | pcacheSize + /* PCache object */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 1883 | journalFileSize + /* The journal file structure */ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1884 | pVfs->szOsFile + /* The main db file */ |
| 1885 | journalFileSize * 2 + /* The two journal files */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1886 | 3*nPathname + 40 /* zFilename, zDirectory, zJournal */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1887 | ); |
| 1888 | if( !pPager ){ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1889 | sqlite3_free(zPathname); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1890 | return SQLITE_NOMEM; |
| 1891 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1892 | pPager->pPCache = (PCache *)&pPager[1]; |
| 1893 | pPtr = ((u8 *)&pPager[1]) + pcacheSize; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1894 | pPager->vfsFlags = vfsFlags; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1895 | pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0]; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 1896 | pPager->sjfd = (sqlite3_file*)&pPtr[pVfs->szOsFile]; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1897 | pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile+journalFileSize]; |
| 1898 | pPager->zFilename = (char*)&pPtr[pVfs->szOsFile+2*journalFileSize]; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1899 | pPager->zDirectory = &pPager->zFilename[nPathname+1]; |
| 1900 | pPager->zJournal = &pPager->zDirectory[nPathname+1]; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1901 | pPager->pVfs = pVfs; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1902 | if( zPathname ){ |
| 1903 | memcpy(pPager->zFilename, zPathname, nPathname+1); |
| 1904 | sqlite3_free(zPathname); |
| 1905 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1906 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1907 | /* Open the pager file. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1908 | */ |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 1909 | if( zFilename && zFilename[0] && !memDb ){ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 1910 | if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1911 | rc = SQLITE_CANTOPEN; |
| 1912 | }else{ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1913 | int fout = 0; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1914 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, |
| 1915 | pPager->vfsFlags, &fout); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1916 | readOnly = (fout&SQLITE_OPEN_READONLY); |
| 1917 | |
| 1918 | /* If the file was successfully opened for read/write access, |
| 1919 | ** choose a default page size in case we have to create the |
| 1920 | ** database file. The default page size is the maximum of: |
| 1921 | ** |
| 1922 | ** + SQLITE_DEFAULT_PAGE_SIZE, |
| 1923 | ** + The value returned by sqlite3OsSectorSize() |
| 1924 | ** + The largest page size that can be written atomically. |
| 1925 | */ |
| 1926 | if( rc==SQLITE_OK && !readOnly ){ |
| 1927 | int iSectorSize = sqlite3OsSectorSize(pPager->fd); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 1928 | if( szPageDflt<iSectorSize ){ |
| 1929 | szPageDflt = iSectorSize; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1930 | } |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 1931 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1932 | { |
| 1933 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| 1934 | int ii; |
| 1935 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 1936 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 1937 | assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 1938 | for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){ |
| 1939 | if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii; |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 1940 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1941 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1942 | #endif |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 1943 | if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ |
| 1944 | szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1945 | } |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 1946 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1947 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1948 | }else{ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1949 | /* If a temporary file is requested, it is not opened immediately. |
| 1950 | ** In this case we accept the default page size and delay actually |
| 1951 | ** opening the file until the first call to OsWrite(). |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 1952 | ** |
| 1953 | ** This branch is also run for an in-memory database. An in-memory |
| 1954 | ** database is the same as a temp-file that is never written out to |
| 1955 | ** disk and uses an in-memory rollback journal. |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1956 | */ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1957 | tempFile = 1; |
| 1958 | pPager->state = PAGER_EXCLUSIVE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1959 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1960 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1961 | if( pPager && rc==SQLITE_OK ){ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 1962 | pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt); |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 1963 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1964 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1965 | /* If an error occured in either of the blocks above. |
| 1966 | ** Free the Pager structure and close the file. |
| 1967 | ** Since the pager is not allocated there is no need to set |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1968 | ** any Pager.errMask variables. |
| 1969 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1970 | if( !pPager || !pPager->pTmpSpace ){ |
| 1971 | sqlite3OsClose(pPager->fd); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1972 | sqlite3_free(pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1973 | return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1974 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1975 | nExtra = FORCE_ALIGNMENT(nExtra); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1976 | sqlite3PcacheOpen(szPageDflt, nExtra, !memDb, |
drh | 41d3027 | 2008-08-20 21:47:45 +0000 | [diff] [blame] | 1977 | !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1978 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1979 | PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename); |
| 1980 | IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename)) |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1981 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1982 | /* Fill in Pager.zDirectory[] */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 1983 | memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1984 | for(i=sqlite3Strlen30(pPager->zDirectory); |
| 1985 | i>0 && pPager->zDirectory[i-1]!='/'; i--){} |
drh | a76c82e | 2003-07-27 18:59:42 +0000 | [diff] [blame] | 1986 | if( i>0 ) pPager->zDirectory[i-1] = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1987 | |
drh | 99b90c3 | 2008-03-17 13:50:58 +0000 | [diff] [blame] | 1988 | /* Fill in Pager.zJournal[] */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1989 | if( zPathname ){ |
| 1990 | memcpy(pPager->zJournal, pPager->zFilename, nPathname); |
| 1991 | memcpy(&pPager->zJournal[nPathname], "-journal", 9); |
| 1992 | }else{ |
| 1993 | pPager->zJournal = 0; |
| 1994 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1995 | |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 1996 | /* pPager->journalOpen = 0; */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1997 | pPager->useJournal = (u8)useJournal; |
| 1998 | pPager->noReadlock = (noReadlock && readOnly) ?1:0; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 1999 | /* pPager->stmtOpen = 0; */ |
| 2000 | /* pPager->stmtInUse = 0; */ |
| 2001 | /* pPager->nRef = 0; */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2002 | pPager->dbSizeValid = (u8)memDb; |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 2003 | pPager->pageSize = szPageDflt; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2004 | /* pPager->stmtSize = 0; */ |
| 2005 | /* pPager->stmtJSize = 0; */ |
| 2006 | /* pPager->nPage = 0; */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2007 | pPager->mxPage = 100; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2008 | pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2009 | /* pPager->state = PAGER_UNLOCK; */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2010 | assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) ); |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2011 | /* pPager->errMask = 0; */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2012 | pPager->tempFile = (u8)tempFile; |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 2013 | assert( tempFile==PAGER_LOCKINGMODE_NORMAL |
| 2014 | || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 2015 | assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2016 | pPager->exclusiveMode = (u8)tempFile; |
| 2017 | pPager->memDb = (u8)memDb; |
| 2018 | pPager->readOnly = (u8)readOnly; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2019 | /* pPager->needSync = 0; */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2020 | pPager->noSync = (pPager->tempFile || !useJournal) ?1:0; |
| 2021 | pPager->fullSync = pPager->noSync ?0:1; |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2022 | pPager->sync_flags = SQLITE_SYNC_NORMAL; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2023 | /* pPager->pFirst = 0; */ |
| 2024 | /* pPager->pFirstSynced = 0; */ |
| 2025 | /* pPager->pLast = 0; */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2026 | pPager->nExtra = nExtra; |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 2027 | pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2028 | assert(pPager->fd->pMethods||tempFile); |
| 2029 | setSectorSize(pPager); |
| 2030 | if( memDb ){ |
| 2031 | pPager->journalMode = PAGER_JOURNALMODE_MEMORY; |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 2032 | } |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2033 | /* pPager->xBusyHandler = 0; */ |
| 2034 | /* pPager->pBusyHandlerArg = 0; */ |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2035 | /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2036 | *ppPager = pPager; |
| 2037 | return SQLITE_OK; |
| 2038 | } |
| 2039 | |
| 2040 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2041 | ** Set the busy handler function. |
| 2042 | */ |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2043 | void sqlite3PagerSetBusyhandler( |
| 2044 | Pager *pPager, |
| 2045 | int (*xBusyHandler)(void *), |
| 2046 | void *pBusyHandlerArg |
| 2047 | ){ |
| 2048 | pPager->xBusyHandler = xBusyHandler; |
| 2049 | pPager->pBusyHandlerArg = pBusyHandlerArg; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2053 | ** Set the reinitializer for this pager. If not NULL, the reinitializer |
| 2054 | ** is called when the content of a page in cache is restored to its original |
| 2055 | ** value as a result of a rollback. The callback gives higher-level code |
| 2056 | ** an opportunity to restore the EXTRA section to agree with the restored |
| 2057 | ** page data. |
| 2058 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 2059 | void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2060 | pPager->xReiniter = xReinit; |
| 2061 | } |
| 2062 | |
| 2063 | /* |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2064 | ** Set the page size to *pPageSize. If the suggest new page size is |
| 2065 | ** inappropriate, then an alternative page size is set to that |
| 2066 | ** value before returning. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2067 | */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2068 | int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){ |
danielk1977 | 1357d9f | 2008-09-16 05:12:24 +0000 | [diff] [blame] | 2069 | int rc = pPager->errCode; |
| 2070 | if( rc==SQLITE_OK ){ |
| 2071 | u16 pageSize = *pPageSize; |
| 2072 | assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) ); |
| 2073 | if( pageSize && pageSize!=pPager->pageSize |
| 2074 | && (pPager->memDb==0 || pPager->dbSize==0) |
| 2075 | && sqlite3PcacheRefCount(pPager->pPCache)==0 |
| 2076 | ){ |
| 2077 | char *pNew = (char *)sqlite3PageMalloc(pageSize); |
| 2078 | if( !pNew ){ |
| 2079 | rc = SQLITE_NOMEM; |
| 2080 | }else{ |
| 2081 | pager_reset(pPager); |
| 2082 | pPager->pageSize = pageSize; |
| 2083 | if( !pPager->memDb ) setSectorSize(pPager); |
| 2084 | sqlite3PageFree(pPager->pTmpSpace); |
| 2085 | pPager->pTmpSpace = pNew; |
| 2086 | sqlite3PcacheSetPageSize(pPager->pPCache, pageSize); |
| 2087 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2088 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2089 | *pPageSize = (u16)pPager->pageSize; |
drh | 1c7880e | 2005-05-20 20:01:55 +0000 | [diff] [blame] | 2090 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2091 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /* |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 2095 | ** Return a pointer to the "temporary page" buffer held internally |
| 2096 | ** by the pager. This is a buffer that is big enough to hold the |
| 2097 | ** entire content of a database page. This buffer is used internally |
| 2098 | ** during rollback and will be overwritten whenever a rollback |
| 2099 | ** occurs. But other modules are free to use it too, as long as |
| 2100 | ** no rollbacks are happening. |
| 2101 | */ |
| 2102 | void *sqlite3PagerTempSpace(Pager *pPager){ |
| 2103 | return pPager->pTmpSpace; |
| 2104 | } |
| 2105 | |
| 2106 | /* |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2107 | ** Attempt to set the maximum database page count if mxPage is positive. |
| 2108 | ** Make no changes if mxPage is zero or negative. And never reduce the |
| 2109 | ** maximum page count below the current size of the database. |
| 2110 | ** |
| 2111 | ** Regardless of mxPage, return the current maximum page count. |
| 2112 | */ |
| 2113 | int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ |
| 2114 | if( mxPage>0 ){ |
| 2115 | pPager->mxPgno = mxPage; |
| 2116 | } |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2117 | sqlite3PagerPagecount(pPager, 0); |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2118 | return pPager->mxPgno; |
| 2119 | } |
| 2120 | |
| 2121 | /* |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2122 | ** The following set of routines are used to disable the simulated |
| 2123 | ** I/O error mechanism. These routines are used to avoid simulated |
| 2124 | ** errors in places where we do not care about errors. |
| 2125 | ** |
| 2126 | ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops |
| 2127 | ** and generate no code. |
| 2128 | */ |
| 2129 | #ifdef SQLITE_TEST |
| 2130 | extern int sqlite3_io_error_pending; |
| 2131 | extern int sqlite3_io_error_hit; |
| 2132 | static int saved_cnt; |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2133 | void disable_simulated_io_errors(void){ |
| 2134 | saved_cnt = sqlite3_io_error_pending; |
| 2135 | sqlite3_io_error_pending = -1; |
| 2136 | } |
| 2137 | void enable_simulated_io_errors(void){ |
| 2138 | sqlite3_io_error_pending = saved_cnt; |
| 2139 | } |
| 2140 | #else |
drh | 152410f | 2005-11-05 15:11:22 +0000 | [diff] [blame] | 2141 | # define disable_simulated_io_errors() |
| 2142 | # define enable_simulated_io_errors() |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2143 | #endif |
| 2144 | |
| 2145 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2146 | ** Read the first N bytes from the beginning of the file into memory |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2147 | ** that pDest points to. |
| 2148 | ** |
| 2149 | ** No error checking is done. The rational for this is that this function |
| 2150 | ** may be called even if the file does not exist or contain a header. In |
| 2151 | ** these cases sqlite3OsRead() will return an error, to which the correct |
| 2152 | ** response is to zero the memory at pDest and continue. A real IO error |
| 2153 | ** will presumably recur and be picked up later (Todo: Think about this). |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2154 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2155 | int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){ |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2156 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2157 | memset(pDest, 0, N); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2158 | assert(pPager->fd->pMethods||pPager->tempFile); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2159 | if( pPager->fd->pMethods ){ |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2160 | IOTRACE(("DBHDR %p 0 %d\n", pPager, N)) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2161 | rc = sqlite3OsRead(pPager->fd, pDest, N, 0); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2162 | if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 2163 | rc = SQLITE_OK; |
| 2164 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2165 | } |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2166 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2170 | ** Return the total number of pages in the disk file associated with |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2171 | ** pPager. |
| 2172 | ** |
| 2173 | ** If the PENDING_BYTE lies on the page directly after the end of the |
| 2174 | ** file, then consider this page part of the file too. For example, if |
| 2175 | ** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the |
| 2176 | ** file is 4096 bytes, 5 is returned instead of 4. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2177 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2178 | int sqlite3PagerPagecount(Pager *pPager, int *pnPage){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2179 | i64 n = 0; |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 2180 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2181 | assert( pPager!=0 ); |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 2182 | if( pPager->errCode ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2183 | rc = pPager->errCode; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2184 | return rc; |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 2185 | } |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 2186 | if( pPager->dbSizeValid ){ |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2187 | n = pPager->dbSize; |
| 2188 | } else { |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2189 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 2190 | if( (pPager->fd->pMethods) |
| 2191 | && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){ |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 2192 | pager_error(pPager, rc); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2193 | return rc; |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2194 | } |
| 2195 | if( n>0 && n<pPager->pageSize ){ |
| 2196 | n = 1; |
| 2197 | }else{ |
| 2198 | n /= pPager->pageSize; |
| 2199 | } |
| 2200 | if( pPager->state!=PAGER_UNLOCK ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2201 | pPager->dbSize = (Pgno)n; |
| 2202 | pPager->dbFileSize = (Pgno)n; |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 2203 | pPager->dbSizeValid = 1; |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2204 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2205 | } |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2206 | if( n==(PENDING_BYTE/pPager->pageSize) ){ |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 2207 | n++; |
| 2208 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2209 | if( n>pPager->mxPgno ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2210 | pPager->mxPgno = (Pgno)n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2211 | } |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2212 | if( pnPage ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2213 | *pnPage = (int)n; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2214 | } |
| 2215 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2219 | ** Forward declaration |
| 2220 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2221 | static int syncJournal(Pager*); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2222 | |
| 2223 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2224 | ** This routine is used to truncate the cache when a database |
| 2225 | ** is truncated. Drop from the cache all pages whose pgno is |
| 2226 | ** larger than pPager->dbSize and is unreferenced. |
| 2227 | ** |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2228 | ** Referenced pages larger than pPager->dbSize are zeroed. |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2229 | ** |
| 2230 | ** Actually, at the point this routine is called, it would be |
| 2231 | ** an error to have a referenced page. But rather than delete |
| 2232 | ** that page and guarantee a subsequent segfault, it seems better |
| 2233 | ** to zero it and hope that we error out sanely. |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2234 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2235 | static void pager_truncate_cache(Pager *pPager){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2236 | sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2239 | /* |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2240 | ** 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] | 2241 | ** is currently not available. Repeat until the busy callback returns |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2242 | ** false or until the lock succeeds. |
| 2243 | ** |
| 2244 | ** Return SQLITE_OK on success and an error code if we cannot obtain |
| 2245 | ** the lock. |
| 2246 | */ |
| 2247 | static int pager_wait_on_lock(Pager *pPager, int locktype){ |
| 2248 | int rc; |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2249 | |
| 2250 | /* The OS lock values must be the same as the Pager lock values */ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2251 | assert( PAGER_SHARED==SHARED_LOCK ); |
| 2252 | assert( PAGER_RESERVED==RESERVED_LOCK ); |
| 2253 | assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2254 | |
| 2255 | /* If the file is currently unlocked then the size must be unknown */ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 2256 | assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2257 | |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2258 | if( pPager->state>=locktype ){ |
| 2259 | rc = SQLITE_OK; |
| 2260 | }else{ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2261 | do { |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2262 | rc = sqlite3OsLock(pPager->fd, locktype); |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2263 | }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) ); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2264 | if( rc==SQLITE_OK ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2265 | pPager->state = (u8)locktype; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2266 | IOTRACE(("LOCK %p %d\n", pPager, locktype)) |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | return rc; |
| 2270 | } |
| 2271 | |
| 2272 | /* |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2273 | ** Truncate the file to the number of pages specified. |
| 2274 | ** |
| 2275 | ** Unless an IO error occurs, this function is guaranteed to modify the |
| 2276 | ** database file itself. If an exclusive lock is not held when this function |
| 2277 | ** is called, one is obtained before truncating the file. |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2278 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2279 | int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2280 | int rc = SQLITE_OK; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2281 | assert( pPager->state>=PAGER_SHARED ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2282 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2283 | sqlite3PagerPagecount(pPager, 0); |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 2284 | if( pPager->errCode ){ |
| 2285 | rc = pPager->errCode; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2286 | }else if( nPage<pPager->dbFileSize ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2287 | rc = syncJournal(pPager); |
| 2288 | if( rc==SQLITE_OK ){ |
| 2289 | /* Get an exclusive lock on the database before truncating. */ |
| 2290 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| 2291 | } |
| 2292 | if( rc==SQLITE_OK ){ |
| 2293 | rc = pager_truncate(pPager, nPage); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2294 | } |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 2295 | } |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2296 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2297 | return rc; |
| 2298 | } |
| 2299 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2300 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2301 | /* |
| 2302 | ** Truncate the in-memory database file image to nPage pages. Unlike |
| 2303 | ** sqlite3PagerTruncate(), this function does not actually modify the |
| 2304 | ** database file on disk. It just sets the internal state of the pager |
| 2305 | ** object so that the truncation will be done when the current |
| 2306 | ** transaction is committed. |
| 2307 | */ |
| 2308 | void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ |
| 2309 | assert( pPager->dbSizeValid ); |
| 2310 | assert( pPager->dbSize>=nPage ); |
| 2311 | pPager->dbSize = nPage; |
| 2312 | } |
| 2313 | |
| 2314 | /* |
| 2315 | ** Return the current size of the database file image in pages. This |
| 2316 | ** function differs from sqlite3PagerPagecount() in two ways: |
| 2317 | ** |
| 2318 | ** a) It may only be called when at least one reference to a database |
| 2319 | ** page is held. This guarantees that the database size is already |
| 2320 | ** known and a call to sqlite3OsFileSize() is not required. |
| 2321 | ** |
| 2322 | ** b) The return value is not adjusted for the locking page. |
| 2323 | */ |
| 2324 | Pgno sqlite3PagerImageSize(Pager *pPager){ |
| 2325 | assert( pPager->dbSizeValid ); |
| 2326 | return pPager->dbSize; |
| 2327 | } |
| 2328 | #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
| 2329 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2330 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2331 | ** Shutdown the page cache. Free all memory and close all files. |
| 2332 | ** |
| 2333 | ** If a transaction was in progress when this routine is called, that |
| 2334 | ** transaction is rolled back. All outstanding pages are invalidated |
| 2335 | ** and their memory is freed. Any attempt to use a page associated |
| 2336 | ** with this page cache after this function returns will likely |
| 2337 | ** result in a coredump. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2338 | ** |
| 2339 | ** This function always succeeds. If a transaction is active an attempt |
| 2340 | ** is made to roll it back. If an error occurs during the rollback |
| 2341 | ** a hot journal may be left in the filesystem but no error is returned |
| 2342 | ** to the caller. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2343 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2344 | int sqlite3PagerClose(Pager *pPager){ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 2345 | |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2346 | disable_simulated_io_errors(); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 2347 | sqlite3BeginBenignMalloc(); |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 2348 | pPager->errCode = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 2349 | pPager->exclusiveMode = 0; |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2350 | pager_reset(pPager); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2351 | if( !MEMDB ){ |
| 2352 | pagerUnlockAndRollback(pPager); |
| 2353 | } |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2354 | enable_simulated_io_errors(); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 2355 | sqlite3EndBenignMalloc(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2356 | PAGERTRACE2("CLOSE %d\n", PAGERID(pPager)); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2357 | IOTRACE(("CLOSE %p\n", pPager)) |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2358 | if( pPager->journalOpen ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2359 | sqlite3OsClose(pPager->jfd); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2360 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 2361 | sqlite3BitvecDestroy(pPager->pInJournal); |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 2362 | sqlite3BitvecDestroy(pPager->pAlwaysRollback); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2363 | releaseAllSavepoint(pPager); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2364 | sqlite3OsClose(pPager->fd); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 2365 | /* Temp files are automatically deleted by the OS |
| 2366 | ** if( pPager->tempFile ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 2367 | ** sqlite3OsDelete(pPager->zFilename); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 2368 | ** } |
| 2369 | */ |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 2370 | |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 2371 | sqlite3PageFree(pPager->pTmpSpace); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2372 | sqlite3PcacheClose(pPager->pPCache); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2373 | sqlite3_free(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2374 | return SQLITE_OK; |
| 2375 | } |
| 2376 | |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 2377 | #if !defined(NDEBUG) || defined(SQLITE_TEST) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2378 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2379 | ** Return the page number for the given page data. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2380 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2381 | Pgno sqlite3PagerPagenumber(DbPage *p){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2382 | return p->pgno; |
| 2383 | } |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 2384 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2385 | |
| 2386 | /* |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2387 | ** Increment the reference count for a page. The input pointer is |
| 2388 | ** a reference to the page data. |
| 2389 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2390 | int sqlite3PagerRef(DbPage *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2391 | sqlite3PcacheRef(pPg); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2392 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2396 | ** Sync the journal. In other words, make sure all the pages that have |
| 2397 | ** been written to the journal have actually reached the surface of the |
| 2398 | ** disk. It is not safe to modify the original database file until after |
| 2399 | ** the journal has been synced. If the original database is modified before |
| 2400 | ** the journal is synced and a power failure occurs, the unsynced journal |
| 2401 | ** data would be lost and we would be unable to completely rollback the |
| 2402 | ** database changes. Database corruption would occur. |
| 2403 | ** |
| 2404 | ** This routine also updates the nRec field in the header of the journal. |
| 2405 | ** (See comments on the pager_playback() routine for additional information.) |
| 2406 | ** If the sync mode is FULL, two syncs will occur. First the whole journal |
| 2407 | ** is synced, then the nRec field is updated, then a second sync occurs. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2408 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2409 | ** For temporary databases, we do not care if we are able to rollback |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2410 | ** after a power failure, so no sync occurs. |
| 2411 | ** |
| 2412 | ** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which |
| 2413 | ** the database is stored, then OsSync() is never called on the journal |
| 2414 | ** file. In this case all that is required is to update the nRec field in |
| 2415 | ** the journal header. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2416 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2417 | ** This routine clears the needSync field of every page current held in |
| 2418 | ** memory. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2419 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2420 | static int syncJournal(Pager *pPager){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2421 | int rc = SQLITE_OK; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2422 | |
| 2423 | /* Sync the journal before modifying the main database |
| 2424 | ** (assuming there is a journal and it needs to be synced.) |
| 2425 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2426 | if( pPager->needSync ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2427 | assert( !pPager->tempFile ); |
| 2428 | if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2429 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2430 | assert( pPager->journalOpen ); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2431 | |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2432 | if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2433 | /* Write the nRec value into the journal file header. If in |
| 2434 | ** full-synchronous mode, sync the journal first. This ensures that |
| 2435 | ** all data has really hit the disk before nRec is updated to mark |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2436 | ** it as a candidate for rollback. |
| 2437 | ** |
| 2438 | ** This is not required if the persistent media supports the |
| 2439 | ** SAFE_APPEND property. Because in this case it is not possible |
| 2440 | ** for garbage data to be appended to the file, the nRec field |
| 2441 | ** is populated with 0xFFFFFFFF when the journal header is written |
| 2442 | ** and never needs to be updated. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2443 | */ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2444 | i64 jrnlOff; |
| 2445 | if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2446 | PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2447 | IOTRACE(("JSYNC %p\n", pPager)) |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2448 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags); |
drh | d8d66e8 | 2003-02-12 02:10:15 +0000 | [diff] [blame] | 2449 | if( rc!=0 ) return rc; |
| 2450 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2451 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2452 | jrnlOff = pPager->journalHdr + sizeof(aJournalMagic); |
| 2453 | IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4)); |
| 2454 | rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec); |
drh | b4746b9 | 2005-09-09 01:32:06 +0000 | [diff] [blame] | 2455 | if( rc ) return rc; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2456 | } |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2457 | if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
| 2458 | PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); |
| 2459 | IOTRACE(("JSYNC %p\n", pPager)) |
| 2460 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| |
| 2461 | (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) |
| 2462 | ); |
| 2463 | if( rc!=0 ) return rc; |
| 2464 | } |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2465 | pPager->journalStarted = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2466 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2467 | pPager->needSync = 0; |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2468 | |
| 2469 | /* Erase the needSync flag from every page. |
| 2470 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 2471 | sqlite3PcacheClearSyncFlags(pPager->pPCache); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2472 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2473 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2474 | return rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | /* |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2478 | ** Given a list of pages (connected by the PgHdr.pDirty pointer) write |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2479 | ** every one of those pages out to the database file. No calls are made |
| 2480 | ** to the page-cache to mark the pages as clean. It is the responsibility |
| 2481 | ** of the caller to use PcacheCleanAll() or PcacheMakeClean() to mark |
| 2482 | ** the pages as clean. |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2483 | */ |
| 2484 | static int pager_write_pagelist(PgHdr *pList){ |
| 2485 | Pager *pPager; |
| 2486 | int rc; |
| 2487 | |
| 2488 | if( pList==0 ) return SQLITE_OK; |
| 2489 | pPager = pList->pPager; |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2490 | |
| 2491 | /* At this point there may be either a RESERVED or EXCLUSIVE lock on the |
| 2492 | ** database file. If there is already an EXCLUSIVE lock, the following |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2493 | ** calls to sqlite3OsLock() are no-ops. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2494 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2495 | ** Moving the lock from RESERVED to EXCLUSIVE actually involves going |
| 2496 | ** through an intermediate state PENDING. A PENDING lock prevents new |
| 2497 | ** readers from attaching to the database but is unsufficient for us to |
| 2498 | ** write. The idea of a PENDING lock is to prevent new readers from |
| 2499 | ** coming in while we wait for existing readers to clear. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2500 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2501 | ** While the pager is in the RESERVED state, the original database file |
| 2502 | ** is unchanged and we can rollback without having to playback the |
| 2503 | ** journal into the original database file. Once we transition to |
| 2504 | ** EXCLUSIVE, it means the database file has been changed and any rollback |
| 2505 | ** will require a journal playback. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2506 | */ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2507 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2508 | if( rc!=SQLITE_OK ){ |
| 2509 | return rc; |
| 2510 | } |
| 2511 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2512 | while( pList ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2513 | |
| 2514 | /* If the file has not yet been opened, open it now. */ |
| 2515 | if( !pPager->fd->pMethods ){ |
| 2516 | assert(pPager->tempFile); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2517 | rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2518 | if( rc ) return rc; |
| 2519 | } |
| 2520 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2521 | /* If there are dirty pages in the page cache with page numbers greater |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2522 | ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2523 | ** make the file smaller (presumably by auto-vacuum code). Do not write |
| 2524 | ** any such pages to the file. |
| 2525 | */ |
danielk1977 | 33e3216 | 2008-08-23 18:53:08 +0000 | [diff] [blame] | 2526 | if( pList->pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2527 | i64 offset = (pList->pgno-1)*(i64)pPager->pageSize; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2528 | char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6); |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2529 | |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 2530 | PAGERTRACE4("STORE %d page %d hash(%08x)\n", |
| 2531 | PAGERID(pPager), pList->pgno, pager_pagehash(pList)); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2532 | IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno)); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2533 | rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2534 | PAGER_INCR(sqlite3_pager_writedb_count); |
| 2535 | PAGER_INCR(pPager->nWrite); |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 2536 | if( pList->pgno==1 ){ |
| 2537 | memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); |
| 2538 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2539 | if( pList->pgno>pPager->dbFileSize ){ |
| 2540 | pPager->dbFileSize = pList->pgno; |
| 2541 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2542 | } |
| 2543 | #ifndef NDEBUG |
| 2544 | else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2545 | PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2546 | } |
| 2547 | #endif |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2548 | if( rc ) return rc; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 2549 | #ifdef SQLITE_CHECK_PAGES |
| 2550 | pList->pageHash = pager_pagehash(pList); |
| 2551 | #endif |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2552 | pList = pList->pDirty; |
| 2553 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2554 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2555 | return SQLITE_OK; |
| 2556 | } |
| 2557 | |
| 2558 | /* |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2559 | ** This function is called by the pcache layer when it has reached some |
| 2560 | ** soft memory limit. The argument is a pointer to a purgeable Pager |
| 2561 | ** object. This function attempts to make a single dirty page that has no |
| 2562 | ** outstanding references (if one exists) clean so that it can be recycled |
| 2563 | ** by the pcache layer. |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2564 | */ |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2565 | static int pagerStress(void *p, PgHdr *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2566 | Pager *pPager = (Pager *)p; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2567 | int rc = SQLITE_OK; |
drh | 8f2e9a1 | 2008-02-26 14:46:04 +0000 | [diff] [blame] | 2568 | |
danielk1977 | 8c20014 | 2008-08-25 07:12:28 +0000 | [diff] [blame] | 2569 | if( pPager->doNotSync ){ |
| 2570 | return SQLITE_OK; |
| 2571 | } |
| 2572 | |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2573 | assert( pPg->flags&PGHDR_DIRTY ); |
| 2574 | if( pPager->errCode==SQLITE_OK ){ |
| 2575 | if( pPg->flags&PGHDR_NEED_SYNC ){ |
| 2576 | rc = syncJournal(pPager); |
| 2577 | if( rc==SQLITE_OK && pPager->fullSync && |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2578 | !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) && |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2579 | !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) |
| 2580 | ){ |
| 2581 | pPager->nRec = 0; |
| 2582 | rc = writeJournalHdr(pPager); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2583 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2584 | } |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 2585 | if( rc==SQLITE_OK ){ |
| 2586 | pPg->pDirty = 0; |
| 2587 | rc = pager_write_pagelist(pPg); |
| 2588 | } |
| 2589 | if( rc!=SQLITE_OK ){ |
| 2590 | pager_error(pPager, rc); |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | if( rc==SQLITE_OK ){ |
| 2595 | sqlite3PcacheMakeClean(pPg); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2596 | } |
| 2597 | return rc; |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2600 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2601 | /* |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 2602 | ** Return 1 if there is a hot journal on the given pager. |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 2603 | ** A hot journal is one that needs to be played back. |
| 2604 | ** |
| 2605 | ** If the current size of the database file is 0 but a journal file |
| 2606 | ** exists, that is probably an old journal left over from a prior |
| 2607 | ** database with the same name. Just delete the journal. |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 2608 | ** |
| 2609 | ** Return negative if unable to determine the status of the journal. |
drh | 82ed1e5 | 2008-04-25 12:25:42 +0000 | [diff] [blame] | 2610 | ** |
| 2611 | ** This routine does not open the journal file to examine its |
| 2612 | ** content. Hence, the journal might contain the name of a master |
| 2613 | ** journal file that has been deleted, and hence not be hot. Or |
| 2614 | ** the header of the journal might be zeroed out. This routine |
| 2615 | ** does not discover these cases of a non-hot journal - if the |
| 2616 | ** journal file exists and is not empty this routine assumes it |
| 2617 | ** is hot. The pager_playback() routine will discover that the |
| 2618 | ** journal file is not really hot and will no-op. |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 2619 | */ |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2620 | static int hasHotJournal(Pager *pPager, int *pExists){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2621 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2622 | int rc = SQLITE_OK; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2623 | int exists = 0; |
| 2624 | int locked = 0; |
drh | 0a846f9 | 2008-08-25 17:23:29 +0000 | [diff] [blame] | 2625 | assert( pPager!=0 ); |
| 2626 | assert( pPager->useJournal ); |
| 2627 | assert( pPager->fd->pMethods ); |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2628 | *pExists = 0; |
drh | 0a846f9 | 2008-08-25 17:23:29 +0000 | [diff] [blame] | 2629 | rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 2630 | if( rc==SQLITE_OK && exists ){ |
| 2631 | rc = sqlite3OsCheckReservedLock(pPager->fd, &locked); |
| 2632 | } |
| 2633 | if( rc==SQLITE_OK && exists && !locked ){ |
| 2634 | int nPage; |
| 2635 | rc = sqlite3PagerPagecount(pPager, &nPage); |
| 2636 | if( rc==SQLITE_OK ){ |
| 2637 | if( nPage==0 ){ |
| 2638 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
| 2639 | }else{ |
| 2640 | *pExists = 1; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 2641 | } |
| 2642 | } |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 2643 | } |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2644 | return rc; |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2648 | ** Read the content of page pPg out of the database file. |
| 2649 | */ |
| 2650 | static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){ |
| 2651 | int rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2652 | i64 offset; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2653 | assert( MEMDB==0 ); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2654 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 2655 | if( !pPager->fd->pMethods ){ |
| 2656 | return SQLITE_IOERR_SHORT_READ; |
| 2657 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2658 | offset = (pgno-1)*(i64)pPager->pageSize; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2659 | rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2660 | PAGER_INCR(sqlite3_pager_readdb_count); |
| 2661 | PAGER_INCR(pPager->nRead); |
| 2662 | IOTRACE(("PGIN %p %d\n", pPager, pgno)); |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 2663 | if( pgno==1 ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2664 | memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24], |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 2665 | sizeof(pPager->dbFileVers)); |
| 2666 | } |
danielk1977 | 3084952 | 2008-08-21 04:35:18 +0000 | [diff] [blame] | 2667 | CODEC1(pPager, pPg->pData, pPg->pgno, 3); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 2668 | PAGERTRACE4("FETCH %d page %d hash(%08x)\n", |
| 2669 | PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2670 | return rc; |
| 2671 | } |
| 2672 | |
| 2673 | |
| 2674 | /* |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2675 | ** This function is called to obtain the shared lock required before |
| 2676 | ** data may be read from the pager cache. If the shared lock has already |
| 2677 | ** been obtained, this function is a no-op. |
danielk1977 | 393f068 | 2007-03-31 10:00:48 +0000 | [diff] [blame] | 2678 | ** |
| 2679 | ** Immediately after obtaining the shared lock (if required), this function |
| 2680 | ** checks for a hot-journal file. If one is found, an emergency rollback |
| 2681 | ** is performed immediately. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2682 | */ |
| 2683 | static int pagerSharedLock(Pager *pPager){ |
| 2684 | int rc = SQLITE_OK; |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2685 | int isErrorReset = 0; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2686 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2687 | /* If this database is opened for exclusive access, has no outstanding |
| 2688 | ** page references and is in an error-state, now is the chance to clear |
| 2689 | ** the error. Discard the contents of the pager-cache and treat any |
| 2690 | ** open journal file as a hot-journal. |
| 2691 | */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2692 | if( !MEMDB && pPager->exclusiveMode |
| 2693 | && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode |
| 2694 | ){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2695 | if( pPager->journalOpen ){ |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2696 | isErrorReset = 1; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2697 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2698 | pPager->errCode = SQLITE_OK; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 2699 | pager_reset(pPager); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | /* If the pager is still in an error state, do not proceed. The error |
| 2703 | ** state will be cleared at some point in the future when all page |
| 2704 | ** references are dropped and the cache can be discarded. |
| 2705 | */ |
| 2706 | if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
| 2707 | return pPager->errCode; |
| 2708 | } |
| 2709 | |
danielk1977 | d300b8a | 2008-08-01 10:50:23 +0000 | [diff] [blame] | 2710 | if( pPager->state==PAGER_UNLOCK || isErrorReset ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2711 | sqlite3_vfs *pVfs = pPager->pVfs; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2712 | int isHotJournal = 0; |
shane | 049fc21 | 2008-10-22 16:26:47 +0000 | [diff] [blame] | 2713 | assert( !MEMDB ); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2714 | assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); |
| 2715 | if( !pPager->noReadlock ){ |
| 2716 | rc = pager_wait_on_lock(pPager, SHARED_LOCK); |
| 2717 | if( rc!=SQLITE_OK ){ |
| 2718 | assert( pPager->state==PAGER_UNLOCK ); |
| 2719 | return pager_error(pPager, rc); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2720 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2721 | assert( pPager->state>=SHARED_LOCK ); |
| 2722 | } |
| 2723 | |
| 2724 | /* If a journal file exists, and there is no RESERVED lock on the |
| 2725 | ** database file, then it either needs to be played back or deleted. |
| 2726 | */ |
| 2727 | if( !isErrorReset ){ |
| 2728 | rc = hasHotJournal(pPager, &isHotJournal); |
| 2729 | if( rc!=SQLITE_OK ){ |
| 2730 | goto failed; |
| 2731 | } |
| 2732 | } |
| 2733 | if( isErrorReset || isHotJournal ){ |
| 2734 | /* Get an EXCLUSIVE lock on the database file. At this point it is |
| 2735 | ** important that a RESERVED lock is not obtained on the way to the |
| 2736 | ** EXCLUSIVE lock. If it were, another process might open the |
| 2737 | ** database file, detect the RESERVED lock, and conclude that the |
| 2738 | ** database is safe to read while this process is still rolling it |
| 2739 | ** back. |
| 2740 | ** |
| 2741 | ** Because the intermediate RESERVED lock is not requested, the |
| 2742 | ** second process will get to this point in the code and fail to |
| 2743 | ** obtain its own EXCLUSIVE lock on the database file. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2744 | */ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2745 | if( pPager->state<EXCLUSIVE_LOCK ){ |
| 2746 | rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2747 | if( rc!=SQLITE_OK ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 2748 | rc = pager_error(pPager, rc); |
| 2749 | goto failed; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2750 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2751 | pPager->state = PAGER_EXCLUSIVE; |
| 2752 | } |
| 2753 | |
| 2754 | /* Open the journal for read/write access. This is because in |
| 2755 | ** exclusive-access mode the file descriptor will be kept open and |
| 2756 | ** possibly used for a transaction later on. On some systems, the |
| 2757 | ** OsTruncate() call used in exclusive-access mode also requires |
| 2758 | ** a read/write file handle. |
| 2759 | */ |
| 2760 | if( !isErrorReset && pPager->journalOpen==0 ){ |
| 2761 | int res; |
| 2762 | rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res); |
| 2763 | if( rc==SQLITE_OK ){ |
| 2764 | if( res ){ |
| 2765 | int fout = 0; |
| 2766 | int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL; |
| 2767 | assert( !pPager->tempFile ); |
| 2768 | rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout); |
| 2769 | assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); |
danielk1977 | 281d8bd | 2008-10-29 07:01:56 +0000 | [diff] [blame] | 2770 | if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){ |
| 2771 | rc = SQLITE_CANTOPEN; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2772 | sqlite3OsClose(pPager->jfd); |
| 2773 | } |
| 2774 | }else{ |
| 2775 | /* If the journal does not exist, that means some other process |
| 2776 | ** has already rolled it back */ |
| 2777 | rc = SQLITE_BUSY; |
| 2778 | } |
| 2779 | } |
| 2780 | } |
| 2781 | if( rc!=SQLITE_OK ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2782 | goto failed; |
| 2783 | } |
| 2784 | pPager->journalOpen = 1; |
| 2785 | pPager->journalStarted = 0; |
| 2786 | pPager->journalOff = 0; |
| 2787 | pPager->setMaster = 0; |
| 2788 | pPager->journalHdr = 0; |
| 2789 | |
| 2790 | /* Playback and delete the journal. Drop the database write |
| 2791 | ** lock and reacquire the read lock. |
| 2792 | */ |
| 2793 | rc = pager_playback(pPager, 1); |
| 2794 | if( rc!=SQLITE_OK ){ |
| 2795 | rc = pager_error(pPager, rc); |
| 2796 | goto failed; |
| 2797 | } |
| 2798 | assert(pPager->state==PAGER_SHARED || |
| 2799 | (pPager->exclusiveMode && pPager->state>PAGER_SHARED) |
| 2800 | ); |
| 2801 | } |
| 2802 | |
| 2803 | if( sqlite3PcachePagecount(pPager->pPCache)>0 ){ |
| 2804 | /* The shared-lock has just been acquired on the database file |
| 2805 | ** and there are already pages in the cache (from a previous |
| 2806 | ** read or write transaction). Check to see if the database |
| 2807 | ** has been modified. If the database has changed, flush the |
| 2808 | ** cache. |
| 2809 | ** |
| 2810 | ** Database changes is detected by looking at 15 bytes beginning |
| 2811 | ** at offset 24 into the file. The first 4 of these 16 bytes are |
| 2812 | ** a 32-bit counter that is incremented with each change. The |
| 2813 | ** other bytes change randomly with each file change when |
| 2814 | ** a codec is in use. |
| 2815 | ** |
| 2816 | ** There is a vanishingly small chance that a change will not be |
| 2817 | ** detected. The chance of an undetected change is so small that |
| 2818 | ** it can be neglected. |
| 2819 | */ |
| 2820 | char dbFileVers[sizeof(pPager->dbFileVers)]; |
| 2821 | sqlite3PagerPagecount(pPager, 0); |
| 2822 | |
| 2823 | if( pPager->errCode ){ |
| 2824 | rc = pPager->errCode; |
| 2825 | goto failed; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 2828 | assert( pPager->dbSizeValid ); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2829 | if( pPager->dbSize>0 ){ |
| 2830 | IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); |
| 2831 | rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); |
| 2832 | if( rc!=SQLITE_OK ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 2833 | goto failed; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2834 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2835 | }else{ |
| 2836 | memset(dbFileVers, 0, sizeof(dbFileVers)); |
| 2837 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2838 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2839 | if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ |
| 2840 | pager_reset(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2841 | } |
| 2842 | } |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 2843 | assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED ); |
| 2844 | if( pPager->state==PAGER_UNLOCK ){ |
| 2845 | pPager->state = PAGER_SHARED; |
| 2846 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 2849 | failed: |
| 2850 | if( rc!=SQLITE_OK ){ |
| 2851 | /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */ |
| 2852 | pager_unlock(pPager); |
| 2853 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2854 | return rc; |
| 2855 | } |
| 2856 | |
| 2857 | /* |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 2858 | ** Make sure we have the content for a page. If the page was |
| 2859 | ** previously acquired with noContent==1, then the content was |
| 2860 | ** just initialized to zeros instead of being read from disk. |
| 2861 | ** But now we need the real data off of disk. So make sure we |
| 2862 | ** have it. Read it in if we do not have it already. |
| 2863 | */ |
| 2864 | static int pager_get_content(PgHdr *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2865 | if( pPg->flags&PGHDR_NEED_READ ){ |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 2866 | int rc = readDbPage(pPg->pPager, pPg, pPg->pgno); |
| 2867 | if( rc==SQLITE_OK ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2868 | pPg->flags &= ~PGHDR_NEED_READ; |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 2869 | }else{ |
| 2870 | return rc; |
| 2871 | } |
| 2872 | } |
| 2873 | return SQLITE_OK; |
| 2874 | } |
| 2875 | |
| 2876 | /* |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2877 | ** If the reference count has reached zero, and the pager is not in the |
| 2878 | ** middle of a write transaction or opened in exclusive mode, unlock it. |
| 2879 | */ |
| 2880 | static void pagerUnlockIfUnused(Pager *pPager){ |
| 2881 | if( (sqlite3PcacheRefCount(pPager->pPCache)==0) |
| 2882 | && (!pPager->exclusiveMode || pPager->journalOff>0) |
| 2883 | ){ |
| 2884 | pagerUnlockAndRollback(pPager); |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | /* |
| 2889 | ** Drop a page from the cache using sqlite3PcacheDrop(). |
| 2890 | ** |
| 2891 | ** If this means there are now no pages with references to them, a rollback |
| 2892 | ** occurs and the lock on the database is removed. |
| 2893 | */ |
| 2894 | static void pagerDropPage(DbPage *pPg){ |
| 2895 | Pager *pPager = pPg->pPager; |
| 2896 | sqlite3PcacheDrop(pPg); |
| 2897 | pagerUnlockIfUnused(pPager); |
| 2898 | } |
| 2899 | |
| 2900 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2901 | ** Acquire a page. |
| 2902 | ** |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 2903 | ** 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] | 2904 | ** This read lock is dropped when the last page is released. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2905 | ** |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 2906 | ** This routine works for any page number greater than 0. If the database |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2907 | ** file is smaller than the requested page, then no actual disk |
| 2908 | ** read occurs and the memory image of the page is initialized to |
| 2909 | ** all zeros. The extra data appended to a page is always initialized |
| 2910 | ** to zeros the first time a page is loaded into memory. |
| 2911 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2912 | ** The acquisition might fail for several reasons. In all cases, |
| 2913 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2914 | ** |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 2915 | ** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2916 | ** 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] | 2917 | ** in memory, this routine goes to disk to read it in whereas Lookup() |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2918 | ** just returns 0. This routine acquires a read-lock the first time it |
| 2919 | ** 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] | 2920 | ** Since Lookup() never goes to disk, it never has to deal with locks |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2921 | ** or journal files. |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 2922 | ** |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2923 | ** If noContent is false, the page contents are actually read from disk. |
| 2924 | ** If noContent is true, it means that we do not care about the contents |
| 2925 | ** of the page at this time, so do not do a disk read. Just fill in the |
| 2926 | ** page content with zeros. But mark the fact that we have not read the |
| 2927 | ** content by setting the PgHdr.needRead flag. Later on, if |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 2928 | ** sqlite3PagerWrite() is called on this page or if this routine is |
| 2929 | ** called again with noContent==0, that means that the content is needed |
| 2930 | ** and the disk read should occur at that point. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2931 | */ |
danielk1977 | 65e0ff3 | 2008-09-19 09:14:43 +0000 | [diff] [blame] | 2932 | int sqlite3PagerAcquire( |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2933 | Pager *pPager, /* The pager open on the database file */ |
| 2934 | Pgno pgno, /* Page number to fetch */ |
| 2935 | DbPage **ppPage, /* Write a pointer to the page here */ |
| 2936 | int noContent /* Do not bother reading content from disk if true */ |
| 2937 | ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2938 | PgHdr *pPg = 0; |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 2939 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2940 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2941 | assert( pPager->state==PAGER_UNLOCK |
| 2942 | || sqlite3PcacheRefCount(pPager->pPCache)>0 |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 2943 | || pgno==1 |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2944 | ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2945 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2946 | /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page |
| 2947 | ** number greater than this, or zero, is requested. |
| 2948 | */ |
drh | 267cb32 | 2005-09-16 17:16:52 +0000 | [diff] [blame] | 2949 | if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2950 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2953 | /* Make sure we have not hit any critical errors. |
| 2954 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 2955 | assert( pPager!=0 ); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 2956 | *ppPage = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2957 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2958 | /* If this is the first page accessed, then get a SHARED lock |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 2959 | ** on the database file. pagerSharedLock() is a no-op if |
| 2960 | ** a database lock is already held. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2961 | */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2962 | rc = pagerSharedLock(pPager); |
| 2963 | if( rc!=SQLITE_OK ){ |
| 2964 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2965 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2966 | assert( pPager->state!=PAGER_UNLOCK ); |
| 2967 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2968 | rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg); |
| 2969 | if( rc!=SQLITE_OK ){ |
| 2970 | return rc; |
| 2971 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2972 | if( pPg->pPager==0 ){ |
| 2973 | /* The pager cache has created a new page. Its content needs to |
| 2974 | ** be initialized. |
| 2975 | */ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 2976 | int nMax; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2977 | PAGER_INCR(pPager->nMiss); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2978 | pPg->pPager = pPager; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2979 | memset(pPg->pExtra, 0, pPager->nExtra); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2980 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2981 | rc = sqlite3PagerPagecount(pPager, &nMax); |
| 2982 | if( rc!=SQLITE_OK ){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2983 | sqlite3PagerUnref(pPg); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 2984 | return rc; |
| 2985 | } |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 2986 | |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 2987 | if( nMax<(int)pgno || MEMDB || noContent ){ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2988 | if( pgno>pPager->mxPgno ){ |
danielk1977 | de3bea7 | 2007-05-09 15:56:39 +0000 | [diff] [blame] | 2989 | sqlite3PagerUnref(pPg); |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2990 | return SQLITE_FULL; |
| 2991 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2992 | memset(pPg->pData, 0, pPager->pageSize); |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 2993 | if( noContent ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 2994 | pPg->flags |= PGHDR_NEED_READ; |
| 2995 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2996 | IOTRACE(("ZERO %p %d\n", pPager, pgno)); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2997 | }else{ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2998 | rc = readDbPage(pPager, pPg, pgno); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2999 | if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3000 | /* sqlite3PagerUnref(pPg); */ |
| 3001 | pagerDropPage(pPg); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 3002 | return rc; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 3003 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3004 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3005 | #ifdef SQLITE_CHECK_PAGES |
| 3006 | pPg->pageHash = pager_pagehash(pPg); |
| 3007 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3008 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3009 | /* The requested page is in the page cache. */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3010 | assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3011 | PAGER_INCR(pPager->nHit); |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3012 | if( !noContent ){ |
| 3013 | rc = pager_get_content(pPg); |
| 3014 | if( rc ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3015 | sqlite3PagerUnref(pPg); |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3016 | return rc; |
| 3017 | } |
| 3018 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3019 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3020 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3021 | *ppPage = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3022 | return SQLITE_OK; |
| 3023 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3024 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3025 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3026 | ** Acquire a page if it is already in the in-memory cache. Do |
| 3027 | ** not read the page from disk. Return a pointer to the page, |
| 3028 | ** or 0 if the page is not in cache. |
| 3029 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3030 | ** See also sqlite3PagerGet(). The difference between this routine |
| 3031 | ** and sqlite3PagerGet() is that _get() will go to the disk and read |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3032 | ** in the page if the page is not already in cache. This routine |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3033 | ** returns NULL if the page is not in cache or if a disk I/O error |
| 3034 | ** has ever happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3035 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3036 | DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3037 | PgHdr *pPg = 0; |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 3038 | assert( pPager!=0 ); |
| 3039 | assert( pgno!=0 ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3040 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3041 | if( (pPager->state!=PAGER_UNLOCK) |
| 3042 | && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL) |
| 3043 | ){ |
| 3044 | sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3045 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3046 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3047 | return pPg; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3051 | ** Release a page. |
| 3052 | ** |
| 3053 | ** If the number of references to the page drop to zero, then the |
| 3054 | ** page is added to the LRU list. When all references to all pages |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3055 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3056 | ** removed. |
| 3057 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3058 | int sqlite3PagerUnref(DbPage *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3059 | if( pPg ){ |
| 3060 | Pager *pPager = pPg->pPager; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3061 | sqlite3PcacheRelease(pPg); |
| 3062 | pagerUnlockIfUnused(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3063 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3064 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3067 | static int openSubJournal(Pager *pPager){ |
| 3068 | int rc = SQLITE_OK; |
| 3069 | if( pPager->journalOpen && !pPager->sjfd->pMethods ){ |
| 3070 | if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ |
| 3071 | sqlite3MemJournalOpen(pPager->sjfd); |
| 3072 | }else{ |
| 3073 | rc = sqlite3PagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL); |
| 3074 | } |
| 3075 | } |
| 3076 | return rc; |
| 3077 | } |
| 3078 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3079 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3080 | ** Create a journal file for pPager. There should already be a RESERVED |
| 3081 | ** or EXCLUSIVE lock on the database file when this routine is called. |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3082 | ** |
| 3083 | ** Return SQLITE_OK if everything. Return an error code and release the |
| 3084 | ** write lock if anything goes wrong. |
| 3085 | */ |
| 3086 | static int pager_open_journal(Pager *pPager){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3087 | sqlite3_vfs *pVfs = pPager->pVfs; |
| 3088 | int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE); |
| 3089 | |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3090 | int rc; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3091 | assert( pPager->state>=PAGER_RESERVED ); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3092 | assert( pPager->useJournal ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3093 | assert( pPager->pInJournal==0 ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3094 | sqlite3PagerPagecount(pPager, 0); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3095 | pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3096 | if( pPager->pInJournal==0 ){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3097 | rc = SQLITE_NOMEM; |
| 3098 | goto failed_to_open_journal; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3099 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3100 | |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3101 | if( pPager->journalOpen==0 ){ |
| 3102 | if( pPager->tempFile ){ |
| 3103 | flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); |
| 3104 | }else{ |
| 3105 | flags |= (SQLITE_OPEN_MAIN_JOURNAL); |
drh | 600e46a | 2007-03-28 01:59:33 +0000 | [diff] [blame] | 3106 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3107 | if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ |
| 3108 | sqlite3MemJournalOpen(pPager->jfd); |
| 3109 | rc = SQLITE_OK; |
| 3110 | }else{ |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3111 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3112 | rc = sqlite3JournalOpen( |
| 3113 | pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) |
| 3114 | ); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3115 | #else |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3116 | rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3117 | #endif |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3118 | } |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3119 | assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); |
| 3120 | pPager->journalOff = 0; |
| 3121 | pPager->setMaster = 0; |
| 3122 | pPager->journalHdr = 0; |
| 3123 | if( rc!=SQLITE_OK ){ |
| 3124 | if( rc==SQLITE_NOMEM ){ |
| 3125 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
| 3126 | } |
| 3127 | goto failed_to_open_journal; |
| 3128 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3129 | } |
| 3130 | pPager->journalOpen = 1; |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 3131 | pPager->journalStarted = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3132 | pPager->needSync = 0; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 3133 | pPager->nRec = 0; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3134 | if( pPager->errCode ){ |
| 3135 | rc = pPager->errCode; |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 3136 | goto failed_to_open_journal; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3137 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3138 | pPager->dbOrigSize = pPager->dbSize; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 3139 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 3140 | rc = writeJournalHdr(pPager); |
| 3141 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3142 | if( pPager->nSavepoint && rc==SQLITE_OK ){ |
| 3143 | rc = openSubJournal(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3144 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3145 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 3146 | rc = pager_end_transaction(pPager, 0); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3147 | if( rc==SQLITE_OK ){ |
| 3148 | rc = SQLITE_FULL; |
| 3149 | } |
| 3150 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3151 | return rc; |
| 3152 | |
| 3153 | failed_to_open_journal: |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3154 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 3155 | pPager->pInJournal = 0; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3156 | return rc; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | /* |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3160 | ** Acquire a write-lock on the database. The lock is removed when |
| 3161 | ** the any of the following happen: |
| 3162 | ** |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3163 | ** * sqlite3PagerCommitPhaseTwo() is called. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3164 | ** * sqlite3PagerRollback() is called. |
| 3165 | ** * sqlite3PagerClose() is called. |
| 3166 | ** * sqlite3PagerUnref() is called to on every outstanding page. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3167 | ** |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3168 | ** The first parameter to this routine is a pointer to any open page of the |
| 3169 | ** database file. Nothing changes about the page - it is used merely to |
| 3170 | ** acquire a pointer to the Pager structure and as proof that there is |
| 3171 | ** already a read-lock on the database. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3172 | ** |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3173 | ** The second parameter indicates how much space in bytes to reserve for a |
| 3174 | ** master journal file-name at the start of the journal when it is created. |
| 3175 | ** |
| 3176 | ** A journal file is opened if this is not a temporary file. For temporary |
| 3177 | ** files, the opening of the journal file is deferred until there is an |
| 3178 | ** actual need to write to the journal. |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3179 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3180 | ** 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] | 3181 | ** |
| 3182 | ** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file |
| 3183 | ** immediately instead of waiting until we try to flush the cache. The |
| 3184 | ** exFlag is ignored if a transaction is already active. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3185 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3186 | int sqlite3PagerBegin(DbPage *pPg, int exFlag){ |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3187 | Pager *pPager = pPg->pPager; |
| 3188 | int rc = SQLITE_OK; |
| 3189 | assert( pPg->nRef>0 ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3190 | assert( pPager->state!=PAGER_UNLOCK ); |
| 3191 | if( pPager->state==PAGER_SHARED ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3192 | assert( pPager->pInJournal==0 ); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3193 | assert( !MEMDB ); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3194 | rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK); |
| 3195 | if( rc==SQLITE_OK ){ |
| 3196 | pPager->state = PAGER_RESERVED; |
| 3197 | if( exFlag ){ |
| 3198 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 3199 | } |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3200 | } |
| 3201 | if( rc!=SQLITE_OK ){ |
| 3202 | return rc; |
| 3203 | } |
| 3204 | pPager->dirtyCache = 0; |
| 3205 | PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager)); |
| 3206 | if( pPager->useJournal && !pPager->tempFile |
| 3207 | && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
| 3208 | rc = pager_open_journal(pPager); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3209 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3210 | }else if( pPager->journalOpen && pPager->journalOff==0 ){ |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3211 | /* This happens when the pager was in exclusive-access mode the last |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3212 | ** time a (read or write) transaction was successfully concluded |
| 3213 | ** by this connection. Instead of deleting the journal file it was |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3214 | ** kept open and either was truncated to 0 bytes or its header was |
| 3215 | ** overwritten with zeros. |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3216 | */ |
| 3217 | assert( pPager->nRec==0 ); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3218 | assert( pPager->dbOrigSize==0 ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3219 | assert( pPager->pInJournal==0 ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3220 | sqlite3PagerPagecount(pPager, 0); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3221 | pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3222 | if( !pPager->pInJournal ){ |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3223 | rc = SQLITE_NOMEM; |
| 3224 | }else{ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3225 | pPager->dbOrigSize = pPager->dbSize; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3226 | rc = writeJournalHdr(pPager); |
| 3227 | } |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3228 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3229 | assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK ); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3230 | return rc; |
| 3231 | } |
| 3232 | |
| 3233 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3234 | ** Mark a data page as writeable. The page is written into the journal |
| 3235 | ** if it is not there already. This routine must be called before making |
| 3236 | ** changes to a page. |
| 3237 | ** |
| 3238 | ** The first time this routine is called, the pager creates a new |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3239 | ** journal and acquires a RESERVED lock on the database. If the RESERVED |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3240 | ** lock could not be acquired, this routine returns SQLITE_BUSY. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3241 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3242 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3243 | ** |
| 3244 | ** If the journal file could not be written because the disk is full, |
| 3245 | ** then this routine returns SQLITE_FULL and does an immediate rollback. |
| 3246 | ** All subsequent write attempts also return SQLITE_FULL until there |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3247 | ** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3248 | ** reset. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3249 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3250 | static int pager_write(PgHdr *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3251 | void *pData = pPg->pData; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 3252 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 3253 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 3254 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3255 | /* Check for errors |
| 3256 | */ |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3257 | if( pPager->errCode ){ |
| 3258 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3259 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3260 | if( pPager->readOnly ){ |
| 3261 | return SQLITE_PERM; |
| 3262 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3263 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 3264 | assert( !pPager->setMaster ); |
| 3265 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3266 | CHECK_PAGE(pPg); |
| 3267 | |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3268 | /* If this page was previously acquired with noContent==1, that means |
| 3269 | ** we didn't really read in the content of the page. This can happen |
| 3270 | ** (for example) when the page is being moved to the freelist. But |
| 3271 | ** now we are (perhaps) moving the page off of the freelist for |
| 3272 | ** reuse and we need to know its original content so that content |
| 3273 | ** can be stored in the rollback journal. So do the read at this |
| 3274 | ** time. |
| 3275 | */ |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3276 | rc = pager_get_content(pPg); |
| 3277 | if( rc ){ |
| 3278 | return rc; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3281 | /* Mark the page as dirty. If the page has already been written |
| 3282 | ** to the journal then we can return right away. |
| 3283 | */ |
drh | c047b9f | 2008-11-21 03:23:43 +0000 | [diff] [blame] | 3284 | sqlite3PcacheMakeDirty(pPg); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3285 | if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3286 | pPager->dirtyCache = 1; |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3287 | pPager->dbModified = 1; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3288 | }else{ |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3289 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3290 | /* If we get this far, it means that the page needs to be |
| 3291 | ** written to the transaction journal or the ckeckpoint journal |
| 3292 | ** or both. |
| 3293 | ** |
| 3294 | ** First check to see that the transaction journal exists and |
| 3295 | ** create it if it does not. |
| 3296 | */ |
| 3297 | assert( pPager->state!=PAGER_UNLOCK ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3298 | rc = sqlite3PagerBegin(pPg, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3299 | if( rc!=SQLITE_OK ){ |
| 3300 | return rc; |
| 3301 | } |
| 3302 | assert( pPager->state>=PAGER_RESERVED ); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3303 | if( !pPager->journalOpen && pPager->useJournal |
| 3304 | && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3305 | rc = pager_open_journal(pPager); |
| 3306 | if( rc!=SQLITE_OK ) return rc; |
| 3307 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3308 | pPager->dirtyCache = 1; |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3309 | pPager->dbModified = 1; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3310 | |
| 3311 | /* The transaction journal now exists and we have a RESERVED or an |
| 3312 | ** EXCLUSIVE lock on the main database file. Write the current page to |
| 3313 | ** the transaction journal if it is not there already. |
| 3314 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 3315 | if( !pageInJournal(pPg) && pPager->journalOpen ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3316 | if( pPg->pgno<=pPager->dbOrigSize ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3317 | u32 cksum; |
| 3318 | char *pData2; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3319 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3320 | /* We should never write to the journal file the page that |
| 3321 | ** contains the database locks. The following assert verifies |
| 3322 | ** that we do not. */ |
| 3323 | assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
| 3324 | pData2 = CODEC2(pPager, pData, pPg->pgno, 7); |
| 3325 | cksum = pager_cksum(pPager, (u8*)pData2); |
| 3326 | rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno); |
| 3327 | if( rc==SQLITE_OK ){ |
| 3328 | rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, |
| 3329 | pPager->journalOff + 4); |
| 3330 | pPager->journalOff += pPager->pageSize+4; |
| 3331 | } |
| 3332 | if( rc==SQLITE_OK ){ |
| 3333 | rc = write32bits(pPager->jfd, pPager->journalOff, cksum); |
| 3334 | pPager->journalOff += 4; |
| 3335 | } |
| 3336 | IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, |
| 3337 | pPager->journalOff, pPager->pageSize)); |
| 3338 | PAGER_INCR(sqlite3_pager_writej_count); |
| 3339 | PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n", |
| 3340 | PAGERID(pPager), pPg->pgno, |
| 3341 | ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 3342 | |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 3343 | /* Even if an IO or diskfull error occurred while journalling the |
| 3344 | ** page in the block above, set the need-sync flag for the page. |
| 3345 | ** Otherwise, when the transaction is rolled back, the logic in |
| 3346 | ** playback_one_page() will think that the page needs to be restored |
| 3347 | ** in the database file. And if an IO error occurs while doing so, |
| 3348 | ** then corruption may follow. |
| 3349 | */ |
| 3350 | if( !pPager->noSync ){ |
| 3351 | pPg->flags |= PGHDR_NEED_SYNC; |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 3352 | pPager->needSync = 1; |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3355 | /* An error has occured writing to the journal file. The |
| 3356 | ** transaction will be rolled back by the layer above. |
| 3357 | */ |
| 3358 | if( rc!=SQLITE_OK ){ |
| 3359 | return rc; |
| 3360 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 3361 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3362 | pPager->nRec++; |
| 3363 | assert( pPager->pInJournal!=0 ); |
| 3364 | sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3365 | addToSavepointBitvecs(pPager, pPg->pgno); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3366 | }else{ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3367 | if( !pPager->journalStarted && !pPager->noSync ){ |
| 3368 | pPg->flags |= PGHDR_NEED_SYNC; |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 3369 | pPager->needSync = 1; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3370 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3371 | PAGERTRACE4("APPEND %d page %d needSync=%d\n", |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3372 | PAGERID(pPager), pPg->pgno, |
| 3373 | ((pPg->flags&PGHDR_NEED_SYNC)?1:0)); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3374 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | /* If the statement journal is open and the page is not in it, |
| 3378 | ** then write the current page to the statement journal. Note that |
| 3379 | ** the statement journal format differs from the standard journal format |
| 3380 | ** in that it omits the checksums and the header. |
| 3381 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3382 | if( subjRequiresPage(pPg) ){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3383 | i64 offset = pPager->stmtNRec*(4+pPager->pageSize); |
| 3384 | char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3385 | assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3386 | rc = write32bits(pPager->sjfd, offset, pPg->pgno); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3387 | if( rc==SQLITE_OK ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3388 | rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4); |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 3389 | } |
danielk1977 | 67ddef6 | 2008-12-23 19:15:56 +0000 | [diff] [blame] | 3390 | PAGERTRACE3("STMT-JOURNAL %d page %d @ %d\n", PAGERID(pPager), pPg->pgno); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3391 | if( rc!=SQLITE_OK ){ |
| 3392 | return rc; |
| 3393 | } |
| 3394 | pPager->stmtNRec++; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3395 | assert( pPager->nSavepoint>0 ); |
| 3396 | addToSavepointBitvecs(pPager, pPg->pgno); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3397 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | /* Update the database size and return. |
| 3401 | */ |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 3402 | assert( pPager->state>=PAGER_SHARED ); |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 3403 | if( pPager->dbSize<pPg->pgno ){ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3404 | pPager->dbSize = pPg->pgno; |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 3405 | if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){ |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 3406 | pPager->dbSize++; |
| 3407 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3408 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 3409 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | /* |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3413 | ** This function is used to mark a data-page as writable. It uses |
| 3414 | ** pager_write() to open a journal file (if it is not already open) |
| 3415 | ** and write the page *pData to the journal. |
| 3416 | ** |
| 3417 | ** The difference between this function and pager_write() is that this |
| 3418 | ** function also deals with the special case where 2 or more pages |
| 3419 | ** fit on a single disk sector. In this case all co-resident pages |
| 3420 | ** must have been written to the journal file before returning. |
| 3421 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3422 | int sqlite3PagerWrite(DbPage *pDbPage){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3423 | int rc = SQLITE_OK; |
| 3424 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3425 | PgHdr *pPg = pDbPage; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3426 | Pager *pPager = pPg->pPager; |
| 3427 | Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); |
| 3428 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3429 | if( nPagePerSector>1 ){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3430 | Pgno nPageCount; /* Total number of pages in database file */ |
| 3431 | Pgno pg1; /* First page of the sector pPg is located on. */ |
| 3432 | int nPage; /* Number of pages starting at pg1 to journal */ |
| 3433 | int ii; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3434 | int needSync = 0; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3435 | |
| 3436 | /* Set the doNotSync flag to 1. This is because we cannot allow a journal |
| 3437 | ** header to be written between the pages journaled by this function. |
| 3438 | */ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3439 | assert( !MEMDB ); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3440 | assert( pPager->doNotSync==0 ); |
| 3441 | pPager->doNotSync = 1; |
| 3442 | |
| 3443 | /* This trick assumes that both the page-size and sector-size are |
| 3444 | ** an integer power of 2. It sets variable pg1 to the identifier |
| 3445 | ** of the first page of the sector pPg is located on. |
| 3446 | */ |
| 3447 | pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; |
| 3448 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3449 | sqlite3PagerPagecount(pPager, (int *)&nPageCount); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3450 | if( pPg->pgno>nPageCount ){ |
| 3451 | nPage = (pPg->pgno - pg1)+1; |
| 3452 | }else if( (pg1+nPagePerSector-1)>nPageCount ){ |
| 3453 | nPage = nPageCount+1-pg1; |
| 3454 | }else{ |
| 3455 | nPage = nPagePerSector; |
| 3456 | } |
| 3457 | assert(nPage>0); |
| 3458 | assert(pg1<=pPg->pgno); |
| 3459 | assert((pg1+nPage)>pPg->pgno); |
| 3460 | |
| 3461 | for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ |
| 3462 | Pgno pg = pg1+ii; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3463 | PgHdr *pPage; |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3464 | if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3465 | if( pg!=PAGER_MJ_PGNO(pPager) ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3466 | rc = sqlite3PagerGet(pPager, pg, &pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3467 | if( rc==SQLITE_OK ){ |
| 3468 | rc = pager_write(pPage); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3469 | if( pPage->flags&PGHDR_NEED_SYNC ){ |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3470 | needSync = 1; |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 3471 | assert(pPager->needSync); |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3472 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3473 | sqlite3PagerUnref(pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3474 | } |
| 3475 | } |
drh | c81945e | 2008-03-10 14:12:53 +0000 | [diff] [blame] | 3476 | }else if( (pPage = pager_lookup(pPager, pg))!=0 ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3477 | if( pPage->flags&PGHDR_NEED_SYNC ){ |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3478 | needSync = 1; |
| 3479 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3480 | sqlite3PagerUnref(pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3481 | } |
| 3482 | } |
| 3483 | |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3484 | /* If the PgHdr.needSync flag is set for any of the nPage pages |
| 3485 | ** starting at pg1, then it needs to be set for all of them. Because |
| 3486 | ** writing to any of these nPage pages may damage the others, the |
| 3487 | ** journal file must contain sync()ed copies of all of them |
| 3488 | ** before any of them can be written out to the database file. |
| 3489 | */ |
| 3490 | if( needSync ){ |
drh | b3df2e1 | 2008-09-17 20:06:26 +0000 | [diff] [blame] | 3491 | assert( !MEMDB && pPager->noSync==0 ); |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3492 | for(ii=0; ii<nPage && needSync; ii++){ |
| 3493 | PgHdr *pPage = pager_lookup(pPager, pg1+ii); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3494 | if( pPage ) pPage->flags |= PGHDR_NEED_SYNC; |
| 3495 | sqlite3PagerUnref(pPage); |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 3496 | } |
| 3497 | assert(pPager->needSync); |
| 3498 | } |
| 3499 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3500 | assert( pPager->doNotSync==1 ); |
| 3501 | pPager->doNotSync = 0; |
| 3502 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3503 | rc = pager_write(pDbPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3504 | } |
| 3505 | return rc; |
| 3506 | } |
| 3507 | |
| 3508 | /* |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 3509 | ** Return TRUE if the page given in the argument was previously passed |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3510 | ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3511 | ** to change the content of the page. |
| 3512 | */ |
danielk1977 | 7d3a666 | 2006-01-23 16:21:05 +0000 | [diff] [blame] | 3513 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3514 | int sqlite3PagerIswriteable(DbPage *pPg){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3515 | return pPg->flags&PGHDR_DIRTY; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3516 | } |
danielk1977 | 7d3a666 | 2006-01-23 16:21:05 +0000 | [diff] [blame] | 3517 | #endif |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3518 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3519 | /* |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3520 | ** 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] | 3521 | ** write the information on page pPg back to the disk, even though |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3522 | ** that page might be marked as dirty. This happens, for example, when |
| 3523 | ** the page has been added as a leaf of the freelist and so its |
| 3524 | ** content no longer matters. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3525 | ** |
| 3526 | ** The overlying software layer calls this routine when all of the data |
| 3527 | ** on the given page is unused. The pager marks the page as clean so |
| 3528 | ** that it does not get written to disk. |
| 3529 | ** |
| 3530 | ** Tests show that this optimization, together with the |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3531 | ** sqlite3PagerDontRollback() below, more than double the speed |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3532 | ** of large INSERT operations and quadruple the speed of large DELETEs. |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 3533 | ** |
| 3534 | ** When this routine is called, set the alwaysRollback flag to true. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3535 | ** Subsequent calls to sqlite3PagerDontRollback() for the same page |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 3536 | ** will thereafter be ignored. This is necessary to avoid a problem |
| 3537 | ** where a page with data is added to the freelist during one part of |
| 3538 | ** a transaction then removed from the freelist during a later part |
| 3539 | ** of the same transaction and reused for some other purpose. When it |
| 3540 | ** is first added to the freelist, this routine is called. When reused, |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3541 | ** the sqlite3PagerDontRollback() routine is called. But because the |
| 3542 | ** page contains critical data, we still need to be sure it gets |
| 3543 | ** rolled back in spite of the sqlite3PagerDontRollback() call. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3544 | */ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3545 | int sqlite3PagerDontWrite(DbPage *pDbPage){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3546 | PgHdr *pPg = pDbPage; |
| 3547 | Pager *pPager = pPg->pPager; |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3548 | int rc; |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 3549 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3550 | if( pPg->pgno>pPager->dbOrigSize ){ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3551 | return SQLITE_OK; |
| 3552 | } |
| 3553 | if( pPager->pAlwaysRollback==0 ){ |
| 3554 | assert( pPager->pInJournal ); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3555 | pPager->pAlwaysRollback = sqlite3BitvecCreate(pPager->dbOrigSize); |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3556 | if( !pPager->pAlwaysRollback ){ |
| 3557 | return SQLITE_NOMEM; |
| 3558 | } |
| 3559 | } |
| 3560 | rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno); |
| 3561 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3562 | if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 3563 | assert( pPager->state>=PAGER_SHARED ); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3564 | if( pPager->dbSize==pPg->pgno && pPager->dbOrigSize<pPager->dbSize ){ |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 3565 | /* If this pages is the last page in the file and the file has grown |
| 3566 | ** during the current transaction, then do NOT mark the page as clean. |
| 3567 | ** When the database file grows, we must make sure that the last page |
| 3568 | ** gets written at least once so that the disk file will be the correct |
| 3569 | ** size. If you do not write this page and the size of the file |
| 3570 | ** on the disk ends up being too small, that can lead to database |
| 3571 | ** corruption during the next transaction. |
| 3572 | */ |
| 3573 | }else{ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3574 | PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)); |
| 3575 | IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) |
danielk1977 | 33e3216 | 2008-08-23 18:53:08 +0000 | [diff] [blame] | 3576 | pPg->flags |= PGHDR_DONT_WRITE; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3577 | #ifdef SQLITE_CHECK_PAGES |
| 3578 | pPg->pageHash = pager_pagehash(pPg); |
| 3579 | #endif |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 3580 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3581 | } |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3582 | return rc; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | /* |
| 3586 | ** A call to this routine tells the pager that if a rollback occurs, |
| 3587 | ** it is not necessary to restore the data on the given page. This |
| 3588 | ** means that the pager does not have to record the given page in the |
| 3589 | ** rollback journal. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3590 | ** |
| 3591 | ** If we have not yet actually read the content of this page (if |
| 3592 | ** the PgHdr.needRead flag is set) then this routine acts as a promise |
| 3593 | ** that we will never need to read the page content in the future. |
| 3594 | ** so the needRead flag can be cleared at this point. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3595 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3596 | void sqlite3PagerDontRollback(DbPage *pPg){ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3597 | Pager *pPager = pPg->pPager; |
| 3598 | |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3599 | assert( pPager->state>=PAGER_RESERVED ); |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3600 | |
| 3601 | /* If the journal file is not open, or DontWrite() has been called on |
| 3602 | ** this page (DontWrite() sets the alwaysRollback flag), then this |
| 3603 | ** function is a no-op. |
| 3604 | */ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 3605 | if( pPager->journalOpen==0 |
| 3606 | || sqlite3BitvecTest(pPager->pAlwaysRollback, pPg->pgno) |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3607 | || pPg->pgno>pPager->dbOrigSize |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3608 | ){ |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 3609 | return; |
| 3610 | } |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3611 | |
drh | c5d0bd9 | 2008-04-14 01:00:57 +0000 | [diff] [blame] | 3612 | #ifdef SQLITE_SECURE_DELETE |
drh | 1feb7dd | 2008-11-19 18:30:29 +0000 | [diff] [blame] | 3613 | if( sqlite3BitvecTest(pPager->pInJournal, pPg->pgno)!=0 |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3614 | || pPg->pgno>pPager->dbOrigSize ){ |
drh | c5d0bd9 | 2008-04-14 01:00:57 +0000 | [diff] [blame] | 3615 | return; |
| 3616 | } |
| 3617 | #endif |
| 3618 | |
| 3619 | /* If SECURE_DELETE is disabled, then there is no way that this |
| 3620 | ** routine can be called on a page for which sqlite3PagerDontWrite() |
| 3621 | ** has not been previously called during the same transaction. |
| 3622 | ** And if DontWrite() has previously been called, the following |
| 3623 | ** conditions must be met. |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 3624 | ** |
| 3625 | ** (Later:) Not true. If the database is corrupted by having duplicate |
| 3626 | ** pages on the freelist (ex: corrupt9.test) then the following is not |
| 3627 | ** necessarily true: |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3628 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3629 | /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->dbOrigSize ); */ |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3630 | |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3631 | assert( pPager->pInJournal!=0 ); |
| 3632 | sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3633 | pPg->flags &= ~PGHDR_NEED_READ; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3634 | addToSavepointBitvecs(pPager, pPg->pgno); |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 3635 | PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)); |
| 3636 | IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno)) |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 3639 | |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3640 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3641 | ** This routine is called to increment the database file change-counter, |
| 3642 | ** stored at byte 24 of the pager file. |
| 3643 | */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3644 | static int pager_incr_changecounter(Pager *pPager, int isDirect){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3645 | PgHdr *pPgHdr; |
| 3646 | u32 change_counter; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3647 | int rc = SQLITE_OK; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3648 | |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 3649 | #ifndef SQLITE_ENABLE_ATOMIC_WRITE |
| 3650 | assert( isDirect==0 ); /* isDirect is only true for atomic writes */ |
| 3651 | #endif |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 3652 | if( !pPager->changeCountDone && pPager->dbSize>0 ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3653 | /* Open page 1 of the file for writing. */ |
| 3654 | rc = sqlite3PagerGet(pPager, 1, &pPgHdr); |
| 3655 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3656 | |
| 3657 | if( !isDirect ){ |
| 3658 | rc = sqlite3PagerWrite(pPgHdr); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3659 | if( rc!=SQLITE_OK ){ |
| 3660 | sqlite3PagerUnref(pPgHdr); |
| 3661 | return rc; |
| 3662 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3665 | /* Increment the value just read and write it back to byte 24. */ |
drh | b100391 | 2007-07-20 00:33:36 +0000 | [diff] [blame] | 3666 | change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3667 | change_counter++; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3668 | put32bits(((char*)pPgHdr->pData)+24, change_counter); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3669 | |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 3670 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3671 | if( isDirect && pPager->fd->pMethods ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3672 | const void *zBuf = pPgHdr->pData; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3673 | assert( pPager->dbFileSize>0 ); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3674 | rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); |
| 3675 | } |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 3676 | #endif |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3677 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3678 | /* Release the page reference. */ |
| 3679 | sqlite3PagerUnref(pPgHdr); |
| 3680 | pPager->changeCountDone = 1; |
| 3681 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3682 | return rc; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | /* |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3686 | ** Sync the pager file to disk. |
| 3687 | */ |
| 3688 | int sqlite3PagerSync(Pager *pPager){ |
| 3689 | int rc; |
drh | 7426f86 | 2008-08-26 21:07:26 +0000 | [diff] [blame] | 3690 | if( MEMDB ){ |
| 3691 | rc = SQLITE_OK; |
| 3692 | }else{ |
| 3693 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
| 3694 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3695 | return rc; |
| 3696 | } |
| 3697 | |
| 3698 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3699 | ** Sync the database file for the pager pPager. zMaster points to the name |
| 3700 | ** of a master journal file that should be written into the individual |
| 3701 | ** journal file. zMaster may be NULL, which is interpreted as no master |
| 3702 | ** journal (a single database transaction). |
| 3703 | ** |
| 3704 | ** This routine ensures that the journal is synced, all dirty pages written |
| 3705 | ** to the database file and the database file synced. The only thing that |
| 3706 | ** remains to commit the transaction is to delete the journal file (or |
| 3707 | ** master journal file if specified). |
| 3708 | ** |
| 3709 | ** Note that if zMaster==NULL, this does not overwrite a previous value |
| 3710 | ** passed to an sqlite3PagerCommitPhaseOne() call. |
| 3711 | ** |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3712 | ** If the final parameter - noSync - is true, then the database file itself |
| 3713 | ** is not synced. The caller must call sqlite3PagerSync() directly to |
| 3714 | ** sync the database file before calling CommitPhaseTwo() to delete the |
| 3715 | ** journal file in this case. |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3716 | */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3717 | int sqlite3PagerCommitPhaseOne( |
| 3718 | Pager *pPager, |
| 3719 | const char *zMaster, |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3720 | int noSync |
| 3721 | ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3722 | int rc = SQLITE_OK; |
| 3723 | |
danielk1977 | dad31b5 | 2008-05-15 11:08:07 +0000 | [diff] [blame] | 3724 | if( pPager->errCode ){ |
| 3725 | return pPager->errCode; |
| 3726 | } |
| 3727 | |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3728 | /* If no changes have been made, we can leave the transaction early. |
| 3729 | */ |
| 3730 | if( pPager->dbModified==0 && |
| 3731 | (pPager->journalMode!=PAGER_JOURNALMODE_DELETE || |
| 3732 | pPager->exclusiveMode!=0) ){ |
| 3733 | assert( pPager->dirtyCache==0 || pPager->journalOpen==0 ); |
| 3734 | return SQLITE_OK; |
| 3735 | } |
| 3736 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3737 | PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", |
| 3738 | pPager->zFilename, zMaster, pPager->dbSize); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3739 | |
| 3740 | /* If this is an in-memory db, or no pages have been written to, or this |
| 3741 | ** function has already been called, it is a no-op. |
| 3742 | */ |
| 3743 | if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){ |
| 3744 | PgHdr *pPg; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3745 | |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3746 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 3747 | /* The atomic-write optimization can be used if all of the |
| 3748 | ** following are true: |
| 3749 | ** |
| 3750 | ** + The file-system supports the atomic-write property for |
| 3751 | ** blocks of size page-size, and |
| 3752 | ** + This commit is not part of a multi-file transaction, and |
| 3753 | ** + Exactly one page has been modified and store in the journal file. |
| 3754 | ** |
| 3755 | ** If the optimization can be used, then the journal file will never |
| 3756 | ** be created for this transaction. |
| 3757 | */ |
danielk1977 | 4d60af9 | 2008-09-08 15:35:06 +0000 | [diff] [blame] | 3758 | int useAtomicWrite; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3759 | pPg = sqlite3PcacheDirtyList(pPager->pPCache); |
danielk1977 | 4d60af9 | 2008-09-08 15:35:06 +0000 | [diff] [blame] | 3760 | useAtomicWrite = ( |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3761 | !zMaster && |
danielk1977 | 700b9c5 | 2008-04-24 12:37:40 +0000 | [diff] [blame] | 3762 | pPager->journalOpen && |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3763 | pPager->journalOff==jrnlBufferSize(pPager) && |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3764 | pPager->dbSize>=pPager->dbFileSize && |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3765 | (pPg==0 || pPg->pDirty==0) |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3766 | ); |
danielk1977 | 700b9c5 | 2008-04-24 12:37:40 +0000 | [diff] [blame] | 3767 | assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF ); |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3768 | if( useAtomicWrite ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3769 | /* Update the nRec field in the journal file. */ |
| 3770 | int offset = pPager->journalHdr + sizeof(aJournalMagic); |
| 3771 | assert(pPager->nRec==1); |
| 3772 | rc = write32bits(pPager->jfd, offset, pPager->nRec); |
| 3773 | |
| 3774 | /* Update the db file change counter. The following call will modify |
| 3775 | ** the in-memory representation of page 1 to include the updated |
| 3776 | ** change counter and then write page 1 directly to the database |
| 3777 | ** file. Because of the atomic-write property of the host file-system, |
| 3778 | ** this is safe. |
| 3779 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3780 | if( rc==SQLITE_OK ){ |
| 3781 | rc = pager_incr_changecounter(pPager, 1); |
| 3782 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3783 | }else{ |
| 3784 | rc = sqlite3JournalCreate(pPager->jfd); |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3787 | if( !useAtomicWrite && rc==SQLITE_OK ) |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3788 | #endif |
| 3789 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3790 | /* If a master journal file name has already been written to the |
| 3791 | ** journal file, then no sync is required. This happens when it is |
| 3792 | ** written, then the process fails to upgrade from a RESERVED to an |
| 3793 | ** EXCLUSIVE lock. The next time the process tries to commit the |
| 3794 | ** transaction the m-j name will have already been written. |
| 3795 | */ |
| 3796 | if( !pPager->setMaster ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3797 | rc = pager_incr_changecounter(pPager, 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3798 | if( rc!=SQLITE_OK ) goto sync_exit; |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 3799 | if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3800 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3801 | if( pPager->dbSize<pPager->dbOrigSize ){ |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 3802 | /* If this transaction has made the database smaller, then all pages |
| 3803 | ** being discarded by the truncation must be written to the journal |
| 3804 | ** file. |
| 3805 | */ |
| 3806 | Pgno i; |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 3807 | Pgno iSkip = PAGER_MJ_PGNO(pPager); |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3808 | Pgno dbSize = pPager->dbSize; |
| 3809 | for( i=pPager->dbSize+1; i<=pPager->dbOrigSize; i++ ){ |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 3810 | if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){ |
| 3811 | rc = sqlite3PagerGet(pPager, i, &pPg); |
| 3812 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 3813 | rc = sqlite3PagerWrite(pPg); |
| 3814 | sqlite3PagerUnref(pPg); |
| 3815 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 3816 | } |
| 3817 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3818 | pPager->dbSize = dbSize; |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 3819 | } |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3820 | #endif |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 3821 | rc = writeMasterJournal(pPager, zMaster); |
| 3822 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 3823 | rc = syncJournal(pPager); |
| 3824 | } |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3825 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3826 | if( rc!=SQLITE_OK ) goto sync_exit; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3827 | |
| 3828 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3829 | if( pPager->dbSize<pPager->dbFileSize ){ |
| 3830 | rc = sqlite3PagerTruncate(pPager, pPager->dbSize); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3831 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 3832 | } |
| 3833 | #endif |
| 3834 | |
| 3835 | /* Write all dirty pages to the database file */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3836 | pPg = sqlite3PcacheDirtyList(pPager->pPCache); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3837 | rc = pager_write_pagelist(pPg); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3838 | if( rc!=SQLITE_OK ){ |
drh | 04c3a46 | 2008-02-26 16:16:45 +0000 | [diff] [blame] | 3839 | assert( rc!=SQLITE_IOERR_BLOCKED ); |
| 3840 | /* The error might have left the dirty list all fouled up here, |
| 3841 | ** but that does not matter because if the if the dirty list did |
| 3842 | ** get corrupted, then the transaction will roll back and |
| 3843 | ** discard the dirty list. There is an assert in |
| 3844 | ** pager_get_all_dirty_pages() that verifies that no attempt |
| 3845 | ** is made to use an invalid dirty list. |
| 3846 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3847 | goto sync_exit; |
| 3848 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3849 | sqlite3PcacheCleanAll(pPager->pPCache); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3850 | |
| 3851 | /* Sync the database file. */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 3852 | if( !pPager->noSync && !noSync ){ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 3853 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3854 | } |
| 3855 | IOTRACE(("DBSYNC %p\n", pPager)) |
| 3856 | |
| 3857 | pPager->state = PAGER_SYNCED; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3858 | }else if( MEMDB && pPager->dbSize<pPager->dbFileSize ){ |
| 3859 | rc = sqlite3PagerTruncate(pPager, pPager->dbSize); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | sync_exit: |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 3863 | if( rc==SQLITE_IOERR_BLOCKED ){ |
| 3864 | /* pager_incr_changecounter() may attempt to obtain an exclusive |
| 3865 | * lock to spill the cache and return IOERR_BLOCKED. But since |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3866 | * there is no chance the cache is inconsistent, it is |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 3867 | * better to return SQLITE_BUSY. |
| 3868 | */ |
| 3869 | rc = SQLITE_BUSY; |
| 3870 | } |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3871 | return rc; |
| 3872 | } |
| 3873 | |
| 3874 | |
| 3875 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3876 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3877 | ** |
| 3878 | ** If the commit fails for any reason, a rollback attempt is made |
| 3879 | ** and an error code is returned. If the commit worked, SQLITE_OK |
| 3880 | ** is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3881 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3882 | int sqlite3PagerCommitPhaseTwo(Pager *pPager){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3883 | int rc = SQLITE_OK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3884 | |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3885 | if( pPager->errCode ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3886 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3887 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3888 | if( pPager->state<PAGER_RESERVED ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3889 | return SQLITE_ERROR; |
| 3890 | } |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 3891 | if( pPager->dbModified==0 && |
| 3892 | (pPager->journalMode!=PAGER_JOURNALMODE_DELETE || |
| 3893 | pPager->exclusiveMode!=0) ){ |
| 3894 | assert( pPager->dirtyCache==0 || pPager->journalOpen==0 ); |
| 3895 | return SQLITE_OK; |
| 3896 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3897 | PAGERTRACE2("COMMIT %d\n", PAGERID(pPager)); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3898 | assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dirtyCache ); |
| 3899 | rc = pager_end_transaction(pPager, pPager->setMaster); |
| 3900 | rc = pager_error(pPager, rc); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3901 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3905 | ** Rollback all changes. The database falls back to PAGER_SHARED mode. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3906 | ** All in-memory cache pages revert to their original data contents. |
| 3907 | ** The journal is deleted. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3908 | ** |
| 3909 | ** This routine cannot fail unless some other process is not following |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 3910 | ** the correct locking protocol or unless some other |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3911 | ** process is writing trash into the journal file (SQLITE_CORRUPT) or |
| 3912 | ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error |
| 3913 | ** codes are returned for all these occasions. Otherwise, |
| 3914 | ** SQLITE_OK is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3915 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3916 | int sqlite3PagerRollback(Pager *pPager){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3917 | int rc = SQLITE_OK; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3918 | PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager)); |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3919 | if( !pPager->dirtyCache || !pPager->journalOpen ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 3920 | rc = pager_end_transaction(pPager, pPager->setMaster); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3921 | }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3922 | if( pPager->state>=PAGER_EXCLUSIVE ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3923 | pager_playback(pPager, 0); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3924 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3925 | rc = pPager->errCode; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3926 | }else{ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3927 | if( pPager->state==PAGER_RESERVED ){ |
| 3928 | int rc2; |
| 3929 | rc = pager_playback(pPager, 0); |
| 3930 | rc2 = pager_end_transaction(pPager, pPager->setMaster); |
| 3931 | if( rc==SQLITE_OK ){ |
| 3932 | rc = rc2; |
| 3933 | } |
| 3934 | }else{ |
| 3935 | rc = pager_playback(pPager, 0); |
| 3936 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 3937 | |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3938 | if( !MEMDB ){ |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 3939 | pPager->dbSizeValid = 0; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 3940 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3941 | |
| 3942 | /* If an error occurs during a ROLLBACK, we can no longer trust the pager |
| 3943 | ** cache. So call pager_error() on the way out to make any error |
| 3944 | ** persistent. |
| 3945 | */ |
| 3946 | rc = pager_error(pPager, rc); |
| 3947 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3948 | return rc; |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 3949 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3950 | |
| 3951 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3952 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 3953 | ** if the database is (in theory) writable. |
| 3954 | */ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3955 | u8 sqlite3PagerIsreadonly(Pager *pPager){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 3956 | return pPager->readOnly; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
| 3959 | /* |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3960 | ** Return the number of references to the pager. |
| 3961 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3962 | int sqlite3PagerRefcount(Pager *pPager){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3963 | return sqlite3PcacheRefCount(pPager->pPCache); |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3966 | /* |
| 3967 | ** Return the number of references to the specified page. |
| 3968 | */ |
| 3969 | int sqlite3PagerPageRefcount(DbPage *pPage){ |
| 3970 | return sqlite3PcachePageRefcount(pPage); |
| 3971 | } |
| 3972 | |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3973 | #ifdef SQLITE_TEST |
| 3974 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3975 | ** This routine is used for testing and analysis only. |
| 3976 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3977 | int *sqlite3PagerStats(Pager *pPager){ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 3978 | static int a[11]; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3979 | a[0] = sqlite3PcacheRefCount(pPager->pPCache); |
| 3980 | a[1] = sqlite3PcachePagecount(pPager->pPCache); |
| 3981 | a[2] = sqlite3PcacheGetCachesize(pPager->pPCache); |
danielk1977 | d92db53 | 2008-11-17 04:56:24 +0000 | [diff] [blame] | 3982 | a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3983 | a[4] = pPager->state; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3984 | a[5] = pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3985 | a[6] = pPager->nHit; |
| 3986 | a[7] = pPager->nMiss; |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 3987 | a[8] = 0; /* Used to be pPager->nOvfl */ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 3988 | a[9] = pPager->nRead; |
| 3989 | a[10] = pPager->nWrite; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3990 | return a; |
| 3991 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3992 | int sqlite3PagerIsMemdb(Pager *pPager){ |
| 3993 | return MEMDB; |
| 3994 | } |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3995 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3996 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 3997 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3998 | ** Ensure that there are at least nSavepoint savepoints open. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 3999 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4000 | int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ |
| 4001 | int rc = SQLITE_OK; |
| 4002 | |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 4003 | if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4004 | int ii; |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame^] | 4005 | PagerSavepoint *aNew; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4006 | |
| 4007 | /* Either the sub-journal is open or there are no active savepoints. */ |
| 4008 | assert( pPager->nSavepoint==0 || pPager->sjfd->pMethods ); |
| 4009 | |
| 4010 | /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM |
| 4011 | ** if the allocation fails. Otherwise, zero the new portion in case a |
| 4012 | ** malloc failure occurs while populating it in the for(...) loop below. |
| 4013 | */ |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame^] | 4014 | aNew = (PagerSavepoint *)sqlite3Realloc( |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4015 | pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint |
| 4016 | ); |
| 4017 | if( !aNew ){ |
| 4018 | return SQLITE_NOMEM; |
| 4019 | } |
| 4020 | memset(&aNew[pPager->nSavepoint], 0, |
| 4021 | (nSavepoint - pPager->nSavepoint) * sizeof(PagerSavepoint) |
| 4022 | ); |
| 4023 | pPager->aSavepoint = aNew; |
| 4024 | ii = pPager->nSavepoint; |
| 4025 | pPager->nSavepoint = nSavepoint; |
| 4026 | |
| 4027 | /* Populate the PagerSavepoint structures just allocated. */ |
| 4028 | for(/* no-op */; ii<nSavepoint; ii++){ |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 4029 | assert( pPager->dbSizeValid ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4030 | aNew[ii].nOrig = pPager->dbSize; |
danielk1977 | 67ddef6 | 2008-12-23 19:15:56 +0000 | [diff] [blame] | 4031 | if( pPager->journalOpen && pPager->journalOff>0 ){ |
| 4032 | aNew[ii].iOffset = pPager->journalOff; |
| 4033 | }else{ |
| 4034 | aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); |
| 4035 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4036 | aNew[ii].iSubRec = pPager->stmtNRec; |
| 4037 | aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); |
| 4038 | if( !aNew[ii].pInSavepoint ){ |
| 4039 | return SQLITE_NOMEM; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4040 | } |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 4041 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4042 | |
| 4043 | /* Open the sub-journal, if it is not already opened. */ |
| 4044 | rc = openSubJournal(pPager); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 4045 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4046 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4047 | return rc; |
| 4048 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4049 | |
| 4050 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4051 | ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE. |
| 4052 | ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with |
| 4053 | ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes |
| 4054 | ** that have occured since savepoint iSavepoint was created. |
| 4055 | ** |
| 4056 | ** In either case, all savepoints with an index greater than iSavepoint |
| 4057 | ** are destroyed. |
| 4058 | ** |
| 4059 | ** If there are less than (iSavepoint+1) active savepoints when this |
| 4060 | ** function is called it is a no-op. |
| 4061 | */ |
| 4062 | int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ |
| 4063 | int rc = SQLITE_OK; |
| 4064 | |
| 4065 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 4066 | |
| 4067 | if( iSavepoint<pPager->nSavepoint ){ |
| 4068 | int ii; |
| 4069 | int nNew = iSavepoint + (op==SAVEPOINT_ROLLBACK); |
| 4070 | for(ii=nNew; ii<pPager->nSavepoint; ii++){ |
| 4071 | sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); |
| 4072 | } |
| 4073 | pPager->nSavepoint = nNew; |
| 4074 | |
danielk1977 | 8a7adb0 | 2008-12-22 11:43:35 +0000 | [diff] [blame] | 4075 | if( op==SAVEPOINT_ROLLBACK && pPager->jfd->pMethods ){ |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 4076 | PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1]; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4077 | rc = pagerPlaybackSavepoint(pPager, pSavepoint); |
| 4078 | assert(rc!=SQLITE_DONE); |
| 4079 | } |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 4080 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4081 | /* If this is a release of the outermost savepoint, truncate |
| 4082 | ** the sub-journal. */ |
| 4083 | if( nNew==0 && op==SAVEPOINT_RELEASE && pPager->sjfd->pMethods ){ |
| 4084 | assert( rc==SQLITE_OK ); |
| 4085 | rc = sqlite3OsTruncate(pPager->sjfd, 0); |
| 4086 | pPager->stmtNRec = 0; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4087 | } |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 4088 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4089 | return rc; |
| 4090 | } |
| 4091 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4092 | /* |
| 4093 | ** Return the full pathname of the database file. |
| 4094 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4095 | const char *sqlite3PagerFilename(Pager *pPager){ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4096 | return pPager->zFilename; |
| 4097 | } |
| 4098 | |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4099 | /* |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 4100 | ** Return the VFS structure for the pager. |
| 4101 | */ |
| 4102 | const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ |
| 4103 | return pPager->pVfs; |
| 4104 | } |
| 4105 | |
| 4106 | /* |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 4107 | ** Return the file handle for the database file associated |
| 4108 | ** with the pager. This might return NULL if the file has |
| 4109 | ** not yet been opened. |
| 4110 | */ |
| 4111 | sqlite3_file *sqlite3PagerFile(Pager *pPager){ |
| 4112 | return pPager->fd; |
| 4113 | } |
| 4114 | |
| 4115 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4116 | ** Return the directory of the database file. |
| 4117 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4118 | const char *sqlite3PagerDirname(Pager *pPager){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4119 | return pPager->zDirectory; |
| 4120 | } |
| 4121 | |
| 4122 | /* |
| 4123 | ** Return the full pathname of the journal file. |
| 4124 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4125 | const char *sqlite3PagerJournalname(Pager *pPager){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4126 | return pPager->zJournal; |
| 4127 | } |
| 4128 | |
| 4129 | /* |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4130 | ** Return true if fsync() calls are disabled for this pager. Return FALSE |
| 4131 | ** if fsync()s are executed normally. |
| 4132 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4133 | int sqlite3PagerNosync(Pager *pPager){ |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4134 | return pPager->noSync; |
| 4135 | } |
| 4136 | |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4137 | #ifdef SQLITE_HAS_CODEC |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4138 | /* |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4139 | ** Set the codec for this pager |
| 4140 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4141 | void sqlite3PagerSetCodec( |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4142 | Pager *pPager, |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4143 | void *(*xCodec)(void*,void*,Pgno,int), |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4144 | void *pCodecArg |
| 4145 | ){ |
| 4146 | pPager->xCodec = xCodec; |
| 4147 | pPager->pCodecArg = pCodecArg; |
| 4148 | } |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4149 | #endif |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4150 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4151 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4152 | /* |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4153 | ** Move the page pPg to location pgno in the file. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4154 | ** |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4155 | ** There must be no references to the page previously located at |
| 4156 | ** pgno (which we call pPgOld) though that page is allowed to be |
drh | b3df2e1 | 2008-09-17 20:06:26 +0000 | [diff] [blame] | 4157 | ** in cache. If the page previously located at pgno is not already |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4158 | ** in the rollback journal, it is not put there by by this routine. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4159 | ** |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4160 | ** References to the page pPg remain valid. Updating any |
| 4161 | ** meta-data associated with pPg (i.e. data stored in the nExtra bytes |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4162 | ** allocated along with the page) is the responsibility of the caller. |
| 4163 | ** |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 4164 | ** A transaction must be active when this routine is called. It used to be |
| 4165 | ** required that a statement transaction was not active, but this restriction |
| 4166 | ** has been removed (CREATE INDEX needs to move a page when a statement |
| 4167 | ** transaction is active). |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 4168 | ** |
| 4169 | ** If the fourth argument, isCommit, is non-zero, then this page is being |
| 4170 | ** moved as part of a database reorganization just before the transaction |
| 4171 | ** is being committed. In this case, it is guaranteed that the database page |
| 4172 | ** pPg refers to will not be written to again within this transaction. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4173 | */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 4174 | int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){ |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4175 | PgHdr *pPgOld; /* The page being overwritten. */ |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4176 | Pgno needSyncPgno = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4177 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4178 | assert( pPg->nRef>0 ); |
| 4179 | |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4180 | PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n", |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4181 | PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 4182 | IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno)) |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 4183 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 4184 | pager_get_content(pPg); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 4185 | |
| 4186 | /* If the journal needs to be sync()ed before page pPg->pgno can |
| 4187 | ** be written to, store pPg->pgno in local variable needSyncPgno. |
| 4188 | ** |
| 4189 | ** If the isCommit flag is set, there is no need to remember that |
| 4190 | ** the journal needs to be sync()ed before database page pPg->pgno |
| 4191 | ** can be written to. The caller has already promised not to write to it. |
| 4192 | */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4193 | if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){ |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4194 | needSyncPgno = pPg->pgno; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 4195 | assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4196 | assert( pPg->flags&PGHDR_DIRTY ); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4197 | assert( pPager->needSync ); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 4200 | /* If the cache contains a page with page-number pgno, remove it |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 4201 | ** from its hash chain. Also, if the PgHdr.needSync was set for |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4202 | ** page pgno before the 'move' operation, it needs to be retained |
| 4203 | ** for the page moved there. |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4204 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 4205 | pPg->flags &= ~PGHDR_NEED_SYNC; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4206 | pPgOld = pager_lookup(pPager, pgno); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4207 | assert( !pPgOld || pPgOld->nRef==1 ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4208 | if( pPgOld ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4209 | pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4210 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4211 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4212 | sqlite3PcacheMove(pPg, pgno); |
| 4213 | if( pPgOld ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 4214 | sqlite3PcacheDrop(pPgOld); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4215 | } |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4216 | |
drh | c047b9f | 2008-11-21 03:23:43 +0000 | [diff] [blame] | 4217 | sqlite3PcacheMakeDirty(pPg); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4218 | pPager->dirtyCache = 1; |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 4219 | pPager->dbModified = 1; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4220 | |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4221 | if( needSyncPgno ){ |
| 4222 | /* If needSyncPgno is non-zero, then the journal file needs to be |
| 4223 | ** sync()ed before any data is written to database file page needSyncPgno. |
| 4224 | ** Currently, no such page exists in the page-cache and the |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 4225 | ** "is journaled" bitvec flag has been set. This needs to be remedied by |
| 4226 | ** loading the page into the pager-cache and setting the PgHdr.needSync |
| 4227 | ** flag. |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4228 | ** |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 4229 | ** If the attempt to load the page into the page-cache fails, (due |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4230 | ** to a malloc() or IO failure), clear the bit in the pInJournal[] |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 4231 | ** array. Otherwise, if the page is loaded and written again in |
| 4232 | ** this transaction, it may be written to the database file before |
| 4233 | ** it is synced into the journal file. This way, it may end up in |
| 4234 | ** the journal file twice, but that is not a problem. |
| 4235 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4236 | ** The sqlite3PagerGet() call may cause the journal to sync. So make |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4237 | ** sure the Pager.needSync flag is set too. |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4238 | */ |
| 4239 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4240 | PgHdr *pPgHdr; |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4241 | assert( pPager->needSync ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4242 | rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4243 | if( rc!=SQLITE_OK ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 4244 | if( pPager->pInJournal && needSyncPgno<=pPager->dbOrigSize ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4245 | sqlite3BitvecClear(pPager->pInJournal, needSyncPgno); |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 4246 | } |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4247 | return rc; |
| 4248 | } |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4249 | pPager->needSync = 1; |
drh | b3df2e1 | 2008-09-17 20:06:26 +0000 | [diff] [blame] | 4250 | assert( pPager->noSync==0 && !MEMDB ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4251 | pPgHdr->flags |= PGHDR_NEED_SYNC; |
drh | c047b9f | 2008-11-21 03:23:43 +0000 | [diff] [blame] | 4252 | sqlite3PcacheMakeDirty(pPgHdr); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4253 | sqlite3PagerUnref(pPgHdr); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4256 | return SQLITE_OK; |
| 4257 | } |
| 4258 | #endif |
| 4259 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4260 | /* |
| 4261 | ** Return a pointer to the data for the specified page. |
| 4262 | */ |
| 4263 | void *sqlite3PagerGetData(DbPage *pPg){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4264 | assert( pPg->nRef>0 || pPg->pPager->memDb ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4265 | return pPg->pData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
| 4268 | /* |
| 4269 | ** Return a pointer to the Pager.nExtra bytes of "extra" space |
| 4270 | ** allocated along with the specified page. |
| 4271 | */ |
| 4272 | void *sqlite3PagerGetExtra(DbPage *pPg){ |
| 4273 | Pager *pPager = pPg->pPager; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 4274 | return (pPager?pPg->pExtra:0); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 4277 | /* |
| 4278 | ** Get/set the locking-mode for this pager. Parameter eMode must be one |
| 4279 | ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or |
| 4280 | ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then |
| 4281 | ** the locking-mode is set to the value specified. |
| 4282 | ** |
| 4283 | ** The returned value is either PAGER_LOCKINGMODE_NORMAL or |
| 4284 | ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated) |
| 4285 | ** locking-mode. |
| 4286 | */ |
| 4287 | int sqlite3PagerLockingMode(Pager *pPager, int eMode){ |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 4288 | assert( eMode==PAGER_LOCKINGMODE_QUERY |
| 4289 | || eMode==PAGER_LOCKINGMODE_NORMAL |
| 4290 | || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 4291 | assert( PAGER_LOCKINGMODE_QUERY<0 ); |
| 4292 | assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 ); |
| 4293 | if( eMode>=0 && !pPager->tempFile ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4294 | pPager->exclusiveMode = (u8)eMode; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 4295 | } |
| 4296 | return (int)pPager->exclusiveMode; |
| 4297 | } |
| 4298 | |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 4299 | /* |
drh | 0433588 | 2008-09-26 21:08:08 +0000 | [diff] [blame] | 4300 | ** 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] | 4301 | ** |
drh | 0433588 | 2008-09-26 21:08:08 +0000 | [diff] [blame] | 4302 | ** PAGER_JOURNALMODE_QUERY |
| 4303 | ** PAGER_JOURNALMODE_DELETE |
| 4304 | ** PAGER_JOURNALMODE_TRUNCATE |
| 4305 | ** PAGER_JOURNALMODE_PERSIST |
| 4306 | ** PAGER_JOURNALMODE_OFF |
| 4307 | ** |
| 4308 | ** If the parameter is not _QUERY, then the journal-mode is set to the |
| 4309 | ** value specified. |
| 4310 | ** |
| 4311 | ** The returned indicate the current (possibly updated) |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 4312 | ** journal-mode. |
| 4313 | */ |
| 4314 | int sqlite3PagerJournalMode(Pager *pPager, int eMode){ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4315 | if( !MEMDB ){ |
| 4316 | assert( eMode==PAGER_JOURNALMODE_QUERY |
| 4317 | || eMode==PAGER_JOURNALMODE_DELETE |
| 4318 | || eMode==PAGER_JOURNALMODE_TRUNCATE |
| 4319 | || eMode==PAGER_JOURNALMODE_PERSIST |
| 4320 | || eMode==PAGER_JOURNALMODE_OFF |
| 4321 | || eMode==PAGER_JOURNALMODE_MEMORY ); |
| 4322 | assert( PAGER_JOURNALMODE_QUERY<0 ); |
| 4323 | if( eMode>=0 ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4324 | pPager->journalMode = (u8)eMode; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 4325 | }else{ |
| 4326 | assert( eMode==PAGER_JOURNALMODE_QUERY ); |
| 4327 | } |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 4328 | } |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 4329 | return (int)pPager->journalMode; |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 4332 | /* |
| 4333 | ** Get/set the size-limit used for persistent journal files. |
| 4334 | */ |
| 4335 | i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){ |
| 4336 | if( iLimit>=-1 ){ |
| 4337 | pPager->journalSizeLimit = iLimit; |
| 4338 | } |
| 4339 | return pPager->journalSizeLimit; |
| 4340 | } |
| 4341 | |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 4342 | #endif /* SQLITE_OMIT_DISKIO */ |