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