Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: third_party/sqlite/src/src/pager.c

Issue 5626002: Update sqlite to 3.7.3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/third_party/sqlite/src
Patch Set: Remove misc change. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/sqlite/src/src/pager.h ('k') | third_party/sqlite/src/src/parse.y » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 ** 2001 September 15 2 ** 2001 September 15
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
11 ************************************************************************* 11 *************************************************************************
12 ** This is the implementation of the page cache subsystem or "pager". 12 ** This is the implementation of the page cache subsystem or "pager".
13 ** 13 **
14 ** The pager is used to access a database disk file. It implements 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 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 16 ** is separate from the database file. The pager also implements file
17 ** locking to prevent two processes from writing the same database 17 ** locking to prevent two processes from writing the same database
18 ** file simultaneously, or one process from reading the database while 18 ** file simultaneously, or one process from reading the database while
19 ** another is writing. 19 ** another is writing.
20 **
21 ** @(#) $Id: pager.c,v 1.629 2009/08/10 17:48:57 drh Exp $
22 */ 20 */
23 #ifndef SQLITE_OMIT_DISKIO 21 #ifndef SQLITE_OMIT_DISKIO
24 #include "sqliteInt.h" 22 #include "sqliteInt.h"
23 #include "wal.h"
24
25
26 /******************* NOTES ON THE DESIGN OF THE PAGER ************************
27 **
28 ** This comment block describes invariants that hold when using a rollback
29 ** journal. These invariants do not apply for journal_mode=WAL,
30 ** journal_mode=MEMORY, or journal_mode=OFF.
31 **
32 ** Within this comment block, a page is deemed to have been synced
33 ** automatically as soon as it is written when PRAGMA synchronous=OFF.
34 ** Otherwise, the page is not synced until the xSync method of the VFS
35 ** is called successfully on the file containing the page.
36 **
37 ** Definition: A page of the database file is said to be "overwriteable" if
38 ** one or more of the following are true about the page:
39 **
40 ** (a) The original content of the page as it was at the beginning of
41 ** the transaction has been written into the rollback journal and
42 ** synced.
43 **
44 ** (b) The page was a freelist leaf page at the start of the transaction.
45 **
46 ** (c) The page number is greater than the largest page that existed in
47 ** the database file at the start of the transaction.
48 **
49 ** (1) A page of the database file is never overwritten unless one of the
50 ** following are true:
51 **
52 ** (a) The page and all other pages on the same sector are overwriteable.
53 **
54 ** (b) The atomic page write optimization is enabled, and the entire
55 ** transaction other than the update of the transaction sequence
56 ** number consists of a single page change.
57 **
58 ** (2) The content of a page written into the rollback journal exactly matches
59 ** both the content in the database when the rollback journal was written
60 ** and the content in the database at the beginning of the current
61 ** transaction.
62 **
63 ** (3) Writes to the database file are an integer multiple of the page size
64 ** in length and are aligned on a page boundary.
65 **
66 ** (4) Reads from the database file are either aligned on a page boundary and
67 ** an integer multiple of the page size in length or are taken from the
68 ** first 100 bytes of the database file.
69 **
70 ** (5) All writes to the database file are synced prior to the rollback journal
71 ** being deleted, truncated, or zeroed.
72 **
73 ** (6) If a master journal file is used, then all writes to the database file
74 ** are synced prior to the master journal being deleted.
75 **
76 ** Definition: Two databases (or the same database at two points it time)
77 ** are said to be "logically equivalent" if they give the same answer to
78 ** all queries. Note in particular the the content of freelist leaf
79 ** pages can be changed arbitarily without effecting the logical equivalence
80 ** of the database.
81 **
82 ** (7) At any time, if any subset, including the empty set and the total set,
83 ** of the unsynced changes to a rollback journal are removed and the
84 ** journal is rolled back, the resulting database file will be logical
85 ** equivalent to the database file at the beginning of the transaction.
86 **
87 ** (8) When a transaction is rolled back, the xTruncate method of the VFS
88 ** is called to restore the database file to the same size it was at
89 ** the beginning of the transaction. (In some VFSes, the xTruncate
90 ** method is a no-op, but that does not change the fact the SQLite will
91 ** invoke it.)
92 **
93 ** (9) Whenever the database file is modified, at least one bit in the range
94 ** of bytes from 24 through 39 inclusive will be changed prior to releasing
95 ** the EXCLUSIVE lock, thus signaling other connections on the same
96 ** database to flush their caches.
97 **
98 ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
99 ** than one billion transactions.
100 **
101 ** (11) A database file is well-formed at the beginning and at the conclusion
102 ** of every transaction.
103 **
104 ** (12) An EXCLUSIVE lock is held on the database file when writing to
105 ** the database file.
106 **
107 ** (13) A SHARED lock is held on the database file while reading any
108 ** content out of the database file.
109 **
110 ******************************************************************************/
25 111
26 /* 112 /*
27 ** Macros for troubleshooting. Normally turned off 113 ** Macros for troubleshooting. Normally turned off
28 */ 114 */
29 #if 0 115 #if 0
30 int sqlite3PagerTrace=1; /* True to enable tracing */ 116 int sqlite3PagerTrace=1; /* True to enable tracing */
31 #define sqlite3DebugPrintf printf 117 #define sqlite3DebugPrintf printf
32 #define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; } 118 #define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
33 #else 119 #else
34 #define PAGERTRACE(X) 120 #define PAGERTRACE(X)
35 #endif 121 #endif
36 122
37 /* 123 /*
38 ** The following two macros are used within the PAGERTRACE() macros above 124 ** The following two macros are used within the PAGERTRACE() macros above
39 ** to print out file-descriptors. 125 ** to print out file-descriptors.
40 ** 126 **
41 ** PAGERID() takes a pointer to a Pager struct as its argument. The 127 ** PAGERID() takes a pointer to a Pager struct as its argument. The
42 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file 128 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
43 ** struct as its argument. 129 ** struct as its argument.
44 */ 130 */
45 #define PAGERID(p) ((int)(p->fd)) 131 #define PAGERID(p) ((int)(p->fd))
46 #define FILEHANDLEID(fd) ((int)fd) 132 #define FILEHANDLEID(fd) ((int)fd)
47 133
48 /* 134 /*
49 ** The page cache as a whole is always in one of the following 135 ** The Pager.eState variable stores the current 'state' of a pager. A
50 ** states: 136 ** pager may be in any one of the seven states shown in the following
51 ** 137 ** state diagram.
52 ** PAGER_UNLOCK The page cache is not currently reading or 138 **
53 ** writing the database file. There is no 139 ** OPEN <------+------+
54 ** data held in memory. This is the initial 140 ** | | |
55 ** state. 141 ** V | |
56 ** 142 ** +---------> READER-------+ |
57 ** PAGER_SHARED The page cache is reading the database. 143 ** | | |
58 ** Writing is not permitted. There can be 144 ** | V |
59 ** multiple readers accessing the same database 145 ** |<-------WRITER_LOCKED------> ERROR
60 ** file at the same time. 146 ** | | ^
61 ** 147 ** | V |
62 ** PAGER_RESERVED This process has reserved the database for writing 148 ** |<------WRITER_CACHEMOD-------->|
63 ** but has not yet made any changes. Only one process 149 ** | | |
64 ** at a time can reserve the database. The original 150 ** | V |
65 ** database file has not been modified so other 151 ** |<-------WRITER_DBMOD---------->|
66 ** processes may still be reading the on-disk 152 ** | | |
67 ** database file. 153 ** | V |
68 ** 154 ** +<------WRITER_FINISHED-------->+
69 ** PAGER_EXCLUSIVE The page cache is writing the database. 155 **
70 ** Access is exclusive. No other processes or 156 **
71 ** threads can be reading or writing while one 157 ** List of state transitions and the C [function] that performs each:
72 ** process is writing. 158 **
73 ** 159 ** OPEN -> READER [sqlite3PagerSharedLock]
74 ** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE 160 ** READER -> OPEN [pager_unlock]
75 ** after all dirty pages have been written to the 161 **
76 ** database file and the file has been synced to 162 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
77 ** disk. All that remains to do is to remove or 163 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
78 ** truncate the journal file and the transaction 164 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
79 ** will be committed. 165 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
80 ** 166 ** WRITER_*** -> READER [pager_end_transaction]
81 ** The page cache comes up in PAGER_UNLOCK. The first time a 167 **
82 ** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED. 168 ** WRITER_*** -> ERROR [pager_error]
83 ** After all pages have been released using sqlite_page_unref(), 169 ** ERROR -> OPEN [pager_unlock]
84 ** the state transitions back to PAGER_UNLOCK. The first time 170 **
85 ** that sqlite3PagerWrite() is called, the state transitions to 171 **
86 ** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be 172 ** OPEN:
87 ** called on an outstanding page which means that the pager must 173 **
88 ** be in PAGER_SHARED before it transitions to PAGER_RESERVED.) 174 ** The pager starts up in this state. Nothing is guaranteed in this
89 ** PAGER_RESERVED means that there is an open rollback journal. 175 ** state - the file may or may not be locked and the database size is
90 ** The transition to PAGER_EXCLUSIVE occurs before any changes 176 ** unknown. The database may not be read or written.
91 ** are made to the database file, though writes to the rollback 177 **
92 ** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback() 178 ** * No read or write transaction is active.
93 ** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED, 179 ** * Any lock, or no lock at all, may be held on the database file.
94 ** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode. 180 ** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
181 **
182 ** READER:
183 **
184 ** In this state all the requirements for reading the database in
185 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
186 ** was) in exclusive-locking mode, a user-level read transaction is
187 ** open. The database size is known in this state.
188 **
189 ** A connection running with locking_mode=normal enters this state when
190 ** it opens a read-transaction on the database and returns to state
191 ** OPEN after the read-transaction is completed. However a connection
192 ** running in locking_mode=exclusive (including temp databases) remains in
193 ** this state even after the read-transaction is closed. The only way
194 ** a locking_mode=exclusive connection can transition from READER to OPEN
195 ** is via the ERROR state (see below).
196 **
197 ** * A read transaction may be active (but a write-transaction cannot).
198 ** * A SHARED or greater lock is held on the database file.
199 ** * The dbSize variable may be trusted (even if a user-level read
200 ** transaction is not active). The dbOrigSize and dbFileSize variables
201 ** may not be trusted at this point.
202 ** * If the database is a WAL database, then the WAL connection is open.
203 ** * Even if a read-transaction is not open, it is guaranteed that
204 ** there is no hot-journal in the file-system.
205 **
206 ** WRITER_LOCKED:
207 **
208 ** The pager moves to this state from READER when a write-transaction
209 ** is first opened on the database. In WRITER_LOCKED state, all locks
210 ** required to start a write-transaction are held, but no actual
211 ** modifications to the cache or database have taken place.
212 **
213 ** In rollback mode, a RESERVED or (if the transaction was opened with
214 ** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
215 ** moving to this state, but the journal file is not written to or opened
216 ** to in this state. If the transaction is committed or rolled back while
217 ** in WRITER_LOCKED state, all that is required is to unlock the database
218 ** file.
219 **
220 ** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
221 ** If the connection is running with locking_mode=exclusive, an attempt
222 ** is made to obtain an EXCLUSIVE lock on the database file.
223 **
224 ** * A write transaction is active.
225 ** * If the connection is open in rollback-mode, a RESERVED or greater
226 ** lock is held on the database file.
227 ** * If the connection is open in WAL-mode, a WAL write transaction
228 ** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
229 ** called).
230 ** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
231 ** * The contents of the pager cache have not been modified.
232 ** * The journal file may or may not be open.
233 ** * Nothing (not even the first header) has been written to the journal.
234 **
235 ** WRITER_CACHEMOD:
236 **
237 ** A pager moves from WRITER_LOCKED state to this state when a page is
238 ** first modified by the upper layer. In rollback mode the journal file
239 ** is opened (if it is not already open) and a header written to the
240 ** start of it. The database file on disk has not been modified.
241 **
242 ** * A write transaction is active.
243 ** * A RESERVED or greater lock is held on the database file.
244 ** * The journal file is open and the first header has been written
245 ** to it, but the header has not been synced to disk.
246 ** * The contents of the page cache have been modified.
247 **
248 ** WRITER_DBMOD:
249 **
250 ** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
251 ** when it modifies the contents of the database file. WAL connections
252 ** never enter this state (since they do not modify the database file,
253 ** just the log file).
254 **
255 ** * A write transaction is active.
256 ** * An EXCLUSIVE or greater lock is held on the database file.
257 ** * The journal file is open and the first header has been written
258 ** and synced to disk.
259 ** * The contents of the page cache have been modified (and possibly
260 ** written to disk).
261 **
262 ** WRITER_FINISHED:
263 **
264 ** It is not possible for a WAL connection to enter this state.
265 **
266 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
267 ** state after the entire transaction has been successfully written into the
268 ** database file. In this state the transaction may be committed simply
269 ** by finalizing the journal file. Once in WRITER_FINISHED state, it is
270 ** not possible to modify the database further. At this point, the upper
271 ** layer must either commit or rollback the transaction.
272 **
273 ** * A write transaction is active.
274 ** * An EXCLUSIVE or greater lock is held on the database file.
275 ** * All writing and syncing of journal and database data has finished.
276 ** If no error occured, all that remains is to finalize the journal to
277 ** commit the transaction. If an error did occur, the caller will need
278 ** to rollback the transaction.
279 **
280 ** ERROR:
281 **
282 ** The ERROR state is entered when an IO or disk-full error (including
283 ** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
284 ** difficult to be sure that the in-memory pager state (cache contents,
285 ** db size etc.) are consistent with the contents of the file-system.
286 **
287 ** Temporary pager files may enter the ERROR state, but in-memory pagers
288 ** cannot.
289 **
290 ** For example, if an IO error occurs while performing a rollback,
291 ** the contents of the page-cache may be left in an inconsistent state.
292 ** At this point it would be dangerous to change back to READER state
293 ** (as usually happens after a rollback). Any subsequent readers might
294 ** report database corruption (due to the inconsistent cache), and if
295 ** they upgrade to writers, they may inadvertently corrupt the database
296 ** file. To avoid this hazard, the pager switches into the ERROR state
297 ** instead of READER following such an error.
298 **
299 ** Once it has entered the ERROR state, any attempt to use the pager
300 ** to read or write data returns an error. Eventually, once all
301 ** outstanding transactions have been abandoned, the pager is able to
302 ** transition back to OPEN state, discarding the contents of the
303 ** page-cache and any other in-memory state at the same time. Everything
304 ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
305 ** when a read-transaction is next opened on the pager (transitioning
306 ** the pager into READER state). At that point the system has recovered
307 ** from the error.
308 **
309 ** Specifically, the pager jumps into the ERROR state if:
310 **
311 ** 1. An error occurs while attempting a rollback. This happens in
312 ** function sqlite3PagerRollback().
313 **
314 ** 2. An error occurs while attempting to finalize a journal file
315 ** following a commit in function sqlite3PagerCommitPhaseTwo().
316 **
317 ** 3. An error occurs while attempting to write to the journal or
318 ** database file in function pagerStress() in order to free up
319 ** memory.
320 **
321 ** In other cases, the error is returned to the b-tree layer. The b-tree
322 ** layer then attempts a rollback operation. If the error condition
323 ** persists, the pager enters the ERROR state via condition (1) above.
324 **
325 ** Condition (3) is necessary because it can be triggered by a read-only
326 ** statement executed within a transaction. In this case, if the error
327 ** code were simply returned to the user, the b-tree layer would not
328 ** automatically attempt a rollback, as it assumes that an error in a
329 ** read-only statement cannot leave the pager in an internally inconsistent
330 ** state.
331 **
332 ** * The Pager.errCode variable is set to something other than SQLITE_OK.
333 ** * There are one or more outstanding references to pages (after the
334 ** last reference is dropped the pager should move back to OPEN state).
335 ** * The pager is not an in-memory pager.
336 **
337 **
338 ** Notes:
339 **
340 ** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
341 ** connection is open in WAL mode. A WAL connection is always in one
342 ** of the first four states.
343 **
344 ** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
345 ** state. There are two exceptions: immediately after exclusive-mode has
346 ** been turned on (and before any read or write transactions are
347 ** executed), and when the pager is leaving the "error state".
348 **
349 ** * See also: assert_pager_state().
95 */ 350 */
96 #define PAGER_UNLOCK 0 351 #define PAGER_OPEN 0
97 #define PAGER_SHARED 1 /* same as SHARED_LOCK */ 352 #define PAGER_READER 1
98 #define PAGER_RESERVED 2 /* same as RESERVED_LOCK */ 353 #define PAGER_WRITER_LOCKED 2
99 #define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */ 354 #define PAGER_WRITER_CACHEMOD 3
100 #define PAGER_SYNCED 5 355 #define PAGER_WRITER_DBMOD 4
356 #define PAGER_WRITER_FINISHED 5
357 #define PAGER_ERROR 6
358
359 /*
360 ** The Pager.eLock variable is almost always set to one of the
361 ** following locking-states, according to the lock currently held on
362 ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
363 ** This variable is kept up to date as locks are taken and released by
364 ** the pagerLockDb() and pagerUnlockDb() wrappers.
365 **
366 ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
367 ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
368 ** the operation was successful. In these circumstances pagerLockDb() and
369 ** pagerUnlockDb() take a conservative approach - eLock is always updated
370 ** when unlocking the file, and only updated when locking the file if the
371 ** VFS call is successful. This way, the Pager.eLock variable may be set
372 ** to a less exclusive (lower) value than the lock that is actually held
373 ** at the system level, but it is never set to a more exclusive value.
374 **
375 ** This is usually safe. If an xUnlock fails or appears to fail, there may
376 ** be a few redundant xLock() calls or a lock may be held for longer than
377 ** required, but nothing really goes wrong.
378 **
379 ** The exception is when the database file is unlocked as the pager moves
380 ** from ERROR to OPEN state. At this point there may be a hot-journal file
381 ** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
382 ** transition, by the same pager or any other). If the call to xUnlock()
383 ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
384 ** can confuse the call to xCheckReservedLock() call made later as part
385 ** of hot-journal detection.
386 **
387 ** xCheckReservedLock() is defined as returning true "if there is a RESERVED
388 ** lock held by this process or any others". So xCheckReservedLock may
389 ** return true because the caller itself is holding an EXCLUSIVE lock (but
390 ** doesn't know it because of a previous error in xUnlock). If this happens
391 ** a hot-journal may be mistaken for a journal being created by an active
392 ** transaction in another process, causing SQLite to read from the database
393 ** without rolling it back.
394 **
395 ** To work around this, if a call to xUnlock() fails when unlocking the
396 ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
397 ** is only changed back to a real locking state after a successful call
398 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
399 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
400 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
401 ** lock on the database file before attempting to roll it back. See function
402 ** PagerSharedLock() for more detail.
403 **
404 ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
405 ** PAGER_OPEN state.
406 */
407 #define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1)
101 408
102 /* 409 /*
103 ** A macro used for invoking the codec if there is one 410 ** A macro used for invoking the codec if there is one
104 */ 411 */
105 #ifdef SQLITE_HAS_CODEC 412 #ifdef SQLITE_HAS_CODEC
106 # define CODEC1(P,D,N,X,E) \ 413 # define CODEC1(P,D,N,X,E) \
107 if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; } 414 if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
108 # define CODEC2(P,D,N,X,E,O) \ 415 # define CODEC2(P,D,N,X,E,O) \
109 if( P->xCodec==0 ){ O=(char*)D; }else \ 416 if( P->xCodec==0 ){ O=(char*)D; }else \
110 if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; } 417 if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
(...skipping 23 matching lines...) Expand all
134 ** journal before the journal-header. This is required during savepoint 441 ** journal before the journal-header. This is required during savepoint
135 ** rollback (see pagerPlaybackSavepoint()). 442 ** rollback (see pagerPlaybackSavepoint()).
136 */ 443 */
137 typedef struct PagerSavepoint PagerSavepoint; 444 typedef struct PagerSavepoint PagerSavepoint;
138 struct PagerSavepoint { 445 struct PagerSavepoint {
139 i64 iOffset; /* Starting offset in main journal */ 446 i64 iOffset; /* Starting offset in main journal */
140 i64 iHdrOffset; /* See above */ 447 i64 iHdrOffset; /* See above */
141 Bitvec *pInSavepoint; /* Set of pages in this savepoint */ 448 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
142 Pgno nOrig; /* Original number of pages in file */ 449 Pgno nOrig; /* Original number of pages in file */
143 Pgno iSubRec; /* Index of first record in sub-journal */ 450 Pgno iSubRec; /* Index of first record in sub-journal */
451 #ifndef SQLITE_OMIT_WAL
452 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
453 #endif
144 }; 454 };
145 455
146 /* 456 /*
147 ** A open page cache is an instance of the following structure. 457 ** A open page cache is an instance of struct Pager. A description of
458 ** some of the more important member variables follows:
148 ** 459 **
149 ** errCode 460 ** eState
150 ** 461 **
151 ** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or 462 ** The current 'state' of the pager object. See the comment and state
152 ** or SQLITE_FULL. Once one of the first three errors occurs, it persists 463 ** diagram above for a description of the pager state.
153 ** and is returned as the result of every major pager API call. The
154 ** SQLITE_FULL return code is slightly different. It persists only until the
155 ** next successful rollback is performed on the pager cache. Also,
156 ** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
157 ** APIs, they may still be used successfully.
158 ** 464 **
159 ** dbSizeValid, dbSize, dbOrigSize, dbFileSize 465 ** eLock
160 ** 466 **
161 ** Managing the size of the database file in pages is a little complicated. 467 ** For a real on-disk database, the current lock held on the database file -
162 ** The variable Pager.dbSize contains the number of pages that the database 468 ** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
163 ** image currently contains. As the database image grows or shrinks this
164 ** variable is updated. The variable Pager.dbFileSize contains the number
165 ** of pages in the database file. This may be different from Pager.dbSize
166 ** if some pages have been appended to the database image but not yet written
167 ** out from the cache to the actual file on disk. Or if the image has been
168 ** truncated by an incremental-vacuum operation. The Pager.dbOrigSize variable
169 ** contains the number of pages in the database image when the current
170 ** transaction was opened. The contents of all three of these variables is
171 ** only guaranteed to be correct if the boolean Pager.dbSizeValid is true.
172 ** 469 **
173 ** TODO: Under what conditions is dbSizeValid set? Cleared? 470 ** For a temporary or in-memory database (neither of which require any
471 ** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
472 ** databases always have Pager.exclusiveMode==1, this tricks the pager
473 ** logic into thinking that it already has all the locks it will ever
474 ** need (and no reason to release them).
475 **
476 ** In some (obscure) circumstances, this variable may also be set to
477 ** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
478 ** details.
174 ** 479 **
175 ** changeCountDone 480 ** changeCountDone
176 ** 481 **
177 ** This boolean variable is used to make sure that the change-counter 482 ** This boolean variable is used to make sure that the change-counter
178 ** (the 4-byte header field at byte offset 24 of the database file) is 483 ** (the 4-byte header field at byte offset 24 of the database file) is
179 ** not updated more often than necessary. 484 ** not updated more often than necessary.
180 ** 485 **
181 ** It is set to true when the change-counter field is updated, which 486 ** It is set to true when the change-counter field is updated, which
182 ** can only happen if an exclusive lock is held on the database file. 487 ** can only happen if an exclusive lock is held on the database file.
183 ** It is cleared (set to false) whenever an exclusive lock is 488 ** It is cleared (set to false) whenever an exclusive lock is
184 ** relinquished on the database file. Each time a transaction is committed, 489 ** relinquished on the database file. Each time a transaction is committed,
185 ** The changeCountDone flag is inspected. If it is true, the work of 490 ** The changeCountDone flag is inspected. If it is true, the work of
186 ** updating the change-counter is omitted for the current transaction. 491 ** updating the change-counter is omitted for the current transaction.
187 ** 492 **
188 ** This mechanism means that when running in exclusive mode, a connection 493 ** This mechanism means that when running in exclusive mode, a connection
189 ** need only update the change-counter once, for the first transaction 494 ** need only update the change-counter once, for the first transaction
190 ** committed. 495 ** committed.
191 ** 496 **
192 ** dbModified
193 **
194 ** The dbModified flag is set whenever a database page is dirtied.
195 ** It is cleared at the end of each transaction.
196 **
197 ** It is used when committing or otherwise ending a transaction. If
198 ** the dbModified flag is clear then less work has to be done.
199 **
200 ** journalStarted
201 **
202 ** This flag is set whenever the the main journal is synced.
203 **
204 ** The point of this flag is that it must be set after the
205 ** first journal header in a journal file has been synced to disk.
206 ** After this has happened, new pages appended to the database
207 ** do not need the PGHDR_NEED_SYNC flag set, as they do not need
208 ** to wait for a journal sync before they can be written out to
209 ** the database file (see function pager_write()).
210 **
211 ** setMaster 497 ** setMaster
212 ** 498 **
213 ** This variable is used to ensure that the master journal file name 499 ** When PagerCommitPhaseOne() is called to commit a transaction, it may
214 ** (if any) is only written into the journal file once. 500 ** (or may not) specify a master-journal name to be written into the
501 ** journal file before it is synced to disk.
215 ** 502 **
216 ** When committing a transaction, the master journal file name (if any) 503 ** Whether or not a journal file contains a master-journal pointer affects
217 ** may be written into the journal file while the pager is still in 504 ** the way in which the journal file is finalized after the transaction is
218 ** PAGER_RESERVED state (see CommitPhaseOne() for the action). It 505 ** committed or rolled back when running in "journal_mode=PERSIST" mode.
219 ** then attempts to upgrade to an exclusive lock. If this attempt 506 ** If a journal file does not contain a master-journal pointer, it is
220 ** fails, then SQLITE_BUSY may be returned to the user and the user 507 ** finalized by overwriting the first journal header with zeroes. If
221 ** may attempt to commit the transaction again later (calling 508 ** it does contain a master-journal pointer the journal file is finalized
222 ** CommitPhaseOne() again). This flag is used to ensure that the 509 ** by truncating it to zero bytes, just as if the connection were
223 ** master journal name is only written to the journal file the first 510 ** running in "journal_mode=truncate" mode.
224 ** time CommitPhaseOne() is called.
225 ** 511 **
226 ** doNotSync 512 ** Journal files that contain master journal pointers cannot be finalized
513 ** simply by overwriting the first journal-header with zeroes, as the
514 ** master journal pointer could interfere with hot-journal rollback of any
515 ** subsequently interrupted transaction that reuses the journal file.
227 ** 516 **
228 ** This variable is set and cleared by sqlite3PagerWrite(). 517 ** The flag is cleared as soon as the journal file is finalized (either
518 ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
519 ** journal file from being successfully finalized, the setMaster flag
520 ** is cleared anyway (and the pager will move to ERROR state).
229 ** 521 **
230 ** needSync 522 ** doNotSpill, doNotSyncSpill
231 ** 523 **
232 ** TODO: It might be easier to set this variable in writeJournalHdr() 524 ** These two boolean variables control the behaviour of cache-spills
233 ** and writeMasterJournal() only. Change its meaning to "unsynced data 525 ** (calls made by the pcache module to the pagerStress() routine to
234 ** has been written to the journal". 526 ** write cached data to the file-system in order to free up memory).
527 **
528 ** When doNotSpill is non-zero, writing to the database from pagerStress()
529 ** is disabled altogether. This is done in a very obscure case that
530 ** comes up during savepoint rollback that requires the pcache module
531 ** to allocate a new page to prevent the journal file from being written
532 ** while it is being traversed by code in pager_playback().
533 **
534 ** If doNotSyncSpill is non-zero, writing to the database from pagerStress()
535 ** is permitted, but syncing the journal file is not. This flag is set
536 ** by sqlite3PagerWrite() when the file-system sector-size is larger than
537 ** the database page-size in order to prevent a journal sync from happening
538 ** in between the journalling of two pages on the same sector.
235 ** 539 **
236 ** subjInMemory 540 ** subjInMemory
237 ** 541 **
238 ** This is a boolean variable. If true, then any required sub-journal 542 ** This is a boolean variable. If true, then any required sub-journal
239 ** is opened as an in-memory journal file. If false, then in-memory 543 ** is opened as an in-memory journal file. If false, then in-memory
240 ** sub-journals are only used for in-memory pager files. 544 ** sub-journals are only used for in-memory pager files.
545 **
546 ** This variable is updated by the upper layer each time a new
547 ** write-transaction is opened.
548 **
549 ** dbSize, dbOrigSize, dbFileSize
550 **
551 ** Variable dbSize is set to the number of pages in the database file.
552 ** It is valid in PAGER_READER and higher states (all states except for
553 ** OPEN and ERROR).
554 **
555 ** dbSize is set based on the size of the database file, which may be
556 ** larger than the size of the database (the value stored at offset
557 ** 28 of the database header by the btree). If the size of the file
558 ** is not an integer multiple of the page-size, the value stored in
559 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
560 ** Except, any file that is greater than 0 bytes in size is considered
561 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
562 ** to dbSize==1).
563 **
564 ** During a write-transaction, if pages with page-numbers greater than
565 ** dbSize are modified in the cache, dbSize is updated accordingly.
566 ** Similarly, if the database is truncated using PagerTruncateImage(),
567 ** dbSize is updated.
568 **
569 ** Variables dbOrigSize and dbFileSize are valid in states
570 ** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
571 ** variable at the start of the transaction. It is used during rollback,
572 ** and to determine whether or not pages need to be journalled before
573 ** being modified.
574 **
575 ** Throughout a write-transaction, dbFileSize contains the size of
576 ** the file on disk in pages. It is set to a copy of dbSize when the
577 ** write-transaction is first opened, and updated when VFS calls are made
578 ** to write or truncate the database file on disk.
579 **
580 ** The only reason the dbFileSize variable is required is to suppress
581 ** unnecessary calls to xTruncate() after committing a transaction. If,
582 ** when a transaction is committed, the dbFileSize variable indicates
583 ** that the database file is larger than the database image (Pager.dbSize),
584 ** pager_truncate() is called. The pager_truncate() call uses xFilesize()
585 ** to measure the database file on disk, and then truncates it if required.
586 ** dbFileSize is not used when rolling back a transaction. In this case
587 ** pager_truncate() is called unconditionally (which means there may be
588 ** a call to xFilesize() that is not strictly required). In either case,
589 ** pager_truncate() may cause the file to become smaller or larger.
590 **
591 ** dbHintSize
592 **
593 ** The dbHintSize variable is used to limit the number of calls made to
594 ** the VFS xFileControl(FCNTL_SIZE_HINT) method.
595 **
596 ** dbHintSize is set to a copy of the dbSize variable when a
597 ** write-transaction is opened (at the same time as dbFileSize and
598 ** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
599 ** dbHintSize is increased to the number of pages that correspond to the
600 ** size-hint passed to the method call. See pager_write_pagelist() for
601 ** details.
602 **
603 ** errCode
604 **
605 ** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
606 ** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
607 ** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
608 ** sub-codes.
241 */ 609 */
242 struct Pager { 610 struct Pager {
243 sqlite3_vfs *pVfs; /* OS functions to use for IO */ 611 sqlite3_vfs *pVfs; /* OS functions to use for IO */
244 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ 612 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
245 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */ 613 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
246 u8 useJournal; /* Use a rollback journal on this file */ 614 u8 useJournal; /* Use a rollback journal on this file */
247 u8 noReadlock; /* Do not bother to obtain readlocks */ 615 u8 noReadlock; /* Do not bother to obtain readlocks */
248 u8 noSync; /* Do not sync the journal if true */ 616 u8 noSync; /* Do not sync the journal if true */
249 u8 fullSync; /* Do extra syncs of the journal for robustness */ 617 u8 fullSync; /* Do extra syncs of the journal for robustness */
250 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */ 618 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
251 u8 tempFile; /* zFilename is a temporary file */ 619 u8 tempFile; /* zFilename is a temporary file */
252 u8 readOnly; /* True for a read-only database */ 620 u8 readOnly; /* True for a read-only database */
253 u8 memDb; /* True to inhibit all file I/O */ 621 u8 memDb; /* True to inhibit all file I/O */
254 622
255 /* The following block contains those class members that are dynamically 623 /**************************************************************************
256 ** modified during normal operations. The other variables in this structure 624 ** The following block contains those class members that change during
257 ** are either constant throughout the lifetime of the pager, or else 625 ** routine opertion. Class members not in this block are either fixed
258 ** used to store configuration parameters that affect the way the pager 626 ** when the pager is first created or else only change when there is a
259 ** operates. 627 ** significant mode change (such as changing the page_size, locking_mode,
260 ** 628 ** or the journal_mode). From another view, these class members describe
261 ** The 'state' variable is described in more detail along with the 629 ** the "state" of the pager, while other class members describe the
262 ** descriptions of the values it may take - PAGER_UNLOCK etc. Many of the 630 ** "configuration" of the pager.
263 ** other variables in this block are described in the comment directly
264 ** above this class definition.
265 */ 631 */
266 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */ 632 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
267 u8 dbModified; /* True if there are any changes to the Db */ 633 u8 eLock; /* Current lock held on database file */
268 u8 needSync; /* True if an fsync() is needed on the journal */
269 u8 journalStarted; /* True if header of journal is synced */
270 u8 changeCountDone; /* Set after incrementing the change-counter */ 634 u8 changeCountDone; /* Set after incrementing the change-counter */
271 u8 setMaster; /* True if a m-j name has been written to jrnl */ 635 u8 setMaster; /* True if a m-j name has been written to jrnl */
272 u8 doNotSync; /* Boolean. While true, do not spill the cache */ 636 u8 doNotSpill; /* Do not spill the cache when non-zero */
273 u8 dbSizeValid; /* Set when dbSize is correct */ 637 u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */
274 u8 subjInMemory; /* True to use in-memory sub-journals */ 638 u8 subjInMemory; /* True to use in-memory sub-journals */
275 Pgno dbSize; /* Number of pages in the database */ 639 Pgno dbSize; /* Number of pages in the database */
276 Pgno dbOrigSize; /* dbSize before the current transaction */ 640 Pgno dbOrigSize; /* dbSize before the current transaction */
277 Pgno dbFileSize; /* Number of pages in the database file */ 641 Pgno dbFileSize; /* Number of pages in the database file */
642 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
278 int errCode; /* One of several kinds of errors */ 643 int errCode; /* One of several kinds of errors */
279 int nRec; /* Pages journalled since last j-header written */ 644 int nRec; /* Pages journalled since last j-header written */
280 u32 cksumInit; /* Quasi-random value added to every checksum */ 645 u32 cksumInit; /* Quasi-random value added to every checksum */
281 u32 nSubRec; /* Number of records written to sub-journal */ 646 u32 nSubRec; /* Number of records written to sub-journal */
282 Bitvec *pInJournal; /* One bit for each page in the database file */ 647 Bitvec *pInJournal; /* One bit for each page in the database file */
283 sqlite3_file *fd; /* File descriptor for database */ 648 sqlite3_file *fd; /* File descriptor for database */
284 sqlite3_file *jfd; /* File descriptor for main journal */ 649 sqlite3_file *jfd; /* File descriptor for main journal */
285 sqlite3_file *sjfd; /* File descriptor for sub-journal */ 650 sqlite3_file *sjfd; /* File descriptor for sub-journal */
286 i64 journalOff; /* Current write offset in the journal file */ 651 i64 journalOff; /* Current write offset in the journal file */
287 i64 journalHdr; /* Byte offset to previous journal header */ 652 i64 journalHdr; /* Byte offset to previous journal header */
653 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
288 PagerSavepoint *aSavepoint; /* Array of active savepoints */ 654 PagerSavepoint *aSavepoint; /* Array of active savepoints */
289 int nSavepoint; /* Number of elements in aSavepoint[] */ 655 int nSavepoint; /* Number of elements in aSavepoint[] */
290 char dbFileVers[16]; /* Changes whenever database file changes */ 656 char dbFileVers[16]; /* Changes whenever database file changes */
291 u32 sectorSize; /* Assumed sector size during rollback */ 657 /*
658 ** End of the routinely-changing class members
659 ***************************************************************************/
292 660
293 u16 nExtra; /* Add this many bytes to each in-memory page */ 661 u16 nExtra; /* Add this many bytes to each in-memory page */
294 i16 nReserve; /* Number of unused bytes at end of each page */ 662 i16 nReserve; /* Number of unused bytes at end of each page */
295 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ 663 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
664 u32 sectorSize; /* Assumed sector size during rollback */
296 int pageSize; /* Number of bytes in a page */ 665 int pageSize; /* Number of bytes in a page */
297 Pgno mxPgno; /* Maximum allowed size of the database */ 666 Pgno mxPgno; /* Maximum allowed size of the database */
667 i64 journalSizeLimit; /* Size limit for persistent journal files */
298 char *zFilename; /* Name of the database file */ 668 char *zFilename; /* Name of the database file */
299 char *zJournal; /* Name of the journal file */ 669 char *zJournal; /* Name of the journal file */
300 int (*xBusyHandler)(void*); /* Function to call when busy */ 670 int (*xBusyHandler)(void*); /* Function to call when busy */
301 void *pBusyHandlerArg; /* Context argument for xBusyHandler */ 671 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
302 #ifdef SQLITE_TEST 672 #ifdef SQLITE_TEST
303 int nHit, nMiss; /* Cache hits and missing */ 673 int nHit, nMiss; /* Cache hits and missing */
304 int nRead, nWrite; /* Database pages read/written */ 674 int nRead, nWrite; /* Database pages read/written */
305 #endif 675 #endif
306 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ 676 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
307 #ifdef SQLITE_HAS_CODEC 677 #ifdef SQLITE_HAS_CODEC
308 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ 678 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
309 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */ 679 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
310 void (*xCodecFree)(void*); /* Destructor for the codec */ 680 void (*xCodecFree)(void*); /* Destructor for the codec */
311 void *pCodec; /* First argument to xCodec... methods */ 681 void *pCodec; /* First argument to xCodec... methods */
312 #endif 682 #endif
313 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ 683 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
314 i64 journalSizeLimit; /* Size limit for persistent journal files */
315 PCache *pPCache; /* Pointer to page cache object */ 684 PCache *pPCache; /* Pointer to page cache object */
316 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */ 685 #ifndef SQLITE_OMIT_WAL
686 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
687 char *zWal; /* File name for write-ahead log */
688 #endif
317 }; 689 };
318 690
319 /* 691 /*
320 ** The following global variables hold counters used for 692 ** The following global variables hold counters used for
321 ** testing purposes only. These variables do not exist in 693 ** testing purposes only. These variables do not exist in
322 ** a non-testing build. These variables are not thread-safe. 694 ** a non-testing build. These variables are not thread-safe.
323 */ 695 */
324 #ifdef SQLITE_TEST 696 #ifdef SQLITE_TEST
325 int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */ 697 int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
326 int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */ 698 int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 # define MEMDB 0 753 # define MEMDB 0
382 #else 754 #else
383 # define MEMDB pPager->memDb 755 # define MEMDB pPager->memDb
384 #endif 756 #endif
385 757
386 /* 758 /*
387 ** The maximum legal page number is (2^31 - 1). 759 ** The maximum legal page number is (2^31 - 1).
388 */ 760 */
389 #define PAGER_MAX_PGNO 2147483647 761 #define PAGER_MAX_PGNO 2147483647
390 762
763 /*
764 ** The argument to this macro is a file descriptor (type sqlite3_file*).
765 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
766 **
767 ** This is so that expressions can be written as:
768 **
769 ** if( isOpen(pPager->jfd) ){ ...
770 **
771 ** instead of
772 **
773 ** if( pPager->jfd->pMethods ){ ...
774 */
775 #define isOpen(pFd) ((pFd)->pMethods)
776
777 /*
778 ** Return true if this pager uses a write-ahead log instead of the usual
779 ** rollback journal. Otherwise false.
780 */
781 #ifndef SQLITE_OMIT_WAL
782 static int pagerUseWal(Pager *pPager){
783 return (pPager->pWal!=0);
784 }
785 #else
786 # define pagerUseWal(x) 0
787 # define pagerRollbackWal(x) 0
788 # define pagerWalFrames(v,w,x,y,z) 0
789 # define pagerOpenWalIfPresent(z) SQLITE_OK
790 # define pagerBeginReadTransaction(z) SQLITE_OK
791 #endif
792
391 /* Begin preload-cache.patch for Chromium */ 793 /* Begin preload-cache.patch for Chromium */
392 /* See comments above the definition. */ 794 /* See comments above the definition. */
393 int sqlite3PagerAcquire2( 795 int sqlite3PagerAcquire2(
394 Pager *pPager, 796 Pager *pPager,
395 Pgno pgno, 797 Pgno pgno,
396 DbPage **ppPage, 798 DbPage **ppPage,
397 int noContent, 799 int noContent,
398 unsigned char *pDataToFill); 800 unsigned char *pDataToFill);
399 /* End preload-cache.patch for Chromium */ 801 /* End preload-cache.patch for Chromium */
400 802
401 #ifndef NDEBUG 803 #ifndef NDEBUG
402 /* 804 /*
403 ** Usage: 805 ** Usage:
404 ** 806 **
405 ** assert( assert_pager_state(pPager) ); 807 ** assert( assert_pager_state(pPager) );
808 **
809 ** This function runs many asserts to try to find inconsistencies in
810 ** the internal state of the Pager object.
406 */ 811 */
407 static int assert_pager_state(Pager *pPager){ 812 static int assert_pager_state(Pager *p){
813 Pager *pPager = p;
408 814
409 /* A temp-file is always in PAGER_EXCLUSIVE or PAGER_SYNCED state. */ 815 /* State must be valid. */
410 assert( pPager->tempFile==0 || pPager->state>=PAGER_EXCLUSIVE ); 816 assert( p->eState==PAGER_OPEN
817 || p->eState==PAGER_READER
818 || p->eState==PAGER_WRITER_LOCKED
819 || p->eState==PAGER_WRITER_CACHEMOD
820 || p->eState==PAGER_WRITER_DBMOD
821 || p->eState==PAGER_WRITER_FINISHED
822 || p->eState==PAGER_ERROR
823 );
411 824
412 /* The changeCountDone flag is always set for temp-files */ 825 /* Regardless of the current state, a temp-file connection always behaves
413 assert( pPager->tempFile==0 || pPager->changeCountDone ); 826 ** as if it has an exclusive lock on the database file. It never updates
827 ** the change-counter field, so the changeCountDone flag is always set.
828 */
829 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
830 assert( p->tempFile==0 || pPager->changeCountDone );
831
832 /* If the useJournal flag is clear, the journal-mode must be "OFF".
833 ** And if the journal-mode is "OFF", the journal file must not be open.
834 */
835 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
836 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
837
838 /* Check that MEMDB implies noSync. And an in-memory journal. Since
839 ** this means an in-memory pager performs no IO at all, it cannot encounter
840 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
841 ** a journal file. (although the in-memory journal implementation may
842 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
843 ** is therefore not possible for an in-memory pager to enter the ERROR
844 ** state.
845 */
846 if( MEMDB ){
847 assert( p->noSync );
848 assert( p->journalMode==PAGER_JOURNALMODE_OFF
849 || p->journalMode==PAGER_JOURNALMODE_MEMORY
850 );
851 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
852 assert( pagerUseWal(p)==0 );
853 }
854
855 /* If changeCountDone is set, a RESERVED lock or greater must be held
856 ** on the file.
857 */
858 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
859 assert( p->eLock!=PENDING_LOCK );
860
861 switch( p->eState ){
862 case PAGER_OPEN:
863 assert( !MEMDB );
864 assert( pPager->errCode==SQLITE_OK );
865 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
866 break;
867
868 case PAGER_READER:
869 assert( pPager->errCode==SQLITE_OK );
870 assert( p->eLock!=UNKNOWN_LOCK );
871 assert( p->eLock>=SHARED_LOCK || p->noReadlock );
872 break;
873
874 case PAGER_WRITER_LOCKED:
875 assert( p->eLock!=UNKNOWN_LOCK );
876 assert( pPager->errCode==SQLITE_OK );
877 if( !pagerUseWal(pPager) ){
878 assert( p->eLock>=RESERVED_LOCK );
879 }
880 assert( pPager->dbSize==pPager->dbOrigSize );
881 assert( pPager->dbOrigSize==pPager->dbFileSize );
882 assert( pPager->dbOrigSize==pPager->dbHintSize );
883 assert( pPager->setMaster==0 );
884 break;
885
886 case PAGER_WRITER_CACHEMOD:
887 assert( p->eLock!=UNKNOWN_LOCK );
888 assert( pPager->errCode==SQLITE_OK );
889 if( !pagerUseWal(pPager) ){
890 /* It is possible that if journal_mode=wal here that neither the
891 ** journal file nor the WAL file are open. This happens during
892 ** a rollback transaction that switches from journal_mode=off
893 ** to journal_mode=wal.
894 */
895 assert( p->eLock>=RESERVED_LOCK );
896 assert( isOpen(p->jfd)
897 || p->journalMode==PAGER_JOURNALMODE_OFF
898 || p->journalMode==PAGER_JOURNALMODE_WAL
899 );
900 }
901 assert( pPager->dbOrigSize==pPager->dbFileSize );
902 assert( pPager->dbOrigSize==pPager->dbHintSize );
903 break;
904
905 case PAGER_WRITER_DBMOD:
906 assert( p->eLock==EXCLUSIVE_LOCK );
907 assert( pPager->errCode==SQLITE_OK );
908 assert( !pagerUseWal(pPager) );
909 assert( p->eLock>=EXCLUSIVE_LOCK );
910 assert( isOpen(p->jfd)
911 || p->journalMode==PAGER_JOURNALMODE_OFF
912 || p->journalMode==PAGER_JOURNALMODE_WAL
913 );
914 assert( pPager->dbOrigSize<=pPager->dbHintSize );
915 break;
916
917 case PAGER_WRITER_FINISHED:
918 assert( p->eLock==EXCLUSIVE_LOCK );
919 assert( pPager->errCode==SQLITE_OK );
920 assert( !pagerUseWal(pPager) );
921 assert( isOpen(p->jfd)
922 || p->journalMode==PAGER_JOURNALMODE_OFF
923 || p->journalMode==PAGER_JOURNALMODE_WAL
924 );
925 break;
926
927 case PAGER_ERROR:
928 /* There must be at least one outstanding reference to the pager if
929 ** in ERROR state. Otherwise the pager should have already dropped
930 ** back to OPEN state.
931 */
932 assert( pPager->errCode!=SQLITE_OK );
933 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
934 break;
935 }
414 936
415 return 1; 937 return 1;
416 } 938 }
939
940 /*
941 ** Return a pointer to a human readable string in a static buffer
942 ** containing the state of the Pager object passed as an argument. This
943 ** is intended to be used within debuggers. For example, as an alternative
944 ** to "print *pPager" in gdb:
945 **
946 ** (gdb) printf "%s", print_pager_state(pPager)
947 */
948 static char *print_pager_state(Pager *p){
949 static char zRet[1024];
950
951 sqlite3_snprintf(1024, zRet,
952 "Filename: %s\n"
953 "State: %s errCode=%d\n"
954 "Lock: %s\n"
955 "Locking mode: locking_mode=%s\n"
956 "Journal mode: journal_mode=%s\n"
957 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
958 "Journal: journalOff=%lld journalHdr=%lld\n"
959 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
960 , p->zFilename
961 , p->eState==PAGER_OPEN ? "OPEN" :
962 p->eState==PAGER_READER ? "READER" :
963 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
964 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
965 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
966 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
967 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
968 , (int)p->errCode
969 , p->eLock==NO_LOCK ? "NO_LOCK" :
970 p->eLock==RESERVED_LOCK ? "RESERVED" :
971 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
972 p->eLock==SHARED_LOCK ? "SHARED" :
973 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
974 , p->exclusiveMode ? "exclusive" : "normal"
975 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
976 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
977 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
978 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
979 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
980 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
981 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
982 , p->journalOff, p->journalHdr
983 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
984 );
985
986 return zRet;
987 }
417 #endif 988 #endif
418 989
419 /* 990 /*
420 ** Return true if it is necessary to write page *pPg into the sub-journal. 991 ** Return true if it is necessary to write page *pPg into the sub-journal.
421 ** A page needs to be written into the sub-journal if there exists one 992 ** A page needs to be written into the sub-journal if there exists one
422 ** or more open savepoints for which: 993 ** or more open savepoints for which:
423 ** 994 **
424 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and 995 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
425 ** * The bit corresponding to the page-number is not set in 996 ** * The bit corresponding to the page-number is not set in
426 ** PagerSavepoint.pInSavepoint. 997 ** PagerSavepoint.pInSavepoint.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 *pRes = sqlite3Get4byte(ac); 1030 *pRes = sqlite3Get4byte(ac);
460 } 1031 }
461 return rc; 1032 return rc;
462 } 1033 }
463 1034
464 /* 1035 /*
465 ** Write a 32-bit integer into a string buffer in big-endian byte order. 1036 ** Write a 32-bit integer into a string buffer in big-endian byte order.
466 */ 1037 */
467 #define put32bits(A,B) sqlite3Put4byte((u8*)A,B) 1038 #define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
468 1039
1040
469 /* 1041 /*
470 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK 1042 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
471 ** on success or an error code is something goes wrong. 1043 ** on success or an error code is something goes wrong.
472 */ 1044 */
473 static int write32bits(sqlite3_file *fd, i64 offset, u32 val){ 1045 static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
474 char ac[4]; 1046 char ac[4];
475 put32bits(ac, val); 1047 put32bits(ac, val);
476 return sqlite3OsWrite(fd, ac, 4, offset); 1048 return sqlite3OsWrite(fd, ac, 4, offset);
477 } 1049 }
478 1050
479 /* 1051 /*
480 ** The argument to this macro is a file descriptor (type sqlite3_file*). 1052 ** Unlock the database file to level eLock, which must be either NO_LOCK
481 ** Return 0 if it is not open, or non-zero (but not 1) if it is. 1053 ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
1054 ** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
482 ** 1055 **
483 ** This is so that expressions can be written as: 1056 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
484 ** 1057 ** called, do not modify it. See the comment above the #define of
485 ** if( isOpen(pPager->jfd) ){ ... 1058 ** UNKNOWN_LOCK for an explanation of this.
486 **
487 ** instead of
488 **
489 ** if( pPager->jfd->pMethods ){ ...
490 */ 1059 */
491 #define isOpen(pFd) ((pFd)->pMethods) 1060 static int pagerUnlockDb(Pager *pPager, int eLock){
1061 int rc = SQLITE_OK;
492 1062
493 /* 1063 assert( !pPager->exclusiveMode );
494 ** If file pFd is open, call sqlite3OsUnlock() on it. 1064 assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
495 */ 1065 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
496 static int osUnlock(sqlite3_file *pFd, int eLock){ 1066 if( isOpen(pPager->fd) ){
497 if( !isOpen(pFd) ){ 1067 assert( pPager->eLock>=eLock );
498 return SQLITE_OK; 1068 rc = sqlite3OsUnlock(pPager->fd, eLock);
1069 if( pPager->eLock!=UNKNOWN_LOCK ){
1070 pPager->eLock = (u8)eLock;
1071 }
1072 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
499 } 1073 }
500 return sqlite3OsUnlock(pFd, eLock); 1074 return rc;
501 } 1075 }
502 1076
503 /* 1077 /*
1078 ** Lock the database file to level eLock, which must be either SHARED_LOCK,
1079 ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
1080 ** Pager.eLock variable to the new locking state.
1081 **
1082 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
1083 ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
1084 ** See the comment above the #define of UNKNOWN_LOCK for an explanation
1085 ** of this.
1086 */
1087 static int pagerLockDb(Pager *pPager, int eLock){
1088 int rc = SQLITE_OK;
1089
1090 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
1091 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
1092 rc = sqlite3OsLock(pPager->fd, eLock);
1093 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
1094 pPager->eLock = (u8)eLock;
1095 IOTRACE(("LOCK %p %d\n", pPager, eLock))
1096 }
1097 }
1098 return rc;
1099 }
1100
1101 /*
504 ** This function determines whether or not the atomic-write optimization 1102 ** This function determines whether or not the atomic-write optimization
505 ** can be used with this pager. The optimization can be used if: 1103 ** can be used with this pager. The optimization can be used if:
506 ** 1104 **
507 ** (a) the value returned by OsDeviceCharacteristics() indicates that 1105 ** (a) the value returned by OsDeviceCharacteristics() indicates that
508 ** a database page may be written atomically, and 1106 ** a database page may be written atomically, and
509 ** (b) the value returned by OsSectorSize() is less than or equal 1107 ** (b) the value returned by OsSectorSize() is less than or equal
510 ** to the page size. 1108 ** to the page size.
511 ** 1109 **
512 ** The optimization is also always enabled for temporary files. It is 1110 ** The optimization is also always enabled for temporary files. It is
513 ** an error to call this function if pPager is opened on an in-memory 1111 ** an error to call this function if pPager is opened on an in-memory
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 } 1164 }
567 1165
568 /* 1166 /*
569 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES 1167 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
570 ** is defined, and NDEBUG is not defined, an assert() statement checks 1168 ** is defined, and NDEBUG is not defined, an assert() statement checks
571 ** that the page is either dirty or still matches the calculated page-hash. 1169 ** that the page is either dirty or still matches the calculated page-hash.
572 */ 1170 */
573 #define CHECK_PAGE(x) checkPage(x) 1171 #define CHECK_PAGE(x) checkPage(x)
574 static void checkPage(PgHdr *pPg){ 1172 static void checkPage(PgHdr *pPg){
575 Pager *pPager = pPg->pPager; 1173 Pager *pPager = pPg->pPager;
576 assert( !pPg->pageHash || pPager->errCode 1174 assert( pPager->eState!=PAGER_ERROR );
577 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) ); 1175 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
578 } 1176 }
579 1177
580 #else 1178 #else
581 #define pager_datahash(X,Y) 0 1179 #define pager_datahash(X,Y) 0
582 #define pager_pagehash(X) 0 1180 #define pager_pagehash(X) 0
1181 #define pager_set_pagehash(X)
583 #define CHECK_PAGE(x) 1182 #define CHECK_PAGE(x)
584 #endif /* SQLITE_CHECK_PAGES */ 1183 #endif /* SQLITE_CHECK_PAGES */
585 1184
586 /* 1185 /*
587 ** When this is called the journal file for pager pPager must be open. 1186 ** When this is called the journal file for pager pPager must be open.
588 ** This function attempts to read a master journal file name from the 1187 ** This function attempts to read a master journal file name from the
589 ** end of the file and, if successful, copies it into memory supplied 1188 ** end of the file and, if successful, copies it into memory supplied
590 ** by the caller. See comments above writeMasterJournal() for the format 1189 ** by the caller. See comments above writeMasterJournal() for the format
591 ** used to store a master journal file name at the end of a journal file. 1190 ** used to store a master journal file name at the end of a journal file.
592 ** 1191 **
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 ** - 4 bytes: Random number used for page hash. 1338 ** - 4 bytes: Random number used for page hash.
740 ** - 4 bytes: Initial database page count. 1339 ** - 4 bytes: Initial database page count.
741 ** - 4 bytes: Sector size used by the process that wrote this journal. 1340 ** - 4 bytes: Sector size used by the process that wrote this journal.
742 ** - 4 bytes: Database page size. 1341 ** - 4 bytes: Database page size.
743 ** 1342 **
744 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. 1343 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
745 */ 1344 */
746 static int writeJournalHdr(Pager *pPager){ 1345 static int writeJournalHdr(Pager *pPager){
747 int rc = SQLITE_OK; /* Return code */ 1346 int rc = SQLITE_OK; /* Return code */
748 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */ 1347 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
749 u32 nHeader = pPager->pageSize; /* Size of buffer pointed to by zHeader */ 1348 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
750 u32 nWrite; /* Bytes of header sector written */ 1349 u32 nWrite; /* Bytes of header sector written */
751 int ii; /* Loop counter */ 1350 int ii; /* Loop counter */
752 1351
753 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */ 1352 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
754 1353
755 if( nHeader>JOURNAL_HDR_SZ(pPager) ){ 1354 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
756 nHeader = JOURNAL_HDR_SZ(pPager); 1355 nHeader = JOURNAL_HDR_SZ(pPager);
757 } 1356 }
758 1357
759 /* If there are active savepoints and any of them were created 1358 /* If there are active savepoints and any of them were created
(...skipping 22 matching lines...) Expand all
782 ** file it may contain some garbage data. There are two scenarios 1381 ** file it may contain some garbage data. There are two scenarios
783 ** where this risk can be ignored: 1382 ** where this risk can be ignored:
784 ** 1383 **
785 ** * When the pager is in no-sync mode. Corruption can follow a 1384 ** * When the pager is in no-sync mode. Corruption can follow a
786 ** power failure in this case anyway. 1385 ** power failure in this case anyway.
787 ** 1386 **
788 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees 1387 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
789 ** that garbage data is never appended to the journal file. 1388 ** that garbage data is never appended to the journal file.
790 */ 1389 */
791 assert( isOpen(pPager->fd) || pPager->noSync ); 1390 assert( isOpen(pPager->fd) || pPager->noSync );
792 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY) 1391 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
793 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 1392 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
794 ){ 1393 ){
795 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); 1394 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
796 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); 1395 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
797 }else{ 1396 }else{
798 memset(zHeader, 0, sizeof(aJournalMagic)+4); 1397 memset(zHeader, 0, sizeof(aJournalMagic)+4);
799 } 1398 }
800 1399
801 /* The random check-hash initialiser */ 1400 /* The random check-hash initialiser */
802 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); 1401 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
(...skipping 27 matching lines...) Expand all
830 ** is done. 1429 ** is done.
831 ** 1430 **
832 ** The loop is required here in case the sector-size is larger than the 1431 ** The loop is required here in case the sector-size is larger than the
833 ** database page size. Since the zHeader buffer is only Pager.pageSize 1432 ** database page size. Since the zHeader buffer is only Pager.pageSize
834 ** bytes in size, more than one call to sqlite3OsWrite() may be required 1433 ** bytes in size, more than one call to sqlite3OsWrite() may be required
835 ** to populate the entire journal header sector. 1434 ** to populate the entire journal header sector.
836 */ 1435 */
837 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){ 1436 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
838 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader)) 1437 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
839 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff); 1438 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
1439 assert( pPager->journalHdr <= pPager->journalOff );
840 pPager->journalOff += nHeader; 1440 pPager->journalOff += nHeader;
841 } 1441 }
842 1442
843 return rc; 1443 return rc;
844 } 1444 }
845 1445
846 /* 1446 /*
847 ** The journal file must be open when this is called. A journal header file 1447 ** The journal file must be open when this is called. A journal header file
848 ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal 1448 ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
849 ** file. The current location in the journal file is given by 1449 ** file. The current location in the journal file is given by
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
905 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec)) 1505 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
906 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit)) 1506 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
907 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize)) 1507 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
908 ){ 1508 ){
909 return rc; 1509 return rc;
910 } 1510 }
911 1511
912 if( pPager->journalOff==0 ){ 1512 if( pPager->journalOff==0 ){
913 u32 iPageSize; /* Page-size field of journal header */ 1513 u32 iPageSize; /* Page-size field of journal header */
914 u32 iSectorSize; /* Sector-size field of journal header */ 1514 u32 iSectorSize; /* Sector-size field of journal header */
915 u16 iPageSize16; /* Copy of iPageSize in 16-bit variable */
916 1515
917 /* Read the page-size and sector-size journal header fields. */ 1516 /* Read the page-size and sector-size journal header fields. */
918 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize)) 1517 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
919 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize)) 1518 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
920 ){ 1519 ){
921 return rc; 1520 return rc;
922 } 1521 }
923 1522
1523 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
1524 ** journal header to zero. In this case, assume that the Pager.pageSize
1525 ** variable is already set to the correct page size.
1526 */
1527 if( iPageSize==0 ){
1528 iPageSize = pPager->pageSize;
1529 }
1530
924 /* Check that the values read from the page-size and sector-size fields 1531 /* Check that the values read from the page-size and sector-size fields
925 ** are within range. To be 'in range', both values need to be a power 1532 ** are within range. To be 'in range', both values need to be a power
926 ** of two greater than or equal to 512, and not greater than their 1533 ** of two greater than or equal to 512 or 32, and not greater than their
927 ** respective compile time maximum limits. 1534 ** respective compile time maximum limits.
928 */ 1535 */
929 if( iPageSize<512 || iSectorSize<512 1536 if( iPageSize<512 || iSectorSize<32
930 || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE 1537 || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
931 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0 1538 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
932 ){ 1539 ){
933 /* If the either the page-size or sector-size in the journal-header is 1540 /* If the either the page-size or sector-size in the journal-header is
934 ** invalid, then the process that wrote the journal-header must have 1541 ** invalid, then the process that wrote the journal-header must have
935 ** crashed before the header was synced. In this case stop reading 1542 ** crashed before the header was synced. In this case stop reading
936 ** the journal file here. 1543 ** the journal file here.
937 */ 1544 */
938 return SQLITE_DONE; 1545 return SQLITE_DONE;
939 } 1546 }
940 1547
941 /* Update the page-size to match the value read from the journal. 1548 /* Update the page-size to match the value read from the journal.
942 ** Use a testcase() macro to make sure that malloc failure within 1549 ** Use a testcase() macro to make sure that malloc failure within
943 ** PagerSetPagesize() is tested. 1550 ** PagerSetPagesize() is tested.
944 */ 1551 */
945 iPageSize16 = (u16)iPageSize; 1552 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
946 rc = sqlite3PagerSetPagesize(pPager, &iPageSize16, -1);
947 testcase( rc!=SQLITE_OK ); 1553 testcase( rc!=SQLITE_OK );
948 assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize );
949 1554
950 /* Update the assumed sector-size to match the value used by 1555 /* Update the assumed sector-size to match the value used by
951 ** the process that created this journal. If this journal was 1556 ** the process that created this journal. If this journal was
952 ** created by a process other than this one, then this routine 1557 ** created by a process other than this one, then this routine
953 ** is being called from within pager_playback(). The local value 1558 ** is being called from within pager_playback(). The local value
954 ** of Pager.sectorSize is restored at the end of that routine. 1559 ** of Pager.sectorSize is restored at the end of that routine.
955 */ 1560 */
956 pPager->sectorSize = iSectorSize; 1561 pPager->sectorSize = iSectorSize;
957 } 1562 }
958 1563
(...skipping 21 matching lines...) Expand all
980 ** If zMaster is a NULL pointer (occurs for a single database transaction), 1585 ** If zMaster is a NULL pointer (occurs for a single database transaction),
981 ** this call is a no-op. 1586 ** this call is a no-op.
982 */ 1587 */
983 static int writeMasterJournal(Pager *pPager, const char *zMaster){ 1588 static int writeMasterJournal(Pager *pPager, const char *zMaster){
984 int rc; /* Return code */ 1589 int rc; /* Return code */
985 int nMaster; /* Length of string zMaster */ 1590 int nMaster; /* Length of string zMaster */
986 i64 iHdrOff; /* Offset of header in journal file */ 1591 i64 iHdrOff; /* Offset of header in journal file */
987 i64 jrnlSize; /* Size of journal file on disk */ 1592 i64 jrnlSize; /* Size of journal file on disk */
988 u32 cksum = 0; /* Checksum of string zMaster */ 1593 u32 cksum = 0; /* Checksum of string zMaster */
989 1594
990 if( !zMaster || pPager->setMaster 1595 assert( pPager->setMaster==0 );
1596 assert( !pagerUseWal(pPager) );
1597
1598 if( !zMaster
991 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 1599 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
992 || pPager->journalMode==PAGER_JOURNALMODE_OFF 1600 || pPager->journalMode==PAGER_JOURNALMODE_OFF
993 ){ 1601 ){
994 return SQLITE_OK; 1602 return SQLITE_OK;
995 } 1603 }
996 pPager->setMaster = 1; 1604 pPager->setMaster = 1;
997 assert( isOpen(pPager->jfd) ); 1605 assert( isOpen(pPager->jfd) );
1606 assert( pPager->journalHdr <= pPager->journalOff );
998 1607
999 /* Calculate the length in bytes and the checksum of zMaster */ 1608 /* Calculate the length in bytes and the checksum of zMaster */
1000 for(nMaster=0; zMaster[nMaster]; nMaster++){ 1609 for(nMaster=0; zMaster[nMaster]; nMaster++){
1001 cksum += zMaster[nMaster]; 1610 cksum += zMaster[nMaster];
1002 } 1611 }
1003 1612
1004 /* If in full-sync mode, advance to the next disk sector before writing 1613 /* If in full-sync mode, advance to the next disk sector before writing
1005 ** the master journal name. This is in case the previous page written to 1614 ** the master journal name. This is in case the previous page written to
1006 ** the journal has already been synced. 1615 ** the journal has already been synced.
1007 */ 1616 */
1008 if( pPager->fullSync ){ 1617 if( pPager->fullSync ){
1009 pPager->journalOff = journalHdrOffset(pPager); 1618 pPager->journalOff = journalHdrOffset(pPager);
1010 } 1619 }
1011 iHdrOff = pPager->journalOff; 1620 iHdrOff = pPager->journalOff;
1012 1621
1013 /* Write the master journal data to the end of the journal file. If 1622 /* Write the master journal data to the end of the journal file. If
1014 ** an error occurs, return the error code to the caller. 1623 ** an error occurs, return the error code to the caller.
1015 */ 1624 */
1016 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager)))) 1625 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
1017 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4))) 1626 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
1018 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster))) 1627 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
1019 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum))) 1628 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
1020 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaste r+8))) 1629 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaste r+8)))
1021 ){ 1630 ){
1022 return rc; 1631 return rc;
1023 } 1632 }
1024 pPager->journalOff += (nMaster+20); 1633 pPager->journalOff += (nMaster+20);
1025 pPager->needSync = !pPager->noSync;
1026 1634
1027 /* If the pager is in peristent-journal mode, then the physical 1635 /* If the pager is in peristent-journal mode, then the physical
1028 ** journal-file may extend past the end of the master-journal name 1636 ** journal-file may extend past the end of the master-journal name
1029 ** and 8 bytes of magic data just written to the file. This is 1637 ** and 8 bytes of magic data just written to the file. This is
1030 ** dangerous because the code to rollback a hot-journal file 1638 ** dangerous because the code to rollback a hot-journal file
1031 ** will not be able to find the master-journal name to determine 1639 ** will not be able to find the master-journal name to determine
1032 ** whether or not the journal is hot. 1640 ** whether or not the journal is hot.
1033 ** 1641 **
1034 ** Easiest thing to do in this scenario is to truncate the journal 1642 ** Easiest thing to do in this scenario is to truncate the journal
1035 ** file to the required size. 1643 ** file to the required size.
(...skipping 15 matching lines...) Expand all
1051 PgHdr *p; /* Return value */ 1659 PgHdr *p; /* Return value */
1052 1660
1053 /* It is not possible for a call to PcacheFetch() with createFlag==0 to 1661 /* It is not possible for a call to PcacheFetch() with createFlag==0 to
1054 ** fail, since no attempt to allocate dynamic memory will be made. 1662 ** fail, since no attempt to allocate dynamic memory will be made.
1055 */ 1663 */
1056 (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p); 1664 (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
1057 return p; 1665 return p;
1058 } 1666 }
1059 1667
1060 /* 1668 /*
1061 ** Unless the pager is in error-state, discard all in-memory pages. If 1669 ** Discard the entire contents of the in-memory page-cache.
1062 ** the pager is in error-state, then this call is a no-op.
1063 **
1064 ** TODO: Why can we not reset the pager while in error state?
1065 */ 1670 */
1066 static void pager_reset(Pager *pPager){ 1671 static void pager_reset(Pager *pPager){
1067 if( SQLITE_OK==pPager->errCode ){ 1672 sqlite3BackupRestart(pPager->pBackup);
1068 sqlite3BackupRestart(pPager->pBackup); 1673 sqlite3PcacheClear(pPager->pPCache);
1069 sqlite3PcacheClear(pPager->pPCache);
1070 pPager->dbSizeValid = 0;
1071 }
1072 } 1674 }
1073 1675
1074 /* 1676 /*
1075 ** Free all structures in the Pager.aSavepoint[] array and set both 1677 ** Free all structures in the Pager.aSavepoint[] array and set both
1076 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal 1678 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
1077 ** if it is open and the pager is not in exclusive mode. 1679 ** if it is open and the pager is not in exclusive mode.
1078 */ 1680 */
1079 static void releaseAllSavepoints(Pager *pPager){ 1681 static void releaseAllSavepoints(Pager *pPager){
1080 int ii; /* Iterator for looping through Pager.aSavepoint */ 1682 int ii; /* Iterator for looping through Pager.aSavepoint */
1081 for(ii=0; ii<pPager->nSavepoint; ii++){ 1683 for(ii=0; ii<pPager->nSavepoint; ii++){
(...skipping 22 matching lines...) Expand all
1104 if( pgno<=p->nOrig ){ 1706 if( pgno<=p->nOrig ){
1105 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno); 1707 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
1106 testcase( rc==SQLITE_NOMEM ); 1708 testcase( rc==SQLITE_NOMEM );
1107 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); 1709 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
1108 } 1710 }
1109 } 1711 }
1110 return rc; 1712 return rc;
1111 } 1713 }
1112 1714
1113 /* 1715 /*
1114 ** Unlock the database file. This function is a no-op if the pager 1716 ** This function is a no-op if the pager is in exclusive mode and not
1115 ** is in exclusive mode. 1717 ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
1718 ** state.
1116 ** 1719 **
1117 ** If the pager is currently in error state, discard the contents of 1720 ** If the pager is not in exclusive-access mode, the database file is
1118 ** the cache and reset the Pager structure internal state. If there is 1721 ** completely unlocked. If the file is unlocked and the file-system does
1119 ** an open journal-file, then the next time a shared-lock is obtained 1722 ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
1120 ** on the pager file (by this or any other process), it will be 1723 ** closed (if it is open).
1121 ** treated as a hot-journal and rolled back. 1724 **
1725 ** If the pager is in ERROR state when this function is called, the
1726 ** contents of the pager cache are discarded before switching back to
1727 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
1728 ** or not, any journal file left in the file-system will be treated
1729 ** as a hot-journal and rolled back the next time a read-transaction
1730 ** is opened (by this or by any other connection).
1122 */ 1731 */
1123 static void pager_unlock(Pager *pPager){ 1732 static void pager_unlock(Pager *pPager){
1124 if( !pPager->exclusiveMode ){
1125 int rc; /* Return code */
1126 1733
1127 /* Always close the journal file when dropping the database lock. 1734 assert( pPager->eState==PAGER_READER
1128 ** Otherwise, another connection with journal_mode=delete might 1735 || pPager->eState==PAGER_OPEN
1129 ** delete the file out from under us. 1736 || pPager->eState==PAGER_ERROR
1737 );
1738
1739 sqlite3BitvecDestroy(pPager->pInJournal);
1740 pPager->pInJournal = 0;
1741 releaseAllSavepoints(pPager);
1742
1743 if( pagerUseWal(pPager) ){
1744 assert( !isOpen(pPager->jfd) );
1745 sqlite3WalEndReadTransaction(pPager->pWal);
1746 pPager->eState = PAGER_OPEN;
1747 }else if( !pPager->exclusiveMode ){
1748 int rc; /* Error code returned by pagerUnlockDb() */
1749 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
1750
1751 /* If the operating system support deletion of open files, then
1752 ** close the journal file when dropping the database lock. Otherwise
1753 ** another connection with journal_mode=delete might delete the file
1754 ** out from under us.
1130 */ 1755 */
1131 sqlite3OsClose(pPager->jfd); 1756 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
1132 sqlite3BitvecDestroy(pPager->pInJournal); 1757 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
1133 pPager->pInJournal = 0; 1758 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
1134 releaseAllSavepoints(pPager); 1759 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
1135 1760 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
1136 /* If the file is unlocked, somebody else might change it. The 1761 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
1137 ** values stored in Pager.dbSize etc. might become invalid if 1762 if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
1138 ** this happens. TODO: Really, this doesn't need to be cleared 1763 || 1!=(pPager->journalMode & 5)
1139 ** until the change-counter check fails in PagerSharedLock(). 1764 ){
1140 */ 1765 sqlite3OsClose(pPager->jfd);
1141 pPager->dbSizeValid = 0;
1142
1143 rc = osUnlock(pPager->fd, NO_LOCK);
1144 if( rc ){
1145 pPager->errCode = rc;
1146 }
1147 IOTRACE(("UNLOCK %p\n", pPager))
1148
1149 /* If Pager.errCode is set, the contents of the pager cache cannot be
1150 ** trusted. Now that the pager file is unlocked, the contents of the
1151 ** cache can be discarded and the error code safely cleared.
1152 */
1153 if( pPager->errCode ){
1154 if( rc==SQLITE_OK ){
1155 pPager->errCode = SQLITE_OK;
1156 }
1157 pager_reset(pPager);
1158 } 1766 }
1159 1767
1768 /* If the pager is in the ERROR state and the call to unlock the database
1769 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
1770 ** above the #define for UNKNOWN_LOCK for an explanation of why this
1771 ** is necessary.
1772 */
1773 rc = pagerUnlockDb(pPager, NO_LOCK);
1774 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
1775 pPager->eLock = UNKNOWN_LOCK;
1776 }
1777
1778 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
1779 ** without clearing the error code. This is intentional - the error
1780 ** code is cleared and the cache reset in the block below.
1781 */
1782 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
1160 pPager->changeCountDone = 0; 1783 pPager->changeCountDone = 0;
1161 pPager->state = PAGER_UNLOCK; 1784 pPager->eState = PAGER_OPEN;
1162 } 1785 }
1786
1787 /* If Pager.errCode is set, the contents of the pager cache cannot be
1788 ** trusted. Now that there are no outstanding references to the pager,
1789 ** it can safely move back to PAGER_OPEN state. This happens in both
1790 ** normal and exclusive-locking mode.
1791 */
1792 if( pPager->errCode ){
1793 assert( !MEMDB );
1794 pager_reset(pPager);
1795 pPager->changeCountDone = pPager->tempFile;
1796 pPager->eState = PAGER_OPEN;
1797 pPager->errCode = SQLITE_OK;
1798 }
1799
1800 pPager->journalOff = 0;
1801 pPager->journalHdr = 0;
1802 pPager->setMaster = 0;
1163 } 1803 }
1164 1804
1165 /* 1805 /*
1166 ** This function should be called when an IOERR, CORRUPT or FULL error 1806 ** This function is called whenever an IOERR or FULL error that requires
1167 ** may have occurred. The first argument is a pointer to the pager 1807 ** the pager to transition into the ERROR state may ahve occurred.
1168 ** structure, the second the error-code about to be returned by a pager 1808 ** The first argument is a pointer to the pager structure, the second
1169 ** API function. The value returned is a copy of the second argument 1809 ** the error-code about to be returned by a pager API function. The
1170 ** to this function. 1810 ** value returned is a copy of the second argument to this function.
1171 ** 1811 **
1172 ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL 1812 ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
1173 ** the error becomes persistent. Until the persisten error is cleared, 1813 ** IOERR sub-codes, the pager enters the ERROR state and the error code
1174 ** subsequent API calls on this Pager will immediately return the same 1814 ** is stored in Pager.errCode. While the pager remains in the ERROR state,
1175 ** error code. 1815 ** all major API calls on the Pager will immediately return Pager.errCode.
1176 ** 1816 **
1177 ** A persistent error indicates that the contents of the pager-cache 1817 ** The ERROR state indicates that the contents of the pager-cache
1178 ** cannot be trusted. This state can be cleared by completely discarding 1818 ** cannot be trusted. This state can be cleared by completely discarding
1179 ** the contents of the pager-cache. If a transaction was active when 1819 ** the contents of the pager-cache. If a transaction was active when
1180 ** the persistent error occurred, then the rollback journal may need 1820 ** the persistent error occurred, then the rollback journal may need
1181 ** to be replayed to restore the contents of the database file (as if 1821 ** to be replayed to restore the contents of the database file (as if
1182 ** it were a hot-journal). 1822 ** it were a hot-journal).
1183 */ 1823 */
1184 static int pager_error(Pager *pPager, int rc){ 1824 static int pager_error(Pager *pPager, int rc){
1185 int rc2 = rc & 0xff; 1825 int rc2 = rc & 0xff;
1186 assert( rc==SQLITE_OK || !MEMDB ); 1826 assert( rc==SQLITE_OK || !MEMDB );
1187 assert( 1827 assert(
1188 pPager->errCode==SQLITE_FULL || 1828 pPager->errCode==SQLITE_FULL ||
1189 pPager->errCode==SQLITE_OK || 1829 pPager->errCode==SQLITE_OK ||
1190 (pPager->errCode & 0xff)==SQLITE_IOERR 1830 (pPager->errCode & 0xff)==SQLITE_IOERR
1191 ); 1831 );
1192 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){ 1832 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
1193 pPager->errCode = rc; 1833 pPager->errCode = rc;
1834 pPager->eState = PAGER_ERROR;
1194 } 1835 }
1195 return rc; 1836 return rc;
1196 } 1837 }
1197 1838
1198 /* 1839 /*
1199 ** Execute a rollback if a transaction is active and unlock the
1200 ** database file.
1201 **
1202 ** If the pager has already entered the error state, do not attempt
1203 ** the rollback at this time. Instead, pager_unlock() is called. The
1204 ** call to pager_unlock() will discard all in-memory pages, unlock
1205 ** the database file and clear the error state. If this means that
1206 ** there is a hot-journal left in the file-system, the next connection
1207 ** to obtain a shared lock on the pager (which may be this one) will
1208 ** roll it back.
1209 **
1210 ** If the pager has not already entered the error state, but an IO or
1211 ** malloc error occurs during a rollback, then this will itself cause
1212 ** the pager to enter the error state. Which will be cleared by the
1213 ** call to pager_unlock(), as described above.
1214 */
1215 static void pagerUnlockAndRollback(Pager *pPager){
1216 if( pPager->errCode==SQLITE_OK && pPager->state>=PAGER_RESERVED ){
1217 sqlite3BeginBenignMalloc();
1218 sqlite3PagerRollback(pPager);
1219 sqlite3EndBenignMalloc();
1220 }
1221 pager_unlock(pPager);
1222 }
1223
1224 /*
1225 ** This routine ends a transaction. A transaction is usually ended by 1840 ** This routine ends a transaction. A transaction is usually ended by
1226 ** either a COMMIT or a ROLLBACK operation. This routine may be called 1841 ** either a COMMIT or a ROLLBACK operation. This routine may be called
1227 ** after rollback of a hot-journal, or if an error occurs while opening 1842 ** after rollback of a hot-journal, or if an error occurs while opening
1228 ** the journal file or writing the very first journal-header of a 1843 ** the journal file or writing the very first journal-header of a
1229 ** database transaction. 1844 ** database transaction.
1230 ** 1845 **
1231 ** If the pager is in PAGER_SHARED or PAGER_UNLOCK state when this 1846 ** This routine is never called in PAGER_ERROR state. If it is called
1232 ** routine is called, it is a no-op (returns SQLITE_OK). 1847 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less
1848 ** exclusive than a RESERVED lock, it is a no-op.
1233 ** 1849 **
1234 ** Otherwise, any active savepoints are released. 1850 ** Otherwise, any active savepoints are released.
1235 ** 1851 **
1236 ** If the journal file is open, then it is "finalized". Once a journal 1852 ** If the journal file is open, then it is "finalized". Once a journal
1237 ** file has been finalized it is not possible to use it to roll back a 1853 ** file has been finalized it is not possible to use it to roll back a
1238 ** transaction. Nor will it be considered to be a hot-journal by this 1854 ** transaction. Nor will it be considered to be a hot-journal by this
1239 ** or any other database connection. Exactly how a journal is finalized 1855 ** or any other database connection. Exactly how a journal is finalized
1240 ** depends on whether or not the pager is running in exclusive mode and 1856 ** depends on whether or not the pager is running in exclusive mode and
1241 ** the current journal-mode (Pager.journalMode value), as follows: 1857 ** the current journal-mode (Pager.journalMode value), as follows:
1242 ** 1858 **
(...skipping 10 matching lines...) Expand all
1253 ** file. An invalid journal file cannot be rolled back. 1869 ** file. An invalid journal file cannot be rolled back.
1254 ** 1870 **
1255 ** journalMode==DELETE 1871 ** journalMode==DELETE
1256 ** The journal file is closed and deleted using sqlite3OsDelete(). 1872 ** The journal file is closed and deleted using sqlite3OsDelete().
1257 ** 1873 **
1258 ** If the pager is running in exclusive mode, this method of finalizing 1874 ** If the pager is running in exclusive mode, this method of finalizing
1259 ** the journal file is never used. Instead, if the journalMode is 1875 ** the journal file is never used. Instead, if the journalMode is
1260 ** DELETE and the pager is in exclusive mode, the method described under 1876 ** DELETE and the pager is in exclusive mode, the method described under
1261 ** journalMode==PERSIST is used instead. 1877 ** journalMode==PERSIST is used instead.
1262 ** 1878 **
1263 ** After the journal is finalized, if running in non-exclusive mode, the 1879 ** After the journal is finalized, the pager moves to PAGER_READER state.
1264 ** pager moves to PAGER_SHARED state (and downgrades the lock on the 1880 ** If running in non-exclusive rollback mode, the lock on the file is
1265 ** database file accordingly). 1881 ** downgraded to a SHARED_LOCK.
1266 **
1267 ** If the pager is running in exclusive mode and is in PAGER_SYNCED state,
1268 ** it moves to PAGER_EXCLUSIVE. No locks are downgraded when running in
1269 ** exclusive mode.
1270 ** 1882 **
1271 ** SQLITE_OK is returned if no error occurs. If an error occurs during 1883 ** SQLITE_OK is returned if no error occurs. If an error occurs during
1272 ** any of the IO operations to finalize the journal file or unlock the 1884 ** any of the IO operations to finalize the journal file or unlock the
1273 ** database then the IO error code is returned to the user. If the 1885 ** database then the IO error code is returned to the user. If the
1274 ** operation to finalize the journal file fails, then the code still 1886 ** operation to finalize the journal file fails, then the code still
1275 ** tries to unlock the database file if not in exclusive mode. If the 1887 ** tries to unlock the database file if not in exclusive mode. If the
1276 ** unlock operation fails as well, then the first error code related 1888 ** unlock operation fails as well, then the first error code related
1277 ** to the first error encountered (the journal finalization one) is 1889 ** to the first error encountered (the journal finalization one) is
1278 ** returned. 1890 ** returned.
1279 */ 1891 */
1280 static int pager_end_transaction(Pager *pPager, int hasMaster){ 1892 static int pager_end_transaction(Pager *pPager, int hasMaster){
1281 int rc = SQLITE_OK; /* Error code from journal finalization operation */ 1893 int rc = SQLITE_OK; /* Error code from journal finalization operation */
1282 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */ 1894 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
1283 1895
1284 if( pPager->state<PAGER_RESERVED ){ 1896 /* Do nothing if the pager does not have an open write transaction
1897 ** or at least a RESERVED lock. This function may be called when there
1898 ** is no write-transaction active but a RESERVED or greater lock is
1899 ** held under two circumstances:
1900 **
1901 ** 1. After a successful hot-journal rollback, it is called with
1902 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
1903 **
1904 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
1905 ** lock switches back to locking_mode=normal and then executes a
1906 ** read-transaction, this function is called with eState==PAGER_READER
1907 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
1908 */
1909 assert( assert_pager_state(pPager) );
1910 assert( pPager->eState!=PAGER_ERROR );
1911 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
1285 return SQLITE_OK; 1912 return SQLITE_OK;
1286 } 1913 }
1914
1287 releaseAllSavepoints(pPager); 1915 releaseAllSavepoints(pPager);
1288
1289 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 ); 1916 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
1290 if( isOpen(pPager->jfd) ){ 1917 if( isOpen(pPager->jfd) ){
1918 assert( !pagerUseWal(pPager) );
1291 1919
1292 /* Finalize the journal file. */ 1920 /* Finalize the journal file. */
1293 if( sqlite3IsMemJournal(pPager->jfd) ){ 1921 if( sqlite3IsMemJournal(pPager->jfd) ){
1294 assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); 1922 assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
1295 sqlite3OsClose(pPager->jfd); 1923 sqlite3OsClose(pPager->jfd);
1296 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){ 1924 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
1297 if( pPager->journalOff==0 ){ 1925 if( pPager->journalOff==0 ){
1298 rc = SQLITE_OK; 1926 rc = SQLITE_OK;
1299 }else{ 1927 }else{
1300 rc = sqlite3OsTruncate(pPager->jfd, 0); 1928 rc = sqlite3OsTruncate(pPager->jfd, 0);
1301 } 1929 }
1302 pPager->journalOff = 0; 1930 pPager->journalOff = 0;
1303 pPager->journalStarted = 0; 1931 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1304 }else if( pPager->exclusiveMode 1932 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
1305 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1306 ){ 1933 ){
1307 rc = zeroJournalHdr(pPager, hasMaster); 1934 rc = zeroJournalHdr(pPager, hasMaster);
1308 pager_error(pPager, rc);
1309 pPager->journalOff = 0; 1935 pPager->journalOff = 0;
1310 pPager->journalStarted = 0;
1311 }else{ 1936 }else{
1312 /* This branch may be executed with Pager.journalMode==MEMORY if 1937 /* This branch may be executed with Pager.journalMode==MEMORY if
1313 ** a hot-journal was just rolled back. In this case the journal 1938 ** a hot-journal was just rolled back. In this case the journal
1314 ** file should be closed and deleted. If this connection writes to 1939 ** file should be closed and deleted. If this connection writes to
1315 ** the database file, it will do so using an in-memory journal. */ 1940 ** the database file, it will do so using an in-memory journal.
1941 */
1316 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 1942 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
1317 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 1943 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
1944 || pPager->journalMode==PAGER_JOURNALMODE_WAL
1318 ); 1945 );
1319 sqlite3OsClose(pPager->jfd); 1946 sqlite3OsClose(pPager->jfd);
1320 if( !pPager->tempFile ){ 1947 if( !pPager->tempFile ){
1321 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); 1948 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
1322 } 1949 }
1323 } 1950 }
1951 }
1324 1952
1325 #ifdef SQLITE_CHECK_PAGES 1953 #ifdef SQLITE_CHECK_PAGES
1326 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); 1954 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
1955 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
1956 PgHdr *p = pager_lookup(pPager, 1);
1957 if( p ){
1958 p->pageHash = 0;
1959 sqlite3PagerUnref(p);
1960 }
1961 }
1327 #endif 1962 #endif
1328 1963
1329 sqlite3PcacheCleanAll(pPager->pPCache); 1964 sqlite3BitvecDestroy(pPager->pInJournal);
1330 sqlite3BitvecDestroy(pPager->pInJournal); 1965 pPager->pInJournal = 0;
1331 pPager->pInJournal = 0; 1966 pPager->nRec = 0;
1332 pPager->nRec = 0; 1967 sqlite3PcacheCleanAll(pPager->pPCache);
1968 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
1969
1970 if( pagerUseWal(pPager) ){
1971 /* Drop the WAL write-lock, if any. Also, if the connection was in
1972 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
1973 ** lock held on the database file.
1974 */
1975 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
1976 assert( rc2==SQLITE_OK );
1333 } 1977 }
1334 1978 if( !pPager->exclusiveMode
1335 if( !pPager->exclusiveMode ){ 1979 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
1336 rc2 = osUnlock(pPager->fd, SHARED_LOCK); 1980 ){
1337 pPager->state = PAGER_SHARED; 1981 rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
1338 pPager->changeCountDone = 0; 1982 pPager->changeCountDone = 0;
1339 }else if( pPager->state==PAGER_SYNCED ){
1340 pPager->state = PAGER_EXCLUSIVE;
1341 } 1983 }
1984 pPager->eState = PAGER_READER;
1342 pPager->setMaster = 0; 1985 pPager->setMaster = 0;
1343 pPager->needSync = 0;
1344 pPager->dbModified = 0;
1345
1346 /* TODO: Is this optimal? Why is the db size invalidated here
1347 ** when the database file is not unlocked? */
1348 pPager->dbOrigSize = 0;
1349 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
1350 if( !MEMDB ){
1351 pPager->dbSizeValid = 0;
1352 }
1353 1986
1354 return (rc==SQLITE_OK?rc2:rc); 1987 return (rc==SQLITE_OK?rc2:rc);
1355 } 1988 }
1356 1989
1357 /* 1990 /*
1991 ** Execute a rollback if a transaction is active and unlock the
1992 ** database file.
1993 **
1994 ** If the pager has already entered the ERROR state, do not attempt
1995 ** the rollback at this time. Instead, pager_unlock() is called. The
1996 ** call to pager_unlock() will discard all in-memory pages, unlock
1997 ** the database file and move the pager back to OPEN state. If this
1998 ** means that there is a hot-journal left in the file-system, the next
1999 ** connection to obtain a shared lock on the pager (which may be this one)
2000 ** will roll it back.
2001 **
2002 ** If the pager has not already entered the ERROR state, but an IO or
2003 ** malloc error occurs during a rollback, then this will itself cause
2004 ** the pager to enter the ERROR state. Which will be cleared by the
2005 ** call to pager_unlock(), as described above.
2006 */
2007 static void pagerUnlockAndRollback(Pager *pPager){
2008 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
2009 assert( assert_pager_state(pPager) );
2010 if( pPager->eState>=PAGER_WRITER_LOCKED ){
2011 sqlite3BeginBenignMalloc();
2012 sqlite3PagerRollback(pPager);
2013 sqlite3EndBenignMalloc();
2014 }else if( !pPager->exclusiveMode ){
2015 assert( pPager->eState==PAGER_READER );
2016 pager_end_transaction(pPager, 0);
2017 }
2018 }
2019 pager_unlock(pPager);
2020 }
2021
2022 /*
1358 ** Parameter aData must point to a buffer of pPager->pageSize bytes 2023 ** Parameter aData must point to a buffer of pPager->pageSize bytes
1359 ** of data. Compute and return a checksum based ont the contents of the 2024 ** of data. Compute and return a checksum based ont the contents of the
1360 ** page of data and the current value of pPager->cksumInit. 2025 ** page of data and the current value of pPager->cksumInit.
1361 ** 2026 **
1362 ** This is not a real checksum. It is really just the sum of the 2027 ** This is not a real checksum. It is really just the sum of the
1363 ** random initial value (pPager->cksumInit) and every 200th byte 2028 ** random initial value (pPager->cksumInit) and every 200th byte
1364 ** of the page data, starting with byte offset (pPager->pageSize%200). 2029 ** of the page data, starting with byte offset (pPager->pageSize%200).
1365 ** Each byte is interpreted as an 8-bit unsigned integer. 2030 ** Each byte is interpreted as an 8-bit unsigned integer.
1366 ** 2031 **
1367 ** Changing the formula used to compute this checksum results in an 2032 ** Changing the formula used to compute this checksum results in an
1368 ** incompatible journal file format. 2033 ** incompatible journal file format.
1369 ** 2034 **
1370 ** If journal corruption occurs due to a power failure, the most likely 2035 ** If journal corruption occurs due to a power failure, the most likely
1371 ** scenario is that one end or the other of the record will be changed. 2036 ** scenario is that one end or the other of the record will be changed.
1372 ** It is much less likely that the two ends of the journal record will be 2037 ** It is much less likely that the two ends of the journal record will be
1373 ** correct and the middle be corrupt. Thus, this "checksum" scheme, 2038 ** correct and the middle be corrupt. Thus, this "checksum" scheme,
1374 ** though fast and simple, catches the mostly likely kind of corruption. 2039 ** though fast and simple, catches the mostly likely kind of corruption.
1375 */ 2040 */
1376 static u32 pager_cksum(Pager *pPager, const u8 *aData){ 2041 static u32 pager_cksum(Pager *pPager, const u8 *aData){
1377 u32 cksum = pPager->cksumInit; /* Checksum value to return */ 2042 u32 cksum = pPager->cksumInit; /* Checksum value to return */
1378 int i = pPager->pageSize-200; /* Loop counter */ 2043 int i = pPager->pageSize-200; /* Loop counter */
1379 while( i>0 ){ 2044 while( i>0 ){
1380 cksum += aData[i]; 2045 cksum += aData[i];
1381 i -= 200; 2046 i -= 200;
1382 } 2047 }
1383 return cksum; 2048 return cksum;
1384 } 2049 }
1385 2050
1386 /* 2051 /*
2052 ** Report the current page size and number of reserved bytes back
2053 ** to the codec.
2054 */
2055 #ifdef SQLITE_HAS_CODEC
2056 static void pagerReportSize(Pager *pPager){
2057 if( pPager->xCodecSizeChng ){
2058 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
2059 (int)pPager->nReserve);
2060 }
2061 }
2062 #else
2063 # define pagerReportSize(X) /* No-op if we do not support a codec */
2064 #endif
2065
2066 /*
1387 ** Read a single page from either the journal file (if isMainJrnl==1) or 2067 ** Read a single page from either the journal file (if isMainJrnl==1) or
1388 ** from the sub-journal (if isMainJrnl==0) and playback that page. 2068 ** from the sub-journal (if isMainJrnl==0) and playback that page.
1389 ** The page begins at offset *pOffset into the file. The *pOffset 2069 ** The page begins at offset *pOffset into the file. The *pOffset
1390 ** value is increased to the start of the next page in the journal. 2070 ** value is increased to the start of the next page in the journal.
1391 ** 2071 **
1392 ** The isMainJrnl flag is true if this is the main rollback journal and 2072 ** The main rollback journal uses checksums - the statement journal does
1393 ** false for the statement journal. The main rollback journal uses 2073 ** not.
1394 ** checksums - the statement journal does not.
1395 ** 2074 **
1396 ** If the page number of the page record read from the (sub-)journal file 2075 ** If the page number of the page record read from the (sub-)journal file
1397 ** is greater than the current value of Pager.dbSize, then playback is 2076 ** is greater than the current value of Pager.dbSize, then playback is
1398 ** skipped and SQLITE_OK is returned. 2077 ** skipped and SQLITE_OK is returned.
1399 ** 2078 **
1400 ** If pDone is not NULL, then it is a record of pages that have already 2079 ** If pDone is not NULL, then it is a record of pages that have already
1401 ** been played back. If the page at *pOffset has already been played back 2080 ** been played back. If the page at *pOffset has already been played back
1402 ** (if the corresponding pDone bit is set) then skip the playback. 2081 ** (if the corresponding pDone bit is set) then skip the playback.
1403 ** Make sure the pDone bit corresponding to the *pOffset page is set 2082 ** Make sure the pDone bit corresponding to the *pOffset page is set
1404 ** prior to returning. 2083 ** prior to returning.
(...skipping 11 matching lines...) Expand all
1416 ** and the checksum field does not match the record content. 2095 ** and the checksum field does not match the record content.
1417 ** 2096 **
1418 ** Neither of these two scenarios are possible during a savepoint rollback. 2097 ** Neither of these two scenarios are possible during a savepoint rollback.
1419 ** 2098 **
1420 ** If this is a savepoint rollback, then memory may have to be dynamically 2099 ** If this is a savepoint rollback, then memory may have to be dynamically
1421 ** allocated by this function. If this is the case and an allocation fails, 2100 ** allocated by this function. If this is the case and an allocation fails,
1422 ** SQLITE_NOMEM is returned. 2101 ** SQLITE_NOMEM is returned.
1423 */ 2102 */
1424 static int pager_playback_one_page( 2103 static int pager_playback_one_page(
1425 Pager *pPager, /* The pager being played back */ 2104 Pager *pPager, /* The pager being played back */
2105 i64 *pOffset, /* Offset of record to playback */
2106 Bitvec *pDone, /* Bitvec of pages already played back */
1426 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */ 2107 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
1427 int isUnsync, /* True if reading from unsynced main journal */ 2108 int isSavepnt /* True for a savepoint rollback */
1428 i64 *pOffset, /* Offset of record to playback */
1429 int isSavepnt, /* True for a savepoint rollback */
1430 Bitvec *pDone /* Bitvec of pages already played back */
1431 ){ 2109 ){
1432 int rc; 2110 int rc;
1433 PgHdr *pPg; /* An existing page in the cache */ 2111 PgHdr *pPg; /* An existing page in the cache */
1434 Pgno pgno; /* The page number of a page in journal */ 2112 Pgno pgno; /* The page number of a page in journal */
1435 u32 cksum; /* Checksum used for sanity checking */ 2113 u32 cksum; /* Checksum used for sanity checking */
1436 u8 *aData; /* Temporary storage for the page */ 2114 char *aData; /* Temporary storage for the page */
1437 sqlite3_file *jfd; /* The file descriptor for the journal file */ 2115 sqlite3_file *jfd; /* The file descriptor for the journal file */
2116 int isSynced; /* True if journal page is synced */
1438 2117
1439 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */ 2118 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
1440 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */ 2119 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
1441 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */ 2120 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
1442 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */ 2121 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
1443 2122
1444 aData = (u8*)pPager->pTmpSpace; 2123 aData = pPager->pTmpSpace;
1445 assert( aData ); /* Temp storage must have already been allocated */ 2124 assert( aData ); /* Temp storage must have already been allocated */
2125 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
2126
2127 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
2128 ** or savepoint rollback done at the request of the caller) or this is
2129 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
2130 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
2131 ** only reads from the main journal, not the sub-journal.
2132 */
2133 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
2134 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
2135 );
2136 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
1446 2137
1447 /* Read the page number and page data from the journal or sub-journal 2138 /* Read the page number and page data from the journal or sub-journal
1448 ** file. Return an error code to the caller if an IO error occurs. 2139 ** file. Return an error code to the caller if an IO error occurs.
1449 */ 2140 */
1450 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd; 2141 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
1451 rc = read32bits(jfd, *pOffset, &pgno); 2142 rc = read32bits(jfd, *pOffset, &pgno);
1452 if( rc!=SQLITE_OK ) return rc; 2143 if( rc!=SQLITE_OK ) return rc;
1453 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, (*pOffset)+4); 2144 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
1454 if( rc!=SQLITE_OK ) return rc; 2145 if( rc!=SQLITE_OK ) return rc;
1455 *pOffset += pPager->pageSize + 4 + isMainJrnl*4; 2146 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
1456 2147
1457 /* Sanity checking on the page. This is more important that I originally 2148 /* Sanity checking on the page. This is more important that I originally
1458 ** thought. If a power failure occurs while the journal is being written, 2149 ** thought. If a power failure occurs while the journal is being written,
1459 ** it could cause invalid data to be written into the journal. We need to 2150 ** it could cause invalid data to be written into the journal. We need to
1460 ** detect this invalid data (with high probability) and ignore it. 2151 ** detect this invalid data (with high probability) and ignore it.
1461 */ 2152 */
1462 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ 2153 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
1463 assert( !isSavepnt ); 2154 assert( !isSavepnt );
1464 return SQLITE_DONE; 2155 return SQLITE_DONE;
1465 } 2156 }
1466 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){ 2157 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
1467 return SQLITE_OK; 2158 return SQLITE_OK;
1468 } 2159 }
1469 if( isMainJrnl ){ 2160 if( isMainJrnl ){
1470 rc = read32bits(jfd, (*pOffset)-4, &cksum); 2161 rc = read32bits(jfd, (*pOffset)-4, &cksum);
1471 if( rc ) return rc; 2162 if( rc ) return rc;
1472 if( !isSavepnt && pager_cksum(pPager, aData)!=cksum ){ 2163 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
1473 return SQLITE_DONE; 2164 return SQLITE_DONE;
1474 } 2165 }
1475 } 2166 }
1476 2167
2168 /* If this page has already been played by before during the current
2169 ** rollback, then don't bother to play it back again.
2170 */
1477 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){ 2171 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
1478 return rc; 2172 return rc;
1479 } 2173 }
1480 2174
1481 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE ); 2175 /* When playing back page 1, restore the nReserve setting
2176 */
2177 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
2178 pPager->nReserve = ((u8*)aData)[20];
2179 pagerReportSize(pPager);
2180 }
1482 2181
1483 /* If the pager is in RESERVED state, then there must be a copy of this 2182 /* If the pager is in CACHEMOD state, then there must be a copy of this
1484 ** page in the pager cache. In this case just update the pager cache, 2183 ** page in the pager cache. In this case just update the pager cache,
1485 ** not the database file. The page is left marked dirty in this case. 2184 ** not the database file. The page is left marked dirty in this case.
1486 ** 2185 **
1487 ** An exception to the above rule: If the database is in no-sync mode 2186 ** An exception to the above rule: If the database is in no-sync mode
1488 ** and a page is moved during an incremental vacuum then the page may 2187 ** and a page is moved during an incremental vacuum then the page may
1489 ** not be in the pager cache. Later: if a malloc() or IO error occurs 2188 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1490 ** during a Movepage() call, then the page may not be in the cache 2189 ** during a Movepage() call, then the page may not be in the cache
1491 ** either. So the condition described in the above paragraph is not 2190 ** either. So the condition described in the above paragraph is not
1492 ** assert()able. 2191 ** assert()able.
1493 ** 2192 **
1494 ** If in EXCLUSIVE state, then we update the pager cache if it exists 2193 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
1495 ** and the main file. The page is then marked not dirty. 2194 ** pager cache if it exists and the main file. The page is then marked
2195 ** not dirty. Since this code is only executed in PAGER_OPEN state for
2196 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
2197 ** if the pager is in OPEN state.
1496 ** 2198 **
1497 ** Ticket #1171: The statement journal might contain page content that is 2199 ** Ticket #1171: The statement journal might contain page content that is
1498 ** different from the page content at the start of the transaction. 2200 ** different from the page content at the start of the transaction.
1499 ** This occurs when a page is changed prior to the start of a statement 2201 ** This occurs when a page is changed prior to the start of a statement
1500 ** then changed again within the statement. When rolling back such a 2202 ** then changed again within the statement. When rolling back such a
1501 ** statement we must not write to the original database unless we know 2203 ** statement we must not write to the original database unless we know
1502 ** for certain that original page contents are synced into the main rollback 2204 ** for certain that original page contents are synced into the main rollback
1503 ** journal. Otherwise, a power loss might leave modified data in the 2205 ** journal. Otherwise, a power loss might leave modified data in the
1504 ** database file without an entry in the rollback journal that can 2206 ** database file without an entry in the rollback journal that can
1505 ** restore the database to its original form. Two conditions must be 2207 ** restore the database to its original form. Two conditions must be
1506 ** met before writing to the database files. (1) the database must be 2208 ** met before writing to the database files. (1) the database must be
1507 ** locked. (2) we know that the original page content is fully synced 2209 ** locked. (2) we know that the original page content is fully synced
1508 ** in the main journal either because the page is not in cache or else 2210 ** in the main journal either because the page is not in cache or else
1509 ** the page is marked as needSync==0. 2211 ** the page is marked as needSync==0.
1510 ** 2212 **
1511 ** 2008-04-14: When attempting to vacuum a corrupt database file, it 2213 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1512 ** is possible to fail a statement on a database that does not yet exist. 2214 ** is possible to fail a statement on a database that does not yet exist.
1513 ** Do not attempt to write if database file has never been opened. 2215 ** Do not attempt to write if database file has never been opened.
1514 */ 2216 */
1515 pPg = pager_lookup(pPager, pgno); 2217 if( pagerUseWal(pPager) ){
2218 pPg = 0;
2219 }else{
2220 pPg = pager_lookup(pPager, pgno);
2221 }
1516 assert( pPg || !MEMDB ); 2222 assert( pPg || !MEMDB );
2223 assert( pPager->eState!=PAGER_OPEN || pPg==0 );
1517 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", 2224 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
1518 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData), 2225 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
1519 (isMainJrnl?"main-journal":"sub-journal") 2226 (isMainJrnl?"main-journal":"sub-journal")
1520 )); 2227 ));
1521 if( (pPager->state>=PAGER_EXCLUSIVE) 2228 if( isMainJrnl ){
1522 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC)) 2229 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
1523 && isOpen(pPager->fd) 2230 }else{
1524 && !isUnsync 2231 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
2232 }
2233 if( isOpen(pPager->fd)
2234 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
2235 && isSynced
1525 ){ 2236 ){
1526 i64 ofst = (pgno-1)*(i64)pPager->pageSize; 2237 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
1527 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst); 2238 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
2239 assert( !pagerUseWal(pPager) );
2240 rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
1528 if( pgno>pPager->dbFileSize ){ 2241 if( pgno>pPager->dbFileSize ){
1529 pPager->dbFileSize = pgno; 2242 pPager->dbFileSize = pgno;
1530 } 2243 }
1531 if( pPager->pBackup ){ 2244 if( pPager->pBackup ){
1532 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM); 2245 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
1533 sqlite3BackupUpdate(pPager->pBackup, pgno, aData); 2246 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
1534 CODEC1(pPager, aData, pgno, 0, rc=SQLITE_NOMEM); 2247 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
1535 } 2248 }
1536 }else if( !isMainJrnl && pPg==0 ){ 2249 }else if( !isMainJrnl && pPg==0 ){
1537 /* If this is a rollback of a savepoint and data was not written to 2250 /* If this is a rollback of a savepoint and data was not written to
1538 ** the database and the page is not in-memory, there is a potential 2251 ** the database and the page is not in-memory, there is a potential
1539 ** problem. When the page is next fetched by the b-tree layer, it 2252 ** problem. When the page is next fetched by the b-tree layer, it
1540 ** will be read from the database file, which may or may not be 2253 ** will be read from the database file, which may or may not be
1541 ** current. 2254 ** current.
1542 ** 2255 **
1543 ** There are a couple of different ways this can happen. All are quite 2256 ** There are a couple of different ways this can happen. All are quite
1544 ** obscure. When running in synchronous mode, this can only happen 2257 ** obscure. When running in synchronous mode, this can only happen
1545 ** if the page is on the free-list at the start of the transaction, then 2258 ** if the page is on the free-list at the start of the transaction, then
1546 ** populated, then moved using sqlite3PagerMovepage(). 2259 ** populated, then moved using sqlite3PagerMovepage().
1547 ** 2260 **
1548 ** The solution is to add an in-memory page to the cache containing 2261 ** The solution is to add an in-memory page to the cache containing
1549 ** the data just read from the sub-journal. Mark the page as dirty 2262 ** the data just read from the sub-journal. Mark the page as dirty
1550 ** and if the pager requires a journal-sync, then mark the page as 2263 ** and if the pager requires a journal-sync, then mark the page as
1551 ** requiring a journal-sync before it is written. 2264 ** requiring a journal-sync before it is written.
1552 */ 2265 */
1553 assert( isSavepnt ); 2266 assert( isSavepnt );
1554 if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1))!=SQLITE_OK ){ 2267 assert( pPager->doNotSpill==0 );
1555 return rc; 2268 pPager->doNotSpill++;
1556 } 2269 rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
2270 assert( pPager->doNotSpill==1 );
2271 pPager->doNotSpill--;
2272 if( rc!=SQLITE_OK ) return rc;
1557 pPg->flags &= ~PGHDR_NEED_READ; 2273 pPg->flags &= ~PGHDR_NEED_READ;
1558 sqlite3PcacheMakeDirty(pPg); 2274 sqlite3PcacheMakeDirty(pPg);
1559 } 2275 }
1560 if( pPg ){ 2276 if( pPg ){
1561 /* No page should ever be explicitly rolled back that is in use, except 2277 /* No page should ever be explicitly rolled back that is in use, except
1562 ** for page 1 which is held in use in order to keep the lock on the 2278 ** for page 1 which is held in use in order to keep the lock on the
1563 ** database active. However such a page may be rolled back as a result 2279 ** database active. However such a page may be rolled back as a result
1564 ** of an internal error resulting in an automatic call to 2280 ** of an internal error resulting in an automatic call to
1565 ** sqlite3PagerRollback(). 2281 ** sqlite3PagerRollback().
1566 */ 2282 */
1567 void *pData; 2283 void *pData;
1568 pData = pPg->pData; 2284 pData = pPg->pData;
1569 memcpy(pData, aData, pPager->pageSize); 2285 memcpy(pData, (u8*)aData, pPager->pageSize);
1570 pPager->xReiniter(pPg); 2286 pPager->xReiniter(pPg);
1571 if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){ 2287 if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
1572 /* If the contents of this page were just restored from the main 2288 /* If the contents of this page were just restored from the main
1573 ** journal file, then its content must be as they were when the 2289 ** journal file, then its content must be as they were when the
1574 ** transaction was first opened. In this case we can mark the page 2290 ** transaction was first opened. In this case we can mark the page
1575 ** as clean, since there will be no need to write it out to the. 2291 ** as clean, since there will be no need to write it out to the
2292 ** database.
1576 ** 2293 **
1577 ** There is one exception to this rule. If the page is being rolled 2294 ** There is one exception to this rule. If the page is being rolled
1578 ** back as part of a savepoint (or statement) rollback from an 2295 ** back as part of a savepoint (or statement) rollback from an
1579 ** unsynced portion of the main journal file, then it is not safe 2296 ** unsynced portion of the main journal file, then it is not safe
1580 ** to mark the page as clean. This is because marking the page as 2297 ** to mark the page as clean. This is because marking the page as
1581 ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is 2298 ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
1582 ** already in the journal file (recorded in Pager.pInJournal) and 2299 ** already in the journal file (recorded in Pager.pInJournal) and
1583 ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to 2300 ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
1584 ** again within this transaction, it will be marked as dirty but 2301 ** again within this transaction, it will be marked as dirty but
1585 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially 2302 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
1586 ** be written out into the database file before its journal file 2303 ** be written out into the database file before its journal file
1587 ** segment is synced. If a crash occurs during or following this, 2304 ** segment is synced. If a crash occurs during or following this,
1588 ** database corruption may ensue. 2305 ** database corruption may ensue.
1589 */ 2306 */
2307 assert( !pagerUseWal(pPager) );
1590 sqlite3PcacheMakeClean(pPg); 2308 sqlite3PcacheMakeClean(pPg);
1591 } 2309 }
1592 #ifdef SQLITE_CHECK_PAGES 2310 pager_set_pagehash(pPg);
1593 pPg->pageHash = pager_pagehash(pPg); 2311
1594 #endif
1595 /* If this was page 1, then restore the value of Pager.dbFileVers. 2312 /* If this was page 1, then restore the value of Pager.dbFileVers.
1596 ** Do this before any decoding. */ 2313 ** Do this before any decoding. */
1597 if( pgno==1 ){ 2314 if( pgno==1 ){
1598 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); 2315 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
1599 } 2316 }
1600 2317
1601 /* Decode the page just read from disk */ 2318 /* Decode the page just read from disk */
1602 CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM); 2319 CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
1603 sqlite3PcacheRelease(pPg); 2320 sqlite3PcacheRelease(pPg);
1604 } 2321 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1648 ** a couple of kilobytes or so - potentially larger than the page 2365 ** a couple of kilobytes or so - potentially larger than the page
1649 ** size. 2366 ** size.
1650 */ 2367 */
1651 static int pager_delmaster(Pager *pPager, const char *zMaster){ 2368 static int pager_delmaster(Pager *pPager, const char *zMaster){
1652 sqlite3_vfs *pVfs = pPager->pVfs; 2369 sqlite3_vfs *pVfs = pPager->pVfs;
1653 int rc; /* Return code */ 2370 int rc; /* Return code */
1654 sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */ 2371 sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
1655 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */ 2372 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
1656 char *zMasterJournal = 0; /* Contents of master journal file */ 2373 char *zMasterJournal = 0; /* Contents of master journal file */
1657 i64 nMasterJournal; /* Size of master journal file */ 2374 i64 nMasterJournal; /* Size of master journal file */
2375 char *zJournal; /* Pointer to one journal within MJ file */
2376 char *zMasterPtr; /* Space to hold MJ filename from a journal file */
2377 int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */
1658 2378
1659 /* Allocate space for both the pJournal and pMaster file descriptors. 2379 /* Allocate space for both the pJournal and pMaster file descriptors.
1660 ** If successful, open the master journal file for reading. 2380 ** If successful, open the master journal file for reading.
1661 */ 2381 */
1662 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2); 2382 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
1663 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); 2383 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
1664 if( !pMaster ){ 2384 if( !pMaster ){
1665 rc = SQLITE_NOMEM; 2385 rc = SQLITE_NOMEM;
1666 }else{ 2386 }else{
1667 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); 2387 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1668 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); 2388 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
1669 } 2389 }
1670 if( rc!=SQLITE_OK ) goto delmaster_out; 2390 if( rc!=SQLITE_OK ) goto delmaster_out;
1671 2391
2392 /* Load the entire master journal file into space obtained from
2393 ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain
2394 ** sufficient space (in zMasterPtr) to hold the names of master
2395 ** journal files extracted from regular rollback-journals.
2396 */
1672 rc = sqlite3OsFileSize(pMaster, &nMasterJournal); 2397 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
1673 if( rc!=SQLITE_OK ) goto delmaster_out; 2398 if( rc!=SQLITE_OK ) goto delmaster_out;
2399 nMasterPtr = pVfs->mxPathname+1;
2400 zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
2401 if( !zMasterJournal ){
2402 rc = SQLITE_NOMEM;
2403 goto delmaster_out;
2404 }
2405 zMasterPtr = &zMasterJournal[nMasterJournal+1];
2406 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
2407 if( rc!=SQLITE_OK ) goto delmaster_out;
2408 zMasterJournal[nMasterJournal] = 0;
1674 2409
1675 if( nMasterJournal>0 ){ 2410 zJournal = zMasterJournal;
1676 char *zJournal; 2411 while( (zJournal-zMasterJournal)<nMasterJournal ){
1677 char *zMasterPtr = 0; 2412 int exists;
1678 int nMasterPtr = pVfs->mxPathname+1; 2413 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1679 2414 if( rc!=SQLITE_OK ){
1680 /* Load the entire master journal file into space obtained from
1681 ** sqlite3_malloc() and pointed to by zMasterJournal.
1682 */
1683 zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
1684 if( !zMasterJournal ){
1685 rc = SQLITE_NOMEM;
1686 goto delmaster_out; 2415 goto delmaster_out;
1687 } 2416 }
1688 zMasterPtr = &zMasterJournal[nMasterJournal+1]; 2417 if( exists ){
1689 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0); 2418 /* One of the journals pointed to by the master journal exists.
1690 if( rc!=SQLITE_OK ) goto delmaster_out; 2419 ** Open it and check if it points at the master journal. If
1691 zMasterJournal[nMasterJournal] = 0; 2420 ** so, return without deleting the master journal file.
1692 2421 */
1693 zJournal = zMasterJournal; 2422 int c;
1694 while( (zJournal-zMasterJournal)<nMasterJournal ){ 2423 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1695 int exists; 2424 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
1696 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1697 if( rc!=SQLITE_OK ){ 2425 if( rc!=SQLITE_OK ){
1698 goto delmaster_out; 2426 goto delmaster_out;
1699 } 2427 }
1700 if( exists ){
1701 /* One of the journals pointed to by the master journal exists.
1702 ** Open it and check if it points at the master journal. If
1703 ** so, return without deleting the master journal file.
1704 */
1705 int c;
1706 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1707 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
1708 if( rc!=SQLITE_OK ){
1709 goto delmaster_out;
1710 }
1711 2428
1712 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr); 2429 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
1713 sqlite3OsClose(pJournal); 2430 sqlite3OsClose(pJournal);
1714 if( rc!=SQLITE_OK ){ 2431 if( rc!=SQLITE_OK ){
1715 goto delmaster_out; 2432 goto delmaster_out;
1716 } 2433 }
1717 2434
1718 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0; 2435 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
1719 if( c ){ 2436 if( c ){
1720 /* We have a match. Do not delete the master journal file. */ 2437 /* We have a match. Do not delete the master journal file. */
1721 goto delmaster_out; 2438 goto delmaster_out;
1722 }
1723 } 2439 }
1724 zJournal += (sqlite3Strlen30(zJournal)+1);
1725 } 2440 }
2441 zJournal += (sqlite3Strlen30(zJournal)+1);
1726 } 2442 }
1727 2443
2444 sqlite3OsClose(pMaster);
1728 rc = sqlite3OsDelete(pVfs, zMaster, 0); 2445 rc = sqlite3OsDelete(pVfs, zMaster, 0);
1729 2446
1730 delmaster_out: 2447 delmaster_out:
1731 if( zMasterJournal ){ 2448 sqlite3_free(zMasterJournal);
1732 sqlite3_free(zMasterJournal);
1733 }
1734 if( pMaster ){ 2449 if( pMaster ){
1735 sqlite3OsClose(pMaster); 2450 sqlite3OsClose(pMaster);
1736 assert( !isOpen(pJournal) ); 2451 assert( !isOpen(pJournal) );
2452 sqlite3_free(pMaster);
1737 } 2453 }
1738 sqlite3_free(pMaster);
1739 return rc; 2454 return rc;
1740 } 2455 }
1741 2456
1742 2457
1743 /* 2458 /*
1744 ** This function is used to change the actual size of the database 2459 ** This function is used to change the actual size of the database
1745 ** file in the file-system. This only happens when committing a transaction, 2460 ** file in the file-system. This only happens when committing a transaction,
1746 ** or rolling back a transaction (including rolling back a hot-journal). 2461 ** or rolling back a transaction (including rolling back a hot-journal).
1747 ** 2462 **
1748 ** If the main database file is not open, or an exclusive lock is not 2463 ** If the main database file is not open, or the pager is not in either
1749 ** held, this function is a no-op. Otherwise, the size of the file is 2464 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
1750 ** changed to nPage pages (nPage*pPager->pageSize bytes). If the file 2465 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
1751 ** on disk is currently larger than nPage pages, then use the VFS 2466 ** If the file on disk is currently larger than nPage pages, then use the VFS
1752 ** xTruncate() method to truncate it. 2467 ** xTruncate() method to truncate it.
1753 ** 2468 **
1754 ** Or, it might might be the case that the file on disk is smaller than 2469 ** Or, it might might be the case that the file on disk is smaller than
1755 ** nPage pages. Some operating system implementations can get confused if 2470 ** nPage pages. Some operating system implementations can get confused if
1756 ** you try to truncate a file to some size that is larger than it 2471 ** you try to truncate a file to some size that is larger than it
1757 ** currently is, so detect this case and write a single zero byte to 2472 ** currently is, so detect this case and write a single zero byte to
1758 ** the end of the new file instead. 2473 ** the end of the new file instead.
1759 ** 2474 **
1760 ** If successful, return SQLITE_OK. If an IO error occurs while modifying 2475 ** If successful, return SQLITE_OK. If an IO error occurs while modifying
1761 ** the database file, return the error code to the caller. 2476 ** the database file, return the error code to the caller.
1762 */ 2477 */
1763 static int pager_truncate(Pager *pPager, Pgno nPage){ 2478 static int pager_truncate(Pager *pPager, Pgno nPage){
1764 int rc = SQLITE_OK; 2479 int rc = SQLITE_OK;
1765 if( pPager->state>=PAGER_EXCLUSIVE && isOpen(pPager->fd) ){ 2480 assert( pPager->eState!=PAGER_ERROR );
2481 assert( pPager->eState!=PAGER_READER );
2482
2483 if( isOpen(pPager->fd)
2484 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
2485 ){
1766 i64 currentSize, newSize; 2486 i64 currentSize, newSize;
2487 assert( pPager->eLock==EXCLUSIVE_LOCK );
1767 /* TODO: Is it safe to use Pager.dbFileSize here? */ 2488 /* TODO: Is it safe to use Pager.dbFileSize here? */
1768 rc = sqlite3OsFileSize(pPager->fd, &currentSize); 2489 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1769 newSize = pPager->pageSize*(i64)nPage; 2490 newSize = pPager->pageSize*(i64)nPage;
1770 if( rc==SQLITE_OK && currentSize!=newSize ){ 2491 if( rc==SQLITE_OK && currentSize!=newSize ){
1771 if( currentSize>newSize ){ 2492 if( currentSize>newSize ){
1772 rc = sqlite3OsTruncate(pPager->fd, newSize); 2493 rc = sqlite3OsTruncate(pPager->fd, newSize);
1773 }else{ 2494 }else{
1774 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1); 2495 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1775 } 2496 }
1776 if( rc==SQLITE_OK ){ 2497 if( rc==SQLITE_OK ){
1777 pPager->dbFileSize = nPage; 2498 pPager->dbFileSize = nPage;
1778 } 2499 }
1779 } 2500 }
1780 } 2501 }
1781 return rc; 2502 return rc;
1782 } 2503 }
1783 2504
1784 /* 2505 /*
1785 ** Set the value of the Pager.sectorSize variable for the given 2506 ** Set the value of the Pager.sectorSize variable for the given
1786 ** pager based on the value returned by the xSectorSize method 2507 ** pager based on the value returned by the xSectorSize method
1787 ** of the open database file. The sector size will be used used 2508 ** of the open database file. The sector size will be used used
1788 ** to determine the size and alignment of journal header and 2509 ** to determine the size and alignment of journal header and
1789 ** master journal pointers within created journal files. 2510 ** master journal pointers within created journal files.
1790 ** 2511 **
1791 ** For temporary files the effective sector size is always 512 bytes. 2512 ** For temporary files the effective sector size is always 512 bytes.
1792 ** 2513 **
1793 ** Otherwise, for non-temporary files, the effective sector size is 2514 ** Otherwise, for non-temporary files, the effective sector size is
1794 ** the value returned by the xSectorSize() method rounded up to 512 if 2515 ** the value returned by the xSectorSize() method rounded up to 32 if
1795 ** it is less than 512, or rounded down to MAX_SECTOR_SIZE if it 2516 ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
1796 ** is greater than MAX_SECTOR_SIZE. 2517 ** is greater than MAX_SECTOR_SIZE.
1797 */ 2518 */
1798 static void setSectorSize(Pager *pPager){ 2519 static void setSectorSize(Pager *pPager){
1799 assert( isOpen(pPager->fd) || pPager->tempFile ); 2520 assert( isOpen(pPager->fd) || pPager->tempFile );
1800 2521
1801 if( !pPager->tempFile ){ 2522 if( !pPager->tempFile ){
1802 /* Sector size doesn't matter for temporary files. Also, the file 2523 /* Sector size doesn't matter for temporary files. Also, the file
1803 ** may not have been opened yet, in which case the OsSectorSize() 2524 ** may not have been opened yet, in which case the OsSectorSize()
1804 ** call will segfault. 2525 ** call will segfault.
1805 */ 2526 */
1806 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd); 2527 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1807 } 2528 }
1808 if( pPager->sectorSize<512 ){ 2529 if( pPager->sectorSize<32 ){
1809 pPager->sectorSize = 512; 2530 pPager->sectorSize = 512;
1810 } 2531 }
1811 if( pPager->sectorSize>MAX_SECTOR_SIZE ){ 2532 if( pPager->sectorSize>MAX_SECTOR_SIZE ){
1812 assert( MAX_SECTOR_SIZE>=512 ); 2533 assert( MAX_SECTOR_SIZE>=512 );
1813 pPager->sectorSize = MAX_SECTOR_SIZE; 2534 pPager->sectorSize = MAX_SECTOR_SIZE;
1814 } 2535 }
1815 } 2536 }
1816 2537
1817 /* 2538 /*
1818 ** Playback the journal and thus restore the database file to 2539 ** Playback the journal and thus restore the database file to
1819 ** the state it was in before we started making changes. 2540 ** the state it was in before we started making changes.
1820 ** 2541 **
1821 ** The journal file format is as follows: 2542 ** The journal file format is as follows:
1822 ** 2543 **
1823 ** (1) 8 byte prefix. A copy of aJournalMagic[]. 2544 ** (1) 8 byte prefix. A copy of aJournalMagic[].
1824 ** (2) 4 byte big-endian integer which is the number of valid page records 2545 ** (2) 4 byte big-endian integer which is the number of valid page records
1825 ** in the journal. If this value is 0xffffffff, then compute the 2546 ** in the journal. If this value is 0xffffffff, then compute the
1826 ** number of page records from the journal size. 2547 ** number of page records from the journal size.
1827 ** (3) 4 byte big-endian integer which is the initial value for the 2548 ** (3) 4 byte big-endian integer which is the initial value for the
1828 ** sanity checksum. 2549 ** sanity checksum.
1829 ** (4) 4 byte integer which is the number of pages to truncate the 2550 ** (4) 4 byte integer which is the number of pages to truncate the
1830 ** database to during a rollback. 2551 ** database to during a rollback.
1831 ** (5) 4 byte big-endian integer which is the sector size. The header 2552 ** (5) 4 byte big-endian integer which is the sector size. The header
1832 ** is this many bytes in size. 2553 ** is this many bytes in size.
1833 ** (6) 4 byte big-endian integer which is the page case. 2554 ** (6) 4 byte big-endian integer which is the page size.
1834 ** (7) 4 byte integer which is the number of bytes in the master journal 2555 ** (7) zero padding out to the next sector size.
1835 ** name. The value may be zero (indicate that there is no master 2556 ** (8) Zero or more pages instances, each as follows:
1836 ** journal.)
1837 ** (8) N bytes of the master journal name. The name will be nul-terminated
1838 ** and might be shorter than the value read from (5). If the first byte
1839 ** of the name is \000 then there is no master journal. The master
1840 ** journal name is stored in UTF-8.
1841 ** (9) Zero or more pages instances, each as follows:
1842 ** + 4 byte page number. 2557 ** + 4 byte page number.
1843 ** + pPager->pageSize bytes of data. 2558 ** + pPager->pageSize bytes of data.
1844 ** + 4 byte checksum 2559 ** + 4 byte checksum
1845 ** 2560 **
1846 ** When we speak of the journal header, we mean the first 8 items above. 2561 ** When we speak of the journal header, we mean the first 7 items above.
1847 ** Each entry in the journal is an instance of the 9th item. 2562 ** Each entry in the journal is an instance of the 8th item.
1848 ** 2563 **
1849 ** Call the value from the second bullet "nRec". nRec is the number of 2564 ** Call the value from the second bullet "nRec". nRec is the number of
1850 ** valid page entries in the journal. In most cases, you can compute the 2565 ** valid page entries in the journal. In most cases, you can compute the
1851 ** value of nRec from the size of the journal file. But if a power 2566 ** value of nRec from the size of the journal file. But if a power
1852 ** failure occurred while the journal was being written, it could be the 2567 ** failure occurred while the journal was being written, it could be the
1853 ** case that the size of the journal file had already been increased but 2568 ** case that the size of the journal file had already been increased but
1854 ** the extra entries had not yet made it safely to disk. In such a case, 2569 ** the extra entries had not yet made it safely to disk. In such a case,
1855 ** the value of nRec computed from the file size would be too large. For 2570 ** the value of nRec computed from the file size would be too large. For
1856 ** that reason, we always use the nRec value in the header. 2571 ** that reason, we always use the nRec value in the header.
1857 ** 2572 **
(...skipping 28 matching lines...) Expand all
1886 int rc; /* Result code of a subroutine */ 2601 int rc; /* Result code of a subroutine */
1887 int res = 1; /* Value returned by sqlite3OsAccess() */ 2602 int res = 1; /* Value returned by sqlite3OsAccess() */
1888 char *zMaster = 0; /* Name of master journal file if any */ 2603 char *zMaster = 0; /* Name of master journal file if any */
1889 int needPagerReset; /* True to reset page prior to first page rollback */ 2604 int needPagerReset; /* True to reset page prior to first page rollback */
1890 2605
1891 /* Figure out how many records are in the journal. Abort early if 2606 /* Figure out how many records are in the journal. Abort early if
1892 ** the journal is empty. 2607 ** the journal is empty.
1893 */ 2608 */
1894 assert( isOpen(pPager->jfd) ); 2609 assert( isOpen(pPager->jfd) );
1895 rc = sqlite3OsFileSize(pPager->jfd, &szJ); 2610 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
1896 if( rc!=SQLITE_OK || szJ==0 ){ 2611 if( rc!=SQLITE_OK ){
1897 goto end_playback; 2612 goto end_playback;
1898 } 2613 }
1899 2614
1900 /* Read the master journal name from the journal, if it is present. 2615 /* Read the master journal name from the journal, if it is present.
1901 ** If a master journal file name is specified, but the file is not 2616 ** If a master journal file name is specified, but the file is not
1902 ** present on disk, then the journal is not hot and does not need to be 2617 ** present on disk, then the journal is not hot and does not need to be
1903 ** played back. 2618 ** played back.
1904 ** 2619 **
1905 ** TODO: Technically the following is an error because it assumes that 2620 ** TODO: Technically the following is an error because it assumes that
1906 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that 2621 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
(...skipping 11 matching lines...) Expand all
1918 goto end_playback; 2633 goto end_playback;
1919 } 2634 }
1920 pPager->journalOff = 0; 2635 pPager->journalOff = 0;
1921 needPagerReset = isHot; 2636 needPagerReset = isHot;
1922 2637
1923 /* This loop terminates either when a readJournalHdr() or 2638 /* This loop terminates either when a readJournalHdr() or
1924 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error 2639 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
1925 ** occurs. 2640 ** occurs.
1926 */ 2641 */
1927 while( 1 ){ 2642 while( 1 ){
1928 int isUnsync = 0;
1929
1930 /* Read the next journal header from the journal file. If there are 2643 /* Read the next journal header from the journal file. If there are
1931 ** not enough bytes left in the journal file for a complete header, or 2644 ** not enough bytes left in the journal file for a complete header, or
1932 ** it is corrupted, then a process must of failed while writing it. 2645 ** it is corrupted, then a process must have failed while writing it.
1933 ** This indicates nothing more needs to be rolled back. 2646 ** This indicates nothing more needs to be rolled back.
1934 */ 2647 */
1935 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg); 2648 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
1936 if( rc!=SQLITE_OK ){ 2649 if( rc!=SQLITE_OK ){
1937 if( rc==SQLITE_DONE ){ 2650 if( rc==SQLITE_DONE ){
1938 rc = SQLITE_OK; 2651 rc = SQLITE_OK;
1939 } 2652 }
1940 goto end_playback; 2653 goto end_playback;
1941 } 2654 }
1942 2655
(...skipping 17 matching lines...) Expand all
1960 ** When rolling back a hot journal, nRec==0 always means that the next 2673 ** When rolling back a hot journal, nRec==0 always means that the next
1961 ** chunk of the journal contains zero pages to be rolled back. But 2674 ** chunk of the journal contains zero pages to be rolled back. But
1962 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in 2675 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
1963 ** the journal, it means that the journal might contain additional 2676 ** the journal, it means that the journal might contain additional
1964 ** pages that need to be rolled back and that the number of pages 2677 ** pages that need to be rolled back and that the number of pages
1965 ** should be computed based on the journal file size. 2678 ** should be computed based on the journal file size.
1966 */ 2679 */
1967 if( nRec==0 && !isHot && 2680 if( nRec==0 && !isHot &&
1968 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){ 2681 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
1969 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager)); 2682 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
1970 isUnsync = 1;
1971 } 2683 }
1972 2684
1973 /* If this is the first header read from the journal, truncate the 2685 /* If this is the first header read from the journal, truncate the
1974 ** database file back to its original size. 2686 ** database file back to its original size.
1975 */ 2687 */
1976 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){ 2688 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
1977 rc = pager_truncate(pPager, mxPg); 2689 rc = pager_truncate(pPager, mxPg);
1978 if( rc!=SQLITE_OK ){ 2690 if( rc!=SQLITE_OK ){
1979 goto end_playback; 2691 goto end_playback;
1980 } 2692 }
1981 pPager->dbSize = mxPg; 2693 pPager->dbSize = mxPg;
1982 } 2694 }
1983 2695
1984 /* Copy original pages out of the journal and back into the 2696 /* Copy original pages out of the journal and back into the
1985 ** database file and/or page cache. 2697 ** database file and/or page cache.
1986 */ 2698 */
1987 for(u=0; u<nRec; u++){ 2699 for(u=0; u<nRec; u++){
1988 if( needPagerReset ){ 2700 if( needPagerReset ){
1989 pager_reset(pPager); 2701 pager_reset(pPager);
1990 needPagerReset = 0; 2702 needPagerReset = 0;
1991 } 2703 }
1992 rc = pager_playback_one_page(pPager,1,isUnsync,&pPager->journalOff,0,0); 2704 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
1993 if( rc!=SQLITE_OK ){ 2705 if( rc!=SQLITE_OK ){
1994 if( rc==SQLITE_DONE ){ 2706 if( rc==SQLITE_DONE ){
1995 rc = SQLITE_OK; 2707 rc = SQLITE_OK;
1996 pPager->journalOff = szJ; 2708 pPager->journalOff = szJ;
1997 break; 2709 break;
2710 }else if( rc==SQLITE_IOERR_SHORT_READ ){
2711 /* If the journal has been truncated, simply stop reading and
2712 ** processing the journal. This might happen if the journal was
2713 ** not completely written and synced prior to a crash. In that
2714 ** case, the database should have never been written in the
2715 ** first place so it is OK to simply abandon the rollback. */
2716 rc = SQLITE_OK;
2717 goto end_playback;
1998 }else{ 2718 }else{
1999 /* If we are unable to rollback, quit and return the error 2719 /* If we are unable to rollback, quit and return the error
2000 ** code. This will cause the pager to enter the error state 2720 ** code. This will cause the pager to enter the error state
2001 ** so that no further harm will be done. Perhaps the next 2721 ** so that no further harm will be done. Perhaps the next
2002 ** process to come along will be able to rollback the database. 2722 ** process to come along will be able to rollback the database.
2003 */ 2723 */
2004 goto end_playback; 2724 goto end_playback;
2005 } 2725 }
2006 } 2726 }
2007 } 2727 }
(...skipping 21 matching lines...) Expand all
2029 ** problems for other processes at some point in the future. So, just 2749 ** problems for other processes at some point in the future. So, just
2030 ** in case this has happened, clear the changeCountDone flag now. 2750 ** in case this has happened, clear the changeCountDone flag now.
2031 */ 2751 */
2032 pPager->changeCountDone = pPager->tempFile; 2752 pPager->changeCountDone = pPager->tempFile;
2033 2753
2034 if( rc==SQLITE_OK ){ 2754 if( rc==SQLITE_OK ){
2035 zMaster = pPager->pTmpSpace; 2755 zMaster = pPager->pTmpSpace;
2036 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); 2756 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
2037 testcase( rc!=SQLITE_OK ); 2757 testcase( rc!=SQLITE_OK );
2038 } 2758 }
2759 if( rc==SQLITE_OK && !pPager->noSync
2760 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
2761 ){
2762 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
2763 }
2039 if( rc==SQLITE_OK ){ 2764 if( rc==SQLITE_OK ){
2040 rc = pager_end_transaction(pPager, zMaster[0]!='\0'); 2765 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
2041 testcase( rc!=SQLITE_OK ); 2766 testcase( rc!=SQLITE_OK );
2042 } 2767 }
2043 if( rc==SQLITE_OK && zMaster[0] && res ){ 2768 if( rc==SQLITE_OK && zMaster[0] && res ){
2044 /* If there was a master journal and this routine will return success, 2769 /* If there was a master journal and this routine will return success,
2045 ** see if it is possible to delete the master journal. 2770 ** see if it is possible to delete the master journal.
2046 */ 2771 */
2047 rc = pager_delmaster(pPager, zMaster); 2772 rc = pager_delmaster(pPager, zMaster);
2048 testcase( rc!=SQLITE_OK ); 2773 testcase( rc!=SQLITE_OK );
2049 } 2774 }
2050 2775
2051 /* The Pager.sectorSize variable may have been updated while rolling 2776 /* The Pager.sectorSize variable may have been updated while rolling
2052 ** back a journal created by a process with a different sector size 2777 ** back a journal created by a process with a different sector size
2053 ** value. Reset it to the correct value for this process. 2778 ** value. Reset it to the correct value for this process.
2054 */ 2779 */
2055 setSectorSize(pPager); 2780 setSectorSize(pPager);
2056 return rc; 2781 return rc;
2057 } 2782 }
2058 2783
2784
2785 /*
2786 ** Read the content for page pPg out of the database file and into
2787 ** pPg->pData. A shared lock or greater must be held on the database
2788 ** file before this function is called.
2789 **
2790 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
2791 ** the value read from the database file.
2792 **
2793 ** If an IO error occurs, then the IO error is returned to the caller.
2794 ** Otherwise, SQLITE_OK is returned.
2795 */
2796 static int readDbPage(PgHdr *pPg){
2797 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
2798 Pgno pgno = pPg->pgno; /* Page number to read */
2799 int rc = SQLITE_OK; /* Return code */
2800 int isInWal = 0; /* True if page is in log file */
2801 int pgsz = pPager->pageSize; /* Number of bytes to read */
2802
2803 assert( pPager->eState>=PAGER_READER && !MEMDB );
2804 assert( isOpen(pPager->fd) );
2805
2806 if( NEVER(!isOpen(pPager->fd)) ){
2807 assert( pPager->tempFile );
2808 memset(pPg->pData, 0, pPager->pageSize);
2809 return SQLITE_OK;
2810 }
2811
2812 if( pagerUseWal(pPager) ){
2813 /* Try to pull the page from the write-ahead log. */
2814 rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
2815 }
2816 if( rc==SQLITE_OK && !isInWal ){
2817 i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
2818 rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
2819 if( rc==SQLITE_IOERR_SHORT_READ ){
2820 rc = SQLITE_OK;
2821 }
2822 }
2823
2824 if( pgno==1 ){
2825 if( rc ){
2826 /* If the read is unsuccessful, set the dbFileVers[] to something
2827 ** that will never be a valid file version. dbFileVers[] is a copy
2828 ** of bytes 24..39 of the database. Bytes 28..31 should always be
2829 ** zero or the size of the database in page. Bytes 32..35 and 35..39
2830 ** should be page numbers which are never 0xffffffff. So filling
2831 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
2832 **
2833 ** For an encrypted database, the situation is more complex: bytes
2834 ** 24..39 of the database are white noise. But the probability of
2835 ** white noising equaling 16 bytes of 0xff is vanishingly small so
2836 ** we should still be ok.
2837 */
2838 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
2839 }else{
2840 u8 *dbFileVers = &((u8*)pPg->pData)[24];
2841 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
2842 }
2843 }
2844 CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
2845
2846 PAGER_INCR(sqlite3_pager_readdb_count);
2847 PAGER_INCR(pPager->nRead);
2848 IOTRACE(("PGIN %p %d\n", pPager, pgno));
2849 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
2850 PAGERID(pPager), pgno, pager_pagehash(pPg)));
2851
2852 return rc;
2853 }
2854
2855 #ifndef SQLITE_OMIT_WAL
2856 /*
2857 ** This function is invoked once for each page that has already been
2858 ** written into the log file when a WAL transaction is rolled back.
2859 ** Parameter iPg is the page number of said page. The pCtx argument
2860 ** is actually a pointer to the Pager structure.
2861 **
2862 ** If page iPg is present in the cache, and has no outstanding references,
2863 ** it is discarded. Otherwise, if there are one or more outstanding
2864 ** references, the page content is reloaded from the database. If the
2865 ** attempt to reload content from the database is required and fails,
2866 ** return an SQLite error code. Otherwise, SQLITE_OK.
2867 */
2868 static int pagerUndoCallback(void *pCtx, Pgno iPg){
2869 int rc = SQLITE_OK;
2870 Pager *pPager = (Pager *)pCtx;
2871 PgHdr *pPg;
2872
2873 pPg = sqlite3PagerLookup(pPager, iPg);
2874 if( pPg ){
2875 if( sqlite3PcachePageRefcount(pPg)==1 ){
2876 sqlite3PcacheDrop(pPg);
2877 }else{
2878 rc = readDbPage(pPg);
2879 if( rc==SQLITE_OK ){
2880 pPager->xReiniter(pPg);
2881 }
2882 sqlite3PagerUnref(pPg);
2883 }
2884 }
2885
2886 /* Normally, if a transaction is rolled back, any backup processes are
2887 ** updated as data is copied out of the rollback journal and into the
2888 ** database. This is not generally possible with a WAL database, as
2889 ** rollback involves simply truncating the log file. Therefore, if one
2890 ** or more frames have already been written to the log (and therefore
2891 ** also copied into the backup databases) as part of this transaction,
2892 ** the backups must be restarted.
2893 */
2894 sqlite3BackupRestart(pPager->pBackup);
2895
2896 return rc;
2897 }
2898
2899 /*
2900 ** This function is called to rollback a transaction on a WAL database.
2901 */
2902 static int pagerRollbackWal(Pager *pPager){
2903 int rc; /* Return Code */
2904 PgHdr *pList; /* List of dirty pages to revert */
2905
2906 /* For all pages in the cache that are currently dirty or have already
2907 ** been written (but not committed) to the log file, do one of the
2908 ** following:
2909 **
2910 ** + Discard the cached page (if refcount==0), or
2911 ** + Reload page content from the database (if refcount>0).
2912 */
2913 pPager->dbSize = pPager->dbOrigSize;
2914 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
2915 pList = sqlite3PcacheDirtyList(pPager->pPCache);
2916 while( pList && rc==SQLITE_OK ){
2917 PgHdr *pNext = pList->pDirty;
2918 rc = pagerUndoCallback((void *)pPager, pList->pgno);
2919 pList = pNext;
2920 }
2921
2922 return rc;
2923 }
2924
2925 /*
2926 ** This function is a wrapper around sqlite3WalFrames(). As well as logging
2927 ** the contents of the list of pages headed by pList (connected by pDirty),
2928 ** this function notifies any active backup processes that the pages have
2929 ** changed.
2930 */
2931 static int pagerWalFrames(
2932 Pager *pPager, /* Pager object */
2933 PgHdr *pList, /* List of frames to log */
2934 Pgno nTruncate, /* Database size after this commit */
2935 int isCommit, /* True if this is a commit */
2936 int sync_flags /* Flags to pass to OsSync() (or 0) */
2937 ){
2938 int rc; /* Return code */
2939
2940 assert( pPager->pWal );
2941 rc = sqlite3WalFrames(pPager->pWal,
2942 pPager->pageSize, pList, nTruncate, isCommit, sync_flags
2943 );
2944 if( rc==SQLITE_OK && pPager->pBackup ){
2945 PgHdr *p;
2946 for(p=pList; p; p=p->pDirty){
2947 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
2948 }
2949 }
2950
2951 #ifdef SQLITE_CHECK_PAGES
2952 {
2953 PgHdr *p;
2954 for(p=pList; p; p=p->pDirty) pager_set_pagehash(p);
2955 }
2956 #endif
2957
2958 return rc;
2959 }
2960
2961 /*
2962 ** Begin a read transaction on the WAL.
2963 **
2964 ** This routine used to be called "pagerOpenSnapshot()" because it essentially
2965 ** makes a snapshot of the database at the current point in time and preserves
2966 ** that snapshot for use by the reader in spite of concurrently changes by
2967 ** other writers or checkpointers.
2968 */
2969 static int pagerBeginReadTransaction(Pager *pPager){
2970 int rc; /* Return code */
2971 int changed = 0; /* True if cache must be reset */
2972
2973 assert( pagerUseWal(pPager) );
2974 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
2975
2976 /* sqlite3WalEndReadTransaction() was not called for the previous
2977 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
2978 ** are in locking_mode=NORMAL and EndRead() was previously called,
2979 ** the duplicate call is harmless.
2980 */
2981 sqlite3WalEndReadTransaction(pPager->pWal);
2982
2983 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
2984 if( rc!=SQLITE_OK || changed ){
2985 pager_reset(pPager);
2986 }
2987
2988 return rc;
2989 }
2990 #endif
2991
2992 /*
2993 ** This function is called as part of the transition from PAGER_OPEN
2994 ** to PAGER_READER state to determine the size of the database file
2995 ** in pages (assuming the page size currently stored in Pager.pageSize).
2996 **
2997 ** If no error occurs, SQLITE_OK is returned and the size of the database
2998 ** in pages is stored in *pnPage. Otherwise, an error code (perhaps
2999 ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
3000 */
3001 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
3002 Pgno nPage; /* Value to return via *pnPage */
3003
3004 /* Query the WAL sub-system for the database size. The WalDbsize()
3005 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
3006 ** if the database size is not available. The database size is not
3007 ** available from the WAL sub-system if the log file is empty or
3008 ** contains no valid committed transactions.
3009 */
3010 assert( pPager->eState==PAGER_OPEN );
3011 assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
3012 nPage = sqlite3WalDbsize(pPager->pWal);
3013
3014 /* If the database size was not available from the WAL sub-system,
3015 ** determine it based on the size of the database file. If the size
3016 ** of the database file is not an integer multiple of the page-size,
3017 ** round down to the nearest page. Except, any file larger than 0
3018 ** bytes in size is considered to contain at least one page.
3019 */
3020 if( nPage==0 ){
3021 i64 n = 0; /* Size of db file in bytes */
3022 assert( isOpen(pPager->fd) || pPager->tempFile );
3023 if( isOpen(pPager->fd) ){
3024 int rc = sqlite3OsFileSize(pPager->fd, &n);
3025 if( rc!=SQLITE_OK ){
3026 return rc;
3027 }
3028 }
3029 nPage = (Pgno)(n / pPager->pageSize);
3030 if( nPage==0 && n>0 ){
3031 nPage = 1;
3032 }
3033 }
3034
3035 /* If the current number of pages in the file is greater than the
3036 ** configured maximum pager number, increase the allowed limit so
3037 ** that the file can be read.
3038 */
3039 if( nPage>pPager->mxPgno ){
3040 pPager->mxPgno = (Pgno)nPage;
3041 }
3042
3043 *pnPage = nPage;
3044 return SQLITE_OK;
3045 }
3046
3047 #ifndef SQLITE_OMIT_WAL
3048 /*
3049 ** Check if the *-wal file that corresponds to the database opened by pPager
3050 ** exists if the database is not empy, or verify that the *-wal file does
3051 ** not exist (by deleting it) if the database file is empty.
3052 **
3053 ** If the database is not empty and the *-wal file exists, open the pager
3054 ** in WAL mode. If the database is empty or if no *-wal file exists and
3055 ** if no error occurs, make sure Pager.journalMode is not set to
3056 ** PAGER_JOURNALMODE_WAL.
3057 **
3058 ** Return SQLITE_OK or an error code.
3059 **
3060 ** The caller must hold a SHARED lock on the database file to call this
3061 ** function. Because an EXCLUSIVE lock on the db file is required to delete
3062 ** a WAL on a none-empty database, this ensures there is no race condition
3063 ** between the xAccess() below and an xDelete() being executed by some
3064 ** other connection.
3065 */
3066 static int pagerOpenWalIfPresent(Pager *pPager){
3067 int rc = SQLITE_OK;
3068 assert( pPager->eState==PAGER_OPEN );
3069 assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
3070
3071 if( !pPager->tempFile ){
3072 int isWal; /* True if WAL file exists */
3073 Pgno nPage; /* Size of the database file */
3074
3075 rc = pagerPagecount(pPager, &nPage);
3076 if( rc ) return rc;
3077 if( nPage==0 ){
3078 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
3079 isWal = 0;
3080 }else{
3081 rc = sqlite3OsAccess(
3082 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
3083 );
3084 }
3085 if( rc==SQLITE_OK ){
3086 if( isWal ){
3087 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
3088 rc = sqlite3PagerOpenWal(pPager, 0);
3089 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
3090 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
3091 }
3092 }
3093 }
3094 return rc;
3095 }
3096 #endif
3097
2059 /* 3098 /*
2060 ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback 3099 ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
2061 ** the entire master journal file. The case pSavepoint==NULL occurs when 3100 ** the entire master journal file. The case pSavepoint==NULL occurs when
2062 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction 3101 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
2063 ** savepoint. 3102 ** savepoint.
2064 ** 3103 **
2065 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is 3104 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
2066 ** being rolled back), then the rollback consists of up to three stages, 3105 ** being rolled back), then the rollback consists of up to three stages,
2067 ** performed in the order specified: 3106 ** performed in the order specified:
2068 ** 3107 **
(...skipping 22 matching lines...) Expand all
2091 ** is reset to the value that it held at the start of the savepoint 3130 ** is reset to the value that it held at the start of the savepoint
2092 ** (or transaction). No page with a page-number greater than this value 3131 ** (or transaction). No page with a page-number greater than this value
2093 ** is played back. If one is encountered it is simply skipped. 3132 ** is played back. If one is encountered it is simply skipped.
2094 */ 3133 */
2095 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){ 3134 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
2096 i64 szJ; /* Effective size of the main journal */ 3135 i64 szJ; /* Effective size of the main journal */
2097 i64 iHdrOff; /* End of first segment of main-journal records */ 3136 i64 iHdrOff; /* End of first segment of main-journal records */
2098 int rc = SQLITE_OK; /* Return code */ 3137 int rc = SQLITE_OK; /* Return code */
2099 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */ 3138 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
2100 3139
2101 assert( pPager->state>=PAGER_SHARED ); 3140 assert( pPager->eState!=PAGER_ERROR );
3141 assert( pPager->eState>=PAGER_WRITER_LOCKED );
2102 3142
2103 /* Allocate a bitvec to use to store the set of pages rolled back */ 3143 /* Allocate a bitvec to use to store the set of pages rolled back */
2104 if( pSavepoint ){ 3144 if( pSavepoint ){
2105 pDone = sqlite3BitvecCreate(pSavepoint->nOrig); 3145 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
2106 if( !pDone ){ 3146 if( !pDone ){
2107 return SQLITE_NOMEM; 3147 return SQLITE_NOMEM;
2108 } 3148 }
2109 } 3149 }
2110 3150
2111 /* Set the database size back to the value it was before the savepoint 3151 /* Set the database size back to the value it was before the savepoint
2112 ** being reverted was opened. 3152 ** being reverted was opened.
2113 */ 3153 */
2114 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize; 3154 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
3155 pPager->changeCountDone = pPager->tempFile;
3156
3157 if( !pSavepoint && pagerUseWal(pPager) ){
3158 return pagerRollbackWal(pPager);
3159 }
2115 3160
2116 /* Use pPager->journalOff as the effective size of the main rollback 3161 /* Use pPager->journalOff as the effective size of the main rollback
2117 ** journal. The actual file might be larger than this in 3162 ** journal. The actual file might be larger than this in
2118 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything 3163 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
2119 ** past pPager->journalOff is off-limits to us. 3164 ** past pPager->journalOff is off-limits to us.
2120 */ 3165 */
2121 szJ = pPager->journalOff; 3166 szJ = pPager->journalOff;
3167 assert( pagerUseWal(pPager)==0 || szJ==0 );
2122 3168
2123 /* Begin by rolling back records from the main journal starting at 3169 /* Begin by rolling back records from the main journal starting at
2124 ** PagerSavepoint.iOffset and continuing to the next journal header. 3170 ** PagerSavepoint.iOffset and continuing to the next journal header.
2125 ** There might be records in the main journal that have a page number 3171 ** There might be records in the main journal that have a page number
2126 ** greater than the current database size (pPager->dbSize) but those 3172 ** greater than the current database size (pPager->dbSize) but those
2127 ** will be skipped automatically. Pages are added to pDone as they 3173 ** will be skipped automatically. Pages are added to pDone as they
2128 ** are played back. 3174 ** are played back.
2129 */ 3175 */
2130 if( pSavepoint ){ 3176 if( pSavepoint && !pagerUseWal(pPager) ){
2131 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ; 3177 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
2132 pPager->journalOff = pSavepoint->iOffset; 3178 pPager->journalOff = pSavepoint->iOffset;
2133 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){ 3179 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
2134 rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone); 3180 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
2135 } 3181 }
2136 assert( rc!=SQLITE_DONE ); 3182 assert( rc!=SQLITE_DONE );
2137 }else{ 3183 }else{
2138 pPager->journalOff = 0; 3184 pPager->journalOff = 0;
2139 } 3185 }
2140 3186
2141 /* Continue rolling back records out of the main journal starting at 3187 /* Continue rolling back records out of the main journal starting at
2142 ** the first journal header seen and continuing until the effective end 3188 ** the first journal header seen and continuing until the effective end
2143 ** of the main journal file. Continue to skip out-of-range pages and 3189 ** of the main journal file. Continue to skip out-of-range pages and
2144 ** continue adding pages rolled back to pDone. 3190 ** continue adding pages rolled back to pDone.
2145 */ 3191 */
2146 while( rc==SQLITE_OK && pPager->journalOff<szJ ){ 3192 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
2147 u32 ii; /* Loop counter */ 3193 u32 ii; /* Loop counter */
2148 u32 nJRec = 0; /* Number of Journal Records */ 3194 u32 nJRec = 0; /* Number of Journal Records */
2149 u32 dummy; 3195 u32 dummy;
2150 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy); 3196 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
2151 assert( rc!=SQLITE_DONE ); 3197 assert( rc!=SQLITE_DONE );
2152 3198
2153 /* 3199 /*
2154 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff" 3200 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
2155 ** test is related to ticket #2565. See the discussion in the 3201 ** test is related to ticket #2565. See the discussion in the
2156 ** pager_playback() function for additional information. 3202 ** pager_playback() function for additional information.
2157 */ 3203 */
2158 if( nJRec==0 3204 if( nJRec==0
2159 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff 3205 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
2160 ){ 3206 ){
2161 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager)); 3207 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
2162 } 3208 }
2163 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){ 3209 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
2164 rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone); 3210 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
2165 } 3211 }
2166 assert( rc!=SQLITE_DONE ); 3212 assert( rc!=SQLITE_DONE );
2167 } 3213 }
2168 assert( rc!=SQLITE_OK || pPager->journalOff==szJ ); 3214 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
2169 3215
2170 /* Finally, rollback pages from the sub-journal. Page that were 3216 /* Finally, rollback pages from the sub-journal. Page that were
2171 ** previously rolled back out of the main journal (and are hence in pDone) 3217 ** previously rolled back out of the main journal (and are hence in pDone)
2172 ** will be skipped. Out-of-range pages are also skipped. 3218 ** will be skipped. Out-of-range pages are also skipped.
2173 */ 3219 */
2174 if( pSavepoint ){ 3220 if( pSavepoint ){
2175 u32 ii; /* Loop counter */ 3221 u32 ii; /* Loop counter */
2176 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize); 3222 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
3223
3224 if( pagerUseWal(pPager) ){
3225 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
3226 }
2177 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){ 3227 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
2178 assert( offset==ii*(4+pPager->pageSize) ); 3228 assert( offset==ii*(4+pPager->pageSize) );
2179 rc = pager_playback_one_page(pPager, 0, 0, &offset, 1, pDone); 3229 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
2180 } 3230 }
2181 assert( rc!=SQLITE_DONE ); 3231 assert( rc!=SQLITE_DONE );
2182 } 3232 }
2183 3233
2184 sqlite3BitvecDestroy(pDone); 3234 sqlite3BitvecDestroy(pDone);
2185 if( rc==SQLITE_OK ){ 3235 if( rc==SQLITE_OK ){
2186 pPager->journalOff = szJ; 3236 pPager->journalOff = szJ;
2187 } 3237 }
3238
2188 return rc; 3239 return rc;
2189 } 3240 }
2190 3241
2191 /* 3242 /*
2192 ** Change the maximum number of in-memory pages that are allowed. 3243 ** Change the maximum number of in-memory pages that are allowed.
2193 */ 3244 */
2194 void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){ 3245 void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
2195 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage); 3246 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
2196 } 3247 }
2197 3248
(...skipping 21 matching lines...) Expand all
2219 ** point of causing damage to the database during rollback. 3270 ** point of causing damage to the database during rollback.
2220 ** 3271 **
2221 ** Numeric values associated with these states are OFF==1, NORMAL=2, 3272 ** Numeric values associated with these states are OFF==1, NORMAL=2,
2222 ** and FULL=3. 3273 ** and FULL=3.
2223 */ 3274 */
2224 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 3275 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
2225 void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){ 3276 void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
2226 pPager->noSync = (level==1 || pPager->tempFile) ?1:0; 3277 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
2227 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0; 3278 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
2228 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL); 3279 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
2229 if( pPager->noSync ) pPager->needSync = 0;
2230 } 3280 }
2231 #endif 3281 #endif
2232 3282
2233 /* 3283 /*
2234 ** The following global variable is incremented whenever the library 3284 ** The following global variable is incremented whenever the library
2235 ** attempts to open a temporary file. This information is used for 3285 ** attempts to open a temporary file. This information is used for
2236 ** testing and analysis only. 3286 ** testing and analysis only.
2237 */ 3287 */
2238 #ifdef SQLITE_TEST 3288 #ifdef SQLITE_TEST
2239 int sqlite3_opentemp_count = 0; 3289 int sqlite3_opentemp_count = 0;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
2296 void sqlite3PagerSetBusyhandler( 3346 void sqlite3PagerSetBusyhandler(
2297 Pager *pPager, /* Pager object */ 3347 Pager *pPager, /* Pager object */
2298 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */ 3348 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
2299 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */ 3349 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
2300 ){ 3350 ){
2301 pPager->xBusyHandler = xBusyHandler; 3351 pPager->xBusyHandler = xBusyHandler;
2302 pPager->pBusyHandlerArg = pBusyHandlerArg; 3352 pPager->pBusyHandlerArg = pBusyHandlerArg;
2303 } 3353 }
2304 3354
2305 /* 3355 /*
2306 ** Report the current page size and number of reserved bytes back
2307 ** to the codec.
2308 */
2309 #ifdef SQLITE_HAS_CODEC
2310 static void pagerReportSize(Pager *pPager){
2311 if( pPager->xCodecSizeChng ){
2312 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
2313 (int)pPager->nReserve);
2314 }
2315 }
2316 #else
2317 # define pagerReportSize(X) /* No-op if we do not support a codec */
2318 #endif
2319
2320 /*
2321 ** Change the page size used by the Pager object. The new page size 3356 ** Change the page size used by the Pager object. The new page size
2322 ** is passed in *pPageSize. 3357 ** is passed in *pPageSize.
2323 ** 3358 **
2324 ** If the pager is in the error state when this function is called, it 3359 ** If the pager is in the error state when this function is called, it
2325 ** is a no-op. The value returned is the error state error code (i.e. 3360 ** is a no-op. The value returned is the error state error code (i.e.
2326 ** one of SQLITE_IOERR, SQLITE_CORRUPT or SQLITE_FULL). 3361 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
2327 ** 3362 **
2328 ** Otherwise, if all of the following are true: 3363 ** Otherwise, if all of the following are true:
2329 ** 3364 **
2330 ** * the new page size (value of *pPageSize) is valid (a power 3365 ** * the new page size (value of *pPageSize) is valid (a power
2331 ** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and 3366 ** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
2332 ** 3367 **
2333 ** * there are no outstanding page references, and 3368 ** * there are no outstanding page references, and
2334 ** 3369 **
2335 ** * the database is either not an in-memory database or it is 3370 ** * the database is either not an in-memory database or it is
2336 ** an in-memory database that currently consists of zero pages. 3371 ** an in-memory database that currently consists of zero pages.
2337 ** 3372 **
2338 ** then the pager object page size is set to *pPageSize. 3373 ** then the pager object page size is set to *pPageSize.
2339 ** 3374 **
2340 ** If the page size is changed, then this function uses sqlite3PagerMalloc() 3375 ** If the page size is changed, then this function uses sqlite3PagerMalloc()
2341 ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt 3376 ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
2342 ** fails, SQLITE_NOMEM is returned and the page size remains unchanged. 3377 ** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
2343 ** In all other cases, SQLITE_OK is returned. 3378 ** In all other cases, SQLITE_OK is returned.
2344 ** 3379 **
2345 ** If the page size is not changed, either because one of the enumerated 3380 ** If the page size is not changed, either because one of the enumerated
2346 ** conditions above is not true, the pager was in error state when this 3381 ** conditions above is not true, the pager was in error state when this
2347 ** function was called, or because the memory allocation attempt failed, 3382 ** function was called, or because the memory allocation attempt failed,
2348 ** then *pPageSize is set to the old, retained page size before returning. 3383 ** then *pPageSize is set to the old, retained page size before returning.
2349 */ 3384 */
2350 int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize, int nReserve){ 3385 int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
2351 int rc = pPager->errCode; 3386 int rc = SQLITE_OK;
2352 3387
3388 /* It is not possible to do a full assert_pager_state() here, as this
3389 ** function may be called from within PagerOpen(), before the state
3390 ** of the Pager object is internally consistent.
3391 **
3392 ** At one point this function returned an error if the pager was in
3393 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
3394 ** there is at least one outstanding page reference, this function
3395 ** is a no-op for that case anyhow.
3396 */
3397
3398 u32 pageSize = *pPageSize;
3399 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
3400 if( (pPager->memDb==0 || pPager->dbSize==0)
3401 && sqlite3PcacheRefCount(pPager->pPCache)==0
3402 && pageSize && pageSize!=(u32)pPager->pageSize
3403 ){
3404 char *pNew = NULL; /* New temp space */
3405 i64 nByte = 0;
3406
3407 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
3408 rc = sqlite3OsFileSize(pPager->fd, &nByte);
3409 }
3410 if( rc==SQLITE_OK ){
3411 pNew = (char *)sqlite3PageMalloc(pageSize);
3412 if( !pNew ) rc = SQLITE_NOMEM;
3413 }
3414
3415 if( rc==SQLITE_OK ){
3416 pager_reset(pPager);
3417 pPager->dbSize = (Pgno)(nByte/pageSize);
3418 pPager->pageSize = pageSize;
3419 sqlite3PageFree(pPager->pTmpSpace);
3420 pPager->pTmpSpace = pNew;
3421 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
3422 }
3423 }
3424
3425 *pPageSize = pPager->pageSize;
2353 if( rc==SQLITE_OK ){ 3426 if( rc==SQLITE_OK ){
2354 u16 pageSize = *pPageSize;
2355 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
2356 if( (pPager->memDb==0 || pPager->dbSize==0)
2357 && sqlite3PcacheRefCount(pPager->pPCache)==0
2358 && pageSize && pageSize!=pPager->pageSize
2359 ){
2360 char *pNew = (char *)sqlite3PageMalloc(pageSize);
2361 if( !pNew ){
2362 rc = SQLITE_NOMEM;
2363 }else{
2364 pager_reset(pPager);
2365 pPager->pageSize = pageSize;
2366 sqlite3PageFree(pPager->pTmpSpace);
2367 pPager->pTmpSpace = pNew;
2368 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
2369 }
2370 }
2371 *pPageSize = (u16)pPager->pageSize;
2372 if( nReserve<0 ) nReserve = pPager->nReserve; 3427 if( nReserve<0 ) nReserve = pPager->nReserve;
2373 assert( nReserve>=0 && nReserve<1000 ); 3428 assert( nReserve>=0 && nReserve<1000 );
2374 pPager->nReserve = (i16)nReserve; 3429 pPager->nReserve = (i16)nReserve;
2375 pagerReportSize(pPager); 3430 pagerReportSize(pPager);
2376 } 3431 }
2377 return rc; 3432 return rc;
2378 } 3433 }
2379 3434
2380 /* 3435 /*
2381 ** Return a pointer to the "temporary page" buffer held internally 3436 ** Return a pointer to the "temporary page" buffer held internally
(...skipping 11 matching lines...) Expand all
2393 ** Attempt to set the maximum database page count if mxPage is positive. 3448 ** Attempt to set the maximum database page count if mxPage is positive.
2394 ** Make no changes if mxPage is zero or negative. And never reduce the 3449 ** Make no changes if mxPage is zero or negative. And never reduce the
2395 ** maximum page count below the current size of the database. 3450 ** maximum page count below the current size of the database.
2396 ** 3451 **
2397 ** Regardless of mxPage, return the current maximum page count. 3452 ** Regardless of mxPage, return the current maximum page count.
2398 */ 3453 */
2399 int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ 3454 int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2400 if( mxPage>0 ){ 3455 if( mxPage>0 ){
2401 pPager->mxPgno = mxPage; 3456 pPager->mxPgno = mxPage;
2402 } 3457 }
2403 sqlite3PagerPagecount(pPager, 0); 3458 if( pPager->eState!=PAGER_OPEN && pPager->mxPgno<pPager->dbSize ){
3459 pPager->mxPgno = pPager->dbSize;
3460 }
2404 return pPager->mxPgno; 3461 return pPager->mxPgno;
2405 } 3462 }
2406 3463
2407 /* 3464 /*
2408 ** The following set of routines are used to disable the simulated 3465 ** The following set of routines are used to disable the simulated
2409 ** I/O error mechanism. These routines are used to avoid simulated 3466 ** I/O error mechanism. These routines are used to avoid simulated
2410 ** errors in places where we do not care about errors. 3467 ** errors in places where we do not care about errors.
2411 ** 3468 **
2412 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops 3469 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2413 ** and generate no code. 3470 ** and generate no code.
(...skipping 25 matching lines...) Expand all
2439 ** zero sized database has a header than consists entirely of zeroes. 3496 ** zero sized database has a header than consists entirely of zeroes.
2440 ** 3497 **
2441 ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered, 3498 ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
2442 ** the error code is returned to the caller and the contents of the 3499 ** the error code is returned to the caller and the contents of the
2443 ** output buffer undefined. 3500 ** output buffer undefined.
2444 */ 3501 */
2445 int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){ 3502 int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
2446 int rc = SQLITE_OK; 3503 int rc = SQLITE_OK;
2447 memset(pDest, 0, N); 3504 memset(pDest, 0, N);
2448 assert( isOpen(pPager->fd) || pPager->tempFile ); 3505 assert( isOpen(pPager->fd) || pPager->tempFile );
3506
3507 /* This routine is only called by btree immediately after creating
3508 ** the Pager object. There has not been an opportunity to transition
3509 ** to WAL mode yet.
3510 */
3511 assert( !pagerUseWal(pPager) );
3512
2449 if( isOpen(pPager->fd) ){ 3513 if( isOpen(pPager->fd) ){
2450 IOTRACE(("DBHDR %p 0 %d\n", pPager, N)) 3514 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
2451 rc = sqlite3OsRead(pPager->fd, pDest, N, 0); 3515 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
2452 if( rc==SQLITE_IOERR_SHORT_READ ){ 3516 if( rc==SQLITE_IOERR_SHORT_READ ){
2453 rc = SQLITE_OK; 3517 rc = SQLITE_OK;
2454 } 3518 }
2455 } 3519 }
2456 return rc; 3520 return rc;
2457 } 3521 }
2458 3522
2459 /* 3523 /*
2460 ** Return the total number of pages in the database file associated 3524 ** This function may only be called when a read-transaction is open on
2461 ** with pPager. Normally, this is calculated as (<db file size>/<page-size>). 3525 ** the pager. It returns the total number of pages in the database.
3526 **
2462 ** However, if the file is between 1 and <page-size> bytes in size, then 3527 ** However, if the file is between 1 and <page-size> bytes in size, then
2463 ** this is considered a 1 page file. 3528 ** this is considered a 1 page file.
2464 **
2465 ** If the pager is in error state when this function is called, then the
2466 ** error state error code is returned and *pnPage left unchanged. Or,
2467 ** if the file system has to be queried for the size of the file and
2468 ** the query attempt returns an IO error, the IO error code is returned
2469 ** and *pnPage is left unchanged.
2470 **
2471 ** Otherwise, if everything is successful, then SQLITE_OK is returned
2472 ** and *pnPage is set to the number of pages in the database.
2473 */ 3529 */
2474 int sqlite3PagerPagecount(Pager *pPager, int *pnPage){ 3530 void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
2475 Pgno nPage; /* Value to return via *pnPage */ 3531 assert( pPager->eState>=PAGER_READER );
2476 3532 assert( pPager->eState!=PAGER_WRITER_FINISHED );
2477 /* If the pager is already in the error state, return the error code. */ 3533 *pnPage = (int)pPager->dbSize;
2478 if( pPager->errCode ){
2479 return pPager->errCode;
2480 }
2481
2482 /* Determine the number of pages in the file. Store this in nPage. */
2483 if( pPager->dbSizeValid ){
2484 nPage = pPager->dbSize;
2485 }else{
2486 int rc; /* Error returned by OsFileSize() */
2487 i64 n = 0; /* File size in bytes returned by OsFileSize() */
2488
2489 assert( isOpen(pPager->fd) || pPager->tempFile );
2490 if( isOpen(pPager->fd) && (0 != (rc = sqlite3OsFileSize(pPager->fd, &n))) ){
2491 pager_error(pPager, rc);
2492 return rc;
2493 }
2494 if( n>0 && n<pPager->pageSize ){
2495 nPage = 1;
2496 }else{
2497 nPage = (Pgno)(n / pPager->pageSize);
2498 }
2499 if( pPager->state!=PAGER_UNLOCK ){
2500 pPager->dbSize = nPage;
2501 pPager->dbFileSize = nPage;
2502 pPager->dbSizeValid = 1;
2503 }
2504 }
2505
2506 /* If the current number of pages in the file is greater than the
2507 ** configured maximum pager number, increase the allowed limit so
2508 ** that the file can be read.
2509 */
2510 if( nPage>pPager->mxPgno ){
2511 pPager->mxPgno = (Pgno)nPage;
2512 }
2513
2514 /* Set the output variable and return SQLITE_OK */
2515 if( pnPage ){
2516 *pnPage = nPage;
2517 }
2518 return SQLITE_OK;
2519 } 3534 }
2520 3535
2521 3536
2522 /* 3537 /*
2523 ** Try to obtain a lock of type locktype on the database file. If 3538 ** Try to obtain a lock of type locktype on the database file. If
2524 ** a similar or greater lock is already held, this function is a no-op 3539 ** a similar or greater lock is already held, this function is a no-op
2525 ** (returning SQLITE_OK immediately). 3540 ** (returning SQLITE_OK immediately).
2526 ** 3541 **
2527 ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke 3542 ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
2528 ** the busy callback if the lock is currently not available. Repeat 3543 ** the busy callback if the lock is currently not available. Repeat
2529 ** until the busy callback returns false or until the attempt to 3544 ** until the busy callback returns false or until the attempt to
2530 ** obtain the lock succeeds. 3545 ** obtain the lock succeeds.
2531 ** 3546 **
2532 ** Return SQLITE_OK on success and an error code if we cannot obtain 3547 ** Return SQLITE_OK on success and an error code if we cannot obtain
2533 ** the lock. If the lock is obtained successfully, set the Pager.state 3548 ** the lock. If the lock is obtained successfully, set the Pager.state
2534 ** variable to locktype before returning. 3549 ** variable to locktype before returning.
2535 */ 3550 */
2536 static int pager_wait_on_lock(Pager *pPager, int locktype){ 3551 static int pager_wait_on_lock(Pager *pPager, int locktype){
2537 int rc; /* Return code */ 3552 int rc; /* Return code */
2538 3553
2539 /* The OS lock values must be the same as the Pager lock values */
2540 assert( PAGER_SHARED==SHARED_LOCK );
2541 assert( PAGER_RESERVED==RESERVED_LOCK );
2542 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
2543
2544 /* If the file is currently unlocked then the size must be unknown */
2545 assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
2546
2547 /* Check that this is either a no-op (because the requested lock is 3554 /* Check that this is either a no-op (because the requested lock is
2548 ** already held, or one of the transistions that the busy-handler 3555 ** already held, or one of the transistions that the busy-handler
2549 ** may be invoked during, according to the comment above 3556 ** may be invoked during, according to the comment above
2550 ** sqlite3PagerSetBusyhandler(). 3557 ** sqlite3PagerSetBusyhandler().
2551 */ 3558 */
2552 assert( (pPager->state>=locktype) 3559 assert( (pPager->eLock>=locktype)
2553 || (pPager->state==PAGER_UNLOCK && locktype==PAGER_SHARED) 3560 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
2554 || (pPager->state==PAGER_RESERVED && locktype==PAGER_EXCLUSIVE) 3561 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
2555 ); 3562 );
2556 3563
2557 if( pPager->state>=locktype ){ 3564 do {
2558 rc = SQLITE_OK; 3565 rc = pagerLockDb(pPager, locktype);
2559 }else{ 3566 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
2560 do {
2561 rc = sqlite3OsLock(pPager->fd, locktype);
2562 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
2563 if( rc==SQLITE_OK ){
2564 pPager->state = (u8)locktype;
2565 IOTRACE(("LOCK %p %d\n", pPager, locktype))
2566 }
2567 }
2568 return rc; 3567 return rc;
2569 } 3568 }
2570 3569
2571 /* 3570 /*
2572 ** Function assertTruncateConstraint(pPager) checks that one of the 3571 ** Function assertTruncateConstraint(pPager) checks that one of the
2573 ** following is true for all dirty pages currently in the page-cache: 3572 ** following is true for all dirty pages currently in the page-cache:
2574 ** 3573 **
2575 ** a) The page number is less than or equal to the size of the 3574 ** a) The page number is less than or equal to the size of the
2576 ** current database image, in pages, OR 3575 ** current database image, in pages, OR
2577 ** 3576 **
(...skipping 24 matching lines...) Expand all
2602 # define assertTruncateConstraint(pPager) 3601 # define assertTruncateConstraint(pPager)
2603 #endif 3602 #endif
2604 3603
2605 /* 3604 /*
2606 ** Truncate the in-memory database file image to nPage pages. This 3605 ** Truncate the in-memory database file image to nPage pages. This
2607 ** function does not actually modify the database file on disk. It 3606 ** function does not actually modify the database file on disk. It
2608 ** just sets the internal state of the pager object so that the 3607 ** just sets the internal state of the pager object so that the
2609 ** truncation will be done when the current transaction is committed. 3608 ** truncation will be done when the current transaction is committed.
2610 */ 3609 */
2611 void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ 3610 void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
2612 assert( pPager->dbSizeValid );
2613 assert( pPager->dbSize>=nPage ); 3611 assert( pPager->dbSize>=nPage );
2614 assert( pPager->state>=PAGER_RESERVED ); 3612 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
2615 pPager->dbSize = nPage; 3613 pPager->dbSize = nPage;
2616 assertTruncateConstraint(pPager); 3614 assertTruncateConstraint(pPager);
2617 } 3615 }
2618 3616
3617
3618 /*
3619 ** This function is called before attempting a hot-journal rollback. It
3620 ** syncs the journal file to disk, then sets pPager->journalHdr to the
3621 ** size of the journal file so that the pager_playback() routine knows
3622 ** that the entire journal file has been synced.
3623 **
3624 ** Syncing a hot-journal to disk before attempting to roll it back ensures
3625 ** that if a power-failure occurs during the rollback, the process that
3626 ** attempts rollback following system recovery sees the same journal
3627 ** content as this process.
3628 **
3629 ** If everything goes as planned, SQLITE_OK is returned. Otherwise,
3630 ** an SQLite error code.
3631 */
3632 static int pagerSyncHotJournal(Pager *pPager){
3633 int rc = SQLITE_OK;
3634 if( !pPager->noSync ){
3635 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
3636 }
3637 if( rc==SQLITE_OK ){
3638 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
3639 }
3640 return rc;
3641 }
3642
2619 /* 3643 /*
2620 ** Shutdown the page cache. Free all memory and close all files. 3644 ** Shutdown the page cache. Free all memory and close all files.
2621 ** 3645 **
2622 ** If a transaction was in progress when this routine is called, that 3646 ** If a transaction was in progress when this routine is called, that
2623 ** transaction is rolled back. All outstanding pages are invalidated 3647 ** transaction is rolled back. All outstanding pages are invalidated
2624 ** and their memory is freed. Any attempt to use a page associated 3648 ** and their memory is freed. Any attempt to use a page associated
2625 ** with this page cache after this function returns will likely 3649 ** with this page cache after this function returns will likely
2626 ** result in a coredump. 3650 ** result in a coredump.
2627 ** 3651 **
2628 ** This function always succeeds. If a transaction is active an attempt 3652 ** This function always succeeds. If a transaction is active an attempt
2629 ** is made to roll it back. If an error occurs during the rollback 3653 ** is made to roll it back. If an error occurs during the rollback
2630 ** a hot journal may be left in the filesystem but no error is returned 3654 ** a hot journal may be left in the filesystem but no error is returned
2631 ** to the caller. 3655 ** to the caller.
2632 */ 3656 */
2633 int sqlite3PagerClose(Pager *pPager){ 3657 int sqlite3PagerClose(Pager *pPager){
3658 u8 *pTmp = (u8 *)pPager->pTmpSpace;
3659
2634 disable_simulated_io_errors(); 3660 disable_simulated_io_errors();
2635 sqlite3BeginBenignMalloc(); 3661 sqlite3BeginBenignMalloc();
2636 pPager->errCode = 0; 3662 /* pPager->errCode = 0; */
2637 pPager->exclusiveMode = 0; 3663 pPager->exclusiveMode = 0;
3664 #ifndef SQLITE_OMIT_WAL
3665 sqlite3WalClose(pPager->pWal,
3666 (pPager->noSync ? 0 : pPager->sync_flags),
3667 pPager->pageSize, pTmp
3668 );
3669 pPager->pWal = 0;
3670 #endif
2638 pager_reset(pPager); 3671 pager_reset(pPager);
2639 if( MEMDB ){ 3672 if( MEMDB ){
2640 pager_unlock(pPager); 3673 pager_unlock(pPager);
2641 }else{ 3674 }else{
2642 /* Set Pager.journalHdr to -1 for the benefit of the pager_playback() 3675 /* If it is open, sync the journal file before calling UnlockAndRollback.
2643 ** call which may be made from within pagerUnlockAndRollback(). If it 3676 ** If this is not done, then an unsynced portion of the open journal
2644 ** is not -1, then the unsynced portion of an open journal file may 3677 ** file may be played back into the database. If a power failure occurs
2645 ** be played back into the database. If a power failure occurs while 3678 ** while this is happening, the database could become corrupt.
2646 ** this is happening, the database may become corrupt. 3679 **
3680 ** If an error occurs while trying to sync the journal, shift the pager
3681 ** into the ERROR state. This causes UnlockAndRollback to unlock the
3682 ** database and close the journal file without attempting to roll it
3683 ** back or finalize it. The next database user will have to do hot-journal
3684 ** rollback before accessing the database file.
2647 */ 3685 */
2648 pPager->journalHdr = -1; 3686 if( isOpen(pPager->jfd) ){
3687 pager_error(pPager, pagerSyncHotJournal(pPager));
3688 }
2649 pagerUnlockAndRollback(pPager); 3689 pagerUnlockAndRollback(pPager);
2650 } 3690 }
2651 sqlite3EndBenignMalloc(); 3691 sqlite3EndBenignMalloc();
2652 enable_simulated_io_errors(); 3692 enable_simulated_io_errors();
2653 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager))); 3693 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
2654 IOTRACE(("CLOSE %p\n", pPager)) 3694 IOTRACE(("CLOSE %p\n", pPager))
3695 sqlite3OsClose(pPager->jfd);
2655 sqlite3OsClose(pPager->fd); 3696 sqlite3OsClose(pPager->fd);
2656 sqlite3PageFree(pPager->pTmpSpace); 3697 sqlite3PageFree(pTmp);
2657 sqlite3PcacheClose(pPager->pPCache); 3698 sqlite3PcacheClose(pPager->pPCache);
2658 3699
2659 #ifdef SQLITE_HAS_CODEC 3700 #ifdef SQLITE_HAS_CODEC
2660 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); 3701 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
2661 #endif 3702 #endif
2662 3703
2663 assert( !pPager->aSavepoint && !pPager->pInJournal ); 3704 assert( !pPager->aSavepoint && !pPager->pInJournal );
2664 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) ); 3705 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
2665 3706
2666 sqlite3_free(pPager); 3707 sqlite3_free(pPager);
(...skipping 14 matching lines...) Expand all
2681 */ 3722 */
2682 void sqlite3PagerRef(DbPage *pPg){ 3723 void sqlite3PagerRef(DbPage *pPg){
2683 sqlite3PcacheRef(pPg); 3724 sqlite3PcacheRef(pPg);
2684 } 3725 }
2685 3726
2686 /* 3727 /*
2687 ** Sync the journal. In other words, make sure all the pages that have 3728 ** Sync the journal. In other words, make sure all the pages that have
2688 ** been written to the journal have actually reached the surface of the 3729 ** been written to the journal have actually reached the surface of the
2689 ** disk and can be restored in the event of a hot-journal rollback. 3730 ** disk and can be restored in the event of a hot-journal rollback.
2690 ** 3731 **
2691 ** If the Pager.needSync flag is not set, then this function is a 3732 ** If the Pager.noSync flag is set, then this function is a no-op.
2692 ** no-op. Otherwise, the actions required depend on the journal-mode 3733 ** Otherwise, the actions required depend on the journal-mode and the
2693 ** and the device characteristics of the the file-system, as follows: 3734 ** device characteristics of the the file-system, as follows:
2694 ** 3735 **
2695 ** * If the journal file is an in-memory journal file, no action need 3736 ** * If the journal file is an in-memory journal file, no action need
2696 ** be taken. 3737 ** be taken.
2697 ** 3738 **
2698 ** * Otherwise, if the device does not support the SAFE_APPEND property, 3739 ** * Otherwise, if the device does not support the SAFE_APPEND property,
2699 ** then the nRec field of the most recently written journal header 3740 ** then the nRec field of the most recently written journal header
2700 ** is updated to contain the number of journal records that have 3741 ** is updated to contain the number of journal records that have
2701 ** been written following it. If the pager is operating in full-sync 3742 ** been written following it. If the pager is operating in full-sync
2702 ** mode, then the journal file is synced before this field is updated. 3743 ** mode, then the journal file is synced before this field is updated.
2703 ** 3744 **
2704 ** * If the device does not support the SEQUENTIAL property, then 3745 ** * If the device does not support the SEQUENTIAL property, then
2705 ** journal file is synced. 3746 ** journal file is synced.
2706 ** 3747 **
2707 ** Or, in pseudo-code: 3748 ** Or, in pseudo-code:
2708 ** 3749 **
2709 ** if( NOT <in-memory journal> ){ 3750 ** if( NOT <in-memory journal> ){
2710 ** if( NOT SAFE_APPEND ){ 3751 ** if( NOT SAFE_APPEND ){
2711 ** if( <full-sync mode> ) xSync(<journal file>); 3752 ** if( <full-sync mode> ) xSync(<journal file>);
2712 ** <update nRec field> 3753 ** <update nRec field>
2713 ** } 3754 ** }
2714 ** if( NOT SEQUENTIAL ) xSync(<journal file>); 3755 ** if( NOT SEQUENTIAL ) xSync(<journal file>);
2715 ** } 3756 ** }
2716 ** 3757 **
2717 ** The Pager.needSync flag is never be set for temporary files, or any
2718 ** file operating in no-sync mode (Pager.noSync set to non-zero).
2719 **
2720 ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every 3758 ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
2721 ** page currently held in memory before returning SQLITE_OK. If an IO 3759 ** page currently held in memory before returning SQLITE_OK. If an IO
2722 ** error is encountered, then the IO error code is returned to the caller. 3760 ** error is encountered, then the IO error code is returned to the caller.
2723 */ 3761 */
2724 static int syncJournal(Pager *pPager){ 3762 static int syncJournal(Pager *pPager, int newHdr){
2725 if( pPager->needSync ){ 3763 int rc; /* Return code */
3764
3765 assert( pPager->eState==PAGER_WRITER_CACHEMOD
3766 || pPager->eState==PAGER_WRITER_DBMOD
3767 );
3768 assert( assert_pager_state(pPager) );
3769 assert( !pagerUseWal(pPager) );
3770
3771 rc = sqlite3PagerExclusiveLock(pPager);
3772 if( rc!=SQLITE_OK ) return rc;
3773
3774 if( !pPager->noSync ){
2726 assert( !pPager->tempFile ); 3775 assert( !pPager->tempFile );
2727 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){ 3776 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
2728 int rc; /* Return code */
2729 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); 3777 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2730 assert( isOpen(pPager->jfd) ); 3778 assert( isOpen(pPager->jfd) );
2731 3779
2732 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ 3780 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
2733 /* This block deals with an obscure problem. If the last connection 3781 /* This block deals with an obscure problem. If the last connection
2734 ** that wrote to this database was operating in persistent-journal 3782 ** that wrote to this database was operating in persistent-journal
2735 ** mode, then the journal file may at this point actually be larger 3783 ** mode, then the journal file may at this point actually be larger
2736 ** than Pager.journalOff bytes. If the next thing in the journal 3784 ** than Pager.journalOff bytes. If the next thing in the journal
2737 ** file happens to be a journal-header (written as part of the 3785 ** file happens to be a journal-header (written as part of the
2738 ** previous connections transaction), and a crash or power-failure 3786 ** previous connection's transaction), and a crash or power-failure
2739 ** occurs after nRec is updated but before this connection writes 3787 ** occurs after nRec is updated but before this connection writes
2740 ** anything else to the journal file (or commits/rolls back its 3788 ** anything else to the journal file (or commits/rolls back its
2741 ** transaction), then SQLite may become confused when doing the 3789 ** transaction), then SQLite may become confused when doing the
2742 ** hot-journal rollback following recovery. It may roll back all 3790 ** hot-journal rollback following recovery. It may roll back all
2743 ** of this connections data, then proceed to rolling back the old, 3791 ** of this connections data, then proceed to rolling back the old,
2744 ** out-of-date data that follows it. Database corruption. 3792 ** out-of-date data that follows it. Database corruption.
2745 ** 3793 **
2746 ** To work around this, if the journal file does appear to contain 3794 ** To work around this, if the journal file does appear to contain
2747 ** a valid header following Pager.journalOff, then write a 0x00 3795 ** a valid header following Pager.journalOff, then write a 0x00
2748 ** byte to the start of it to prevent it from being recognized. 3796 ** byte to the start of it to prevent it from being recognized.
2749 ** 3797 **
2750 ** Variable iNextHdrOffset is set to the offset at which this 3798 ** Variable iNextHdrOffset is set to the offset at which this
2751 ** problematic header will occur, if it exists. aMagic is used 3799 ** problematic header will occur, if it exists. aMagic is used
2752 ** as a temporary buffer to inspect the first couple of bytes of 3800 ** as a temporary buffer to inspect the first couple of bytes of
2753 ** the potential journal header. 3801 ** the potential journal header.
2754 */ 3802 */
2755 i64 iNextHdrOffset; 3803 i64 iNextHdrOffset;
2756 u8 aMagic[8]; 3804 u8 aMagic[8];
2757 » u8 zHeader[sizeof(aJournalMagic)+4]; 3805 u8 zHeader[sizeof(aJournalMagic)+4];
2758 3806
2759 » memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); 3807 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
2760 » put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec); 3808 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
2761 3809
2762 iNextHdrOffset = journalHdrOffset(pPager); 3810 iNextHdrOffset = journalHdrOffset(pPager);
2763 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset); 3811 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
2764 if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){ 3812 if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
2765 static const u8 zerobyte = 0; 3813 static const u8 zerobyte = 0;
2766 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset); 3814 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
2767 } 3815 }
2768 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ 3816 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
2769 return rc; 3817 return rc;
2770 } 3818 }
(...skipping 11 matching lines...) Expand all
2782 */ 3830 */
2783 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ 3831 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2784 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager))); 3832 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
2785 IOTRACE(("JSYNC %p\n", pPager)) 3833 IOTRACE(("JSYNC %p\n", pPager))
2786 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags); 3834 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
2787 if( rc!=SQLITE_OK ) return rc; 3835 if( rc!=SQLITE_OK ) return rc;
2788 } 3836 }
2789 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr)); 3837 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
2790 rc = sqlite3OsWrite( 3838 rc = sqlite3OsWrite(
2791 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr 3839 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
2792 » ); 3840 );
2793 if( rc!=SQLITE_OK ) return rc; 3841 if( rc!=SQLITE_OK ) return rc;
2794 } 3842 }
2795 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ 3843 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2796 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager))); 3844 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
2797 IOTRACE(("JSYNC %p\n", pPager)) 3845 IOTRACE(("JSYNC %p\n", pPager))
2798 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| 3846 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2799 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) 3847 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2800 ); 3848 );
2801 if( rc!=SQLITE_OK ) return rc; 3849 if( rc!=SQLITE_OK ) return rc;
2802 } 3850 }
3851
3852 pPager->journalHdr = pPager->journalOff;
3853 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
3854 pPager->nRec = 0;
3855 rc = writeJournalHdr(pPager);
3856 if( rc!=SQLITE_OK ) return rc;
3857 }
3858 }else{
3859 pPager->journalHdr = pPager->journalOff;
2803 } 3860 }
2804
2805 /* The journal file was just successfully synced. Set Pager.needSync
2806 ** to zero and clear the PGHDR_NEED_SYNC flag on all pagess.
2807 */
2808 pPager->needSync = 0;
2809 pPager->journalStarted = 1;
2810 sqlite3PcacheClearSyncFlags(pPager->pPCache);
2811 } 3861 }
2812 3862
3863 /* Unless the pager is in noSync mode, the journal file was just
3864 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
3865 ** all pages.
3866 */
3867 sqlite3PcacheClearSyncFlags(pPager->pPCache);
3868 pPager->eState = PAGER_WRITER_DBMOD;
3869 assert( assert_pager_state(pPager) );
2813 return SQLITE_OK; 3870 return SQLITE_OK;
2814 } 3871 }
2815 3872
2816 /* 3873 /*
2817 ** The argument is the first in a linked list of dirty pages connected 3874 ** The argument is the first in a linked list of dirty pages connected
2818 ** by the PgHdr.pDirty pointer. This function writes each one of the 3875 ** by the PgHdr.pDirty pointer. This function writes each one of the
2819 ** in-memory pages in the list to the database file. The argument may 3876 ** in-memory pages in the list to the database file. The argument may
2820 ** be NULL, representing an empty list. In this case this function is 3877 ** be NULL, representing an empty list. In this case this function is
2821 ** a no-op. 3878 ** a no-op.
2822 ** 3879 **
(...skipping 15 matching lines...) Expand all
2838 ** 3895 **
2839 ** If writing out a page causes the database file to grow, Pager.dbFileSize 3896 ** If writing out a page causes the database file to grow, Pager.dbFileSize
2840 ** is updated accordingly. If page 1 is written out, then the value cached 3897 ** is updated accordingly. If page 1 is written out, then the value cached
2841 ** in Pager.dbFileVers[] is updated to match the new value stored in 3898 ** in Pager.dbFileVers[] is updated to match the new value stored in
2842 ** the database file. 3899 ** the database file.
2843 ** 3900 **
2844 ** If everything is successful, SQLITE_OK is returned. If an IO error 3901 ** If everything is successful, SQLITE_OK is returned. If an IO error
2845 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot 3902 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
2846 ** be obtained, SQLITE_BUSY is returned. 3903 ** be obtained, SQLITE_BUSY is returned.
2847 */ 3904 */
2848 static int pager_write_pagelist(PgHdr *pList){ 3905 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
2849 Pager *pPager; /* Pager object */ 3906 int rc = SQLITE_OK; /* Return code */
2850 int rc; /* Return code */
2851 3907
2852 if( NEVER(pList==0) ) return SQLITE_OK; 3908 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
2853 pPager = pList->pPager; 3909 assert( !pagerUseWal(pPager) );
2854 3910 assert( pPager->eState==PAGER_WRITER_DBMOD );
2855 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the 3911 assert( pPager->eLock==EXCLUSIVE_LOCK );
2856 ** database file. If there is already an EXCLUSIVE lock, the following
2857 ** call is a no-op.
2858 **
2859 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2860 ** through an intermediate state PENDING. A PENDING lock prevents new
2861 ** readers from attaching to the database but is unsufficient for us to
2862 ** write. The idea of a PENDING lock is to prevent new readers from
2863 ** coming in while we wait for existing readers to clear.
2864 **
2865 ** While the pager is in the RESERVED state, the original database file
2866 ** is unchanged and we can rollback without having to playback the
2867 ** journal into the original database file. Once we transition to
2868 ** EXCLUSIVE, it means the database file has been changed and any rollback
2869 ** will require a journal playback.
2870 */
2871 assert( pPager->state>=PAGER_RESERVED );
2872 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2873 3912
2874 /* If the file is a temp-file has not yet been opened, open it now. It 3913 /* If the file is a temp-file has not yet been opened, open it now. It
2875 ** is not possible for rc to be other than SQLITE_OK if this branch 3914 ** is not possible for rc to be other than SQLITE_OK if this branch
2876 ** is taken, as pager_wait_on_lock() is a no-op for temp-files. 3915 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
2877 */ 3916 */
2878 if( !isOpen(pPager->fd) ){ 3917 if( !isOpen(pPager->fd) ){
2879 assert( pPager->tempFile && rc==SQLITE_OK ); 3918 assert( pPager->tempFile && rc==SQLITE_OK );
2880 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags); 3919 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
2881 } 3920 }
2882 3921
3922 /* Before the first write, give the VFS a hint of what the final
3923 ** file size will be.
3924 */
3925 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
3926 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
3927 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
3928 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
3929 pPager->dbHintSize = pPager->dbSize;
3930 }
3931
2883 while( rc==SQLITE_OK && pList ){ 3932 while( rc==SQLITE_OK && pList ){
2884 Pgno pgno = pList->pgno; 3933 Pgno pgno = pList->pgno;
2885 3934
2886 /* If there are dirty pages in the page cache with page numbers greater 3935 /* If there are dirty pages in the page cache with page numbers greater
2887 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to 3936 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
2888 ** make the file smaller (presumably by auto-vacuum code). Do not write 3937 ** make the file smaller (presumably by auto-vacuum code). Do not write
2889 ** any such pages to the file. 3938 ** any such pages to the file.
2890 ** 3939 **
2891 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag 3940 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
2892 ** set (set by sqlite3PagerDontWrite()). 3941 ** set (set by sqlite3PagerDontWrite()).
2893 */ 3942 */
2894 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){ 3943 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
2895 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */ 3944 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
2896 char *pData; /* Data to write */ 3945 char *pData; /* Data to write */
2897 3946
3947 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
3948
2898 /* Encode the database */ 3949 /* Encode the database */
2899 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData); 3950 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
2900 3951
2901 /* Write out the page data. */ 3952 /* Write out the page data. */
2902 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); 3953 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
2903 3954
2904 /* If page 1 was just written, update Pager.dbFileVers to match 3955 /* If page 1 was just written, update Pager.dbFileVers to match
2905 ** the value now stored in the database file. If writing this 3956 ** the value now stored in the database file. If writing this
2906 ** page caused the database file to grow, update dbFileSize. 3957 ** page caused the database file to grow, update dbFileSize.
2907 */ 3958 */
2908 if( pgno==1 ){ 3959 if( pgno==1 ){
2909 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); 3960 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2910 } 3961 }
2911 if( pgno>pPager->dbFileSize ){ 3962 if( pgno>pPager->dbFileSize ){
2912 pPager->dbFileSize = pgno; 3963 pPager->dbFileSize = pgno;
2913 } 3964 }
2914 3965
2915 /* Update any backup objects copying the contents of this pager. */ 3966 /* Update any backup objects copying the contents of this pager. */
2916 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData); 3967 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
2917 3968
2918 PAGERTRACE(("STORE %d page %d hash(%08x)\n", 3969 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
2919 PAGERID(pPager), pgno, pager_pagehash(pList))); 3970 PAGERID(pPager), pgno, pager_pagehash(pList)));
2920 IOTRACE(("PGOUT %p %d\n", pPager, pgno)); 3971 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
2921 PAGER_INCR(sqlite3_pager_writedb_count); 3972 PAGER_INCR(sqlite3_pager_writedb_count);
2922 PAGER_INCR(pPager->nWrite); 3973 PAGER_INCR(pPager->nWrite);
2923 }else{ 3974 }else{
2924 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno)); 3975 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
2925 } 3976 }
2926 #ifdef SQLITE_CHECK_PAGES 3977 pager_set_pagehash(pList);
2927 pList->pageHash = pager_pagehash(pList);
2928 #endif
2929 pList = pList->pDirty; 3978 pList = pList->pDirty;
2930 } 3979 }
2931 3980
2932 return rc; 3981 return rc;
2933 } 3982 }
2934 3983
2935 /* 3984 /*
3985 ** Ensure that the sub-journal file is open. If it is already open, this
3986 ** function is a no-op.
3987 **
3988 ** SQLITE_OK is returned if everything goes according to plan. An
3989 ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
3990 ** fails.
3991 */
3992 static int openSubJournal(Pager *pPager){
3993 int rc = SQLITE_OK;
3994 if( !isOpen(pPager->sjfd) ){
3995 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
3996 sqlite3MemJournalOpen(pPager->sjfd);
3997 }else{
3998 rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
3999 }
4000 }
4001 return rc;
4002 }
4003
4004 /*
2936 ** Append a record of the current state of page pPg to the sub-journal. 4005 ** Append a record of the current state of page pPg to the sub-journal.
2937 ** It is the callers responsibility to use subjRequiresPage() to check 4006 ** It is the callers responsibility to use subjRequiresPage() to check
2938 ** that it is really required before calling this function. 4007 ** that it is really required before calling this function.
2939 ** 4008 **
2940 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs 4009 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
2941 ** for all open savepoints before returning. 4010 ** for all open savepoints before returning.
2942 ** 4011 **
2943 ** This function returns SQLITE_OK if everything is successful, an IO 4012 ** This function returns SQLITE_OK if everything is successful, an IO
2944 ** error code if the attempt to write to the sub-journal fails, or 4013 ** error code if the attempt to write to the sub-journal fails, or
2945 ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint 4014 ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
2946 ** bitvec. 4015 ** bitvec.
2947 */ 4016 */
2948 static int subjournalPage(PgHdr *pPg){ 4017 static int subjournalPage(PgHdr *pPg){
2949 int rc = SQLITE_OK; 4018 int rc = SQLITE_OK;
2950 Pager *pPager = pPg->pPager; 4019 Pager *pPager = pPg->pPager;
2951 if( isOpen(pPager->sjfd) ){ 4020 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
2952 void *pData = pPg->pData;
2953 i64 offset = pPager->nSubRec*(4+pPager->pageSize);
2954 char *pData2;
2955 4021
2956 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); 4022 /* Open the sub-journal, if it has not already been opened */
2957 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno)); 4023 assert( pPager->useJournal );
4024 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
4025 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
4026 assert( pagerUseWal(pPager)
4027 || pageInJournal(pPg)
4028 || pPg->pgno>pPager->dbOrigSize
4029 );
4030 rc = openSubJournal(pPager);
4031
4032 /* If the sub-journal was opened successfully (or was already open),
4033 ** write the journal record into the file. */
4034 if( rc==SQLITE_OK ){
4035 void *pData = pPg->pData;
4036 i64 offset = pPager->nSubRec*(4+pPager->pageSize);
4037 char *pData2;
2958 4038
2959 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); 4039 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
2960 rc = write32bits(pPager->sjfd, offset, pPg->pgno); 4040 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
2961 if( rc==SQLITE_OK ){ 4041 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
2962 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4); 4042 if( rc==SQLITE_OK ){
4043 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
4044 }
2963 } 4045 }
2964 } 4046 }
2965 if( rc==SQLITE_OK ){ 4047 if( rc==SQLITE_OK ){
2966 pPager->nSubRec++; 4048 pPager->nSubRec++;
2967 assert( pPager->nSavepoint>0 ); 4049 assert( pPager->nSavepoint>0 );
2968 rc = addToSavepointBitvecs(pPager, pPg->pgno); 4050 rc = addToSavepointBitvecs(pPager, pPg->pgno);
2969 } 4051 }
2970 return rc; 4052 return rc;
2971 } 4053 }
2972 4054
2973
2974 /* 4055 /*
2975 ** This function is called by the pcache layer when it has reached some 4056 ** This function is called by the pcache layer when it has reached some
2976 ** soft memory limit. The first argument is a pointer to a Pager object 4057 ** soft memory limit. The first argument is a pointer to a Pager object
2977 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory 4058 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
2978 ** database). The second argument is a reference to a page that is 4059 ** database). The second argument is a reference to a page that is
2979 ** currently dirty but has no outstanding references. The page 4060 ** currently dirty but has no outstanding references. The page
2980 ** is always associated with the Pager object passed as the first 4061 ** is always associated with the Pager object passed as the first
2981 ** argument. 4062 ** argument.
2982 ** 4063 **
2983 ** The job of this function is to make pPg clean by writing its contents 4064 ** The job of this function is to make pPg clean by writing its contents
2984 ** out to the database file, if possible. This may involve syncing the 4065 ** out to the database file, if possible. This may involve syncing the
2985 ** journal file. 4066 ** journal file.
2986 ** 4067 **
2987 ** If successful, sqlite3PcacheMakeClean() is called on the page and 4068 ** If successful, sqlite3PcacheMakeClean() is called on the page and
2988 ** SQLITE_OK returned. If an IO error occurs while trying to make the 4069 ** SQLITE_OK returned. If an IO error occurs while trying to make the
2989 ** page clean, the IO error code is returned. If the page cannot be 4070 ** page clean, the IO error code is returned. If the page cannot be
2990 ** made clean for some other reason, but no error occurs, then SQLITE_OK 4071 ** made clean for some other reason, but no error occurs, then SQLITE_OK
2991 ** is returned by sqlite3PcacheMakeClean() is not called. 4072 ** is returned by sqlite3PcacheMakeClean() is not called.
2992 */ 4073 */
2993 static int pagerStress(void *p, PgHdr *pPg){ 4074 static int pagerStress(void *p, PgHdr *pPg){
2994 Pager *pPager = (Pager *)p; 4075 Pager *pPager = (Pager *)p;
2995 int rc = SQLITE_OK; 4076 int rc = SQLITE_OK;
2996 4077
2997 assert( pPg->pPager==pPager ); 4078 assert( pPg->pPager==pPager );
2998 assert( pPg->flags&PGHDR_DIRTY ); 4079 assert( pPg->flags&PGHDR_DIRTY );
2999 4080
3000 /* The doNotSync flag is set by the sqlite3PagerWrite() function while it 4081 /* The doNotSyncSpill flag is set during times when doing a sync of
3001 ** is journalling a set of two or more database pages that are stored 4082 ** journal (and adding a new header) is not allowed. This occurs
3002 ** on the same disk sector. Syncing the journal is not allowed while 4083 ** during calls to sqlite3PagerWrite() while trying to journal multiple
3003 ** this is happening as it is important that all members of such a 4084 ** pages belonging to the same sector.
3004 ** set of pages are synced to disk together. So, if the page this function
3005 ** is trying to make clean will require a journal sync and the doNotSync
3006 ** flag is set, return without doing anything. The pcache layer will
3007 ** just have to go ahead and allocate a new page buffer instead of
3008 ** reusing pPg.
3009 ** 4085 **
3010 ** Similarly, if the pager has already entered the error state, do not 4086 ** The doNotSpill flag inhibits all cache spilling regardless of whether
3011 ** try to write the contents of pPg to disk. 4087 ** or not a sync is required. This is set during a rollback.
4088 **
4089 ** Spilling is also prohibited when in an error state since that could
4090 ** lead to database corruption. In the current implementaton it
4091 ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
4092 ** while in the error state, hence it is impossible for this routine to
4093 ** be called in the error state. Nevertheless, we include a NEVER()
4094 ** test for the error state as a safeguard against future changes.
3012 */ 4095 */
3013 if( NEVER(pPager->errCode) 4096 if( NEVER(pPager->errCode) ) return SQLITE_OK;
3014 || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC) 4097 if( pPager->doNotSpill ) return SQLITE_OK;
3015 ){ 4098 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
3016 return SQLITE_OK; 4099 return SQLITE_OK;
3017 } 4100 }
3018 4101
3019 /* Sync the journal file if required. */ 4102 pPg->pDirty = 0;
3020 if( pPg->flags&PGHDR_NEED_SYNC ){ 4103 if( pagerUseWal(pPager) ){
3021 rc = syncJournal(pPager); 4104 /* Write a single frame for this page to the log. */
3022 if( rc==SQLITE_OK && pPager->fullSync && 4105 if( subjRequiresPage(pPg) ){
3023 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) && 4106 rc = subjournalPage(pPg);
3024 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 4107 }
4108 if( rc==SQLITE_OK ){
4109 rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
4110 }
4111 }else{
4112
4113 /* Sync the journal file if required. */
4114 if( pPg->flags&PGHDR_NEED_SYNC
4115 || pPager->eState==PAGER_WRITER_CACHEMOD
3025 ){ 4116 ){
3026 pPager->nRec = 0; 4117 rc = syncJournal(pPager, 1);
3027 rc = writeJournalHdr(pPager); 4118 }
4119
4120 /* If the page number of this page is larger than the current size of
4121 ** the database image, it may need to be written to the sub-journal.
4122 ** This is because the call to pager_write_pagelist() below will not
4123 ** actually write data to the file in this case.
4124 **
4125 ** Consider the following sequence of events:
4126 **
4127 ** BEGIN;
4128 ** <journal page X>
4129 ** <modify page X>
4130 ** SAVEPOINT sp;
4131 ** <shrink database file to Y pages>
4132 ** pagerStress(page X)
4133 ** ROLLBACK TO sp;
4134 **
4135 ** If (X>Y), then when pagerStress is called page X will not be written
4136 ** out to the database file, but will be dropped from the cache. Then,
4137 ** following the "ROLLBACK TO sp" statement, reading page X will read
4138 ** data from the database file. This will be the copy of page X as it
4139 ** was when the transaction started, not as it was when "SAVEPOINT sp"
4140 ** was executed.
4141 **
4142 ** The solution is to write the current data for page X into the
4143 ** sub-journal file now (if it is not already there), so that it will
4144 ** be restored to its current value when the "ROLLBACK TO sp" is
4145 ** executed.
4146 */
4147 if( NEVER(
4148 rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
4149 ) ){
4150 rc = subjournalPage(pPg);
4151 }
4152
4153 /* Write the contents of the page out to the database file. */
4154 if( rc==SQLITE_OK ){
4155 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
4156 rc = pager_write_pagelist(pPager, pPg);
3028 } 4157 }
3029 } 4158 }
3030 4159
3031 /* If the page number of this page is larger than the current size of
3032 ** the database image, it may need to be written to the sub-journal.
3033 ** This is because the call to pager_write_pagelist() below will not
3034 ** actually write data to the file in this case.
3035 **
3036 ** Consider the following sequence of events:
3037 **
3038 ** BEGIN;
3039 ** <journal page X>
3040 ** <modify page X>
3041 ** SAVEPOINT sp;
3042 ** <shrink database file to Y pages>
3043 ** pagerStress(page X)
3044 ** ROLLBACK TO sp;
3045 **
3046 ** If (X>Y), then when pagerStress is called page X will not be written
3047 ** out to the database file, but will be dropped from the cache. Then,
3048 ** following the "ROLLBACK TO sp" statement, reading page X will read
3049 ** data from the database file. This will be the copy of page X as it
3050 ** was when the transaction started, not as it was when "SAVEPOINT sp"
3051 ** was executed.
3052 **
3053 ** The solution is to write the current data for page X into the
3054 ** sub-journal file now (if it is not already there), so that it will
3055 ** be restored to its current value when the "ROLLBACK TO sp" is
3056 ** executed.
3057 */
3058 if( NEVER(
3059 rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
3060 ) ){
3061 rc = subjournalPage(pPg);
3062 }
3063
3064 /* Write the contents of the page out to the database file. */
3065 if( rc==SQLITE_OK ){
3066 pPg->pDirty = 0;
3067 rc = pager_write_pagelist(pPg);
3068 }
3069
3070 /* Mark the page as clean. */ 4160 /* Mark the page as clean. */
3071 if( rc==SQLITE_OK ){ 4161 if( rc==SQLITE_OK ){
3072 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno)); 4162 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
3073 sqlite3PcacheMakeClean(pPg); 4163 sqlite3PcacheMakeClean(pPg);
3074 } 4164 }
3075 4165
3076 return pager_error(pPager, rc); 4166 return pager_error(pPager, rc);
3077 } 4167 }
3078 4168
3079 4169
3080 /* 4170 /*
3081 ** Allocate and initialize a new Pager object and put a pointer to it 4171 ** Allocate and initialize a new Pager object and put a pointer to it
3082 ** in *ppPager. The pager should eventually be freed by passing it 4172 ** in *ppPager. The pager should eventually be freed by passing it
3083 ** to sqlite3PagerClose(). 4173 ** to sqlite3PagerClose().
3084 ** 4174 **
3085 ** The zFilename argument is the path to the database file to open. 4175 ** The zFilename argument is the path to the database file to open.
3086 ** If zFilename is NULL then a randomly-named temporary file is created 4176 ** If zFilename is NULL then a randomly-named temporary file is created
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
3121 int rc = SQLITE_OK; /* Return code */ 4211 int rc = SQLITE_OK; /* Return code */
3122 int tempFile = 0; /* True for temp files (incl. in-memory files) */ 4212 int tempFile = 0; /* True for temp files (incl. in-memory files) */
3123 int memDb = 0; /* True if this is an in-memory file */ 4213 int memDb = 0; /* True if this is an in-memory file */
3124 int readOnly = 0; /* True if this is a read-only file */ 4214 int readOnly = 0; /* True if this is a read-only file */
3125 int journalFileSize; /* Bytes to allocate for each journal fd */ 4215 int journalFileSize; /* Bytes to allocate for each journal fd */
3126 char *zPathname = 0; /* Full path to database file */ 4216 char *zPathname = 0; /* Full path to database file */
3127 int nPathname = 0; /* Number of bytes in zPathname */ 4217 int nPathname = 0; /* Number of bytes in zPathname */
3128 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */ 4218 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
3129 int noReadlock = (flags & PAGER_NO_READLOCK)!=0; /* True to omit read-lock */ 4219 int noReadlock = (flags & PAGER_NO_READLOCK)!=0; /* True to omit read-lock */
3130 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */ 4220 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
3131 u16 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */ 4221 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
3132 4222
3133 /* Figure out how much space is required for each journal file-handle 4223 /* Figure out how much space is required for each journal file-handle
3134 ** (there are two of them, the main journal and the sub-journal). This 4224 ** (there are two of them, the main journal and the sub-journal). This
3135 ** is the maximum space required for an in-memory journal file handle 4225 ** is the maximum space required for an in-memory journal file handle
3136 ** and a regular journal file-handle. Note that a "regular journal-handle" 4226 ** and a regular journal file-handle. Note that a "regular journal-handle"
3137 ** may be a wrapper capable of caching the first portion of the journal 4227 ** may be a wrapper capable of caching the first portion of the journal
3138 ** file in memory to implement the atomic-write optimization (see 4228 ** file in memory to implement the atomic-write optimization (see
3139 ** source file journal.c). 4229 ** source file journal.c).
3140 */ 4230 */
3141 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){ 4231 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
3142 journalFileSize = ROUND8(sqlite3JournalSize(pVfs)); 4232 journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
3143 }else{ 4233 }else{
3144 journalFileSize = ROUND8(sqlite3MemJournalSize()); 4234 journalFileSize = ROUND8(sqlite3MemJournalSize());
3145 } 4235 }
3146 4236
3147 /* Set the output variable to NULL in case an error occurs. */ 4237 /* Set the output variable to NULL in case an error occurs. */
3148 *ppPager = 0; 4238 *ppPager = 0;
3149 4239
4240 #ifndef SQLITE_OMIT_MEMORYDB
4241 if( flags & PAGER_MEMORY ){
4242 memDb = 1;
4243 zFilename = 0;
4244 }
4245 #endif
4246
3150 /* Compute and store the full pathname in an allocated buffer pointed 4247 /* Compute and store the full pathname in an allocated buffer pointed
3151 ** to by zPathname, length nPathname. Or, if this is a temporary file, 4248 ** to by zPathname, length nPathname. Or, if this is a temporary file,
3152 ** leave both nPathname and zPathname set to 0. 4249 ** leave both nPathname and zPathname set to 0.
3153 */ 4250 */
3154 if( zFilename && zFilename[0] ){ 4251 if( zFilename && zFilename[0] ){
3155 nPathname = pVfs->mxPathname+1; 4252 nPathname = pVfs->mxPathname+1;
3156 zPathname = sqlite3Malloc(nPathname*2); 4253 zPathname = sqlite3Malloc(nPathname*2);
3157 if( zPathname==0 ){ 4254 if( zPathname==0 ){
3158 return SQLITE_NOMEM; 4255 return SQLITE_NOMEM;
3159 } 4256 }
3160 #ifndef SQLITE_OMIT_MEMORYDB 4257 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
3161 if( strcmp(zFilename,":memory:")==0 ){ 4258 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
3162 memDb = 1;
3163 zPathname[0] = 0;
3164 }else
3165 #endif
3166 {
3167 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
3168 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
3169 }
3170
3171 nPathname = sqlite3Strlen30(zPathname); 4259 nPathname = sqlite3Strlen30(zPathname);
3172 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){ 4260 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
3173 /* This branch is taken when the journal path required by 4261 /* This branch is taken when the journal path required by
3174 ** the database being opened will be more than pVfs->mxPathname 4262 ** the database being opened will be more than pVfs->mxPathname
3175 ** bytes in length. This means the database cannot be opened, 4263 ** bytes in length. This means the database cannot be opened,
3176 ** as it will not be possible to open the journal file or even 4264 ** as it will not be possible to open the journal file or even
3177 ** check for a hot-journal before reading. 4265 ** check for a hot-journal before reading.
3178 */ 4266 */
3179 rc = SQLITE_CANTOPEN; 4267 rc = SQLITE_CANTOPEN_BKPT;
3180 } 4268 }
3181 if( rc!=SQLITE_OK ){ 4269 if( rc!=SQLITE_OK ){
3182 sqlite3_free(zPathname); 4270 sqlite3_free(zPathname);
3183 return rc; 4271 return rc;
3184 } 4272 }
3185 } 4273 }
3186 4274
3187 /* Allocate memory for the Pager structure, PCache object, the 4275 /* Allocate memory for the Pager structure, PCache object, the
3188 ** three file descriptors, the database file name and the journal 4276 ** three file descriptors, the database file name and the journal
3189 ** file name. The layout in memory is as follows: 4277 ** file name. The layout in memory is as follows:
3190 ** 4278 **
3191 ** Pager object (sizeof(Pager) bytes) 4279 ** Pager object (sizeof(Pager) bytes)
3192 ** PCache object (sqlite3PcacheSize() bytes) 4280 ** PCache object (sqlite3PcacheSize() bytes)
3193 ** Database file handle (pVfs->szOsFile bytes) 4281 ** Database file handle (pVfs->szOsFile bytes)
3194 ** Sub-journal file handle (journalFileSize bytes) 4282 ** Sub-journal file handle (journalFileSize bytes)
3195 ** Main journal file handle (journalFileSize bytes) 4283 ** Main journal file handle (journalFileSize bytes)
3196 ** Database file name (nPathname+1 bytes) 4284 ** Database file name (nPathname+1 bytes)
3197 ** Journal file name (nPathname+8+1 bytes) 4285 ** Journal file name (nPathname+8+1 bytes)
3198 */ 4286 */
3199 pPtr = (u8 *)sqlite3MallocZero( 4287 pPtr = (u8 *)sqlite3MallocZero(
3200 ROUND8(sizeof(*pPager)) + /* Pager structure */ 4288 ROUND8(sizeof(*pPager)) + /* Pager structure */
3201 ROUND8(pcacheSize) + /* PCache object */ 4289 ROUND8(pcacheSize) + /* PCache object */
3202 ROUND8(pVfs->szOsFile) + /* The main db file */ 4290 ROUND8(pVfs->szOsFile) + /* The main db file */
3203 journalFileSize * 2 + /* The two journal files */ 4291 journalFileSize * 2 + /* The two journal files */
3204 nPathname + 1 + /* zFilename */ 4292 nPathname + 1 + /* zFilename */
3205 nPathname + 8 + 1 /* zJournal */ 4293 nPathname + 8 + 1 /* zJournal */
4294 #ifndef SQLITE_OMIT_WAL
4295 + nPathname + 4 + 1 /* zWal */
4296 #endif
3206 ); 4297 );
3207 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) ); 4298 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
3208 if( !pPtr ){ 4299 if( !pPtr ){
3209 sqlite3_free(zPathname); 4300 sqlite3_free(zPathname);
3210 return SQLITE_NOMEM; 4301 return SQLITE_NOMEM;
3211 } 4302 }
3212 pPager = (Pager*)(pPtr); 4303 pPager = (Pager*)(pPtr);
3213 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager))); 4304 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
3214 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize)); 4305 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
3215 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile)); 4306 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
3216 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize); 4307 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
3217 pPager->zFilename = (char*)(pPtr += journalFileSize); 4308 pPager->zFilename = (char*)(pPtr += journalFileSize);
3218 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); 4309 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
3219 4310
3220 /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */ 4311 /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
3221 if( zPathname ){ 4312 if( zPathname ){
4313 assert( nPathname>0 );
3222 pPager->zJournal = (char*)(pPtr += nPathname + 1); 4314 pPager->zJournal = (char*)(pPtr += nPathname + 1);
3223 memcpy(pPager->zFilename, zPathname, nPathname); 4315 memcpy(pPager->zFilename, zPathname, nPathname);
3224 memcpy(pPager->zJournal, zPathname, nPathname); 4316 memcpy(pPager->zJournal, zPathname, nPathname);
3225 memcpy(&pPager->zJournal[nPathname], "-journal", 8); 4317 memcpy(&pPager->zJournal[nPathname], "-journal", 8);
3226 if( pPager->zFilename[0]==0 ) pPager->zJournal[0] = 0; 4318 #ifndef SQLITE_OMIT_WAL
4319 pPager->zWal = &pPager->zJournal[nPathname+8+1];
4320 memcpy(pPager->zWal, zPathname, nPathname);
4321 memcpy(&pPager->zWal[nPathname], "-wal", 4);
4322 #endif
3227 sqlite3_free(zPathname); 4323 sqlite3_free(zPathname);
3228 } 4324 }
3229 pPager->pVfs = pVfs; 4325 pPager->pVfs = pVfs;
3230 pPager->vfsFlags = vfsFlags; 4326 pPager->vfsFlags = vfsFlags;
3231 4327
3232 /* Open the pager file. 4328 /* Open the pager file.
3233 */ 4329 */
3234 if( zFilename && zFilename[0] && !memDb ){ 4330 if( zFilename && zFilename[0] ){
3235 int fout = 0; /* VFS flags returned by xOpen() */ 4331 int fout = 0; /* VFS flags returned by xOpen() */
3236 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout); 4332 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
4333 assert( !memDb );
3237 readOnly = (fout&SQLITE_OPEN_READONLY); 4334 readOnly = (fout&SQLITE_OPEN_READONLY);
3238 4335
3239 /* If the file was successfully opened for read/write access, 4336 /* If the file was successfully opened for read/write access,
3240 ** choose a default page size in case we have to create the 4337 ** choose a default page size in case we have to create the
3241 ** database file. The default page size is the maximum of: 4338 ** database file. The default page size is the maximum of:
3242 ** 4339 **
3243 ** + SQLITE_DEFAULT_PAGE_SIZE, 4340 ** + SQLITE_DEFAULT_PAGE_SIZE,
3244 ** + The value returned by sqlite3OsSectorSize() 4341 ** + The value returned by sqlite3OsSectorSize()
3245 ** + The largest page size that can be written atomically. 4342 ** + The largest page size that can be written atomically.
3246 */ 4343 */
3247 if( rc==SQLITE_OK && !readOnly ){ 4344 if( rc==SQLITE_OK && !readOnly ){
3248 setSectorSize(pPager); 4345 setSectorSize(pPager);
3249 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE); 4346 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
3250 if( szPageDflt<pPager->sectorSize ){ 4347 if( szPageDflt<pPager->sectorSize ){
3251 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ 4348 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
3252 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE; 4349 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
3253 }else{ 4350 }else{
3254 szPageDflt = (u16)pPager->sectorSize; 4351 szPageDflt = (u32)pPager->sectorSize;
3255 } 4352 }
3256 } 4353 }
3257 #ifdef SQLITE_ENABLE_ATOMIC_WRITE 4354 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
3258 { 4355 {
3259 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); 4356 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
3260 int ii; 4357 int ii;
3261 assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); 4358 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
3262 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); 4359 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
3263 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536); 4360 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
3264 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){ 4361 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
3265 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){ 4362 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
3266 szPageDflt = ii; 4363 szPageDflt = ii;
3267 } 4364 }
3268 } 4365 }
3269 } 4366 }
3270 #endif 4367 #endif
3271 } 4368 }
3272 }else{ 4369 }else{
3273 /* If a temporary file is requested, it is not opened immediately. 4370 /* If a temporary file is requested, it is not opened immediately.
3274 ** In this case we accept the default page size and delay actually 4371 ** In this case we accept the default page size and delay actually
3275 ** opening the file until the first call to OsWrite(). 4372 ** opening the file until the first call to OsWrite().
3276 ** 4373 **
3277 ** This branch is also run for an in-memory database. An in-memory 4374 ** This branch is also run for an in-memory database. An in-memory
3278 ** database is the same as a temp-file that is never written out to 4375 ** database is the same as a temp-file that is never written out to
3279 ** disk and uses an in-memory rollback journal. 4376 ** disk and uses an in-memory rollback journal.
3280 */ 4377 */
3281 tempFile = 1; 4378 tempFile = 1;
3282 pPager->state = PAGER_EXCLUSIVE; 4379 pPager->eState = PAGER_READER;
4380 pPager->eLock = EXCLUSIVE_LOCK;
3283 readOnly = (vfsFlags&SQLITE_OPEN_READONLY); 4381 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
3284 } 4382 }
3285 4383
3286 /* The following call to PagerSetPagesize() serves to set the value of 4384 /* The following call to PagerSetPagesize() serves to set the value of
3287 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer. 4385 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
3288 */ 4386 */
3289 if( rc==SQLITE_OK ){ 4387 if( rc==SQLITE_OK ){
3290 assert( pPager->memDb==0 ); 4388 assert( pPager->memDb==0 );
3291 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1); 4389 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
3292 testcase( rc!=SQLITE_OK ); 4390 testcase( rc!=SQLITE_OK );
(...skipping 16 matching lines...) Expand all
3309 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); 4407 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
3310 4408
3311 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename)); 4409 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
3312 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename)) 4410 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
3313 4411
3314 pPager->useJournal = (u8)useJournal; 4412 pPager->useJournal = (u8)useJournal;
3315 pPager->noReadlock = (noReadlock && readOnly) ?1:0; 4413 pPager->noReadlock = (noReadlock && readOnly) ?1:0;
3316 /* pPager->stmtOpen = 0; */ 4414 /* pPager->stmtOpen = 0; */
3317 /* pPager->stmtInUse = 0; */ 4415 /* pPager->stmtInUse = 0; */
3318 /* pPager->nRef = 0; */ 4416 /* pPager->nRef = 0; */
3319 pPager->dbSizeValid = (u8)memDb;
3320 /* pPager->stmtSize = 0; */ 4417 /* pPager->stmtSize = 0; */
3321 /* pPager->stmtJSize = 0; */ 4418 /* pPager->stmtJSize = 0; */
3322 /* pPager->nPage = 0; */ 4419 /* pPager->nPage = 0; */
3323 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; 4420 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
3324 /* pPager->state = PAGER_UNLOCK; */ 4421 /* pPager->state = PAGER_UNLOCK; */
4422 #if 0
3325 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) ); 4423 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
4424 #endif
3326 /* pPager->errMask = 0; */ 4425 /* pPager->errMask = 0; */
3327 pPager->tempFile = (u8)tempFile; 4426 pPager->tempFile = (u8)tempFile;
3328 assert( tempFile==PAGER_LOCKINGMODE_NORMAL 4427 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
3329 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); 4428 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
3330 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); 4429 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
3331 pPager->exclusiveMode = (u8)tempFile; 4430 pPager->exclusiveMode = (u8)tempFile;
3332 pPager->changeCountDone = pPager->tempFile; 4431 pPager->changeCountDone = pPager->tempFile;
3333 pPager->memDb = (u8)memDb; 4432 pPager->memDb = (u8)memDb;
3334 pPager->readOnly = (u8)readOnly; 4433 pPager->readOnly = (u8)readOnly;
3335 /* pPager->needSync = 0; */
3336 assert( useJournal || pPager->tempFile ); 4434 assert( useJournal || pPager->tempFile );
3337 pPager->noSync = pPager->tempFile; 4435 pPager->noSync = pPager->tempFile;
3338 pPager->fullSync = pPager->noSync ?0:1; 4436 pPager->fullSync = pPager->noSync ?0:1;
3339 pPager->sync_flags = SQLITE_SYNC_NORMAL; 4437 pPager->sync_flags = SQLITE_SYNC_NORMAL;
3340 /* pPager->pFirst = 0; */ 4438 /* pPager->pFirst = 0; */
3341 /* pPager->pFirstSynced = 0; */ 4439 /* pPager->pFirstSynced = 0; */
3342 /* pPager->pLast = 0; */ 4440 /* pPager->pLast = 0; */
3343 pPager->nExtra = (u16)nExtra; 4441 pPager->nExtra = (u16)nExtra;
3344 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT; 4442 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
3345 assert( isOpen(pPager->fd) || tempFile ); 4443 assert( isOpen(pPager->fd) || tempFile );
3346 setSectorSize(pPager); 4444 setSectorSize(pPager);
3347 if( !useJournal ){ 4445 if( !useJournal ){
3348 pPager->journalMode = PAGER_JOURNALMODE_OFF; 4446 pPager->journalMode = PAGER_JOURNALMODE_OFF;
3349 }else if( memDb ){ 4447 }else if( memDb ){
3350 pPager->journalMode = PAGER_JOURNALMODE_MEMORY; 4448 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
3351 } 4449 }
3352 /* pPager->xBusyHandler = 0; */ 4450 /* pPager->xBusyHandler = 0; */
3353 /* pPager->pBusyHandlerArg = 0; */ 4451 /* pPager->pBusyHandlerArg = 0; */
3354 pPager->xReiniter = xReinit; 4452 pPager->xReiniter = xReinit;
3355 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ 4453 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
4454
3356 *ppPager = pPager; 4455 *ppPager = pPager;
3357 return SQLITE_OK; 4456 return SQLITE_OK;
3358 } 4457 }
3359 4458
3360 4459
3361 4460
3362 /* 4461 /*
3363 ** This function is called after transitioning from PAGER_UNLOCK to 4462 ** This function is called after transitioning from PAGER_UNLOCK to
3364 ** PAGER_SHARED state. It tests if there is a hot journal present in 4463 ** PAGER_SHARED state. It tests if there is a hot journal present in
3365 ** the file-system for the given pager. A hot journal is one that 4464 ** the file-system for the given pager. A hot journal is one that
(...skipping 19 matching lines...) Expand all
3385 ** will not roll it back. 4484 ** will not roll it back.
3386 ** 4485 **
3387 ** If a hot-journal file is found to exist, *pExists is set to 1 and 4486 ** If a hot-journal file is found to exist, *pExists is set to 1 and
3388 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is 4487 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
3389 ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying 4488 ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
3390 ** to determine whether or not a hot-journal file exists, the IO error 4489 ** to determine whether or not a hot-journal file exists, the IO error
3391 ** code is returned and the value of *pExists is undefined. 4490 ** code is returned and the value of *pExists is undefined.
3392 */ 4491 */
3393 static int hasHotJournal(Pager *pPager, int *pExists){ 4492 static int hasHotJournal(Pager *pPager, int *pExists){
3394 sqlite3_vfs * const pVfs = pPager->pVfs; 4493 sqlite3_vfs * const pVfs = pPager->pVfs;
3395 int rc; /* Return code */ 4494 int rc = SQLITE_OK; /* Return code */
3396 int exists; /* True if a journal file is present */ 4495 int exists = 1; /* True if a journal file is present */
4496 int jrnlOpen = !!isOpen(pPager->jfd);
3397 4497
3398 assert( pPager!=0 );
3399 assert( pPager->useJournal ); 4498 assert( pPager->useJournal );
3400 assert( isOpen(pPager->fd) ); 4499 assert( isOpen(pPager->fd) );
3401 assert( !isOpen(pPager->jfd) ); 4500 assert( pPager->eState==PAGER_OPEN );
3402 assert( pPager->state <= PAGER_SHARED ); 4501
4502 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
4503 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
4504 ));
3403 4505
3404 *pExists = 0; 4506 *pExists = 0;
3405 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists); 4507 if( !jrnlOpen ){
4508 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
4509 }
3406 if( rc==SQLITE_OK && exists ){ 4510 if( rc==SQLITE_OK && exists ){
3407 int locked; /* True if some process holds a RESERVED lock */ 4511 int locked = 0; /* True if some process holds a RESERVED lock */
3408 4512
3409 /* Race condition here: Another process might have been holding the 4513 /* Race condition here: Another process might have been holding the
3410 ** the RESERVED lock and have a journal open at the sqlite3OsAccess() 4514 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
3411 ** call above, but then delete the journal and drop the lock before 4515 ** call above, but then delete the journal and drop the lock before
3412 ** we get to the following sqlite3OsCheckReservedLock() call. If that 4516 ** we get to the following sqlite3OsCheckReservedLock() call. If that
3413 ** is the case, this routine might think there is a hot journal when 4517 ** is the case, this routine might think there is a hot journal when
3414 ** in fact there is none. This results in a false-positive which will 4518 ** in fact there is none. This results in a false-positive which will
3415 ** be dealt with by the playback routine. Ticket #3883. 4519 ** be dealt with by the playback routine. Ticket #3883.
3416 */ 4520 */
3417 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked); 4521 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
3418 if( rc==SQLITE_OK && !locked ){ 4522 if( rc==SQLITE_OK && !locked ){
3419 int nPage; 4523 Pgno nPage; /* Number of pages in database file */
3420 4524
3421 /* Check the size of the database file. If it consists of 0 pages, 4525 /* Check the size of the database file. If it consists of 0 pages,
3422 ** then delete the journal file. See the header comment above for 4526 ** then delete the journal file. See the header comment above for
3423 ** the reasoning here. Delete the obsolete journal file under 4527 ** the reasoning here. Delete the obsolete journal file under
3424 ** a RESERVED lock to avoid race conditions and to avoid violating 4528 ** a RESERVED lock to avoid race conditions and to avoid violating
3425 ** [H33020]. 4529 ** [H33020].
3426 */ 4530 */
3427 rc = sqlite3PagerPagecount(pPager, &nPage); 4531 rc = pagerPagecount(pPager, &nPage);
3428 if( rc==SQLITE_OK ){ 4532 if( rc==SQLITE_OK ){
3429 if( nPage==0 ){ 4533 if( nPage==0 ){
3430 sqlite3BeginBenignMalloc(); 4534 sqlite3BeginBenignMalloc();
3431 if( sqlite3OsLock(pPager->fd, RESERVED_LOCK)==SQLITE_OK ){ 4535 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
3432 sqlite3OsDelete(pVfs, pPager->zJournal, 0); 4536 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3433 sqlite3OsUnlock(pPager->fd, SHARED_LOCK); 4537 pagerUnlockDb(pPager, SHARED_LOCK);
3434 } 4538 }
3435 sqlite3EndBenignMalloc(); 4539 sqlite3EndBenignMalloc();
3436 }else{ 4540 }else{
3437 /* The journal file exists and no other connection has a reserved 4541 /* The journal file exists and no other connection has a reserved
3438 ** or greater lock on the database file. Now check that there is 4542 ** or greater lock on the database file. Now check that there is
3439 ** at least one non-zero bytes at the start of the journal file. 4543 ** at least one non-zero bytes at the start of the journal file.
3440 ** If there is, then we consider this journal to be hot. If not, 4544 ** If there is, then we consider this journal to be hot. If not,
3441 ** it can be ignored. 4545 ** it can be ignored.
3442 */ 4546 */
3443 int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL; 4547 if( !jrnlOpen ){
3444 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f); 4548 int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
4549 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
4550 }
3445 if( rc==SQLITE_OK ){ 4551 if( rc==SQLITE_OK ){
3446 u8 first = 0; 4552 u8 first = 0;
3447 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0); 4553 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
3448 if( rc==SQLITE_IOERR_SHORT_READ ){ 4554 if( rc==SQLITE_IOERR_SHORT_READ ){
3449 rc = SQLITE_OK; 4555 rc = SQLITE_OK;
3450 } 4556 }
3451 sqlite3OsClose(pPager->jfd); 4557 if( !jrnlOpen ){
4558 sqlite3OsClose(pPager->jfd);
4559 }
3452 *pExists = (first!=0); 4560 *pExists = (first!=0);
3453 }else if( rc==SQLITE_CANTOPEN ){ 4561 }else if( rc==SQLITE_CANTOPEN ){
3454 /* If we cannot open the rollback journal file in order to see if 4562 /* If we cannot open the rollback journal file in order to see if
3455 ** its has a zero header, that might be due to an I/O error, or 4563 ** its has a zero header, that might be due to an I/O error, or
3456 ** it might be due to the race condition described above and in 4564 ** it might be due to the race condition described above and in
3457 ** ticket #3883. Either way, assume that the journal is hot. 4565 ** ticket #3883. Either way, assume that the journal is hot.
3458 ** This might be a false positive. But if it is, then the 4566 ** This might be a false positive. But if it is, then the
3459 ** automatic journal playback and recovery mechanism will deal 4567 ** automatic journal playback and recovery mechanism will deal
3460 ** with it under an EXCLUSIVE lock where we do not need to 4568 ** with it under an EXCLUSIVE lock where we do not need to
3461 ** worry so much with race conditions. 4569 ** worry so much with race conditions.
3462 */ 4570 */
3463 *pExists = 1; 4571 *pExists = 1;
3464 rc = SQLITE_OK; 4572 rc = SQLITE_OK;
3465 } 4573 }
3466 } 4574 }
3467 } 4575 }
3468 } 4576 }
3469 } 4577 }
3470 4578
3471 return rc; 4579 return rc;
3472 } 4580 }
3473 4581
3474 /* 4582 /*
3475 ** Read the content for page pPg out of the database file and into
3476 ** pPg->pData. A shared lock or greater must be held on the database
3477 ** file before this function is called.
3478 **
3479 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
3480 ** the value read from the database file.
3481 **
3482 ** If an IO error occurs, then the IO error is returned to the caller.
3483 ** Otherwise, SQLITE_OK is returned.
3484 */
3485 static int readDbPage(PgHdr *pPg){
3486 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
3487 Pgno pgno = pPg->pgno; /* Page number to read */
3488 int rc; /* Return code */
3489 i64 iOffset; /* Byte offset of file to read from */
3490
3491 assert( pPager->state>=PAGER_SHARED && !MEMDB );
3492 assert( isOpen(pPager->fd) );
3493
3494 if( NEVER(!isOpen(pPager->fd)) ){
3495 assert( pPager->tempFile );
3496 memset(pPg->pData, 0, pPager->pageSize);
3497 return SQLITE_OK;
3498 }
3499 iOffset = (pgno-1)*(i64)pPager->pageSize;
3500 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
3501 if( rc==SQLITE_IOERR_SHORT_READ ){
3502 rc = SQLITE_OK;
3503 }
3504 if( pgno==1 ){
3505 u8 *dbFileVers = &((u8*)pPg->pData)[24];
3506 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
3507 }
3508 CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
3509
3510 PAGER_INCR(sqlite3_pager_readdb_count);
3511 PAGER_INCR(pPager->nRead);
3512 IOTRACE(("PGIN %p %d\n", pPager, pgno));
3513 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
3514 PAGERID(pPager), pgno, pager_pagehash(pPg)));
3515
3516 return rc;
3517 }
3518
3519 /*
3520 ** This function is called to obtain a shared lock on the database file. 4583 ** This function is called to obtain a shared lock on the database file.
3521 ** It is illegal to call sqlite3PagerAcquire() until after this function 4584 ** It is illegal to call sqlite3PagerAcquire() until after this function
3522 ** has been successfully called. If a shared-lock is already held when 4585 ** has been successfully called. If a shared-lock is already held when
3523 ** this function is called, it is a no-op. 4586 ** this function is called, it is a no-op.
3524 ** 4587 **
3525 ** The following operations are also performed by this function. 4588 ** The following operations are also performed by this function.
3526 ** 4589 **
3527 ** 1) If the pager is currently in PAGER_UNLOCK state (no lock held 4590 ** 1) If the pager is currently in PAGER_OPEN state (no lock held
3528 ** on the database file), then an attempt is made to obtain a 4591 ** on the database file), then an attempt is made to obtain a
3529 ** SHARED lock on the database file. Immediately after obtaining 4592 ** SHARED lock on the database file. Immediately after obtaining
3530 ** the SHARED lock, the file-system is checked for a hot-journal, 4593 ** the SHARED lock, the file-system is checked for a hot-journal,
3531 ** which is played back if present. Following any hot-journal 4594 ** which is played back if present. Following any hot-journal
3532 ** rollback, the contents of the cache are validated by checking 4595 ** rollback, the contents of the cache are validated by checking
3533 ** the 'change-counter' field of the database file header and 4596 ** the 'change-counter' field of the database file header and
3534 ** discarded if they are found to be invalid. 4597 ** discarded if they are found to be invalid.
3535 ** 4598 **
3536 ** 2) If the pager is running in exclusive-mode, and there are currently 4599 ** 2) If the pager is running in exclusive-mode, and there are currently
3537 ** no outstanding references to any pages, and is in the error state, 4600 ** no outstanding references to any pages, and is in the error state,
3538 ** then an attempt is made to clear the error state by discarding 4601 ** then an attempt is made to clear the error state by discarding
3539 ** the contents of the page cache and rolling back any open journal 4602 ** the contents of the page cache and rolling back any open journal
3540 ** file. 4603 ** file.
3541 ** 4604 **
3542 ** If the operation described by (2) above is not attempted, and if the 4605 ** If everything is successful, SQLITE_OK is returned. If an IO error
3543 ** pager is in an error state other than SQLITE_FULL when this is called, 4606 ** occurs while locking the database, checking for a hot-journal file or
3544 ** the error state error code is returned. It is permitted to read the 4607 ** rolling back a journal file, the IO error code is returned.
3545 ** database when in SQLITE_FULL error state.
3546 **
3547 ** Otherwise, if everything is successful, SQLITE_OK is returned. If an
3548 ** IO error occurs while locking the database, checking for a hot-journal
3549 ** file or rolling back a journal file, the IO error code is returned.
3550 */ 4608 */
3551 int sqlite3PagerSharedLock(Pager *pPager){ 4609 int sqlite3PagerSharedLock(Pager *pPager){
3552 int rc = SQLITE_OK; /* Return code */ 4610 int rc = SQLITE_OK; /* Return code */
3553 int isErrorReset = 0; /* True if recovering from error state */
3554 4611
3555 /* This routine is only called from b-tree and only when there are no 4612 /* This routine is only called from b-tree and only when there are no
3556 ** outstanding pages */ 4613 ** outstanding pages. This implies that the pager state should either
4614 ** be OPEN or READER. READER is only possible if the pager is or was in
4615 ** exclusive access mode.
4616 */
3557 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); 4617 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
4618 assert( assert_pager_state(pPager) );
4619 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
3558 if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; } 4620 if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
3559 4621
3560 /* If this database is in an error-state, now is a chance to clear 4622 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
3561 ** the error. Discard the contents of the pager-cache and rollback 4623 int bHotJournal = 1; /* True if there exists a hot journal-file */
3562 ** any hot journal in the file-system.
3563 */
3564 if( pPager->errCode ){
3565 if( isOpen(pPager->jfd) || pPager->zJournal ){
3566 isErrorReset = 1;
3567 }
3568 pPager->errCode = SQLITE_OK;
3569 pager_reset(pPager);
3570 }
3571 4624
3572 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
3573 sqlite3_vfs * const pVfs = pPager->pVfs;
3574 int isHotJournal = 0;
3575 assert( !MEMDB ); 4625 assert( !MEMDB );
3576 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); 4626 assert( pPager->noReadlock==0 || pPager->readOnly );
3577 if( pPager->noReadlock ){ 4627
3578 assert( pPager->readOnly ); 4628 if( pPager->noReadlock==0 ){
3579 pPager->state = PAGER_SHARED;
3580 }else{
3581 rc = pager_wait_on_lock(pPager, SHARED_LOCK); 4629 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3582 if( rc!=SQLITE_OK ){ 4630 if( rc!=SQLITE_OK ){
3583 assert( pPager->state==PAGER_UNLOCK ); 4631 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
3584 return pager_error(pPager, rc); 4632 goto failed;
3585 } 4633 }
3586 } 4634 }
3587 assert( pPager->state>=SHARED_LOCK );
3588 4635
3589 /* If a journal file exists, and there is no RESERVED lock on the 4636 /* If a journal file exists, and there is no RESERVED lock on the
3590 ** database file, then it either needs to be played back or deleted. 4637 ** database file, then it either needs to be played back or deleted.
3591 */ 4638 */
3592 if( !isErrorReset ){ 4639 if( pPager->eLock<=SHARED_LOCK ){
3593 assert( pPager->state <= PAGER_SHARED ); 4640 rc = hasHotJournal(pPager, &bHotJournal);
3594 rc = hasHotJournal(pPager, &isHotJournal);
3595 if( rc!=SQLITE_OK ){
3596 goto failed;
3597 }
3598 } 4641 }
3599 if( isErrorReset || isHotJournal ){ 4642 if( rc!=SQLITE_OK ){
4643 goto failed;
4644 }
4645 if( bHotJournal ){
3600 /* Get an EXCLUSIVE lock on the database file. At this point it is 4646 /* Get an EXCLUSIVE lock on the database file. At this point it is
3601 ** important that a RESERVED lock is not obtained on the way to the 4647 ** important that a RESERVED lock is not obtained on the way to the
3602 ** EXCLUSIVE lock. If it were, another process might open the 4648 ** EXCLUSIVE lock. If it were, another process might open the
3603 ** database file, detect the RESERVED lock, and conclude that the 4649 ** database file, detect the RESERVED lock, and conclude that the
3604 ** database is safe to read while this process is still rolling the 4650 ** database is safe to read while this process is still rolling the
3605 ** hot-journal back. 4651 ** hot-journal back.
3606 ** 4652 **
3607 ** Because the intermediate RESERVED lock is not requested, any 4653 ** Because the intermediate RESERVED lock is not requested, any
3608 ** other process attempting to access the database file will get to 4654 ** other process attempting to access the database file will get to
3609 ** this point in the code and fail to obtain its own EXCLUSIVE lock 4655 ** this point in the code and fail to obtain its own EXCLUSIVE lock
3610 ** on the database file. 4656 ** on the database file.
4657 **
4658 ** Unless the pager is in locking_mode=exclusive mode, the lock is
4659 ** downgraded to SHARED_LOCK before this function returns.
3611 */ 4660 */
3612 if( pPager->state<EXCLUSIVE_LOCK ){ 4661 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
3613 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK); 4662 if( rc!=SQLITE_OK ){
3614 if( rc!=SQLITE_OK ){ 4663 goto failed;
3615 rc = pager_error(pPager, rc);
3616 goto failed;
3617 }
3618 pPager->state = PAGER_EXCLUSIVE;
3619 } 4664 }
3620 4665
3621 /* Open the journal for read/write access. This is because in 4666 /* If it is not already open and the file exists on disk, open the
3622 ** exclusive-access mode the file descriptor will be kept open and 4667 ** journal for read/write access. Write access is required because
3623 ** possibly used for a transaction later on. On some systems, the 4668 ** in exclusive-access mode the file descriptor will be kept open
3624 ** OsTruncate() call used in exclusive-access mode also requires 4669 ** and possibly used for a transaction later on. Also, write-access
3625 ** a read/write file handle. 4670 ** is usually required to finalize the journal in journal_mode=persist
4671 ** mode (and also for journal_mode=truncate on some systems).
4672 **
4673 ** If the journal does not exist, it usually means that some
4674 ** other connection managed to get in and roll it back before
4675 ** this connection obtained the exclusive lock above. Or, it
4676 ** may mean that the pager was in the error-state when this
4677 ** function was called and the journal file does not exist.
3626 */ 4678 */
3627 if( !isOpen(pPager->jfd) ){ 4679 if( !isOpen(pPager->jfd) ){
3628 int res; 4680 sqlite3_vfs * const pVfs = pPager->pVfs;
3629 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res); 4681 int bExists; /* True if journal file exists */
3630 if( rc==SQLITE_OK ){ 4682 rc = sqlite3OsAccess(
3631 if( res ){ 4683 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
3632 int fout = 0; 4684 if( rc==SQLITE_OK && bExists ){
3633 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL; 4685 int fout = 0;
3634 assert( !pPager->tempFile ); 4686 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3635 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout); 4687 assert( !pPager->tempFile );
3636 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); 4688 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3637 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){ 4689 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
3638 rc = SQLITE_CANTOPEN; 4690 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
3639 sqlite3OsClose(pPager->jfd); 4691 rc = SQLITE_CANTOPEN_BKPT;
3640 } 4692 sqlite3OsClose(pPager->jfd);
3641 }else{
3642 /* If the journal does not exist, it usually means that some
3643 ** other connection managed to get in and roll it back before
3644 ** this connection obtained the exclusive lock above. Or, it
3645 ** may mean that the pager was in the error-state when this
3646 ** function was called and the journal file does not exist. */
3647 rc = pager_end_transaction(pPager, 0);
3648 } 4693 }
3649 } 4694 }
3650 } 4695 }
3651 if( rc!=SQLITE_OK ){
3652 goto failed;
3653 }
3654
3655 /* TODO: Why are these cleared here? Is it necessary? */
3656 pPager->journalStarted = 0;
3657 pPager->journalOff = 0;
3658 pPager->setMaster = 0;
3659 pPager->journalHdr = 0;
3660 4696
3661 /* Playback and delete the journal. Drop the database write 4697 /* Playback and delete the journal. Drop the database write
3662 ** lock and reacquire the read lock. Purge the cache before 4698 ** lock and reacquire the read lock. Purge the cache before
3663 ** playing back the hot-journal so that we don't end up with 4699 ** playing back the hot-journal so that we don't end up with
3664 ** an inconsistent cache. 4700 ** an inconsistent cache. Sync the hot journal before playing
4701 ** it back since the process that crashed and left the hot journal
4702 ** probably did not sync it and we are required to always sync
4703 ** the journal before playing it back.
3665 */ 4704 */
3666 if( isOpen(pPager->jfd) ){ 4705 if( isOpen(pPager->jfd) ){
3667 rc = pager_playback(pPager, 1); 4706 assert( rc==SQLITE_OK );
3668 if( rc!=SQLITE_OK ){ 4707 rc = pagerSyncHotJournal(pPager);
3669 rc = pager_error(pPager, rc); 4708 if( rc==SQLITE_OK ){
3670 goto failed; 4709 rc = pager_playback(pPager, 1);
4710 pPager->eState = PAGER_OPEN;
3671 } 4711 }
4712 }else if( !pPager->exclusiveMode ){
4713 pagerUnlockDb(pPager, SHARED_LOCK);
3672 } 4714 }
3673 assert( (pPager->state==PAGER_SHARED) 4715
3674 || (pPager->exclusiveMode && pPager->state>PAGER_SHARED) 4716 if( rc!=SQLITE_OK ){
4717 /* This branch is taken if an error occurs while trying to open
4718 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
4719 ** pager_unlock() routine will be called before returning to unlock
4720 ** the file. If the unlock attempt fails, then Pager.eLock must be
4721 ** set to UNKNOWN_LOCK (see the comment above the #define for
4722 ** UNKNOWN_LOCK above for an explanation).
4723 **
4724 ** In order to get pager_unlock() to do this, set Pager.eState to
4725 ** PAGER_ERROR now. This is not actually counted as a transition
4726 ** to ERROR state in the state diagram at the top of this file,
4727 ** since we know that the same call to pager_unlock() will very
4728 ** shortly transition the pager object to the OPEN state. Calling
4729 ** assert_pager_state() would fail now, as it should not be possible
4730 ** to be in ERROR state when there are zero outstanding page
4731 ** references.
4732 */
4733 pager_error(pPager, rc);
4734 goto failed;
4735 }
4736
4737 assert( pPager->eState==PAGER_OPEN );
4738 assert( (pPager->eLock==SHARED_LOCK)
4739 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
3675 ); 4740 );
3676 } 4741 }
3677 4742
3678 if( pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0 ){ 4743 if( !pPager->tempFile
4744 && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
4745 ){
3679 /* The shared-lock has just been acquired on the database file 4746 /* The shared-lock has just been acquired on the database file
3680 ** and there are already pages in the cache (from a previous 4747 ** and there are already pages in the cache (from a previous
3681 ** read or write transaction). Check to see if the database 4748 ** read or write transaction). Check to see if the database
3682 ** has been modified. If the database has changed, flush the 4749 ** has been modified. If the database has changed, flush the
3683 ** cache. 4750 ** cache.
3684 ** 4751 **
3685 ** Database changes is detected by looking at 15 bytes beginning 4752 ** Database changes is detected by looking at 15 bytes beginning
3686 ** at offset 24 into the file. The first 4 of these 16 bytes are 4753 ** at offset 24 into the file. The first 4 of these 16 bytes are
3687 ** a 32-bit counter that is incremented with each change. The 4754 ** a 32-bit counter that is incremented with each change. The
3688 ** other bytes change randomly with each file change when 4755 ** other bytes change randomly with each file change when
3689 ** a codec is in use. 4756 ** a codec is in use.
3690 ** 4757 **
3691 ** There is a vanishingly small chance that a change will not be 4758 ** There is a vanishingly small chance that a change will not be
3692 ** detected. The chance of an undetected change is so small that 4759 ** detected. The chance of an undetected change is so small that
3693 ** it can be neglected. 4760 ** it can be neglected.
3694 */ 4761 */
4762 Pgno nPage = 0;
3695 char dbFileVers[sizeof(pPager->dbFileVers)]; 4763 char dbFileVers[sizeof(pPager->dbFileVers)];
3696 sqlite3PagerPagecount(pPager, 0);
3697 4764
3698 if( pPager->errCode ){ 4765 rc = pagerPagecount(pPager, &nPage);
3699 rc = pPager->errCode; 4766 if( rc ) goto failed;
3700 goto failed;
3701 }
3702 4767
3703 assert( pPager->dbSizeValid ); 4768 if( nPage>0 ){
3704 if( pPager->dbSize>0 ){
3705 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); 4769 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
3706 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); 4770 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
3707 if( rc!=SQLITE_OK ){ 4771 if( rc!=SQLITE_OK ){
3708 goto failed; 4772 goto failed;
3709 } 4773 }
3710 }else{ 4774 }else{
3711 memset(dbFileVers, 0, sizeof(dbFileVers)); 4775 memset(dbFileVers, 0, sizeof(dbFileVers));
3712 } 4776 }
3713 4777
3714 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ 4778 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
3715 pager_reset(pPager); 4779 pager_reset(pPager);
3716 } 4780 }
3717 } 4781 }
3718 assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED ); 4782
4783 /* If there is a WAL file in the file-system, open this database in WAL
4784 ** mode. Otherwise, the following function call is a no-op.
4785 */
4786 rc = pagerOpenWalIfPresent(pPager);
4787 #ifndef SQLITE_OMIT_WAL
4788 assert( pPager->pWal==0 || rc==SQLITE_OK );
4789 #endif
4790 }
4791
4792 if( pagerUseWal(pPager) ){
4793 assert( rc==SQLITE_OK );
4794 rc = pagerBeginReadTransaction(pPager);
4795 }
4796
4797 if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
4798 rc = pagerPagecount(pPager, &pPager->dbSize);
3719 } 4799 }
3720 4800
3721 failed: 4801 failed:
3722 if( rc!=SQLITE_OK ){ 4802 if( rc!=SQLITE_OK ){
3723 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */ 4803 assert( !MEMDB );
3724 pager_unlock(pPager); 4804 pager_unlock(pPager);
4805 assert( pPager->eState==PAGER_OPEN );
4806 }else{
4807 pPager->eState = PAGER_READER;
3725 } 4808 }
3726 return rc; 4809 return rc;
3727 } 4810 }
3728 4811
3729 /* 4812 /*
3730 ** If the reference count has reached zero, rollback any active 4813 ** If the reference count has reached zero, rollback any active
3731 ** transaction and unlock the pager. 4814 ** transaction and unlock the pager.
3732 ** 4815 **
3733 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in 4816 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
3734 ** the rollback journal, the unlock is not performed and there is 4817 ** the rollback journal, the unlock is not performed and there is
3735 ** nothing to rollback, so this routine is a no-op. 4818 ** nothing to rollback, so this routine is a no-op.
3736 */ 4819 */
3737 static void pagerUnlockIfUnused(Pager *pPager){ 4820 static void pagerUnlockIfUnused(Pager *pPager){
3738 if( (sqlite3PcacheRefCount(pPager->pPCache)==0) 4821 if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
3739 && (!pPager->exclusiveMode || pPager->journalOff>0)
3740 ){
3741 pagerUnlockAndRollback(pPager); 4822 pagerUnlockAndRollback(pPager);
3742 } 4823 }
3743 } 4824 }
3744 4825
3745 /* 4826 /*
3746 ** Acquire a reference to page number pgno in pager pPager (a page 4827 ** Acquire a reference to page number pgno in pager pPager (a page
3747 ** reference has type DbPage*). If the requested reference is 4828 ** reference has type DbPage*). If the requested reference is
3748 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned. 4829 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
3749 ** 4830 **
3750 ** If the requested page is already in the cache, it is returned. 4831 ** If the requested page is already in the cache, it is returned.
(...skipping 12 matching lines...) Expand all
3763 ** requested page is not already stored in the cache, then no 4844 ** requested page is not already stored in the cache, then no
3764 ** actual disk read occurs. In this case the memory image of the 4845 ** actual disk read occurs. In this case the memory image of the
3765 ** page is initialized to all zeros. 4846 ** page is initialized to all zeros.
3766 ** 4847 **
3767 ** If noContent is true, it means that we do not care about the contents 4848 ** If noContent is true, it means that we do not care about the contents
3768 ** of the page. This occurs in two seperate scenarios: 4849 ** of the page. This occurs in two seperate scenarios:
3769 ** 4850 **
3770 ** a) When reading a free-list leaf page from the database, and 4851 ** a) When reading a free-list leaf page from the database, and
3771 ** 4852 **
3772 ** b) When a savepoint is being rolled back and we need to load 4853 ** b) When a savepoint is being rolled back and we need to load
3773 ** a new page into the cache to populate with the data read 4854 ** a new page into the cache to be filled with the data read
3774 ** from the savepoint journal. 4855 ** from the savepoint journal.
3775 ** 4856 **
3776 ** If noContent is true, then the data returned is zeroed instead of 4857 ** If noContent is true, then the data returned is zeroed instead of
3777 ** being read from the database. Additionally, the bits corresponding 4858 ** being read from the database. Additionally, the bits corresponding
3778 ** to pgno in Pager.pInJournal (bitvec of pages already written to the 4859 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
3779 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open 4860 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
3780 ** savepoints are set. This means if the page is made writable at any 4861 ** savepoints are set. This means if the page is made writable at any
3781 ** point in the future, using a call to sqlite3PagerWrite(), its contents 4862 ** point in the future, using a call to sqlite3PagerWrite(), its contents
3782 ** will not be journaled. This saves IO. 4863 ** will not be journaled. This saves IO.
3783 ** 4864 **
(...skipping 29 matching lines...) Expand all
3813 int sqlite3PagerAcquire2( 4894 int sqlite3PagerAcquire2(
3814 Pager *pPager, /* The pager open on the database file */ 4895 Pager *pPager, /* The pager open on the database file */
3815 Pgno pgno, /* Page number to fetch */ 4896 Pgno pgno, /* Page number to fetch */
3816 DbPage **ppPage, /* Write a pointer to the page here */ 4897 DbPage **ppPage, /* Write a pointer to the page here */
3817 int noContent, /* Do not bother reading content from disk if true */ 4898 int noContent, /* Do not bother reading content from disk if true */
3818 unsigned char* pDataToFill 4899 unsigned char* pDataToFill
3819 ){ 4900 ){
3820 int rc; 4901 int rc;
3821 PgHdr *pPg; 4902 PgHdr *pPg;
3822 4903
4904 assert( pPager->eState>=PAGER_READER );
3823 assert( assert_pager_state(pPager) ); 4905 assert( assert_pager_state(pPager) );
3824 assert( pPager->state>PAGER_UNLOCK );
3825 4906
3826 if( pgno==0 ){ 4907 if( pgno==0 ){
3827 return SQLITE_CORRUPT_BKPT; 4908 return SQLITE_CORRUPT_BKPT;
3828 } 4909 }
3829 4910
3830 /* If the pager is in the error state, return an error immediately. 4911 /* If the pager is in the error state, return an error immediately.
3831 ** Otherwise, request the page from the PCache layer. */ 4912 ** Otherwise, request the page from the PCache layer. */
3832 if( pPager->errCode!=SQLITE_OK && pPager->errCode!=SQLITE_FULL ){ 4913 if( pPager->errCode!=SQLITE_OK ){
3833 rc = pPager->errCode; 4914 rc = pPager->errCode;
3834 }else{ 4915 }else{
3835 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage); 4916 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
3836 } 4917 }
3837 4918
3838 if( rc!=SQLITE_OK ){ 4919 if( rc!=SQLITE_OK ){
3839 /* Either the call to sqlite3PcacheFetch() returned an error or the 4920 /* Either the call to sqlite3PcacheFetch() returned an error or the
3840 ** pager was already in the error-state when this function was called. 4921 ** pager was already in the error-state when this function was called.
3841 ** Set pPg to 0 and jump to the exception handler. */ 4922 ** Set pPg to 0 and jump to the exception handler. */
3842 pPg = 0; 4923 pPg = 0;
3843 goto pager_acquire_err; 4924 goto pager_acquire_err;
3844 } 4925 }
3845 assert( (*ppPage)->pgno==pgno ); 4926 assert( (*ppPage)->pgno==pgno );
3846 assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 ); 4927 assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
3847 4928
3848 if( (*ppPage)->pPager ){ 4929 if( (*ppPage)->pPager && !noContent ){
3849 /* In this case the pcache already contains an initialized copy of 4930 /* In this case the pcache already contains an initialized copy of
3850 ** the page. Return without further ado. */ 4931 ** the page. Return without further ado. */
3851 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); 4932 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
3852 PAGER_INCR(pPager->nHit); 4933 PAGER_INCR(pPager->nHit);
3853 return SQLITE_OK; 4934 return SQLITE_OK;
3854 4935
3855 }else{ 4936 }else{
3856 /* The pager cache has created a new page. Its content needs to 4937 /* The pager cache has created a new page. Its content needs to
3857 ** be initialized. */ 4938 ** be initialized. */
3858 int nMax;
3859 4939
3860 PAGER_INCR(pPager->nMiss); 4940 PAGER_INCR(pPager->nMiss);
3861 pPg = *ppPage; 4941 pPg = *ppPage;
3862 pPg->pPager = pPager; 4942 pPg->pPager = pPager;
3863 4943
3864 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page 4944 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3865 ** number greater than this, or the unused locking-page, is requested. */ 4945 ** number greater than this, or the unused locking-page, is requested. */
3866 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){ 4946 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
3867 rc = SQLITE_CORRUPT_BKPT; 4947 rc = SQLITE_CORRUPT_BKPT;
3868 goto pager_acquire_err; 4948 goto pager_acquire_err;
3869 } 4949 }
3870 4950
3871 rc = sqlite3PagerPagecount(pPager, &nMax); 4951 if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
3872 if( rc!=SQLITE_OK ){
3873 goto pager_acquire_err;
3874 }
3875
3876 if( nMax<(int)pgno || MEMDB || noContent ){
3877 if( pgno>pPager->mxPgno ){ 4952 if( pgno>pPager->mxPgno ){
3878 » rc = SQLITE_FULL; 4953 rc = SQLITE_FULL;
3879 » goto pager_acquire_err; 4954 goto pager_acquire_err;
3880 } 4955 }
3881 if( noContent ){ 4956 if( noContent ){
3882 /* Failure to set the bits in the InJournal bit-vectors is benign. 4957 /* Failure to set the bits in the InJournal bit-vectors is benign.
3883 ** It merely means that we might do some extra work to journal a 4958 ** It merely means that we might do some extra work to journal a
3884 ** page that does not need to be journaled. Nevertheless, be sure 4959 ** page that does not need to be journaled. Nevertheless, be sure
3885 ** to test the case where a malloc error occurs while trying to set 4960 ** to test the case where a malloc error occurs while trying to set
3886 ** a bit in a bit vector. 4961 ** a bit in a bit vector.
3887 */ 4962 */
3888 sqlite3BeginBenignMalloc(); 4963 sqlite3BeginBenignMalloc();
3889 if( pgno<=pPager->dbOrigSize ){ 4964 if( pgno<=pPager->dbOrigSize ){
3890 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno); 4965 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
3891 testcase( rc==SQLITE_NOMEM ); 4966 testcase( rc==SQLITE_NOMEM );
3892 } 4967 }
3893 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno); 4968 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
3894 testcase( rc==SQLITE_NOMEM ); 4969 testcase( rc==SQLITE_NOMEM );
3895 sqlite3EndBenignMalloc(); 4970 sqlite3EndBenignMalloc();
3896 }else{
3897 memset(pPg->pData, 0, pPager->pageSize);
3898 } 4971 }
4972 memset(pPg->pData, 0, pPager->pageSize);
3899 IOTRACE(("ZERO %p %d\n", pPager, pgno)); 4973 IOTRACE(("ZERO %p %d\n", pPager, pgno));
3900 }else{ 4974 }else{
3901 assert( pPg->pPager==pPager ); 4975 assert( pPg->pPager==pPager );
3902 if( pDataToFill ){ 4976 if( pDataToFill ){
3903 /* Just copy from the given memory */ 4977 /* Just copy from the given memory */
3904 memcpy(pPg->pData, pDataToFill, pPager->pageSize); 4978 memcpy(pPg->pData, pDataToFill, pPager->pageSize);
3905 CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM; 4979 CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM;
3906 goto pager_acquire_err); 4980 goto pager_acquire_err);
3907 }else{ 4981 }else{
3908 /* Load from disk (old regular sqlite code path) */ 4982 /* Load from disk (old regular sqlite code path) */
3909 rc = readDbPage(pPg); 4983 rc = readDbPage(pPg);
3910 if( rc!=SQLITE_OK ){ 4984 if( rc!=SQLITE_OK ){
3911 goto pager_acquire_err; 4985 goto pager_acquire_err;
3912 } 4986 }
3913 } 4987 }
3914 } 4988 }
3915 #ifdef SQLITE_CHECK_PAGES 4989 pager_set_pagehash(pPg);
3916 pPg->pageHash = pager_pagehash(pPg);
3917 #endif
3918 } 4990 }
3919 4991
3920 return SQLITE_OK; 4992 return SQLITE_OK;
3921 4993
3922 pager_acquire_err: 4994 pager_acquire_err:
3923 assert( rc!=SQLITE_OK ); 4995 assert( rc!=SQLITE_OK );
3924 if( pPg ){ 4996 if( pPg ){
3925 sqlite3PcacheDrop(pPg); 4997 sqlite3PcacheDrop(pPg);
3926 } 4998 }
3927 pagerUnlockIfUnused(pPager); 4999 pagerUnlockIfUnused(pPager);
3928 5000
3929 *ppPage = 0; 5001 *ppPage = 0;
3930 return rc; 5002 return rc;
3931 } 5003 }
3932 5004
3933 /* 5005 /*
3934 ** Acquire a page if it is already in the in-memory cache. Do 5006 ** Acquire a page if it is already in the in-memory cache. Do
3935 ** not read the page from disk. Return a pointer to the page, 5007 ** not read the page from disk. Return a pointer to the page,
3936 ** or 0 if the page is not in cache. Also, return 0 if the 5008 ** or 0 if the page is not in cache.
3937 ** pager is in PAGER_UNLOCK state when this function is called,
3938 ** or if the pager is in an error state other than SQLITE_FULL.
3939 ** 5009 **
3940 ** See also sqlite3PagerGet(). The difference between this routine 5010 ** See also sqlite3PagerGet(). The difference between this routine
3941 ** and sqlite3PagerGet() is that _get() will go to the disk and read 5011 ** and sqlite3PagerGet() is that _get() will go to the disk and read
3942 ** in the page if the page is not already in cache. This routine 5012 ** in the page if the page is not already in cache. This routine
3943 ** returns NULL if the page is not in cache or if a disk I/O error 5013 ** returns NULL if the page is not in cache or if a disk I/O error
3944 ** has ever happened. 5014 ** has ever happened.
3945 */ 5015 */
3946 DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ 5016 DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
3947 PgHdr *pPg = 0; 5017 PgHdr *pPg = 0;
3948 assert( pPager!=0 ); 5018 assert( pPager!=0 );
3949 assert( pgno!=0 ); 5019 assert( pgno!=0 );
3950 assert( pPager->pPCache!=0 ); 5020 assert( pPager->pPCache!=0 );
3951 assert( pPager->state > PAGER_UNLOCK ); 5021 assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
3952 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg); 5022 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
3953 return pPg; 5023 return pPg;
3954 } 5024 }
3955 5025
3956 /* 5026 /*
3957 ** Release a page reference. 5027 ** Release a page reference.
3958 ** 5028 **
3959 ** If the number of references to the page drop to zero, then the 5029 ** If the number of references to the page drop to zero, then the
3960 ** page is added to the LRU list. When all references to all pages 5030 ** page is added to the LRU list. When all references to all pages
3961 ** are released, a rollback occurs and the lock on the database is 5031 ** are released, a rollback occurs and the lock on the database is
3962 ** removed. 5032 ** removed.
3963 */ 5033 */
3964 void sqlite3PagerUnref(DbPage *pPg){ 5034 void sqlite3PagerUnref(DbPage *pPg){
3965 if( pPg ){ 5035 if( pPg ){
3966 Pager *pPager = pPg->pPager; 5036 Pager *pPager = pPg->pPager;
3967 sqlite3PcacheRelease(pPg); 5037 sqlite3PcacheRelease(pPg);
3968 pagerUnlockIfUnused(pPager); 5038 pagerUnlockIfUnused(pPager);
3969 } 5039 }
3970 } 5040 }
3971 5041
3972 /* 5042 /*
3973 ** If the main journal file has already been opened, ensure that the
3974 ** sub-journal file is open too. If the main journal is not open,
3975 ** this function is a no-op.
3976 **
3977 ** SQLITE_OK is returned if everything goes according to plan.
3978 ** An SQLITE_IOERR_XXX error code is returned if a call to
3979 ** sqlite3OsOpen() fails.
3980 */
3981 static int openSubJournal(Pager *pPager){
3982 int rc = SQLITE_OK;
3983 if( isOpen(pPager->jfd) && !isOpen(pPager->sjfd) ){
3984 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
3985 sqlite3MemJournalOpen(pPager->sjfd);
3986 }else{
3987 rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
3988 }
3989 }
3990 return rc;
3991 }
3992
3993 /*
3994 ** This function is called at the start of every write transaction. 5043 ** This function is called at the start of every write transaction.
3995 ** There must already be a RESERVED or EXCLUSIVE lock on the database 5044 ** There must already be a RESERVED or EXCLUSIVE lock on the database
3996 ** file when this routine is called. 5045 ** file when this routine is called.
3997 ** 5046 **
3998 ** Open the journal file for pager pPager and write a journal header 5047 ** Open the journal file for pager pPager and write a journal header
3999 ** to the start of it. If there are active savepoints, open the sub-journal 5048 ** to the start of it. If there are active savepoints, open the sub-journal
4000 ** as well. This function is only used when the journal file is being 5049 ** as well. This function is only used when the journal file is being
4001 ** opened to write a rollback log for a transaction. It is not used 5050 ** opened to write a rollback log for a transaction. It is not used
4002 ** when opening a hot journal file to roll it back. 5051 ** when opening a hot journal file to roll it back.
4003 ** 5052 **
4004 ** If the journal file is already open (as it may be in exclusive mode), 5053 ** If the journal file is already open (as it may be in exclusive mode),
4005 ** then this function just writes a journal header to the start of the 5054 ** then this function just writes a journal header to the start of the
4006 ** already open file. 5055 ** already open file.
4007 ** 5056 **
4008 ** Whether or not the journal file is opened by this function, the 5057 ** Whether or not the journal file is opened by this function, the
4009 ** Pager.pInJournal bitvec structure is allocated. 5058 ** Pager.pInJournal bitvec structure is allocated.
4010 ** 5059 **
4011 ** Return SQLITE_OK if everything is successful. Otherwise, return 5060 ** Return SQLITE_OK if everything is successful. Otherwise, return
4012 ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or 5061 ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
4013 ** an IO error code if opening or writing the journal file fails. 5062 ** an IO error code if opening or writing the journal file fails.
4014 */ 5063 */
4015 static int pager_open_journal(Pager *pPager){ 5064 static int pager_open_journal(Pager *pPager){
4016 int rc = SQLITE_OK; /* Return code */ 5065 int rc = SQLITE_OK; /* Return code */
4017 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */ 5066 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
4018 5067
4019 assert( pPager->state>=PAGER_RESERVED ); 5068 assert( pPager->eState==PAGER_WRITER_LOCKED );
4020 assert( pPager->useJournal ); 5069 assert( assert_pager_state(pPager) );
4021 assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF );
4022 assert( pPager->pInJournal==0 ); 5070 assert( pPager->pInJournal==0 );
4023 5071
4024 /* If already in the error state, this function is a no-op. But on 5072 /* If already in the error state, this function is a no-op. But on
4025 ** the other hand, this routine is never called if we are already in 5073 ** the other hand, this routine is never called if we are already in
4026 ** an error state. */ 5074 ** an error state. */
4027 if( NEVER(pPager->errCode) ) return pPager->errCode; 5075 if( NEVER(pPager->errCode) ) return pPager->errCode;
4028 5076
4029 /* TODO: Is it really possible to get here with dbSizeValid==0? If not, 5077 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
4030 ** the call to PagerPagecount() can be removed. 5078 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
4031 */ 5079 if( pPager->pInJournal==0 ){
4032 testcase( pPager->dbSizeValid==0 ); 5080 return SQLITE_NOMEM;
4033 sqlite3PagerPagecount(pPager, 0); 5081 }
4034 5082
4035 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); 5083 /* Open the journal file if it is not already open. */
4036 if( pPager->pInJournal==0 ){ 5084 if( !isOpen(pPager->jfd) ){
4037 return SQLITE_NOMEM; 5085 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
4038 } 5086 sqlite3MemJournalOpen(pPager->jfd);
4039 5087 }else{
4040 /* Open the journal file if it is not already open. */ 5088 const int flags = /* VFS flags to open journal file */
4041 if( !isOpen(pPager->jfd) ){ 5089 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
4042 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ 5090 (pPager->tempFile ?
4043 sqlite3MemJournalOpen(pPager->jfd); 5091 (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
4044 }else{ 5092 (SQLITE_OPEN_MAIN_JOURNAL)
4045 const int flags = /* VFS flags to open journal file */ 5093 );
4046 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| 5094 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
4047 (pPager->tempFile ? 5095 rc = sqlite3JournalOpen(
4048 (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL): 5096 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
4049 (SQLITE_OPEN_MAIN_JOURNAL)
4050 ); 5097 );
4051 #ifdef SQLITE_ENABLE_ATOMIC_WRITE 5098 #else
4052 rc = sqlite3JournalOpen( 5099 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
4053 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) 5100 #endif
4054 ); 5101 }
4055 #else 5102 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
4056 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
4057 #endif
4058 } 5103 }
4059 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); 5104
4060 } 5105
4061 5106 /* Write the first journal header to the journal file and open
4062 5107 ** the sub-journal if necessary.
4063 /* Write the first journal header to the journal file and open 5108 */
4064 ** the sub-journal if necessary. 5109 if( rc==SQLITE_OK ){
4065 */ 5110 /* TODO: Check if all of these are really required. */
4066 if( rc==SQLITE_OK ){ 5111 pPager->nRec = 0;
4067 /* TODO: Check if all of these are really required. */ 5112 pPager->journalOff = 0;
4068 pPager->dbOrigSize = pPager->dbSize; 5113 pPager->setMaster = 0;
4069 pPager->journalStarted = 0; 5114 pPager->journalHdr = 0;
4070 pPager->needSync = 0; 5115 rc = writeJournalHdr(pPager);
4071 pPager->nRec = 0; 5116 }
4072 pPager->journalOff = 0;
4073 pPager->setMaster = 0;
4074 pPager->journalHdr = 0;
4075 rc = writeJournalHdr(pPager);
4076 }
4077 if( rc==SQLITE_OK && pPager->nSavepoint ){
4078 rc = openSubJournal(pPager);
4079 } 5117 }
4080 5118
4081 if( rc!=SQLITE_OK ){ 5119 if( rc!=SQLITE_OK ){
4082 sqlite3BitvecDestroy(pPager->pInJournal); 5120 sqlite3BitvecDestroy(pPager->pInJournal);
4083 pPager->pInJournal = 0; 5121 pPager->pInJournal = 0;
5122 }else{
5123 assert( pPager->eState==PAGER_WRITER_LOCKED );
5124 pPager->eState = PAGER_WRITER_CACHEMOD;
4084 } 5125 }
5126
4085 return rc; 5127 return rc;
4086 } 5128 }
4087 5129
4088 /* 5130 /*
4089 ** Begin a write-transaction on the specified pager object. If a 5131 ** Begin a write-transaction on the specified pager object. If a
4090 ** write-transaction has already been opened, this function is a no-op. 5132 ** write-transaction has already been opened, this function is a no-op.
4091 ** 5133 **
4092 ** If the exFlag argument is false, then acquire at least a RESERVED 5134 ** If the exFlag argument is false, then acquire at least a RESERVED
4093 ** lock on the database file. If exFlag is true, then acquire at least 5135 ** lock on the database file. If exFlag is true, then acquire at least
4094 ** an EXCLUSIVE lock. If such a lock is already held, no locking 5136 ** an EXCLUSIVE lock. If such a lock is already held, no locking
4095 ** functions need be called. 5137 ** functions need be called.
4096 ** 5138 **
4097 ** If this is not a temporary or in-memory file and, the journal file is
4098 ** opened if it has not been already. For a temporary file, the opening
4099 ** of the journal file is deferred until there is an actual need to
4100 ** write to the journal. TODO: Why handle temporary files differently?
4101 **
4102 ** If the journal file is opened (or if it is already open), then a
4103 ** journal-header is written to the start of it.
4104 **
4105 ** If the subjInMemory argument is non-zero, then any sub-journal opened 5139 ** If the subjInMemory argument is non-zero, then any sub-journal opened
4106 ** within this transaction will be opened as an in-memory file. This 5140 ** within this transaction will be opened as an in-memory file. This
4107 ** has no effect if the sub-journal is already opened (as it may be when 5141 ** has no effect if the sub-journal is already opened (as it may be when
4108 ** running in exclusive mode) or if the transaction does not require a 5142 ** running in exclusive mode) or if the transaction does not require a
4109 ** sub-journal. If the subjInMemory argument is zero, then any required 5143 ** sub-journal. If the subjInMemory argument is zero, then any required
4110 ** sub-journal is implemented in-memory if pPager is an in-memory database, 5144 ** sub-journal is implemented in-memory if pPager is an in-memory database,
4111 ** or using a temporary file otherwise. 5145 ** or using a temporary file otherwise.
4112 */ 5146 */
4113 int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){ 5147 int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
4114 int rc = SQLITE_OK; 5148 int rc = SQLITE_OK;
4115 assert( pPager->state!=PAGER_UNLOCK ); 5149
5150 if( pPager->errCode ) return pPager->errCode;
5151 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
4116 pPager->subjInMemory = (u8)subjInMemory; 5152 pPager->subjInMemory = (u8)subjInMemory;
4117 if( pPager->state==PAGER_SHARED ){ 5153
5154 if( ALWAYS(pPager->eState==PAGER_READER) ){
4118 assert( pPager->pInJournal==0 ); 5155 assert( pPager->pInJournal==0 );
4119 assert( !MEMDB && !pPager->tempFile );
4120 5156
4121 /* Obtain a RESERVED lock on the database file. If the exFlag parameter 5157 if( pagerUseWal(pPager) ){
4122 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The 5158 /* If the pager is configured to use locking_mode=exclusive, and an
4123 ** busy-handler callback can be used when upgrading to the EXCLUSIVE 5159 ** exclusive lock on the database is not already held, obtain it now.
4124 ** lock, but not when obtaining the RESERVED lock. 5160 */
4125 */ 5161 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
4126 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK); 5162 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
4127 if( rc==SQLITE_OK ){ 5163 if( rc!=SQLITE_OK ){
4128 pPager->state = PAGER_RESERVED; 5164 return rc;
4129 if( exFlag ){ 5165 }
5166 sqlite3WalExclusiveMode(pPager->pWal, 1);
5167 }
5168
5169 /* Grab the write lock on the log file. If successful, upgrade to
5170 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
5171 ** The busy-handler is not invoked if another connection already
5172 ** holds the write-lock. If possible, the upper layer will call it.
5173 */
5174 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
5175 }else{
5176 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
5177 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
5178 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
5179 ** lock, but not when obtaining the RESERVED lock.
5180 */
5181 rc = pagerLockDb(pPager, RESERVED_LOCK);
5182 if( rc==SQLITE_OK && exFlag ){
4130 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); 5183 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
4131 } 5184 }
4132 } 5185 }
4133 5186
4134 /* If the required locks were successfully obtained, open the journal 5187 if( rc==SQLITE_OK ){
4135 ** file and write the first journal-header to it. 5188 /* Change to WRITER_LOCKED state.
4136 */ 5189 **
4137 if( rc==SQLITE_OK && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ 5190 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
4138 rc = pager_open_journal(pPager); 5191 ** when it has an open transaction, but never to DBMOD or FINISHED.
5192 ** This is because in those states the code to roll back savepoint
5193 ** transactions may copy data from the sub-journal into the database
5194 ** file as well as into the page cache. Which would be incorrect in
5195 ** WAL mode.
5196 */
5197 pPager->eState = PAGER_WRITER_LOCKED;
5198 pPager->dbHintSize = pPager->dbSize;
5199 pPager->dbFileSize = pPager->dbSize;
5200 pPager->dbOrigSize = pPager->dbSize;
5201 pPager->journalOff = 0;
4139 } 5202 }
4140 }else if( isOpen(pPager->jfd) && pPager->journalOff==0 ){ 5203
4141 /* This happens when the pager was in exclusive-access mode the last 5204 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
4142 ** time a (read or write) transaction was successfully concluded 5205 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
4143 ** by this connection. Instead of deleting the journal file it was 5206 assert( assert_pager_state(pPager) );
4144 ** kept open and either was truncated to 0 bytes or its header was
4145 ** overwritten with zeros.
4146 */
4147 assert( pPager->nRec==0 );
4148 assert( pPager->dbOrigSize==0 );
4149 assert( pPager->pInJournal==0 );
4150 rc = pager_open_journal(pPager);
4151 } 5207 }
4152 5208
4153 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager))); 5209 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
4154 assert( !isOpen(pPager->jfd) || pPager->journalOff>0 || rc!=SQLITE_OK );
4155 if( rc!=SQLITE_OK ){
4156 assert( !pPager->dbModified );
4157 /* Ignore any IO error that occurs within pager_end_transaction(). The
4158 ** purpose of this call is to reset the internal state of the pager
4159 ** sub-system. It doesn't matter if the journal-file is not properly
4160 ** finalized at this point (since it is not a valid journal file anyway).
4161 */
4162 pager_end_transaction(pPager, 0);
4163 }
4164 return rc; 5210 return rc;
4165 } 5211 }
4166 5212
4167 /* 5213 /*
4168 ** Mark a single data page as writeable. The page is written into the 5214 ** Mark a single data page as writeable. The page is written into the
4169 ** main journal or sub-journal as required. If the page is written into 5215 ** main journal or sub-journal as required. If the page is written into
4170 ** one of the journals, the corresponding bit is set in the 5216 ** one of the journals, the corresponding bit is set in the
4171 ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs 5217 ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
4172 ** of any open savepoints as appropriate. 5218 ** of any open savepoints as appropriate.
4173 */ 5219 */
4174 static int pager_write(PgHdr *pPg){ 5220 static int pager_write(PgHdr *pPg){
4175 void *pData = pPg->pData; 5221 void *pData = pPg->pData;
4176 Pager *pPager = pPg->pPager; 5222 Pager *pPager = pPg->pPager;
4177 int rc = SQLITE_OK; 5223 int rc = SQLITE_OK;
4178 5224
4179 /* This routine is not called unless a transaction has already been 5225 /* This routine is not called unless a write-transaction has already
4180 ** started. 5226 ** been started. The journal file may or may not be open at this point.
5227 ** It is never called in the ERROR state.
4181 */ 5228 */
4182 assert( pPager->state>=PAGER_RESERVED ); 5229 assert( pPager->eState==PAGER_WRITER_LOCKED
5230 || pPager->eState==PAGER_WRITER_CACHEMOD
5231 || pPager->eState==PAGER_WRITER_DBMOD
5232 );
5233 assert( assert_pager_state(pPager) );
4183 5234
4184 /* If an error has been previously detected, we should not be 5235 /* If an error has been previously detected, report the same error
4185 ** calling this routine. Repeat the error for robustness. 5236 ** again. This should not happen, but the check provides robustness. */
4186 */
4187 if( NEVER(pPager->errCode) ) return pPager->errCode; 5237 if( NEVER(pPager->errCode) ) return pPager->errCode;
4188 5238
4189 /* Higher-level routines never call this function if database is not 5239 /* Higher-level routines never call this function if database is not
4190 ** writable. But check anyway, just for robustness. */ 5240 ** writable. But check anyway, just for robustness. */
4191 if( NEVER(pPager->readOnly) ) return SQLITE_PERM; 5241 if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
4192 5242
4193 assert( !pPager->setMaster ); 5243 CHECK_PAGE(pPg);
4194 5244
4195 CHECK_PAGE(pPg); 5245 /* The journal file needs to be opened. Higher level routines have already
5246 ** obtained the necessary locks to begin the write-transaction, but the
5247 ** rollback journal might not yet be open. Open it now if this is the case.
5248 **
5249 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
5250 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
5251 ** an error might occur and the pager would end up in WRITER_LOCKED state
5252 ** with pages marked as dirty in the cache.
5253 */
5254 if( pPager->eState==PAGER_WRITER_LOCKED ){
5255 rc = pager_open_journal(pPager);
5256 if( rc!=SQLITE_OK ) return rc;
5257 }
5258 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
5259 assert( assert_pager_state(pPager) );
4196 5260
4197 /* Mark the page as dirty. If the page has already been written 5261 /* Mark the page as dirty. If the page has already been written
4198 ** to the journal then we can return right away. 5262 ** to the journal then we can return right away.
4199 */ 5263 */
4200 sqlite3PcacheMakeDirty(pPg); 5264 sqlite3PcacheMakeDirty(pPg);
4201 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){ 5265 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
4202 pPager->dbModified = 1; 5266 assert( !pagerUseWal(pPager) );
4203 }else{ 5267 }else{
4204
4205 /* If we get this far, it means that the page needs to be
4206 ** written to the transaction journal or the ckeckpoint journal
4207 ** or both.
4208 **
4209 ** Higher level routines should have already started a transaction,
4210 ** which means they have acquired the necessary locks and opened
4211 ** a rollback journal. Double-check to makes sure this is the case.
4212 */
4213 rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory);
4214 if( NEVER(rc!=SQLITE_OK) ){
4215 return rc;
4216 }
4217 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
4218 assert( pPager->useJournal );
4219 rc = pager_open_journal(pPager);
4220 if( rc!=SQLITE_OK ) return rc;
4221 }
4222 pPager->dbModified = 1;
4223 5268
4224 /* The transaction journal now exists and we have a RESERVED or an 5269 /* The transaction journal now exists and we have a RESERVED or an
4225 ** EXCLUSIVE lock on the main database file. Write the current page to 5270 ** EXCLUSIVE lock on the main database file. Write the current page to
4226 ** the transaction journal if it is not there already. 5271 ** the transaction journal if it is not there already.
4227 */ 5272 */
4228 if( !pageInJournal(pPg) && isOpen(pPager->jfd) ){ 5273 if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
4229 if( pPg->pgno<=pPager->dbOrigSize ){ 5274 assert( pagerUseWal(pPager)==0 );
5275 if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
4230 u32 cksum; 5276 u32 cksum;
4231 char *pData2; 5277 char *pData2;
5278 i64 iOff = pPager->journalOff;
4232 5279
4233 /* We should never write to the journal file the page that 5280 /* We should never write to the journal file the page that
4234 ** contains the database locks. The following assert verifies 5281 ** contains the database locks. The following assert verifies
4235 ** that we do not. */ 5282 ** that we do not. */
4236 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); 5283 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
5284
5285 assert( pPager->journalHdr<=pPager->journalOff );
4237 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); 5286 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
4238 cksum = pager_cksum(pPager, (u8*)pData2); 5287 cksum = pager_cksum(pPager, (u8*)pData2);
4239 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno); 5288
4240 if( rc==SQLITE_OK ){ 5289 /* Even if an IO or diskfull error occurs while journalling the
4241 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, 5290 ** page in the block above, set the need-sync flag for the page.
4242 pPager->journalOff + 4); 5291 ** Otherwise, when the transaction is rolled back, the logic in
4243 pPager->journalOff += pPager->pageSize+4; 5292 ** playback_one_page() will think that the page needs to be restored
4244 } 5293 ** in the database file. And if an IO error occurs while doing so,
4245 if( rc==SQLITE_OK ){ 5294 ** then corruption may follow.
4246 rc = write32bits(pPager->jfd, pPager->journalOff, cksum); 5295 */
4247 pPager->journalOff += 4; 5296 pPg->flags |= PGHDR_NEED_SYNC;
4248 } 5297
5298 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
5299 if( rc!=SQLITE_OK ) return rc;
5300 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
5301 if( rc!=SQLITE_OK ) return rc;
5302 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
5303 if( rc!=SQLITE_OK ) return rc;
5304
4249 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 5305 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
4250 pPager->journalOff, pPager->pageSize)); 5306 pPager->journalOff, pPager->pageSize));
4251 PAGER_INCR(sqlite3_pager_writej_count); 5307 PAGER_INCR(sqlite3_pager_writej_count);
4252 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n", 5308 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4253 PAGERID(pPager), pPg->pgno, 5309 PAGERID(pPager), pPg->pgno,
4254 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg))); 5310 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
4255 5311
4256 /* Even if an IO or diskfull error occurred while journalling the 5312 pPager->journalOff += 8 + pPager->pageSize;
4257 ** page in the block above, set the need-sync flag for the page.
4258 ** Otherwise, when the transaction is rolled back, the logic in
4259 ** playback_one_page() will think that the page needs to be restored
4260 ** in the database file. And if an IO error occurs while doing so,
4261 ** then corruption may follow.
4262 */
4263 if( !pPager->noSync ){
4264 pPg->flags |= PGHDR_NEED_SYNC;
4265 pPager->needSync = 1;
4266 }
4267
4268 /* An error has occurred writing to the journal file. The
4269 ** transaction will be rolled back by the layer above.
4270 */
4271 if( rc!=SQLITE_OK ){
4272 return rc;
4273 }
4274
4275 pPager->nRec++; 5313 pPager->nRec++;
4276 assert( pPager->pInJournal!=0 ); 5314 assert( pPager->pInJournal!=0 );
4277 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); 5315 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
4278 testcase( rc==SQLITE_NOMEM ); 5316 testcase( rc==SQLITE_NOMEM );
4279 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); 5317 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4280 rc |= addToSavepointBitvecs(pPager, pPg->pgno); 5318 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
4281 if( rc!=SQLITE_OK ){ 5319 if( rc!=SQLITE_OK ){
4282 assert( rc==SQLITE_NOMEM ); 5320 assert( rc==SQLITE_NOMEM );
4283 return rc; 5321 return rc;
4284 } 5322 }
4285 }else{ 5323 }else{
4286 if( !pPager->journalStarted && !pPager->noSync ){ 5324 if( pPager->eState!=PAGER_WRITER_DBMOD ){
4287 pPg->flags |= PGHDR_NEED_SYNC; 5325 pPg->flags |= PGHDR_NEED_SYNC;
4288 pPager->needSync = 1;
4289 } 5326 }
4290 PAGERTRACE(("APPEND %d page %d needSync=%d\n", 5327 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
4291 PAGERID(pPager), pPg->pgno, 5328 PAGERID(pPager), pPg->pgno,
4292 ((pPg->flags&PGHDR_NEED_SYNC)?1:0))); 5329 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
4293 } 5330 }
4294 } 5331 }
4295 5332
4296 /* If the statement journal is open and the page is not in it, 5333 /* If the statement journal is open and the page is not in it,
4297 ** then write the current page to the statement journal. Note that 5334 ** then write the current page to the statement journal. Note that
4298 ** the statement journal format differs from the standard journal format 5335 ** the statement journal format differs from the standard journal format
4299 ** in that it omits the checksums and the header. 5336 ** in that it omits the checksums and the header.
4300 */ 5337 */
4301 if( subjRequiresPage(pPg) ){ 5338 if( subjRequiresPage(pPg) ){
4302 rc = subjournalPage(pPg); 5339 rc = subjournalPage(pPg);
4303 } 5340 }
4304 } 5341 }
4305 5342
4306 /* Update the database size and return. 5343 /* Update the database size and return.
4307 */ 5344 */
4308 assert( pPager->state>=PAGER_SHARED );
4309 if( pPager->dbSize<pPg->pgno ){ 5345 if( pPager->dbSize<pPg->pgno ){
4310 pPager->dbSize = pPg->pgno; 5346 pPager->dbSize = pPg->pgno;
4311 } 5347 }
4312 return rc; 5348 return rc;
4313 } 5349 }
4314 5350
4315 /* 5351 /*
4316 ** Mark a data page as writeable. This routine must be called before 5352 ** Mark a data page as writeable. This routine must be called before
4317 ** making changes to a page. The caller must check the return value 5353 ** making changes to a page. The caller must check the return value
4318 ** of this function and be careful not to change any page data unless 5354 ** of this function and be careful not to change any page data unless
4319 ** this routine returns SQLITE_OK. 5355 ** this routine returns SQLITE_OK.
4320 ** 5356 **
4321 ** The difference between this function and pager_write() is that this 5357 ** The difference between this function and pager_write() is that this
4322 ** function also deals with the special case where 2 or more pages 5358 ** function also deals with the special case where 2 or more pages
4323 ** fit on a single disk sector. In this case all co-resident pages 5359 ** fit on a single disk sector. In this case all co-resident pages
4324 ** must have been written to the journal file before returning. 5360 ** must have been written to the journal file before returning.
4325 ** 5361 **
4326 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned 5362 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
4327 ** as appropriate. Otherwise, SQLITE_OK. 5363 ** as appropriate. Otherwise, SQLITE_OK.
4328 */ 5364 */
4329 int sqlite3PagerWrite(DbPage *pDbPage){ 5365 int sqlite3PagerWrite(DbPage *pDbPage){
4330 int rc = SQLITE_OK; 5366 int rc = SQLITE_OK;
4331 5367
4332 PgHdr *pPg = pDbPage; 5368 PgHdr *pPg = pDbPage;
4333 Pager *pPager = pPg->pPager; 5369 Pager *pPager = pPg->pPager;
4334 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); 5370 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4335 5371
5372 assert( pPager->eState>=PAGER_WRITER_LOCKED );
5373 assert( pPager->eState!=PAGER_ERROR );
5374 assert( assert_pager_state(pPager) );
5375
4336 if( nPagePerSector>1 ){ 5376 if( nPagePerSector>1 ){
4337 Pgno nPageCount; /* Total number of pages in database file */ 5377 Pgno nPageCount; /* Total number of pages in database file */
4338 Pgno pg1; /* First page of the sector pPg is located on. */ 5378 Pgno pg1; /* First page of the sector pPg is located on. */
4339 int nPage; /* Number of pages starting at pg1 to journal */ 5379 int nPage = 0; /* Number of pages starting at pg1 to journal */
4340 int ii; /* Loop counter */ 5380 int ii; /* Loop counter */
4341 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */ 5381 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
4342 5382
4343 /* Set the doNotSync flag to 1. This is because we cannot allow a journal 5383 /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
4344 ** header to be written between the pages journaled by this function. 5384 ** a journal header to be written between the pages journaled by
5385 ** this function.
4345 */ 5386 */
4346 assert( !MEMDB ); 5387 assert( !MEMDB );
4347 assert( pPager->doNotSync==0 ); 5388 assert( pPager->doNotSyncSpill==0 );
4348 pPager->doNotSync = 1; 5389 pPager->doNotSyncSpill++;
4349 5390
4350 /* This trick assumes that both the page-size and sector-size are 5391 /* This trick assumes that both the page-size and sector-size are
4351 ** an integer power of 2. It sets variable pg1 to the identifier 5392 ** an integer power of 2. It sets variable pg1 to the identifier
4352 ** of the first page of the sector pPg is located on. 5393 ** of the first page of the sector pPg is located on.
4353 */ 5394 */
4354 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; 5395 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4355 5396
4356 sqlite3PagerPagecount(pPager, (int *)&nPageCount); 5397 nPageCount = pPager->dbSize;
4357 if( pPg->pgno>nPageCount ){ 5398 if( pPg->pgno>nPageCount ){
4358 nPage = (pPg->pgno - pg1)+1; 5399 nPage = (pPg->pgno - pg1)+1;
4359 }else if( (pg1+nPagePerSector-1)>nPageCount ){ 5400 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4360 nPage = nPageCount+1-pg1; 5401 nPage = nPageCount+1-pg1;
4361 }else{ 5402 }else{
4362 nPage = nPagePerSector; 5403 nPage = nPagePerSector;
4363 } 5404 }
4364 assert(nPage>0); 5405 assert(nPage>0);
4365 assert(pg1<=pPg->pgno); 5406 assert(pg1<=pPg->pgno);
4366 assert((pg1+nPage)>pPg->pgno); 5407 assert((pg1+nPage)>pPg->pgno);
4367 5408
4368 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ 5409 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4369 Pgno pg = pg1+ii; 5410 Pgno pg = pg1+ii;
4370 PgHdr *pPage; 5411 PgHdr *pPage;
4371 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ 5412 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
4372 if( pg!=PAGER_MJ_PGNO(pPager) ){ 5413 if( pg!=PAGER_MJ_PGNO(pPager) ){
4373 rc = sqlite3PagerGet(pPager, pg, &pPage); 5414 rc = sqlite3PagerGet(pPager, pg, &pPage);
4374 if( rc==SQLITE_OK ){ 5415 if( rc==SQLITE_OK ){
4375 rc = pager_write(pPage); 5416 rc = pager_write(pPage);
4376 if( pPage->flags&PGHDR_NEED_SYNC ){ 5417 if( pPage->flags&PGHDR_NEED_SYNC ){
4377 needSync = 1; 5418 needSync = 1;
4378 assert(pPager->needSync);
4379 } 5419 }
4380 sqlite3PagerUnref(pPage); 5420 sqlite3PagerUnref(pPage);
4381 } 5421 }
4382 } 5422 }
4383 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){ 5423 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
4384 if( pPage->flags&PGHDR_NEED_SYNC ){ 5424 if( pPage->flags&PGHDR_NEED_SYNC ){
4385 needSync = 1; 5425 needSync = 1;
4386 } 5426 }
4387 sqlite3PagerUnref(pPage); 5427 sqlite3PagerUnref(pPage);
4388 } 5428 }
4389 } 5429 }
4390 5430
4391 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 5431 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
4392 ** starting at pg1, then it needs to be set for all of them. Because 5432 ** starting at pg1, then it needs to be set for all of them. Because
4393 ** writing to any of these nPage pages may damage the others, the 5433 ** writing to any of these nPage pages may damage the others, the
4394 ** journal file must contain sync()ed copies of all of them 5434 ** journal file must contain sync()ed copies of all of them
4395 ** before any of them can be written out to the database file. 5435 ** before any of them can be written out to the database file.
4396 */ 5436 */
4397 if( rc==SQLITE_OK && needSync ){ 5437 if( rc==SQLITE_OK && needSync ){
4398 assert( !MEMDB && pPager->noSync==0 ); 5438 assert( !MEMDB );
4399 for(ii=0; ii<nPage; ii++){ 5439 for(ii=0; ii<nPage; ii++){
4400 PgHdr *pPage = pager_lookup(pPager, pg1+ii); 5440 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4401 if( pPage ){ 5441 if( pPage ){
4402 pPage->flags |= PGHDR_NEED_SYNC; 5442 pPage->flags |= PGHDR_NEED_SYNC;
4403 sqlite3PagerUnref(pPage); 5443 sqlite3PagerUnref(pPage);
4404 } 5444 }
4405 } 5445 }
4406 assert(pPager->needSync);
4407 } 5446 }
4408 5447
4409 assert( pPager->doNotSync==1 ); 5448 assert( pPager->doNotSyncSpill==1 );
4410 pPager->doNotSync = 0; 5449 pPager->doNotSyncSpill--;
4411 }else{ 5450 }else{
4412 rc = pager_write(pDbPage); 5451 rc = pager_write(pDbPage);
4413 } 5452 }
4414 return rc; 5453 return rc;
4415 } 5454 }
4416 5455
4417 /* 5456 /*
4418 ** Return TRUE if the page given in the argument was previously passed 5457 ** Return TRUE if the page given in the argument was previously passed
4419 ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok 5458 ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
4420 ** to change the content of the page. 5459 ** to change the content of the page.
(...skipping 17 matching lines...) Expand all
4438 ** 5477 **
4439 ** Tests show that this optimization can quadruple the speed of large 5478 ** Tests show that this optimization can quadruple the speed of large
4440 ** DELETE operations. 5479 ** DELETE operations.
4441 */ 5480 */
4442 void sqlite3PagerDontWrite(PgHdr *pPg){ 5481 void sqlite3PagerDontWrite(PgHdr *pPg){
4443 Pager *pPager = pPg->pPager; 5482 Pager *pPager = pPg->pPager;
4444 if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ 5483 if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
4445 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager))); 5484 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
4446 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) 5485 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
4447 pPg->flags |= PGHDR_DONT_WRITE; 5486 pPg->flags |= PGHDR_DONT_WRITE;
4448 #ifdef SQLITE_CHECK_PAGES 5487 pager_set_pagehash(pPg);
4449 pPg->pageHash = pager_pagehash(pPg);
4450 #endif
4451 } 5488 }
4452 } 5489 }
4453 5490
4454 /* 5491 /*
4455 ** This routine is called to increment the value of the database file 5492 ** This routine is called to increment the value of the database file
4456 ** change-counter, stored as a 4-byte big-endian integer starting at 5493 ** change-counter, stored as a 4-byte big-endian integer starting at
4457 ** byte offset 24 of the pager file. 5494 ** byte offset 24 of the pager file.
4458 ** 5495 **
4459 ** If the isDirectMode flag is zero, then this is done by calling 5496 ** If the isDirectMode flag is zero, then this is done by calling
4460 ** sqlite3PagerWrite() on page 1, then modifying the contents of the 5497 ** sqlite3PagerWrite() on page 1, then modifying the contents of the
4461 ** page data. In this case the file will be updated when the current 5498 ** page data. In this case the file will be updated when the current
4462 ** transaction is committed. 5499 ** transaction is committed.
4463 ** 5500 **
4464 ** The isDirectMode flag may only be non-zero if the library was compiled 5501 ** The isDirectMode flag may only be non-zero if the library was compiled
4465 ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case, 5502 ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
4466 ** if isDirect is non-zero, then the database file is updated directly 5503 ** if isDirect is non-zero, then the database file is updated directly
4467 ** by writing an updated version of page 1 using a call to the 5504 ** by writing an updated version of page 1 using a call to the
4468 ** sqlite3OsWrite() function. 5505 ** sqlite3OsWrite() function.
4469 */ 5506 */
4470 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ 5507 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
4471 int rc = SQLITE_OK; 5508 int rc = SQLITE_OK;
4472 5509
5510 assert( pPager->eState==PAGER_WRITER_CACHEMOD
5511 || pPager->eState==PAGER_WRITER_DBMOD
5512 );
5513 assert( assert_pager_state(pPager) );
5514
4473 /* Declare and initialize constant integer 'isDirect'. If the 5515 /* Declare and initialize constant integer 'isDirect'. If the
4474 ** atomic-write optimization is enabled in this build, then isDirect 5516 ** atomic-write optimization is enabled in this build, then isDirect
4475 ** is initialized to the value passed as the isDirectMode parameter 5517 ** is initialized to the value passed as the isDirectMode parameter
4476 ** to this function. Otherwise, it is always set to zero. 5518 ** to this function. Otherwise, it is always set to zero.
4477 ** 5519 **
4478 ** The idea is that if the atomic-write optimization is not 5520 ** The idea is that if the atomic-write optimization is not
4479 ** enabled at compile time, the compiler can omit the tests of 5521 ** enabled at compile time, the compiler can omit the tests of
4480 ** 'isDirect' below, as well as the block enclosed in the 5522 ** 'isDirect' below, as well as the block enclosed in the
4481 ** "if( isDirect )" condition. 5523 ** "if( isDirect )" condition.
4482 */ 5524 */
4483 #ifndef SQLITE_ENABLE_ATOMIC_WRITE 5525 #ifndef SQLITE_ENABLE_ATOMIC_WRITE
4484 # define DIRECT_MODE 0 5526 # define DIRECT_MODE 0
4485 assert( isDirectMode==0 ); 5527 assert( isDirectMode==0 );
4486 UNUSED_PARAMETER(isDirectMode); 5528 UNUSED_PARAMETER(isDirectMode);
4487 #else 5529 #else
4488 # define DIRECT_MODE isDirectMode 5530 # define DIRECT_MODE isDirectMode
4489 #endif 5531 #endif
4490 5532
4491 assert( pPager->state>=PAGER_RESERVED ); 5533 if( !pPager->changeCountDone && pPager->dbSize>0 ){
4492 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
4493 PgHdr *pPgHdr; /* Reference to page 1 */ 5534 PgHdr *pPgHdr; /* Reference to page 1 */
4494 u32 change_counter; /* Initial value of change-counter field */ 5535 u32 change_counter; /* Initial value of change-counter field */
4495 5536
4496 assert( !pPager->tempFile && isOpen(pPager->fd) ); 5537 assert( !pPager->tempFile && isOpen(pPager->fd) );
4497 5538
4498 /* Open page 1 of the file for writing. */ 5539 /* Open page 1 of the file for writing. */
4499 rc = sqlite3PagerGet(pPager, 1, &pPgHdr); 5540 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4500 assert( pPgHdr==0 || rc==SQLITE_OK ); 5541 assert( pPgHdr==0 || rc==SQLITE_OK );
4501 5542
4502 /* If page one was fetched successfully, and this function is not 5543 /* If page one was fetched successfully, and this function is not
4503 ** operating in direct-mode, make page 1 writable. When not in 5544 ** operating in direct-mode, make page 1 writable. When not in
4504 ** direct mode, page 1 is always held in cache and hence the PagerGet() 5545 ** direct mode, page 1 is always held in cache and hence the PagerGet()
4505 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK. 5546 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
4506 */ 5547 */
4507 if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){ 5548 if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
4508 rc = sqlite3PagerWrite(pPgHdr); 5549 rc = sqlite3PagerWrite(pPgHdr);
4509 } 5550 }
4510 5551
4511 if( rc==SQLITE_OK ){ 5552 if( rc==SQLITE_OK ){
4512 /* Increment the value just read and write it back to byte 24. */ 5553 /* Increment the value just read and write it back to byte 24. */
4513 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers); 5554 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
4514 change_counter++; 5555 change_counter++;
4515 put32bits(((char*)pPgHdr->pData)+24, change_counter); 5556 put32bits(((char*)pPgHdr->pData)+24, change_counter);
4516 5557
5558 /* Also store the SQLite version number in bytes 96..99 and in
5559 ** bytes 92..95 store the change counter for which the version number
5560 ** is valid. */
5561 put32bits(((char*)pPgHdr->pData)+92, change_counter);
5562 put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER);
5563
4517 /* If running in direct mode, write the contents of page 1 to the file. */ 5564 /* If running in direct mode, write the contents of page 1 to the file. */
4518 if( DIRECT_MODE ){ 5565 if( DIRECT_MODE ){
4519 const void *zBuf = pPgHdr->pData; 5566 const void *zBuf;
4520 assert( pPager->dbFileSize>0 ); 5567 assert( pPager->dbFileSize>0 );
4521 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); 5568 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
5569 if( rc==SQLITE_OK ){
5570 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
5571 }
4522 if( rc==SQLITE_OK ){ 5572 if( rc==SQLITE_OK ){
4523 pPager->changeCountDone = 1; 5573 pPager->changeCountDone = 1;
4524 } 5574 }
4525 }else{ 5575 }else{
4526 pPager->changeCountDone = 1; 5576 pPager->changeCountDone = 1;
4527 } 5577 }
4528 } 5578 }
4529 5579
4530 /* Release the page reference. */ 5580 /* Release the page reference. */
4531 sqlite3PagerUnref(pPgHdr); 5581 sqlite3PagerUnref(pPgHdr);
(...skipping 13 matching lines...) Expand all
4545 assert( !MEMDB ); 5595 assert( !MEMDB );
4546 if( pPager->noSync ){ 5596 if( pPager->noSync ){
4547 rc = SQLITE_OK; 5597 rc = SQLITE_OK;
4548 }else{ 5598 }else{
4549 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); 5599 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4550 } 5600 }
4551 return rc; 5601 return rc;
4552 } 5602 }
4553 5603
4554 /* 5604 /*
5605 ** This function may only be called while a write-transaction is active in
5606 ** rollback. If the connection is in WAL mode, this call is a no-op.
5607 ** Otherwise, if the connection does not already have an EXCLUSIVE lock on
5608 ** the database file, an attempt is made to obtain one.
5609 **
5610 ** If the EXCLUSIVE lock is already held or the attempt to obtain it is
5611 ** successful, or the connection is in WAL mode, SQLITE_OK is returned.
5612 ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
5613 ** returned.
5614 */
5615 int sqlite3PagerExclusiveLock(Pager *pPager){
5616 int rc = SQLITE_OK;
5617 assert( pPager->eState==PAGER_WRITER_CACHEMOD
5618 || pPager->eState==PAGER_WRITER_DBMOD
5619 || pPager->eState==PAGER_WRITER_LOCKED
5620 );
5621 assert( assert_pager_state(pPager) );
5622 if( 0==pagerUseWal(pPager) ){
5623 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
5624 }
5625 return rc;
5626 }
5627
5628 /*
4555 ** Sync the database file for the pager pPager. zMaster points to the name 5629 ** Sync the database file for the pager pPager. zMaster points to the name
4556 ** of a master journal file that should be written into the individual 5630 ** of a master journal file that should be written into the individual
4557 ** journal file. zMaster may be NULL, which is interpreted as no master 5631 ** journal file. zMaster may be NULL, which is interpreted as no master
4558 ** journal (a single database transaction). 5632 ** journal (a single database transaction).
4559 ** 5633 **
4560 ** This routine ensures that: 5634 ** This routine ensures that:
4561 ** 5635 **
4562 ** * The database file change-counter is updated, 5636 ** * The database file change-counter is updated,
4563 ** * the journal is synced (unless the atomic-write optimization is used), 5637 ** * the journal is synced (unless the atomic-write optimization is used),
4564 ** * all dirty pages are written to the database file, 5638 ** * all dirty pages are written to the database file,
(...skipping 12 matching lines...) Expand all
4577 ** sync the database file before calling CommitPhaseTwo() to delete the 5651 ** sync the database file before calling CommitPhaseTwo() to delete the
4578 ** journal file in this case. 5652 ** journal file in this case.
4579 */ 5653 */
4580 int sqlite3PagerCommitPhaseOne( 5654 int sqlite3PagerCommitPhaseOne(
4581 Pager *pPager, /* Pager object */ 5655 Pager *pPager, /* Pager object */
4582 const char *zMaster, /* If not NULL, the master journal name */ 5656 const char *zMaster, /* If not NULL, the master journal name */
4583 int noSync /* True to omit the xSync on the db file */ 5657 int noSync /* True to omit the xSync on the db file */
4584 ){ 5658 ){
4585 int rc = SQLITE_OK; /* Return code */ 5659 int rc = SQLITE_OK; /* Return code */
4586 5660
4587 /* The dbOrigSize is never set if journal_mode=OFF */ 5661 assert( pPager->eState==PAGER_WRITER_LOCKED
4588 assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 ); 5662 || pPager->eState==PAGER_WRITER_CACHEMOD
5663 || pPager->eState==PAGER_WRITER_DBMOD
5664 || pPager->eState==PAGER_ERROR
5665 );
5666 assert( assert_pager_state(pPager) );
4589 5667
4590 /* If a prior error occurred, this routine should not be called. ROLLBACK 5668 /* If a prior error occurred, report that error again. */
4591 ** is the appropriate response to an error, not COMMIT. Guard against
4592 ** coding errors by repeating the prior error. */
4593 if( NEVER(pPager->errCode) ) return pPager->errCode; 5669 if( NEVER(pPager->errCode) ) return pPager->errCode;
4594 5670
4595 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 5671 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
4596 pPager->zFilename, zMaster, pPager->dbSize)); 5672 pPager->zFilename, zMaster, pPager->dbSize));
4597 5673
4598 if( MEMDB && pPager->dbModified ){ 5674 /* If no database changes have been made, return early. */
5675 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
5676
5677 if( MEMDB ){
4599 /* If this is an in-memory db, or no pages have been written to, or this 5678 /* If this is an in-memory db, or no pages have been written to, or this
4600 ** function has already been called, it is mostly a no-op. However, any 5679 ** function has already been called, it is mostly a no-op. However, any
4601 ** backup in progress needs to be restarted. 5680 ** backup in progress needs to be restarted.
4602 */ 5681 */
4603 sqlite3BackupRestart(pPager->pBackup); 5682 sqlite3BackupRestart(pPager->pBackup);
4604 }else if( pPager->state!=PAGER_SYNCED && pPager->dbModified ){ 5683 }else{
4605 5684 if( pagerUseWal(pPager) ){
4606 /* The following block updates the change-counter. Exactly how it 5685 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
4607 ** does this depends on whether or not the atomic-update optimization 5686 if( pList ){
4608 ** was enabled at compile time, and if this transaction meets the 5687 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1,
4609 ** runtime criteria to use the operation: 5688 (pPager->fullSync ? pPager->sync_flags : 0)
4610 ** 5689 );
4611 ** * The file-system supports the atomic-write property for 5690 }
4612 ** blocks of size page-size, and 5691 if( rc==SQLITE_OK ){
4613 ** * This commit is not part of a multi-file transaction, and 5692 sqlite3PcacheCleanAll(pPager->pPCache);
4614 ** * Exactly one page has been modified and store in the journal file. 5693 }
4615 ** 5694 }else{
4616 ** If the optimization was not enabled at compile time, then the 5695 /* The following block updates the change-counter. Exactly how it
4617 ** pager_incr_changecounter() function is called to update the change 5696 ** does this depends on whether or not the atomic-update optimization
4618 ** counter in 'indirect-mode'. If the optimization is compiled in but 5697 ** was enabled at compile time, and if this transaction meets the
4619 ** is not applicable to this transaction, call sqlite3JournalCreate() 5698 ** runtime criteria to use the operation:
4620 ** to make sure the journal file has actually been created, then call 5699 **
4621 ** pager_incr_changecounter() to update the change-counter in indirect 5700 ** * The file-system supports the atomic-write property for
4622 ** mode. 5701 ** blocks of size page-size, and
4623 ** 5702 ** * This commit is not part of a multi-file transaction, and
4624 ** Otherwise, if the optimization is both enabled and applicable, 5703 ** * Exactly one page has been modified and store in the journal file.
4625 ** then call pager_incr_changecounter() to update the change-counter 5704 **
4626 ** in 'direct' mode. In this case the journal file will never be 5705 ** If the optimization was not enabled at compile time, then the
4627 ** created for this transaction. 5706 ** pager_incr_changecounter() function is called to update the change
4628 */ 5707 ** counter in 'indirect-mode'. If the optimization is compiled in but
4629 #ifdef SQLITE_ENABLE_ATOMIC_WRITE 5708 ** is not applicable to this transaction, call sqlite3JournalCreate()
4630 PgHdr *pPg; 5709 ** to make sure the journal file has actually been created, then call
4631 assert( isOpen(pPager->jfd) || pPager->journalMode==PAGER_JOURNALMODE_OFF ); 5710 ** pager_incr_changecounter() to update the change-counter in indirect
4632 if( !zMaster && isOpen(pPager->jfd) 5711 ** mode.
4633 && pPager->journalOff==jrnlBufferSize(pPager) 5712 **
4634 && pPager->dbSize>=pPager->dbFileSize 5713 ** Otherwise, if the optimization is both enabled and applicable,
4635 && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty) 5714 ** then call pager_incr_changecounter() to update the change-counter
4636 ){ 5715 ** in 'direct' mode. In this case the journal file will never be
4637 /* Update the db file change counter via the direct-write method. The 5716 ** created for this transaction.
4638 ** following call will modify the in-memory representation of page 1
4639 ** to include the updated change counter and then write page 1
4640 ** directly to the database file. Because of the atomic-write
4641 ** property of the host file-system, this is safe.
4642 */ 5717 */
4643 rc = pager_incr_changecounter(pPager, 1); 5718 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
4644 }else{ 5719 PgHdr *pPg;
4645 rc = sqlite3JournalCreate(pPager->jfd); 5720 assert( isOpen(pPager->jfd)
4646 if( rc==SQLITE_OK ){ 5721 || pPager->journalMode==PAGER_JOURNALMODE_OFF
4647 rc = pager_incr_changecounter(pPager, 0); 5722 || pPager->journalMode==PAGER_JOURNALMODE_WAL
5723 );
5724 if( !zMaster && isOpen(pPager->jfd)
5725 && pPager->journalOff==jrnlBufferSize(pPager)
5726 && pPager->dbSize>=pPager->dbOrigSize
5727 && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
5728 ){
5729 /* Update the db file change counter via the direct-write method. The
5730 ** following call will modify the in-memory representation of page 1
5731 ** to include the updated change counter and then write page 1
5732 ** directly to the database file. Because of the atomic-write
5733 ** property of the host file-system, this is safe.
5734 */
5735 rc = pager_incr_changecounter(pPager, 1);
5736 }else{
5737 rc = sqlite3JournalCreate(pPager->jfd);
5738 if( rc==SQLITE_OK ){
5739 rc = pager_incr_changecounter(pPager, 0);
5740 }
4648 } 5741 }
5742 #else
5743 rc = pager_incr_changecounter(pPager, 0);
5744 #endif
5745 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5746
5747 /* If this transaction has made the database smaller, then all pages
5748 ** being discarded by the truncation must be written to the journal
5749 ** file. This can only happen in auto-vacuum mode.
5750 **
5751 ** Before reading the pages with page numbers larger than the
5752 ** current value of Pager.dbSize, set dbSize back to the value
5753 ** that it took at the start of the transaction. Otherwise, the
5754 ** calls to sqlite3PagerGet() return zeroed pages instead of
5755 ** reading data from the database file.
5756 */
5757 #ifndef SQLITE_OMIT_AUTOVACUUM
5758 if( pPager->dbSize<pPager->dbOrigSize
5759 && pPager->journalMode!=PAGER_JOURNALMODE_OFF
5760 ){
5761 Pgno i; /* Iterator variable */
5762 const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
5763 const Pgno dbSize = pPager->dbSize; /* Database image size */
5764 pPager->dbSize = pPager->dbOrigSize;
5765 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
5766 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
5767 PgHdr *pPage; /* Page to journal */
5768 rc = sqlite3PagerGet(pPager, i, &pPage);
5769 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5770 rc = sqlite3PagerWrite(pPage);
5771 sqlite3PagerUnref(pPage);
5772 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5773 }
5774 }
5775 pPager->dbSize = dbSize;
5776 }
5777 #endif
5778
5779 /* Write the master journal name into the journal file. If a master
5780 ** journal file name has already been written to the journal file,
5781 ** or if zMaster is NULL (no master journal), then this call is a no-op.
5782 */
5783 rc = writeMasterJournal(pPager, zMaster);
5784 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5785
5786 /* Sync the journal file and write all dirty pages to the database.
5787 ** If the atomic-update optimization is being used, this sync will not
5788 ** create the journal file or perform any real IO.
5789 **
5790 ** Because the change-counter page was just modified, unless the
5791 ** atomic-update optimization is used it is almost certain that the
5792 ** journal requires a sync here. However, in locking_mode=exclusive
5793 ** on a system under memory pressure it is just possible that this is
5794 ** not the case. In this case it is likely enough that the redundant
5795 ** xSync() call will be changed to a no-op by the OS anyhow.
5796 */
5797 rc = syncJournal(pPager, 0);
5798 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5799
5800 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
5801 if( rc!=SQLITE_OK ){
5802 assert( rc!=SQLITE_IOERR_BLOCKED );
5803 goto commit_phase_one_exit;
5804 }
5805 sqlite3PcacheCleanAll(pPager->pPCache);
5806
5807 /* If the file on disk is not the same size as the database image,
5808 ** then use pager_truncate to grow or shrink the file here.
5809 */
5810 if( pPager->dbSize!=pPager->dbFileSize ){
5811 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
5812 assert( pPager->eState==PAGER_WRITER_DBMOD );
5813 rc = pager_truncate(pPager, nNew);
5814 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5815 }
5816
5817 /* Finally, sync the database file. */
5818 if( !pPager->noSync && !noSync ){
5819 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
5820 }
5821 IOTRACE(("DBSYNC %p\n", pPager))
4649 } 5822 }
4650 #else
4651 rc = pager_incr_changecounter(pPager, 0);
4652 #endif
4653 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
4654
4655 /* If this transaction has made the database smaller, then all pages
4656 ** being discarded by the truncation must be written to the journal
4657 ** file. This can only happen in auto-vacuum mode.
4658 **
4659 ** Before reading the pages with page numbers larger than the
4660 ** current value of Pager.dbSize, set dbSize back to the value
4661 ** that it took at the start of the transaction. Otherwise, the
4662 ** calls to sqlite3PagerGet() return zeroed pages instead of
4663 ** reading data from the database file.
4664 **
4665 ** When journal_mode==OFF the dbOrigSize is always zero, so this
4666 ** block never runs if journal_mode=OFF.
4667 */
4668 #ifndef SQLITE_OMIT_AUTOVACUUM
4669 if( pPager->dbSize<pPager->dbOrigSize
4670 && ALWAYS(pPager->journalMode!=PAGER_JOURNALMODE_OFF)
4671 ){
4672 Pgno i; /* Iterator variable */
4673 const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
4674 const Pgno dbSize = pPager->dbSize; /* Database image size */
4675 pPager->dbSize = pPager->dbOrigSize;
4676 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
4677 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
4678 PgHdr *pPage; /* Page to journal */
4679 rc = sqlite3PagerGet(pPager, i, &pPage);
4680 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
4681 rc = sqlite3PagerWrite(pPage);
4682 sqlite3PagerUnref(pPage);
4683 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
4684 }
4685 }
4686 pPager->dbSize = dbSize;
4687 }
4688 #endif
4689
4690 /* Write the master journal name into the journal file. If a master
4691 ** journal file name has already been written to the journal file,
4692 ** or if zMaster is NULL (no master journal), then this call is a no-op.
4693 */
4694 rc = writeMasterJournal(pPager, zMaster);
4695 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
4696
4697 /* Sync the journal file. If the atomic-update optimization is being
4698 ** used, this call will not create the journal file or perform any
4699 ** real IO.
4700 */
4701 rc = syncJournal(pPager);
4702 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
4703
4704 /* Write all dirty pages to the database file. */
4705 rc = pager_write_pagelist(sqlite3PcacheDirtyList(pPager->pPCache));
4706 if( rc!=SQLITE_OK ){
4707 assert( rc!=SQLITE_IOERR_BLOCKED );
4708 goto commit_phase_one_exit;
4709 }
4710 sqlite3PcacheCleanAll(pPager->pPCache);
4711
4712 /* If the file on disk is not the same size as the database image,
4713 ** then use pager_truncate to grow or shrink the file here.
4714 */
4715 if( pPager->dbSize!=pPager->dbFileSize ){
4716 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
4717 assert( pPager->state>=PAGER_EXCLUSIVE );
4718 rc = pager_truncate(pPager, nNew);
4719 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
4720 }
4721
4722 /* Finally, sync the database file. */
4723 if( !pPager->noSync && !noSync ){
4724 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4725 }
4726 IOTRACE(("DBSYNC %p\n", pPager))
4727
4728 pPager->state = PAGER_SYNCED;
4729 } 5823 }
4730 5824
4731 commit_phase_one_exit: 5825 commit_phase_one_exit:
5826 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
5827 pPager->eState = PAGER_WRITER_FINISHED;
5828 }
4732 return rc; 5829 return rc;
4733 } 5830 }
4734 5831
4735 5832
4736 /* 5833 /*
4737 ** When this function is called, the database file has been completely 5834 ** When this function is called, the database file has been completely
4738 ** updated to reflect the changes made by the current transaction and 5835 ** updated to reflect the changes made by the current transaction and
4739 ** synced to disk. The journal file still exists in the file-system 5836 ** synced to disk. The journal file still exists in the file-system
4740 ** though, and if a failure occurs at this point it will eventually 5837 ** though, and if a failure occurs at this point it will eventually
4741 ** be used as a hot-journal and the current transaction rolled back. 5838 ** be used as a hot-journal and the current transaction rolled back.
4742 ** 5839 **
4743 ** This function finalizes the journal file, either by deleting, 5840 ** This function finalizes the journal file, either by deleting,
4744 ** truncating or partially zeroing it, so that it cannot be used 5841 ** truncating or partially zeroing it, so that it cannot be used
4745 ** for hot-journal rollback. Once this is done the transaction is 5842 ** for hot-journal rollback. Once this is done the transaction is
4746 ** irrevocably committed. 5843 ** irrevocably committed.
4747 ** 5844 **
4748 ** If an error occurs, an IO error code is returned and the pager 5845 ** If an error occurs, an IO error code is returned and the pager
4749 ** moves into the error state. Otherwise, SQLITE_OK is returned. 5846 ** moves into the error state. Otherwise, SQLITE_OK is returned.
4750 */ 5847 */
4751 int sqlite3PagerCommitPhaseTwo(Pager *pPager){ 5848 int sqlite3PagerCommitPhaseTwo(Pager *pPager){
4752 int rc = SQLITE_OK; /* Return code */ 5849 int rc = SQLITE_OK; /* Return code */
4753 5850
4754 /* This routine should not be called if a prior error has occurred. 5851 /* This routine should not be called if a prior error has occurred.
4755 ** But if (due to a coding error elsewhere in the system) it does get 5852 ** But if (due to a coding error elsewhere in the system) it does get
4756 ** called, just return the same error code without doing anything. */ 5853 ** called, just return the same error code without doing anything. */
4757 if( NEVER(pPager->errCode) ) return pPager->errCode; 5854 if( NEVER(pPager->errCode) ) return pPager->errCode;
4758 5855
4759 /* This function should not be called if the pager is not in at least 5856 assert( pPager->eState==PAGER_WRITER_LOCKED
4760 ** PAGER_RESERVED state. And indeed SQLite never does this. But it is 5857 || pPager->eState==PAGER_WRITER_FINISHED
4761 ** nice to have this defensive test here anyway. 5858 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
4762 */ 5859 );
4763 if( NEVER(pPager->state<PAGER_RESERVED) ) return SQLITE_ERROR; 5860 assert( assert_pager_state(pPager) );
4764 5861
4765 /* An optimization. If the database was not actually modified during 5862 /* An optimization. If the database was not actually modified during
4766 ** this transaction, the pager is running in exclusive-mode and is 5863 ** this transaction, the pager is running in exclusive-mode and is
4767 ** using persistent journals, then this function is a no-op. 5864 ** using persistent journals, then this function is a no-op.
4768 ** 5865 **
4769 ** The start of the journal file currently contains a single journal 5866 ** The start of the journal file currently contains a single journal
4770 ** header with the nRec field set to 0. If such a journal is used as 5867 ** header with the nRec field set to 0. If such a journal is used as
4771 ** a hot-journal during hot-journal rollback, 0 changes will be made 5868 ** a hot-journal during hot-journal rollback, 0 changes will be made
4772 ** to the database file. So there is no need to zero the journal 5869 ** to the database file. So there is no need to zero the journal
4773 ** header. Since the pager is in exclusive mode, there is no need 5870 ** header. Since the pager is in exclusive mode, there is no need
4774 ** to drop any locks either. 5871 ** to drop any locks either.
4775 */ 5872 */
4776 if( pPager->dbModified==0 && pPager->exclusiveMode 5873 if( pPager->eState==PAGER_WRITER_LOCKED
5874 && pPager->exclusiveMode
4777 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST 5875 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
4778 ){ 5876 ){
4779 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ); 5877 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
5878 pPager->eState = PAGER_READER;
4780 return SQLITE_OK; 5879 return SQLITE_OK;
4781 } 5880 }
4782 5881
4783 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager))); 5882 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
4784 assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dbModified );
4785 rc = pager_end_transaction(pPager, pPager->setMaster); 5883 rc = pager_end_transaction(pPager, pPager->setMaster);
4786 return pager_error(pPager, rc); 5884 return pager_error(pPager, rc);
4787 } 5885 }
4788 5886
4789 /* 5887 /*
4790 ** Rollback all changes. The database falls back to PAGER_SHARED mode. 5888 ** If a write transaction is open, then all changes made within the
5889 ** transaction are reverted and the current write-transaction is closed.
5890 ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
5891 ** state if an error occurs.
4791 ** 5892 **
4792 ** This function performs two tasks: 5893 ** If the pager is already in PAGER_ERROR state when this function is called,
5894 ** it returns Pager.errCode immediately. No work is performed in this case.
5895 **
5896 ** Otherwise, in rollback mode, this function performs two functions:
4793 ** 5897 **
4794 ** 1) It rolls back the journal file, restoring all database file and 5898 ** 1) It rolls back the journal file, restoring all database file and
4795 ** in-memory cache pages to the state they were in when the transaction 5899 ** in-memory cache pages to the state they were in when the transaction
4796 ** was opened, and 5900 ** was opened, and
5901 **
4797 ** 2) It finalizes the journal file, so that it is not used for hot 5902 ** 2) It finalizes the journal file, so that it is not used for hot
4798 ** rollback at any point in the future. 5903 ** rollback at any point in the future.
4799 ** 5904 **
4800 ** subject to the following qualifications: 5905 ** Finalization of the journal file (task 2) is only performed if the
5906 ** rollback is successful.
4801 ** 5907 **
4802 ** * If the journal file is not yet open when this function is called, 5908 ** In WAL mode, all cache-entries containing data modified within the
4803 ** then only (2) is performed. In this case there is no journal file 5909 ** current transaction are either expelled from the cache or reverted to
4804 ** to roll back. 5910 ** their pre-transaction state by re-reading data from the database or
4805 ** 5911 ** WAL files. The WAL transaction is then closed.
4806 ** * If in an error state other than SQLITE_FULL, then task (1) is
4807 ** performed. If successful, task (2). Regardless of the outcome
4808 ** of either, the error state error code is returned to the caller
4809 ** (i.e. either SQLITE_IOERR or SQLITE_CORRUPT).
4810 **
4811 ** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether
4812 ** or not (1) is succussful, also attempt (2). If successful, return
4813 ** SQLITE_OK. Otherwise, enter the error state and return the first
4814 ** error code encountered.
4815 **
4816 ** In this case there is no chance that the database was written to.
4817 ** So is safe to finalize the journal file even if the playback
4818 ** (operation 1) failed. However the pager must enter the error state
4819 ** as the contents of the in-memory cache are now suspect.
4820 **
4821 ** * Finally, if in PAGER_EXCLUSIVE state, then attempt (1). Only
4822 ** attempt (2) if (1) is successful. Return SQLITE_OK if successful,
4823 ** otherwise enter the error state and return the error code from the
4824 ** failing operation.
4825 **
4826 ** In this case the database file may have been written to. So if the
4827 ** playback operation did not succeed it would not be safe to finalize
4828 ** the journal file. It needs to be left in the file-system so that
4829 ** some other process can use it to restore the database state (by
4830 ** hot-journal rollback).
4831 */ 5912 */
4832 int sqlite3PagerRollback(Pager *pPager){ 5913 int sqlite3PagerRollback(Pager *pPager){
4833 int rc = SQLITE_OK; /* Return code */ 5914 int rc = SQLITE_OK; /* Return code */
4834 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager))); 5915 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
4835 if( !pPager->dbModified || !isOpen(pPager->jfd) ){ 5916
4836 rc = pager_end_transaction(pPager, pPager->setMaster); 5917 /* PagerRollback() is a no-op if called in READER or OPEN state. If
4837 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){ 5918 ** the pager is already in the ERROR state, the rollback is not
4838 if( pPager->state>=PAGER_EXCLUSIVE ){ 5919 ** attempted here. Instead, the error code is returned to the caller.
4839 pager_playback(pPager, 0); 5920 */
4840 } 5921 assert( assert_pager_state(pPager) );
4841 rc = pPager->errCode; 5922 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
5923 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
5924
5925 if( pagerUseWal(pPager) ){
5926 int rc2;
5927 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
5928 rc2 = pager_end_transaction(pPager, pPager->setMaster);
5929 if( rc==SQLITE_OK ) rc = rc2;
5930 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
5931 rc = pager_end_transaction(pPager, 0);
4842 }else{ 5932 }else{
4843 if( pPager->state==PAGER_RESERVED ){ 5933 rc = pager_playback(pPager, 0);
4844 int rc2; 5934 }
4845 rc = pager_playback(pPager, 0);
4846 rc2 = pager_end_transaction(pPager, pPager->setMaster);
4847 if( rc==SQLITE_OK ){
4848 rc = rc2;
4849 }
4850 }else{
4851 rc = pager_playback(pPager, 0);
4852 }
4853 5935
4854 if( !MEMDB ){ 5936 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
4855 pPager->dbSizeValid = 0; 5937 assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
4856 }
4857 5938
4858 /* If an error occurs during a ROLLBACK, we can no longer trust the pager 5939 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4859 ** cache. So call pager_error() on the way out to make any error 5940 ** cache. So call pager_error() on the way out to make any error persistent.
4860 ** persistent. 5941 */
4861 */ 5942 return pager_error(pPager, rc);
4862 rc = pager_error(pPager, rc);
4863 }
4864 return rc;
4865 } 5943 }
4866 5944
4867 /* 5945 /*
4868 ** Return TRUE if the database file is opened read-only. Return FALSE 5946 ** Return TRUE if the database file is opened read-only. Return FALSE
4869 ** if the database is (in theory) writable. 5947 ** if the database is (in theory) writable.
4870 */ 5948 */
4871 u8 sqlite3PagerIsreadonly(Pager *pPager){ 5949 u8 sqlite3PagerIsreadonly(Pager *pPager){
4872 return pPager->readOnly; 5950 return pPager->readOnly;
4873 } 5951 }
4874 5952
4875 /* 5953 /*
4876 ** Return the number of references to the pager. 5954 ** Return the number of references to the pager.
4877 */ 5955 */
4878 int sqlite3PagerRefcount(Pager *pPager){ 5956 int sqlite3PagerRefcount(Pager *pPager){
4879 return sqlite3PcacheRefCount(pPager->pPCache); 5957 return sqlite3PcacheRefCount(pPager->pPCache);
4880 } 5958 }
4881 5959
4882 /* 5960 /*
5961 ** Return the approximate number of bytes of memory currently
5962 ** used by the pager and its associated cache.
5963 */
5964 int sqlite3PagerMemUsed(Pager *pPager){
5965 int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
5966 + 5*sizeof(void*);
5967 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
5968 + sqlite3MallocSize(pPager)
5969 + pPager->pageSize;
5970 }
5971
5972 /*
4883 ** Return the number of references to the specified page. 5973 ** Return the number of references to the specified page.
4884 */ 5974 */
4885 int sqlite3PagerPageRefcount(DbPage *pPage){ 5975 int sqlite3PagerPageRefcount(DbPage *pPage){
4886 return sqlite3PcachePageRefcount(pPage); 5976 return sqlite3PcachePageRefcount(pPage);
4887 } 5977 }
4888 5978
4889 #ifdef SQLITE_TEST 5979 #ifdef SQLITE_TEST
4890 /* 5980 /*
4891 ** This routine is used for testing and analysis only. 5981 ** This routine is used for testing and analysis only.
4892 */ 5982 */
4893 int *sqlite3PagerStats(Pager *pPager){ 5983 int *sqlite3PagerStats(Pager *pPager){
4894 static int a[11]; 5984 static int a[11];
4895 a[0] = sqlite3PcacheRefCount(pPager->pPCache); 5985 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
4896 a[1] = sqlite3PcachePagecount(pPager->pPCache); 5986 a[1] = sqlite3PcachePagecount(pPager->pPCache);
4897 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache); 5987 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
4898 a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1; 5988 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
4899 a[4] = pPager->state; 5989 a[4] = pPager->eState;
4900 a[5] = pPager->errCode; 5990 a[5] = pPager->errCode;
4901 a[6] = pPager->nHit; 5991 a[6] = pPager->nHit;
4902 a[7] = pPager->nMiss; 5992 a[7] = pPager->nMiss;
4903 a[8] = 0; /* Used to be pPager->nOvfl */ 5993 a[8] = 0; /* Used to be pPager->nOvfl */
4904 a[9] = pPager->nRead; 5994 a[9] = pPager->nRead;
4905 a[10] = pPager->nWrite; 5995 a[10] = pPager->nWrite;
4906 return a; 5996 return a;
4907 } 5997 }
4908 #endif 5998 #endif
4909 5999
(...skipping 11 matching lines...) Expand all
4921 ** equal to nSavepoint, then this function is a no-op. 6011 ** equal to nSavepoint, then this function is a no-op.
4922 ** 6012 **
4923 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 6013 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
4924 ** occurs while opening the sub-journal file, then an IO error code is 6014 ** occurs while opening the sub-journal file, then an IO error code is
4925 ** returned. Otherwise, SQLITE_OK. 6015 ** returned. Otherwise, SQLITE_OK.
4926 */ 6016 */
4927 int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ 6017 int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
4928 int rc = SQLITE_OK; /* Return code */ 6018 int rc = SQLITE_OK; /* Return code */
4929 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */ 6019 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
4930 6020
6021 assert( pPager->eState>=PAGER_WRITER_LOCKED );
6022 assert( assert_pager_state(pPager) );
6023
4931 if( nSavepoint>nCurrent && pPager->useJournal ){ 6024 if( nSavepoint>nCurrent && pPager->useJournal ){
4932 int ii; /* Iterator variable */ 6025 int ii; /* Iterator variable */
4933 PagerSavepoint *aNew; /* New Pager.aSavepoint array */ 6026 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
4934 6027
4935 /* Either there is no active journal or the sub-journal is open or
4936 ** the journal is always stored in memory */
4937 assert( pPager->nSavepoint==0 || isOpen(pPager->sjfd) ||
4938 pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
4939
4940 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM 6028 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
4941 ** if the allocation fails. Otherwise, zero the new portion in case a 6029 ** if the allocation fails. Otherwise, zero the new portion in case a
4942 ** malloc failure occurs while populating it in the for(...) loop below. 6030 ** malloc failure occurs while populating it in the for(...) loop below.
4943 */ 6031 */
4944 aNew = (PagerSavepoint *)sqlite3Realloc( 6032 aNew = (PagerSavepoint *)sqlite3Realloc(
4945 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint 6033 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
4946 ); 6034 );
4947 if( !aNew ){ 6035 if( !aNew ){
4948 return SQLITE_NOMEM; 6036 return SQLITE_NOMEM;
4949 } 6037 }
4950 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); 6038 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
4951 pPager->aSavepoint = aNew; 6039 pPager->aSavepoint = aNew;
4952 pPager->nSavepoint = nSavepoint;
4953 6040
4954 /* Populate the PagerSavepoint structures just allocated. */ 6041 /* Populate the PagerSavepoint structures just allocated. */
4955 for(ii=nCurrent; ii<nSavepoint; ii++){ 6042 for(ii=nCurrent; ii<nSavepoint; ii++){
4956 assert( pPager->dbSizeValid );
4957 aNew[ii].nOrig = pPager->dbSize; 6043 aNew[ii].nOrig = pPager->dbSize;
4958 if( isOpen(pPager->jfd) && ALWAYS(pPager->journalOff>0) ){ 6044 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
4959 aNew[ii].iOffset = pPager->journalOff; 6045 aNew[ii].iOffset = pPager->journalOff;
4960 }else{ 6046 }else{
4961 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); 6047 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
4962 } 6048 }
4963 aNew[ii].iSubRec = pPager->nSubRec; 6049 aNew[ii].iSubRec = pPager->nSubRec;
4964 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); 6050 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
4965 if( !aNew[ii].pInSavepoint ){ 6051 if( !aNew[ii].pInSavepoint ){
4966 return SQLITE_NOMEM; 6052 return SQLITE_NOMEM;
4967 } 6053 }
6054 if( pagerUseWal(pPager) ){
6055 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
6056 }
6057 pPager->nSavepoint = ii+1;
4968 } 6058 }
4969 6059 assert( pPager->nSavepoint==nSavepoint );
4970 /* Open the sub-journal, if it is not already opened. */
4971 rc = openSubJournal(pPager);
4972 assertTruncateConstraint(pPager); 6060 assertTruncateConstraint(pPager);
4973 } 6061 }
4974 6062
4975 return rc; 6063 return rc;
4976 } 6064 }
4977 6065
4978 /* 6066 /*
4979 ** This function is called to rollback or release (commit) a savepoint. 6067 ** This function is called to rollback or release (commit) a savepoint.
4980 ** The savepoint to release or rollback need not be the most recently 6068 ** The savepoint to release or rollback need not be the most recently
4981 ** created savepoint. 6069 ** created savepoint.
(...skipping 17 matching lines...) Expand all
4999 ** 6087 **
5000 ** In any case, all savepoints with an index greater than iSavepoint 6088 ** In any case, all savepoints with an index greater than iSavepoint
5001 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE), 6089 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
5002 ** then savepoint iSavepoint is also destroyed. 6090 ** then savepoint iSavepoint is also destroyed.
5003 ** 6091 **
5004 ** This function may return SQLITE_NOMEM if a memory allocation fails, 6092 ** This function may return SQLITE_NOMEM if a memory allocation fails,
5005 ** or an IO error code if an IO error occurs while rolling back a 6093 ** or an IO error code if an IO error occurs while rolling back a
5006 ** savepoint. If no errors occur, SQLITE_OK is returned. 6094 ** savepoint. If no errors occur, SQLITE_OK is returned.
5007 */ 6095 */
5008 int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ 6096 int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
5009 int rc = SQLITE_OK; 6097 int rc = pPager->errCode; /* Return code */
5010 6098
5011 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); 6099 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
5012 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK ); 6100 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
5013 6101
5014 if( iSavepoint<pPager->nSavepoint ){ 6102 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
5015 int ii; /* Iterator variable */ 6103 int ii; /* Iterator variable */
5016 int nNew; /* Number of remaining savepoints after this op. */ 6104 int nNew; /* Number of remaining savepoints after this op. */
5017 6105
5018 /* Figure out how many savepoints will still be active after this 6106 /* Figure out how many savepoints will still be active after this
5019 ** operation. Store this value in nNew. Then free resources associated 6107 ** operation. Store this value in nNew. Then free resources associated
5020 ** with any savepoints that are destroyed by this operation. 6108 ** with any savepoints that are destroyed by this operation.
5021 */ 6109 */
5022 nNew = iSavepoint + (op==SAVEPOINT_ROLLBACK); 6110 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
5023 for(ii=nNew; ii<pPager->nSavepoint; ii++){ 6111 for(ii=nNew; ii<pPager->nSavepoint; ii++){
5024 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); 6112 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
5025 } 6113 }
5026 pPager->nSavepoint = nNew; 6114 pPager->nSavepoint = nNew;
5027 6115
5028 /* If this is a rollback operation, playback the specified savepoint. 6116 /* If this is a release of the outermost savepoint, truncate
6117 ** the sub-journal to zero bytes in size. */
6118 if( op==SAVEPOINT_RELEASE ){
6119 if( nNew==0 && isOpen(pPager->sjfd) ){
6120 /* Only truncate if it is an in-memory sub-journal. */
6121 if( sqlite3IsMemJournal(pPager->sjfd) ){
6122 rc = sqlite3OsTruncate(pPager->sjfd, 0);
6123 assert( rc==SQLITE_OK );
6124 }
6125 pPager->nSubRec = 0;
6126 }
6127 }
6128 /* Else this is a rollback operation, playback the specified savepoint.
5029 ** If this is a temp-file, it is possible that the journal file has 6129 ** If this is a temp-file, it is possible that the journal file has
5030 ** not yet been opened. In this case there have been no changes to 6130 ** not yet been opened. In this case there have been no changes to
5031 ** the database file, so the playback operation can be skipped. 6131 ** the database file, so the playback operation can be skipped.
5032 */ 6132 */
5033 if( op==SAVEPOINT_ROLLBACK && isOpen(pPager->jfd) ){ 6133 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
5034 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1]; 6134 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
5035 rc = pagerPlaybackSavepoint(pPager, pSavepoint); 6135 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
5036 assert(rc!=SQLITE_DONE); 6136 assert(rc!=SQLITE_DONE);
5037 } 6137 }
5038
5039 /* If this is a release of the outermost savepoint, truncate
5040 ** the sub-journal to zero bytes in size. */
5041 if( nNew==0 && op==SAVEPOINT_RELEASE && isOpen(pPager->sjfd) ){
5042 assert( rc==SQLITE_OK );
5043 rc = sqlite3OsTruncate(pPager->sjfd, 0);
5044 pPager->nSubRec = 0;
5045 }
5046 } 6138 }
6139
5047 return rc; 6140 return rc;
5048 } 6141 }
5049 6142
5050 /* 6143 /*
5051 ** Return the full pathname of the database file. 6144 ** Return the full pathname of the database file.
5052 */ 6145 */
5053 const char *sqlite3PagerFilename(Pager *pPager){ 6146 const char *sqlite3PagerFilename(Pager *pPager){
5054 return pPager->zFilename; 6147 return pPager->zFilename;
5055 } 6148 }
5056 6149
(...skipping 25 matching lines...) Expand all
5082 ** if fsync()s are executed normally. 6175 ** if fsync()s are executed normally.
5083 */ 6176 */
5084 int sqlite3PagerNosync(Pager *pPager){ 6177 int sqlite3PagerNosync(Pager *pPager){
5085 return pPager->noSync; 6178 return pPager->noSync;
5086 } 6179 }
5087 6180
5088 #ifdef SQLITE_HAS_CODEC 6181 #ifdef SQLITE_HAS_CODEC
5089 /* 6182 /*
5090 ** Set or retrieve the codec for this pager 6183 ** Set or retrieve the codec for this pager
5091 */ 6184 */
5092 static void sqlite3PagerSetCodec( 6185 void sqlite3PagerSetCodec(
5093 Pager *pPager, 6186 Pager *pPager,
5094 void *(*xCodec)(void*,void*,Pgno,int), 6187 void *(*xCodec)(void*,void*,Pgno,int),
5095 void (*xCodecSizeChng)(void*,int,int), 6188 void (*xCodecSizeChng)(void*,int,int),
5096 void (*xCodecFree)(void*), 6189 void (*xCodecFree)(void*),
5097 void *pCodec 6190 void *pCodec
5098 ){ 6191 ){
5099 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); 6192 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
5100 pPager->xCodec = xCodec; 6193 pPager->xCodec = pPager->memDb ? 0 : xCodec;
5101 pPager->xCodecSizeChng = xCodecSizeChng; 6194 pPager->xCodecSizeChng = xCodecSizeChng;
5102 pPager->xCodecFree = xCodecFree; 6195 pPager->xCodecFree = xCodecFree;
5103 pPager->pCodec = pCodec; 6196 pPager->pCodec = pCodec;
5104 pagerReportSize(pPager); 6197 pagerReportSize(pPager);
5105 } 6198 }
5106 static void *sqlite3PagerGetCodec(Pager *pPager){ 6199 void *sqlite3PagerGetCodec(Pager *pPager){
5107 return pPager->pCodec; 6200 return pPager->pCodec;
5108 } 6201 }
5109 #endif 6202 #endif
5110 6203
5111 #ifndef SQLITE_OMIT_AUTOVACUUM 6204 #ifndef SQLITE_OMIT_AUTOVACUUM
5112 /* 6205 /*
5113 ** Move the page pPg to location pgno in the file. 6206 ** Move the page pPg to location pgno in the file.
5114 ** 6207 **
5115 ** There must be no references to the page previously located at 6208 ** There must be no references to the page previously located at
5116 ** pgno (which we call pPgOld) though that page is allowed to be 6209 ** pgno (which we call pPgOld) though that page is allowed to be
(...skipping 17 matching lines...) Expand all
5134 ** This function may return SQLITE_NOMEM or an IO error code if an error 6227 ** This function may return SQLITE_NOMEM or an IO error code if an error
5135 ** occurs. Otherwise, it returns SQLITE_OK. 6228 ** occurs. Otherwise, it returns SQLITE_OK.
5136 */ 6229 */
5137 int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){ 6230 int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
5138 PgHdr *pPgOld; /* The page being overwritten. */ 6231 PgHdr *pPgOld; /* The page being overwritten. */
5139 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */ 6232 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
5140 int rc; /* Return code */ 6233 int rc; /* Return code */
5141 Pgno origPgno; /* The original page number */ 6234 Pgno origPgno; /* The original page number */
5142 6235
5143 assert( pPg->nRef>0 ); 6236 assert( pPg->nRef>0 );
6237 assert( pPager->eState==PAGER_WRITER_CACHEMOD
6238 || pPager->eState==PAGER_WRITER_DBMOD
6239 );
6240 assert( assert_pager_state(pPager) );
6241
6242 /* In order to be able to rollback, an in-memory database must journal
6243 ** the page we are moving from.
6244 */
6245 if( MEMDB ){
6246 rc = sqlite3PagerWrite(pPg);
6247 if( rc ) return rc;
6248 }
5144 6249
5145 /* If the page being moved is dirty and has not been saved by the latest 6250 /* If the page being moved is dirty and has not been saved by the latest
5146 ** savepoint, then save the current contents of the page into the 6251 ** savepoint, then save the current contents of the page into the
5147 ** sub-journal now. This is required to handle the following scenario: 6252 ** sub-journal now. This is required to handle the following scenario:
5148 ** 6253 **
5149 ** BEGIN; 6254 ** BEGIN;
5150 ** <journal page X, then modify it in memory> 6255 ** <journal page X, then modify it in memory>
5151 ** SAVEPOINT one; 6256 ** SAVEPOINT one;
5152 ** <Move page X to location Y> 6257 ** <Move page X to location Y>
5153 ** ROLLBACK TO one; 6258 ** ROLLBACK TO one;
5154 ** 6259 **
5155 ** If page X were not written to the sub-journal here, it would not 6260 ** If page X were not written to the sub-journal here, it would not
5156 ** be possible to restore its contents when the "ROLLBACK TO one" 6261 ** be possible to restore its contents when the "ROLLBACK TO one"
5157 ** statement were is processed. 6262 ** statement were is processed.
5158 ** 6263 **
5159 ** subjournalPage() may need to allocate space to store pPg->pgno into 6264 ** subjournalPage() may need to allocate space to store pPg->pgno into
5160 ** one or more savepoint bitvecs. This is the reason this function 6265 ** one or more savepoint bitvecs. This is the reason this function
5161 ** may return SQLITE_NOMEM. 6266 ** may return SQLITE_NOMEM.
5162 */ 6267 */
5163 if( pPg->flags&PGHDR_DIRTY 6268 if( pPg->flags&PGHDR_DIRTY
5164 && subjRequiresPage(pPg) 6269 && subjRequiresPage(pPg)
5165 && SQLITE_OK!=(rc = subjournalPage(pPg)) 6270 && SQLITE_OK!=(rc = subjournalPage(pPg))
5166 ){ 6271 ){
5167 return rc; 6272 return rc;
5168 } 6273 }
5169 6274
5170 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", 6275 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
5171 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno)); 6276 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
5172 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno)) 6277 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
5173 6278
5174 /* If the journal needs to be sync()ed before page pPg->pgno can 6279 /* If the journal needs to be sync()ed before page pPg->pgno can
5175 ** be written to, store pPg->pgno in local variable needSyncPgno. 6280 ** be written to, store pPg->pgno in local variable needSyncPgno.
5176 ** 6281 **
5177 ** If the isCommit flag is set, there is no need to remember that 6282 ** If the isCommit flag is set, there is no need to remember that
5178 ** the journal needs to be sync()ed before database page pPg->pgno 6283 ** the journal needs to be sync()ed before database page pPg->pgno
5179 ** can be written to. The caller has already promised not to write to it. 6284 ** can be written to. The caller has already promised not to write to it.
5180 */ 6285 */
5181 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){ 6286 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
5182 needSyncPgno = pPg->pgno; 6287 needSyncPgno = pPg->pgno;
5183 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); 6288 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
5184 assert( pPg->flags&PGHDR_DIRTY ); 6289 assert( pPg->flags&PGHDR_DIRTY );
5185 assert( pPager->needSync );
5186 } 6290 }
5187 6291
5188 /* If the cache contains a page with page-number pgno, remove it 6292 /* If the cache contains a page with page-number pgno, remove it
5189 ** from its hash chain. Also, if the PgHdr.needSync was set for 6293 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
5190 ** page pgno before the 'move' operation, it needs to be retained 6294 ** page pgno before the 'move' operation, it needs to be retained
5191 ** for the page moved there. 6295 ** for the page moved there.
5192 */ 6296 */
5193 pPg->flags &= ~PGHDR_NEED_SYNC; 6297 pPg->flags &= ~PGHDR_NEED_SYNC;
5194 pPgOld = pager_lookup(pPager, pgno); 6298 pPgOld = pager_lookup(pPager, pgno);
5195 assert( !pPgOld || pPgOld->nRef==1 ); 6299 assert( !pPgOld || pPgOld->nRef==1 );
5196 if( pPgOld ){ 6300 if( pPgOld ){
5197 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); 6301 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
5198 sqlite3PcacheDrop(pPgOld); 6302 if( MEMDB ){
6303 /* Do not discard pages from an in-memory database since we might
6304 ** need to rollback later. Just move the page out of the way. */
6305 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
6306 }else{
6307 sqlite3PcacheDrop(pPgOld);
6308 }
5199 } 6309 }
5200 6310
5201 origPgno = pPg->pgno; 6311 origPgno = pPg->pgno;
5202 sqlite3PcacheMove(pPg, pgno); 6312 sqlite3PcacheMove(pPg, pgno);
5203 sqlite3PcacheMakeDirty(pPg); 6313 sqlite3PcacheMakeDirty(pPg);
5204 pPager->dbModified = 1; 6314
6315 /* For an in-memory database, make sure the original page continues
6316 ** to exist, in case the transaction needs to roll back. Use pPgOld
6317 ** as the original page since it has already been allocated.
6318 */
6319 if( MEMDB ){
6320 assert( pPgOld );
6321 sqlite3PcacheMove(pPgOld, origPgno);
6322 sqlite3PagerUnref(pPgOld);
6323 }
5205 6324
5206 if( needSyncPgno ){ 6325 if( needSyncPgno ){
5207 /* If needSyncPgno is non-zero, then the journal file needs to be 6326 /* If needSyncPgno is non-zero, then the journal file needs to be
5208 ** sync()ed before any data is written to database file page needSyncPgno. 6327 ** sync()ed before any data is written to database file page needSyncPgno.
5209 ** Currently, no such page exists in the page-cache and the 6328 ** Currently, no such page exists in the page-cache and the
5210 ** "is journaled" bitvec flag has been set. This needs to be remedied by 6329 ** "is journaled" bitvec flag has been set. This needs to be remedied by
5211 ** loading the page into the pager-cache and setting the PgHdr.needSync 6330 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
5212 ** flag. 6331 ** flag.
5213 ** 6332 **
5214 ** If the attempt to load the page into the page-cache fails, (due 6333 ** If the attempt to load the page into the page-cache fails, (due
5215 ** to a malloc() or IO failure), clear the bit in the pInJournal[] 6334 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
5216 ** array. Otherwise, if the page is loaded and written again in 6335 ** array. Otherwise, if the page is loaded and written again in
5217 ** this transaction, it may be written to the database file before 6336 ** this transaction, it may be written to the database file before
5218 ** it is synced into the journal file. This way, it may end up in 6337 ** it is synced into the journal file. This way, it may end up in
5219 ** the journal file twice, but that is not a problem. 6338 ** the journal file twice, but that is not a problem.
5220 **
5221 ** The sqlite3PagerGet() call may cause the journal to sync. So make
5222 ** sure the Pager.needSync flag is set too.
5223 */ 6339 */
5224 PgHdr *pPgHdr; 6340 PgHdr *pPgHdr;
5225 assert( pPager->needSync );
5226 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); 6341 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
5227 if( rc!=SQLITE_OK ){ 6342 if( rc!=SQLITE_OK ){
5228 if( needSyncPgno<=pPager->dbOrigSize ){ 6343 if( needSyncPgno<=pPager->dbOrigSize ){
5229 assert( pPager->pTmpSpace!=0 ); 6344 assert( pPager->pTmpSpace!=0 );
5230 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace); 6345 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
5231 } 6346 }
5232 return rc; 6347 return rc;
5233 } 6348 }
5234 pPager->needSync = 1;
5235 assert( pPager->noSync==0 && !MEMDB );
5236 pPgHdr->flags |= PGHDR_NEED_SYNC; 6349 pPgHdr->flags |= PGHDR_NEED_SYNC;
5237 sqlite3PcacheMakeDirty(pPgHdr); 6350 sqlite3PcacheMakeDirty(pPgHdr);
5238 sqlite3PagerUnref(pPgHdr); 6351 sqlite3PagerUnref(pPgHdr);
5239 } 6352 }
5240 6353
5241 /*
5242 ** For an in-memory database, make sure the original page continues
5243 ** to exist, in case the transaction needs to roll back. We allocate
5244 ** the page now, instead of at rollback, because we can better deal
5245 ** with an out-of-memory error now. Ticket #3761.
5246 */
5247 if( MEMDB ){
5248 DbPage *pNew;
5249 rc = sqlite3PagerAcquire(pPager, origPgno, &pNew, 1);
5250 if( rc!=SQLITE_OK ){
5251 sqlite3PcacheMove(pPg, origPgno);
5252 return rc;
5253 }
5254 sqlite3PagerUnref(pNew);
5255 }
5256
5257 return SQLITE_OK; 6354 return SQLITE_OK;
5258 } 6355 }
5259 #endif 6356 #endif
5260 6357
5261 /* Begin preload-cache.patch for Chromium */ 6358 /* Begin preload-cache.patch for Chromium */
5262 /** 6359 /**
5263 ** When making large allocations, there is no need to stress the heap and 6360 ** When making large allocations, there is no need to stress the heap and
5264 ** potentially hold its lock while we allocate a bunch of memory. If we know 6361 ** potentially hold its lock while we allocate a bunch of memory. If we know
5265 ** the allocation will be large, go directly to the OS instead of the heap. 6362 ** the allocation will be large, go directly to the OS instead of the heap.
5266 **/ 6363 **/
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
5375 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); 6472 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5376 assert( PAGER_LOCKINGMODE_QUERY<0 ); 6473 assert( PAGER_LOCKINGMODE_QUERY<0 );
5377 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 ); 6474 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5378 if( eMode>=0 && !pPager->tempFile ){ 6475 if( eMode>=0 && !pPager->tempFile ){
5379 pPager->exclusiveMode = (u8)eMode; 6476 pPager->exclusiveMode = (u8)eMode;
5380 } 6477 }
5381 return (int)pPager->exclusiveMode; 6478 return (int)pPager->exclusiveMode;
5382 } 6479 }
5383 6480
5384 /* 6481 /*
5385 ** Get/set the journal-mode for this pager. Parameter eMode must be one of: 6482 ** Set the journal-mode for this pager. Parameter eMode must be one of:
5386 ** 6483 **
5387 ** PAGER_JOURNALMODE_QUERY
5388 ** PAGER_JOURNALMODE_DELETE 6484 ** PAGER_JOURNALMODE_DELETE
5389 ** PAGER_JOURNALMODE_TRUNCATE 6485 ** PAGER_JOURNALMODE_TRUNCATE
5390 ** PAGER_JOURNALMODE_PERSIST 6486 ** PAGER_JOURNALMODE_PERSIST
5391 ** PAGER_JOURNALMODE_OFF 6487 ** PAGER_JOURNALMODE_OFF
5392 ** PAGER_JOURNALMODE_MEMORY 6488 ** PAGER_JOURNALMODE_MEMORY
6489 ** PAGER_JOURNALMODE_WAL
5393 ** 6490 **
5394 ** If the parameter is not _QUERY, then the journal_mode is set to the 6491 ** The journalmode is set to the value specified if the change is allowed.
5395 ** value specified if the change is allowed. The change is disallowed 6492 ** The change may be disallowed for the following reasons:
5396 ** for the following reasons:
5397 ** 6493 **
5398 ** * An in-memory database can only have its journal_mode set to _OFF 6494 ** * An in-memory database can only have its journal_mode set to _OFF
5399 ** or _MEMORY. 6495 ** or _MEMORY.
5400 ** 6496 **
5401 ** * The journal mode may not be changed while a transaction is active. 6497 ** * Temporary databases cannot have _WAL journalmode.
5402 ** 6498 **
5403 ** The returned indicate the current (possibly updated) journal-mode. 6499 ** The returned indicate the current (possibly updated) journal-mode.
5404 */ 6500 */
5405 int sqlite3PagerJournalMode(Pager *pPager, int eMode){ 6501 int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
5406 assert( eMode==PAGER_JOURNALMODE_QUERY 6502 u8 eOld = pPager->journalMode; /* Prior journalmode */
5407 || eMode==PAGER_JOURNALMODE_DELETE 6503
6504 #ifdef SQLITE_DEBUG
6505 /* The print_pager_state() routine is intended to be used by the debugger
6506 ** only. We invoke it once here to suppress a compiler warning. */
6507 print_pager_state(pPager);
6508 #endif
6509
6510
6511 /* The eMode parameter is always valid */
6512 assert( eMode==PAGER_JOURNALMODE_DELETE
5408 || eMode==PAGER_JOURNALMODE_TRUNCATE 6513 || eMode==PAGER_JOURNALMODE_TRUNCATE
5409 || eMode==PAGER_JOURNALMODE_PERSIST 6514 || eMode==PAGER_JOURNALMODE_PERSIST
5410 || eMode==PAGER_JOURNALMODE_OFF 6515 || eMode==PAGER_JOURNALMODE_OFF
6516 || eMode==PAGER_JOURNALMODE_WAL
5411 || eMode==PAGER_JOURNALMODE_MEMORY ); 6517 || eMode==PAGER_JOURNALMODE_MEMORY );
5412 assert( PAGER_JOURNALMODE_QUERY<0 ); 6518
5413 if( eMode>=0 6519 /* This routine is only called from the OP_JournalMode opcode, and
5414 && (!MEMDB || eMode==PAGER_JOURNALMODE_MEMORY 6520 ** the logic there will never allow a temporary file to be changed
5415 || eMode==PAGER_JOURNALMODE_OFF) 6521 ** to WAL mode.
5416 && !pPager->dbModified 6522 */
5417 && (!isOpen(pPager->jfd) || 0==pPager->journalOff) 6523 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
5418 ){ 6524
5419 if( isOpen(pPager->jfd) ){ 6525 /* Do allow the journalmode of an in-memory database to be set to
6526 ** anything other than MEMORY or OFF
6527 */
6528 if( MEMDB ){
6529 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
6530 if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
6531 eMode = eOld;
6532 }
6533 }
6534
6535 if( eMode!=eOld ){
6536
6537 /* Change the journal mode. */
6538 assert( pPager->eState!=PAGER_ERROR );
6539 pPager->journalMode = (u8)eMode;
6540
6541 /* When transistioning from TRUNCATE or PERSIST to any other journal
6542 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
6543 ** delete the journal file.
6544 */
6545 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
6546 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
6547 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
6548 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
6549 assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
6550 assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
6551
6552 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
6553 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
6554
6555 /* In this case we would like to delete the journal file. If it is
6556 ** not possible, then that is not a problem. Deleting the journal file
6557 ** here is an optimization only.
6558 **
6559 ** Before deleting the journal file, obtain a RESERVED lock on the
6560 ** database file. This ensures that the journal file is not deleted
6561 ** while it is in use by some other client.
6562 */
5420 sqlite3OsClose(pPager->jfd); 6563 sqlite3OsClose(pPager->jfd);
6564 if( pPager->eLock>=RESERVED_LOCK ){
6565 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
6566 }else{
6567 int rc = SQLITE_OK;
6568 int state = pPager->eState;
6569 assert( state==PAGER_OPEN || state==PAGER_READER );
6570 if( state==PAGER_OPEN ){
6571 rc = sqlite3PagerSharedLock(pPager);
6572 }
6573 if( pPager->eState==PAGER_READER ){
6574 assert( rc==SQLITE_OK );
6575 rc = pagerLockDb(pPager, RESERVED_LOCK);
6576 }
6577 if( rc==SQLITE_OK ){
6578 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
6579 }
6580 if( rc==SQLITE_OK && state==PAGER_READER ){
6581 pagerUnlockDb(pPager, SHARED_LOCK);
6582 }else if( state==PAGER_OPEN ){
6583 pager_unlock(pPager);
6584 }
6585 assert( state==pPager->eState );
6586 }
5421 } 6587 }
5422 pPager->journalMode = (u8)eMode;
5423 } 6588 }
6589
6590 /* Return the new journal mode */
5424 return (int)pPager->journalMode; 6591 return (int)pPager->journalMode;
5425 } 6592 }
5426 6593
5427 /* 6594 /*
6595 ** Return the current journal mode.
6596 */
6597 int sqlite3PagerGetJournalMode(Pager *pPager){
6598 return (int)pPager->journalMode;
6599 }
6600
6601 /*
6602 ** Return TRUE if the pager is in a state where it is OK to change the
6603 ** journalmode. Journalmode changes can only happen when the database
6604 ** is unmodified.
6605 */
6606 int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
6607 assert( assert_pager_state(pPager) );
6608 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
6609 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
6610 return 1;
6611 }
6612
6613 /*
5428 ** Get/set the size-limit used for persistent journal files. 6614 ** Get/set the size-limit used for persistent journal files.
5429 ** 6615 **
5430 ** Setting the size limit to -1 means no limit is enforced. 6616 ** Setting the size limit to -1 means no limit is enforced.
5431 ** An attempt to set a limit smaller than -1 is a no-op. 6617 ** An attempt to set a limit smaller than -1 is a no-op.
5432 */ 6618 */
5433 i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){ 6619 i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
5434 if( iLimit>=-1 ){ 6620 if( iLimit>=-1 ){
5435 pPager->journalSizeLimit = iLimit; 6621 pPager->journalSizeLimit = iLimit;
5436 } 6622 }
5437 return pPager->journalSizeLimit; 6623 return pPager->journalSizeLimit;
5438 } 6624 }
5439 6625
5440 /* 6626 /*
5441 ** Return a pointer to the pPager->pBackup variable. The backup module 6627 ** Return a pointer to the pPager->pBackup variable. The backup module
5442 ** in backup.c maintains the content of this variable. This module 6628 ** in backup.c maintains the content of this variable. This module
5443 ** uses it opaquely as an argument to sqlite3BackupRestart() and 6629 ** uses it opaquely as an argument to sqlite3BackupRestart() and
5444 ** sqlite3BackupUpdate() only. 6630 ** sqlite3BackupUpdate() only.
5445 */ 6631 */
5446 sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){ 6632 sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
5447 return &pPager->pBackup; 6633 return &pPager->pBackup;
5448 } 6634 }
5449 6635
6636 #ifndef SQLITE_OMIT_WAL
6637 /*
6638 ** This function is called when the user invokes "PRAGMA checkpoint".
6639 */
6640 int sqlite3PagerCheckpoint(Pager *pPager){
6641 int rc = SQLITE_OK;
6642 if( pPager->pWal ){
6643 u8 *zBuf = (u8 *)pPager->pTmpSpace;
6644 rc = sqlite3WalCheckpoint(pPager->pWal,
6645 (pPager->noSync ? 0 : pPager->sync_flags),
6646 pPager->pageSize, zBuf
6647 );
6648 }
6649 return rc;
6650 }
6651
6652 int sqlite3PagerWalCallback(Pager *pPager){
6653 return sqlite3WalCallback(pPager->pWal);
6654 }
6655
6656 /*
6657 ** Return true if the underlying VFS for the given pager supports the
6658 ** primitives necessary for write-ahead logging.
6659 */
6660 int sqlite3PagerWalSupported(Pager *pPager){
6661 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
6662 return pMethods->iVersion>=2 && pMethods->xShmMap!=0;
6663 }
6664
6665 /*
6666 ** The caller must be holding a SHARED lock on the database file to call
6667 ** this function.
6668 **
6669 ** If the pager passed as the first argument is open on a real database
6670 ** file (not a temp file or an in-memory database), and the WAL file
6671 ** is not already open, make an attempt to open it now. If successful,
6672 ** return SQLITE_OK. If an error occurs or the VFS used by the pager does
6673 ** not support the xShmXXX() methods, return an error code. *pbOpen is
6674 ** not modified in either case.
6675 **
6676 ** If the pager is open on a temp-file (or in-memory database), or if
6677 ** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
6678 ** without doing anything.
6679 */
6680 int sqlite3PagerOpenWal(
6681 Pager *pPager, /* Pager object */
6682 int *pbOpen /* OUT: Set to true if call is a no-op */
6683 ){
6684 int rc = SQLITE_OK; /* Return code */
6685
6686 assert( assert_pager_state(pPager) );
6687 assert( pPager->eState==PAGER_OPEN || pbOpen );
6688 assert( pPager->eState==PAGER_READER || !pbOpen );
6689 assert( pbOpen==0 || *pbOpen==0 );
6690 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
6691
6692 if( !pPager->tempFile && !pPager->pWal ){
6693 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
6694
6695 /* Close any rollback journal previously open */
6696 sqlite3OsClose(pPager->jfd);
6697
6698 /* Open the connection to the log file. If this operation fails,
6699 ** (e.g. due to malloc() failure), unlock the database file and
6700 ** return an error code.
6701 */
6702 rc = sqlite3WalOpen(pPager->pVfs, pPager->fd, pPager->zWal, &pPager->pWal);
6703 if( rc==SQLITE_OK ){
6704 pPager->journalMode = PAGER_JOURNALMODE_WAL;
6705 pPager->eState = PAGER_OPEN;
6706 }
6707 }else{
6708 *pbOpen = 1;
6709 }
6710
6711 return rc;
6712 }
6713
6714 /*
6715 ** This function is called to close the connection to the log file prior
6716 ** to switching from WAL to rollback mode.
6717 **
6718 ** Before closing the log file, this function attempts to take an
6719 ** EXCLUSIVE lock on the database file. If this cannot be obtained, an
6720 ** error (SQLITE_BUSY) is returned and the log connection is not closed.
6721 ** If successful, the EXCLUSIVE lock is not released before returning.
6722 */
6723 int sqlite3PagerCloseWal(Pager *pPager){
6724 int rc = SQLITE_OK;
6725
6726 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
6727
6728 /* If the log file is not already open, but does exist in the file-system,
6729 ** it may need to be checkpointed before the connection can switch to
6730 ** rollback mode. Open it now so this can happen.
6731 */
6732 if( !pPager->pWal ){
6733 int logexists = 0;
6734 rc = pagerLockDb(pPager, SHARED_LOCK);
6735 if( rc==SQLITE_OK ){
6736 rc = sqlite3OsAccess(
6737 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
6738 );
6739 }
6740 if( rc==SQLITE_OK && logexists ){
6741 rc = sqlite3WalOpen(pPager->pVfs, pPager->fd,
6742 pPager->zWal, &pPager->pWal);
6743 }
6744 }
6745
6746 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
6747 ** the database file, the log and log-summary files will be deleted.
6748 */
6749 if( rc==SQLITE_OK && pPager->pWal ){
6750 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
6751 if( rc==SQLITE_OK ){
6752 rc = sqlite3WalClose(pPager->pWal,
6753 (pPager->noSync ? 0 : pPager->sync_flags),
6754 pPager->pageSize, (u8*)pPager->pTmpSpace
6755 );
6756 pPager->pWal = 0;
6757 }else{
6758 /* If we cannot get an EXCLUSIVE lock, downgrade the PENDING lock
6759 ** that we did get back to SHARED. */
6760 pagerUnlockDb(pPager, SQLITE_LOCK_SHARED);
6761 }
6762 }
6763 return rc;
6764 }
6765
6766 #ifdef SQLITE_HAS_CODEC
6767 /*
6768 ** This function is called by the wal module when writing page content
6769 ** into the log file.
6770 **
6771 ** This function returns a pointer to a buffer containing the encrypted
6772 ** page content. If a malloc fails, this function may return NULL.
6773 */
6774 void *sqlite3PagerCodec(PgHdr *pPg){
6775 void *aData = 0;
6776 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
6777 return aData;
6778 }
6779 #endif /* SQLITE_HAS_CODEC */
6780
6781 #endif /* !SQLITE_OMIT_WAL */
6782
5450 #endif /* SQLITE_OMIT_DISKIO */ 6783 #endif /* SQLITE_OMIT_DISKIO */
OLDNEW
« no previous file with comments | « third_party/sqlite/src/src/pager.h ('k') | third_party/sqlite/src/src/parse.y » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698