drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 12 | ** This is the implementation of the page cache subsystem or "pager". |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 13 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 14 | ** The pager is used to access a database disk file. It implements |
| 15 | ** atomic commit and rollback through the use of a journal file that |
| 16 | ** is separate from the database file. The pager also implements file |
| 17 | ** locking to prevent two processes from writing the same database |
| 18 | ** file simultaneously, or one process from reading the database while |
| 19 | ** another is writing. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 20 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 21 | ** @(#) $Id: pager.c,v 1.381 2007/08/30 08:08:17 danielk1977 Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 22 | */ |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 23 | #ifndef SQLITE_OMIT_DISKIO |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 24 | #include "sqliteInt.h" |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 25 | #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 | ** |
| 50 | ** PAGERID() takes a pointer to a Pager struct as it's argument. The |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 51 | ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 52 | ** struct as it's argument. |
| 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 |
| 148 | ** fsync() operation before it's memory can be reclaimed. If no such |
| 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 | ** |
| 216 | ** inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0 |
| 217 | ** |
| 218 | ** The pPager->aInJournal[] array is only valid for the original |
| 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 |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 274 | u32 notUsed; /* Buffer space */ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 275 | #ifdef SQLITE_CHECK_PAGES |
| 276 | u32 pageHash; |
| 277 | #endif |
drh | 1c7880e | 2005-05-20 20:01:55 +0000 | [diff] [blame] | 278 | /* pPager->pageSize bytes of page data follow this header */ |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 279 | /* Pager.nExtra bytes of local data follow the page data */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 280 | }; |
| 281 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 282 | /* |
| 283 | ** For an in-memory only database, some extra information is recorded about |
| 284 | ** each page so that changes can be rolled back. (Journal files are not |
| 285 | ** used for in-memory databases.) The following information is added to |
| 286 | ** the end of every EXTRA block for in-memory databases. |
| 287 | ** |
| 288 | ** This information could have been added directly to the PgHdr structure. |
| 289 | ** But then it would take up an extra 8 bytes of storage on every PgHdr |
| 290 | ** even for disk-based databases. Splitting it out saves 8 bytes. This |
| 291 | ** is only a savings of 0.8% but those percentages add up. |
| 292 | */ |
| 293 | typedef struct PgHistory PgHistory; |
| 294 | struct PgHistory { |
| 295 | u8 *pOrig; /* Original page text. Restore to this on a full rollback */ |
| 296 | u8 *pStmt; /* Text as it was at the beginning of the current statement */ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 297 | PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */ |
| 298 | u8 inStmt; /* TRUE if in the statement subjournal */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 299 | }; |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | ** A macro used for invoking the codec if there is one |
| 303 | */ |
| 304 | #ifdef SQLITE_HAS_CODEC |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 305 | # define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); } |
| 306 | # 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] | 307 | #else |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 308 | # define CODEC1(P,D,N,X) /* NO-OP */ |
| 309 | # define CODEC2(P,D,N,X) ((char*)D) |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 310 | #endif |
| 311 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 312 | /* |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 313 | ** Convert a pointer to a PgHdr into a pointer to its data |
| 314 | ** and back again. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 315 | */ |
| 316 | #define PGHDR_TO_DATA(P) ((void*)(&(P)[1])) |
| 317 | #define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1]) |
drh | 1c7880e | 2005-05-20 20:01:55 +0000 | [diff] [blame] | 318 | #define PGHDR_TO_EXTRA(G,P) ((void*)&((char*)(&(G)[1]))[(P)->pageSize]) |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 319 | #define PGHDR_TO_HIST(P,PGR) \ |
drh | 1c7880e | 2005-05-20 20:01:55 +0000 | [diff] [blame] | 320 | ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra]) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 321 | |
| 322 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 323 | ** A open page cache is an instance of the following structure. |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 324 | ** |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 325 | ** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 326 | ** or SQLITE_FULL. Once one of the first three errors occurs, it persists |
| 327 | ** and is returned as the result of every major pager API call. The |
| 328 | ** SQLITE_FULL return code is slightly different. It persists only until the |
| 329 | ** next successful rollback is performed on the pager cache. Also, |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 330 | ** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup() |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 331 | ** APIs, they may still be used successfully. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 332 | */ |
| 333 | struct Pager { |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 334 | sqlite3_vfs *pVfs; /* OS functions to use for IO */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 335 | u8 journalOpen; /* True if journal file descriptors is valid */ |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 336 | u8 journalStarted; /* True if header of journal is synced */ |
| 337 | u8 useJournal; /* Use a rollback journal on this file */ |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 338 | u8 noReadlock; /* Do not bother to obtain readlocks */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 339 | u8 stmtOpen; /* True if the statement subjournal is open */ |
| 340 | u8 stmtInUse; /* True we are in a statement subtransaction */ |
| 341 | u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 342 | u8 noSync; /* Do not sync the journal if true */ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 343 | u8 fullSync; /* Do extra syncs of the journal for robustness */ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 344 | u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 345 | u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 346 | u8 tempFile; /* zFilename is a temporary file */ |
| 347 | u8 readOnly; /* True for a read-only database */ |
| 348 | u8 needSync; /* True if an fsync() is needed on the journal */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 349 | u8 dirtyCache; /* True if cached pages have changed */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 350 | u8 alwaysRollback; /* Disable DontRollback() for all pages */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 351 | u8 memDb; /* True to inhibit all file I/O */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 352 | u8 setMaster; /* True if a m-j name has been written to jrnl */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 353 | u8 doNotSync; /* Boolean. While true, do not spill the cache */ |
| 354 | u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ |
| 355 | u8 changeCountDone; /* Set after incrementing the change-counter */ |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 356 | int errCode; /* One of several kinds of errors */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 357 | int dbSize; /* Number of pages in the file */ |
| 358 | int origDbSize; /* dbSize before the current change */ |
| 359 | int stmtSize; /* Size of database (in pages) at stmt_begin() */ |
| 360 | int nRec; /* Number of pages written to the journal */ |
| 361 | u32 cksumInit; /* Quasi-random value added to every checksum */ |
| 362 | int stmtNRec; /* Number of records in stmt subjournal */ |
| 363 | int nExtra; /* Add this many bytes to each in-memory page */ |
| 364 | int pageSize; /* Number of bytes in a page */ |
| 365 | int nPage; /* Total number of in-memory pages */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 366 | int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ |
| 367 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 368 | Pgno mxPgno; /* Maximum allowed size of the database */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 369 | u8 *aInJournal; /* One bit for each page in the database file */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 370 | u8 *aInStmt; /* One bit for each page in the database */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 371 | char *zFilename; /* Name of the database file */ |
| 372 | char *zJournal; /* Name of the journal file */ |
| 373 | char *zDirectory; /* Directory hold database and journal files */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 374 | sqlite3_file *fd, *jfd; /* File descriptors for database and journal */ |
| 375 | sqlite3_file *stfd; /* File descriptor for the statement subjournal*/ |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 376 | BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 377 | PagerLruList lru; /* LRU list of free pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 378 | PgHdr *pAll; /* List of all pages */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 379 | PgHdr *pStmt; /* List of pages in the statement subjournal */ |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 380 | PgHdr *pDirty; /* List of all dirty pages */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 381 | i64 journalOff; /* Current byte offset in the journal file */ |
| 382 | i64 journalHdr; /* Byte offset to previous journal header */ |
| 383 | i64 stmtHdrOff; /* First journal header written this statement */ |
| 384 | i64 stmtCksum; /* cksumInit when statement was started */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 385 | i64 stmtJSize; /* Size of journal at stmt_begin() */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 386 | int sectorSize; /* Assumed sector size during rollback */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 387 | #ifdef SQLITE_TEST |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 388 | int nHit, nMiss; /* Cache hits and missing */ |
| 389 | int nRead, nWrite; /* Database pages read/written */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 390 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 391 | void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */ |
| 392 | void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */ |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 393 | #ifdef SQLITE_HAS_CODEC |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 394 | void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 395 | void *pCodecArg; /* First argument to xCodec() */ |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 396 | #endif |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 397 | int nHash; /* Size of the pager hash table */ |
| 398 | PgHdr **aHash; /* Hash table to map page number to PgHdr */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 399 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 400 | Pager *pNext; /* Doubly linked list of pagers on which */ |
| 401 | Pager *pPrev; /* sqlite3_release_memory() will work */ |
| 402 | int iInUseMM; /* Non-zero if unavailable to MM */ |
| 403 | int iInUseDB; /* Non-zero if in sqlite3_release_memory() */ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 404 | #endif |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 405 | char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 406 | char dbFileVers[16]; /* Changes whenever database file changes */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | /* |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 410 | ** The following global variables hold counters used for |
| 411 | ** testing purposes only. These variables do not exist in |
| 412 | ** a non-testing build. These variables are not thread-safe. |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 413 | */ |
| 414 | #ifdef SQLITE_TEST |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 415 | int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */ |
| 416 | int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */ |
| 417 | int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */ |
| 418 | int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */ |
| 419 | # define PAGER_INCR(v) v++ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 420 | #else |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 421 | # define PAGER_INCR(v) |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 422 | #endif |
| 423 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 424 | /* |
| 425 | ** The following variable points to the head of a double-linked list |
| 426 | ** of all pagers that are eligible for page stealing by the |
| 427 | ** sqlite3_release_memory() interface. Access to this list is |
| 428 | ** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex. |
| 429 | */ |
| 430 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 431 | static Pager *sqlite3PagerList = 0; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 432 | static PagerLruList sqlite3LruPageList = {0, 0, 0}; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 433 | #endif |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 434 | |
| 435 | |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 436 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 437 | ** Journal files begin with the following magic string. The data |
| 438 | ** was obtained from /dev/random. It is used only as a sanity check. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 439 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 440 | ** Since version 2.8.0, the journal format contains additional sanity |
| 441 | ** checking information. If the power fails while the journal is begin |
| 442 | ** written, semi-random garbage data might appear in the journal |
| 443 | ** file after power is restored. If an attempt is then made |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 444 | ** to roll the journal back, the database could be corrupted. The additional |
| 445 | ** sanity checking data is an attempt to discover the garbage in the |
| 446 | ** journal and ignore it. |
| 447 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 448 | ** The sanity checking information for the new journal format consists |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 449 | ** 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] | 450 | ** the page number and the pPager->pageSize bytes of data for the page. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 451 | ** This cksum is initialized to a 32-bit random value that appears in the |
| 452 | ** journal file right after the header. The random initializer is important, |
| 453 | ** because garbage data that appears at the end of a journal is likely |
| 454 | ** data that was once in other files that have now been deleted. If the |
| 455 | ** garbage data came from an obsolete journal file, the checksums might |
| 456 | ** be correct. But by initializing the checksum to random value which |
| 457 | ** is different for every journal, we minimize that risk. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 458 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 459 | static const unsigned char aJournalMagic[] = { |
| 460 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7, |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | /* |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 464 | ** The size of the header and of each page in the journal is determined |
| 465 | ** by the following macros. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 466 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 467 | #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8) |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 468 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 469 | /* |
| 470 | ** The journal header size for this pager. In the future, this could be |
| 471 | ** set to some value read from the disk controller. The important |
| 472 | ** characteristic is that it is the same size as a disk sector. |
| 473 | */ |
| 474 | #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize) |
| 475 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 476 | /* |
| 477 | ** The macro MEMDB is true if we are dealing with an in-memory database. |
| 478 | ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set, |
| 479 | ** the value of MEMDB will be a constant and the compiler will optimize |
| 480 | ** out code that would never execute. |
| 481 | */ |
| 482 | #ifdef SQLITE_OMIT_MEMORYDB |
| 483 | # define MEMDB 0 |
| 484 | #else |
| 485 | # define MEMDB pPager->memDb |
| 486 | #endif |
| 487 | |
| 488 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 489 | ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is |
| 490 | ** reserved for working around a windows/posix incompatibility). It is |
| 491 | ** used in the journal to signify that the remainder of the journal file |
| 492 | ** is devoted to storing a master journal name - there are no more pages to |
| 493 | ** roll back. See comments for function writeMasterJournal() for details. |
| 494 | */ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 495 | /* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */ |
| 496 | #define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1) |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 497 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 498 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 499 | ** The maximum legal page number is (2^31 - 1). |
| 500 | */ |
| 501 | #define PAGER_MAX_PGNO 2147483647 |
| 502 | |
| 503 | /* |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 504 | ** The pagerEnter() and pagerLeave() routines acquire and release |
| 505 | ** a mutex on each pager. The mutex is recursive. |
| 506 | ** |
| 507 | ** This is a special-purpose mutex. It only provides mutual exclusion |
| 508 | ** between the Btree and the Memory Management sqlite3_release_memory() |
| 509 | ** function. It does not prevent, for example, two Btrees from accessing |
| 510 | ** the same pager at the same time. Other general-purpose mutexes in |
| 511 | ** the btree layer handle that chore. |
| 512 | */ |
| 513 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 514 | static void pagerEnter(Pager *p){ |
| 515 | p->iInUseDB++; |
| 516 | if( p->iInUseMM && p->iInUseDB==1 ){ |
| 517 | sqlite3_mutex *mutex; |
| 518 | mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
| 519 | p->iInUseDB = 0; |
| 520 | sqlite3_mutex_enter(mutex); |
| 521 | p->iInUseDB = 1; |
| 522 | sqlite3_mutex_leave(mutex); |
| 523 | } |
| 524 | assert( p->iInUseMM==0 ); |
| 525 | } |
| 526 | static void pagerLeave(Pager *p){ |
| 527 | p->iInUseDB--; |
| 528 | assert( p->iInUseDB>=0 ); |
| 529 | } |
| 530 | #else |
| 531 | # define pagerEnter(X) |
| 532 | # define pagerLeave(X) |
| 533 | #endif |
| 534 | |
| 535 | /* |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 536 | ** Enable reference count tracking (for debugging) here: |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 537 | */ |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 538 | #ifdef SQLITE_DEBUG |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 539 | int pager3_refinfo_enable = 0; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 540 | static void pager_refinfo(PgHdr *p){ |
| 541 | static int cnt = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 542 | if( !pager3_refinfo_enable ) return; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 543 | sqlite3DebugPrintf( |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 544 | "REFCNT: %4d addr=%p nRef=%-3d total=%d\n", |
| 545 | p->pgno, PGHDR_TO_DATA(p), p->nRef, p->pPager->nRef |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 546 | ); |
| 547 | cnt++; /* Something to set a breakpoint on */ |
| 548 | } |
| 549 | # define REFINFO(X) pager_refinfo(X) |
| 550 | #else |
| 551 | # define REFINFO(X) |
| 552 | #endif |
| 553 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 554 | /* |
| 555 | ** Add page pPg to the end of the linked list managed by structure |
| 556 | ** pList (pPg becomes the last entry in the list - the most recently |
| 557 | ** used). Argument pLink should point to either pPg->free or pPg->gfree, |
| 558 | ** depending on whether pPg is being added to the pager-specific or |
| 559 | ** global LRU list. |
| 560 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 561 | static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){ |
| 562 | pLink->pNext = 0; |
| 563 | pLink->pPrev = pList->pLast; |
| 564 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 565 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 566 | assert(pLink==&pPg->free || pLink==&pPg->gfree); |
| 567 | assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList); |
| 568 | #endif |
| 569 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 570 | if( pList->pLast ){ |
| 571 | int iOff = (char *)pLink - (char *)pPg; |
| 572 | PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]); |
| 573 | pLastLink->pNext = pPg; |
| 574 | }else{ |
| 575 | assert(!pList->pFirst); |
| 576 | pList->pFirst = pPg; |
| 577 | } |
| 578 | |
| 579 | pList->pLast = pPg; |
| 580 | if( !pList->pFirstSynced && pPg->needSync==0 ){ |
| 581 | pList->pFirstSynced = pPg; |
| 582 | } |
| 583 | } |
| 584 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 585 | /* |
| 586 | ** Remove pPg from the list managed by the structure pointed to by pList. |
| 587 | ** |
| 588 | ** Argument pLink should point to either pPg->free or pPg->gfree, depending |
| 589 | ** on whether pPg is being added to the pager-specific or global LRU list. |
| 590 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 591 | static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){ |
| 592 | int iOff = (char *)pLink - (char *)pPg; |
| 593 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 594 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 595 | assert(pLink==&pPg->free || pLink==&pPg->gfree); |
| 596 | assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList); |
| 597 | #endif |
| 598 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 599 | if( pPg==pList->pFirst ){ |
| 600 | pList->pFirst = pLink->pNext; |
| 601 | } |
| 602 | if( pPg==pList->pLast ){ |
| 603 | pList->pLast = pLink->pPrev; |
| 604 | } |
| 605 | if( pLink->pPrev ){ |
| 606 | PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]); |
| 607 | pPrevLink->pNext = pLink->pNext; |
| 608 | } |
| 609 | if( pLink->pNext ){ |
| 610 | PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]); |
| 611 | pNextLink->pPrev = pLink->pPrev; |
| 612 | } |
| 613 | if( pPg==pList->pFirstSynced ){ |
| 614 | PgHdr *p = pLink->pNext; |
| 615 | while( p && p->needSync ){ |
| 616 | PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]); |
| 617 | p = pL->pNext; |
| 618 | } |
| 619 | pList->pFirstSynced = p; |
| 620 | } |
| 621 | |
| 622 | pLink->pNext = pLink->pPrev = 0; |
| 623 | } |
| 624 | |
| 625 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 626 | ** Add page pPg to the list of free pages for the pager. If |
| 627 | ** memory-management is enabled, also add the page to the global |
| 628 | ** list of free pages. |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 629 | */ |
| 630 | static void lruListAdd(PgHdr *pPg){ |
| 631 | listAdd(&pPg->pPager->lru, &pPg->free, pPg); |
| 632 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 633 | if( !pPg->pPager->memDb ){ |
| 634 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 635 | listAdd(&sqlite3LruPageList, &pPg->gfree, pPg); |
| 636 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 637 | } |
| 638 | #endif |
| 639 | } |
| 640 | |
| 641 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 642 | ** Remove page pPg from the list of free pages for the associated pager. |
| 643 | ** If memory-management is enabled, also remove pPg from the global list |
| 644 | ** of free pages. |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 645 | */ |
| 646 | static void lruListRemove(PgHdr *pPg){ |
| 647 | listRemove(&pPg->pPager->lru, &pPg->free, pPg); |
| 648 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 649 | if( !pPg->pPager->memDb ){ |
| 650 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 651 | listRemove(&sqlite3LruPageList, &pPg->gfree, pPg); |
| 652 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 653 | } |
| 654 | #endif |
| 655 | } |
| 656 | |
| 657 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 658 | ** This function is called just after the needSync flag has been cleared |
| 659 | ** from all pages managed by pPager (usually because the journal file |
| 660 | ** has just been synced). It updates the pPager->lru.pFirstSynced variable |
| 661 | ** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced |
| 662 | ** variable also. |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 663 | */ |
| 664 | static void lruListSetFirstSynced(Pager *pPager){ |
| 665 | pPager->lru.pFirstSynced = pPager->lru.pFirst; |
| 666 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 667 | if( !pPager->memDb ){ |
| 668 | PgHdr *p; |
| 669 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 670 | for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext); |
| 671 | assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced); |
| 672 | sqlite3LruPageList.pFirstSynced = p; |
| 673 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 674 | } |
| 675 | #endif |
| 676 | } |
| 677 | |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 678 | /* |
| 679 | ** Return true if page *pPg has already been written to the statement |
| 680 | ** journal (or statement snapshot has been created, if *pPg is part |
| 681 | ** of an in-memory database). |
| 682 | */ |
| 683 | static int pageInStatement(PgHdr *pPg){ |
| 684 | Pager *pPager = pPg->pPager; |
| 685 | if( MEMDB ){ |
| 686 | return PGHDR_TO_HIST(pPg, pPager)->inStmt; |
| 687 | }else{ |
| 688 | Pgno pgno = pPg->pgno; |
| 689 | u8 *a = pPager->aInStmt; |
| 690 | return (a && (int)pgno<=pPager->stmtSize && (a[pgno/8] & (1<<(pgno&7)))); |
| 691 | } |
| 692 | } |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 693 | |
| 694 | /* |
| 695 | ** Change the size of the pager hash table to N. N must be a power |
| 696 | ** of two. |
| 697 | */ |
| 698 | static void pager_resize_hash_table(Pager *pPager, int N){ |
| 699 | PgHdr **aHash, *pPg; |
| 700 | assert( N>0 && (N&(N-1))==0 ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 701 | pagerLeave(pPager); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 702 | sqlite3MallocBenignFailure((int)pPager->aHash); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 703 | aHash = sqlite3MallocZero( sizeof(aHash[0])*N ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 704 | pagerEnter(pPager); |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 705 | if( aHash==0 ){ |
| 706 | /* Failure to rehash is not an error. It is only a performance hit. */ |
| 707 | return; |
| 708 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 709 | sqlite3_free(pPager->aHash); |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 710 | pPager->nHash = N; |
| 711 | pPager->aHash = aHash; |
| 712 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 713 | int h; |
| 714 | if( pPg->pgno==0 ){ |
| 715 | assert( pPg->pNextHash==0 && pPg->pPrevHash==0 ); |
| 716 | continue; |
| 717 | } |
| 718 | h = pPg->pgno & (N-1); |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 719 | pPg->pNextHash = aHash[h]; |
| 720 | if( aHash[h] ){ |
| 721 | aHash[h]->pPrevHash = pPg; |
| 722 | } |
| 723 | aHash[h] = pPg; |
| 724 | pPg->pPrevHash = 0; |
| 725 | } |
| 726 | } |
| 727 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 728 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 729 | ** Read a 32-bit integer from the given file descriptor. Store the integer |
| 730 | ** that is read in *pRes. Return SQLITE_OK if everything worked, or an |
| 731 | ** error code is something goes wrong. |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 732 | ** |
| 733 | ** All values are stored on disk as big-endian. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 734 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 735 | static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){ |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 736 | unsigned char ac[4]; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 737 | int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset); |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 738 | if( rc==SQLITE_OK ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 739 | *pRes = sqlite3Get4byte(ac); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 740 | } |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 741 | return rc; |
| 742 | } |
| 743 | |
| 744 | /* |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 745 | ** Write a 32-bit integer into a string buffer in big-endian byte order. |
| 746 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 747 | #define put32bits(A,B) sqlite3Put4byte((u8*)A,B) |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 748 | |
| 749 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 750 | ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK |
| 751 | ** on success or an error code is something goes wrong. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 752 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 753 | static int write32bits(sqlite3_file *fd, i64 offset, u32 val){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 754 | char ac[4]; |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 755 | put32bits(ac, val); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 756 | return sqlite3OsWrite(fd, ac, 4, offset); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 757 | } |
| 758 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 759 | /* |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 760 | ** If file pFd is open, call sqlite3OsUnlock() on it. |
| 761 | */ |
| 762 | static int osUnlock(sqlite3_file *pFd, int eLock){ |
| 763 | if( !pFd->pMethods ){ |
| 764 | return SQLITE_OK; |
| 765 | } |
| 766 | return sqlite3OsUnlock(pFd, eLock); |
| 767 | } |
| 768 | |
| 769 | /* |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 770 | ** This function determines whether or not the atomic-write optimization |
| 771 | ** can be used with this pager. The optimization can be used if: |
| 772 | ** |
| 773 | ** (a) the value returned by OsDeviceCharacteristics() indicates that |
| 774 | ** a database page may be written atomically, and |
| 775 | ** (b) the value returned by OsSectorSize() is less than or equal |
| 776 | ** to the page size. |
| 777 | ** |
| 778 | ** If the optimization cannot be used, 0 is returned. If it can be used, |
| 779 | ** then the value returned is the size of the journal file when it |
| 780 | ** contains rollback data for exactly one page. |
| 781 | */ |
| 782 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 783 | static int jrnlBufferSize(Pager *pPager){ |
| 784 | int dc; /* Device characteristics */ |
| 785 | int nSector; /* Sector size */ |
| 786 | int nPage; /* Page size */ |
| 787 | sqlite3_file *fd = pPager->fd; |
| 788 | |
| 789 | if( fd->pMethods ){ |
| 790 | dc = sqlite3OsDeviceCharacteristics(fd); |
| 791 | nSector = sqlite3OsSectorSize(fd); |
| 792 | nPage = pPager->pageSize; |
| 793 | } |
| 794 | |
| 795 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 796 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 797 | |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 798 | if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 799 | return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); |
| 800 | } |
| 801 | return 0; |
| 802 | } |
| 803 | #endif |
| 804 | |
| 805 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 806 | ** This function should be called when an error occurs within the pager |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 807 | ** code. The first argument is a pointer to the pager structure, the |
| 808 | ** second the error-code about to be returned by a pager API function. |
| 809 | ** The value returned is a copy of the second argument to this function. |
| 810 | ** |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 811 | ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 812 | ** the error becomes persistent. All subsequent API calls on this Pager |
| 813 | ** will immediately return the same error code. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 814 | */ |
| 815 | static int pager_error(Pager *pPager, int rc){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 816 | int rc2 = rc & 0xff; |
drh | 34f5621 | 2007-08-10 23:56:35 +0000 | [diff] [blame] | 817 | assert( |
| 818 | pPager->errCode==SQLITE_FULL || |
| 819 | pPager->errCode==SQLITE_OK || |
| 820 | (pPager->errCode & 0xff)==SQLITE_IOERR |
| 821 | ); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 822 | if( |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 823 | rc2==SQLITE_FULL || |
| 824 | rc2==SQLITE_IOERR || |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 825 | rc2==SQLITE_CORRUPT |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 826 | ){ |
| 827 | pPager->errCode = rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 828 | } |
| 829 | return rc; |
| 830 | } |
| 831 | |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 832 | /* |
| 833 | ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking |
| 834 | ** on the cache using a hash function. This is used for testing |
| 835 | ** and debugging only. |
| 836 | */ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 837 | #ifdef SQLITE_CHECK_PAGES |
| 838 | /* |
| 839 | ** Return a 32-bit hash of the page data for pPage. |
| 840 | */ |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 841 | static u32 pager_datahash(int nByte, unsigned char *pData){ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 842 | u32 hash = 0; |
| 843 | int i; |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 844 | for(i=0; i<nByte; i++){ |
| 845 | hash = (hash*1039) + pData[i]; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 846 | } |
| 847 | return hash; |
| 848 | } |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 849 | static u32 pager_pagehash(PgHdr *pPage){ |
| 850 | return pager_datahash(pPage->pPager->pageSize, |
| 851 | (unsigned char *)PGHDR_TO_DATA(pPage)); |
| 852 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 853 | |
| 854 | /* |
| 855 | ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES |
| 856 | ** is defined, and NDEBUG is not defined, an assert() statement checks |
| 857 | ** that the page is either dirty or still matches the calculated page-hash. |
| 858 | */ |
| 859 | #define CHECK_PAGE(x) checkPage(x) |
| 860 | static void checkPage(PgHdr *pPg){ |
| 861 | Pager *pPager = pPg->pPager; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 862 | assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty || |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 863 | pPg->pageHash==pager_pagehash(pPg) ); |
| 864 | } |
| 865 | |
| 866 | #else |
drh | 8ffa817 | 2007-06-18 17:25:17 +0000 | [diff] [blame] | 867 | #define pager_datahash(X,Y) 0 |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 868 | #define pager_pagehash(X) 0 |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 869 | #define CHECK_PAGE(x) |
| 870 | #endif |
| 871 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 872 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 873 | ** When this is called the journal file for pager pPager must be open. |
| 874 | ** 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^] | 875 | ** written into memory supplied by the caller. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 876 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 877 | ** zMaster must point to a buffer of at least nMaster bytes allocated by |
| 878 | ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is |
| 879 | ** enough space to write the master journal name). If the master journal |
| 880 | ** name in the journal is longer than nMaster bytes (including a |
| 881 | ** nul-terminator), then this is handled as if no master journal name |
| 882 | ** were present in the journal. |
| 883 | ** |
| 884 | ** 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] | 885 | ** SQLITE_OK returned. |
| 886 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 887 | static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 888 | int rc; |
| 889 | u32 len; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 890 | i64 szJ; |
danielk1977 | c3e8f5e | 2004-06-28 01:16:46 +0000 | [diff] [blame] | 891 | u32 cksum; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 892 | int i; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 893 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
| 894 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 895 | zMaster[0] = '\0'; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 896 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 897 | rc = sqlite3OsFileSize(pJrnl, &szJ); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 898 | if( rc!=SQLITE_OK || szJ<16 ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 899 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 900 | rc = read32bits(pJrnl, szJ-16, &len); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 901 | if( rc!=SQLITE_OK ) return rc; |
| 902 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 903 | if( len>=nMaster ){ |
| 904 | return SQLITE_OK; |
| 905 | } |
| 906 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 907 | rc = read32bits(pJrnl, szJ-12, &cksum); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 908 | if( rc!=SQLITE_OK ) return rc; |
| 909 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 910 | rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 911 | if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc; |
| 912 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 913 | rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 914 | if( rc!=SQLITE_OK ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 915 | return rc; |
| 916 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 917 | zMaster[len] = '\0'; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 918 | |
| 919 | /* See if the checksum matches the master journal name */ |
| 920 | for(i=0; i<len; i++){ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 921 | cksum -= zMaster[i]; |
| 922 | } |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 923 | if( cksum ){ |
| 924 | /* If the checksum doesn't add up, then one or more of the disk sectors |
| 925 | ** containing the master journal filename is corrupted. This means |
| 926 | ** definitely roll back, so just return SQLITE_OK and report a (nul) |
| 927 | ** master-journal filename. |
| 928 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 929 | zMaster[0] = '\0'; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 930 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 931 | |
| 932 | return SQLITE_OK; |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | ** Seek the journal file descriptor to the next sector boundary where a |
| 937 | ** journal header may be read or written. Pager.journalOff is updated with |
| 938 | ** the new seek offset. |
| 939 | ** |
| 940 | ** i.e for a sector size of 512: |
| 941 | ** |
| 942 | ** Input Offset Output Offset |
| 943 | ** --------------------------------------- |
| 944 | ** 0 0 |
| 945 | ** 512 512 |
| 946 | ** 100 512 |
| 947 | ** 2000 2048 |
| 948 | ** |
| 949 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 950 | static void seekJournalHdr(Pager *pPager){ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 951 | i64 offset = 0; |
| 952 | i64 c = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 953 | if( c ){ |
| 954 | offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager); |
| 955 | } |
| 956 | assert( offset%JOURNAL_HDR_SZ(pPager)==0 ); |
| 957 | assert( offset>=c ); |
| 958 | assert( (offset-c)<JOURNAL_HDR_SZ(pPager) ); |
| 959 | pPager->journalOff = offset; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* |
| 963 | ** The journal file must be open when this routine is called. A journal |
| 964 | ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the |
| 965 | ** current location. |
| 966 | ** |
| 967 | ** The format for the journal header is as follows: |
| 968 | ** - 8 bytes: Magic identifying journal format. |
| 969 | ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on. |
| 970 | ** - 4 bytes: Random number used for page hash. |
| 971 | ** - 4 bytes: Initial database page count. |
| 972 | ** - 4 bytes: Sector size used by the process that wrote this journal. |
| 973 | ** |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 974 | ** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 975 | */ |
| 976 | static int writeJournalHdr(Pager *pPager){ |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 977 | char zHeader[sizeof(aJournalMagic)+16]; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 978 | int rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 979 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 980 | if( pPager->stmtHdrOff==0 ){ |
| 981 | pPager->stmtHdrOff = pPager->journalOff; |
| 982 | } |
| 983 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 984 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 985 | pPager->journalHdr = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 986 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 987 | memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 988 | |
| 989 | /* |
| 990 | ** Write the nRec Field - the number of page records that follow this |
| 991 | ** journal header. Normally, zero is written to this value at this time. |
| 992 | ** After the records are added to the journal (and the journal synced, |
| 993 | ** if in full-sync mode), the zero is overwritten with the true number |
| 994 | ** of records (see syncJournal()). |
| 995 | ** |
| 996 | ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When |
| 997 | ** reading the journal this value tells SQLite to assume that the |
| 998 | ** rest of the journal file contains valid page records. This assumption |
| 999 | ** is dangerous, as if a failure occured whilst writing to the journal |
| 1000 | ** file it may contain some garbage data. There are two scenarios |
| 1001 | ** where this risk can be ignored: |
| 1002 | ** |
| 1003 | ** * When the pager is in no-sync mode. Corruption can follow a |
| 1004 | ** power failure in this case anyway. |
| 1005 | ** |
| 1006 | ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees |
| 1007 | ** that garbage data is never appended to the journal file. |
| 1008 | */ |
| 1009 | assert(pPager->fd->pMethods||pPager->noSync); |
| 1010 | if( (pPager->noSync) |
| 1011 | || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) |
| 1012 | ){ |
| 1013 | put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); |
| 1014 | }else{ |
| 1015 | put32bits(&zHeader[sizeof(aJournalMagic)], 0); |
| 1016 | } |
| 1017 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1018 | /* The random check-hash initialiser */ |
| 1019 | sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); |
| 1020 | put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit); |
| 1021 | /* The initial database size */ |
| 1022 | put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize); |
| 1023 | /* The assumed sector size for this process */ |
| 1024 | put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1025 | IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader))) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1026 | rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff); |
| 1027 | pPager->journalOff += JOURNAL_HDR_SZ(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1028 | |
| 1029 | /* The journal header has been written successfully. Seek the journal |
| 1030 | ** file descriptor to the end of the journal header sector. |
| 1031 | */ |
| 1032 | if( rc==SQLITE_OK ){ |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1033 | IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1)) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1034 | rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1035 | } |
| 1036 | return rc; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | ** The journal file must be open when this is called. A journal header file |
| 1041 | ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal |
| 1042 | ** file. See comments above function writeJournalHdr() for a description of |
| 1043 | ** the journal header format. |
| 1044 | ** |
| 1045 | ** If the header is read successfully, *nRec is set to the number of |
| 1046 | ** page records following this header and *dbSize is set to the size of the |
| 1047 | ** database before the transaction began, in pages. Also, pPager->cksumInit |
| 1048 | ** is set to the value read from the journal header. SQLITE_OK is returned |
| 1049 | ** in this case. |
| 1050 | ** |
| 1051 | ** If the journal header file appears to be corrupted, SQLITE_DONE is |
| 1052 | ** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes |
| 1053 | ** cannot be read from the journal file an error code is returned. |
| 1054 | */ |
| 1055 | static int readJournalHdr( |
| 1056 | Pager *pPager, |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1057 | i64 journalSize, |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1058 | u32 *pNRec, |
| 1059 | u32 *pDbSize |
| 1060 | ){ |
| 1061 | int rc; |
| 1062 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1063 | i64 jrnlOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1064 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1065 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1066 | if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){ |
| 1067 | return SQLITE_DONE; |
| 1068 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1069 | jrnlOff = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1070 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1071 | rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1072 | if( rc ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1073 | jrnlOff += sizeof(aMagic); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1074 | |
| 1075 | if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){ |
| 1076 | return SQLITE_DONE; |
| 1077 | } |
| 1078 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1079 | rc = read32bits(pPager->jfd, jrnlOff, pNRec); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1080 | if( rc ) return rc; |
| 1081 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1082 | rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1083 | if( rc ) return rc; |
| 1084 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1085 | rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1086 | if( rc ) return rc; |
| 1087 | |
| 1088 | /* Update the assumed sector-size to match the value used by |
| 1089 | ** the process that created this journal. If this journal was |
| 1090 | ** created by a process other than this one, then this routine |
| 1091 | ** is being called from within pager_playback(). The local value |
| 1092 | ** of Pager.sectorSize is restored at the end of that routine. |
| 1093 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1094 | rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1095 | if( rc ) return rc; |
| 1096 | |
| 1097 | pPager->journalOff += JOURNAL_HDR_SZ(pPager); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1098 | return SQLITE_OK; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | /* |
| 1103 | ** Write the supplied master journal name into the journal file for pager |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1104 | ** pPager at the current location. The master journal name must be the last |
| 1105 | ** thing written to a journal file. If the pager is in full-sync mode, the |
| 1106 | ** journal file descriptor is advanced to the next sector boundary before |
| 1107 | ** anything is written. The format is: |
| 1108 | ** |
| 1109 | ** + 4 bytes: PAGER_MJ_PGNO. |
| 1110 | ** + N bytes: length of master journal name. |
| 1111 | ** + 4 bytes: N |
| 1112 | ** + 4 bytes: Master journal name checksum. |
| 1113 | ** + 8 bytes: aJournalMagic[]. |
| 1114 | ** |
| 1115 | ** The master journal page checksum is the sum of the bytes in the master |
| 1116 | ** journal name. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1117 | ** |
| 1118 | ** If zMaster is a NULL pointer (occurs for a single database transaction), |
| 1119 | ** this call is a no-op. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1120 | */ |
| 1121 | static int writeMasterJournal(Pager *pPager, const char *zMaster){ |
| 1122 | int rc; |
| 1123 | int len; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1124 | int i; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1125 | i64 jrnlOff; |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1126 | u32 cksum = 0; |
| 1127 | char zBuf[sizeof(aJournalMagic)+2*4]; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1128 | |
| 1129 | if( !zMaster || pPager->setMaster) return SQLITE_OK; |
| 1130 | pPager->setMaster = 1; |
| 1131 | |
| 1132 | len = strlen(zMaster); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1133 | for(i=0; i<len; i++){ |
| 1134 | cksum += zMaster[i]; |
| 1135 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1136 | |
| 1137 | /* If in full-sync mode, advance to the next disk sector before writing |
| 1138 | ** the master journal name. This is in case the previous page written to |
| 1139 | ** the journal has already been synced. |
| 1140 | */ |
| 1141 | if( pPager->fullSync ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1142 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1143 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1144 | jrnlOff = pPager->journalOff; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1145 | pPager->journalOff += (len+20); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1146 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1147 | rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager)); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1148 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1149 | jrnlOff += 4; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1150 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1151 | rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1152 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1153 | jrnlOff += len; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1154 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1155 | put32bits(zBuf, len); |
| 1156 | put32bits(&zBuf[4], cksum); |
| 1157 | memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic)); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1158 | rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff); |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1159 | pPager->needSync = !pPager->noSync; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1160 | return rc; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1164 | ** 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] | 1165 | ** statement journal. |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1166 | ** |
| 1167 | ** The Pager keeps a separate list of pages that are currently in |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1168 | ** the statement journal. This helps the sqlite3PagerStmtCommit() |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1169 | ** routine run MUCH faster for the common case where there are many |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1170 | ** pages in memory but only a few are in the statement journal. |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1171 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1172 | static void page_add_to_stmt_list(PgHdr *pPg){ |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1173 | Pager *pPager = pPg->pPager; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 1174 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 1175 | assert( MEMDB ); |
| 1176 | if( !pHist->inStmt ){ |
| 1177 | assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 ); |
| 1178 | if( pPager->pStmt ){ |
| 1179 | PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg; |
| 1180 | } |
| 1181 | pHist->pNextStmt = pPager->pStmt; |
| 1182 | pPager->pStmt = pPg; |
| 1183 | pHist->inStmt = 1; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1184 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1188 | ** Find a page in the hash table given its page number. Return |
| 1189 | ** a pointer to the page or NULL if not found. |
| 1190 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1191 | static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 1192 | PgHdr *p; |
| 1193 | if( pPager->aHash==0 ) return 0; |
| 1194 | p = pPager->aHash[pgno & (pPager->nHash-1)]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1195 | while( p && p->pgno!=pgno ){ |
| 1196 | p = p->pNextHash; |
| 1197 | } |
| 1198 | return p; |
| 1199 | } |
| 1200 | |
| 1201 | /* |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 1202 | ** Unlock the database file. |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 1203 | */ |
| 1204 | static void pager_unlock(Pager *pPager){ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1205 | if( !pPager->exclusiveMode ){ |
| 1206 | if( !MEMDB ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1207 | osUnlock(pPager->fd, NO_LOCK); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1208 | pPager->dbSize = -1; |
| 1209 | IOTRACE(("UNLOCK %p\n", pPager)) |
| 1210 | } |
| 1211 | pPager->state = PAGER_UNLOCK; |
| 1212 | pPager->changeCountDone = 0; |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 1213 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | ** Execute a rollback if a transaction is active and unlock the |
| 1218 | ** database file. This is a no-op if the pager has already entered |
| 1219 | ** the error-state. |
| 1220 | */ |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1221 | static void pagerUnlockAndRollback(Pager *p){ |
| 1222 | if( p->errCode ) return; |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 1223 | assert( p->state>=PAGER_RESERVED || p->journalOpen==0 ); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1224 | if( p->state>=PAGER_RESERVED ){ |
| 1225 | sqlite3PagerRollback(p); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1226 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1227 | pager_unlock(p); |
| 1228 | assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) ); |
| 1229 | assert( p->errCode || !p->stmtOpen || p->exclusiveMode ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1234 | ** Clear the in-memory cache. This routine |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1235 | ** sets the state of the pager back to what it was when it was first |
| 1236 | ** opened. Any outstanding pages are invalidated and subsequent attempts |
| 1237 | ** to access those pages will likely result in a coredump. |
| 1238 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1239 | static void pager_reset(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1240 | PgHdr *pPg, *pNext; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 1241 | if( pPager->errCode ) return; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1242 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1243 | IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno)); |
| 1244 | PAGER_INCR(sqlite3_pager_pgfree_count); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1245 | pNext = pPg->pNextAll; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 1246 | lruListRemove(pPg); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1247 | sqlite3_free(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1248 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 1249 | assert(pPager->lru.pFirst==0); |
| 1250 | assert(pPager->lru.pFirstSynced==0); |
| 1251 | assert(pPager->lru.pLast==0); |
danielk1977 | b94bf85 | 2007-03-19 13:53:37 +0000 | [diff] [blame] | 1252 | pPager->pStmt = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1253 | pPager->pAll = 0; |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 1254 | pPager->nHash = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1255 | sqlite3_free(pPager->aHash); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1256 | pPager->nPage = 0; |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 1257 | pPager->aHash = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1258 | pPager->nRef = 0; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1262 | ** This routine ends a transaction. A transaction is ended by either |
| 1263 | ** a COMMIT or a ROLLBACK. |
| 1264 | ** |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1265 | ** When this routine is called, the pager has the journal file open and |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1266 | ** a RESERVED or EXCLUSIVE lock on the database. This routine will release |
| 1267 | ** the database lock and acquires a SHARED lock in its place if that is |
| 1268 | ** the appropriate thing to do. Release locks usually is appropriate, |
| 1269 | ** unless we are in exclusive access mode or unless this is a |
| 1270 | ** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation. |
| 1271 | ** |
| 1272 | ** The journal file is either deleted or truncated. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1273 | ** |
| 1274 | ** TODO: Consider keeping the journal file open for temporary databases. |
| 1275 | ** This might give a performance improvement on windows where opening |
| 1276 | ** a file is an expensive operation. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1277 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1278 | static int pager_end_transaction(Pager *pPager){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1279 | PgHdr *pPg; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1280 | int rc = SQLITE_OK; |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1281 | int rc2 = SQLITE_OK; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1282 | assert( !MEMDB ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1283 | if( pPager->state<PAGER_RESERVED ){ |
| 1284 | return SQLITE_OK; |
| 1285 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1286 | sqlite3PagerStmtCommit(pPager); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1287 | if( pPager->stmtOpen && !pPager->exclusiveMode ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1288 | sqlite3OsClose(pPager->stfd); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1289 | pPager->stmtOpen = 0; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1290 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1291 | if( pPager->journalOpen ){ |
drh | 01fa4c3 | 2007-04-02 11:22:22 +0000 | [diff] [blame] | 1292 | if( pPager->exclusiveMode |
| 1293 | && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1294 | pPager->journalOff = 0; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1295 | pPager->journalStarted = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1296 | }else{ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1297 | sqlite3OsClose(pPager->jfd); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1298 | pPager->journalOpen = 0; |
drh | 01fa4c3 | 2007-04-02 11:22:22 +0000 | [diff] [blame] | 1299 | if( rc==SQLITE_OK ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1300 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
danielk1977 | 7152de8 | 2007-03-29 17:28:14 +0000 | [diff] [blame] | 1301 | } |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1302 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1303 | sqlite3_free( pPager->aInJournal ); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1304 | pPager->aInJournal = 0; |
| 1305 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1306 | pPg->inJournal = 0; |
| 1307 | pPg->dirty = 0; |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 1308 | pPg->needSync = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1309 | pPg->alwaysRollback = 0; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1310 | #ifdef SQLITE_CHECK_PAGES |
| 1311 | pPg->pageHash = pager_pagehash(pPg); |
| 1312 | #endif |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1313 | } |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 1314 | pPager->pDirty = 0; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1315 | pPager->dirtyCache = 0; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1316 | pPager->nRec = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1317 | }else{ |
drh | 88b01a1 | 2005-03-28 18:04:27 +0000 | [diff] [blame] | 1318 | assert( pPager->aInJournal==0 ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1319 | assert( pPager->dirtyCache==0 || pPager->useJournal==0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1320 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1321 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1322 | if( !pPager->exclusiveMode ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1323 | rc2 = osUnlock(pPager->fd, SHARED_LOCK); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1324 | pPager->state = PAGER_SHARED; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1325 | }else if( pPager->state==PAGER_SYNCED ){ |
| 1326 | pPager->state = PAGER_EXCLUSIVE; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1327 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1328 | pPager->origDbSize = 0; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1329 | pPager->setMaster = 0; |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 1330 | pPager->needSync = 0; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 1331 | lruListSetFirstSynced(pPager); |
drh | 588f5bc | 2007-01-02 18:41:54 +0000 | [diff] [blame] | 1332 | pPager->dbSize = -1; |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1333 | |
| 1334 | return (rc==SQLITE_OK?rc2:rc); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1337 | /* |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1338 | ** Compute and return a checksum for the page of data. |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1339 | ** |
| 1340 | ** 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] | 1341 | ** random initial value and the page number. We experimented with |
| 1342 | ** a checksum of the entire data, but that was found to be too slow. |
| 1343 | ** |
| 1344 | ** Note that the page number is stored at the beginning of data and |
| 1345 | ** the checksum is stored at the end. This is important. If journal |
| 1346 | ** corruption occurs due to a power failure, the most likely scenario |
| 1347 | ** is that one end or the other of the record will be changed. It is |
| 1348 | ** much less likely that the two ends of the journal record will be |
| 1349 | ** correct and the middle be corrupt. Thus, this "checksum" scheme, |
| 1350 | ** though fast and simple, catches the mostly likely kind of corruption. |
| 1351 | ** |
| 1352 | ** FIX ME: Consider adding every 200th (or so) byte of the data to the |
| 1353 | ** checksum. That way if a single page spans 3 or more disk sectors and |
| 1354 | ** only the middle sector is corrupt, we will still have a reasonable |
| 1355 | ** chance of failing the checksum and thus detecting the problem. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1356 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1357 | static u32 pager_cksum(Pager *pPager, const u8 *aData){ |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1358 | u32 cksum = pPager->cksumInit; |
| 1359 | int i = pPager->pageSize-200; |
| 1360 | while( i>0 ){ |
| 1361 | cksum += aData[i]; |
| 1362 | i -= 200; |
| 1363 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1364 | return cksum; |
| 1365 | } |
| 1366 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 1367 | /* Forward declaration */ |
| 1368 | static void makeClean(PgHdr*); |
| 1369 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1370 | /* |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1371 | ** Read a single page from the journal file opened on file descriptor |
| 1372 | ** jfd. Playback this one page. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1373 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1374 | ** If useCksum==0 it means this journal does not use checksums. Checksums |
| 1375 | ** are not used in statement journals because statement journals do not |
| 1376 | ** need to survive power failures. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1377 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1378 | static int pager_playback_one_page( |
| 1379 | Pager *pPager, |
| 1380 | sqlite3_file *jfd, |
| 1381 | i64 offset, |
| 1382 | int useCksum |
| 1383 | ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1384 | int rc; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1385 | PgHdr *pPg; /* An existing page in the cache */ |
| 1386 | Pgno pgno; /* The page number of a page in journal */ |
| 1387 | u32 cksum; /* Checksum used for sanity checking */ |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 1388 | u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1389 | |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1390 | /* useCksum should be true for the main journal and false for |
| 1391 | ** statement journals. Verify that this is always the case |
| 1392 | */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1393 | assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) ); |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 1394 | assert( aData ); |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1395 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1396 | rc = read32bits(jfd, offset, &pgno); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1397 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1398 | rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1399 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1400 | pPager->journalOff += pPager->pageSize + 4; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1401 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1402 | /* Sanity checking on the page. This is more important that I originally |
| 1403 | ** thought. If a power failure occurs while the journal is being written, |
| 1404 | ** it could cause invalid data to be written into the journal. We need to |
| 1405 | ** detect this invalid data (with high probability) and ignore it. |
| 1406 | */ |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1407 | if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1408 | return SQLITE_DONE; |
| 1409 | } |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1410 | if( pgno>(unsigned)pPager->dbSize ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1411 | return SQLITE_OK; |
| 1412 | } |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1413 | if( useCksum ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1414 | rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1415 | if( rc ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1416 | pPager->journalOff += 4; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1417 | if( pager_cksum(pPager, aData)!=cksum ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1418 | return SQLITE_DONE; |
| 1419 | } |
| 1420 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1421 | |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 1422 | assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE ); |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1423 | |
| 1424 | /* If the pager is in RESERVED state, then there must be a copy of this |
| 1425 | ** page in the pager cache. In this case just update the pager cache, |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 1426 | ** not the database file. The page is left marked dirty in this case. |
| 1427 | ** |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1428 | ** An exception to the above rule: If the database is in no-sync mode |
| 1429 | ** and a page is moved during an incremental vacuum then the page may |
danielk1977 | 369f3a0 | 2007-05-24 09:41:20 +0000 | [diff] [blame] | 1430 | ** not be in the pager cache. Later: if a malloc() or IO error occurs |
| 1431 | ** during a Movepage() call, then the page may not be in the cache |
| 1432 | ** either. So the condition described in the above paragraph is not |
| 1433 | ** assert()able. |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1434 | ** |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1435 | ** If in EXCLUSIVE state, then we update the pager cache if it exists |
| 1436 | ** and the main file. The page is then marked not dirty. |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1437 | ** |
| 1438 | ** Ticket #1171: The statement journal might contain page content that is |
| 1439 | ** different from the page content at the start of the transaction. |
| 1440 | ** This occurs when a page is changed prior to the start of a statement |
| 1441 | ** then changed again within the statement. When rolling back such a |
| 1442 | ** statement we must not write to the original database unless we know |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 1443 | ** for certain that original page contents are synced into the main rollback |
| 1444 | ** journal. Otherwise, a power loss might leave modified data in the |
| 1445 | ** database file without an entry in the rollback journal that can |
| 1446 | ** restore the database to its original form. Two conditions must be |
| 1447 | ** met before writing to the database files. (1) the database must be |
| 1448 | ** locked. (2) we know that the original page content is fully synced |
| 1449 | ** in the main journal either because the page is not in cache or else |
| 1450 | ** the page is marked as needSync==0. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1451 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1452 | pPg = pager_lookup(pPager, pgno); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 1453 | PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n", |
| 1454 | PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData)); |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1455 | if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1456 | i64 offset = (pgno-1)*(i64)pPager->pageSize; |
| 1457 | rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 1458 | if( pPg ){ |
| 1459 | makeClean(pPg); |
| 1460 | } |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1461 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1462 | if( pPg ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1463 | /* No page should ever be explicitly rolled back that is in use, except |
| 1464 | ** for page 1 which is held in use in order to keep the lock on the |
| 1465 | ** database active. However such a page may be rolled back as a result |
| 1466 | ** of an internal error resulting in an automatic call to |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1467 | ** sqlite3PagerRollback(). |
drh | acf4ac9 | 2003-12-17 23:57:34 +0000 | [diff] [blame] | 1468 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1469 | void *pData; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1470 | /* assert( pPg->nRef==0 || pPg->pgno==1 ); */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1471 | pData = PGHDR_TO_DATA(pPg); |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1472 | memcpy(pData, aData, pPager->pageSize); |
danielk1977 | 9038bb6 | 2007-04-09 11:20:54 +0000 | [diff] [blame] | 1473 | if( pPager->xReiniter ){ |
| 1474 | pPager->xReiniter(pPg, pPager->pageSize); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1475 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1476 | #ifdef SQLITE_CHECK_PAGES |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1477 | pPg->pageHash = pager_pagehash(pPg); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1478 | #endif |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1479 | /* If this was page 1, then restore the value of Pager.dbFileVers. |
| 1480 | ** Do this before any decoding. */ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1481 | if( pgno==1 ){ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1482 | memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1483 | } |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1484 | |
| 1485 | /* Decode the page just read from disk */ |
| 1486 | CODEC1(pPager, pData, pPg->pgno, 3); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1487 | } |
| 1488 | return rc; |
| 1489 | } |
| 1490 | |
| 1491 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1492 | ** Parameter zMaster is the name of a master journal file. A single journal |
| 1493 | ** file that referred to the master journal file has just been rolled back. |
| 1494 | ** This routine checks if it is possible to delete the master journal file, |
| 1495 | ** and does so if it is. |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1496 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1497 | ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not |
| 1498 | ** available for use within this function. |
| 1499 | ** |
| 1500 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1501 | ** The master journal file contains the names of all child journals. |
| 1502 | ** To tell if a master journal can be deleted, check to each of the |
| 1503 | ** children. If all children are either missing or do not refer to |
| 1504 | ** a different master journal, then this master journal can be deleted. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1505 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1506 | static int pager_delmaster(Pager *pPager, const char *zMaster){ |
| 1507 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1508 | int rc; |
| 1509 | int master_open = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1510 | sqlite3_file *pMaster; |
| 1511 | sqlite3_file *pJournal; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1512 | char *zMasterJournal = 0; /* Contents of master journal file */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1513 | i64 nMasterJournal; /* Size of master journal file */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1514 | |
| 1515 | /* Open the master journal file exclusively in case some other process |
| 1516 | ** is running this routine also. Not that it makes too much difference. |
| 1517 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1518 | pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1519 | pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1520 | if( !pMaster ){ |
| 1521 | rc = SQLITE_NOMEM; |
| 1522 | }else{ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1523 | int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); |
| 1524 | rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1525 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1526 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1527 | master_open = 1; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1528 | |
| 1529 | rc = sqlite3OsFileSize(pMaster, &nMasterJournal); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1530 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1531 | |
| 1532 | if( nMasterJournal>0 ){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1533 | char *zJournal; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1534 | char *zMasterPtr = 0; |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1535 | int nMasterPtr = pPager->pVfs->mxPathname+1; |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1536 | |
| 1537 | /* Load the entire master journal file into space obtained from |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1538 | ** sqlite3_malloc() and pointed to by zMasterJournal. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1539 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1540 | zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1541 | if( !zMasterJournal ){ |
| 1542 | rc = SQLITE_NOMEM; |
| 1543 | goto delmaster_out; |
| 1544 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1545 | zMasterPtr = &zMasterJournal[nMasterJournal]; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1546 | rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1547 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1548 | |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1549 | zJournal = zMasterJournal; |
| 1550 | while( (zJournal-zMasterJournal)<nMasterJournal ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1551 | if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1552 | /* One of the journals pointed to by the master journal exists. |
| 1553 | ** Open it and check if it points at the master journal. If |
| 1554 | ** so, return without deleting the master journal file. |
| 1555 | */ |
drh | 3b7b78b | 2004-11-24 01:16:43 +0000 | [diff] [blame] | 1556 | int c; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1557 | int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL); |
| 1558 | rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1559 | if( rc!=SQLITE_OK ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1560 | goto delmaster_out; |
| 1561 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1562 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1563 | rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1564 | sqlite3OsClose(pJournal); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 1565 | if( rc!=SQLITE_OK ){ |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 1566 | goto delmaster_out; |
| 1567 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1568 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1569 | c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0; |
drh | 3b7b78b | 2004-11-24 01:16:43 +0000 | [diff] [blame] | 1570 | if( c ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1571 | /* We have a match. Do not delete the master journal file. */ |
| 1572 | goto delmaster_out; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1573 | } |
| 1574 | } |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1575 | zJournal += (strlen(zJournal)+1); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1576 | } |
| 1577 | } |
| 1578 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1579 | rc = sqlite3OsDelete(pVfs, zMaster, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1580 | |
| 1581 | delmaster_out: |
| 1582 | if( zMasterJournal ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1583 | sqlite3_free(zMasterJournal); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1584 | } |
| 1585 | if( master_open ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1586 | sqlite3OsClose(pMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1587 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1588 | sqlite3_free(pMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1589 | return rc; |
| 1590 | } |
| 1591 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1592 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1593 | static void pager_truncate_cache(Pager *pPager); |
| 1594 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1595 | /* |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1596 | ** Truncate the main file of the given pager to the number of pages |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1597 | ** indicated. Also truncate the cached representation of the file. |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1598 | */ |
| 1599 | static int pager_truncate(Pager *pPager, int nPage){ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1600 | int rc = SQLITE_OK; |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1601 | if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1602 | rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage); |
| 1603 | } |
| 1604 | if( rc==SQLITE_OK ){ |
| 1605 | pPager->dbSize = nPage; |
| 1606 | pager_truncate_cache(pPager); |
| 1607 | } |
| 1608 | return rc; |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | /* |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1612 | ** Set the sectorSize for the given pager. |
| 1613 | ** |
| 1614 | ** The sector size is the larger of the sector size reported |
| 1615 | ** by sqlite3OsSectorSize() and the pageSize. |
| 1616 | */ |
| 1617 | static void setSectorSize(Pager *pPager){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1618 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 1619 | if( !pPager->tempFile ){ |
| 1620 | /* Sector size doesn't matter for temporary files. Also, the file |
| 1621 | ** may not have been opened yet, in whcih case the OsSectorSize() |
| 1622 | ** call will segfault. |
| 1623 | */ |
| 1624 | pPager->sectorSize = sqlite3OsSectorSize(pPager->fd); |
| 1625 | } |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1626 | if( pPager->sectorSize<pPager->pageSize ){ |
| 1627 | pPager->sectorSize = pPager->pageSize; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1632 | ** Playback the journal and thus restore the database file to |
| 1633 | ** the state it was in before we started making changes. |
| 1634 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1635 | ** The journal file format is as follows: |
| 1636 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1637 | ** (1) 8 byte prefix. A copy of aJournalMagic[]. |
| 1638 | ** (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] | 1639 | ** in the journal. If this value is 0xffffffff, then compute the |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1640 | ** number of page records from the journal size. |
| 1641 | ** (3) 4 byte big-endian integer which is the initial value for the |
| 1642 | ** sanity checksum. |
| 1643 | ** (4) 4 byte integer which is the number of pages to truncate the |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1644 | ** database to during a rollback. |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1645 | ** (5) 4 byte integer which is the number of bytes in the master journal |
| 1646 | ** name. The value may be zero (indicate that there is no master |
| 1647 | ** journal.) |
| 1648 | ** (6) N bytes of the master journal name. The name will be nul-terminated |
| 1649 | ** and might be shorter than the value read from (5). If the first byte |
| 1650 | ** of the name is \000 then there is no master journal. The master |
| 1651 | ** journal name is stored in UTF-8. |
| 1652 | ** (7) Zero or more pages instances, each as follows: |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1653 | ** + 4 byte page number. |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1654 | ** + pPager->pageSize bytes of data. |
| 1655 | ** + 4 byte checksum |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1656 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1657 | ** When we speak of the journal header, we mean the first 6 items above. |
| 1658 | ** Each entry in the journal is an instance of the 7th item. |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1659 | ** |
| 1660 | ** Call the value from the second bullet "nRec". nRec is the number of |
| 1661 | ** valid page entries in the journal. In most cases, you can compute the |
| 1662 | ** value of nRec from the size of the journal file. But if a power |
| 1663 | ** failure occurred while the journal was being written, it could be the |
| 1664 | ** case that the size of the journal file had already been increased but |
| 1665 | ** the extra entries had not yet made it safely to disk. In such a case, |
| 1666 | ** the value of nRec computed from the file size would be too large. For |
| 1667 | ** that reason, we always use the nRec value in the header. |
| 1668 | ** |
| 1669 | ** If the nRec value is 0xffffffff it means that nRec should be computed |
| 1670 | ** from the file size. This value is used when the user selects the |
| 1671 | ** no-sync option for the journal. A power failure could lead to corruption |
| 1672 | ** in this case. But for things like temporary table (which will be |
| 1673 | ** deleted when the power is restored) we don't care. |
| 1674 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1675 | ** If the file opened as the journal file is not a well-formed |
danielk1977 | ece80f1 | 2004-06-23 01:05:26 +0000 | [diff] [blame] | 1676 | ** journal file then all pages up to the first corrupted page are rolled |
| 1677 | ** back (or no pages if the journal header is corrupted). The journal file |
| 1678 | ** is then deleted and SQLITE_OK returned, just as if no corruption had |
| 1679 | ** been encountered. |
| 1680 | ** |
| 1681 | ** If an I/O or malloc() error occurs, the journal-file is not deleted |
| 1682 | ** and an error code is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1683 | */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1684 | static int pager_playback(Pager *pPager, int isHot){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1685 | sqlite3_vfs *pVfs = pPager->pVfs; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1686 | i64 szJ; /* Size of the journal file in bytes */ |
danielk1977 | c3e8f5e | 2004-06-28 01:16:46 +0000 | [diff] [blame] | 1687 | u32 nRec; /* Number of Records in the journal */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1688 | int i; /* Loop counter */ |
| 1689 | Pgno mxPg = 0; /* Size of the original file in pages */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1690 | int rc; /* Result code of a subroutine */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1691 | char *zMaster = 0; /* Name of master journal file if any */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1692 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1693 | /* Figure out how many records are in the journal. Abort early if |
| 1694 | ** the journal is empty. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1695 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1696 | assert( pPager->journalOpen ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1697 | rc = sqlite3OsFileSize(pPager->jfd, &szJ); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1698 | if( rc!=SQLITE_OK || szJ==0 ){ |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1699 | goto end_playback; |
| 1700 | } |
drh | 240c579 | 2004-02-08 00:40:52 +0000 | [diff] [blame] | 1701 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1702 | /* Read the master journal name from the journal, if it is present. |
| 1703 | ** If a master journal file name is specified, but the file is not |
| 1704 | ** present on disk, then the journal is not hot and does not need to be |
| 1705 | ** played back. |
drh | 240c579 | 2004-02-08 00:40:52 +0000 | [diff] [blame] | 1706 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1707 | zMaster = pPager->pTmpSpace; |
| 1708 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1709 | assert( rc!=SQLITE_DONE ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1710 | if( rc!=SQLITE_OK |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1711 | || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS)) |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1712 | ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1713 | zMaster = 0; |
| 1714 | if( rc==SQLITE_DONE ) rc = SQLITE_OK; |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1715 | goto end_playback; |
| 1716 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1717 | pPager->journalOff = 0; |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1718 | zMaster = 0; |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1719 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1720 | /* This loop terminates either when the readJournalHdr() call returns |
| 1721 | ** SQLITE_DONE or an IO error occurs. */ |
| 1722 | while( 1 ){ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1723 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1724 | /* Read the next journal header from the journal file. If there are |
| 1725 | ** not enough bytes left in the journal file for a complete header, or |
| 1726 | ** it is corrupted, then a process must of failed while writing it. |
| 1727 | ** This indicates nothing more needs to be rolled back. |
| 1728 | */ |
| 1729 | rc = readJournalHdr(pPager, szJ, &nRec, &mxPg); |
| 1730 | if( rc!=SQLITE_OK ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1731 | if( rc==SQLITE_DONE ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1732 | rc = SQLITE_OK; |
| 1733 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1734 | goto end_playback; |
| 1735 | } |
| 1736 | |
| 1737 | /* If nRec is 0xffffffff, then this journal was created by a process |
| 1738 | ** working in no-sync mode. This means that the rest of the journal |
| 1739 | ** file consists of pages, there are no more journal headers. Compute |
| 1740 | ** the value of nRec based on this assumption. |
| 1741 | */ |
| 1742 | if( nRec==0xffffffff ){ |
| 1743 | assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ); |
| 1744 | nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager); |
| 1745 | } |
| 1746 | |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1747 | /* 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] | 1748 | ** process and if this is the final header in the journal, then it means |
| 1749 | ** that this part of the journal was being filled but has not yet been |
| 1750 | ** synced to disk. Compute the number of pages based on the remaining |
| 1751 | ** size of the file. |
| 1752 | ** |
| 1753 | ** The third term of the test was added to fix ticket #2565. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1754 | */ |
drh | 8940f4e | 2007-08-11 00:26:20 +0000 | [diff] [blame] | 1755 | if( nRec==0 && !isHot && |
| 1756 | pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1757 | nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager); |
| 1758 | } |
| 1759 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1760 | /* If this is the first header read from the journal, truncate the |
| 1761 | ** database file back to it's original size. |
| 1762 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1763 | if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){ |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1764 | rc = pager_truncate(pPager, mxPg); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1765 | if( rc!=SQLITE_OK ){ |
| 1766 | goto end_playback; |
| 1767 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1770 | /* Copy original pages out of the journal and back into the database file. |
| 1771 | */ |
| 1772 | for(i=0; i<nRec; i++){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1773 | rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1774 | if( rc!=SQLITE_OK ){ |
| 1775 | if( rc==SQLITE_DONE ){ |
| 1776 | rc = SQLITE_OK; |
| 1777 | pPager->journalOff = szJ; |
| 1778 | break; |
| 1779 | }else{ |
| 1780 | goto end_playback; |
| 1781 | } |
| 1782 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1783 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1784 | } |
drh | 580eeaf | 2006-02-24 03:09:37 +0000 | [diff] [blame] | 1785 | /*NOTREACHED*/ |
| 1786 | assert( 0 ); |
drh | 4a0681e | 2003-02-13 01:58:20 +0000 | [diff] [blame] | 1787 | |
| 1788 | end_playback: |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 1789 | if( rc==SQLITE_OK ){ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1790 | zMaster = pPager->pTmpSpace; |
| 1791 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
| 1792 | } |
| 1793 | if( rc==SQLITE_OK ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1794 | rc = pager_end_transaction(pPager); |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 1795 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1796 | if( rc==SQLITE_OK && zMaster[0] ){ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1797 | /* If there was a master journal and this routine will return success, |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1798 | ** see if it is possible to delete the master journal. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1799 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame^] | 1800 | rc = pager_delmaster(pPager, zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1801 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1802 | |
| 1803 | /* The Pager.sectorSize variable may have been updated while rolling |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 1804 | ** back a journal created by a process with a different sector size |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1805 | ** value. Reset it to the correct value for this process. |
| 1806 | */ |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1807 | setSectorSize(pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1808 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1812 | ** Playback the statement journal. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1813 | ** |
| 1814 | ** This is similar to playing back the transaction journal but with |
| 1815 | ** a few extra twists. |
| 1816 | ** |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1817 | ** (1) The number of pages in the database file at the start of |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1818 | ** the statement is stored in pPager->stmtSize, not in the |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1819 | ** journal file itself. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1820 | ** |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1821 | ** (2) In addition to playing back the statement journal, also |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1822 | ** playback all pages of the transaction journal beginning |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1823 | ** at offset pPager->stmtJSize. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1824 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1825 | static int pager_stmt_playback(Pager *pPager){ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1826 | i64 szJ; /* Size of the full journal */ |
| 1827 | i64 hdrOff; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1828 | int nRec; /* Number of Records */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1829 | int i; /* Loop counter */ |
| 1830 | int rc; |
| 1831 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1832 | szJ = pPager->journalOff; |
| 1833 | #ifndef NDEBUG |
| 1834 | { |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1835 | i64 os_szJ; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1836 | rc = sqlite3OsFileSize(pPager->jfd, &os_szJ); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1837 | if( rc!=SQLITE_OK ) return rc; |
| 1838 | assert( szJ==os_szJ ); |
| 1839 | } |
| 1840 | #endif |
| 1841 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 1842 | /* Set hdrOff to be the offset just after the end of the last journal |
| 1843 | ** page written before the first journal-header for this statement |
| 1844 | ** transaction was written, or the end of the file if no journal |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1845 | ** header was written. |
| 1846 | */ |
| 1847 | hdrOff = pPager->stmtHdrOff; |
| 1848 | assert( pPager->fullSync || !hdrOff ); |
| 1849 | if( !hdrOff ){ |
| 1850 | hdrOff = szJ; |
| 1851 | } |
| 1852 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1853 | /* Truncate the database back to its original size. |
| 1854 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1855 | rc = pager_truncate(pPager, pPager->stmtSize); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 1856 | assert( pPager->state>=PAGER_SHARED ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1857 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1858 | /* Figure out how many records are in the statement journal. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1859 | */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1860 | assert( pPager->stmtInUse && pPager->journalOpen ); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1861 | nRec = pPager->stmtNRec; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1862 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1863 | /* Copy original pages out of the statement journal and back into the |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1864 | ** database file. Note that the statement journal omits checksums from |
| 1865 | ** each record since power-failure recovery is not important to statement |
| 1866 | ** journals. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1867 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1868 | for(i=0; i<nRec; i++){ |
| 1869 | i64 offset = i*(4+pPager->pageSize); |
| 1870 | rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1871 | assert( rc!=SQLITE_DONE ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1872 | if( rc!=SQLITE_OK ) goto end_stmt_playback; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1875 | /* Now roll some pages back from the transaction journal. Pager.stmtJSize |
| 1876 | ** was the size of the journal file when this statement was started, so |
| 1877 | ** everything after that needs to be rolled back, either into the |
| 1878 | ** database, the memory cache, or both. |
| 1879 | ** |
| 1880 | ** If it is not zero, then Pager.stmtHdrOff is the offset to the start |
| 1881 | ** of the first journal header written during this statement transaction. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1882 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1883 | pPager->journalOff = pPager->stmtJSize; |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1884 | pPager->cksumInit = pPager->stmtCksum; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 1885 | while( pPager->journalOff < hdrOff ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1886 | rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1887 | assert( rc!=SQLITE_DONE ); |
| 1888 | if( rc!=SQLITE_OK ) goto end_stmt_playback; |
| 1889 | } |
| 1890 | |
| 1891 | while( pPager->journalOff < szJ ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1892 | u32 nJRec; /* Number of Journal Records */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1893 | u32 dummy; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1894 | rc = readJournalHdr(pPager, szJ, &nJRec, &dummy); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1895 | if( rc!=SQLITE_OK ){ |
| 1896 | assert( rc!=SQLITE_DONE ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1897 | goto end_stmt_playback; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1898 | } |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1899 | if( nJRec==0 ){ |
| 1900 | nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8); |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1901 | } |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1902 | for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1903 | rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1904 | assert( rc!=SQLITE_DONE ); |
| 1905 | if( rc!=SQLITE_OK ) goto end_stmt_playback; |
| 1906 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1907 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1908 | |
| 1909 | pPager->journalOff = szJ; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1910 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1911 | end_stmt_playback: |
danielk1977 | 8a7aea3 | 2006-01-23 15:25:48 +0000 | [diff] [blame] | 1912 | if( rc==SQLITE_OK) { |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1913 | pPager->journalOff = szJ; |
| 1914 | /* pager_reload_cache(pPager); */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1915 | } |
| 1916 | return rc; |
| 1917 | } |
| 1918 | |
| 1919 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1920 | ** Change the maximum number of in-memory pages that are allowed. |
| 1921 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1922 | void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){ |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1923 | if( mxPage>10 ){ |
| 1924 | pPager->mxPage = mxPage; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1925 | }else{ |
| 1926 | pPager->mxPage = 10; |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1931 | ** Adjust the robustness of the database to damage due to OS crashes |
| 1932 | ** or power failures by changing the number of syncs()s when writing |
| 1933 | ** the rollback journal. There are three levels: |
| 1934 | ** |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1935 | ** OFF sqlite3OsSync() is never called. This is the default |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1936 | ** for temporary and transient files. |
| 1937 | ** |
| 1938 | ** NORMAL The journal is synced once before writes begin on the |
| 1939 | ** database. This is normally adequate protection, but |
| 1940 | ** it is theoretically possible, though very unlikely, |
| 1941 | ** that an inopertune power failure could leave the journal |
| 1942 | ** in a state which would cause damage to the database |
| 1943 | ** when it is rolled back. |
| 1944 | ** |
| 1945 | ** FULL The journal is synced twice before writes begin on the |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1946 | ** database (with some additional information - the nRec field |
| 1947 | ** of the journal header - being written in between the two |
| 1948 | ** syncs). If we assume that writing a |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1949 | ** single disk sector is atomic, then this mode provides |
| 1950 | ** assurance that the journal will not be corrupted to the |
| 1951 | ** point of causing damage to the database during rollback. |
| 1952 | ** |
| 1953 | ** Numeric values associated with these states are OFF==1, NORMAL=2, |
| 1954 | ** and FULL=3. |
| 1955 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1956 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1957 | void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){ |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1958 | pPager->noSync = level==1 || pPager->tempFile; |
| 1959 | pPager->fullSync = level==3 && !pPager->tempFile; |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 1960 | pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1961 | if( pPager->noSync ) pPager->needSync = 0; |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1962 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1963 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1964 | |
| 1965 | /* |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1966 | ** The following global variable is incremented whenever the library |
| 1967 | ** attempts to open a temporary file. This information is used for |
| 1968 | ** testing and analysis only. |
| 1969 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1970 | #ifdef SQLITE_TEST |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1971 | int sqlite3_opentemp_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1972 | #endif |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 1973 | |
| 1974 | /* |
drh | 3f56e6e | 2007-03-15 01:16:47 +0000 | [diff] [blame] | 1975 | ** Open a temporary file. |
| 1976 | ** |
| 1977 | ** Write the file descriptor into *fd. Return SQLITE_OK on success or some |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1978 | ** other error code if we fail. The OS will automatically delete the temporary |
| 1979 | ** file when it is closed. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1980 | ** |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 1981 | ** If zFilename is 0, then an appropriate temporary filename is |
| 1982 | ** generated automatically and SQLITE_OPEN_SUBJOURNAL is passed to |
| 1983 | ** the OS layer as the file type. |
| 1984 | ** |
| 1985 | ** If zFilename is not 0, SQLITE_OPEN_TEMP_DB is passed as the file type. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1986 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1987 | static int sqlite3PagerOpentemp( |
| 1988 | sqlite3_vfs *pVfs, |
| 1989 | sqlite3_file *pFile, |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 1990 | char *zFilename |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1991 | ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1992 | int rc; |
danielk1977 | 863c0f9 | 2007-08-23 11:47:59 +0000 | [diff] [blame] | 1993 | int flags = ( |
| 1994 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| |
| 1995 | SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_DELETEONCLOSE |
| 1996 | ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1997 | |
| 1998 | char *zFree = 0; |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 1999 | if( zFilename==0 ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2000 | zFree = (char *)sqlite3_malloc(pVfs->mxPathname); |
| 2001 | if( !zFree ){ |
| 2002 | return SQLITE_NOMEM; |
| 2003 | } |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2004 | zFilename = zFree; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2005 | flags |= SQLITE_OPEN_SUBJOURNAL; |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2006 | rc = sqlite3OsGetTempName(pVfs, zFilename); |
| 2007 | if( rc!=SQLITE_OK ){ |
| 2008 | sqlite3_free(zFree); |
| 2009 | return rc; |
| 2010 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2011 | }else{ |
| 2012 | flags |= SQLITE_OPEN_TEMP_DB; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2013 | } |
drh | 3f56e6e | 2007-03-15 01:16:47 +0000 | [diff] [blame] | 2014 | |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2015 | #ifdef SQLITE_TEST |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 2016 | sqlite3_opentemp_count++; /* Used for testing and analysis only */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2017 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2018 | |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2019 | rc = sqlite3OsOpen(pVfs, zFilename, pFile, flags, 0); |
| 2020 | assert( rc!=SQLITE_OK || pFile->pMethods ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2021 | sqlite3_free(zFree); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2022 | return rc; |
| 2023 | } |
| 2024 | |
| 2025 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2026 | ** 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] | 2027 | ** 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] | 2028 | ** the first call to sqlite3PagerGet() and is only held open until the |
| 2029 | ** last page is released using sqlite3PagerUnref(). |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2030 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2031 | ** If zFilename is NULL then a randomly-named temporary file is created |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2032 | ** and used as the file to be cached. The file will be deleted |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2033 | ** automatically when it is closed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2034 | ** |
| 2035 | ** If zFilename is ":memory:" then all information is held in cache. |
| 2036 | ** It is never written to disk. This can be used to implement an |
| 2037 | ** in-memory database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2038 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2039 | int sqlite3PagerOpen( |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2040 | sqlite3_vfs *pVfs, /* The virtual file system to use */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2041 | Pager **ppPager, /* Return the Pager structure here */ |
| 2042 | const char *zFilename, /* Name of the database file to open */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 2043 | int nExtra, /* Extra bytes append to each in-memory page */ |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 2044 | int flags /* flags controlling this file */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2045 | ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2046 | u8 *pPtr; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2047 | Pager *pPager = 0; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2048 | int rc = SQLITE_OK; |
| 2049 | int i; |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 2050 | int tempFile = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2051 | int memDb = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2052 | int readOnly = 0; |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 2053 | int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; |
| 2054 | int noReadlock = (flags & PAGER_NO_READLOCK)!=0; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 2055 | int journalFileSize = sqlite3JournalSize(pVfs); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2056 | int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE; |
| 2057 | char *zPathname; |
| 2058 | int nPathname; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2059 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2060 | /* The default return is a NULL pointer */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2061 | *ppPager = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2062 | |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2063 | /* Compute the full pathname */ |
| 2064 | zPathname = sqlite3_malloc(pVfs->mxPathname+1); |
| 2065 | if( zPathname==0 ){ |
| 2066 | return SQLITE_NOMEM; |
| 2067 | } |
| 2068 | if( zFilename && zFilename[0] ){ |
| 2069 | #ifndef SQLITE_OMIT_MEMORYDB |
| 2070 | if( strcmp(zFilename,":memory:")==0 ){ |
| 2071 | memDb = 1; |
| 2072 | zPathname[0] = 0; |
| 2073 | }else |
| 2074 | #endif |
| 2075 | { |
| 2076 | rc = sqlite3OsFullPathname(pVfs, zFilename, zPathname); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2077 | } |
| 2078 | }else{ |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2079 | rc = sqlite3OsGetTempName(pVfs, zPathname); |
| 2080 | } |
| 2081 | if( rc!=SQLITE_OK ){ |
| 2082 | sqlite3_free(zPathname); |
| 2083 | return rc; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2084 | } |
| 2085 | nPathname = strlen(zPathname); |
| 2086 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2087 | /* Allocate memory for the pager structure */ |
| 2088 | pPager = sqlite3MallocZero( |
| 2089 | sizeof(*pPager) + /* Pager structure */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 2090 | journalFileSize + /* The journal file structure */ |
| 2091 | pVfs->szOsFile * 2 + /* The db and stmt journal files */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2092 | nPathname * 3 + 30 /* zFilename, zDirectory, zJournal */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2093 | ); |
| 2094 | if( !pPager ){ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2095 | sqlite3_free(zPathname); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2096 | return SQLITE_NOMEM; |
| 2097 | } |
| 2098 | pPtr = (u8 *)&pPager[1]; |
| 2099 | pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0]; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 2100 | pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1]; |
| 2101 | pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2]; |
| 2102 | pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize]; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2103 | pPager->zDirectory = &pPager->zFilename[nPathname+1]; |
| 2104 | pPager->zJournal = &pPager->zDirectory[nPathname+1]; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2105 | pPager->pVfs = pVfs; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2106 | memcpy(pPager->zFilename, zPathname, nPathname+1); |
| 2107 | sqlite3_free(zPathname); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2108 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2109 | /* Open the pager file. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2110 | */ |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2111 | if( zFilename && zFilename[0] && !memDb ){ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2112 | if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){ |
| 2113 | rc = SQLITE_CANTOPEN; |
| 2114 | }else{ |
| 2115 | /*** FIXME: Might need to be SQLITE_OPEN_TEMP_DB. Need to pass in |
| 2116 | **** a flag from higher up. |
| 2117 | ****/ |
| 2118 | int oflag = |
| 2119 | (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB); |
| 2120 | int fout = 0; |
| 2121 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, oflag, &fout); |
| 2122 | readOnly = (fout&SQLITE_OPEN_READONLY); |
| 2123 | |
| 2124 | /* If the file was successfully opened for read/write access, |
| 2125 | ** choose a default page size in case we have to create the |
| 2126 | ** database file. The default page size is the maximum of: |
| 2127 | ** |
| 2128 | ** + SQLITE_DEFAULT_PAGE_SIZE, |
| 2129 | ** + The value returned by sqlite3OsSectorSize() |
| 2130 | ** + The largest page size that can be written atomically. |
| 2131 | */ |
| 2132 | if( rc==SQLITE_OK && !readOnly ){ |
| 2133 | int iSectorSize = sqlite3OsSectorSize(pPager->fd); |
| 2134 | if( nDefaultPage<iSectorSize ){ |
| 2135 | nDefaultPage = iSectorSize; |
| 2136 | } |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2137 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2138 | { |
| 2139 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| 2140 | int ii; |
| 2141 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 2142 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 2143 | assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536); |
| 2144 | for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){ |
| 2145 | if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii; |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2146 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2147 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2148 | #endif |
| 2149 | if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ |
| 2150 | nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE; |
| 2151 | } |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 2152 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2153 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2154 | }else if( !memDb ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2155 | /* If a temporary file is requested, it is not opened immediately. |
| 2156 | ** In this case we accept the default page size and delay actually |
| 2157 | ** opening the file until the first call to OsWrite(). |
| 2158 | */ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2159 | tempFile = 1; |
| 2160 | pPager->state = PAGER_EXCLUSIVE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2161 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2162 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2163 | if( pPager && rc==SQLITE_OK ){ |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2164 | pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage); |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 2165 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2166 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2167 | /* If an error occured in either of the blocks above. |
| 2168 | ** Free the Pager structure and close the file. |
| 2169 | ** Since the pager is not allocated there is no need to set |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2170 | ** any Pager.errMask variables. |
| 2171 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2172 | if( !pPager || !pPager->pTmpSpace ){ |
| 2173 | sqlite3OsClose(pPager->fd); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2174 | sqlite3_free(pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2175 | return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2176 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2177 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2178 | PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename); |
| 2179 | IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename)) |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2180 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2181 | /* Fill in Pager.zDirectory[] */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2182 | memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2183 | for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){} |
drh | a76c82e | 2003-07-27 18:59:42 +0000 | [diff] [blame] | 2184 | if( i>0 ) pPager->zDirectory[i-1] = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2185 | |
| 2186 | /* Fill in Pager.zJournal[] */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2187 | memcpy(pPager->zJournal, pPager->zFilename, nPathname); |
| 2188 | memcpy(&pPager->zJournal[nPathname], "-journal", 9); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2189 | |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2190 | /* pPager->journalOpen = 0; */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2191 | pPager->useJournal = useJournal && !memDb; |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 2192 | pPager->noReadlock = noReadlock && readOnly; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2193 | /* pPager->stmtOpen = 0; */ |
| 2194 | /* pPager->stmtInUse = 0; */ |
| 2195 | /* pPager->nRef = 0; */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2196 | pPager->dbSize = memDb-1; |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2197 | pPager->pageSize = nDefaultPage; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2198 | /* pPager->stmtSize = 0; */ |
| 2199 | /* pPager->stmtJSize = 0; */ |
| 2200 | /* pPager->nPage = 0; */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2201 | pPager->mxPage = 100; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2202 | pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2203 | /* pPager->state = PAGER_UNLOCK; */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2204 | assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) ); |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2205 | /* pPager->errMask = 0; */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2206 | pPager->tempFile = tempFile; |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 2207 | assert( tempFile==PAGER_LOCKINGMODE_NORMAL |
| 2208 | || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 2209 | assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); |
| 2210 | pPager->exclusiveMode = tempFile; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2211 | pPager->memDb = memDb; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2212 | pPager->readOnly = readOnly; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2213 | /* pPager->needSync = 0; */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 2214 | pPager->noSync = pPager->tempFile || !useJournal; |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 2215 | pPager->fullSync = (pPager->noSync?0:1); |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2216 | pPager->sync_flags = SQLITE_SYNC_NORMAL; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2217 | /* pPager->pFirst = 0; */ |
| 2218 | /* pPager->pFirstSynced = 0; */ |
| 2219 | /* pPager->pLast = 0; */ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 2220 | pPager->nExtra = FORCE_ALIGNMENT(nExtra); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2221 | assert(pPager->fd->pMethods||memDb||tempFile); |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 2222 | if( !memDb ){ |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 2223 | setSectorSize(pPager); |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 2224 | } |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2225 | /* pPager->pBusyHandler = 0; */ |
| 2226 | /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2227 | *ppPager = pPager; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2228 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2229 | pPager->iInUseMM = 0; |
| 2230 | pPager->iInUseDB = 0; |
| 2231 | if( !memDb ){ |
| 2232 | sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
| 2233 | sqlite3_mutex_enter(mutex); |
| 2234 | pPager->pNext = sqlite3PagerList; |
| 2235 | if( sqlite3PagerList ){ |
| 2236 | assert( sqlite3PagerList->pPrev==0 ); |
| 2237 | sqlite3PagerList->pPrev = pPager; |
| 2238 | } |
| 2239 | pPager->pPrev = 0; |
| 2240 | sqlite3PagerList = pPager; |
| 2241 | sqlite3_mutex_leave(mutex); |
| 2242 | } |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 2243 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2244 | return SQLITE_OK; |
| 2245 | } |
| 2246 | |
| 2247 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2248 | ** Set the busy handler function. |
| 2249 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2250 | void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2251 | pPager->pBusyHandler = pBusyHandler; |
| 2252 | } |
| 2253 | |
| 2254 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2255 | ** Set the destructor for this pager. If not NULL, the destructor is called |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2256 | ** when the reference count on each page reaches zero. The destructor can |
| 2257 | ** 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] | 2258 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2259 | ** The destructor is not called as a result sqlite3PagerClose(). |
| 2260 | ** Destructors are only called by sqlite3PagerUnref(). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2261 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2262 | void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2263 | pPager->xDestructor = xDesc; |
| 2264 | } |
| 2265 | |
| 2266 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2267 | ** Set the reinitializer for this pager. If not NULL, the reinitializer |
| 2268 | ** is called when the content of a page in cache is restored to its original |
| 2269 | ** value as a result of a rollback. The callback gives higher-level code |
| 2270 | ** an opportunity to restore the EXTRA section to agree with the restored |
| 2271 | ** page data. |
| 2272 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2273 | void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2274 | pPager->xReiniter = xReinit; |
| 2275 | } |
| 2276 | |
| 2277 | /* |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2278 | ** Set the page size to *pPageSize. If the suggest new page size is |
| 2279 | ** inappropriate, then an alternative page size is set to that |
| 2280 | ** value before returning. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2281 | */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2282 | int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){ |
| 2283 | int rc = SQLITE_OK; |
| 2284 | u16 pageSize = *pPageSize; |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2285 | assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2286 | if( pageSize && pageSize!=pPager->pageSize |
| 2287 | && !pPager->memDb && pPager->nRef==0 |
| 2288 | ){ |
| 2289 | char *pNew = (char *)sqlite3_malloc(pageSize); |
| 2290 | if( !pNew ){ |
| 2291 | rc = SQLITE_NOMEM; |
| 2292 | }else{ |
| 2293 | pagerEnter(pPager); |
| 2294 | pager_reset(pPager); |
| 2295 | pPager->pageSize = pageSize; |
| 2296 | setSectorSize(pPager); |
| 2297 | sqlite3_free(pPager->pTmpSpace); |
| 2298 | pPager->pTmpSpace = pNew; |
| 2299 | pagerLeave(pPager); |
| 2300 | } |
drh | 1c7880e | 2005-05-20 20:01:55 +0000 | [diff] [blame] | 2301 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2302 | *pPageSize = pPager->pageSize; |
| 2303 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | /* |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2307 | ** Attempt to set the maximum database page count if mxPage is positive. |
| 2308 | ** Make no changes if mxPage is zero or negative. And never reduce the |
| 2309 | ** maximum page count below the current size of the database. |
| 2310 | ** |
| 2311 | ** Regardless of mxPage, return the current maximum page count. |
| 2312 | */ |
| 2313 | int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ |
| 2314 | if( mxPage>0 ){ |
| 2315 | pPager->mxPgno = mxPage; |
| 2316 | } |
| 2317 | sqlite3PagerPagecount(pPager); |
| 2318 | return pPager->mxPgno; |
| 2319 | } |
| 2320 | |
| 2321 | /* |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2322 | ** The following set of routines are used to disable the simulated |
| 2323 | ** I/O error mechanism. These routines are used to avoid simulated |
| 2324 | ** errors in places where we do not care about errors. |
| 2325 | ** |
| 2326 | ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops |
| 2327 | ** and generate no code. |
| 2328 | */ |
| 2329 | #ifdef SQLITE_TEST |
| 2330 | extern int sqlite3_io_error_pending; |
| 2331 | extern int sqlite3_io_error_hit; |
| 2332 | static int saved_cnt; |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2333 | void disable_simulated_io_errors(void){ |
| 2334 | saved_cnt = sqlite3_io_error_pending; |
| 2335 | sqlite3_io_error_pending = -1; |
| 2336 | } |
| 2337 | void enable_simulated_io_errors(void){ |
| 2338 | sqlite3_io_error_pending = saved_cnt; |
| 2339 | } |
| 2340 | #else |
drh | 152410f | 2005-11-05 15:11:22 +0000 | [diff] [blame] | 2341 | # define disable_simulated_io_errors() |
| 2342 | # define enable_simulated_io_errors() |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2343 | #endif |
| 2344 | |
| 2345 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2346 | ** Read the first N bytes from the beginning of the file into memory |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2347 | ** that pDest points to. |
| 2348 | ** |
| 2349 | ** No error checking is done. The rational for this is that this function |
| 2350 | ** may be called even if the file does not exist or contain a header. In |
| 2351 | ** these cases sqlite3OsRead() will return an error, to which the correct |
| 2352 | ** response is to zero the memory at pDest and continue. A real IO error |
| 2353 | ** will presumably recur and be picked up later (Todo: Think about this). |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2354 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2355 | int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){ |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2356 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2357 | memset(pDest, 0, N); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2358 | assert(MEMDB||pPager->fd->pMethods||pPager->tempFile); |
| 2359 | if( pPager->fd->pMethods ){ |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2360 | IOTRACE(("DBHDR %p 0 %d\n", pPager, N)) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2361 | rc = sqlite3OsRead(pPager->fd, pDest, N, 0); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2362 | if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 2363 | rc = SQLITE_OK; |
| 2364 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2365 | } |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2366 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2370 | ** Return the total number of pages in the disk file associated with |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2371 | ** pPager. |
| 2372 | ** |
| 2373 | ** If the PENDING_BYTE lies on the page directly after the end of the |
| 2374 | ** file, then consider this page part of the file too. For example, if |
| 2375 | ** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the |
| 2376 | ** file is 4096 bytes, 5 is returned instead of 4. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2377 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2378 | int sqlite3PagerPagecount(Pager *pPager){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2379 | i64 n = 0; |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 2380 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2381 | assert( pPager!=0 ); |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 2382 | if( pPager->errCode ){ |
| 2383 | return 0; |
| 2384 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2385 | if( pPager->dbSize>=0 ){ |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2386 | n = pPager->dbSize; |
| 2387 | } else { |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2388 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 2389 | if( (pPager->fd->pMethods) |
| 2390 | && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){ |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 2391 | pager_error(pPager, rc); |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2392 | return 0; |
| 2393 | } |
| 2394 | if( n>0 && n<pPager->pageSize ){ |
| 2395 | n = 1; |
| 2396 | }else{ |
| 2397 | n /= pPager->pageSize; |
| 2398 | } |
| 2399 | if( pPager->state!=PAGER_UNLOCK ){ |
| 2400 | pPager->dbSize = n; |
| 2401 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2402 | } |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2403 | if( n==(PENDING_BYTE/pPager->pageSize) ){ |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 2404 | n++; |
| 2405 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2406 | if( n>pPager->mxPgno ){ |
| 2407 | pPager->mxPgno = n; |
| 2408 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2409 | return n; |
| 2410 | } |
| 2411 | |
drh | f07e7d5 | 2006-04-07 13:54:46 +0000 | [diff] [blame] | 2412 | |
| 2413 | #ifndef SQLITE_OMIT_MEMORYDB |
| 2414 | /* |
| 2415 | ** Clear a PgHistory block |
| 2416 | */ |
| 2417 | static void clearHistory(PgHistory *pHist){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2418 | sqlite3_free(pHist->pOrig); |
| 2419 | sqlite3_free(pHist->pStmt); |
drh | f07e7d5 | 2006-04-07 13:54:46 +0000 | [diff] [blame] | 2420 | pHist->pOrig = 0; |
| 2421 | pHist->pStmt = 0; |
| 2422 | } |
| 2423 | #else |
| 2424 | #define clearHistory(x) |
| 2425 | #endif |
| 2426 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2427 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2428 | ** Forward declaration |
| 2429 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2430 | static int syncJournal(Pager*); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2431 | |
| 2432 | /* |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2433 | ** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate |
| 2434 | ** 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] | 2435 | ** sqlite3PagerMovepage() routine can leave a page in the |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2436 | ** pNextFree/pPrevFree list that is not a part of any hash-chain. |
| 2437 | */ |
| 2438 | static void unlinkHashChain(Pager *pPager, PgHdr *pPg){ |
| 2439 | if( pPg->pgno==0 ){ |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 2440 | assert( pPg->pNextHash==0 && pPg->pPrevHash==0 ); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2441 | return; |
| 2442 | } |
| 2443 | if( pPg->pNextHash ){ |
| 2444 | pPg->pNextHash->pPrevHash = pPg->pPrevHash; |
| 2445 | } |
| 2446 | if( pPg->pPrevHash ){ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 2447 | assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg ); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2448 | pPg->pPrevHash->pNextHash = pPg->pNextHash; |
| 2449 | }else{ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 2450 | int h = pPg->pgno & (pPager->nHash-1); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2451 | pPager->aHash[h] = pPg->pNextHash; |
| 2452 | } |
drh | 5229ae4 | 2006-03-23 23:29:04 +0000 | [diff] [blame] | 2453 | if( MEMDB ){ |
drh | 5229ae4 | 2006-03-23 23:29:04 +0000 | [diff] [blame] | 2454 | clearHistory(PGHDR_TO_HIST(pPg, pPager)); |
| 2455 | } |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2456 | pPg->pgno = 0; |
| 2457 | pPg->pNextHash = pPg->pPrevHash = 0; |
| 2458 | } |
| 2459 | |
| 2460 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2461 | ** Unlink a page from the free list (the list of all pages where nRef==0) |
| 2462 | ** and from its hash collision chain. |
| 2463 | */ |
| 2464 | static void unlinkPage(PgHdr *pPg){ |
| 2465 | Pager *pPager = pPg->pPager; |
| 2466 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2467 | /* Unlink from free page list */ |
| 2468 | lruListRemove(pPg); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2469 | |
| 2470 | /* Unlink from the pgno hash table */ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2471 | unlinkHashChain(pPager, pPg); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2475 | ** This routine is used to truncate the cache when a database |
| 2476 | ** is truncated. Drop from the cache all pages whose pgno is |
| 2477 | ** larger than pPager->dbSize and is unreferenced. |
| 2478 | ** |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2479 | ** Referenced pages larger than pPager->dbSize are zeroed. |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2480 | ** |
| 2481 | ** Actually, at the point this routine is called, it would be |
| 2482 | ** an error to have a referenced page. But rather than delete |
| 2483 | ** that page and guarantee a subsequent segfault, it seems better |
| 2484 | ** to zero it and hope that we error out sanely. |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2485 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2486 | static void pager_truncate_cache(Pager *pPager){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2487 | PgHdr *pPg; |
| 2488 | PgHdr **ppPg; |
| 2489 | int dbSize = pPager->dbSize; |
| 2490 | |
| 2491 | ppPg = &pPager->pAll; |
| 2492 | while( (pPg = *ppPg)!=0 ){ |
| 2493 | if( pPg->pgno<=dbSize ){ |
| 2494 | ppPg = &pPg->pNextAll; |
| 2495 | }else if( pPg->nRef>0 ){ |
| 2496 | memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize); |
| 2497 | ppPg = &pPg->pNextAll; |
| 2498 | }else{ |
| 2499 | *ppPg = pPg->pNextAll; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2500 | IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno)); |
| 2501 | PAGER_INCR(sqlite3_pager_pgfree_count); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2502 | unlinkPage(pPg); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 2503 | makeClean(pPg); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2504 | sqlite3_free(pPg); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2505 | pPager->nPage--; |
| 2506 | } |
| 2507 | } |
| 2508 | } |
| 2509 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2510 | /* |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2511 | ** 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] | 2512 | ** is currently not available. Repeat until the busy callback returns |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2513 | ** false or until the lock succeeds. |
| 2514 | ** |
| 2515 | ** Return SQLITE_OK on success and an error code if we cannot obtain |
| 2516 | ** the lock. |
| 2517 | */ |
| 2518 | static int pager_wait_on_lock(Pager *pPager, int locktype){ |
| 2519 | int rc; |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2520 | |
| 2521 | /* The OS lock values must be the same as the Pager lock values */ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2522 | assert( PAGER_SHARED==SHARED_LOCK ); |
| 2523 | assert( PAGER_RESERVED==RESERVED_LOCK ); |
| 2524 | assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2525 | |
| 2526 | /* If the file is currently unlocked then the size must be unknown */ |
| 2527 | assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB ); |
| 2528 | |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2529 | if( pPager->state>=locktype ){ |
| 2530 | rc = SQLITE_OK; |
| 2531 | }else{ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2532 | do { |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2533 | rc = sqlite3OsLock(pPager->fd, locktype); |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 2534 | }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) ); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2535 | if( rc==SQLITE_OK ){ |
| 2536 | pPager->state = locktype; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2537 | IOTRACE(("LOCK %p %d\n", pPager, locktype)) |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2538 | } |
| 2539 | } |
| 2540 | return rc; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2544 | ** Truncate the file to the number of pages specified. |
| 2545 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2546 | int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2547 | int rc; |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2548 | assert( pPager->state>=PAGER_SHARED || MEMDB ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2549 | sqlite3PagerPagecount(pPager); |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 2550 | if( pPager->errCode ){ |
| 2551 | rc = pPager->errCode; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 2552 | return rc; |
| 2553 | } |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 2554 | if( nPage>=(unsigned)pPager->dbSize ){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2555 | return SQLITE_OK; |
| 2556 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2557 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2558 | pPager->dbSize = nPage; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2559 | pager_truncate_cache(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2560 | return SQLITE_OK; |
| 2561 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2562 | pagerEnter(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2563 | rc = syncJournal(pPager); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2564 | pagerLeave(pPager); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 2565 | if( rc!=SQLITE_OK ){ |
| 2566 | return rc; |
| 2567 | } |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2568 | |
| 2569 | /* Get an exclusive lock on the database before truncating. */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2570 | pagerEnter(pPager); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2571 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2572 | pagerLeave(pPager); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2573 | if( rc!=SQLITE_OK ){ |
| 2574 | return rc; |
| 2575 | } |
| 2576 | |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 2577 | rc = pager_truncate(pPager, nPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2578 | return rc; |
| 2579 | } |
| 2580 | |
| 2581 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2582 | ** Shutdown the page cache. Free all memory and close all files. |
| 2583 | ** |
| 2584 | ** If a transaction was in progress when this routine is called, that |
| 2585 | ** transaction is rolled back. All outstanding pages are invalidated |
| 2586 | ** and their memory is freed. Any attempt to use a page associated |
| 2587 | ** with this page cache after this function returns will likely |
| 2588 | ** result in a coredump. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2589 | ** |
| 2590 | ** This function always succeeds. If a transaction is active an attempt |
| 2591 | ** is made to roll it back. If an error occurs during the rollback |
| 2592 | ** a hot journal may be left in the filesystem but no error is returned |
| 2593 | ** to the caller. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2594 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2595 | int sqlite3PagerClose(Pager *pPager){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2596 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2597 | if( !MEMDB ){ |
| 2598 | sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
| 2599 | sqlite3_mutex_enter(mutex); |
| 2600 | if( pPager->pPrev ){ |
| 2601 | pPager->pPrev->pNext = pPager->pNext; |
| 2602 | }else{ |
| 2603 | sqlite3PagerList = pPager->pNext; |
| 2604 | } |
| 2605 | if( pPager->pNext ){ |
| 2606 | pPager->pNext->pPrev = pPager->pPrev; |
| 2607 | } |
| 2608 | sqlite3_mutex_leave(mutex); |
| 2609 | } |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 2610 | #endif |
| 2611 | |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2612 | disable_simulated_io_errors(); |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 2613 | pPager->errCode = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 2614 | pPager->exclusiveMode = 0; |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2615 | pager_reset(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2616 | pagerUnlockAndRollback(pPager); |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2617 | enable_simulated_io_errors(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2618 | PAGERTRACE2("CLOSE %d\n", PAGERID(pPager)); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2619 | IOTRACE(("CLOSE %p\n", pPager)) |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 2620 | assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) ); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2621 | if( pPager->journalOpen ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2622 | sqlite3OsClose(pPager->jfd); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2623 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2624 | sqlite3_free(pPager->aInJournal); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2625 | if( pPager->stmtOpen ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2626 | sqlite3OsClose(pPager->stfd); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2627 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2628 | sqlite3OsClose(pPager->fd); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 2629 | /* Temp files are automatically deleted by the OS |
| 2630 | ** if( pPager->tempFile ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 2631 | ** sqlite3OsDelete(pPager->zFilename); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 2632 | ** } |
| 2633 | */ |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 2634 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2635 | sqlite3_free(pPager->aHash); |
| 2636 | sqlite3_free(pPager->pTmpSpace); |
| 2637 | sqlite3_free(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2638 | return SQLITE_OK; |
| 2639 | } |
| 2640 | |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 2641 | #if !defined(NDEBUG) || defined(SQLITE_TEST) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2642 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2643 | ** Return the page number for the given page data. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2644 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2645 | Pgno sqlite3PagerPagenumber(DbPage *p){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2646 | return p->pgno; |
| 2647 | } |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 2648 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2649 | |
| 2650 | /* |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2651 | ** The page_ref() function increments the reference count for a page. |
| 2652 | ** 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] | 2653 | ** remove it from the freelist. |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2654 | ** |
| 2655 | ** For non-test systems, page_ref() is a macro that calls _page_ref() |
| 2656 | ** online of the reference count is zero. For test systems, page_ref() |
| 2657 | ** is a real function so that we can set breakpoints and trace it. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2658 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 2659 | static void _page_ref(PgHdr *pPg){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2660 | if( pPg->nRef==0 ){ |
| 2661 | /* The page is currently on the freelist. Remove it. */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2662 | lruListRemove(pPg); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2663 | pPg->pPager->nRef++; |
| 2664 | } |
| 2665 | pPg->nRef++; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2666 | REFINFO(pPg); |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2667 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 2668 | #ifdef SQLITE_DEBUG |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2669 | static void page_ref(PgHdr *pPg){ |
| 2670 | if( pPg->nRef==0 ){ |
| 2671 | _page_ref(pPg); |
| 2672 | }else{ |
| 2673 | pPg->nRef++; |
| 2674 | REFINFO(pPg); |
| 2675 | } |
| 2676 | } |
| 2677 | #else |
| 2678 | # define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++) |
| 2679 | #endif |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2680 | |
| 2681 | /* |
| 2682 | ** Increment the reference count for a page. The input pointer is |
| 2683 | ** a reference to the page data. |
| 2684 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2685 | int sqlite3PagerRef(DbPage *pPg){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2686 | pagerEnter(pPg->pPager); |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2687 | page_ref(pPg); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2688 | pagerLeave(pPg->pPager); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2689 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2693 | ** Sync the journal. In other words, make sure all the pages that have |
| 2694 | ** been written to the journal have actually reached the surface of the |
| 2695 | ** disk. It is not safe to modify the original database file until after |
| 2696 | ** the journal has been synced. If the original database is modified before |
| 2697 | ** the journal is synced and a power failure occurs, the unsynced journal |
| 2698 | ** data would be lost and we would be unable to completely rollback the |
| 2699 | ** database changes. Database corruption would occur. |
| 2700 | ** |
| 2701 | ** This routine also updates the nRec field in the header of the journal. |
| 2702 | ** (See comments on the pager_playback() routine for additional information.) |
| 2703 | ** If the sync mode is FULL, two syncs will occur. First the whole journal |
| 2704 | ** is synced, then the nRec field is updated, then a second sync occurs. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2705 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2706 | ** For temporary databases, we do not care if we are able to rollback |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2707 | ** after a power failure, so no sync occurs. |
| 2708 | ** |
| 2709 | ** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which |
| 2710 | ** the database is stored, then OsSync() is never called on the journal |
| 2711 | ** file. In this case all that is required is to update the nRec field in |
| 2712 | ** the journal header. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2713 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2714 | ** This routine clears the needSync field of every page current held in |
| 2715 | ** memory. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2716 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2717 | static int syncJournal(Pager *pPager){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2718 | PgHdr *pPg; |
| 2719 | int rc = SQLITE_OK; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2720 | |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2721 | |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2722 | /* Sync the journal before modifying the main database |
| 2723 | ** (assuming there is a journal and it needs to be synced.) |
| 2724 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2725 | if( pPager->needSync ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2726 | if( !pPager->tempFile ){ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2727 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2728 | assert( pPager->journalOpen ); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2729 | |
drh | 946966f | 2004-02-25 02:20:41 +0000 | [diff] [blame] | 2730 | /* assert( !pPager->noSync ); // noSync might be set if synchronous |
| 2731 | ** was turned off after the transaction was started. Ticket #615 */ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2732 | #ifndef NDEBUG |
| 2733 | { |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2734 | /* Make sure the pPager->nRec counter we are keeping agrees |
| 2735 | ** with the nRec computed from the size of the journal file. |
| 2736 | */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 2737 | i64 jSz; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2738 | rc = sqlite3OsFileSize(pPager->jfd, &jSz); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2739 | if( rc!=0 ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2740 | assert( pPager->journalOff==jSz ); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2741 | } |
| 2742 | #endif |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2743 | if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2744 | /* Write the nRec value into the journal file header. If in |
| 2745 | ** full-synchronous mode, sync the journal first. This ensures that |
| 2746 | ** all data has really hit the disk before nRec is updated to mark |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2747 | ** it as a candidate for rollback. |
| 2748 | ** |
| 2749 | ** This is not required if the persistent media supports the |
| 2750 | ** SAFE_APPEND property. Because in this case it is not possible |
| 2751 | ** for garbage data to be appended to the file, the nRec field |
| 2752 | ** is populated with 0xFFFFFFFF when the journal header is written |
| 2753 | ** and never needs to be updated. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2754 | */ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2755 | i64 jrnlOff; |
| 2756 | if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2757 | PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2758 | IOTRACE(("JSYNC %p\n", pPager)) |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2759 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags); |
drh | d8d66e8 | 2003-02-12 02:10:15 +0000 | [diff] [blame] | 2760 | if( rc!=0 ) return rc; |
| 2761 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2762 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2763 | jrnlOff = pPager->journalHdr + sizeof(aJournalMagic); |
| 2764 | IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4)); |
| 2765 | rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec); |
drh | b4746b9 | 2005-09-09 01:32:06 +0000 | [diff] [blame] | 2766 | if( rc ) return rc; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2767 | } |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2768 | if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
| 2769 | PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); |
| 2770 | IOTRACE(("JSYNC %p\n", pPager)) |
| 2771 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| |
| 2772 | (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) |
| 2773 | ); |
| 2774 | if( rc!=0 ) return rc; |
| 2775 | } |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2776 | pPager->journalStarted = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2777 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2778 | pPager->needSync = 0; |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2779 | |
| 2780 | /* Erase the needSync flag from every page. |
| 2781 | */ |
| 2782 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 2783 | pPg->needSync = 0; |
| 2784 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2785 | lruListSetFirstSynced(pPager); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2786 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2787 | |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2788 | #ifndef NDEBUG |
| 2789 | /* If the Pager.needSync flag is clear then the PgHdr.needSync |
| 2790 | ** flag must also be clear for all pages. Verify that this |
| 2791 | ** invariant is true. |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2792 | */ |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2793 | else{ |
| 2794 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 2795 | assert( pPg->needSync==0 ); |
| 2796 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2797 | assert( pPager->lru.pFirstSynced==pPager->lru.pFirst ); |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2798 | } |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2799 | #endif |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2800 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2801 | return rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | /* |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2805 | ** Merge two lists of pages connected by pDirty and in pgno order. |
| 2806 | ** Do not both fixing the pPrevDirty pointers. |
| 2807 | */ |
| 2808 | static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){ |
| 2809 | PgHdr result, *pTail; |
| 2810 | pTail = &result; |
| 2811 | while( pA && pB ){ |
| 2812 | if( pA->pgno<pB->pgno ){ |
| 2813 | pTail->pDirty = pA; |
| 2814 | pTail = pA; |
| 2815 | pA = pA->pDirty; |
| 2816 | }else{ |
| 2817 | pTail->pDirty = pB; |
| 2818 | pTail = pB; |
| 2819 | pB = pB->pDirty; |
| 2820 | } |
| 2821 | } |
| 2822 | if( pA ){ |
| 2823 | pTail->pDirty = pA; |
| 2824 | }else if( pB ){ |
| 2825 | pTail->pDirty = pB; |
| 2826 | }else{ |
| 2827 | pTail->pDirty = 0; |
| 2828 | } |
| 2829 | return result.pDirty; |
| 2830 | } |
| 2831 | |
| 2832 | /* |
| 2833 | ** Sort the list of pages in accending order by pgno. Pages are |
| 2834 | ** connected by pDirty pointers. The pPrevDirty pointers are |
| 2835 | ** corrupted by this sort. |
| 2836 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2837 | #define N_SORT_BUCKET_ALLOC 25 |
| 2838 | #define N_SORT_BUCKET 25 |
| 2839 | #ifdef SQLITE_TEST |
| 2840 | int sqlite3_pager_n_sort_bucket = 0; |
| 2841 | #undef N_SORT_BUCKET |
| 2842 | #define N_SORT_BUCKET \ |
| 2843 | (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC) |
| 2844 | #endif |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2845 | static PgHdr *sort_pagelist(PgHdr *pIn){ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2846 | PgHdr *a[N_SORT_BUCKET_ALLOC], *p; |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2847 | int i; |
| 2848 | memset(a, 0, sizeof(a)); |
| 2849 | while( pIn ){ |
| 2850 | p = pIn; |
| 2851 | pIn = p->pDirty; |
| 2852 | p->pDirty = 0; |
| 2853 | for(i=0; i<N_SORT_BUCKET-1; i++){ |
| 2854 | if( a[i]==0 ){ |
| 2855 | a[i] = p; |
| 2856 | break; |
| 2857 | }else{ |
| 2858 | p = merge_pagelist(a[i], p); |
| 2859 | a[i] = 0; |
| 2860 | } |
| 2861 | } |
| 2862 | if( i==N_SORT_BUCKET-1 ){ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2863 | /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET) |
| 2864 | ** elements in the input list. This is possible, but impractical. |
| 2865 | ** Testing this line is the point of global variable |
| 2866 | ** sqlite3_pager_n_sort_bucket. |
| 2867 | */ |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2868 | a[i] = merge_pagelist(a[i], p); |
| 2869 | } |
| 2870 | } |
| 2871 | p = a[0]; |
| 2872 | for(i=1; i<N_SORT_BUCKET; i++){ |
| 2873 | p = merge_pagelist(p, a[i]); |
| 2874 | } |
| 2875 | return p; |
| 2876 | } |
| 2877 | |
| 2878 | /* |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2879 | ** Given a list of pages (connected by the PgHdr.pDirty pointer) write |
| 2880 | ** every one of those pages out to the database file and mark them all |
| 2881 | ** as clean. |
| 2882 | */ |
| 2883 | static int pager_write_pagelist(PgHdr *pList){ |
| 2884 | Pager *pPager; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2885 | PgHdr *p; |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2886 | int rc; |
| 2887 | |
| 2888 | if( pList==0 ) return SQLITE_OK; |
| 2889 | pPager = pList->pPager; |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2890 | |
| 2891 | /* At this point there may be either a RESERVED or EXCLUSIVE lock on the |
| 2892 | ** database file. If there is already an EXCLUSIVE lock, the following |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2893 | ** calls to sqlite3OsLock() are no-ops. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2894 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2895 | ** Moving the lock from RESERVED to EXCLUSIVE actually involves going |
| 2896 | ** through an intermediate state PENDING. A PENDING lock prevents new |
| 2897 | ** readers from attaching to the database but is unsufficient for us to |
| 2898 | ** write. The idea of a PENDING lock is to prevent new readers from |
| 2899 | ** coming in while we wait for existing readers to clear. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2900 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2901 | ** While the pager is in the RESERVED state, the original database file |
| 2902 | ** is unchanged and we can rollback without having to playback the |
| 2903 | ** journal into the original database file. Once we transition to |
| 2904 | ** EXCLUSIVE, it means the database file has been changed and any rollback |
| 2905 | ** will require a journal playback. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2906 | */ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2907 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2908 | if( rc!=SQLITE_OK ){ |
| 2909 | return rc; |
| 2910 | } |
| 2911 | |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2912 | pList = sort_pagelist(pList); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2913 | for(p=pList; p; p=p->pDirty){ |
| 2914 | assert( p->dirty ); |
| 2915 | p->dirty = 0; |
| 2916 | } |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2917 | while( pList ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2918 | |
| 2919 | /* If the file has not yet been opened, open it now. */ |
| 2920 | if( !pPager->fd->pMethods ){ |
| 2921 | assert(pPager->tempFile); |
| 2922 | rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename); |
| 2923 | if( rc ) return rc; |
| 2924 | } |
| 2925 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2926 | /* If there are dirty pages in the page cache with page numbers greater |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2927 | ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2928 | ** make the file smaller (presumably by auto-vacuum code). Do not write |
| 2929 | ** any such pages to the file. |
| 2930 | */ |
| 2931 | if( pList->pgno<=pPager->dbSize ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2932 | i64 offset = (pList->pgno-1)*(i64)pPager->pageSize; |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 2933 | char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 2934 | PAGERTRACE4("STORE %d page %d hash(%08x)\n", |
| 2935 | PAGERID(pPager), pList->pgno, pager_pagehash(pList)); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2936 | IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno)); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2937 | rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2938 | PAGER_INCR(sqlite3_pager_writedb_count); |
| 2939 | PAGER_INCR(pPager->nWrite); |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 2940 | if( pList->pgno==1 ){ |
| 2941 | memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); |
| 2942 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2943 | } |
| 2944 | #ifndef NDEBUG |
| 2945 | else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2946 | PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2947 | } |
| 2948 | #endif |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2949 | if( rc ) return rc; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 2950 | #ifdef SQLITE_CHECK_PAGES |
| 2951 | pList->pageHash = pager_pagehash(pList); |
| 2952 | #endif |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2953 | pList = pList->pDirty; |
| 2954 | } |
| 2955 | return SQLITE_OK; |
| 2956 | } |
| 2957 | |
| 2958 | /* |
| 2959 | ** Collect every dirty page into a dirty list and |
| 2960 | ** return a pointer to the head of that list. All pages are |
| 2961 | ** collected even if they are still in use. |
| 2962 | */ |
| 2963 | static PgHdr *pager_get_all_dirty_pages(Pager *pPager){ |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 2964 | return pPager->pDirty; |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | /* |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 2968 | ** Return TRUE if there is a hot journal on the given pager. |
| 2969 | ** A hot journal is one that needs to be played back. |
| 2970 | ** |
| 2971 | ** If the current size of the database file is 0 but a journal file |
| 2972 | ** exists, that is probably an old journal left over from a prior |
| 2973 | ** database with the same name. Just delete the journal. |
| 2974 | */ |
| 2975 | static int hasHotJournal(Pager *pPager){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2976 | sqlite3_vfs *pVfs = pPager->pVfs; |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 2977 | if( !pPager->useJournal ) return 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2978 | if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){ |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 2979 | return 0; |
| 2980 | } |
| 2981 | if( sqlite3OsCheckReservedLock(pPager->fd) ){ |
| 2982 | return 0; |
| 2983 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2984 | if( sqlite3PagerPagecount(pPager)==0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2985 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 2986 | return 0; |
| 2987 | }else{ |
| 2988 | return 1; |
| 2989 | } |
| 2990 | } |
| 2991 | |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 2992 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2993 | ** Try to find a page in the cache that can be recycled. |
| 2994 | ** |
| 2995 | ** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 2996 | ** does not set the pPager->errCode variable. |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 2997 | */ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 2998 | static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){ |
| 2999 | PgHdr *pPg; |
| 3000 | *ppPg = 0; |
| 3001 | |
danielk1977 | c551edc | 2007-04-05 13:12:13 +0000 | [diff] [blame] | 3002 | assert(!MEMDB); |
| 3003 | |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3004 | /* Find a page to recycle. Try to locate a page that does not |
| 3005 | ** require us to do an fsync() on the journal. |
| 3006 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3007 | pPg = pPager->lru.pFirstSynced; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3008 | |
| 3009 | /* If we could not find a page that does not require an fsync() |
| 3010 | ** on the journal file then fsync the journal file. This is a |
| 3011 | ** very slow operation, so we work hard to avoid it. But sometimes |
| 3012 | ** it can't be helped. |
| 3013 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3014 | if( pPg==0 && pPager->lru.pFirst && syncOk && !MEMDB){ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 3015 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3016 | int rc = syncJournal(pPager); |
| 3017 | if( rc!=0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3018 | return rc; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3019 | } |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 3020 | if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3021 | /* If in full-sync mode, write a new journal header into the |
| 3022 | ** journal file. This is done to avoid ever modifying a journal |
| 3023 | ** header that is involved in the rollback of pages that have |
| 3024 | ** already been written to the database (in case the header is |
| 3025 | ** trashed when the nRec field is updated). |
| 3026 | */ |
| 3027 | pPager->nRec = 0; |
| 3028 | assert( pPager->journalOff > 0 ); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3029 | assert( pPager->doNotSync==0 ); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3030 | rc = writeJournalHdr(pPager); |
| 3031 | if( rc!=0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3032 | return rc; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3033 | } |
| 3034 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3035 | pPg = pPager->lru.pFirst; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3036 | } |
| 3037 | if( pPg==0 ){ |
| 3038 | return SQLITE_OK; |
| 3039 | } |
| 3040 | |
| 3041 | assert( pPg->nRef==0 ); |
| 3042 | |
| 3043 | /* Write the page to the database file if it is dirty. |
| 3044 | */ |
| 3045 | if( pPg->dirty ){ |
| 3046 | int rc; |
| 3047 | assert( pPg->needSync==0 ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3048 | makeClean(pPg); |
| 3049 | pPg->dirty = 1; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3050 | pPg->pDirty = 0; |
| 3051 | rc = pager_write_pagelist( pPg ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3052 | pPg->dirty = 0; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3053 | if( rc!=SQLITE_OK ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3054 | return rc; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3055 | } |
| 3056 | } |
| 3057 | assert( pPg->dirty==0 ); |
| 3058 | |
| 3059 | /* If the page we are recycling is marked as alwaysRollback, then |
| 3060 | ** set the global alwaysRollback flag, thus disabling the |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3061 | ** sqlite3PagerDontRollback() optimization for the rest of this transaction. |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3062 | ** It is necessary to do this because the page marked alwaysRollback |
| 3063 | ** might be reloaded at a later time but at that point we won't remember |
| 3064 | ** that is was marked alwaysRollback. This means that all pages must |
| 3065 | ** be marked as alwaysRollback from here on out. |
| 3066 | */ |
| 3067 | if( pPg->alwaysRollback ){ |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 3068 | IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager)) |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3069 | pPager->alwaysRollback = 1; |
| 3070 | } |
| 3071 | |
| 3072 | /* Unlink the old page from the free list and the hash table |
| 3073 | */ |
| 3074 | unlinkPage(pPg); |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 3075 | assert( pPg->pgno==0 ); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3076 | |
| 3077 | *ppPg = pPg; |
| 3078 | return SQLITE_OK; |
| 3079 | } |
| 3080 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3081 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3082 | /* |
| 3083 | ** This function is called to free superfluous dynamically allocated memory |
| 3084 | ** held by the pager system. Memory in use by any SQLite pager allocated |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3085 | ** by the current thread may be sqlite3_free()ed. |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3086 | ** |
| 3087 | ** nReq is the number of bytes of memory required. Once this much has |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3088 | ** been released, the function returns. The return value is the total number |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3089 | ** of bytes of memory released. |
| 3090 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3091 | int sqlite3PagerReleaseMemory(int nReq){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3092 | int nReleased = 0; /* Bytes of memory released so far */ |
| 3093 | sqlite3_mutex *mutex; /* The MEM2 mutex */ |
| 3094 | Pager *pPager; /* For looping over pagers */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3095 | int rc = SQLITE_OK; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3096 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3097 | /* Acquire the memory-management mutex |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 3098 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3099 | mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
| 3100 | sqlite3_mutex_enter(mutex); |
| 3101 | |
| 3102 | /* Signal all database connections that memory management wants |
| 3103 | ** to have access to the pagers. |
| 3104 | */ |
| 3105 | for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){ |
| 3106 | pPager->iInUseMM = 1; |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3109 | while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){ |
| 3110 | PgHdr *pPg; |
| 3111 | PgHdr *pRecycled; |
| 3112 | |
| 3113 | /* Try to find a page to recycle that does not require a sync(). If |
| 3114 | ** this is not possible, find one that does require a sync(). |
| 3115 | */ |
| 3116 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 3117 | pPg = sqlite3LruPageList.pFirstSynced; |
| 3118 | while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){ |
| 3119 | pPg = pPg->gfree.pNext; |
| 3120 | } |
| 3121 | if( !pPg ){ |
| 3122 | pPg = sqlite3LruPageList.pFirst; |
| 3123 | while( pPg && pPg->pPager->iInUseDB ){ |
| 3124 | pPg = pPg->gfree.pNext; |
| 3125 | } |
| 3126 | } |
| 3127 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3128 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 3129 | /* If pPg==0, then the block above has failed to find a page to |
| 3130 | ** recycle. In this case return early - no further memory will |
| 3131 | ** be released. |
| 3132 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3133 | if( !pPg ) break; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3134 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3135 | pPager = pPg->pPager; |
| 3136 | assert(!pPg->needSync || pPg==pPager->lru.pFirst); |
| 3137 | assert(pPg->needSync || pPg==pPager->lru.pFirstSynced); |
| 3138 | |
| 3139 | rc = pager_recycle(pPager, 1, &pRecycled); |
| 3140 | assert(pRecycled==pPg || rc!=SQLITE_OK); |
| 3141 | if( rc==SQLITE_OK ){ |
| 3142 | /* We've found a page to free. At this point the page has been |
| 3143 | ** removed from the page hash-table, free-list and synced-list |
| 3144 | ** (pFirstSynced). It is still in the all pages (pAll) list. |
| 3145 | ** Remove it from this list before freeing. |
| 3146 | ** |
| 3147 | ** Todo: Check the Pager.pStmt list to make sure this is Ok. It |
| 3148 | ** probably is though. |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3149 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3150 | PgHdr *pTmp; |
| 3151 | assert( pPg ); |
| 3152 | if( pPg==pPager->pAll ){ |
| 3153 | pPager->pAll = pPg->pNextAll; |
| 3154 | }else{ |
| 3155 | for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){} |
| 3156 | pTmp->pNextAll = pPg->pNextAll; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3157 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3158 | nReleased += ( |
| 3159 | sizeof(*pPg) + pPager->pageSize |
| 3160 | + sizeof(u32) + pPager->nExtra |
| 3161 | + MEMDB*sizeof(PgHistory) |
| 3162 | ); |
| 3163 | IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno)); |
| 3164 | PAGER_INCR(sqlite3_pager_pgfree_count); |
| 3165 | sqlite3_free(pPg); |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 3166 | pPager->nPage--; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3167 | }else{ |
| 3168 | /* An error occured whilst writing to the database file or |
| 3169 | ** journal in pager_recycle(). The error is not returned to the |
| 3170 | ** caller of this function. Instead, set the Pager.errCode variable. |
| 3171 | ** The error will be returned to the user (or users, in the case |
| 3172 | ** of a shared pager cache) of the pager for which the error occured. |
| 3173 | */ |
| 3174 | assert( |
| 3175 | (rc&0xff)==SQLITE_IOERR || |
| 3176 | rc==SQLITE_FULL || |
| 3177 | rc==SQLITE_BUSY |
| 3178 | ); |
| 3179 | assert( pPager->state>=PAGER_RESERVED ); |
| 3180 | pager_error(pPager, rc); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3181 | } |
| 3182 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3183 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3184 | /* Clear the memory management flags and release the mutex |
| 3185 | */ |
| 3186 | for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){ |
| 3187 | pPager->iInUseMM = 0; |
| 3188 | } |
| 3189 | sqlite3_mutex_leave(mutex); |
| 3190 | |
| 3191 | /* Return the number of bytes released |
| 3192 | */ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3193 | return nReleased; |
| 3194 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3195 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3196 | |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3197 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3198 | ** Read the content of page pPg out of the database file. |
| 3199 | */ |
| 3200 | static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){ |
| 3201 | int rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3202 | i64 offset; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3203 | assert( MEMDB==0 ); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 3204 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 3205 | if( !pPager->fd->pMethods ){ |
| 3206 | return SQLITE_IOERR_SHORT_READ; |
| 3207 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3208 | offset = (pgno-1)*(i64)pPager->pageSize; |
| 3209 | rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3210 | PAGER_INCR(sqlite3_pager_readdb_count); |
| 3211 | PAGER_INCR(pPager->nRead); |
| 3212 | IOTRACE(("PGIN %p %d\n", pPager, pgno)); |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3213 | if( pgno==1 ){ |
| 3214 | memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24], |
| 3215 | sizeof(pPager->dbFileVers)); |
| 3216 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3217 | CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 3218 | PAGERTRACE4("FETCH %d page %d hash(%08x)\n", |
| 3219 | PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3220 | return rc; |
| 3221 | } |
| 3222 | |
| 3223 | |
| 3224 | /* |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3225 | ** This function is called to obtain the shared lock required before |
| 3226 | ** data may be read from the pager cache. If the shared lock has already |
| 3227 | ** been obtained, this function is a no-op. |
danielk1977 | 393f068 | 2007-03-31 10:00:48 +0000 | [diff] [blame] | 3228 | ** |
| 3229 | ** Immediately after obtaining the shared lock (if required), this function |
| 3230 | ** checks for a hot-journal file. If one is found, an emergency rollback |
| 3231 | ** is performed immediately. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3232 | */ |
| 3233 | static int pagerSharedLock(Pager *pPager){ |
| 3234 | int rc = SQLITE_OK; |
| 3235 | |
| 3236 | if( pPager->state==PAGER_UNLOCK ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3237 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3238 | if( !MEMDB ){ |
| 3239 | assert( pPager->nRef==0 ); |
| 3240 | if( !pPager->noReadlock ){ |
| 3241 | rc = pager_wait_on_lock(pPager, SHARED_LOCK); |
| 3242 | if( rc!=SQLITE_OK ){ |
| 3243 | return pager_error(pPager, rc); |
| 3244 | } |
| 3245 | assert( pPager->state>=SHARED_LOCK ); |
| 3246 | } |
| 3247 | |
| 3248 | /* If a journal file exists, and there is no RESERVED lock on the |
| 3249 | ** database file, then it either needs to be played back or deleted. |
| 3250 | */ |
| 3251 | if( hasHotJournal(pPager) ){ |
| 3252 | /* Get an EXCLUSIVE lock on the database file. At this point it is |
| 3253 | ** important that a RESERVED lock is not obtained on the way to the |
| 3254 | ** EXCLUSIVE lock. If it were, another process might open the |
| 3255 | ** database file, detect the RESERVED lock, and conclude that the |
| 3256 | ** database is safe to read while this process is still rolling it |
| 3257 | ** back. |
| 3258 | ** |
| 3259 | ** Because the intermediate RESERVED lock is not requested, the |
| 3260 | ** second process will get to this point in the code and fail to |
| 3261 | ** obtain it's own EXCLUSIVE lock on the database file. |
| 3262 | */ |
| 3263 | rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK); |
| 3264 | if( rc!=SQLITE_OK ){ |
| 3265 | pager_unlock(pPager); |
| 3266 | return pager_error(pPager, rc); |
| 3267 | } |
| 3268 | pPager->state = PAGER_EXCLUSIVE; |
| 3269 | |
| 3270 | /* Open the journal for reading only. Return SQLITE_BUSY if |
| 3271 | ** we are unable to open the journal file. |
| 3272 | ** |
| 3273 | ** The journal file does not need to be locked itself. The |
| 3274 | ** journal file is never open unless the main database file holds |
| 3275 | ** a write lock, so there is never any chance of two or more |
| 3276 | ** processes opening the journal at the same time. |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3277 | ** |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3278 | ** Open the journal for read/write access. This is because in |
| 3279 | ** exclusive-access mode the file descriptor will be kept open and |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3280 | ** possibly used for a transaction later on. On some systems, the |
| 3281 | ** OsTruncate() call used in exclusive-access mode also requires |
| 3282 | ** a read/write file handle. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3283 | */ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3284 | rc = SQLITE_BUSY; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3285 | if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){ |
| 3286 | int fout = 0; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3287 | int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL; |
danielk1977 | 7152de8 | 2007-03-29 17:28:14 +0000 | [diff] [blame] | 3288 | assert( !pPager->tempFile ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 3289 | rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, &fout); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3290 | assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); |
| 3291 | if( fout&SQLITE_OPEN_READONLY ){ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3292 | rc = SQLITE_BUSY; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3293 | sqlite3OsClose(pPager->jfd); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3294 | } |
| 3295 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3296 | if( rc!=SQLITE_OK ){ |
| 3297 | pager_unlock(pPager); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 3298 | return (rc==SQLITE_NOMEM?rc:SQLITE_BUSY); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3299 | } |
| 3300 | pPager->journalOpen = 1; |
| 3301 | pPager->journalStarted = 0; |
| 3302 | pPager->journalOff = 0; |
| 3303 | pPager->setMaster = 0; |
| 3304 | pPager->journalHdr = 0; |
| 3305 | |
| 3306 | /* Playback and delete the journal. Drop the database write |
| 3307 | ** lock and reacquire the read lock. |
| 3308 | */ |
| 3309 | rc = pager_playback(pPager, 1); |
| 3310 | if( rc!=SQLITE_OK ){ |
| 3311 | return pager_error(pPager, rc); |
| 3312 | } |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 3313 | assert(pPager->state==PAGER_SHARED || |
| 3314 | (pPager->exclusiveMode && pPager->state>PAGER_SHARED) |
| 3315 | ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | if( pPager->pAll ){ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3319 | /* The shared-lock has just been acquired on the database file |
| 3320 | ** and there are already pages in the cache (from a previous |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3321 | ** read or write transaction). Check to see if the database |
| 3322 | ** has been modified. If the database has changed, flush the |
| 3323 | ** cache. |
| 3324 | ** |
| 3325 | ** Database changes is detected by looking at 15 bytes beginning |
| 3326 | ** at offset 24 into the file. The first 4 of these 16 bytes are |
| 3327 | ** a 32-bit counter that is incremented with each change. The |
| 3328 | ** other bytes change randomly with each file change when |
| 3329 | ** a codec is in use. |
| 3330 | ** |
| 3331 | ** There is a vanishingly small chance that a change will not be |
drh | 6fa5103 | 2007-05-09 20:35:31 +0000 | [diff] [blame] | 3332 | ** detected. The chance of an undetected change is so small that |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3333 | ** it can be neglected. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3334 | */ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3335 | char dbFileVers[sizeof(pPager->dbFileVers)]; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3336 | sqlite3PagerPagecount(pPager); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3337 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3338 | if( pPager->errCode ){ |
| 3339 | return pPager->errCode; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3342 | if( pPager->dbSize>0 ){ |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 3343 | IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3344 | rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3345 | if( rc!=SQLITE_OK ){ |
| 3346 | return rc; |
| 3347 | } |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3348 | }else{ |
| 3349 | memset(dbFileVers, 0, sizeof(dbFileVers)); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3352 | if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3353 | pager_reset(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3354 | } |
| 3355 | } |
| 3356 | } |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 3357 | assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED ); |
| 3358 | if( pPager->state==PAGER_UNLOCK ){ |
| 3359 | pPager->state = PAGER_SHARED; |
| 3360 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | return rc; |
| 3364 | } |
| 3365 | |
| 3366 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3367 | ** Allocate a PgHdr object. Either create a new one or reuse |
| 3368 | ** an existing one that is not otherwise in use. |
| 3369 | ** |
| 3370 | ** A new PgHdr structure is created if any of the following are |
| 3371 | ** true: |
| 3372 | ** |
| 3373 | ** (1) We have not exceeded our maximum allocated cache size |
| 3374 | ** as set by the "PRAGMA cache_size" command. |
| 3375 | ** |
| 3376 | ** (2) There are no unused PgHdr objects available at this time. |
| 3377 | ** |
| 3378 | ** (3) This is an in-memory database. |
| 3379 | ** |
| 3380 | ** (4) There are no PgHdr objects that do not require a journal |
| 3381 | ** file sync and a sync of the journal file is currently |
| 3382 | ** prohibited. |
| 3383 | ** |
| 3384 | ** Otherwise, reuse an existing PgHdr. In other words, reuse an |
| 3385 | ** existing PgHdr if all of the following are true: |
| 3386 | ** |
| 3387 | ** (1) We have reached or exceeded the maximum cache size |
| 3388 | ** allowed by "PRAGMA cache_size". |
| 3389 | ** |
| 3390 | ** (2) There is a PgHdr available with PgHdr->nRef==0 |
| 3391 | ** |
| 3392 | ** (3) We are not in an in-memory database |
| 3393 | ** |
| 3394 | ** (4) Either there is an available PgHdr that does not need |
| 3395 | ** to be synced to disk or else disk syncing is currently |
| 3396 | ** allowed. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3397 | */ |
| 3398 | static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){ |
| 3399 | int rc = SQLITE_OK; |
| 3400 | PgHdr *pPg; |
| 3401 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3402 | /* Create a new PgHdr if any of the four conditions defined |
danielk1977 | 2a6bdf6 | 2007-08-20 16:07:00 +0000 | [diff] [blame] | 3403 | ** above are met: */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3404 | if( pPager->nPage<pPager->mxPage |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3405 | || pPager->lru.pFirst==0 |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3406 | || MEMDB |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3407 | || (pPager->lru.pFirstSynced==0 && pPager->doNotSync) |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3408 | ){ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3409 | if( pPager->nPage>=pPager->nHash ){ |
| 3410 | pager_resize_hash_table(pPager, |
| 3411 | pPager->nHash<256 ? 256 : pPager->nHash*2); |
| 3412 | if( pPager->nHash==0 ){ |
| 3413 | rc = SQLITE_NOMEM; |
| 3414 | goto pager_allocate_out; |
| 3415 | } |
| 3416 | } |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3417 | pagerLeave(pPager); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3418 | pPg = sqlite3_malloc( sizeof(*pPg) + pPager->pageSize |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3419 | + sizeof(u32) + pPager->nExtra |
| 3420 | + MEMDB*sizeof(PgHistory) ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3421 | pagerEnter(pPager); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3422 | if( pPg==0 ){ |
| 3423 | rc = SQLITE_NOMEM; |
| 3424 | goto pager_allocate_out; |
| 3425 | } |
| 3426 | memset(pPg, 0, sizeof(*pPg)); |
| 3427 | if( MEMDB ){ |
| 3428 | memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory)); |
| 3429 | } |
| 3430 | pPg->pPager = pPager; |
| 3431 | pPg->pNextAll = pPager->pAll; |
| 3432 | pPager->pAll = pPg; |
| 3433 | pPager->nPage++; |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3434 | }else{ |
| 3435 | /* Recycle an existing page with a zero ref-count. */ |
| 3436 | rc = pager_recycle(pPager, 1, &pPg); |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 3437 | if( rc==SQLITE_BUSY ){ |
| 3438 | rc = SQLITE_IOERR_BLOCKED; |
| 3439 | } |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3440 | if( rc!=SQLITE_OK ){ |
| 3441 | goto pager_allocate_out; |
| 3442 | } |
| 3443 | assert( pPager->state>=SHARED_LOCK ); |
| 3444 | assert(pPg); |
| 3445 | } |
| 3446 | *ppPg = pPg; |
| 3447 | |
| 3448 | pager_allocate_out: |
| 3449 | return rc; |
| 3450 | } |
| 3451 | |
| 3452 | /* |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3453 | ** Make sure we have the content for a page. If the page was |
| 3454 | ** previously acquired with noContent==1, then the content was |
| 3455 | ** just initialized to zeros instead of being read from disk. |
| 3456 | ** But now we need the real data off of disk. So make sure we |
| 3457 | ** have it. Read it in if we do not have it already. |
| 3458 | */ |
| 3459 | static int pager_get_content(PgHdr *pPg){ |
| 3460 | if( pPg->needRead ){ |
| 3461 | int rc = readDbPage(pPg->pPager, pPg, pPg->pgno); |
| 3462 | if( rc==SQLITE_OK ){ |
| 3463 | pPg->needRead = 0; |
| 3464 | }else{ |
| 3465 | return rc; |
| 3466 | } |
| 3467 | } |
| 3468 | return SQLITE_OK; |
| 3469 | } |
| 3470 | |
| 3471 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3472 | ** Acquire a page. |
| 3473 | ** |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 3474 | ** 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] | 3475 | ** This read lock is dropped when the last page is released. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3476 | ** |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3477 | ** This routine works for any page number greater than 0. If the database |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3478 | ** file is smaller than the requested page, then no actual disk |
| 3479 | ** read occurs and the memory image of the page is initialized to |
| 3480 | ** all zeros. The extra data appended to a page is always initialized |
| 3481 | ** to zeros the first time a page is loaded into memory. |
| 3482 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3483 | ** The acquisition might fail for several reasons. In all cases, |
| 3484 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3485 | ** |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3486 | ** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3487 | ** 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] | 3488 | ** in memory, this routine goes to disk to read it in whereas Lookup() |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3489 | ** just returns 0. This routine acquires a read-lock the first time it |
| 3490 | ** 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] | 3491 | ** Since Lookup() never goes to disk, it never has to deal with locks |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3492 | ** or journal files. |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 3493 | ** |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3494 | ** If noContent is false, the page contents are actually read from disk. |
| 3495 | ** If noContent is true, it means that we do not care about the contents |
| 3496 | ** of the page at this time, so do not do a disk read. Just fill in the |
| 3497 | ** page content with zeros. But mark the fact that we have not read the |
| 3498 | ** content by setting the PgHdr.needRead flag. Later on, if |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3499 | ** sqlite3PagerWrite() is called on this page or if this routine is |
| 3500 | ** called again with noContent==0, that means that the content is needed |
| 3501 | ** and the disk read should occur at that point. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3502 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3503 | static int pagerAcquire( |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3504 | Pager *pPager, /* The pager open on the database file */ |
| 3505 | Pgno pgno, /* Page number to fetch */ |
| 3506 | DbPage **ppPage, /* Write a pointer to the page here */ |
| 3507 | int noContent /* Do not bother reading content from disk if true */ |
| 3508 | ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3509 | PgHdr *pPg; |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3510 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3511 | |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3512 | assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 ); |
| 3513 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 3514 | /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page |
| 3515 | ** number greater than this, or zero, is requested. |
| 3516 | */ |
drh | 267cb32 | 2005-09-16 17:16:52 +0000 | [diff] [blame] | 3517 | if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3518 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3521 | /* Make sure we have not hit any critical errors. |
| 3522 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 3523 | assert( pPager!=0 ); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3524 | *ppPage = 0; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3525 | if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
| 3526 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3529 | /* If this is the first page accessed, then get a SHARED lock |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3530 | ** on the database file. pagerSharedLock() is a no-op if |
| 3531 | ** a database lock is already held. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3532 | */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3533 | rc = pagerSharedLock(pPager); |
| 3534 | if( rc!=SQLITE_OK ){ |
| 3535 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3536 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3537 | assert( pPager->state!=PAGER_UNLOCK ); |
| 3538 | |
| 3539 | pPg = pager_lookup(pPager, pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3540 | if( pPg==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3541 | /* The requested page is not in the page cache. */ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3542 | int nMax; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3543 | int h; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3544 | PAGER_INCR(pPager->nMiss); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3545 | rc = pagerAllocatePage(pPager, &pPg); |
| 3546 | if( rc!=SQLITE_OK ){ |
| 3547 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3548 | } |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3549 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3550 | pPg->pgno = pgno; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 3551 | assert( !MEMDB || pgno>pPager->stmtSize ); |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 3552 | if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 3553 | #if 0 |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3554 | sqlite3CheckMemory(pPager->aInJournal, pgno/8); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 3555 | #endif |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 3556 | assert( pPager->journalOpen ); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3557 | pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 3558 | pPg->needSync = 0; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3559 | }else{ |
| 3560 | pPg->inJournal = 0; |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 3561 | pPg->needSync = 0; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3562 | } |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 3563 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3564 | makeClean(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3565 | pPg->nRef = 1; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3566 | REFINFO(pPg); |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3567 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3568 | pPager->nRef++; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3569 | if( pPager->nExtra>0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 3570 | memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3571 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3572 | nMax = sqlite3PagerPagecount(pPager); |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3573 | if( pPager->errCode ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3574 | sqlite3PagerUnref(pPg); |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3575 | rc = pPager->errCode; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3576 | return rc; |
| 3577 | } |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3578 | |
| 3579 | /* Populate the page with data, either by reading from the database |
| 3580 | ** file, or by setting the entire page to zero. |
| 3581 | */ |
drh | d215acf | 2007-04-13 04:01:58 +0000 | [diff] [blame] | 3582 | if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 3583 | if( pgno>pPager->mxPgno ){ |
danielk1977 | de3bea7 | 2007-05-09 15:56:39 +0000 | [diff] [blame] | 3584 | sqlite3PagerUnref(pPg); |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 3585 | return SQLITE_FULL; |
| 3586 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 3587 | memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize); |
drh | d215acf | 2007-04-13 04:01:58 +0000 | [diff] [blame] | 3588 | pPg->needRead = noContent && !pPager->alwaysRollback; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3589 | IOTRACE(("ZERO %p %d\n", pPager, pgno)); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3590 | }else{ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3591 | rc = readDbPage(pPager, pPg, pgno); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 3592 | if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ |
| 3593 | pPg->pgno = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3594 | sqlite3PagerUnref(pPg); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 3595 | return rc; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 3596 | } |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 3597 | pPg->needRead = 0; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3598 | } |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3599 | |
| 3600 | /* Link the page into the page hash table */ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 3601 | h = pgno & (pPager->nHash-1); |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 3602 | assert( pgno!=0 ); |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3603 | pPg->pNextHash = pPager->aHash[h]; |
| 3604 | pPager->aHash[h] = pPg; |
| 3605 | if( pPg->pNextHash ){ |
| 3606 | assert( pPg->pNextHash->pPrevHash==0 ); |
| 3607 | pPg->pNextHash->pPrevHash = pPg; |
| 3608 | } |
| 3609 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3610 | #ifdef SQLITE_CHECK_PAGES |
| 3611 | pPg->pageHash = pager_pagehash(pPg); |
| 3612 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3613 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3614 | /* The requested page is in the page cache. */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3615 | assert(pPager->nRef>0 || pgno==1); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3616 | PAGER_INCR(pPager->nHit); |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3617 | if( !noContent ){ |
| 3618 | rc = pager_get_content(pPg); |
| 3619 | if( rc ){ |
| 3620 | return rc; |
| 3621 | } |
| 3622 | } |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 3623 | page_ref(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3624 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3625 | *ppPage = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3626 | return SQLITE_OK; |
| 3627 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3628 | int sqlite3PagerAcquire( |
| 3629 | Pager *pPager, /* The pager open on the database file */ |
| 3630 | Pgno pgno, /* Page number to fetch */ |
| 3631 | DbPage **ppPage, /* Write a pointer to the page here */ |
| 3632 | int noContent /* Do not bother reading content from disk if true */ |
| 3633 | ){ |
| 3634 | int rc; |
| 3635 | pagerEnter(pPager); |
| 3636 | rc = pagerAcquire(pPager, pgno, ppPage, noContent); |
| 3637 | pagerLeave(pPager); |
| 3638 | return rc; |
| 3639 | } |
| 3640 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3641 | |
| 3642 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3643 | ** Acquire a page if it is already in the in-memory cache. Do |
| 3644 | ** not read the page from disk. Return a pointer to the page, |
| 3645 | ** or 0 if the page is not in cache. |
| 3646 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3647 | ** See also sqlite3PagerGet(). The difference between this routine |
| 3648 | ** and sqlite3PagerGet() is that _get() will go to the disk and read |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3649 | ** in the page if the page is not already in cache. This routine |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3650 | ** returns NULL if the page is not in cache or if a disk I/O error |
| 3651 | ** has ever happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3652 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3653 | DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3654 | PgHdr *pPg = 0; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3655 | |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 3656 | assert( pPager!=0 ); |
| 3657 | assert( pgno!=0 ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3658 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3659 | pagerEnter(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3660 | if( pPager->state==PAGER_UNLOCK ){ |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3661 | assert( !pPager->pAll || pPager->exclusiveMode ); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3662 | }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
| 3663 | /* Do nothing */ |
| 3664 | }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){ |
| 3665 | page_ref(pPg); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3666 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3667 | pagerLeave(pPager); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3668 | return pPg; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3672 | ** Release a page. |
| 3673 | ** |
| 3674 | ** If the number of references to the page drop to zero, then the |
| 3675 | ** page is added to the LRU list. When all references to all pages |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3676 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3677 | ** removed. |
| 3678 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3679 | int sqlite3PagerUnref(DbPage *pPg){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3680 | |
| 3681 | /* Decrement the reference count for this page |
| 3682 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3683 | assert( pPg->nRef>0 ); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3684 | pagerEnter(pPg->pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3685 | pPg->nRef--; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3686 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3687 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3688 | CHECK_PAGE(pPg); |
| 3689 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3690 | /* When the number of references to a page reach 0, call the |
| 3691 | ** destructor and add the page to the freelist. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3692 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3693 | if( pPg->nRef==0 ){ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3694 | Pager *pPager = pPg->pPager; |
| 3695 | |
| 3696 | lruListAdd(pPg); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3697 | if( pPager->xDestructor ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3698 | pPager->xDestructor(pPg, pPager->pageSize); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3699 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3700 | |
| 3701 | /* When all pages reach the freelist, drop the read lock from |
| 3702 | ** the database file. |
| 3703 | */ |
| 3704 | pPager->nRef--; |
| 3705 | assert( pPager->nRef>=0 ); |
danielk1977 | 3dedc19 | 2007-03-27 17:37:31 +0000 | [diff] [blame] | 3706 | if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3707 | pagerUnlockAndRollback(pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3708 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3709 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3710 | pagerLeave(pPg->pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3711 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3715 | ** Create a journal file for pPager. There should already be a RESERVED |
| 3716 | ** or EXCLUSIVE lock on the database file when this routine is called. |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3717 | ** |
| 3718 | ** Return SQLITE_OK if everything. Return an error code and release the |
| 3719 | ** write lock if anything goes wrong. |
| 3720 | */ |
| 3721 | static int pager_open_journal(Pager *pPager){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3722 | sqlite3_vfs *pVfs = pPager->pVfs; |
| 3723 | int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE); |
| 3724 | |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3725 | int rc; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3726 | assert( !MEMDB ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3727 | assert( pPager->state>=PAGER_RESERVED ); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3728 | assert( pPager->journalOpen==0 ); |
| 3729 | assert( pPager->useJournal ); |
drh | 88b01a1 | 2005-03-28 18:04:27 +0000 | [diff] [blame] | 3730 | assert( pPager->aInJournal==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3731 | sqlite3PagerPagecount(pPager); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3732 | pagerLeave(pPager); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3733 | pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3734 | pagerEnter(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3735 | if( pPager->aInJournal==0 ){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3736 | rc = SQLITE_NOMEM; |
| 3737 | goto failed_to_open_journal; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3738 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3739 | |
| 3740 | if( pPager->tempFile ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3741 | flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); |
| 3742 | }else{ |
| 3743 | flags |= (SQLITE_OPEN_MAIN_JOURNAL); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3744 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3745 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 3746 | rc = sqlite3JournalOpen( |
| 3747 | pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) |
| 3748 | ); |
| 3749 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3750 | rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3751 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3752 | assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 3753 | pPager->journalOff = 0; |
| 3754 | pPager->setMaster = 0; |
| 3755 | pPager->journalHdr = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3756 | if( rc!=SQLITE_OK ){ |
drh | 600e46a | 2007-03-28 01:59:33 +0000 | [diff] [blame] | 3757 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3758 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
drh | 600e46a | 2007-03-28 01:59:33 +0000 | [diff] [blame] | 3759 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3760 | goto failed_to_open_journal; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3761 | } |
| 3762 | pPager->journalOpen = 1; |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 3763 | pPager->journalStarted = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3764 | pPager->needSync = 0; |
| 3765 | pPager->alwaysRollback = 0; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 3766 | pPager->nRec = 0; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3767 | if( pPager->errCode ){ |
| 3768 | rc = pPager->errCode; |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 3769 | goto failed_to_open_journal; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3770 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3771 | pPager->origDbSize = pPager->dbSize; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 3772 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 3773 | rc = writeJournalHdr(pPager); |
| 3774 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 3775 | if( pPager->stmtAutoopen && rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3776 | rc = sqlite3PagerStmtBegin(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3777 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 3778 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3779 | rc = pager_end_transaction(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3780 | if( rc==SQLITE_OK ){ |
| 3781 | rc = SQLITE_FULL; |
| 3782 | } |
| 3783 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3784 | return rc; |
| 3785 | |
| 3786 | failed_to_open_journal: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3787 | sqlite3_free(pPager->aInJournal); |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3788 | pPager->aInJournal = 0; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3789 | return rc; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3790 | } |
| 3791 | |
| 3792 | /* |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3793 | ** Acquire a write-lock on the database. The lock is removed when |
| 3794 | ** the any of the following happen: |
| 3795 | ** |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3796 | ** * sqlite3PagerCommitPhaseTwo() is called. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3797 | ** * sqlite3PagerRollback() is called. |
| 3798 | ** * sqlite3PagerClose() is called. |
| 3799 | ** * sqlite3PagerUnref() is called to on every outstanding page. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3800 | ** |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3801 | ** The first parameter to this routine is a pointer to any open page of the |
| 3802 | ** database file. Nothing changes about the page - it is used merely to |
| 3803 | ** acquire a pointer to the Pager structure and as proof that there is |
| 3804 | ** already a read-lock on the database. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3805 | ** |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3806 | ** The second parameter indicates how much space in bytes to reserve for a |
| 3807 | ** master journal file-name at the start of the journal when it is created. |
| 3808 | ** |
| 3809 | ** A journal file is opened if this is not a temporary file. For temporary |
| 3810 | ** files, the opening of the journal file is deferred until there is an |
| 3811 | ** actual need to write to the journal. |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3812 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3813 | ** 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] | 3814 | ** |
| 3815 | ** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file |
| 3816 | ** immediately instead of waiting until we try to flush the cache. The |
| 3817 | ** exFlag is ignored if a transaction is already active. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3818 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3819 | int sqlite3PagerBegin(DbPage *pPg, int exFlag){ |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3820 | Pager *pPager = pPg->pPager; |
| 3821 | int rc = SQLITE_OK; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3822 | pagerEnter(pPager); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3823 | assert( pPg->nRef>0 ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3824 | assert( pPager->state!=PAGER_UNLOCK ); |
| 3825 | if( pPager->state==PAGER_SHARED ){ |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3826 | assert( pPager->aInJournal==0 ); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3827 | if( MEMDB ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3828 | pPager->state = PAGER_EXCLUSIVE; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 3829 | pPager->origDbSize = pPager->dbSize; |
| 3830 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3831 | rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK); |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 3832 | if( rc==SQLITE_OK ){ |
| 3833 | pPager->state = PAGER_RESERVED; |
| 3834 | if( exFlag ){ |
| 3835 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| 3836 | } |
| 3837 | } |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 3838 | if( rc!=SQLITE_OK ){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3839 | pagerLeave(pPager); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 3840 | return rc; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 3841 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3842 | pPager->dirtyCache = 0; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3843 | PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager)); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 3844 | if( pPager->useJournal && !pPager->tempFile ){ |
| 3845 | rc = pager_open_journal(pPager); |
| 3846 | } |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3847 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3848 | }else if( pPager->journalOpen && pPager->journalOff==0 ){ |
| 3849 | /* This happens when the pager was in exclusive-access mode last |
| 3850 | ** time a (read or write) transaction was successfully concluded |
| 3851 | ** by this connection. Instead of deleting the journal file it was |
| 3852 | ** kept open and truncated to 0 bytes. |
| 3853 | */ |
| 3854 | assert( pPager->nRec==0 ); |
| 3855 | assert( pPager->origDbSize==0 ); |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 3856 | assert( pPager->aInJournal==0 ); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3857 | sqlite3PagerPagecount(pPager); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3858 | pagerLeave(pPager); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3859 | pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3860 | pagerEnter(pPager); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3861 | if( !pPager->aInJournal ){ |
| 3862 | rc = SQLITE_NOMEM; |
| 3863 | }else{ |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 3864 | pPager->origDbSize = pPager->dbSize; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3865 | rc = writeJournalHdr(pPager); |
| 3866 | } |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3867 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3868 | assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK ); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3869 | pagerLeave(pPager); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3870 | return rc; |
| 3871 | } |
| 3872 | |
| 3873 | /* |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3874 | ** Make a page dirty. Set its dirty flag and add it to the dirty |
| 3875 | ** page list. |
| 3876 | */ |
| 3877 | static void makeDirty(PgHdr *pPg){ |
| 3878 | if( pPg->dirty==0 ){ |
| 3879 | Pager *pPager = pPg->pPager; |
| 3880 | pPg->dirty = 1; |
| 3881 | pPg->pDirty = pPager->pDirty; |
| 3882 | if( pPager->pDirty ){ |
| 3883 | pPager->pDirty->pPrevDirty = pPg; |
| 3884 | } |
| 3885 | pPg->pPrevDirty = 0; |
| 3886 | pPager->pDirty = pPg; |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | /* |
| 3891 | ** Make a page clean. Clear its dirty bit and remove it from the |
| 3892 | ** dirty page list. |
| 3893 | */ |
| 3894 | static void makeClean(PgHdr *pPg){ |
| 3895 | if( pPg->dirty ){ |
| 3896 | pPg->dirty = 0; |
| 3897 | if( pPg->pDirty ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3898 | assert( pPg->pDirty->pPrevDirty==pPg ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3899 | pPg->pDirty->pPrevDirty = pPg->pPrevDirty; |
| 3900 | } |
| 3901 | if( pPg->pPrevDirty ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3902 | assert( pPg->pPrevDirty->pDirty==pPg ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3903 | pPg->pPrevDirty->pDirty = pPg->pDirty; |
| 3904 | }else{ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3905 | assert( pPg->pPager->pDirty==pPg ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3906 | pPg->pPager->pDirty = pPg->pDirty; |
| 3907 | } |
| 3908 | } |
| 3909 | } |
| 3910 | |
| 3911 | |
| 3912 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3913 | ** Mark a data page as writeable. The page is written into the journal |
| 3914 | ** if it is not there already. This routine must be called before making |
| 3915 | ** changes to a page. |
| 3916 | ** |
| 3917 | ** The first time this routine is called, the pager creates a new |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3918 | ** journal and acquires a RESERVED lock on the database. If the RESERVED |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3919 | ** lock could not be acquired, this routine returns SQLITE_BUSY. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3920 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3921 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3922 | ** |
| 3923 | ** If the journal file could not be written because the disk is full, |
| 3924 | ** then this routine returns SQLITE_FULL and does an immediate rollback. |
| 3925 | ** All subsequent write attempts also return SQLITE_FULL until there |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3926 | ** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3927 | ** reset. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3928 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3929 | static int pager_write(PgHdr *pPg){ |
| 3930 | void *pData = PGHDR_TO_DATA(pPg); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 3931 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 3932 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 3933 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3934 | /* Check for errors |
| 3935 | */ |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3936 | if( pPager->errCode ){ |
| 3937 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3938 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3939 | if( pPager->readOnly ){ |
| 3940 | return SQLITE_PERM; |
| 3941 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3942 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 3943 | assert( !pPager->setMaster ); |
| 3944 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3945 | CHECK_PAGE(pPg); |
| 3946 | |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3947 | /* If this page was previously acquired with noContent==1, that means |
| 3948 | ** we didn't really read in the content of the page. This can happen |
| 3949 | ** (for example) when the page is being moved to the freelist. But |
| 3950 | ** now we are (perhaps) moving the page off of the freelist for |
| 3951 | ** reuse and we need to know its original content so that content |
| 3952 | ** can be stored in the rollback journal. So do the read at this |
| 3953 | ** time. |
| 3954 | */ |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3955 | rc = pager_get_content(pPg); |
| 3956 | if( rc ){ |
| 3957 | return rc; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3960 | /* Mark the page as dirty. If the page has already been written |
| 3961 | ** to the journal then we can return right away. |
| 3962 | */ |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3963 | makeDirty(pPg); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 3964 | if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3965 | pPager->dirtyCache = 1; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3966 | }else{ |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3967 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3968 | /* If we get this far, it means that the page needs to be |
| 3969 | ** written to the transaction journal or the ckeckpoint journal |
| 3970 | ** or both. |
| 3971 | ** |
| 3972 | ** First check to see that the transaction journal exists and |
| 3973 | ** create it if it does not. |
| 3974 | */ |
| 3975 | assert( pPager->state!=PAGER_UNLOCK ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3976 | rc = sqlite3PagerBegin(pPg, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3977 | if( rc!=SQLITE_OK ){ |
| 3978 | return rc; |
| 3979 | } |
| 3980 | assert( pPager->state>=PAGER_RESERVED ); |
| 3981 | if( !pPager->journalOpen && pPager->useJournal ){ |
| 3982 | rc = pager_open_journal(pPager); |
| 3983 | if( rc!=SQLITE_OK ) return rc; |
| 3984 | } |
| 3985 | assert( pPager->journalOpen || !pPager->useJournal ); |
| 3986 | pPager->dirtyCache = 1; |
| 3987 | |
| 3988 | /* The transaction journal now exists and we have a RESERVED or an |
| 3989 | ** EXCLUSIVE lock on the main database file. Write the current page to |
| 3990 | ** the transaction journal if it is not there already. |
| 3991 | */ |
| 3992 | if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){ |
| 3993 | if( (int)pPg->pgno <= pPager->origDbSize ){ |
| 3994 | int szPg; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3995 | if( MEMDB ){ |
| 3996 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3997 | PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3998 | assert( pHist->pOrig==0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3999 | pHist->pOrig = sqlite3_malloc( pPager->pageSize ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4000 | if( pHist->pOrig ){ |
| 4001 | memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize); |
| 4002 | } |
| 4003 | }else{ |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4004 | u32 cksum, saved; |
| 4005 | char *pData2, *pEnd; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4006 | |
drh | 267cb32 | 2005-09-16 17:16:52 +0000 | [diff] [blame] | 4007 | /* We should never write to the journal file the page that |
| 4008 | ** contains the database locks. The following assert verifies |
| 4009 | ** that we do not. */ |
| 4010 | assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4011 | pData2 = CODEC2(pPager, pData, pPg->pgno, 7); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 4012 | cksum = pager_cksum(pPager, (u8*)pData2); |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4013 | pEnd = pData2 + pPager->pageSize; |
| 4014 | pData2 -= 4; |
| 4015 | saved = *(u32*)pEnd; |
| 4016 | put32bits(pEnd, cksum); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4017 | szPg = pPager->pageSize+8; |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4018 | put32bits(pData2, pPg->pgno); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4019 | rc = sqlite3OsWrite(pPager->jfd, pData2, szPg, pPager->journalOff); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 4020 | IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4021 | pPager->journalOff, szPg)); |
| 4022 | PAGER_INCR(sqlite3_pager_writej_count); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4023 | pPager->journalOff += szPg; |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 4024 | PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n", |
| 4025 | PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg)); |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4026 | *(u32*)pEnd = saved; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4027 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 4028 | /* An error has occured writing to the journal file. The |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4029 | ** transaction will be rolled back by the layer above. |
| 4030 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4031 | if( rc!=SQLITE_OK ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4032 | return rc; |
| 4033 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4034 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4035 | pPager->nRec++; |
| 4036 | assert( pPager->aInJournal!=0 ); |
| 4037 | pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
| 4038 | pPg->needSync = !pPager->noSync; |
| 4039 | if( pPager->stmtInUse ){ |
| 4040 | pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4041 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4042 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 4043 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4044 | pPg->needSync = !pPager->journalStarted && !pPager->noSync; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4045 | PAGERTRACE4("APPEND %d page %d needSync=%d\n", |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 4046 | PAGERID(pPager), pPg->pgno, pPg->needSync); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4047 | } |
| 4048 | if( pPg->needSync ){ |
| 4049 | pPager->needSync = 1; |
| 4050 | } |
| 4051 | pPg->inJournal = 1; |
| 4052 | } |
| 4053 | |
| 4054 | /* If the statement journal is open and the page is not in it, |
| 4055 | ** then write the current page to the statement journal. Note that |
| 4056 | ** the statement journal format differs from the standard journal format |
| 4057 | ** in that it omits the checksums and the header. |
| 4058 | */ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4059 | if( pPager->stmtInUse |
| 4060 | && !pageInStatement(pPg) |
| 4061 | && (int)pPg->pgno<=pPager->stmtSize |
| 4062 | ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4063 | assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); |
| 4064 | if( MEMDB ){ |
| 4065 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 4066 | assert( pHist->pStmt==0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4067 | pHist->pStmt = sqlite3_malloc( pPager->pageSize ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4068 | if( pHist->pStmt ){ |
| 4069 | memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize); |
| 4070 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4071 | PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4072 | page_add_to_stmt_list(pPg); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4073 | }else{ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4074 | i64 offset = pPager->stmtNRec*(4+pPager->pageSize); |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4075 | char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4; |
| 4076 | put32bits(pData2, pPg->pgno); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4077 | rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4, offset); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4078 | PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4079 | if( rc!=SQLITE_OK ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4080 | return rc; |
| 4081 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4082 | pPager->stmtNRec++; |
| 4083 | assert( pPager->aInStmt!=0 ); |
| 4084 | pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 4085 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4086 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | /* Update the database size and return. |
| 4090 | */ |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 4091 | assert( pPager->state>=PAGER_SHARED ); |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 4092 | if( pPager->dbSize<(int)pPg->pgno ){ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 4093 | pPager->dbSize = pPg->pgno; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4094 | if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){ |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 4095 | pPager->dbSize++; |
| 4096 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 4097 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 4098 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | /* |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4102 | ** This function is used to mark a data-page as writable. It uses |
| 4103 | ** pager_write() to open a journal file (if it is not already open) |
| 4104 | ** and write the page *pData to the journal. |
| 4105 | ** |
| 4106 | ** The difference between this function and pager_write() is that this |
| 4107 | ** function also deals with the special case where 2 or more pages |
| 4108 | ** fit on a single disk sector. In this case all co-resident pages |
| 4109 | ** must have been written to the journal file before returning. |
| 4110 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4111 | int sqlite3PagerWrite(DbPage *pDbPage){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4112 | int rc = SQLITE_OK; |
| 4113 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4114 | PgHdr *pPg = pDbPage; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4115 | Pager *pPager = pPg->pPager; |
| 4116 | Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); |
| 4117 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4118 | pagerEnter(pPager); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4119 | if( !MEMDB && nPagePerSector>1 ){ |
| 4120 | Pgno nPageCount; /* Total number of pages in database file */ |
| 4121 | Pgno pg1; /* First page of the sector pPg is located on. */ |
| 4122 | int nPage; /* Number of pages starting at pg1 to journal */ |
| 4123 | int ii; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4124 | int needSync = 0; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4125 | |
| 4126 | /* Set the doNotSync flag to 1. This is because we cannot allow a journal |
| 4127 | ** header to be written between the pages journaled by this function. |
| 4128 | */ |
| 4129 | assert( pPager->doNotSync==0 ); |
| 4130 | pPager->doNotSync = 1; |
| 4131 | |
| 4132 | /* This trick assumes that both the page-size and sector-size are |
| 4133 | ** an integer power of 2. It sets variable pg1 to the identifier |
| 4134 | ** of the first page of the sector pPg is located on. |
| 4135 | */ |
| 4136 | pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; |
| 4137 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4138 | nPageCount = sqlite3PagerPagecount(pPager); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4139 | if( pPg->pgno>nPageCount ){ |
| 4140 | nPage = (pPg->pgno - pg1)+1; |
| 4141 | }else if( (pg1+nPagePerSector-1)>nPageCount ){ |
| 4142 | nPage = nPageCount+1-pg1; |
| 4143 | }else{ |
| 4144 | nPage = nPagePerSector; |
| 4145 | } |
| 4146 | assert(nPage>0); |
| 4147 | assert(pg1<=pPg->pgno); |
| 4148 | assert((pg1+nPage)>pPg->pgno); |
| 4149 | |
| 4150 | for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ |
| 4151 | Pgno pg = pg1+ii; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4152 | PgHdr *pPage; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4153 | if( !pPager->aInJournal || pg==pPg->pgno || |
| 4154 | pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7))) |
| 4155 | ) { |
| 4156 | if( pg!=PAGER_MJ_PGNO(pPager) ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4157 | rc = sqlite3PagerGet(pPager, pg, &pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4158 | if( rc==SQLITE_OK ){ |
| 4159 | rc = pager_write(pPage); |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4160 | if( pPage->needSync ){ |
| 4161 | needSync = 1; |
| 4162 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4163 | sqlite3PagerUnref(pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4164 | } |
| 4165 | } |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4166 | }else if( (pPage = pager_lookup(pPager, pg)) ){ |
| 4167 | if( pPage->needSync ){ |
| 4168 | needSync = 1; |
| 4169 | } |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4170 | } |
| 4171 | } |
| 4172 | |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4173 | /* If the PgHdr.needSync flag is set for any of the nPage pages |
| 4174 | ** starting at pg1, then it needs to be set for all of them. Because |
| 4175 | ** writing to any of these nPage pages may damage the others, the |
| 4176 | ** journal file must contain sync()ed copies of all of them |
| 4177 | ** before any of them can be written out to the database file. |
| 4178 | */ |
| 4179 | if( needSync ){ |
| 4180 | for(ii=0; ii<nPage && needSync; ii++){ |
| 4181 | PgHdr *pPage = pager_lookup(pPager, pg1+ii); |
| 4182 | if( pPage ) pPage->needSync = 1; |
| 4183 | } |
| 4184 | assert(pPager->needSync); |
| 4185 | } |
| 4186 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4187 | assert( pPager->doNotSync==1 ); |
| 4188 | pPager->doNotSync = 0; |
| 4189 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4190 | rc = pager_write(pDbPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4191 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4192 | pagerLeave(pPager); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4193 | return rc; |
| 4194 | } |
| 4195 | |
| 4196 | /* |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 4197 | ** Return TRUE if the page given in the argument was previously passed |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4198 | ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4199 | ** to change the content of the page. |
| 4200 | */ |
danielk1977 | 7d3a666 | 2006-01-23 16:21:05 +0000 | [diff] [blame] | 4201 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4202 | int sqlite3PagerIswriteable(DbPage *pPg){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4203 | return pPg->dirty; |
| 4204 | } |
danielk1977 | 7d3a666 | 2006-01-23 16:21:05 +0000 | [diff] [blame] | 4205 | #endif |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4206 | |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 4207 | #ifndef SQLITE_OMIT_VACUUM |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4208 | /* |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4209 | ** Replace the content of a single page with the information in the third |
| 4210 | ** argument. |
| 4211 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4212 | int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){ |
| 4213 | PgHdr *pPg; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4214 | int rc; |
| 4215 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4216 | pagerEnter(pPager); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4217 | rc = sqlite3PagerGet(pPager, pgno, &pPg); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4218 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4219 | rc = sqlite3PagerWrite(pPg); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4220 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4221 | memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4222 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4223 | sqlite3PagerUnref(pPg); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4224 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4225 | pagerLeave(pPager); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4226 | return rc; |
| 4227 | } |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 4228 | #endif |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4229 | |
| 4230 | /* |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4231 | ** 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] | 4232 | ** write the information on page pPg back to the disk, even though |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4233 | ** that page might be marked as dirty. |
| 4234 | ** |
| 4235 | ** The overlying software layer calls this routine when all of the data |
| 4236 | ** on the given page is unused. The pager marks the page as clean so |
| 4237 | ** that it does not get written to disk. |
| 4238 | ** |
| 4239 | ** Tests show that this optimization, together with the |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4240 | ** sqlite3PagerDontRollback() below, more than double the speed |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4241 | ** of large INSERT operations and quadruple the speed of large DELETEs. |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4242 | ** |
| 4243 | ** When this routine is called, set the alwaysRollback flag to true. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4244 | ** Subsequent calls to sqlite3PagerDontRollback() for the same page |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4245 | ** will thereafter be ignored. This is necessary to avoid a problem |
| 4246 | ** where a page with data is added to the freelist during one part of |
| 4247 | ** a transaction then removed from the freelist during a later part |
| 4248 | ** of the same transaction and reused for some other purpose. When it |
| 4249 | ** is first added to the freelist, this routine is called. When reused, |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4250 | ** the sqlite3PagerDontRollback() routine is called. But because the |
| 4251 | ** page contains critical data, we still need to be sure it gets |
| 4252 | ** rolled back in spite of the sqlite3PagerDontRollback() call. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4253 | */ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4254 | void sqlite3PagerDontWrite(DbPage *pDbPage){ |
| 4255 | PgHdr *pPg = pDbPage; |
| 4256 | Pager *pPager = pPg->pPager; |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4257 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4258 | if( MEMDB ) return; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4259 | pagerEnter(pPager); |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4260 | pPg->alwaysRollback = 1; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4261 | if( pPg->dirty && !pPager->stmtInUse ){ |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 4262 | assert( pPager->state>=PAGER_SHARED ); |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 4263 | if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){ |
| 4264 | /* If this pages is the last page in the file and the file has grown |
| 4265 | ** during the current transaction, then do NOT mark the page as clean. |
| 4266 | ** When the database file grows, we must make sure that the last page |
| 4267 | ** gets written at least once so that the disk file will be the correct |
| 4268 | ** size. If you do not write this page and the size of the file |
| 4269 | ** on the disk ends up being too small, that can lead to database |
| 4270 | ** corruption during the next transaction. |
| 4271 | */ |
| 4272 | }else{ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4273 | PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)); |
| 4274 | IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4275 | makeClean(pPg); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 4276 | #ifdef SQLITE_CHECK_PAGES |
| 4277 | pPg->pageHash = pager_pagehash(pPg); |
| 4278 | #endif |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 4279 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4280 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4281 | pagerLeave(pPager); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | /* |
| 4285 | ** A call to this routine tells the pager that if a rollback occurs, |
| 4286 | ** it is not necessary to restore the data on the given page. This |
| 4287 | ** means that the pager does not have to record the given page in the |
| 4288 | ** rollback journal. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4289 | ** |
| 4290 | ** If we have not yet actually read the content of this page (if |
| 4291 | ** the PgHdr.needRead flag is set) then this routine acts as a promise |
| 4292 | ** that we will never need to read the page content in the future. |
| 4293 | ** so the needRead flag can be cleared at this point. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4294 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4295 | void sqlite3PagerDontRollback(DbPage *pPg){ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4296 | Pager *pPager = pPg->pPager; |
| 4297 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4298 | pagerEnter(pPager); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4299 | assert( pPager->state>=PAGER_RESERVED ); |
| 4300 | if( pPager->journalOpen==0 ) return; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4301 | if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4302 | if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){ |
| 4303 | assert( pPager->aInJournal!=0 ); |
| 4304 | pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
| 4305 | pPg->inJournal = 1; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4306 | pPg->needRead = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4307 | if( pPager->stmtInUse ){ |
| 4308 | pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4309 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4310 | PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 4311 | IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno)) |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4312 | } |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4313 | if( pPager->stmtInUse |
| 4314 | && !pageInStatement(pPg) |
| 4315 | && (int)pPg->pgno<=pPager->stmtSize |
| 4316 | ){ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4317 | assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4318 | assert( pPager->aInStmt!=0 ); |
| 4319 | pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4320 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4321 | pagerLeave(pPager); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4322 | } |
| 4323 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4324 | |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4325 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4326 | ** This routine is called to increment the database file change-counter, |
| 4327 | ** stored at byte 24 of the pager file. |
| 4328 | */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4329 | static int pager_incr_changecounter(Pager *pPager, int isDirect){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4330 | PgHdr *pPgHdr; |
| 4331 | u32 change_counter; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4332 | int rc = SQLITE_OK; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4333 | |
| 4334 | if( !pPager->changeCountDone ){ |
| 4335 | /* Open page 1 of the file for writing. */ |
| 4336 | rc = sqlite3PagerGet(pPager, 1, &pPgHdr); |
| 4337 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4338 | |
| 4339 | if( !isDirect ){ |
| 4340 | rc = sqlite3PagerWrite(pPgHdr); |
| 4341 | if( rc!=SQLITE_OK ) return rc; |
| 4342 | } |
| 4343 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4344 | /* Increment the value just read and write it back to byte 24. */ |
drh | b100391 | 2007-07-20 00:33:36 +0000 | [diff] [blame] | 4345 | change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4346 | change_counter++; |
| 4347 | put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4348 | |
| 4349 | if( isDirect && pPager->fd->pMethods ){ |
| 4350 | const void *zBuf = PGHDR_TO_DATA(pPgHdr); |
| 4351 | rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); |
| 4352 | } |
| 4353 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4354 | /* Release the page reference. */ |
| 4355 | sqlite3PagerUnref(pPgHdr); |
| 4356 | pPager->changeCountDone = 1; |
| 4357 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4358 | return rc; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | /* |
| 4362 | ** Sync the database file for the pager pPager. zMaster points to the name |
| 4363 | ** of a master journal file that should be written into the individual |
| 4364 | ** journal file. zMaster may be NULL, which is interpreted as no master |
| 4365 | ** journal (a single database transaction). |
| 4366 | ** |
| 4367 | ** This routine ensures that the journal is synced, all dirty pages written |
| 4368 | ** to the database file and the database file synced. The only thing that |
| 4369 | ** remains to commit the transaction is to delete the journal file (or |
| 4370 | ** master journal file if specified). |
| 4371 | ** |
| 4372 | ** Note that if zMaster==NULL, this does not overwrite a previous value |
| 4373 | ** passed to an sqlite3PagerCommitPhaseOne() call. |
| 4374 | ** |
| 4375 | ** If parameter nTrunc is non-zero, then the pager file is truncated to |
| 4376 | ** nTrunc pages (this is used by auto-vacuum databases). |
| 4377 | */ |
| 4378 | int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){ |
| 4379 | int rc = SQLITE_OK; |
| 4380 | |
| 4381 | PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n", |
| 4382 | pPager->zFilename, zMaster, nTrunc); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4383 | pagerEnter(pPager); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4384 | |
| 4385 | /* If this is an in-memory db, or no pages have been written to, or this |
| 4386 | ** function has already been called, it is a no-op. |
| 4387 | */ |
| 4388 | if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){ |
| 4389 | PgHdr *pPg; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4390 | |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4391 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 4392 | /* The atomic-write optimization can be used if all of the |
| 4393 | ** following are true: |
| 4394 | ** |
| 4395 | ** + The file-system supports the atomic-write property for |
| 4396 | ** blocks of size page-size, and |
| 4397 | ** + This commit is not part of a multi-file transaction, and |
| 4398 | ** + Exactly one page has been modified and store in the journal file. |
| 4399 | ** |
| 4400 | ** If the optimization can be used, then the journal file will never |
| 4401 | ** be created for this transaction. |
| 4402 | */ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4403 | int useAtomicWrite = ( |
| 4404 | !zMaster && |
| 4405 | pPager->journalOff==jrnlBufferSize(pPager) && |
| 4406 | nTrunc==0 && |
| 4407 | (0==pPager->pDirty || 0==pPager->pDirty->pDirty) |
| 4408 | ); |
| 4409 | if( useAtomicWrite ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4410 | /* Update the nRec field in the journal file. */ |
| 4411 | int offset = pPager->journalHdr + sizeof(aJournalMagic); |
| 4412 | assert(pPager->nRec==1); |
| 4413 | rc = write32bits(pPager->jfd, offset, pPager->nRec); |
| 4414 | |
| 4415 | /* Update the db file change counter. The following call will modify |
| 4416 | ** the in-memory representation of page 1 to include the updated |
| 4417 | ** change counter and then write page 1 directly to the database |
| 4418 | ** file. Because of the atomic-write property of the host file-system, |
| 4419 | ** this is safe. |
| 4420 | */ |
| 4421 | rc = pager_incr_changecounter(pPager, 1); |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4422 | }else{ |
| 4423 | rc = sqlite3JournalCreate(pPager->jfd); |
| 4424 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4425 | } |
| 4426 | |
| 4427 | if( !useAtomicWrite ) |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4428 | #endif |
| 4429 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4430 | /* If a master journal file name has already been written to the |
| 4431 | ** journal file, then no sync is required. This happens when it is |
| 4432 | ** written, then the process fails to upgrade from a RESERVED to an |
| 4433 | ** EXCLUSIVE lock. The next time the process tries to commit the |
| 4434 | ** transaction the m-j name will have already been written. |
| 4435 | */ |
| 4436 | if( !pPager->setMaster ){ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4437 | assert( pPager->journalOpen ); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4438 | rc = pager_incr_changecounter(pPager, 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4439 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4440 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4441 | if( nTrunc!=0 ){ |
| 4442 | /* If this transaction has made the database smaller, then all pages |
| 4443 | ** being discarded by the truncation must be written to the journal |
| 4444 | ** file. |
| 4445 | */ |
| 4446 | Pgno i; |
| 4447 | int iSkip = PAGER_MJ_PGNO(pPager); |
| 4448 | for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){ |
| 4449 | if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){ |
| 4450 | rc = sqlite3PagerGet(pPager, i, &pPg); |
| 4451 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4452 | rc = sqlite3PagerWrite(pPg); |
| 4453 | sqlite3PagerUnref(pPg); |
| 4454 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4455 | } |
| 4456 | } |
| 4457 | } |
| 4458 | #endif |
| 4459 | rc = writeMasterJournal(pPager, zMaster); |
| 4460 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4461 | rc = syncJournal(pPager); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4462 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4463 | if( rc!=SQLITE_OK ) goto sync_exit; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4464 | |
| 4465 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4466 | if( nTrunc!=0 ){ |
| 4467 | rc = sqlite3PagerTruncate(pPager, nTrunc); |
| 4468 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4469 | } |
| 4470 | #endif |
| 4471 | |
| 4472 | /* Write all dirty pages to the database file */ |
| 4473 | pPg = pager_get_all_dirty_pages(pPager); |
| 4474 | rc = pager_write_pagelist(pPg); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4475 | if( rc!=SQLITE_OK ){ |
| 4476 | while( pPg && !pPg->dirty ){ pPg = pPg->pDirty; } |
| 4477 | pPager->pDirty = pPg; |
| 4478 | goto sync_exit; |
| 4479 | } |
drh | 3cdd7d3 | 2007-03-30 14:46:01 +0000 | [diff] [blame] | 4480 | pPager->pDirty = 0; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4481 | |
| 4482 | /* Sync the database file. */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 4483 | if( !pPager->noSync ){ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 4484 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4485 | } |
| 4486 | IOTRACE(("DBSYNC %p\n", pPager)) |
| 4487 | |
| 4488 | pPager->state = PAGER_SYNCED; |
| 4489 | }else if( MEMDB && nTrunc!=0 ){ |
| 4490 | rc = sqlite3PagerTruncate(pPager, nTrunc); |
| 4491 | } |
| 4492 | |
| 4493 | sync_exit: |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 4494 | if( rc==SQLITE_IOERR_BLOCKED ){ |
| 4495 | /* pager_incr_changecounter() may attempt to obtain an exclusive |
| 4496 | * lock to spill the cache and return IOERR_BLOCKED. But since |
| 4497 | * there is no chance the cache is inconsistent, it's |
| 4498 | * better to return SQLITE_BUSY. |
| 4499 | */ |
| 4500 | rc = SQLITE_BUSY; |
| 4501 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4502 | pagerLeave(pPager); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4503 | return rc; |
| 4504 | } |
| 4505 | |
| 4506 | |
| 4507 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4508 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4509 | ** |
| 4510 | ** If the commit fails for any reason, a rollback attempt is made |
| 4511 | ** and an error code is returned. If the commit worked, SQLITE_OK |
| 4512 | ** is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4513 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4514 | int sqlite3PagerCommitPhaseTwo(Pager *pPager){ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 4515 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4516 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4517 | |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4518 | if( pPager->errCode ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 4519 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4520 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4521 | if( pPager->state<PAGER_RESERVED ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4522 | return SQLITE_ERROR; |
| 4523 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4524 | pagerEnter(pPager); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4525 | PAGERTRACE2("COMMIT %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4526 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4527 | pPg = pager_get_all_dirty_pages(pPager); |
| 4528 | while( pPg ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4529 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 4530 | clearHistory(pHist); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4531 | pPg->dirty = 0; |
| 4532 | pPg->inJournal = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4533 | pHist->inStmt = 0; |
drh | 5229ae4 | 2006-03-23 23:29:04 +0000 | [diff] [blame] | 4534 | pPg->needSync = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4535 | pHist->pPrevStmt = pHist->pNextStmt = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4536 | pPg = pPg->pDirty; |
| 4537 | } |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4538 | pPager->pDirty = 0; |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 4539 | #ifndef NDEBUG |
| 4540 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 4541 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 4542 | assert( !pPg->alwaysRollback ); |
| 4543 | assert( !pHist->pOrig ); |
| 4544 | assert( !pHist->pStmt ); |
| 4545 | } |
| 4546 | #endif |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4547 | pPager->pStmt = 0; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4548 | pPager->state = PAGER_SHARED; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4549 | return SQLITE_OK; |
| 4550 | } |
drh | 3cdd7d3 | 2007-03-30 14:46:01 +0000 | [diff] [blame] | 4551 | assert( pPager->journalOpen || !pPager->dirtyCache ); |
| 4552 | assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache ); |
| 4553 | rc = pager_end_transaction(pPager); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4554 | rc = pager_error(pPager, rc); |
| 4555 | pagerLeave(pPager); |
| 4556 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4560 | ** Rollback all changes. The database falls back to PAGER_SHARED mode. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4561 | ** All in-memory cache pages revert to their original data contents. |
| 4562 | ** The journal is deleted. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4563 | ** |
| 4564 | ** This routine cannot fail unless some other process is not following |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 4565 | ** the correct locking protocol or unless some other |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4566 | ** process is writing trash into the journal file (SQLITE_CORRUPT) or |
| 4567 | ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error |
| 4568 | ** codes are returned for all these occasions. Otherwise, |
| 4569 | ** SQLITE_OK is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4570 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4571 | int sqlite3PagerRollback(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4572 | int rc; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4573 | PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4574 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4575 | PgHdr *p; |
| 4576 | for(p=pPager->pAll; p; p=p->pNextAll){ |
| 4577 | PgHistory *pHist; |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 4578 | assert( !p->alwaysRollback ); |
| 4579 | if( !p->dirty ){ |
| 4580 | assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig ); |
| 4581 | assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt ); |
| 4582 | continue; |
| 4583 | } |
| 4584 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4585 | pHist = PGHDR_TO_HIST(p, pPager); |
| 4586 | if( pHist->pOrig ){ |
| 4587 | memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4588 | PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager)); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4589 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4590 | PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager)); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4591 | } |
| 4592 | clearHistory(pHist); |
| 4593 | p->dirty = 0; |
| 4594 | p->inJournal = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4595 | pHist->inStmt = 0; |
| 4596 | pHist->pPrevStmt = pHist->pNextStmt = 0; |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 4597 | if( pPager->xReiniter ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4598 | pPager->xReiniter(p, pPager->pageSize); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 4599 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4600 | } |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4601 | pPager->pDirty = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4602 | pPager->pStmt = 0; |
| 4603 | pPager->dbSize = pPager->origDbSize; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 4604 | pager_truncate_cache(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4605 | pPager->stmtInUse = 0; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4606 | pPager->state = PAGER_SHARED; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4607 | return SQLITE_OK; |
| 4608 | } |
| 4609 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4610 | pagerEnter(pPager); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4611 | if( !pPager->dirtyCache || !pPager->journalOpen ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4612 | rc = pager_end_transaction(pPager); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4613 | pagerLeave(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4614 | return rc; |
| 4615 | } |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 4616 | |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4617 | if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4618 | if( pPager->state>=PAGER_EXCLUSIVE ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 4619 | pager_playback(pPager, 0); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4620 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4621 | pagerLeave(pPager); |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4622 | return pPager->errCode; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4623 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4624 | if( pPager->state==PAGER_RESERVED ){ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 4625 | int rc2; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 4626 | rc = pager_playback(pPager, 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4627 | rc2 = pager_end_transaction(pPager); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4628 | if( rc==SQLITE_OK ){ |
| 4629 | rc = rc2; |
| 4630 | } |
| 4631 | }else{ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 4632 | rc = pager_playback(pPager, 0); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4633 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 4634 | /* pager_reset(pPager); */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4635 | pPager->dbSize = -1; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4636 | |
| 4637 | /* If an error occurs during a ROLLBACK, we can no longer trust the pager |
| 4638 | ** cache. So call pager_error() on the way out to make any error |
| 4639 | ** persistent. |
| 4640 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4641 | rc = pager_error(pPager, rc); |
| 4642 | pagerLeave(pPager); |
| 4643 | return rc; |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 4644 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4645 | |
| 4646 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4647 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 4648 | ** if the database is (in theory) writable. |
| 4649 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4650 | int sqlite3PagerIsreadonly(Pager *pPager){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 4651 | return pPager->readOnly; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | /* |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4655 | ** Return the number of references to the pager. |
| 4656 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4657 | int sqlite3PagerRefcount(Pager *pPager){ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4658 | return pPager->nRef; |
| 4659 | } |
| 4660 | |
| 4661 | #ifdef SQLITE_TEST |
| 4662 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4663 | ** This routine is used for testing and analysis only. |
| 4664 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4665 | int *sqlite3PagerStats(Pager *pPager){ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 4666 | static int a[11]; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4667 | a[0] = pPager->nRef; |
| 4668 | a[1] = pPager->nPage; |
| 4669 | a[2] = pPager->mxPage; |
| 4670 | a[3] = pPager->dbSize; |
| 4671 | a[4] = pPager->state; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4672 | a[5] = pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4673 | a[6] = pPager->nHit; |
| 4674 | a[7] = pPager->nMiss; |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4675 | a[8] = 0; /* Used to be pPager->nOvfl */ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 4676 | a[9] = pPager->nRead; |
| 4677 | a[10] = pPager->nWrite; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4678 | return a; |
| 4679 | } |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4680 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4681 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4682 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4683 | ** Set the statement rollback point. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4684 | ** |
| 4685 | ** This routine should be called with the transaction journal already |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4686 | ** open. A new statement journal is created that can be used to rollback |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4687 | ** changes of a single SQL command within a larger transaction. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4688 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4689 | static int pagerStmtBegin(Pager *pPager){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4690 | int rc; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4691 | assert( !pPager->stmtInUse ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 4692 | assert( pPager->state>=PAGER_SHARED ); |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 4693 | assert( pPager->dbSize>=0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4694 | PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4695 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4696 | pPager->stmtInUse = 1; |
| 4697 | pPager->stmtSize = pPager->dbSize; |
| 4698 | return SQLITE_OK; |
| 4699 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4700 | if( !pPager->journalOpen ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4701 | pPager->stmtAutoopen = 1; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4702 | return SQLITE_OK; |
| 4703 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4704 | assert( pPager->journalOpen ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 4705 | pagerLeave(pPager); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4706 | pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 4707 | pagerEnter(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4708 | if( pPager->aInStmt==0 ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4709 | /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4710 | return SQLITE_NOMEM; |
| 4711 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 4712 | #ifndef NDEBUG |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4713 | rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4714 | if( rc ) goto stmt_begin_failed; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 4715 | assert( pPager->stmtJSize == pPager->journalOff ); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 4716 | #endif |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 4717 | pPager->stmtJSize = pPager->journalOff; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4718 | pPager->stmtSize = pPager->dbSize; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 4719 | pPager->stmtHdrOff = 0; |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 4720 | pPager->stmtCksum = pPager->cksumInit; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4721 | if( !pPager->stmtOpen ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4722 | rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, 0); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4723 | if( rc ) goto stmt_begin_failed; |
| 4724 | pPager->stmtOpen = 1; |
| 4725 | pPager->stmtNRec = 0; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 4726 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4727 | pPager->stmtInUse = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4728 | return SQLITE_OK; |
| 4729 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4730 | stmt_begin_failed: |
| 4731 | if( pPager->aInStmt ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4732 | sqlite3_free(pPager->aInStmt); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4733 | pPager->aInStmt = 0; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4734 | } |
| 4735 | return rc; |
| 4736 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4737 | int sqlite3PagerStmtBegin(Pager *pPager){ |
| 4738 | int rc; |
| 4739 | pagerEnter(pPager); |
| 4740 | rc = pagerStmtBegin(pPager); |
| 4741 | pagerLeave(pPager); |
| 4742 | return rc; |
| 4743 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4744 | |
| 4745 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4746 | ** Commit a statement. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4747 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4748 | int sqlite3PagerStmtCommit(Pager *pPager){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4749 | pagerEnter(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4750 | if( pPager->stmtInUse ){ |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 4751 | PgHdr *pPg, *pNext; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4752 | PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4753 | if( !MEMDB ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4754 | /* sqlite3OsTruncate(pPager->stfd, 0); */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4755 | sqlite3_free( pPager->aInStmt ); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4756 | pPager->aInStmt = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4757 | }else{ |
| 4758 | for(pPg=pPager->pStmt; pPg; pPg=pNext){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4759 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4760 | pNext = pHist->pNextStmt; |
| 4761 | assert( pHist->inStmt ); |
| 4762 | pHist->inStmt = 0; |
| 4763 | pHist->pPrevStmt = pHist->pNextStmt = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4764 | sqlite3_free(pHist->pStmt); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4765 | pHist->pStmt = 0; |
| 4766 | } |
| 4767 | } |
| 4768 | pPager->stmtNRec = 0; |
| 4769 | pPager->stmtInUse = 0; |
| 4770 | pPager->pStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 4771 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4772 | pPager->stmtAutoopen = 0; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4773 | pagerLeave(pPager); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4774 | return SQLITE_OK; |
| 4775 | } |
| 4776 | |
| 4777 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4778 | ** Rollback a statement. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4779 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4780 | int sqlite3PagerStmtRollback(Pager *pPager){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4781 | int rc; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4782 | pagerEnter(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4783 | if( pPager->stmtInUse ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4784 | PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4785 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4786 | PgHdr *pPg; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4787 | PgHistory *pHist; |
| 4788 | for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){ |
| 4789 | pHist = PGHDR_TO_HIST(pPg, pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4790 | if( pHist->pStmt ){ |
| 4791 | memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4792 | sqlite3_free(pHist->pStmt); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4793 | pHist->pStmt = 0; |
| 4794 | } |
| 4795 | } |
| 4796 | pPager->dbSize = pPager->stmtSize; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 4797 | pager_truncate_cache(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4798 | rc = SQLITE_OK; |
| 4799 | }else{ |
| 4800 | rc = pager_stmt_playback(pPager); |
| 4801 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4802 | sqlite3PagerStmtCommit(pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 4803 | }else{ |
| 4804 | rc = SQLITE_OK; |
| 4805 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4806 | pPager->stmtAutoopen = 0; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4807 | pagerLeave(pPager); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4808 | return rc; |
| 4809 | } |
| 4810 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4811 | /* |
| 4812 | ** Return the full pathname of the database file. |
| 4813 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4814 | const char *sqlite3PagerFilename(Pager *pPager){ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4815 | return pPager->zFilename; |
| 4816 | } |
| 4817 | |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4818 | /* |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 4819 | ** Return the VFS structure for the pager. |
| 4820 | */ |
| 4821 | const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ |
| 4822 | return pPager->pVfs; |
| 4823 | } |
| 4824 | |
| 4825 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4826 | ** Return the directory of the database file. |
| 4827 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4828 | const char *sqlite3PagerDirname(Pager *pPager){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4829 | return pPager->zDirectory; |
| 4830 | } |
| 4831 | |
| 4832 | /* |
| 4833 | ** Return the full pathname of the journal file. |
| 4834 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4835 | const char *sqlite3PagerJournalname(Pager *pPager){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4836 | return pPager->zJournal; |
| 4837 | } |
| 4838 | |
| 4839 | /* |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4840 | ** Return true if fsync() calls are disabled for this pager. Return FALSE |
| 4841 | ** if fsync()s are executed normally. |
| 4842 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4843 | int sqlite3PagerNosync(Pager *pPager){ |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4844 | return pPager->noSync; |
| 4845 | } |
| 4846 | |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4847 | #ifdef SQLITE_HAS_CODEC |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4848 | /* |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4849 | ** Set the codec for this pager |
| 4850 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4851 | void sqlite3PagerSetCodec( |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4852 | Pager *pPager, |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4853 | void *(*xCodec)(void*,void*,Pgno,int), |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4854 | void *pCodecArg |
| 4855 | ){ |
| 4856 | pPager->xCodec = xCodec; |
| 4857 | pPager->pCodecArg = pCodecArg; |
| 4858 | } |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4859 | #endif |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4860 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4861 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4862 | /* |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4863 | ** Move the page pPg to location pgno in the file. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4864 | ** |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4865 | ** There must be no references to the page previously located at |
| 4866 | ** pgno (which we call pPgOld) though that page is allowed to be |
| 4867 | ** in cache. If the page previous located at pgno is not already |
| 4868 | ** in the rollback journal, it is not put there by by this routine. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4869 | ** |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4870 | ** References to the page pPg remain valid. Updating any |
| 4871 | ** meta-data associated with pPg (i.e. data stored in the nExtra bytes |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4872 | ** allocated along with the page) is the responsibility of the caller. |
| 4873 | ** |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 4874 | ** A transaction must be active when this routine is called. It used to be |
| 4875 | ** required that a statement transaction was not active, but this restriction |
| 4876 | ** has been removed (CREATE INDEX needs to move a page when a statement |
| 4877 | ** transaction is active). |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4878 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4879 | int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){ |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4880 | PgHdr *pPgOld; /* The page being overwritten. */ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4881 | int h; |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4882 | Pgno needSyncPgno = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4883 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4884 | pagerEnter(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4885 | assert( pPg->nRef>0 ); |
| 4886 | |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4887 | PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n", |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4888 | PAGERID(pPager), pPg->pgno, pPg->needSync, pgno); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 4889 | IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno)) |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 4890 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 4891 | pager_get_content(pPg); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4892 | if( pPg->needSync ){ |
| 4893 | needSyncPgno = pPg->pgno; |
drh | 4f0aee4 | 2007-06-16 18:39:41 +0000 | [diff] [blame] | 4894 | assert( pPg->inJournal || (int)pgno>pPager->origDbSize ); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4895 | assert( pPg->dirty ); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4896 | assert( pPager->needSync ); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4899 | /* Unlink pPg from it's hash-chain */ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4900 | unlinkHashChain(pPager, pPg); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4901 | |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 4902 | /* If the cache contains a page with page-number pgno, remove it |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4903 | ** from it's hash chain. Also, if the PgHdr.needSync was set for |
| 4904 | ** page pgno before the 'move' operation, it needs to be retained |
| 4905 | ** for the page moved there. |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4906 | */ |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4907 | pPg->needSync = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4908 | pPgOld = pager_lookup(pPager, pgno); |
| 4909 | if( pPgOld ){ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4910 | assert( pPgOld->nRef==0 ); |
| 4911 | unlinkHashChain(pPager, pPgOld); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4912 | makeClean(pPgOld); |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4913 | pPg->needSync = pPgOld->needSync; |
| 4914 | }else{ |
| 4915 | pPg->needSync = 0; |
| 4916 | } |
| 4917 | if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){ |
| 4918 | pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4919 | }else{ |
| 4920 | pPg->inJournal = 0; |
drh | 732c817 | 2007-06-16 11:17:45 +0000 | [diff] [blame] | 4921 | assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4922 | } |
| 4923 | |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4924 | /* 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] | 4925 | assert( pgno!=0 ); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4926 | pPg->pgno = pgno; |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 4927 | h = pgno & (pPager->nHash-1); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 4928 | if( pPager->aHash[h] ){ |
| 4929 | assert( pPager->aHash[h]->pPrevHash==0 ); |
| 4930 | pPager->aHash[h]->pPrevHash = pPg; |
| 4931 | } |
| 4932 | pPg->pNextHash = pPager->aHash[h]; |
| 4933 | pPager->aHash[h] = pPg; |
| 4934 | pPg->pPrevHash = 0; |
| 4935 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4936 | makeDirty(pPg); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4937 | pPager->dirtyCache = 1; |
| 4938 | |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4939 | if( needSyncPgno ){ |
| 4940 | /* If needSyncPgno is non-zero, then the journal file needs to be |
| 4941 | ** sync()ed before any data is written to database file page needSyncPgno. |
| 4942 | ** Currently, no such page exists in the page-cache and the |
| 4943 | ** Pager.aInJournal bit has been set. This needs to be remedied by loading |
| 4944 | ** the page into the pager-cache and setting the PgHdr.needSync flag. |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4945 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4946 | ** The sqlite3PagerGet() call may cause the journal to sync. So make |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4947 | ** sure the Pager.needSync flag is set too. |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4948 | */ |
| 4949 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4950 | PgHdr *pPgHdr; |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4951 | assert( pPager->needSync ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4952 | rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4953 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4954 | pPager->needSync = 1; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4955 | pPgHdr->needSync = 1; |
| 4956 | pPgHdr->inJournal = 1; |
| 4957 | makeDirty(pPgHdr); |
| 4958 | sqlite3PagerUnref(pPgHdr); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 4959 | } |
| 4960 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4961 | pagerLeave(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4962 | return SQLITE_OK; |
| 4963 | } |
| 4964 | #endif |
| 4965 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4966 | /* |
| 4967 | ** Return a pointer to the data for the specified page. |
| 4968 | */ |
| 4969 | void *sqlite3PagerGetData(DbPage *pPg){ |
| 4970 | return PGHDR_TO_DATA(pPg); |
| 4971 | } |
| 4972 | |
| 4973 | /* |
| 4974 | ** Return a pointer to the Pager.nExtra bytes of "extra" space |
| 4975 | ** allocated along with the specified page. |
| 4976 | */ |
| 4977 | void *sqlite3PagerGetExtra(DbPage *pPg){ |
| 4978 | Pager *pPager = pPg->pPager; |
| 4979 | return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0); |
| 4980 | } |
| 4981 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 4982 | /* |
| 4983 | ** Get/set the locking-mode for this pager. Parameter eMode must be one |
| 4984 | ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or |
| 4985 | ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then |
| 4986 | ** the locking-mode is set to the value specified. |
| 4987 | ** |
| 4988 | ** The returned value is either PAGER_LOCKINGMODE_NORMAL or |
| 4989 | ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated) |
| 4990 | ** locking-mode. |
| 4991 | */ |
| 4992 | int sqlite3PagerLockingMode(Pager *pPager, int eMode){ |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 4993 | assert( eMode==PAGER_LOCKINGMODE_QUERY |
| 4994 | || eMode==PAGER_LOCKINGMODE_NORMAL |
| 4995 | || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 4996 | assert( PAGER_LOCKINGMODE_QUERY<0 ); |
| 4997 | assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 ); |
| 4998 | if( eMode>=0 && !pPager->tempFile ){ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 4999 | pPager->exclusiveMode = eMode; |
| 5000 | } |
| 5001 | return (int)pPager->exclusiveMode; |
| 5002 | } |
| 5003 | |
dougcurrie | 81c95ef | 2004-06-18 23:21:47 +0000 | [diff] [blame] | 5004 | #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 5005 | /* |
| 5006 | ** Return the current state of the file lock for the given pager. |
| 5007 | ** The return value is one of NO_LOCK, SHARED_LOCK, RESERVED_LOCK, |
| 5008 | ** PENDING_LOCK, or EXCLUSIVE_LOCK. |
| 5009 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5010 | int sqlite3PagerLockstate(Pager *pPager){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5011 | return sqlite3OsLockState(pPager->fd); |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 5012 | } |
| 5013 | #endif |
| 5014 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 5015 | #ifdef SQLITE_DEBUG |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5016 | /* |
| 5017 | ** Print a listing of all referenced pages and their ref count. |
| 5018 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5019 | void sqlite3PagerRefdump(Pager *pPager){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5020 | PgHdr *pPg; |
| 5021 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 5022 | if( pPg->nRef<=0 ) continue; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5023 | sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n", |
| 5024 | pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef); |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5025 | } |
| 5026 | } |
| 5027 | #endif |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 5028 | |
| 5029 | #endif /* SQLITE_OMIT_DISKIO */ |