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 | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 21 | ** @(#) $Id: pager.c,v 1.458 2008/06/17 15:12:01 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 | #include <assert.h> |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 26 | #include <string.h> |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 29 | ** Macros for troubleshooting. Normally turned off |
| 30 | */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 31 | #if 0 |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 32 | #define sqlite3DebugPrintf printf |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 33 | #define PAGERTRACE1(X) sqlite3DebugPrintf(X) |
| 34 | #define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y) |
| 35 | #define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z) |
| 36 | #define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W) |
| 37 | #define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 38 | #else |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 39 | #define PAGERTRACE1(X) |
| 40 | #define PAGERTRACE2(X,Y) |
| 41 | #define PAGERTRACE3(X,Y,Z) |
| 42 | #define PAGERTRACE4(X,Y,Z,W) |
| 43 | #define PAGERTRACE5(X,Y,Z,W,V) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 46 | /* |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 47 | ** The following two macros are used within the PAGERTRACEX() macros above |
drh | d86959f | 2005-11-26 03:51:18 +0000 | [diff] [blame] | 48 | ** to print out file-descriptors. |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 49 | ** |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 50 | ** PAGERID() takes a pointer to a Pager struct as its argument. The |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 51 | ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 52 | ** struct as its argument. |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 53 | */ |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 54 | #define PAGERID(p) ((int)(p->fd)) |
| 55 | #define FILEHANDLEID(fd) ((int)fd) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 56 | |
| 57 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 58 | ** The page cache as a whole is always in one of the following |
| 59 | ** states: |
| 60 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 61 | ** PAGER_UNLOCK The page cache is not currently reading or |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 62 | ** writing the database file. There is no |
| 63 | ** data held in memory. This is the initial |
| 64 | ** state. |
| 65 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 66 | ** PAGER_SHARED The page cache is reading the database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 67 | ** Writing is not permitted. There can be |
| 68 | ** multiple readers accessing the same database |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 69 | ** file at the same time. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 70 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 71 | ** PAGER_RESERVED This process has reserved the database for writing |
| 72 | ** but has not yet made any changes. Only one process |
| 73 | ** at a time can reserve the database. The original |
| 74 | ** database file has not been modified so other |
| 75 | ** processes may still be reading the on-disk |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 76 | ** database file. |
| 77 | ** |
| 78 | ** PAGER_EXCLUSIVE The page cache is writing the database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 79 | ** Access is exclusive. No other processes or |
| 80 | ** threads can be reading or writing while one |
| 81 | ** process is writing. |
| 82 | ** |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 83 | ** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE |
| 84 | ** after all dirty pages have been written to the |
| 85 | ** database file and the file has been synced to |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 86 | ** disk. All that remains to do is to remove or |
| 87 | ** truncate the journal file and the transaction |
| 88 | ** will be committed. |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 89 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 90 | ** The page cache comes up in PAGER_UNLOCK. The first time a |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 91 | ** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 92 | ** After all pages have been released using sqlite_page_unref(), |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 93 | ** the state transitions back to PAGER_UNLOCK. The first time |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 94 | ** that sqlite3PagerWrite() is called, the state transitions to |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 95 | ** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 96 | ** called on an outstanding page which means that the pager must |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 97 | ** be in PAGER_SHARED before it transitions to PAGER_RESERVED.) |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 98 | ** PAGER_RESERVED means that there is an open rollback journal. |
| 99 | ** The transition to PAGER_EXCLUSIVE occurs before any changes |
| 100 | ** are made to the database file, though writes to the rollback |
| 101 | ** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback() |
| 102 | ** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED, |
| 103 | ** 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] | 104 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 105 | #define PAGER_UNLOCK 0 |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 106 | #define PAGER_SHARED 1 /* same as SHARED_LOCK */ |
| 107 | #define PAGER_RESERVED 2 /* same as RESERVED_LOCK */ |
| 108 | #define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */ |
| 109 | #define PAGER_SYNCED 5 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 110 | |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 111 | /* |
| 112 | ** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time, |
| 113 | ** then failed attempts to get a reserved lock will invoke the busy callback. |
| 114 | ** This is off by default. To see why, consider the following scenario: |
| 115 | ** |
| 116 | ** Suppose thread A already has a shared lock and wants a reserved lock. |
| 117 | ** Thread B already has a reserved lock and wants an exclusive lock. If |
| 118 | ** both threads are using their busy callbacks, it might be a long time |
| 119 | ** be for one of the threads give up and allows the other to proceed. |
| 120 | ** But if the thread trying to get the reserved lock gives up quickly |
| 121 | ** (if it never invokes its busy callback) then the contention will be |
| 122 | ** resolved quickly. |
| 123 | */ |
| 124 | #ifndef SQLITE_BUSY_RESERVED_LOCK |
| 125 | # define SQLITE_BUSY_RESERVED_LOCK 0 |
| 126 | #endif |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 127 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 128 | /* |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 129 | ** This macro rounds values up so that if the value is an address it |
| 130 | ** is guaranteed to be an address that is aligned to an 8-byte boundary. |
| 131 | */ |
| 132 | #define FORCE_ALIGNMENT(X) (((X)+7)&~7) |
| 133 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 134 | typedef struct PgHdr PgHdr; |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | ** Each pager stores all currently unreferenced pages in a list sorted |
| 138 | ** in least-recently-used (LRU) order (i.e. the first item on the list has |
| 139 | ** not been referenced in a long time, the last item has been recently |
| 140 | ** used). An instance of this structure is included as part of each |
| 141 | ** pager structure for this purpose (variable Pager.lru). |
| 142 | ** |
| 143 | ** Additionally, if memory-management is enabled, all unreferenced pages |
| 144 | ** are stored in a global LRU list (global variable sqlite3LruPageList). |
| 145 | ** |
| 146 | ** In both cases, the PagerLruList.pFirstSynced variable points to |
| 147 | ** the first page in the corresponding list that does not require an |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 148 | ** fsync() operation before its memory can be reclaimed. If no such |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 149 | ** page exists, PagerLruList.pFirstSynced is set to NULL. |
| 150 | */ |
| 151 | typedef struct PagerLruList PagerLruList; |
| 152 | struct PagerLruList { |
| 153 | PgHdr *pFirst; /* First page in LRU list */ |
| 154 | PgHdr *pLast; /* Last page in LRU list (the most recently used) */ |
| 155 | PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0 */ |
| 156 | }; |
| 157 | |
| 158 | /* |
| 159 | ** The following structure contains the next and previous pointers used |
| 160 | ** to link a PgHdr structure into a PagerLruList linked list. |
| 161 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 162 | typedef struct PagerLruLink PagerLruLink; |
| 163 | struct PagerLruLink { |
| 164 | PgHdr *pNext; |
| 165 | PgHdr *pPrev; |
| 166 | }; |
| 167 | |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 168 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 169 | ** Each in-memory image of a page begins with the following header. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 170 | ** This header is only visible to this pager module. The client |
| 171 | ** code that calls pager sees only the data that follows the header. |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 172 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 173 | ** Client code should call sqlite3PagerWrite() on a page prior to making |
| 174 | ** any modifications to that page. The first time sqlite3PagerWrite() |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 175 | ** is called, the original page contents are written into the rollback |
| 176 | ** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once |
| 177 | ** the journal page has made it onto the disk surface, PgHdr.needSync |
| 178 | ** is cleared. The modified page cannot be written back into the original |
| 179 | ** database file until the journal pages has been synced to disk and the |
| 180 | ** PgHdr.needSync has been cleared. |
| 181 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 182 | ** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 183 | ** is cleared again when the page content is written back to the original |
| 184 | ** database file. |
drh | 732c817 | 2007-06-16 11:17:45 +0000 | [diff] [blame] | 185 | ** |
| 186 | ** Details of important structure elements: |
| 187 | ** |
| 188 | ** needSync |
| 189 | ** |
| 190 | ** If this is true, this means that it is not safe to write the page |
| 191 | ** content to the database because the original content needed |
| 192 | ** for rollback has not by synced to the main rollback journal. |
| 193 | ** The original content may have been written to the rollback journal |
| 194 | ** but it has not yet been synced. So we cannot write to the database |
| 195 | ** file because power failure might cause the page in the journal file |
| 196 | ** to never reach the disk. It is as if the write to the journal file |
| 197 | ** does not occur until the journal file is synced. |
| 198 | ** |
| 199 | ** This flag is false if the page content exactly matches what |
| 200 | ** currently exists in the database file. The needSync flag is also |
| 201 | ** false if the original content has been written to the main rollback |
| 202 | ** journal and synced. If the page represents a new page that has |
| 203 | ** been added onto the end of the database during the current |
| 204 | ** transaction, the needSync flag is true until the original database |
| 205 | ** size in the journal header has been synced to disk. |
| 206 | ** |
| 207 | ** inJournal |
| 208 | ** |
| 209 | ** This is true if the original page has been written into the main |
| 210 | ** rollback journal. This is always false for new pages added to |
| 211 | ** the end of the database file during the current transaction. |
| 212 | ** And this flag says nothing about whether or not the journal |
| 213 | ** has been synced to disk. For pages that are in the original |
| 214 | ** database file, the following expression should always be true: |
| 215 | ** |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 216 | ** inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno) |
drh | 732c817 | 2007-06-16 11:17:45 +0000 | [diff] [blame] | 217 | ** |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 218 | ** The pPager->pInJournal object is only valid for the original |
drh | 732c817 | 2007-06-16 11:17:45 +0000 | [diff] [blame] | 219 | ** pages of the database, not new pages that are added to the end |
| 220 | ** of the database, so obviously the above expression cannot be |
| 221 | ** valid for new pages. For new pages inJournal is always 0. |
| 222 | ** |
| 223 | ** dirty |
| 224 | ** |
| 225 | ** When true, this means that the content of the page has been |
| 226 | ** modified and needs to be written back to the database file. |
| 227 | ** If false, it means that either the content of the page is |
| 228 | ** unchanged or else the content is unimportant and we do not |
| 229 | ** care whether or not it is preserved. |
| 230 | ** |
| 231 | ** alwaysRollback |
| 232 | ** |
| 233 | ** This means that the sqlite3PagerDontRollback() API should be |
| 234 | ** ignored for this page. The DontRollback() API attempts to say |
| 235 | ** that the content of the page on disk is unimportant (it is an |
| 236 | ** unused page on the freelist) so that it is unnecessary to |
| 237 | ** rollback changes to this page because the content of the page |
| 238 | ** can change without changing the meaning of the database. This |
| 239 | ** flag overrides any DontRollback() attempt. This flag is set |
| 240 | ** when a page that originally contained valid data is added to |
| 241 | ** the freelist. Later in the same transaction, this page might |
| 242 | ** be pulled from the freelist and reused for something different |
| 243 | ** and at that point the DontRollback() API will be called because |
| 244 | ** pages taken from the freelist do not need to be protected by |
| 245 | ** the rollback journal. But this flag says that the page was |
| 246 | ** not originally part of the freelist so that it still needs to |
| 247 | ** be rolled back in spite of any subsequent DontRollback() calls. |
| 248 | ** |
| 249 | ** needRead |
| 250 | ** |
| 251 | ** This flag means (when true) that the content of the page has |
| 252 | ** not yet been loaded from disk. The in-memory content is just |
| 253 | ** garbage. (Actually, we zero the content, but you should not |
| 254 | ** make any assumptions about the content nevertheless.) If the |
| 255 | ** content is needed in the future, it should be read from the |
| 256 | ** original database file. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 257 | */ |
| 258 | struct PgHdr { |
| 259 | Pager *pPager; /* The pager to which this page belongs */ |
| 260 | Pgno pgno; /* The page number for this page */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 261 | PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 262 | PagerLruLink free; /* Next and previous free pages */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 263 | PgHdr *pNextAll; /* A list of all pages */ |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 264 | u8 inJournal; /* TRUE if has been written to journal */ |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 265 | u8 dirty; /* TRUE if we need to write back changes */ |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 266 | u8 needSync; /* Sync journal before writing this page */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 267 | u8 alwaysRollback; /* Disable DontRollback() for this page */ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 268 | u8 needRead; /* Read content if PagerWrite() is called */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 269 | short int nRef; /* Number of users of this page */ |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 270 | PgHdr *pDirty, *pPrevDirty; /* Dirty pages */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 271 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 272 | PagerLruLink gfree; /* Global list of nRef==0 pages */ |
| 273 | #endif |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 274 | #ifdef SQLITE_CHECK_PAGES |
| 275 | u32 pageHash; |
| 276 | #endif |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 277 | void *pData; /* Page data */ |
| 278 | /* Pager.nExtra bytes of local data appended to this header */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 281 | /* |
| 282 | ** For an in-memory only database, some extra information is recorded about |
| 283 | ** each page so that changes can be rolled back. (Journal files are not |
| 284 | ** used for in-memory databases.) The following information is added to |
| 285 | ** the end of every EXTRA block for in-memory databases. |
| 286 | ** |
| 287 | ** This information could have been added directly to the PgHdr structure. |
| 288 | ** But then it would take up an extra 8 bytes of storage on every PgHdr |
| 289 | ** even for disk-based databases. Splitting it out saves 8 bytes. This |
| 290 | ** is only a savings of 0.8% but those percentages add up. |
| 291 | */ |
| 292 | typedef struct PgHistory PgHistory; |
| 293 | struct PgHistory { |
| 294 | u8 *pOrig; /* Original page text. Restore to this on a full rollback */ |
| 295 | u8 *pStmt; /* Text as it was at the beginning of the current statement */ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 296 | PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */ |
| 297 | u8 inStmt; /* TRUE if in the statement subjournal */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 298 | }; |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | ** A macro used for invoking the codec if there is one |
| 302 | */ |
| 303 | #ifdef SQLITE_HAS_CODEC |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 304 | # define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); } |
| 305 | # 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] | 306 | #else |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 307 | # define CODEC1(P,D,N,X) /* NO-OP */ |
| 308 | # define CODEC2(P,D,N,X) ((char*)D) |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 309 | #endif |
| 310 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 311 | /* |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 312 | ** Convert a pointer to a PgHdr into a pointer to its data |
| 313 | ** and back again. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 314 | */ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 315 | #define PGHDR_TO_DATA(P) ((P)->pData) |
| 316 | #define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1])) |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 317 | #define PGHDR_TO_HIST(P,PGR) \ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 318 | ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra]) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 319 | |
| 320 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 321 | ** A open page cache is an instance of the following structure. |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 322 | ** |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 323 | ** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 324 | ** or SQLITE_FULL. Once one of the first three errors occurs, it persists |
| 325 | ** and is returned as the result of every major pager API call. The |
| 326 | ** SQLITE_FULL return code is slightly different. It persists only until the |
| 327 | ** next successful rollback is performed on the pager cache. Also, |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 328 | ** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup() |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 329 | ** APIs, they may still be used successfully. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 330 | */ |
| 331 | struct Pager { |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 332 | sqlite3_vfs *pVfs; /* OS functions to use for IO */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 333 | u8 journalOpen; /* True if journal file descriptors is valid */ |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 334 | u8 journalStarted; /* True if header of journal is synced */ |
| 335 | u8 useJournal; /* Use a rollback journal on this file */ |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 336 | u8 noReadlock; /* Do not bother to obtain readlocks */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 337 | u8 stmtOpen; /* True if the statement subjournal is open */ |
| 338 | u8 stmtInUse; /* True we are in a statement subtransaction */ |
| 339 | u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 340 | u8 noSync; /* Do not sync the journal if true */ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 341 | u8 fullSync; /* Do extra syncs of the journal for robustness */ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 342 | u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 343 | u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 344 | u8 tempFile; /* zFilename is a temporary file */ |
| 345 | u8 readOnly; /* True for a read-only database */ |
| 346 | u8 needSync; /* True if an fsync() is needed on the journal */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 347 | u8 dirtyCache; /* True if cached pages have changed */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 348 | u8 alwaysRollback; /* Disable DontRollback() for all pages */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 349 | u8 memDb; /* True to inhibit all file I/O */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 350 | u8 setMaster; /* True if a m-j name has been written to jrnl */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 351 | u8 doNotSync; /* Boolean. While true, do not spill the cache */ |
| 352 | u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 353 | u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */ |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 354 | u8 dbModified; /* True if there are any changes to the Db */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 355 | u8 changeCountDone; /* Set after incrementing the change-counter */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 356 | u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 357 | int errCode; /* One of several kinds of errors */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 358 | int dbSize; /* Number of pages in the file */ |
| 359 | int origDbSize; /* dbSize before the current change */ |
| 360 | int stmtSize; /* Size of database (in pages) at stmt_begin() */ |
| 361 | int nRec; /* Number of pages written to the journal */ |
| 362 | u32 cksumInit; /* Quasi-random value added to every checksum */ |
| 363 | int stmtNRec; /* Number of records in stmt subjournal */ |
| 364 | int nExtra; /* Add this many bytes to each in-memory page */ |
| 365 | int pageSize; /* Number of bytes in a page */ |
| 366 | int nPage; /* Total number of in-memory pages */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 367 | int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ |
| 368 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 369 | Pgno mxPgno; /* Maximum allowed size of the database */ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 370 | Bitvec *pInJournal; /* One bit for each page in the database file */ |
| 371 | Bitvec *pInStmt; /* One bit for each page in the database */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 372 | char *zFilename; /* Name of the database file */ |
| 373 | char *zJournal; /* Name of the journal file */ |
| 374 | char *zDirectory; /* Directory hold database and journal files */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 375 | sqlite3_file *fd, *jfd; /* File descriptors for database and journal */ |
| 376 | sqlite3_file *stfd; /* File descriptor for the statement subjournal*/ |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 377 | BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 378 | PagerLruList lru; /* LRU list of free pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 379 | PgHdr *pAll; /* List of all pages */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 380 | PgHdr *pStmt; /* List of pages in the statement subjournal */ |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 381 | PgHdr *pDirty; /* List of all dirty pages */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 382 | i64 journalOff; /* Current byte offset in the journal file */ |
| 383 | i64 journalHdr; /* Byte offset to previous journal header */ |
| 384 | i64 stmtHdrOff; /* First journal header written this statement */ |
| 385 | i64 stmtCksum; /* cksumInit when statement was started */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 386 | i64 stmtJSize; /* Size of journal at stmt_begin() */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 387 | int sectorSize; /* Assumed sector size during rollback */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 388 | #ifdef SQLITE_TEST |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 389 | int nHit, nMiss; /* Cache hits and missing */ |
| 390 | int nRead, nWrite; /* Database pages read/written */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 391 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 392 | void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */ |
| 393 | void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */ |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 394 | #ifdef SQLITE_HAS_CODEC |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 395 | void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 396 | void *pCodecArg; /* First argument to xCodec() */ |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 397 | #endif |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 398 | int nHash; /* Size of the pager hash table */ |
| 399 | PgHdr **aHash; /* Hash table to map page number to PgHdr */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 400 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 401 | Pager *pNext; /* Doubly linked list of pagers on which */ |
| 402 | Pager *pPrev; /* sqlite3_release_memory() will work */ |
drh | 80105af | 2008-05-21 15:38:14 +0000 | [diff] [blame] | 403 | volatile int iInUseMM; /* Non-zero if unavailable to MM */ |
| 404 | volatile int iInUseDB; /* Non-zero if in sqlite3_release_memory() */ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 405 | #endif |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 406 | char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 407 | char dbFileVers[16]; /* Changes whenever database file changes */ |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 408 | i64 journalSizeLimit; /* Size limit for persistent journal files */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | /* |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 412 | ** The following global variables hold counters used for |
| 413 | ** testing purposes only. These variables do not exist in |
| 414 | ** a non-testing build. These variables are not thread-safe. |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 415 | */ |
| 416 | #ifdef SQLITE_TEST |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 417 | int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */ |
| 418 | int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */ |
| 419 | int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */ |
| 420 | int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */ |
| 421 | # define PAGER_INCR(v) v++ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 422 | #else |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 423 | # define PAGER_INCR(v) |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 424 | #endif |
| 425 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 426 | /* |
| 427 | ** The following variable points to the head of a double-linked list |
| 428 | ** of all pagers that are eligible for page stealing by the |
| 429 | ** sqlite3_release_memory() interface. Access to this list is |
| 430 | ** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex. |
| 431 | */ |
| 432 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 433 | static Pager *sqlite3PagerList = 0; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 434 | static PagerLruList sqlite3LruPageList = {0, 0, 0}; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 435 | #endif |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 436 | |
| 437 | |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 438 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 439 | ** Journal files begin with the following magic string. The data |
| 440 | ** was obtained from /dev/random. It is used only as a sanity check. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 441 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 442 | ** Since version 2.8.0, the journal format contains additional sanity |
| 443 | ** checking information. If the power fails while the journal is begin |
| 444 | ** written, semi-random garbage data might appear in the journal |
| 445 | ** file after power is restored. If an attempt is then made |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 446 | ** to roll the journal back, the database could be corrupted. The additional |
| 447 | ** sanity checking data is an attempt to discover the garbage in the |
| 448 | ** journal and ignore it. |
| 449 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 450 | ** The sanity checking information for the new journal format consists |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 451 | ** 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] | 452 | ** the page number and the pPager->pageSize bytes of data for the page. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 453 | ** This cksum is initialized to a 32-bit random value that appears in the |
| 454 | ** journal file right after the header. The random initializer is important, |
| 455 | ** because garbage data that appears at the end of a journal is likely |
| 456 | ** data that was once in other files that have now been deleted. If the |
| 457 | ** garbage data came from an obsolete journal file, the checksums might |
| 458 | ** be correct. But by initializing the checksum to random value which |
| 459 | ** is different for every journal, we minimize that risk. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 460 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 461 | static const unsigned char aJournalMagic[] = { |
| 462 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7, |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | /* |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 466 | ** The size of the header and of each page in the journal is determined |
| 467 | ** by the following macros. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 468 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 469 | #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8) |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 470 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 471 | /* |
| 472 | ** The journal header size for this pager. In the future, this could be |
| 473 | ** set to some value read from the disk controller. The important |
| 474 | ** characteristic is that it is the same size as a disk sector. |
| 475 | */ |
| 476 | #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize) |
| 477 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 478 | /* |
| 479 | ** The macro MEMDB is true if we are dealing with an in-memory database. |
| 480 | ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set, |
| 481 | ** the value of MEMDB will be a constant and the compiler will optimize |
| 482 | ** out code that would never execute. |
| 483 | */ |
| 484 | #ifdef SQLITE_OMIT_MEMORYDB |
| 485 | # define MEMDB 0 |
| 486 | #else |
| 487 | # define MEMDB pPager->memDb |
| 488 | #endif |
| 489 | |
| 490 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 491 | ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is |
| 492 | ** reserved for working around a windows/posix incompatibility). It is |
| 493 | ** used in the journal to signify that the remainder of the journal file |
| 494 | ** is devoted to storing a master journal name - there are no more pages to |
| 495 | ** roll back. See comments for function writeMasterJournal() for details. |
| 496 | */ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 497 | /* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */ |
| 498 | #define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1) |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 499 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 500 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 501 | ** The maximum legal page number is (2^31 - 1). |
| 502 | */ |
| 503 | #define PAGER_MAX_PGNO 2147483647 |
| 504 | |
| 505 | /* |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 506 | ** The pagerEnter() and pagerLeave() routines acquire and release |
| 507 | ** a mutex on each pager. The mutex is recursive. |
| 508 | ** |
| 509 | ** This is a special-purpose mutex. It only provides mutual exclusion |
| 510 | ** between the Btree and the Memory Management sqlite3_release_memory() |
| 511 | ** function. It does not prevent, for example, two Btrees from accessing |
| 512 | ** the same pager at the same time. Other general-purpose mutexes in |
| 513 | ** the btree layer handle that chore. |
| 514 | */ |
| 515 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 516 | static void pagerEnter(Pager *p){ |
| 517 | p->iInUseDB++; |
| 518 | if( p->iInUseMM && p->iInUseDB==1 ){ |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 519 | #ifndef SQLITE_MUTEX_NOOP |
drh | 64e2bb7 | 2008-05-07 12:29:55 +0000 | [diff] [blame] | 520 | sqlite3_mutex *mutex; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 521 | mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 522 | #endif |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 523 | p->iInUseDB = 0; |
| 524 | sqlite3_mutex_enter(mutex); |
| 525 | p->iInUseDB = 1; |
| 526 | sqlite3_mutex_leave(mutex); |
| 527 | } |
| 528 | assert( p->iInUseMM==0 ); |
| 529 | } |
| 530 | static void pagerLeave(Pager *p){ |
| 531 | p->iInUseDB--; |
| 532 | assert( p->iInUseDB>=0 ); |
| 533 | } |
| 534 | #else |
| 535 | # define pagerEnter(X) |
| 536 | # define pagerLeave(X) |
| 537 | #endif |
| 538 | |
| 539 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 540 | ** Add page pPg to the end of the linked list managed by structure |
| 541 | ** pList (pPg becomes the last entry in the list - the most recently |
| 542 | ** used). Argument pLink should point to either pPg->free or pPg->gfree, |
| 543 | ** depending on whether pPg is being added to the pager-specific or |
| 544 | ** global LRU list. |
| 545 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 546 | static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){ |
| 547 | pLink->pNext = 0; |
| 548 | pLink->pPrev = pList->pLast; |
| 549 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 550 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 551 | assert(pLink==&pPg->free || pLink==&pPg->gfree); |
| 552 | assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList); |
| 553 | #endif |
| 554 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 555 | if( pList->pLast ){ |
| 556 | int iOff = (char *)pLink - (char *)pPg; |
| 557 | PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]); |
| 558 | pLastLink->pNext = pPg; |
| 559 | }else{ |
| 560 | assert(!pList->pFirst); |
| 561 | pList->pFirst = pPg; |
| 562 | } |
| 563 | |
| 564 | pList->pLast = pPg; |
| 565 | if( !pList->pFirstSynced && pPg->needSync==0 ){ |
| 566 | pList->pFirstSynced = pPg; |
| 567 | } |
| 568 | } |
| 569 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 570 | /* |
| 571 | ** Remove pPg from the list managed by the structure pointed to by pList. |
| 572 | ** |
| 573 | ** Argument pLink should point to either pPg->free or pPg->gfree, depending |
| 574 | ** on whether pPg is being added to the pager-specific or global LRU list. |
| 575 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 576 | static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){ |
| 577 | int iOff = (char *)pLink - (char *)pPg; |
| 578 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 579 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 580 | assert(pLink==&pPg->free || pLink==&pPg->gfree); |
| 581 | assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList); |
| 582 | #endif |
| 583 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 584 | if( pPg==pList->pFirst ){ |
| 585 | pList->pFirst = pLink->pNext; |
| 586 | } |
| 587 | if( pPg==pList->pLast ){ |
| 588 | pList->pLast = pLink->pPrev; |
| 589 | } |
| 590 | if( pLink->pPrev ){ |
| 591 | PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]); |
| 592 | pPrevLink->pNext = pLink->pNext; |
| 593 | } |
| 594 | if( pLink->pNext ){ |
| 595 | PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]); |
| 596 | pNextLink->pPrev = pLink->pPrev; |
| 597 | } |
| 598 | if( pPg==pList->pFirstSynced ){ |
| 599 | PgHdr *p = pLink->pNext; |
| 600 | while( p && p->needSync ){ |
| 601 | PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]); |
| 602 | p = pL->pNext; |
| 603 | } |
| 604 | pList->pFirstSynced = p; |
| 605 | } |
| 606 | |
| 607 | pLink->pNext = pLink->pPrev = 0; |
| 608 | } |
| 609 | |
| 610 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 611 | ** Add page pPg to the list of free pages for the pager. If |
| 612 | ** memory-management is enabled, also add the page to the global |
| 613 | ** list of free pages. |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 614 | */ |
| 615 | static void lruListAdd(PgHdr *pPg){ |
| 616 | listAdd(&pPg->pPager->lru, &pPg->free, pPg); |
| 617 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 618 | if( !pPg->pPager->memDb ){ |
| 619 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 620 | listAdd(&sqlite3LruPageList, &pPg->gfree, pPg); |
| 621 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 622 | } |
| 623 | #endif |
| 624 | } |
| 625 | |
| 626 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 627 | ** Remove page pPg from the list of free pages for the associated pager. |
| 628 | ** If memory-management is enabled, also remove pPg from the global list |
| 629 | ** of free pages. |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 630 | */ |
| 631 | static void lruListRemove(PgHdr *pPg){ |
| 632 | listRemove(&pPg->pPager->lru, &pPg->free, pPg); |
| 633 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 634 | if( !pPg->pPager->memDb ){ |
| 635 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 636 | listRemove(&sqlite3LruPageList, &pPg->gfree, pPg); |
| 637 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 638 | } |
| 639 | #endif |
| 640 | } |
| 641 | |
| 642 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 643 | ** This function is called just after the needSync flag has been cleared |
| 644 | ** from all pages managed by pPager (usually because the journal file |
| 645 | ** has just been synced). It updates the pPager->lru.pFirstSynced variable |
| 646 | ** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced |
| 647 | ** variable also. |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 648 | */ |
| 649 | static void lruListSetFirstSynced(Pager *pPager){ |
| 650 | pPager->lru.pFirstSynced = pPager->lru.pFirst; |
| 651 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 652 | if( !pPager->memDb ){ |
| 653 | PgHdr *p; |
| 654 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 655 | for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext); |
| 656 | assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced); |
| 657 | sqlite3LruPageList.pFirstSynced = p; |
| 658 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 659 | } |
| 660 | #endif |
| 661 | } |
| 662 | |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 663 | /* |
| 664 | ** Return true if page *pPg has already been written to the statement |
| 665 | ** journal (or statement snapshot has been created, if *pPg is part |
| 666 | ** of an in-memory database). |
| 667 | */ |
| 668 | static int pageInStatement(PgHdr *pPg){ |
| 669 | Pager *pPager = pPg->pPager; |
| 670 | if( MEMDB ){ |
| 671 | return PGHDR_TO_HIST(pPg, pPager)->inStmt; |
| 672 | }else{ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 673 | return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 676 | |
| 677 | /* |
| 678 | ** Change the size of the pager hash table to N. N must be a power |
| 679 | ** of two. |
| 680 | */ |
| 681 | static void pager_resize_hash_table(Pager *pPager, int N){ |
| 682 | PgHdr **aHash, *pPg; |
| 683 | assert( N>0 && (N&(N-1))==0 ); |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 684 | #ifdef SQLITE_MALLOC_SOFT_LIMIT |
| 685 | if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){ |
| 686 | N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]); |
| 687 | } |
| 688 | if( N==pPager->nHash ) return; |
| 689 | #endif |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 690 | pagerLeave(pPager); |
drh | 4873d5f | 2008-05-13 13:27:33 +0000 | [diff] [blame] | 691 | if( pPager->aHash!=0 ) sqlite3FaultBeginBenign(SQLITE_FAULTINJECTOR_MALLOC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 692 | aHash = sqlite3MallocZero( sizeof(aHash[0])*N ); |
drh | 4873d5f | 2008-05-13 13:27:33 +0000 | [diff] [blame] | 693 | if( pPager->aHash!=0 ) sqlite3FaultEndBenign(SQLITE_FAULTINJECTOR_MALLOC); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 694 | pagerEnter(pPager); |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 695 | if( aHash==0 ){ |
| 696 | /* Failure to rehash is not an error. It is only a performance hit. */ |
| 697 | return; |
| 698 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 699 | sqlite3_free(pPager->aHash); |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 700 | pPager->nHash = N; |
| 701 | pPager->aHash = aHash; |
| 702 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 703 | int h; |
| 704 | if( pPg->pgno==0 ){ |
| 705 | assert( pPg->pNextHash==0 && pPg->pPrevHash==0 ); |
| 706 | continue; |
| 707 | } |
| 708 | h = pPg->pgno & (N-1); |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 709 | pPg->pNextHash = aHash[h]; |
| 710 | if( aHash[h] ){ |
| 711 | aHash[h]->pPrevHash = pPg; |
| 712 | } |
| 713 | aHash[h] = pPg; |
| 714 | pPg->pPrevHash = 0; |
| 715 | } |
| 716 | } |
| 717 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 718 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 719 | ** Read a 32-bit integer from the given file descriptor. Store the integer |
| 720 | ** that is read in *pRes. Return SQLITE_OK if everything worked, or an |
| 721 | ** error code is something goes wrong. |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 722 | ** |
| 723 | ** All values are stored on disk as big-endian. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 724 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 725 | static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){ |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 726 | unsigned char ac[4]; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 727 | int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset); |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 728 | if( rc==SQLITE_OK ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 729 | *pRes = sqlite3Get4byte(ac); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 730 | } |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 731 | return rc; |
| 732 | } |
| 733 | |
| 734 | /* |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 735 | ** Write a 32-bit integer into a string buffer in big-endian byte order. |
| 736 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 737 | #define put32bits(A,B) sqlite3Put4byte((u8*)A,B) |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 738 | |
| 739 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 740 | ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK |
| 741 | ** on success or an error code is something goes wrong. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 742 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 743 | static int write32bits(sqlite3_file *fd, i64 offset, u32 val){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 744 | char ac[4]; |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 745 | put32bits(ac, val); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 746 | return sqlite3OsWrite(fd, ac, 4, offset); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 747 | } |
| 748 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 749 | /* |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 750 | ** If file pFd is open, call sqlite3OsUnlock() on it. |
| 751 | */ |
| 752 | static int osUnlock(sqlite3_file *pFd, int eLock){ |
| 753 | if( !pFd->pMethods ){ |
| 754 | return SQLITE_OK; |
| 755 | } |
| 756 | return sqlite3OsUnlock(pFd, eLock); |
| 757 | } |
| 758 | |
| 759 | /* |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 760 | ** This function determines whether or not the atomic-write optimization |
| 761 | ** can be used with this pager. The optimization can be used if: |
| 762 | ** |
| 763 | ** (a) the value returned by OsDeviceCharacteristics() indicates that |
| 764 | ** a database page may be written atomically, and |
| 765 | ** (b) the value returned by OsSectorSize() is less than or equal |
| 766 | ** to the page size. |
| 767 | ** |
| 768 | ** If the optimization cannot be used, 0 is returned. If it can be used, |
| 769 | ** then the value returned is the size of the journal file when it |
| 770 | ** contains rollback data for exactly one page. |
| 771 | */ |
| 772 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 773 | static int jrnlBufferSize(Pager *pPager){ |
| 774 | int dc; /* Device characteristics */ |
| 775 | int nSector; /* Sector size */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 776 | int szPage; /* Page size */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 777 | sqlite3_file *fd = pPager->fd; |
| 778 | |
| 779 | if( fd->pMethods ){ |
| 780 | dc = sqlite3OsDeviceCharacteristics(fd); |
| 781 | nSector = sqlite3OsSectorSize(fd); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 782 | szPage = pPager->pageSize; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 786 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 787 | |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 788 | if( !fd->pMethods || |
| 789 | (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 790 | return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); |
| 791 | } |
| 792 | return 0; |
| 793 | } |
| 794 | #endif |
| 795 | |
| 796 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 797 | ** This function should be called when an error occurs within the pager |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 798 | ** code. The first argument is a pointer to the pager structure, the |
| 799 | ** second the error-code about to be returned by a pager API function. |
| 800 | ** The value returned is a copy of the second argument to this function. |
| 801 | ** |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 802 | ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 803 | ** the error becomes persistent. Until the persisten error is cleared, |
| 804 | ** subsequent API calls on this Pager will immediately return the same |
| 805 | ** error code. |
| 806 | ** |
| 807 | ** A persistent error indicates that the contents of the pager-cache |
| 808 | ** cannot be trusted. This state can be cleared by completely discarding |
| 809 | ** the contents of the pager-cache. If a transaction was active when |
| 810 | ** the persistent error occured, then the rollback journal may need |
| 811 | ** to be replayed. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 812 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 813 | static void pager_unlock(Pager *pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 814 | static int pager_error(Pager *pPager, int rc){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 815 | int rc2 = rc & 0xff; |
drh | 34f5621 | 2007-08-10 23:56:35 +0000 | [diff] [blame] | 816 | assert( |
| 817 | pPager->errCode==SQLITE_FULL || |
| 818 | pPager->errCode==SQLITE_OK || |
| 819 | (pPager->errCode & 0xff)==SQLITE_IOERR |
| 820 | ); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 821 | if( |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 822 | rc2==SQLITE_FULL || |
| 823 | rc2==SQLITE_IOERR || |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 824 | rc2==SQLITE_CORRUPT |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 825 | ){ |
| 826 | pPager->errCode = rc; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 827 | if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){ |
| 828 | /* If the pager is already unlocked, call pager_unlock() now to |
| 829 | ** clear the error state and ensure that the pager-cache is |
| 830 | ** completely empty. |
| 831 | */ |
| 832 | pager_unlock(pPager); |
| 833 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 834 | } |
| 835 | return rc; |
| 836 | } |
| 837 | |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 838 | /* |
| 839 | ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking |
| 840 | ** on the cache using a hash function. This is used for testing |
| 841 | ** and debugging only. |
| 842 | */ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 843 | #ifdef SQLITE_CHECK_PAGES |
| 844 | /* |
| 845 | ** Return a 32-bit hash of the page data for pPage. |
| 846 | */ |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 847 | static u32 pager_datahash(int nByte, unsigned char *pData){ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 848 | u32 hash = 0; |
| 849 | int i; |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 850 | for(i=0; i<nByte; i++){ |
| 851 | hash = (hash*1039) + pData[i]; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 852 | } |
| 853 | return hash; |
| 854 | } |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 855 | static u32 pager_pagehash(PgHdr *pPage){ |
| 856 | return pager_datahash(pPage->pPager->pageSize, |
| 857 | (unsigned char *)PGHDR_TO_DATA(pPage)); |
| 858 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 859 | |
| 860 | /* |
| 861 | ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES |
| 862 | ** is defined, and NDEBUG is not defined, an assert() statement checks |
| 863 | ** that the page is either dirty or still matches the calculated page-hash. |
| 864 | */ |
| 865 | #define CHECK_PAGE(x) checkPage(x) |
| 866 | static void checkPage(PgHdr *pPg){ |
| 867 | Pager *pPager = pPg->pPager; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 868 | assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty || |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 869 | pPg->pageHash==pager_pagehash(pPg) ); |
| 870 | } |
| 871 | |
| 872 | #else |
drh | 8ffa817 | 2007-06-18 17:25:17 +0000 | [diff] [blame] | 873 | #define pager_datahash(X,Y) 0 |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 874 | #define pager_pagehash(X) 0 |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 875 | #define CHECK_PAGE(x) |
| 876 | #endif |
| 877 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 878 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 879 | ** When this is called the journal file for pager pPager must be open. |
| 880 | ** 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] | 881 | ** written into memory supplied by the caller. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 882 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 883 | ** zMaster must point to a buffer of at least nMaster bytes allocated by |
| 884 | ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is |
| 885 | ** enough space to write the master journal name). If the master journal |
| 886 | ** name in the journal is longer than nMaster bytes (including a |
| 887 | ** nul-terminator), then this is handled as if no master journal name |
| 888 | ** were present in the journal. |
| 889 | ** |
| 890 | ** 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] | 891 | ** SQLITE_OK returned. |
| 892 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 893 | static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 894 | int rc; |
| 895 | u32 len; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 896 | i64 szJ; |
danielk1977 | c3e8f5e | 2004-06-28 01:16:46 +0000 | [diff] [blame] | 897 | u32 cksum; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 898 | int i; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 899 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
| 900 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 901 | zMaster[0] = '\0'; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 902 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 903 | rc = sqlite3OsFileSize(pJrnl, &szJ); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 904 | if( rc!=SQLITE_OK || szJ<16 ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 905 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 906 | rc = read32bits(pJrnl, szJ-16, &len); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 907 | if( rc!=SQLITE_OK ) return rc; |
| 908 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 909 | if( len>=nMaster ){ |
| 910 | return SQLITE_OK; |
| 911 | } |
| 912 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 913 | rc = read32bits(pJrnl, szJ-12, &cksum); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 914 | if( rc!=SQLITE_OK ) return rc; |
| 915 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 916 | rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 917 | if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc; |
| 918 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 919 | rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 920 | if( rc!=SQLITE_OK ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 921 | return rc; |
| 922 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 923 | zMaster[len] = '\0'; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 924 | |
| 925 | /* See if the checksum matches the master journal name */ |
| 926 | for(i=0; i<len; i++){ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 927 | cksum -= zMaster[i]; |
| 928 | } |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 929 | if( cksum ){ |
| 930 | /* If the checksum doesn't add up, then one or more of the disk sectors |
| 931 | ** containing the master journal filename is corrupted. This means |
| 932 | ** definitely roll back, so just return SQLITE_OK and report a (nul) |
| 933 | ** master-journal filename. |
| 934 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 935 | zMaster[0] = '\0'; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 936 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 937 | |
| 938 | return SQLITE_OK; |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | ** Seek the journal file descriptor to the next sector boundary where a |
| 943 | ** journal header may be read or written. Pager.journalOff is updated with |
| 944 | ** the new seek offset. |
| 945 | ** |
| 946 | ** i.e for a sector size of 512: |
| 947 | ** |
| 948 | ** Input Offset Output Offset |
| 949 | ** --------------------------------------- |
| 950 | ** 0 0 |
| 951 | ** 512 512 |
| 952 | ** 100 512 |
| 953 | ** 2000 2048 |
| 954 | ** |
| 955 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 956 | static void seekJournalHdr(Pager *pPager){ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 957 | i64 offset = 0; |
| 958 | i64 c = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 959 | if( c ){ |
| 960 | offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager); |
| 961 | } |
| 962 | assert( offset%JOURNAL_HDR_SZ(pPager)==0 ); |
| 963 | assert( offset>=c ); |
| 964 | assert( (offset-c)<JOURNAL_HDR_SZ(pPager) ); |
| 965 | pPager->journalOff = offset; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /* |
drh | f3a8762 | 2008-04-17 14:16:42 +0000 | [diff] [blame] | 969 | ** Write zeros over the header of the journal file. This has the |
| 970 | ** effect of invalidating the journal file and committing the |
| 971 | ** transaction. |
| 972 | */ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 973 | static int zeroJournalHdr(Pager *pPager, int doTruncate){ |
| 974 | int rc = SQLITE_OK; |
drh | f3a8762 | 2008-04-17 14:16:42 +0000 | [diff] [blame] | 975 | static const char zeroHdr[28]; |
| 976 | |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 977 | if( pPager->journalOff ){ |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 978 | i64 iLimit = pPager->journalSizeLimit; |
| 979 | |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 980 | IOTRACE(("JZEROHDR %p\n", pPager)) |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 981 | if( doTruncate || iLimit==0 ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 982 | rc = sqlite3OsTruncate(pPager->jfd, 0); |
| 983 | }else{ |
| 984 | rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0); |
| 985 | } |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 986 | if( rc==SQLITE_OK && !pPager->noSync ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 987 | rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags); |
| 988 | } |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 989 | |
| 990 | /* At this point the transaction is committed but the write lock |
| 991 | ** is still held on the file. If there is a size limit configured for |
| 992 | ** the persistent journal and the journal file currently consumes more |
| 993 | ** space than that limit allows for, truncate it now. There is no need |
| 994 | ** to sync the file following this operation. |
| 995 | */ |
| 996 | if( rc==SQLITE_OK && iLimit>0 ){ |
| 997 | i64 sz; |
| 998 | rc = sqlite3OsFileSize(pPager->jfd, &sz); |
| 999 | if( rc==SQLITE_OK && sz>iLimit ){ |
| 1000 | rc = sqlite3OsTruncate(pPager->jfd, iLimit); |
| 1001 | } |
| 1002 | } |
drh | a06ecba | 2008-04-22 17:15:17 +0000 | [diff] [blame] | 1003 | } |
drh | f3a8762 | 2008-04-17 14:16:42 +0000 | [diff] [blame] | 1004 | return rc; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1008 | ** The journal file must be open when this routine is called. A journal |
| 1009 | ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the |
| 1010 | ** current location. |
| 1011 | ** |
| 1012 | ** The format for the journal header is as follows: |
| 1013 | ** - 8 bytes: Magic identifying journal format. |
| 1014 | ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on. |
| 1015 | ** - 4 bytes: Random number used for page hash. |
| 1016 | ** - 4 bytes: Initial database page count. |
| 1017 | ** - 4 bytes: Sector size used by the process that wrote this journal. |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 1018 | ** - 4 bytes: Database page size. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1019 | ** |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 1020 | ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1021 | */ |
| 1022 | static int writeJournalHdr(Pager *pPager){ |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 1023 | int rc = SQLITE_OK; |
| 1024 | char *zHeader = pPager->pTmpSpace; |
| 1025 | int nHeader = pPager->pageSize; |
| 1026 | int nWrite; |
| 1027 | |
| 1028 | if( nHeader>JOURNAL_HDR_SZ(pPager) ){ |
| 1029 | nHeader = JOURNAL_HDR_SZ(pPager); |
| 1030 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1031 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 1032 | if( pPager->stmtHdrOff==0 ){ |
| 1033 | pPager->stmtHdrOff = pPager->journalOff; |
| 1034 | } |
| 1035 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1036 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1037 | pPager->journalHdr = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1038 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1039 | memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 1040 | |
| 1041 | /* |
| 1042 | ** Write the nRec Field - the number of page records that follow this |
| 1043 | ** journal header. Normally, zero is written to this value at this time. |
| 1044 | ** After the records are added to the journal (and the journal synced, |
| 1045 | ** if in full-sync mode), the zero is overwritten with the true number |
| 1046 | ** of records (see syncJournal()). |
| 1047 | ** |
| 1048 | ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When |
| 1049 | ** reading the journal this value tells SQLite to assume that the |
| 1050 | ** rest of the journal file contains valid page records. This assumption |
| 1051 | ** is dangerous, as if a failure occured whilst writing to the journal |
| 1052 | ** file it may contain some garbage data. There are two scenarios |
| 1053 | ** where this risk can be ignored: |
| 1054 | ** |
| 1055 | ** * When the pager is in no-sync mode. Corruption can follow a |
| 1056 | ** power failure in this case anyway. |
| 1057 | ** |
| 1058 | ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees |
| 1059 | ** that garbage data is never appended to the journal file. |
| 1060 | */ |
| 1061 | assert(pPager->fd->pMethods||pPager->noSync); |
| 1062 | if( (pPager->noSync) |
| 1063 | || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) |
| 1064 | ){ |
| 1065 | put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); |
| 1066 | }else{ |
| 1067 | put32bits(&zHeader[sizeof(aJournalMagic)], 0); |
| 1068 | } |
| 1069 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1070 | /* The random check-hash initialiser */ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 1071 | sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1072 | put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit); |
| 1073 | /* The initial database size */ |
| 1074 | put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize); |
| 1075 | /* The assumed sector size for this process */ |
| 1076 | put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize); |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 1077 | if( pPager->journalHdr==0 ){ |
| 1078 | /* The page size */ |
| 1079 | put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize); |
| 1080 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1081 | |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 1082 | for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){ |
| 1083 | IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader)) |
| 1084 | rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff); |
| 1085 | pPager->journalOff += nHeader; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1086 | } |
danielk1977 | a664f8e | 2008-04-22 14:31:48 +0000 | [diff] [blame] | 1087 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1088 | return rc; |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | ** The journal file must be open when this is called. A journal header file |
| 1093 | ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal |
| 1094 | ** file. See comments above function writeJournalHdr() for a description of |
| 1095 | ** the journal header format. |
| 1096 | ** |
| 1097 | ** If the header is read successfully, *nRec is set to the number of |
| 1098 | ** page records following this header and *dbSize is set to the size of the |
| 1099 | ** database before the transaction began, in pages. Also, pPager->cksumInit |
| 1100 | ** is set to the value read from the journal header. SQLITE_OK is returned |
| 1101 | ** in this case. |
| 1102 | ** |
| 1103 | ** If the journal header file appears to be corrupted, SQLITE_DONE is |
| 1104 | ** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes |
| 1105 | ** cannot be read from the journal file an error code is returned. |
| 1106 | */ |
| 1107 | static int readJournalHdr( |
| 1108 | Pager *pPager, |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1109 | i64 journalSize, |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1110 | u32 *pNRec, |
| 1111 | u32 *pDbSize |
| 1112 | ){ |
| 1113 | int rc; |
| 1114 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1115 | i64 jrnlOff; |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 1116 | int iPageSize; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1117 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1118 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1119 | if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){ |
| 1120 | return SQLITE_DONE; |
| 1121 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1122 | jrnlOff = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1123 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1124 | rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1125 | if( rc ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1126 | jrnlOff += sizeof(aMagic); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1127 | |
| 1128 | if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){ |
| 1129 | return SQLITE_DONE; |
| 1130 | } |
| 1131 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1132 | rc = read32bits(pPager->jfd, jrnlOff, pNRec); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1133 | if( rc ) return rc; |
| 1134 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1135 | rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1136 | if( rc ) return rc; |
| 1137 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1138 | rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1139 | if( rc ) return rc; |
| 1140 | |
danielk1977 | 67c007b | 2008-03-20 04:45:49 +0000 | [diff] [blame] | 1141 | rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize); |
| 1142 | if( rc==SQLITE_OK |
| 1143 | && iPageSize>=512 |
| 1144 | && iPageSize<=SQLITE_MAX_PAGE_SIZE |
| 1145 | && ((iPageSize-1)&iPageSize)==0 |
| 1146 | ){ |
| 1147 | u16 pagesize = iPageSize; |
| 1148 | rc = sqlite3PagerSetPagesize(pPager, &pagesize); |
| 1149 | } |
| 1150 | if( rc ) return rc; |
| 1151 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1152 | /* Update the assumed sector-size to match the value used by |
| 1153 | ** the process that created this journal. If this journal was |
| 1154 | ** created by a process other than this one, then this routine |
| 1155 | ** is being called from within pager_playback(). The local value |
| 1156 | ** of Pager.sectorSize is restored at the end of that routine. |
| 1157 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1158 | rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1159 | if( rc ) return rc; |
| 1160 | |
| 1161 | pPager->journalOff += JOURNAL_HDR_SZ(pPager); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1162 | return SQLITE_OK; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | /* |
| 1167 | ** Write the supplied master journal name into the journal file for pager |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1168 | ** pPager at the current location. The master journal name must be the last |
| 1169 | ** thing written to a journal file. If the pager is in full-sync mode, the |
| 1170 | ** journal file descriptor is advanced to the next sector boundary before |
| 1171 | ** anything is written. The format is: |
| 1172 | ** |
| 1173 | ** + 4 bytes: PAGER_MJ_PGNO. |
| 1174 | ** + N bytes: length of master journal name. |
| 1175 | ** + 4 bytes: N |
| 1176 | ** + 4 bytes: Master journal name checksum. |
| 1177 | ** + 8 bytes: aJournalMagic[]. |
| 1178 | ** |
| 1179 | ** The master journal page checksum is the sum of the bytes in the master |
| 1180 | ** journal name. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1181 | ** |
| 1182 | ** If zMaster is a NULL pointer (occurs for a single database transaction), |
| 1183 | ** this call is a no-op. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1184 | */ |
| 1185 | static int writeMasterJournal(Pager *pPager, const char *zMaster){ |
| 1186 | int rc; |
| 1187 | int len; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1188 | int i; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1189 | i64 jrnlOff; |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 1190 | i64 jrnlSize; |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1191 | u32 cksum = 0; |
| 1192 | char zBuf[sizeof(aJournalMagic)+2*4]; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1193 | |
| 1194 | if( !zMaster || pPager->setMaster) return SQLITE_OK; |
| 1195 | pPager->setMaster = 1; |
| 1196 | |
| 1197 | len = strlen(zMaster); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1198 | for(i=0; i<len; i++){ |
| 1199 | cksum += zMaster[i]; |
| 1200 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1201 | |
| 1202 | /* If in full-sync mode, advance to the next disk sector before writing |
| 1203 | ** the master journal name. This is in case the previous page written to |
| 1204 | ** the journal has already been synced. |
| 1205 | */ |
| 1206 | if( pPager->fullSync ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1207 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1208 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1209 | jrnlOff = pPager->journalOff; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1210 | pPager->journalOff += (len+20); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1211 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1212 | rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager)); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1213 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1214 | jrnlOff += 4; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1215 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1216 | rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1217 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1218 | jrnlOff += len; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1219 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1220 | put32bits(zBuf, len); |
| 1221 | put32bits(&zBuf[4], cksum); |
| 1222 | memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic)); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1223 | rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 1224 | jrnlOff += 8+sizeof(aJournalMagic); |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1225 | pPager->needSync = !pPager->noSync; |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 1226 | |
| 1227 | /* If the pager is in peristent-journal mode, then the physical |
| 1228 | ** journal-file may extend past the end of the master-journal name |
| 1229 | ** and 8 bytes of magic data just written to the file. This is |
| 1230 | ** dangerous because the code to rollback a hot-journal file |
| 1231 | ** will not be able to find the master-journal name to determine |
| 1232 | ** whether or not the journal is hot. |
| 1233 | ** |
| 1234 | ** Easiest thing to do in this scenario is to truncate the journal |
| 1235 | ** file to the required size. |
| 1236 | */ |
| 1237 | if( (rc==SQLITE_OK) |
| 1238 | && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK |
| 1239 | && jrnlSize>jrnlOff |
| 1240 | ){ |
| 1241 | rc = sqlite3OsTruncate(pPager->jfd, jrnlOff); |
| 1242 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1243 | return rc; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1247 | ** Add or remove a page from the list of all pages that are in the |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1248 | ** statement journal. |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1249 | ** |
| 1250 | ** The Pager keeps a separate list of pages that are currently in |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1251 | ** the statement journal. This helps the sqlite3PagerStmtCommit() |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1252 | ** routine run MUCH faster for the common case where there are many |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1253 | ** pages in memory but only a few are in the statement journal. |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1254 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1255 | static void page_add_to_stmt_list(PgHdr *pPg){ |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1256 | Pager *pPager = pPg->pPager; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 1257 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 1258 | assert( MEMDB ); |
| 1259 | if( !pHist->inStmt ){ |
| 1260 | assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 ); |
| 1261 | if( pPager->pStmt ){ |
| 1262 | PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg; |
| 1263 | } |
| 1264 | pHist->pNextStmt = pPager->pStmt; |
| 1265 | pPager->pStmt = pPg; |
| 1266 | pHist->inStmt = 1; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1267 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1271 | ** Find a page in the hash table given its page number. Return |
| 1272 | ** a pointer to the page or NULL if not found. |
| 1273 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1274 | static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 1275 | PgHdr *p; |
| 1276 | if( pPager->aHash==0 ) return 0; |
| 1277 | p = pPager->aHash[pgno & (pPager->nHash-1)]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1278 | while( p && p->pgno!=pgno ){ |
| 1279 | p = p->pNextHash; |
| 1280 | } |
| 1281 | return p; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1285 | ** Clear the in-memory cache. This routine |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1286 | ** sets the state of the pager back to what it was when it was first |
| 1287 | ** opened. Any outstanding pages are invalidated and subsequent attempts |
| 1288 | ** to access those pages will likely result in a coredump. |
| 1289 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1290 | static void pager_reset(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1291 | PgHdr *pPg, *pNext; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 1292 | if( pPager->errCode ) return; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1293 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1294 | IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno)); |
| 1295 | PAGER_INCR(sqlite3_pager_pgfree_count); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1296 | pNext = pPg->pNextAll; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 1297 | lruListRemove(pPg); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 1298 | sqlite3PageFree(pPg->pData); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1299 | sqlite3_free(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1300 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 1301 | assert(pPager->lru.pFirst==0); |
| 1302 | assert(pPager->lru.pFirstSynced==0); |
| 1303 | assert(pPager->lru.pLast==0); |
danielk1977 | b94bf85 | 2007-03-19 13:53:37 +0000 | [diff] [blame] | 1304 | pPager->pStmt = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1305 | pPager->pAll = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1306 | pPager->pDirty = 0; |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 1307 | pPager->nHash = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1308 | sqlite3_free(pPager->aHash); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1309 | pPager->nPage = 0; |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 1310 | pPager->aHash = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1311 | pPager->nRef = 0; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /* |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1315 | ** Unlock the database file. |
| 1316 | ** |
| 1317 | ** If the pager is currently in error state, discard the contents of |
| 1318 | ** the cache and reset the Pager structure internal state. If there is |
| 1319 | ** an open journal-file, then the next time a shared-lock is obtained |
| 1320 | ** on the pager file (by this or any other process), it will be |
| 1321 | ** treated as a hot-journal and rolled back. |
| 1322 | */ |
| 1323 | static void pager_unlock(Pager *pPager){ |
| 1324 | if( !pPager->exclusiveMode ){ |
| 1325 | if( !MEMDB ){ |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1326 | int rc = osUnlock(pPager->fd, NO_LOCK); |
drh | 308aa32 | 2008-03-07 20:14:38 +0000 | [diff] [blame] | 1327 | if( rc ) pPager->errCode = rc; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1328 | pPager->dbSize = -1; |
| 1329 | IOTRACE(("UNLOCK %p\n", pPager)) |
| 1330 | |
drh | 16e45a4 | 2008-04-19 20:34:18 +0000 | [diff] [blame] | 1331 | /* Always close the journal file when dropping the database lock. |
| 1332 | ** Otherwise, another connection with journal_mode=delete might |
| 1333 | ** delete the file out from under us. |
| 1334 | */ |
| 1335 | if( pPager->journalOpen ){ |
| 1336 | sqlite3OsClose(pPager->jfd); |
| 1337 | pPager->journalOpen = 0; |
| 1338 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 1339 | pPager->pInJournal = 0; |
| 1340 | } |
| 1341 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1342 | /* If Pager.errCode is set, the contents of the pager cache cannot be |
| 1343 | ** trusted. Now that the pager file is unlocked, the contents of the |
| 1344 | ** cache can be discarded and the error code safely cleared. |
| 1345 | */ |
| 1346 | if( pPager->errCode ){ |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1347 | if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1348 | pager_reset(pPager); |
| 1349 | if( pPager->stmtOpen ){ |
| 1350 | sqlite3OsClose(pPager->stfd); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1351 | sqlite3BitvecDestroy(pPager->pInStmt); |
| 1352 | pPager->pInStmt = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1353 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1354 | pPager->stmtOpen = 0; |
| 1355 | pPager->stmtInUse = 0; |
| 1356 | pPager->journalOff = 0; |
| 1357 | pPager->journalStarted = 0; |
| 1358 | pPager->stmtAutoopen = 0; |
| 1359 | pPager->origDbSize = 0; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | if( !MEMDB || pPager->errCode==SQLITE_OK ){ |
| 1364 | pPager->state = PAGER_UNLOCK; |
| 1365 | pPager->changeCountDone = 0; |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | ** Execute a rollback if a transaction is active and unlock the |
| 1372 | ** database file. If the pager has already entered the error state, |
| 1373 | ** do not attempt the rollback. |
| 1374 | */ |
| 1375 | static void pagerUnlockAndRollback(Pager *p){ |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 1376 | /* assert( p->state>=PAGER_RESERVED || p->journalOpen==0 ); */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1377 | if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){ |
drh | 4873d5f | 2008-05-13 13:27:33 +0000 | [diff] [blame] | 1378 | sqlite3FaultBeginBenign(-1); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1379 | sqlite3PagerRollback(p); |
drh | 4873d5f | 2008-05-13 13:27:33 +0000 | [diff] [blame] | 1380 | sqlite3FaultEndBenign(-1); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1381 | } |
| 1382 | pager_unlock(p); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 1383 | #if 0 |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1384 | assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) ); |
| 1385 | assert( p->errCode || !p->stmtOpen || p->exclusiveMode ); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 1386 | #endif |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1390 | ** This routine ends a transaction. A transaction is ended by either |
| 1391 | ** a COMMIT or a ROLLBACK. |
| 1392 | ** |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1393 | ** When this routine is called, the pager has the journal file open and |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1394 | ** a RESERVED or EXCLUSIVE lock on the database. This routine will release |
| 1395 | ** the database lock and acquires a SHARED lock in its place if that is |
| 1396 | ** the appropriate thing to do. Release locks usually is appropriate, |
| 1397 | ** unless we are in exclusive access mode or unless this is a |
| 1398 | ** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation. |
| 1399 | ** |
| 1400 | ** The journal file is either deleted or truncated. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1401 | ** |
| 1402 | ** TODO: Consider keeping the journal file open for temporary databases. |
| 1403 | ** This might give a performance improvement on windows where opening |
| 1404 | ** a file is an expensive operation. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1405 | */ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 1406 | static int pager_end_transaction(Pager *pPager, int hasMaster){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1407 | PgHdr *pPg; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1408 | int rc = SQLITE_OK; |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1409 | int rc2 = SQLITE_OK; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1410 | assert( !MEMDB ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1411 | if( pPager->state<PAGER_RESERVED ){ |
| 1412 | return SQLITE_OK; |
| 1413 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1414 | sqlite3PagerStmtCommit(pPager); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1415 | if( pPager->stmtOpen && !pPager->exclusiveMode ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1416 | sqlite3OsClose(pPager->stfd); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1417 | pPager->stmtOpen = 0; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1418 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1419 | if( pPager->journalOpen ){ |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1420 | if( pPager->exclusiveMode |
| 1421 | || pPager->journalMode==PAGER_JOURNALMODE_PERSIST |
| 1422 | ){ |
| 1423 | rc = zeroJournalHdr(pPager, hasMaster); |
| 1424 | pager_error(pPager, rc); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1425 | pPager->journalOff = 0; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1426 | pPager->journalStarted = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1427 | }else{ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1428 | sqlite3OsClose(pPager->jfd); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1429 | pPager->journalOpen = 0; |
danielk1977 | 0f01fda | 2008-06-06 16:14:02 +0000 | [diff] [blame] | 1430 | if( rc==SQLITE_OK && !pPager->tempFile ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1431 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
danielk1977 | 7152de8 | 2007-03-29 17:28:14 +0000 | [diff] [blame] | 1432 | } |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1433 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1434 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 1435 | pPager->pInJournal = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1436 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1437 | pPg->inJournal = 0; |
| 1438 | pPg->dirty = 0; |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 1439 | pPg->needSync = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1440 | pPg->alwaysRollback = 0; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1441 | #ifdef SQLITE_CHECK_PAGES |
| 1442 | pPg->pageHash = pager_pagehash(pPg); |
| 1443 | #endif |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1444 | } |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 1445 | pPager->pDirty = 0; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1446 | pPager->dirtyCache = 0; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1447 | pPager->nRec = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1448 | }else{ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1449 | assert( pPager->pInJournal==0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1450 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1451 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1452 | if( !pPager->exclusiveMode ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1453 | rc2 = osUnlock(pPager->fd, SHARED_LOCK); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1454 | pPager->state = PAGER_SHARED; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1455 | }else if( pPager->state==PAGER_SYNCED ){ |
| 1456 | pPager->state = PAGER_EXCLUSIVE; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1457 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1458 | pPager->origDbSize = 0; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1459 | pPager->setMaster = 0; |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 1460 | pPager->needSync = 0; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 1461 | lruListSetFirstSynced(pPager); |
drh | 588f5bc | 2007-01-02 18:41:54 +0000 | [diff] [blame] | 1462 | pPager->dbSize = -1; |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 1463 | pPager->dbModified = 0; |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1464 | |
| 1465 | return (rc==SQLITE_OK?rc2:rc); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1468 | /* |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1469 | ** Compute and return a checksum for the page of data. |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1470 | ** |
| 1471 | ** 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] | 1472 | ** random initial value and the page number. We experimented with |
| 1473 | ** a checksum of the entire data, but that was found to be too slow. |
| 1474 | ** |
| 1475 | ** Note that the page number is stored at the beginning of data and |
| 1476 | ** the checksum is stored at the end. This is important. If journal |
| 1477 | ** corruption occurs due to a power failure, the most likely scenario |
| 1478 | ** is that one end or the other of the record will be changed. It is |
| 1479 | ** much less likely that the two ends of the journal record will be |
| 1480 | ** correct and the middle be corrupt. Thus, this "checksum" scheme, |
| 1481 | ** though fast and simple, catches the mostly likely kind of corruption. |
| 1482 | ** |
| 1483 | ** FIX ME: Consider adding every 200th (or so) byte of the data to the |
| 1484 | ** checksum. That way if a single page spans 3 or more disk sectors and |
| 1485 | ** only the middle sector is corrupt, we will still have a reasonable |
| 1486 | ** chance of failing the checksum and thus detecting the problem. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1487 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1488 | static u32 pager_cksum(Pager *pPager, const u8 *aData){ |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1489 | u32 cksum = pPager->cksumInit; |
| 1490 | int i = pPager->pageSize-200; |
| 1491 | while( i>0 ){ |
| 1492 | cksum += aData[i]; |
| 1493 | i -= 200; |
| 1494 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1495 | return cksum; |
| 1496 | } |
| 1497 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 1498 | /* Forward declaration */ |
| 1499 | static void makeClean(PgHdr*); |
| 1500 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1501 | /* |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1502 | ** Read a single page from the journal file opened on file descriptor |
| 1503 | ** jfd. Playback this one page. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1504 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1505 | ** If useCksum==0 it means this journal does not use checksums. Checksums |
| 1506 | ** are not used in statement journals because statement journals do not |
| 1507 | ** need to survive power failures. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1508 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1509 | static int pager_playback_one_page( |
| 1510 | Pager *pPager, |
| 1511 | sqlite3_file *jfd, |
| 1512 | i64 offset, |
| 1513 | int useCksum |
| 1514 | ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1515 | int rc; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1516 | PgHdr *pPg; /* An existing page in the cache */ |
| 1517 | Pgno pgno; /* The page number of a page in journal */ |
| 1518 | u32 cksum; /* Checksum used for sanity checking */ |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 1519 | u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1520 | |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1521 | /* useCksum should be true for the main journal and false for |
| 1522 | ** statement journals. Verify that this is always the case |
| 1523 | */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1524 | assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) ); |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 1525 | assert( aData ); |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1526 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1527 | rc = read32bits(jfd, offset, &pgno); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1528 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1529 | rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1530 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1531 | pPager->journalOff += pPager->pageSize + 4; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1532 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1533 | /* Sanity checking on the page. This is more important that I originally |
| 1534 | ** thought. If a power failure occurs while the journal is being written, |
| 1535 | ** it could cause invalid data to be written into the journal. We need to |
| 1536 | ** detect this invalid data (with high probability) and ignore it. |
| 1537 | */ |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1538 | if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1539 | return SQLITE_DONE; |
| 1540 | } |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1541 | if( pgno>(unsigned)pPager->dbSize ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1542 | return SQLITE_OK; |
| 1543 | } |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1544 | if( useCksum ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1545 | rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1546 | if( rc ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1547 | pPager->journalOff += 4; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1548 | if( pager_cksum(pPager, aData)!=cksum ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1549 | return SQLITE_DONE; |
| 1550 | } |
| 1551 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1552 | |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 1553 | assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE ); |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1554 | |
| 1555 | /* If the pager is in RESERVED state, then there must be a copy of this |
| 1556 | ** page in the pager cache. In this case just update the pager cache, |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 1557 | ** not the database file. The page is left marked dirty in this case. |
| 1558 | ** |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1559 | ** An exception to the above rule: If the database is in no-sync mode |
| 1560 | ** and a page is moved during an incremental vacuum then the page may |
danielk1977 | 369f3a0 | 2007-05-24 09:41:20 +0000 | [diff] [blame] | 1561 | ** not be in the pager cache. Later: if a malloc() or IO error occurs |
| 1562 | ** during a Movepage() call, then the page may not be in the cache |
| 1563 | ** either. So the condition described in the above paragraph is not |
| 1564 | ** assert()able. |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1565 | ** |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1566 | ** If in EXCLUSIVE state, then we update the pager cache if it exists |
| 1567 | ** and the main file. The page is then marked not dirty. |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1568 | ** |
| 1569 | ** Ticket #1171: The statement journal might contain page content that is |
| 1570 | ** different from the page content at the start of the transaction. |
| 1571 | ** This occurs when a page is changed prior to the start of a statement |
| 1572 | ** then changed again within the statement. When rolling back such a |
| 1573 | ** statement we must not write to the original database unless we know |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 1574 | ** for certain that original page contents are synced into the main rollback |
| 1575 | ** journal. Otherwise, a power loss might leave modified data in the |
| 1576 | ** database file without an entry in the rollback journal that can |
| 1577 | ** restore the database to its original form. Two conditions must be |
| 1578 | ** met before writing to the database files. (1) the database must be |
| 1579 | ** locked. (2) we know that the original page content is fully synced |
| 1580 | ** in the main journal either because the page is not in cache or else |
| 1581 | ** the page is marked as needSync==0. |
drh | 4c02a23 | 2008-04-14 23:13:45 +0000 | [diff] [blame] | 1582 | ** |
| 1583 | ** 2008-04-14: When attempting to vacuum a corrupt database file, it |
| 1584 | ** is possible to fail a statement on a database that does not yet exist. |
| 1585 | ** Do not attempt to write if database file has never been opened. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1586 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1587 | pPg = pager_lookup(pPager, pgno); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 1588 | PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n", |
| 1589 | PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData)); |
drh | 4c02a23 | 2008-04-14 23:13:45 +0000 | [diff] [blame] | 1590 | if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) |
| 1591 | && pPager->fd->pMethods ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1592 | i64 offset = (pgno-1)*(i64)pPager->pageSize; |
| 1593 | rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 1594 | if( pPg ){ |
| 1595 | makeClean(pPg); |
| 1596 | } |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1597 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1598 | if( pPg ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1599 | /* No page should ever be explicitly rolled back that is in use, except |
| 1600 | ** for page 1 which is held in use in order to keep the lock on the |
| 1601 | ** database active. However such a page may be rolled back as a result |
| 1602 | ** of an internal error resulting in an automatic call to |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1603 | ** sqlite3PagerRollback(). |
drh | acf4ac9 | 2003-12-17 23:57:34 +0000 | [diff] [blame] | 1604 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1605 | void *pData; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1606 | /* assert( pPg->nRef==0 || pPg->pgno==1 ); */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1607 | pData = PGHDR_TO_DATA(pPg); |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1608 | memcpy(pData, aData, pPager->pageSize); |
danielk1977 | 9038bb6 | 2007-04-09 11:20:54 +0000 | [diff] [blame] | 1609 | if( pPager->xReiniter ){ |
| 1610 | pPager->xReiniter(pPg, pPager->pageSize); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1611 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1612 | #ifdef SQLITE_CHECK_PAGES |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1613 | pPg->pageHash = pager_pagehash(pPg); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1614 | #endif |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1615 | /* If this was page 1, then restore the value of Pager.dbFileVers. |
| 1616 | ** Do this before any decoding. */ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1617 | if( pgno==1 ){ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1618 | memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1619 | } |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1620 | |
| 1621 | /* Decode the page just read from disk */ |
| 1622 | CODEC1(pPager, pData, pPg->pgno, 3); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1623 | } |
| 1624 | return rc; |
| 1625 | } |
| 1626 | |
| 1627 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1628 | ** Parameter zMaster is the name of a master journal file. A single journal |
| 1629 | ** file that referred to the master journal file has just been rolled back. |
| 1630 | ** This routine checks if it is possible to delete the master journal file, |
| 1631 | ** and does so if it is. |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1632 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1633 | ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not |
| 1634 | ** available for use within this function. |
| 1635 | ** |
| 1636 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1637 | ** The master journal file contains the names of all child journals. |
| 1638 | ** To tell if a master journal can be deleted, check to each of the |
| 1639 | ** children. If all children are either missing or do not refer to |
| 1640 | ** a different master journal, then this master journal can be deleted. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1641 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1642 | static int pager_delmaster(Pager *pPager, const char *zMaster){ |
| 1643 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1644 | int rc; |
| 1645 | int master_open = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1646 | sqlite3_file *pMaster; |
| 1647 | sqlite3_file *pJournal; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1648 | char *zMasterJournal = 0; /* Contents of master journal file */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1649 | i64 nMasterJournal; /* Size of master journal file */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1650 | |
| 1651 | /* Open the master journal file exclusively in case some other process |
| 1652 | ** is running this routine also. Not that it makes too much difference. |
| 1653 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1654 | pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1655 | pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1656 | if( !pMaster ){ |
| 1657 | rc = SQLITE_NOMEM; |
| 1658 | }else{ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1659 | int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); |
| 1660 | rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1661 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1662 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1663 | master_open = 1; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1664 | |
| 1665 | rc = sqlite3OsFileSize(pMaster, &nMasterJournal); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1666 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1667 | |
| 1668 | if( nMasterJournal>0 ){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1669 | char *zJournal; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1670 | char *zMasterPtr = 0; |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1671 | int nMasterPtr = pPager->pVfs->mxPathname+1; |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1672 | |
| 1673 | /* Load the entire master journal file into space obtained from |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1674 | ** sqlite3_malloc() and pointed to by zMasterJournal. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1675 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1676 | zMasterJournal = (char *)sqlite3Malloc(nMasterJournal + nMasterPtr); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1677 | if( !zMasterJournal ){ |
| 1678 | rc = SQLITE_NOMEM; |
| 1679 | goto delmaster_out; |
| 1680 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1681 | zMasterPtr = &zMasterJournal[nMasterJournal]; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1682 | rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1683 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1684 | |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1685 | zJournal = zMasterJournal; |
| 1686 | while( (zJournal-zMasterJournal)<nMasterJournal ){ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1687 | int exists; |
| 1688 | rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 1689 | if( rc!=SQLITE_OK ){ |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 1690 | goto delmaster_out; |
| 1691 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1692 | if( exists ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1693 | /* One of the journals pointed to by the master journal exists. |
| 1694 | ** Open it and check if it points at the master journal. If |
| 1695 | ** so, return without deleting the master journal file. |
| 1696 | */ |
drh | 3b7b78b | 2004-11-24 01:16:43 +0000 | [diff] [blame] | 1697 | int c; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1698 | int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL); |
| 1699 | rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1700 | if( rc!=SQLITE_OK ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1701 | goto delmaster_out; |
| 1702 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1703 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1704 | rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1705 | sqlite3OsClose(pJournal); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 1706 | if( rc!=SQLITE_OK ){ |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 1707 | goto delmaster_out; |
| 1708 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1709 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1710 | c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0; |
drh | 3b7b78b | 2004-11-24 01:16:43 +0000 | [diff] [blame] | 1711 | if( c ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1712 | /* We have a match. Do not delete the master journal file. */ |
| 1713 | goto delmaster_out; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1714 | } |
| 1715 | } |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1716 | zJournal += (strlen(zJournal)+1); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1720 | rc = sqlite3OsDelete(pVfs, zMaster, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1721 | |
| 1722 | delmaster_out: |
| 1723 | if( zMasterJournal ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1724 | sqlite3_free(zMasterJournal); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1725 | } |
| 1726 | if( master_open ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1727 | sqlite3OsClose(pMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1728 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1729 | sqlite3_free(pMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1730 | return rc; |
| 1731 | } |
| 1732 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1733 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1734 | static void pager_truncate_cache(Pager *pPager); |
| 1735 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1736 | /* |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1737 | ** Truncate the main file of the given pager to the number of pages |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1738 | ** indicated. Also truncate the cached representation of the file. |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1739 | ** |
| 1740 | ** Might might be the case that the file on disk is smaller than nPage. |
| 1741 | ** This can happen, for example, if we are in the middle of a transaction |
| 1742 | ** which has extended the file size and the new pages are still all held |
| 1743 | ** in cache, then an INSERT or UPDATE does a statement rollback. Some |
| 1744 | ** operating system implementations can get confused if you try to |
| 1745 | ** truncate a file to some size that is larger than it currently is, |
danielk1977 | 06e11af | 2008-05-06 18:13:26 +0000 | [diff] [blame] | 1746 | ** so detect this case and write a single zero byte to the end of the new |
| 1747 | ** file instead. |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1748 | */ |
| 1749 | static int pager_truncate(Pager *pPager, int nPage){ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1750 | int rc = SQLITE_OK; |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1751 | if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){ |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1752 | i64 currentSize, newSize; |
| 1753 | rc = sqlite3OsFileSize(pPager->fd, ¤tSize); |
| 1754 | newSize = pPager->pageSize*(i64)nPage; |
danielk1977 | 06e11af | 2008-05-06 18:13:26 +0000 | [diff] [blame] | 1755 | if( rc==SQLITE_OK && currentSize!=newSize ){ |
| 1756 | if( currentSize>newSize ){ |
| 1757 | rc = sqlite3OsTruncate(pPager->fd, newSize); |
| 1758 | }else{ |
| 1759 | rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1); |
| 1760 | } |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1761 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1762 | } |
| 1763 | if( rc==SQLITE_OK ){ |
| 1764 | pPager->dbSize = nPage; |
| 1765 | pager_truncate_cache(pPager); |
| 1766 | } |
| 1767 | return rc; |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | /* |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1771 | ** Set the sectorSize for the given pager. |
| 1772 | ** |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1773 | ** The sector size is at least as big as the sector size reported |
| 1774 | ** by sqlite3OsSectorSize(). The minimum sector size is 512. |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1775 | */ |
| 1776 | static void setSectorSize(Pager *pPager){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1777 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 1778 | if( !pPager->tempFile ){ |
| 1779 | /* Sector size doesn't matter for temporary files. Also, the file |
| 1780 | ** may not have been opened yet, in whcih case the OsSectorSize() |
| 1781 | ** call will segfault. |
| 1782 | */ |
| 1783 | pPager->sectorSize = sqlite3OsSectorSize(pPager->fd); |
| 1784 | } |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1785 | if( pPager->sectorSize<512 ){ |
| 1786 | pPager->sectorSize = 512; |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1791 | ** Playback the journal and thus restore the database file to |
| 1792 | ** the state it was in before we started making changes. |
| 1793 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1794 | ** The journal file format is as follows: |
| 1795 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1796 | ** (1) 8 byte prefix. A copy of aJournalMagic[]. |
| 1797 | ** (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] | 1798 | ** in the journal. If this value is 0xffffffff, then compute the |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1799 | ** number of page records from the journal size. |
| 1800 | ** (3) 4 byte big-endian integer which is the initial value for the |
| 1801 | ** sanity checksum. |
| 1802 | ** (4) 4 byte integer which is the number of pages to truncate the |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1803 | ** database to during a rollback. |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1804 | ** (5) 4 byte big-endian integer which is the sector size. The header |
| 1805 | ** is this many bytes in size. |
| 1806 | ** (6) 4 byte big-endian integer which is the page case. |
| 1807 | ** (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] | 1808 | ** name. The value may be zero (indicate that there is no master |
| 1809 | ** journal.) |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1810 | ** (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] | 1811 | ** and might be shorter than the value read from (5). If the first byte |
| 1812 | ** of the name is \000 then there is no master journal. The master |
| 1813 | ** journal name is stored in UTF-8. |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1814 | ** (9) Zero or more pages instances, each as follows: |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1815 | ** + 4 byte page number. |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1816 | ** + pPager->pageSize bytes of data. |
| 1817 | ** + 4 byte checksum |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1818 | ** |
drh | 334c80d | 2008-03-28 17:41:13 +0000 | [diff] [blame] | 1819 | ** When we speak of the journal header, we mean the first 8 items above. |
| 1820 | ** Each entry in the journal is an instance of the 9th item. |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1821 | ** |
| 1822 | ** Call the value from the second bullet "nRec". nRec is the number of |
| 1823 | ** valid page entries in the journal. In most cases, you can compute the |
| 1824 | ** value of nRec from the size of the journal file. But if a power |
| 1825 | ** failure occurred while the journal was being written, it could be the |
| 1826 | ** case that the size of the journal file had already been increased but |
| 1827 | ** the extra entries had not yet made it safely to disk. In such a case, |
| 1828 | ** the value of nRec computed from the file size would be too large. For |
| 1829 | ** that reason, we always use the nRec value in the header. |
| 1830 | ** |
| 1831 | ** If the nRec value is 0xffffffff it means that nRec should be computed |
| 1832 | ** from the file size. This value is used when the user selects the |
| 1833 | ** no-sync option for the journal. A power failure could lead to corruption |
| 1834 | ** in this case. But for things like temporary table (which will be |
| 1835 | ** deleted when the power is restored) we don't care. |
| 1836 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1837 | ** If the file opened as the journal file is not a well-formed |
danielk1977 | ece80f1 | 2004-06-23 01:05:26 +0000 | [diff] [blame] | 1838 | ** journal file then all pages up to the first corrupted page are rolled |
| 1839 | ** back (or no pages if the journal header is corrupted). The journal file |
| 1840 | ** is then deleted and SQLITE_OK returned, just as if no corruption had |
| 1841 | ** been encountered. |
| 1842 | ** |
| 1843 | ** If an I/O or malloc() error occurs, the journal-file is not deleted |
| 1844 | ** and an error code is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1845 | */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1846 | static int pager_playback(Pager *pPager, int isHot){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1847 | sqlite3_vfs *pVfs = pPager->pVfs; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1848 | i64 szJ; /* Size of the journal file in bytes */ |
danielk1977 | c3e8f5e | 2004-06-28 01:16:46 +0000 | [diff] [blame] | 1849 | u32 nRec; /* Number of Records in the journal */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1850 | int i; /* Loop counter */ |
| 1851 | Pgno mxPg = 0; /* Size of the original file in pages */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1852 | int rc; /* Result code of a subroutine */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1853 | int res = 1; /* Value returned by sqlite3OsAccess() */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1854 | char *zMaster = 0; /* Name of master journal file if any */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1855 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1856 | /* Figure out how many records are in the journal. Abort early if |
| 1857 | ** the journal is empty. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1858 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1859 | assert( pPager->journalOpen ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1860 | rc = sqlite3OsFileSize(pPager->jfd, &szJ); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1861 | if( rc!=SQLITE_OK || szJ==0 ){ |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1862 | goto end_playback; |
| 1863 | } |
drh | 240c579 | 2004-02-08 00:40:52 +0000 | [diff] [blame] | 1864 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1865 | /* Read the master journal name from the journal, if it is present. |
| 1866 | ** If a master journal file name is specified, but the file is not |
| 1867 | ** present on disk, then the journal is not hot and does not need to be |
| 1868 | ** played back. |
drh | 240c579 | 2004-02-08 00:40:52 +0000 | [diff] [blame] | 1869 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1870 | zMaster = pPager->pTmpSpace; |
| 1871 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1872 | if( rc==SQLITE_OK && zMaster[0] ){ |
| 1873 | rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1874 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1875 | zMaster = 0; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1876 | if( rc!=SQLITE_OK || !res ){ |
danielk1977 | ce98bba | 2008-04-03 10:13:01 +0000 | [diff] [blame] | 1877 | goto end_playback; |
| 1878 | } |
| 1879 | pPager->journalOff = 0; |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1880 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1881 | /* This loop terminates either when the readJournalHdr() call returns |
| 1882 | ** SQLITE_DONE or an IO error occurs. */ |
| 1883 | while( 1 ){ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1884 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1885 | /* Read the next journal header from the journal file. If there are |
| 1886 | ** not enough bytes left in the journal file for a complete header, or |
| 1887 | ** it is corrupted, then a process must of failed while writing it. |
| 1888 | ** This indicates nothing more needs to be rolled back. |
| 1889 | */ |
| 1890 | rc = readJournalHdr(pPager, szJ, &nRec, &mxPg); |
| 1891 | if( rc!=SQLITE_OK ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1892 | if( rc==SQLITE_DONE ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1893 | rc = SQLITE_OK; |
| 1894 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1895 | goto end_playback; |
| 1896 | } |
| 1897 | |
| 1898 | /* If nRec is 0xffffffff, then this journal was created by a process |
| 1899 | ** working in no-sync mode. This means that the rest of the journal |
| 1900 | ** file consists of pages, there are no more journal headers. Compute |
| 1901 | ** the value of nRec based on this assumption. |
| 1902 | */ |
| 1903 | if( nRec==0xffffffff ){ |
| 1904 | assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ); |
| 1905 | nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager); |
| 1906 | } |
| 1907 | |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1908 | /* 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] | 1909 | ** process and if this is the final header in the journal, then it means |
| 1910 | ** that this part of the journal was being filled but has not yet been |
| 1911 | ** synced to disk. Compute the number of pages based on the remaining |
| 1912 | ** size of the file. |
| 1913 | ** |
| 1914 | ** The third term of the test was added to fix ticket #2565. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1915 | */ |
drh | 8940f4e | 2007-08-11 00:26:20 +0000 | [diff] [blame] | 1916 | if( nRec==0 && !isHot && |
| 1917 | pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1918 | nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager); |
| 1919 | } |
| 1920 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1921 | /* If this is the first header read from the journal, truncate the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 1922 | ** database file back to its original size. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1923 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1924 | if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){ |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1925 | rc = pager_truncate(pPager, mxPg); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1926 | if( rc!=SQLITE_OK ){ |
| 1927 | goto end_playback; |
| 1928 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1931 | /* Copy original pages out of the journal and back into the database file. |
| 1932 | */ |
| 1933 | for(i=0; i<nRec; i++){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1934 | rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1935 | if( rc!=SQLITE_OK ){ |
| 1936 | if( rc==SQLITE_DONE ){ |
| 1937 | rc = SQLITE_OK; |
| 1938 | pPager->journalOff = szJ; |
| 1939 | break; |
| 1940 | }else{ |
| 1941 | goto end_playback; |
| 1942 | } |
| 1943 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1944 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1945 | } |
drh | 580eeaf | 2006-02-24 03:09:37 +0000 | [diff] [blame] | 1946 | /*NOTREACHED*/ |
| 1947 | assert( 0 ); |
drh | 4a0681e | 2003-02-13 01:58:20 +0000 | [diff] [blame] | 1948 | |
| 1949 | end_playback: |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 1950 | if( rc==SQLITE_OK ){ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1951 | zMaster = pPager->pTmpSpace; |
| 1952 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
| 1953 | } |
| 1954 | if( rc==SQLITE_OK ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 1955 | rc = pager_end_transaction(pPager, zMaster[0]!='\0'); |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 1956 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1957 | if( rc==SQLITE_OK && zMaster[0] ){ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1958 | /* If there was a master journal and this routine will return success, |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1959 | ** see if it is possible to delete the master journal. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1960 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1961 | rc = pager_delmaster(pPager, zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1962 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1963 | |
| 1964 | /* The Pager.sectorSize variable may have been updated while rolling |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 1965 | ** back a journal created by a process with a different sector size |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1966 | ** value. Reset it to the correct value for this process. |
| 1967 | */ |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1968 | setSectorSize(pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1969 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1973 | ** Playback the statement journal. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1974 | ** |
| 1975 | ** This is similar to playing back the transaction journal but with |
| 1976 | ** a few extra twists. |
| 1977 | ** |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1978 | ** (1) The number of pages in the database file at the start of |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1979 | ** the statement is stored in pPager->stmtSize, not in the |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1980 | ** journal file itself. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1981 | ** |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1982 | ** (2) In addition to playing back the statement journal, also |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1983 | ** playback all pages of the transaction journal beginning |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1984 | ** at offset pPager->stmtJSize. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1985 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1986 | static int pager_stmt_playback(Pager *pPager){ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1987 | i64 szJ; /* Size of the full journal */ |
| 1988 | i64 hdrOff; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1989 | int nRec; /* Number of Records */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1990 | int i; /* Loop counter */ |
| 1991 | int rc; |
| 1992 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1993 | szJ = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1994 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 1995 | /* Set hdrOff to be the offset just after the end of the last journal |
| 1996 | ** page written before the first journal-header for this statement |
| 1997 | ** transaction was written, or the end of the file if no journal |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1998 | ** header was written. |
| 1999 | */ |
| 2000 | hdrOff = pPager->stmtHdrOff; |
| 2001 | assert( pPager->fullSync || !hdrOff ); |
| 2002 | if( !hdrOff ){ |
| 2003 | hdrOff = szJ; |
| 2004 | } |
| 2005 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2006 | /* Truncate the database back to its original size. |
| 2007 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2008 | rc = pager_truncate(pPager, pPager->stmtSize); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2009 | assert( pPager->state>=PAGER_SHARED ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2010 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2011 | /* Figure out how many records are in the statement journal. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2012 | */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2013 | assert( pPager->stmtInUse && pPager->journalOpen ); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2014 | nRec = pPager->stmtNRec; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2015 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2016 | /* Copy original pages out of the statement journal and back into the |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 2017 | ** database file. Note that the statement journal omits checksums from |
| 2018 | ** each record since power-failure recovery is not important to statement |
| 2019 | ** journals. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2020 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2021 | for(i=0; i<nRec; i++){ |
| 2022 | i64 offset = i*(4+pPager->pageSize); |
| 2023 | rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2024 | assert( rc!=SQLITE_DONE ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2025 | if( rc!=SQLITE_OK ) goto end_stmt_playback; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2028 | /* Now roll some pages back from the transaction journal. Pager.stmtJSize |
| 2029 | ** was the size of the journal file when this statement was started, so |
| 2030 | ** everything after that needs to be rolled back, either into the |
| 2031 | ** database, the memory cache, or both. |
| 2032 | ** |
| 2033 | ** If it is not zero, then Pager.stmtHdrOff is the offset to the start |
| 2034 | ** of the first journal header written during this statement transaction. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2035 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2036 | pPager->journalOff = pPager->stmtJSize; |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 2037 | pPager->cksumInit = pPager->stmtCksum; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 2038 | while( pPager->journalOff < hdrOff ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2039 | rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2040 | assert( rc!=SQLITE_DONE ); |
| 2041 | if( rc!=SQLITE_OK ) goto end_stmt_playback; |
| 2042 | } |
| 2043 | |
| 2044 | while( pPager->journalOff < szJ ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 2045 | u32 nJRec; /* Number of Journal Records */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2046 | u32 dummy; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 2047 | rc = readJournalHdr(pPager, szJ, &nJRec, &dummy); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2048 | if( rc!=SQLITE_OK ){ |
| 2049 | assert( rc!=SQLITE_DONE ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2050 | goto end_stmt_playback; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2051 | } |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 2052 | if( nJRec==0 ){ |
| 2053 | nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8); |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 2054 | } |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 2055 | for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2056 | rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2057 | assert( rc!=SQLITE_DONE ); |
| 2058 | if( rc!=SQLITE_OK ) goto end_stmt_playback; |
| 2059 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2060 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2061 | |
| 2062 | pPager->journalOff = szJ; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2063 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2064 | end_stmt_playback: |
danielk1977 | 8a7aea3 | 2006-01-23 15:25:48 +0000 | [diff] [blame] | 2065 | if( rc==SQLITE_OK) { |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 2066 | pPager->journalOff = szJ; |
| 2067 | /* pager_reload_cache(pPager); */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2068 | } |
| 2069 | return rc; |
| 2070 | } |
| 2071 | |
| 2072 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2073 | ** Change the maximum number of in-memory pages that are allowed. |
| 2074 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2075 | void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){ |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2076 | if( mxPage>10 ){ |
| 2077 | pPager->mxPage = mxPage; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 2078 | }else{ |
| 2079 | pPager->mxPage = 10; |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2084 | ** Adjust the robustness of the database to damage due to OS crashes |
| 2085 | ** or power failures by changing the number of syncs()s when writing |
| 2086 | ** the rollback journal. There are three levels: |
| 2087 | ** |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2088 | ** OFF sqlite3OsSync() is never called. This is the default |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2089 | ** for temporary and transient files. |
| 2090 | ** |
| 2091 | ** NORMAL The journal is synced once before writes begin on the |
| 2092 | ** database. This is normally adequate protection, but |
| 2093 | ** it is theoretically possible, though very unlikely, |
| 2094 | ** that an inopertune power failure could leave the journal |
| 2095 | ** in a state which would cause damage to the database |
| 2096 | ** when it is rolled back. |
| 2097 | ** |
| 2098 | ** FULL The journal is synced twice before writes begin on the |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2099 | ** database (with some additional information - the nRec field |
| 2100 | ** of the journal header - being written in between the two |
| 2101 | ** syncs). If we assume that writing a |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2102 | ** single disk sector is atomic, then this mode provides |
| 2103 | ** assurance that the journal will not be corrupted to the |
| 2104 | ** point of causing damage to the database during rollback. |
| 2105 | ** |
| 2106 | ** Numeric values associated with these states are OFF==1, NORMAL=2, |
| 2107 | ** and FULL=3. |
| 2108 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2109 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2110 | void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){ |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2111 | pPager->noSync = level==1 || pPager->tempFile; |
| 2112 | pPager->fullSync = level==3 && !pPager->tempFile; |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2113 | pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2114 | if( pPager->noSync ) pPager->needSync = 0; |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2115 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2116 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2117 | |
| 2118 | /* |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 2119 | ** The following global variable is incremented whenever the library |
| 2120 | ** attempts to open a temporary file. This information is used for |
| 2121 | ** testing and analysis only. |
| 2122 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2123 | #ifdef SQLITE_TEST |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 2124 | int sqlite3_opentemp_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2125 | #endif |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 2126 | |
| 2127 | /* |
drh | 3f56e6e | 2007-03-15 01:16:47 +0000 | [diff] [blame] | 2128 | ** Open a temporary file. |
| 2129 | ** |
| 2130 | ** Write the file descriptor into *fd. Return SQLITE_OK on success or some |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2131 | ** other error code if we fail. The OS will automatically delete the temporary |
| 2132 | ** file when it is closed. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2133 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2134 | static int sqlite3PagerOpentemp( |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2135 | Pager *pPager, /* The pager object */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2136 | sqlite3_file *pFile, /* Write the file descriptor here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2137 | int vfsFlags /* Flags passed through to the VFS */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2138 | ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2139 | int rc; |
drh | 3f56e6e | 2007-03-15 01:16:47 +0000 | [diff] [blame] | 2140 | |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2141 | #ifdef SQLITE_TEST |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 2142 | sqlite3_opentemp_count++; /* Used for testing and analysis only */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2143 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2144 | |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2145 | vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
| 2146 | SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2147 | rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0); |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2148 | assert( rc!=SQLITE_OK || pFile->pMethods ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2149 | return rc; |
| 2150 | } |
| 2151 | |
| 2152 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2153 | ** 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] | 2154 | ** 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] | 2155 | ** the first call to sqlite3PagerGet() and is only held open until the |
| 2156 | ** last page is released using sqlite3PagerUnref(). |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2157 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2158 | ** If zFilename is NULL then a randomly-named temporary file is created |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2159 | ** and used as the file to be cached. The file will be deleted |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2160 | ** automatically when it is closed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2161 | ** |
| 2162 | ** If zFilename is ":memory:" then all information is held in cache. |
| 2163 | ** It is never written to disk. This can be used to implement an |
| 2164 | ** in-memory database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2165 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2166 | int sqlite3PagerOpen( |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2167 | sqlite3_vfs *pVfs, /* The virtual file system to use */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2168 | Pager **ppPager, /* Return the Pager structure here */ |
| 2169 | const char *zFilename, /* Name of the database file to open */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 2170 | int nExtra, /* Extra bytes append to each in-memory page */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2171 | int flags, /* flags controlling this file */ |
| 2172 | int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2173 | ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2174 | u8 *pPtr; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2175 | Pager *pPager = 0; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2176 | int rc = SQLITE_OK; |
| 2177 | int i; |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 2178 | int tempFile = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2179 | int memDb = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2180 | int readOnly = 0; |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 2181 | int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; |
| 2182 | int noReadlock = (flags & PAGER_NO_READLOCK)!=0; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 2183 | int journalFileSize = sqlite3JournalSize(pVfs); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2184 | int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2185 | char *zPathname = 0; |
| 2186 | int nPathname = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2187 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2188 | /* The default return is a NULL pointer */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2189 | *ppPager = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2190 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2191 | /* Compute and store the full pathname in an allocated buffer pointed |
| 2192 | ** to by zPathname, length nPathname. Or, if this is a temporary file, |
| 2193 | ** leave both nPathname and zPathname set to 0. |
| 2194 | */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2195 | if( zFilename && zFilename[0] ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2196 | nPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2197 | zPathname = sqlite3Malloc(nPathname*2); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2198 | if( zPathname==0 ){ |
| 2199 | return SQLITE_NOMEM; |
| 2200 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2201 | #ifndef SQLITE_OMIT_MEMORYDB |
| 2202 | if( strcmp(zFilename,":memory:")==0 ){ |
| 2203 | memDb = 1; |
| 2204 | zPathname[0] = 0; |
| 2205 | }else |
| 2206 | #endif |
| 2207 | { |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 2208 | rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2209 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2210 | if( rc!=SQLITE_OK ){ |
| 2211 | sqlite3_free(zPathname); |
| 2212 | return rc; |
| 2213 | } |
| 2214 | nPathname = strlen(zPathname); |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2215 | } |
drh | 99b90c3 | 2008-03-17 13:50:58 +0000 | [diff] [blame] | 2216 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2217 | /* Allocate memory for the pager structure */ |
| 2218 | pPager = sqlite3MallocZero( |
| 2219 | sizeof(*pPager) + /* Pager structure */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 2220 | journalFileSize + /* The journal file structure */ |
drh | 17c0ba2 | 2008-02-26 18:40:11 +0000 | [diff] [blame] | 2221 | pVfs->szOsFile * 3 + /* The main db and two journal files */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2222 | 3*nPathname + 40 /* zFilename, zDirectory, zJournal */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2223 | ); |
| 2224 | if( !pPager ){ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2225 | sqlite3_free(zPathname); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2226 | return SQLITE_NOMEM; |
| 2227 | } |
| 2228 | pPtr = (u8 *)&pPager[1]; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2229 | pPager->vfsFlags = vfsFlags; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2230 | pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0]; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 2231 | pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1]; |
| 2232 | pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2]; |
| 2233 | pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize]; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2234 | pPager->zDirectory = &pPager->zFilename[nPathname+1]; |
| 2235 | pPager->zJournal = &pPager->zDirectory[nPathname+1]; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2236 | pPager->pVfs = pVfs; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2237 | if( zPathname ){ |
| 2238 | memcpy(pPager->zFilename, zPathname, nPathname+1); |
| 2239 | sqlite3_free(zPathname); |
| 2240 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2241 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2242 | /* Open the pager file. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2243 | */ |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2244 | if( zFilename && zFilename[0] && !memDb ){ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2245 | if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){ |
| 2246 | rc = SQLITE_CANTOPEN; |
| 2247 | }else{ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2248 | int fout = 0; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2249 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, |
| 2250 | pPager->vfsFlags, &fout); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2251 | readOnly = (fout&SQLITE_OPEN_READONLY); |
| 2252 | |
| 2253 | /* If the file was successfully opened for read/write access, |
| 2254 | ** choose a default page size in case we have to create the |
| 2255 | ** database file. The default page size is the maximum of: |
| 2256 | ** |
| 2257 | ** + SQLITE_DEFAULT_PAGE_SIZE, |
| 2258 | ** + The value returned by sqlite3OsSectorSize() |
| 2259 | ** + The largest page size that can be written atomically. |
| 2260 | */ |
| 2261 | if( rc==SQLITE_OK && !readOnly ){ |
| 2262 | int iSectorSize = sqlite3OsSectorSize(pPager->fd); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2263 | if( szPageDflt<iSectorSize ){ |
| 2264 | szPageDflt = iSectorSize; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2265 | } |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2266 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2267 | { |
| 2268 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| 2269 | int ii; |
| 2270 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 2271 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 2272 | assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2273 | for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){ |
| 2274 | if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii; |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2275 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2276 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2277 | #endif |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2278 | if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ |
| 2279 | szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2280 | } |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 2281 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2282 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2283 | }else if( !memDb ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2284 | /* If a temporary file is requested, it is not opened immediately. |
| 2285 | ** In this case we accept the default page size and delay actually |
| 2286 | ** opening the file until the first call to OsWrite(). |
| 2287 | */ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2288 | tempFile = 1; |
| 2289 | pPager->state = PAGER_EXCLUSIVE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2290 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2291 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2292 | if( pPager && rc==SQLITE_OK ){ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2293 | pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt); |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 2294 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2295 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2296 | /* If an error occured in either of the blocks above. |
| 2297 | ** Free the Pager structure and close the file. |
| 2298 | ** Since the pager is not allocated there is no need to set |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2299 | ** any Pager.errMask variables. |
| 2300 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2301 | if( !pPager || !pPager->pTmpSpace ){ |
| 2302 | sqlite3OsClose(pPager->fd); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2303 | sqlite3_free(pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2304 | return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2305 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2306 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2307 | PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename); |
| 2308 | IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename)) |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2309 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2310 | /* Fill in Pager.zDirectory[] */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2311 | memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2312 | for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){} |
drh | a76c82e | 2003-07-27 18:59:42 +0000 | [diff] [blame] | 2313 | if( i>0 ) pPager->zDirectory[i-1] = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2314 | |
drh | 99b90c3 | 2008-03-17 13:50:58 +0000 | [diff] [blame] | 2315 | /* Fill in Pager.zJournal[] */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2316 | if( zPathname ){ |
| 2317 | memcpy(pPager->zJournal, pPager->zFilename, nPathname); |
| 2318 | memcpy(&pPager->zJournal[nPathname], "-journal", 9); |
| 2319 | }else{ |
| 2320 | pPager->zJournal = 0; |
| 2321 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2322 | |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2323 | /* pPager->journalOpen = 0; */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2324 | pPager->useJournal = useJournal && !memDb; |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 2325 | pPager->noReadlock = noReadlock && readOnly; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2326 | /* pPager->stmtOpen = 0; */ |
| 2327 | /* pPager->stmtInUse = 0; */ |
| 2328 | /* pPager->nRef = 0; */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2329 | pPager->dbSize = memDb-1; |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2330 | pPager->pageSize = szPageDflt; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2331 | /* pPager->stmtSize = 0; */ |
| 2332 | /* pPager->stmtJSize = 0; */ |
| 2333 | /* pPager->nPage = 0; */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2334 | pPager->mxPage = 100; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2335 | pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2336 | /* pPager->state = PAGER_UNLOCK; */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2337 | assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) ); |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2338 | /* pPager->errMask = 0; */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2339 | pPager->tempFile = tempFile; |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 2340 | assert( tempFile==PAGER_LOCKINGMODE_NORMAL |
| 2341 | || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 2342 | assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); |
| 2343 | pPager->exclusiveMode = tempFile; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2344 | pPager->memDb = memDb; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2345 | pPager->readOnly = readOnly; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2346 | /* pPager->needSync = 0; */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 2347 | pPager->noSync = pPager->tempFile || !useJournal; |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 2348 | pPager->fullSync = (pPager->noSync?0:1); |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2349 | pPager->sync_flags = SQLITE_SYNC_NORMAL; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2350 | /* pPager->pFirst = 0; */ |
| 2351 | /* pPager->pFirstSynced = 0; */ |
| 2352 | /* pPager->pLast = 0; */ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 2353 | pPager->nExtra = FORCE_ALIGNMENT(nExtra); |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 2354 | pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT; |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2355 | assert(pPager->fd->pMethods||memDb||tempFile); |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 2356 | if( !memDb ){ |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 2357 | setSectorSize(pPager); |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 2358 | } |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2359 | /* pPager->pBusyHandler = 0; */ |
| 2360 | /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2361 | *ppPager = pPager; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2362 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2363 | pPager->iInUseMM = 0; |
| 2364 | pPager->iInUseDB = 0; |
| 2365 | if( !memDb ){ |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 2366 | #ifndef SQLITE_MUTEX_NOOP |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2367 | sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 2368 | #endif |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2369 | sqlite3_mutex_enter(mutex); |
| 2370 | pPager->pNext = sqlite3PagerList; |
| 2371 | if( sqlite3PagerList ){ |
| 2372 | assert( sqlite3PagerList->pPrev==0 ); |
| 2373 | sqlite3PagerList->pPrev = pPager; |
| 2374 | } |
| 2375 | pPager->pPrev = 0; |
| 2376 | sqlite3PagerList = pPager; |
| 2377 | sqlite3_mutex_leave(mutex); |
| 2378 | } |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 2379 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2380 | return SQLITE_OK; |
| 2381 | } |
| 2382 | |
| 2383 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2384 | ** Set the busy handler function. |
| 2385 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2386 | void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2387 | pPager->pBusyHandler = pBusyHandler; |
| 2388 | } |
| 2389 | |
| 2390 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2391 | ** Set the destructor for this pager. If not NULL, the destructor is called |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2392 | ** when the reference count on each page reaches zero. The destructor can |
| 2393 | ** be used to clean up information in the extra segment appended to each page. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2394 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2395 | ** The destructor is not called as a result sqlite3PagerClose(). |
| 2396 | ** Destructors are only called by sqlite3PagerUnref(). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2397 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2398 | void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2399 | pPager->xDestructor = xDesc; |
| 2400 | } |
| 2401 | |
| 2402 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2403 | ** Set the reinitializer for this pager. If not NULL, the reinitializer |
| 2404 | ** is called when the content of a page in cache is restored to its original |
| 2405 | ** value as a result of a rollback. The callback gives higher-level code |
| 2406 | ** an opportunity to restore the EXTRA section to agree with the restored |
| 2407 | ** page data. |
| 2408 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2409 | void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2410 | pPager->xReiniter = xReinit; |
| 2411 | } |
| 2412 | |
| 2413 | /* |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2414 | ** Set the page size to *pPageSize. If the suggest new page size is |
| 2415 | ** inappropriate, then an alternative page size is set to that |
| 2416 | ** value before returning. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2417 | */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2418 | int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){ |
| 2419 | int rc = SQLITE_OK; |
| 2420 | u16 pageSize = *pPageSize; |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2421 | assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2422 | if( pageSize && pageSize!=pPager->pageSize |
| 2423 | && !pPager->memDb && pPager->nRef==0 |
| 2424 | ){ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2425 | char *pNew = (char *)sqlite3PageMalloc(pageSize); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2426 | if( !pNew ){ |
| 2427 | rc = SQLITE_NOMEM; |
| 2428 | }else{ |
| 2429 | pagerEnter(pPager); |
| 2430 | pager_reset(pPager); |
| 2431 | pPager->pageSize = pageSize; |
| 2432 | setSectorSize(pPager); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2433 | sqlite3PageFree(pPager->pTmpSpace); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2434 | pPager->pTmpSpace = pNew; |
| 2435 | pagerLeave(pPager); |
| 2436 | } |
drh | 1c7880e | 2005-05-20 20:01:55 +0000 | [diff] [blame] | 2437 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2438 | *pPageSize = pPager->pageSize; |
| 2439 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | /* |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 2443 | ** Return a pointer to the "temporary page" buffer held internally |
| 2444 | ** by the pager. This is a buffer that is big enough to hold the |
| 2445 | ** entire content of a database page. This buffer is used internally |
| 2446 | ** during rollback and will be overwritten whenever a rollback |
| 2447 | ** occurs. But other modules are free to use it too, as long as |
| 2448 | ** no rollbacks are happening. |
| 2449 | */ |
| 2450 | void *sqlite3PagerTempSpace(Pager *pPager){ |
| 2451 | return pPager->pTmpSpace; |
| 2452 | } |
| 2453 | |
| 2454 | /* |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2455 | ** Attempt to set the maximum database page count if mxPage is positive. |
| 2456 | ** Make no changes if mxPage is zero or negative. And never reduce the |
| 2457 | ** maximum page count below the current size of the database. |
| 2458 | ** |
| 2459 | ** Regardless of mxPage, return the current maximum page count. |
| 2460 | */ |
| 2461 | int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ |
| 2462 | if( mxPage>0 ){ |
| 2463 | pPager->mxPgno = mxPage; |
| 2464 | } |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2465 | sqlite3PagerPagecount(pPager, 0); |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2466 | return pPager->mxPgno; |
| 2467 | } |
| 2468 | |
| 2469 | /* |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2470 | ** The following set of routines are used to disable the simulated |
| 2471 | ** I/O error mechanism. These routines are used to avoid simulated |
| 2472 | ** errors in places where we do not care about errors. |
| 2473 | ** |
| 2474 | ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops |
| 2475 | ** and generate no code. |
| 2476 | */ |
| 2477 | #ifdef SQLITE_TEST |
| 2478 | extern int sqlite3_io_error_pending; |
| 2479 | extern int sqlite3_io_error_hit; |
| 2480 | static int saved_cnt; |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2481 | void disable_simulated_io_errors(void){ |
| 2482 | saved_cnt = sqlite3_io_error_pending; |
| 2483 | sqlite3_io_error_pending = -1; |
| 2484 | } |
| 2485 | void enable_simulated_io_errors(void){ |
| 2486 | sqlite3_io_error_pending = saved_cnt; |
| 2487 | } |
| 2488 | #else |
drh | 152410f | 2005-11-05 15:11:22 +0000 | [diff] [blame] | 2489 | # define disable_simulated_io_errors() |
| 2490 | # define enable_simulated_io_errors() |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2491 | #endif |
| 2492 | |
| 2493 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2494 | ** Read the first N bytes from the beginning of the file into memory |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2495 | ** that pDest points to. |
| 2496 | ** |
| 2497 | ** No error checking is done. The rational for this is that this function |
| 2498 | ** may be called even if the file does not exist or contain a header. In |
| 2499 | ** these cases sqlite3OsRead() will return an error, to which the correct |
| 2500 | ** response is to zero the memory at pDest and continue. A real IO error |
| 2501 | ** will presumably recur and be picked up later (Todo: Think about this). |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2502 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2503 | int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){ |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2504 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2505 | memset(pDest, 0, N); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2506 | assert(MEMDB||pPager->fd->pMethods||pPager->tempFile); |
| 2507 | if( pPager->fd->pMethods ){ |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2508 | IOTRACE(("DBHDR %p 0 %d\n", pPager, N)) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2509 | rc = sqlite3OsRead(pPager->fd, pDest, N, 0); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2510 | if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 2511 | rc = SQLITE_OK; |
| 2512 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2513 | } |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2514 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2518 | ** Return the total number of pages in the disk file associated with |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2519 | ** pPager. |
| 2520 | ** |
| 2521 | ** If the PENDING_BYTE lies on the page directly after the end of the |
| 2522 | ** file, then consider this page part of the file too. For example, if |
| 2523 | ** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the |
| 2524 | ** file is 4096 bytes, 5 is returned instead of 4. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2525 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2526 | int sqlite3PagerPagecount(Pager *pPager, int *pnPage){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2527 | i64 n = 0; |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 2528 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2529 | assert( pPager!=0 ); |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 2530 | if( pPager->errCode ){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2531 | return pPager->errCode; |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 2532 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2533 | if( pPager->dbSize>=0 ){ |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2534 | n = pPager->dbSize; |
| 2535 | } else { |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2536 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 2537 | if( (pPager->fd->pMethods) |
| 2538 | && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2539 | pPager->nRef++; |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 2540 | pager_error(pPager, rc); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2541 | pPager->nRef--; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2542 | return rc; |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2543 | } |
| 2544 | if( n>0 && n<pPager->pageSize ){ |
| 2545 | n = 1; |
| 2546 | }else{ |
| 2547 | n /= pPager->pageSize; |
| 2548 | } |
| 2549 | if( pPager->state!=PAGER_UNLOCK ){ |
| 2550 | pPager->dbSize = n; |
| 2551 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2552 | } |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2553 | if( n==(PENDING_BYTE/pPager->pageSize) ){ |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 2554 | n++; |
| 2555 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2556 | if( n>pPager->mxPgno ){ |
| 2557 | pPager->mxPgno = n; |
| 2558 | } |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2559 | if( pnPage ){ |
| 2560 | *pnPage = n; |
| 2561 | } |
| 2562 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
drh | f07e7d5 | 2006-04-07 13:54:46 +0000 | [diff] [blame] | 2565 | |
| 2566 | #ifndef SQLITE_OMIT_MEMORYDB |
| 2567 | /* |
| 2568 | ** Clear a PgHistory block |
| 2569 | */ |
| 2570 | static void clearHistory(PgHistory *pHist){ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2571 | sqlite3PageFree(pHist->pOrig); |
| 2572 | sqlite3PageFree(pHist->pStmt); |
drh | f07e7d5 | 2006-04-07 13:54:46 +0000 | [diff] [blame] | 2573 | pHist->pOrig = 0; |
| 2574 | pHist->pStmt = 0; |
| 2575 | } |
| 2576 | #else |
| 2577 | #define clearHistory(x) |
| 2578 | #endif |
| 2579 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2580 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2581 | ** Forward declaration |
| 2582 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2583 | static int syncJournal(Pager*); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2584 | |
| 2585 | /* |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2586 | ** Unlink pPg from its hash chain. Also set the page number to 0 to indicate |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2587 | ** that the page is not part of any hash chain. This is required because the |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2588 | ** sqlite3PagerMovepage() routine can leave a page in the |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2589 | ** pNextFree/pPrevFree list that is not a part of any hash-chain. |
| 2590 | */ |
| 2591 | static void unlinkHashChain(Pager *pPager, PgHdr *pPg){ |
| 2592 | if( pPg->pgno==0 ){ |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 2593 | assert( pPg->pNextHash==0 && pPg->pPrevHash==0 ); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2594 | return; |
| 2595 | } |
| 2596 | if( pPg->pNextHash ){ |
| 2597 | pPg->pNextHash->pPrevHash = pPg->pPrevHash; |
| 2598 | } |
| 2599 | if( pPg->pPrevHash ){ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 2600 | assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg ); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2601 | pPg->pPrevHash->pNextHash = pPg->pNextHash; |
| 2602 | }else{ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 2603 | int h = pPg->pgno & (pPager->nHash-1); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2604 | pPager->aHash[h] = pPg->pNextHash; |
| 2605 | } |
drh | 5229ae4 | 2006-03-23 23:29:04 +0000 | [diff] [blame] | 2606 | if( MEMDB ){ |
drh | 5229ae4 | 2006-03-23 23:29:04 +0000 | [diff] [blame] | 2607 | clearHistory(PGHDR_TO_HIST(pPg, pPager)); |
| 2608 | } |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2609 | pPg->pgno = 0; |
| 2610 | pPg->pNextHash = pPg->pPrevHash = 0; |
| 2611 | } |
| 2612 | |
| 2613 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2614 | ** Unlink a page from the free list (the list of all pages where nRef==0) |
| 2615 | ** and from its hash collision chain. |
| 2616 | */ |
| 2617 | static void unlinkPage(PgHdr *pPg){ |
| 2618 | Pager *pPager = pPg->pPager; |
| 2619 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2620 | /* Unlink from free page list */ |
| 2621 | lruListRemove(pPg); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2622 | |
| 2623 | /* Unlink from the pgno hash table */ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2624 | unlinkHashChain(pPager, pPg); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2628 | ** This routine is used to truncate the cache when a database |
| 2629 | ** is truncated. Drop from the cache all pages whose pgno is |
| 2630 | ** larger than pPager->dbSize and is unreferenced. |
| 2631 | ** |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2632 | ** Referenced pages larger than pPager->dbSize are zeroed. |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2633 | ** |
| 2634 | ** Actually, at the point this routine is called, it would be |
| 2635 | ** an error to have a referenced page. But rather than delete |
| 2636 | ** that page and guarantee a subsequent segfault, it seems better |
| 2637 | ** to zero it and hope that we error out sanely. |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2638 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2639 | static void pager_truncate_cache(Pager *pPager){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2640 | PgHdr *pPg; |
| 2641 | PgHdr **ppPg; |
| 2642 | int dbSize = pPager->dbSize; |
| 2643 | |
| 2644 | ppPg = &pPager->pAll; |
| 2645 | while( (pPg = *ppPg)!=0 ){ |
| 2646 | if( pPg->pgno<=dbSize ){ |
| 2647 | ppPg = &pPg->pNextAll; |
| 2648 | }else if( pPg->nRef>0 ){ |
| 2649 | memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize); |
| 2650 | ppPg = &pPg->pNextAll; |
| 2651 | }else{ |
| 2652 | *ppPg = pPg->pNextAll; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2653 | IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno)); |
| 2654 | PAGER_INCR(sqlite3_pager_pgfree_count); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2655 | unlinkPage(pPg); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 2656 | makeClean(pPg); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2657 | sqlite3PageFree(pPg->pData); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2658 | sqlite3_free(pPg); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2659 | pPager->nPage--; |
| 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2664 | /* |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2665 | ** 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] | 2666 | ** is currently not available. Repeat until the busy callback returns |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2667 | ** false or until the lock succeeds. |
| 2668 | ** |
| 2669 | ** Return SQLITE_OK on success and an error code if we cannot obtain |
| 2670 | ** the lock. |
| 2671 | */ |
| 2672 | static int pager_wait_on_lock(Pager *pPager, int locktype){ |
| 2673 | int rc; |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2674 | |
| 2675 | /* The OS lock values must be the same as the Pager lock values */ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2676 | assert( PAGER_SHARED==SHARED_LOCK ); |
| 2677 | assert( PAGER_RESERVED==RESERVED_LOCK ); |
| 2678 | assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2679 | |
| 2680 | /* If the file is currently unlocked then the size must be unknown */ |
| 2681 | assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB ); |
| 2682 | |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2683 | if( pPager->state>=locktype ){ |
| 2684 | rc = SQLITE_OK; |
| 2685 | }else{ |
drh | befdf83 | 2008-03-14 19:33:05 +0000 | [diff] [blame] | 2686 | if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0; |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2687 | do { |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2688 | rc = sqlite3OsLock(pPager->fd, locktype); |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 2689 | }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) ); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2690 | if( rc==SQLITE_OK ){ |
| 2691 | pPager->state = locktype; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2692 | IOTRACE(("LOCK %p %d\n", pPager, locktype)) |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | return rc; |
| 2696 | } |
| 2697 | |
| 2698 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2699 | ** Truncate the file to the number of pages specified. |
| 2700 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2701 | int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2702 | int rc; |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2703 | assert( pPager->state>=PAGER_SHARED || MEMDB ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2704 | sqlite3PagerPagecount(pPager, 0); |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 2705 | if( pPager->errCode ){ |
| 2706 | rc = pPager->errCode; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 2707 | return rc; |
| 2708 | } |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 2709 | if( nPage>=(unsigned)pPager->dbSize ){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2710 | return SQLITE_OK; |
| 2711 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2712 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2713 | pPager->dbSize = nPage; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2714 | pager_truncate_cache(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2715 | return SQLITE_OK; |
| 2716 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2717 | pagerEnter(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2718 | rc = syncJournal(pPager); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2719 | pagerLeave(pPager); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 2720 | if( rc!=SQLITE_OK ){ |
| 2721 | return rc; |
| 2722 | } |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2723 | |
| 2724 | /* Get an exclusive lock on the database before truncating. */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2725 | pagerEnter(pPager); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2726 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2727 | pagerLeave(pPager); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2728 | if( rc!=SQLITE_OK ){ |
| 2729 | return rc; |
| 2730 | } |
| 2731 | |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 2732 | rc = pager_truncate(pPager, nPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2733 | return rc; |
| 2734 | } |
| 2735 | |
| 2736 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2737 | ** Shutdown the page cache. Free all memory and close all files. |
| 2738 | ** |
| 2739 | ** If a transaction was in progress when this routine is called, that |
| 2740 | ** transaction is rolled back. All outstanding pages are invalidated |
| 2741 | ** and their memory is freed. Any attempt to use a page associated |
| 2742 | ** with this page cache after this function returns will likely |
| 2743 | ** result in a coredump. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2744 | ** |
| 2745 | ** This function always succeeds. If a transaction is active an attempt |
| 2746 | ** is made to roll it back. If an error occurs during the rollback |
| 2747 | ** a hot journal may be left in the filesystem but no error is returned |
| 2748 | ** to the caller. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2749 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2750 | int sqlite3PagerClose(Pager *pPager){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2751 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2752 | if( !MEMDB ){ |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 2753 | #ifndef SQLITE_MUTEX_NOOP |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2754 | sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 2755 | #endif |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2756 | sqlite3_mutex_enter(mutex); |
| 2757 | if( pPager->pPrev ){ |
| 2758 | pPager->pPrev->pNext = pPager->pNext; |
| 2759 | }else{ |
| 2760 | sqlite3PagerList = pPager->pNext; |
| 2761 | } |
| 2762 | if( pPager->pNext ){ |
| 2763 | pPager->pNext->pPrev = pPager->pPrev; |
| 2764 | } |
| 2765 | sqlite3_mutex_leave(mutex); |
| 2766 | } |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 2767 | #endif |
| 2768 | |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2769 | disable_simulated_io_errors(); |
drh | 4873d5f | 2008-05-13 13:27:33 +0000 | [diff] [blame] | 2770 | sqlite3FaultBeginBenign(-1); |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 2771 | pPager->errCode = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 2772 | pPager->exclusiveMode = 0; |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2773 | pager_reset(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2774 | pagerUnlockAndRollback(pPager); |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2775 | enable_simulated_io_errors(); |
drh | 4873d5f | 2008-05-13 13:27:33 +0000 | [diff] [blame] | 2776 | sqlite3FaultEndBenign(-1); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2777 | PAGERTRACE2("CLOSE %d\n", PAGERID(pPager)); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2778 | IOTRACE(("CLOSE %p\n", pPager)) |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2779 | if( pPager->journalOpen ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2780 | sqlite3OsClose(pPager->jfd); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2781 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 2782 | sqlite3BitvecDestroy(pPager->pInJournal); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2783 | if( pPager->stmtOpen ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2784 | sqlite3OsClose(pPager->stfd); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2785 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2786 | sqlite3OsClose(pPager->fd); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 2787 | /* Temp files are automatically deleted by the OS |
| 2788 | ** if( pPager->tempFile ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 2789 | ** sqlite3OsDelete(pPager->zFilename); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 2790 | ** } |
| 2791 | */ |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 2792 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2793 | sqlite3_free(pPager->aHash); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 2794 | sqlite3PageFree(pPager->pTmpSpace); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2795 | sqlite3_free(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2796 | return SQLITE_OK; |
| 2797 | } |
| 2798 | |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 2799 | #if !defined(NDEBUG) || defined(SQLITE_TEST) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2800 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2801 | ** Return the page number for the given page data. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2802 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2803 | Pgno sqlite3PagerPagenumber(DbPage *p){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2804 | return p->pgno; |
| 2805 | } |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 2806 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2807 | |
| 2808 | /* |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2809 | ** The page_ref() function increments the reference count for a page. |
| 2810 | ** If the page is currently on the freelist (the reference count is zero) then |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2811 | ** remove it from the freelist. |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2812 | ** |
| 2813 | ** For non-test systems, page_ref() is a macro that calls _page_ref() |
| 2814 | ** online of the reference count is zero. For test systems, page_ref() |
| 2815 | ** is a real function so that we can set breakpoints and trace it. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2816 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 2817 | static void _page_ref(PgHdr *pPg){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2818 | if( pPg->nRef==0 ){ |
| 2819 | /* The page is currently on the freelist. Remove it. */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2820 | lruListRemove(pPg); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2821 | pPg->pPager->nRef++; |
| 2822 | } |
| 2823 | pPg->nRef++; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2824 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 2825 | #ifdef SQLITE_DEBUG |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2826 | static void page_ref(PgHdr *pPg){ |
| 2827 | if( pPg->nRef==0 ){ |
| 2828 | _page_ref(pPg); |
| 2829 | }else{ |
| 2830 | pPg->nRef++; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2831 | } |
| 2832 | } |
| 2833 | #else |
| 2834 | # define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++) |
| 2835 | #endif |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2836 | |
| 2837 | /* |
| 2838 | ** Increment the reference count for a page. The input pointer is |
| 2839 | ** a reference to the page data. |
| 2840 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2841 | int sqlite3PagerRef(DbPage *pPg){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2842 | pagerEnter(pPg->pPager); |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2843 | page_ref(pPg); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2844 | pagerLeave(pPg->pPager); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2845 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2849 | ** Sync the journal. In other words, make sure all the pages that have |
| 2850 | ** been written to the journal have actually reached the surface of the |
| 2851 | ** disk. It is not safe to modify the original database file until after |
| 2852 | ** the journal has been synced. If the original database is modified before |
| 2853 | ** the journal is synced and a power failure occurs, the unsynced journal |
| 2854 | ** data would be lost and we would be unable to completely rollback the |
| 2855 | ** database changes. Database corruption would occur. |
| 2856 | ** |
| 2857 | ** This routine also updates the nRec field in the header of the journal. |
| 2858 | ** (See comments on the pager_playback() routine for additional information.) |
| 2859 | ** If the sync mode is FULL, two syncs will occur. First the whole journal |
| 2860 | ** is synced, then the nRec field is updated, then a second sync occurs. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2861 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2862 | ** For temporary databases, we do not care if we are able to rollback |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2863 | ** after a power failure, so no sync occurs. |
| 2864 | ** |
| 2865 | ** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which |
| 2866 | ** the database is stored, then OsSync() is never called on the journal |
| 2867 | ** file. In this case all that is required is to update the nRec field in |
| 2868 | ** the journal header. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2869 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2870 | ** This routine clears the needSync field of every page current held in |
| 2871 | ** memory. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2872 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2873 | static int syncJournal(Pager *pPager){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2874 | PgHdr *pPg; |
| 2875 | int rc = SQLITE_OK; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2876 | |
| 2877 | /* Sync the journal before modifying the main database |
| 2878 | ** (assuming there is a journal and it needs to be synced.) |
| 2879 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2880 | if( pPager->needSync ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2881 | if( !pPager->tempFile ){ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2882 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2883 | assert( pPager->journalOpen ); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2884 | |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2885 | if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2886 | /* Write the nRec value into the journal file header. If in |
| 2887 | ** full-synchronous mode, sync the journal first. This ensures that |
| 2888 | ** all data has really hit the disk before nRec is updated to mark |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2889 | ** it as a candidate for rollback. |
| 2890 | ** |
| 2891 | ** This is not required if the persistent media supports the |
| 2892 | ** SAFE_APPEND property. Because in this case it is not possible |
| 2893 | ** for garbage data to be appended to the file, the nRec field |
| 2894 | ** is populated with 0xFFFFFFFF when the journal header is written |
| 2895 | ** and never needs to be updated. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2896 | */ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2897 | i64 jrnlOff; |
| 2898 | if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2899 | PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2900 | IOTRACE(("JSYNC %p\n", pPager)) |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2901 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags); |
drh | d8d66e8 | 2003-02-12 02:10:15 +0000 | [diff] [blame] | 2902 | if( rc!=0 ) return rc; |
| 2903 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2904 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2905 | jrnlOff = pPager->journalHdr + sizeof(aJournalMagic); |
| 2906 | IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4)); |
| 2907 | rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec); |
drh | b4746b9 | 2005-09-09 01:32:06 +0000 | [diff] [blame] | 2908 | if( rc ) return rc; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2909 | } |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2910 | if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
| 2911 | PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); |
| 2912 | IOTRACE(("JSYNC %p\n", pPager)) |
| 2913 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| |
| 2914 | (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) |
| 2915 | ); |
| 2916 | if( rc!=0 ) return rc; |
| 2917 | } |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2918 | pPager->journalStarted = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2919 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2920 | pPager->needSync = 0; |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2921 | |
| 2922 | /* Erase the needSync flag from every page. |
| 2923 | */ |
| 2924 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 2925 | pPg->needSync = 0; |
| 2926 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2927 | lruListSetFirstSynced(pPager); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2928 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2929 | |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2930 | #ifndef NDEBUG |
| 2931 | /* If the Pager.needSync flag is clear then the PgHdr.needSync |
| 2932 | ** flag must also be clear for all pages. Verify that this |
| 2933 | ** invariant is true. |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2934 | */ |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2935 | else{ |
| 2936 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 2937 | assert( pPg->needSync==0 ); |
| 2938 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2939 | assert( pPager->lru.pFirstSynced==pPager->lru.pFirst ); |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2940 | } |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2941 | #endif |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2942 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2943 | return rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | /* |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2947 | ** Merge two lists of pages connected by pDirty and in pgno order. |
| 2948 | ** Do not both fixing the pPrevDirty pointers. |
| 2949 | */ |
| 2950 | static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){ |
| 2951 | PgHdr result, *pTail; |
| 2952 | pTail = &result; |
| 2953 | while( pA && pB ){ |
| 2954 | if( pA->pgno<pB->pgno ){ |
| 2955 | pTail->pDirty = pA; |
| 2956 | pTail = pA; |
| 2957 | pA = pA->pDirty; |
| 2958 | }else{ |
| 2959 | pTail->pDirty = pB; |
| 2960 | pTail = pB; |
| 2961 | pB = pB->pDirty; |
| 2962 | } |
| 2963 | } |
| 2964 | if( pA ){ |
| 2965 | pTail->pDirty = pA; |
| 2966 | }else if( pB ){ |
| 2967 | pTail->pDirty = pB; |
| 2968 | }else{ |
| 2969 | pTail->pDirty = 0; |
| 2970 | } |
| 2971 | return result.pDirty; |
| 2972 | } |
| 2973 | |
| 2974 | /* |
| 2975 | ** Sort the list of pages in accending order by pgno. Pages are |
| 2976 | ** connected by pDirty pointers. The pPrevDirty pointers are |
| 2977 | ** corrupted by this sort. |
| 2978 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2979 | #define N_SORT_BUCKET_ALLOC 25 |
| 2980 | #define N_SORT_BUCKET 25 |
| 2981 | #ifdef SQLITE_TEST |
| 2982 | int sqlite3_pager_n_sort_bucket = 0; |
| 2983 | #undef N_SORT_BUCKET |
| 2984 | #define N_SORT_BUCKET \ |
| 2985 | (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC) |
| 2986 | #endif |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2987 | static PgHdr *sort_pagelist(PgHdr *pIn){ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2988 | PgHdr *a[N_SORT_BUCKET_ALLOC], *p; |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2989 | int i; |
| 2990 | memset(a, 0, sizeof(a)); |
| 2991 | while( pIn ){ |
| 2992 | p = pIn; |
| 2993 | pIn = p->pDirty; |
| 2994 | p->pDirty = 0; |
| 2995 | for(i=0; i<N_SORT_BUCKET-1; i++){ |
| 2996 | if( a[i]==0 ){ |
| 2997 | a[i] = p; |
| 2998 | break; |
| 2999 | }else{ |
| 3000 | p = merge_pagelist(a[i], p); |
| 3001 | a[i] = 0; |
| 3002 | } |
| 3003 | } |
| 3004 | if( i==N_SORT_BUCKET-1 ){ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3005 | /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET) |
| 3006 | ** elements in the input list. This is possible, but impractical. |
| 3007 | ** Testing this line is the point of global variable |
| 3008 | ** sqlite3_pager_n_sort_bucket. |
| 3009 | */ |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 3010 | a[i] = merge_pagelist(a[i], p); |
| 3011 | } |
| 3012 | } |
| 3013 | p = a[0]; |
| 3014 | for(i=1; i<N_SORT_BUCKET; i++){ |
| 3015 | p = merge_pagelist(p, a[i]); |
| 3016 | } |
| 3017 | return p; |
| 3018 | } |
| 3019 | |
| 3020 | /* |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 3021 | ** Given a list of pages (connected by the PgHdr.pDirty pointer) write |
| 3022 | ** every one of those pages out to the database file and mark them all |
| 3023 | ** as clean. |
| 3024 | */ |
| 3025 | static int pager_write_pagelist(PgHdr *pList){ |
| 3026 | Pager *pPager; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3027 | PgHdr *p; |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 3028 | int rc; |
| 3029 | |
| 3030 | if( pList==0 ) return SQLITE_OK; |
| 3031 | pPager = pList->pPager; |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 3032 | |
| 3033 | /* At this point there may be either a RESERVED or EXCLUSIVE lock on the |
| 3034 | ** database file. If there is already an EXCLUSIVE lock, the following |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3035 | ** calls to sqlite3OsLock() are no-ops. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 3036 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3037 | ** Moving the lock from RESERVED to EXCLUSIVE actually involves going |
| 3038 | ** through an intermediate state PENDING. A PENDING lock prevents new |
| 3039 | ** readers from attaching to the database but is unsufficient for us to |
| 3040 | ** write. The idea of a PENDING lock is to prevent new readers from |
| 3041 | ** coming in while we wait for existing readers to clear. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 3042 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3043 | ** While the pager is in the RESERVED state, the original database file |
| 3044 | ** is unchanged and we can rollback without having to playback the |
| 3045 | ** journal into the original database file. Once we transition to |
| 3046 | ** EXCLUSIVE, it means the database file has been changed and any rollback |
| 3047 | ** will require a journal playback. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 3048 | */ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 3049 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 3050 | if( rc!=SQLITE_OK ){ |
| 3051 | return rc; |
| 3052 | } |
| 3053 | |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 3054 | pList = sort_pagelist(pList); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3055 | for(p=pList; p; p=p->pDirty){ |
| 3056 | assert( p->dirty ); |
| 3057 | p->dirty = 0; |
| 3058 | } |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 3059 | while( pList ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 3060 | |
| 3061 | /* If the file has not yet been opened, open it now. */ |
| 3062 | if( !pPager->fd->pMethods ){ |
| 3063 | assert(pPager->tempFile); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3064 | rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 3065 | if( rc ) return rc; |
| 3066 | } |
| 3067 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3068 | /* If there are dirty pages in the page cache with page numbers greater |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3069 | ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3070 | ** make the file smaller (presumably by auto-vacuum code). Do not write |
| 3071 | ** any such pages to the file. |
| 3072 | */ |
| 3073 | if( pList->pgno<=pPager->dbSize ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3074 | i64 offset = (pList->pgno-1)*(i64)pPager->pageSize; |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 3075 | char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 3076 | PAGERTRACE4("STORE %d page %d hash(%08x)\n", |
| 3077 | PAGERID(pPager), pList->pgno, pager_pagehash(pList)); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3078 | IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno)); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3079 | rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3080 | PAGER_INCR(sqlite3_pager_writedb_count); |
| 3081 | PAGER_INCR(pPager->nWrite); |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3082 | if( pList->pgno==1 ){ |
| 3083 | memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); |
| 3084 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3085 | } |
| 3086 | #ifndef NDEBUG |
| 3087 | else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3088 | PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3089 | } |
| 3090 | #endif |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 3091 | if( rc ) return rc; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3092 | #ifdef SQLITE_CHECK_PAGES |
| 3093 | pList->pageHash = pager_pagehash(pList); |
| 3094 | #endif |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 3095 | pList = pList->pDirty; |
| 3096 | } |
| 3097 | return SQLITE_OK; |
| 3098 | } |
| 3099 | |
| 3100 | /* |
| 3101 | ** Collect every dirty page into a dirty list and |
| 3102 | ** return a pointer to the head of that list. All pages are |
| 3103 | ** collected even if they are still in use. |
| 3104 | */ |
| 3105 | static PgHdr *pager_get_all_dirty_pages(Pager *pPager){ |
drh | 8f2e9a1 | 2008-02-26 14:46:04 +0000 | [diff] [blame] | 3106 | |
| 3107 | #ifndef NDEBUG |
| 3108 | /* Verify the sanity of the dirty list when we are running |
| 3109 | ** in debugging mode. This is expensive, so do not |
| 3110 | ** do this on a normal build. */ |
| 3111 | int n1 = 0; |
| 3112 | int n2 = 0; |
| 3113 | PgHdr *p; |
| 3114 | for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; } |
| 3115 | for(p=pPager->pDirty; p; p=p->pDirty){ n2++; } |
| 3116 | assert( n1==n2 ); |
| 3117 | #endif |
| 3118 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3119 | return pPager->pDirty; |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | /* |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 3123 | ** Return 1 if there is a hot journal on the given pager. |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3124 | ** A hot journal is one that needs to be played back. |
| 3125 | ** |
| 3126 | ** If the current size of the database file is 0 but a journal file |
| 3127 | ** exists, that is probably an old journal left over from a prior |
| 3128 | ** database with the same name. Just delete the journal. |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 3129 | ** |
| 3130 | ** Return negative if unable to determine the status of the journal. |
drh | 82ed1e5 | 2008-04-25 12:25:42 +0000 | [diff] [blame] | 3131 | ** |
| 3132 | ** This routine does not open the journal file to examine its |
| 3133 | ** content. Hence, the journal might contain the name of a master |
| 3134 | ** journal file that has been deleted, and hence not be hot. Or |
| 3135 | ** the header of the journal might be zeroed out. This routine |
| 3136 | ** does not discover these cases of a non-hot journal - if the |
| 3137 | ** journal file exists and is not empty this routine assumes it |
| 3138 | ** is hot. The pager_playback() routine will discover that the |
| 3139 | ** journal file is not really hot and will no-op. |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3140 | */ |
| 3141 | static int hasHotJournal(Pager *pPager){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3142 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 3143 | int res = 0; |
| 3144 | if( pPager->useJournal && pPager->fd->pMethods ){ |
| 3145 | int rc; |
| 3146 | int exists; |
| 3147 | int locked; |
| 3148 | |
| 3149 | rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 3150 | if( rc==SQLITE_OK && exists ){ |
| 3151 | rc = sqlite3OsCheckReservedLock(pPager->fd, &locked); |
| 3152 | } |
| 3153 | |
| 3154 | if( rc==SQLITE_OK && exists && !locked ){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3155 | int nPage; |
| 3156 | rc = sqlite3PagerPagecount(pPager, &nPage); |
| 3157 | if( rc==SQLITE_OK && nPage==0 ){ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 3158 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
| 3159 | exists = 0; |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | res = (rc!=SQLITE_OK ? -1 : (exists && !locked)); |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 3164 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 3165 | |
| 3166 | return res; |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 3169 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3170 | ** Try to find a page in the cache that can be recycled. |
| 3171 | ** |
| 3172 | ** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3173 | ** does not set the pPager->errCode variable. |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 3174 | */ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3175 | static int pager_recycle(Pager *pPager, PgHdr **ppPg){ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3176 | PgHdr *pPg; |
| 3177 | *ppPg = 0; |
| 3178 | |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3179 | /* It is illegal to call this function unless the pager object |
| 3180 | ** pointed to by pPager has at least one free page (page with nRef==0). |
| 3181 | */ |
danielk1977 | c551edc | 2007-04-05 13:12:13 +0000 | [diff] [blame] | 3182 | assert(!MEMDB); |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3183 | assert(pPager->lru.pFirst); |
danielk1977 | c551edc | 2007-04-05 13:12:13 +0000 | [diff] [blame] | 3184 | |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3185 | /* Find a page to recycle. Try to locate a page that does not |
| 3186 | ** require us to do an fsync() on the journal. |
| 3187 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3188 | pPg = pPager->lru.pFirstSynced; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3189 | |
| 3190 | /* If we could not find a page that does not require an fsync() |
| 3191 | ** on the journal file then fsync the journal file. This is a |
| 3192 | ** very slow operation, so we work hard to avoid it. But sometimes |
| 3193 | ** it can't be helped. |
| 3194 | */ |
danielk1977 | c41cc39 | 2008-05-15 08:34:54 +0000 | [diff] [blame] | 3195 | if( pPg==0 && pPager->lru.pFirst ){ |
| 3196 | if( !pPager->errCode ){ |
| 3197 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| 3198 | int rc = syncJournal(pPager); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3199 | if( rc!=0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3200 | return rc; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3201 | } |
danielk1977 | c41cc39 | 2008-05-15 08:34:54 +0000 | [diff] [blame] | 3202 | if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
| 3203 | /* If in full-sync mode, write a new journal header into the |
| 3204 | ** journal file. This is done to avoid ever modifying a journal |
| 3205 | ** header that is involved in the rollback of pages that have |
| 3206 | ** already been written to the database (in case the header is |
| 3207 | ** trashed when the nRec field is updated). |
| 3208 | */ |
| 3209 | pPager->nRec = 0; |
| 3210 | assert( pPager->journalOff > 0 ); |
| 3211 | assert( pPager->doNotSync==0 ); |
| 3212 | rc = writeJournalHdr(pPager); |
| 3213 | if( rc!=0 ){ |
| 3214 | return rc; |
| 3215 | } |
| 3216 | } |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3217 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3218 | pPg = pPager->lru.pFirst; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3219 | } |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3220 | |
| 3221 | assert( pPg->nRef==0 ); |
| 3222 | |
| 3223 | /* Write the page to the database file if it is dirty. |
| 3224 | */ |
danielk1977 | c41cc39 | 2008-05-15 08:34:54 +0000 | [diff] [blame] | 3225 | if( pPg->dirty && !pPager->errCode ){ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3226 | int rc; |
| 3227 | assert( pPg->needSync==0 ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3228 | makeClean(pPg); |
| 3229 | pPg->dirty = 1; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3230 | pPg->pDirty = 0; |
| 3231 | rc = pager_write_pagelist( pPg ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3232 | pPg->dirty = 0; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3233 | if( rc!=SQLITE_OK ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3234 | return rc; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3235 | } |
| 3236 | } |
danielk1977 | c41cc39 | 2008-05-15 08:34:54 +0000 | [diff] [blame] | 3237 | assert( pPg->dirty==0 || pPager->errCode ); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3238 | |
| 3239 | /* If the page we are recycling is marked as alwaysRollback, then |
| 3240 | ** set the global alwaysRollback flag, thus disabling the |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3241 | ** sqlite3PagerDontRollback() optimization for the rest of this transaction. |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3242 | ** It is necessary to do this because the page marked alwaysRollback |
| 3243 | ** might be reloaded at a later time but at that point we won't remember |
| 3244 | ** that is was marked alwaysRollback. This means that all pages must |
| 3245 | ** be marked as alwaysRollback from here on out. |
| 3246 | */ |
| 3247 | if( pPg->alwaysRollback ){ |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 3248 | IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager)) |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3249 | pPager->alwaysRollback = 1; |
| 3250 | } |
| 3251 | |
| 3252 | /* Unlink the old page from the free list and the hash table |
| 3253 | */ |
| 3254 | unlinkPage(pPg); |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 3255 | assert( pPg->pgno==0 ); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3256 | |
| 3257 | *ppPg = pPg; |
| 3258 | return SQLITE_OK; |
| 3259 | } |
| 3260 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3261 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3262 | /* |
| 3263 | ** This function is called to free superfluous dynamically allocated memory |
| 3264 | ** held by the pager system. Memory in use by any SQLite pager allocated |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3265 | ** by the current thread may be sqlite3_free()ed. |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3266 | ** |
| 3267 | ** nReq is the number of bytes of memory required. Once this much has |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3268 | ** been released, the function returns. The return value is the total number |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3269 | ** of bytes of memory released. |
| 3270 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3271 | int sqlite3PagerReleaseMemory(int nReq){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3272 | int nReleased = 0; /* Bytes of memory released so far */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3273 | Pager *pPager; /* For looping over pagers */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3274 | BusyHandler *savedBusy; /* Saved copy of the busy handler */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3275 | int rc = SQLITE_OK; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3276 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3277 | /* Acquire the memory-management mutex |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 3278 | */ |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 3279 | #ifndef SQLITE_MUTEX_NOOP |
drh | 64e2bb7 | 2008-05-07 12:29:55 +0000 | [diff] [blame] | 3280 | sqlite3_mutex *mutex; /* The MEM2 mutex */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3281 | mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 3282 | #endif |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3283 | sqlite3_mutex_enter(mutex); |
| 3284 | |
| 3285 | /* Signal all database connections that memory management wants |
| 3286 | ** to have access to the pagers. |
| 3287 | */ |
| 3288 | for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){ |
| 3289 | pPager->iInUseMM = 1; |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3292 | while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){ |
| 3293 | PgHdr *pPg; |
| 3294 | PgHdr *pRecycled; |
| 3295 | |
| 3296 | /* Try to find a page to recycle that does not require a sync(). If |
| 3297 | ** this is not possible, find one that does require a sync(). |
| 3298 | */ |
| 3299 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 3300 | pPg = sqlite3LruPageList.pFirstSynced; |
| 3301 | while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){ |
| 3302 | pPg = pPg->gfree.pNext; |
| 3303 | } |
| 3304 | if( !pPg ){ |
| 3305 | pPg = sqlite3LruPageList.pFirst; |
| 3306 | while( pPg && pPg->pPager->iInUseDB ){ |
| 3307 | pPg = pPg->gfree.pNext; |
| 3308 | } |
| 3309 | } |
| 3310 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3311 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 3312 | /* If pPg==0, then the block above has failed to find a page to |
| 3313 | ** recycle. In this case return early - no further memory will |
| 3314 | ** be released. |
| 3315 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3316 | if( !pPg ) break; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3317 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3318 | pPager = pPg->pPager; |
| 3319 | assert(!pPg->needSync || pPg==pPager->lru.pFirst); |
| 3320 | assert(pPg->needSync || pPg==pPager->lru.pFirstSynced); |
| 3321 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3322 | savedBusy = pPager->pBusyHandler; |
| 3323 | pPager->pBusyHandler = 0; |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3324 | rc = pager_recycle(pPager, &pRecycled); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3325 | pPager->pBusyHandler = savedBusy; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3326 | assert(pRecycled==pPg || rc!=SQLITE_OK); |
| 3327 | if( rc==SQLITE_OK ){ |
| 3328 | /* We've found a page to free. At this point the page has been |
| 3329 | ** removed from the page hash-table, free-list and synced-list |
| 3330 | ** (pFirstSynced). It is still in the all pages (pAll) list. |
| 3331 | ** Remove it from this list before freeing. |
| 3332 | ** |
| 3333 | ** Todo: Check the Pager.pStmt list to make sure this is Ok. It |
| 3334 | ** probably is though. |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3335 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3336 | PgHdr *pTmp; |
| 3337 | assert( pPg ); |
| 3338 | if( pPg==pPager->pAll ){ |
| 3339 | pPager->pAll = pPg->pNextAll; |
| 3340 | }else{ |
| 3341 | for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){} |
| 3342 | pTmp->pNextAll = pPg->pNextAll; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3343 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3344 | nReleased += ( |
| 3345 | sizeof(*pPg) + pPager->pageSize |
| 3346 | + sizeof(u32) + pPager->nExtra |
| 3347 | + MEMDB*sizeof(PgHistory) |
| 3348 | ); |
| 3349 | IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno)); |
| 3350 | PAGER_INCR(sqlite3_pager_pgfree_count); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 3351 | sqlite3PageFree(pPg->pData); |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3352 | sqlite3_free(pPg); |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 3353 | pPager->nPage--; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3354 | }else{ |
| 3355 | /* An error occured whilst writing to the database file or |
| 3356 | ** journal in pager_recycle(). The error is not returned to the |
| 3357 | ** caller of this function. Instead, set the Pager.errCode variable. |
| 3358 | ** The error will be returned to the user (or users, in the case |
| 3359 | ** of a shared pager cache) of the pager for which the error occured. |
| 3360 | */ |
| 3361 | assert( |
| 3362 | (rc&0xff)==SQLITE_IOERR || |
| 3363 | rc==SQLITE_FULL || |
| 3364 | rc==SQLITE_BUSY |
| 3365 | ); |
| 3366 | assert( pPager->state>=PAGER_RESERVED ); |
| 3367 | pager_error(pPager, rc); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3368 | } |
| 3369 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3370 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3371 | /* Clear the memory management flags and release the mutex |
| 3372 | */ |
| 3373 | for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){ |
| 3374 | pPager->iInUseMM = 0; |
| 3375 | } |
| 3376 | sqlite3_mutex_leave(mutex); |
| 3377 | |
| 3378 | /* Return the number of bytes released |
| 3379 | */ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3380 | return nReleased; |
| 3381 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3382 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3383 | |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3384 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3385 | ** Read the content of page pPg out of the database file. |
| 3386 | */ |
| 3387 | static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){ |
| 3388 | int rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3389 | i64 offset; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3390 | assert( MEMDB==0 ); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 3391 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 3392 | if( !pPager->fd->pMethods ){ |
| 3393 | return SQLITE_IOERR_SHORT_READ; |
| 3394 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3395 | offset = (pgno-1)*(i64)pPager->pageSize; |
| 3396 | rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3397 | PAGER_INCR(sqlite3_pager_readdb_count); |
| 3398 | PAGER_INCR(pPager->nRead); |
| 3399 | IOTRACE(("PGIN %p %d\n", pPager, pgno)); |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3400 | if( pgno==1 ){ |
| 3401 | memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24], |
| 3402 | sizeof(pPager->dbFileVers)); |
| 3403 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3404 | CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 3405 | PAGERTRACE4("FETCH %d page %d hash(%08x)\n", |
| 3406 | PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3407 | return rc; |
| 3408 | } |
| 3409 | |
| 3410 | |
| 3411 | /* |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3412 | ** This function is called to obtain the shared lock required before |
| 3413 | ** data may be read from the pager cache. If the shared lock has already |
| 3414 | ** been obtained, this function is a no-op. |
danielk1977 | 393f068 | 2007-03-31 10:00:48 +0000 | [diff] [blame] | 3415 | ** |
| 3416 | ** Immediately after obtaining the shared lock (if required), this function |
| 3417 | ** checks for a hot-journal file. If one is found, an emergency rollback |
| 3418 | ** is performed immediately. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3419 | */ |
| 3420 | static int pagerSharedLock(Pager *pPager){ |
| 3421 | int rc = SQLITE_OK; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3422 | int isHot = 0; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3423 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3424 | /* If this database is opened for exclusive access, has no outstanding |
| 3425 | ** page references and is in an error-state, now is the chance to clear |
| 3426 | ** the error. Discard the contents of the pager-cache and treat any |
| 3427 | ** open journal file as a hot-journal. |
| 3428 | */ |
| 3429 | if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){ |
| 3430 | if( pPager->journalOpen ){ |
| 3431 | isHot = 1; |
| 3432 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3433 | pPager->errCode = SQLITE_OK; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 3434 | pager_reset(pPager); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | /* If the pager is still in an error state, do not proceed. The error |
| 3438 | ** state will be cleared at some point in the future when all page |
| 3439 | ** references are dropped and the cache can be discarded. |
| 3440 | */ |
| 3441 | if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
| 3442 | return pPager->errCode; |
| 3443 | } |
| 3444 | |
| 3445 | if( pPager->state==PAGER_UNLOCK || isHot ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3446 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3447 | if( !MEMDB ){ |
| 3448 | assert( pPager->nRef==0 ); |
| 3449 | if( !pPager->noReadlock ){ |
| 3450 | rc = pager_wait_on_lock(pPager, SHARED_LOCK); |
| 3451 | if( rc!=SQLITE_OK ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 3452 | assert( pPager->state==PAGER_UNLOCK ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3453 | return pager_error(pPager, rc); |
| 3454 | } |
| 3455 | assert( pPager->state>=SHARED_LOCK ); |
| 3456 | } |
| 3457 | |
| 3458 | /* If a journal file exists, and there is no RESERVED lock on the |
| 3459 | ** database file, then it either needs to be played back or deleted. |
| 3460 | */ |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 3461 | rc = hasHotJournal(pPager); |
| 3462 | if( rc<0 ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 3463 | rc = SQLITE_IOERR_NOMEM; |
| 3464 | goto failed; |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 3465 | } |
| 3466 | if( rc==1 || isHot ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3467 | /* Get an EXCLUSIVE lock on the database file. At this point it is |
| 3468 | ** important that a RESERVED lock is not obtained on the way to the |
| 3469 | ** EXCLUSIVE lock. If it were, another process might open the |
| 3470 | ** database file, detect the RESERVED lock, and conclude that the |
| 3471 | ** database is safe to read while this process is still rolling it |
| 3472 | ** back. |
| 3473 | ** |
| 3474 | ** Because the intermediate RESERVED lock is not requested, the |
| 3475 | ** second process will get to this point in the code and fail to |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3476 | ** obtain its own EXCLUSIVE lock on the database file. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3477 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3478 | if( pPager->state<EXCLUSIVE_LOCK ){ |
| 3479 | rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK); |
| 3480 | if( rc!=SQLITE_OK ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 3481 | rc = pager_error(pPager, rc); |
| 3482 | goto failed; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3483 | } |
| 3484 | pPager->state = PAGER_EXCLUSIVE; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3485 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3486 | |
drh | 16e45a4 | 2008-04-19 20:34:18 +0000 | [diff] [blame] | 3487 | /* Open the journal for read/write access. This is because in |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3488 | ** exclusive-access mode the file descriptor will be kept open and |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3489 | ** possibly used for a transaction later on. On some systems, the |
| 3490 | ** OsTruncate() call used in exclusive-access mode also requires |
| 3491 | ** a read/write file handle. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3492 | */ |
drh | 16e45a4 | 2008-04-19 20:34:18 +0000 | [diff] [blame] | 3493 | if( !isHot && pPager->journalOpen==0 ){ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 3494 | int res; |
| 3495 | rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res); |
| 3496 | if( rc==SQLITE_OK ){ |
| 3497 | if( res ){ |
| 3498 | int fout = 0; |
| 3499 | int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL; |
| 3500 | assert( !pPager->tempFile ); |
| 3501 | rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout); |
| 3502 | assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); |
| 3503 | if( fout&SQLITE_OPEN_READONLY ){ |
| 3504 | rc = SQLITE_BUSY; |
| 3505 | sqlite3OsClose(pPager->jfd); |
| 3506 | } |
| 3507 | }else{ |
| 3508 | /* If the journal does not exist, that means some other process |
| 3509 | ** has already rolled it back */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3510 | rc = SQLITE_BUSY; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3511 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3512 | } |
| 3513 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3514 | if( rc!=SQLITE_OK ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 3515 | if( rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_UNLOCK |
| 3516 | && rc!=SQLITE_IOERR_NOMEM |
| 3517 | ){ |
| 3518 | rc = SQLITE_BUSY; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 3519 | } |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 3520 | goto failed; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3521 | } |
| 3522 | pPager->journalOpen = 1; |
| 3523 | pPager->journalStarted = 0; |
| 3524 | pPager->journalOff = 0; |
| 3525 | pPager->setMaster = 0; |
| 3526 | pPager->journalHdr = 0; |
| 3527 | |
| 3528 | /* Playback and delete the journal. Drop the database write |
| 3529 | ** lock and reacquire the read lock. |
| 3530 | */ |
| 3531 | rc = pager_playback(pPager, 1); |
| 3532 | if( rc!=SQLITE_OK ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 3533 | rc = pager_error(pPager, rc); |
| 3534 | goto failed; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3535 | } |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 3536 | assert(pPager->state==PAGER_SHARED || |
| 3537 | (pPager->exclusiveMode && pPager->state>PAGER_SHARED) |
| 3538 | ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | if( pPager->pAll ){ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3542 | /* The shared-lock has just been acquired on the database file |
| 3543 | ** and there are already pages in the cache (from a previous |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3544 | ** read or write transaction). Check to see if the database |
| 3545 | ** has been modified. If the database has changed, flush the |
| 3546 | ** cache. |
| 3547 | ** |
| 3548 | ** Database changes is detected by looking at 15 bytes beginning |
| 3549 | ** at offset 24 into the file. The first 4 of these 16 bytes are |
| 3550 | ** a 32-bit counter that is incremented with each change. The |
| 3551 | ** other bytes change randomly with each file change when |
| 3552 | ** a codec is in use. |
| 3553 | ** |
| 3554 | ** There is a vanishingly small chance that a change will not be |
drh | 6fa5103 | 2007-05-09 20:35:31 +0000 | [diff] [blame] | 3555 | ** detected. The chance of an undetected change is so small that |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3556 | ** it can be neglected. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3557 | */ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3558 | char dbFileVers[sizeof(pPager->dbFileVers)]; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3559 | sqlite3PagerPagecount(pPager, 0); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3560 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3561 | if( pPager->errCode ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 3562 | rc = pPager->errCode; |
| 3563 | goto failed; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3566 | if( pPager->dbSize>0 ){ |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 3567 | IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3568 | rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3569 | if( rc!=SQLITE_OK ){ |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 3570 | goto failed; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3571 | } |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3572 | }else{ |
| 3573 | memset(dbFileVers, 0, sizeof(dbFileVers)); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3576 | if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3577 | pager_reset(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3578 | } |
| 3579 | } |
| 3580 | } |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 3581 | assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED ); |
| 3582 | if( pPager->state==PAGER_UNLOCK ){ |
| 3583 | pPager->state = PAGER_SHARED; |
| 3584 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3585 | } |
| 3586 | |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 3587 | failed: |
| 3588 | if( rc!=SQLITE_OK ){ |
| 3589 | /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */ |
| 3590 | pager_unlock(pPager); |
| 3591 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3592 | return rc; |
| 3593 | } |
| 3594 | |
| 3595 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3596 | ** Allocate a PgHdr object. Either create a new one or reuse |
| 3597 | ** an existing one that is not otherwise in use. |
| 3598 | ** |
| 3599 | ** A new PgHdr structure is created if any of the following are |
| 3600 | ** true: |
| 3601 | ** |
| 3602 | ** (1) We have not exceeded our maximum allocated cache size |
| 3603 | ** as set by the "PRAGMA cache_size" command. |
| 3604 | ** |
| 3605 | ** (2) There are no unused PgHdr objects available at this time. |
| 3606 | ** |
| 3607 | ** (3) This is an in-memory database. |
| 3608 | ** |
| 3609 | ** (4) There are no PgHdr objects that do not require a journal |
| 3610 | ** file sync and a sync of the journal file is currently |
| 3611 | ** prohibited. |
| 3612 | ** |
| 3613 | ** Otherwise, reuse an existing PgHdr. In other words, reuse an |
| 3614 | ** existing PgHdr if all of the following are true: |
| 3615 | ** |
| 3616 | ** (1) We have reached or exceeded the maximum cache size |
| 3617 | ** allowed by "PRAGMA cache_size". |
| 3618 | ** |
| 3619 | ** (2) There is a PgHdr available with PgHdr->nRef==0 |
| 3620 | ** |
| 3621 | ** (3) We are not in an in-memory database |
| 3622 | ** |
| 3623 | ** (4) Either there is an available PgHdr that does not need |
| 3624 | ** to be synced to disk or else disk syncing is currently |
| 3625 | ** allowed. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3626 | */ |
| 3627 | static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){ |
| 3628 | int rc = SQLITE_OK; |
| 3629 | PgHdr *pPg; |
drh | 1e3af33 | 2007-10-20 13:17:54 +0000 | [diff] [blame] | 3630 | int nByteHdr; |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3631 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3632 | /* Create a new PgHdr if any of the four conditions defined |
danielk1977 | 2a6bdf6 | 2007-08-20 16:07:00 +0000 | [diff] [blame] | 3633 | ** above are met: */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3634 | if( pPager->nPage<pPager->mxPage |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3635 | || pPager->lru.pFirst==0 |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3636 | || MEMDB |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3637 | || (pPager->lru.pFirstSynced==0 && pPager->doNotSync) |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3638 | ){ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 3639 | void *pData; |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3640 | if( pPager->nPage>=pPager->nHash ){ |
| 3641 | pager_resize_hash_table(pPager, |
| 3642 | pPager->nHash<256 ? 256 : pPager->nHash*2); |
| 3643 | if( pPager->nHash==0 ){ |
| 3644 | rc = SQLITE_NOMEM; |
| 3645 | goto pager_allocate_out; |
| 3646 | } |
| 3647 | } |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3648 | pagerLeave(pPager); |
drh | 1e3af33 | 2007-10-20 13:17:54 +0000 | [diff] [blame] | 3649 | nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra |
| 3650 | + MEMDB*sizeof(PgHistory); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 3651 | pPg = sqlite3Malloc( nByteHdr ); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 3652 | if( pPg ){ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 3653 | pData = sqlite3PageMalloc( pPager->pageSize ); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 3654 | if( pData==0 ){ |
| 3655 | sqlite3_free(pPg); |
| 3656 | pPg = 0; |
| 3657 | } |
| 3658 | } |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3659 | pagerEnter(pPager); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3660 | if( pPg==0 ){ |
| 3661 | rc = SQLITE_NOMEM; |
| 3662 | goto pager_allocate_out; |
| 3663 | } |
drh | 1e3af33 | 2007-10-20 13:17:54 +0000 | [diff] [blame] | 3664 | memset(pPg, 0, nByteHdr); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 3665 | pPg->pData = pData; |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3666 | pPg->pPager = pPager; |
| 3667 | pPg->pNextAll = pPager->pAll; |
| 3668 | pPager->pAll = pPg; |
| 3669 | pPager->nPage++; |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3670 | }else{ |
| 3671 | /* Recycle an existing page with a zero ref-count. */ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3672 | rc = pager_recycle(pPager, &pPg); |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 3673 | if( rc==SQLITE_BUSY ){ |
| 3674 | rc = SQLITE_IOERR_BLOCKED; |
| 3675 | } |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3676 | if( rc!=SQLITE_OK ){ |
| 3677 | goto pager_allocate_out; |
| 3678 | } |
| 3679 | assert( pPager->state>=SHARED_LOCK ); |
| 3680 | assert(pPg); |
| 3681 | } |
| 3682 | *ppPg = pPg; |
| 3683 | |
| 3684 | pager_allocate_out: |
| 3685 | return rc; |
| 3686 | } |
| 3687 | |
| 3688 | /* |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3689 | ** Make sure we have the content for a page. If the page was |
| 3690 | ** previously acquired with noContent==1, then the content was |
| 3691 | ** just initialized to zeros instead of being read from disk. |
| 3692 | ** But now we need the real data off of disk. So make sure we |
| 3693 | ** have it. Read it in if we do not have it already. |
| 3694 | */ |
| 3695 | static int pager_get_content(PgHdr *pPg){ |
| 3696 | if( pPg->needRead ){ |
| 3697 | int rc = readDbPage(pPg->pPager, pPg, pPg->pgno); |
| 3698 | if( rc==SQLITE_OK ){ |
| 3699 | pPg->needRead = 0; |
| 3700 | }else{ |
| 3701 | return rc; |
| 3702 | } |
| 3703 | } |
| 3704 | return SQLITE_OK; |
| 3705 | } |
| 3706 | |
| 3707 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3708 | ** Acquire a page. |
| 3709 | ** |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 3710 | ** 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] | 3711 | ** This read lock is dropped when the last page is released. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3712 | ** |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3713 | ** This routine works for any page number greater than 0. If the database |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3714 | ** file is smaller than the requested page, then no actual disk |
| 3715 | ** read occurs and the memory image of the page is initialized to |
| 3716 | ** all zeros. The extra data appended to a page is always initialized |
| 3717 | ** to zeros the first time a page is loaded into memory. |
| 3718 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3719 | ** The acquisition might fail for several reasons. In all cases, |
| 3720 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3721 | ** |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3722 | ** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3723 | ** 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] | 3724 | ** in memory, this routine goes to disk to read it in whereas Lookup() |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3725 | ** just returns 0. This routine acquires a read-lock the first time it |
| 3726 | ** 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] | 3727 | ** Since Lookup() never goes to disk, it never has to deal with locks |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3728 | ** or journal files. |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 3729 | ** |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3730 | ** If noContent is false, the page contents are actually read from disk. |
| 3731 | ** If noContent is true, it means that we do not care about the contents |
| 3732 | ** of the page at this time, so do not do a disk read. Just fill in the |
| 3733 | ** page content with zeros. But mark the fact that we have not read the |
| 3734 | ** content by setting the PgHdr.needRead flag. Later on, if |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3735 | ** sqlite3PagerWrite() is called on this page or if this routine is |
| 3736 | ** called again with noContent==0, that means that the content is needed |
| 3737 | ** and the disk read should occur at that point. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3738 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3739 | static int pagerAcquire( |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3740 | Pager *pPager, /* The pager open on the database file */ |
| 3741 | Pgno pgno, /* Page number to fetch */ |
| 3742 | DbPage **ppPage, /* Write a pointer to the page here */ |
| 3743 | int noContent /* Do not bother reading content from disk if true */ |
| 3744 | ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3745 | PgHdr *pPg; |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3746 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3747 | |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3748 | assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 ); |
| 3749 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 3750 | /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page |
| 3751 | ** number greater than this, or zero, is requested. |
| 3752 | */ |
drh | 267cb32 | 2005-09-16 17:16:52 +0000 | [diff] [blame] | 3753 | if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3754 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 3755 | } |
| 3756 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3757 | /* Make sure we have not hit any critical errors. |
| 3758 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 3759 | assert( pPager!=0 ); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3760 | *ppPage = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3761 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3762 | /* If this is the first page accessed, then get a SHARED lock |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3763 | ** on the database file. pagerSharedLock() is a no-op if |
| 3764 | ** a database lock is already held. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3765 | */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3766 | rc = pagerSharedLock(pPager); |
| 3767 | if( rc!=SQLITE_OK ){ |
| 3768 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3769 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3770 | assert( pPager->state!=PAGER_UNLOCK ); |
| 3771 | |
| 3772 | pPg = pager_lookup(pPager, pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3773 | if( pPg==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3774 | /* The requested page is not in the page cache. */ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3775 | int nMax; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3776 | int h; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3777 | PAGER_INCR(pPager->nMiss); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3778 | rc = pagerAllocatePage(pPager, &pPg); |
| 3779 | if( rc!=SQLITE_OK ){ |
| 3780 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3781 | } |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3782 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3783 | pPg->pgno = pgno; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 3784 | assert( !MEMDB || pgno>pPager->stmtSize ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3785 | pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno); |
| 3786 | pPg->needSync = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 3787 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3788 | makeClean(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3789 | pPg->nRef = 1; |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3790 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3791 | pPager->nRef++; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3792 | if( pPager->nExtra>0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 3793 | memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3794 | } |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3795 | rc = sqlite3PagerPagecount(pPager, &nMax); |
| 3796 | if( rc!=SQLITE_OK ){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3797 | sqlite3PagerUnref(pPg); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3798 | return rc; |
| 3799 | } |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3800 | |
| 3801 | /* Populate the page with data, either by reading from the database |
| 3802 | ** file, or by setting the entire page to zero. |
| 3803 | */ |
drh | d215acf | 2007-04-13 04:01:58 +0000 | [diff] [blame] | 3804 | if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 3805 | if( pgno>pPager->mxPgno ){ |
danielk1977 | de3bea7 | 2007-05-09 15:56:39 +0000 | [diff] [blame] | 3806 | sqlite3PagerUnref(pPg); |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 3807 | return SQLITE_FULL; |
| 3808 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 3809 | memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize); |
drh | d215acf | 2007-04-13 04:01:58 +0000 | [diff] [blame] | 3810 | pPg->needRead = noContent && !pPager->alwaysRollback; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3811 | IOTRACE(("ZERO %p %d\n", pPager, pgno)); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3812 | }else{ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3813 | rc = readDbPage(pPager, pPg, pgno); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 3814 | if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ |
| 3815 | pPg->pgno = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3816 | sqlite3PagerUnref(pPg); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 3817 | return rc; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 3818 | } |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 3819 | pPg->needRead = 0; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3820 | } |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3821 | |
| 3822 | /* Link the page into the page hash table */ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 3823 | h = pgno & (pPager->nHash-1); |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 3824 | assert( pgno!=0 ); |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3825 | pPg->pNextHash = pPager->aHash[h]; |
| 3826 | pPager->aHash[h] = pPg; |
| 3827 | if( pPg->pNextHash ){ |
| 3828 | assert( pPg->pNextHash->pPrevHash==0 ); |
| 3829 | pPg->pNextHash->pPrevHash = pPg; |
| 3830 | } |
| 3831 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3832 | #ifdef SQLITE_CHECK_PAGES |
| 3833 | pPg->pageHash = pager_pagehash(pPg); |
| 3834 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3835 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3836 | /* The requested page is in the page cache. */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3837 | assert(pPager->nRef>0 || pgno==1); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3838 | PAGER_INCR(pPager->nHit); |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3839 | if( !noContent ){ |
| 3840 | rc = pager_get_content(pPg); |
| 3841 | if( rc ){ |
| 3842 | return rc; |
| 3843 | } |
| 3844 | } |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 3845 | page_ref(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3846 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3847 | *ppPage = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3848 | return SQLITE_OK; |
| 3849 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3850 | int sqlite3PagerAcquire( |
| 3851 | Pager *pPager, /* The pager open on the database file */ |
| 3852 | Pgno pgno, /* Page number to fetch */ |
| 3853 | DbPage **ppPage, /* Write a pointer to the page here */ |
| 3854 | int noContent /* Do not bother reading content from disk if true */ |
| 3855 | ){ |
| 3856 | int rc; |
| 3857 | pagerEnter(pPager); |
| 3858 | rc = pagerAcquire(pPager, pgno, ppPage, noContent); |
| 3859 | pagerLeave(pPager); |
| 3860 | return rc; |
| 3861 | } |
| 3862 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3863 | |
| 3864 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3865 | ** Acquire a page if it is already in the in-memory cache. Do |
| 3866 | ** not read the page from disk. Return a pointer to the page, |
| 3867 | ** or 0 if the page is not in cache. |
| 3868 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3869 | ** See also sqlite3PagerGet(). The difference between this routine |
| 3870 | ** and sqlite3PagerGet() is that _get() will go to the disk and read |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3871 | ** in the page if the page is not already in cache. This routine |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3872 | ** returns NULL if the page is not in cache or if a disk I/O error |
| 3873 | ** has ever happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3874 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3875 | DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3876 | PgHdr *pPg = 0; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3877 | |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 3878 | assert( pPager!=0 ); |
| 3879 | assert( pgno!=0 ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3880 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3881 | pagerEnter(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3882 | if( pPager->state==PAGER_UNLOCK ){ |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3883 | assert( !pPager->pAll || pPager->exclusiveMode ); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3884 | }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
| 3885 | /* Do nothing */ |
| 3886 | }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){ |
| 3887 | page_ref(pPg); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3888 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3889 | pagerLeave(pPager); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3890 | return pPg; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3894 | ** Release a page. |
| 3895 | ** |
| 3896 | ** If the number of references to the page drop to zero, then the |
| 3897 | ** page is added to the LRU list. When all references to all pages |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3898 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3899 | ** removed. |
| 3900 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3901 | int sqlite3PagerUnref(DbPage *pPg){ |
drh | 6eac06e | 2008-05-07 12:45:41 +0000 | [diff] [blame] | 3902 | Pager *pPager; |
| 3903 | |
| 3904 | if( pPg==0 ) return SQLITE_OK; |
| 3905 | pPager = pPg->pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3906 | |
| 3907 | /* Decrement the reference count for this page |
| 3908 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3909 | assert( pPg->nRef>0 ); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3910 | pagerEnter(pPg->pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3911 | pPg->nRef--; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3912 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3913 | CHECK_PAGE(pPg); |
| 3914 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3915 | /* When the number of references to a page reach 0, call the |
| 3916 | ** destructor and add the page to the freelist. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3917 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3918 | if( pPg->nRef==0 ){ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3919 | |
| 3920 | lruListAdd(pPg); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3921 | if( pPager->xDestructor ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3922 | pPager->xDestructor(pPg, pPager->pageSize); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3923 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3924 | |
| 3925 | /* When all pages reach the freelist, drop the read lock from |
| 3926 | ** the database file. |
| 3927 | */ |
| 3928 | pPager->nRef--; |
| 3929 | assert( pPager->nRef>=0 ); |
danielk1977 | 3dedc19 | 2007-03-27 17:37:31 +0000 | [diff] [blame] | 3930 | if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3931 | pagerUnlockAndRollback(pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3932 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3933 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3934 | pagerLeave(pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3935 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3939 | ** Create a journal file for pPager. There should already be a RESERVED |
| 3940 | ** or EXCLUSIVE lock on the database file when this routine is called. |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3941 | ** |
| 3942 | ** Return SQLITE_OK if everything. Return an error code and release the |
| 3943 | ** write lock if anything goes wrong. |
| 3944 | */ |
| 3945 | static int pager_open_journal(Pager *pPager){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3946 | sqlite3_vfs *pVfs = pPager->pVfs; |
| 3947 | int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE); |
| 3948 | |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3949 | int rc; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3950 | assert( !MEMDB ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3951 | assert( pPager->state>=PAGER_RESERVED ); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3952 | assert( pPager->useJournal ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3953 | assert( pPager->pInJournal==0 ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3954 | sqlite3PagerPagecount(pPager, 0); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3955 | pagerLeave(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3956 | pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3957 | pagerEnter(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3958 | if( pPager->pInJournal==0 ){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3959 | rc = SQLITE_NOMEM; |
| 3960 | goto failed_to_open_journal; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3961 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3962 | |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3963 | if( pPager->journalOpen==0 ){ |
| 3964 | if( pPager->tempFile ){ |
| 3965 | flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); |
| 3966 | }else{ |
| 3967 | flags |= (SQLITE_OPEN_MAIN_JOURNAL); |
drh | 600e46a | 2007-03-28 01:59:33 +0000 | [diff] [blame] | 3968 | } |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 3969 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 3970 | rc = sqlite3JournalOpen( |
| 3971 | pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) |
| 3972 | ); |
| 3973 | #else |
| 3974 | rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); |
| 3975 | #endif |
| 3976 | assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); |
| 3977 | pPager->journalOff = 0; |
| 3978 | pPager->setMaster = 0; |
| 3979 | pPager->journalHdr = 0; |
| 3980 | if( rc!=SQLITE_OK ){ |
| 3981 | if( rc==SQLITE_NOMEM ){ |
| 3982 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
| 3983 | } |
| 3984 | goto failed_to_open_journal; |
| 3985 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3986 | } |
| 3987 | pPager->journalOpen = 1; |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 3988 | pPager->journalStarted = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3989 | pPager->needSync = 0; |
| 3990 | pPager->alwaysRollback = 0; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 3991 | pPager->nRec = 0; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3992 | if( pPager->errCode ){ |
| 3993 | rc = pPager->errCode; |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 3994 | goto failed_to_open_journal; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3995 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3996 | pPager->origDbSize = pPager->dbSize; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 3997 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 3998 | rc = writeJournalHdr(pPager); |
| 3999 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4000 | if( pPager->stmtAutoopen && rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4001 | rc = sqlite3PagerStmtBegin(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4002 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 4003 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 4004 | rc = pager_end_transaction(pPager, 0); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4005 | if( rc==SQLITE_OK ){ |
| 4006 | rc = SQLITE_FULL; |
| 4007 | } |
| 4008 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 4009 | return rc; |
| 4010 | |
| 4011 | failed_to_open_journal: |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4012 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 4013 | pPager->pInJournal = 0; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 4014 | return rc; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | /* |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4018 | ** Acquire a write-lock on the database. The lock is removed when |
| 4019 | ** the any of the following happen: |
| 4020 | ** |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4021 | ** * sqlite3PagerCommitPhaseTwo() is called. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4022 | ** * sqlite3PagerRollback() is called. |
| 4023 | ** * sqlite3PagerClose() is called. |
| 4024 | ** * sqlite3PagerUnref() is called to on every outstanding page. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4025 | ** |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 4026 | ** The first parameter to this routine is a pointer to any open page of the |
| 4027 | ** database file. Nothing changes about the page - it is used merely to |
| 4028 | ** acquire a pointer to the Pager structure and as proof that there is |
| 4029 | ** already a read-lock on the database. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4030 | ** |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 4031 | ** The second parameter indicates how much space in bytes to reserve for a |
| 4032 | ** master journal file-name at the start of the journal when it is created. |
| 4033 | ** |
| 4034 | ** A journal file is opened if this is not a temporary file. For temporary |
| 4035 | ** files, the opening of the journal file is deferred until there is an |
| 4036 | ** actual need to write to the journal. |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4037 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4038 | ** 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] | 4039 | ** |
| 4040 | ** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file |
| 4041 | ** immediately instead of waiting until we try to flush the cache. The |
| 4042 | ** exFlag is ignored if a transaction is already active. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4043 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4044 | int sqlite3PagerBegin(DbPage *pPg, int exFlag){ |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4045 | Pager *pPager = pPg->pPager; |
| 4046 | int rc = SQLITE_OK; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4047 | pagerEnter(pPager); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4048 | assert( pPg->nRef>0 ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4049 | assert( pPager->state!=PAGER_UNLOCK ); |
| 4050 | if( pPager->state==PAGER_SHARED ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4051 | assert( pPager->pInJournal==0 ); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4052 | if( MEMDB ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4053 | pPager->state = PAGER_EXCLUSIVE; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4054 | pPager->origDbSize = pPager->dbSize; |
| 4055 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4056 | rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK); |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 4057 | if( rc==SQLITE_OK ){ |
| 4058 | pPager->state = PAGER_RESERVED; |
| 4059 | if( exFlag ){ |
| 4060 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| 4061 | } |
| 4062 | } |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 4063 | if( rc!=SQLITE_OK ){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4064 | pagerLeave(pPager); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 4065 | return rc; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4066 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4067 | pPager->dirtyCache = 0; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4068 | PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager)); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 4069 | if( pPager->useJournal && !pPager->tempFile |
| 4070 | && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4071 | rc = pager_open_journal(pPager); |
| 4072 | } |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4073 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 4074 | }else if( pPager->journalOpen && pPager->journalOff==0 ){ |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 4075 | /* This happens when the pager was in exclusive-access mode the last |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 4076 | ** time a (read or write) transaction was successfully concluded |
| 4077 | ** by this connection. Instead of deleting the journal file it was |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 4078 | ** kept open and either was truncated to 0 bytes or its header was |
| 4079 | ** overwritten with zeros. |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 4080 | */ |
| 4081 | assert( pPager->nRec==0 ); |
| 4082 | assert( pPager->origDbSize==0 ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4083 | assert( pPager->pInJournal==0 ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4084 | sqlite3PagerPagecount(pPager, 0); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 4085 | pagerLeave(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4086 | pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 4087 | pagerEnter(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4088 | if( !pPager->pInJournal ){ |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 4089 | rc = SQLITE_NOMEM; |
| 4090 | }else{ |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 4091 | pPager->origDbSize = pPager->dbSize; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 4092 | rc = writeJournalHdr(pPager); |
| 4093 | } |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4094 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 4095 | assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK ); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4096 | pagerLeave(pPager); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4097 | return rc; |
| 4098 | } |
| 4099 | |
| 4100 | /* |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4101 | ** Make a page dirty. Set its dirty flag and add it to the dirty |
| 4102 | ** page list. |
| 4103 | */ |
| 4104 | static void makeDirty(PgHdr *pPg){ |
| 4105 | if( pPg->dirty==0 ){ |
| 4106 | Pager *pPager = pPg->pPager; |
| 4107 | pPg->dirty = 1; |
| 4108 | pPg->pDirty = pPager->pDirty; |
| 4109 | if( pPager->pDirty ){ |
| 4110 | pPager->pDirty->pPrevDirty = pPg; |
| 4111 | } |
| 4112 | pPg->pPrevDirty = 0; |
| 4113 | pPager->pDirty = pPg; |
| 4114 | } |
| 4115 | } |
| 4116 | |
| 4117 | /* |
| 4118 | ** Make a page clean. Clear its dirty bit and remove it from the |
| 4119 | ** dirty page list. |
| 4120 | */ |
| 4121 | static void makeClean(PgHdr *pPg){ |
| 4122 | if( pPg->dirty ){ |
| 4123 | pPg->dirty = 0; |
| 4124 | if( pPg->pDirty ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4125 | assert( pPg->pDirty->pPrevDirty==pPg ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4126 | pPg->pDirty->pPrevDirty = pPg->pPrevDirty; |
| 4127 | } |
| 4128 | if( pPg->pPrevDirty ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4129 | assert( pPg->pPrevDirty->pDirty==pPg ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4130 | pPg->pPrevDirty->pDirty = pPg->pDirty; |
| 4131 | }else{ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4132 | assert( pPg->pPager->pDirty==pPg ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4133 | pPg->pPager->pDirty = pPg->pDirty; |
| 4134 | } |
| 4135 | } |
| 4136 | } |
| 4137 | |
| 4138 | |
| 4139 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4140 | ** Mark a data page as writeable. The page is written into the journal |
| 4141 | ** if it is not there already. This routine must be called before making |
| 4142 | ** changes to a page. |
| 4143 | ** |
| 4144 | ** The first time this routine is called, the pager creates a new |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4145 | ** journal and acquires a RESERVED lock on the database. If the RESERVED |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4146 | ** lock could not be acquired, this routine returns SQLITE_BUSY. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 4147 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4148 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4149 | ** |
| 4150 | ** If the journal file could not be written because the disk is full, |
| 4151 | ** then this routine returns SQLITE_FULL and does an immediate rollback. |
| 4152 | ** All subsequent write attempts also return SQLITE_FULL until there |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4153 | ** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4154 | ** reset. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4155 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4156 | static int pager_write(PgHdr *pPg){ |
| 4157 | void *pData = PGHDR_TO_DATA(pPg); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 4158 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 4159 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 4160 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 4161 | /* Check for errors |
| 4162 | */ |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4163 | if( pPager->errCode ){ |
| 4164 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4165 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4166 | if( pPager->readOnly ){ |
| 4167 | return SQLITE_PERM; |
| 4168 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 4169 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 4170 | assert( !pPager->setMaster ); |
| 4171 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 4172 | CHECK_PAGE(pPg); |
| 4173 | |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4174 | /* If this page was previously acquired with noContent==1, that means |
| 4175 | ** we didn't really read in the content of the page. This can happen |
| 4176 | ** (for example) when the page is being moved to the freelist. But |
| 4177 | ** now we are (perhaps) moving the page off of the freelist for |
| 4178 | ** reuse and we need to know its original content so that content |
| 4179 | ** can be stored in the rollback journal. So do the read at this |
| 4180 | ** time. |
| 4181 | */ |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 4182 | rc = pager_get_content(pPg); |
| 4183 | if( rc ){ |
| 4184 | return rc; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 4187 | /* Mark the page as dirty. If the page has already been written |
| 4188 | ** to the journal then we can return right away. |
| 4189 | */ |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4190 | makeDirty(pPg); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4191 | if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4192 | pPager->dirtyCache = 1; |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 4193 | pPager->dbModified = 1; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4194 | }else{ |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 4195 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4196 | /* If we get this far, it means that the page needs to be |
| 4197 | ** written to the transaction journal or the ckeckpoint journal |
| 4198 | ** or both. |
| 4199 | ** |
| 4200 | ** First check to see that the transaction journal exists and |
| 4201 | ** create it if it does not. |
| 4202 | */ |
| 4203 | assert( pPager->state!=PAGER_UNLOCK ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4204 | rc = sqlite3PagerBegin(pPg, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4205 | if( rc!=SQLITE_OK ){ |
| 4206 | return rc; |
| 4207 | } |
| 4208 | assert( pPager->state>=PAGER_RESERVED ); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 4209 | if( !pPager->journalOpen && pPager->useJournal |
| 4210 | && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4211 | rc = pager_open_journal(pPager); |
| 4212 | if( rc!=SQLITE_OK ) return rc; |
| 4213 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4214 | pPager->dirtyCache = 1; |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 4215 | pPager->dbModified = 1; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4216 | |
| 4217 | /* The transaction journal now exists and we have a RESERVED or an |
| 4218 | ** EXCLUSIVE lock on the main database file. Write the current page to |
| 4219 | ** the transaction journal if it is not there already. |
| 4220 | */ |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 4221 | if( !pPg->inJournal && (pPager->journalOpen || MEMDB) ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4222 | if( (int)pPg->pgno <= pPager->origDbSize ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4223 | if( MEMDB ){ |
| 4224 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4225 | PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4226 | assert( pHist->pOrig==0 ); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 4227 | pHist->pOrig = sqlite3PageMalloc( pPager->pageSize ); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 4228 | if( !pHist->pOrig ){ |
| 4229 | return SQLITE_NOMEM; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4230 | } |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 4231 | memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4232 | }else{ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4233 | u32 cksum; |
| 4234 | char *pData2; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4235 | |
drh | 267cb32 | 2005-09-16 17:16:52 +0000 | [diff] [blame] | 4236 | /* We should never write to the journal file the page that |
| 4237 | ** contains the database locks. The following assert verifies |
| 4238 | ** that we do not. */ |
| 4239 | assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4240 | pData2 = CODEC2(pPager, pData, pPg->pgno, 7); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 4241 | cksum = pager_cksum(pPager, (u8*)pData2); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4242 | rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno); |
| 4243 | if( rc==SQLITE_OK ){ |
| 4244 | rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, |
| 4245 | pPager->journalOff + 4); |
| 4246 | pPager->journalOff += pPager->pageSize+4; |
| 4247 | } |
| 4248 | if( rc==SQLITE_OK ){ |
| 4249 | rc = write32bits(pPager->jfd, pPager->journalOff, cksum); |
| 4250 | pPager->journalOff += 4; |
| 4251 | } |
| 4252 | IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, |
danielk1977 | 667a6c9 | 2007-09-10 06:12:04 +0000 | [diff] [blame] | 4253 | pPager->journalOff, pPager->pageSize)); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4254 | PAGER_INCR(sqlite3_pager_writej_count); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 4255 | PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n", |
| 4256 | PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg)); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4257 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 4258 | /* An error has occured writing to the journal file. The |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4259 | ** transaction will be rolled back by the layer above. |
| 4260 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4261 | if( rc!=SQLITE_OK ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4262 | return rc; |
| 4263 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4264 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4265 | pPager->nRec++; |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4266 | assert( pPager->pInJournal!=0 ); |
| 4267 | sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4268 | pPg->needSync = !pPager->noSync; |
| 4269 | if( pPager->stmtInUse ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4270 | sqlite3BitvecSet(pPager->pInStmt, pPg->pgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4271 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4272 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 4273 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4274 | pPg->needSync = !pPager->journalStarted && !pPager->noSync; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4275 | PAGERTRACE4("APPEND %d page %d needSync=%d\n", |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 4276 | PAGERID(pPager), pPg->pgno, pPg->needSync); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4277 | } |
| 4278 | if( pPg->needSync ){ |
| 4279 | pPager->needSync = 1; |
| 4280 | } |
| 4281 | pPg->inJournal = 1; |
| 4282 | } |
| 4283 | |
| 4284 | /* If the statement journal is open and the page is not in it, |
| 4285 | ** then write the current page to the statement journal. Note that |
| 4286 | ** the statement journal format differs from the standard journal format |
| 4287 | ** in that it omits the checksums and the header. |
| 4288 | */ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4289 | if( pPager->stmtInUse |
| 4290 | && !pageInStatement(pPg) |
| 4291 | && (int)pPg->pgno<=pPager->stmtSize |
| 4292 | ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4293 | assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); |
| 4294 | if( MEMDB ){ |
| 4295 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 4296 | assert( pHist->pStmt==0 ); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 4297 | pHist->pStmt = sqlite3PageMalloc( pPager->pageSize ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4298 | if( pHist->pStmt ){ |
| 4299 | memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize); |
| 4300 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4301 | PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4302 | page_add_to_stmt_list(pPg); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4303 | }else{ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4304 | i64 offset = pPager->stmtNRec*(4+pPager->pageSize); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4305 | char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7); |
| 4306 | rc = write32bits(pPager->stfd, offset, pPg->pgno); |
| 4307 | if( rc==SQLITE_OK ){ |
| 4308 | rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4); |
| 4309 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4310 | PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4311 | if( rc!=SQLITE_OK ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4312 | return rc; |
| 4313 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4314 | pPager->stmtNRec++; |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4315 | assert( pPager->pInStmt!=0 ); |
| 4316 | sqlite3BitvecSet(pPager->pInStmt, pPg->pgno); |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 4317 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4318 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | /* Update the database size and return. |
| 4322 | */ |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 4323 | assert( pPager->state>=PAGER_SHARED ); |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 4324 | if( pPager->dbSize<(int)pPg->pgno ){ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 4325 | pPager->dbSize = pPg->pgno; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4326 | if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){ |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 4327 | pPager->dbSize++; |
| 4328 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 4329 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 4330 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | /* |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4334 | ** This function is used to mark a data-page as writable. It uses |
| 4335 | ** pager_write() to open a journal file (if it is not already open) |
| 4336 | ** and write the page *pData to the journal. |
| 4337 | ** |
| 4338 | ** The difference between this function and pager_write() is that this |
| 4339 | ** function also deals with the special case where 2 or more pages |
| 4340 | ** fit on a single disk sector. In this case all co-resident pages |
| 4341 | ** must have been written to the journal file before returning. |
| 4342 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4343 | int sqlite3PagerWrite(DbPage *pDbPage){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4344 | int rc = SQLITE_OK; |
| 4345 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4346 | PgHdr *pPg = pDbPage; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4347 | Pager *pPager = pPg->pPager; |
| 4348 | Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); |
| 4349 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4350 | pagerEnter(pPager); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4351 | if( !MEMDB && nPagePerSector>1 ){ |
| 4352 | Pgno nPageCount; /* Total number of pages in database file */ |
| 4353 | Pgno pg1; /* First page of the sector pPg is located on. */ |
| 4354 | int nPage; /* Number of pages starting at pg1 to journal */ |
| 4355 | int ii; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4356 | int needSync = 0; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4357 | |
| 4358 | /* Set the doNotSync flag to 1. This is because we cannot allow a journal |
| 4359 | ** header to be written between the pages journaled by this function. |
| 4360 | */ |
| 4361 | assert( pPager->doNotSync==0 ); |
| 4362 | pPager->doNotSync = 1; |
| 4363 | |
| 4364 | /* This trick assumes that both the page-size and sector-size are |
| 4365 | ** an integer power of 2. It sets variable pg1 to the identifier |
| 4366 | ** of the first page of the sector pPg is located on. |
| 4367 | */ |
| 4368 | pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; |
| 4369 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4370 | sqlite3PagerPagecount(pPager, (int *)&nPageCount); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4371 | if( pPg->pgno>nPageCount ){ |
| 4372 | nPage = (pPg->pgno - pg1)+1; |
| 4373 | }else if( (pg1+nPagePerSector-1)>nPageCount ){ |
| 4374 | nPage = nPageCount+1-pg1; |
| 4375 | }else{ |
| 4376 | nPage = nPagePerSector; |
| 4377 | } |
| 4378 | assert(nPage>0); |
| 4379 | assert(pg1<=pPg->pgno); |
| 4380 | assert((pg1+nPage)>pPg->pgno); |
| 4381 | |
| 4382 | for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ |
| 4383 | Pgno pg = pg1+ii; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4384 | PgHdr *pPage; |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4385 | if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4386 | if( pg!=PAGER_MJ_PGNO(pPager) ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4387 | rc = sqlite3PagerGet(pPager, pg, &pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4388 | if( rc==SQLITE_OK ){ |
| 4389 | rc = pager_write(pPage); |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4390 | if( pPage->needSync ){ |
| 4391 | needSync = 1; |
| 4392 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4393 | sqlite3PagerUnref(pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4394 | } |
| 4395 | } |
drh | c81945e | 2008-03-10 14:12:53 +0000 | [diff] [blame] | 4396 | }else if( (pPage = pager_lookup(pPager, pg))!=0 ){ |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4397 | if( pPage->needSync ){ |
| 4398 | needSync = 1; |
| 4399 | } |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4400 | } |
| 4401 | } |
| 4402 | |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4403 | /* If the PgHdr.needSync flag is set for any of the nPage pages |
| 4404 | ** starting at pg1, then it needs to be set for all of them. Because |
| 4405 | ** writing to any of these nPage pages may damage the others, the |
| 4406 | ** journal file must contain sync()ed copies of all of them |
| 4407 | ** before any of them can be written out to the database file. |
| 4408 | */ |
| 4409 | if( needSync ){ |
| 4410 | for(ii=0; ii<nPage && needSync; ii++){ |
| 4411 | PgHdr *pPage = pager_lookup(pPager, pg1+ii); |
| 4412 | if( pPage ) pPage->needSync = 1; |
| 4413 | } |
| 4414 | assert(pPager->needSync); |
| 4415 | } |
| 4416 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4417 | assert( pPager->doNotSync==1 ); |
| 4418 | pPager->doNotSync = 0; |
| 4419 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4420 | rc = pager_write(pDbPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4421 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4422 | pagerLeave(pPager); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4423 | return rc; |
| 4424 | } |
| 4425 | |
| 4426 | /* |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 4427 | ** Return TRUE if the page given in the argument was previously passed |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4428 | ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4429 | ** to change the content of the page. |
| 4430 | */ |
danielk1977 | 7d3a666 | 2006-01-23 16:21:05 +0000 | [diff] [blame] | 4431 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4432 | int sqlite3PagerIswriteable(DbPage *pPg){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4433 | return pPg->dirty; |
| 4434 | } |
danielk1977 | 7d3a666 | 2006-01-23 16:21:05 +0000 | [diff] [blame] | 4435 | #endif |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4436 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4437 | /* |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4438 | ** 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] | 4439 | ** write the information on page pPg back to the disk, even though |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4440 | ** that page might be marked as dirty. |
| 4441 | ** |
| 4442 | ** The overlying software layer calls this routine when all of the data |
| 4443 | ** on the given page is unused. The pager marks the page as clean so |
| 4444 | ** that it does not get written to disk. |
| 4445 | ** |
| 4446 | ** Tests show that this optimization, together with the |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4447 | ** sqlite3PagerDontRollback() below, more than double the speed |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4448 | ** of large INSERT operations and quadruple the speed of large DELETEs. |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4449 | ** |
| 4450 | ** When this routine is called, set the alwaysRollback flag to true. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4451 | ** Subsequent calls to sqlite3PagerDontRollback() for the same page |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4452 | ** will thereafter be ignored. This is necessary to avoid a problem |
| 4453 | ** where a page with data is added to the freelist during one part of |
| 4454 | ** a transaction then removed from the freelist during a later part |
| 4455 | ** of the same transaction and reused for some other purpose. When it |
| 4456 | ** is first added to the freelist, this routine is called. When reused, |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4457 | ** the sqlite3PagerDontRollback() routine is called. But because the |
| 4458 | ** page contains critical data, we still need to be sure it gets |
| 4459 | ** rolled back in spite of the sqlite3PagerDontRollback() call. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4460 | */ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4461 | void sqlite3PagerDontWrite(DbPage *pDbPage){ |
| 4462 | PgHdr *pPg = pDbPage; |
| 4463 | Pager *pPager = pPg->pPager; |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4464 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4465 | if( MEMDB ) return; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4466 | pagerEnter(pPager); |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4467 | pPg->alwaysRollback = 1; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4468 | if( pPg->dirty && !pPager->stmtInUse ){ |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 4469 | assert( pPager->state>=PAGER_SHARED ); |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 4470 | if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){ |
| 4471 | /* If this pages is the last page in the file and the file has grown |
| 4472 | ** during the current transaction, then do NOT mark the page as clean. |
| 4473 | ** When the database file grows, we must make sure that the last page |
| 4474 | ** gets written at least once so that the disk file will be the correct |
| 4475 | ** size. If you do not write this page and the size of the file |
| 4476 | ** on the disk ends up being too small, that can lead to database |
| 4477 | ** corruption during the next transaction. |
| 4478 | */ |
| 4479 | }else{ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4480 | PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)); |
| 4481 | IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4482 | makeClean(pPg); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 4483 | #ifdef SQLITE_CHECK_PAGES |
| 4484 | pPg->pageHash = pager_pagehash(pPg); |
| 4485 | #endif |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 4486 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4487 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4488 | pagerLeave(pPager); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | /* |
| 4492 | ** A call to this routine tells the pager that if a rollback occurs, |
| 4493 | ** it is not necessary to restore the data on the given page. This |
| 4494 | ** means that the pager does not have to record the given page in the |
| 4495 | ** rollback journal. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4496 | ** |
| 4497 | ** If we have not yet actually read the content of this page (if |
| 4498 | ** the PgHdr.needRead flag is set) then this routine acts as a promise |
| 4499 | ** that we will never need to read the page content in the future. |
| 4500 | ** so the needRead flag can be cleared at this point. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4501 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4502 | void sqlite3PagerDontRollback(DbPage *pPg){ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4503 | Pager *pPager = pPg->pPager; |
| 4504 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4505 | pagerEnter(pPager); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4506 | assert( pPager->state>=PAGER_RESERVED ); |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 4507 | |
| 4508 | /* If the journal file is not open, or DontWrite() has been called on |
| 4509 | ** this page (DontWrite() sets the alwaysRollback flag), then this |
| 4510 | ** function is a no-op. |
| 4511 | */ |
| 4512 | if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){ |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4513 | pagerLeave(pPager); |
| 4514 | return; |
| 4515 | } |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 4516 | assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */ |
| 4517 | |
drh | c5d0bd9 | 2008-04-14 01:00:57 +0000 | [diff] [blame] | 4518 | #ifdef SQLITE_SECURE_DELETE |
| 4519 | if( pPg->inJournal || (int)pPg->pgno > pPager->origDbSize ){ |
| 4520 | return; |
| 4521 | } |
| 4522 | #endif |
| 4523 | |
| 4524 | /* If SECURE_DELETE is disabled, then there is no way that this |
| 4525 | ** routine can be called on a page for which sqlite3PagerDontWrite() |
| 4526 | ** has not been previously called during the same transaction. |
| 4527 | ** And if DontWrite() has previously been called, the following |
| 4528 | ** conditions must be met. |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 4529 | */ |
| 4530 | assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); |
| 4531 | |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4532 | assert( pPager->pInJournal!=0 ); |
| 4533 | sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 4534 | pPg->inJournal = 1; |
| 4535 | pPg->needRead = 0; |
| 4536 | if( pPager->stmtInUse ){ |
drh | c5d0bd9 | 2008-04-14 01:00:57 +0000 | [diff] [blame] | 4537 | assert( pPager->stmtSize >= pPager->origDbSize ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4538 | sqlite3BitvecSet(pPager->pInStmt, pPg->pgno); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4539 | } |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 4540 | PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)); |
| 4541 | IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno)) |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4542 | pagerLeave(pPager); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4545 | |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4546 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4547 | ** This routine is called to increment the database file change-counter, |
| 4548 | ** stored at byte 24 of the pager file. |
| 4549 | */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4550 | static int pager_incr_changecounter(Pager *pPager, int isDirect){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4551 | PgHdr *pPgHdr; |
| 4552 | u32 change_counter; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4553 | int rc = SQLITE_OK; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4554 | |
| 4555 | if( !pPager->changeCountDone ){ |
| 4556 | /* Open page 1 of the file for writing. */ |
| 4557 | rc = sqlite3PagerGet(pPager, 1, &pPgHdr); |
| 4558 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4559 | |
| 4560 | if( !isDirect ){ |
| 4561 | rc = sqlite3PagerWrite(pPgHdr); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 4562 | if( rc!=SQLITE_OK ){ |
| 4563 | sqlite3PagerUnref(pPgHdr); |
| 4564 | return rc; |
| 4565 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4568 | /* Increment the value just read and write it back to byte 24. */ |
drh | b100391 | 2007-07-20 00:33:36 +0000 | [diff] [blame] | 4569 | change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4570 | change_counter++; |
| 4571 | put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4572 | |
| 4573 | if( isDirect && pPager->fd->pMethods ){ |
| 4574 | const void *zBuf = PGHDR_TO_DATA(pPgHdr); |
| 4575 | rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); |
| 4576 | } |
| 4577 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4578 | /* Release the page reference. */ |
| 4579 | sqlite3PagerUnref(pPgHdr); |
| 4580 | pPager->changeCountDone = 1; |
| 4581 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4582 | return rc; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | /* |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 4586 | ** Sync the pager file to disk. |
| 4587 | */ |
| 4588 | int sqlite3PagerSync(Pager *pPager){ |
| 4589 | int rc; |
| 4590 | pagerEnter(pPager); |
| 4591 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
| 4592 | pagerLeave(pPager); |
| 4593 | return rc; |
| 4594 | } |
| 4595 | |
| 4596 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4597 | ** Sync the database file for the pager pPager. zMaster points to the name |
| 4598 | ** of a master journal file that should be written into the individual |
| 4599 | ** journal file. zMaster may be NULL, which is interpreted as no master |
| 4600 | ** journal (a single database transaction). |
| 4601 | ** |
| 4602 | ** This routine ensures that the journal is synced, all dirty pages written |
| 4603 | ** to the database file and the database file synced. The only thing that |
| 4604 | ** remains to commit the transaction is to delete the journal file (or |
| 4605 | ** master journal file if specified). |
| 4606 | ** |
| 4607 | ** Note that if zMaster==NULL, this does not overwrite a previous value |
| 4608 | ** passed to an sqlite3PagerCommitPhaseOne() call. |
| 4609 | ** |
| 4610 | ** If parameter nTrunc is non-zero, then the pager file is truncated to |
| 4611 | ** nTrunc pages (this is used by auto-vacuum databases). |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 4612 | ** |
| 4613 | ** If the final parameter - noSync - is true, then the database file itself |
| 4614 | ** is not synced. The caller must call sqlite3PagerSync() directly to |
| 4615 | ** sync the database file before calling CommitPhaseTwo() to delete the |
| 4616 | ** journal file in this case. |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4617 | */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 4618 | int sqlite3PagerCommitPhaseOne( |
| 4619 | Pager *pPager, |
| 4620 | const char *zMaster, |
| 4621 | Pgno nTrunc, |
| 4622 | int noSync |
| 4623 | ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4624 | int rc = SQLITE_OK; |
| 4625 | |
danielk1977 | dad31b5 | 2008-05-15 11:08:07 +0000 | [diff] [blame] | 4626 | if( pPager->errCode ){ |
| 4627 | return pPager->errCode; |
| 4628 | } |
| 4629 | |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 4630 | /* If no changes have been made, we can leave the transaction early. |
| 4631 | */ |
| 4632 | if( pPager->dbModified==0 && |
| 4633 | (pPager->journalMode!=PAGER_JOURNALMODE_DELETE || |
| 4634 | pPager->exclusiveMode!=0) ){ |
| 4635 | assert( pPager->dirtyCache==0 || pPager->journalOpen==0 ); |
| 4636 | return SQLITE_OK; |
| 4637 | } |
| 4638 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4639 | PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n", |
| 4640 | pPager->zFilename, zMaster, nTrunc); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4641 | pagerEnter(pPager); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4642 | |
| 4643 | /* If this is an in-memory db, or no pages have been written to, or this |
| 4644 | ** function has already been called, it is a no-op. |
| 4645 | */ |
| 4646 | if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){ |
| 4647 | PgHdr *pPg; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4648 | |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4649 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 4650 | /* The atomic-write optimization can be used if all of the |
| 4651 | ** following are true: |
| 4652 | ** |
| 4653 | ** + The file-system supports the atomic-write property for |
| 4654 | ** blocks of size page-size, and |
| 4655 | ** + This commit is not part of a multi-file transaction, and |
| 4656 | ** + Exactly one page has been modified and store in the journal file. |
| 4657 | ** |
| 4658 | ** If the optimization can be used, then the journal file will never |
| 4659 | ** be created for this transaction. |
| 4660 | */ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4661 | int useAtomicWrite = ( |
| 4662 | !zMaster && |
danielk1977 | 700b9c5 | 2008-04-24 12:37:40 +0000 | [diff] [blame] | 4663 | pPager->journalOpen && |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4664 | pPager->journalOff==jrnlBufferSize(pPager) && |
| 4665 | nTrunc==0 && |
| 4666 | (0==pPager->pDirty || 0==pPager->pDirty->pDirty) |
| 4667 | ); |
danielk1977 | 700b9c5 | 2008-04-24 12:37:40 +0000 | [diff] [blame] | 4668 | assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF ); |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4669 | if( useAtomicWrite ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4670 | /* Update the nRec field in the journal file. */ |
| 4671 | int offset = pPager->journalHdr + sizeof(aJournalMagic); |
| 4672 | assert(pPager->nRec==1); |
| 4673 | rc = write32bits(pPager->jfd, offset, pPager->nRec); |
| 4674 | |
| 4675 | /* Update the db file change counter. The following call will modify |
| 4676 | ** the in-memory representation of page 1 to include the updated |
| 4677 | ** change counter and then write page 1 directly to the database |
| 4678 | ** file. Because of the atomic-write property of the host file-system, |
| 4679 | ** this is safe. |
| 4680 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 4681 | if( rc==SQLITE_OK ){ |
| 4682 | rc = pager_incr_changecounter(pPager, 1); |
| 4683 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4684 | }else{ |
| 4685 | rc = sqlite3JournalCreate(pPager->jfd); |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4686 | } |
| 4687 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 4688 | if( !useAtomicWrite && rc==SQLITE_OK ) |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4689 | #endif |
| 4690 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4691 | /* If a master journal file name has already been written to the |
| 4692 | ** journal file, then no sync is required. This happens when it is |
| 4693 | ** written, then the process fails to upgrade from a RESERVED to an |
| 4694 | ** EXCLUSIVE lock. The next time the process tries to commit the |
| 4695 | ** transaction the m-j name will have already been written. |
| 4696 | */ |
| 4697 | if( !pPager->setMaster ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4698 | rc = pager_incr_changecounter(pPager, 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4699 | if( rc!=SQLITE_OK ) goto sync_exit; |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 4700 | if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4701 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 4702 | if( nTrunc!=0 ){ |
| 4703 | /* If this transaction has made the database smaller, then all pages |
| 4704 | ** being discarded by the truncation must be written to the journal |
| 4705 | ** file. |
| 4706 | */ |
| 4707 | Pgno i; |
| 4708 | int iSkip = PAGER_MJ_PGNO(pPager); |
| 4709 | for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){ |
| 4710 | if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){ |
| 4711 | rc = sqlite3PagerGet(pPager, i, &pPg); |
| 4712 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4713 | rc = sqlite3PagerWrite(pPg); |
| 4714 | sqlite3PagerUnref(pPg); |
| 4715 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4716 | } |
| 4717 | } |
| 4718 | } |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4719 | #endif |
danielk1977 | 71aa7ff | 2008-05-20 07:05:09 +0000 | [diff] [blame] | 4720 | rc = writeMasterJournal(pPager, zMaster); |
| 4721 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4722 | rc = syncJournal(pPager); |
| 4723 | } |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4724 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4725 | if( rc!=SQLITE_OK ) goto sync_exit; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4726 | |
| 4727 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4728 | if( nTrunc!=0 ){ |
| 4729 | rc = sqlite3PagerTruncate(pPager, nTrunc); |
| 4730 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4731 | } |
| 4732 | #endif |
| 4733 | |
| 4734 | /* Write all dirty pages to the database file */ |
| 4735 | pPg = pager_get_all_dirty_pages(pPager); |
| 4736 | rc = pager_write_pagelist(pPg); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4737 | if( rc!=SQLITE_OK ){ |
drh | 04c3a46 | 2008-02-26 16:16:45 +0000 | [diff] [blame] | 4738 | assert( rc!=SQLITE_IOERR_BLOCKED ); |
| 4739 | /* The error might have left the dirty list all fouled up here, |
| 4740 | ** but that does not matter because if the if the dirty list did |
| 4741 | ** get corrupted, then the transaction will roll back and |
| 4742 | ** discard the dirty list. There is an assert in |
| 4743 | ** pager_get_all_dirty_pages() that verifies that no attempt |
| 4744 | ** is made to use an invalid dirty list. |
| 4745 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4746 | goto sync_exit; |
| 4747 | } |
drh | 3cdd7d3 | 2007-03-30 14:46:01 +0000 | [diff] [blame] | 4748 | pPager->pDirty = 0; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4749 | |
| 4750 | /* Sync the database file. */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 4751 | if( !pPager->noSync && !noSync ){ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 4752 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4753 | } |
| 4754 | IOTRACE(("DBSYNC %p\n", pPager)) |
| 4755 | |
| 4756 | pPager->state = PAGER_SYNCED; |
| 4757 | }else if( MEMDB && nTrunc!=0 ){ |
| 4758 | rc = sqlite3PagerTruncate(pPager, nTrunc); |
| 4759 | } |
| 4760 | |
| 4761 | sync_exit: |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 4762 | if( rc==SQLITE_IOERR_BLOCKED ){ |
| 4763 | /* pager_incr_changecounter() may attempt to obtain an exclusive |
| 4764 | * lock to spill the cache and return IOERR_BLOCKED. But since |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 4765 | * there is no chance the cache is inconsistent, it is |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 4766 | * better to return SQLITE_BUSY. |
| 4767 | */ |
| 4768 | rc = SQLITE_BUSY; |
| 4769 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4770 | pagerLeave(pPager); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4771 | return rc; |
| 4772 | } |
| 4773 | |
| 4774 | |
| 4775 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4776 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4777 | ** |
| 4778 | ** If the commit fails for any reason, a rollback attempt is made |
| 4779 | ** and an error code is returned. If the commit worked, SQLITE_OK |
| 4780 | ** is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4781 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4782 | int sqlite3PagerCommitPhaseTwo(Pager *pPager){ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 4783 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4784 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4785 | |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4786 | if( pPager->errCode ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 4787 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4788 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4789 | if( pPager->state<PAGER_RESERVED ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4790 | return SQLITE_ERROR; |
| 4791 | } |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 4792 | if( pPager->dbModified==0 && |
| 4793 | (pPager->journalMode!=PAGER_JOURNALMODE_DELETE || |
| 4794 | pPager->exclusiveMode!=0) ){ |
| 4795 | assert( pPager->dirtyCache==0 || pPager->journalOpen==0 ); |
| 4796 | return SQLITE_OK; |
| 4797 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4798 | pagerEnter(pPager); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4799 | PAGERTRACE2("COMMIT %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4800 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4801 | pPg = pager_get_all_dirty_pages(pPager); |
| 4802 | while( pPg ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4803 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 4804 | clearHistory(pHist); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4805 | pPg->dirty = 0; |
| 4806 | pPg->inJournal = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4807 | pHist->inStmt = 0; |
drh | 5229ae4 | 2006-03-23 23:29:04 +0000 | [diff] [blame] | 4808 | pPg->needSync = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4809 | pHist->pPrevStmt = pHist->pNextStmt = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4810 | pPg = pPg->pDirty; |
| 4811 | } |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4812 | pPager->pDirty = 0; |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 4813 | #ifndef NDEBUG |
| 4814 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 4815 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 4816 | assert( !pPg->alwaysRollback ); |
| 4817 | assert( !pHist->pOrig ); |
| 4818 | assert( !pHist->pStmt ); |
| 4819 | } |
| 4820 | #endif |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4821 | pPager->pStmt = 0; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4822 | pPager->state = PAGER_SHARED; |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4823 | pagerLeave(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4824 | return SQLITE_OK; |
| 4825 | } |
drh | 3cdd7d3 | 2007-03-30 14:46:01 +0000 | [diff] [blame] | 4826 | assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache ); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 4827 | rc = pager_end_transaction(pPager, pPager->setMaster); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4828 | rc = pager_error(pPager, rc); |
| 4829 | pagerLeave(pPager); |
| 4830 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4834 | ** Rollback all changes. The database falls back to PAGER_SHARED mode. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4835 | ** All in-memory cache pages revert to their original data contents. |
| 4836 | ** The journal is deleted. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4837 | ** |
| 4838 | ** This routine cannot fail unless some other process is not following |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 4839 | ** the correct locking protocol or unless some other |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4840 | ** process is writing trash into the journal file (SQLITE_CORRUPT) or |
| 4841 | ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error |
| 4842 | ** codes are returned for all these occasions. Otherwise, |
| 4843 | ** SQLITE_OK is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4844 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4845 | int sqlite3PagerRollback(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4846 | int rc; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4847 | PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4848 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4849 | PgHdr *p; |
| 4850 | for(p=pPager->pAll; p; p=p->pNextAll){ |
| 4851 | PgHistory *pHist; |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 4852 | assert( !p->alwaysRollback ); |
| 4853 | if( !p->dirty ){ |
| 4854 | assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig ); |
| 4855 | assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt ); |
| 4856 | continue; |
| 4857 | } |
| 4858 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4859 | pHist = PGHDR_TO_HIST(p, pPager); |
| 4860 | if( pHist->pOrig ){ |
| 4861 | memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4862 | PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager)); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4863 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4864 | PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager)); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4865 | } |
| 4866 | clearHistory(pHist); |
| 4867 | p->dirty = 0; |
| 4868 | p->inJournal = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4869 | pHist->inStmt = 0; |
| 4870 | pHist->pPrevStmt = pHist->pNextStmt = 0; |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 4871 | if( pPager->xReiniter ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4872 | pPager->xReiniter(p, pPager->pageSize); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 4873 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4874 | } |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4875 | pPager->pDirty = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4876 | pPager->pStmt = 0; |
| 4877 | pPager->dbSize = pPager->origDbSize; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 4878 | pager_truncate_cache(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4879 | pPager->stmtInUse = 0; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4880 | pPager->state = PAGER_SHARED; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4881 | return SQLITE_OK; |
| 4882 | } |
| 4883 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4884 | pagerEnter(pPager); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4885 | if( !pPager->dirtyCache || !pPager->journalOpen ){ |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 4886 | rc = pager_end_transaction(pPager, pPager->setMaster); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4887 | pagerLeave(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4888 | return rc; |
| 4889 | } |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 4890 | |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4891 | if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4892 | if( pPager->state>=PAGER_EXCLUSIVE ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 4893 | pager_playback(pPager, 0); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4894 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4895 | pagerLeave(pPager); |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4896 | return pPager->errCode; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4897 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4898 | if( pPager->state==PAGER_RESERVED ){ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 4899 | int rc2; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 4900 | rc = pager_playback(pPager, 0); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 4901 | rc2 = pager_end_transaction(pPager, pPager->setMaster); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4902 | if( rc==SQLITE_OK ){ |
| 4903 | rc = rc2; |
| 4904 | } |
| 4905 | }else{ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 4906 | rc = pager_playback(pPager, 0); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4907 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 4908 | /* pager_reset(pPager); */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4909 | pPager->dbSize = -1; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4910 | |
| 4911 | /* If an error occurs during a ROLLBACK, we can no longer trust the pager |
| 4912 | ** cache. So call pager_error() on the way out to make any error |
| 4913 | ** persistent. |
| 4914 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4915 | rc = pager_error(pPager, rc); |
| 4916 | pagerLeave(pPager); |
| 4917 | return rc; |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 4918 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4919 | |
| 4920 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4921 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 4922 | ** if the database is (in theory) writable. |
| 4923 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4924 | int sqlite3PagerIsreadonly(Pager *pPager){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 4925 | return pPager->readOnly; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | /* |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4929 | ** Return the number of references to the pager. |
| 4930 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4931 | int sqlite3PagerRefcount(Pager *pPager){ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4932 | return pPager->nRef; |
| 4933 | } |
| 4934 | |
| 4935 | #ifdef SQLITE_TEST |
| 4936 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4937 | ** This routine is used for testing and analysis only. |
| 4938 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4939 | int *sqlite3PagerStats(Pager *pPager){ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 4940 | static int a[11]; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4941 | a[0] = pPager->nRef; |
| 4942 | a[1] = pPager->nPage; |
| 4943 | a[2] = pPager->mxPage; |
| 4944 | a[3] = pPager->dbSize; |
| 4945 | a[4] = pPager->state; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4946 | a[5] = pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4947 | a[6] = pPager->nHit; |
| 4948 | a[7] = pPager->nMiss; |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4949 | a[8] = 0; /* Used to be pPager->nOvfl */ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 4950 | a[9] = pPager->nRead; |
| 4951 | a[10] = pPager->nWrite; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4952 | return a; |
| 4953 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4954 | int sqlite3PagerIsMemdb(Pager *pPager){ |
| 4955 | return MEMDB; |
| 4956 | } |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4957 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4958 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4959 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4960 | ** Set the statement rollback point. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4961 | ** |
| 4962 | ** This routine should be called with the transaction journal already |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4963 | ** open. A new statement journal is created that can be used to rollback |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4964 | ** changes of a single SQL command within a larger transaction. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4965 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4966 | static int pagerStmtBegin(Pager *pPager){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4967 | int rc; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4968 | assert( !pPager->stmtInUse ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 4969 | assert( pPager->state>=PAGER_SHARED ); |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 4970 | assert( pPager->dbSize>=0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4971 | PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4972 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4973 | pPager->stmtInUse = 1; |
| 4974 | pPager->stmtSize = pPager->dbSize; |
| 4975 | return SQLITE_OK; |
| 4976 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4977 | if( !pPager->journalOpen ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4978 | pPager->stmtAutoopen = 1; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4979 | return SQLITE_OK; |
| 4980 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4981 | assert( pPager->journalOpen ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 4982 | pagerLeave(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4983 | assert( pPager->pInStmt==0 ); |
| 4984 | pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 4985 | pagerEnter(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4986 | if( pPager->pInStmt==0 ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4987 | /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4988 | return SQLITE_NOMEM; |
| 4989 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 4990 | pPager->stmtJSize = pPager->journalOff; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4991 | pPager->stmtSize = pPager->dbSize; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 4992 | pPager->stmtHdrOff = 0; |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 4993 | pPager->stmtCksum = pPager->cksumInit; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4994 | if( !pPager->stmtOpen ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4995 | rc = sqlite3PagerOpentemp(pPager, pPager->stfd, SQLITE_OPEN_SUBJOURNAL); |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 4996 | if( rc ){ |
| 4997 | goto stmt_begin_failed; |
| 4998 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4999 | pPager->stmtOpen = 1; |
| 5000 | pPager->stmtNRec = 0; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 5001 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5002 | pPager->stmtInUse = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 5003 | return SQLITE_OK; |
| 5004 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5005 | stmt_begin_failed: |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 5006 | if( pPager->pInStmt ){ |
| 5007 | sqlite3BitvecDestroy(pPager->pInStmt); |
| 5008 | pPager->pInStmt = 0; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 5009 | } |
| 5010 | return rc; |
| 5011 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 5012 | int sqlite3PagerStmtBegin(Pager *pPager){ |
| 5013 | int rc; |
| 5014 | pagerEnter(pPager); |
| 5015 | rc = pagerStmtBegin(pPager); |
| 5016 | pagerLeave(pPager); |
| 5017 | return rc; |
| 5018 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 5019 | |
| 5020 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5021 | ** Commit a statement. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 5022 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5023 | int sqlite3PagerStmtCommit(Pager *pPager){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 5024 | pagerEnter(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5025 | if( pPager->stmtInUse ){ |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 5026 | PgHdr *pPg, *pNext; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5027 | PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5028 | if( !MEMDB ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5029 | /* sqlite3OsTruncate(pPager->stfd, 0); */ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 5030 | sqlite3BitvecDestroy(pPager->pInStmt); |
| 5031 | pPager->pInStmt = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5032 | }else{ |
| 5033 | for(pPg=pPager->pStmt; pPg; pPg=pNext){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5034 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5035 | pNext = pHist->pNextStmt; |
| 5036 | assert( pHist->inStmt ); |
| 5037 | pHist->inStmt = 0; |
| 5038 | pHist->pPrevStmt = pHist->pNextStmt = 0; |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 5039 | sqlite3PageFree(pHist->pStmt); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5040 | pHist->pStmt = 0; |
| 5041 | } |
| 5042 | } |
| 5043 | pPager->stmtNRec = 0; |
| 5044 | pPager->stmtInUse = 0; |
| 5045 | pPager->pStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 5046 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5047 | pPager->stmtAutoopen = 0; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 5048 | pagerLeave(pPager); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 5049 | return SQLITE_OK; |
| 5050 | } |
| 5051 | |
| 5052 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5053 | ** Rollback a statement. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 5054 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5055 | int sqlite3PagerStmtRollback(Pager *pPager){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 5056 | int rc; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 5057 | pagerEnter(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5058 | if( pPager->stmtInUse ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5059 | PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5060 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5061 | PgHdr *pPg; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5062 | PgHistory *pHist; |
| 5063 | for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){ |
| 5064 | pHist = PGHDR_TO_HIST(pPg, pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5065 | if( pHist->pStmt ){ |
| 5066 | memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 5067 | sqlite3PageFree(pHist->pStmt); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5068 | pHist->pStmt = 0; |
| 5069 | } |
| 5070 | } |
| 5071 | pPager->dbSize = pPager->stmtSize; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 5072 | pager_truncate_cache(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5073 | rc = SQLITE_OK; |
| 5074 | }else{ |
| 5075 | rc = pager_stmt_playback(pPager); |
| 5076 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5077 | sqlite3PagerStmtCommit(pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 5078 | }else{ |
| 5079 | rc = SQLITE_OK; |
| 5080 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 5081 | pPager->stmtAutoopen = 0; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 5082 | pagerLeave(pPager); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 5083 | return rc; |
| 5084 | } |
| 5085 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5086 | /* |
| 5087 | ** Return the full pathname of the database file. |
| 5088 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5089 | const char *sqlite3PagerFilename(Pager *pPager){ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5090 | return pPager->zFilename; |
| 5091 | } |
| 5092 | |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 5093 | /* |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 5094 | ** Return the VFS structure for the pager. |
| 5095 | */ |
| 5096 | const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ |
| 5097 | return pPager->pVfs; |
| 5098 | } |
| 5099 | |
| 5100 | /* |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 5101 | ** Return the file handle for the database file associated |
| 5102 | ** with the pager. This might return NULL if the file has |
| 5103 | ** not yet been opened. |
| 5104 | */ |
| 5105 | sqlite3_file *sqlite3PagerFile(Pager *pPager){ |
| 5106 | return pPager->fd; |
| 5107 | } |
| 5108 | |
| 5109 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 5110 | ** Return the directory of the database file. |
| 5111 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5112 | const char *sqlite3PagerDirname(Pager *pPager){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 5113 | return pPager->zDirectory; |
| 5114 | } |
| 5115 | |
| 5116 | /* |
| 5117 | ** Return the full pathname of the journal file. |
| 5118 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5119 | const char *sqlite3PagerJournalname(Pager *pPager){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 5120 | return pPager->zJournal; |
| 5121 | } |
| 5122 | |
| 5123 | /* |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 5124 | ** Return true if fsync() calls are disabled for this pager. Return FALSE |
| 5125 | ** if fsync()s are executed normally. |
| 5126 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5127 | int sqlite3PagerNosync(Pager *pPager){ |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 5128 | return pPager->noSync; |
| 5129 | } |
| 5130 | |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 5131 | #ifdef SQLITE_HAS_CODEC |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 5132 | /* |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 5133 | ** Set the codec for this pager |
| 5134 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5135 | void sqlite3PagerSetCodec( |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 5136 | Pager *pPager, |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 5137 | void *(*xCodec)(void*,void*,Pgno,int), |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 5138 | void *pCodecArg |
| 5139 | ){ |
| 5140 | pPager->xCodec = xCodec; |
| 5141 | pPager->pCodecArg = pCodecArg; |
| 5142 | } |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 5143 | #endif |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 5144 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5145 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5146 | /* |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 5147 | ** Move the page pPg to location pgno in the file. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5148 | ** |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 5149 | ** There must be no references to the page previously located at |
| 5150 | ** pgno (which we call pPgOld) though that page is allowed to be |
| 5151 | ** in cache. If the page previous located at pgno is not already |
| 5152 | ** in the rollback journal, it is not put there by by this routine. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5153 | ** |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 5154 | ** References to the page pPg remain valid. Updating any |
| 5155 | ** meta-data associated with pPg (i.e. data stored in the nExtra bytes |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5156 | ** allocated along with the page) is the responsibility of the caller. |
| 5157 | ** |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 5158 | ** A transaction must be active when this routine is called. It used to be |
| 5159 | ** required that a statement transaction was not active, but this restriction |
| 5160 | ** has been removed (CREATE INDEX needs to move a page when a statement |
| 5161 | ** transaction is active). |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5162 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5163 | int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){ |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 5164 | PgHdr *pPgOld; /* The page being overwritten. */ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5165 | int h; |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5166 | Pgno needSyncPgno = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5167 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 5168 | pagerEnter(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5169 | assert( pPg->nRef>0 ); |
| 5170 | |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5171 | PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n", |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5172 | PAGERID(pPager), pPg->pgno, pPg->needSync, pgno); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5173 | IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno)) |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 5174 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 5175 | pager_get_content(pPg); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5176 | if( pPg->needSync ){ |
| 5177 | needSyncPgno = pPg->pgno; |
drh | 4f0aee4 | 2007-06-16 18:39:41 +0000 | [diff] [blame] | 5178 | assert( pPg->inJournal || (int)pgno>pPager->origDbSize ); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5179 | assert( pPg->dirty ); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5180 | assert( pPager->needSync ); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5181 | } |
| 5182 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5183 | /* Unlink pPg from its hash-chain */ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5184 | unlinkHashChain(pPager, pPg); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5185 | |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 5186 | /* If the cache contains a page with page-number pgno, remove it |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5187 | ** from its hash chain. Also, if the PgHdr.needSync was set for |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5188 | ** page pgno before the 'move' operation, it needs to be retained |
| 5189 | ** for the page moved there. |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5190 | */ |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 5191 | pPg->needSync = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5192 | pPgOld = pager_lookup(pPager, pgno); |
| 5193 | if( pPgOld ){ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5194 | assert( pPgOld->nRef==0 ); |
| 5195 | unlinkHashChain(pPager, pPgOld); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 5196 | makeClean(pPgOld); |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 5197 | pPg->needSync = pPgOld->needSync; |
| 5198 | }else{ |
| 5199 | pPg->needSync = 0; |
| 5200 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 5201 | pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5202 | |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5203 | /* Change the page number for pPg and insert it into the new hash-chain. */ |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 5204 | assert( pgno!=0 ); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5205 | pPg->pgno = pgno; |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 5206 | h = pgno & (pPager->nHash-1); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5207 | if( pPager->aHash[h] ){ |
| 5208 | assert( pPager->aHash[h]->pPrevHash==0 ); |
| 5209 | pPager->aHash[h]->pPrevHash = pPg; |
| 5210 | } |
| 5211 | pPg->pNextHash = pPager->aHash[h]; |
| 5212 | pPager->aHash[h] = pPg; |
| 5213 | pPg->pPrevHash = 0; |
| 5214 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 5215 | makeDirty(pPg); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5216 | pPager->dirtyCache = 1; |
drh | d138c01 | 2008-05-13 00:58:18 +0000 | [diff] [blame] | 5217 | pPager->dbModified = 1; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5218 | |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5219 | if( needSyncPgno ){ |
| 5220 | /* If needSyncPgno is non-zero, then the journal file needs to be |
| 5221 | ** sync()ed before any data is written to database file page needSyncPgno. |
| 5222 | ** Currently, no such page exists in the page-cache and the |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 5223 | ** Pager.pInJournal bit has been set. This needs to be remedied by loading |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5224 | ** the page into the pager-cache and setting the PgHdr.needSync flag. |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5225 | ** |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 5226 | ** If the attempt to load the page into the page-cache fails, (due |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 5227 | ** to a malloc() or IO failure), clear the bit in the pInJournal[] |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 5228 | ** array. Otherwise, if the page is loaded and written again in |
| 5229 | ** this transaction, it may be written to the database file before |
| 5230 | ** it is synced into the journal file. This way, it may end up in |
| 5231 | ** the journal file twice, but that is not a problem. |
| 5232 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5233 | ** The sqlite3PagerGet() call may cause the journal to sync. So make |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5234 | ** sure the Pager.needSync flag is set too. |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5235 | */ |
| 5236 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5237 | PgHdr *pPgHdr; |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5238 | assert( pPager->needSync ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5239 | rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 5240 | if( rc!=SQLITE_OK ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 5241 | if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){ |
| 5242 | sqlite3BitvecClear(pPager->pInJournal, needSyncPgno); |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 5243 | } |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 5244 | pagerLeave(pPager); |
| 5245 | return rc; |
| 5246 | } |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5247 | pPager->needSync = 1; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5248 | pPgHdr->needSync = 1; |
| 5249 | pPgHdr->inJournal = 1; |
| 5250 | makeDirty(pPgHdr); |
| 5251 | sqlite3PagerUnref(pPgHdr); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5252 | } |
| 5253 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 5254 | pagerLeave(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5255 | return SQLITE_OK; |
| 5256 | } |
| 5257 | #endif |
| 5258 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5259 | /* |
| 5260 | ** Return a pointer to the data for the specified page. |
| 5261 | */ |
| 5262 | void *sqlite3PagerGetData(DbPage *pPg){ |
| 5263 | return PGHDR_TO_DATA(pPg); |
| 5264 | } |
| 5265 | |
| 5266 | /* |
| 5267 | ** Return a pointer to the Pager.nExtra bytes of "extra" space |
| 5268 | ** allocated along with the specified page. |
| 5269 | */ |
| 5270 | void *sqlite3PagerGetExtra(DbPage *pPg){ |
| 5271 | Pager *pPager = pPg->pPager; |
| 5272 | return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0); |
| 5273 | } |
| 5274 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 5275 | /* |
| 5276 | ** Get/set the locking-mode for this pager. Parameter eMode must be one |
| 5277 | ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or |
| 5278 | ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then |
| 5279 | ** the locking-mode is set to the value specified. |
| 5280 | ** |
| 5281 | ** The returned value is either PAGER_LOCKINGMODE_NORMAL or |
| 5282 | ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated) |
| 5283 | ** locking-mode. |
| 5284 | */ |
| 5285 | int sqlite3PagerLockingMode(Pager *pPager, int eMode){ |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 5286 | assert( eMode==PAGER_LOCKINGMODE_QUERY |
| 5287 | || eMode==PAGER_LOCKINGMODE_NORMAL |
| 5288 | || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 5289 | assert( PAGER_LOCKINGMODE_QUERY<0 ); |
| 5290 | assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 ); |
| 5291 | if( eMode>=0 && !pPager->tempFile ){ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 5292 | pPager->exclusiveMode = eMode; |
| 5293 | } |
| 5294 | return (int)pPager->exclusiveMode; |
| 5295 | } |
| 5296 | |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 5297 | /* |
| 5298 | ** Get/set the journal-mode for this pager. Parameter eMode must be one |
| 5299 | ** of PAGER_JOURNALMODE_QUERY, PAGER_JOURNALMODE_DELETE or |
| 5300 | ** PAGER_JOURNALMODE_PERSIST. If the parameter is not _QUERY, then |
| 5301 | ** the journal-mode is set to the value specified. |
| 5302 | ** |
| 5303 | ** The returned value is either PAGER_JOURNALMODE_DELETE or |
| 5304 | ** PAGER_JOURNALMODE_PERSIST, indicating the current (possibly updated) |
| 5305 | ** journal-mode. |
| 5306 | */ |
| 5307 | int sqlite3PagerJournalMode(Pager *pPager, int eMode){ |
| 5308 | assert( eMode==PAGER_JOURNALMODE_QUERY |
| 5309 | || eMode==PAGER_JOURNALMODE_DELETE |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 5310 | || eMode==PAGER_JOURNALMODE_PERSIST |
| 5311 | || eMode==PAGER_JOURNALMODE_OFF ); |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 5312 | assert( PAGER_JOURNALMODE_QUERY<0 ); |
| 5313 | assert( PAGER_JOURNALMODE_DELETE>=0 && PAGER_JOURNALMODE_PERSIST>=0 ); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 5314 | if( eMode>=0 ){ |
| 5315 | pPager->journalMode = eMode; |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 5316 | } |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 5317 | return (int)pPager->journalMode; |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 5320 | /* |
| 5321 | ** Get/set the size-limit used for persistent journal files. |
| 5322 | */ |
| 5323 | i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){ |
| 5324 | if( iLimit>=-1 ){ |
| 5325 | pPager->journalSizeLimit = iLimit; |
| 5326 | } |
| 5327 | return pPager->journalSizeLimit; |
| 5328 | } |
| 5329 | |
drh | d919fe1 | 2007-12-11 19:34:44 +0000 | [diff] [blame] | 5330 | #ifdef SQLITE_TEST |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5331 | /* |
| 5332 | ** Print a listing of all referenced pages and their ref count. |
| 5333 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5334 | void sqlite3PagerRefdump(Pager *pPager){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5335 | PgHdr *pPg; |
| 5336 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 5337 | if( pPg->nRef<=0 ) continue; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5338 | sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n", |
| 5339 | pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef); |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5340 | } |
| 5341 | } |
| 5342 | #endif |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 5343 | |
| 5344 | #endif /* SQLITE_OMIT_DISKIO */ |