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