000001 /*
000002 ** 2010 February 1
000003 **
000004 ** The author disclaims copyright to this source code. In place of
000005 ** a legal notice, here is a blessing:
000006 **
000007 ** May you do good and not evil.
000008 ** May you find forgiveness for yourself and forgive others.
000009 ** May you share freely, never taking more than you give.
000010 **
000011 *************************************************************************
000012 **
000013 ** This file contains the implementation of a write-ahead log (WAL) used in
000014 ** "journal_mode=WAL" mode.
000015 **
000016 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
000017 **
000018 ** A WAL file consists of a header followed by zero or more "frames".
000019 ** Each frame records the revised content of a single page from the
000020 ** database file. All changes to the database are recorded by writing
000021 ** frames into the WAL. Transactions commit when a frame is written that
000022 ** contains a commit marker. A single WAL can and usually does record
000023 ** multiple transactions. Periodically, the content of the WAL is
000024 ** transferred back into the database file in an operation called a
000025 ** "checkpoint".
000026 **
000027 ** A single WAL file can be used multiple times. In other words, the
000028 ** WAL can fill up with frames and then be checkpointed and then new
000029 ** frames can overwrite the old ones. A WAL always grows from beginning
000030 ** toward the end. Checksums and counters attached to each frame are
000031 ** used to determine which frames within the WAL are valid and which
000032 ** are leftovers from prior checkpoints.
000033 **
000034 ** The WAL header is 32 bytes in size and consists of the following eight
000035 ** big-endian 32-bit unsigned integer values:
000036 **
000037 ** 0: Magic number. 0x377f0682 or 0x377f0683
000038 ** 4: File format version. Currently 3007000
000039 ** 8: Database page size. Example: 1024
000040 ** 12: Checkpoint sequence number
000041 ** 16: Salt-1, random integer incremented with each checkpoint
000042 ** 20: Salt-2, a different random integer changing with each ckpt
000043 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
000044 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
000045 **
000046 ** Immediately following the wal-header are zero or more frames. Each
000047 ** frame consists of a 24-byte frame-header followed by <page-size> bytes
000048 ** of page data. The frame-header is six big-endian 32-bit unsigned
000049 ** integer values, as follows:
000050 **
000051 ** 0: Page number.
000052 ** 4: For commit records, the size of the database image in pages
000053 ** after the commit. For all other records, zero.
000054 ** 8: Salt-1 (copied from the header)
000055 ** 12: Salt-2 (copied from the header)
000056 ** 16: Checksum-1.
000057 ** 20: Checksum-2.
000058 **
000059 ** A frame is considered valid if and only if the following conditions are
000060 ** true:
000061 **
000062 ** (1) The salt-1 and salt-2 values in the frame-header match
000063 ** salt values in the wal-header
000064 **
000065 ** (2) The checksum values in the final 8 bytes of the frame-header
000066 ** exactly match the checksum computed consecutively on the
000067 ** WAL header and the first 8 bytes and the content of all frames
000068 ** up to and including the current frame.
000069 **
000070 ** The checksum is computed using 32-bit big-endian integers if the
000071 ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
000072 ** is computed using little-endian if the magic number is 0x377f0682.
000073 ** The checksum values are always stored in the frame header in a
000074 ** big-endian format regardless of which byte order is used to compute
000075 ** the checksum. The checksum is computed by interpreting the input as
000076 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
000077 ** algorithm used for the checksum is as follows:
000078 **
000079 ** for i from 0 to n-1 step 2:
000080 ** s0 += x[i] + s1;
000081 ** s1 += x[i+1] + s0;
000082 ** endfor
000083 **
000084 ** Note that s0 and s1 are both weighted checksums using fibonacci weights
000085 ** in reverse order (the largest fibonacci weight occurs on the first element
000086 ** of the sequence being summed.) The s1 value spans all 32-bit
000087 ** terms of the sequence whereas s0 omits the final term.
000088 **
000089 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
000090 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
000091 ** The VFS.xSync operations serve as write barriers - all writes launched
000092 ** before the xSync must complete before any write that launches after the
000093 ** xSync begins.
000094 **
000095 ** After each checkpoint, the salt-1 value is incremented and the salt-2
000096 ** value is randomized. This prevents old and new frames in the WAL from
000097 ** being considered valid at the same time and being checkpointing together
000098 ** following a crash.
000099 **
000100 ** READER ALGORITHM
000101 **
000102 ** To read a page from the database (call it page number P), a reader
000103 ** first checks the WAL to see if it contains page P. If so, then the
000104 ** last valid instance of page P that is a followed by a commit frame
000105 ** or is a commit frame itself becomes the value read. If the WAL
000106 ** contains no copies of page P that are valid and which are a commit
000107 ** frame or are followed by a commit frame, then page P is read from
000108 ** the database file.
000109 **
000110 ** To start a read transaction, the reader records the index of the last
000111 ** valid frame in the WAL. The reader uses this recorded "mxFrame" value
000112 ** for all subsequent read operations. New transactions can be appended
000113 ** to the WAL, but as long as the reader uses its original mxFrame value
000114 ** and ignores the newly appended content, it will see a consistent snapshot
000115 ** of the database from a single point in time. This technique allows
000116 ** multiple concurrent readers to view different versions of the database
000117 ** content simultaneously.
000118 **
000119 ** The reader algorithm in the previous paragraphs works correctly, but
000120 ** because frames for page P can appear anywhere within the WAL, the
000121 ** reader has to scan the entire WAL looking for page P frames. If the
000122 ** WAL is large (multiple megabytes is typical) that scan can be slow,
000123 ** and read performance suffers. To overcome this problem, a separate
000124 ** data structure called the wal-index is maintained to expedite the
000125 ** search for frames of a particular page.
000126 **
000127 ** WAL-INDEX FORMAT
000128 **
000129 ** Conceptually, the wal-index is shared memory, though VFS implementations
000130 ** might choose to implement the wal-index using a mmapped file. Because
000131 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
000132 ** on a network filesystem. All users of the database must be able to
000133 ** share memory.
000134 **
000135 ** In the default unix and windows implementation, the wal-index is a mmapped
000136 ** file whose name is the database name with a "-shm" suffix added. For that
000137 ** reason, the wal-index is sometimes called the "shm" file.
000138 **
000139 ** The wal-index is transient. After a crash, the wal-index can (and should
000140 ** be) reconstructed from the original WAL file. In fact, the VFS is required
000141 ** to either truncate or zero the header of the wal-index when the last
000142 ** connection to it closes. Because the wal-index is transient, it can
000143 ** use an architecture-specific format; it does not have to be cross-platform.
000144 ** Hence, unlike the database and WAL file formats which store all values
000145 ** as big endian, the wal-index can store multi-byte values in the native
000146 ** byte order of the host computer.
000147 **
000148 ** The purpose of the wal-index is to answer this question quickly: Given
000149 ** a page number P and a maximum frame index M, return the index of the
000150 ** last frame in the wal before frame M for page P in the WAL, or return
000151 ** NULL if there are no frames for page P in the WAL prior to M.
000152 **
000153 ** The wal-index consists of a header region, followed by an one or
000154 ** more index blocks.
000155 **
000156 ** The wal-index header contains the total number of frames within the WAL
000157 ** in the mxFrame field.
000158 **
000159 ** Each index block except for the first contains information on
000160 ** HASHTABLE_NPAGE frames. The first index block contains information on
000161 ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
000162 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
000163 ** first index block are the same size as all other index blocks in the
000164 ** wal-index. The values are:
000165 **
000166 ** HASHTABLE_NPAGE 4096
000167 ** HASHTABLE_NPAGE_ONE 4062
000168 **
000169 ** Each index block contains two sections, a page-mapping that contains the
000170 ** database page number associated with each wal frame, and a hash-table
000171 ** that allows readers to query an index block for a specific page number.
000172 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
000173 ** for the first index block) 32-bit page numbers. The first entry in the
000174 ** first index-block contains the database page number corresponding to the
000175 ** first frame in the WAL file. The first entry in the second index block
000176 ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
000177 ** the log, and so on.
000178 **
000179 ** The last index block in a wal-index usually contains less than the full
000180 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
000181 ** depending on the contents of the WAL file. This does not change the
000182 ** allocated size of the page-mapping array - the page-mapping array merely
000183 ** contains unused entries.
000184 **
000185 ** Even without using the hash table, the last frame for page P
000186 ** can be found by scanning the page-mapping sections of each index block
000187 ** starting with the last index block and moving toward the first, and
000188 ** within each index block, starting at the end and moving toward the
000189 ** beginning. The first entry that equals P corresponds to the frame
000190 ** holding the content for that page.
000191 **
000192 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
000193 ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
000194 ** hash table for each page number in the mapping section, so the hash
000195 ** table is never more than half full. The expected number of collisions
000196 ** prior to finding a match is 1. Each entry of the hash table is an
000197 ** 1-based index of an entry in the mapping section of the same
000198 ** index block. Let K be the 1-based index of the largest entry in
000199 ** the mapping section. (For index blocks other than the last, K will
000200 ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
000201 ** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
000202 ** contain a value of 0.
000203 **
000204 ** To look for page P in the hash table, first compute a hash iKey on
000205 ** P as follows:
000206 **
000207 ** iKey = (P * 383) % HASHTABLE_NSLOT
000208 **
000209 ** Then start scanning entries of the hash table, starting with iKey
000210 ** (wrapping around to the beginning when the end of the hash table is
000211 ** reached) until an unused hash slot is found. Let the first unused slot
000212 ** be at index iUnused. (iUnused might be less than iKey if there was
000213 ** wrap-around.) Because the hash table is never more than half full,
000214 ** the search is guaranteed to eventually hit an unused entry. Let
000215 ** iMax be the value between iKey and iUnused, closest to iUnused,
000216 ** where aHash[iMax]==P. If there is no iMax entry (if there exists
000217 ** no hash slot such that aHash[i]==p) then page P is not in the
000218 ** current index block. Otherwise the iMax-th mapping entry of the
000219 ** current index block corresponds to the last entry that references
000220 ** page P.
000221 **
000222 ** A hash search begins with the last index block and moves toward the
000223 ** first index block, looking for entries corresponding to page P. On
000224 ** average, only two or three slots in each index block need to be
000225 ** examined in order to either find the last entry for page P, or to
000226 ** establish that no such entry exists in the block. Each index block
000227 ** holds over 4000 entries. So two or three index blocks are sufficient
000228 ** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
000229 ** comparisons (on average) suffice to either locate a frame in the
000230 ** WAL or to establish that the frame does not exist in the WAL. This
000231 ** is much faster than scanning the entire 10MB WAL.
000232 **
000233 ** Note that entries are added in order of increasing K. Hence, one
000234 ** reader might be using some value K0 and a second reader that started
000235 ** at a later time (after additional transactions were added to the WAL
000236 ** and to the wal-index) might be using a different value K1, where K1>K0.
000237 ** Both readers can use the same hash table and mapping section to get
000238 ** the correct result. There may be entries in the hash table with
000239 ** K>K0 but to the first reader, those entries will appear to be unused
000240 ** slots in the hash table and so the first reader will get an answer as
000241 ** if no values greater than K0 had ever been inserted into the hash table
000242 ** in the first place - which is what reader one wants. Meanwhile, the
000243 ** second reader using K1 will see additional values that were inserted
000244 ** later, which is exactly what reader two wants.
000245 **
000246 ** When a rollback occurs, the value of K is decreased. Hash table entries
000247 ** that correspond to frames greater than the new K value are removed
000248 ** from the hash table at this point.
000249 */
000250 #ifndef SQLITE_OMIT_WAL
000251
000252 #include "wal.h"
000253
000254 /*
000255 ** Trace output macros
000256 */
000257 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
000258 int sqlite3WalTrace = 0;
000259 # define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
000260 #else
000261 # define WALTRACE(X)
000262 #endif
000263
000264 /*
000265 ** The maximum (and only) versions of the wal and wal-index formats
000266 ** that may be interpreted by this version of SQLite.
000267 **
000268 ** If a client begins recovering a WAL file and finds that (a) the checksum
000269 ** values in the wal-header are correct and (b) the version field is not
000270 ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
000271 **
000272 ** Similarly, if a client successfully reads a wal-index header (i.e. the
000273 ** checksum test is successful) and finds that the version field is not
000274 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
000275 ** returns SQLITE_CANTOPEN.
000276 */
000277 #define WAL_MAX_VERSION 3007000
000278 #define WALINDEX_MAX_VERSION 3007000
000279
000280 /*
000281 ** Index numbers for various locking bytes. WAL_NREADER is the number
000282 ** of available reader locks and should be at least 3. The default
000283 ** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
000284 **
000285 ** Technically, the various VFSes are free to implement these locks however
000286 ** they see fit. However, compatibility is encouraged so that VFSes can
000287 ** interoperate. The standard implementation used on both unix and windows
000288 ** is for the index number to indicate a byte offset into the
000289 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
000290 ** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
000291 ** should be 120) is the location in the shm file for the first locking
000292 ** byte.
000293 */
000294 #define WAL_WRITE_LOCK 0
000295 #define WAL_ALL_BUT_WRITE 1
000296 #define WAL_CKPT_LOCK 1
000297 #define WAL_RECOVER_LOCK 2
000298 #define WAL_READ_LOCK(I) (3+(I))
000299 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
000300
000301
000302 /* Object declarations */
000303 typedef struct WalIndexHdr WalIndexHdr;
000304 typedef struct WalIterator WalIterator;
000305 typedef struct WalCkptInfo WalCkptInfo;
000306
000307
000308 /*
000309 ** The following object holds a copy of the wal-index header content.
000310 **
000311 ** The actual header in the wal-index consists of two copies of this
000312 ** object followed by one instance of the WalCkptInfo object.
000313 ** For all versions of SQLite through 3.10.0 and probably beyond,
000314 ** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
000315 ** the total header size is 136 bytes.
000316 **
000317 ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
000318 ** Or it can be 1 to represent a 65536-byte page. The latter case was
000319 ** added in 3.7.1 when support for 64K pages was added.
000320 */
000321 struct WalIndexHdr {
000322 u32 iVersion; /* Wal-index version */
000323 u32 unused; /* Unused (padding) field */
000324 u32 iChange; /* Counter incremented each transaction */
000325 u8 isInit; /* 1 when initialized */
000326 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
000327 u16 szPage; /* Database page size in bytes. 1==64K */
000328 u32 mxFrame; /* Index of last valid frame in the WAL */
000329 u32 nPage; /* Size of database in pages */
000330 u32 aFrameCksum[2]; /* Checksum of last frame in log */
000331 u32 aSalt[2]; /* Two salt values copied from WAL header */
000332 u32 aCksum[2]; /* Checksum over all prior fields */
000333 };
000334
000335 /*
000336 ** A copy of the following object occurs in the wal-index immediately
000337 ** following the second copy of the WalIndexHdr. This object stores
000338 ** information used by checkpoint.
000339 **
000340 ** nBackfill is the number of frames in the WAL that have been written
000341 ** back into the database. (We call the act of moving content from WAL to
000342 ** database "backfilling".) The nBackfill number is never greater than
000343 ** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
000344 ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
000345 ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
000346 ** mxFrame back to zero when the WAL is reset.
000347 **
000348 ** nBackfillAttempted is the largest value of nBackfill that a checkpoint
000349 ** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however
000350 ** the nBackfillAttempted is set before any backfilling is done and the
000351 ** nBackfill is only set after all backfilling completes. So if a checkpoint
000352 ** crashes, nBackfillAttempted might be larger than nBackfill. The
000353 ** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
000354 **
000355 ** The aLock[] field is a set of bytes used for locking. These bytes should
000356 ** never be read or written.
000357 **
000358 ** There is one entry in aReadMark[] for each reader lock. If a reader
000359 ** holds read-lock K, then the value in aReadMark[K] is no greater than
000360 ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
000361 ** for any aReadMark[] means that entry is unused. aReadMark[0] is
000362 ** a special case; its value is never used and it exists as a place-holder
000363 ** to avoid having to offset aReadMark[] indexes by one. Readers holding
000364 ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
000365 ** directly from the database.
000366 **
000367 ** The value of aReadMark[K] may only be changed by a thread that
000368 ** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
000369 ** aReadMark[K] cannot changed while there is a reader is using that mark
000370 ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
000371 **
000372 ** The checkpointer may only transfer frames from WAL to database where
000373 ** the frame numbers are less than or equal to every aReadMark[] that is
000374 ** in use (that is, every aReadMark[j] for which there is a corresponding
000375 ** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
000376 ** largest value and will increase an unused aReadMark[] to mxFrame if there
000377 ** is not already an aReadMark[] equal to mxFrame. The exception to the
000378 ** previous sentence is when nBackfill equals mxFrame (meaning that everything
000379 ** in the WAL has been backfilled into the database) then new readers
000380 ** will choose aReadMark[0] which has value 0 and hence such reader will
000381 ** get all their all content directly from the database file and ignore
000382 ** the WAL.
000383 **
000384 ** Writers normally append new frames to the end of the WAL. However,
000385 ** if nBackfill equals mxFrame (meaning that all WAL content has been
000386 ** written back into the database) and if no readers are using the WAL
000387 ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
000388 ** the writer will first "reset" the WAL back to the beginning and start
000389 ** writing new content beginning at frame 1.
000390 **
000391 ** We assume that 32-bit loads are atomic and so no locks are needed in
000392 ** order to read from any aReadMark[] entries.
000393 */
000394 struct WalCkptInfo {
000395 u32 nBackfill; /* Number of WAL frames backfilled into DB */
000396 u32 aReadMark[WAL_NREADER]; /* Reader marks */
000397 u8 aLock[SQLITE_SHM_NLOCK]; /* Reserved space for locks */
000398 u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */
000399 u32 notUsed0; /* Available for future enhancements */
000400 };
000401 #define READMARK_NOT_USED 0xffffffff
000402
000403 /*
000404 ** This is a schematic view of the complete 136-byte header of the
000405 ** wal-index file (also known as the -shm file):
000406 **
000407 ** +-----------------------------+
000408 ** 0: | iVersion | \
000409 ** +-----------------------------+ |
000410 ** 4: | (unused padding) | |
000411 ** +-----------------------------+ |
000412 ** 8: | iChange | |
000413 ** +-------+-------+-------------+ |
000414 ** 12: | bInit | bBig | szPage | |
000415 ** +-------+-------+-------------+ |
000416 ** 16: | mxFrame | | First copy of the
000417 ** +-----------------------------+ | WalIndexHdr object
000418 ** 20: | nPage | |
000419 ** +-----------------------------+ |
000420 ** 24: | aFrameCksum | |
000421 ** | | |
000422 ** +-----------------------------+ |
000423 ** 32: | aSalt | |
000424 ** | | |
000425 ** +-----------------------------+ |
000426 ** 40: | aCksum | |
000427 ** | | /
000428 ** +-----------------------------+
000429 ** 48: | iVersion | \
000430 ** +-----------------------------+ |
000431 ** 52: | (unused padding) | |
000432 ** +-----------------------------+ |
000433 ** 56: | iChange | |
000434 ** +-------+-------+-------------+ |
000435 ** 60: | bInit | bBig | szPage | |
000436 ** +-------+-------+-------------+ | Second copy of the
000437 ** 64: | mxFrame | | WalIndexHdr
000438 ** +-----------------------------+ |
000439 ** 68: | nPage | |
000440 ** +-----------------------------+ |
000441 ** 72: | aFrameCksum | |
000442 ** | | |
000443 ** +-----------------------------+ |
000444 ** 80: | aSalt | |
000445 ** | | |
000446 ** +-----------------------------+ |
000447 ** 88: | aCksum | |
000448 ** | | /
000449 ** +-----------------------------+
000450 ** 96: | nBackfill |
000451 ** +-----------------------------+
000452 ** 100: | 5 read marks |
000453 ** | |
000454 ** | |
000455 ** | |
000456 ** | |
000457 ** +-------+-------+------+------+
000458 ** 120: | Write | Ckpt | Rcvr | Rd0 | \
000459 ** +-------+-------+------+------+ ) 8 lock bytes
000460 ** | Read1 | Read2 | Rd3 | Rd4 | /
000461 ** +-------+-------+------+------+
000462 ** 128: | nBackfillAttempted |
000463 ** +-----------------------------+
000464 ** 132: | (unused padding) |
000465 ** +-----------------------------+
000466 */
000467
000468 /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
000469 ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
000470 ** only support mandatory file-locks, we do not read or write data
000471 ** from the region of the file on which locks are applied.
000472 */
000473 #define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
000474 #define WALINDEX_HDR_SIZE (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
000475
000476 /* Size of header before each frame in wal */
000477 #define WAL_FRAME_HDRSIZE 24
000478
000479 /* Size of write ahead log header, including checksum. */
000480 #define WAL_HDRSIZE 32
000481
000482 /* WAL magic value. Either this value, or the same value with the least
000483 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
000484 ** big-endian format in the first 4 bytes of a WAL file.
000485 **
000486 ** If the LSB is set, then the checksums for each frame within the WAL
000487 ** file are calculated by treating all data as an array of 32-bit
000488 ** big-endian words. Otherwise, they are calculated by interpreting
000489 ** all data as 32-bit little-endian words.
000490 */
000491 #define WAL_MAGIC 0x377f0682
000492
000493 /*
000494 ** Return the offset of frame iFrame in the write-ahead log file,
000495 ** assuming a database page size of szPage bytes. The offset returned
000496 ** is to the start of the write-ahead log frame-header.
000497 */
000498 #define walFrameOffset(iFrame, szPage) ( \
000499 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
000500 )
000501
000502 /*
000503 ** An open write-ahead log file is represented by an instance of the
000504 ** following object.
000505 */
000506 struct Wal {
000507 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
000508 sqlite3_file *pDbFd; /* File handle for the database file */
000509 sqlite3_file *pWalFd; /* File handle for WAL file */
000510 u32 iCallback; /* Value to pass to log callback (or 0) */
000511 i64 mxWalSize; /* Truncate WAL to this size upon reset */
000512 int nWiData; /* Size of array apWiData */
000513 int szFirstBlock; /* Size of first block written to WAL file */
000514 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
000515 u32 szPage; /* Database page size */
000516 i16 readLock; /* Which read lock is being held. -1 for none */
000517 u8 syncFlags; /* Flags to use to sync header writes */
000518 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
000519 u8 writeLock; /* True if in a write transaction */
000520 u8 ckptLock; /* True if holding a checkpoint lock */
000521 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
000522 u8 truncateOnCommit; /* True to truncate WAL file on commit */
000523 u8 syncHeader; /* Fsync the WAL header if true */
000524 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
000525 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
000526 WalIndexHdr hdr; /* Wal-index header for current transaction */
000527 u32 minFrame; /* Ignore wal frames before this one */
000528 u32 iReCksum; /* On commit, recalculate checksums from here */
000529 const char *zWalName; /* Name of WAL file */
000530 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
000531 #ifdef SQLITE_USE_SEH
000532 u32 lockMask; /* Mask of locks held */
000533 void *pFree; /* Pointer to sqlite3_free() if exception thrown */
000534 u32 *pWiValue; /* Value to write into apWiData[iWiPg] */
000535 int iWiPg; /* Write pWiValue into apWiData[iWiPg] */
000536 int iSysErrno; /* System error code following exception */
000537 #endif
000538 #ifdef SQLITE_DEBUG
000539 int nSehTry; /* Number of nested SEH_TRY{} blocks */
000540 u8 lockError; /* True if a locking error has occurred */
000541 #endif
000542 #ifdef SQLITE_ENABLE_SNAPSHOT
000543 WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */
000544 int bGetSnapshot; /* Transaction opened for sqlite3_get_snapshot() */
000545 #endif
000546 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
000547 sqlite3 *db;
000548 #endif
000549 };
000550
000551 /*
000552 ** Candidate values for Wal.exclusiveMode.
000553 */
000554 #define WAL_NORMAL_MODE 0
000555 #define WAL_EXCLUSIVE_MODE 1
000556 #define WAL_HEAPMEMORY_MODE 2
000557
000558 /*
000559 ** Possible values for WAL.readOnly
000560 */
000561 #define WAL_RDWR 0 /* Normal read/write connection */
000562 #define WAL_RDONLY 1 /* The WAL file is readonly */
000563 #define WAL_SHM_RDONLY 2 /* The SHM file is readonly */
000564
000565 /*
000566 ** Each page of the wal-index mapping contains a hash-table made up of
000567 ** an array of HASHTABLE_NSLOT elements of the following type.
000568 */
000569 typedef u16 ht_slot;
000570
000571 /*
000572 ** This structure is used to implement an iterator that loops through
000573 ** all frames in the WAL in database page order. Where two or more frames
000574 ** correspond to the same database page, the iterator visits only the
000575 ** frame most recently written to the WAL (in other words, the frame with
000576 ** the largest index).
000577 **
000578 ** The internals of this structure are only accessed by:
000579 **
000580 ** walIteratorInit() - Create a new iterator,
000581 ** walIteratorNext() - Step an iterator,
000582 ** walIteratorFree() - Free an iterator.
000583 **
000584 ** This functionality is used by the checkpoint code (see walCheckpoint()).
000585 */
000586 struct WalIterator {
000587 u32 iPrior; /* Last result returned from the iterator */
000588 int nSegment; /* Number of entries in aSegment[] */
000589 struct WalSegment {
000590 int iNext; /* Next slot in aIndex[] not yet returned */
000591 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
000592 u32 *aPgno; /* Array of page numbers. */
000593 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
000594 int iZero; /* Frame number associated with aPgno[0] */
000595 } aSegment[1]; /* One for every 32KB page in the wal-index */
000596 };
000597
000598 /*
000599 ** Define the parameters of the hash tables in the wal-index file. There
000600 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
000601 ** wal-index.
000602 **
000603 ** Changing any of these constants will alter the wal-index format and
000604 ** create incompatibilities.
000605 */
000606 #define HASHTABLE_NPAGE 4096 /* Must be power of 2 */
000607 #define HASHTABLE_HASH_1 383 /* Should be prime */
000608 #define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
000609
000610 /*
000611 ** The block of page numbers associated with the first hash-table in a
000612 ** wal-index is smaller than usual. This is so that there is a complete
000613 ** hash-table on each aligned 32KB page of the wal-index.
000614 */
000615 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
000616
000617 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
000618 #define WALINDEX_PGSZ ( \
000619 sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
000620 )
000621
000622 /*
000623 ** Structured Exception Handling (SEH) is a Windows-specific technique
000624 ** for catching exceptions raised while accessing memory-mapped files.
000625 **
000626 ** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and
000627 ** deal with system-level errors that arise during WAL -shm file processing.
000628 ** Without this compile-time option, any system-level faults that appear
000629 ** while accessing the memory-mapped -shm file will cause a process-wide
000630 ** signal to be deliver, which will more than likely cause the entire
000631 ** process to exit.
000632 */
000633 #ifdef SQLITE_USE_SEH
000634 #include <Windows.h>
000635
000636 /* Beginning of a block of code in which an exception might occur */
000637 # define SEH_TRY __try { \
000638 assert( walAssertLockmask(pWal) && pWal->nSehTry==0 ); \
000639 VVA_ONLY(pWal->nSehTry++);
000640
000641 /* The end of a block of code in which an exception might occur */
000642 # define SEH_EXCEPT(X) \
000643 VVA_ONLY(pWal->nSehTry--); \
000644 assert( pWal->nSehTry==0 ); \
000645 } __except( sehExceptionFilter(pWal, GetExceptionCode(), GetExceptionInformation() ) ){ X }
000646
000647 /* Simulate a memory-mapping fault in the -shm file for testing purposes */
000648 # define SEH_INJECT_FAULT sehInjectFault(pWal)
000649
000650 /*
000651 ** The second argument is the return value of GetExceptionCode() for the
000652 ** current exception. Return EXCEPTION_EXECUTE_HANDLER if the exception code
000653 ** indicates that the exception may have been caused by accessing the *-shm
000654 ** file mapping. Or EXCEPTION_CONTINUE_SEARCH otherwise.
000655 */
000656 static int sehExceptionFilter(Wal *pWal, int eCode, EXCEPTION_POINTERS *p){
000657 VVA_ONLY(pWal->nSehTry--);
000658 if( eCode==EXCEPTION_IN_PAGE_ERROR ){
000659 if( p && p->ExceptionRecord && p->ExceptionRecord->NumberParameters>=3 ){
000660 /* From MSDN: For this type of exception, the first element of the
000661 ** ExceptionInformation[] array is a read-write flag - 0 if the exception
000662 ** was thrown while reading, 1 if while writing. The second element is
000663 ** the virtual address being accessed. The "third array element specifies
000664 ** the underlying NTSTATUS code that resulted in the exception". */
000665 pWal->iSysErrno = (int)p->ExceptionRecord->ExceptionInformation[2];
000666 }
000667 return EXCEPTION_EXECUTE_HANDLER;
000668 }
000669 return EXCEPTION_CONTINUE_SEARCH;
000670 }
000671
000672 /*
000673 ** If one is configured, invoke the xTestCallback callback with 650 as
000674 ** the argument. If it returns true, throw the same exception that is
000675 ** thrown by the system if the *-shm file mapping is accessed after it
000676 ** has been invalidated.
000677 */
000678 static void sehInjectFault(Wal *pWal){
000679 int res;
000680 assert( pWal->nSehTry>0 );
000681
000682 res = sqlite3FaultSim(650);
000683 if( res!=0 ){
000684 ULONG_PTR aArg[3];
000685 aArg[0] = 0;
000686 aArg[1] = 0;
000687 aArg[2] = (ULONG_PTR)res;
000688 RaiseException(EXCEPTION_IN_PAGE_ERROR, 0, 3, (const ULONG_PTR*)aArg);
000689 }
000690 }
000691
000692 /*
000693 ** There are two ways to use this macro. To set a pointer to be freed
000694 ** if an exception is thrown:
000695 **
000696 ** SEH_FREE_ON_ERROR(0, pPtr);
000697 **
000698 ** and to cancel the same:
000699 **
000700 ** SEH_FREE_ON_ERROR(pPtr, 0);
000701 **
000702 ** In the first case, there must not already be a pointer registered to
000703 ** be freed. In the second case, pPtr must be the registered pointer.
000704 */
000705 #define SEH_FREE_ON_ERROR(X,Y) \
000706 assert( (X==0 || Y==0) && pWal->pFree==X ); pWal->pFree = Y
000707
000708 /*
000709 ** There are two ways to use this macro. To arrange for pWal->apWiData[iPg]
000710 ** to be set to pValue if an exception is thrown:
000711 **
000712 ** SEH_SET_ON_ERROR(iPg, pValue);
000713 **
000714 ** and to cancel the same:
000715 **
000716 ** SEH_SET_ON_ERROR(0, 0);
000717 */
000718 #define SEH_SET_ON_ERROR(X,Y) pWal->iWiPg = X; pWal->pWiValue = Y
000719
000720 #else
000721 # define SEH_TRY VVA_ONLY(pWal->nSehTry++);
000722 # define SEH_EXCEPT(X) VVA_ONLY(pWal->nSehTry--); assert( pWal->nSehTry==0 );
000723 # define SEH_INJECT_FAULT assert( pWal->nSehTry>0 );
000724 # define SEH_FREE_ON_ERROR(X,Y)
000725 # define SEH_SET_ON_ERROR(X,Y)
000726 #endif /* ifdef SQLITE_USE_SEH */
000727
000728
000729 /*
000730 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
000731 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
000732 ** numbered from zero.
000733 **
000734 ** If the wal-index is currently smaller the iPage pages then the size
000735 ** of the wal-index might be increased, but only if it is safe to do
000736 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
000737 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
000738 **
000739 ** Three possible result scenarios:
000740 **
000741 ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
000742 ** (2) rc>=SQLITE_ERROR and *ppPage==NULL
000743 ** (3) rc==SQLITE_OK and *ppPage==NULL // only if iPage==0
000744 **
000745 ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
000746 */
000747 static SQLITE_NOINLINE int walIndexPageRealloc(
000748 Wal *pWal, /* The WAL context */
000749 int iPage, /* The page we seek */
000750 volatile u32 **ppPage /* Write the page pointer here */
000751 ){
000752 int rc = SQLITE_OK;
000753
000754 /* Enlarge the pWal->apWiData[] array if required */
000755 if( pWal->nWiData<=iPage ){
000756 sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
000757 volatile u32 **apNew;
000758 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
000759 if( !apNew ){
000760 *ppPage = 0;
000761 return SQLITE_NOMEM_BKPT;
000762 }
000763 memset((void*)&apNew[pWal->nWiData], 0,
000764 sizeof(u32*)*(iPage+1-pWal->nWiData));
000765 pWal->apWiData = apNew;
000766 pWal->nWiData = iPage+1;
000767 }
000768
000769 /* Request a pointer to the required page from the VFS */
000770 assert( pWal->apWiData[iPage]==0 );
000771 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
000772 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
000773 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
000774 }else{
000775 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
000776 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
000777 );
000778 assert( pWal->apWiData[iPage]!=0
000779 || rc!=SQLITE_OK
000780 || (pWal->writeLock==0 && iPage==0) );
000781 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
000782 if( rc==SQLITE_OK ){
000783 if( iPage>0 && sqlite3FaultSim(600) ) rc = SQLITE_NOMEM;
000784 }else if( (rc&0xff)==SQLITE_READONLY ){
000785 pWal->readOnly |= WAL_SHM_RDONLY;
000786 if( rc==SQLITE_READONLY ){
000787 rc = SQLITE_OK;
000788 }
000789 }
000790 }
000791
000792 *ppPage = pWal->apWiData[iPage];
000793 assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
000794 return rc;
000795 }
000796 static int walIndexPage(
000797 Wal *pWal, /* The WAL context */
000798 int iPage, /* The page we seek */
000799 volatile u32 **ppPage /* Write the page pointer here */
000800 ){
000801 SEH_INJECT_FAULT;
000802 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
000803 return walIndexPageRealloc(pWal, iPage, ppPage);
000804 }
000805 return SQLITE_OK;
000806 }
000807
000808 /*
000809 ** Return a pointer to the WalCkptInfo structure in the wal-index.
000810 */
000811 static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
000812 assert( pWal->nWiData>0 && pWal->apWiData[0] );
000813 SEH_INJECT_FAULT;
000814 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
000815 }
000816
000817 /*
000818 ** Return a pointer to the WalIndexHdr structure in the wal-index.
000819 */
000820 static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
000821 assert( pWal->nWiData>0 && pWal->apWiData[0] );
000822 SEH_INJECT_FAULT;
000823 return (volatile WalIndexHdr*)pWal->apWiData[0];
000824 }
000825
000826 /*
000827 ** The argument to this macro must be of type u32. On a little-endian
000828 ** architecture, it returns the u32 value that results from interpreting
000829 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
000830 ** returns the value that would be produced by interpreting the 4 bytes
000831 ** of the input value as a little-endian integer.
000832 */
000833 #define BYTESWAP32(x) ( \
000834 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
000835 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
000836 )
000837
000838 /*
000839 ** Generate or extend an 8 byte checksum based on the data in
000840 ** array aByte[] and the initial values of aIn[0] and aIn[1] (or
000841 ** initial values of 0 and 0 if aIn==NULL).
000842 **
000843 ** The checksum is written back into aOut[] before returning.
000844 **
000845 ** nByte must be a positive multiple of 8.
000846 */
000847 static void walChecksumBytes(
000848 int nativeCksum, /* True for native byte-order, false for non-native */
000849 u8 *a, /* Content to be checksummed */
000850 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
000851 const u32 *aIn, /* Initial checksum value input */
000852 u32 *aOut /* OUT: Final checksum value output */
000853 ){
000854 u32 s1, s2;
000855 u32 *aData = (u32 *)a;
000856 u32 *aEnd = (u32 *)&a[nByte];
000857
000858 if( aIn ){
000859 s1 = aIn[0];
000860 s2 = aIn[1];
000861 }else{
000862 s1 = s2 = 0;
000863 }
000864
000865 assert( nByte>=8 );
000866 assert( (nByte&0x00000007)==0 );
000867 assert( nByte<=65536 );
000868 assert( nByte%4==0 );
000869
000870 if( !nativeCksum ){
000871 do {
000872 s1 += BYTESWAP32(aData[0]) + s2;
000873 s2 += BYTESWAP32(aData[1]) + s1;
000874 aData += 2;
000875 }while( aData<aEnd );
000876 }else if( nByte%64==0 ){
000877 do {
000878 s1 += *aData++ + s2;
000879 s2 += *aData++ + s1;
000880 s1 += *aData++ + s2;
000881 s2 += *aData++ + s1;
000882 s1 += *aData++ + s2;
000883 s2 += *aData++ + s1;
000884 s1 += *aData++ + s2;
000885 s2 += *aData++ + s1;
000886 s1 += *aData++ + s2;
000887 s2 += *aData++ + s1;
000888 s1 += *aData++ + s2;
000889 s2 += *aData++ + s1;
000890 s1 += *aData++ + s2;
000891 s2 += *aData++ + s1;
000892 s1 += *aData++ + s2;
000893 s2 += *aData++ + s1;
000894 }while( aData<aEnd );
000895 }else{
000896 do {
000897 s1 += *aData++ + s2;
000898 s2 += *aData++ + s1;
000899 }while( aData<aEnd );
000900 }
000901 assert( aData==aEnd );
000902
000903 aOut[0] = s1;
000904 aOut[1] = s2;
000905 }
000906
000907 /*
000908 ** If there is the possibility of concurrent access to the SHM file
000909 ** from multiple threads and/or processes, then do a memory barrier.
000910 */
000911 static void walShmBarrier(Wal *pWal){
000912 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
000913 sqlite3OsShmBarrier(pWal->pDbFd);
000914 }
000915 }
000916
000917 /*
000918 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
000919 ** definition as a hint that the function contains constructs that
000920 ** might give false-positive TSAN warnings.
000921 **
000922 ** See tag-20200519-1.
000923 */
000924 #if defined(__clang__) && !defined(SQLITE_NO_TSAN)
000925 # define SQLITE_NO_TSAN __attribute__((no_sanitize_thread))
000926 #else
000927 # define SQLITE_NO_TSAN
000928 #endif
000929
000930 /*
000931 ** Write the header information in pWal->hdr into the wal-index.
000932 **
000933 ** The checksum on pWal->hdr is updated before it is written.
000934 */
000935 static SQLITE_NO_TSAN void walIndexWriteHdr(Wal *pWal){
000936 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
000937 const int nCksum = offsetof(WalIndexHdr, aCksum);
000938
000939 assert( pWal->writeLock );
000940 pWal->hdr.isInit = 1;
000941 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
000942 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
000943 /* Possible TSAN false-positive. See tag-20200519-1 */
000944 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
000945 walShmBarrier(pWal);
000946 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
000947 }
000948
000949 /*
000950 ** This function encodes a single frame header and writes it to a buffer
000951 ** supplied by the caller. A frame-header is made up of a series of
000952 ** 4-byte big-endian integers, as follows:
000953 **
000954 ** 0: Page number.
000955 ** 4: For commit records, the size of the database image in pages
000956 ** after the commit. For all other records, zero.
000957 ** 8: Salt-1 (copied from the wal-header)
000958 ** 12: Salt-2 (copied from the wal-header)
000959 ** 16: Checksum-1.
000960 ** 20: Checksum-2.
000961 */
000962 static void walEncodeFrame(
000963 Wal *pWal, /* The write-ahead log */
000964 u32 iPage, /* Database page number for frame */
000965 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
000966 u8 *aData, /* Pointer to page data */
000967 u8 *aFrame /* OUT: Write encoded frame here */
000968 ){
000969 int nativeCksum; /* True for native byte-order checksums */
000970 u32 *aCksum = pWal->hdr.aFrameCksum;
000971 assert( WAL_FRAME_HDRSIZE==24 );
000972 sqlite3Put4byte(&aFrame[0], iPage);
000973 sqlite3Put4byte(&aFrame[4], nTruncate);
000974 if( pWal->iReCksum==0 ){
000975 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
000976
000977 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
000978 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
000979 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
000980
000981 sqlite3Put4byte(&aFrame[16], aCksum[0]);
000982 sqlite3Put4byte(&aFrame[20], aCksum[1]);
000983 }else{
000984 memset(&aFrame[8], 0, 16);
000985 }
000986 }
000987
000988 /*
000989 ** Check to see if the frame with header in aFrame[] and content
000990 ** in aData[] is valid. If it is a valid frame, fill *piPage and
000991 ** *pnTruncate and return true. Return if the frame is not valid.
000992 */
000993 static int walDecodeFrame(
000994 Wal *pWal, /* The write-ahead log */
000995 u32 *piPage, /* OUT: Database page number for frame */
000996 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
000997 u8 *aData, /* Pointer to page data (for checksum) */
000998 u8 *aFrame /* Frame data */
000999 ){
001000 int nativeCksum; /* True for native byte-order checksums */
001001 u32 *aCksum = pWal->hdr.aFrameCksum;
001002 u32 pgno; /* Page number of the frame */
001003 assert( WAL_FRAME_HDRSIZE==24 );
001004
001005 /* A frame is only valid if the salt values in the frame-header
001006 ** match the salt values in the wal-header.
001007 */
001008 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
001009 return 0;
001010 }
001011
001012 /* A frame is only valid if the page number is greater than zero.
001013 */
001014 pgno = sqlite3Get4byte(&aFrame[0]);
001015 if( pgno==0 ){
001016 return 0;
001017 }
001018
001019 /* A frame is only valid if a checksum of the WAL header,
001020 ** all prior frames, the first 16 bytes of this frame-header,
001021 ** and the frame-data matches the checksum in the last 8
001022 ** bytes of this frame-header.
001023 */
001024 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
001025 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
001026 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
001027 if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
001028 || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
001029 ){
001030 /* Checksum failed. */
001031 return 0;
001032 }
001033
001034 /* If we reach this point, the frame is valid. Return the page number
001035 ** and the new database size.
001036 */
001037 *piPage = pgno;
001038 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
001039 return 1;
001040 }
001041
001042
001043 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
001044 /*
001045 ** Names of locks. This routine is used to provide debugging output and is not
001046 ** a part of an ordinary build.
001047 */
001048 static const char *walLockName(int lockIdx){
001049 if( lockIdx==WAL_WRITE_LOCK ){
001050 return "WRITE-LOCK";
001051 }else if( lockIdx==WAL_CKPT_LOCK ){
001052 return "CKPT-LOCK";
001053 }else if( lockIdx==WAL_RECOVER_LOCK ){
001054 return "RECOVER-LOCK";
001055 }else{
001056 static char zName[15];
001057 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
001058 lockIdx-WAL_READ_LOCK(0));
001059 return zName;
001060 }
001061 }
001062 #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
001063
001064
001065 /*
001066 ** Set or release locks on the WAL. Locks are either shared or exclusive.
001067 ** A lock cannot be moved directly between shared and exclusive - it must go
001068 ** through the unlocked state first.
001069 **
001070 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
001071 */
001072 static int walLockShared(Wal *pWal, int lockIdx){
001073 int rc;
001074 if( pWal->exclusiveMode ) return SQLITE_OK;
001075 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
001076 SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
001077 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
001078 walLockName(lockIdx), rc ? "failed" : "ok"));
001079 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
001080 #ifdef SQLITE_USE_SEH
001081 if( rc==SQLITE_OK ) pWal->lockMask |= (1 << lockIdx);
001082 #endif
001083 return rc;
001084 }
001085 static void walUnlockShared(Wal *pWal, int lockIdx){
001086 if( pWal->exclusiveMode ) return;
001087 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
001088 SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
001089 #ifdef SQLITE_USE_SEH
001090 pWal->lockMask &= ~(1 << lockIdx);
001091 #endif
001092 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
001093 }
001094 static int walLockExclusive(Wal *pWal, int lockIdx, int n){
001095 int rc;
001096 if( pWal->exclusiveMode ) return SQLITE_OK;
001097 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
001098 SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
001099 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
001100 walLockName(lockIdx), n, rc ? "failed" : "ok"));
001101 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
001102 #ifdef SQLITE_USE_SEH
001103 if( rc==SQLITE_OK ){
001104 pWal->lockMask |= (((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
001105 }
001106 #endif
001107 return rc;
001108 }
001109 static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
001110 if( pWal->exclusiveMode ) return;
001111 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
001112 SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
001113 #ifdef SQLITE_USE_SEH
001114 pWal->lockMask &= ~(((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
001115 #endif
001116 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
001117 walLockName(lockIdx), n));
001118 }
001119
001120 /*
001121 ** Compute a hash on a page number. The resulting hash value must land
001122 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
001123 ** the hash to the next value in the event of a collision.
001124 */
001125 static int walHash(u32 iPage){
001126 assert( iPage>0 );
001127 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
001128 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
001129 }
001130 static int walNextHash(int iPriorHash){
001131 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
001132 }
001133
001134 /*
001135 ** An instance of the WalHashLoc object is used to describe the location
001136 ** of a page hash table in the wal-index. This becomes the return value
001137 ** from walHashGet().
001138 */
001139 typedef struct WalHashLoc WalHashLoc;
001140 struct WalHashLoc {
001141 volatile ht_slot *aHash; /* Start of the wal-index hash table */
001142 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
001143 u32 iZero; /* One less than the frame number of first indexed*/
001144 };
001145
001146 /*
001147 ** Return pointers to the hash table and page number array stored on
001148 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
001149 ** numbered starting from 0.
001150 **
001151 ** Set output variable pLoc->aHash to point to the start of the hash table
001152 ** in the wal-index file. Set pLoc->iZero to one less than the frame
001153 ** number of the first frame indexed by this hash table. If a
001154 ** slot in the hash table is set to N, it refers to frame number
001155 ** (pLoc->iZero+N) in the log.
001156 **
001157 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
001158 ** first frame indexed by the hash table, frame (pLoc->iZero).
001159 */
001160 static int walHashGet(
001161 Wal *pWal, /* WAL handle */
001162 int iHash, /* Find the iHash'th table */
001163 WalHashLoc *pLoc /* OUT: Hash table location */
001164 ){
001165 int rc; /* Return code */
001166
001167 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
001168 assert( rc==SQLITE_OK || iHash>0 );
001169
001170 if( pLoc->aPgno ){
001171 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
001172 if( iHash==0 ){
001173 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
001174 pLoc->iZero = 0;
001175 }else{
001176 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
001177 }
001178 }else if( NEVER(rc==SQLITE_OK) ){
001179 rc = SQLITE_ERROR;
001180 }
001181 return rc;
001182 }
001183
001184 /*
001185 ** Return the number of the wal-index page that contains the hash-table
001186 ** and page-number array that contain entries corresponding to WAL frame
001187 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
001188 ** are numbered starting from 0.
001189 */
001190 static int walFramePage(u32 iFrame){
001191 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
001192 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
001193 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
001194 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
001195 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
001196 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
001197 );
001198 assert( iHash>=0 );
001199 return iHash;
001200 }
001201
001202 /*
001203 ** Return the page number associated with frame iFrame in this WAL.
001204 */
001205 static u32 walFramePgno(Wal *pWal, u32 iFrame){
001206 int iHash = walFramePage(iFrame);
001207 SEH_INJECT_FAULT;
001208 if( iHash==0 ){
001209 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
001210 }
001211 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
001212 }
001213
001214 /*
001215 ** Remove entries from the hash table that point to WAL slots greater
001216 ** than pWal->hdr.mxFrame.
001217 **
001218 ** This function is called whenever pWal->hdr.mxFrame is decreased due
001219 ** to a rollback or savepoint.
001220 **
001221 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
001222 ** updated. Any later hash tables will be automatically cleared when
001223 ** pWal->hdr.mxFrame advances to the point where those hash tables are
001224 ** actually needed.
001225 */
001226 static void walCleanupHash(Wal *pWal){
001227 WalHashLoc sLoc; /* Hash table location */
001228 int iLimit = 0; /* Zero values greater than this */
001229 int nByte; /* Number of bytes to zero in aPgno[] */
001230 int i; /* Used to iterate through aHash[] */
001231
001232 assert( pWal->writeLock );
001233 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
001234 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
001235 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
001236
001237 if( pWal->hdr.mxFrame==0 ) return;
001238
001239 /* Obtain pointers to the hash-table and page-number array containing
001240 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
001241 ** that the page said hash-table and array reside on is already mapped.(1)
001242 */
001243 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
001244 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
001245 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
001246 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
001247
001248 /* Zero all hash-table entries that correspond to frame numbers greater
001249 ** than pWal->hdr.mxFrame.
001250 */
001251 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
001252 assert( iLimit>0 );
001253 for(i=0; i<HASHTABLE_NSLOT; i++){
001254 if( sLoc.aHash[i]>iLimit ){
001255 sLoc.aHash[i] = 0;
001256 }
001257 }
001258
001259 /* Zero the entries in the aPgno array that correspond to frames with
001260 ** frame numbers greater than pWal->hdr.mxFrame.
001261 */
001262 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
001263 assert( nByte>=0 );
001264 memset((void *)&sLoc.aPgno[iLimit], 0, nByte);
001265
001266 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
001267 /* Verify that the every entry in the mapping region is still reachable
001268 ** via the hash table even after the cleanup.
001269 */
001270 if( iLimit ){
001271 int j; /* Loop counter */
001272 int iKey; /* Hash key */
001273 for(j=0; j<iLimit; j++){
001274 for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
001275 if( sLoc.aHash[iKey]==j+1 ) break;
001276 }
001277 assert( sLoc.aHash[iKey]==j+1 );
001278 }
001279 }
001280 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
001281 }
001282
001283
001284 /*
001285 ** Set an entry in the wal-index that will map database page number
001286 ** pPage into WAL frame iFrame.
001287 */
001288 static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
001289 int rc; /* Return code */
001290 WalHashLoc sLoc; /* Wal-index hash table location */
001291
001292 rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);
001293
001294 /* Assuming the wal-index file was successfully mapped, populate the
001295 ** page number array and hash table entry.
001296 */
001297 if( rc==SQLITE_OK ){
001298 int iKey; /* Hash table key */
001299 int idx; /* Value to write to hash-table slot */
001300 int nCollide; /* Number of hash collisions */
001301
001302 idx = iFrame - sLoc.iZero;
001303 assert( idx <= HASHTABLE_NSLOT/2 + 1 );
001304
001305 /* If this is the first entry to be added to this hash-table, zero the
001306 ** entire hash table and aPgno[] array before proceeding.
001307 */
001308 if( idx==1 ){
001309 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
001310 assert( nByte>=0 );
001311 memset((void*)sLoc.aPgno, 0, nByte);
001312 }
001313
001314 /* If the entry in aPgno[] is already set, then the previous writer
001315 ** must have exited unexpectedly in the middle of a transaction (after
001316 ** writing one or more dirty pages to the WAL to free up memory).
001317 ** Remove the remnants of that writers uncommitted transaction from
001318 ** the hash-table before writing any new entries.
001319 */
001320 if( sLoc.aPgno[idx-1] ){
001321 walCleanupHash(pWal);
001322 assert( !sLoc.aPgno[idx-1] );
001323 }
001324
001325 /* Write the aPgno[] array entry and the hash-table slot. */
001326 nCollide = idx;
001327 for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
001328 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
001329 }
001330 sLoc.aPgno[idx-1] = iPage;
001331 AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx);
001332
001333 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
001334 /* Verify that the number of entries in the hash table exactly equals
001335 ** the number of entries in the mapping region.
001336 */
001337 {
001338 int i; /* Loop counter */
001339 int nEntry = 0; /* Number of entries in the hash table */
001340 for(i=0; i<HASHTABLE_NSLOT; i++){ if( sLoc.aHash[i] ) nEntry++; }
001341 assert( nEntry==idx );
001342 }
001343
001344 /* Verify that the every entry in the mapping region is reachable
001345 ** via the hash table. This turns out to be a really, really expensive
001346 ** thing to check, so only do this occasionally - not on every
001347 ** iteration.
001348 */
001349 if( (idx&0x3ff)==0 ){
001350 int i; /* Loop counter */
001351 for(i=0; i<idx; i++){
001352 for(iKey=walHash(sLoc.aPgno[i]);
001353 sLoc.aHash[iKey];
001354 iKey=walNextHash(iKey)){
001355 if( sLoc.aHash[iKey]==i+1 ) break;
001356 }
001357 assert( sLoc.aHash[iKey]==i+1 );
001358 }
001359 }
001360 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
001361 }
001362
001363 return rc;
001364 }
001365
001366
001367 /*
001368 ** Recover the wal-index by reading the write-ahead log file.
001369 **
001370 ** This routine first tries to establish an exclusive lock on the
001371 ** wal-index to prevent other threads/processes from doing anything
001372 ** with the WAL or wal-index while recovery is running. The
001373 ** WAL_RECOVER_LOCK is also held so that other threads will know
001374 ** that this thread is running recovery. If unable to establish
001375 ** the necessary locks, this routine returns SQLITE_BUSY.
001376 */
001377 static int walIndexRecover(Wal *pWal){
001378 int rc; /* Return Code */
001379 i64 nSize; /* Size of log file */
001380 u32 aFrameCksum[2] = {0, 0};
001381 int iLock; /* Lock offset to lock for checkpoint */
001382
001383 /* Obtain an exclusive lock on all byte in the locking range not already
001384 ** locked by the caller. The caller is guaranteed to have locked the
001385 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
001386 ** If successful, the same bytes that are locked here are unlocked before
001387 ** this function returns.
001388 */
001389 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
001390 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
001391 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
001392 assert( pWal->writeLock );
001393 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
001394 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
001395 if( rc ){
001396 return rc;
001397 }
001398
001399 WALTRACE(("WAL%p: recovery begin...\n", pWal));
001400
001401 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
001402
001403 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
001404 if( rc!=SQLITE_OK ){
001405 goto recovery_error;
001406 }
001407
001408 if( nSize>WAL_HDRSIZE ){
001409 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
001410 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
001411 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
001412 int szFrame; /* Number of bytes in buffer aFrame[] */
001413 u8 *aData; /* Pointer to data part of aFrame buffer */
001414 int szPage; /* Page size according to the log */
001415 u32 magic; /* Magic value read from WAL header */
001416 u32 version; /* Magic value read from WAL header */
001417 int isValid; /* True if this frame is valid */
001418 u32 iPg; /* Current 32KB wal-index page */
001419 u32 iLastFrame; /* Last frame in wal, based on nSize alone */
001420
001421 /* Read in the WAL header. */
001422 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
001423 if( rc!=SQLITE_OK ){
001424 goto recovery_error;
001425 }
001426
001427 /* If the database page size is not a power of two, or is greater than
001428 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
001429 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
001430 ** WAL file.
001431 */
001432 magic = sqlite3Get4byte(&aBuf[0]);
001433 szPage = sqlite3Get4byte(&aBuf[8]);
001434 if( (magic&0xFFFFFFFE)!=WAL_MAGIC
001435 || szPage&(szPage-1)
001436 || szPage>SQLITE_MAX_PAGE_SIZE
001437 || szPage<512
001438 ){
001439 goto finished;
001440 }
001441 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
001442 pWal->szPage = szPage;
001443 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
001444 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
001445
001446 /* Verify that the WAL header checksum is correct */
001447 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
001448 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
001449 );
001450 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
001451 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
001452 ){
001453 goto finished;
001454 }
001455
001456 /* Verify that the version number on the WAL format is one that
001457 ** are able to understand */
001458 version = sqlite3Get4byte(&aBuf[4]);
001459 if( version!=WAL_MAX_VERSION ){
001460 rc = SQLITE_CANTOPEN_BKPT;
001461 goto finished;
001462 }
001463
001464 /* Malloc a buffer to read frames into. */
001465 szFrame = szPage + WAL_FRAME_HDRSIZE;
001466 aFrame = (u8 *)sqlite3_malloc64(szFrame + WALINDEX_PGSZ);
001467 SEH_FREE_ON_ERROR(0, aFrame);
001468 if( !aFrame ){
001469 rc = SQLITE_NOMEM_BKPT;
001470 goto recovery_error;
001471 }
001472 aData = &aFrame[WAL_FRAME_HDRSIZE];
001473 aPrivate = (u32*)&aData[szPage];
001474
001475 /* Read all frames from the log file. */
001476 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
001477 for(iPg=0; iPg<=(u32)walFramePage(iLastFrame); iPg++){
001478 u32 *aShare;
001479 u32 iFrame; /* Index of last frame read */
001480 u32 iLast = MIN(iLastFrame, HASHTABLE_NPAGE_ONE+iPg*HASHTABLE_NPAGE);
001481 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
001482 u32 nHdr, nHdr32;
001483 rc = walIndexPage(pWal, iPg, (volatile u32**)&aShare);
001484 assert( aShare!=0 || rc!=SQLITE_OK );
001485 if( aShare==0 ) break;
001486 SEH_SET_ON_ERROR(iPg, aShare);
001487 pWal->apWiData[iPg] = aPrivate;
001488
001489 for(iFrame=iFirst; iFrame<=iLast; iFrame++){
001490 i64 iOffset = walFrameOffset(iFrame, szPage);
001491 u32 pgno; /* Database page number for frame */
001492 u32 nTruncate; /* dbsize field from frame header */
001493
001494 /* Read and decode the next log frame. */
001495 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
001496 if( rc!=SQLITE_OK ) break;
001497 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
001498 if( !isValid ) break;
001499 rc = walIndexAppend(pWal, iFrame, pgno);
001500 if( NEVER(rc!=SQLITE_OK) ) break;
001501
001502 /* If nTruncate is non-zero, this is a commit record. */
001503 if( nTruncate ){
001504 pWal->hdr.mxFrame = iFrame;
001505 pWal->hdr.nPage = nTruncate;
001506 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
001507 testcase( szPage<=32768 );
001508 testcase( szPage>=65536 );
001509 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
001510 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
001511 }
001512 }
001513 pWal->apWiData[iPg] = aShare;
001514 SEH_SET_ON_ERROR(0,0);
001515 nHdr = (iPg==0 ? WALINDEX_HDR_SIZE : 0);
001516 nHdr32 = nHdr / sizeof(u32);
001517 #ifndef SQLITE_SAFER_WALINDEX_RECOVERY
001518 /* Memcpy() should work fine here, on all reasonable implementations.
001519 ** Technically, memcpy() might change the destination to some
001520 ** intermediate value before setting to the final value, and that might
001521 ** cause a concurrent reader to malfunction. Memcpy() is allowed to
001522 ** do that, according to the spec, but no memcpy() implementation that
001523 ** we know of actually does that, which is why we say that memcpy()
001524 ** is safe for this. Memcpy() is certainly a lot faster.
001525 */
001526 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
001527 #else
001528 /* In the event that some platform is found for which memcpy()
001529 ** changes the destination to some intermediate value before
001530 ** setting the final value, this alternative copy routine is
001531 ** provided.
001532 */
001533 {
001534 int i;
001535 for(i=nHdr32; i<WALINDEX_PGSZ/sizeof(u32); i++){
001536 if( aShare[i]!=aPrivate[i] ){
001537 /* Atomic memory operations are not required here because if
001538 ** the value needs to be changed, that means it is not being
001539 ** accessed concurrently. */
001540 aShare[i] = aPrivate[i];
001541 }
001542 }
001543 }
001544 #endif
001545 SEH_INJECT_FAULT;
001546 if( iFrame<=iLast ) break;
001547 }
001548
001549 SEH_FREE_ON_ERROR(aFrame, 0);
001550 sqlite3_free(aFrame);
001551 }
001552
001553 finished:
001554 if( rc==SQLITE_OK ){
001555 volatile WalCkptInfo *pInfo;
001556 int i;
001557 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
001558 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
001559 walIndexWriteHdr(pWal);
001560
001561 /* Reset the checkpoint-header. This is safe because this thread is
001562 ** currently holding locks that exclude all other writers and
001563 ** checkpointers. Then set the values of read-mark slots 1 through N.
001564 */
001565 pInfo = walCkptInfo(pWal);
001566 pInfo->nBackfill = 0;
001567 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
001568 pInfo->aReadMark[0] = 0;
001569 for(i=1; i<WAL_NREADER; i++){
001570 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
001571 if( rc==SQLITE_OK ){
001572 if( i==1 && pWal->hdr.mxFrame ){
001573 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
001574 }else{
001575 pInfo->aReadMark[i] = READMARK_NOT_USED;
001576 }
001577 SEH_INJECT_FAULT;
001578 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
001579 }else if( rc!=SQLITE_BUSY ){
001580 goto recovery_error;
001581 }
001582 }
001583
001584 /* If more than one frame was recovered from the log file, report an
001585 ** event via sqlite3_log(). This is to help with identifying performance
001586 ** problems caused by applications routinely shutting down without
001587 ** checkpointing the log file.
001588 */
001589 if( pWal->hdr.nPage ){
001590 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
001591 "recovered %d frames from WAL file %s",
001592 pWal->hdr.mxFrame, pWal->zWalName
001593 );
001594 }
001595 }
001596
001597 recovery_error:
001598 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
001599 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
001600 return rc;
001601 }
001602
001603 /*
001604 ** Close an open wal-index.
001605 */
001606 static void walIndexClose(Wal *pWal, int isDelete){
001607 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
001608 int i;
001609 for(i=0; i<pWal->nWiData; i++){
001610 sqlite3_free((void *)pWal->apWiData[i]);
001611 pWal->apWiData[i] = 0;
001612 }
001613 }
001614 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
001615 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
001616 }
001617 }
001618
001619 /*
001620 ** Open a connection to the WAL file zWalName. The database file must
001621 ** already be opened on connection pDbFd. The buffer that zWalName points
001622 ** to must remain valid for the lifetime of the returned Wal* handle.
001623 **
001624 ** A SHARED lock should be held on the database file when this function
001625 ** is called. The purpose of this SHARED lock is to prevent any other
001626 ** client from unlinking the WAL or wal-index file. If another process
001627 ** were to do this just after this client opened one of these files, the
001628 ** system would be badly broken.
001629 **
001630 ** If the log file is successfully opened, SQLITE_OK is returned and
001631 ** *ppWal is set to point to a new WAL handle. If an error occurs,
001632 ** an SQLite error code is returned and *ppWal is left unmodified.
001633 */
001634 int sqlite3WalOpen(
001635 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
001636 sqlite3_file *pDbFd, /* The open database file */
001637 const char *zWalName, /* Name of the WAL file */
001638 int bNoShm, /* True to run in heap-memory mode */
001639 i64 mxWalSize, /* Truncate WAL to this size on reset */
001640 Wal **ppWal /* OUT: Allocated Wal handle */
001641 ){
001642 int rc; /* Return Code */
001643 Wal *pRet; /* Object to allocate and return */
001644 int flags; /* Flags passed to OsOpen() */
001645
001646 assert( zWalName && zWalName[0] );
001647 assert( pDbFd );
001648
001649 /* Verify the values of various constants. Any changes to the values
001650 ** of these constants would result in an incompatible on-disk format
001651 ** for the -shm file. Any change that causes one of these asserts to
001652 ** fail is a backward compatibility problem, even if the change otherwise
001653 ** works.
001654 **
001655 ** This table also serves as a helpful cross-reference when trying to
001656 ** interpret hex dumps of the -shm file.
001657 */
001658 assert( 48 == sizeof(WalIndexHdr) );
001659 assert( 40 == sizeof(WalCkptInfo) );
001660 assert( 120 == WALINDEX_LOCK_OFFSET );
001661 assert( 136 == WALINDEX_HDR_SIZE );
001662 assert( 4096 == HASHTABLE_NPAGE );
001663 assert( 4062 == HASHTABLE_NPAGE_ONE );
001664 assert( 8192 == HASHTABLE_NSLOT );
001665 assert( 383 == HASHTABLE_HASH_1 );
001666 assert( 32768 == WALINDEX_PGSZ );
001667 assert( 8 == SQLITE_SHM_NLOCK );
001668 assert( 5 == WAL_NREADER );
001669 assert( 24 == WAL_FRAME_HDRSIZE );
001670 assert( 32 == WAL_HDRSIZE );
001671 assert( 120 == WALINDEX_LOCK_OFFSET + WAL_WRITE_LOCK );
001672 assert( 121 == WALINDEX_LOCK_OFFSET + WAL_CKPT_LOCK );
001673 assert( 122 == WALINDEX_LOCK_OFFSET + WAL_RECOVER_LOCK );
001674 assert( 123 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(0) );
001675 assert( 124 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(1) );
001676 assert( 125 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(2) );
001677 assert( 126 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(3) );
001678 assert( 127 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(4) );
001679
001680 /* In the amalgamation, the os_unix.c and os_win.c source files come before
001681 ** this source file. Verify that the #defines of the locking byte offsets
001682 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
001683 ** For that matter, if the lock offset ever changes from its initial design
001684 ** value of 120, we need to know that so there is an assert() to check it.
001685 */
001686 #ifdef WIN_SHM_BASE
001687 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
001688 #endif
001689 #ifdef UNIX_SHM_BASE
001690 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
001691 #endif
001692
001693
001694 /* Allocate an instance of struct Wal to return. */
001695 *ppWal = 0;
001696 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
001697 if( !pRet ){
001698 return SQLITE_NOMEM_BKPT;
001699 }
001700
001701 pRet->pVfs = pVfs;
001702 pRet->pWalFd = (sqlite3_file *)&pRet[1];
001703 pRet->pDbFd = pDbFd;
001704 pRet->readLock = -1;
001705 pRet->mxWalSize = mxWalSize;
001706 pRet->zWalName = zWalName;
001707 pRet->syncHeader = 1;
001708 pRet->padToSectorBoundary = 1;
001709 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
001710
001711 /* Open file handle on the write-ahead log file. */
001712 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
001713 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
001714 if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
001715 pRet->readOnly = WAL_RDONLY;
001716 }
001717
001718 if( rc!=SQLITE_OK ){
001719 walIndexClose(pRet, 0);
001720 sqlite3OsClose(pRet->pWalFd);
001721 sqlite3_free(pRet);
001722 }else{
001723 int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
001724 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
001725 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
001726 pRet->padToSectorBoundary = 0;
001727 }
001728 *ppWal = pRet;
001729 WALTRACE(("WAL%d: opened\n", pRet));
001730 }
001731 return rc;
001732 }
001733
001734 /*
001735 ** Change the size to which the WAL file is truncated on each reset.
001736 */
001737 void sqlite3WalLimit(Wal *pWal, i64 iLimit){
001738 if( pWal ) pWal->mxWalSize = iLimit;
001739 }
001740
001741 /*
001742 ** Find the smallest page number out of all pages held in the WAL that
001743 ** has not been returned by any prior invocation of this method on the
001744 ** same WalIterator object. Write into *piFrame the frame index where
001745 ** that page was last written into the WAL. Write into *piPage the page
001746 ** number.
001747 **
001748 ** Return 0 on success. If there are no pages in the WAL with a page
001749 ** number larger than *piPage, then return 1.
001750 */
001751 static int walIteratorNext(
001752 WalIterator *p, /* Iterator */
001753 u32 *piPage, /* OUT: The page number of the next page */
001754 u32 *piFrame /* OUT: Wal frame index of next page */
001755 ){
001756 u32 iMin; /* Result pgno must be greater than iMin */
001757 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
001758 int i; /* For looping through segments */
001759
001760 iMin = p->iPrior;
001761 assert( iMin<0xffffffff );
001762 for(i=p->nSegment-1; i>=0; i--){
001763 struct WalSegment *pSegment = &p->aSegment[i];
001764 while( pSegment->iNext<pSegment->nEntry ){
001765 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
001766 if( iPg>iMin ){
001767 if( iPg<iRet ){
001768 iRet = iPg;
001769 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
001770 }
001771 break;
001772 }
001773 pSegment->iNext++;
001774 }
001775 }
001776
001777 *piPage = p->iPrior = iRet;
001778 return (iRet==0xFFFFFFFF);
001779 }
001780
001781 /*
001782 ** This function merges two sorted lists into a single sorted list.
001783 **
001784 ** aLeft[] and aRight[] are arrays of indices. The sort key is
001785 ** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
001786 ** is guaranteed for all J<K:
001787 **
001788 ** aContent[aLeft[J]] < aContent[aLeft[K]]
001789 ** aContent[aRight[J]] < aContent[aRight[K]]
001790 **
001791 ** This routine overwrites aRight[] with a new (probably longer) sequence
001792 ** of indices such that the aRight[] contains every index that appears in
001793 ** either aLeft[] or the old aRight[] and such that the second condition
001794 ** above is still met.
001795 **
001796 ** The aContent[aLeft[X]] values will be unique for all X. And the
001797 ** aContent[aRight[X]] values will be unique too. But there might be
001798 ** one or more combinations of X and Y such that
001799 **
001800 ** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
001801 **
001802 ** When that happens, omit the aLeft[X] and use the aRight[Y] index.
001803 */
001804 static void walMerge(
001805 const u32 *aContent, /* Pages in wal - keys for the sort */
001806 ht_slot *aLeft, /* IN: Left hand input list */
001807 int nLeft, /* IN: Elements in array *paLeft */
001808 ht_slot **paRight, /* IN/OUT: Right hand input list */
001809 int *pnRight, /* IN/OUT: Elements in *paRight */
001810 ht_slot *aTmp /* Temporary buffer */
001811 ){
001812 int iLeft = 0; /* Current index in aLeft */
001813 int iRight = 0; /* Current index in aRight */
001814 int iOut = 0; /* Current index in output buffer */
001815 int nRight = *pnRight;
001816 ht_slot *aRight = *paRight;
001817
001818 assert( nLeft>0 && nRight>0 );
001819 while( iRight<nRight || iLeft<nLeft ){
001820 ht_slot logpage;
001821 Pgno dbpage;
001822
001823 if( (iLeft<nLeft)
001824 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
001825 ){
001826 logpage = aLeft[iLeft++];
001827 }else{
001828 logpage = aRight[iRight++];
001829 }
001830 dbpage = aContent[logpage];
001831
001832 aTmp[iOut++] = logpage;
001833 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
001834
001835 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
001836 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
001837 }
001838
001839 *paRight = aLeft;
001840 *pnRight = iOut;
001841 memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
001842 }
001843
001844 /*
001845 ** Sort the elements in list aList using aContent[] as the sort key.
001846 ** Remove elements with duplicate keys, preferring to keep the
001847 ** larger aList[] values.
001848 **
001849 ** The aList[] entries are indices into aContent[]. The values in
001850 ** aList[] are to be sorted so that for all J<K:
001851 **
001852 ** aContent[aList[J]] < aContent[aList[K]]
001853 **
001854 ** For any X and Y such that
001855 **
001856 ** aContent[aList[X]] == aContent[aList[Y]]
001857 **
001858 ** Keep the larger of the two values aList[X] and aList[Y] and discard
001859 ** the smaller.
001860 */
001861 static void walMergesort(
001862 const u32 *aContent, /* Pages in wal */
001863 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
001864 ht_slot *aList, /* IN/OUT: List to sort */
001865 int *pnList /* IN/OUT: Number of elements in aList[] */
001866 ){
001867 struct Sublist {
001868 int nList; /* Number of elements in aList */
001869 ht_slot *aList; /* Pointer to sub-list content */
001870 };
001871
001872 const int nList = *pnList; /* Size of input list */
001873 int nMerge = 0; /* Number of elements in list aMerge */
001874 ht_slot *aMerge = 0; /* List to be merged */
001875 int iList; /* Index into input list */
001876 u32 iSub = 0; /* Index into aSub array */
001877 struct Sublist aSub[13]; /* Array of sub-lists */
001878
001879 memset(aSub, 0, sizeof(aSub));
001880 assert( nList<=HASHTABLE_NPAGE && nList>0 );
001881 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
001882
001883 for(iList=0; iList<nList; iList++){
001884 nMerge = 1;
001885 aMerge = &aList[iList];
001886 for(iSub=0; iList & (1<<iSub); iSub++){
001887 struct Sublist *p;
001888 assert( iSub<ArraySize(aSub) );
001889 p = &aSub[iSub];
001890 assert( p->aList && p->nList<=(1<<iSub) );
001891 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
001892 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
001893 }
001894 aSub[iSub].aList = aMerge;
001895 aSub[iSub].nList = nMerge;
001896 }
001897
001898 for(iSub++; iSub<ArraySize(aSub); iSub++){
001899 if( nList & (1<<iSub) ){
001900 struct Sublist *p;
001901 assert( iSub<ArraySize(aSub) );
001902 p = &aSub[iSub];
001903 assert( p->nList<=(1<<iSub) );
001904 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
001905 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
001906 }
001907 }
001908 assert( aMerge==aList );
001909 *pnList = nMerge;
001910
001911 #ifdef SQLITE_DEBUG
001912 {
001913 int i;
001914 for(i=1; i<*pnList; i++){
001915 assert( aContent[aList[i]] > aContent[aList[i-1]] );
001916 }
001917 }
001918 #endif
001919 }
001920
001921 /*
001922 ** Free an iterator allocated by walIteratorInit().
001923 */
001924 static void walIteratorFree(WalIterator *p){
001925 sqlite3_free(p);
001926 }
001927
001928 /*
001929 ** Construct a WalInterator object that can be used to loop over all
001930 ** pages in the WAL following frame nBackfill in ascending order. Frames
001931 ** nBackfill or earlier may be included - excluding them is an optimization
001932 ** only. The caller must hold the checkpoint lock.
001933 **
001934 ** On success, make *pp point to the newly allocated WalInterator object
001935 ** return SQLITE_OK. Otherwise, return an error code. If this routine
001936 ** returns an error, the value of *pp is undefined.
001937 **
001938 ** The calling routine should invoke walIteratorFree() to destroy the
001939 ** WalIterator object when it has finished with it.
001940 */
001941 static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
001942 WalIterator *p; /* Return value */
001943 int nSegment; /* Number of segments to merge */
001944 u32 iLast; /* Last frame in log */
001945 sqlite3_int64 nByte; /* Number of bytes to allocate */
001946 int i; /* Iterator variable */
001947 ht_slot *aTmp; /* Temp space used by merge-sort */
001948 int rc = SQLITE_OK; /* Return Code */
001949
001950 /* This routine only runs while holding the checkpoint lock. And
001951 ** it only runs if there is actually content in the log (mxFrame>0).
001952 */
001953 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
001954 iLast = pWal->hdr.mxFrame;
001955
001956 /* Allocate space for the WalIterator object. */
001957 nSegment = walFramePage(iLast) + 1;
001958 nByte = sizeof(WalIterator)
001959 + (nSegment-1)*sizeof(struct WalSegment)
001960 + iLast*sizeof(ht_slot);
001961 p = (WalIterator *)sqlite3_malloc64(nByte
001962 + sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
001963 );
001964 if( !p ){
001965 return SQLITE_NOMEM_BKPT;
001966 }
001967 memset(p, 0, nByte);
001968 p->nSegment = nSegment;
001969 aTmp = (ht_slot*)&(((u8*)p)[nByte]);
001970 SEH_FREE_ON_ERROR(0, p);
001971 for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
001972 WalHashLoc sLoc;
001973
001974 rc = walHashGet(pWal, i, &sLoc);
001975 if( rc==SQLITE_OK ){
001976 int j; /* Counter variable */
001977 int nEntry; /* Number of entries in this segment */
001978 ht_slot *aIndex; /* Sorted index for this segment */
001979
001980 if( (i+1)==nSegment ){
001981 nEntry = (int)(iLast - sLoc.iZero);
001982 }else{
001983 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
001984 }
001985 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
001986 sLoc.iZero++;
001987
001988 for(j=0; j<nEntry; j++){
001989 aIndex[j] = (ht_slot)j;
001990 }
001991 walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
001992 p->aSegment[i].iZero = sLoc.iZero;
001993 p->aSegment[i].nEntry = nEntry;
001994 p->aSegment[i].aIndex = aIndex;
001995 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
001996 }
001997 }
001998 if( rc!=SQLITE_OK ){
001999 SEH_FREE_ON_ERROR(p, 0);
002000 walIteratorFree(p);
002001 p = 0;
002002 }
002003 *pp = p;
002004 return rc;
002005 }
002006
002007 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
002008
002009
002010 /*
002011 ** Attempt to enable blocking locks that block for nMs ms. Return 1 if
002012 ** blocking locks are successfully enabled, or 0 otherwise.
002013 */
002014 static int walEnableBlockingMs(Wal *pWal, int nMs){
002015 int rc = sqlite3OsFileControl(
002016 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&nMs
002017 );
002018 return (rc==SQLITE_OK);
002019 }
002020
002021 /*
002022 ** Attempt to enable blocking locks. Blocking locks are enabled only if (a)
002023 ** they are supported by the VFS, and (b) the database handle is configured
002024 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
002025 ** or 0 otherwise.
002026 */
002027 static int walEnableBlocking(Wal *pWal){
002028 int res = 0;
002029 if( pWal->db ){
002030 int tmout = pWal->db->busyTimeout;
002031 if( tmout ){
002032 res = walEnableBlockingMs(pWal, tmout);
002033 }
002034 }
002035 return res;
002036 }
002037
002038 /*
002039 ** Disable blocking locks.
002040 */
002041 static void walDisableBlocking(Wal *pWal){
002042 int tmout = 0;
002043 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
002044 }
002045
002046 /*
002047 ** If parameter bLock is true, attempt to enable blocking locks, take
002048 ** the WRITER lock, and then disable blocking locks. If blocking locks
002049 ** cannot be enabled, no attempt to obtain the WRITER lock is made. Return
002050 ** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not
002051 ** an error if blocking locks can not be enabled.
002052 **
002053 ** If the bLock parameter is false and the WRITER lock is held, release it.
002054 */
002055 int sqlite3WalWriteLock(Wal *pWal, int bLock){
002056 int rc = SQLITE_OK;
002057 assert( pWal->readLock<0 || bLock==0 );
002058 if( bLock ){
002059 assert( pWal->db );
002060 if( walEnableBlocking(pWal) ){
002061 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
002062 if( rc==SQLITE_OK ){
002063 pWal->writeLock = 1;
002064 }
002065 walDisableBlocking(pWal);
002066 }
002067 }else if( pWal->writeLock ){
002068 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
002069 pWal->writeLock = 0;
002070 }
002071 return rc;
002072 }
002073
002074 /*
002075 ** Set the database handle used to determine if blocking locks are required.
002076 */
002077 void sqlite3WalDb(Wal *pWal, sqlite3 *db){
002078 pWal->db = db;
002079 }
002080
002081 #else
002082 # define walEnableBlocking(x) 0
002083 # define walDisableBlocking(x)
002084 # define walEnableBlockingMs(pWal, ms) 0
002085 # define sqlite3WalDb(pWal, db)
002086 #endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */
002087
002088
002089 /*
002090 ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
002091 ** n. If the attempt fails and parameter xBusy is not NULL, then it is a
002092 ** busy-handler function. Invoke it and retry the lock until either the
002093 ** lock is successfully obtained or the busy-handler returns 0.
002094 */
002095 static int walBusyLock(
002096 Wal *pWal, /* WAL connection */
002097 int (*xBusy)(void*), /* Function to call when busy */
002098 void *pBusyArg, /* Context argument for xBusyHandler */
002099 int lockIdx, /* Offset of first byte to lock */
002100 int n /* Number of bytes to lock */
002101 ){
002102 int rc;
002103 do {
002104 rc = walLockExclusive(pWal, lockIdx, n);
002105 }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
002106 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
002107 if( rc==SQLITE_BUSY_TIMEOUT ){
002108 walDisableBlocking(pWal);
002109 rc = SQLITE_BUSY;
002110 }
002111 #endif
002112 return rc;
002113 }
002114
002115 /*
002116 ** The cache of the wal-index header must be valid to call this function.
002117 ** Return the page-size in bytes used by the database.
002118 */
002119 static int walPagesize(Wal *pWal){
002120 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
002121 }
002122
002123 /*
002124 ** The following is guaranteed when this function is called:
002125 **
002126 ** a) the WRITER lock is held,
002127 ** b) the entire log file has been checkpointed, and
002128 ** c) any existing readers are reading exclusively from the database
002129 ** file - there are no readers that may attempt to read a frame from
002130 ** the log file.
002131 **
002132 ** This function updates the shared-memory structures so that the next
002133 ** client to write to the database (which may be this one) does so by
002134 ** writing frames into the start of the log file.
002135 **
002136 ** The value of parameter salt1 is used as the aSalt[1] value in the
002137 ** new wal-index header. It should be passed a pseudo-random value (i.e.
002138 ** one obtained from sqlite3_randomness()).
002139 */
002140 static void walRestartHdr(Wal *pWal, u32 salt1){
002141 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
002142 int i; /* Loop counter */
002143 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
002144 pWal->nCkpt++;
002145 pWal->hdr.mxFrame = 0;
002146 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
002147 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
002148 walIndexWriteHdr(pWal);
002149 AtomicStore(&pInfo->nBackfill, 0);
002150 pInfo->nBackfillAttempted = 0;
002151 pInfo->aReadMark[1] = 0;
002152 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
002153 assert( pInfo->aReadMark[0]==0 );
002154 }
002155
002156 /*
002157 ** Copy as much content as we can from the WAL back into the database file
002158 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
002159 **
002160 ** The amount of information copies from WAL to database might be limited
002161 ** by active readers. This routine will never overwrite a database page
002162 ** that a concurrent reader might be using.
002163 **
002164 ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
002165 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
002166 ** checkpoints are always run by a background thread or background
002167 ** process, foreground threads will never block on a lengthy fsync call.
002168 **
002169 ** Fsync is called on the WAL before writing content out of the WAL and
002170 ** into the database. This ensures that if the new content is persistent
002171 ** in the WAL and can be recovered following a power-loss or hard reset.
002172 **
002173 ** Fsync is also called on the database file if (and only if) the entire
002174 ** WAL content is copied into the database file. This second fsync makes
002175 ** it safe to delete the WAL since the new content will persist in the
002176 ** database file.
002177 **
002178 ** This routine uses and updates the nBackfill field of the wal-index header.
002179 ** This is the only routine that will increase the value of nBackfill.
002180 ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
002181 ** its value.)
002182 **
002183 ** The caller must be holding sufficient locks to ensure that no other
002184 ** checkpoint is running (in any other thread or process) at the same
002185 ** time.
002186 */
002187 static int walCheckpoint(
002188 Wal *pWal, /* Wal connection */
002189 sqlite3 *db, /* Check for interrupts on this handle */
002190 int eMode, /* One of PASSIVE, FULL or RESTART */
002191 int (*xBusy)(void*), /* Function to call when busy */
002192 void *pBusyArg, /* Context argument for xBusyHandler */
002193 int sync_flags, /* Flags for OsSync() (or 0) */
002194 u8 *zBuf /* Temporary buffer to use */
002195 ){
002196 int rc = SQLITE_OK; /* Return code */
002197 int szPage; /* Database page-size */
002198 WalIterator *pIter = 0; /* Wal iterator context */
002199 u32 iDbpage = 0; /* Next database page to write */
002200 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
002201 u32 mxSafeFrame; /* Max frame that can be backfilled */
002202 u32 mxPage; /* Max database page to write */
002203 int i; /* Loop counter */
002204 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
002205
002206 szPage = walPagesize(pWal);
002207 testcase( szPage<=32768 );
002208 testcase( szPage>=65536 );
002209 pInfo = walCkptInfo(pWal);
002210 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
002211
002212 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
002213 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
002214 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
002215
002216 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
002217 ** safe to write into the database. Frames beyond mxSafeFrame might
002218 ** overwrite database pages that are in use by active readers and thus
002219 ** cannot be backfilled from the WAL.
002220 */
002221 mxSafeFrame = pWal->hdr.mxFrame;
002222 mxPage = pWal->hdr.nPage;
002223 for(i=1; i<WAL_NREADER; i++){
002224 u32 y = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
002225 if( mxSafeFrame>y ){
002226 assert( y<=pWal->hdr.mxFrame );
002227 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
002228 if( rc==SQLITE_OK ){
002229 u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
002230 AtomicStore(pInfo->aReadMark+i, iMark); SEH_INJECT_FAULT;
002231 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
002232 }else if( rc==SQLITE_BUSY ){
002233 mxSafeFrame = y;
002234 xBusy = 0;
002235 }else{
002236 goto walcheckpoint_out;
002237 }
002238 }
002239 }
002240
002241 /* Allocate the iterator */
002242 if( pInfo->nBackfill<mxSafeFrame ){
002243 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
002244 assert( rc==SQLITE_OK || pIter==0 );
002245 }
002246
002247 if( pIter
002248 && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0),1))==SQLITE_OK
002249 ){
002250 u32 nBackfill = pInfo->nBackfill;
002251 pInfo->nBackfillAttempted = mxSafeFrame; SEH_INJECT_FAULT;
002252
002253 /* Sync the WAL to disk */
002254 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
002255
002256 /* If the database may grow as a result of this checkpoint, hint
002257 ** about the eventual size of the db file to the VFS layer.
002258 */
002259 if( rc==SQLITE_OK ){
002260 i64 nReq = ((i64)mxPage * szPage);
002261 i64 nSize; /* Current size of database file */
002262 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
002263 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
002264 if( rc==SQLITE_OK && nSize<nReq ){
002265 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
002266 /* If the size of the final database is larger than the current
002267 ** database plus the amount of data in the wal file, plus the
002268 ** maximum size of the pending-byte page (65536 bytes), then
002269 ** must be corruption somewhere. */
002270 rc = SQLITE_CORRUPT_BKPT;
002271 }else{
002272 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
002273 }
002274 }
002275
002276 }
002277
002278 /* Iterate through the contents of the WAL, copying data to the db file */
002279 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
002280 i64 iOffset;
002281 assert( walFramePgno(pWal, iFrame)==iDbpage );
002282 SEH_INJECT_FAULT;
002283 if( AtomicLoad(&db->u1.isInterrupted) ){
002284 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
002285 break;
002286 }
002287 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
002288 continue;
002289 }
002290 iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
002291 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
002292 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
002293 if( rc!=SQLITE_OK ) break;
002294 iOffset = (iDbpage-1)*(i64)szPage;
002295 testcase( IS_BIG_INT(iOffset) );
002296 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
002297 if( rc!=SQLITE_OK ) break;
002298 }
002299 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
002300
002301 /* If work was actually accomplished... */
002302 if( rc==SQLITE_OK ){
002303 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
002304 i64 szDb = pWal->hdr.nPage*(i64)szPage;
002305 testcase( IS_BIG_INT(szDb) );
002306 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
002307 if( rc==SQLITE_OK ){
002308 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
002309 }
002310 }
002311 if( rc==SQLITE_OK ){
002312 AtomicStore(&pInfo->nBackfill, mxSafeFrame); SEH_INJECT_FAULT;
002313 }
002314 }
002315
002316 /* Release the reader lock held while backfilling */
002317 walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
002318 }
002319
002320 if( rc==SQLITE_BUSY ){
002321 /* Reset the return code so as not to report a checkpoint failure
002322 ** just because there are active readers. */
002323 rc = SQLITE_OK;
002324 }
002325 }
002326
002327 /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
002328 ** entire wal file has been copied into the database file, then block
002329 ** until all readers have finished using the wal file. This ensures that
002330 ** the next process to write to the database restarts the wal file.
002331 */
002332 if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
002333 assert( pWal->writeLock );
002334 SEH_INJECT_FAULT;
002335 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
002336 rc = SQLITE_BUSY;
002337 }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){
002338 u32 salt1;
002339 sqlite3_randomness(4, &salt1);
002340 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
002341 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
002342 if( rc==SQLITE_OK ){
002343 if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){
002344 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
002345 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
002346 ** truncates the log file to zero bytes just prior to a
002347 ** successful return.
002348 **
002349 ** In theory, it might be safe to do this without updating the
002350 ** wal-index header in shared memory, as all subsequent reader or
002351 ** writer clients should see that the entire log file has been
002352 ** checkpointed and behave accordingly. This seems unsafe though,
002353 ** as it would leave the system in a state where the contents of
002354 ** the wal-index header do not match the contents of the
002355 ** file-system. To avoid this, update the wal-index header to
002356 ** indicate that the log file contains zero valid frames. */
002357 walRestartHdr(pWal, salt1);
002358 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
002359 }
002360 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
002361 }
002362 }
002363 }
002364
002365 walcheckpoint_out:
002366 SEH_FREE_ON_ERROR(pIter, 0);
002367 walIteratorFree(pIter);
002368 return rc;
002369 }
002370
002371 /*
002372 ** If the WAL file is currently larger than nMax bytes in size, truncate
002373 ** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
002374 */
002375 static void walLimitSize(Wal *pWal, i64 nMax){
002376 i64 sz;
002377 int rx;
002378 sqlite3BeginBenignMalloc();
002379 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
002380 if( rx==SQLITE_OK && (sz > nMax ) ){
002381 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
002382 }
002383 sqlite3EndBenignMalloc();
002384 if( rx ){
002385 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
002386 }
002387 }
002388
002389 #ifdef SQLITE_USE_SEH
002390 /*
002391 ** This is the "standard" exception handler used in a few places to handle
002392 ** an exception thrown by reading from the *-shm mapping after it has become
002393 ** invalid in SQLITE_USE_SEH builds. It is used as follows:
002394 **
002395 ** SEH_TRY { ... }
002396 ** SEH_EXCEPT( rc = walHandleException(pWal); )
002397 **
002398 ** This function does three things:
002399 **
002400 ** 1) Determines the locks that should be held, based on the contents of
002401 ** the Wal.readLock, Wal.writeLock and Wal.ckptLock variables. All other
002402 ** held locks are assumed to be transient locks that would have been
002403 ** released had the exception not been thrown and are dropped.
002404 **
002405 ** 2) Frees the pointer at Wal.pFree, if any, using sqlite3_free().
002406 **
002407 ** 3) Set pWal->apWiData[pWal->iWiPg] to pWal->pWiValue if not NULL
002408 **
002409 ** 4) Returns SQLITE_IOERR.
002410 */
002411 static int walHandleException(Wal *pWal){
002412 if( pWal->exclusiveMode==0 ){
002413 static const int S = 1;
002414 static const int E = (1<<SQLITE_SHM_NLOCK);
002415 int ii;
002416 u32 mUnlock = pWal->lockMask & ~(
002417 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
002418 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
002419 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
002420 );
002421 for(ii=0; ii<SQLITE_SHM_NLOCK; ii++){
002422 if( (S<<ii) & mUnlock ) walUnlockShared(pWal, ii);
002423 if( (E<<ii) & mUnlock ) walUnlockExclusive(pWal, ii, 1);
002424 }
002425 }
002426 sqlite3_free(pWal->pFree);
002427 pWal->pFree = 0;
002428 if( pWal->pWiValue ){
002429 pWal->apWiData[pWal->iWiPg] = pWal->pWiValue;
002430 pWal->pWiValue = 0;
002431 }
002432 return SQLITE_IOERR_IN_PAGE;
002433 }
002434
002435 /*
002436 ** Assert that the Wal.lockMask mask, which indicates the locks held
002437 ** by the connection, is consistent with the Wal.readLock, Wal.writeLock
002438 ** and Wal.ckptLock variables. To be used as:
002439 **
002440 ** assert( walAssertLockmask(pWal) );
002441 */
002442 static int walAssertLockmask(Wal *pWal){
002443 if( pWal->exclusiveMode==0 ){
002444 static const int S = 1;
002445 static const int E = (1<<SQLITE_SHM_NLOCK);
002446 u32 mExpect = (
002447 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
002448 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
002449 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
002450 #ifdef SQLITE_ENABLE_SNAPSHOT
002451 | (pWal->pSnapshot ? (pWal->lockMask & (1 << WAL_CKPT_LOCK)) : 0)
002452 #endif
002453 );
002454 assert( mExpect==pWal->lockMask );
002455 }
002456 return 1;
002457 }
002458
002459 /*
002460 ** Return and zero the "system error" field set when an
002461 ** EXCEPTION_IN_PAGE_ERROR exception is caught.
002462 */
002463 int sqlite3WalSystemErrno(Wal *pWal){
002464 int iRet = 0;
002465 if( pWal ){
002466 iRet = pWal->iSysErrno;
002467 pWal->iSysErrno = 0;
002468 }
002469 return iRet;
002470 }
002471
002472 #else
002473 # define walAssertLockmask(x) 1
002474 #endif /* ifdef SQLITE_USE_SEH */
002475
002476 /*
002477 ** Close a connection to a log file.
002478 */
002479 int sqlite3WalClose(
002480 Wal *pWal, /* Wal to close */
002481 sqlite3 *db, /* For interrupt flag */
002482 int sync_flags, /* Flags to pass to OsSync() (or 0) */
002483 int nBuf,
002484 u8 *zBuf /* Buffer of at least nBuf bytes */
002485 ){
002486 int rc = SQLITE_OK;
002487 if( pWal ){
002488 int isDelete = 0; /* True to unlink wal and wal-index files */
002489
002490 assert( walAssertLockmask(pWal) );
002491
002492 /* If an EXCLUSIVE lock can be obtained on the database file (using the
002493 ** ordinary, rollback-mode locking methods, this guarantees that the
002494 ** connection associated with this log file is the only connection to
002495 ** the database. In this case checkpoint the database and unlink both
002496 ** the wal and wal-index files.
002497 **
002498 ** The EXCLUSIVE lock is not released before returning.
002499 */
002500 if( zBuf!=0
002501 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
002502 ){
002503 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
002504 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
002505 }
002506 rc = sqlite3WalCheckpoint(pWal, db,
002507 SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
002508 );
002509 if( rc==SQLITE_OK ){
002510 int bPersist = -1;
002511 sqlite3OsFileControlHint(
002512 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
002513 );
002514 if( bPersist!=1 ){
002515 /* Try to delete the WAL file if the checkpoint completed and
002516 ** fsynced (rc==SQLITE_OK) and if we are not in persistent-wal
002517 ** mode (!bPersist) */
002518 isDelete = 1;
002519 }else if( pWal->mxWalSize>=0 ){
002520 /* Try to truncate the WAL file to zero bytes if the checkpoint
002521 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
002522 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
002523 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
002524 ** to zero bytes as truncating to the journal_size_limit might
002525 ** leave a corrupt WAL file on disk. */
002526 walLimitSize(pWal, 0);
002527 }
002528 }
002529 }
002530
002531 walIndexClose(pWal, isDelete);
002532 sqlite3OsClose(pWal->pWalFd);
002533 if( isDelete ){
002534 sqlite3BeginBenignMalloc();
002535 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
002536 sqlite3EndBenignMalloc();
002537 }
002538 WALTRACE(("WAL%p: closed\n", pWal));
002539 sqlite3_free((void *)pWal->apWiData);
002540 sqlite3_free(pWal);
002541 }
002542 return rc;
002543 }
002544
002545 /*
002546 ** Try to read the wal-index header. Return 0 on success and 1 if
002547 ** there is a problem.
002548 **
002549 ** The wal-index is in shared memory. Another thread or process might
002550 ** be writing the header at the same time this procedure is trying to
002551 ** read it, which might result in inconsistency. A dirty read is detected
002552 ** by verifying that both copies of the header are the same and also by
002553 ** a checksum on the header.
002554 **
002555 ** If and only if the read is consistent and the header is different from
002556 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
002557 ** and *pChanged is set to 1.
002558 **
002559 ** If the checksum cannot be verified return non-zero. If the header
002560 ** is read successfully and the checksum verified, return zero.
002561 */
002562 static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){
002563 u32 aCksum[2]; /* Checksum on the header content */
002564 WalIndexHdr h1, h2; /* Two copies of the header content */
002565 WalIndexHdr volatile *aHdr; /* Header in shared memory */
002566
002567 /* The first page of the wal-index must be mapped at this point. */
002568 assert( pWal->nWiData>0 && pWal->apWiData[0] );
002569
002570 /* Read the header. This might happen concurrently with a write to the
002571 ** same area of shared memory on a different CPU in a SMP,
002572 ** meaning it is possible that an inconsistent snapshot is read
002573 ** from the file. If this happens, return non-zero.
002574 **
002575 ** tag-20200519-1:
002576 ** There are two copies of the header at the beginning of the wal-index.
002577 ** When reading, read [0] first then [1]. Writes are in the reverse order.
002578 ** Memory barriers are used to prevent the compiler or the hardware from
002579 ** reordering the reads and writes. TSAN and similar tools can sometimes
002580 ** give false-positive warnings about these accesses because the tools do not
002581 ** account for the double-read and the memory barrier. The use of mutexes
002582 ** here would be problematic as the memory being accessed is potentially
002583 ** shared among multiple processes and not all mutex implementations work
002584 ** reliably in that environment.
002585 */
002586 aHdr = walIndexHdr(pWal);
002587 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
002588 walShmBarrier(pWal);
002589 memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
002590
002591 if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
002592 return 1; /* Dirty read */
002593 }
002594 if( h1.isInit==0 ){
002595 return 1; /* Malformed header - probably all zeros */
002596 }
002597 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
002598 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
002599 return 1; /* Checksum does not match */
002600 }
002601
002602 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
002603 *pChanged = 1;
002604 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
002605 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
002606 testcase( pWal->szPage<=32768 );
002607 testcase( pWal->szPage>=65536 );
002608 }
002609
002610 /* The header was successfully read. Return zero. */
002611 return 0;
002612 }
002613
002614 /*
002615 ** This is the value that walTryBeginRead returns when it needs to
002616 ** be retried.
002617 */
002618 #define WAL_RETRY (-1)
002619
002620 /*
002621 ** Read the wal-index header from the wal-index and into pWal->hdr.
002622 ** If the wal-header appears to be corrupt, try to reconstruct the
002623 ** wal-index from the WAL before returning.
002624 **
002625 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
002626 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
002627 ** to 0.
002628 **
002629 ** If the wal-index header is successfully read, return SQLITE_OK.
002630 ** Otherwise an SQLite error code.
002631 */
002632 static int walIndexReadHdr(Wal *pWal, int *pChanged){
002633 int rc; /* Return code */
002634 int badHdr; /* True if a header read failed */
002635 volatile u32 *page0; /* Chunk of wal-index containing header */
002636
002637 /* Ensure that page 0 of the wal-index (the page that contains the
002638 ** wal-index header) is mapped. Return early if an error occurs here.
002639 */
002640 assert( pChanged );
002641 rc = walIndexPage(pWal, 0, &page0);
002642 if( rc!=SQLITE_OK ){
002643 assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */
002644 if( rc==SQLITE_READONLY_CANTINIT ){
002645 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
002646 ** was openable but is not writable, and this thread is unable to
002647 ** confirm that another write-capable connection has the shared-memory
002648 ** open, and hence the content of the shared-memory is unreliable,
002649 ** since the shared-memory might be inconsistent with the WAL file
002650 ** and there is no writer on hand to fix it. */
002651 assert( page0==0 );
002652 assert( pWal->writeLock==0 );
002653 assert( pWal->readOnly & WAL_SHM_RDONLY );
002654 pWal->bShmUnreliable = 1;
002655 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
002656 *pChanged = 1;
002657 }else{
002658 return rc; /* Any other non-OK return is just an error */
002659 }
002660 }else{
002661 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
002662 ** is zero, which prevents the SHM from growing */
002663 testcase( page0!=0 );
002664 }
002665 assert( page0!=0 || pWal->writeLock==0 );
002666
002667 /* If the first page of the wal-index has been mapped, try to read the
002668 ** wal-index header immediately, without holding any lock. This usually
002669 ** works, but may fail if the wal-index header is corrupt or currently
002670 ** being modified by another thread or process.
002671 */
002672 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
002673
002674 /* If the first attempt failed, it might have been due to a race
002675 ** with a writer. So get a WRITE lock and try again.
002676 */
002677 if( badHdr ){
002678 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
002679 if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
002680 walUnlockShared(pWal, WAL_WRITE_LOCK);
002681 rc = SQLITE_READONLY_RECOVERY;
002682 }
002683 }else{
002684 int bWriteLock = pWal->writeLock;
002685 if( bWriteLock
002686 || SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1))
002687 ){
002688 pWal->writeLock = 1;
002689 if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
002690 badHdr = walIndexTryHdr(pWal, pChanged);
002691 if( badHdr ){
002692 /* If the wal-index header is still malformed even while holding
002693 ** a WRITE lock, it can only mean that the header is corrupted and
002694 ** needs to be reconstructed. So run recovery to do exactly that.
002695 ** Disable blocking locks first. */
002696 walDisableBlocking(pWal);
002697 rc = walIndexRecover(pWal);
002698 *pChanged = 1;
002699 }
002700 }
002701 if( bWriteLock==0 ){
002702 pWal->writeLock = 0;
002703 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
002704 }
002705 }
002706 }
002707 }
002708
002709 /* If the header is read successfully, check the version number to make
002710 ** sure the wal-index was not constructed with some future format that
002711 ** this version of SQLite cannot understand.
002712 */
002713 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
002714 rc = SQLITE_CANTOPEN_BKPT;
002715 }
002716 if( pWal->bShmUnreliable ){
002717 if( rc!=SQLITE_OK ){
002718 walIndexClose(pWal, 0);
002719 pWal->bShmUnreliable = 0;
002720 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
002721 /* walIndexRecover() might have returned SHORT_READ if a concurrent
002722 ** writer truncated the WAL out from under it. If that happens, it
002723 ** indicates that a writer has fixed the SHM file for us, so retry */
002724 if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY;
002725 }
002726 pWal->exclusiveMode = WAL_NORMAL_MODE;
002727 }
002728
002729 return rc;
002730 }
002731
002732 /*
002733 ** Open a transaction in a connection where the shared-memory is read-only
002734 ** and where we cannot verify that there is a separate write-capable connection
002735 ** on hand to keep the shared-memory up-to-date with the WAL file.
002736 **
002737 ** This can happen, for example, when the shared-memory is implemented by
002738 ** memory-mapping a *-shm file, where a prior writer has shut down and
002739 ** left the *-shm file on disk, and now the present connection is trying
002740 ** to use that database but lacks write permission on the *-shm file.
002741 ** Other scenarios are also possible, depending on the VFS implementation.
002742 **
002743 ** Precondition:
002744 **
002745 ** The *-wal file has been read and an appropriate wal-index has been
002746 ** constructed in pWal->apWiData[] using heap memory instead of shared
002747 ** memory.
002748 **
002749 ** If this function returns SQLITE_OK, then the read transaction has
002750 ** been successfully opened. In this case output variable (*pChanged)
002751 ** is set to true before returning if the caller should discard the
002752 ** contents of the page cache before proceeding. Or, if it returns
002753 ** WAL_RETRY, then the heap memory wal-index has been discarded and
002754 ** the caller should retry opening the read transaction from the
002755 ** beginning (including attempting to map the *-shm file).
002756 **
002757 ** If an error occurs, an SQLite error code is returned.
002758 */
002759 static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
002760 i64 szWal; /* Size of wal file on disk in bytes */
002761 i64 iOffset; /* Current offset when reading wal file */
002762 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
002763 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
002764 int szFrame; /* Number of bytes in buffer aFrame[] */
002765 u8 *aData; /* Pointer to data part of aFrame buffer */
002766 volatile void *pDummy; /* Dummy argument for xShmMap */
002767 int rc; /* Return code */
002768 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
002769
002770 assert( pWal->bShmUnreliable );
002771 assert( pWal->readOnly & WAL_SHM_RDONLY );
002772 assert( pWal->nWiData>0 && pWal->apWiData[0] );
002773
002774 /* Take WAL_READ_LOCK(0). This has the effect of preventing any
002775 ** writers from running a checkpoint, but does not stop them
002776 ** from running recovery. */
002777 rc = walLockShared(pWal, WAL_READ_LOCK(0));
002778 if( rc!=SQLITE_OK ){
002779 if( rc==SQLITE_BUSY ) rc = WAL_RETRY;
002780 goto begin_unreliable_shm_out;
002781 }
002782 pWal->readLock = 0;
002783
002784 /* Check to see if a separate writer has attached to the shared-memory area,
002785 ** thus making the shared-memory "reliable" again. Do this by invoking
002786 ** the xShmMap() routine of the VFS and looking to see if the return
002787 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
002788 **
002789 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
002790 ** cause the heap-memory WAL-index to be discarded and the actual
002791 ** shared memory to be used in its place.
002792 **
002793 ** This step is important because, even though this connection is holding
002794 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
002795 ** have already checkpointed the WAL file and, while the current
002796 ** is active, wrap the WAL and start overwriting frames that this
002797 ** process wants to use.
002798 **
002799 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
002800 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
002801 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
002802 ** even if some external agent does a "chmod" to make the shared-memory
002803 ** writable by us, until sqlite3OsShmUnmap() has been called.
002804 ** This is a requirement on the VFS implementation.
002805 */
002806 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
002807 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
002808 if( rc!=SQLITE_READONLY_CANTINIT ){
002809 rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc);
002810 goto begin_unreliable_shm_out;
002811 }
002812
002813 /* We reach this point only if the real shared-memory is still unreliable.
002814 ** Assume the in-memory WAL-index substitute is correct and load it
002815 ** into pWal->hdr.
002816 */
002817 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
002818
002819 /* Make sure some writer hasn't come in and changed the WAL file out
002820 ** from under us, then disconnected, while we were not looking.
002821 */
002822 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
002823 if( rc!=SQLITE_OK ){
002824 goto begin_unreliable_shm_out;
002825 }
002826 if( szWal<WAL_HDRSIZE ){
002827 /* If the wal file is too small to contain a wal-header and the
002828 ** wal-index header has mxFrame==0, then it must be safe to proceed
002829 ** reading the database file only. However, the page cache cannot
002830 ** be trusted, as a read/write connection may have connected, written
002831 ** the db, run a checkpoint, truncated the wal file and disconnected
002832 ** since this client's last read transaction. */
002833 *pChanged = 1;
002834 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
002835 goto begin_unreliable_shm_out;
002836 }
002837
002838 /* Check the salt keys at the start of the wal file still match. */
002839 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
002840 if( rc!=SQLITE_OK ){
002841 goto begin_unreliable_shm_out;
002842 }
002843 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
002844 /* Some writer has wrapped the WAL file while we were not looking.
002845 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
002846 ** rebuilt. */
002847 rc = WAL_RETRY;
002848 goto begin_unreliable_shm_out;
002849 }
002850
002851 /* Allocate a buffer to read frames into */
002852 assert( (pWal->szPage & (pWal->szPage-1))==0 );
002853 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
002854 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
002855 aFrame = (u8 *)sqlite3_malloc64(szFrame);
002856 if( aFrame==0 ){
002857 rc = SQLITE_NOMEM_BKPT;
002858 goto begin_unreliable_shm_out;
002859 }
002860 aData = &aFrame[WAL_FRAME_HDRSIZE];
002861
002862 /* Check to see if a complete transaction has been appended to the
002863 ** wal file since the heap-memory wal-index was created. If so, the
002864 ** heap-memory wal-index is discarded and WAL_RETRY returned to
002865 ** the caller. */
002866 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
002867 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
002868 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
002869 iOffset+szFrame<=szWal;
002870 iOffset+=szFrame
002871 ){
002872 u32 pgno; /* Database page number for frame */
002873 u32 nTruncate; /* dbsize field from frame header */
002874
002875 /* Read and decode the next log frame. */
002876 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
002877 if( rc!=SQLITE_OK ) break;
002878 if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
002879
002880 /* If nTruncate is non-zero, then a complete transaction has been
002881 ** appended to this wal file. Set rc to WAL_RETRY and break out of
002882 ** the loop. */
002883 if( nTruncate ){
002884 rc = WAL_RETRY;
002885 break;
002886 }
002887 }
002888 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
002889 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
002890
002891 begin_unreliable_shm_out:
002892 sqlite3_free(aFrame);
002893 if( rc!=SQLITE_OK ){
002894 int i;
002895 for(i=0; i<pWal->nWiData; i++){
002896 sqlite3_free((void*)pWal->apWiData[i]);
002897 pWal->apWiData[i] = 0;
002898 }
002899 pWal->bShmUnreliable = 0;
002900 sqlite3WalEndReadTransaction(pWal);
002901 *pChanged = 1;
002902 }
002903 return rc;
002904 }
002905
002906 /*
002907 ** The final argument passed to walTryBeginRead() is of type (int*). The
002908 ** caller should invoke walTryBeginRead as follows:
002909 **
002910 ** int cnt = 0;
002911 ** do {
002912 ** rc = walTryBeginRead(..., &cnt);
002913 ** }while( rc==WAL_RETRY );
002914 **
002915 ** The final value of "cnt" is of no use to the caller. It is used by
002916 ** the implementation of walTryBeginRead() as follows:
002917 **
002918 ** + Each time walTryBeginRead() is called, it is incremented. Once
002919 ** it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead()
002920 ** has many times been invoked and failed with WAL_RETRY - walTryBeginRead()
002921 ** returns SQLITE_PROTOCOL.
002922 **
002923 ** + If SQLITE_ENABLE_SETLK_TIMEOUT is defined and walTryBeginRead() failed
002924 ** because a blocking lock timed out (SQLITE_BUSY_TIMEOUT from the OS
002925 ** layer), the WAL_RETRY_BLOCKED_MASK bit is set in "cnt". In this case
002926 ** the next invocation of walTryBeginRead() may omit an expected call to
002927 ** sqlite3OsSleep(). There has already been a delay when the previous call
002928 ** waited on a lock.
002929 */
002930 #define WAL_RETRY_PROTOCOL_LIMIT 100
002931 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
002932 # define WAL_RETRY_BLOCKED_MASK 0x10000000
002933 #else
002934 # define WAL_RETRY_BLOCKED_MASK 0
002935 #endif
002936
002937 /*
002938 ** Attempt to start a read transaction. This might fail due to a race or
002939 ** other transient condition. When that happens, it returns WAL_RETRY to
002940 ** indicate to the caller that it is safe to retry immediately.
002941 **
002942 ** On success return SQLITE_OK. On a permanent failure (such an
002943 ** I/O error or an SQLITE_BUSY because another process is running
002944 ** recovery) return a positive error code.
002945 **
002946 ** The useWal parameter is true to force the use of the WAL and disable
002947 ** the case where the WAL is bypassed because it has been completely
002948 ** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
002949 ** to make a copy of the wal-index header into pWal->hdr. If the
002950 ** wal-index header has changed, *pChanged is set to 1 (as an indication
002951 ** to the caller that the local page cache is obsolete and needs to be
002952 ** flushed.) When useWal==1, the wal-index header is assumed to already
002953 ** be loaded and the pChanged parameter is unused.
002954 **
002955 ** The caller must set the cnt parameter to the number of prior calls to
002956 ** this routine during the current read attempt that returned WAL_RETRY.
002957 ** This routine will start taking more aggressive measures to clear the
002958 ** race conditions after multiple WAL_RETRY returns, and after an excessive
002959 ** number of errors will ultimately return SQLITE_PROTOCOL. The
002960 ** SQLITE_PROTOCOL return indicates that some other process has gone rogue
002961 ** and is not honoring the locking protocol. There is a vanishingly small
002962 ** chance that SQLITE_PROTOCOL could be returned because of a run of really
002963 ** bad luck when there is lots of contention for the wal-index, but that
002964 ** possibility is so small that it can be safely neglected, we believe.
002965 **
002966 ** On success, this routine obtains a read lock on
002967 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
002968 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
002969 ** that means the Wal does not hold any read lock. The reader must not
002970 ** access any database page that is modified by a WAL frame up to and
002971 ** including frame number aReadMark[pWal->readLock]. The reader will
002972 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
002973 ** Or if pWal->readLock==0, then the reader will ignore the WAL
002974 ** completely and get all content directly from the database file.
002975 ** If the useWal parameter is 1 then the WAL will never be ignored and
002976 ** this routine will always set pWal->readLock>0 on success.
002977 ** When the read transaction is completed, the caller must release the
002978 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
002979 **
002980 ** This routine uses the nBackfill and aReadMark[] fields of the header
002981 ** to select a particular WAL_READ_LOCK() that strives to let the
002982 ** checkpoint process do as much work as possible. This routine might
002983 ** update values of the aReadMark[] array in the header, but if it does
002984 ** so it takes care to hold an exclusive lock on the corresponding
002985 ** WAL_READ_LOCK() while changing values.
002986 */
002987 static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){
002988 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
002989 int rc = SQLITE_OK; /* Return code */
002990 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
002991 int nBlockTmout = 0;
002992 #endif
002993
002994 assert( pWal->readLock<0 ); /* Not currently locked */
002995
002996 /* useWal may only be set for read/write connections */
002997 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
002998
002999 /* Take steps to avoid spinning forever if there is a protocol error.
003000 **
003001 ** Circumstances that cause a RETRY should only last for the briefest
003002 ** instances of time. No I/O or other system calls are done while the
003003 ** locks are held, so the locks should not be held for very long. But
003004 ** if we are unlucky, another process that is holding a lock might get
003005 ** paged out or take a page-fault that is time-consuming to resolve,
003006 ** during the few nanoseconds that it is holding the lock. In that case,
003007 ** it might take longer than normal for the lock to free.
003008 **
003009 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
003010 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
003011 ** is more of a scheduler yield than an actual delay. But on the 10th
003012 ** an subsequent retries, the delays start becoming longer and longer,
003013 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
003014 ** The total delay time before giving up is less than 10 seconds.
003015 */
003016 (*pCnt)++;
003017 if( *pCnt>5 ){
003018 int nDelay = 1; /* Pause time in microseconds */
003019 int cnt = (*pCnt & ~WAL_RETRY_BLOCKED_MASK);
003020 if( cnt>WAL_RETRY_PROTOCOL_LIMIT ){
003021 VVA_ONLY( pWal->lockError = 1; )
003022 return SQLITE_PROTOCOL;
003023 }
003024 if( *pCnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
003025 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
003026 /* In SQLITE_ENABLE_SETLK_TIMEOUT builds, configure the file-descriptor
003027 ** to block for locks for approximately nDelay us. This affects three
003028 ** locks: (a) the shared lock taken on the DMS slot in os_unix.c (if
003029 ** using os_unix.c), (b) the WRITER lock taken in walIndexReadHdr() if the
003030 ** first attempted read fails, and (c) the shared lock taken on the
003031 ** read-mark.
003032 **
003033 ** If the previous call failed due to an SQLITE_BUSY_TIMEOUT error,
003034 ** then sleep for the minimum of 1us. The previous call already provided
003035 ** an extra delay while it was blocking on the lock.
003036 */
003037 nBlockTmout = (nDelay+998) / 1000;
003038 if( !useWal && walEnableBlockingMs(pWal, nBlockTmout) ){
003039 if( *pCnt & WAL_RETRY_BLOCKED_MASK ) nDelay = 1;
003040 }
003041 #endif
003042 sqlite3OsSleep(pWal->pVfs, nDelay);
003043 *pCnt &= ~WAL_RETRY_BLOCKED_MASK;
003044 }
003045
003046 if( !useWal ){
003047 assert( rc==SQLITE_OK );
003048 if( pWal->bShmUnreliable==0 ){
003049 rc = walIndexReadHdr(pWal, pChanged);
003050 }
003051 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
003052 walDisableBlocking(pWal);
003053 if( rc==SQLITE_BUSY_TIMEOUT ){
003054 rc = SQLITE_BUSY;
003055 *pCnt |= WAL_RETRY_BLOCKED_MASK;
003056 }
003057 #endif
003058 if( rc==SQLITE_BUSY ){
003059 /* If there is not a recovery running in another thread or process
003060 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
003061 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
003062 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
003063 ** would be technically correct. But the race is benign since with
003064 ** WAL_RETRY this routine will be called again and will probably be
003065 ** right on the second iteration.
003066 */
003067 if( pWal->apWiData[0]==0 ){
003068 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
003069 ** We assume this is a transient condition, so return WAL_RETRY. The
003070 ** xShmMap() implementation used by the default unix and win32 VFS
003071 ** modules may return SQLITE_BUSY due to a race condition in the
003072 ** code that determines whether or not the shared-memory region
003073 ** must be zeroed before the requested page is returned.
003074 */
003075 rc = WAL_RETRY;
003076 }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
003077 walUnlockShared(pWal, WAL_RECOVER_LOCK);
003078 rc = WAL_RETRY;
003079 }else if( rc==SQLITE_BUSY ){
003080 rc = SQLITE_BUSY_RECOVERY;
003081 }
003082 }
003083 if( rc!=SQLITE_OK ){
003084 return rc;
003085 }
003086 else if( pWal->bShmUnreliable ){
003087 return walBeginShmUnreliable(pWal, pChanged);
003088 }
003089 }
003090
003091 assert( pWal->nWiData>0 );
003092 assert( pWal->apWiData[0]!=0 );
003093 pInfo = walCkptInfo(pWal);
003094 SEH_INJECT_FAULT;
003095 {
003096 u32 mxReadMark; /* Largest aReadMark[] value */
003097 int mxI; /* Index of largest aReadMark[] value */
003098 int i; /* Loop counter */
003099 u32 mxFrame; /* Wal frame to lock to */
003100 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
003101 #ifdef SQLITE_ENABLE_SNAPSHOT
003102 && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0)
003103 #endif
003104 ){
003105 /* The WAL has been completely backfilled (or it is empty).
003106 ** and can be safely ignored.
003107 */
003108 rc = walLockShared(pWal, WAL_READ_LOCK(0));
003109 walShmBarrier(pWal);
003110 if( rc==SQLITE_OK ){
003111 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr,sizeof(WalIndexHdr)) ){
003112 /* It is not safe to allow the reader to continue here if frames
003113 ** may have been appended to the log before READ_LOCK(0) was obtained.
003114 ** When holding READ_LOCK(0), the reader ignores the entire log file,
003115 ** which implies that the database file contains a trustworthy
003116 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
003117 ** happening, this is usually correct.
003118 **
003119 ** However, if frames have been appended to the log (or if the log
003120 ** is wrapped and written for that matter) before the READ_LOCK(0)
003121 ** is obtained, that is not necessarily true. A checkpointer may
003122 ** have started to backfill the appended frames but crashed before
003123 ** it finished. Leaving a corrupt image in the database file.
003124 */
003125 walUnlockShared(pWal, WAL_READ_LOCK(0));
003126 return WAL_RETRY;
003127 }
003128 pWal->readLock = 0;
003129 return SQLITE_OK;
003130 }else if( rc!=SQLITE_BUSY ){
003131 return rc;
003132 }
003133 }
003134
003135 /* If we get this far, it means that the reader will want to use
003136 ** the WAL to get at content from recent commits. The job now is
003137 ** to select one of the aReadMark[] entries that is closest to
003138 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
003139 */
003140 mxReadMark = 0;
003141 mxI = 0;
003142 mxFrame = pWal->hdr.mxFrame;
003143 #ifdef SQLITE_ENABLE_SNAPSHOT
003144 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
003145 mxFrame = pWal->pSnapshot->mxFrame;
003146 }
003147 #endif
003148 for(i=1; i<WAL_NREADER; i++){
003149 u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
003150 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
003151 assert( thisMark!=READMARK_NOT_USED );
003152 mxReadMark = thisMark;
003153 mxI = i;
003154 }
003155 }
003156 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
003157 && (mxReadMark<mxFrame || mxI==0)
003158 ){
003159 for(i=1; i<WAL_NREADER; i++){
003160 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
003161 if( rc==SQLITE_OK ){
003162 AtomicStore(pInfo->aReadMark+i,mxFrame);
003163 mxReadMark = mxFrame;
003164 mxI = i;
003165 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
003166 break;
003167 }else if( rc!=SQLITE_BUSY ){
003168 return rc;
003169 }
003170 }
003171 }
003172 if( mxI==0 ){
003173 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
003174 return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
003175 }
003176
003177 (void)walEnableBlockingMs(pWal, nBlockTmout);
003178 rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
003179 walDisableBlocking(pWal);
003180 if( rc ){
003181 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
003182 if( rc==SQLITE_BUSY_TIMEOUT ){
003183 *pCnt |= WAL_RETRY_BLOCKED_MASK;
003184 }
003185 #else
003186 assert( rc!=SQLITE_BUSY_TIMEOUT );
003187 #endif
003188 assert((rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT);
003189 return (rc&0xFF)==SQLITE_BUSY ? WAL_RETRY : rc;
003190 }
003191 /* Now that the read-lock has been obtained, check that neither the
003192 ** value in the aReadMark[] array or the contents of the wal-index
003193 ** header have changed.
003194 **
003195 ** It is necessary to check that the wal-index header did not change
003196 ** between the time it was read and when the shared-lock was obtained
003197 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
003198 ** that the log file may have been wrapped by a writer, or that frames
003199 ** that occur later in the log than pWal->hdr.mxFrame may have been
003200 ** copied into the database by a checkpointer. If either of these things
003201 ** happened, then reading the database with the current value of
003202 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
003203 ** instead.
003204 **
003205 ** Before checking that the live wal-index header has not changed
003206 ** since it was read, set Wal.minFrame to the first frame in the wal
003207 ** file that has not yet been checkpointed. This client will not need
003208 ** to read any frames earlier than minFrame from the wal file - they
003209 ** can be safely read directly from the database file.
003210 **
003211 ** Because a ShmBarrier() call is made between taking the copy of
003212 ** nBackfill and checking that the wal-header in shared-memory still
003213 ** matches the one cached in pWal->hdr, it is guaranteed that the
003214 ** checkpointer that set nBackfill was not working with a wal-index
003215 ** header newer than that cached in pWal->hdr. If it were, that could
003216 ** cause a problem. The checkpointer could omit to checkpoint
003217 ** a version of page X that lies before pWal->minFrame (call that version
003218 ** A) on the basis that there is a newer version (version B) of the same
003219 ** page later in the wal file. But if version B happens to like past
003220 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
003221 ** that it can read version A from the database file. However, since
003222 ** we can guarantee that the checkpointer that set nBackfill could not
003223 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
003224 */
003225 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT;
003226 walShmBarrier(pWal);
003227 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
003228 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
003229 ){
003230 walUnlockShared(pWal, WAL_READ_LOCK(mxI));
003231 return WAL_RETRY;
003232 }else{
003233 assert( mxReadMark<=pWal->hdr.mxFrame );
003234 pWal->readLock = (i16)mxI;
003235 }
003236 }
003237 return rc;
003238 }
003239
003240 #ifdef SQLITE_ENABLE_SNAPSHOT
003241 /*
003242 ** This function does the work of sqlite3WalSnapshotRecover().
003243 */
003244 static int walSnapshotRecover(
003245 Wal *pWal, /* WAL handle */
003246 void *pBuf1, /* Temp buffer pWal->szPage bytes in size */
003247 void *pBuf2 /* Temp buffer pWal->szPage bytes in size */
003248 ){
003249 int szPage = (int)pWal->szPage;
003250 int rc;
003251 i64 szDb; /* Size of db file in bytes */
003252
003253 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
003254 if( rc==SQLITE_OK ){
003255 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
003256 u32 i = pInfo->nBackfillAttempted;
003257 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
003258 WalHashLoc sLoc; /* Hash table location */
003259 u32 pgno; /* Page number in db file */
003260 i64 iDbOff; /* Offset of db file entry */
003261 i64 iWalOff; /* Offset of wal file entry */
003262
003263 rc = walHashGet(pWal, walFramePage(i), &sLoc);
003264 if( rc!=SQLITE_OK ) break;
003265 assert( i - sLoc.iZero - 1 >=0 );
003266 pgno = sLoc.aPgno[i-sLoc.iZero-1];
003267 iDbOff = (i64)(pgno-1) * szPage;
003268
003269 if( iDbOff+szPage<=szDb ){
003270 iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE;
003271 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
003272
003273 if( rc==SQLITE_OK ){
003274 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
003275 }
003276
003277 if( rc!=SQLITE_OK || 0==memcmp(pBuf1, pBuf2, szPage) ){
003278 break;
003279 }
003280 }
003281
003282 pInfo->nBackfillAttempted = i-1;
003283 }
003284 }
003285
003286 return rc;
003287 }
003288
003289 /*
003290 ** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
003291 ** variable so that older snapshots can be accessed. To do this, loop
003292 ** through all wal frames from nBackfillAttempted to (nBackfill+1),
003293 ** comparing their content to the corresponding page with the database
003294 ** file, if any. Set nBackfillAttempted to the frame number of the
003295 ** first frame for which the wal file content matches the db file.
003296 **
003297 ** This is only really safe if the file-system is such that any page
003298 ** writes made by earlier checkpointers were atomic operations, which
003299 ** is not always true. It is also possible that nBackfillAttempted
003300 ** may be left set to a value larger than expected, if a wal frame
003301 ** contains content that duplicate of an earlier version of the same
003302 ** page.
003303 **
003304 ** SQLITE_OK is returned if successful, or an SQLite error code if an
003305 ** error occurs. It is not an error if nBackfillAttempted cannot be
003306 ** decreased at all.
003307 */
003308 int sqlite3WalSnapshotRecover(Wal *pWal){
003309 int rc;
003310
003311 assert( pWal->readLock>=0 );
003312 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
003313 if( rc==SQLITE_OK ){
003314 void *pBuf1 = sqlite3_malloc(pWal->szPage);
003315 void *pBuf2 = sqlite3_malloc(pWal->szPage);
003316 if( pBuf1==0 || pBuf2==0 ){
003317 rc = SQLITE_NOMEM;
003318 }else{
003319 pWal->ckptLock = 1;
003320 SEH_TRY {
003321 rc = walSnapshotRecover(pWal, pBuf1, pBuf2);
003322 }
003323 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; )
003324 pWal->ckptLock = 0;
003325 }
003326
003327 sqlite3_free(pBuf1);
003328 sqlite3_free(pBuf2);
003329 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
003330 }
003331
003332 return rc;
003333 }
003334 #endif /* SQLITE_ENABLE_SNAPSHOT */
003335
003336 /*
003337 ** This function does the work of sqlite3WalBeginReadTransaction() (see
003338 ** below). That function simply calls this one inside an SEH_TRY{...} block.
003339 */
003340 static int walBeginReadTransaction(Wal *pWal, int *pChanged){
003341 int rc; /* Return code */
003342 int cnt = 0; /* Number of TryBeginRead attempts */
003343 #ifdef SQLITE_ENABLE_SNAPSHOT
003344 int ckptLock = 0;
003345 int bChanged = 0;
003346 WalIndexHdr *pSnapshot = pWal->pSnapshot;
003347 #endif
003348
003349 assert( pWal->ckptLock==0 );
003350 assert( pWal->nSehTry>0 );
003351
003352 #ifdef SQLITE_ENABLE_SNAPSHOT
003353 if( pSnapshot ){
003354 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
003355 bChanged = 1;
003356 }
003357
003358 /* It is possible that there is a checkpointer thread running
003359 ** concurrent with this code. If this is the case, it may be that the
003360 ** checkpointer has already determined that it will checkpoint
003361 ** snapshot X, where X is later in the wal file than pSnapshot, but
003362 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
003363 ** its intent. To avoid the race condition this leads to, ensure that
003364 ** there is no checkpointer process by taking a shared CKPT lock
003365 ** before checking pInfo->nBackfillAttempted. */
003366 (void)walEnableBlocking(pWal);
003367 rc = walLockShared(pWal, WAL_CKPT_LOCK);
003368 walDisableBlocking(pWal);
003369
003370 if( rc!=SQLITE_OK ){
003371 return rc;
003372 }
003373 ckptLock = 1;
003374 }
003375 #endif
003376
003377 do{
003378 rc = walTryBeginRead(pWal, pChanged, 0, &cnt);
003379 }while( rc==WAL_RETRY );
003380 testcase( (rc&0xff)==SQLITE_BUSY );
003381 testcase( (rc&0xff)==SQLITE_IOERR );
003382 testcase( rc==SQLITE_PROTOCOL );
003383 testcase( rc==SQLITE_OK );
003384
003385 #ifdef SQLITE_ENABLE_SNAPSHOT
003386 if( rc==SQLITE_OK ){
003387 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
003388 /* At this point the client has a lock on an aReadMark[] slot holding
003389 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
003390 ** is populated with the wal-index header corresponding to the head
003391 ** of the wal file. Verify that pSnapshot is still valid before
003392 ** continuing. Reasons why pSnapshot might no longer be valid:
003393 **
003394 ** (1) The WAL file has been reset since the snapshot was taken.
003395 ** In this case, the salt will have changed.
003396 **
003397 ** (2) A checkpoint as been attempted that wrote frames past
003398 ** pSnapshot->mxFrame into the database file. Note that the
003399 ** checkpoint need not have completed for this to cause problems.
003400 */
003401 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
003402
003403 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
003404 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
003405
003406 /* Check that the wal file has not been wrapped. Assuming that it has
003407 ** not, also check that no checkpointer has attempted to checkpoint any
003408 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
003409 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
003410 ** with *pSnapshot and set *pChanged as appropriate for opening the
003411 ** snapshot. */
003412 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
003413 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
003414 ){
003415 assert( pWal->readLock>0 );
003416 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
003417 *pChanged = bChanged;
003418 }else{
003419 rc = SQLITE_ERROR_SNAPSHOT;
003420 }
003421
003422 /* A client using a non-current snapshot may not ignore any frames
003423 ** from the start of the wal file. This is because, for a system
003424 ** where (minFrame < iSnapshot < maxFrame), a checkpointer may
003425 ** have omitted to checkpoint a frame earlier than minFrame in
003426 ** the file because there exists a frame after iSnapshot that
003427 ** is the same database page. */
003428 pWal->minFrame = 1;
003429
003430 if( rc!=SQLITE_OK ){
003431 sqlite3WalEndReadTransaction(pWal);
003432 }
003433 }
003434 }
003435
003436 /* Release the shared CKPT lock obtained above. */
003437 if( ckptLock ){
003438 assert( pSnapshot );
003439 walUnlockShared(pWal, WAL_CKPT_LOCK);
003440 }
003441 #endif
003442 return rc;
003443 }
003444
003445 /*
003446 ** Begin a read transaction on the database.
003447 **
003448 ** This routine used to be called sqlite3OpenSnapshot() and with good reason:
003449 ** it takes a snapshot of the state of the WAL and wal-index for the current
003450 ** instant in time. The current thread will continue to use this snapshot.
003451 ** Other threads might append new content to the WAL and wal-index but
003452 ** that extra content is ignored by the current thread.
003453 **
003454 ** If the database contents have changes since the previous read
003455 ** transaction, then *pChanged is set to 1 before returning. The
003456 ** Pager layer will use this to know that its cache is stale and
003457 ** needs to be flushed.
003458 */
003459 int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
003460 int rc;
003461 SEH_TRY {
003462 rc = walBeginReadTransaction(pWal, pChanged);
003463 }
003464 SEH_EXCEPT( rc = walHandleException(pWal); )
003465 return rc;
003466 }
003467
003468 /*
003469 ** Finish with a read transaction. All this does is release the
003470 ** read-lock.
003471 */
003472 void sqlite3WalEndReadTransaction(Wal *pWal){
003473 sqlite3WalEndWriteTransaction(pWal);
003474 if( pWal->readLock>=0 ){
003475 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
003476 pWal->readLock = -1;
003477 }
003478 }
003479
003480 /*
003481 ** Search the wal file for page pgno. If found, set *piRead to the frame that
003482 ** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
003483 ** to zero.
003484 **
003485 ** Return SQLITE_OK if successful, or an error code if an error occurs. If an
003486 ** error does occur, the final value of *piRead is undefined.
003487 */
003488 static int walFindFrame(
003489 Wal *pWal, /* WAL handle */
003490 Pgno pgno, /* Database page number to read data for */
003491 u32 *piRead /* OUT: Frame number (or zero) */
003492 ){
003493 u32 iRead = 0; /* If !=0, WAL frame to return data from */
003494 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
003495 int iHash; /* Used to loop through N hash tables */
003496 int iMinHash;
003497
003498 /* This routine is only be called from within a read transaction. */
003499 assert( pWal->readLock>=0 || pWal->lockError );
003500
003501 /* If the "last page" field of the wal-index header snapshot is 0, then
003502 ** no data will be read from the wal under any circumstances. Return early
003503 ** in this case as an optimization. Likewise, if pWal->readLock==0,
003504 ** then the WAL is ignored by the reader so return early, as if the
003505 ** WAL were empty.
003506 */
003507 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
003508 *piRead = 0;
003509 return SQLITE_OK;
003510 }
003511
003512 /* Search the hash table or tables for an entry matching page number
003513 ** pgno. Each iteration of the following for() loop searches one
003514 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
003515 **
003516 ** This code might run concurrently to the code in walIndexAppend()
003517 ** that adds entries to the wal-index (and possibly to this hash
003518 ** table). This means the value just read from the hash
003519 ** slot (aHash[iKey]) may have been added before or after the
003520 ** current read transaction was opened. Values added after the
003521 ** read transaction was opened may have been written incorrectly -
003522 ** i.e. these slots may contain garbage data. However, we assume
003523 ** that any slots written before the current read transaction was
003524 ** opened remain unmodified.
003525 **
003526 ** For the reasons above, the if(...) condition featured in the inner
003527 ** loop of the following block is more stringent that would be required
003528 ** if we had exclusive access to the hash-table:
003529 **
003530 ** (aPgno[iFrame]==pgno):
003531 ** This condition filters out normal hash-table collisions.
003532 **
003533 ** (iFrame<=iLast):
003534 ** This condition filters out entries that were added to the hash
003535 ** table after the current read-transaction had started.
003536 */
003537 iMinHash = walFramePage(pWal->minFrame);
003538 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
003539 WalHashLoc sLoc; /* Hash table location */
003540 int iKey; /* Hash slot index */
003541 int nCollide; /* Number of hash collisions remaining */
003542 int rc; /* Error code */
003543 u32 iH;
003544
003545 rc = walHashGet(pWal, iHash, &sLoc);
003546 if( rc!=SQLITE_OK ){
003547 return rc;
003548 }
003549 nCollide = HASHTABLE_NSLOT;
003550 iKey = walHash(pgno);
003551 SEH_INJECT_FAULT;
003552 while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){
003553 u32 iFrame = iH + sLoc.iZero;
003554 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
003555 assert( iFrame>iRead || CORRUPT_DB );
003556 iRead = iFrame;
003557 }
003558 if( (nCollide--)==0 ){
003559 *piRead = 0;
003560 return SQLITE_CORRUPT_BKPT;
003561 }
003562 iKey = walNextHash(iKey);
003563 }
003564 if( iRead ) break;
003565 }
003566
003567 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
003568 /* If expensive assert() statements are available, do a linear search
003569 ** of the wal-index file content. Make sure the results agree with the
003570 ** result obtained using the hash indexes above. */
003571 {
003572 u32 iRead2 = 0;
003573 u32 iTest;
003574 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
003575 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
003576 if( walFramePgno(pWal, iTest)==pgno ){
003577 iRead2 = iTest;
003578 break;
003579 }
003580 }
003581 assert( iRead==iRead2 );
003582 }
003583 #endif
003584
003585 *piRead = iRead;
003586 return SQLITE_OK;
003587 }
003588
003589 /*
003590 ** Search the wal file for page pgno. If found, set *piRead to the frame that
003591 ** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
003592 ** to zero.
003593 **
003594 ** Return SQLITE_OK if successful, or an error code if an error occurs. If an
003595 ** error does occur, the final value of *piRead is undefined.
003596 **
003597 ** The difference between this function and walFindFrame() is that this
003598 ** function wraps walFindFrame() in an SEH_TRY{...} block.
003599 */
003600 int sqlite3WalFindFrame(
003601 Wal *pWal, /* WAL handle */
003602 Pgno pgno, /* Database page number to read data for */
003603 u32 *piRead /* OUT: Frame number (or zero) */
003604 ){
003605 int rc;
003606 SEH_TRY {
003607 rc = walFindFrame(pWal, pgno, piRead);
003608 }
003609 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; )
003610 return rc;
003611 }
003612
003613 /*
003614 ** Read the contents of frame iRead from the wal file into buffer pOut
003615 ** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
003616 ** error code otherwise.
003617 */
003618 int sqlite3WalReadFrame(
003619 Wal *pWal, /* WAL handle */
003620 u32 iRead, /* Frame to read */
003621 int nOut, /* Size of buffer pOut in bytes */
003622 u8 *pOut /* Buffer to write page data to */
003623 ){
003624 int sz;
003625 i64 iOffset;
003626 sz = pWal->hdr.szPage;
003627 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
003628 testcase( sz<=32768 );
003629 testcase( sz>=65536 );
003630 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
003631 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
003632 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
003633 }
003634
003635 /*
003636 ** Return the size of the database in pages (or zero, if unknown).
003637 */
003638 Pgno sqlite3WalDbsize(Wal *pWal){
003639 if( pWal && ALWAYS(pWal->readLock>=0) ){
003640 return pWal->hdr.nPage;
003641 }
003642 return 0;
003643 }
003644
003645
003646 /*
003647 ** This function starts a write transaction on the WAL.
003648 **
003649 ** A read transaction must have already been started by a prior call
003650 ** to sqlite3WalBeginReadTransaction().
003651 **
003652 ** If another thread or process has written into the database since
003653 ** the read transaction was started, then it is not possible for this
003654 ** thread to write as doing so would cause a fork. So this routine
003655 ** returns SQLITE_BUSY in that case and no write transaction is started.
003656 **
003657 ** There can only be a single writer active at a time.
003658 */
003659 int sqlite3WalBeginWriteTransaction(Wal *pWal){
003660 int rc;
003661
003662 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
003663 /* If the write-lock is already held, then it was obtained before the
003664 ** read-transaction was even opened, making this call a no-op.
003665 ** Return early. */
003666 if( pWal->writeLock ){
003667 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
003668 return SQLITE_OK;
003669 }
003670 #endif
003671
003672 /* Cannot start a write transaction without first holding a read
003673 ** transaction. */
003674 assert( pWal->readLock>=0 );
003675 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
003676
003677 if( pWal->readOnly ){
003678 return SQLITE_READONLY;
003679 }
003680
003681 /* Only one writer allowed at a time. Get the write lock. Return
003682 ** SQLITE_BUSY if unable.
003683 */
003684 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
003685 if( rc ){
003686 return rc;
003687 }
003688 pWal->writeLock = 1;
003689
003690 /* If another connection has written to the database file since the
003691 ** time the read transaction on this connection was started, then
003692 ** the write is disallowed.
003693 */
003694 SEH_TRY {
003695 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
003696 rc = SQLITE_BUSY_SNAPSHOT;
003697 }
003698 }
003699 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; )
003700
003701 if( rc!=SQLITE_OK ){
003702 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
003703 pWal->writeLock = 0;
003704 }
003705 return rc;
003706 }
003707
003708 /*
003709 ** End a write transaction. The commit has already been done. This
003710 ** routine merely releases the lock.
003711 */
003712 int sqlite3WalEndWriteTransaction(Wal *pWal){
003713 if( pWal->writeLock ){
003714 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
003715 pWal->writeLock = 0;
003716 pWal->iReCksum = 0;
003717 pWal->truncateOnCommit = 0;
003718 }
003719 return SQLITE_OK;
003720 }
003721
003722 /*
003723 ** If any data has been written (but not committed) to the log file, this
003724 ** function moves the write-pointer back to the start of the transaction.
003725 **
003726 ** Additionally, the callback function is invoked for each frame written
003727 ** to the WAL since the start of the transaction. If the callback returns
003728 ** other than SQLITE_OK, it is not invoked again and the error code is
003729 ** returned to the caller.
003730 **
003731 ** Otherwise, if the callback function does not return an error, this
003732 ** function returns SQLITE_OK.
003733 */
003734 int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
003735 int rc = SQLITE_OK;
003736 if( ALWAYS(pWal->writeLock) ){
003737 Pgno iMax = pWal->hdr.mxFrame;
003738 Pgno iFrame;
003739
003740 SEH_TRY {
003741 /* Restore the clients cache of the wal-index header to the state it
003742 ** was in before the client began writing to the database.
003743 */
003744 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
003745
003746 for(iFrame=pWal->hdr.mxFrame+1;
003747 ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
003748 iFrame++
003749 ){
003750 /* This call cannot fail. Unless the page for which the page number
003751 ** is passed as the second argument is (a) in the cache and
003752 ** (b) has an outstanding reference, then xUndo is either a no-op
003753 ** (if (a) is false) or simply expels the page from the cache (if (b)
003754 ** is false).
003755 **
003756 ** If the upper layer is doing a rollback, it is guaranteed that there
003757 ** are no outstanding references to any page other than page 1. And
003758 ** page 1 is never written to the log until the transaction is
003759 ** committed. As a result, the call to xUndo may not fail.
003760 */
003761 assert( walFramePgno(pWal, iFrame)!=1 );
003762 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
003763 }
003764 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
003765 }
003766 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; )
003767 }
003768 return rc;
003769 }
003770
003771 /*
003772 ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
003773 ** values. This function populates the array with values required to
003774 ** "rollback" the write position of the WAL handle back to the current
003775 ** point in the event of a savepoint rollback (via WalSavepointUndo()).
003776 */
003777 void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
003778 assert( pWal->writeLock );
003779 aWalData[0] = pWal->hdr.mxFrame;
003780 aWalData[1] = pWal->hdr.aFrameCksum[0];
003781 aWalData[2] = pWal->hdr.aFrameCksum[1];
003782 aWalData[3] = pWal->nCkpt;
003783 }
003784
003785 /*
003786 ** Move the write position of the WAL back to the point identified by
003787 ** the values in the aWalData[] array. aWalData must point to an array
003788 ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
003789 ** by a call to WalSavepoint().
003790 */
003791 int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
003792 int rc = SQLITE_OK;
003793
003794 assert( pWal->writeLock );
003795 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
003796
003797 if( aWalData[3]!=pWal->nCkpt ){
003798 /* This savepoint was opened immediately after the write-transaction
003799 ** was started. Right after that, the writer decided to wrap around
003800 ** to the start of the log. Update the savepoint values to match.
003801 */
003802 aWalData[0] = 0;
003803 aWalData[3] = pWal->nCkpt;
003804 }
003805
003806 if( aWalData[0]<pWal->hdr.mxFrame ){
003807 pWal->hdr.mxFrame = aWalData[0];
003808 pWal->hdr.aFrameCksum[0] = aWalData[1];
003809 pWal->hdr.aFrameCksum[1] = aWalData[2];
003810 SEH_TRY {
003811 walCleanupHash(pWal);
003812 }
003813 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; )
003814 }
003815
003816 return rc;
003817 }
003818
003819 /*
003820 ** This function is called just before writing a set of frames to the log
003821 ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
003822 ** to the current log file, it is possible to overwrite the start of the
003823 ** existing log file with the new frames (i.e. "reset" the log). If so,
003824 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
003825 ** unchanged.
003826 **
003827 ** SQLITE_OK is returned if no error is encountered (regardless of whether
003828 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
003829 ** if an error occurs.
003830 */
003831 static int walRestartLog(Wal *pWal){
003832 int rc = SQLITE_OK;
003833 int cnt;
003834
003835 if( pWal->readLock==0 ){
003836 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
003837 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
003838 if( pInfo->nBackfill>0 ){
003839 u32 salt1;
003840 sqlite3_randomness(4, &salt1);
003841 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
003842 if( rc==SQLITE_OK ){
003843 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
003844 ** readers are currently using the WAL), then the transactions
003845 ** frames will overwrite the start of the existing log. Update the
003846 ** wal-index header to reflect this.
003847 **
003848 ** In theory it would be Ok to update the cache of the header only
003849 ** at this point. But updating the actual wal-index header is also
003850 ** safe and means there is no special case for sqlite3WalUndo()
003851 ** to handle if this transaction is rolled back. */
003852 walRestartHdr(pWal, salt1);
003853 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
003854 }else if( rc!=SQLITE_BUSY ){
003855 return rc;
003856 }
003857 }
003858 walUnlockShared(pWal, WAL_READ_LOCK(0));
003859 pWal->readLock = -1;
003860 cnt = 0;
003861 do{
003862 int notUsed;
003863 rc = walTryBeginRead(pWal, ¬Used, 1, &cnt);
003864 }while( rc==WAL_RETRY );
003865 assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
003866 testcase( (rc&0xff)==SQLITE_IOERR );
003867 testcase( rc==SQLITE_PROTOCOL );
003868 testcase( rc==SQLITE_OK );
003869 }
003870 return rc;
003871 }
003872
003873 /*
003874 ** Information about the current state of the WAL file and where
003875 ** the next fsync should occur - passed from sqlite3WalFrames() into
003876 ** walWriteToLog().
003877 */
003878 typedef struct WalWriter {
003879 Wal *pWal; /* The complete WAL information */
003880 sqlite3_file *pFd; /* The WAL file to which we write */
003881 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
003882 int syncFlags; /* Flags for the fsync */
003883 int szPage; /* Size of one page */
003884 } WalWriter;
003885
003886 /*
003887 ** Write iAmt bytes of content into the WAL file beginning at iOffset.
003888 ** Do a sync when crossing the p->iSyncPoint boundary.
003889 **
003890 ** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
003891 ** first write the part before iSyncPoint, then sync, then write the
003892 ** rest.
003893 */
003894 static int walWriteToLog(
003895 WalWriter *p, /* WAL to write to */
003896 void *pContent, /* Content to be written */
003897 int iAmt, /* Number of bytes to write */
003898 sqlite3_int64 iOffset /* Start writing at this offset */
003899 ){
003900 int rc;
003901 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
003902 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
003903 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
003904 if( rc ) return rc;
003905 iOffset += iFirstAmt;
003906 iAmt -= iFirstAmt;
003907 pContent = (void*)(iFirstAmt + (char*)pContent);
003908 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
003909 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
003910 if( iAmt==0 || rc ) return rc;
003911 }
003912 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
003913 return rc;
003914 }
003915
003916 /*
003917 ** Write out a single frame of the WAL
003918 */
003919 static int walWriteOneFrame(
003920 WalWriter *p, /* Where to write the frame */
003921 PgHdr *pPage, /* The page of the frame to be written */
003922 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
003923 sqlite3_int64 iOffset /* Byte offset at which to write */
003924 ){
003925 int rc; /* Result code from subfunctions */
003926 void *pData; /* Data actually written */
003927 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
003928 pData = pPage->pData;
003929 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
003930 rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
003931 if( rc ) return rc;
003932 /* Write the page data */
003933 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
003934 return rc;
003935 }
003936
003937 /*
003938 ** This function is called as part of committing a transaction within which
003939 ** one or more frames have been overwritten. It updates the checksums for
003940 ** all frames written to the wal file by the current transaction starting
003941 ** with the earliest to have been overwritten.
003942 **
003943 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
003944 */
003945 static int walRewriteChecksums(Wal *pWal, u32 iLast){
003946 const int szPage = pWal->szPage;/* Database page size */
003947 int rc = SQLITE_OK; /* Return code */
003948 u8 *aBuf; /* Buffer to load data from wal file into */
003949 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
003950 u32 iRead; /* Next frame to read from wal file */
003951 i64 iCksumOff;
003952
003953 aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE);
003954 if( aBuf==0 ) return SQLITE_NOMEM_BKPT;
003955
003956 /* Find the checksum values to use as input for the recalculating the
003957 ** first checksum. If the first frame is frame 1 (implying that the current
003958 ** transaction restarted the wal file), these values must be read from the
003959 ** wal-file header. Otherwise, read them from the frame header of the
003960 ** previous frame. */
003961 assert( pWal->iReCksum>0 );
003962 if( pWal->iReCksum==1 ){
003963 iCksumOff = 24;
003964 }else{
003965 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
003966 }
003967 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
003968 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
003969 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
003970
003971 iRead = pWal->iReCksum;
003972 pWal->iReCksum = 0;
003973 for(; rc==SQLITE_OK && iRead<=iLast; iRead++){
003974 i64 iOff = walFrameOffset(iRead, szPage);
003975 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
003976 if( rc==SQLITE_OK ){
003977 u32 iPgno, nDbSize;
003978 iPgno = sqlite3Get4byte(aBuf);
003979 nDbSize = sqlite3Get4byte(&aBuf[4]);
003980
003981 walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE], aFrame);
003982 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
003983 }
003984 }
003985
003986 sqlite3_free(aBuf);
003987 return rc;
003988 }
003989
003990 /*
003991 ** Write a set of frames to the log. The caller must hold the write-lock
003992 ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
003993 */
003994 static int walFrames(
003995 Wal *pWal, /* Wal handle to write to */
003996 int szPage, /* Database page-size in bytes */
003997 PgHdr *pList, /* List of dirty pages to write */
003998 Pgno nTruncate, /* Database size after this commit */
003999 int isCommit, /* True if this is a commit */
004000 int sync_flags /* Flags to pass to OsSync() (or 0) */
004001 ){
004002 int rc; /* Used to catch return codes */
004003 u32 iFrame; /* Next frame address */
004004 PgHdr *p; /* Iterator to run through pList with. */
004005 PgHdr *pLast = 0; /* Last frame in list */
004006 int nExtra = 0; /* Number of extra copies of last page */
004007 int szFrame; /* The size of a single frame */
004008 i64 iOffset; /* Next byte to write in WAL file */
004009 WalWriter w; /* The writer */
004010 u32 iFirst = 0; /* First frame that may be overwritten */
004011 WalIndexHdr *pLive; /* Pointer to shared header */
004012
004013 assert( pList );
004014 assert( pWal->writeLock );
004015
004016 /* If this frame set completes a transaction, then nTruncate>0. If
004017 ** nTruncate==0 then this frame set does not complete the transaction. */
004018 assert( (isCommit!=0)==(nTruncate!=0) );
004019
004020 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
004021 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
004022 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
004023 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
004024 }
004025 #endif
004026
004027 pLive = (WalIndexHdr*)walIndexHdr(pWal);
004028 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
004029 iFirst = pLive->mxFrame+1;
004030 }
004031
004032 /* See if it is possible to write these frames into the start of the
004033 ** log file, instead of appending to it at pWal->hdr.mxFrame.
004034 */
004035 if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
004036 return rc;
004037 }
004038
004039 /* If this is the first frame written into the log, write the WAL
004040 ** header to the start of the WAL file. See comments at the top of
004041 ** this source file for a description of the WAL header format.
004042 */
004043 iFrame = pWal->hdr.mxFrame;
004044 if( iFrame==0 ){
004045 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
004046 u32 aCksum[2]; /* Checksum for wal-header */
004047
004048 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
004049 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
004050 sqlite3Put4byte(&aWalHdr[8], szPage);
004051 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
004052 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
004053 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
004054 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
004055 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
004056 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
004057
004058 pWal->szPage = szPage;
004059 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
004060 pWal->hdr.aFrameCksum[0] = aCksum[0];
004061 pWal->hdr.aFrameCksum[1] = aCksum[1];
004062 pWal->truncateOnCommit = 1;
004063
004064 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
004065 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
004066 if( rc!=SQLITE_OK ){
004067 return rc;
004068 }
004069
004070 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
004071 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
004072 ** an out-of-order write following a WAL restart could result in
004073 ** database corruption. See the ticket:
004074 **
004075 ** https://sqlite.org/src/info/ff5be73dee
004076 */
004077 if( pWal->syncHeader ){
004078 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
004079 if( rc ) return rc;
004080 }
004081 }
004082 if( (int)pWal->szPage!=szPage ){
004083 return SQLITE_CORRUPT_BKPT; /* TH3 test case: cov1/corrupt155.test */
004084 }
004085
004086 /* Setup information needed to write frames into the WAL */
004087 w.pWal = pWal;
004088 w.pFd = pWal->pWalFd;
004089 w.iSyncPoint = 0;
004090 w.syncFlags = sync_flags;
004091 w.szPage = szPage;
004092 iOffset = walFrameOffset(iFrame+1, szPage);
004093 szFrame = szPage + WAL_FRAME_HDRSIZE;
004094
004095 /* Write all frames into the log file exactly once */
004096 for(p=pList; p; p=p->pDirty){
004097 int nDbSize; /* 0 normally. Positive == commit flag */
004098
004099 /* Check if this page has already been written into the wal file by
004100 ** the current transaction. If so, overwrite the existing frame and
004101 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
004102 ** checksums must be recomputed when the transaction is committed. */
004103 if( iFirst && (p->pDirty || isCommit==0) ){
004104 u32 iWrite = 0;
004105 VVA_ONLY(rc =) walFindFrame(pWal, p->pgno, &iWrite);
004106 assert( rc==SQLITE_OK || iWrite==0 );
004107 if( iWrite>=iFirst ){
004108 i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
004109 void *pData;
004110 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
004111 pWal->iReCksum = iWrite;
004112 }
004113 pData = p->pData;
004114 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
004115 if( rc ) return rc;
004116 p->flags &= ~PGHDR_WAL_APPEND;
004117 continue;
004118 }
004119 }
004120
004121 iFrame++;
004122 assert( iOffset==walFrameOffset(iFrame, szPage) );
004123 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
004124 rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
004125 if( rc ) return rc;
004126 pLast = p;
004127 iOffset += szFrame;
004128 p->flags |= PGHDR_WAL_APPEND;
004129 }
004130
004131 /* Recalculate checksums within the wal file if required. */
004132 if( isCommit && pWal->iReCksum ){
004133 rc = walRewriteChecksums(pWal, iFrame);
004134 if( rc ) return rc;
004135 }
004136
004137 /* If this is the end of a transaction, then we might need to pad
004138 ** the transaction and/or sync the WAL file.
004139 **
004140 ** Padding and syncing only occur if this set of frames complete a
004141 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
004142 ** or synchronous==OFF, then no padding or syncing are needed.
004143 **
004144 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
004145 ** needed and only the sync is done. If padding is needed, then the
004146 ** final frame is repeated (with its commit mark) until the next sector
004147 ** boundary is crossed. Only the part of the WAL prior to the last
004148 ** sector boundary is synced; the part of the last frame that extends
004149 ** past the sector boundary is written after the sync.
004150 */
004151 if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){
004152 int bSync = 1;
004153 if( pWal->padToSectorBoundary ){
004154 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
004155 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
004156 bSync = (w.iSyncPoint==iOffset);
004157 testcase( bSync );
004158 while( iOffset<w.iSyncPoint ){
004159 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
004160 if( rc ) return rc;
004161 iOffset += szFrame;
004162 nExtra++;
004163 assert( pLast!=0 );
004164 }
004165 }
004166 if( bSync ){
004167 assert( rc==SQLITE_OK );
004168 rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags));
004169 }
004170 }
004171
004172 /* If this frame set completes the first transaction in the WAL and
004173 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
004174 ** journal size limit, if possible.
004175 */
004176 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
004177 i64 sz = pWal->mxWalSize;
004178 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
004179 sz = walFrameOffset(iFrame+nExtra+1, szPage);
004180 }
004181 walLimitSize(pWal, sz);
004182 pWal->truncateOnCommit = 0;
004183 }
004184
004185 /* Append data to the wal-index. It is not necessary to lock the
004186 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
004187 ** guarantees that there are no other writers, and no data that may
004188 ** be in use by existing readers is being overwritten.
004189 */
004190 iFrame = pWal->hdr.mxFrame;
004191 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
004192 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
004193 iFrame++;
004194 rc = walIndexAppend(pWal, iFrame, p->pgno);
004195 }
004196 assert( pLast!=0 || nExtra==0 );
004197 while( rc==SQLITE_OK && nExtra>0 ){
004198 iFrame++;
004199 nExtra--;
004200 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
004201 }
004202
004203 if( rc==SQLITE_OK ){
004204 /* Update the private copy of the header. */
004205 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
004206 testcase( szPage<=32768 );
004207 testcase( szPage>=65536 );
004208 pWal->hdr.mxFrame = iFrame;
004209 if( isCommit ){
004210 pWal->hdr.iChange++;
004211 pWal->hdr.nPage = nTruncate;
004212 }
004213 /* If this is a commit, update the wal-index header too. */
004214 if( isCommit ){
004215 walIndexWriteHdr(pWal);
004216 pWal->iCallback = iFrame;
004217 }
004218 }
004219
004220 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
004221 return rc;
004222 }
004223
004224 /*
004225 ** Write a set of frames to the log. The caller must hold the write-lock
004226 ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
004227 **
004228 ** The difference between this function and walFrames() is that this
004229 ** function wraps walFrames() in an SEH_TRY{...} block.
004230 */
004231 int sqlite3WalFrames(
004232 Wal *pWal, /* Wal handle to write to */
004233 int szPage, /* Database page-size in bytes */
004234 PgHdr *pList, /* List of dirty pages to write */
004235 Pgno nTruncate, /* Database size after this commit */
004236 int isCommit, /* True if this is a commit */
004237 int sync_flags /* Flags to pass to OsSync() (or 0) */
004238 ){
004239 int rc;
004240 SEH_TRY {
004241 rc = walFrames(pWal, szPage, pList, nTruncate, isCommit, sync_flags);
004242 }
004243 SEH_EXCEPT( rc = walHandleException(pWal); )
004244 return rc;
004245 }
004246
004247 /*
004248 ** This routine is called to implement sqlite3_wal_checkpoint() and
004249 ** related interfaces.
004250 **
004251 ** Obtain a CHECKPOINT lock and then backfill as much information as
004252 ** we can from WAL into the database.
004253 **
004254 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
004255 ** callback. In this case this function runs a blocking checkpoint.
004256 */
004257 int sqlite3WalCheckpoint(
004258 Wal *pWal, /* Wal connection */
004259 sqlite3 *db, /* Check this handle's interrupt flag */
004260 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
004261 int (*xBusy)(void*), /* Function to call when busy */
004262 void *pBusyArg, /* Context argument for xBusyHandler */
004263 int sync_flags, /* Flags to sync db file with (or 0) */
004264 int nBuf, /* Size of temporary buffer */
004265 u8 *zBuf, /* Temporary buffer to use */
004266 int *pnLog, /* OUT: Number of frames in WAL */
004267 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
004268 ){
004269 int rc; /* Return code */
004270 int isChanged = 0; /* True if a new wal-index header is loaded */
004271 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
004272 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
004273
004274 assert( pWal->ckptLock==0 );
004275 assert( pWal->writeLock==0 );
004276
004277 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
004278 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
004279 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
004280
004281 if( pWal->readOnly ) return SQLITE_READONLY;
004282 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
004283
004284 /* Enable blocking locks, if possible. */
004285 sqlite3WalDb(pWal, db);
004286 if( xBusy2 ) (void)walEnableBlocking(pWal);
004287
004288 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
004289 ** "checkpoint" lock on the database file.
004290 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
004291 ** checkpoint operation at the same time, the lock cannot be obtained and
004292 ** SQLITE_BUSY is returned.
004293 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
004294 ** it will not be invoked in this case.
004295 */
004296 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
004297 testcase( rc==SQLITE_BUSY );
004298 testcase( rc!=SQLITE_OK && xBusy2!=0 );
004299 if( rc==SQLITE_OK ){
004300 pWal->ckptLock = 1;
004301
004302 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
004303 ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
004304 ** file.
004305 **
004306 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
004307 ** immediately, and a busy-handler is configured, it is invoked and the
004308 ** writer lock retried until either the busy-handler returns 0 or the
004309 ** lock is successfully obtained.
004310 */
004311 if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
004312 rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, 1);
004313 if( rc==SQLITE_OK ){
004314 pWal->writeLock = 1;
004315 }else if( rc==SQLITE_BUSY ){
004316 eMode2 = SQLITE_CHECKPOINT_PASSIVE;
004317 xBusy2 = 0;
004318 rc = SQLITE_OK;
004319 }
004320 }
004321 }
004322
004323
004324 /* Read the wal-index header. */
004325 SEH_TRY {
004326 if( rc==SQLITE_OK ){
004327 /* For a passive checkpoint, do not re-enable blocking locks after
004328 ** reading the wal-index header. A passive checkpoint should not block
004329 ** or invoke the busy handler. The only lock such a checkpoint may
004330 ** attempt to obtain is a lock on a read-slot, and it should give up
004331 ** immediately and do a partial checkpoint if it cannot obtain it. */
004332 walDisableBlocking(pWal);
004333 rc = walIndexReadHdr(pWal, &isChanged);
004334 if( eMode2!=SQLITE_CHECKPOINT_PASSIVE ) (void)walEnableBlocking(pWal);
004335 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
004336 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
004337 }
004338 }
004339
004340 /* Copy data from the log to the database file. */
004341 if( rc==SQLITE_OK ){
004342 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
004343 rc = SQLITE_CORRUPT_BKPT;
004344 }else{
004345 rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags,zBuf);
004346 }
004347
004348 /* If no error occurred, set the output variables. */
004349 if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
004350 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
004351 SEH_INJECT_FAULT;
004352 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
004353 }
004354 }
004355 }
004356 SEH_EXCEPT( rc = walHandleException(pWal); )
004357
004358 if( isChanged ){
004359 /* If a new wal-index header was loaded before the checkpoint was
004360 ** performed, then the pager-cache associated with pWal is now
004361 ** out of date. So zero the cached wal-index header to ensure that
004362 ** next time the pager opens a snapshot on this database it knows that
004363 ** the cache needs to be reset.
004364 */
004365 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
004366 }
004367
004368 walDisableBlocking(pWal);
004369 sqlite3WalDb(pWal, 0);
004370
004371 /* Release the locks. */
004372 sqlite3WalEndWriteTransaction(pWal);
004373 if( pWal->ckptLock ){
004374 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
004375 pWal->ckptLock = 0;
004376 }
004377 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
004378 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
004379 if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
004380 #endif
004381 return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
004382 }
004383
004384 /* Return the value to pass to a sqlite3_wal_hook callback, the
004385 ** number of frames in the WAL at the point of the last commit since
004386 ** sqlite3WalCallback() was called. If no commits have occurred since
004387 ** the last call, then return 0.
004388 */
004389 int sqlite3WalCallback(Wal *pWal){
004390 u32 ret = 0;
004391 if( pWal ){
004392 ret = pWal->iCallback;
004393 pWal->iCallback = 0;
004394 }
004395 return (int)ret;
004396 }
004397
004398 /*
004399 ** This function is called to change the WAL subsystem into or out
004400 ** of locking_mode=EXCLUSIVE.
004401 **
004402 ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
004403 ** into locking_mode=NORMAL. This means that we must acquire a lock
004404 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
004405 ** or if the acquisition of the lock fails, then return 0. If the
004406 ** transition out of exclusive-mode is successful, return 1. This
004407 ** operation must occur while the pager is still holding the exclusive
004408 ** lock on the main database file.
004409 **
004410 ** If op is one, then change from locking_mode=NORMAL into
004411 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
004412 ** be released. Return 1 if the transition is made and 0 if the
004413 ** WAL is already in exclusive-locking mode - meaning that this
004414 ** routine is a no-op. The pager must already hold the exclusive lock
004415 ** on the main database file before invoking this operation.
004416 **
004417 ** If op is negative, then do a dry-run of the op==1 case but do
004418 ** not actually change anything. The pager uses this to see if it
004419 ** should acquire the database exclusive lock prior to invoking
004420 ** the op==1 case.
004421 */
004422 int sqlite3WalExclusiveMode(Wal *pWal, int op){
004423 int rc;
004424 assert( pWal->writeLock==0 );
004425 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
004426
004427 /* pWal->readLock is usually set, but might be -1 if there was a
004428 ** prior error while attempting to acquire are read-lock. This cannot
004429 ** happen if the connection is actually in exclusive mode (as no xShmLock
004430 ** locks are taken in this case). Nor should the pager attempt to
004431 ** upgrade to exclusive-mode following such an error.
004432 */
004433 #ifndef SQLITE_USE_SEH
004434 assert( pWal->readLock>=0 || pWal->lockError );
004435 #endif
004436 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
004437
004438 if( op==0 ){
004439 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
004440 pWal->exclusiveMode = WAL_NORMAL_MODE;
004441 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
004442 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
004443 }
004444 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
004445 }else{
004446 /* Already in locking_mode=NORMAL */
004447 rc = 0;
004448 }
004449 }else if( op>0 ){
004450 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
004451 assert( pWal->readLock>=0 );
004452 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
004453 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
004454 rc = 1;
004455 }else{
004456 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
004457 }
004458 return rc;
004459 }
004460
004461 /*
004462 ** Return true if the argument is non-NULL and the WAL module is using
004463 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
004464 ** WAL module is using shared-memory, return false.
004465 */
004466 int sqlite3WalHeapMemory(Wal *pWal){
004467 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
004468 }
004469
004470 #ifdef SQLITE_ENABLE_SNAPSHOT
004471 /* Create a snapshot object. The content of a snapshot is opaque to
004472 ** every other subsystem, so the WAL module can put whatever it needs
004473 ** in the object.
004474 */
004475 int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
004476 int rc = SQLITE_OK;
004477 WalIndexHdr *pRet;
004478 static const u32 aZero[4] = { 0, 0, 0, 0 };
004479
004480 assert( pWal->readLock>=0 && pWal->writeLock==0 );
004481
004482 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
004483 *ppSnapshot = 0;
004484 return SQLITE_ERROR;
004485 }
004486 pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
004487 if( pRet==0 ){
004488 rc = SQLITE_NOMEM_BKPT;
004489 }else{
004490 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
004491 *ppSnapshot = (sqlite3_snapshot*)pRet;
004492 }
004493
004494 return rc;
004495 }
004496
004497 /* Try to open on pSnapshot when the next read-transaction starts
004498 */
004499 void sqlite3WalSnapshotOpen(
004500 Wal *pWal,
004501 sqlite3_snapshot *pSnapshot
004502 ){
004503 if( pSnapshot && ((WalIndexHdr*)pSnapshot)->iVersion==0 ){
004504 /* iVersion==0 means that this is a call to sqlite3_snapshot_get(). In
004505 ** this case set the bGetSnapshot flag so that if the call to
004506 ** sqlite3_snapshot_get() is about to read transaction on this wal
004507 ** file, it does not take read-lock 0 if the wal file has been completely
004508 ** checkpointed. Taking read-lock 0 would work, but then it would be
004509 ** possible for a subsequent writer to destroy the snapshot even while
004510 ** this connection is holding its read-transaction open. This is contrary
004511 ** to user expectations, so we avoid it by not taking read-lock 0. */
004512 pWal->bGetSnapshot = 1;
004513 }else{
004514 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
004515 pWal->bGetSnapshot = 0;
004516 }
004517 }
004518
004519 /*
004520 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
004521 ** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
004522 */
004523 int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
004524 WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
004525 WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
004526
004527 /* aSalt[0] is a copy of the value stored in the wal file header. It
004528 ** is incremented each time the wal file is restarted. */
004529 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
004530 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
004531 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
004532 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
004533 return 0;
004534 }
004535
004536 /*
004537 ** The caller currently has a read transaction open on the database.
004538 ** This function takes a SHARED lock on the CHECKPOINTER slot and then
004539 ** checks if the snapshot passed as the second argument is still
004540 ** available. If so, SQLITE_OK is returned.
004541 **
004542 ** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
004543 ** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
004544 ** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
004545 ** lock is released before returning.
004546 */
004547 int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
004548 int rc;
004549 SEH_TRY {
004550 rc = walLockShared(pWal, WAL_CKPT_LOCK);
004551 if( rc==SQLITE_OK ){
004552 WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
004553 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
004554 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
004555 ){
004556 rc = SQLITE_ERROR_SNAPSHOT;
004557 walUnlockShared(pWal, WAL_CKPT_LOCK);
004558 }
004559 }
004560 }
004561 SEH_EXCEPT( rc = walHandleException(pWal); )
004562 return rc;
004563 }
004564
004565 /*
004566 ** Release a lock obtained by an earlier successful call to
004567 ** sqlite3WalSnapshotCheck().
004568 */
004569 void sqlite3WalSnapshotUnlock(Wal *pWal){
004570 assert( pWal );
004571 walUnlockShared(pWal, WAL_CKPT_LOCK);
004572 }
004573
004574
004575 #endif /* SQLITE_ENABLE_SNAPSHOT */
004576
004577 #ifdef SQLITE_ENABLE_ZIPVFS
004578 /*
004579 ** If the argument is not NULL, it points to a Wal object that holds a
004580 ** read-lock. This function returns the database page-size if it is known,
004581 ** or zero if it is not (or if pWal is NULL).
004582 */
004583 int sqlite3WalFramesize(Wal *pWal){
004584 assert( pWal==0 || pWal->readLock>=0 );
004585 return (pWal ? pWal->szPage : 0);
004586 }
004587 #endif
004588
004589 /* Return the sqlite3_file object for the WAL file
004590 */
004591 sqlite3_file *sqlite3WalFile(Wal *pWal){
004592 return pWal->pWalFd;
004593 }
004594
004595 #endif /* #ifndef SQLITE_OMIT_WAL */