drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 12 | ** This is the implementation of the page cache subsystem or "pager". |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 13 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 14 | ** The pager is used to access a database disk file. It implements |
| 15 | ** atomic commit and rollback through the use of a journal file that |
| 16 | ** is separate from the database file. The pager also implements file |
| 17 | ** locking to prevent two processes from writing the same database |
| 18 | ** file simultaneously, or one process from reading the database while |
| 19 | ** another is writing. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 20 | ** |
drh | c81945e | 2008-03-10 14:12:53 +0000 | [diff] [blame^] | 21 | ** @(#) $Id: pager.c,v 1.415 2008/03/10 14:12:53 drh Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 22 | */ |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 23 | #ifndef SQLITE_OMIT_DISKIO |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 24 | #include "sqliteInt.h" |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 25 | #include <assert.h> |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 26 | #include <string.h> |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 29 | ** Macros for troubleshooting. Normally turned off |
| 30 | */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 31 | #if 0 |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 32 | #define sqlite3DebugPrintf printf |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 33 | #define PAGERTRACE1(X) sqlite3DebugPrintf(X) |
| 34 | #define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y) |
| 35 | #define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z) |
| 36 | #define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W) |
| 37 | #define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 38 | #else |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 39 | #define PAGERTRACE1(X) |
| 40 | #define PAGERTRACE2(X,Y) |
| 41 | #define PAGERTRACE3(X,Y,Z) |
| 42 | #define PAGERTRACE4(X,Y,Z,W) |
| 43 | #define PAGERTRACE5(X,Y,Z,W,V) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 46 | /* |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 47 | ** The following two macros are used within the PAGERTRACEX() macros above |
drh | d86959f | 2005-11-26 03:51:18 +0000 | [diff] [blame] | 48 | ** to print out file-descriptors. |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 49 | ** |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 50 | ** PAGERID() takes a pointer to a Pager struct as its argument. The |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 51 | ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 52 | ** struct as its argument. |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 53 | */ |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 54 | #define PAGERID(p) ((int)(p->fd)) |
| 55 | #define FILEHANDLEID(fd) ((int)fd) |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 56 | |
| 57 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 58 | ** The page cache as a whole is always in one of the following |
| 59 | ** states: |
| 60 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 61 | ** PAGER_UNLOCK The page cache is not currently reading or |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 62 | ** writing the database file. There is no |
| 63 | ** data held in memory. This is the initial |
| 64 | ** state. |
| 65 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 66 | ** PAGER_SHARED The page cache is reading the database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 67 | ** Writing is not permitted. There can be |
| 68 | ** multiple readers accessing the same database |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 69 | ** file at the same time. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 70 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 71 | ** PAGER_RESERVED This process has reserved the database for writing |
| 72 | ** but has not yet made any changes. Only one process |
| 73 | ** at a time can reserve the database. The original |
| 74 | ** database file has not been modified so other |
| 75 | ** processes may still be reading the on-disk |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 76 | ** database file. |
| 77 | ** |
| 78 | ** PAGER_EXCLUSIVE The page cache is writing the database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 79 | ** Access is exclusive. No other processes or |
| 80 | ** threads can be reading or writing while one |
| 81 | ** process is writing. |
| 82 | ** |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 83 | ** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE |
| 84 | ** after all dirty pages have been written to the |
| 85 | ** database file and the file has been synced to |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 86 | ** disk. All that remains to do is to remove or |
| 87 | ** truncate the journal file and the transaction |
| 88 | ** will be committed. |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 89 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 90 | ** The page cache comes up in PAGER_UNLOCK. The first time a |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 91 | ** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 92 | ** After all pages have been released using sqlite_page_unref(), |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 93 | ** the state transitions back to PAGER_UNLOCK. The first time |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 94 | ** that sqlite3PagerWrite() is called, the state transitions to |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 95 | ** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 96 | ** called on an outstanding page which means that the pager must |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 97 | ** be in PAGER_SHARED before it transitions to PAGER_RESERVED.) |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 98 | ** PAGER_RESERVED means that there is an open rollback journal. |
| 99 | ** The transition to PAGER_EXCLUSIVE occurs before any changes |
| 100 | ** are made to the database file, though writes to the rollback |
| 101 | ** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback() |
| 102 | ** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED, |
| 103 | ** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 104 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 105 | #define PAGER_UNLOCK 0 |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 106 | #define PAGER_SHARED 1 /* same as SHARED_LOCK */ |
| 107 | #define PAGER_RESERVED 2 /* same as RESERVED_LOCK */ |
| 108 | #define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */ |
| 109 | #define PAGER_SYNCED 5 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 110 | |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 111 | /* |
| 112 | ** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time, |
| 113 | ** then failed attempts to get a reserved lock will invoke the busy callback. |
| 114 | ** This is off by default. To see why, consider the following scenario: |
| 115 | ** |
| 116 | ** Suppose thread A already has a shared lock and wants a reserved lock. |
| 117 | ** Thread B already has a reserved lock and wants an exclusive lock. If |
| 118 | ** both threads are using their busy callbacks, it might be a long time |
| 119 | ** be for one of the threads give up and allows the other to proceed. |
| 120 | ** But if the thread trying to get the reserved lock gives up quickly |
| 121 | ** (if it never invokes its busy callback) then the contention will be |
| 122 | ** resolved quickly. |
| 123 | */ |
| 124 | #ifndef SQLITE_BUSY_RESERVED_LOCK |
| 125 | # define SQLITE_BUSY_RESERVED_LOCK 0 |
| 126 | #endif |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 127 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 128 | /* |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 129 | ** This macro rounds values up so that if the value is an address it |
| 130 | ** is guaranteed to be an address that is aligned to an 8-byte boundary. |
| 131 | */ |
| 132 | #define FORCE_ALIGNMENT(X) (((X)+7)&~7) |
| 133 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 134 | typedef struct PgHdr PgHdr; |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | ** Each pager stores all currently unreferenced pages in a list sorted |
| 138 | ** in least-recently-used (LRU) order (i.e. the first item on the list has |
| 139 | ** not been referenced in a long time, the last item has been recently |
| 140 | ** used). An instance of this structure is included as part of each |
| 141 | ** pager structure for this purpose (variable Pager.lru). |
| 142 | ** |
| 143 | ** Additionally, if memory-management is enabled, all unreferenced pages |
| 144 | ** are stored in a global LRU list (global variable sqlite3LruPageList). |
| 145 | ** |
| 146 | ** In both cases, the PagerLruList.pFirstSynced variable points to |
| 147 | ** the first page in the corresponding list that does not require an |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 148 | ** fsync() operation before its memory can be reclaimed. If no such |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 149 | ** page exists, PagerLruList.pFirstSynced is set to NULL. |
| 150 | */ |
| 151 | typedef struct PagerLruList PagerLruList; |
| 152 | struct PagerLruList { |
| 153 | PgHdr *pFirst; /* First page in LRU list */ |
| 154 | PgHdr *pLast; /* Last page in LRU list (the most recently used) */ |
| 155 | PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0 */ |
| 156 | }; |
| 157 | |
| 158 | /* |
| 159 | ** The following structure contains the next and previous pointers used |
| 160 | ** to link a PgHdr structure into a PagerLruList linked list. |
| 161 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 162 | typedef struct PagerLruLink PagerLruLink; |
| 163 | struct PagerLruLink { |
| 164 | PgHdr *pNext; |
| 165 | PgHdr *pPrev; |
| 166 | }; |
| 167 | |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 168 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 169 | ** Each in-memory image of a page begins with the following header. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 170 | ** This header is only visible to this pager module. The client |
| 171 | ** code that calls pager sees only the data that follows the header. |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 172 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 173 | ** Client code should call sqlite3PagerWrite() on a page prior to making |
| 174 | ** any modifications to that page. The first time sqlite3PagerWrite() |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 175 | ** is called, the original page contents are written into the rollback |
| 176 | ** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once |
| 177 | ** the journal page has made it onto the disk surface, PgHdr.needSync |
| 178 | ** is cleared. The modified page cannot be written back into the original |
| 179 | ** database file until the journal pages has been synced to disk and the |
| 180 | ** PgHdr.needSync has been cleared. |
| 181 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 182 | ** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 183 | ** is cleared again when the page content is written back to the original |
| 184 | ** database file. |
drh | 732c817 | 2007-06-16 11:17:45 +0000 | [diff] [blame] | 185 | ** |
| 186 | ** Details of important structure elements: |
| 187 | ** |
| 188 | ** needSync |
| 189 | ** |
| 190 | ** If this is true, this means that it is not safe to write the page |
| 191 | ** content to the database because the original content needed |
| 192 | ** for rollback has not by synced to the main rollback journal. |
| 193 | ** The original content may have been written to the rollback journal |
| 194 | ** but it has not yet been synced. So we cannot write to the database |
| 195 | ** file because power failure might cause the page in the journal file |
| 196 | ** to never reach the disk. It is as if the write to the journal file |
| 197 | ** does not occur until the journal file is synced. |
| 198 | ** |
| 199 | ** This flag is false if the page content exactly matches what |
| 200 | ** currently exists in the database file. The needSync flag is also |
| 201 | ** false if the original content has been written to the main rollback |
| 202 | ** journal and synced. If the page represents a new page that has |
| 203 | ** been added onto the end of the database during the current |
| 204 | ** transaction, the needSync flag is true until the original database |
| 205 | ** size in the journal header has been synced to disk. |
| 206 | ** |
| 207 | ** inJournal |
| 208 | ** |
| 209 | ** This is true if the original page has been written into the main |
| 210 | ** rollback journal. This is always false for new pages added to |
| 211 | ** the end of the database file during the current transaction. |
| 212 | ** And this flag says nothing about whether or not the journal |
| 213 | ** has been synced to disk. For pages that are in the original |
| 214 | ** database file, the following expression should always be true: |
| 215 | ** |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 216 | ** inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno) |
drh | 732c817 | 2007-06-16 11:17:45 +0000 | [diff] [blame] | 217 | ** |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 218 | ** The pPager->pInJournal object is only valid for the original |
drh | 732c817 | 2007-06-16 11:17:45 +0000 | [diff] [blame] | 219 | ** pages of the database, not new pages that are added to the end |
| 220 | ** of the database, so obviously the above expression cannot be |
| 221 | ** valid for new pages. For new pages inJournal is always 0. |
| 222 | ** |
| 223 | ** dirty |
| 224 | ** |
| 225 | ** When true, this means that the content of the page has been |
| 226 | ** modified and needs to be written back to the database file. |
| 227 | ** If false, it means that either the content of the page is |
| 228 | ** unchanged or else the content is unimportant and we do not |
| 229 | ** care whether or not it is preserved. |
| 230 | ** |
| 231 | ** alwaysRollback |
| 232 | ** |
| 233 | ** This means that the sqlite3PagerDontRollback() API should be |
| 234 | ** ignored for this page. The DontRollback() API attempts to say |
| 235 | ** that the content of the page on disk is unimportant (it is an |
| 236 | ** unused page on the freelist) so that it is unnecessary to |
| 237 | ** rollback changes to this page because the content of the page |
| 238 | ** can change without changing the meaning of the database. This |
| 239 | ** flag overrides any DontRollback() attempt. This flag is set |
| 240 | ** when a page that originally contained valid data is added to |
| 241 | ** the freelist. Later in the same transaction, this page might |
| 242 | ** be pulled from the freelist and reused for something different |
| 243 | ** and at that point the DontRollback() API will be called because |
| 244 | ** pages taken from the freelist do not need to be protected by |
| 245 | ** the rollback journal. But this flag says that the page was |
| 246 | ** not originally part of the freelist so that it still needs to |
| 247 | ** be rolled back in spite of any subsequent DontRollback() calls. |
| 248 | ** |
| 249 | ** needRead |
| 250 | ** |
| 251 | ** This flag means (when true) that the content of the page has |
| 252 | ** not yet been loaded from disk. The in-memory content is just |
| 253 | ** garbage. (Actually, we zero the content, but you should not |
| 254 | ** make any assumptions about the content nevertheless.) If the |
| 255 | ** content is needed in the future, it should be read from the |
| 256 | ** original database file. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 257 | */ |
| 258 | struct PgHdr { |
| 259 | Pager *pPager; /* The pager to which this page belongs */ |
| 260 | Pgno pgno; /* The page number for this page */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 261 | PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 262 | PagerLruLink free; /* Next and previous free pages */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 263 | PgHdr *pNextAll; /* A list of all pages */ |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 264 | u8 inJournal; /* TRUE if has been written to journal */ |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 265 | u8 dirty; /* TRUE if we need to write back changes */ |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 266 | u8 needSync; /* Sync journal before writing this page */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 267 | u8 alwaysRollback; /* Disable DontRollback() for this page */ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 268 | u8 needRead; /* Read content if PagerWrite() is called */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 269 | short int nRef; /* Number of users of this page */ |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 270 | PgHdr *pDirty, *pPrevDirty; /* Dirty pages */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 271 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 272 | PagerLruLink gfree; /* Global list of nRef==0 pages */ |
| 273 | #endif |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 274 | #ifdef SQLITE_CHECK_PAGES |
| 275 | u32 pageHash; |
| 276 | #endif |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 277 | void *pData; /* Page data */ |
| 278 | /* Pager.nExtra bytes of local data appended to this header */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 281 | /* |
| 282 | ** For an in-memory only database, some extra information is recorded about |
| 283 | ** each page so that changes can be rolled back. (Journal files are not |
| 284 | ** used for in-memory databases.) The following information is added to |
| 285 | ** the end of every EXTRA block for in-memory databases. |
| 286 | ** |
| 287 | ** This information could have been added directly to the PgHdr structure. |
| 288 | ** But then it would take up an extra 8 bytes of storage on every PgHdr |
| 289 | ** even for disk-based databases. Splitting it out saves 8 bytes. This |
| 290 | ** is only a savings of 0.8% but those percentages add up. |
| 291 | */ |
| 292 | typedef struct PgHistory PgHistory; |
| 293 | struct PgHistory { |
| 294 | u8 *pOrig; /* Original page text. Restore to this on a full rollback */ |
| 295 | u8 *pStmt; /* Text as it was at the beginning of the current statement */ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 296 | PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */ |
| 297 | u8 inStmt; /* TRUE if in the statement subjournal */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 298 | }; |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | ** A macro used for invoking the codec if there is one |
| 302 | */ |
| 303 | #ifdef SQLITE_HAS_CODEC |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 304 | # define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); } |
| 305 | # define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D)) |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 306 | #else |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 307 | # define CODEC1(P,D,N,X) /* NO-OP */ |
| 308 | # define CODEC2(P,D,N,X) ((char*)D) |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 309 | #endif |
| 310 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 311 | /* |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 312 | ** Convert a pointer to a PgHdr into a pointer to its data |
| 313 | ** and back again. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 314 | */ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 315 | #define PGHDR_TO_DATA(P) ((P)->pData) |
| 316 | #define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1])) |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 317 | #define PGHDR_TO_HIST(P,PGR) \ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 318 | ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra]) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 319 | |
| 320 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 321 | ** A open page cache is an instance of the following structure. |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 322 | ** |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 323 | ** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 324 | ** or SQLITE_FULL. Once one of the first three errors occurs, it persists |
| 325 | ** and is returned as the result of every major pager API call. The |
| 326 | ** SQLITE_FULL return code is slightly different. It persists only until the |
| 327 | ** next successful rollback is performed on the pager cache. Also, |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 328 | ** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup() |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 329 | ** APIs, they may still be used successfully. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 330 | */ |
| 331 | struct Pager { |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 332 | sqlite3_vfs *pVfs; /* OS functions to use for IO */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 333 | u8 journalOpen; /* True if journal file descriptors is valid */ |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 334 | u8 journalStarted; /* True if header of journal is synced */ |
| 335 | u8 useJournal; /* Use a rollback journal on this file */ |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 336 | u8 noReadlock; /* Do not bother to obtain readlocks */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 337 | u8 stmtOpen; /* True if the statement subjournal is open */ |
| 338 | u8 stmtInUse; /* True we are in a statement subtransaction */ |
| 339 | u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 340 | u8 noSync; /* Do not sync the journal if true */ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 341 | u8 fullSync; /* Do extra syncs of the journal for robustness */ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 342 | u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 343 | u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 344 | u8 tempFile; /* zFilename is a temporary file */ |
| 345 | u8 readOnly; /* True for a read-only database */ |
| 346 | u8 needSync; /* True if an fsync() is needed on the journal */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 347 | u8 dirtyCache; /* True if cached pages have changed */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 348 | u8 alwaysRollback; /* Disable DontRollback() for all pages */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 349 | u8 memDb; /* True to inhibit all file I/O */ |
drh | 6d156e4 | 2005-05-20 20:11:20 +0000 | [diff] [blame] | 350 | u8 setMaster; /* True if a m-j name has been written to jrnl */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 351 | u8 doNotSync; /* Boolean. While true, do not spill the cache */ |
| 352 | u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ |
| 353 | u8 changeCountDone; /* Set after incrementing the change-counter */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 354 | u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 355 | int errCode; /* One of several kinds of errors */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 356 | int dbSize; /* Number of pages in the file */ |
| 357 | int origDbSize; /* dbSize before the current change */ |
| 358 | int stmtSize; /* Size of database (in pages) at stmt_begin() */ |
| 359 | int nRec; /* Number of pages written to the journal */ |
| 360 | u32 cksumInit; /* Quasi-random value added to every checksum */ |
| 361 | int stmtNRec; /* Number of records in stmt subjournal */ |
| 362 | int nExtra; /* Add this many bytes to each in-memory page */ |
| 363 | int pageSize; /* Number of bytes in a page */ |
| 364 | int nPage; /* Total number of in-memory pages */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 365 | int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ |
| 366 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 367 | Pgno mxPgno; /* Maximum allowed size of the database */ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 368 | Bitvec *pInJournal; /* One bit for each page in the database file */ |
| 369 | Bitvec *pInStmt; /* One bit for each page in the database */ |
drh | fcd35c7 | 2005-05-21 02:48:08 +0000 | [diff] [blame] | 370 | char *zFilename; /* Name of the database file */ |
| 371 | char *zJournal; /* Name of the journal file */ |
| 372 | char *zDirectory; /* Directory hold database and journal files */ |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 373 | char *zStmtJrnl; /* Name of the statement journal file */ |
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 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 536 | ** Add page pPg to the end of the linked list managed by structure |
| 537 | ** pList (pPg becomes the last entry in the list - the most recently |
| 538 | ** used). Argument pLink should point to either pPg->free or pPg->gfree, |
| 539 | ** depending on whether pPg is being added to the pager-specific or |
| 540 | ** global LRU list. |
| 541 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 542 | static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){ |
| 543 | pLink->pNext = 0; |
| 544 | pLink->pPrev = pList->pLast; |
| 545 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 546 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 547 | assert(pLink==&pPg->free || pLink==&pPg->gfree); |
| 548 | assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList); |
| 549 | #endif |
| 550 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 551 | if( pList->pLast ){ |
| 552 | int iOff = (char *)pLink - (char *)pPg; |
| 553 | PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]); |
| 554 | pLastLink->pNext = pPg; |
| 555 | }else{ |
| 556 | assert(!pList->pFirst); |
| 557 | pList->pFirst = pPg; |
| 558 | } |
| 559 | |
| 560 | pList->pLast = pPg; |
| 561 | if( !pList->pFirstSynced && pPg->needSync==0 ){ |
| 562 | pList->pFirstSynced = pPg; |
| 563 | } |
| 564 | } |
| 565 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 566 | /* |
| 567 | ** Remove pPg from the list managed by the structure pointed to by pList. |
| 568 | ** |
| 569 | ** Argument pLink should point to either pPg->free or pPg->gfree, depending |
| 570 | ** on whether pPg is being added to the pager-specific or global LRU list. |
| 571 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 572 | static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){ |
| 573 | int iOff = (char *)pLink - (char *)pPg; |
| 574 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 575 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 576 | assert(pLink==&pPg->free || pLink==&pPg->gfree); |
| 577 | assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList); |
| 578 | #endif |
| 579 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 580 | if( pPg==pList->pFirst ){ |
| 581 | pList->pFirst = pLink->pNext; |
| 582 | } |
| 583 | if( pPg==pList->pLast ){ |
| 584 | pList->pLast = pLink->pPrev; |
| 585 | } |
| 586 | if( pLink->pPrev ){ |
| 587 | PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]); |
| 588 | pPrevLink->pNext = pLink->pNext; |
| 589 | } |
| 590 | if( pLink->pNext ){ |
| 591 | PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]); |
| 592 | pNextLink->pPrev = pLink->pPrev; |
| 593 | } |
| 594 | if( pPg==pList->pFirstSynced ){ |
| 595 | PgHdr *p = pLink->pNext; |
| 596 | while( p && p->needSync ){ |
| 597 | PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]); |
| 598 | p = pL->pNext; |
| 599 | } |
| 600 | pList->pFirstSynced = p; |
| 601 | } |
| 602 | |
| 603 | pLink->pNext = pLink->pPrev = 0; |
| 604 | } |
| 605 | |
| 606 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 607 | ** Add page pPg to the list of free pages for the pager. If |
| 608 | ** memory-management is enabled, also add the page to the global |
| 609 | ** list of free pages. |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 610 | */ |
| 611 | static void lruListAdd(PgHdr *pPg){ |
| 612 | listAdd(&pPg->pPager->lru, &pPg->free, pPg); |
| 613 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 614 | if( !pPg->pPager->memDb ){ |
| 615 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 616 | listAdd(&sqlite3LruPageList, &pPg->gfree, pPg); |
| 617 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 618 | } |
| 619 | #endif |
| 620 | } |
| 621 | |
| 622 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 623 | ** Remove page pPg from the list of free pages for the associated pager. |
| 624 | ** If memory-management is enabled, also remove pPg from the global list |
| 625 | ** of free pages. |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 626 | */ |
| 627 | static void lruListRemove(PgHdr *pPg){ |
| 628 | listRemove(&pPg->pPager->lru, &pPg->free, pPg); |
| 629 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 630 | if( !pPg->pPager->memDb ){ |
| 631 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 632 | listRemove(&sqlite3LruPageList, &pPg->gfree, pPg); |
| 633 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 634 | } |
| 635 | #endif |
| 636 | } |
| 637 | |
| 638 | /* |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 639 | ** This function is called just after the needSync flag has been cleared |
| 640 | ** from all pages managed by pPager (usually because the journal file |
| 641 | ** has just been synced). It updates the pPager->lru.pFirstSynced variable |
| 642 | ** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced |
| 643 | ** variable also. |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 644 | */ |
| 645 | static void lruListSetFirstSynced(Pager *pPager){ |
| 646 | pPager->lru.pFirstSynced = pPager->lru.pFirst; |
| 647 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 648 | if( !pPager->memDb ){ |
| 649 | PgHdr *p; |
| 650 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 651 | for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext); |
| 652 | assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced); |
| 653 | sqlite3LruPageList.pFirstSynced = p; |
| 654 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 655 | } |
| 656 | #endif |
| 657 | } |
| 658 | |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 659 | /* |
| 660 | ** Return true if page *pPg has already been written to the statement |
| 661 | ** journal (or statement snapshot has been created, if *pPg is part |
| 662 | ** of an in-memory database). |
| 663 | */ |
| 664 | static int pageInStatement(PgHdr *pPg){ |
| 665 | Pager *pPager = pPg->pPager; |
| 666 | if( MEMDB ){ |
| 667 | return PGHDR_TO_HIST(pPg, pPager)->inStmt; |
| 668 | }else{ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 669 | return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 672 | |
| 673 | /* |
| 674 | ** Change the size of the pager hash table to N. N must be a power |
| 675 | ** of two. |
| 676 | */ |
| 677 | static void pager_resize_hash_table(Pager *pPager, int N){ |
| 678 | PgHdr **aHash, *pPg; |
| 679 | assert( N>0 && (N&(N-1))==0 ); |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 680 | #ifdef SQLITE_MALLOC_SOFT_LIMIT |
| 681 | if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){ |
| 682 | N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]); |
| 683 | } |
| 684 | if( N==pPager->nHash ) return; |
| 685 | #endif |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 686 | pagerLeave(pPager); |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 687 | sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pPager->aHash!=0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 688 | aHash = sqlite3MallocZero( sizeof(aHash[0])*N ); |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 689 | sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 690 | pagerEnter(pPager); |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 691 | if( aHash==0 ){ |
| 692 | /* Failure to rehash is not an error. It is only a performance hit. */ |
| 693 | return; |
| 694 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 695 | sqlite3_free(pPager->aHash); |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 696 | pPager->nHash = N; |
| 697 | pPager->aHash = aHash; |
| 698 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 699 | int h; |
| 700 | if( pPg->pgno==0 ){ |
| 701 | assert( pPg->pNextHash==0 && pPg->pPrevHash==0 ); |
| 702 | continue; |
| 703 | } |
| 704 | h = pPg->pgno & (N-1); |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 705 | pPg->pNextHash = aHash[h]; |
| 706 | if( aHash[h] ){ |
| 707 | aHash[h]->pPrevHash = pPg; |
| 708 | } |
| 709 | aHash[h] = pPg; |
| 710 | pPg->pPrevHash = 0; |
| 711 | } |
| 712 | } |
| 713 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 714 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 715 | ** Read a 32-bit integer from the given file descriptor. Store the integer |
| 716 | ** that is read in *pRes. Return SQLITE_OK if everything worked, or an |
| 717 | ** error code is something goes wrong. |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 718 | ** |
| 719 | ** All values are stored on disk as big-endian. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 720 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 721 | static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){ |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 722 | unsigned char ac[4]; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 723 | int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset); |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 724 | if( rc==SQLITE_OK ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 725 | *pRes = sqlite3Get4byte(ac); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 726 | } |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 727 | return rc; |
| 728 | } |
| 729 | |
| 730 | /* |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 731 | ** Write a 32-bit integer into a string buffer in big-endian byte order. |
| 732 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 733 | #define put32bits(A,B) sqlite3Put4byte((u8*)A,B) |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 734 | |
| 735 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 736 | ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK |
| 737 | ** on success or an error code is something goes wrong. |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 738 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 739 | static int write32bits(sqlite3_file *fd, i64 offset, u32 val){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 740 | char ac[4]; |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 741 | put32bits(ac, val); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 742 | return sqlite3OsWrite(fd, ac, 4, offset); |
drh | 94f3331 | 2002-08-12 12:29:56 +0000 | [diff] [blame] | 743 | } |
| 744 | |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 745 | /* |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 746 | ** If file pFd is open, call sqlite3OsUnlock() on it. |
| 747 | */ |
| 748 | static int osUnlock(sqlite3_file *pFd, int eLock){ |
| 749 | if( !pFd->pMethods ){ |
| 750 | return SQLITE_OK; |
| 751 | } |
| 752 | return sqlite3OsUnlock(pFd, eLock); |
| 753 | } |
| 754 | |
| 755 | /* |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 756 | ** This function determines whether or not the atomic-write optimization |
| 757 | ** can be used with this pager. The optimization can be used if: |
| 758 | ** |
| 759 | ** (a) the value returned by OsDeviceCharacteristics() indicates that |
| 760 | ** a database page may be written atomically, and |
| 761 | ** (b) the value returned by OsSectorSize() is less than or equal |
| 762 | ** to the page size. |
| 763 | ** |
| 764 | ** If the optimization cannot be used, 0 is returned. If it can be used, |
| 765 | ** then the value returned is the size of the journal file when it |
| 766 | ** contains rollback data for exactly one page. |
| 767 | */ |
| 768 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 769 | static int jrnlBufferSize(Pager *pPager){ |
| 770 | int dc; /* Device characteristics */ |
| 771 | int nSector; /* Sector size */ |
| 772 | int nPage; /* Page size */ |
| 773 | sqlite3_file *fd = pPager->fd; |
| 774 | |
| 775 | if( fd->pMethods ){ |
| 776 | dc = sqlite3OsDeviceCharacteristics(fd); |
| 777 | nSector = sqlite3OsSectorSize(fd); |
| 778 | nPage = pPager->pageSize; |
| 779 | } |
| 780 | |
| 781 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 782 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 783 | |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 784 | if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 785 | return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); |
| 786 | } |
| 787 | return 0; |
| 788 | } |
| 789 | #endif |
| 790 | |
| 791 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 792 | ** This function should be called when an error occurs within the pager |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 793 | ** code. The first argument is a pointer to the pager structure, the |
| 794 | ** second the error-code about to be returned by a pager API function. |
| 795 | ** The value returned is a copy of the second argument to this function. |
| 796 | ** |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 797 | ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 798 | ** the error becomes persistent. Until the persisten error is cleared, |
| 799 | ** subsequent API calls on this Pager will immediately return the same |
| 800 | ** error code. |
| 801 | ** |
| 802 | ** A persistent error indicates that the contents of the pager-cache |
| 803 | ** cannot be trusted. This state can be cleared by completely discarding |
| 804 | ** the contents of the pager-cache. If a transaction was active when |
| 805 | ** the persistent error occured, then the rollback journal may need |
| 806 | ** to be replayed. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 807 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 808 | static void pager_unlock(Pager *pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 809 | static int pager_error(Pager *pPager, int rc){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 810 | int rc2 = rc & 0xff; |
drh | 34f5621 | 2007-08-10 23:56:35 +0000 | [diff] [blame] | 811 | assert( |
| 812 | pPager->errCode==SQLITE_FULL || |
| 813 | pPager->errCode==SQLITE_OK || |
| 814 | (pPager->errCode & 0xff)==SQLITE_IOERR |
| 815 | ); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 816 | if( |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 817 | rc2==SQLITE_FULL || |
| 818 | rc2==SQLITE_IOERR || |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 819 | rc2==SQLITE_CORRUPT |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 820 | ){ |
| 821 | pPager->errCode = rc; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 822 | if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){ |
| 823 | /* If the pager is already unlocked, call pager_unlock() now to |
| 824 | ** clear the error state and ensure that the pager-cache is |
| 825 | ** completely empty. |
| 826 | */ |
| 827 | pager_unlock(pPager); |
| 828 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 829 | } |
| 830 | return rc; |
| 831 | } |
| 832 | |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 833 | /* |
| 834 | ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking |
| 835 | ** on the cache using a hash function. This is used for testing |
| 836 | ** and debugging only. |
| 837 | */ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 838 | #ifdef SQLITE_CHECK_PAGES |
| 839 | /* |
| 840 | ** Return a 32-bit hash of the page data for pPage. |
| 841 | */ |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 842 | static u32 pager_datahash(int nByte, unsigned char *pData){ |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 843 | u32 hash = 0; |
| 844 | int i; |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 845 | for(i=0; i<nByte; i++){ |
| 846 | hash = (hash*1039) + pData[i]; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 847 | } |
| 848 | return hash; |
| 849 | } |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 850 | static u32 pager_pagehash(PgHdr *pPage){ |
| 851 | return pager_datahash(pPage->pPager->pageSize, |
| 852 | (unsigned char *)PGHDR_TO_DATA(pPage)); |
| 853 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 854 | |
| 855 | /* |
| 856 | ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES |
| 857 | ** is defined, and NDEBUG is not defined, an assert() statement checks |
| 858 | ** that the page is either dirty or still matches the calculated page-hash. |
| 859 | */ |
| 860 | #define CHECK_PAGE(x) checkPage(x) |
| 861 | static void checkPage(PgHdr *pPg){ |
| 862 | Pager *pPager = pPg->pPager; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 863 | assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty || |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 864 | pPg->pageHash==pager_pagehash(pPg) ); |
| 865 | } |
| 866 | |
| 867 | #else |
drh | 8ffa817 | 2007-06-18 17:25:17 +0000 | [diff] [blame] | 868 | #define pager_datahash(X,Y) 0 |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 869 | #define pager_pagehash(X) 0 |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 870 | #define CHECK_PAGE(x) |
| 871 | #endif |
| 872 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 873 | /* |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 874 | ** When this is called the journal file for pager pPager must be open. |
| 875 | ** 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] | 876 | ** written into memory supplied by the caller. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 877 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 878 | ** zMaster must point to a buffer of at least nMaster bytes allocated by |
| 879 | ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is |
| 880 | ** enough space to write the master journal name). If the master journal |
| 881 | ** name in the journal is longer than nMaster bytes (including a |
| 882 | ** nul-terminator), then this is handled as if no master journal name |
| 883 | ** were present in the journal. |
| 884 | ** |
| 885 | ** 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] | 886 | ** SQLITE_OK returned. |
| 887 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 888 | static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 889 | int rc; |
| 890 | u32 len; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 891 | i64 szJ; |
danielk1977 | c3e8f5e | 2004-06-28 01:16:46 +0000 | [diff] [blame] | 892 | u32 cksum; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 893 | int i; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 894 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
| 895 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 896 | zMaster[0] = '\0'; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 897 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 898 | rc = sqlite3OsFileSize(pJrnl, &szJ); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 899 | if( rc!=SQLITE_OK || szJ<16 ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 900 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 901 | rc = read32bits(pJrnl, szJ-16, &len); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 902 | if( rc!=SQLITE_OK ) return rc; |
| 903 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 904 | if( len>=nMaster ){ |
| 905 | return SQLITE_OK; |
| 906 | } |
| 907 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 908 | rc = read32bits(pJrnl, szJ-12, &cksum); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 909 | if( rc!=SQLITE_OK ) return rc; |
| 910 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 911 | rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 912 | if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc; |
| 913 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 914 | rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 915 | if( rc!=SQLITE_OK ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 916 | return rc; |
| 917 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 918 | zMaster[len] = '\0'; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 919 | |
| 920 | /* See if the checksum matches the master journal name */ |
| 921 | for(i=0; i<len; i++){ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 922 | cksum -= zMaster[i]; |
| 923 | } |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 924 | if( cksum ){ |
| 925 | /* If the checksum doesn't add up, then one or more of the disk sectors |
| 926 | ** containing the master journal filename is corrupted. This means |
| 927 | ** definitely roll back, so just return SQLITE_OK and report a (nul) |
| 928 | ** master-journal filename. |
| 929 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 930 | zMaster[0] = '\0'; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 931 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 932 | |
| 933 | return SQLITE_OK; |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | ** Seek the journal file descriptor to the next sector boundary where a |
| 938 | ** journal header may be read or written. Pager.journalOff is updated with |
| 939 | ** the new seek offset. |
| 940 | ** |
| 941 | ** i.e for a sector size of 512: |
| 942 | ** |
| 943 | ** Input Offset Output Offset |
| 944 | ** --------------------------------------- |
| 945 | ** 0 0 |
| 946 | ** 512 512 |
| 947 | ** 100 512 |
| 948 | ** 2000 2048 |
| 949 | ** |
| 950 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 951 | static void seekJournalHdr(Pager *pPager){ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 952 | i64 offset = 0; |
| 953 | i64 c = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 954 | if( c ){ |
| 955 | offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager); |
| 956 | } |
| 957 | assert( offset%JOURNAL_HDR_SZ(pPager)==0 ); |
| 958 | assert( offset>=c ); |
| 959 | assert( (offset-c)<JOURNAL_HDR_SZ(pPager) ); |
| 960 | pPager->journalOff = offset; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* |
| 964 | ** The journal file must be open when this routine is called. A journal |
| 965 | ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the |
| 966 | ** current location. |
| 967 | ** |
| 968 | ** The format for the journal header is as follows: |
| 969 | ** - 8 bytes: Magic identifying journal format. |
| 970 | ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on. |
| 971 | ** - 4 bytes: Random number used for page hash. |
| 972 | ** - 4 bytes: Initial database page count. |
| 973 | ** - 4 bytes: Sector size used by the process that wrote this journal. |
| 974 | ** |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 975 | ** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 976 | */ |
| 977 | static int writeJournalHdr(Pager *pPager){ |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 978 | char zHeader[sizeof(aJournalMagic)+16]; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 979 | int rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 980 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 981 | if( pPager->stmtHdrOff==0 ){ |
| 982 | pPager->stmtHdrOff = pPager->journalOff; |
| 983 | } |
| 984 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 985 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 986 | pPager->journalHdr = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 987 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 988 | memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 989 | |
| 990 | /* |
| 991 | ** Write the nRec Field - the number of page records that follow this |
| 992 | ** journal header. Normally, zero is written to this value at this time. |
| 993 | ** After the records are added to the journal (and the journal synced, |
| 994 | ** if in full-sync mode), the zero is overwritten with the true number |
| 995 | ** of records (see syncJournal()). |
| 996 | ** |
| 997 | ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When |
| 998 | ** reading the journal this value tells SQLite to assume that the |
| 999 | ** rest of the journal file contains valid page records. This assumption |
| 1000 | ** is dangerous, as if a failure occured whilst writing to the journal |
| 1001 | ** file it may contain some garbage data. There are two scenarios |
| 1002 | ** where this risk can be ignored: |
| 1003 | ** |
| 1004 | ** * When the pager is in no-sync mode. Corruption can follow a |
| 1005 | ** power failure in this case anyway. |
| 1006 | ** |
| 1007 | ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees |
| 1008 | ** that garbage data is never appended to the journal file. |
| 1009 | */ |
| 1010 | assert(pPager->fd->pMethods||pPager->noSync); |
| 1011 | if( (pPager->noSync) |
| 1012 | || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) |
| 1013 | ){ |
| 1014 | put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); |
| 1015 | }else{ |
| 1016 | put32bits(&zHeader[sizeof(aJournalMagic)], 0); |
| 1017 | } |
| 1018 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1019 | /* The random check-hash initialiser */ |
| 1020 | sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); |
| 1021 | put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit); |
| 1022 | /* The initial database size */ |
| 1023 | put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize); |
| 1024 | /* The assumed sector size for this process */ |
| 1025 | put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1026 | IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader))) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1027 | rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff); |
| 1028 | pPager->journalOff += JOURNAL_HDR_SZ(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1029 | |
| 1030 | /* The journal header has been written successfully. Seek the journal |
| 1031 | ** file descriptor to the end of the journal header sector. |
| 1032 | */ |
| 1033 | if( rc==SQLITE_OK ){ |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1034 | IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1)) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1035 | rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1036 | } |
| 1037 | return rc; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | ** The journal file must be open when this is called. A journal header file |
| 1042 | ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal |
| 1043 | ** file. See comments above function writeJournalHdr() for a description of |
| 1044 | ** the journal header format. |
| 1045 | ** |
| 1046 | ** If the header is read successfully, *nRec is set to the number of |
| 1047 | ** page records following this header and *dbSize is set to the size of the |
| 1048 | ** database before the transaction began, in pages. Also, pPager->cksumInit |
| 1049 | ** is set to the value read from the journal header. SQLITE_OK is returned |
| 1050 | ** in this case. |
| 1051 | ** |
| 1052 | ** If the journal header file appears to be corrupted, SQLITE_DONE is |
| 1053 | ** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes |
| 1054 | ** cannot be read from the journal file an error code is returned. |
| 1055 | */ |
| 1056 | static int readJournalHdr( |
| 1057 | Pager *pPager, |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1058 | i64 journalSize, |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1059 | u32 *pNRec, |
| 1060 | u32 *pDbSize |
| 1061 | ){ |
| 1062 | int rc; |
| 1063 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1064 | i64 jrnlOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1065 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1066 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1067 | if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){ |
| 1068 | return SQLITE_DONE; |
| 1069 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1070 | jrnlOff = pPager->journalOff; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1071 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1072 | rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1073 | if( rc ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1074 | jrnlOff += sizeof(aMagic); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1075 | |
| 1076 | if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){ |
| 1077 | return SQLITE_DONE; |
| 1078 | } |
| 1079 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1080 | rc = read32bits(pPager->jfd, jrnlOff, pNRec); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1081 | if( rc ) return rc; |
| 1082 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1083 | rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1084 | if( rc ) return rc; |
| 1085 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1086 | rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1087 | if( rc ) return rc; |
| 1088 | |
| 1089 | /* Update the assumed sector-size to match the value used by |
| 1090 | ** the process that created this journal. If this journal was |
| 1091 | ** created by a process other than this one, then this routine |
| 1092 | ** is being called from within pager_playback(). The local value |
| 1093 | ** of Pager.sectorSize is restored at the end of that routine. |
| 1094 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1095 | rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1096 | if( rc ) return rc; |
| 1097 | |
| 1098 | pPager->journalOff += JOURNAL_HDR_SZ(pPager); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1099 | return SQLITE_OK; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | /* |
| 1104 | ** Write the supplied master journal name into the journal file for pager |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1105 | ** pPager at the current location. The master journal name must be the last |
| 1106 | ** thing written to a journal file. If the pager is in full-sync mode, the |
| 1107 | ** journal file descriptor is advanced to the next sector boundary before |
| 1108 | ** anything is written. The format is: |
| 1109 | ** |
| 1110 | ** + 4 bytes: PAGER_MJ_PGNO. |
| 1111 | ** + N bytes: length of master journal name. |
| 1112 | ** + 4 bytes: N |
| 1113 | ** + 4 bytes: Master journal name checksum. |
| 1114 | ** + 8 bytes: aJournalMagic[]. |
| 1115 | ** |
| 1116 | ** The master journal page checksum is the sum of the bytes in the master |
| 1117 | ** journal name. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1118 | ** |
| 1119 | ** If zMaster is a NULL pointer (occurs for a single database transaction), |
| 1120 | ** this call is a no-op. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1121 | */ |
| 1122 | static int writeMasterJournal(Pager *pPager, const char *zMaster){ |
| 1123 | int rc; |
| 1124 | int len; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1125 | int i; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1126 | i64 jrnlOff; |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1127 | u32 cksum = 0; |
| 1128 | char zBuf[sizeof(aJournalMagic)+2*4]; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1129 | |
| 1130 | if( !zMaster || pPager->setMaster) return SQLITE_OK; |
| 1131 | pPager->setMaster = 1; |
| 1132 | |
| 1133 | len = strlen(zMaster); |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1134 | for(i=0; i<len; i++){ |
| 1135 | cksum += zMaster[i]; |
| 1136 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1137 | |
| 1138 | /* If in full-sync mode, advance to the next disk sector before writing |
| 1139 | ** the master journal name. This is in case the previous page written to |
| 1140 | ** the journal has already been synced. |
| 1141 | */ |
| 1142 | if( pPager->fullSync ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1143 | seekJournalHdr(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1144 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1145 | jrnlOff = pPager->journalOff; |
danielk1977 | cafadba | 2004-06-25 11:11:54 +0000 | [diff] [blame] | 1146 | pPager->journalOff += (len+20); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1147 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1148 | rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager)); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1149 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1150 | jrnlOff += 4; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1151 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1152 | rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1153 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1154 | jrnlOff += len; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1155 | |
drh | 97b5748 | 2006-01-10 20:32:32 +0000 | [diff] [blame] | 1156 | put32bits(zBuf, len); |
| 1157 | put32bits(&zBuf[4], cksum); |
| 1158 | memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic)); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1159 | rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff); |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1160 | pPager->needSync = !pPager->noSync; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1161 | return rc; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1165 | ** 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] | 1166 | ** statement journal. |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1167 | ** |
| 1168 | ** The Pager keeps a separate list of pages that are currently in |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1169 | ** the statement journal. This helps the sqlite3PagerStmtCommit() |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1170 | ** routine run MUCH faster for the common case where there are many |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1171 | ** pages in memory but only a few are in the statement journal. |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1172 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1173 | static void page_add_to_stmt_list(PgHdr *pPg){ |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1174 | Pager *pPager = pPg->pPager; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 1175 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 1176 | assert( MEMDB ); |
| 1177 | if( !pHist->inStmt ){ |
| 1178 | assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 ); |
| 1179 | if( pPager->pStmt ){ |
| 1180 | PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg; |
| 1181 | } |
| 1182 | pHist->pNextStmt = pPager->pStmt; |
| 1183 | pPager->pStmt = pPg; |
| 1184 | pHist->inStmt = 1; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1185 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1189 | ** Find a page in the hash table given its page number. Return |
| 1190 | ** a pointer to the page or NULL if not found. |
| 1191 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1192 | static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 1193 | PgHdr *p; |
| 1194 | if( pPager->aHash==0 ) return 0; |
| 1195 | p = pPager->aHash[pgno & (pPager->nHash-1)]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1196 | while( p && p->pgno!=pgno ){ |
| 1197 | p = p->pNextHash; |
| 1198 | } |
| 1199 | return p; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1203 | ** Clear the in-memory cache. This routine |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1204 | ** sets the state of the pager back to what it was when it was first |
| 1205 | ** opened. Any outstanding pages are invalidated and subsequent attempts |
| 1206 | ** to access those pages will likely result in a coredump. |
| 1207 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1208 | static void pager_reset(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1209 | PgHdr *pPg, *pNext; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 1210 | if( pPager->errCode ) return; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1211 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1212 | IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno)); |
| 1213 | PAGER_INCR(sqlite3_pager_pgfree_count); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1214 | pNext = pPg->pNextAll; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 1215 | lruListRemove(pPg); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 1216 | sqlite3_free(pPg->pData); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1217 | sqlite3_free(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1218 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 1219 | assert(pPager->lru.pFirst==0); |
| 1220 | assert(pPager->lru.pFirstSynced==0); |
| 1221 | assert(pPager->lru.pLast==0); |
danielk1977 | b94bf85 | 2007-03-19 13:53:37 +0000 | [diff] [blame] | 1222 | pPager->pStmt = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1223 | pPager->pAll = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1224 | pPager->pDirty = 0; |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 1225 | pPager->nHash = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1226 | sqlite3_free(pPager->aHash); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1227 | pPager->nPage = 0; |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 1228 | pPager->aHash = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1229 | pPager->nRef = 0; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1233 | ** Unlock the database file. |
| 1234 | ** |
| 1235 | ** If the pager is currently in error state, discard the contents of |
| 1236 | ** the cache and reset the Pager structure internal state. If there is |
| 1237 | ** an open journal-file, then the next time a shared-lock is obtained |
| 1238 | ** on the pager file (by this or any other process), it will be |
| 1239 | ** treated as a hot-journal and rolled back. |
| 1240 | */ |
| 1241 | static void pager_unlock(Pager *pPager){ |
| 1242 | if( !pPager->exclusiveMode ){ |
| 1243 | if( !MEMDB ){ |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1244 | int rc = osUnlock(pPager->fd, NO_LOCK); |
drh | 308aa32 | 2008-03-07 20:14:38 +0000 | [diff] [blame] | 1245 | if( rc ) pPager->errCode = rc; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1246 | pPager->dbSize = -1; |
| 1247 | IOTRACE(("UNLOCK %p\n", pPager)) |
| 1248 | |
| 1249 | /* If Pager.errCode is set, the contents of the pager cache cannot be |
| 1250 | ** trusted. Now that the pager file is unlocked, the contents of the |
| 1251 | ** cache can be discarded and the error code safely cleared. |
| 1252 | */ |
| 1253 | if( pPager->errCode ){ |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1254 | if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1255 | pager_reset(pPager); |
| 1256 | if( pPager->stmtOpen ){ |
| 1257 | sqlite3OsClose(pPager->stfd); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1258 | sqlite3BitvecDestroy(pPager->pInStmt); |
| 1259 | pPager->pInStmt = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1260 | } |
| 1261 | if( pPager->journalOpen ){ |
| 1262 | sqlite3OsClose(pPager->jfd); |
| 1263 | pPager->journalOpen = 0; |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1264 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 1265 | pPager->pInJournal = 0; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1266 | } |
| 1267 | pPager->stmtOpen = 0; |
| 1268 | pPager->stmtInUse = 0; |
| 1269 | pPager->journalOff = 0; |
| 1270 | pPager->journalStarted = 0; |
| 1271 | pPager->stmtAutoopen = 0; |
| 1272 | pPager->origDbSize = 0; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | if( !MEMDB || pPager->errCode==SQLITE_OK ){ |
| 1277 | pPager->state = PAGER_UNLOCK; |
| 1278 | pPager->changeCountDone = 0; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | /* |
| 1284 | ** Execute a rollback if a transaction is active and unlock the |
| 1285 | ** database file. If the pager has already entered the error state, |
| 1286 | ** do not attempt the rollback. |
| 1287 | */ |
| 1288 | static void pagerUnlockAndRollback(Pager *p){ |
| 1289 | assert( p->state>=PAGER_RESERVED || p->journalOpen==0 ); |
| 1290 | if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){ |
| 1291 | sqlite3PagerRollback(p); |
| 1292 | } |
| 1293 | pager_unlock(p); |
| 1294 | assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) ); |
| 1295 | assert( p->errCode || !p->stmtOpen || p->exclusiveMode ); |
| 1296 | } |
| 1297 | |
| 1298 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1299 | ** This routine ends a transaction. A transaction is ended by either |
| 1300 | ** a COMMIT or a ROLLBACK. |
| 1301 | ** |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1302 | ** When this routine is called, the pager has the journal file open and |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1303 | ** a RESERVED or EXCLUSIVE lock on the database. This routine will release |
| 1304 | ** the database lock and acquires a SHARED lock in its place if that is |
| 1305 | ** the appropriate thing to do. Release locks usually is appropriate, |
| 1306 | ** unless we are in exclusive access mode or unless this is a |
| 1307 | ** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation. |
| 1308 | ** |
| 1309 | ** The journal file is either deleted or truncated. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1310 | ** |
| 1311 | ** TODO: Consider keeping the journal file open for temporary databases. |
| 1312 | ** This might give a performance improvement on windows where opening |
| 1313 | ** a file is an expensive operation. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1314 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1315 | static int pager_end_transaction(Pager *pPager){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1316 | PgHdr *pPg; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1317 | int rc = SQLITE_OK; |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1318 | int rc2 = SQLITE_OK; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1319 | assert( !MEMDB ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1320 | if( pPager->state<PAGER_RESERVED ){ |
| 1321 | return SQLITE_OK; |
| 1322 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1323 | sqlite3PagerStmtCommit(pPager); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1324 | if( pPager->stmtOpen && !pPager->exclusiveMode ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1325 | sqlite3OsClose(pPager->stfd); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1326 | pPager->stmtOpen = 0; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 1327 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1328 | if( pPager->journalOpen ){ |
drh | 01fa4c3 | 2007-04-02 11:22:22 +0000 | [diff] [blame] | 1329 | if( pPager->exclusiveMode |
| 1330 | && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1331 | pPager->journalOff = 0; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1332 | pPager->journalStarted = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1333 | }else{ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1334 | sqlite3OsClose(pPager->jfd); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1335 | pPager->journalOpen = 0; |
drh | 01fa4c3 | 2007-04-02 11:22:22 +0000 | [diff] [blame] | 1336 | if( rc==SQLITE_OK ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1337 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
danielk1977 | 7152de8 | 2007-03-29 17:28:14 +0000 | [diff] [blame] | 1338 | } |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1339 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1340 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 1341 | pPager->pInJournal = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1342 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1343 | pPg->inJournal = 0; |
| 1344 | pPg->dirty = 0; |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 1345 | pPg->needSync = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1346 | pPg->alwaysRollback = 0; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1347 | #ifdef SQLITE_CHECK_PAGES |
| 1348 | pPg->pageHash = pager_pagehash(pPg); |
| 1349 | #endif |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1350 | } |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 1351 | pPager->pDirty = 0; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1352 | pPager->dirtyCache = 0; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1353 | pPager->nRec = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1354 | }else{ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 1355 | assert( pPager->pInJournal==0 ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1356 | assert( pPager->dirtyCache==0 || pPager->useJournal==0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1357 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1358 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1359 | if( !pPager->exclusiveMode ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1360 | rc2 = osUnlock(pPager->fd, SHARED_LOCK); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1361 | pPager->state = PAGER_SHARED; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1362 | }else if( pPager->state==PAGER_SYNCED ){ |
| 1363 | pPager->state = PAGER_EXCLUSIVE; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1364 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1365 | pPager->origDbSize = 0; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1366 | pPager->setMaster = 0; |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 1367 | pPager->needSync = 0; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 1368 | lruListSetFirstSynced(pPager); |
drh | 588f5bc | 2007-01-02 18:41:54 +0000 | [diff] [blame] | 1369 | pPager->dbSize = -1; |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1370 | |
| 1371 | return (rc==SQLITE_OK?rc2:rc); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1374 | /* |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1375 | ** Compute and return a checksum for the page of data. |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1376 | ** |
| 1377 | ** 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] | 1378 | ** random initial value and the page number. We experimented with |
| 1379 | ** a checksum of the entire data, but that was found to be too slow. |
| 1380 | ** |
| 1381 | ** Note that the page number is stored at the beginning of data and |
| 1382 | ** the checksum is stored at the end. This is important. If journal |
| 1383 | ** corruption occurs due to a power failure, the most likely scenario |
| 1384 | ** is that one end or the other of the record will be changed. It is |
| 1385 | ** much less likely that the two ends of the journal record will be |
| 1386 | ** correct and the middle be corrupt. Thus, this "checksum" scheme, |
| 1387 | ** though fast and simple, catches the mostly likely kind of corruption. |
| 1388 | ** |
| 1389 | ** FIX ME: Consider adding every 200th (or so) byte of the data to the |
| 1390 | ** checksum. That way if a single page spans 3 or more disk sectors and |
| 1391 | ** only the middle sector is corrupt, we will still have a reasonable |
| 1392 | ** chance of failing the checksum and thus detecting the problem. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1393 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1394 | static u32 pager_cksum(Pager *pPager, const u8 *aData){ |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1395 | u32 cksum = pPager->cksumInit; |
| 1396 | int i = pPager->pageSize-200; |
| 1397 | while( i>0 ){ |
| 1398 | cksum += aData[i]; |
| 1399 | i -= 200; |
| 1400 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1401 | return cksum; |
| 1402 | } |
| 1403 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 1404 | /* Forward declaration */ |
| 1405 | static void makeClean(PgHdr*); |
| 1406 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1407 | /* |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1408 | ** Read a single page from the journal file opened on file descriptor |
| 1409 | ** jfd. Playback this one page. |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1410 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1411 | ** If useCksum==0 it means this journal does not use checksums. Checksums |
| 1412 | ** are not used in statement journals because statement journals do not |
| 1413 | ** need to survive power failures. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1414 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1415 | static int pager_playback_one_page( |
| 1416 | Pager *pPager, |
| 1417 | sqlite3_file *jfd, |
| 1418 | i64 offset, |
| 1419 | int useCksum |
| 1420 | ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1421 | int rc; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1422 | PgHdr *pPg; /* An existing page in the cache */ |
| 1423 | Pgno pgno; /* The page number of a page in journal */ |
| 1424 | u32 cksum; /* Checksum used for sanity checking */ |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 1425 | u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1426 | |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1427 | /* useCksum should be true for the main journal and false for |
| 1428 | ** statement journals. Verify that this is always the case |
| 1429 | */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1430 | assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) ); |
danielk1977 | 8186df8 | 2007-03-06 13:45:59 +0000 | [diff] [blame] | 1431 | assert( aData ); |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1432 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1433 | rc = read32bits(jfd, offset, &pgno); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1434 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1435 | rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1436 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1437 | pPager->journalOff += pPager->pageSize + 4; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1438 | |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1439 | /* Sanity checking on the page. This is more important that I originally |
| 1440 | ** thought. If a power failure occurs while the journal is being written, |
| 1441 | ** it could cause invalid data to be written into the journal. We need to |
| 1442 | ** detect this invalid data (with high probability) and ignore it. |
| 1443 | */ |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1444 | if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1445 | return SQLITE_DONE; |
| 1446 | } |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1447 | if( pgno>(unsigned)pPager->dbSize ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1448 | return SQLITE_OK; |
| 1449 | } |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1450 | if( useCksum ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1451 | rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum); |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 1452 | if( rc ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1453 | pPager->journalOff += 4; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1454 | if( pager_cksum(pPager, aData)!=cksum ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1455 | return SQLITE_DONE; |
| 1456 | } |
| 1457 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1458 | |
danielk1977 | aa5ccdf | 2004-06-14 05:10:42 +0000 | [diff] [blame] | 1459 | assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE ); |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1460 | |
| 1461 | /* If the pager is in RESERVED state, then there must be a copy of this |
| 1462 | ** page in the pager cache. In this case just update the pager cache, |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 1463 | ** not the database file. The page is left marked dirty in this case. |
| 1464 | ** |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1465 | ** An exception to the above rule: If the database is in no-sync mode |
| 1466 | ** and a page is moved during an incremental vacuum then the page may |
danielk1977 | 369f3a0 | 2007-05-24 09:41:20 +0000 | [diff] [blame] | 1467 | ** not be in the pager cache. Later: if a malloc() or IO error occurs |
| 1468 | ** during a Movepage() call, then the page may not be in the cache |
| 1469 | ** either. So the condition described in the above paragraph is not |
| 1470 | ** assert()able. |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1471 | ** |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1472 | ** If in EXCLUSIVE state, then we update the pager cache if it exists |
| 1473 | ** and the main file. The page is then marked not dirty. |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1474 | ** |
| 1475 | ** Ticket #1171: The statement journal might contain page content that is |
| 1476 | ** different from the page content at the start of the transaction. |
| 1477 | ** This occurs when a page is changed prior to the start of a statement |
| 1478 | ** then changed again within the statement. When rolling back such a |
| 1479 | ** statement we must not write to the original database unless we know |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 1480 | ** for certain that original page contents are synced into the main rollback |
| 1481 | ** journal. Otherwise, a power loss might leave modified data in the |
| 1482 | ** database file without an entry in the rollback journal that can |
| 1483 | ** restore the database to its original form. Two conditions must be |
| 1484 | ** met before writing to the database files. (1) the database must be |
| 1485 | ** locked. (2) we know that the original page content is fully synced |
| 1486 | ** in the main journal either because the page is not in cache or else |
| 1487 | ** the page is marked as needSync==0. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1488 | */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1489 | pPg = pager_lookup(pPager, pgno); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 1490 | PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n", |
| 1491 | PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData)); |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1492 | if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1493 | i64 offset = (pgno-1)*(i64)pPager->pageSize; |
| 1494 | rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 1495 | if( pPg ){ |
| 1496 | makeClean(pPg); |
| 1497 | } |
danielk1977 | a3f3a5f | 2004-06-10 04:32:16 +0000 | [diff] [blame] | 1498 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1499 | if( pPg ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1500 | /* No page should ever be explicitly rolled back that is in use, except |
| 1501 | ** for page 1 which is held in use in order to keep the lock on the |
| 1502 | ** database active. However such a page may be rolled back as a result |
| 1503 | ** of an internal error resulting in an automatic call to |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1504 | ** sqlite3PagerRollback(). |
drh | acf4ac9 | 2003-12-17 23:57:34 +0000 | [diff] [blame] | 1505 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1506 | void *pData; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1507 | /* assert( pPg->nRef==0 || pPg->pgno==1 ); */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1508 | pData = PGHDR_TO_DATA(pPg); |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1509 | memcpy(pData, aData, pPager->pageSize); |
danielk1977 | 9038bb6 | 2007-04-09 11:20:54 +0000 | [diff] [blame] | 1510 | if( pPager->xReiniter ){ |
| 1511 | pPager->xReiniter(pPg, pPager->pageSize); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1512 | } |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1513 | #ifdef SQLITE_CHECK_PAGES |
drh | 9636284 | 2005-03-20 22:47:56 +0000 | [diff] [blame] | 1514 | pPg->pageHash = pager_pagehash(pPg); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 1515 | #endif |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1516 | /* If this was page 1, then restore the value of Pager.dbFileVers. |
| 1517 | ** Do this before any decoding. */ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1518 | if( pgno==1 ){ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1519 | memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 1520 | } |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 1521 | |
| 1522 | /* Decode the page just read from disk */ |
| 1523 | CODEC1(pPager, pData, pPg->pgno, 3); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1524 | } |
| 1525 | return rc; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1529 | ** Parameter zMaster is the name of a master journal file. A single journal |
| 1530 | ** file that referred to the master journal file has just been rolled back. |
| 1531 | ** This routine checks if it is possible to delete the master journal file, |
| 1532 | ** and does so if it is. |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1533 | ** |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1534 | ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not |
| 1535 | ** available for use within this function. |
| 1536 | ** |
| 1537 | ** |
drh | 726de59 | 2004-06-10 23:35:50 +0000 | [diff] [blame] | 1538 | ** The master journal file contains the names of all child journals. |
| 1539 | ** To tell if a master journal can be deleted, check to each of the |
| 1540 | ** children. If all children are either missing or do not refer to |
| 1541 | ** a different master journal, then this master journal can be deleted. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1542 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1543 | static int pager_delmaster(Pager *pPager, const char *zMaster){ |
| 1544 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1545 | int rc; |
| 1546 | int master_open = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1547 | sqlite3_file *pMaster; |
| 1548 | sqlite3_file *pJournal; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1549 | char *zMasterJournal = 0; /* Contents of master journal file */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1550 | i64 nMasterJournal; /* Size of master journal file */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1551 | |
| 1552 | /* Open the master journal file exclusively in case some other process |
| 1553 | ** is running this routine also. Not that it makes too much difference. |
| 1554 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1555 | pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1556 | pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1557 | if( !pMaster ){ |
| 1558 | rc = SQLITE_NOMEM; |
| 1559 | }else{ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1560 | int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); |
| 1561 | rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1562 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1563 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1564 | master_open = 1; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1565 | |
| 1566 | rc = sqlite3OsFileSize(pMaster, &nMasterJournal); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1567 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1568 | |
| 1569 | if( nMasterJournal>0 ){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1570 | char *zJournal; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1571 | char *zMasterPtr = 0; |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1572 | int nMasterPtr = pPager->pVfs->mxPathname+1; |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1573 | |
| 1574 | /* Load the entire master journal file into space obtained from |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1575 | ** sqlite3_malloc() and pointed to by zMasterJournal. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1576 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1577 | zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1578 | if( !zMasterJournal ){ |
| 1579 | rc = SQLITE_NOMEM; |
| 1580 | goto delmaster_out; |
| 1581 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1582 | zMasterPtr = &zMasterJournal[nMasterJournal]; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1583 | rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1584 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 1585 | |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1586 | zJournal = zMasterJournal; |
| 1587 | while( (zJournal-zMasterJournal)<nMasterJournal ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1588 | if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1589 | /* One of the journals pointed to by the master journal exists. |
| 1590 | ** Open it and check if it points at the master journal. If |
| 1591 | ** so, return without deleting the master journal file. |
| 1592 | */ |
drh | 3b7b78b | 2004-11-24 01:16:43 +0000 | [diff] [blame] | 1593 | int c; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1594 | int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL); |
| 1595 | rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1596 | if( rc!=SQLITE_OK ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1597 | goto delmaster_out; |
| 1598 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1599 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1600 | rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1601 | sqlite3OsClose(pJournal); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 1602 | if( rc!=SQLITE_OK ){ |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 1603 | goto delmaster_out; |
| 1604 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1605 | |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1606 | c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0; |
drh | 3b7b78b | 2004-11-24 01:16:43 +0000 | [diff] [blame] | 1607 | if( c ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1608 | /* We have a match. Do not delete the master journal file. */ |
| 1609 | goto delmaster_out; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1610 | } |
| 1611 | } |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1612 | zJournal += (strlen(zJournal)+1); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1616 | rc = sqlite3OsDelete(pVfs, zMaster, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1617 | |
| 1618 | delmaster_out: |
| 1619 | if( zMasterJournal ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1620 | sqlite3_free(zMasterJournal); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1621 | } |
| 1622 | if( master_open ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1623 | sqlite3OsClose(pMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1624 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1625 | sqlite3_free(pMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1626 | return rc; |
| 1627 | } |
| 1628 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1629 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1630 | static void pager_truncate_cache(Pager *pPager); |
| 1631 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1632 | /* |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1633 | ** Truncate the main file of the given pager to the number of pages |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1634 | ** indicated. Also truncate the cached representation of the file. |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1635 | ** |
| 1636 | ** Might might be the case that the file on disk is smaller than nPage. |
| 1637 | ** This can happen, for example, if we are in the middle of a transaction |
| 1638 | ** which has extended the file size and the new pages are still all held |
| 1639 | ** in cache, then an INSERT or UPDATE does a statement rollback. Some |
| 1640 | ** operating system implementations can get confused if you try to |
| 1641 | ** truncate a file to some size that is larger than it currently is, |
| 1642 | ** so detect this case and do not do the truncation. |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1643 | */ |
| 1644 | static int pager_truncate(Pager *pPager, int nPage){ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1645 | int rc = SQLITE_OK; |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1646 | if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){ |
drh | 7fe3f7e | 2007-11-29 18:44:27 +0000 | [diff] [blame] | 1647 | i64 currentSize, newSize; |
| 1648 | rc = sqlite3OsFileSize(pPager->fd, ¤tSize); |
| 1649 | newSize = pPager->pageSize*(i64)nPage; |
| 1650 | if( rc==SQLITE_OK && currentSize>newSize ){ |
| 1651 | rc = sqlite3OsTruncate(pPager->fd, newSize); |
| 1652 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1653 | } |
| 1654 | if( rc==SQLITE_OK ){ |
| 1655 | pPager->dbSize = nPage; |
| 1656 | pager_truncate_cache(pPager); |
| 1657 | } |
| 1658 | return rc; |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | /* |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1662 | ** Set the sectorSize for the given pager. |
| 1663 | ** |
| 1664 | ** The sector size is the larger of the sector size reported |
| 1665 | ** by sqlite3OsSectorSize() and the pageSize. |
| 1666 | */ |
| 1667 | static void setSectorSize(Pager *pPager){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 1668 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 1669 | if( !pPager->tempFile ){ |
| 1670 | /* Sector size doesn't matter for temporary files. Also, the file |
| 1671 | ** may not have been opened yet, in whcih case the OsSectorSize() |
| 1672 | ** call will segfault. |
| 1673 | */ |
| 1674 | pPager->sectorSize = sqlite3OsSectorSize(pPager->fd); |
| 1675 | } |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1676 | if( pPager->sectorSize<pPager->pageSize ){ |
| 1677 | pPager->sectorSize = pPager->pageSize; |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1682 | ** Playback the journal and thus restore the database file to |
| 1683 | ** the state it was in before we started making changes. |
| 1684 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1685 | ** The journal file format is as follows: |
| 1686 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1687 | ** (1) 8 byte prefix. A copy of aJournalMagic[]. |
| 1688 | ** (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] | 1689 | ** in the journal. If this value is 0xffffffff, then compute the |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1690 | ** number of page records from the journal size. |
| 1691 | ** (3) 4 byte big-endian integer which is the initial value for the |
| 1692 | ** sanity checksum. |
| 1693 | ** (4) 4 byte integer which is the number of pages to truncate the |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1694 | ** database to during a rollback. |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1695 | ** (5) 4 byte integer which is the number of bytes in the master journal |
| 1696 | ** name. The value may be zero (indicate that there is no master |
| 1697 | ** journal.) |
| 1698 | ** (6) N bytes of the master journal name. The name will be nul-terminated |
| 1699 | ** and might be shorter than the value read from (5). If the first byte |
| 1700 | ** of the name is \000 then there is no master journal. The master |
| 1701 | ** journal name is stored in UTF-8. |
| 1702 | ** (7) Zero or more pages instances, each as follows: |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1703 | ** + 4 byte page number. |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1704 | ** + pPager->pageSize bytes of data. |
| 1705 | ** + 4 byte checksum |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1706 | ** |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1707 | ** When we speak of the journal header, we mean the first 6 items above. |
| 1708 | ** Each entry in the journal is an instance of the 7th item. |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1709 | ** |
| 1710 | ** Call the value from the second bullet "nRec". nRec is the number of |
| 1711 | ** valid page entries in the journal. In most cases, you can compute the |
| 1712 | ** value of nRec from the size of the journal file. But if a power |
| 1713 | ** failure occurred while the journal was being written, it could be the |
| 1714 | ** case that the size of the journal file had already been increased but |
| 1715 | ** the extra entries had not yet made it safely to disk. In such a case, |
| 1716 | ** the value of nRec computed from the file size would be too large. For |
| 1717 | ** that reason, we always use the nRec value in the header. |
| 1718 | ** |
| 1719 | ** If the nRec value is 0xffffffff it means that nRec should be computed |
| 1720 | ** from the file size. This value is used when the user selects the |
| 1721 | ** no-sync option for the journal. A power failure could lead to corruption |
| 1722 | ** in this case. But for things like temporary table (which will be |
| 1723 | ** deleted when the power is restored) we don't care. |
| 1724 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1725 | ** If the file opened as the journal file is not a well-formed |
danielk1977 | ece80f1 | 2004-06-23 01:05:26 +0000 | [diff] [blame] | 1726 | ** journal file then all pages up to the first corrupted page are rolled |
| 1727 | ** back (or no pages if the journal header is corrupted). The journal file |
| 1728 | ** is then deleted and SQLITE_OK returned, just as if no corruption had |
| 1729 | ** been encountered. |
| 1730 | ** |
| 1731 | ** If an I/O or malloc() error occurs, the journal-file is not deleted |
| 1732 | ** and an error code is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1733 | */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1734 | static int pager_playback(Pager *pPager, int isHot){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1735 | sqlite3_vfs *pVfs = pPager->pVfs; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1736 | i64 szJ; /* Size of the journal file in bytes */ |
danielk1977 | c3e8f5e | 2004-06-28 01:16:46 +0000 | [diff] [blame] | 1737 | u32 nRec; /* Number of Records in the journal */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1738 | int i; /* Loop counter */ |
| 1739 | Pgno mxPg = 0; /* Size of the original file in pages */ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1740 | int rc; /* Result code of a subroutine */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1741 | char *zMaster = 0; /* Name of master journal file if any */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1742 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1743 | /* Figure out how many records are in the journal. Abort early if |
| 1744 | ** the journal is empty. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1745 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1746 | assert( pPager->journalOpen ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1747 | rc = sqlite3OsFileSize(pPager->jfd, &szJ); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 1748 | if( rc!=SQLITE_OK || szJ==0 ){ |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1749 | goto end_playback; |
| 1750 | } |
drh | 240c579 | 2004-02-08 00:40:52 +0000 | [diff] [blame] | 1751 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1752 | /* Read the master journal name from the journal, if it is present. |
| 1753 | ** If a master journal file name is specified, but the file is not |
| 1754 | ** present on disk, then the journal is not hot and does not need to be |
| 1755 | ** played back. |
drh | 240c579 | 2004-02-08 00:40:52 +0000 | [diff] [blame] | 1756 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1757 | zMaster = pPager->pTmpSpace; |
| 1758 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1759 | assert( rc!=SQLITE_DONE ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1760 | if( rc!=SQLITE_OK |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1761 | || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS)) |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1762 | ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1763 | zMaster = 0; |
| 1764 | if( rc==SQLITE_DONE ) rc = SQLITE_OK; |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1765 | goto end_playback; |
| 1766 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1767 | pPager->journalOff = 0; |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1768 | zMaster = 0; |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1769 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1770 | /* This loop terminates either when the readJournalHdr() call returns |
| 1771 | ** SQLITE_DONE or an IO error occurs. */ |
| 1772 | while( 1 ){ |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1773 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1774 | /* Read the next journal header from the journal file. If there are |
| 1775 | ** not enough bytes left in the journal file for a complete header, or |
| 1776 | ** it is corrupted, then a process must of failed while writing it. |
| 1777 | ** This indicates nothing more needs to be rolled back. |
| 1778 | */ |
| 1779 | rc = readJournalHdr(pPager, szJ, &nRec, &mxPg); |
| 1780 | if( rc!=SQLITE_OK ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1781 | if( rc==SQLITE_DONE ){ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1782 | rc = SQLITE_OK; |
| 1783 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1784 | goto end_playback; |
| 1785 | } |
| 1786 | |
| 1787 | /* If nRec is 0xffffffff, then this journal was created by a process |
| 1788 | ** working in no-sync mode. This means that the rest of the journal |
| 1789 | ** file consists of pages, there are no more journal headers. Compute |
| 1790 | ** the value of nRec based on this assumption. |
| 1791 | */ |
| 1792 | if( nRec==0xffffffff ){ |
| 1793 | assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ); |
| 1794 | nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager); |
| 1795 | } |
| 1796 | |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1797 | /* 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] | 1798 | ** process and if this is the final header in the journal, then it means |
| 1799 | ** that this part of the journal was being filled but has not yet been |
| 1800 | ** synced to disk. Compute the number of pages based on the remaining |
| 1801 | ** size of the file. |
| 1802 | ** |
| 1803 | ** The third term of the test was added to fix ticket #2565. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1804 | */ |
drh | 8940f4e | 2007-08-11 00:26:20 +0000 | [diff] [blame] | 1805 | if( nRec==0 && !isHot && |
| 1806 | pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 1807 | nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager); |
| 1808 | } |
| 1809 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1810 | /* If this is the first header read from the journal, truncate the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 1811 | ** database file back to its original size. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1812 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1813 | if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){ |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 1814 | rc = pager_truncate(pPager, mxPg); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1815 | if( rc!=SQLITE_OK ){ |
| 1816 | goto end_playback; |
| 1817 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1820 | /* Copy original pages out of the journal and back into the database file. |
| 1821 | */ |
| 1822 | for(i=0; i<nRec; i++){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1823 | rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1824 | if( rc!=SQLITE_OK ){ |
| 1825 | if( rc==SQLITE_DONE ){ |
| 1826 | rc = SQLITE_OK; |
| 1827 | pPager->journalOff = szJ; |
| 1828 | break; |
| 1829 | }else{ |
| 1830 | goto end_playback; |
| 1831 | } |
| 1832 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1833 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1834 | } |
drh | 580eeaf | 2006-02-24 03:09:37 +0000 | [diff] [blame] | 1835 | /*NOTREACHED*/ |
| 1836 | assert( 0 ); |
drh | 4a0681e | 2003-02-13 01:58:20 +0000 | [diff] [blame] | 1837 | |
| 1838 | end_playback: |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 1839 | if( rc==SQLITE_OK ){ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1840 | zMaster = pPager->pTmpSpace; |
| 1841 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
| 1842 | } |
| 1843 | if( rc==SQLITE_OK ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1844 | rc = pager_end_transaction(pPager); |
danielk1977 | 8191bff | 2004-06-28 04:52:30 +0000 | [diff] [blame] | 1845 | } |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1846 | if( rc==SQLITE_OK && zMaster[0] ){ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1847 | /* If there was a master journal and this routine will return success, |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1848 | ** see if it is possible to delete the master journal. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1849 | */ |
danielk1977 | 65839c6 | 2007-08-30 08:08:17 +0000 | [diff] [blame] | 1850 | rc = pager_delmaster(pPager, zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1851 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1852 | |
| 1853 | /* The Pager.sectorSize variable may have been updated while rolling |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 1854 | ** back a journal created by a process with a different sector size |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1855 | ** value. Reset it to the correct value for this process. |
| 1856 | */ |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 1857 | setSectorSize(pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1858 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1862 | ** Playback the statement journal. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1863 | ** |
| 1864 | ** This is similar to playing back the transaction journal but with |
| 1865 | ** a few extra twists. |
| 1866 | ** |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1867 | ** (1) The number of pages in the database file at the start of |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1868 | ** the statement is stored in pPager->stmtSize, not in the |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1869 | ** journal file itself. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1870 | ** |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1871 | ** (2) In addition to playing back the statement journal, also |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1872 | ** playback all pages of the transaction journal beginning |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1873 | ** at offset pPager->stmtJSize. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1874 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1875 | static int pager_stmt_playback(Pager *pPager){ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1876 | i64 szJ; /* Size of the full journal */ |
| 1877 | i64 hdrOff; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1878 | int nRec; /* Number of Records */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1879 | int i; /* Loop counter */ |
| 1880 | int rc; |
| 1881 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1882 | szJ = pPager->journalOff; |
| 1883 | #ifndef NDEBUG |
| 1884 | { |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 1885 | i64 os_szJ; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1886 | rc = sqlite3OsFileSize(pPager->jfd, &os_szJ); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1887 | if( rc!=SQLITE_OK ) return rc; |
| 1888 | assert( szJ==os_szJ ); |
| 1889 | } |
| 1890 | #endif |
| 1891 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 1892 | /* Set hdrOff to be the offset just after the end of the last journal |
| 1893 | ** page written before the first journal-header for this statement |
| 1894 | ** transaction was written, or the end of the file if no journal |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1895 | ** header was written. |
| 1896 | */ |
| 1897 | hdrOff = pPager->stmtHdrOff; |
| 1898 | assert( pPager->fullSync || !hdrOff ); |
| 1899 | if( !hdrOff ){ |
| 1900 | hdrOff = szJ; |
| 1901 | } |
| 1902 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1903 | /* Truncate the database back to its original size. |
| 1904 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 1905 | rc = pager_truncate(pPager, pPager->stmtSize); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 1906 | assert( pPager->state>=PAGER_SHARED ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1907 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1908 | /* Figure out how many records are in the statement journal. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1909 | */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1910 | assert( pPager->stmtInUse && pPager->journalOpen ); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1911 | nRec = pPager->stmtNRec; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1912 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 1913 | /* Copy original pages out of the statement journal and back into the |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 1914 | ** database file. Note that the statement journal omits checksums from |
| 1915 | ** each record since power-failure recovery is not important to statement |
| 1916 | ** journals. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1917 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1918 | for(i=0; i<nRec; i++){ |
| 1919 | i64 offset = i*(4+pPager->pageSize); |
| 1920 | rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1921 | assert( rc!=SQLITE_DONE ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1922 | if( rc!=SQLITE_OK ) goto end_stmt_playback; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1925 | /* Now roll some pages back from the transaction journal. Pager.stmtJSize |
| 1926 | ** was the size of the journal file when this statement was started, so |
| 1927 | ** everything after that needs to be rolled back, either into the |
| 1928 | ** database, the memory cache, or both. |
| 1929 | ** |
| 1930 | ** If it is not zero, then Pager.stmtHdrOff is the offset to the start |
| 1931 | ** of the first journal header written during this statement transaction. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1932 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1933 | pPager->journalOff = pPager->stmtJSize; |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1934 | pPager->cksumInit = pPager->stmtCksum; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 1935 | while( pPager->journalOff < hdrOff ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1936 | rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1937 | assert( rc!=SQLITE_DONE ); |
| 1938 | if( rc!=SQLITE_OK ) goto end_stmt_playback; |
| 1939 | } |
| 1940 | |
| 1941 | while( pPager->journalOff < szJ ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1942 | u32 nJRec; /* Number of Journal Records */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1943 | u32 dummy; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1944 | rc = readJournalHdr(pPager, szJ, &nJRec, &dummy); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1945 | if( rc!=SQLITE_OK ){ |
| 1946 | assert( rc!=SQLITE_DONE ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1947 | goto end_stmt_playback; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 1948 | } |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1949 | if( nJRec==0 ){ |
| 1950 | nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8); |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1951 | } |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1952 | for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1953 | rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1954 | assert( rc!=SQLITE_DONE ); |
| 1955 | if( rc!=SQLITE_OK ) goto end_stmt_playback; |
| 1956 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1957 | } |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 1958 | |
| 1959 | pPager->journalOff = szJ; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1960 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1961 | end_stmt_playback: |
danielk1977 | 8a7aea3 | 2006-01-23 15:25:48 +0000 | [diff] [blame] | 1962 | if( rc==SQLITE_OK) { |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 1963 | pPager->journalOff = szJ; |
| 1964 | /* pager_reload_cache(pPager); */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1965 | } |
| 1966 | return rc; |
| 1967 | } |
| 1968 | |
| 1969 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1970 | ** Change the maximum number of in-memory pages that are allowed. |
| 1971 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1972 | void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){ |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1973 | if( mxPage>10 ){ |
| 1974 | pPager->mxPage = mxPage; |
danielk1977 | ef317ab | 2004-06-23 10:43:10 +0000 | [diff] [blame] | 1975 | }else{ |
| 1976 | pPager->mxPage = 10; |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1981 | ** Adjust the robustness of the database to damage due to OS crashes |
| 1982 | ** or power failures by changing the number of syncs()s when writing |
| 1983 | ** the rollback journal. There are three levels: |
| 1984 | ** |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1985 | ** OFF sqlite3OsSync() is never called. This is the default |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1986 | ** for temporary and transient files. |
| 1987 | ** |
| 1988 | ** NORMAL The journal is synced once before writes begin on the |
| 1989 | ** database. This is normally adequate protection, but |
| 1990 | ** it is theoretically possible, though very unlikely, |
| 1991 | ** that an inopertune power failure could leave the journal |
| 1992 | ** in a state which would cause damage to the database |
| 1993 | ** when it is rolled back. |
| 1994 | ** |
| 1995 | ** FULL The journal is synced twice before writes begin on the |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 1996 | ** database (with some additional information - the nRec field |
| 1997 | ** of the journal header - being written in between the two |
| 1998 | ** syncs). If we assume that writing a |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1999 | ** single disk sector is atomic, then this mode provides |
| 2000 | ** assurance that the journal will not be corrupted to the |
| 2001 | ** point of causing damage to the database during rollback. |
| 2002 | ** |
| 2003 | ** Numeric values associated with these states are OFF==1, NORMAL=2, |
| 2004 | ** and FULL=3. |
| 2005 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2006 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2007 | void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){ |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2008 | pPager->noSync = level==1 || pPager->tempFile; |
| 2009 | pPager->fullSync = level==3 && !pPager->tempFile; |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2010 | pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2011 | if( pPager->noSync ) pPager->needSync = 0; |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2012 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2013 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2014 | |
| 2015 | /* |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 2016 | ** The following global variable is incremented whenever the library |
| 2017 | ** attempts to open a temporary file. This information is used for |
| 2018 | ** testing and analysis only. |
| 2019 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2020 | #ifdef SQLITE_TEST |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 2021 | int sqlite3_opentemp_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2022 | #endif |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 2023 | |
| 2024 | /* |
drh | 3f56e6e | 2007-03-15 01:16:47 +0000 | [diff] [blame] | 2025 | ** Open a temporary file. |
| 2026 | ** |
| 2027 | ** Write the file descriptor into *fd. Return SQLITE_OK on success or some |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2028 | ** other error code if we fail. The OS will automatically delete the temporary |
| 2029 | ** file when it is closed. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2030 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2031 | static int sqlite3PagerOpentemp( |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2032 | sqlite3_vfs *pVfs, /* The virtual file system layer */ |
| 2033 | sqlite3_file *pFile, /* Write the file descriptor here */ |
| 2034 | char *zFilename, /* Name of the file. Might be NULL */ |
| 2035 | int vfsFlags /* Flags passed through to the VFS */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2036 | ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2037 | int rc; |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 2038 | assert( zFilename!=0 ); |
drh | 3f56e6e | 2007-03-15 01:16:47 +0000 | [diff] [blame] | 2039 | |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2040 | #ifdef SQLITE_TEST |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 2041 | sqlite3_opentemp_count++; /* Used for testing and analysis only */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2042 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2043 | |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2044 | vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
| 2045 | SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE; |
| 2046 | rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0); |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2047 | assert( rc!=SQLITE_OK || pFile->pMethods ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2048 | return rc; |
| 2049 | } |
| 2050 | |
| 2051 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2052 | ** 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] | 2053 | ** 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] | 2054 | ** the first call to sqlite3PagerGet() and is only held open until the |
| 2055 | ** last page is released using sqlite3PagerUnref(). |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2056 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2057 | ** If zFilename is NULL then a randomly-named temporary file is created |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2058 | ** and used as the file to be cached. The file will be deleted |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2059 | ** automatically when it is closed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2060 | ** |
| 2061 | ** If zFilename is ":memory:" then all information is held in cache. |
| 2062 | ** It is never written to disk. This can be used to implement an |
| 2063 | ** in-memory database. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2064 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2065 | int sqlite3PagerOpen( |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2066 | sqlite3_vfs *pVfs, /* The virtual file system to use */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2067 | Pager **ppPager, /* Return the Pager structure here */ |
| 2068 | const char *zFilename, /* Name of the database file to open */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 2069 | int nExtra, /* Extra bytes append to each in-memory page */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2070 | int flags, /* flags controlling this file */ |
| 2071 | int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2072 | ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2073 | u8 *pPtr; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2074 | Pager *pPager = 0; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2075 | int rc = SQLITE_OK; |
| 2076 | int i; |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 2077 | int tempFile = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2078 | int memDb = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2079 | int readOnly = 0; |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 2080 | int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; |
| 2081 | int noReadlock = (flags & PAGER_NO_READLOCK)!=0; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 2082 | int journalFileSize = sqlite3JournalSize(pVfs); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2083 | int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE; |
| 2084 | char *zPathname; |
| 2085 | int nPathname; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2086 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2087 | /* The default return is a NULL pointer */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2088 | *ppPager = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2089 | |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2090 | /* Compute the full pathname */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 2091 | nPathname = pVfs->mxPathname+1; |
| 2092 | zPathname = sqlite3_malloc(nPathname); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2093 | if( zPathname==0 ){ |
| 2094 | return SQLITE_NOMEM; |
| 2095 | } |
| 2096 | if( zFilename && zFilename[0] ){ |
| 2097 | #ifndef SQLITE_OMIT_MEMORYDB |
| 2098 | if( strcmp(zFilename,":memory:")==0 ){ |
| 2099 | memDb = 1; |
| 2100 | zPathname[0] = 0; |
| 2101 | }else |
| 2102 | #endif |
| 2103 | { |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 2104 | rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2105 | } |
| 2106 | }else{ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 2107 | rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname); |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2108 | } |
| 2109 | if( rc!=SQLITE_OK ){ |
| 2110 | sqlite3_free(zPathname); |
| 2111 | return rc; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2112 | } |
| 2113 | nPathname = strlen(zPathname); |
| 2114 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2115 | /* Allocate memory for the pager structure */ |
| 2116 | pPager = sqlite3MallocZero( |
| 2117 | sizeof(*pPager) + /* Pager structure */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 2118 | journalFileSize + /* The journal file structure */ |
drh | 17c0ba2 | 2008-02-26 18:40:11 +0000 | [diff] [blame] | 2119 | pVfs->szOsFile * 3 + /* The main db and two journal files */ |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 2120 | 4*nPathname + 40 /* zFilename, zDirectory, zJournal, zStmtJrnl */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2121 | ); |
| 2122 | if( !pPager ){ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2123 | sqlite3_free(zPathname); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2124 | return SQLITE_NOMEM; |
| 2125 | } |
| 2126 | pPtr = (u8 *)&pPager[1]; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2127 | pPager->vfsFlags = vfsFlags; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2128 | pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0]; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 2129 | pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1]; |
| 2130 | pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2]; |
| 2131 | pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize]; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2132 | pPager->zDirectory = &pPager->zFilename[nPathname+1]; |
| 2133 | pPager->zJournal = &pPager->zDirectory[nPathname+1]; |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 2134 | pPager->zStmtJrnl = &pPager->zJournal[nPathname+10]; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2135 | pPager->pVfs = pVfs; |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2136 | memcpy(pPager->zFilename, zPathname, nPathname+1); |
| 2137 | sqlite3_free(zPathname); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2138 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2139 | /* Open the pager file. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2140 | */ |
drh | ae28c01 | 2007-08-24 16:29:23 +0000 | [diff] [blame] | 2141 | if( zFilename && zFilename[0] && !memDb ){ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2142 | if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){ |
| 2143 | rc = SQLITE_CANTOPEN; |
| 2144 | }else{ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2145 | int fout = 0; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2146 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, |
| 2147 | pPager->vfsFlags, &fout); |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2148 | readOnly = (fout&SQLITE_OPEN_READONLY); |
| 2149 | |
| 2150 | /* If the file was successfully opened for read/write access, |
| 2151 | ** choose a default page size in case we have to create the |
| 2152 | ** database file. The default page size is the maximum of: |
| 2153 | ** |
| 2154 | ** + SQLITE_DEFAULT_PAGE_SIZE, |
| 2155 | ** + The value returned by sqlite3OsSectorSize() |
| 2156 | ** + The largest page size that can be written atomically. |
| 2157 | */ |
| 2158 | if( rc==SQLITE_OK && !readOnly ){ |
| 2159 | int iSectorSize = sqlite3OsSectorSize(pPager->fd); |
| 2160 | if( nDefaultPage<iSectorSize ){ |
| 2161 | nDefaultPage = iSectorSize; |
| 2162 | } |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2163 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2164 | { |
| 2165 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| 2166 | int ii; |
| 2167 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 2168 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 2169 | assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536); |
| 2170 | for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){ |
| 2171 | if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii; |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2172 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2173 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2174 | #endif |
| 2175 | if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ |
| 2176 | nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE; |
| 2177 | } |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 2178 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2179 | } |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2180 | }else if( !memDb ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2181 | /* If a temporary file is requested, it is not opened immediately. |
| 2182 | ** In this case we accept the default page size and delay actually |
| 2183 | ** opening the file until the first call to OsWrite(). |
| 2184 | */ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2185 | tempFile = 1; |
| 2186 | pPager->state = PAGER_EXCLUSIVE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2187 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2188 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2189 | if( pPager && rc==SQLITE_OK ){ |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2190 | pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage); |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 2191 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2192 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2193 | /* If an error occured in either of the blocks above. |
| 2194 | ** Free the Pager structure and close the file. |
| 2195 | ** Since the pager is not allocated there is no need to set |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2196 | ** any Pager.errMask variables. |
| 2197 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2198 | if( !pPager || !pPager->pTmpSpace ){ |
| 2199 | sqlite3OsClose(pPager->fd); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2200 | sqlite3_free(pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2201 | return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2202 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2203 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2204 | PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename); |
| 2205 | IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename)) |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2206 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2207 | /* Fill in Pager.zDirectory[] */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2208 | memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2209 | for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){} |
drh | a76c82e | 2003-07-27 18:59:42 +0000 | [diff] [blame] | 2210 | if( i>0 ) pPager->zDirectory[i-1] = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2211 | |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 2212 | /* Fill in Pager.zJournal[] and Pager.zStmtJrnl[] */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2213 | memcpy(pPager->zJournal, pPager->zFilename, nPathname); |
| 2214 | memcpy(&pPager->zJournal[nPathname], "-journal", 9); |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 2215 | memcpy(pPager->zStmtJrnl, pPager->zFilename, nPathname); |
| 2216 | memcpy(&pPager->zStmtJrnl[nPathname], "-stmtjrnl", 10); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2217 | |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2218 | /* pPager->journalOpen = 0; */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2219 | pPager->useJournal = useJournal && !memDb; |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 2220 | pPager->noReadlock = noReadlock && readOnly; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2221 | /* pPager->stmtOpen = 0; */ |
| 2222 | /* pPager->stmtInUse = 0; */ |
| 2223 | /* pPager->nRef = 0; */ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2224 | pPager->dbSize = memDb-1; |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2225 | pPager->pageSize = nDefaultPage; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2226 | /* pPager->stmtSize = 0; */ |
| 2227 | /* pPager->stmtJSize = 0; */ |
| 2228 | /* pPager->nPage = 0; */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2229 | pPager->mxPage = 100; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2230 | pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2231 | /* pPager->state = PAGER_UNLOCK; */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 2232 | assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) ); |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2233 | /* pPager->errMask = 0; */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2234 | pPager->tempFile = tempFile; |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 2235 | assert( tempFile==PAGER_LOCKINGMODE_NORMAL |
| 2236 | || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 2237 | assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); |
| 2238 | pPager->exclusiveMode = tempFile; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2239 | pPager->memDb = memDb; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2240 | pPager->readOnly = readOnly; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2241 | /* pPager->needSync = 0; */ |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 2242 | pPager->noSync = pPager->tempFile || !useJournal; |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 2243 | pPager->fullSync = (pPager->noSync?0:1); |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2244 | pPager->sync_flags = SQLITE_SYNC_NORMAL; |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2245 | /* pPager->pFirst = 0; */ |
| 2246 | /* pPager->pFirstSynced = 0; */ |
| 2247 | /* pPager->pLast = 0; */ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 2248 | pPager->nExtra = FORCE_ALIGNMENT(nExtra); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2249 | assert(pPager->fd->pMethods||memDb||tempFile); |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 2250 | if( !memDb ){ |
drh | c80f058 | 2007-05-01 16:59:48 +0000 | [diff] [blame] | 2251 | setSectorSize(pPager); |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 2252 | } |
drh | 3b59a5c | 2006-01-15 20:28:28 +0000 | [diff] [blame] | 2253 | /* pPager->pBusyHandler = 0; */ |
| 2254 | /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2255 | *ppPager = pPager; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2256 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2257 | pPager->iInUseMM = 0; |
| 2258 | pPager->iInUseDB = 0; |
| 2259 | if( !memDb ){ |
| 2260 | sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
| 2261 | sqlite3_mutex_enter(mutex); |
| 2262 | pPager->pNext = sqlite3PagerList; |
| 2263 | if( sqlite3PagerList ){ |
| 2264 | assert( sqlite3PagerList->pPrev==0 ); |
| 2265 | sqlite3PagerList->pPrev = pPager; |
| 2266 | } |
| 2267 | pPager->pPrev = 0; |
| 2268 | sqlite3PagerList = pPager; |
| 2269 | sqlite3_mutex_leave(mutex); |
| 2270 | } |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 2271 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2272 | return SQLITE_OK; |
| 2273 | } |
| 2274 | |
| 2275 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2276 | ** Set the busy handler function. |
| 2277 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2278 | void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2279 | pPager->pBusyHandler = pBusyHandler; |
| 2280 | } |
| 2281 | |
| 2282 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2283 | ** Set the destructor for this pager. If not NULL, the destructor is called |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2284 | ** when the reference count on each page reaches zero. The destructor can |
| 2285 | ** 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] | 2286 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2287 | ** The destructor is not called as a result sqlite3PagerClose(). |
| 2288 | ** Destructors are only called by sqlite3PagerUnref(). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2289 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2290 | void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2291 | pPager->xDestructor = xDesc; |
| 2292 | } |
| 2293 | |
| 2294 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2295 | ** Set the reinitializer for this pager. If not NULL, the reinitializer |
| 2296 | ** is called when the content of a page in cache is restored to its original |
| 2297 | ** value as a result of a rollback. The callback gives higher-level code |
| 2298 | ** an opportunity to restore the EXTRA section to agree with the restored |
| 2299 | ** page data. |
| 2300 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2301 | void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2302 | pPager->xReiniter = xReinit; |
| 2303 | } |
| 2304 | |
| 2305 | /* |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2306 | ** Set the page size to *pPageSize. If the suggest new page size is |
| 2307 | ** inappropriate, then an alternative page size is set to that |
| 2308 | ** value before returning. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2309 | */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2310 | int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){ |
| 2311 | int rc = SQLITE_OK; |
| 2312 | u16 pageSize = *pPageSize; |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 2313 | assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2314 | if( pageSize && pageSize!=pPager->pageSize |
| 2315 | && !pPager->memDb && pPager->nRef==0 |
| 2316 | ){ |
| 2317 | char *pNew = (char *)sqlite3_malloc(pageSize); |
| 2318 | if( !pNew ){ |
| 2319 | rc = SQLITE_NOMEM; |
| 2320 | }else{ |
| 2321 | pagerEnter(pPager); |
| 2322 | pager_reset(pPager); |
| 2323 | pPager->pageSize = pageSize; |
| 2324 | setSectorSize(pPager); |
| 2325 | sqlite3_free(pPager->pTmpSpace); |
| 2326 | pPager->pTmpSpace = pNew; |
| 2327 | pagerLeave(pPager); |
| 2328 | } |
drh | 1c7880e | 2005-05-20 20:01:55 +0000 | [diff] [blame] | 2329 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2330 | *pPageSize = pPager->pageSize; |
| 2331 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | /* |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 2335 | ** Return a pointer to the "temporary page" buffer held internally |
| 2336 | ** by the pager. This is a buffer that is big enough to hold the |
| 2337 | ** entire content of a database page. This buffer is used internally |
| 2338 | ** during rollback and will be overwritten whenever a rollback |
| 2339 | ** occurs. But other modules are free to use it too, as long as |
| 2340 | ** no rollbacks are happening. |
| 2341 | */ |
| 2342 | void *sqlite3PagerTempSpace(Pager *pPager){ |
| 2343 | return pPager->pTmpSpace; |
| 2344 | } |
| 2345 | |
| 2346 | /* |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2347 | ** Attempt to set the maximum database page count if mxPage is positive. |
| 2348 | ** Make no changes if mxPage is zero or negative. And never reduce the |
| 2349 | ** maximum page count below the current size of the database. |
| 2350 | ** |
| 2351 | ** Regardless of mxPage, return the current maximum page count. |
| 2352 | */ |
| 2353 | int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ |
| 2354 | if( mxPage>0 ){ |
| 2355 | pPager->mxPgno = mxPage; |
| 2356 | } |
| 2357 | sqlite3PagerPagecount(pPager); |
| 2358 | return pPager->mxPgno; |
| 2359 | } |
| 2360 | |
| 2361 | /* |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2362 | ** The following set of routines are used to disable the simulated |
| 2363 | ** I/O error mechanism. These routines are used to avoid simulated |
| 2364 | ** errors in places where we do not care about errors. |
| 2365 | ** |
| 2366 | ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops |
| 2367 | ** and generate no code. |
| 2368 | */ |
| 2369 | #ifdef SQLITE_TEST |
| 2370 | extern int sqlite3_io_error_pending; |
| 2371 | extern int sqlite3_io_error_hit; |
| 2372 | static int saved_cnt; |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2373 | void disable_simulated_io_errors(void){ |
| 2374 | saved_cnt = sqlite3_io_error_pending; |
| 2375 | sqlite3_io_error_pending = -1; |
| 2376 | } |
| 2377 | void enable_simulated_io_errors(void){ |
| 2378 | sqlite3_io_error_pending = saved_cnt; |
| 2379 | } |
| 2380 | #else |
drh | 152410f | 2005-11-05 15:11:22 +0000 | [diff] [blame] | 2381 | # define disable_simulated_io_errors() |
| 2382 | # define enable_simulated_io_errors() |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 2383 | #endif |
| 2384 | |
| 2385 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2386 | ** Read the first N bytes from the beginning of the file into memory |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2387 | ** that pDest points to. |
| 2388 | ** |
| 2389 | ** No error checking is done. The rational for this is that this function |
| 2390 | ** may be called even if the file does not exist or contain a header. In |
| 2391 | ** these cases sqlite3OsRead() will return an error, to which the correct |
| 2392 | ** response is to zero the memory at pDest and continue. A real IO error |
| 2393 | ** will presumably recur and be picked up later (Todo: Think about this). |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2394 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2395 | int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){ |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2396 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2397 | memset(pDest, 0, N); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2398 | assert(MEMDB||pPager->fd->pMethods||pPager->tempFile); |
| 2399 | if( pPager->fd->pMethods ){ |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2400 | IOTRACE(("DBHDR %p 0 %d\n", pPager, N)) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2401 | rc = sqlite3OsRead(pPager->fd, pDest, N, 0); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2402 | if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 2403 | rc = SQLITE_OK; |
| 2404 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2405 | } |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 2406 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2410 | ** Return the total number of pages in the disk file associated with |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2411 | ** pPager. |
| 2412 | ** |
| 2413 | ** If the PENDING_BYTE lies on the page directly after the end of the |
| 2414 | ** file, then consider this page part of the file too. For example, if |
| 2415 | ** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the |
| 2416 | ** file is 4096 bytes, 5 is returned instead of 4. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2417 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2418 | int sqlite3PagerPagecount(Pager *pPager){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2419 | i64 n = 0; |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 2420 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2421 | assert( pPager!=0 ); |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 2422 | if( pPager->errCode ){ |
danielk1977 | 43468d9 | 2008-02-26 06:05:31 +0000 | [diff] [blame] | 2423 | return -1; |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 2424 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2425 | if( pPager->dbSize>=0 ){ |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2426 | n = pPager->dbSize; |
| 2427 | } else { |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2428 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 2429 | if( (pPager->fd->pMethods) |
| 2430 | && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2431 | pPager->nRef++; |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 2432 | pager_error(pPager, rc); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 2433 | pPager->nRef--; |
danielk1977 | 43468d9 | 2008-02-26 06:05:31 +0000 | [diff] [blame] | 2434 | return -1; |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2435 | } |
| 2436 | if( n>0 && n<pPager->pageSize ){ |
| 2437 | n = 1; |
| 2438 | }else{ |
| 2439 | n /= pPager->pageSize; |
| 2440 | } |
| 2441 | if( pPager->state!=PAGER_UNLOCK ){ |
| 2442 | pPager->dbSize = n; |
| 2443 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2444 | } |
danielk1977 | 15f411d | 2005-09-16 10:18:45 +0000 | [diff] [blame] | 2445 | if( n==(PENDING_BYTE/pPager->pageSize) ){ |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 2446 | n++; |
| 2447 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2448 | if( n>pPager->mxPgno ){ |
| 2449 | pPager->mxPgno = n; |
| 2450 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2451 | return n; |
| 2452 | } |
| 2453 | |
drh | f07e7d5 | 2006-04-07 13:54:46 +0000 | [diff] [blame] | 2454 | |
| 2455 | #ifndef SQLITE_OMIT_MEMORYDB |
| 2456 | /* |
| 2457 | ** Clear a PgHistory block |
| 2458 | */ |
| 2459 | static void clearHistory(PgHistory *pHist){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2460 | sqlite3_free(pHist->pOrig); |
| 2461 | sqlite3_free(pHist->pStmt); |
drh | f07e7d5 | 2006-04-07 13:54:46 +0000 | [diff] [blame] | 2462 | pHist->pOrig = 0; |
| 2463 | pHist->pStmt = 0; |
| 2464 | } |
| 2465 | #else |
| 2466 | #define clearHistory(x) |
| 2467 | #endif |
| 2468 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2469 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2470 | ** Forward declaration |
| 2471 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2472 | static int syncJournal(Pager*); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2473 | |
| 2474 | /* |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2475 | ** Unlink pPg from its hash chain. Also set the page number to 0 to indicate |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2476 | ** 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] | 2477 | ** sqlite3PagerMovepage() routine can leave a page in the |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2478 | ** pNextFree/pPrevFree list that is not a part of any hash-chain. |
| 2479 | */ |
| 2480 | static void unlinkHashChain(Pager *pPager, PgHdr *pPg){ |
| 2481 | if( pPg->pgno==0 ){ |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 2482 | assert( pPg->pNextHash==0 && pPg->pPrevHash==0 ); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2483 | return; |
| 2484 | } |
| 2485 | if( pPg->pNextHash ){ |
| 2486 | pPg->pNextHash->pPrevHash = pPg->pPrevHash; |
| 2487 | } |
| 2488 | if( pPg->pPrevHash ){ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 2489 | assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg ); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2490 | pPg->pPrevHash->pNextHash = pPg->pNextHash; |
| 2491 | }else{ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 2492 | int h = pPg->pgno & (pPager->nHash-1); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2493 | pPager->aHash[h] = pPg->pNextHash; |
| 2494 | } |
drh | 5229ae4 | 2006-03-23 23:29:04 +0000 | [diff] [blame] | 2495 | if( MEMDB ){ |
drh | 5229ae4 | 2006-03-23 23:29:04 +0000 | [diff] [blame] | 2496 | clearHistory(PGHDR_TO_HIST(pPg, pPager)); |
| 2497 | } |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2498 | pPg->pgno = 0; |
| 2499 | pPg->pNextHash = pPg->pPrevHash = 0; |
| 2500 | } |
| 2501 | |
| 2502 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2503 | ** Unlink a page from the free list (the list of all pages where nRef==0) |
| 2504 | ** and from its hash collision chain. |
| 2505 | */ |
| 2506 | static void unlinkPage(PgHdr *pPg){ |
| 2507 | Pager *pPager = pPg->pPager; |
| 2508 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2509 | /* Unlink from free page list */ |
| 2510 | lruListRemove(pPg); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2511 | |
| 2512 | /* Unlink from the pgno hash table */ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 2513 | unlinkHashChain(pPager, pPg); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
| 2516 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2517 | ** This routine is used to truncate the cache when a database |
| 2518 | ** is truncated. Drop from the cache all pages whose pgno is |
| 2519 | ** larger than pPager->dbSize and is unreferenced. |
| 2520 | ** |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2521 | ** Referenced pages larger than pPager->dbSize are zeroed. |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2522 | ** |
| 2523 | ** Actually, at the point this routine is called, it would be |
| 2524 | ** an error to have a referenced page. But rather than delete |
| 2525 | ** that page and guarantee a subsequent segfault, it seems better |
| 2526 | ** to zero it and hope that we error out sanely. |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2527 | */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2528 | static void pager_truncate_cache(Pager *pPager){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2529 | PgHdr *pPg; |
| 2530 | PgHdr **ppPg; |
| 2531 | int dbSize = pPager->dbSize; |
| 2532 | |
| 2533 | ppPg = &pPager->pAll; |
| 2534 | while( (pPg = *ppPg)!=0 ){ |
| 2535 | if( pPg->pgno<=dbSize ){ |
| 2536 | ppPg = &pPg->pNextAll; |
| 2537 | }else if( pPg->nRef>0 ){ |
| 2538 | memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize); |
| 2539 | ppPg = &pPg->pNextAll; |
| 2540 | }else{ |
| 2541 | *ppPg = pPg->pNextAll; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2542 | IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno)); |
| 2543 | PAGER_INCR(sqlite3_pager_pgfree_count); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2544 | unlinkPage(pPg); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 2545 | makeClean(pPg); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 2546 | sqlite3_free(pPg->pData); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2547 | sqlite3_free(pPg); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2548 | pPager->nPage--; |
| 2549 | } |
| 2550 | } |
| 2551 | } |
| 2552 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2553 | /* |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2554 | ** 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] | 2555 | ** is currently not available. Repeat until the busy callback returns |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2556 | ** false or until the lock succeeds. |
| 2557 | ** |
| 2558 | ** Return SQLITE_OK on success and an error code if we cannot obtain |
| 2559 | ** the lock. |
| 2560 | */ |
| 2561 | static int pager_wait_on_lock(Pager *pPager, int locktype){ |
| 2562 | int rc; |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2563 | |
| 2564 | /* The OS lock values must be the same as the Pager lock values */ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2565 | assert( PAGER_SHARED==SHARED_LOCK ); |
| 2566 | assert( PAGER_RESERVED==RESERVED_LOCK ); |
| 2567 | assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2568 | |
| 2569 | /* If the file is currently unlocked then the size must be unknown */ |
| 2570 | assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB ); |
| 2571 | |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2572 | if( pPager->state>=locktype ){ |
| 2573 | rc = SQLITE_OK; |
| 2574 | }else{ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2575 | do { |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2576 | rc = sqlite3OsLock(pPager->fd, locktype); |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 2577 | }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) ); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2578 | if( rc==SQLITE_OK ){ |
| 2579 | pPager->state = locktype; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2580 | IOTRACE(("LOCK %p %d\n", pPager, locktype)) |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2581 | } |
| 2582 | } |
| 2583 | return rc; |
| 2584 | } |
| 2585 | |
| 2586 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2587 | ** Truncate the file to the number of pages specified. |
| 2588 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2589 | int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2590 | int rc; |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 2591 | assert( pPager->state>=PAGER_SHARED || MEMDB ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2592 | sqlite3PagerPagecount(pPager); |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 2593 | if( pPager->errCode ){ |
| 2594 | rc = pPager->errCode; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 2595 | return rc; |
| 2596 | } |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 2597 | if( nPage>=(unsigned)pPager->dbSize ){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2598 | return SQLITE_OK; |
| 2599 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2600 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2601 | pPager->dbSize = nPage; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 2602 | pager_truncate_cache(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 2603 | return SQLITE_OK; |
| 2604 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2605 | pagerEnter(pPager); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2606 | rc = syncJournal(pPager); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2607 | pagerLeave(pPager); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 2608 | if( rc!=SQLITE_OK ){ |
| 2609 | return rc; |
| 2610 | } |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2611 | |
| 2612 | /* Get an exclusive lock on the database before truncating. */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2613 | pagerEnter(pPager); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2614 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2615 | pagerLeave(pPager); |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 2616 | if( rc!=SQLITE_OK ){ |
| 2617 | return rc; |
| 2618 | } |
| 2619 | |
drh | cb4c40b | 2004-08-18 19:09:43 +0000 | [diff] [blame] | 2620 | rc = pager_truncate(pPager, nPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 2621 | return rc; |
| 2622 | } |
| 2623 | |
| 2624 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2625 | ** Shutdown the page cache. Free all memory and close all files. |
| 2626 | ** |
| 2627 | ** If a transaction was in progress when this routine is called, that |
| 2628 | ** transaction is rolled back. All outstanding pages are invalidated |
| 2629 | ** and their memory is freed. Any attempt to use a page associated |
| 2630 | ** with this page cache after this function returns will likely |
| 2631 | ** result in a coredump. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2632 | ** |
| 2633 | ** This function always succeeds. If a transaction is active an attempt |
| 2634 | ** is made to roll it back. If an error occurs during the rollback |
| 2635 | ** a hot journal may be left in the filesystem but no error is returned |
| 2636 | ** to the caller. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2637 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2638 | int sqlite3PagerClose(Pager *pPager){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2639 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2640 | if( !MEMDB ){ |
| 2641 | sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
| 2642 | sqlite3_mutex_enter(mutex); |
| 2643 | if( pPager->pPrev ){ |
| 2644 | pPager->pPrev->pNext = pPager->pNext; |
| 2645 | }else{ |
| 2646 | sqlite3PagerList = pPager->pNext; |
| 2647 | } |
| 2648 | if( pPager->pNext ){ |
| 2649 | pPager->pNext->pPrev = pPager->pPrev; |
| 2650 | } |
| 2651 | sqlite3_mutex_leave(mutex); |
| 2652 | } |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 2653 | #endif |
| 2654 | |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2655 | disable_simulated_io_errors(); |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 2656 | pPager->errCode = 0; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 2657 | pPager->exclusiveMode = 0; |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2658 | pager_reset(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 2659 | pagerUnlockAndRollback(pPager); |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 2660 | enable_simulated_io_errors(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2661 | PAGERTRACE2("CLOSE %d\n", PAGERID(pPager)); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2662 | IOTRACE(("CLOSE %p\n", pPager)) |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 2663 | assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) ); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2664 | if( pPager->journalOpen ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2665 | sqlite3OsClose(pPager->jfd); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2666 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 2667 | sqlite3BitvecDestroy(pPager->pInJournal); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2668 | if( pPager->stmtOpen ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2669 | sqlite3OsClose(pPager->stfd); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 2670 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2671 | sqlite3OsClose(pPager->fd); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 2672 | /* Temp files are automatically deleted by the OS |
| 2673 | ** if( pPager->tempFile ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 2674 | ** sqlite3OsDelete(pPager->zFilename); |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 2675 | ** } |
| 2676 | */ |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 2677 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2678 | sqlite3_free(pPager->aHash); |
| 2679 | sqlite3_free(pPager->pTmpSpace); |
| 2680 | sqlite3_free(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2681 | return SQLITE_OK; |
| 2682 | } |
| 2683 | |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 2684 | #if !defined(NDEBUG) || defined(SQLITE_TEST) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2685 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2686 | ** Return the page number for the given page data. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2687 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2688 | Pgno sqlite3PagerPagenumber(DbPage *p){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2689 | return p->pgno; |
| 2690 | } |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 2691 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2692 | |
| 2693 | /* |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2694 | ** The page_ref() function increments the reference count for a page. |
| 2695 | ** 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] | 2696 | ** remove it from the freelist. |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2697 | ** |
| 2698 | ** For non-test systems, page_ref() is a macro that calls _page_ref() |
| 2699 | ** online of the reference count is zero. For test systems, page_ref() |
| 2700 | ** is a real function so that we can set breakpoints and trace it. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2701 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 2702 | static void _page_ref(PgHdr *pPg){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2703 | if( pPg->nRef==0 ){ |
| 2704 | /* The page is currently on the freelist. Remove it. */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2705 | lruListRemove(pPg); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2706 | pPg->pPager->nRef++; |
| 2707 | } |
| 2708 | pPg->nRef++; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2709 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 2710 | #ifdef SQLITE_DEBUG |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2711 | static void page_ref(PgHdr *pPg){ |
| 2712 | if( pPg->nRef==0 ){ |
| 2713 | _page_ref(pPg); |
| 2714 | }else{ |
| 2715 | pPg->nRef++; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2716 | } |
| 2717 | } |
| 2718 | #else |
| 2719 | # define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++) |
| 2720 | #endif |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2721 | |
| 2722 | /* |
| 2723 | ** Increment the reference count for a page. The input pointer is |
| 2724 | ** a reference to the page data. |
| 2725 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2726 | int sqlite3PagerRef(DbPage *pPg){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2727 | pagerEnter(pPg->pPager); |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 2728 | page_ref(pPg); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2729 | pagerLeave(pPg->pPager); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2730 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | /* |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2734 | ** Sync the journal. In other words, make sure all the pages that have |
| 2735 | ** been written to the journal have actually reached the surface of the |
| 2736 | ** disk. It is not safe to modify the original database file until after |
| 2737 | ** the journal has been synced. If the original database is modified before |
| 2738 | ** the journal is synced and a power failure occurs, the unsynced journal |
| 2739 | ** data would be lost and we would be unable to completely rollback the |
| 2740 | ** database changes. Database corruption would occur. |
| 2741 | ** |
| 2742 | ** This routine also updates the nRec field in the header of the journal. |
| 2743 | ** (See comments on the pager_playback() routine for additional information.) |
| 2744 | ** If the sync mode is FULL, two syncs will occur. First the whole journal |
| 2745 | ** is synced, then the nRec field is updated, then a second sync occurs. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2746 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2747 | ** For temporary databases, we do not care if we are able to rollback |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2748 | ** after a power failure, so no sync occurs. |
| 2749 | ** |
| 2750 | ** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which |
| 2751 | ** the database is stored, then OsSync() is never called on the journal |
| 2752 | ** file. In this case all that is required is to update the nRec field in |
| 2753 | ** the journal header. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2754 | ** |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2755 | ** This routine clears the needSync field of every page current held in |
| 2756 | ** memory. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2757 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2758 | static int syncJournal(Pager *pPager){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2759 | PgHdr *pPg; |
| 2760 | int rc = SQLITE_OK; |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2761 | |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2762 | |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2763 | /* Sync the journal before modifying the main database |
| 2764 | ** (assuming there is a journal and it needs to be synced.) |
| 2765 | */ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2766 | if( pPager->needSync ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2767 | if( !pPager->tempFile ){ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2768 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2769 | assert( pPager->journalOpen ); |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2770 | |
drh | 946966f | 2004-02-25 02:20:41 +0000 | [diff] [blame] | 2771 | /* assert( !pPager->noSync ); // noSync might be set if synchronous |
| 2772 | ** was turned off after the transaction was started. Ticket #615 */ |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2773 | #ifndef NDEBUG |
| 2774 | { |
drh | 34e79ce | 2004-02-08 06:05:46 +0000 | [diff] [blame] | 2775 | /* Make sure the pPager->nRec counter we are keeping agrees |
| 2776 | ** with the nRec computed from the size of the journal file. |
| 2777 | */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 2778 | i64 jSz; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2779 | rc = sqlite3OsFileSize(pPager->jfd, &jSz); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2780 | if( rc!=0 ) return rc; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2781 | assert( pPager->journalOff==jSz ); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2782 | } |
| 2783 | #endif |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2784 | if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2785 | /* Write the nRec value into the journal file header. If in |
| 2786 | ** full-synchronous mode, sync the journal first. This ensures that |
| 2787 | ** all data has really hit the disk before nRec is updated to mark |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2788 | ** it as a candidate for rollback. |
| 2789 | ** |
| 2790 | ** This is not required if the persistent media supports the |
| 2791 | ** SAFE_APPEND property. Because in this case it is not possible |
| 2792 | ** for garbage data to be appended to the file, the nRec field |
| 2793 | ** is populated with 0xFFFFFFFF when the journal header is written |
| 2794 | ** and never needs to be updated. |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 2795 | */ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2796 | i64 jrnlOff; |
| 2797 | if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2798 | PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2799 | IOTRACE(("JSYNC %p\n", pPager)) |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 2800 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags); |
drh | d8d66e8 | 2003-02-12 02:10:15 +0000 | [diff] [blame] | 2801 | if( rc!=0 ) return rc; |
| 2802 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2803 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2804 | jrnlOff = pPager->journalHdr + sizeof(aJournalMagic); |
| 2805 | IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4)); |
| 2806 | rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec); |
drh | b4746b9 | 2005-09-09 01:32:06 +0000 | [diff] [blame] | 2807 | if( rc ) return rc; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 2808 | } |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 2809 | if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
| 2810 | PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); |
| 2811 | IOTRACE(("JSYNC %p\n", pPager)) |
| 2812 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| |
| 2813 | (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) |
| 2814 | ); |
| 2815 | if( rc!=0 ) return rc; |
| 2816 | } |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2817 | pPager->journalStarted = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 2818 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2819 | pPager->needSync = 0; |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2820 | |
| 2821 | /* Erase the needSync flag from every page. |
| 2822 | */ |
| 2823 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 2824 | pPg->needSync = 0; |
| 2825 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2826 | lruListSetFirstSynced(pPager); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2827 | } |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2828 | |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2829 | #ifndef NDEBUG |
| 2830 | /* If the Pager.needSync flag is clear then the PgHdr.needSync |
| 2831 | ** flag must also be clear for all pages. Verify that this |
| 2832 | ** invariant is true. |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2833 | */ |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2834 | else{ |
| 2835 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 2836 | assert( pPg->needSync==0 ); |
| 2837 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 2838 | assert( pPager->lru.pFirstSynced==pPager->lru.pFirst ); |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 2839 | } |
drh | 341eae8 | 2003-01-21 02:39:36 +0000 | [diff] [blame] | 2840 | #endif |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 2841 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2842 | return rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | /* |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2846 | ** Merge two lists of pages connected by pDirty and in pgno order. |
| 2847 | ** Do not both fixing the pPrevDirty pointers. |
| 2848 | */ |
| 2849 | static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){ |
| 2850 | PgHdr result, *pTail; |
| 2851 | pTail = &result; |
| 2852 | while( pA && pB ){ |
| 2853 | if( pA->pgno<pB->pgno ){ |
| 2854 | pTail->pDirty = pA; |
| 2855 | pTail = pA; |
| 2856 | pA = pA->pDirty; |
| 2857 | }else{ |
| 2858 | pTail->pDirty = pB; |
| 2859 | pTail = pB; |
| 2860 | pB = pB->pDirty; |
| 2861 | } |
| 2862 | } |
| 2863 | if( pA ){ |
| 2864 | pTail->pDirty = pA; |
| 2865 | }else if( pB ){ |
| 2866 | pTail->pDirty = pB; |
| 2867 | }else{ |
| 2868 | pTail->pDirty = 0; |
| 2869 | } |
| 2870 | return result.pDirty; |
| 2871 | } |
| 2872 | |
| 2873 | /* |
| 2874 | ** Sort the list of pages in accending order by pgno. Pages are |
| 2875 | ** connected by pDirty pointers. The pPrevDirty pointers are |
| 2876 | ** corrupted by this sort. |
| 2877 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2878 | #define N_SORT_BUCKET_ALLOC 25 |
| 2879 | #define N_SORT_BUCKET 25 |
| 2880 | #ifdef SQLITE_TEST |
| 2881 | int sqlite3_pager_n_sort_bucket = 0; |
| 2882 | #undef N_SORT_BUCKET |
| 2883 | #define N_SORT_BUCKET \ |
| 2884 | (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC) |
| 2885 | #endif |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2886 | static PgHdr *sort_pagelist(PgHdr *pIn){ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2887 | PgHdr *a[N_SORT_BUCKET_ALLOC], *p; |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2888 | int i; |
| 2889 | memset(a, 0, sizeof(a)); |
| 2890 | while( pIn ){ |
| 2891 | p = pIn; |
| 2892 | pIn = p->pDirty; |
| 2893 | p->pDirty = 0; |
| 2894 | for(i=0; i<N_SORT_BUCKET-1; i++){ |
| 2895 | if( a[i]==0 ){ |
| 2896 | a[i] = p; |
| 2897 | break; |
| 2898 | }else{ |
| 2899 | p = merge_pagelist(a[i], p); |
| 2900 | a[i] = 0; |
| 2901 | } |
| 2902 | } |
| 2903 | if( i==N_SORT_BUCKET-1 ){ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2904 | /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET) |
| 2905 | ** elements in the input list. This is possible, but impractical. |
| 2906 | ** Testing this line is the point of global variable |
| 2907 | ** sqlite3_pager_n_sort_bucket. |
| 2908 | */ |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2909 | a[i] = merge_pagelist(a[i], p); |
| 2910 | } |
| 2911 | } |
| 2912 | p = a[0]; |
| 2913 | for(i=1; i<N_SORT_BUCKET; i++){ |
| 2914 | p = merge_pagelist(p, a[i]); |
| 2915 | } |
| 2916 | return p; |
| 2917 | } |
| 2918 | |
| 2919 | /* |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2920 | ** Given a list of pages (connected by the PgHdr.pDirty pointer) write |
| 2921 | ** every one of those pages out to the database file and mark them all |
| 2922 | ** as clean. |
| 2923 | */ |
| 2924 | static int pager_write_pagelist(PgHdr *pList){ |
| 2925 | Pager *pPager; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2926 | PgHdr *p; |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2927 | int rc; |
| 2928 | |
| 2929 | if( pList==0 ) return SQLITE_OK; |
| 2930 | pPager = pList->pPager; |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2931 | |
| 2932 | /* At this point there may be either a RESERVED or EXCLUSIVE lock on the |
| 2933 | ** database file. If there is already an EXCLUSIVE lock, the following |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2934 | ** calls to sqlite3OsLock() are no-ops. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2935 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2936 | ** Moving the lock from RESERVED to EXCLUSIVE actually involves going |
| 2937 | ** through an intermediate state PENDING. A PENDING lock prevents new |
| 2938 | ** readers from attaching to the database but is unsufficient for us to |
| 2939 | ** write. The idea of a PENDING lock is to prevent new readers from |
| 2940 | ** coming in while we wait for existing readers to clear. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2941 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2942 | ** While the pager is in the RESERVED state, the original database file |
| 2943 | ** is unchanged and we can rollback without having to playback the |
| 2944 | ** journal into the original database file. Once we transition to |
| 2945 | ** EXCLUSIVE, it means the database file has been changed and any rollback |
| 2946 | ** will require a journal playback. |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2947 | */ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2948 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 2949 | if( rc!=SQLITE_OK ){ |
| 2950 | return rc; |
| 2951 | } |
| 2952 | |
drh | dc3e10b | 2006-06-15 14:31:06 +0000 | [diff] [blame] | 2953 | pList = sort_pagelist(pList); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2954 | for(p=pList; p; p=p->pDirty){ |
| 2955 | assert( p->dirty ); |
| 2956 | p->dirty = 0; |
| 2957 | } |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2958 | while( pList ){ |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2959 | |
| 2960 | /* If the file has not yet been opened, open it now. */ |
| 2961 | if( !pPager->fd->pMethods ){ |
| 2962 | assert(pPager->tempFile); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2963 | rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename, |
| 2964 | pPager->vfsFlags); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 2965 | if( rc ) return rc; |
| 2966 | } |
| 2967 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2968 | /* If there are dirty pages in the page cache with page numbers greater |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2969 | ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2970 | ** make the file smaller (presumably by auto-vacuum code). Do not write |
| 2971 | ** any such pages to the file. |
| 2972 | */ |
| 2973 | if( pList->pgno<=pPager->dbSize ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2974 | i64 offset = (pList->pgno-1)*(i64)pPager->pageSize; |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 2975 | char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 2976 | PAGERTRACE4("STORE %d page %d hash(%08x)\n", |
| 2977 | PAGERID(pPager), pList->pgno, pager_pagehash(pList)); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2978 | IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno)); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2979 | rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 2980 | PAGER_INCR(sqlite3_pager_writedb_count); |
| 2981 | PAGER_INCR(pPager->nWrite); |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 2982 | if( pList->pgno==1 ){ |
| 2983 | memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); |
| 2984 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2985 | } |
| 2986 | #ifndef NDEBUG |
| 2987 | else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2988 | PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2989 | } |
| 2990 | #endif |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2991 | if( rc ) return rc; |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 2992 | #ifdef SQLITE_CHECK_PAGES |
| 2993 | pList->pageHash = pager_pagehash(pList); |
| 2994 | #endif |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 2995 | pList = pList->pDirty; |
| 2996 | } |
| 2997 | return SQLITE_OK; |
| 2998 | } |
| 2999 | |
| 3000 | /* |
| 3001 | ** Collect every dirty page into a dirty list and |
| 3002 | ** return a pointer to the head of that list. All pages are |
| 3003 | ** collected even if they are still in use. |
| 3004 | */ |
| 3005 | static PgHdr *pager_get_all_dirty_pages(Pager *pPager){ |
drh | 8f2e9a1 | 2008-02-26 14:46:04 +0000 | [diff] [blame] | 3006 | |
| 3007 | #ifndef NDEBUG |
| 3008 | /* Verify the sanity of the dirty list when we are running |
| 3009 | ** in debugging mode. This is expensive, so do not |
| 3010 | ** do this on a normal build. */ |
| 3011 | int n1 = 0; |
| 3012 | int n2 = 0; |
| 3013 | PgHdr *p; |
| 3014 | for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; } |
| 3015 | for(p=pPager->pDirty; p; p=p->pDirty){ n2++; } |
| 3016 | assert( n1==n2 ); |
| 3017 | #endif |
| 3018 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3019 | return pPager->pDirty; |
drh | 2554f8b | 2003-01-22 01:26:44 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /* |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3023 | ** Return TRUE if there is a hot journal on the given pager. |
| 3024 | ** A hot journal is one that needs to be played back. |
| 3025 | ** |
| 3026 | ** If the current size of the database file is 0 but a journal file |
| 3027 | ** exists, that is probably an old journal left over from a prior |
| 3028 | ** database with the same name. Just delete the journal. |
| 3029 | */ |
| 3030 | static int hasHotJournal(Pager *pPager){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3031 | sqlite3_vfs *pVfs = pPager->pVfs; |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3032 | if( !pPager->useJournal ) return 0; |
drh | d3cf247 | 2007-11-27 16:55:07 +0000 | [diff] [blame] | 3033 | if( !pPager->fd->pMethods ) return 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3034 | if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){ |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 3035 | return 0; |
| 3036 | } |
| 3037 | if( sqlite3OsCheckReservedLock(pPager->fd) ){ |
| 3038 | return 0; |
| 3039 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3040 | if( sqlite3PagerPagecount(pPager)==0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3041 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3042 | return 0; |
| 3043 | }else{ |
| 3044 | return 1; |
| 3045 | } |
| 3046 | } |
| 3047 | |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 3048 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3049 | ** Try to find a page in the cache that can be recycled. |
| 3050 | ** |
| 3051 | ** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3052 | ** does not set the pPager->errCode variable. |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 3053 | */ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3054 | static int pager_recycle(Pager *pPager, PgHdr **ppPg){ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3055 | PgHdr *pPg; |
| 3056 | *ppPg = 0; |
| 3057 | |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3058 | /* It is illegal to call this function unless the pager object |
| 3059 | ** pointed to by pPager has at least one free page (page with nRef==0). |
| 3060 | */ |
danielk1977 | c551edc | 2007-04-05 13:12:13 +0000 | [diff] [blame] | 3061 | assert(!MEMDB); |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3062 | assert(pPager->lru.pFirst); |
danielk1977 | c551edc | 2007-04-05 13:12:13 +0000 | [diff] [blame] | 3063 | |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3064 | /* Find a page to recycle. Try to locate a page that does not |
| 3065 | ** require us to do an fsync() on the journal. |
| 3066 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3067 | pPg = pPager->lru.pFirstSynced; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3068 | |
| 3069 | /* If we could not find a page that does not require an fsync() |
| 3070 | ** on the journal file then fsync the journal file. This is a |
| 3071 | ** very slow operation, so we work hard to avoid it. But sometimes |
| 3072 | ** it can't be helped. |
| 3073 | */ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3074 | if( pPg==0 && pPager->lru.pFirst){ |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 3075 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3076 | int rc = syncJournal(pPager); |
| 3077 | if( rc!=0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3078 | return rc; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3079 | } |
danielk1977 | 4cd2cd5 | 2007-08-23 14:48:23 +0000 | [diff] [blame] | 3080 | if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3081 | /* If in full-sync mode, write a new journal header into the |
| 3082 | ** journal file. This is done to avoid ever modifying a journal |
| 3083 | ** header that is involved in the rollback of pages that have |
| 3084 | ** already been written to the database (in case the header is |
| 3085 | ** trashed when the nRec field is updated). |
| 3086 | */ |
| 3087 | pPager->nRec = 0; |
| 3088 | assert( pPager->journalOff > 0 ); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 3089 | assert( pPager->doNotSync==0 ); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3090 | rc = writeJournalHdr(pPager); |
| 3091 | if( rc!=0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3092 | return rc; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3093 | } |
| 3094 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3095 | pPg = pPager->lru.pFirst; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3096 | } |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3097 | |
| 3098 | assert( pPg->nRef==0 ); |
| 3099 | |
| 3100 | /* Write the page to the database file if it is dirty. |
| 3101 | */ |
| 3102 | if( pPg->dirty ){ |
| 3103 | int rc; |
| 3104 | assert( pPg->needSync==0 ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3105 | makeClean(pPg); |
| 3106 | pPg->dirty = 1; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3107 | pPg->pDirty = 0; |
| 3108 | rc = pager_write_pagelist( pPg ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3109 | pPg->dirty = 0; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3110 | if( rc!=SQLITE_OK ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3111 | return rc; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | assert( pPg->dirty==0 ); |
| 3115 | |
| 3116 | /* If the page we are recycling is marked as alwaysRollback, then |
| 3117 | ** set the global alwaysRollback flag, thus disabling the |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3118 | ** sqlite3PagerDontRollback() optimization for the rest of this transaction. |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3119 | ** It is necessary to do this because the page marked alwaysRollback |
| 3120 | ** might be reloaded at a later time but at that point we won't remember |
| 3121 | ** that is was marked alwaysRollback. This means that all pages must |
| 3122 | ** be marked as alwaysRollback from here on out. |
| 3123 | */ |
| 3124 | if( pPg->alwaysRollback ){ |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 3125 | IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager)) |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3126 | pPager->alwaysRollback = 1; |
| 3127 | } |
| 3128 | |
| 3129 | /* Unlink the old page from the free list and the hash table |
| 3130 | */ |
| 3131 | unlinkPage(pPg); |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 3132 | assert( pPg->pgno==0 ); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3133 | |
| 3134 | *ppPg = pPg; |
| 3135 | return SQLITE_OK; |
| 3136 | } |
| 3137 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3138 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3139 | /* |
| 3140 | ** This function is called to free superfluous dynamically allocated memory |
| 3141 | ** held by the pager system. Memory in use by any SQLite pager allocated |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3142 | ** by the current thread may be sqlite3_free()ed. |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3143 | ** |
| 3144 | ** nReq is the number of bytes of memory required. Once this much has |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3145 | ** been released, the function returns. The return value is the total number |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3146 | ** of bytes of memory released. |
| 3147 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3148 | int sqlite3PagerReleaseMemory(int nReq){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3149 | int nReleased = 0; /* Bytes of memory released so far */ |
| 3150 | sqlite3_mutex *mutex; /* The MEM2 mutex */ |
| 3151 | Pager *pPager; /* For looping over pagers */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3152 | BusyHandler *savedBusy; /* Saved copy of the busy handler */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3153 | int rc = SQLITE_OK; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3154 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3155 | /* Acquire the memory-management mutex |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 3156 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3157 | mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2); |
| 3158 | sqlite3_mutex_enter(mutex); |
| 3159 | |
| 3160 | /* Signal all database connections that memory management wants |
| 3161 | ** to have access to the pagers. |
| 3162 | */ |
| 3163 | for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){ |
| 3164 | pPager->iInUseMM = 1; |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3167 | while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){ |
| 3168 | PgHdr *pPg; |
| 3169 | PgHdr *pRecycled; |
| 3170 | |
| 3171 | /* Try to find a page to recycle that does not require a sync(). If |
| 3172 | ** this is not possible, find one that does require a sync(). |
| 3173 | */ |
| 3174 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
| 3175 | pPg = sqlite3LruPageList.pFirstSynced; |
| 3176 | while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){ |
| 3177 | pPg = pPg->gfree.pNext; |
| 3178 | } |
| 3179 | if( !pPg ){ |
| 3180 | pPg = sqlite3LruPageList.pFirst; |
| 3181 | while( pPg && pPg->pPager->iInUseDB ){ |
| 3182 | pPg = pPg->gfree.pNext; |
| 3183 | } |
| 3184 | } |
| 3185 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU)); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3186 | |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 3187 | /* If pPg==0, then the block above has failed to find a page to |
| 3188 | ** recycle. In this case return early - no further memory will |
| 3189 | ** be released. |
| 3190 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3191 | if( !pPg ) break; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3192 | |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3193 | pPager = pPg->pPager; |
| 3194 | assert(!pPg->needSync || pPg==pPager->lru.pFirst); |
| 3195 | assert(pPg->needSync || pPg==pPager->lru.pFirstSynced); |
| 3196 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3197 | savedBusy = pPager->pBusyHandler; |
| 3198 | pPager->pBusyHandler = 0; |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3199 | rc = pager_recycle(pPager, &pRecycled); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3200 | pPager->pBusyHandler = savedBusy; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3201 | assert(pRecycled==pPg || rc!=SQLITE_OK); |
| 3202 | if( rc==SQLITE_OK ){ |
| 3203 | /* We've found a page to free. At this point the page has been |
| 3204 | ** removed from the page hash-table, free-list and synced-list |
| 3205 | ** (pFirstSynced). It is still in the all pages (pAll) list. |
| 3206 | ** Remove it from this list before freeing. |
| 3207 | ** |
| 3208 | ** Todo: Check the Pager.pStmt list to make sure this is Ok. It |
| 3209 | ** probably is though. |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3210 | */ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3211 | PgHdr *pTmp; |
| 3212 | assert( pPg ); |
| 3213 | if( pPg==pPager->pAll ){ |
| 3214 | pPager->pAll = pPg->pNextAll; |
| 3215 | }else{ |
| 3216 | for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){} |
| 3217 | pTmp->pNextAll = pPg->pNextAll; |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3218 | } |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3219 | nReleased += ( |
| 3220 | sizeof(*pPg) + pPager->pageSize |
| 3221 | + sizeof(u32) + pPager->nExtra |
| 3222 | + MEMDB*sizeof(PgHistory) |
| 3223 | ); |
| 3224 | IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno)); |
| 3225 | PAGER_INCR(sqlite3_pager_pgfree_count); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 3226 | sqlite3_free(pPg->pData); |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3227 | sqlite3_free(pPg); |
danielk1977 | 84f786f | 2007-08-28 08:00:17 +0000 | [diff] [blame] | 3228 | pPager->nPage--; |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3229 | }else{ |
| 3230 | /* An error occured whilst writing to the database file or |
| 3231 | ** journal in pager_recycle(). The error is not returned to the |
| 3232 | ** caller of this function. Instead, set the Pager.errCode variable. |
| 3233 | ** The error will be returned to the user (or users, in the case |
| 3234 | ** of a shared pager cache) of the pager for which the error occured. |
| 3235 | */ |
| 3236 | assert( |
| 3237 | (rc&0xff)==SQLITE_IOERR || |
| 3238 | rc==SQLITE_FULL || |
| 3239 | rc==SQLITE_BUSY |
| 3240 | ); |
| 3241 | assert( pPager->state>=PAGER_RESERVED ); |
| 3242 | pager_error(pPager, rc); |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3243 | } |
| 3244 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3245 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3246 | /* Clear the memory management flags and release the mutex |
| 3247 | */ |
| 3248 | for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){ |
| 3249 | pPager->iInUseMM = 0; |
| 3250 | } |
| 3251 | sqlite3_mutex_leave(mutex); |
| 3252 | |
| 3253 | /* Return the number of bytes released |
| 3254 | */ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3255 | return nReleased; |
| 3256 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3257 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
danielk1977 | 13f7299 | 2005-12-18 08:51:22 +0000 | [diff] [blame] | 3258 | |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3259 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3260 | ** Read the content of page pPg out of the database file. |
| 3261 | */ |
| 3262 | static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){ |
| 3263 | int rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3264 | i64 offset; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3265 | assert( MEMDB==0 ); |
danielk1977 | 7a2b1ee | 2007-08-21 14:27:01 +0000 | [diff] [blame] | 3266 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 3267 | if( !pPager->fd->pMethods ){ |
| 3268 | return SQLITE_IOERR_SHORT_READ; |
| 3269 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3270 | offset = (pgno-1)*(i64)pPager->pageSize; |
| 3271 | rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3272 | PAGER_INCR(sqlite3_pager_readdb_count); |
| 3273 | PAGER_INCR(pPager->nRead); |
| 3274 | IOTRACE(("PGIN %p %d\n", pPager, pgno)); |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3275 | if( pgno==1 ){ |
| 3276 | memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24], |
| 3277 | sizeof(pPager->dbFileVers)); |
| 3278 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3279 | CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 3280 | PAGERTRACE4("FETCH %d page %d hash(%08x)\n", |
| 3281 | PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3282 | return rc; |
| 3283 | } |
| 3284 | |
| 3285 | |
| 3286 | /* |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3287 | ** This function is called to obtain the shared lock required before |
| 3288 | ** data may be read from the pager cache. If the shared lock has already |
| 3289 | ** been obtained, this function is a no-op. |
danielk1977 | 393f068 | 2007-03-31 10:00:48 +0000 | [diff] [blame] | 3290 | ** |
| 3291 | ** Immediately after obtaining the shared lock (if required), this function |
| 3292 | ** checks for a hot-journal file. If one is found, an emergency rollback |
| 3293 | ** is performed immediately. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3294 | */ |
| 3295 | static int pagerSharedLock(Pager *pPager){ |
| 3296 | int rc = SQLITE_OK; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3297 | int isHot = 0; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3298 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3299 | /* If this database is opened for exclusive access, has no outstanding |
| 3300 | ** page references and is in an error-state, now is the chance to clear |
| 3301 | ** the error. Discard the contents of the pager-cache and treat any |
| 3302 | ** open journal file as a hot-journal. |
| 3303 | */ |
| 3304 | if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){ |
| 3305 | if( pPager->journalOpen ){ |
| 3306 | isHot = 1; |
| 3307 | } |
| 3308 | pager_reset(pPager); |
| 3309 | pPager->errCode = SQLITE_OK; |
| 3310 | } |
| 3311 | |
| 3312 | /* If the pager is still in an error state, do not proceed. The error |
| 3313 | ** state will be cleared at some point in the future when all page |
| 3314 | ** references are dropped and the cache can be discarded. |
| 3315 | */ |
| 3316 | if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
| 3317 | return pPager->errCode; |
| 3318 | } |
| 3319 | |
| 3320 | if( pPager->state==PAGER_UNLOCK || isHot ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3321 | sqlite3_vfs *pVfs = pPager->pVfs; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3322 | if( !MEMDB ){ |
| 3323 | assert( pPager->nRef==0 ); |
| 3324 | if( !pPager->noReadlock ){ |
| 3325 | rc = pager_wait_on_lock(pPager, SHARED_LOCK); |
| 3326 | if( rc!=SQLITE_OK ){ |
| 3327 | return pager_error(pPager, rc); |
| 3328 | } |
| 3329 | assert( pPager->state>=SHARED_LOCK ); |
| 3330 | } |
| 3331 | |
| 3332 | /* If a journal file exists, and there is no RESERVED lock on the |
| 3333 | ** database file, then it either needs to be played back or deleted. |
| 3334 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3335 | if( hasHotJournal(pPager) || isHot ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3336 | /* Get an EXCLUSIVE lock on the database file. At this point it is |
| 3337 | ** important that a RESERVED lock is not obtained on the way to the |
| 3338 | ** EXCLUSIVE lock. If it were, another process might open the |
| 3339 | ** database file, detect the RESERVED lock, and conclude that the |
| 3340 | ** database is safe to read while this process is still rolling it |
| 3341 | ** back. |
| 3342 | ** |
| 3343 | ** Because the intermediate RESERVED lock is not requested, the |
| 3344 | ** second process will get to this point in the code and fail to |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3345 | ** obtain its own EXCLUSIVE lock on the database file. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3346 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3347 | if( pPager->state<EXCLUSIVE_LOCK ){ |
| 3348 | rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK); |
| 3349 | if( rc!=SQLITE_OK ){ |
| 3350 | pager_unlock(pPager); |
| 3351 | return pager_error(pPager, rc); |
| 3352 | } |
| 3353 | pPager->state = PAGER_EXCLUSIVE; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3354 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3355 | |
| 3356 | /* Open the journal for reading only. Return SQLITE_BUSY if |
| 3357 | ** we are unable to open the journal file. |
| 3358 | ** |
| 3359 | ** The journal file does not need to be locked itself. The |
| 3360 | ** journal file is never open unless the main database file holds |
| 3361 | ** a write lock, so there is never any chance of two or more |
| 3362 | ** processes opening the journal at the same time. |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3363 | ** |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3364 | ** Open the journal for read/write access. This is because in |
| 3365 | ** exclusive-access mode the file descriptor will be kept open and |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3366 | ** possibly used for a transaction later on. On some systems, the |
| 3367 | ** OsTruncate() call used in exclusive-access mode also requires |
| 3368 | ** a read/write file handle. |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3369 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3370 | if( !isHot ){ |
| 3371 | rc = SQLITE_BUSY; |
| 3372 | if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){ |
| 3373 | int fout = 0; |
| 3374 | int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL; |
| 3375 | assert( !pPager->tempFile ); |
| 3376 | rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout); |
| 3377 | assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); |
| 3378 | if( fout&SQLITE_OPEN_READONLY ){ |
| 3379 | rc = SQLITE_BUSY; |
| 3380 | sqlite3OsClose(pPager->jfd); |
| 3381 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3382 | } |
| 3383 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3384 | if( rc!=SQLITE_OK ){ |
| 3385 | pager_unlock(pPager); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 3386 | switch( rc ){ |
| 3387 | case SQLITE_NOMEM: |
| 3388 | case SQLITE_IOERR_UNLOCK: |
| 3389 | case SQLITE_IOERR_NOMEM: |
| 3390 | return rc; |
| 3391 | default: |
| 3392 | return SQLITE_BUSY; |
| 3393 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3394 | } |
| 3395 | pPager->journalOpen = 1; |
| 3396 | pPager->journalStarted = 0; |
| 3397 | pPager->journalOff = 0; |
| 3398 | pPager->setMaster = 0; |
| 3399 | pPager->journalHdr = 0; |
| 3400 | |
| 3401 | /* Playback and delete the journal. Drop the database write |
| 3402 | ** lock and reacquire the read lock. |
| 3403 | */ |
| 3404 | rc = pager_playback(pPager, 1); |
| 3405 | if( rc!=SQLITE_OK ){ |
| 3406 | return pager_error(pPager, rc); |
| 3407 | } |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 3408 | assert(pPager->state==PAGER_SHARED || |
| 3409 | (pPager->exclusiveMode && pPager->state>PAGER_SHARED) |
| 3410 | ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | if( pPager->pAll ){ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3414 | /* The shared-lock has just been acquired on the database file |
| 3415 | ** and there are already pages in the cache (from a previous |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3416 | ** read or write transaction). Check to see if the database |
| 3417 | ** has been modified. If the database has changed, flush the |
| 3418 | ** cache. |
| 3419 | ** |
| 3420 | ** Database changes is detected by looking at 15 bytes beginning |
| 3421 | ** at offset 24 into the file. The first 4 of these 16 bytes are |
| 3422 | ** a 32-bit counter that is incremented with each change. The |
| 3423 | ** other bytes change randomly with each file change when |
| 3424 | ** a codec is in use. |
| 3425 | ** |
| 3426 | ** There is a vanishingly small chance that a change will not be |
drh | 6fa5103 | 2007-05-09 20:35:31 +0000 | [diff] [blame] | 3427 | ** detected. The chance of an undetected change is so small that |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3428 | ** it can be neglected. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3429 | */ |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3430 | char dbFileVers[sizeof(pPager->dbFileVers)]; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3431 | sqlite3PagerPagecount(pPager); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3432 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3433 | if( pPager->errCode ){ |
| 3434 | return pPager->errCode; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3437 | if( pPager->dbSize>0 ){ |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 3438 | IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3439 | rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3440 | if( rc!=SQLITE_OK ){ |
| 3441 | return rc; |
| 3442 | } |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3443 | }else{ |
| 3444 | memset(dbFileVers, 0, sizeof(dbFileVers)); |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
drh | 86a8811 | 2007-04-16 15:02:19 +0000 | [diff] [blame] | 3447 | if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3448 | pager_reset(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3449 | } |
| 3450 | } |
| 3451 | } |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 3452 | assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED ); |
| 3453 | if( pPager->state==PAGER_UNLOCK ){ |
| 3454 | pPager->state = PAGER_SHARED; |
| 3455 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
| 3458 | return rc; |
| 3459 | } |
| 3460 | |
| 3461 | /* |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3462 | ** Allocate a PgHdr object. Either create a new one or reuse |
| 3463 | ** an existing one that is not otherwise in use. |
| 3464 | ** |
| 3465 | ** A new PgHdr structure is created if any of the following are |
| 3466 | ** true: |
| 3467 | ** |
| 3468 | ** (1) We have not exceeded our maximum allocated cache size |
| 3469 | ** as set by the "PRAGMA cache_size" command. |
| 3470 | ** |
| 3471 | ** (2) There are no unused PgHdr objects available at this time. |
| 3472 | ** |
| 3473 | ** (3) This is an in-memory database. |
| 3474 | ** |
| 3475 | ** (4) There are no PgHdr objects that do not require a journal |
| 3476 | ** file sync and a sync of the journal file is currently |
| 3477 | ** prohibited. |
| 3478 | ** |
| 3479 | ** Otherwise, reuse an existing PgHdr. In other words, reuse an |
| 3480 | ** existing PgHdr if all of the following are true: |
| 3481 | ** |
| 3482 | ** (1) We have reached or exceeded the maximum cache size |
| 3483 | ** allowed by "PRAGMA cache_size". |
| 3484 | ** |
| 3485 | ** (2) There is a PgHdr available with PgHdr->nRef==0 |
| 3486 | ** |
| 3487 | ** (3) We are not in an in-memory database |
| 3488 | ** |
| 3489 | ** (4) Either there is an available PgHdr that does not need |
| 3490 | ** to be synced to disk or else disk syncing is currently |
| 3491 | ** allowed. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3492 | */ |
| 3493 | static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){ |
| 3494 | int rc = SQLITE_OK; |
| 3495 | PgHdr *pPg; |
drh | 1e3af33 | 2007-10-20 13:17:54 +0000 | [diff] [blame] | 3496 | int nByteHdr; |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3497 | |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3498 | /* Create a new PgHdr if any of the four conditions defined |
danielk1977 | 2a6bdf6 | 2007-08-20 16:07:00 +0000 | [diff] [blame] | 3499 | ** above are met: */ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3500 | if( pPager->nPage<pPager->mxPage |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3501 | || pPager->lru.pFirst==0 |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3502 | || MEMDB |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3503 | || (pPager->lru.pFirstSynced==0 && pPager->doNotSync) |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3504 | ){ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 3505 | void *pData; |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3506 | if( pPager->nPage>=pPager->nHash ){ |
| 3507 | pager_resize_hash_table(pPager, |
| 3508 | pPager->nHash<256 ? 256 : pPager->nHash*2); |
| 3509 | if( pPager->nHash==0 ){ |
| 3510 | rc = SQLITE_NOMEM; |
| 3511 | goto pager_allocate_out; |
| 3512 | } |
| 3513 | } |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3514 | pagerLeave(pPager); |
drh | 1e3af33 | 2007-10-20 13:17:54 +0000 | [diff] [blame] | 3515 | nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra |
| 3516 | + MEMDB*sizeof(PgHistory); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 3517 | pPg = sqlite3_malloc( nByteHdr ); |
| 3518 | if( pPg ){ |
| 3519 | pData = sqlite3_malloc( pPager->pageSize ); |
| 3520 | if( pData==0 ){ |
| 3521 | sqlite3_free(pPg); |
| 3522 | pPg = 0; |
| 3523 | } |
| 3524 | } |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3525 | pagerEnter(pPager); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3526 | if( pPg==0 ){ |
| 3527 | rc = SQLITE_NOMEM; |
| 3528 | goto pager_allocate_out; |
| 3529 | } |
drh | 1e3af33 | 2007-10-20 13:17:54 +0000 | [diff] [blame] | 3530 | memset(pPg, 0, nByteHdr); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 3531 | pPg->pData = pData; |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3532 | pPg->pPager = pPager; |
| 3533 | pPg->pNextAll = pPager->pAll; |
| 3534 | pPager->pAll = pPg; |
| 3535 | pPager->nPage++; |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3536 | }else{ |
| 3537 | /* Recycle an existing page with a zero ref-count. */ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 3538 | rc = pager_recycle(pPager, &pPg); |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 3539 | if( rc==SQLITE_BUSY ){ |
| 3540 | rc = SQLITE_IOERR_BLOCKED; |
| 3541 | } |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3542 | if( rc!=SQLITE_OK ){ |
| 3543 | goto pager_allocate_out; |
| 3544 | } |
| 3545 | assert( pPager->state>=SHARED_LOCK ); |
| 3546 | assert(pPg); |
| 3547 | } |
| 3548 | *ppPg = pPg; |
| 3549 | |
| 3550 | pager_allocate_out: |
| 3551 | return rc; |
| 3552 | } |
| 3553 | |
| 3554 | /* |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3555 | ** Make sure we have the content for a page. If the page was |
| 3556 | ** previously acquired with noContent==1, then the content was |
| 3557 | ** just initialized to zeros instead of being read from disk. |
| 3558 | ** But now we need the real data off of disk. So make sure we |
| 3559 | ** have it. Read it in if we do not have it already. |
| 3560 | */ |
| 3561 | static int pager_get_content(PgHdr *pPg){ |
| 3562 | if( pPg->needRead ){ |
| 3563 | int rc = readDbPage(pPg->pPager, pPg, pPg->pgno); |
| 3564 | if( rc==SQLITE_OK ){ |
| 3565 | pPg->needRead = 0; |
| 3566 | }else{ |
| 3567 | return rc; |
| 3568 | } |
| 3569 | } |
| 3570 | return SQLITE_OK; |
| 3571 | } |
| 3572 | |
| 3573 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3574 | ** Acquire a page. |
| 3575 | ** |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 3576 | ** 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] | 3577 | ** This read lock is dropped when the last page is released. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3578 | ** |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3579 | ** This routine works for any page number greater than 0. If the database |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3580 | ** file is smaller than the requested page, then no actual disk |
| 3581 | ** read occurs and the memory image of the page is initialized to |
| 3582 | ** all zeros. The extra data appended to a page is always initialized |
| 3583 | ** to zeros the first time a page is loaded into memory. |
| 3584 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3585 | ** The acquisition might fail for several reasons. In all cases, |
| 3586 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3587 | ** |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3588 | ** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3589 | ** 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] | 3590 | ** in memory, this routine goes to disk to read it in whereas Lookup() |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3591 | ** just returns 0. This routine acquires a read-lock the first time it |
| 3592 | ** 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] | 3593 | ** Since Lookup() never goes to disk, it never has to deal with locks |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3594 | ** or journal files. |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 3595 | ** |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3596 | ** If noContent is false, the page contents are actually read from disk. |
| 3597 | ** If noContent is true, it means that we do not care about the contents |
| 3598 | ** of the page at this time, so do not do a disk read. Just fill in the |
| 3599 | ** page content with zeros. But mark the fact that we have not read the |
| 3600 | ** content by setting the PgHdr.needRead flag. Later on, if |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3601 | ** sqlite3PagerWrite() is called on this page or if this routine is |
| 3602 | ** called again with noContent==0, that means that the content is needed |
| 3603 | ** and the disk read should occur at that point. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3604 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3605 | static int pagerAcquire( |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3606 | Pager *pPager, /* The pager open on the database file */ |
| 3607 | Pgno pgno, /* Page number to fetch */ |
| 3608 | DbPage **ppPage, /* Write a pointer to the page here */ |
| 3609 | int noContent /* Do not bother reading content from disk if true */ |
| 3610 | ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3611 | PgHdr *pPg; |
drh | 165ffe9 | 2005-03-15 17:09:30 +0000 | [diff] [blame] | 3612 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3613 | |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3614 | assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 ); |
| 3615 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 3616 | /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page |
| 3617 | ** number greater than this, or zero, is requested. |
| 3618 | */ |
drh | 267cb32 | 2005-09-16 17:16:52 +0000 | [diff] [blame] | 3619 | if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3620 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3623 | /* Make sure we have not hit any critical errors. |
| 3624 | */ |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 3625 | assert( pPager!=0 ); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3626 | *ppPage = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3627 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3628 | /* If this is the first page accessed, then get a SHARED lock |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3629 | ** on the database file. pagerSharedLock() is a no-op if |
| 3630 | ** a database lock is already held. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3631 | */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3632 | rc = pagerSharedLock(pPager); |
| 3633 | if( rc!=SQLITE_OK ){ |
| 3634 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3635 | } |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3636 | assert( pPager->state!=PAGER_UNLOCK ); |
| 3637 | |
| 3638 | pPg = pager_lookup(pPager, pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3639 | if( pPg==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3640 | /* The requested page is not in the page cache. */ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3641 | int nMax; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3642 | int h; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3643 | PAGER_INCR(pPager->nMiss); |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3644 | rc = pagerAllocatePage(pPager, &pPg); |
| 3645 | if( rc!=SQLITE_OK ){ |
| 3646 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3647 | } |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3648 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3649 | pPg->pgno = pgno; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 3650 | assert( !MEMDB || pgno>pPager->stmtSize ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3651 | pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno); |
| 3652 | pPg->needSync = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 3653 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3654 | makeClean(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3655 | pPg->nRef = 1; |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3656 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3657 | pPager->nRef++; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3658 | if( pPager->nExtra>0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 3659 | memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3660 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 3661 | nMax = sqlite3PagerPagecount(pPager); |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3662 | if( pPager->errCode ){ |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3663 | rc = pPager->errCode; |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3664 | sqlite3PagerUnref(pPg); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3665 | return rc; |
| 3666 | } |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3667 | |
| 3668 | /* Populate the page with data, either by reading from the database |
| 3669 | ** file, or by setting the entire page to zero. |
| 3670 | */ |
drh | d215acf | 2007-04-13 04:01:58 +0000 | [diff] [blame] | 3671 | if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 3672 | if( pgno>pPager->mxPgno ){ |
danielk1977 | de3bea7 | 2007-05-09 15:56:39 +0000 | [diff] [blame] | 3673 | sqlite3PagerUnref(pPg); |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 3674 | return SQLITE_FULL; |
| 3675 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 3676 | memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize); |
drh | d215acf | 2007-04-13 04:01:58 +0000 | [diff] [blame] | 3677 | pPg->needRead = noContent && !pPager->alwaysRollback; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3678 | IOTRACE(("ZERO %p %d\n", pPager, pgno)); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3679 | }else{ |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 3680 | rc = readDbPage(pPager, pPg, pgno); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 3681 | if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ |
| 3682 | pPg->pgno = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3683 | sqlite3PagerUnref(pPg); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 3684 | return rc; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 3685 | } |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 3686 | pPg->needRead = 0; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 3687 | } |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3688 | |
| 3689 | /* Link the page into the page hash table */ |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 3690 | h = pgno & (pPager->nHash-1); |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 3691 | assert( pgno!=0 ); |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3692 | pPg->pNextHash = pPager->aHash[h]; |
| 3693 | pPager->aHash[h] = pPg; |
| 3694 | if( pPg->pNextHash ){ |
| 3695 | assert( pPg->pNextHash->pPrevHash==0 ); |
| 3696 | pPg->pNextHash->pPrevHash = pPg; |
| 3697 | } |
| 3698 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3699 | #ifdef SQLITE_CHECK_PAGES |
| 3700 | pPg->pageHash = pager_pagehash(pPg); |
| 3701 | #endif |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3702 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3703 | /* The requested page is in the page cache. */ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3704 | assert(pPager->nRef>0 || pgno==1); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3705 | PAGER_INCR(pPager->nHit); |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 3706 | if( !noContent ){ |
| 3707 | rc = pager_get_content(pPg); |
| 3708 | if( rc ){ |
| 3709 | return rc; |
| 3710 | } |
| 3711 | } |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 3712 | page_ref(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3713 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3714 | *ppPage = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3715 | return SQLITE_OK; |
| 3716 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3717 | int sqlite3PagerAcquire( |
| 3718 | Pager *pPager, /* The pager open on the database file */ |
| 3719 | Pgno pgno, /* Page number to fetch */ |
| 3720 | DbPage **ppPage, /* Write a pointer to the page here */ |
| 3721 | int noContent /* Do not bother reading content from disk if true */ |
| 3722 | ){ |
| 3723 | int rc; |
| 3724 | pagerEnter(pPager); |
| 3725 | rc = pagerAcquire(pPager, pgno, ppPage, noContent); |
| 3726 | pagerLeave(pPager); |
| 3727 | return rc; |
| 3728 | } |
| 3729 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3730 | |
| 3731 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3732 | ** Acquire a page if it is already in the in-memory cache. Do |
| 3733 | ** not read the page from disk. Return a pointer to the page, |
| 3734 | ** or 0 if the page is not in cache. |
| 3735 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3736 | ** See also sqlite3PagerGet(). The difference between this routine |
| 3737 | ** and sqlite3PagerGet() is that _get() will go to the disk and read |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3738 | ** in the page if the page is not already in cache. This routine |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3739 | ** returns NULL if the page is not in cache or if a disk I/O error |
| 3740 | ** has ever happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3741 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3742 | DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3743 | PgHdr *pPg = 0; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3744 | |
drh | 836faa4 | 2003-01-11 13:30:57 +0000 | [diff] [blame] | 3745 | assert( pPager!=0 ); |
| 3746 | assert( pgno!=0 ); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3747 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3748 | pagerEnter(pPager); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3749 | if( pPager->state==PAGER_UNLOCK ){ |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3750 | assert( !pPager->pAll || pPager->exclusiveMode ); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3751 | }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
| 3752 | /* Do nothing */ |
| 3753 | }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){ |
| 3754 | page_ref(pPg); |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3755 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3756 | pagerLeave(pPager); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3757 | return pPg; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3761 | ** Release a page. |
| 3762 | ** |
| 3763 | ** If the number of references to the page drop to zero, then the |
| 3764 | ** page is added to the LRU list. When all references to all pages |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3765 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3766 | ** removed. |
| 3767 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3768 | int sqlite3PagerUnref(DbPage *pPg){ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3769 | Pager *pPager = pPg->pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3770 | |
| 3771 | /* Decrement the reference count for this page |
| 3772 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3773 | assert( pPg->nRef>0 ); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3774 | pagerEnter(pPg->pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3775 | pPg->nRef--; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3776 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 3777 | CHECK_PAGE(pPg); |
| 3778 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3779 | /* When the number of references to a page reach 0, call the |
| 3780 | ** destructor and add the page to the freelist. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3781 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3782 | if( pPg->nRef==0 ){ |
danielk1977 | 9f61c2f | 2007-08-27 17:27:49 +0000 | [diff] [blame] | 3783 | |
| 3784 | lruListAdd(pPg); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3785 | if( pPager->xDestructor ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3786 | pPager->xDestructor(pPg, pPager->pageSize); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3787 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3788 | |
| 3789 | /* When all pages reach the freelist, drop the read lock from |
| 3790 | ** the database file. |
| 3791 | */ |
| 3792 | pPager->nRef--; |
| 3793 | assert( pPager->nRef>=0 ); |
danielk1977 | 3dedc19 | 2007-03-27 17:37:31 +0000 | [diff] [blame] | 3794 | if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 3795 | pagerUnlockAndRollback(pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3796 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3797 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3798 | pagerLeave(pPager); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3799 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3803 | ** Create a journal file for pPager. There should already be a RESERVED |
| 3804 | ** or EXCLUSIVE lock on the database file when this routine is called. |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3805 | ** |
| 3806 | ** Return SQLITE_OK if everything. Return an error code and release the |
| 3807 | ** write lock if anything goes wrong. |
| 3808 | */ |
| 3809 | static int pager_open_journal(Pager *pPager){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3810 | sqlite3_vfs *pVfs = pPager->pVfs; |
| 3811 | int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE); |
| 3812 | |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3813 | int rc; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3814 | assert( !MEMDB ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3815 | assert( pPager->state>=PAGER_RESERVED ); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3816 | assert( pPager->journalOpen==0 ); |
| 3817 | assert( pPager->useJournal ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3818 | assert( pPager->pInJournal==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3819 | sqlite3PagerPagecount(pPager); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3820 | pagerLeave(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3821 | pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3822 | pagerEnter(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3823 | if( pPager->pInJournal==0 ){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3824 | rc = SQLITE_NOMEM; |
| 3825 | goto failed_to_open_journal; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3826 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3827 | |
| 3828 | if( pPager->tempFile ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3829 | flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); |
| 3830 | }else{ |
| 3831 | flags |= (SQLITE_OPEN_MAIN_JOURNAL); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3832 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3833 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 3834 | rc = sqlite3JournalOpen( |
| 3835 | pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) |
| 3836 | ); |
| 3837 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3838 | rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 3839 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3840 | assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 3841 | pPager->journalOff = 0; |
| 3842 | pPager->setMaster = 0; |
| 3843 | pPager->journalHdr = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3844 | if( rc!=SQLITE_OK ){ |
drh | 600e46a | 2007-03-28 01:59:33 +0000 | [diff] [blame] | 3845 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3846 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
drh | 600e46a | 2007-03-28 01:59:33 +0000 | [diff] [blame] | 3847 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3848 | goto failed_to_open_journal; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3849 | } |
| 3850 | pPager->journalOpen = 1; |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 3851 | pPager->journalStarted = 0; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3852 | pPager->needSync = 0; |
| 3853 | pPager->alwaysRollback = 0; |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 3854 | pPager->nRec = 0; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 3855 | if( pPager->errCode ){ |
| 3856 | rc = pPager->errCode; |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 3857 | goto failed_to_open_journal; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 3858 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3859 | pPager->origDbSize = pPager->dbSize; |
drh | ae2b40c | 2004-06-09 19:03:54 +0000 | [diff] [blame] | 3860 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 3861 | rc = writeJournalHdr(pPager); |
| 3862 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 3863 | if( pPager->stmtAutoopen && rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3864 | rc = sqlite3PagerStmtBegin(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3865 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 3866 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3867 | rc = pager_end_transaction(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3868 | if( rc==SQLITE_OK ){ |
| 3869 | rc = SQLITE_FULL; |
| 3870 | } |
| 3871 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3872 | return rc; |
| 3873 | |
| 3874 | failed_to_open_journal: |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3875 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 3876 | pPager->pInJournal = 0; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 3877 | return rc; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | /* |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3881 | ** Acquire a write-lock on the database. The lock is removed when |
| 3882 | ** the any of the following happen: |
| 3883 | ** |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3884 | ** * sqlite3PagerCommitPhaseTwo() is called. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3885 | ** * sqlite3PagerRollback() is called. |
| 3886 | ** * sqlite3PagerClose() is called. |
| 3887 | ** * sqlite3PagerUnref() is called to on every outstanding page. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3888 | ** |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3889 | ** The first parameter to this routine is a pointer to any open page of the |
| 3890 | ** database file. Nothing changes about the page - it is used merely to |
| 3891 | ** acquire a pointer to the Pager structure and as proof that there is |
| 3892 | ** already a read-lock on the database. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3893 | ** |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3894 | ** The second parameter indicates how much space in bytes to reserve for a |
| 3895 | ** master journal file-name at the start of the journal when it is created. |
| 3896 | ** |
| 3897 | ** A journal file is opened if this is not a temporary file. For temporary |
| 3898 | ** files, the opening of the journal file is deferred until there is an |
| 3899 | ** actual need to write to the journal. |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 3900 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3901 | ** 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] | 3902 | ** |
| 3903 | ** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file |
| 3904 | ** immediately instead of waiting until we try to flush the cache. The |
| 3905 | ** exFlag is ignored if a transaction is already active. |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3906 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3907 | int sqlite3PagerBegin(DbPage *pPg, int exFlag){ |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3908 | Pager *pPager = pPg->pPager; |
| 3909 | int rc = SQLITE_OK; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3910 | pagerEnter(pPager); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3911 | assert( pPg->nRef>0 ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3912 | assert( pPager->state!=PAGER_UNLOCK ); |
| 3913 | if( pPager->state==PAGER_SHARED ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3914 | assert( pPager->pInJournal==0 ); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3915 | if( MEMDB ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3916 | pPager->state = PAGER_EXCLUSIVE; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 3917 | pPager->origDbSize = pPager->dbSize; |
| 3918 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3919 | rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK); |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 3920 | if( rc==SQLITE_OK ){ |
| 3921 | pPager->state = PAGER_RESERVED; |
| 3922 | if( exFlag ){ |
| 3923 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| 3924 | } |
| 3925 | } |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 3926 | if( rc!=SQLITE_OK ){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3927 | pagerLeave(pPager); |
danielk1977 | 9eed505 | 2004-06-04 10:38:30 +0000 | [diff] [blame] | 3928 | return rc; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 3929 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 3930 | pPager->dirtyCache = 0; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3931 | PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager)); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 3932 | if( pPager->useJournal && !pPager->tempFile ){ |
| 3933 | rc = pager_open_journal(pPager); |
| 3934 | } |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3935 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3936 | }else if( pPager->journalOpen && pPager->journalOff==0 ){ |
| 3937 | /* This happens when the pager was in exclusive-access mode last |
| 3938 | ** time a (read or write) transaction was successfully concluded |
| 3939 | ** by this connection. Instead of deleting the journal file it was |
| 3940 | ** kept open and truncated to 0 bytes. |
| 3941 | */ |
| 3942 | assert( pPager->nRec==0 ); |
| 3943 | assert( pPager->origDbSize==0 ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3944 | assert( pPager->pInJournal==0 ); |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3945 | sqlite3PagerPagecount(pPager); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3946 | pagerLeave(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3947 | pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 3948 | pagerEnter(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 3949 | if( !pPager->pInJournal ){ |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3950 | rc = SQLITE_NOMEM; |
| 3951 | }else{ |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 3952 | pPager->origDbSize = pPager->dbSize; |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3953 | rc = writeJournalHdr(pPager); |
| 3954 | } |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3955 | } |
danielk1977 | 334cdb6 | 2007-03-26 08:05:12 +0000 | [diff] [blame] | 3956 | assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK ); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 3957 | pagerLeave(pPager); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 3958 | return rc; |
| 3959 | } |
| 3960 | |
| 3961 | /* |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3962 | ** Make a page dirty. Set its dirty flag and add it to the dirty |
| 3963 | ** page list. |
| 3964 | */ |
| 3965 | static void makeDirty(PgHdr *pPg){ |
| 3966 | if( pPg->dirty==0 ){ |
| 3967 | Pager *pPager = pPg->pPager; |
| 3968 | pPg->dirty = 1; |
| 3969 | pPg->pDirty = pPager->pDirty; |
| 3970 | if( pPager->pDirty ){ |
| 3971 | pPager->pDirty->pPrevDirty = pPg; |
| 3972 | } |
| 3973 | pPg->pPrevDirty = 0; |
| 3974 | pPager->pDirty = pPg; |
| 3975 | } |
| 3976 | } |
| 3977 | |
| 3978 | /* |
| 3979 | ** Make a page clean. Clear its dirty bit and remove it from the |
| 3980 | ** dirty page list. |
| 3981 | */ |
| 3982 | static void makeClean(PgHdr *pPg){ |
| 3983 | if( pPg->dirty ){ |
| 3984 | pPg->dirty = 0; |
| 3985 | if( pPg->pDirty ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3986 | assert( pPg->pDirty->pPrevDirty==pPg ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3987 | pPg->pDirty->pPrevDirty = pPg->pPrevDirty; |
| 3988 | } |
| 3989 | if( pPg->pPrevDirty ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3990 | assert( pPg->pPrevDirty->pDirty==pPg ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3991 | pPg->pPrevDirty->pDirty = pPg->pDirty; |
| 3992 | }else{ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3993 | assert( pPg->pPager->pDirty==pPg ); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 3994 | pPg->pPager->pDirty = pPg->pDirty; |
| 3995 | } |
| 3996 | } |
| 3997 | } |
| 3998 | |
| 3999 | |
| 4000 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4001 | ** Mark a data page as writeable. The page is written into the journal |
| 4002 | ** if it is not there already. This routine must be called before making |
| 4003 | ** changes to a page. |
| 4004 | ** |
| 4005 | ** The first time this routine is called, the pager creates a new |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4006 | ** journal and acquires a RESERVED lock on the database. If the RESERVED |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4007 | ** lock could not be acquired, this routine returns SQLITE_BUSY. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 4008 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4009 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4010 | ** |
| 4011 | ** If the journal file could not be written because the disk is full, |
| 4012 | ** then this routine returns SQLITE_FULL and does an immediate rollback. |
| 4013 | ** All subsequent write attempts also return SQLITE_FULL until there |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4014 | ** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4015 | ** reset. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4016 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4017 | static int pager_write(PgHdr *pPg){ |
| 4018 | void *pData = PGHDR_TO_DATA(pPg); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 4019 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 4020 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 4021 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 4022 | /* Check for errors |
| 4023 | */ |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4024 | if( pPager->errCode ){ |
| 4025 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4026 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4027 | if( pPager->readOnly ){ |
| 4028 | return SQLITE_PERM; |
| 4029 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 4030 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 4031 | assert( !pPager->setMaster ); |
| 4032 | |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 4033 | CHECK_PAGE(pPg); |
| 4034 | |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4035 | /* If this page was previously acquired with noContent==1, that means |
| 4036 | ** we didn't really read in the content of the page. This can happen |
| 4037 | ** (for example) when the page is being moved to the freelist. But |
| 4038 | ** now we are (perhaps) moving the page off of the freelist for |
| 4039 | ** reuse and we need to know its original content so that content |
| 4040 | ** can be stored in the rollback journal. So do the read at this |
| 4041 | ** time. |
| 4042 | */ |
drh | d33d5a8 | 2007-04-26 12:11:28 +0000 | [diff] [blame] | 4043 | rc = pager_get_content(pPg); |
| 4044 | if( rc ){ |
| 4045 | return rc; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 4048 | /* Mark the page as dirty. If the page has already been written |
| 4049 | ** to the journal then we can return right away. |
| 4050 | */ |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4051 | makeDirty(pPg); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4052 | if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4053 | pPager->dirtyCache = 1; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4054 | }else{ |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 4055 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4056 | /* If we get this far, it means that the page needs to be |
| 4057 | ** written to the transaction journal or the ckeckpoint journal |
| 4058 | ** or both. |
| 4059 | ** |
| 4060 | ** First check to see that the transaction journal exists and |
| 4061 | ** create it if it does not. |
| 4062 | */ |
| 4063 | assert( pPager->state!=PAGER_UNLOCK ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4064 | rc = sqlite3PagerBegin(pPg, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4065 | if( rc!=SQLITE_OK ){ |
| 4066 | return rc; |
| 4067 | } |
| 4068 | assert( pPager->state>=PAGER_RESERVED ); |
| 4069 | if( !pPager->journalOpen && pPager->useJournal ){ |
| 4070 | rc = pager_open_journal(pPager); |
| 4071 | if( rc!=SQLITE_OK ) return rc; |
| 4072 | } |
| 4073 | assert( pPager->journalOpen || !pPager->useJournal ); |
| 4074 | pPager->dirtyCache = 1; |
| 4075 | |
| 4076 | /* The transaction journal now exists and we have a RESERVED or an |
| 4077 | ** EXCLUSIVE lock on the main database file. Write the current page to |
| 4078 | ** the transaction journal if it is not there already. |
| 4079 | */ |
| 4080 | if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){ |
| 4081 | if( (int)pPg->pgno <= pPager->origDbSize ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4082 | if( MEMDB ){ |
| 4083 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4084 | PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4085 | assert( pHist->pOrig==0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4086 | pHist->pOrig = sqlite3_malloc( pPager->pageSize ); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 4087 | if( !pHist->pOrig ){ |
| 4088 | return SQLITE_NOMEM; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4089 | } |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 4090 | memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4091 | }else{ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4092 | u32 cksum; |
| 4093 | char *pData2; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4094 | |
drh | 267cb32 | 2005-09-16 17:16:52 +0000 | [diff] [blame] | 4095 | /* We should never write to the journal file the page that |
| 4096 | ** contains the database locks. The following assert verifies |
| 4097 | ** that we do not. */ |
| 4098 | assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4099 | pData2 = CODEC2(pPager, pData, pPg->pgno, 7); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 4100 | cksum = pager_cksum(pPager, (u8*)pData2); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4101 | rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno); |
| 4102 | if( rc==SQLITE_OK ){ |
| 4103 | rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, |
| 4104 | pPager->journalOff + 4); |
| 4105 | pPager->journalOff += pPager->pageSize+4; |
| 4106 | } |
| 4107 | if( rc==SQLITE_OK ){ |
| 4108 | rc = write32bits(pPager->jfd, pPager->journalOff, cksum); |
| 4109 | pPager->journalOff += 4; |
| 4110 | } |
| 4111 | IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, |
danielk1977 | 667a6c9 | 2007-09-10 06:12:04 +0000 | [diff] [blame] | 4112 | pPager->journalOff, pPager->pageSize)); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4113 | PAGER_INCR(sqlite3_pager_writej_count); |
drh | 477731b | 2007-06-16 03:06:27 +0000 | [diff] [blame] | 4114 | PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n", |
| 4115 | PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg)); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4116 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 4117 | /* An error has occured writing to the journal file. The |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4118 | ** transaction will be rolled back by the layer above. |
| 4119 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4120 | if( rc!=SQLITE_OK ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4121 | return rc; |
| 4122 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4123 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4124 | pPager->nRec++; |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4125 | assert( pPager->pInJournal!=0 ); |
| 4126 | sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4127 | pPg->needSync = !pPager->noSync; |
| 4128 | if( pPager->stmtInUse ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4129 | sqlite3BitvecSet(pPager->pInStmt, pPg->pgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4130 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4131 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 4132 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4133 | pPg->needSync = !pPager->journalStarted && !pPager->noSync; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4134 | PAGERTRACE4("APPEND %d page %d needSync=%d\n", |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 4135 | PAGERID(pPager), pPg->pgno, pPg->needSync); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4136 | } |
| 4137 | if( pPg->needSync ){ |
| 4138 | pPager->needSync = 1; |
| 4139 | } |
| 4140 | pPg->inJournal = 1; |
| 4141 | } |
| 4142 | |
| 4143 | /* If the statement journal is open and the page is not in it, |
| 4144 | ** then write the current page to the statement journal. Note that |
| 4145 | ** the statement journal format differs from the standard journal format |
| 4146 | ** in that it omits the checksums and the header. |
| 4147 | */ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4148 | if( pPager->stmtInUse |
| 4149 | && !pageInStatement(pPg) |
| 4150 | && (int)pPg->pgno<=pPager->stmtSize |
| 4151 | ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4152 | assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); |
| 4153 | if( MEMDB ){ |
| 4154 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 4155 | assert( pHist->pStmt==0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4156 | pHist->pStmt = sqlite3_malloc( pPager->pageSize ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4157 | if( pHist->pStmt ){ |
| 4158 | memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize); |
| 4159 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4160 | PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4161 | page_add_to_stmt_list(pPg); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4162 | }else{ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4163 | i64 offset = pPager->stmtNRec*(4+pPager->pageSize); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4164 | char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7); |
| 4165 | rc = write32bits(pPager->stfd, offset, pPg->pgno); |
| 4166 | if( rc==SQLITE_OK ){ |
| 4167 | rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4); |
| 4168 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4169 | PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4170 | if( rc!=SQLITE_OK ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4171 | return rc; |
| 4172 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4173 | pPager->stmtNRec++; |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4174 | assert( pPager->pInStmt!=0 ); |
| 4175 | sqlite3BitvecSet(pPager->pInStmt, pPg->pgno); |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 4176 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4177 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | /* Update the database size and return. |
| 4181 | */ |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 4182 | assert( pPager->state>=PAGER_SHARED ); |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 4183 | if( pPager->dbSize<(int)pPg->pgno ){ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 4184 | pPager->dbSize = pPg->pgno; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4185 | if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){ |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 4186 | pPager->dbSize++; |
| 4187 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 4188 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 4189 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | /* |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4193 | ** This function is used to mark a data-page as writable. It uses |
| 4194 | ** pager_write() to open a journal file (if it is not already open) |
| 4195 | ** and write the page *pData to the journal. |
| 4196 | ** |
| 4197 | ** The difference between this function and pager_write() is that this |
| 4198 | ** function also deals with the special case where 2 or more pages |
| 4199 | ** fit on a single disk sector. In this case all co-resident pages |
| 4200 | ** must have been written to the journal file before returning. |
| 4201 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4202 | int sqlite3PagerWrite(DbPage *pDbPage){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4203 | int rc = SQLITE_OK; |
| 4204 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4205 | PgHdr *pPg = pDbPage; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4206 | Pager *pPager = pPg->pPager; |
| 4207 | Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); |
| 4208 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4209 | pagerEnter(pPager); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4210 | if( !MEMDB && nPagePerSector>1 ){ |
| 4211 | Pgno nPageCount; /* Total number of pages in database file */ |
| 4212 | Pgno pg1; /* First page of the sector pPg is located on. */ |
| 4213 | int nPage; /* Number of pages starting at pg1 to journal */ |
| 4214 | int ii; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4215 | int needSync = 0; |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4216 | |
| 4217 | /* Set the doNotSync flag to 1. This is because we cannot allow a journal |
| 4218 | ** header to be written between the pages journaled by this function. |
| 4219 | */ |
| 4220 | assert( pPager->doNotSync==0 ); |
| 4221 | pPager->doNotSync = 1; |
| 4222 | |
| 4223 | /* This trick assumes that both the page-size and sector-size are |
| 4224 | ** an integer power of 2. It sets variable pg1 to the identifier |
| 4225 | ** of the first page of the sector pPg is located on. |
| 4226 | */ |
| 4227 | pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; |
| 4228 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4229 | nPageCount = sqlite3PagerPagecount(pPager); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4230 | if( pPg->pgno>nPageCount ){ |
| 4231 | nPage = (pPg->pgno - pg1)+1; |
| 4232 | }else if( (pg1+nPagePerSector-1)>nPageCount ){ |
| 4233 | nPage = nPageCount+1-pg1; |
| 4234 | }else{ |
| 4235 | nPage = nPagePerSector; |
| 4236 | } |
| 4237 | assert(nPage>0); |
| 4238 | assert(pg1<=pPg->pgno); |
| 4239 | assert((pg1+nPage)>pPg->pgno); |
| 4240 | |
| 4241 | for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ |
| 4242 | Pgno pg = pg1+ii; |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4243 | PgHdr *pPage; |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4244 | if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4245 | if( pg!=PAGER_MJ_PGNO(pPager) ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4246 | rc = sqlite3PagerGet(pPager, pg, &pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4247 | if( rc==SQLITE_OK ){ |
| 4248 | rc = pager_write(pPage); |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4249 | if( pPage->needSync ){ |
| 4250 | needSync = 1; |
| 4251 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4252 | sqlite3PagerUnref(pPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4253 | } |
| 4254 | } |
drh | c81945e | 2008-03-10 14:12:53 +0000 | [diff] [blame^] | 4255 | }else if( (pPage = pager_lookup(pPager, pg))!=0 ){ |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4256 | if( pPage->needSync ){ |
| 4257 | needSync = 1; |
| 4258 | } |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4259 | } |
| 4260 | } |
| 4261 | |
danielk1977 | dd97a49 | 2007-08-22 18:54:32 +0000 | [diff] [blame] | 4262 | /* If the PgHdr.needSync flag is set for any of the nPage pages |
| 4263 | ** starting at pg1, then it needs to be set for all of them. Because |
| 4264 | ** writing to any of these nPage pages may damage the others, the |
| 4265 | ** journal file must contain sync()ed copies of all of them |
| 4266 | ** before any of them can be written out to the database file. |
| 4267 | */ |
| 4268 | if( needSync ){ |
| 4269 | for(ii=0; ii<nPage && needSync; ii++){ |
| 4270 | PgHdr *pPage = pager_lookup(pPager, pg1+ii); |
| 4271 | if( pPage ) pPage->needSync = 1; |
| 4272 | } |
| 4273 | assert(pPager->needSync); |
| 4274 | } |
| 4275 | |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4276 | assert( pPager->doNotSync==1 ); |
| 4277 | pPager->doNotSync = 0; |
| 4278 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4279 | rc = pager_write(pDbPage); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4280 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4281 | pagerLeave(pPager); |
danielk1977 | 4099f6e | 2007-03-19 11:25:20 +0000 | [diff] [blame] | 4282 | return rc; |
| 4283 | } |
| 4284 | |
| 4285 | /* |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 4286 | ** Return TRUE if the page given in the argument was previously passed |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4287 | ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4288 | ** to change the content of the page. |
| 4289 | */ |
danielk1977 | 7d3a666 | 2006-01-23 16:21:05 +0000 | [diff] [blame] | 4290 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4291 | int sqlite3PagerIswriteable(DbPage *pPg){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4292 | return pPg->dirty; |
| 4293 | } |
danielk1977 | 7d3a666 | 2006-01-23 16:21:05 +0000 | [diff] [blame] | 4294 | #endif |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4295 | |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 4296 | #ifndef SQLITE_OMIT_VACUUM |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4297 | /* |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4298 | ** Replace the content of a single page with the information in the third |
| 4299 | ** argument. |
| 4300 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4301 | int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){ |
| 4302 | PgHdr *pPg; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4303 | int rc; |
| 4304 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4305 | pagerEnter(pPager); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4306 | rc = sqlite3PagerGet(pPager, pgno, &pPg); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4307 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4308 | rc = sqlite3PagerWrite(pPg); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4309 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4310 | memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4311 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4312 | sqlite3PagerUnref(pPg); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4313 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4314 | pagerLeave(pPager); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4315 | return rc; |
| 4316 | } |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 4317 | #endif |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4318 | |
| 4319 | /* |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4320 | ** 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] | 4321 | ** write the information on page pPg back to the disk, even though |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4322 | ** that page might be marked as dirty. |
| 4323 | ** |
| 4324 | ** The overlying software layer calls this routine when all of the data |
| 4325 | ** on the given page is unused. The pager marks the page as clean so |
| 4326 | ** that it does not get written to disk. |
| 4327 | ** |
| 4328 | ** Tests show that this optimization, together with the |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4329 | ** sqlite3PagerDontRollback() below, more than double the speed |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4330 | ** of large INSERT operations and quadruple the speed of large DELETEs. |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4331 | ** |
| 4332 | ** When this routine is called, set the alwaysRollback flag to true. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4333 | ** Subsequent calls to sqlite3PagerDontRollback() for the same page |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4334 | ** will thereafter be ignored. This is necessary to avoid a problem |
| 4335 | ** where a page with data is added to the freelist during one part of |
| 4336 | ** a transaction then removed from the freelist during a later part |
| 4337 | ** of the same transaction and reused for some other purpose. When it |
| 4338 | ** is first added to the freelist, this routine is called. When reused, |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4339 | ** the sqlite3PagerDontRollback() routine is called. But because the |
| 4340 | ** page contains critical data, we still need to be sure it gets |
| 4341 | ** rolled back in spite of the sqlite3PagerDontRollback() call. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4342 | */ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4343 | void sqlite3PagerDontWrite(DbPage *pDbPage){ |
| 4344 | PgHdr *pPg = pDbPage; |
| 4345 | Pager *pPager = pPg->pPager; |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4346 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4347 | if( MEMDB ) return; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4348 | pagerEnter(pPager); |
drh | 8e298f9 | 2002-07-06 16:28:47 +0000 | [diff] [blame] | 4349 | pPg->alwaysRollback = 1; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4350 | if( pPg->dirty && !pPager->stmtInUse ){ |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 4351 | assert( pPager->state>=PAGER_SHARED ); |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 4352 | if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){ |
| 4353 | /* If this pages is the last page in the file and the file has grown |
| 4354 | ** during the current transaction, then do NOT mark the page as clean. |
| 4355 | ** When the database file grows, we must make sure that the last page |
| 4356 | ** gets written at least once so that the disk file will be the correct |
| 4357 | ** size. If you do not write this page and the size of the file |
| 4358 | ** on the disk ends up being too small, that can lead to database |
| 4359 | ** corruption during the next transaction. |
| 4360 | */ |
| 4361 | }else{ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4362 | PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)); |
| 4363 | IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4364 | makeClean(pPg); |
danielk1977 | 3c40737 | 2005-02-15 02:54:14 +0000 | [diff] [blame] | 4365 | #ifdef SQLITE_CHECK_PAGES |
| 4366 | pPg->pageHash = pager_pagehash(pPg); |
| 4367 | #endif |
drh | 8124a30 | 2002-06-25 14:43:57 +0000 | [diff] [blame] | 4368 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4369 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4370 | pagerLeave(pPager); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | /* |
| 4374 | ** A call to this routine tells the pager that if a rollback occurs, |
| 4375 | ** it is not necessary to restore the data on the given page. This |
| 4376 | ** means that the pager does not have to record the given page in the |
| 4377 | ** rollback journal. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4378 | ** |
| 4379 | ** If we have not yet actually read the content of this page (if |
| 4380 | ** the PgHdr.needRead flag is set) then this routine acts as a promise |
| 4381 | ** that we will never need to read the page content in the future. |
| 4382 | ** so the needRead flag can be cleared at this point. |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 4383 | ** |
| 4384 | ** This routine is only called from a single place in the sqlite btree |
| 4385 | ** code (when a leaf is removed from the free-list). This allows the |
| 4386 | ** following assumptions to be made about pPg: |
| 4387 | ** |
| 4388 | ** 1. PagerDontWrite() has been called on the page, OR |
| 4389 | ** PagerWrite() has not yet been called on the page. |
| 4390 | ** |
| 4391 | ** 2. The page existed when the transaction was started. |
| 4392 | ** |
| 4393 | ** Details: DontRollback() (this routine) is only called when a leaf is |
| 4394 | ** removed from the free list. DontWrite() is called whenever a page |
| 4395 | ** becomes a free-list leaf. |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4396 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4397 | void sqlite3PagerDontRollback(DbPage *pPg){ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4398 | Pager *pPager = pPg->pPager; |
| 4399 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4400 | pagerEnter(pPager); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4401 | assert( pPager->state>=PAGER_RESERVED ); |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 4402 | |
| 4403 | /* If the journal file is not open, or DontWrite() has been called on |
| 4404 | ** this page (DontWrite() sets the alwaysRollback flag), then this |
| 4405 | ** function is a no-op. |
| 4406 | */ |
| 4407 | if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){ |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4408 | pagerLeave(pPager); |
| 4409 | return; |
| 4410 | } |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 4411 | assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */ |
| 4412 | |
| 4413 | /* Check that PagerWrite() has not yet been called on this page, and |
| 4414 | ** that the page existed when the transaction started. |
| 4415 | */ |
| 4416 | assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); |
| 4417 | |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4418 | assert( pPager->pInJournal!=0 ); |
| 4419 | sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 4420 | pPg->inJournal = 1; |
| 4421 | pPg->needRead = 0; |
| 4422 | if( pPager->stmtInUse ){ |
| 4423 | assert( pPager->stmtSize <= pPager->origDbSize ); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4424 | sqlite3BitvecSet(pPager->pInStmt, pPg->pgno); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4425 | } |
danielk1977 | a55e935 | 2008-01-21 13:04:34 +0000 | [diff] [blame] | 4426 | PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)); |
| 4427 | IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno)) |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4428 | pagerLeave(pPager); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4431 | |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4432 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4433 | ** This routine is called to increment the database file change-counter, |
| 4434 | ** stored at byte 24 of the pager file. |
| 4435 | */ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4436 | static int pager_incr_changecounter(Pager *pPager, int isDirect){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4437 | PgHdr *pPgHdr; |
| 4438 | u32 change_counter; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4439 | int rc = SQLITE_OK; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4440 | |
| 4441 | if( !pPager->changeCountDone ){ |
| 4442 | /* Open page 1 of the file for writing. */ |
| 4443 | rc = sqlite3PagerGet(pPager, 1, &pPgHdr); |
| 4444 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4445 | |
| 4446 | if( !isDirect ){ |
| 4447 | rc = sqlite3PagerWrite(pPgHdr); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 4448 | if( rc!=SQLITE_OK ){ |
| 4449 | sqlite3PagerUnref(pPgHdr); |
| 4450 | return rc; |
| 4451 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4452 | } |
| 4453 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4454 | /* Increment the value just read and write it back to byte 24. */ |
drh | b100391 | 2007-07-20 00:33:36 +0000 | [diff] [blame] | 4455 | change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4456 | change_counter++; |
| 4457 | put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4458 | |
| 4459 | if( isDirect && pPager->fd->pMethods ){ |
| 4460 | const void *zBuf = PGHDR_TO_DATA(pPgHdr); |
| 4461 | rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); |
| 4462 | } |
| 4463 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4464 | /* Release the page reference. */ |
| 4465 | sqlite3PagerUnref(pPgHdr); |
| 4466 | pPager->changeCountDone = 1; |
| 4467 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4468 | return rc; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | /* |
| 4472 | ** Sync the database file for the pager pPager. zMaster points to the name |
| 4473 | ** of a master journal file that should be written into the individual |
| 4474 | ** journal file. zMaster may be NULL, which is interpreted as no master |
| 4475 | ** journal (a single database transaction). |
| 4476 | ** |
| 4477 | ** This routine ensures that the journal is synced, all dirty pages written |
| 4478 | ** to the database file and the database file synced. The only thing that |
| 4479 | ** remains to commit the transaction is to delete the journal file (or |
| 4480 | ** master journal file if specified). |
| 4481 | ** |
| 4482 | ** Note that if zMaster==NULL, this does not overwrite a previous value |
| 4483 | ** passed to an sqlite3PagerCommitPhaseOne() call. |
| 4484 | ** |
| 4485 | ** If parameter nTrunc is non-zero, then the pager file is truncated to |
| 4486 | ** nTrunc pages (this is used by auto-vacuum databases). |
| 4487 | */ |
| 4488 | int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){ |
| 4489 | int rc = SQLITE_OK; |
| 4490 | |
| 4491 | PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n", |
| 4492 | pPager->zFilename, zMaster, nTrunc); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4493 | pagerEnter(pPager); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4494 | |
| 4495 | /* If this is an in-memory db, or no pages have been written to, or this |
| 4496 | ** function has already been called, it is a no-op. |
| 4497 | */ |
| 4498 | if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){ |
| 4499 | PgHdr *pPg; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4500 | |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4501 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 4502 | /* The atomic-write optimization can be used if all of the |
| 4503 | ** following are true: |
| 4504 | ** |
| 4505 | ** + The file-system supports the atomic-write property for |
| 4506 | ** blocks of size page-size, and |
| 4507 | ** + This commit is not part of a multi-file transaction, and |
| 4508 | ** + Exactly one page has been modified and store in the journal file. |
| 4509 | ** |
| 4510 | ** If the optimization can be used, then the journal file will never |
| 4511 | ** be created for this transaction. |
| 4512 | */ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4513 | int useAtomicWrite = ( |
| 4514 | !zMaster && |
| 4515 | pPager->journalOff==jrnlBufferSize(pPager) && |
| 4516 | nTrunc==0 && |
| 4517 | (0==pPager->pDirty || 0==pPager->pDirty->pDirty) |
| 4518 | ); |
| 4519 | if( useAtomicWrite ){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4520 | /* Update the nRec field in the journal file. */ |
| 4521 | int offset = pPager->journalHdr + sizeof(aJournalMagic); |
| 4522 | assert(pPager->nRec==1); |
| 4523 | rc = write32bits(pPager->jfd, offset, pPager->nRec); |
| 4524 | |
| 4525 | /* Update the db file change counter. The following call will modify |
| 4526 | ** the in-memory representation of page 1 to include the updated |
| 4527 | ** change counter and then write page 1 directly to the database |
| 4528 | ** file. Because of the atomic-write property of the host file-system, |
| 4529 | ** this is safe. |
| 4530 | */ |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 4531 | if( rc==SQLITE_OK ){ |
| 4532 | rc = pager_incr_changecounter(pPager, 1); |
| 4533 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4534 | }else{ |
| 4535 | rc = sqlite3JournalCreate(pPager->jfd); |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 4538 | if( !useAtomicWrite && rc==SQLITE_OK ) |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4539 | #endif |
| 4540 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4541 | /* If a master journal file name has already been written to the |
| 4542 | ** journal file, then no sync is required. This happens when it is |
| 4543 | ** written, then the process fails to upgrade from a RESERVED to an |
| 4544 | ** EXCLUSIVE lock. The next time the process tries to commit the |
| 4545 | ** transaction the m-j name will have already been written. |
| 4546 | */ |
| 4547 | if( !pPager->setMaster ){ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 4548 | assert( pPager->journalOpen ); |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4549 | rc = pager_incr_changecounter(pPager, 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4550 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4551 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4552 | if( nTrunc!=0 ){ |
| 4553 | /* If this transaction has made the database smaller, then all pages |
| 4554 | ** being discarded by the truncation must be written to the journal |
| 4555 | ** file. |
| 4556 | */ |
| 4557 | Pgno i; |
| 4558 | int iSkip = PAGER_MJ_PGNO(pPager); |
| 4559 | for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4560 | if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4561 | rc = sqlite3PagerGet(pPager, i, &pPg); |
| 4562 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4563 | rc = sqlite3PagerWrite(pPg); |
| 4564 | sqlite3PagerUnref(pPg); |
| 4565 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4566 | } |
| 4567 | } |
| 4568 | } |
| 4569 | #endif |
| 4570 | rc = writeMasterJournal(pPager, zMaster); |
| 4571 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4572 | rc = syncJournal(pPager); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4573 | } |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 4574 | if( rc!=SQLITE_OK ) goto sync_exit; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4575 | |
| 4576 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4577 | if( nTrunc!=0 ){ |
| 4578 | rc = sqlite3PagerTruncate(pPager, nTrunc); |
| 4579 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 4580 | } |
| 4581 | #endif |
| 4582 | |
| 4583 | /* Write all dirty pages to the database file */ |
| 4584 | pPg = pager_get_all_dirty_pages(pPager); |
| 4585 | rc = pager_write_pagelist(pPg); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4586 | if( rc!=SQLITE_OK ){ |
drh | 04c3a46 | 2008-02-26 16:16:45 +0000 | [diff] [blame] | 4587 | assert( rc!=SQLITE_IOERR_BLOCKED ); |
| 4588 | /* The error might have left the dirty list all fouled up here, |
| 4589 | ** but that does not matter because if the if the dirty list did |
| 4590 | ** get corrupted, then the transaction will roll back and |
| 4591 | ** discard the dirty list. There is an assert in |
| 4592 | ** pager_get_all_dirty_pages() that verifies that no attempt |
| 4593 | ** is made to use an invalid dirty list. |
| 4594 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4595 | goto sync_exit; |
| 4596 | } |
drh | 3cdd7d3 | 2007-03-30 14:46:01 +0000 | [diff] [blame] | 4597 | pPager->pDirty = 0; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4598 | |
| 4599 | /* Sync the database file. */ |
drh | 1cc8c44 | 2007-08-24 16:08:29 +0000 | [diff] [blame] | 4600 | if( !pPager->noSync ){ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 4601 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4602 | } |
| 4603 | IOTRACE(("DBSYNC %p\n", pPager)) |
| 4604 | |
| 4605 | pPager->state = PAGER_SYNCED; |
| 4606 | }else if( MEMDB && nTrunc!=0 ){ |
| 4607 | rc = sqlite3PagerTruncate(pPager, nTrunc); |
| 4608 | } |
| 4609 | |
| 4610 | sync_exit: |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 4611 | if( rc==SQLITE_IOERR_BLOCKED ){ |
| 4612 | /* pager_incr_changecounter() may attempt to obtain an exclusive |
| 4613 | * lock to spill the cache and return IOERR_BLOCKED. But since |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 4614 | * there is no chance the cache is inconsistent, it is |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 4615 | * better to return SQLITE_BUSY. |
| 4616 | */ |
| 4617 | rc = SQLITE_BUSY; |
| 4618 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4619 | pagerLeave(pPager); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4620 | return rc; |
| 4621 | } |
| 4622 | |
| 4623 | |
| 4624 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4625 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4626 | ** |
| 4627 | ** If the commit fails for any reason, a rollback attempt is made |
| 4628 | ** and an error code is returned. If the commit worked, SQLITE_OK |
| 4629 | ** is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4630 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4631 | int sqlite3PagerCommitPhaseTwo(Pager *pPager){ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 4632 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4633 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4634 | |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4635 | if( pPager->errCode ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 4636 | return pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4637 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4638 | if( pPager->state<PAGER_RESERVED ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4639 | return SQLITE_ERROR; |
| 4640 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4641 | pagerEnter(pPager); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4642 | PAGERTRACE2("COMMIT %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4643 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4644 | pPg = pager_get_all_dirty_pages(pPager); |
| 4645 | while( pPg ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4646 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 4647 | clearHistory(pHist); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4648 | pPg->dirty = 0; |
| 4649 | pPg->inJournal = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4650 | pHist->inStmt = 0; |
drh | 5229ae4 | 2006-03-23 23:29:04 +0000 | [diff] [blame] | 4651 | pPg->needSync = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4652 | pHist->pPrevStmt = pHist->pNextStmt = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4653 | pPg = pPg->pDirty; |
| 4654 | } |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4655 | pPager->pDirty = 0; |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 4656 | #ifndef NDEBUG |
| 4657 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 4658 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
| 4659 | assert( !pPg->alwaysRollback ); |
| 4660 | assert( !pHist->pOrig ); |
| 4661 | assert( !pHist->pStmt ); |
| 4662 | } |
| 4663 | #endif |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4664 | pPager->pStmt = 0; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4665 | pPager->state = PAGER_SHARED; |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4666 | pagerLeave(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4667 | return SQLITE_OK; |
| 4668 | } |
drh | 3cdd7d3 | 2007-03-30 14:46:01 +0000 | [diff] [blame] | 4669 | assert( pPager->journalOpen || !pPager->dirtyCache ); |
| 4670 | assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache ); |
| 4671 | rc = pager_end_transaction(pPager); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4672 | rc = pager_error(pPager, rc); |
| 4673 | pagerLeave(pPager); |
| 4674 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
| 4677 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4678 | ** Rollback all changes. The database falls back to PAGER_SHARED mode. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4679 | ** All in-memory cache pages revert to their original data contents. |
| 4680 | ** The journal is deleted. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4681 | ** |
| 4682 | ** This routine cannot fail unless some other process is not following |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 4683 | ** the correct locking protocol or unless some other |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4684 | ** process is writing trash into the journal file (SQLITE_CORRUPT) or |
| 4685 | ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error |
| 4686 | ** codes are returned for all these occasions. Otherwise, |
| 4687 | ** SQLITE_OK is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4688 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4689 | int sqlite3PagerRollback(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4690 | int rc; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4691 | PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4692 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4693 | PgHdr *p; |
| 4694 | for(p=pPager->pAll; p; p=p->pNextAll){ |
| 4695 | PgHistory *pHist; |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 4696 | assert( !p->alwaysRollback ); |
| 4697 | if( !p->dirty ){ |
| 4698 | assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig ); |
| 4699 | assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt ); |
| 4700 | continue; |
| 4701 | } |
| 4702 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4703 | pHist = PGHDR_TO_HIST(p, pPager); |
| 4704 | if( pHist->pOrig ){ |
| 4705 | memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4706 | PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager)); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4707 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4708 | PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager)); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4709 | } |
| 4710 | clearHistory(pHist); |
| 4711 | p->dirty = 0; |
| 4712 | p->inJournal = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4713 | pHist->inStmt = 0; |
| 4714 | pHist->pPrevStmt = pHist->pNextStmt = 0; |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 4715 | if( pPager->xReiniter ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4716 | pPager->xReiniter(p, pPager->pageSize); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 4717 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4718 | } |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 4719 | pPager->pDirty = 0; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4720 | pPager->pStmt = 0; |
| 4721 | pPager->dbSize = pPager->origDbSize; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 4722 | pager_truncate_cache(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4723 | pPager->stmtInUse = 0; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4724 | pPager->state = PAGER_SHARED; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4725 | return SQLITE_OK; |
| 4726 | } |
| 4727 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4728 | pagerEnter(pPager); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4729 | if( !pPager->dirtyCache || !pPager->journalOpen ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4730 | rc = pager_end_transaction(pPager); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4731 | pagerLeave(pPager); |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4732 | return rc; |
| 4733 | } |
drh | db48ee0 | 2003-01-16 13:42:43 +0000 | [diff] [blame] | 4734 | |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4735 | if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4736 | if( pPager->state>=PAGER_EXCLUSIVE ){ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 4737 | pager_playback(pPager, 0); |
drh | 4b845d7 | 2002-03-05 12:41:19 +0000 | [diff] [blame] | 4738 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4739 | pagerLeave(pPager); |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4740 | return pPager->errCode; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 4741 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4742 | if( pPager->state==PAGER_RESERVED ){ |
danielk1977 | 1722181 | 2005-02-15 03:38:05 +0000 | [diff] [blame] | 4743 | int rc2; |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 4744 | rc = pager_playback(pPager, 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 4745 | rc2 = pager_end_transaction(pPager); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 4746 | if( rc==SQLITE_OK ){ |
| 4747 | rc = rc2; |
| 4748 | } |
| 4749 | }else{ |
danielk1977 | e277be0 | 2007-03-23 18:12:06 +0000 | [diff] [blame] | 4750 | rc = pager_playback(pPager, 0); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4751 | } |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 4752 | /* pager_reset(pPager); */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4753 | pPager->dbSize = -1; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4754 | |
| 4755 | /* If an error occurs during a ROLLBACK, we can no longer trust the pager |
| 4756 | ** cache. So call pager_error() on the way out to make any error |
| 4757 | ** persistent. |
| 4758 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4759 | rc = pager_error(pPager, rc); |
| 4760 | pagerLeave(pPager); |
| 4761 | return rc; |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 4762 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4763 | |
| 4764 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4765 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 4766 | ** if the database is (in theory) writable. |
| 4767 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4768 | int sqlite3PagerIsreadonly(Pager *pPager){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 4769 | return pPager->readOnly; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4770 | } |
| 4771 | |
| 4772 | /* |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4773 | ** Return the number of references to the pager. |
| 4774 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4775 | int sqlite3PagerRefcount(Pager *pPager){ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4776 | return pPager->nRef; |
| 4777 | } |
| 4778 | |
| 4779 | #ifdef SQLITE_TEST |
| 4780 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4781 | ** This routine is used for testing and analysis only. |
| 4782 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4783 | int *sqlite3PagerStats(Pager *pPager){ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 4784 | static int a[11]; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4785 | a[0] = pPager->nRef; |
| 4786 | a[1] = pPager->nPage; |
| 4787 | a[2] = pPager->mxPage; |
| 4788 | a[3] = pPager->dbSize; |
| 4789 | a[4] = pPager->state; |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 4790 | a[5] = pPager->errCode; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4791 | a[6] = pPager->nHit; |
| 4792 | a[7] = pPager->nMiss; |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4793 | a[8] = 0; /* Used to be pPager->nOvfl */ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 4794 | a[9] = pPager->nRead; |
| 4795 | a[10] = pPager->nWrite; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 4796 | return a; |
| 4797 | } |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4798 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4799 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4800 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4801 | ** Set the statement rollback point. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4802 | ** |
| 4803 | ** This routine should be called with the transaction journal already |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4804 | ** open. A new statement journal is created that can be used to rollback |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4805 | ** changes of a single SQL command within a larger transaction. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4806 | */ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4807 | static int pagerStmtBegin(Pager *pPager){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4808 | int rc; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4809 | assert( !pPager->stmtInUse ); |
drh | 1aa2d8b | 2007-01-03 15:34:29 +0000 | [diff] [blame] | 4810 | assert( pPager->state>=PAGER_SHARED ); |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 4811 | assert( pPager->dbSize>=0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4812 | PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4813 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4814 | pPager->stmtInUse = 1; |
| 4815 | pPager->stmtSize = pPager->dbSize; |
| 4816 | return SQLITE_OK; |
| 4817 | } |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4818 | if( !pPager->journalOpen ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4819 | pPager->stmtAutoopen = 1; |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 4820 | return SQLITE_OK; |
| 4821 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4822 | assert( pPager->journalOpen ); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 4823 | pagerLeave(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4824 | assert( pPager->pInStmt==0 ); |
| 4825 | pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize); |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 4826 | pagerEnter(pPager); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4827 | if( pPager->pInStmt==0 ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4828 | /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4829 | return SQLITE_NOMEM; |
| 4830 | } |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 4831 | #ifndef NDEBUG |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4832 | rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4833 | if( rc ) goto stmt_begin_failed; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 4834 | assert( pPager->stmtJSize == pPager->journalOff ); |
drh | 968af52 | 2003-02-11 14:55:40 +0000 | [diff] [blame] | 4835 | #endif |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 4836 | pPager->stmtJSize = pPager->journalOff; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4837 | pPager->stmtSize = pPager->dbSize; |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 4838 | pPager->stmtHdrOff = 0; |
danielk1977 | 75edc16 | 2004-06-26 01:48:18 +0000 | [diff] [blame] | 4839 | pPager->stmtCksum = pPager->cksumInit; |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4840 | if( !pPager->stmtOpen ){ |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 4841 | rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl, |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4842 | SQLITE_OPEN_SUBJOURNAL); |
drh | 334b299 | 2007-09-06 23:28:23 +0000 | [diff] [blame] | 4843 | if( rc ){ |
| 4844 | goto stmt_begin_failed; |
| 4845 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4846 | pPager->stmtOpen = 1; |
| 4847 | pPager->stmtNRec = 0; |
drh | 0f89253 | 2002-05-30 12:27:03 +0000 | [diff] [blame] | 4848 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4849 | pPager->stmtInUse = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4850 | return SQLITE_OK; |
| 4851 | |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4852 | stmt_begin_failed: |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4853 | if( pPager->pInStmt ){ |
| 4854 | sqlite3BitvecDestroy(pPager->pInStmt); |
| 4855 | pPager->pInStmt = 0; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4856 | } |
| 4857 | return rc; |
| 4858 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4859 | int sqlite3PagerStmtBegin(Pager *pPager){ |
| 4860 | int rc; |
| 4861 | pagerEnter(pPager); |
| 4862 | rc = pagerStmtBegin(pPager); |
| 4863 | pagerLeave(pPager); |
| 4864 | return rc; |
| 4865 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4866 | |
| 4867 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4868 | ** Commit a statement. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4869 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4870 | int sqlite3PagerStmtCommit(Pager *pPager){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4871 | pagerEnter(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4872 | if( pPager->stmtInUse ){ |
drh | 03eb96a | 2002-11-10 23:32:56 +0000 | [diff] [blame] | 4873 | PgHdr *pPg, *pNext; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4874 | PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4875 | if( !MEMDB ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4876 | /* sqlite3OsTruncate(pPager->stfd, 0); */ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 4877 | sqlite3BitvecDestroy(pPager->pInStmt); |
| 4878 | pPager->pInStmt = 0; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4879 | }else{ |
| 4880 | for(pPg=pPager->pStmt; pPg; pPg=pNext){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4881 | PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4882 | pNext = pHist->pNextStmt; |
| 4883 | assert( pHist->inStmt ); |
| 4884 | pHist->inStmt = 0; |
| 4885 | pHist->pPrevStmt = pHist->pNextStmt = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4886 | sqlite3_free(pHist->pStmt); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4887 | pHist->pStmt = 0; |
| 4888 | } |
| 4889 | } |
| 4890 | pPager->stmtNRec = 0; |
| 4891 | pPager->stmtInUse = 0; |
| 4892 | pPager->pStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 4893 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4894 | pPager->stmtAutoopen = 0; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4895 | pagerLeave(pPager); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4896 | return SQLITE_OK; |
| 4897 | } |
| 4898 | |
| 4899 | /* |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4900 | ** Rollback a statement. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4901 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4902 | int sqlite3PagerStmtRollback(Pager *pPager){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4903 | int rc; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4904 | pagerEnter(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4905 | if( pPager->stmtInUse ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4906 | PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager)); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4907 | if( MEMDB ){ |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4908 | PgHdr *pPg; |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 4909 | PgHistory *pHist; |
| 4910 | for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){ |
| 4911 | pHist = PGHDR_TO_HIST(pPg, pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4912 | if( pHist->pStmt ){ |
| 4913 | memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4914 | sqlite3_free(pHist->pStmt); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4915 | pHist->pStmt = 0; |
| 4916 | } |
| 4917 | } |
| 4918 | pPager->dbSize = pPager->stmtSize; |
danielk1977 | e180dd9 | 2007-04-05 17:15:52 +0000 | [diff] [blame] | 4919 | pager_truncate_cache(pPager); |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4920 | rc = SQLITE_OK; |
| 4921 | }else{ |
| 4922 | rc = pager_stmt_playback(pPager); |
| 4923 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4924 | sqlite3PagerStmtCommit(pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 4925 | }else{ |
| 4926 | rc = SQLITE_OK; |
| 4927 | } |
drh | ac69b05 | 2004-05-12 13:30:07 +0000 | [diff] [blame] | 4928 | pPager->stmtAutoopen = 0; |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4929 | pagerLeave(pPager); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 4930 | return rc; |
| 4931 | } |
| 4932 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4933 | /* |
| 4934 | ** Return the full pathname of the database file. |
| 4935 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4936 | const char *sqlite3PagerFilename(Pager *pPager){ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4937 | return pPager->zFilename; |
| 4938 | } |
| 4939 | |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4940 | /* |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 4941 | ** Return the VFS structure for the pager. |
| 4942 | */ |
| 4943 | const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ |
| 4944 | return pPager->pVfs; |
| 4945 | } |
| 4946 | |
| 4947 | /* |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 4948 | ** Return the file handle for the database file associated |
| 4949 | ** with the pager. This might return NULL if the file has |
| 4950 | ** not yet been opened. |
| 4951 | */ |
| 4952 | sqlite3_file *sqlite3PagerFile(Pager *pPager){ |
| 4953 | return pPager->fd; |
| 4954 | } |
| 4955 | |
| 4956 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4957 | ** Return the directory of the database file. |
| 4958 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4959 | const char *sqlite3PagerDirname(Pager *pPager){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4960 | return pPager->zDirectory; |
| 4961 | } |
| 4962 | |
| 4963 | /* |
| 4964 | ** Return the full pathname of the journal file. |
| 4965 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4966 | const char *sqlite3PagerJournalname(Pager *pPager){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4967 | return pPager->zJournal; |
| 4968 | } |
| 4969 | |
| 4970 | /* |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4971 | ** Return true if fsync() calls are disabled for this pager. Return FALSE |
| 4972 | ** if fsync()s are executed normally. |
| 4973 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4974 | int sqlite3PagerNosync(Pager *pPager){ |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4975 | return pPager->noSync; |
| 4976 | } |
| 4977 | |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4978 | #ifdef SQLITE_HAS_CODEC |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 4979 | /* |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4980 | ** Set the codec for this pager |
| 4981 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4982 | void sqlite3PagerSetCodec( |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4983 | Pager *pPager, |
drh | c001c58 | 2006-03-06 18:23:16 +0000 | [diff] [blame] | 4984 | void *(*xCodec)(void*,void*,Pgno,int), |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4985 | void *pCodecArg |
| 4986 | ){ |
| 4987 | pPager->xCodec = xCodec; |
| 4988 | pPager->pCodecArg = pCodecArg; |
| 4989 | } |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 4990 | #endif |
drh | b20ea9d | 2004-02-09 01:20:36 +0000 | [diff] [blame] | 4991 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4992 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4993 | /* |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 4994 | ** Move the page pPg to location pgno in the file. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4995 | ** |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 4996 | ** There must be no references to the page previously located at |
| 4997 | ** pgno (which we call pPgOld) though that page is allowed to be |
| 4998 | ** in cache. If the page previous located at pgno is not already |
| 4999 | ** in the rollback journal, it is not put there by by this routine. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5000 | ** |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 5001 | ** References to the page pPg remain valid. Updating any |
| 5002 | ** meta-data associated with pPg (i.e. data stored in the nExtra bytes |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5003 | ** allocated along with the page) is the responsibility of the caller. |
| 5004 | ** |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 5005 | ** A transaction must be active when this routine is called. It used to be |
| 5006 | ** required that a statement transaction was not active, but this restriction |
| 5007 | ** has been removed (CREATE INDEX needs to move a page when a statement |
| 5008 | ** transaction is active). |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5009 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5010 | int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){ |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 5011 | PgHdr *pPgOld; /* The page being overwritten. */ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5012 | int h; |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5013 | Pgno needSyncPgno = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5014 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 5015 | pagerEnter(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5016 | assert( pPg->nRef>0 ); |
| 5017 | |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5018 | PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n", |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5019 | PAGERID(pPager), pPg->pgno, pPg->needSync, pgno); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5020 | IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno)) |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 5021 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 5022 | pager_get_content(pPg); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5023 | if( pPg->needSync ){ |
| 5024 | needSyncPgno = pPg->pgno; |
drh | 4f0aee4 | 2007-06-16 18:39:41 +0000 | [diff] [blame] | 5025 | assert( pPg->inJournal || (int)pgno>pPager->origDbSize ); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5026 | assert( pPg->dirty ); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5027 | assert( pPager->needSync ); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5030 | /* Unlink pPg from its hash-chain */ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5031 | unlinkHashChain(pPager, pPg); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5032 | |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 5033 | /* If the cache contains a page with page-number pgno, remove it |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5034 | ** from its hash chain. Also, if the PgHdr.needSync was set for |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5035 | ** page pgno before the 'move' operation, it needs to be retained |
| 5036 | ** for the page moved there. |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5037 | */ |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 5038 | pPg->needSync = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5039 | pPgOld = pager_lookup(pPager, pgno); |
| 5040 | if( pPgOld ){ |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5041 | assert( pPgOld->nRef==0 ); |
| 5042 | unlinkHashChain(pPager, pPgOld); |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 5043 | makeClean(pPgOld); |
drh | 5e38531 | 2007-06-16 04:42:12 +0000 | [diff] [blame] | 5044 | pPg->needSync = pPgOld->needSync; |
| 5045 | }else{ |
| 5046 | pPg->needSync = 0; |
| 5047 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 5048 | pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5049 | |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5050 | /* 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] | 5051 | assert( pgno!=0 ); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5052 | pPg->pgno = pgno; |
drh | 8ca0c72 | 2006-05-07 17:49:38 +0000 | [diff] [blame] | 5053 | h = pgno & (pPager->nHash-1); |
danielk1977 | f5fdda8 | 2004-11-03 08:44:05 +0000 | [diff] [blame] | 5054 | if( pPager->aHash[h] ){ |
| 5055 | assert( pPager->aHash[h]->pPrevHash==0 ); |
| 5056 | pPager->aHash[h]->pPrevHash = pPg; |
| 5057 | } |
| 5058 | pPg->pNextHash = pPager->aHash[h]; |
| 5059 | pPager->aHash[h] = pPg; |
| 5060 | pPg->pPrevHash = 0; |
| 5061 | |
drh | 605ad8f | 2006-05-03 23:34:05 +0000 | [diff] [blame] | 5062 | makeDirty(pPg); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5063 | pPager->dirtyCache = 1; |
| 5064 | |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5065 | if( needSyncPgno ){ |
| 5066 | /* If needSyncPgno is non-zero, then the journal file needs to be |
| 5067 | ** sync()ed before any data is written to database file page needSyncPgno. |
| 5068 | ** Currently, no such page exists in the page-cache and the |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 5069 | ** Pager.pInJournal bit has been set. This needs to be remedied by loading |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5070 | ** the page into the pager-cache and setting the PgHdr.needSync flag. |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5071 | ** |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 5072 | ** If the attempt to load the page into the page-cache fails, (due |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 5073 | ** to a malloc() or IO failure), clear the bit in the pInJournal[] |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 5074 | ** array. Otherwise, if the page is loaded and written again in |
| 5075 | ** this transaction, it may be written to the database file before |
| 5076 | ** it is synced into the journal file. This way, it may end up in |
| 5077 | ** the journal file twice, but that is not a problem. |
| 5078 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5079 | ** The sqlite3PagerGet() call may cause the journal to sync. So make |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5080 | ** sure the Pager.needSync flag is set too. |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5081 | */ |
| 5082 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5083 | PgHdr *pPgHdr; |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5084 | assert( pPager->needSync ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5085 | rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 5086 | if( rc!=SQLITE_OK ){ |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 5087 | if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){ |
| 5088 | sqlite3BitvecClear(pPager->pInJournal, needSyncPgno); |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 5089 | } |
danielk1977 | 87c29a9 | 2008-01-18 11:33:16 +0000 | [diff] [blame] | 5090 | pagerLeave(pPager); |
| 5091 | return rc; |
| 5092 | } |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5093 | pPager->needSync = 1; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5094 | pPgHdr->needSync = 1; |
| 5095 | pPgHdr->inJournal = 1; |
| 5096 | makeDirty(pPgHdr); |
| 5097 | sqlite3PagerUnref(pPgHdr); |
danielk1977 | 94daf7f | 2004-11-08 09:26:09 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 5100 | pagerLeave(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5101 | return SQLITE_OK; |
| 5102 | } |
| 5103 | #endif |
| 5104 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5105 | /* |
| 5106 | ** Return a pointer to the data for the specified page. |
| 5107 | */ |
| 5108 | void *sqlite3PagerGetData(DbPage *pPg){ |
| 5109 | return PGHDR_TO_DATA(pPg); |
| 5110 | } |
| 5111 | |
| 5112 | /* |
| 5113 | ** Return a pointer to the Pager.nExtra bytes of "extra" space |
| 5114 | ** allocated along with the specified page. |
| 5115 | */ |
| 5116 | void *sqlite3PagerGetExtra(DbPage *pPg){ |
| 5117 | Pager *pPager = pPg->pPager; |
| 5118 | return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0); |
| 5119 | } |
| 5120 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 5121 | /* |
| 5122 | ** Get/set the locking-mode for this pager. Parameter eMode must be one |
| 5123 | ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or |
| 5124 | ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then |
| 5125 | ** the locking-mode is set to the value specified. |
| 5126 | ** |
| 5127 | ** The returned value is either PAGER_LOCKINGMODE_NORMAL or |
| 5128 | ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated) |
| 5129 | ** locking-mode. |
| 5130 | */ |
| 5131 | int sqlite3PagerLockingMode(Pager *pPager, int eMode){ |
drh | 369339d | 2007-03-30 16:01:55 +0000 | [diff] [blame] | 5132 | assert( eMode==PAGER_LOCKINGMODE_QUERY |
| 5133 | || eMode==PAGER_LOCKINGMODE_NORMAL |
| 5134 | || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 5135 | assert( PAGER_LOCKINGMODE_QUERY<0 ); |
| 5136 | assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 ); |
| 5137 | if( eMode>=0 && !pPager->tempFile ){ |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 5138 | pPager->exclusiveMode = eMode; |
| 5139 | } |
| 5140 | return (int)pPager->exclusiveMode; |
| 5141 | } |
| 5142 | |
drh | d919fe1 | 2007-12-11 19:34:44 +0000 | [diff] [blame] | 5143 | #ifdef SQLITE_TEST |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5144 | /* |
| 5145 | ** Print a listing of all referenced pages and their ref count. |
| 5146 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5147 | void sqlite3PagerRefdump(Pager *pPager){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5148 | PgHdr *pPg; |
| 5149 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 5150 | if( pPg->nRef<=0 ) continue; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5151 | sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n", |
| 5152 | pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef); |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5153 | } |
| 5154 | } |
| 5155 | #endif |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 5156 | |
| 5157 | #endif /* SQLITE_OMIT_DISKIO */ |