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