blob: 82d2a6eadf091c758ba9ffd93d2b1e2ae23c6ec5 [file] [log] [blame]
drhed7c8552001-04-11 14:29:21 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhed7c8552001-04-11 14:29:21 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhed7c8552001-04-11 14:29:21 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drhed7c8552001-04-11 14:29:21 +000010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:26 +000012** This is the implementation of the page cache subsystem or "pager".
drhed7c8552001-04-11 14:29:21 +000013**
drhb19a2bc2001-09-16 00:13:26 +000014** The pager is used to access a database disk file. It implements
15** atomic commit and rollback through the use of a journal file that
16** is separate from the database file. The pager also implements file
17** locking to prevent two processes from writing the same database
18** file simultaneously, or one process from reading the database while
19** another is writing.
drhed7c8552001-04-11 14:29:21 +000020**
danielk1977443c0592009-01-16 15:21:05 +000021** @(#) $Id: pager.c,v 1.552 2009/01/16 15:21:06 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025
26/*
drhdb48ee02003-01-16 13:42:43 +000027** Macros for troubleshooting. Normally turned off
28*/
danielk1977466be562004-06-10 02:16:01 +000029#if 0
danielk1977f2c31ad2009-01-06 13:40:08 +000030int sqlite3PagerTrace=1; /* True to enable tracing */
drhd3627af2006-12-18 18:34:51 +000031#define sqlite3DebugPrintf printf
drh30d53702009-01-06 15:58:57 +000032#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
drhdb48ee02003-01-16 13:42:43 +000033#else
drh30d53702009-01-06 15:58:57 +000034#define PAGERTRACE(X)
drhdb48ee02003-01-16 13:42:43 +000035#endif
36
danielk1977599fcba2004-11-08 07:13:13 +000037/*
drh30d53702009-01-06 15:58:57 +000038** The following two macros are used within the PAGERTRACE() macros above
drhd86959f2005-11-26 03:51:18 +000039** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000040**
drh85b623f2007-12-13 21:54:09 +000041** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000042** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000043** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000044*/
drhc001c582006-03-06 18:23:16 +000045#define PAGERID(p) ((int)(p->fd))
46#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000047
48/*
drhed7c8552001-04-11 14:29:21 +000049** The page cache as a whole is always in one of the following
50** states:
51**
drha6abd042004-06-09 17:37:22 +000052** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000053** writing the database file. There is no
54** data held in memory. This is the initial
55** state.
56**
drha6abd042004-06-09 17:37:22 +000057** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000058** Writing is not permitted. There can be
59** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000060** file at the same time.
drhed7c8552001-04-11 14:29:21 +000061**
drh726de592004-06-10 23:35:50 +000062** PAGER_RESERVED This process has reserved the database for writing
63** but has not yet made any changes. Only one process
64** at a time can reserve the database. The original
65** database file has not been modified so other
66** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000067** database file.
68**
69** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000070** Access is exclusive. No other processes or
71** threads can be reading or writing while one
72** process is writing.
73**
danielk1977aa5ccdf2004-06-14 05:10:42 +000074** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
75** after all dirty pages have been written to the
76** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000077** disk. All that remains to do is to remove or
78** truncate the journal file and the transaction
79** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000080**
drha6abd042004-06-09 17:37:22 +000081** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000082** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000083** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000084** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000085** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000086** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000087** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000088** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000089** PAGER_RESERVED means that there is an open rollback journal.
90** The transition to PAGER_EXCLUSIVE occurs before any changes
91** are made to the database file, though writes to the rollback
92** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
93** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
94** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +000095*/
drha6abd042004-06-09 17:37:22 +000096#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +000097#define PAGER_SHARED 1 /* same as SHARED_LOCK */
98#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
99#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
100#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000101
drh684917c2004-10-05 02:41:42 +0000102/*
drh887dc4c2004-10-22 16:22:57 +0000103** This macro rounds values up so that if the value is an address it
104** is guaranteed to be an address that is aligned to an 8-byte boundary.
105*/
106#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
107
drh9eb9e262004-02-11 02:18:05 +0000108/*
109** A macro used for invoking the codec if there is one
110*/
111#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000112# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
113# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000114#else
drhc001c582006-03-06 18:23:16 +0000115# define CODEC1(P,D,N,X) /* NO-OP */
116# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000117#endif
118
drhed7c8552001-04-11 14:29:21 +0000119/*
danielk19777cbd5892009-01-10 16:15:09 +0000120** The maximum allowed sector size. 16MB. If the xSectorsize() method
121** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
122** This could conceivably cause corruption following a power failure on
123** such a system. This is currently an undocumented limit.
124*/
125#define MAX_SECTOR_SIZE 0x0100000
126
127/*
danielk1977fd7f0452008-12-17 17:30:26 +0000128** An instance of the following structure is allocated for each active
129** savepoint and statement transaction in the system. All such structures
130** are stored in the Pager.aSavepoint[] array, which is allocated and
131** resized using sqlite3Realloc().
132**
133** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
134** set to 0. If a journal-header is written into the main journal while
135** the savepoint is active, then iHdrOffset is set to the byte offset
136** immediately following the last journal record written into the main
137** journal before the journal-header. This is required during savepoint
138** rollback (see pagerPlaybackSavepoint()).
139*/
140typedef struct PagerSavepoint PagerSavepoint;
141struct PagerSavepoint {
142 i64 iOffset; /* Starting offset in main journal */
143 i64 iHdrOffset; /* See above */
144 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
145 Pgno nOrig; /* Original number of pages in file */
146 Pgno iSubRec; /* Index of first record in sub-journal */
147};
148
149/*
drhed7c8552001-04-11 14:29:21 +0000150** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000151**
danielk1977443c0592009-01-16 15:21:05 +0000152** errCode
danielk19773460d192008-12-27 15:23:13 +0000153**
danielk1977443c0592009-01-16 15:21:05 +0000154** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
155** or SQLITE_FULL. Once one of the first three errors occurs, it persists
156** and is returned as the result of every major pager API call. The
157** SQLITE_FULL return code is slightly different. It persists only until the
158** next successful rollback is performed on the pager cache. Also,
159** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
160** APIs, they may still be used successfully.
161**
162** dbSizeValid, dbSize, dbOrigSize, dbFileSize
163**
164** Managing the size of the database file in pages is a little complicated.
165** The variable Pager.dbSize contains the number of pages that the database
166** image currently contains. As the database image grows or shrinks this
167** variable is updated. The variable Pager.dbFileSize contains the number
168** of pages in the database file. This may be different from Pager.dbSize
169** if some pages have been appended to the database image but not yet written
170** out from the cache to the actual file on disk. Or if the image has been
171** truncated by an incremental-vacuum operation. The Pager.dbOrigSize variable
172** contains the number of pages in the database image when the current
173** transaction was opened. The contents of all three of these variables is
174** only guaranteed to be correct if the boolean Pager.dbSizeValid is true.
175**
176** TODO: Under what conditions is dbSizeValid set? Cleared?
177**
178** changeCountDone
179**
180** This boolean variable is used to make sure that the change-counter
181** (the 4-byte header field at byte offset 24 of the database file) is
182** not updated more often than necessary.
183**
184** It is set to true when the change-counter field is updated, which
185** can only happen if an exclusive lock is held on the database file.
186** It is cleared (set to false) whenever an exclusive lock is
187** relinquished on the database file. Each time a transaction is committed,
188** The changeCountDone flag is inspected. If it is true, the work of
189** updating the change-counter is omitted for the current transaction.
190**
191** This mechanism means that when running in exclusive mode, a connection
192** need only update the change-counter once, for the first transaction
193** committed.
194**
195** dbModified
196**
197** The dbModified flag is set whenever a database page is dirtied.
198** It is cleared at the end of each transaction.
199**
200** It is used when committing or otherwise ending a transaction. If
201** the dbModified flag is clear then less work has to be done.
202**
203** TODO: Check some of the logic surrounding this optimization.
204**
205** journalStarted
206**
207** This flag is set whenever the the main journal is synced.
208**
209** The point of this flag is that it must be set after the
210** first journal header in a journal file has been synced to disk.
211** After this has happened, new pages appended to the database
212** do not need the PGHDR_NEED_SYNC flag set, as they do not need
213** to wait for a journal sync before they can be written out to
214** the database file (see function pager_write()).
215**
216** setMaster
217**
218** This variable is used to ensure that the master journal file name
219** (if any) is only written into the journal file once.
220**
221** When committing a transaction, the master journal file name (if any)
222** may be written into the journal file while the pager is still in
223** PAGER_RESERVED state (see CommitPhaseOne() for the action). It
224** then attempts to upgrade to an exclusive lock. If this attempt
225** fails, then SQLITE_BUSY may be returned to the user and the user
226** may attempt to commit the transaction again later (calling
227** CommitPhaseOne() again). This flag is used to ensure that the
228** master journal name is only written to the journal file the first
229** time CommitPhaseOne() is called.
230**
231** doNotSync
232**
233** This variable is set and cleared by sqlite3PagerWrite().
234**
235** needSync
236**
237** TODO: It might be easier to set this variable in writeJournalHdr()
238** and writeMasterJournal() only. Change its meaning to "unsynced data
239** has been written to the journal".
drhed7c8552001-04-11 14:29:21 +0000240*/
241struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000242 sqlite3_vfs *pVfs; /* OS functions to use for IO */
danielk1977443c0592009-01-16 15:21:05 +0000243 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
244 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drh34e79ce2004-02-08 06:05:46 +0000245 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000246 u8 noReadlock; /* Do not bother to obtain readlocks */
drh603240c2002-03-05 01:11:12 +0000247 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000248 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000249 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drh603240c2002-03-05 01:11:12 +0000250 u8 tempFile; /* zFilename is a temporary file */
251 u8 readOnly; /* True for a read-only database */
drhac69b052004-05-12 13:30:07 +0000252 u8 memDb; /* True to inhibit all file I/O */
danielk1977443c0592009-01-16 15:21:05 +0000253
254 /* The following block contains those class members that are dynamically
255 ** modified during normal operations. The other variables in this structure
256 ** are either constant throughout the lifetime of the pager, or else
257 ** used to store configuration parameters that affect the way the pager
258 ** operates.
259 **
260 ** The 'state' variable is described in more detail along with the
261 ** descriptions of the values it may take - PAGER_UNLOCK etc. Many of the
262 ** other variables in this block are described in the comment directly
263 ** above this class definition.
264 */
265 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
266 u8 dbModified; /* True if there are any changes to the Db */
267 u8 needSync; /* True if an fsync() is needed on the journal */
268 u8 journalStarted; /* True if header of journal is synced */
269 u8 changeCountDone; /* Set after incrementing the change-counter */
drh6d156e42005-05-20 20:11:20 +0000270 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000271 u8 doNotSync; /* Boolean. While true, do not spill the cache */
danielk1977d92db532008-11-17 04:56:24 +0000272 u8 dbSizeValid; /* Set when dbSize is correct */
danielk19773460d192008-12-27 15:23:13 +0000273 Pgno dbSize; /* Number of pages in the database */
274 Pgno dbOrigSize; /* dbSize before the current transaction */
275 Pgno dbFileSize; /* Number of pages in the database file */
drhe49f9822006-09-15 12:29:16 +0000276 int errCode; /* One of several kinds of errors */
danielk1977443c0592009-01-16 15:21:05 +0000277 int nRec; /* Pages journalled since last j-header written */
drhfcd35c72005-05-21 02:48:08 +0000278 u32 cksumInit; /* Quasi-random value added to every checksum */
danielk1977443c0592009-01-16 15:21:05 +0000279 u32 nSubRec; /* Number of records written to sub-journal */
drhf5e7bb52008-02-18 14:47:33 +0000280 Bitvec *pInJournal; /* One bit for each page in the database file */
danielk1977443c0592009-01-16 15:21:05 +0000281 sqlite3_file *fd; /* File descriptor for database */
282 sqlite3_file *jfd; /* File descriptor for main journal */
283 sqlite3_file *sjfd; /* File descriptor for sub-journal */
284 i64 journalOff; /* Current write offset in the journal file */
285 i64 journalHdr; /* Byte offset to previous journal header */
286 PagerSavepoint *aSavepoint; /* Array of active savepoints */
287 int nSavepoint; /* Number of elements in aSavepoint[] */
288 char dbFileVers[16]; /* Changes whenever database file changes */
289 u32 sectorSize; /* Assumed sector size during rollback */
290
291 int nExtra; /* Add this many bytes to each in-memory page */
292 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
293 int pageSize; /* Number of bytes in a page */
294 Pgno mxPgno; /* Maximum allowed size of the database */
drhfcd35c72005-05-21 02:48:08 +0000295 char *zFilename; /* Name of the database file */
296 char *zJournal; /* Name of the journal file */
danielk19771ceedd32008-11-19 10:22:33 +0000297 int (*xBusyHandler)(void*); /* Function to call when busy */
298 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
drhfcd35c72005-05-21 02:48:08 +0000299#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000300 int nHit, nMiss; /* Cache hits and missing */
301 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000302#endif
danielk1977eaa06f62008-09-18 17:34:44 +0000303 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000304#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000305 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000306 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000307#endif
danielk19778186df82007-03-06 13:45:59 +0000308 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
danielk1977b53e4962008-06-04 06:45:59 +0000309 i64 journalSizeLimit; /* Size limit for persistent journal files */
danielk19778c0a7912008-08-20 14:49:23 +0000310 PCache *pPCache; /* Pointer to page cache object */
drhd9b02572001-04-15 00:37:09 +0000311};
312
313/*
drh538f5702007-04-13 02:14:30 +0000314** The following global variables hold counters used for
315** testing purposes only. These variables do not exist in
316** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000317*/
318#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000319int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
320int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
321int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
drh538f5702007-04-13 02:14:30 +0000322# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000323#else
drh538f5702007-04-13 02:14:30 +0000324# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000325#endif
326
drh538f5702007-04-13 02:14:30 +0000327
328
drhfcd35c72005-05-21 02:48:08 +0000329/*
drh5e00f6c2001-09-13 13:46:56 +0000330** Journal files begin with the following magic string. The data
331** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000332**
drhae2b40c2004-06-09 19:03:54 +0000333** Since version 2.8.0, the journal format contains additional sanity
drh30d53702009-01-06 15:58:57 +0000334** checking information. If the power fails while the journal is being
drhae2b40c2004-06-09 19:03:54 +0000335** written, semi-random garbage data might appear in the journal
336** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000337** to roll the journal back, the database could be corrupted. The additional
338** sanity checking data is an attempt to discover the garbage in the
339** journal and ignore it.
340**
drhae2b40c2004-06-09 19:03:54 +0000341** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000342** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000343** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000344** This cksum is initialized to a 32-bit random value that appears in the
345** journal file right after the header. The random initializer is important,
346** because garbage data that appears at the end of a journal is likely
347** data that was once in other files that have now been deleted. If the
348** garbage data came from an obsolete journal file, the checksums might
349** be correct. But by initializing the checksum to random value which
350** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000351*/
drhae2b40c2004-06-09 19:03:54 +0000352static const unsigned char aJournalMagic[] = {
353 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000354};
355
356/*
danielk1977443c0592009-01-16 15:21:05 +0000357** The size of the of each page record in the journal is given by
358** the following macro.
drh968af522003-02-11 14:55:40 +0000359*/
drhae2b40c2004-06-09 19:03:54 +0000360#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000361
danielk197776572402004-06-25 02:38:54 +0000362/*
danielk1977443c0592009-01-16 15:21:05 +0000363** The journal header size for this pager. This is usually the same
364** size as a single disk sector. See also setSectorSize().
danielk197776572402004-06-25 02:38:54 +0000365*/
366#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
367
drhb7f91642004-10-31 02:22:47 +0000368/*
369** The macro MEMDB is true if we are dealing with an in-memory database.
370** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
371** the value of MEMDB will be a constant and the compiler will optimize
372** out code that would never execute.
373*/
374#ifdef SQLITE_OMIT_MEMORYDB
375# define MEMDB 0
376#else
377# define MEMDB pPager->memDb
378#endif
379
380/*
danielk197726836652005-01-17 01:33:13 +0000381** The maximum legal page number is (2^31 - 1).
382*/
383#define PAGER_MAX_PGNO 2147483647
384
385/*
danielk19773460d192008-12-27 15:23:13 +0000386** Return true if it is necessary to write page *pPg into the sub-journal.
387** A page needs to be written into the sub-journal if there exists one
388** or more open savepoints for which:
danielk1977fd7f0452008-12-17 17:30:26 +0000389**
danielk19773460d192008-12-27 15:23:13 +0000390** * The page-number is less than or equal to PagerSavepoint.nOrig, and
391** * The bit corresponding to the page-number is not set in
392** PagerSavepoint.pInSavepoint.
danielk1977f35843b2007-04-07 15:03:17 +0000393*/
danielk19773460d192008-12-27 15:23:13 +0000394static int subjRequiresPage(PgHdr *pPg){
395 Pgno pgno = pPg->pgno;
danielk1977f35843b2007-04-07 15:03:17 +0000396 Pager *pPager = pPg->pPager;
danielk19773460d192008-12-27 15:23:13 +0000397 int i;
398 for(i=0; i<pPager->nSavepoint; i++){
399 PagerSavepoint *p = &pPager->aSavepoint[i];
400 if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
401 return 1;
402 }
danielk1977fd7f0452008-12-17 17:30:26 +0000403 }
danielk19773460d192008-12-27 15:23:13 +0000404 return 0;
danielk1977f35843b2007-04-07 15:03:17 +0000405}
drh8ca0c722006-05-07 17:49:38 +0000406
danielk19773460d192008-12-27 15:23:13 +0000407/*
408** Return true if the page is already in the journal file.
409*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000410static int pageInJournal(PgHdr *pPg){
411 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
412}
413
drh8ca0c722006-05-07 17:49:38 +0000414/*
drh34e79ce2004-02-08 06:05:46 +0000415** Read a 32-bit integer from the given file descriptor. Store the integer
416** that is read in *pRes. Return SQLITE_OK if everything worked, or an
417** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000418**
419** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000420*/
danielk197762079062007-08-15 17:08:46 +0000421static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000422 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000423 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000424 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000425 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000426 }
drh94f33312002-08-12 12:29:56 +0000427 return rc;
428}
429
430/*
drh97b57482006-01-10 20:32:32 +0000431** Write a 32-bit integer into a string buffer in big-endian byte order.
432*/
drha3152892007-05-05 11:48:52 +0000433#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000434
435/*
drh34e79ce2004-02-08 06:05:46 +0000436** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
437** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000438*/
danielk197762079062007-08-15 17:08:46 +0000439static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000440 char ac[4];
drh97b57482006-01-10 20:32:32 +0000441 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000442 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000443}
444
drh2554f8b2003-01-22 01:26:44 +0000445/*
danielk1977443c0592009-01-16 15:21:05 +0000446** The argument to this macro is a file descriptor (type sqlite3_file*).
447** Return 0 if it is not open, or non-zero (but not 1) if it is.
448**
449** This is so that expressions can be written as:
450**
451** if( isOpen(pPager->jfd) ){ ...
452**
453** instead of
454**
455** if( pPager->jfd->pMethods ){ ...
456*/
457#define isOpen(pFd) ((pFd)->pMethods)
458
459/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000460** If file pFd is open, call sqlite3OsUnlock() on it.
461*/
462static int osUnlock(sqlite3_file *pFd, int eLock){
danielk1977443c0592009-01-16 15:21:05 +0000463 if( !isOpen(pFd) ){
danielk19777a2b1ee2007-08-21 14:27:01 +0000464 return SQLITE_OK;
465 }
466 return sqlite3OsUnlock(pFd, eLock);
467}
468
469/*
danielk1977c7b60172007-08-22 11:22:03 +0000470** This function determines whether or not the atomic-write optimization
471** can be used with this pager. The optimization can be used if:
472**
473** (a) the value returned by OsDeviceCharacteristics() indicates that
474** a database page may be written atomically, and
475** (b) the value returned by OsSectorSize() is less than or equal
476** to the page size.
477**
danielk1977443c0592009-01-16 15:21:05 +0000478** The optimization is also always enabled for temporary files. It is
479** an error to call this function if pPager is opened on an in-memory
480** database.
481**
danielk1977c7b60172007-08-22 11:22:03 +0000482** If the optimization cannot be used, 0 is returned. If it can be used,
483** then the value returned is the size of the journal file when it
484** contains rollback data for exactly one page.
485*/
486#ifdef SQLITE_ENABLE_ATOMIC_WRITE
487static int jrnlBufferSize(Pager *pPager){
danielk1977443c0592009-01-16 15:21:05 +0000488 assert( !MEMDB );
489 if( !pPager->tempFile ){
490 int dc; /* Device characteristics */
491 int nSector; /* Sector size */
492 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000493
danielk1977443c0592009-01-16 15:21:05 +0000494 assert( isOpen(pPager->fd) );
495 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk19777cbd5892009-01-10 16:15:09 +0000496 nSector = pPager->sectorSize;
drhfacf0302008-06-17 15:12:00 +0000497 szPage = pPager->pageSize;
danielk1977443c0592009-01-16 15:21:05 +0000498
499 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
500 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
501 if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
502 return 0;
503 }
danielk1977c7b60172007-08-22 11:22:03 +0000504 }
505
danielk1977443c0592009-01-16 15:21:05 +0000506 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
danielk1977c7b60172007-08-22 11:22:03 +0000507}
508#endif
509
510/*
danielk1977443c0592009-01-16 15:21:05 +0000511** This function should be called when an IOERR, CORRUPT or FULL error
512** may have occured. The first argument is a pointer to the pager
513** structure, the second the error-code about to be returned by a pager
514** API function. The value returned is a copy of the second argument
515** to this function.
danielk1977a96a7102006-01-16 12:46:41 +0000516**
drh4f0ee682007-03-30 20:43:40 +0000517** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000518** the error becomes persistent. Until the persisten error is cleared,
519** subsequent API calls on this Pager will immediately return the same
520** error code.
521**
522** A persistent error indicates that the contents of the pager-cache
523** cannot be trusted. This state can be cleared by completely discarding
524** the contents of the pager-cache. If a transaction was active when
525** the persistent error occured, then the rollback journal may need
danielk1977443c0592009-01-16 15:21:05 +0000526** to be replayed to restore the contents of the database file (as if
527** it were a hot-journal).
danielk1977aef0bf62005-12-30 16:28:01 +0000528*/
danielk1977ae72d982007-10-03 08:46:44 +0000529static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000530static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000531 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000532 assert(
533 pPager->errCode==SQLITE_FULL ||
534 pPager->errCode==SQLITE_OK ||
535 (pPager->errCode & 0xff)==SQLITE_IOERR
536 );
danielk1977979f38e2007-03-27 16:19:51 +0000537 if(
drh4ac285a2006-09-15 07:28:50 +0000538 rc2==SQLITE_FULL ||
539 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000540 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000541 ){
542 pPager->errCode = rc;
danielk19778c0a7912008-08-20 14:49:23 +0000543 if( pPager->state==PAGER_UNLOCK
544 && sqlite3PcacheRefCount(pPager->pPCache)==0
545 ){
danielk1977ae72d982007-10-03 08:46:44 +0000546 /* If the pager is already unlocked, call pager_unlock() now to
547 ** clear the error state and ensure that the pager-cache is
548 ** completely empty.
549 */
550 pager_unlock(pPager);
551 }
danielk1977aef0bf62005-12-30 16:28:01 +0000552 }
553 return rc;
554}
555
drh477731b2007-06-16 03:06:27 +0000556/*
557** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
558** on the cache using a hash function. This is used for testing
559** and debugging only.
560*/
danielk19773c407372005-02-15 02:54:14 +0000561#ifdef SQLITE_CHECK_PAGES
562/*
563** Return a 32-bit hash of the page data for pPage.
564*/
drh477731b2007-06-16 03:06:27 +0000565static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000566 u32 hash = 0;
567 int i;
drh477731b2007-06-16 03:06:27 +0000568 for(i=0; i<nByte; i++){
569 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000570 }
571 return hash;
572}
drh477731b2007-06-16 03:06:27 +0000573static u32 pager_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000574 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
575}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000576static void pager_set_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000577 pPage->pageHash = pager_pagehash(pPage);
drh477731b2007-06-16 03:06:27 +0000578}
danielk19773c407372005-02-15 02:54:14 +0000579
580/*
581** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
582** is defined, and NDEBUG is not defined, an assert() statement checks
583** that the page is either dirty or still matches the calculated page-hash.
584*/
585#define CHECK_PAGE(x) checkPage(x)
586static void checkPage(PgHdr *pPg){
587 Pager *pPager = pPg->pPager;
danielk1977b3175382008-10-17 18:51:52 +0000588 assert( !pPg->pageHash || pPager->errCode
danielk19778c0a7912008-08-20 14:49:23 +0000589 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
danielk19773c407372005-02-15 02:54:14 +0000590}
591
592#else
drh8ffa8172007-06-18 17:25:17 +0000593#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000594#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000595#define CHECK_PAGE(x)
drh41d30272008-08-20 21:47:45 +0000596#endif /* SQLITE_CHECK_PAGES */
danielk19773c407372005-02-15 02:54:14 +0000597
drhed7c8552001-04-11 14:29:21 +0000598/*
danielk197776572402004-06-25 02:38:54 +0000599** When this is called the journal file for pager pPager must be open.
danielk1977443c0592009-01-16 15:21:05 +0000600** This function attempts to read a master journal file name from the
601** end of the file and, if successful, copies it into memory supplied
602** by the caller. See comments above writeMasterJournal() for the format
603** used to store a master journal file name at the end of a journal file.
danielk197776572402004-06-25 02:38:54 +0000604**
danielk197765839c62007-08-30 08:08:17 +0000605** zMaster must point to a buffer of at least nMaster bytes allocated by
606** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
607** enough space to write the master journal name). If the master journal
608** name in the journal is longer than nMaster bytes (including a
609** nul-terminator), then this is handled as if no master journal name
610** were present in the journal.
611**
danielk1977443c0592009-01-16 15:21:05 +0000612** If a master journal file name is present at the end of the journal
613** file, then it is copied into the buffer pointed to by zMaster. A
614** nul-terminator byte is appended to the buffer following the master
615** journal file name.
616**
617** If it is determined that no master journal file name is present
618** zMaster[0] is set to 0 and SQLITE_OK returned.
619**
620** If an error occurs while reading from the journal file, an SQLite
621** error code is returned.
danielk197776572402004-06-25 02:38:54 +0000622*/
danielk1977d92db532008-11-17 04:56:24 +0000623static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
danielk1977443c0592009-01-16 15:21:05 +0000624 int rc; /* Return code */
625 u32 len; /* Length in bytes of master journal name */
626 i64 szJ; /* Total size in bytes of journal file pJrnl */
627 u32 cksum; /* MJ checksum value read from journal */
628 u32 u; /* Unsigned loop counter */
629 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197765839c62007-08-30 08:08:17 +0000630 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000631
danielk1977443c0592009-01-16 15:21:05 +0000632 if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
633 || szJ<16
634 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
635 || len>=nMaster
636 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
637 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
638 || memcmp(aMagic, aJournalMagic, 8)
639 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
640 ){
danielk197776572402004-06-25 02:38:54 +0000641 return rc;
642 }
danielk1977cafadba2004-06-25 11:11:54 +0000643
644 /* See if the checksum matches the master journal name */
shane0b8d2762008-07-22 05:18:00 +0000645 for(u=0; u<len; u++){
646 cksum -= zMaster[u];
danielk1977443c0592009-01-16 15:21:05 +0000647 }
danielk19778191bff2004-06-28 04:52:30 +0000648 if( cksum ){
649 /* If the checksum doesn't add up, then one or more of the disk sectors
650 ** containing the master journal filename is corrupted. This means
651 ** definitely roll back, so just return SQLITE_OK and report a (nul)
652 ** master-journal filename.
653 */
danielk1977443c0592009-01-16 15:21:05 +0000654 len = 0;
danielk1977cafadba2004-06-25 11:11:54 +0000655 }
danielk1977443c0592009-01-16 15:21:05 +0000656 zMaster[len] = '\0';
danielk197776572402004-06-25 02:38:54 +0000657
658 return SQLITE_OK;
659}
660
661/*
danielk1977443c0592009-01-16 15:21:05 +0000662** Return the offset of the sector boundary at or immediately
663** following the value in pPager->journalOff, assuming a sector
664** size of pPager->sectorSize bytes.
danielk197776572402004-06-25 02:38:54 +0000665**
666** i.e for a sector size of 512:
667**
danielk1977443c0592009-01-16 15:21:05 +0000668** Pager.journalOff Return value
669** ---------------------------------------
670** 0 0
671** 512 512
672** 100 512
673** 2000 2048
danielk197776572402004-06-25 02:38:54 +0000674**
675*/
danielk1977112f7522009-01-08 17:50:45 +0000676static i64 journalHdrOffset(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000677 i64 offset = 0;
678 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000679 if( c ){
680 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
681 }
682 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
683 assert( offset>=c );
684 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
danielk1977112f7522009-01-08 17:50:45 +0000685 return offset;
686}
danielk197776572402004-06-25 02:38:54 +0000687
688/*
danielk1977443c0592009-01-16 15:21:05 +0000689** The journal file must be open when this function is called.
690**
691** This function is a no-op if the journal file has not been written to
692** within the current transaction (i.e. if Pager.journalOff==0).
693**
694** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
695** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
696** zero the 28-byte header at the start of the journal file. In either case,
697** if the pager is not in no-sync mode, sync the journal file immediately
698** after writing or truncating it.
699**
700** If Pager.journalSizeLimit is set to a positive, non-zero value, and
701** following the truncation or zeroing described above the size of the
702** journal file in bytes is larger than this value, then truncate the
703** journal file to Pager.journalSizeLimit bytes. The journal file does
704** not need to be synced following this operation.
705**
706** If an IO error occurs, abandon processing and return the IO error code.
707** Otherwise, return SQLITE_OK.
drhf3a87622008-04-17 14:16:42 +0000708*/
danielk1977df2566a2008-05-07 19:11:03 +0000709static int zeroJournalHdr(Pager *pPager, int doTruncate){
danielk1977443c0592009-01-16 15:21:05 +0000710 int rc = SQLITE_OK; /* Return code */
711 assert( isOpen(pPager->jfd) );
danielk1977df2566a2008-05-07 19:11:03 +0000712 if( pPager->journalOff ){
danielk1977443c0592009-01-16 15:21:05 +0000713 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
danielk1977b53e4962008-06-04 06:45:59 +0000714
danielk1977df2566a2008-05-07 19:11:03 +0000715 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000716 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000717 rc = sqlite3OsTruncate(pPager->jfd, 0);
718 }else{
danielk1977443c0592009-01-16 15:21:05 +0000719 static const char zeroHdr[28] = {0};
danielk1977df2566a2008-05-07 19:11:03 +0000720 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
721 }
danielk197781620542008-06-07 05:19:37 +0000722 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000723 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
724 }
danielk1977b53e4962008-06-04 06:45:59 +0000725
726 /* At this point the transaction is committed but the write lock
727 ** is still held on the file. If there is a size limit configured for
728 ** the persistent journal and the journal file currently consumes more
729 ** space than that limit allows for, truncate it now. There is no need
730 ** to sync the file following this operation.
731 */
732 if( rc==SQLITE_OK && iLimit>0 ){
733 i64 sz;
734 rc = sqlite3OsFileSize(pPager->jfd, &sz);
735 if( rc==SQLITE_OK && sz>iLimit ){
736 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
737 }
738 }
drha06ecba2008-04-22 17:15:17 +0000739 }
drhf3a87622008-04-17 14:16:42 +0000740 return rc;
741}
742
743/*
danielk197776572402004-06-25 02:38:54 +0000744** The journal file must be open when this routine is called. A journal
745** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
746** current location.
747**
748** The format for the journal header is as follows:
749** - 8 bytes: Magic identifying journal format.
750** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
751** - 4 bytes: Random number used for page hash.
752** - 4 bytes: Initial database page count.
753** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000754** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000755**
danielk197767c007b2008-03-20 04:45:49 +0000756** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000757*/
758static int writeJournalHdr(Pager *pPager){
danielk1977443c0592009-01-16 15:21:05 +0000759 int rc = SQLITE_OK; /* Return code */
760 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
761 u32 nHeader = pPager->pageSize; /* Size of buffer pointed to by zHeader */
762 u32 nWrite; /* Bytes of header sector written */
763 int ii; /* Loop counter */
764
765 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
danielk1977a664f8e2008-04-22 14:31:48 +0000766
767 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
768 nHeader = JOURNAL_HDR_SZ(pPager);
769 }
danielk197776572402004-06-25 02:38:54 +0000770
danielk1977443c0592009-01-16 15:21:05 +0000771 /* If there are active savepoints and any of them were created
772 ** since the most recent journal header was written, update the
773 ** PagerSavepoint.iHdrOffset fields now.
danielk1977fd7f0452008-12-17 17:30:26 +0000774 */
775 for(ii=0; ii<pPager->nSavepoint; ii++){
776 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
777 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
778 }
danielk19774099f6e2007-03-19 11:25:20 +0000779 }
780
danielk1977443c0592009-01-16 15:21:05 +0000781 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
drh97b57482006-01-10 20:32:32 +0000782 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000783
784 /*
785 ** Write the nRec Field - the number of page records that follow this
786 ** journal header. Normally, zero is written to this value at this time.
787 ** After the records are added to the journal (and the journal synced,
788 ** if in full-sync mode), the zero is overwritten with the true number
789 ** of records (see syncJournal()).
790 **
791 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
792 ** reading the journal this value tells SQLite to assume that the
793 ** rest of the journal file contains valid page records. This assumption
794 ** is dangerous, as if a failure occured whilst writing to the journal
795 ** file it may contain some garbage data. There are two scenarios
796 ** where this risk can be ignored:
797 **
798 ** * When the pager is in no-sync mode. Corruption can follow a
799 ** power failure in this case anyway.
800 **
801 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
802 ** that garbage data is never appended to the journal file.
803 */
danielk1977443c0592009-01-16 15:21:05 +0000804 assert( isOpen(pPager->fd) || pPager->noSync );
danielk1977b3175382008-10-17 18:51:52 +0000805 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
danielk19774cd2cd52007-08-23 14:48:23 +0000806 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
807 ){
808 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
809 }else{
810 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
811 }
812
drh97b57482006-01-10 20:32:32 +0000813 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +0000814 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +0000815 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
816 /* The initial database size */
danielk19773460d192008-12-27 15:23:13 +0000817 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
drh97b57482006-01-10 20:32:32 +0000818 /* The assumed sector size for this process */
819 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drh08609ce2008-11-29 22:49:23 +0000820
danielk1977443c0592009-01-16 15:21:05 +0000821 /* The page size */
822 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
823
drh08609ce2008-11-29 22:49:23 +0000824 /* Initializing the tail of the buffer is not necessary. Everything
825 ** works find if the following memset() is omitted. But initializing
826 ** the memory prevents valgrind from complaining, so we are willing to
827 ** take the performance hit.
828 */
danielk1977443c0592009-01-16 15:21:05 +0000829 memset(&zHeader[sizeof(aJournalMagic)+20], 0,
830 nHeader-(sizeof(aJournalMagic)+20));
drh08609ce2008-11-29 22:49:23 +0000831
danielk1977443c0592009-01-16 15:21:05 +0000832 /* In theory, it is only necessary to write the 28 bytes that the
833 ** journal header consumes to the journal file here. Then increment the
834 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
835 ** record is written to the following sector (leaving a gap in the file
836 ** that will be implicitly filled in by the OS).
837 **
838 ** However it has been discovered that on some systems this pattern can
839 ** be significantly slower than contiguously writing data to the file,
840 ** even if that means explicitly writing data to the block of
841 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
842 ** is done.
843 **
844 ** The loop is required here in case the sector-size is larger than the
845 ** database page size. Since the zHeader buffer is only Pager.pageSize
846 ** bytes in size, more than one call to sqlite3OsWrite() may be required
847 ** to populate the entire journal header sector.
848 */
danielk1977a664f8e2008-04-22 14:31:48 +0000849 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
850 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
851 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
852 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +0000853 }
danielk1977a664f8e2008-04-22 14:31:48 +0000854
danielk197776572402004-06-25 02:38:54 +0000855 return rc;
856}
857
858/*
859** The journal file must be open when this is called. A journal header file
860** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
drhd6e5e092009-01-07 02:03:35 +0000861** file. The current location in the journal file is given by
danielk1977443c0592009-01-16 15:21:05 +0000862** pPager->journalOff. See comments above function writeJournalHdr() for
drhd6e5e092009-01-07 02:03:35 +0000863** a description of the journal header format.
danielk197776572402004-06-25 02:38:54 +0000864**
danielk1977443c0592009-01-16 15:21:05 +0000865** If the header is read successfully, *pNRec is set to the number of
866** page records following this header and *pDbSize is set to the size of the
danielk197776572402004-06-25 02:38:54 +0000867** database before the transaction began, in pages. Also, pPager->cksumInit
868** is set to the value read from the journal header. SQLITE_OK is returned
869** in this case.
870**
871** If the journal header file appears to be corrupted, SQLITE_DONE is
danielk1977443c0592009-01-16 15:21:05 +0000872** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
danielk197776572402004-06-25 02:38:54 +0000873** cannot be read from the journal file an error code is returned.
874*/
875static int readJournalHdr(
danielk1977443c0592009-01-16 15:21:05 +0000876 Pager *pPager, /* Pager object */
877 i64 journalSize, /* Size of the open journal file in bytes */
878 u32 *pNRec, /* OUT: Value read from the nRec field */
879 u32 *pDbSize /* OUT: Value of original database size field */
danielk197776572402004-06-25 02:38:54 +0000880){
danielk1977443c0592009-01-16 15:21:05 +0000881 int rc; /* Return code */
882 unsigned char aMagic[8]; /* A buffer to hold the magic header */
883 i64 iHdrOff; /* Offset of journal header being read */
danielk197776572402004-06-25 02:38:54 +0000884
danielk1977443c0592009-01-16 15:21:05 +0000885 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
886
887 /* Advance Pager.journalOff to the start of the next sector. If the
888 ** journal file is too small for there to be a header stored at this
889 ** point, return SQLITE_DONE.
890 */
891 pPager->journalOff = journalHdrOffset(pPager);
danielk197776572402004-06-25 02:38:54 +0000892 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
893 return SQLITE_DONE;
894 }
danielk1977443c0592009-01-16 15:21:05 +0000895 iHdrOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000896
danielk1977443c0592009-01-16 15:21:05 +0000897 /* Read in the first 8 bytes of the journal header. If they do not match
898 ** the magic string found at the start of each journal header, return
899 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
900 ** proceed.
901 */
902 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
903 if( rc ){
904 return rc;
905 }
danielk197776572402004-06-25 02:38:54 +0000906 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
907 return SQLITE_DONE;
908 }
909
danielk1977443c0592009-01-16 15:21:05 +0000910 /* Read the first three 32-bit fields of the journal header: The nRec
911 ** field, the checksum-initializer and the database size at the start
912 ** of the transaction. Return an error code if anything goes wrong.
913 */
914 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
915 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
916 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
917 ){
918 return rc;
919 }
danielk197776572402004-06-25 02:38:54 +0000920
danielk19777cbd5892009-01-10 16:15:09 +0000921 if( pPager->journalOff==0 ){
danielk1977443c0592009-01-16 15:21:05 +0000922 u32 iPageSize; /* Page-size field of journal header */
923 u32 iSectorSize; /* Sector-size field of journal header */
924 u16 iPageSize16; /* Copy of iPageSize in 16-bit variable */
danielk197767c007b2008-03-20 04:45:49 +0000925
danielk1977443c0592009-01-16 15:21:05 +0000926 /* Read the page-size and sector-size journal header fields. */
927 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
928 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
danielk19777cbd5892009-01-10 16:15:09 +0000929 ){
danielk1977443c0592009-01-16 15:21:05 +0000930 return rc;
danielk19777cbd5892009-01-10 16:15:09 +0000931 }
danielk1977443c0592009-01-16 15:21:05 +0000932
933 /* Check that the values read from the page-size and sector-size fields
934 ** are within range. To be 'in range', both values need to be a power
935 ** of two greater than or equal to 512, and not greater than their
936 ** respective compile time maximum limits.
937 */
938 if( iPageSize<512 || iSectorSize<512
939 || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
940 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
941 ){
942 /* If the either the page-size or sector-size in the journal-header is
943 ** invalid, then the process that wrote the journal-header must have
944 ** crashed before the header was synced. In this case stop reading
945 ** the journal file here.
946 */
947 return SQLITE_DONE;
948 }
949
950 /* Update the page-size to match the value read from the journal.
951 ** Use a testcase() macro to make sure that malloc failure within
952 ** PagerSetPagesize() is tested.
953 */
954 iPageSize16 = (u16)iPageSize;
955 rc = sqlite3PagerSetPagesize(pPager, &iPageSize16);
956 testcase( rc!=SQLITE_OK );
957 assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize );
958
danielk19777cbd5892009-01-10 16:15:09 +0000959 /* Update the assumed sector-size to match the value used by
960 ** the process that created this journal. If this journal was
961 ** created by a process other than this one, then this routine
962 ** is being called from within pager_playback(). The local value
963 ** of Pager.sectorSize is restored at the end of that routine.
964 */
danielk19777cbd5892009-01-10 16:15:09 +0000965 pPager->sectorSize = iSectorSize;
drh98c58352008-11-07 00:24:53 +0000966 }
danielk197776572402004-06-25 02:38:54 +0000967
968 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk1977443c0592009-01-16 15:21:05 +0000969 return rc;
danielk197776572402004-06-25 02:38:54 +0000970}
971
972
973/*
974** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000975** pPager at the current location. The master journal name must be the last
976** thing written to a journal file. If the pager is in full-sync mode, the
977** journal file descriptor is advanced to the next sector boundary before
978** anything is written. The format is:
979**
danielk1977443c0592009-01-16 15:21:05 +0000980** + 4 bytes: PAGER_MJ_PGNO.
981** + N bytes: Master journal filename in utf-8.
982** + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
983** + 4 bytes: Master journal name checksum.
984** + 8 bytes: aJournalMagic[].
danielk1977cafadba2004-06-25 11:11:54 +0000985**
986** The master journal page checksum is the sum of the bytes in the master
danielk1977443c0592009-01-16 15:21:05 +0000987** journal name, where each byte is interpreted as a signed 8-bit integer.
danielk1977aef0bf62005-12-30 16:28:01 +0000988**
989** If zMaster is a NULL pointer (occurs for a single database transaction),
990** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000991*/
992static int writeMasterJournal(Pager *pPager, const char *zMaster){
danielk1977443c0592009-01-16 15:21:05 +0000993 int rc; /* Return code */
994 int nMaster; /* Length of string zMaster */
995 i64 iHdrOff; /* Offset of header in journal file */
996 i64 jrnlSize; /* Size of journal file on disk */
997 u32 cksum = 0; /* Checksum of string zMaster */
danielk197776572402004-06-25 02:38:54 +0000998
danielk1977443c0592009-01-16 15:21:05 +0000999 assert( isOpen(pPager->jfd) );
1000 assert( !pPager->setMaster );
1001
1002 if( !zMaster ) return SQLITE_OK;
danielk1977b3175382008-10-17 18:51:52 +00001003 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ) return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001004 pPager->setMaster = 1;
1005
danielk1977443c0592009-01-16 15:21:05 +00001006 /* Calculate the length in bytes and the checksum of zMaster */
1007 for(nMaster=0; zMaster[nMaster]; nMaster++){
1008 cksum += zMaster[nMaster];
danielk1977cafadba2004-06-25 11:11:54 +00001009 }
danielk197776572402004-06-25 02:38:54 +00001010
1011 /* If in full-sync mode, advance to the next disk sector before writing
1012 ** the master journal name. This is in case the previous page written to
1013 ** the journal has already been synced.
1014 */
1015 if( pPager->fullSync ){
danielk1977443c0592009-01-16 15:21:05 +00001016 pPager->journalOff = journalHdrOffset(pPager);
danielk197776572402004-06-25 02:38:54 +00001017 }
danielk1977443c0592009-01-16 15:21:05 +00001018 iHdrOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001019
danielk1977443c0592009-01-16 15:21:05 +00001020 /* Write the master journal data to the end of the journal file. If
1021 ** an error occurs, return the error code to the caller.
1022 */
1023 if( (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager)))
1024 || (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4))
1025 || (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster))
1026 || (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum))
1027 || (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8))
1028 ){
1029 return rc;
1030 }
1031 pPager->journalOff += (nMaster+20);
drh2c8997b2005-08-27 16:36:48 +00001032 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +00001033
1034 /* If the pager is in peristent-journal mode, then the physical
1035 ** journal-file may extend past the end of the master-journal name
1036 ** and 8 bytes of magic data just written to the file. This is
1037 ** dangerous because the code to rollback a hot-journal file
1038 ** will not be able to find the master-journal name to determine
1039 ** whether or not the journal is hot.
1040 **
1041 ** Easiest thing to do in this scenario is to truncate the journal
1042 ** file to the required size.
1043 */
danielk1977443c0592009-01-16 15:21:05 +00001044 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
1045 && jrnlSize>pPager->journalOff
danielk1977df2566a2008-05-07 19:11:03 +00001046 ){
danielk1977443c0592009-01-16 15:21:05 +00001047 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
danielk1977df2566a2008-05-07 19:11:03 +00001048 }
danielk197776572402004-06-25 02:38:54 +00001049 return rc;
1050}
1051
1052/*
danielk1977443c0592009-01-16 15:21:05 +00001053** Find a page in the hash table given its page number. Return
1054** a pointer to the page or NULL if the requested page is not
1055** already in memory.
drhed7c8552001-04-11 14:29:21 +00001056*/
drhd9b02572001-04-15 00:37:09 +00001057static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
danielk1977443c0592009-01-16 15:21:05 +00001058 PgHdr *p; /* Return value */
1059
1060 /* It is not possible for a call to PcacheFetch() with createFlag==0 to
1061 ** fail, since no attempt to allocate dynamic memory will be made.
1062 */
1063 (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
drhed7c8552001-04-11 14:29:21 +00001064 return p;
1065}
1066
1067/*
danielk1977443c0592009-01-16 15:21:05 +00001068** Unless the pager is in error-state, discard all in-memory pages. If
1069** the pager is in error-state, then this call is a no-op.
drhed7c8552001-04-11 14:29:21 +00001070*/
drhd9b02572001-04-15 00:37:09 +00001071static void pager_reset(Pager *pPager){
danielk1977443c0592009-01-16 15:21:05 +00001072 if( SQLITE_OK==pPager->errCode ){
1073 sqlite3PcacheClear(pPager->pPCache);
1074 }
danielk1977e277be02007-03-23 18:12:06 +00001075}
1076
danielk197734cf35d2008-12-18 18:31:38 +00001077/*
1078** Free all structures in the Pager.aSavepoint[] array and set both
1079** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
1080** if it is open and the pager is not in exclusive mode.
1081*/
danielk1977443c0592009-01-16 15:21:05 +00001082static void releaseAllSavepoints(Pager *pPager){
1083 int ii; /* Iterator for looping through Pager.aSavepoint */
danielk1977fd7f0452008-12-17 17:30:26 +00001084 for(ii=0; ii<pPager->nSavepoint; ii++){
1085 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
1086 }
1087 if( !pPager->exclusiveMode ){
1088 sqlite3OsClose(pPager->sjfd);
1089 }
1090 sqlite3_free(pPager->aSavepoint);
1091 pPager->aSavepoint = 0;
1092 pPager->nSavepoint = 0;
danielk1977443c0592009-01-16 15:21:05 +00001093 pPager->nSubRec = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00001094}
1095
danielk197734cf35d2008-12-18 18:31:38 +00001096/*
danielk1977443c0592009-01-16 15:21:05 +00001097** Set the bit number pgno in the PagerSavepoint.pInSavepoint
1098** bitvecs of all open savepoints. Return SQLITE_OK if successful
1099** or SQLITE_NOMEM if a malloc failure occurs.
danielk197734cf35d2008-12-18 18:31:38 +00001100*/
danielk1977fd7f0452008-12-17 17:30:26 +00001101static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
drh7539b6b2009-01-02 21:39:39 +00001102 int ii; /* Loop counter */
1103 int rc = SQLITE_OK; /* Result code */
1104
danielk1977fd7f0452008-12-17 17:30:26 +00001105 for(ii=0; ii<pPager->nSavepoint; ii++){
1106 PagerSavepoint *p = &pPager->aSavepoint[ii];
1107 if( pgno<=p->nOrig ){
drh7539b6b2009-01-02 21:39:39 +00001108 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
danielk1977443c0592009-01-16 15:21:05 +00001109 testcase( rc==SQLITE_NOMEM );
drh7539b6b2009-01-02 21:39:39 +00001110 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
danielk1977fd7f0452008-12-17 17:30:26 +00001111 }
1112 }
drh7539b6b2009-01-02 21:39:39 +00001113 return rc;
danielk1977fd7f0452008-12-17 17:30:26 +00001114}
1115
danielk1977e277be02007-03-23 18:12:06 +00001116/*
danielk1977443c0592009-01-16 15:21:05 +00001117** Unlock the database file. This function is a no-op if the pager
1118** is in exclusive mode.
danielk1977ae72d982007-10-03 08:46:44 +00001119**
1120** If the pager is currently in error state, discard the contents of
1121** the cache and reset the Pager structure internal state. If there is
1122** an open journal-file, then the next time a shared-lock is obtained
1123** on the pager file (by this or any other process), it will be
1124** treated as a hot-journal and rolled back.
1125*/
1126static void pager_unlock(Pager *pPager){
1127 if( !pPager->exclusiveMode ){
danielk1977443c0592009-01-16 15:21:05 +00001128 int rc; /* Return code */
danielk1977ae72d982007-10-03 08:46:44 +00001129
danielk1977b3175382008-10-17 18:51:52 +00001130 /* Always close the journal file when dropping the database lock.
1131 ** Otherwise, another connection with journal_mode=delete might
1132 ** delete the file out from under us.
1133 */
danielk1977443c0592009-01-16 15:21:05 +00001134 sqlite3OsClose(pPager->jfd);
1135 sqlite3BitvecDestroy(pPager->pInJournal);
1136 pPager->pInJournal = 0;
1137 releaseAllSavepoints(pPager);
1138
1139 /* If the file is unlocked, somebody else might change it. The
1140 ** values stored in Pager.dbSize etc. might become invalid if
1141 ** this happens. TODO: Really, this doesn't need to be cleared
1142 ** until the change-counter check fails in pagerSharedLock().
1143 */
1144 pPager->dbSizeValid = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001145
danielk19775f2d46b2009-01-13 16:03:44 +00001146 rc = osUnlock(pPager->fd, NO_LOCK);
danielk1977443c0592009-01-16 15:21:05 +00001147 if( rc ){
1148 pPager->errCode = rc;
1149 }
danielk19775f2d46b2009-01-13 16:03:44 +00001150 IOTRACE(("UNLOCK %p\n", pPager))
1151
danielk1977b3175382008-10-17 18:51:52 +00001152 /* If Pager.errCode is set, the contents of the pager cache cannot be
1153 ** trusted. Now that the pager file is unlocked, the contents of the
1154 ** cache can be discarded and the error code safely cleared.
1155 */
1156 if( pPager->errCode ){
danielk1977443c0592009-01-16 15:21:05 +00001157 if( rc==SQLITE_OK ){
1158 pPager->errCode = SQLITE_OK;
1159 }
danielk1977b3175382008-10-17 18:51:52 +00001160 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00001161 }
danielk1977b3175382008-10-17 18:51:52 +00001162
danielk1977b3175382008-10-17 18:51:52 +00001163 pPager->changeCountDone = 0;
danielk1977443c0592009-01-16 15:21:05 +00001164 pPager->state = PAGER_UNLOCK;
danielk1977ae72d982007-10-03 08:46:44 +00001165 }
1166}
1167
1168/*
1169** Execute a rollback if a transaction is active and unlock the
danielk1977443c0592009-01-16 15:21:05 +00001170** database file.
1171**
1172** If the pager has already entered the error state, do not attempt
1173** the rollback at this time. Instead, pager_unlock() is called. The
1174** call to pager_unlock() will discard all in-memory pages, unlock
1175** the database file and clear the error state. If this means that
1176** there is a hot-journal left in the file-system, the next connection
1177** to obtain a shared lock on the pager (which may be this one) will
1178** roll it back.
1179**
1180** If the pager has not already entered the error state, but an IO or
1181** malloc error occurs during a rollback, then this will itself cause
1182** the pager to enter the error state. Which will be cleared by the
1183** call to pager_unlock(), as described above.
danielk1977ae72d982007-10-03 08:46:44 +00001184*/
danielk1977443c0592009-01-16 15:21:05 +00001185static void pagerUnlockAndRollback(Pager *pPager){
1186 if( pPager->errCode==SQLITE_OK && pPager->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +00001187 sqlite3BeginBenignMalloc();
danielk1977443c0592009-01-16 15:21:05 +00001188 sqlite3PagerRollback(pPager);
danielk19772d1d86f2008-06-20 14:59:51 +00001189 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001190 }
danielk1977443c0592009-01-16 15:21:05 +00001191 pager_unlock(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00001192}
1193
1194/*
danielk1977443c0592009-01-16 15:21:05 +00001195** This routine ends a transaction. A transaction is usually ended by
1196** either a COMMIT or a ROLLBACK operation. This routine may be called
1197** after rollback of a hot-journal, or if an error occurs while opening
1198** the journal file or writing the very first journal-header of a
1199** database transaction.
1200**
1201** If the pager is in PAGER_SHARED or PAGER_UNLOCK state when this
1202** routine is called, it is a no-op (returns SQLITE_OK).
drh80e35f42007-03-30 14:06:34 +00001203**
danielk1977443c0592009-01-16 15:21:05 +00001204** Otherwise, any active savepoints are released.
drh80e35f42007-03-30 14:06:34 +00001205**
danielk1977443c0592009-01-16 15:21:05 +00001206** If the journal file is open, then it is "finalized". Once a journal
1207** file has been finalized it is not possible to use it to roll back a
1208** transaction. Nor will it be considered to be a hot-journal by this
1209** or any other database connection. Exactly how a journal is finalized
1210** depends on whether or not the pager is running in exclusive mode and
1211** the current journal-mode (Pager.journalMode value), as follows:
drh50457892003-09-06 01:10:47 +00001212**
danielk1977443c0592009-01-16 15:21:05 +00001213** journalMode==MEMORY
1214** Journal file descriptor is simply closed. This destroys an
1215** in-memory journal.
1216**
1217** journalMode==TRUNCATE
1218** Journal file is truncated to zero bytes in size.
1219**
1220** journalMode==PERSIST
1221** The first 28 bytes of the journal file are zeroed. This invalidates
1222** the first journal header in the file, and hence the entire journal
1223** file. An invalid journal file cannot be rolled back.
1224**
1225** journalMode==DELETE
1226** The journal file is closed and deleted using sqlite3OsDelete().
1227**
1228** If the pager is running in exclusive mode, this method of finalizing
1229** the journal file is never used. Instead, if the journalMode is
1230** DELETE and the pager is in exclusive mode, the method described under
1231** journalMode==PERSIST is used instead.
1232**
1233** After the journal is finalized, if running in non-exclusive mode, the
1234** pager moves to PAGER_SHARED state (and downgrades the lock on the
1235** database file accordingly).
1236**
1237** If the pager is running in exclusive mode and is in PAGER_SYNCED state,
1238** it moves to PAGER_EXCLUSIVE. No locks are downgraded when running in
1239** exclusive mode.
1240**
1241** SQLITE_OK is returned if no error occurs. If an error occurs during
1242** any of the IO operations to finalize the journal file or unlock the
1243** database then the IO error code is returned to the user. If the
1244** operation to finalize the journal file fails, then the code still
1245** tries to unlock the database file if not in exclusive mode. If the
1246** unlock operation fails as well, then the first error code related
1247** to the first error encountered (the journal finalization one) is
1248** returned.
drhed7c8552001-04-11 14:29:21 +00001249*/
danielk1977df2566a2008-05-07 19:11:03 +00001250static int pager_end_transaction(Pager *pPager, int hasMaster){
danielk1977443c0592009-01-16 15:21:05 +00001251 int rc = SQLITE_OK; /* Error code from journal finalization operation */
1252 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
1253
drha6abd042004-06-09 17:37:22 +00001254 if( pPager->state<PAGER_RESERVED ){
1255 return SQLITE_OK;
1256 }
danielk1977443c0592009-01-16 15:21:05 +00001257 releaseAllSavepoints(pPager);
1258
1259 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
1260 if( isOpen(pPager->jfd) ){
1261
1262 /* TODO: There's a problem here if a journal-file was opened in MEMORY
1263 ** mode and then the journal-mode is changed to TRUNCATE or PERSIST
1264 ** during the transaction. This code should be changed to assume
1265 ** that the journal mode has not changed since the transaction was
1266 ** started. And the sqlite3PagerJournalMode() function should be
1267 ** changed to make sure that this is the case too.
1268 */
1269
1270 /* Finalize the journal file. */
danielk1977b3175382008-10-17 18:51:52 +00001271 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
1272 int isMemoryJournal = sqlite3IsMemJournal(pPager->jfd);
1273 sqlite3OsClose(pPager->jfd);
danielk1977b3175382008-10-17 18:51:52 +00001274 if( !isMemoryJournal ){
1275 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
1276 }
1277 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE
drh04335882008-09-26 21:08:08 +00001278 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){
1279 pPager->journalOff = 0;
1280 pPager->journalStarted = 0;
1281 }else if( pPager->exclusiveMode
danielk197793f7af92008-05-09 16:57:50 +00001282 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1283 ){
1284 rc = zeroJournalHdr(pPager, hasMaster);
1285 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +00001286 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001287 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001288 }else{
drh04335882008-09-26 21:08:08 +00001289 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || rc );
danielk1977b4b47412007-08-17 15:53:36 +00001290 sqlite3OsClose(pPager->jfd);
danielk19770f01fda2008-06-06 16:14:02 +00001291 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00001292 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001293 }
danielk197741483462007-03-24 16:45:04 +00001294 }
danielk1977443c0592009-01-16 15:21:05 +00001295
danielk19773c407372005-02-15 02:54:14 +00001296#ifdef SQLITE_CHECK_PAGES
danielk1977bc2ca9e2008-11-13 14:28:28 +00001297 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
danielk19773c407372005-02-15 02:54:14 +00001298#endif
danielk1977443c0592009-01-16 15:21:05 +00001299
danielk1977bc2ca9e2008-11-13 14:28:28 +00001300 sqlite3PcacheCleanAll(pPager->pPCache);
danielk1977443c0592009-01-16 15:21:05 +00001301 sqlite3BitvecDestroy(pPager->pInJournal);
1302 pPager->pInJournal = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001303 pPager->nRec = 0;
drhd9b02572001-04-15 00:37:09 +00001304 }
danielk1977979f38e2007-03-27 16:19:51 +00001305
danielk197741483462007-03-24 16:45:04 +00001306 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001307 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001308 pPager->state = PAGER_SHARED;
danielk1977104f1fe2009-01-14 17:45:57 +00001309 pPager->changeCountDone = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001310 }else if( pPager->state==PAGER_SYNCED ){
1311 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001312 }
danielk197776572402004-06-25 02:38:54 +00001313 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001314 pPager->needSync = 0;
danielk1977443c0592009-01-16 15:21:05 +00001315 pPager->dbModified = 0;
1316
1317 /* TODO: Is this optimal? Why is the db size invalidated here
1318 ** when the database file is not unlocked? */
1319 pPager->dbOrigSize = 0;
danielk1977f90b7262009-01-07 15:18:20 +00001320 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
danielk1977b3175382008-10-17 18:51:52 +00001321 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00001322 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00001323 }
danielk1977979f38e2007-03-27 16:19:51 +00001324
1325 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001326}
1327
drhed7c8552001-04-11 14:29:21 +00001328/*
danielk1977443c0592009-01-16 15:21:05 +00001329** Parameter aData must point to a buffer of pPager->pageSize bytes
1330** of data. Compute and return a checksum based ont the contents of the
1331** page of data and the current value of pPager->cksumInit.
drh34e79ce2004-02-08 06:05:46 +00001332**
danielk1977443c0592009-01-16 15:21:05 +00001333** This is not a real checksum. It is really just the sum of the
1334** random initial value (pPager->cksumInit) and every 200th byte
1335** of the page data, starting with byte offset (pPager->pageSize%200).
1336** Each byte is interpreted as an 8-bit unsigned integer.
drh726de592004-06-10 23:35:50 +00001337**
danielk1977443c0592009-01-16 15:21:05 +00001338** Changing the formula used to compute this checksum results in an
1339** incompatible journal file format.
1340**
1341** If journal corruption occurs due to a power failure, the most likely
1342** scenario is that one end or the other of the record will be changed.
1343** It is much less likely that the two ends of the journal record will be
drh726de592004-06-10 23:35:50 +00001344** correct and the middle be corrupt. Thus, this "checksum" scheme,
1345** though fast and simple, catches the mostly likely kind of corruption.
drh968af522003-02-11 14:55:40 +00001346*/
drh74161702006-02-24 02:53:49 +00001347static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977443c0592009-01-16 15:21:05 +00001348 u32 cksum = pPager->cksumInit; /* Checksum value to return */
1349 int i = pPager->pageSize-200; /* Loop counter */
danielk1977ef317ab2004-06-23 10:43:10 +00001350 while( i>0 ){
1351 cksum += aData[i];
1352 i -= 200;
1353 }
drh968af522003-02-11 14:55:40 +00001354 return cksum;
1355}
1356
1357/*
drhd6e5e092009-01-07 02:03:35 +00001358** Read a single page from either the journal file (if isMainJrnl==1) or
1359** from the sub-journal (if isMainJrnl==0) and playback that page.
danielk1977443c0592009-01-16 15:21:05 +00001360** The page begins at offset *pOffset into the file. The *pOffset
drhd6e5e092009-01-07 02:03:35 +00001361** value is increased to the start of the next page in the journal.
drh968af522003-02-11 14:55:40 +00001362**
drhc13148f2008-08-27 18:03:20 +00001363** The isMainJrnl flag is true if this is the main rollback journal and
1364** false for the statement journal. The main rollback journal uses
1365** checksums - the statement journal does not.
drhd6e5e092009-01-07 02:03:35 +00001366**
danielk1977443c0592009-01-16 15:21:05 +00001367** If the page number of the page record read from the (sub-)journal file
1368** is greater than the current value of Pager.dbSize, then playback is
1369** skipped and SQLITE_OK is returned.
1370**
drhd6e5e092009-01-07 02:03:35 +00001371** If pDone is not NULL, then it is a record of pages that have already
1372** been played back. If the page at *pOffset has already been played back
1373** (if the corresponding pDone bit is set) then skip the playback.
1374** Make sure the pDone bit corresponding to the *pOffset page is set
1375** prior to returning.
danielk1977443c0592009-01-16 15:21:05 +00001376**
1377** If the page record is successfully read from the (sub-)journal file
1378** and played back, then SQLITE_OK is returned. If an IO error occurs
1379** while reading the record from the (sub-)journal file or while writing
1380** to the database file, then the IO error code is returned. If data
1381** is successfully read from the (sub-)journal file but appears to be
1382** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
1383** two circumstances:
1384**
1385** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
1386** * If the record is being rolled back from the main journal file
1387** and the checksum field does not match the record content.
1388**
1389** Neither of these two scenarios are possible during a savepoint rollback.
1390**
1391** If this is a savepoint rollback, then memory may have to be dynamically
1392** allocated by this function. If this is the case and an allocation fails,
1393** SQLITE_NOMEM is returned.
drhfa86c412002-02-02 15:01:15 +00001394*/
danielk197762079062007-08-15 17:08:46 +00001395static int pager_playback_one_page(
danielk1977fd7f0452008-12-17 17:30:26 +00001396 Pager *pPager, /* The pager being played back */
1397 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
drhd6e5e092009-01-07 02:03:35 +00001398 i64 *pOffset, /* Offset of record to playback */
danielk1977ecfef982008-12-20 08:39:57 +00001399 int isSavepnt, /* True for a savepoint rollback */
danielk1977fd7f0452008-12-17 17:30:26 +00001400 Bitvec *pDone /* Bitvec of pages already played back */
danielk197762079062007-08-15 17:08:46 +00001401){
drhfa86c412002-02-02 15:01:15 +00001402 int rc;
drhae2b40c2004-06-09 19:03:54 +00001403 PgHdr *pPg; /* An existing page in the cache */
1404 Pgno pgno; /* The page number of a page in journal */
1405 u32 cksum; /* Checksum used for sanity checking */
drhd6e5e092009-01-07 02:03:35 +00001406 u8 *aData; /* Temporary storage for the page */
1407 sqlite3_file *jfd; /* The file descriptor for the journal file */
drhfa86c412002-02-02 15:01:15 +00001408
drhd6e5e092009-01-07 02:03:35 +00001409 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
1410 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
1411 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
1412 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
drh96362842005-03-20 22:47:56 +00001413
drhd6e5e092009-01-07 02:03:35 +00001414 aData = (u8*)pPager->pTmpSpace;
1415 assert( aData ); /* Temp storage must have already been allocated */
1416
danielk1977443c0592009-01-16 15:21:05 +00001417 /* Read the page number and page data from the journal or sub-journal
1418 ** file. Return an error code to the caller if an IO error occurs.
1419 */
drhd6e5e092009-01-07 02:03:35 +00001420 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
drhd6e5e092009-01-07 02:03:35 +00001421 rc = read32bits(jfd, *pOffset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001422 if( rc!=SQLITE_OK ) return rc;
drhd6e5e092009-01-07 02:03:35 +00001423 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, (*pOffset)+4);
drh99ee3602003-02-16 19:13:36 +00001424 if( rc!=SQLITE_OK ) return rc;
drhd6e5e092009-01-07 02:03:35 +00001425 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
drhfa86c412002-02-02 15:01:15 +00001426
drh968af522003-02-11 14:55:40 +00001427 /* Sanity checking on the page. This is more important that I originally
1428 ** thought. If a power failure occurs while the journal is being written,
1429 ** it could cause invalid data to be written into the journal. We need to
1430 ** detect this invalid data (with high probability) and ignore it.
1431 */
danielk197775edc162004-06-26 01:48:18 +00001432 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
danielk1977443c0592009-01-16 15:21:05 +00001433 assert( !isSavepnt );
drh968af522003-02-11 14:55:40 +00001434 return SQLITE_DONE;
1435 }
danielk1977fd7f0452008-12-17 17:30:26 +00001436 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
drh968af522003-02-11 14:55:40 +00001437 return SQLITE_OK;
1438 }
drhc13148f2008-08-27 18:03:20 +00001439 if( isMainJrnl ){
drhd6e5e092009-01-07 02:03:35 +00001440 rc = read32bits(jfd, (*pOffset)-4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001441 if( rc ) return rc;
danielk1977ecfef982008-12-20 08:39:57 +00001442 if( !isSavepnt && pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001443 return SQLITE_DONE;
1444 }
1445 }
danielk1977443c0592009-01-16 15:21:05 +00001446
danielk1977fd7f0452008-12-17 17:30:26 +00001447 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno)) ){
1448 return rc;
1449 }
drhfa86c412002-02-02 15:01:15 +00001450
danielk1977aa5ccdf2004-06-14 05:10:42 +00001451 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001452
1453 /* If the pager is in RESERVED state, then there must be a copy of this
1454 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001455 ** not the database file. The page is left marked dirty in this case.
1456 **
danielk19772df71c72007-05-24 07:22:42 +00001457 ** An exception to the above rule: If the database is in no-sync mode
1458 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001459 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1460 ** during a Movepage() call, then the page may not be in the cache
1461 ** either. So the condition described in the above paragraph is not
1462 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001463 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001464 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1465 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001466 **
1467 ** Ticket #1171: The statement journal might contain page content that is
1468 ** different from the page content at the start of the transaction.
1469 ** This occurs when a page is changed prior to the start of a statement
1470 ** then changed again within the statement. When rolling back such a
1471 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001472 ** for certain that original page contents are synced into the main rollback
1473 ** journal. Otherwise, a power loss might leave modified data in the
1474 ** database file without an entry in the rollback journal that can
1475 ** restore the database to its original form. Two conditions must be
1476 ** met before writing to the database files. (1) the database must be
1477 ** locked. (2) we know that the original page content is fully synced
1478 ** in the main journal either because the page is not in cache or else
1479 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001480 **
1481 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1482 ** is possible to fail a statement on a database that does not yet exist.
1483 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001484 */
drhae2b40c2004-06-09 19:03:54 +00001485 pPg = pager_lookup(pPager, pgno);
drh30d53702009-01-06 15:58:57 +00001486 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
danielk1977ecfef982008-12-20 08:39:57 +00001487 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData),
1488 (isMainJrnl?"main-journal":"sub-journal")
drh30d53702009-01-06 15:58:57 +00001489 ));
danielk19778c0a7912008-08-20 14:49:23 +00001490 if( (pPager->state>=PAGER_EXCLUSIVE)
1491 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
danielk1977443c0592009-01-16 15:21:05 +00001492 && isOpen(pPager->fd)
danielk19778c0a7912008-08-20 14:49:23 +00001493 ){
drh281b21d2008-08-22 12:57:08 +00001494 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
1495 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
danielk19773460d192008-12-27 15:23:13 +00001496 if( pgno>pPager->dbFileSize ){
1497 pPager->dbFileSize = pgno;
1498 }
danielk1977f2c31ad2009-01-06 13:40:08 +00001499 }else if( !isMainJrnl && pPg==0 ){
1500 /* If this is a rollback of a savepoint and data was not written to
1501 ** the database and the page is not in-memory, there is a potential
1502 ** problem. When the page is next fetched by the b-tree layer, it
1503 ** will be read from the database file, which may or may not be
1504 ** current.
1505 **
1506 ** There are a couple of different ways this can happen. All are quite
danielk1977401b65e2009-01-06 14:34:34 +00001507 ** obscure. When running in synchronous mode, this can only happen
danielk1977f2c31ad2009-01-06 13:40:08 +00001508 ** if the page is on the free-list at the start of the transaction, then
1509 ** populated, then moved using sqlite3PagerMovepage().
1510 **
1511 ** The solution is to add an in-memory page to the cache containing
1512 ** the data just read from the sub-journal. Mark the page as dirty
1513 ** and if the pager requires a journal-sync, then mark the page as
1514 ** requiring a journal-sync before it is written.
1515 */
1516 assert( isSavepnt );
1517 if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1)) ){
1518 return rc;
1519 }
1520 pPg->flags &= ~PGHDR_NEED_READ;
1521 sqlite3PcacheMakeDirty(pPg);
danielk1977a3f3a5f2004-06-10 04:32:16 +00001522 }
drhfa86c412002-02-02 15:01:15 +00001523 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001524 /* No page should ever be explicitly rolled back that is in use, except
1525 ** for page 1 which is held in use in order to keep the lock on the
1526 ** database active. However such a page may be rolled back as a result
1527 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001528 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001529 */
drhb6f41482004-05-14 01:58:11 +00001530 void *pData;
danielk19778c0a7912008-08-20 14:49:23 +00001531 pData = pPg->pData;
drhae2b40c2004-06-09 19:03:54 +00001532 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001533 if( pPager->xReiniter ){
danielk1977eaa06f62008-09-18 17:34:44 +00001534 pPager->xReiniter(pPg);
drhde647132004-05-07 17:57:49 +00001535 }
danielk1977443c0592009-01-16 15:21:05 +00001536 if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
danielk1977488af092008-12-19 16:31:11 +00001537 /* If the contents of this page were just restored from the main
1538 ** journal file, then its content must be as they were when the
1539 ** transaction was first opened. In this case we can mark the page
1540 ** as clean, since there will be no need to write it out to the.
1541 **
1542 ** There is one exception to this rule. If the page is being rolled
1543 ** back as part of a savepoint (or statement) rollback from an
1544 ** unsynced portion of the main journal file, then it is not safe
1545 ** to mark the page as clean. This is because marking the page as
1546 ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
1547 ** already in the journal file (recorded in Pager.pInJournal) and
1548 ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
1549 ** again within this transaction, it will be marked as dirty but
1550 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
1551 ** be written out into the database file before its journal file
1552 ** segment is synced. If a crash occurs during or following this,
1553 ** database corruption may ensue.
1554 */
drhc047b9f2008-11-21 03:23:43 +00001555 sqlite3PcacheMakeClean(pPg);
1556 }
danielk19773c407372005-02-15 02:54:14 +00001557#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001558 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001559#endif
drh86a88112007-04-16 15:02:19 +00001560 /* If this was page 1, then restore the value of Pager.dbFileVers.
1561 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001562 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001563 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001564 }
drh86a88112007-04-16 15:02:19 +00001565
1566 /* Decode the page just read from disk */
1567 CODEC1(pPager, pData, pPg->pgno, 3);
danielk19778c0a7912008-08-20 14:49:23 +00001568 sqlite3PcacheRelease(pPg);
drhfa86c412002-02-02 15:01:15 +00001569 }
1570 return rc;
1571}
1572
drhee03d622009-01-07 15:33:45 +00001573#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
drhd6e5e092009-01-07 02:03:35 +00001574/*
1575** This routine looks ahead into the main journal file and determines
1576** whether or not the next record (the record that begins at file
1577** offset pPager->journalOff) is a well-formed page record consisting
1578** of a valid page number, pPage->pageSize bytes of content, followed
1579** by a valid checksum.
1580**
1581** The pager never needs to know this in order to do its job. This
1582** routine is only used from with assert() and testcase() macros.
1583*/
1584static int pagerNextJournalPageIsValid(Pager *pPager){
1585 Pgno pgno; /* The page number of the page */
1586 u32 cksum; /* The page checksum */
1587 int rc; /* Return code from read operations */
1588 sqlite3_file *fd; /* The file descriptor from which we are reading */
1589 u8 *aData; /* Content of the page */
1590
1591 /* Read the page number header */
1592 fd = pPager->jfd;
1593 rc = read32bits(fd, pPager->journalOff, &pgno);
1594 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1595 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ return 0; } /*NO_TEST*/
1596 if( pgno>(Pgno)pPager->dbSize ){ return 0; } /*NO_TEST*/
1597
1598 /* Read the checksum */
1599 rc = read32bits(fd, pPager->journalOff+pPager->pageSize+4, &cksum);
1600 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1601
1602 /* Read the data and verify the checksum */
1603 aData = (u8*)pPager->pTmpSpace;
1604 rc = sqlite3OsRead(fd, aData, pPager->pageSize, pPager->journalOff+4);
1605 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1606 if( pager_cksum(pPager, aData)!=cksum ){ return 0; } /*NO_TEST*/
1607
1608 /* Reach this point only if the page is valid */
1609 return 1;
1610}
1611#endif /* !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST) */
1612
drhfa86c412002-02-02 15:01:15 +00001613/*
danielk197713adf8a2004-06-03 16:08:41 +00001614** Parameter zMaster is the name of a master journal file. A single journal
1615** file that referred to the master journal file has just been rolled back.
1616** This routine checks if it is possible to delete the master journal file,
1617** and does so if it is.
drh726de592004-06-10 23:35:50 +00001618**
danielk197765839c62007-08-30 08:08:17 +00001619** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1620** available for use within this function.
1621**
danielk1977443c0592009-01-16 15:21:05 +00001622** When a master journal file is created, it is populated with the names
1623** of all of its child journals, one after another, formatted as utf-8
1624** encoded text. The end of each child journal file is marked with a
1625** nul-terminator byte (0x00). i.e. the entire contents of a master journal
1626** file for a transaction involving two databases might be:
danielk197765839c62007-08-30 08:08:17 +00001627**
danielk1977443c0592009-01-16 15:21:05 +00001628** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
1629**
1630** A master journal file may only be deleted once all of its child
1631** journals have been rolled back.
1632**
1633** This function reads the contents of the master-journal file into
1634** memory and loops through each of the child journal names. For
1635** each child journal, it checks if:
1636**
1637** * if the child journal exists, and if so
1638** * if the child journal contains a reference to master journal
1639** file zMaster
1640**
1641** If a child journal can be found that matches both of the criteria
1642** above, this function returns without doing anything. Otherwise, if
1643** no such child journal can be found, file zMaster is deleted from
1644** the file-system using sqlite3OsDelete().
1645**
1646** If an IO error within this function, an error code is returned. This
1647** function allocates memory by calling sqlite3Malloc(). If an allocation
1648** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
1649** occur, SQLITE_OK is returned.
1650**
1651** TODO: This function allocates a single block of memory to load
1652** the entire contents of the master journal file. This could be
1653** a couple of kilobytes or so - potentially larger than the page
1654** size.
danielk197713adf8a2004-06-03 16:08:41 +00001655*/
danielk1977b4b47412007-08-17 15:53:36 +00001656static int pager_delmaster(Pager *pPager, const char *zMaster){
1657 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977443c0592009-01-16 15:21:05 +00001658 int rc; /* Return code */
1659 sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
1660 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
danielk197713adf8a2004-06-03 16:08:41 +00001661 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001662 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001663
1664 /* Open the master journal file exclusively in case some other process
1665 ** is running this routine also. Not that it makes too much difference.
1666 */
drhe5ae5732008-06-15 02:51:47 +00001667 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001668 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001669 if( !pMaster ){
1670 rc = SQLITE_NOMEM;
1671 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001672 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1673 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001674 }
danielk197713adf8a2004-06-03 16:08:41 +00001675 if( rc!=SQLITE_OK ) goto delmaster_out;
danielk1977b4b47412007-08-17 15:53:36 +00001676
1677 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001678 if( rc!=SQLITE_OK ) goto delmaster_out;
1679
1680 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001681 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001682 char *zMasterPtr = 0;
danielk1977443c0592009-01-16 15:21:05 +00001683 int nMasterPtr = pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001684
1685 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001686 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001687 */
drh4f21c4a2008-12-10 22:15:00 +00001688 zMasterJournal = (char *)sqlite3Malloc((int)nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001689 if( !zMasterJournal ){
1690 rc = SQLITE_NOMEM;
1691 goto delmaster_out;
1692 }
danielk197765839c62007-08-30 08:08:17 +00001693 zMasterPtr = &zMasterJournal[nMasterJournal];
drh4f21c4a2008-12-10 22:15:00 +00001694 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001695 if( rc!=SQLITE_OK ) goto delmaster_out;
1696
danielk19775865e3d2004-06-14 06:03:57 +00001697 zJournal = zMasterJournal;
1698 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001699 int exists;
1700 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1701 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001702 goto delmaster_out;
1703 }
danielk1977861f7452008-06-05 11:39:11 +00001704 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001705 /* One of the journals pointed to by the master journal exists.
1706 ** Open it and check if it points at the master journal. If
1707 ** so, return without deleting the master journal file.
1708 */
drh3b7b78b2004-11-24 01:16:43 +00001709 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001710 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1711 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001712 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001713 goto delmaster_out;
1714 }
danielk197713adf8a2004-06-03 16:08:41 +00001715
danielk197765839c62007-08-30 08:08:17 +00001716 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001717 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001718 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001719 goto delmaster_out;
1720 }
danielk197776572402004-06-25 02:38:54 +00001721
danielk197765839c62007-08-30 08:08:17 +00001722 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001723 if( c ){
danielk197776572402004-06-25 02:38:54 +00001724 /* We have a match. Do not delete the master journal file. */
1725 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001726 }
1727 }
drhea678832008-12-10 19:26:22 +00001728 zJournal += (sqlite3Strlen30(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001729 }
1730 }
1731
danielk1977fee2d252007-08-18 10:59:19 +00001732 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001733
1734delmaster_out:
1735 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001736 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001737 }
danielk1977443c0592009-01-16 15:21:05 +00001738 if( pMaster ){
danielk1977b4b47412007-08-17 15:53:36 +00001739 sqlite3OsClose(pMaster);
danielk1977443c0592009-01-16 15:21:05 +00001740 assert( !isOpen(pJournal) );
danielk197713adf8a2004-06-03 16:08:41 +00001741 }
danielk1977b4b47412007-08-17 15:53:36 +00001742 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001743 return rc;
1744}
1745
drha6abd042004-06-09 17:37:22 +00001746
drha6abd042004-06-09 17:37:22 +00001747/*
danielk1977443c0592009-01-16 15:21:05 +00001748** This function is used to change the actual size of the database
1749** file in the file-system. This only happens when committing a transaction,
1750** or rolling back a transaction (including rolling back a hot-journal).
drh7fe3f7e2007-11-29 18:44:27 +00001751**
danielk1977443c0592009-01-16 15:21:05 +00001752** If the main database file is not open, or an exclusive lock is not
1753** held, this function is a no-op. Otherwise, the size of the file is
1754** changed to nPage pages (nPage*pPager->pageSize bytes). If the file
1755** on disk is currently larger than nPage pages, then use the VFS
1756** xTruncate() method to truncate it.
1757**
1758** Or, it might might be the case that the file on disk is smaller than
1759** nPage pages. Some operating system implementations can get confused if
1760** you try to truncate a file to some size that is larger than it
1761** currently is, so detect this case and write a single zero byte to
1762** the end of the new file instead.
1763**
1764** If successful, return SQLITE_OK. If an IO error occurs while modifying
1765** the database file, return the error code to the caller.
drhcb4c40b2004-08-18 19:09:43 +00001766*/
danielk1977d92db532008-11-17 04:56:24 +00001767static int pager_truncate(Pager *pPager, Pgno nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001768 int rc = SQLITE_OK;
danielk1977443c0592009-01-16 15:21:05 +00001769 if( pPager->state>=PAGER_EXCLUSIVE && isOpen(pPager->fd) ){
drh7fe3f7e2007-11-29 18:44:27 +00001770 i64 currentSize, newSize;
danielk1977443c0592009-01-16 15:21:05 +00001771 /* TODO: Is it safe to use Pager.dbFileSize here? */
drh7fe3f7e2007-11-29 18:44:27 +00001772 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1773 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001774 if( rc==SQLITE_OK && currentSize!=newSize ){
1775 if( currentSize>newSize ){
1776 rc = sqlite3OsTruncate(pPager->fd, newSize);
1777 }else{
1778 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1779 }
danielk19773460d192008-12-27 15:23:13 +00001780 if( rc==SQLITE_OK ){
1781 pPager->dbFileSize = nPage;
1782 }
drh7fe3f7e2007-11-29 18:44:27 +00001783 }
danielk1977e180dd92007-04-05 17:15:52 +00001784 }
danielk1977e180dd92007-04-05 17:15:52 +00001785 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001786}
1787
1788/*
danielk1977443c0592009-01-16 15:21:05 +00001789** Set the value of the Pager.sectorSize variable for the given
1790** pager based on the value returned by the xSectorSize method
1791** of the open database file. The sector size will be used used
1792** to determine the size and alignment of journal header and
1793** master journal pointers within created journal files.
drhc80f0582007-05-01 16:59:48 +00001794**
danielk1977443c0592009-01-16 15:21:05 +00001795** For temporary files the effective sector size is always 512 bytes.
1796**
1797** Otherwise, for non-temporary files, the effective sector size is
1798** the value returned by the xSectorSize() method rounded up to 512 if
1799** it is less than 512, or rounded down to MAX_SECTOR_SIZE if it
1800** is greater than MAX_SECTOR_SIZE.
drhc80f0582007-05-01 16:59:48 +00001801*/
1802static void setSectorSize(Pager *pPager){
danielk1977443c0592009-01-16 15:21:05 +00001803 assert( isOpen(pPager->fd) || pPager->tempFile );
1804
danielk19777a2b1ee2007-08-21 14:27:01 +00001805 if( !pPager->tempFile ){
1806 /* Sector size doesn't matter for temporary files. Also, the file
danielk1977443c0592009-01-16 15:21:05 +00001807 ** may not have been opened yet, in which case the OsSectorSize()
danielk19777a2b1ee2007-08-21 14:27:01 +00001808 ** call will segfault.
1809 */
1810 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1811 }
drh334c80d2008-03-28 17:41:13 +00001812 if( pPager->sectorSize<512 ){
1813 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001814 }
danielk19777cbd5892009-01-10 16:15:09 +00001815 if( pPager->sectorSize>MAX_SECTOR_SIZE ){
danielk1977443c0592009-01-16 15:21:05 +00001816 assert( MAX_SECTOR_SIZE>=512 );
danielk19777cbd5892009-01-10 16:15:09 +00001817 pPager->sectorSize = MAX_SECTOR_SIZE;
1818 }
drhc80f0582007-05-01 16:59:48 +00001819}
1820
1821/*
drhed7c8552001-04-11 14:29:21 +00001822** Playback the journal and thus restore the database file to
1823** the state it was in before we started making changes.
1824**
drh34e79ce2004-02-08 06:05:46 +00001825** The journal file format is as follows:
1826**
drhae2b40c2004-06-09 19:03:54 +00001827** (1) 8 byte prefix. A copy of aJournalMagic[].
1828** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001829** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001830** number of page records from the journal size.
1831** (3) 4 byte big-endian integer which is the initial value for the
1832** sanity checksum.
1833** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001834** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001835** (5) 4 byte big-endian integer which is the sector size. The header
1836** is this many bytes in size.
1837** (6) 4 byte big-endian integer which is the page case.
1838** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001839** name. The value may be zero (indicate that there is no master
1840** journal.)
drh334c80d2008-03-28 17:41:13 +00001841** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001842** and might be shorter than the value read from (5). If the first byte
1843** of the name is \000 then there is no master journal. The master
1844** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001845** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001846** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001847** + pPager->pageSize bytes of data.
1848** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001849**
drh334c80d2008-03-28 17:41:13 +00001850** When we speak of the journal header, we mean the first 8 items above.
1851** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001852**
1853** Call the value from the second bullet "nRec". nRec is the number of
1854** valid page entries in the journal. In most cases, you can compute the
1855** value of nRec from the size of the journal file. But if a power
1856** failure occurred while the journal was being written, it could be the
1857** case that the size of the journal file had already been increased but
1858** the extra entries had not yet made it safely to disk. In such a case,
1859** the value of nRec computed from the file size would be too large. For
1860** that reason, we always use the nRec value in the header.
1861**
1862** If the nRec value is 0xffffffff it means that nRec should be computed
1863** from the file size. This value is used when the user selects the
1864** no-sync option for the journal. A power failure could lead to corruption
1865** in this case. But for things like temporary table (which will be
1866** deleted when the power is restored) we don't care.
1867**
drhd9b02572001-04-15 00:37:09 +00001868** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001869** journal file then all pages up to the first corrupted page are rolled
1870** back (or no pages if the journal header is corrupted). The journal file
1871** is then deleted and SQLITE_OK returned, just as if no corruption had
1872** been encountered.
1873**
1874** If an I/O or malloc() error occurs, the journal-file is not deleted
1875** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001876*/
danielk1977e277be02007-03-23 18:12:06 +00001877static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001878 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001879 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001880 u32 nRec; /* Number of Records in the journal */
shane0b8d2762008-07-22 05:18:00 +00001881 u32 u; /* Unsigned loop counter */
drhd9b02572001-04-15 00:37:09 +00001882 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001883 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001884 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001885 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001886
drhc3a64ba2001-11-22 00:01:27 +00001887 /* Figure out how many records are in the journal. Abort early if
1888 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001889 */
danielk1977443c0592009-01-16 15:21:05 +00001890 assert( isOpen(pPager->jfd) );
drh054889e2005-11-30 03:20:31 +00001891 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001892 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001893 goto end_playback;
1894 }
drh240c5792004-02-08 00:40:52 +00001895
danielk197776572402004-06-25 02:38:54 +00001896 /* Read the master journal name from the journal, if it is present.
1897 ** If a master journal file name is specified, but the file is not
1898 ** present on disk, then the journal is not hot and does not need to be
1899 ** played back.
danielk1977443c0592009-01-16 15:21:05 +00001900 **
1901 ** TODO: Technically the following is an error because it assumes that
1902 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
1903 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
1904 ** mxPathname is 512, which is the same as the minimum allowable value
1905 ** for pageSize.
drh240c5792004-02-08 00:40:52 +00001906 */
danielk197765839c62007-08-30 08:08:17 +00001907 zMaster = pPager->pTmpSpace;
1908 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001909 if( rc==SQLITE_OK && zMaster[0] ){
1910 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001911 }
danielk197765839c62007-08-30 08:08:17 +00001912 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001913 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001914 goto end_playback;
1915 }
1916 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001917
danielk1977443c0592009-01-16 15:21:05 +00001918 /* This loop terminates either when a readJournalHdr() or
1919 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
1920 ** occurs.
1921 */
danielk197776572402004-06-25 02:38:54 +00001922 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001923
danielk197776572402004-06-25 02:38:54 +00001924 /* Read the next journal header from the journal file. If there are
1925 ** not enough bytes left in the journal file for a complete header, or
1926 ** it is corrupted, then a process must of failed while writing it.
1927 ** This indicates nothing more needs to be rolled back.
1928 */
1929 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1930 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001931 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001932 rc = SQLITE_OK;
1933 }
danielk197776572402004-06-25 02:38:54 +00001934 goto end_playback;
1935 }
1936
1937 /* If nRec is 0xffffffff, then this journal was created by a process
1938 ** working in no-sync mode. This means that the rest of the journal
1939 ** file consists of pages, there are no more journal headers. Compute
1940 ** the value of nRec based on this assumption.
1941 */
1942 if( nRec==0xffffffff ){
1943 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
drh4f21c4a2008-12-10 22:15:00 +00001944 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
danielk197776572402004-06-25 02:38:54 +00001945 }
1946
danielk1977e277be02007-03-23 18:12:06 +00001947 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001948 ** process and if this is the final header in the journal, then it means
1949 ** that this part of the journal was being filled but has not yet been
1950 ** synced to disk. Compute the number of pages based on the remaining
1951 ** size of the file.
1952 **
1953 ** The third term of the test was added to fix ticket #2565.
drhd6e5e092009-01-07 02:03:35 +00001954 ** When rolling back a hot journal, nRec==0 always means that the next
1955 ** chunk of the journal contains zero pages to be rolled back. But
1956 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
1957 ** the journal, it means that the journal might contain additional
1958 ** pages that need to be rolled back and that the number of pages
drhee03d622009-01-07 15:33:45 +00001959 ** should be computed based on the journal file size.
danielk1977e277be02007-03-23 18:12:06 +00001960 */
drh4fd18c42009-01-08 15:24:01 +00001961 testcase( nRec==0 && !isHot
drhd6e5e092009-01-07 02:03:35 +00001962 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)!=pPager->journalOff
1963 && ((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager))>0
drh4fd18c42009-01-08 15:24:01 +00001964 && pagerNextJournalPageIsValid(pPager)
drhd6e5e092009-01-07 02:03:35 +00001965 );
drh8940f4e2007-08-11 00:26:20 +00001966 if( nRec==0 && !isHot &&
1967 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
drh4f21c4a2008-12-10 22:15:00 +00001968 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
danielk1977e277be02007-03-23 18:12:06 +00001969 }
1970
danielk197776572402004-06-25 02:38:54 +00001971 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001972 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001973 */
danielk1977e180dd92007-04-05 17:15:52 +00001974 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001975 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001976 if( rc!=SQLITE_OK ){
1977 goto end_playback;
1978 }
danielk1977f90b7262009-01-07 15:18:20 +00001979 pPager->dbSize = mxPg;
danielk197776572402004-06-25 02:38:54 +00001980 }
1981
danielk1977443c0592009-01-16 15:21:05 +00001982 /* Copy original pages out of the journal and back into the
1983 ** database file and/or page cache.
danielk197776572402004-06-25 02:38:54 +00001984 */
shane0b8d2762008-07-22 05:18:00 +00001985 for(u=0; u<nRec; u++){
drhd6e5e092009-01-07 02:03:35 +00001986 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 0, 0);
danielk197776572402004-06-25 02:38:54 +00001987 if( rc!=SQLITE_OK ){
1988 if( rc==SQLITE_DONE ){
1989 rc = SQLITE_OK;
1990 pPager->journalOff = szJ;
1991 break;
1992 }else{
drha9625ea2008-09-03 00:08:29 +00001993 /* If we are unable to rollback, then the database is probably
1994 ** going to end up being corrupt. It is corrupt to us, anyhow.
1995 ** Perhaps the next process to come along can fix it....
1996 */
danielk197798c21902008-09-23 16:41:29 +00001997 rc = SQLITE_CORRUPT_BKPT;
danielk197776572402004-06-25 02:38:54 +00001998 goto end_playback;
1999 }
2000 }
drh968af522003-02-11 14:55:40 +00002001 }
drhed7c8552001-04-11 14:29:21 +00002002 }
drh580eeaf2006-02-24 03:09:37 +00002003 /*NOTREACHED*/
2004 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00002005
2006end_playback:
drh8f941bc2009-01-14 23:03:40 +00002007 /* Following a rollback, the database file should be back in its original
2008 ** state prior to the start of the transaction, so invoke the
2009 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
2010 ** assertion that the transaction counter was modified.
2011 */
2012 assert(
2013 pPager->fd->pMethods==0 ||
2014 sqlite3OsFileControl(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0)>=SQLITE_OK
2015 );
2016
danielk19778191bff2004-06-28 04:52:30 +00002017 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00002018 zMaster = pPager->pTmpSpace;
2019 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977443c0592009-01-16 15:21:05 +00002020 testcase( rc!=SQLITE_OK );
danielk197765839c62007-08-30 08:08:17 +00002021 }
2022 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00002023 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk1977443c0592009-01-16 15:21:05 +00002024 testcase( rc!=SQLITE_OK );
danielk19778191bff2004-06-28 04:52:30 +00002025 }
danielk1977c56774e2008-10-07 11:51:20 +00002026 if( rc==SQLITE_OK && zMaster[0] && res ){
danielk1977979f38e2007-03-27 16:19:51 +00002027 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00002028 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00002029 */
danielk197765839c62007-08-30 08:08:17 +00002030 rc = pager_delmaster(pPager, zMaster);
danielk1977443c0592009-01-16 15:21:05 +00002031 testcase( rc!=SQLITE_OK );
danielk197713adf8a2004-06-03 16:08:41 +00002032 }
danielk197776572402004-06-25 02:38:54 +00002033
2034 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00002035 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00002036 ** value. Reset it to the correct value for this process.
2037 */
drhc80f0582007-05-01 16:59:48 +00002038 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00002039 return rc;
drhed7c8552001-04-11 14:29:21 +00002040}
2041
2042/*
danielk1977443c0592009-01-16 15:21:05 +00002043** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
2044** the entire master journal file. The case pSavepoint==NULL occurs when
2045** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
2046** savepoint.
drhd6e5e092009-01-07 02:03:35 +00002047**
danielk1977443c0592009-01-16 15:21:05 +00002048** When pSavepoint is not NULL (meaning a non-transaction savepoint is
2049** being rolled back), then the rollback consists of up to three stages,
2050** performed in the order specified:
2051**
2052** * Pages are played back from the main journal starting at byte
2053** offset PagerSavepoint.iOffset and continuing to
2054** PagerSavepoint.iHdrOffset, or to the end of the main journal
2055** file if PagerSavepoint.iHdrOffset is zero.
2056**
2057** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
2058** back starting from the journal header immediately following
2059** PagerSavepoint.iHdrOffset to the end of the main journal file.
2060**
2061** * Pages are then played back from the sub-journal file, starting
2062** with the PagerSavepoint.iSubRec and continuing to the end of
2063** the journal file.
2064**
2065** Throughout the rollback process, each time a page is rolled back, the
2066** corresponding bit is set in a bitvec structure (variable pDone in the
2067** implementation below). This is used to ensure that a page is only
2068** rolled back the first time it is encountered in either journal.
2069**
2070** If pSavepoint is NULL, then pages are only played back from the main
2071** journal file. There is no need for a bitvec in this case.
2072**
2073** In either case, before playback commences the Pager.dbSize variable
2074** is reset to the value that it held at the start of the savepoint
2075** (or transaction). No page with a page-number greater than this value
2076** is played back. If one is encountered it is simply skipped.
drhfa86c412002-02-02 15:01:15 +00002077*/
danielk1977fd7f0452008-12-17 17:30:26 +00002078static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
drhd6e5e092009-01-07 02:03:35 +00002079 i64 szJ; /* Effective size of the main journal */
danielk1977fd7f0452008-12-17 17:30:26 +00002080 i64 iHdrOff; /* End of first segment of main-journal records */
danielk1977f2c31ad2009-01-06 13:40:08 +00002081 int rc = SQLITE_OK; /* Return code */
danielk1977fd7f0452008-12-17 17:30:26 +00002082 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
drhfa86c412002-02-02 15:01:15 +00002083
danielk1977443c0592009-01-16 15:21:05 +00002084 assert( pPager->state>=PAGER_SHARED );
2085
danielk1977fd7f0452008-12-17 17:30:26 +00002086 /* Allocate a bitvec to use to store the set of pages rolled back */
2087 if( pSavepoint ){
2088 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
2089 if( !pDone ){
2090 return SQLITE_NOMEM;
2091 }
danielk197776572402004-06-25 02:38:54 +00002092 }
danielk1977fd7f0452008-12-17 17:30:26 +00002093
danielk1977443c0592009-01-16 15:21:05 +00002094 /* Set the database size back to the value it was before the savepoint
2095 ** being reverted was opened.
drhfa86c412002-02-02 15:01:15 +00002096 */
drhd6e5e092009-01-07 02:03:35 +00002097 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
drhfa86c412002-02-02 15:01:15 +00002098
drhd6e5e092009-01-07 02:03:35 +00002099 /* Use pPager->journalOff as the effective size of the main rollback
2100 ** journal. The actual file might be larger than this in
2101 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
2102 ** past pPager->journalOff is off-limits to us.
drhfa86c412002-02-02 15:01:15 +00002103 */
danielk1977fd7f0452008-12-17 17:30:26 +00002104 szJ = pPager->journalOff;
drhd6e5e092009-01-07 02:03:35 +00002105
2106 /* Begin by rolling back records from the main journal starting at
2107 ** PagerSavepoint.iOffset and continuing to the next journal header.
2108 ** There might be records in the main journal that have a page number
2109 ** greater than the current database size (pPager->dbSize) but those
2110 ** will be skipped automatically. Pages are added to pDone as they
2111 ** are played back.
2112 */
danielk1977fd7f0452008-12-17 17:30:26 +00002113 if( pSavepoint ){
2114 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
2115 pPager->journalOff = pSavepoint->iOffset;
2116 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
drhd6e5e092009-01-07 02:03:35 +00002117 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone);
danielk1977fd7f0452008-12-17 17:30:26 +00002118 }
danielk1977443c0592009-01-16 15:21:05 +00002119 assert( rc!=SQLITE_DONE );
danielk1977fd7f0452008-12-17 17:30:26 +00002120 }else{
2121 pPager->journalOff = 0;
drhfa86c412002-02-02 15:01:15 +00002122 }
drhd6e5e092009-01-07 02:03:35 +00002123
2124 /* Continue rolling back records out of the main journal starting at
2125 ** the first journal header seen and continuing until the effective end
2126 ** of the main journal file. Continue to skip out-of-range pages and
2127 ** continue adding pages rolled back to pDone.
2128 */
danielk1977fd7f0452008-12-17 17:30:26 +00002129 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
danielk1977443c0592009-01-16 15:21:05 +00002130 u32 ii; /* Loop counter */
danielk1977c81806f2009-01-01 15:20:37 +00002131 u32 nJRec = 0; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00002132 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00002133 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
danielk1977fd7f0452008-12-17 17:30:26 +00002134 assert( rc!=SQLITE_DONE );
drhd6e5e092009-01-07 02:03:35 +00002135
2136 /*
2137 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
2138 ** test is related to ticket #2565. See the discussion in the
2139 ** pager_playback() function for additional information.
2140 */
drhee03d622009-01-07 15:33:45 +00002141 assert( !(nJRec==0
drhd6e5e092009-01-07 02:03:35 +00002142 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)!=pPager->journalOff
2143 && ((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager))>0
drhee03d622009-01-07 15:33:45 +00002144 && pagerNextJournalPageIsValid(pPager))
drhd6e5e092009-01-07 02:03:35 +00002145 );
2146 if( nJRec==0
2147 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
2148 ){
2149 nJRec = (szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager);
danielk197775edc162004-06-26 01:48:18 +00002150 }
danielk197712dd5492008-12-18 15:45:07 +00002151 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
drhd6e5e092009-01-07 02:03:35 +00002152 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone);
danielk1977fd7f0452008-12-17 17:30:26 +00002153 }
danielk1977443c0592009-01-16 15:21:05 +00002154 assert( rc!=SQLITE_DONE );
danielk1977fd7f0452008-12-17 17:30:26 +00002155 }
2156 assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
2157
drhd6e5e092009-01-07 02:03:35 +00002158 /* Finally, rollback pages from the sub-journal. Page that were
2159 ** previously rolled back out of the main journal (and are hence in pDone)
2160 ** will be skipped. Out-of-range pages are also skipped.
2161 */
danielk1977fd7f0452008-12-17 17:30:26 +00002162 if( pSavepoint ){
danielk1977443c0592009-01-16 15:21:05 +00002163 u32 ii; /* Loop counter */
drhd6e5e092009-01-07 02:03:35 +00002164 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
danielk1977443c0592009-01-16 15:21:05 +00002165 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
2166 assert( offset==ii*(4+pPager->pageSize) );
drhd6e5e092009-01-07 02:03:35 +00002167 rc = pager_playback_one_page(pPager, 0, &offset, 1, pDone);
danielk197776572402004-06-25 02:38:54 +00002168 }
danielk1977443c0592009-01-16 15:21:05 +00002169 assert( rc!=SQLITE_DONE );
drhfa86c412002-02-02 15:01:15 +00002170 }
danielk197776572402004-06-25 02:38:54 +00002171
danielk1977fd7f0452008-12-17 17:30:26 +00002172 sqlite3BitvecDestroy(pDone);
2173 if( rc==SQLITE_OK ){
danielk197775edc162004-06-26 01:48:18 +00002174 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00002175 }
2176 return rc;
2177}
2178
2179/*
drhf57b14a2001-09-14 18:54:08 +00002180** Change the maximum number of in-memory pages that are allowed.
2181*/
danielk19773b8a05f2007-03-19 17:44:26 +00002182void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +00002183 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
drhf57b14a2001-09-14 18:54:08 +00002184}
2185
2186/*
drh973b6e32003-02-12 14:09:42 +00002187** Adjust the robustness of the database to damage due to OS crashes
2188** or power failures by changing the number of syncs()s when writing
2189** the rollback journal. There are three levels:
2190**
drh054889e2005-11-30 03:20:31 +00002191** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00002192** for temporary and transient files.
2193**
2194** NORMAL The journal is synced once before writes begin on the
2195** database. This is normally adequate protection, but
2196** it is theoretically possible, though very unlikely,
2197** that an inopertune power failure could leave the journal
2198** in a state which would cause damage to the database
2199** when it is rolled back.
2200**
2201** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00002202** database (with some additional information - the nRec field
2203** of the journal header - being written in between the two
2204** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00002205** single disk sector is atomic, then this mode provides
2206** assurance that the journal will not be corrupted to the
2207** point of causing damage to the database during rollback.
2208**
2209** Numeric values associated with these states are OFF==1, NORMAL=2,
2210** and FULL=3.
2211*/
danielk197793758c82005-01-21 08:13:14 +00002212#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh281b21d2008-08-22 12:57:08 +00002213void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
drh4f21c4a2008-12-10 22:15:00 +00002214 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
2215 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
drh281b21d2008-08-22 12:57:08 +00002216 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00002217 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00002218}
danielk197793758c82005-01-21 08:13:14 +00002219#endif
drh973b6e32003-02-12 14:09:42 +00002220
2221/*
drhaf6df112005-06-07 02:12:30 +00002222** The following global variable is incremented whenever the library
2223** attempts to open a temporary file. This information is used for
2224** testing and analysis only.
2225*/
drh0f7eb612006-08-08 13:51:43 +00002226#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002227int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00002228#endif
drhaf6df112005-06-07 02:12:30 +00002229
2230/*
danielk1977443c0592009-01-16 15:21:05 +00002231** Open a temporary file.
drh3f56e6e2007-03-15 01:16:47 +00002232**
danielk1977443c0592009-01-16 15:21:05 +00002233** Write the file descriptor into *pFile. Return SQLITE_OK on success
2234** or some other error code if we fail. The OS will automatically
2235** delete the temporary file when it is closed.
2236**
2237** The flags passed to the VFS layer xOpen() call are those specified
2238** by parameter vfsFlags ORed with the following:
2239**
2240** SQLITE_OPEN_READWRITE
2241** SQLITE_OPEN_CREATE
2242** SQLITE_OPEN_EXCLUSIVE
2243** SQLITE_OPEN_DELETEONCLOSE
drhfa86c412002-02-02 15:01:15 +00002244*/
danielk1977443c0592009-01-16 15:21:05 +00002245static int pagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00002246 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00002247 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00002248 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00002249){
danielk1977443c0592009-01-16 15:21:05 +00002250 int rc; /* Return code */
drh3f56e6e2007-03-15 01:16:47 +00002251
drh0f7eb612006-08-08 13:51:43 +00002252#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002253 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002254#endif
danielk1977b4b47412007-08-17 15:53:36 +00002255
drh33f4e022007-09-03 15:19:34 +00002256 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2257 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00002258 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
danielk1977443c0592009-01-16 15:21:05 +00002259 assert( rc!=SQLITE_OK || isOpen(pFile) );
drhfa86c412002-02-02 15:01:15 +00002260 return rc;
2261}
2262
danielk1977a858aa22008-08-22 16:22:17 +00002263static int pagerStress(void *,PgHdr *);
danielk19778c0a7912008-08-20 14:49:23 +00002264
drhfa86c412002-02-02 15:01:15 +00002265/*
danielk1977443c0592009-01-16 15:21:05 +00002266** Allocate and initialize a new Pager object and put a pointer to it
2267** in *ppPager. The pager should eventually be freed by passing it
2268** to sqlite3PagerClose().
drh382c0242001-10-06 16:33:02 +00002269**
danielk1977443c0592009-01-16 15:21:05 +00002270** The zFilename argument is the path to the database file to open.
drh6446c4d2001-12-15 14:22:18 +00002271** If zFilename is NULL then a randomly-named temporary file is created
danielk1977443c0592009-01-16 15:21:05 +00002272** and used as the file to be cached. Temporary files are be deleted
2273** automatically when they are closed. If zFilename is ":memory:" then
2274** all information is held in cache. It is never written to disk.
2275** This can be used to implement an in-memory database.
drh90f5ecb2004-07-22 01:19:35 +00002276**
danielk1977443c0592009-01-16 15:21:05 +00002277** The nExtra parameter specifies the number of bytes of space allocated
2278** along with each page reference. This space is available to the user
2279** via the sqlite3PagerGetExtra() API.
2280**
2281** The flags argument is used to specify properties that affect the
2282** operation of the pager. It should be passed some bitwise combination
2283** of the PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK flags.
2284**
2285** The vfsFlags parameter is a bitmask to pass to the flags parameter
2286** of the xOpen() method of the supplied VFS when opening files.
2287**
2288** If the pager object is allocated and the specified file opened
2289** successfully, SQLITE_OK is returned and *ppPager set to point to
2290** the new pager object. If an error occurs, *ppPager is set to NULL
2291** and error code returned. This function may return SQLITE_NOMEM
2292** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
2293** various SQLITE_IO_XXX errors.
drhed7c8552001-04-11 14:29:21 +00002294*/
danielk19773b8a05f2007-03-19 17:44:26 +00002295int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002296 sqlite3_vfs *pVfs, /* The virtual file system to use */
danielk1977443c0592009-01-16 15:21:05 +00002297 Pager **ppPager, /* OUT: Return the Pager structure here */
drh7e3b0a02001-04-28 16:52:40 +00002298 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002299 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002300 int flags, /* flags controlling this file */
2301 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002302){
danielk1977b4b47412007-08-17 15:53:36 +00002303 u8 *pPtr;
danielk1977443c0592009-01-16 15:21:05 +00002304 Pager *pPager = 0; /* Pager object to allocate and return */
2305 int rc = SQLITE_OK; /* Return code */
2306 int tempFile = 0; /* True for temp files (incl. in-memory files) */
2307 int memDb = 0; /* True if this is an in-memory file */
2308 int readOnly = 0; /* True if this is a read-only file */
2309 int journalFileSize; /* Bytes to allocate for each journal fd */
2310 char *zPathname = 0; /* Full path to database file */
2311 int nPathname = 0; /* Number of bytes in zPathname */
2312 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
2313 int noReadlock = (flags & PAGER_NO_READLOCK)!=0; /* True to omit read-lock */
2314 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
2315 u16 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
danielk1977b4b47412007-08-17 15:53:36 +00002316
danielk1977443c0592009-01-16 15:21:05 +00002317 /* Figure out how much space is required for each journal file-handle
2318 ** (there are two of them, the main journal and the sub-journal). This
2319 ** is the maximum space required for an in-memory journal file handle
2320 ** and a regular journal file-handle. Note that a "regular journal-handle"
2321 ** may be a wrapper capable of caching the first portion of the journal
2322 ** file in memory to implement the atomic-write optimization (see
2323 ** source file journal.c).
2324 */
danielk1977b3175382008-10-17 18:51:52 +00002325 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
2326 journalFileSize = sqlite3JournalSize(pVfs);
2327 }else{
2328 journalFileSize = sqlite3MemJournalSize();
2329 }
2330
danielk1977443c0592009-01-16 15:21:05 +00002331 /* Set the output variable to NULL in case an error occurs. */
drhd9b02572001-04-15 00:37:09 +00002332 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002333
danielk197717b90b52008-06-06 11:11:25 +00002334 /* Compute and store the full pathname in an allocated buffer pointed
2335 ** to by zPathname, length nPathname. Or, if this is a temporary file,
2336 ** leave both nPathname and zPathname set to 0.
2337 */
drh1cc8c442007-08-24 16:08:29 +00002338 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00002339 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00002340 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00002341 if( zPathname==0 ){
2342 return SQLITE_NOMEM;
2343 }
drh1cc8c442007-08-24 16:08:29 +00002344#ifndef SQLITE_OMIT_MEMORYDB
2345 if( strcmp(zFilename,":memory:")==0 ){
2346 memDb = 1;
2347 zPathname[0] = 0;
2348 }else
2349#endif
2350 {
danielk1977adfb9b02007-09-17 07:02:56 +00002351 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002352 }
danielk1977443c0592009-01-16 15:21:05 +00002353
2354 nPathname = sqlite3Strlen30(zPathname);
2355 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
2356 /* This branch is taken when the journal path required by
2357 ** the database being opened will be more than pVfs->mxPathname
2358 ** bytes in length. This means the database cannot be opened,
2359 ** as it will not be possible to open the journal file or even
2360 ** check for a hot-journal before reading.
2361 */
2362 rc = SQLITE_CANTOPEN;
2363 }
danielk197717b90b52008-06-06 11:11:25 +00002364 if( rc!=SQLITE_OK ){
2365 sqlite3_free(zPathname);
2366 return rc;
2367 }
drhae28c012007-08-24 16:29:23 +00002368 }
drh99b90c32008-03-17 13:50:58 +00002369
danielk1977443c0592009-01-16 15:21:05 +00002370 /* Allocate memory for the Pager structure, PCache object, the
2371 ** three file descriptors, the database file name and the journal
2372 ** file name. The layout in memory is as follows:
2373 **
2374 ** Pager object (sizeof(Pager) bytes)
2375 ** PCache object (sqlite3PcacheSize() bytes)
2376 ** Database file handle (pVfs->szOsFile bytes)
2377 ** Sub-journal file handle (journalFileSize bytes)
2378 ** Main journal file handle (journalFileSize bytes)
2379 ** Database file name (nPathname+1 bytes)
2380 ** Journal file name (nPathname+8+1 bytes)
2381 */
2382 pPtr = (u8 *)sqlite3MallocZero(
danielk1977b4b47412007-08-17 15:53:36 +00002383 sizeof(*pPager) + /* Pager structure */
danielk19778c0a7912008-08-20 14:49:23 +00002384 pcacheSize + /* PCache object */
danielk1977b3175382008-10-17 18:51:52 +00002385 pVfs->szOsFile + /* The main db file */
2386 journalFileSize * 2 + /* The two journal files */
danielk1977443c0592009-01-16 15:21:05 +00002387 nPathname + 1 + /* zFilename */
2388 nPathname + 8 + 1 /* zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00002389 );
danielk1977443c0592009-01-16 15:21:05 +00002390 if( !pPtr ){
drh1cc8c442007-08-24 16:08:29 +00002391 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002392 return SQLITE_NOMEM;
2393 }
danielk1977443c0592009-01-16 15:21:05 +00002394 pPager = (Pager*)(pPtr);
2395 pPager->pPCache = (PCache*)(pPtr += sizeof(*pPager));
2396 pPager->fd = (sqlite3_file*)(pPtr += pcacheSize);
2397 pPager->sjfd = (sqlite3_file*)(pPtr += pVfs->szOsFile);
2398 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
2399 pPager->zFilename = (char*)(pPtr += journalFileSize);
2400
2401 /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
danielk197717b90b52008-06-06 11:11:25 +00002402 if( zPathname ){
danielk1977443c0592009-01-16 15:21:05 +00002403 pPager->zJournal = (char*)(pPtr += nPathname + 1);
2404 memcpy(pPager->zFilename, zPathname, nPathname);
2405 memcpy(pPager->zJournal, zPathname, nPathname);
2406 memcpy(&pPager->zJournal[nPathname], "-journal", 8);
danielk197717b90b52008-06-06 11:11:25 +00002407 sqlite3_free(zPathname);
2408 }
danielk1977443c0592009-01-16 15:21:05 +00002409 pPager->pVfs = pVfs;
2410 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002411
drh153c62c2007-08-24 03:51:33 +00002412 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002413 */
drhae28c012007-08-24 16:29:23 +00002414 if( zFilename && zFilename[0] && !memDb ){
danielk1977443c0592009-01-16 15:21:05 +00002415 int fout = 0; /* VFS flags returned by xOpen() */
2416 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
2417 readOnly = (fout&SQLITE_OPEN_READONLY);
drh1cc8c442007-08-24 16:08:29 +00002418
danielk1977443c0592009-01-16 15:21:05 +00002419 /* If the file was successfully opened for read/write access,
2420 ** choose a default page size in case we have to create the
2421 ** database file. The default page size is the maximum of:
2422 **
2423 ** + SQLITE_DEFAULT_PAGE_SIZE,
2424 ** + The value returned by sqlite3OsSectorSize()
2425 ** + The largest page size that can be written atomically.
2426 */
2427 if( rc==SQLITE_OK && !readOnly ){
2428 setSectorSize(pPager);
2429 if( szPageDflt<pPager->sectorSize ){
2430 szPageDflt = pPager->sectorSize;
2431 }
danielk19779663b8f2007-08-24 11:52:28 +00002432#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977443c0592009-01-16 15:21:05 +00002433 {
2434 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2435 int ii;
2436 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2437 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2438 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2439 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2440 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
2441 szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002442 }
danielk1977b4b47412007-08-17 15:53:36 +00002443 }
danielk1977443c0592009-01-16 15:21:05 +00002444 }
drh1cc8c442007-08-24 16:08:29 +00002445#endif
danielk1977443c0592009-01-16 15:21:05 +00002446 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2447 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
danielk19778def5ea2004-06-16 10:39:52 +00002448 }
drhac69b052004-05-12 13:30:07 +00002449 }
danielk1977b3175382008-10-17 18:51:52 +00002450 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00002451 /* If a temporary file is requested, it is not opened immediately.
2452 ** In this case we accept the default page size and delay actually
2453 ** opening the file until the first call to OsWrite().
danielk1977b3175382008-10-17 18:51:52 +00002454 **
2455 ** This branch is also run for an in-memory database. An in-memory
2456 ** database is the same as a temp-file that is never written out to
2457 ** disk and uses an in-memory rollback journal.
danielk19777a2b1ee2007-08-21 14:27:01 +00002458 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002459 tempFile = 1;
2460 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002461 }
danielk1977aef0bf62005-12-30 16:28:01 +00002462
danielk1977443c0592009-01-16 15:21:05 +00002463 /* The following call to PagerSetPagesize() serves to set the value of
2464 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
2465 */
2466 if( rc==SQLITE_OK ){
2467 assert( pPager->memDb==0 );
2468 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt);
2469 testcase( rc!=SQLITE_OK );
drh3e7a6092002-12-07 21:45:14 +00002470 }
danielk1977aef0bf62005-12-30 16:28:01 +00002471
danielk1977443c0592009-01-16 15:21:05 +00002472 /* If an error occured in either of the blocks above, free the
2473 ** Pager structure and close the file.
danielk1977aef0bf62005-12-30 16:28:01 +00002474 */
danielk1977443c0592009-01-16 15:21:05 +00002475 if( rc!=SQLITE_OK ){
2476 assert( !pPager->pTmpSpace );
danielk1977b4b47412007-08-17 15:53:36 +00002477 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002478 sqlite3_free(pPager);
danielk1977443c0592009-01-16 15:21:05 +00002479 return rc;
drhed7c8552001-04-11 14:29:21 +00002480 }
danielk1977443c0592009-01-16 15:21:05 +00002481
2482 /* Initialize the PCache object. */
danielk19778c0a7912008-08-20 14:49:23 +00002483 nExtra = FORCE_ALIGNMENT(nExtra);
danielk197771d5d2c2008-09-29 11:49:47 +00002484 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
drh41d30272008-08-20 21:47:45 +00002485 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
danielk1977aef0bf62005-12-30 16:28:01 +00002486
drh30d53702009-01-06 15:58:57 +00002487 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
drh153c62c2007-08-24 03:51:33 +00002488 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002489
drh4f21c4a2008-12-10 22:15:00 +00002490 pPager->useJournal = (u8)useJournal;
2491 pPager->noReadlock = (noReadlock && readOnly) ?1:0;
drh3b59a5c2006-01-15 20:28:28 +00002492 /* pPager->stmtOpen = 0; */
2493 /* pPager->stmtInUse = 0; */
2494 /* pPager->nRef = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002495 pPager->dbSizeValid = (u8)memDb;
drh3b59a5c2006-01-15 20:28:28 +00002496 /* pPager->stmtSize = 0; */
2497 /* pPager->stmtJSize = 0; */
2498 /* pPager->nPage = 0; */
drhf8e632b2007-05-08 14:51:36 +00002499 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002500 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002501 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002502 /* pPager->errMask = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002503 pPager->tempFile = (u8)tempFile;
drh369339d2007-03-30 16:01:55 +00002504 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2505 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2506 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
drh4f21c4a2008-12-10 22:15:00 +00002507 pPager->exclusiveMode = (u8)tempFile;
2508 pPager->memDb = (u8)memDb;
2509 pPager->readOnly = (u8)readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002510 /* pPager->needSync = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002511 pPager->noSync = (pPager->tempFile || !useJournal) ?1:0;
2512 pPager->fullSync = pPager->noSync ?0:1;
danielk1977f036aef2007-08-20 05:36:51 +00002513 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002514 /* pPager->pFirst = 0; */
2515 /* pPager->pFirstSynced = 0; */
2516 /* pPager->pLast = 0; */
danielk19778c0a7912008-08-20 14:49:23 +00002517 pPager->nExtra = nExtra;
danielk1977b53e4962008-06-04 06:45:59 +00002518 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk1977443c0592009-01-16 15:21:05 +00002519 assert( isOpen(pPager->fd) || tempFile );
danielk1977b3175382008-10-17 18:51:52 +00002520 setSectorSize(pPager);
2521 if( memDb ){
2522 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
danielk1977b4721172007-03-19 05:54:48 +00002523 }
danielk19771ceedd32008-11-19 10:22:33 +00002524 /* pPager->xBusyHandler = 0; */
2525 /* pPager->pBusyHandlerArg = 0; */
drh3b59a5c2006-01-15 20:28:28 +00002526 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002527 *ppPager = pPager;
2528 return SQLITE_OK;
2529}
2530
2531/*
drh90f5ecb2004-07-22 01:19:35 +00002532** Set the busy handler function.
danielk1977443c0592009-01-16 15:21:05 +00002533**
2534** The pager invokes the busy-handler if sqlite3OsLock() returns
2535** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
2536** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
2537** lock. It does *not* invoke the busy handler when upgrading from
2538** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
2539** (which occurs during hot-journal rollback). Summary:
2540**
2541** Transition | Invokes xBusyHandler
2542** --------------------------------------------------------
2543** NO_LOCK -> SHARED_LOCK | Yes
2544** SHARED_LOCK -> RESERVED_LOCK | No
2545** SHARED_LOCK -> EXCLUSIVE_LOCK | No
2546** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
2547**
2548** If the busy-handler callback returns non-zero, the lock is
2549** retried. If it returns zero, then the SQLITE_BUSY error is
2550** returned to the caller of the pager API function.
drh90f5ecb2004-07-22 01:19:35 +00002551*/
danielk19771ceedd32008-11-19 10:22:33 +00002552void sqlite3PagerSetBusyhandler(
danielk1977443c0592009-01-16 15:21:05 +00002553 Pager *pPager, /* Pager object */
2554 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
2555 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
danielk19771ceedd32008-11-19 10:22:33 +00002556){
2557 pPager->xBusyHandler = xBusyHandler;
2558 pPager->pBusyHandlerArg = pBusyHandlerArg;
drh90f5ecb2004-07-22 01:19:35 +00002559}
2560
2561/*
danielk1977443c0592009-01-16 15:21:05 +00002562** Set the reinitializer for this pager. If not NULL, the reinitializer
2563** is called when the content of a page in cache is modified (restored)
2564** as part of a transaction or savepoint rollback. The callback gives
2565** higher-level code an opportunity to restore the EXTRA section to
2566** agree with the restored page data.
drha6abd042004-06-09 17:37:22 +00002567*/
danielk1977eaa06f62008-09-18 17:34:44 +00002568void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
drha6abd042004-06-09 17:37:22 +00002569 pPager->xReiniter = xReinit;
2570}
2571
2572/*
danielk1977443c0592009-01-16 15:21:05 +00002573** Change the page size used by the Pager object. The new page size
2574** is passed in *pPageSize.
2575**
2576** If the pager is in the error state when this function is called, it
2577** is a no-op. The value returned is the error state error code (i.e.
2578** one of SQLITE_IOERR, SQLITE_CORRUPT or SQLITE_FULL).
2579**
2580** Otherwise, if all of the following are true:
2581**
2582** * the new page size (value of *pPageSize) is valid (a power
2583** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
2584**
2585** * there are no outstanding page references, and
2586**
2587** * the database is either not an in-memory database or it is
2588** an in-memory database that currently consists of zero pages.
2589**
2590** then the pager object page size is set to *pPageSize.
2591**
2592** If the page size is changed, then this function uses sqlite3PagerMalloc()
2593** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
2594** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
2595** In all other cases, SQLITE_OK is returned.
2596**
2597** If the page size is not changed, either because one of the enumerated
2598** conditions above is not true, the pager was in error state when this
2599** function was called, or because the memory allocation attempt failed,
2600** then *pPageSize is set to the old, retained page size before returning.
drh90f5ecb2004-07-22 01:19:35 +00002601*/
danielk1977a1644fd2007-08-29 12:31:25 +00002602int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
danielk19771357d9f2008-09-16 05:12:24 +00002603 int rc = pPager->errCode;
2604 if( rc==SQLITE_OK ){
2605 u16 pageSize = *pPageSize;
2606 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
2607 if( pageSize && pageSize!=pPager->pageSize
2608 && (pPager->memDb==0 || pPager->dbSize==0)
2609 && sqlite3PcacheRefCount(pPager->pPCache)==0
2610 ){
2611 char *pNew = (char *)sqlite3PageMalloc(pageSize);
2612 if( !pNew ){
2613 rc = SQLITE_NOMEM;
2614 }else{
2615 pager_reset(pPager);
2616 pPager->pageSize = pageSize;
danielk19771357d9f2008-09-16 05:12:24 +00002617 sqlite3PageFree(pPager->pTmpSpace);
2618 pPager->pTmpSpace = pNew;
2619 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
2620 }
danielk1977a1644fd2007-08-29 12:31:25 +00002621 }
drh4f21c4a2008-12-10 22:15:00 +00002622 *pPageSize = (u16)pPager->pageSize;
drh1c7880e2005-05-20 20:01:55 +00002623 }
danielk1977a1644fd2007-08-29 12:31:25 +00002624 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002625}
2626
2627/*
drh26b79942007-11-28 16:19:56 +00002628** Return a pointer to the "temporary page" buffer held internally
2629** by the pager. This is a buffer that is big enough to hold the
2630** entire content of a database page. This buffer is used internally
2631** during rollback and will be overwritten whenever a rollback
2632** occurs. But other modules are free to use it too, as long as
2633** no rollbacks are happening.
2634*/
2635void *sqlite3PagerTempSpace(Pager *pPager){
2636 return pPager->pTmpSpace;
2637}
2638
2639/*
drhf8e632b2007-05-08 14:51:36 +00002640** Attempt to set the maximum database page count if mxPage is positive.
2641** Make no changes if mxPage is zero or negative. And never reduce the
2642** maximum page count below the current size of the database.
2643**
2644** Regardless of mxPage, return the current maximum page count.
2645*/
2646int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2647 if( mxPage>0 ){
2648 pPager->mxPgno = mxPage;
2649 }
danielk1977ad0132d2008-06-07 08:58:22 +00002650 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002651 return pPager->mxPgno;
2652}
2653
2654/*
drhc9ac5ca2005-11-04 22:03:30 +00002655** The following set of routines are used to disable the simulated
2656** I/O error mechanism. These routines are used to avoid simulated
2657** errors in places where we do not care about errors.
2658**
2659** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2660** and generate no code.
2661*/
2662#ifdef SQLITE_TEST
2663extern int sqlite3_io_error_pending;
2664extern int sqlite3_io_error_hit;
2665static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002666void disable_simulated_io_errors(void){
2667 saved_cnt = sqlite3_io_error_pending;
2668 sqlite3_io_error_pending = -1;
2669}
2670void enable_simulated_io_errors(void){
2671 sqlite3_io_error_pending = saved_cnt;
2672}
2673#else
drh152410f2005-11-05 15:11:22 +00002674# define disable_simulated_io_errors()
2675# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002676#endif
2677
2678/*
drh90f5ecb2004-07-22 01:19:35 +00002679** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002680** that pDest points to.
2681**
danielk1977443c0592009-01-16 15:21:05 +00002682** If the pager was opened on a transient file (zFilename==""), or
2683** opened on a file less than N bytes in size, the output buffer is
2684** zeroed and SQLITE_OK returned. The rationale for this is that this
2685** function is used to read database headers, and a new transient or
2686** zero sized database has a header than consists entirely of zeroes.
2687**
2688** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
2689** the error code is returned to the caller and the contents of the
2690** output buffer undefined.
drh90f5ecb2004-07-22 01:19:35 +00002691*/
danielk19773b8a05f2007-03-19 17:44:26 +00002692int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002693 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002694 memset(pDest, 0, N);
danielk1977443c0592009-01-16 15:21:05 +00002695 assert( isOpen(pPager->fd) || pPager->tempFile );
2696 if( isOpen(pPager->fd) ){
drhb0603412007-02-28 04:47:26 +00002697 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002698 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002699 if( rc==SQLITE_IOERR_SHORT_READ ){
2700 rc = SQLITE_OK;
2701 }
drh90f5ecb2004-07-22 01:19:35 +00002702 }
drh551b7732006-11-06 21:20:25 +00002703 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002704}
2705
2706/*
danielk1977443c0592009-01-16 15:21:05 +00002707** Return the total number of pages in the database file associated
2708** with pPager. Normally, this is calculated as (<db file size>/<page-size>).
2709** However, if the file is between 1 and <page-size> bytes in size, then
2710** this is considered a 1 page file.
danielk197715f411d2005-09-16 10:18:45 +00002711**
danielk1977443c0592009-01-16 15:21:05 +00002712** If the pager is in error state when this function is called, then the
2713** error state error code is returned and *pnPage left unchanged. Or,
2714** if the file system has to be queried for the size of the file and
2715** the query attempt returns an IO error, the IO error code is returned
2716** and *pnPage is left unchanged.
2717**
2718** Otherwise, if everything is successful, then SQLITE_OK is returned
2719** and *pnPage is set to the number of pages in the database.
drhed7c8552001-04-11 14:29:21 +00002720*/
danielk1977ad0132d2008-06-07 08:58:22 +00002721int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk1977443c0592009-01-16 15:21:05 +00002722 Pgno nPage; /* Value to return via *pnPage */
2723
2724 /* If the pager is already in the error state, return the error code. */
drha7aea3d2007-03-15 12:51:16 +00002725 if( pPager->errCode ){
danielk1977443c0592009-01-16 15:21:05 +00002726 return pPager->errCode;
drha7aea3d2007-03-15 12:51:16 +00002727 }
danielk1977443c0592009-01-16 15:21:05 +00002728
2729 /* Determine the number of pages in the file. Store this in nPage. */
danielk1977d92db532008-11-17 04:56:24 +00002730 if( pPager->dbSizeValid ){
danielk1977443c0592009-01-16 15:21:05 +00002731 nPage = pPager->dbSize;
2732 }else{
2733 int rc; /* Error returned by OsFileSize() */
2734 i64 n = 0; /* File size in bytes returned by OsFileSize() */
2735
2736 assert( isOpen(pPager->fd) || pPager->tempFile );
2737 if( isOpen(pPager->fd) && (rc = sqlite3OsFileSize(pPager->fd, &n)) ){
drhe49f9822006-09-15 12:29:16 +00002738 pager_error(pPager, rc);
danielk1977ad0132d2008-06-07 08:58:22 +00002739 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002740 }
2741 if( n>0 && n<pPager->pageSize ){
danielk1977443c0592009-01-16 15:21:05 +00002742 nPage = 1;
danielk197715f411d2005-09-16 10:18:45 +00002743 }else{
danielk1977443c0592009-01-16 15:21:05 +00002744 nPage = n / pPager->pageSize;
danielk197715f411d2005-09-16 10:18:45 +00002745 }
2746 if( pPager->state!=PAGER_UNLOCK ){
danielk1977443c0592009-01-16 15:21:05 +00002747 pPager->dbSize = (Pgno)nPage;
2748 pPager->dbFileSize = (Pgno)nPage;
danielk1977d92db532008-11-17 04:56:24 +00002749 pPager->dbSizeValid = 1;
danielk197715f411d2005-09-16 10:18:45 +00002750 }
drhed7c8552001-04-11 14:29:21 +00002751 }
danielk1977443c0592009-01-16 15:21:05 +00002752
2753 /* If the current number of pages in the file is greater than the
2754 ** configured maximum pager number, increase the allowed limit so
2755 ** that the file can be read.
2756 */
2757 if( nPage>pPager->mxPgno ){
2758 pPager->mxPgno = (Pgno)nPage;
drh1f595712004-06-15 01:40:29 +00002759 }
danielk1977443c0592009-01-16 15:21:05 +00002760
2761 /* Set the output variable and return SQLITE_OK */
danielk1977ad0132d2008-06-07 08:58:22 +00002762 if( pnPage ){
danielk1977443c0592009-01-16 15:21:05 +00002763 *pnPage = nPage;
danielk1977ad0132d2008-06-07 08:58:22 +00002764 }
2765 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002766}
2767
2768/*
danielk1977443c0592009-01-16 15:21:05 +00002769** Forward declaration.
drhf7c57532003-04-25 13:22:51 +00002770*/
danielk197776572402004-06-25 02:38:54 +00002771static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002772
2773/*
danielk1977443c0592009-01-16 15:21:05 +00002774** Try to obtain a lock of type locktype on the database file. If
2775** a similar or greater lock is already held, this function is a no-op
2776** (returning SQLITE_OK immediately).
2777**
2778** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
2779** the busy callback if the lock is currently not available. Repeat
2780** until the busy callback returns false or until the attempt to
2781** obtain the lock succeeds.
danielk197717221812005-02-15 03:38:05 +00002782**
2783** Return SQLITE_OK on success and an error code if we cannot obtain
danielk1977443c0592009-01-16 15:21:05 +00002784** the lock. If the lock is obtained successfully, set the Pager.state
2785** variable to locktype before returning.
danielk197717221812005-02-15 03:38:05 +00002786*/
2787static int pager_wait_on_lock(Pager *pPager, int locktype){
danielk1977443c0592009-01-16 15:21:05 +00002788 int rc; /* Return code */
drh1aa2d8b2007-01-03 15:34:29 +00002789
2790 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002791 assert( PAGER_SHARED==SHARED_LOCK );
2792 assert( PAGER_RESERVED==RESERVED_LOCK );
2793 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002794
2795 /* If the file is currently unlocked then the size must be unknown */
danielk1977d92db532008-11-17 04:56:24 +00002796 assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
drh1aa2d8b2007-01-03 15:34:29 +00002797
danielk1977443c0592009-01-16 15:21:05 +00002798 /* Check that this is either a no-op (because the requested lock is
2799 ** already held, or one of the transistions that the busy-handler
2800 ** may be invoked during, according to the comment above
2801 ** sqlite3PagerSetBusyhandler().
2802 */
2803 assert( (pPager->state>=locktype)
2804 || (pPager->state==PAGER_UNLOCK && locktype==PAGER_SHARED)
2805 || (pPager->state==PAGER_RESERVED && locktype==PAGER_EXCLUSIVE)
2806 );
2807
danielk197717221812005-02-15 03:38:05 +00002808 if( pPager->state>=locktype ){
2809 rc = SQLITE_OK;
2810 }else{
danielk197717221812005-02-15 03:38:05 +00002811 do {
drh054889e2005-11-30 03:20:31 +00002812 rc = sqlite3OsLock(pPager->fd, locktype);
danielk19771ceedd32008-11-19 10:22:33 +00002813 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
danielk197717221812005-02-15 03:38:05 +00002814 if( rc==SQLITE_OK ){
drh4f21c4a2008-12-10 22:15:00 +00002815 pPager->state = (u8)locktype;
drhb0603412007-02-28 04:47:26 +00002816 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002817 }
2818 }
2819 return rc;
2820}
2821
danielk19773460d192008-12-27 15:23:13 +00002822#ifndef SQLITE_OMIT_AUTOVACUUM
2823/*
danielk1977f90b7262009-01-07 15:18:20 +00002824** Truncate the in-memory database file image to nPage pages. This
2825** function does not actually modify the database file on disk. It
2826** just sets the internal state of the pager object so that the
2827** truncation will be done when the current transaction is committed.
danielk19773460d192008-12-27 15:23:13 +00002828*/
2829void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
2830 assert( pPager->dbSizeValid );
2831 assert( pPager->dbSize>=nPage );
danielk1977443c0592009-01-16 15:21:05 +00002832 assert( pPager->state>=PAGER_RESERVED );
danielk19773460d192008-12-27 15:23:13 +00002833 pPager->dbSize = nPage;
2834}
danielk19773460d192008-12-27 15:23:13 +00002835#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
2836
drhf7c57532003-04-25 13:22:51 +00002837/*
drhed7c8552001-04-11 14:29:21 +00002838** Shutdown the page cache. Free all memory and close all files.
2839**
2840** If a transaction was in progress when this routine is called, that
2841** transaction is rolled back. All outstanding pages are invalidated
2842** and their memory is freed. Any attempt to use a page associated
2843** with this page cache after this function returns will likely
2844** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002845**
2846** This function always succeeds. If a transaction is active an attempt
2847** is made to roll it back. If an error occurs during the rollback
2848** a hot journal may be left in the filesystem but no error is returned
2849** to the caller.
drhed7c8552001-04-11 14:29:21 +00002850*/
danielk19773b8a05f2007-03-19 17:44:26 +00002851int sqlite3PagerClose(Pager *pPager){
drhbafda092007-01-03 23:36:22 +00002852 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002853 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002854 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002855 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002856 pager_reset(pPager);
danielk1977443c0592009-01-16 15:21:05 +00002857 if( MEMDB ){
2858 pager_unlock(pPager);
2859 }else{
danielk1977f2c31ad2009-01-06 13:40:08 +00002860 /* Set Pager.journalHdr to -1 for the benefit of the pager_playback()
2861 ** call which may be made from within pagerUnlockAndRollback(). If it
2862 ** is not -1, then the unsynced portion of an open journal file may
2863 ** be played back into the database. If a power failure occurs while
2864 ** this is happening, the database may become corrupt.
2865 */
2866 pPager->journalHdr = -1;
danielk1977b3175382008-10-17 18:51:52 +00002867 pagerUnlockAndRollback(pPager);
2868 }
danielk19772d1d86f2008-06-20 14:59:51 +00002869 sqlite3EndBenignMalloc();
danielk1977443c0592009-01-16 15:21:05 +00002870 enable_simulated_io_errors();
drh30d53702009-01-06 15:58:57 +00002871 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
drhb0603412007-02-28 04:47:26 +00002872 IOTRACE(("CLOSE %p\n", pPager))
danielk1977b4b47412007-08-17 15:53:36 +00002873 sqlite3OsClose(pPager->fd);
drhfacf0302008-06-17 15:12:00 +00002874 sqlite3PageFree(pPager->pTmpSpace);
danielk19778c0a7912008-08-20 14:49:23 +00002875 sqlite3PcacheClose(pPager->pPCache);
danielk1977443c0592009-01-16 15:21:05 +00002876
2877 assert( !pPager->aSavepoint && !pPager->pInJournal );
2878 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
2879
drh17435752007-08-16 04:30:38 +00002880 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002881 return SQLITE_OK;
2882}
2883
drh87cc3b32007-05-08 21:45:27 +00002884#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002885/*
danielk1977443c0592009-01-16 15:21:05 +00002886** Return the page number for page pPg.
drhed7c8552001-04-11 14:29:21 +00002887*/
danielk1977443c0592009-01-16 15:21:05 +00002888Pgno sqlite3PagerPagenumber(DbPage *pPg){
2889 return pPg->pgno;
drhed7c8552001-04-11 14:29:21 +00002890}
drh87cc3b32007-05-08 21:45:27 +00002891#endif
drhed7c8552001-04-11 14:29:21 +00002892
2893/*
danielk1977443c0592009-01-16 15:21:05 +00002894** Increment the reference count for page pPg.
drhdf0b3b02001-06-23 11:36:20 +00002895*/
danielk1977443c0592009-01-16 15:21:05 +00002896void sqlite3PagerRef(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002897 sqlite3PcacheRef(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002898}
2899
2900/*
danielk1977443c0592009-01-16 15:21:05 +00002901** Sync the journal. In other words, make sure all the pages that have
drh34e79ce2004-02-08 06:05:46 +00002902** been written to the journal have actually reached the surface of the
danielk1977443c0592009-01-16 15:21:05 +00002903** disk and can be restored in the event of a hot-journal rollback.
drhb19a2bc2001-09-16 00:13:26 +00002904**
danielk1977443c0592009-01-16 15:21:05 +00002905** If the Pager.needSync flag is not set, then this function is a
2906** no-op. Otherwise, the actions required depend on the journal-mode
2907** and the device characteristics of the the file-system, as follows:
danielk19774cd2cd52007-08-23 14:48:23 +00002908**
danielk1977443c0592009-01-16 15:21:05 +00002909** * If the journal file is an in-memory journal file, no action need
2910** be taken.
drhfa86c412002-02-02 15:01:15 +00002911**
danielk1977443c0592009-01-16 15:21:05 +00002912** * Otherwise, if the device does not support the SAFE_APPEND property,
2913** then the nRec field of the most recently written journal header
2914** is updated to contain the number of journal records that have
2915** been written following it. If the pager is operating in full-sync
2916** mode, then the journal file is synced before this field is updated.
2917**
2918** * If the device does not support the SEQUENTIAL property, then
2919** journal file is synced.
2920**
2921** Or, in pseudo-code:
2922**
2923** if( NOT <in-memory journal> ){
2924** if( NOT SAFE_APPEND ){
2925** if( <full-sync mode> ) xSync(<journal file>);
2926** <update nRec field>
2927** }
2928** if( NOT SEQUENTIAL ) xSync(<journal file>);
2929** }
2930**
2931** The Pager.needSync flag is never be set for temporary files, or any
2932** file operating in no-sync mode (Pager.noSync set to non-zero).
2933**
2934** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
2935** page currently held in memory before returning SQLITE_OK. If an IO
2936** error is encountered, then the IO error code is returned to the caller.
drh50e5dad2001-09-15 00:57:28 +00002937*/
danielk197776572402004-06-25 02:38:54 +00002938static int syncJournal(Pager *pPager){
danielk197776572402004-06-25 02:38:54 +00002939 if( pPager->needSync ){
danielk1977b3175382008-10-17 18:51:52 +00002940 assert( !pPager->tempFile );
2941 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
danielk1977443c0592009-01-16 15:21:05 +00002942 int rc; /* Return code */
2943 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2944 assert( isOpen(pPager->jfd) );
danielk19774cd2cd52007-08-23 14:48:23 +00002945
danielk19774cd2cd52007-08-23 14:48:23 +00002946 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk1977443c0592009-01-16 15:21:05 +00002947 /* Variable iNRecOffset is set to the offset in the journal file
2948 ** of the nRec field of the most recently written journal header.
2949 ** This field will be updated following the xSync() operation
2950 ** on the journal file. */
2951 i64 iNRecOffset = pPager->journalHdr + sizeof(aJournalMagic);
danielk1977112f7522009-01-08 17:50:45 +00002952
2953 /* This block deals with an obscure problem. If the last connection
2954 ** that wrote to this database was operating in persistent-journal
2955 ** mode, then the journal file may at this point actually be larger
2956 ** than Pager.journalOff bytes. If the next thing in the journal
2957 ** file happens to be a journal-header (written as part of the
2958 ** previous connections transaction), and a crash or power-failure
2959 ** occurs after nRec is updated but before this connection writes
2960 ** anything else to the journal file (or commits/rolls back its
2961 ** transaction), then SQLite may become confused when doing the
2962 ** hot-journal rollback following recovery. It may roll back all
2963 ** of this connections data, then proceed to rolling back the old,
2964 ** out-of-date data that follows it. Database corruption.
2965 **
2966 ** To work around this, if the journal file does appear to contain
2967 ** a valid header following Pager.journalOff, then write a 0x00
2968 ** byte to the start of it to prevent it from being recognized.
danielk1977443c0592009-01-16 15:21:05 +00002969 **
2970 ** Variable iNextHdrOffset is set to the offset at which this
2971 ** problematic header will occur, if it exists. aMagic is used
2972 ** as a temporary buffer to inspect the first couple of bytes of
2973 ** the potential journal header.
danielk1977112f7522009-01-08 17:50:45 +00002974 */
danielk1977443c0592009-01-16 15:21:05 +00002975 i64 iNextHdrOffset = journalHdrOffset(pPager);
2976 u8 aMagic[8];
2977 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
2978 if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
danielk1977112f7522009-01-08 17:50:45 +00002979 static const u8 zerobyte = 0;
danielk1977443c0592009-01-16 15:21:05 +00002980 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
danielk1977112f7522009-01-08 17:50:45 +00002981 }
2982 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
2983 return rc;
2984 }
2985
danielk197776572402004-06-25 02:38:54 +00002986 /* Write the nRec value into the journal file header. If in
2987 ** full-synchronous mode, sync the journal first. This ensures that
2988 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002989 ** it as a candidate for rollback.
2990 **
2991 ** This is not required if the persistent media supports the
2992 ** SAFE_APPEND property. Because in this case it is not possible
2993 ** for garbage data to be appended to the file, the nRec field
2994 ** is populated with 0xFFFFFFFF when the journal header is written
2995 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002996 */
danielk19774cd2cd52007-08-23 14:48:23 +00002997 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh30d53702009-01-06 15:58:57 +00002998 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
drhb0603412007-02-28 04:47:26 +00002999 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00003000 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
danielk1977443c0592009-01-16 15:21:05 +00003001 if( rc!=SQLITE_OK ) return rc;
drhd8d66e82003-02-12 02:10:15 +00003002 }
danielk1977443c0592009-01-16 15:21:05 +00003003 IOTRACE(("JHDR %p %lld %d\n", pPager, iNRecOffset, 4));
3004 rc = write32bits(pPager->jfd, iNRecOffset, pPager->nRec);
3005 if( rc!=SQLITE_OK ) return rc;
drh968af522003-02-11 14:55:40 +00003006 }
danielk19774cd2cd52007-08-23 14:48:23 +00003007 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh30d53702009-01-06 15:58:57 +00003008 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
danielk19774cd2cd52007-08-23 14:48:23 +00003009 IOTRACE(("JSYNC %p\n", pPager))
3010 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
3011 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
3012 );
danielk1977443c0592009-01-16 15:21:05 +00003013 if( rc!=SQLITE_OK ) return rc;
danielk19774cd2cd52007-08-23 14:48:23 +00003014 }
drhfa86c412002-02-02 15:01:15 +00003015 }
drh341eae82003-01-21 02:39:36 +00003016
danielk1977443c0592009-01-16 15:21:05 +00003017 /* The journal file was just successfully synced. Set Pager.needSync
3018 ** to zero and clear the PGHDR_NEED_SYNC flag on all pagess.
drh341eae82003-01-21 02:39:36 +00003019 */
danielk1977443c0592009-01-16 15:21:05 +00003020 pPager->needSync = 0;
3021 pPager->journalStarted = 1;
danielk1977bc2ca9e2008-11-13 14:28:28 +00003022 sqlite3PcacheClearSyncFlags(pPager->pPCache);
drh50e5dad2001-09-15 00:57:28 +00003023 }
drh03eb96a2002-11-10 23:32:56 +00003024
danielk1977443c0592009-01-16 15:21:05 +00003025 return SQLITE_OK;
drh50e5dad2001-09-15 00:57:28 +00003026}
3027
3028/*
danielk1977443c0592009-01-16 15:21:05 +00003029** The argument is the first in a linked list of dirty pages connected
3030** by the PgHdr.pDirty pointer. This function writes each one of the
3031** in-memory pages in the list to the database file. The argument may
3032** be NULL, representing an empty list. In this case this function is
3033** a no-op.
3034**
3035** The pager must hold at least a RESERVED lock when this function
3036** is called. Before writing anything to the database file, this lock
3037** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
3038** SQLITE_BUSY is returned and no data is written to the database file.
3039**
3040** If the pager is a temp-file pager and the actual file-system file
3041** is not yet open, it is created and opened before any data is
3042** written out.
3043**
3044** Once the lock has been upgraded and, if necessary, the file opened,
3045** the pages are written out to the database file in list order. Writing
3046** a page is skipped if it meets either of the following criteria:
3047**
3048** * The page number is greater than Pager.dbSize, or
3049** * The PGHDR_DONT_WRITE flag is set on the page.
3050**
3051** If writing out a page causes the database file to grow, Pager.dbFileSize
3052** is updated accordingly. If page 1 is written out, then the value cached
3053** in Pager.dbFileVers[] is updated to match the new value stored in
3054** the database file.
3055**
3056** If everything is successful, SQLITE_OK is returned. If an IO error
3057** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
3058** be obtained, SQLITE_BUSY is returned.
drh2554f8b2003-01-22 01:26:44 +00003059*/
3060static int pager_write_pagelist(PgHdr *pList){
danielk1977443c0592009-01-16 15:21:05 +00003061 Pager *pPager; /* Pager object */
3062 int rc; /* Return code */
drh2554f8b2003-01-22 01:26:44 +00003063
3064 if( pList==0 ) return SQLITE_OK;
3065 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00003066
3067 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
3068 ** database file. If there is already an EXCLUSIVE lock, the following
danielk1977443c0592009-01-16 15:21:05 +00003069 ** call is a no-op.
danielk19779eed5052004-06-04 10:38:30 +00003070 **
drha6abd042004-06-09 17:37:22 +00003071 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
3072 ** through an intermediate state PENDING. A PENDING lock prevents new
3073 ** readers from attaching to the database but is unsufficient for us to
3074 ** write. The idea of a PENDING lock is to prevent new readers from
3075 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00003076 **
drha6abd042004-06-09 17:37:22 +00003077 ** While the pager is in the RESERVED state, the original database file
3078 ** is unchanged and we can rollback without having to playback the
3079 ** journal into the original database file. Once we transition to
3080 ** EXCLUSIVE, it means the database file has been changed and any rollback
3081 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00003082 */
danielk1977443c0592009-01-16 15:21:05 +00003083 assert( pPager->state>=PAGER_RESERVED );
drh684917c2004-10-05 02:41:42 +00003084 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk1977443c0592009-01-16 15:21:05 +00003085
3086 /* If the file is a temp-file has not yet been opened, open it now. It
3087 ** is not possible for rc to be other than SQLITE_OK if this branch
3088 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
3089 */
3090 if( !isOpen(pPager->fd) ){
3091 assert( pPager->tempFile && rc==SQLITE_OK );
3092 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19779eed5052004-06-04 10:38:30 +00003093 }
3094
danielk1977443c0592009-01-16 15:21:05 +00003095 while( rc==SQLITE_OK && pList ){
3096 Pgno pgno = pList->pgno;
danielk19777a2b1ee2007-08-21 14:27:01 +00003097
danielk1977687566d2004-11-02 12:56:41 +00003098 /* If there are dirty pages in the page cache with page numbers greater
danielk1977f90b7262009-01-07 15:18:20 +00003099 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
danielk1977687566d2004-11-02 12:56:41 +00003100 ** make the file smaller (presumably by auto-vacuum code). Do not write
3101 ** any such pages to the file.
danielk1977443c0592009-01-16 15:21:05 +00003102 **
3103 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
3104 ** set (set by sqlite3PagerDontWrite()).
danielk1977687566d2004-11-02 12:56:41 +00003105 */
danielk1977443c0592009-01-16 15:21:05 +00003106 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
3107 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
3108 char *pData = CODEC2(pPager, pList->pData, pgno, 6); /* Data to write */
danielk197712dd5492008-12-18 15:45:07 +00003109
danielk1977443c0592009-01-16 15:21:05 +00003110 /* Write out the page data. */
danielk197762079062007-08-15 17:08:46 +00003111 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
danielk1977443c0592009-01-16 15:21:05 +00003112
3113 /* If page 1 was just written, update Pager.dbFileVers to match
3114 ** the value now stored in the database file. If writing this
3115 ** page caused the database file to grow, update dbFileSize.
3116 */
3117 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00003118 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
3119 }
danielk1977443c0592009-01-16 15:21:05 +00003120 if( pgno>pPager->dbFileSize ){
3121 pPager->dbFileSize = pgno;
danielk19773460d192008-12-27 15:23:13 +00003122 }
danielk1977443c0592009-01-16 15:21:05 +00003123
3124 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
3125 PAGERID(pPager), pgno, pager_pagehash(pList)));
3126 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
3127 PAGER_INCR(sqlite3_pager_writedb_count);
3128 PAGER_INCR(pPager->nWrite);
3129 }else{
3130 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
danielk1977687566d2004-11-02 12:56:41 +00003131 }
danielk19773c407372005-02-15 02:54:14 +00003132#ifdef SQLITE_CHECK_PAGES
3133 pList->pageHash = pager_pagehash(pList);
3134#endif
drh2554f8b2003-01-22 01:26:44 +00003135 pList = pList->pDirty;
3136 }
danielk19778c0a7912008-08-20 14:49:23 +00003137
danielk1977443c0592009-01-16 15:21:05 +00003138 return rc;
drh2554f8b2003-01-22 01:26:44 +00003139}
3140
3141/*
danielk1977443c0592009-01-16 15:21:05 +00003142** Append a record of the current state of page pPg to the sub-journal.
3143** It is the callers responsibility to use subjRequiresPage() to check
3144** that it is really required before calling this function.
3145**
3146** If successful, set the bit corresponding to pPg->pgno in the bitvecs
3147** for all open savepoints before returning.
3148**
3149** This function returns SQLITE_OK if everything is successful, an IO
3150** error code if the attempt to write to the sub-journal fails, or
3151** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
3152** bitvec.
danielk1977f2c31ad2009-01-06 13:40:08 +00003153*/
3154static int subjournalPage(PgHdr *pPg){
3155 int rc;
3156 void *pData = pPg->pData;
3157 Pager *pPager = pPg->pPager;
danielk1977443c0592009-01-16 15:21:05 +00003158 i64 offset = pPager->nSubRec*(4+pPager->pageSize);
danielk1977f2c31ad2009-01-06 13:40:08 +00003159 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
3160
drh30d53702009-01-06 15:58:57 +00003161 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
danielk1977f2c31ad2009-01-06 13:40:08 +00003162
3163 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
3164 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
3165 if( rc==SQLITE_OK ){
3166 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
3167 }
3168 if( rc==SQLITE_OK ){
danielk1977443c0592009-01-16 15:21:05 +00003169 pPager->nSubRec++;
danielk1977f2c31ad2009-01-06 13:40:08 +00003170 assert( pPager->nSavepoint>0 );
3171 rc = addToSavepointBitvecs(pPager, pPg->pgno);
danielk1977443c0592009-01-16 15:21:05 +00003172 testcase( rc!=SQLITE_OK );
danielk1977f2c31ad2009-01-06 13:40:08 +00003173 }
3174 return rc;
3175}
3176
3177
3178/*
danielk19778c0a7912008-08-20 14:49:23 +00003179** This function is called by the pcache layer when it has reached some
danielk1977443c0592009-01-16 15:21:05 +00003180** soft memory limit. The first argument is a pointer to a Pager object
3181** (cast as a void*). The pager is always 'purgeable' (not an in-memory
3182** database). The second argument is a reference to a page that is
3183** currently dirty but has no outstanding references. The page
3184** is always associated with the Pager object passed as the first
3185** argument.
3186**
3187** The job of this function is to make pPg clean by writing its contents
3188** out to the database file, if possible. This may involve syncing the
3189** journal file.
3190**
3191** If successful, sqlite3PcacheMakeClean() is called on the page and
3192** SQLITE_OK returned. If an IO error occurs while trying to make the
3193** page clean, the IO error code is returned. If the page cannot be
3194** made clean for some other reason, but no error occurs, then SQLITE_OK
3195** is returned by sqlite3PcacheMakeClean() is not called.
drh2554f8b2003-01-22 01:26:44 +00003196*/
danielk1977a858aa22008-08-22 16:22:17 +00003197static int pagerStress(void *p, PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003198 Pager *pPager = (Pager *)p;
danielk19778c0a7912008-08-20 14:49:23 +00003199 int rc = SQLITE_OK;
drh8f2e9a12008-02-26 14:46:04 +00003200
danielk1977443c0592009-01-16 15:21:05 +00003201 assert( pPg->pPager==pPager );
3202 assert( pPg->flags&PGHDR_DIRTY );
3203
3204 /* The doNotSync flag is set by the sqlite3PagerWrite() function while it
3205 ** is journalling a set of two or more database pages that are stored
3206 ** on the same disk sector. Syncing the journal is not allowed while
3207 ** this is happening as it is important that all members of such a
3208 ** set of pages are synced to disk together. So, if the page this function
3209 ** is trying to make clean will require a journal sync and the doNotSync
3210 ** flag is set, return without doing anything. The pcache layer will
3211 ** just have to go ahead and allocate a new page buffer instead of
3212 ** reusing pPg.
3213 **
3214 ** Similarly, if the pager has already entered the error state, do not
3215 ** try to write the contents of pPg to disk.
3216 */
3217 if( pPager->errCode || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC) ){
danielk19778c200142008-08-25 07:12:28 +00003218 return SQLITE_OK;
3219 }
3220
danielk1977443c0592009-01-16 15:21:05 +00003221 /* Sync the journal file if required. */
3222 if( pPg->flags&PGHDR_NEED_SYNC ){
3223 rc = syncJournal(pPager);
3224 if( rc==SQLITE_OK && pPager->fullSync &&
3225 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
3226 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
3227 ){
3228 pPager->nRec = 0;
3229 rc = writeJournalHdr(pPager);
danielk1977a858aa22008-08-22 16:22:17 +00003230 }
3231 }
3232
danielk1977443c0592009-01-16 15:21:05 +00003233 /* If the page number of this page is larger than the current size of
3234 ** the database image, it may need to be written to the sub-journal.
3235 ** This is because the call to pager_write_pagelist() below will not
3236 ** actually write data to the file in this case.
3237 **
3238 ** Consider the following sequence of events:
3239 **
3240 ** BEGIN;
3241 ** <journal page X>
3242 ** <modify page X>
3243 ** SAVEPOINT sp;
3244 ** <shrink database file to Y pages>
3245 ** pagerStress(page X)
3246 ** ROLLBACK TO sp;
3247 **
3248 ** If (X>Y), then when pagerStress is called page X will not be written
3249 ** out to the database file, but will be dropped from the cache. Then,
3250 ** following the "ROLLBACK TO sp" statement, reading page X will read
3251 ** data from the database file. This will be the copy of page X as it
3252 ** was when the transaction started, not as it was when "SAVEPOINT sp"
3253 ** was executed.
3254 **
3255 ** The solution is to write the current data for page X into the
3256 ** sub-journal file now (if it is not already there), so that it will
3257 ** be restored to its current value when the "ROLLBACK TO sp" is
3258 ** executed.
3259 */
3260 if( rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg) ){
3261 rc = subjournalPage(pPg);
3262 }
3263
3264 /* Write the contents of the page out to the database file. */
3265 if( rc==SQLITE_OK ){
3266 pPg->pDirty = 0;
3267 rc = pager_write_pagelist(pPg);
3268 }
3269
3270 /* Mark the page as clean. */
danielk1977a858aa22008-08-22 16:22:17 +00003271 if( rc==SQLITE_OK ){
drh30d53702009-01-06 15:58:57 +00003272 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
danielk1977a858aa22008-08-22 16:22:17 +00003273 sqlite3PcacheMakeClean(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00003274 }
danielk1977443c0592009-01-16 15:21:05 +00003275
3276 return pager_error(pPager, rc);
drh2554f8b2003-01-22 01:26:44 +00003277}
3278
danielk19778c0a7912008-08-20 14:49:23 +00003279
drh2554f8b2003-01-22 01:26:44 +00003280/*
danielk1977443c0592009-01-16 15:21:05 +00003281** This function is called after transitioning from PAGER_UNLOCK to
3282** PAGER_SHARED state. It tests if there is a hot journal present in
3283** the file-system for the given pager. A hot journal is one that
3284** needs to be played back. According to this function, a hot-journal
3285** file exists if the following three criteria are met:
3286**
3287** * The journal file exists in the file system, and
3288** * No process holds a RESERVED or greater lock on the database file, and
3289** * The database file itself is greater than 0 bytes in size.
drh165ffe92005-03-15 17:09:30 +00003290**
3291** If the current size of the database file is 0 but a journal file
3292** exists, that is probably an old journal left over from a prior
danielk1977443c0592009-01-16 15:21:05 +00003293** database with the same name. In this case the journal file is
3294** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
3295** is returned.
drh82ed1e52008-04-25 12:25:42 +00003296**
3297** This routine does not open the journal file to examine its
3298** content. Hence, the journal might contain the name of a master
3299** journal file that has been deleted, and hence not be hot. Or
3300** the header of the journal might be zeroed out. This routine
3301** does not discover these cases of a non-hot journal - if the
3302** journal file exists and is not empty this routine assumes it
3303** is hot. The pager_playback() routine will discover that the
3304** journal file is not really hot and will no-op.
danielk1977443c0592009-01-16 15:21:05 +00003305**
3306** If a hot-journal file is found to exist, *pExists is set to 1 and
3307** SQLITE_OK returned. If no hot-journal file is present, *pExists is
3308** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
3309** to determine whether or not a hot-journal file exists, the IO error
3310** code is returned and the value of *pExists is undefined.
drh165ffe92005-03-15 17:09:30 +00003311*/
danielk1977d300b8a2008-08-01 10:50:23 +00003312static int hasHotJournal(Pager *pPager, int *pExists){
danielk1977443c0592009-01-16 15:21:05 +00003313 sqlite3_vfs * const pVfs = pPager->pVfs;
3314 int rc; /* Return code */
3315 int exists = 0; /* True if a journal file is present */
3316 int locked = 0; /* True if some process holds a RESERVED lock */
3317
drh0a846f92008-08-25 17:23:29 +00003318 assert( pPager!=0 );
3319 assert( pPager->useJournal );
danielk1977443c0592009-01-16 15:21:05 +00003320 assert( isOpen(pPager->fd) );
3321
danielk1977d300b8a2008-08-01 10:50:23 +00003322 *pExists = 0;
drh0a846f92008-08-25 17:23:29 +00003323 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
3324 if( rc==SQLITE_OK && exists ){
3325 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
danielk1977443c0592009-01-16 15:21:05 +00003326 if( rc==SQLITE_OK && !locked ){
3327 int nPage;
3328 rc = sqlite3PagerPagecount(pPager, &nPage);
3329 if( rc==SQLITE_OK ){
3330 if( nPage==0 ){
3331 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3332 }else{
3333 *pExists = 1;
3334 }
danielk1977861f7452008-06-05 11:39:11 +00003335 }
3336 }
drhbb5f18d2007-04-06 18:23:17 +00003337 }
danielk1977d300b8a2008-08-01 10:50:23 +00003338 return rc;
drh165ffe92005-03-15 17:09:30 +00003339}
3340
3341/*
danielk1977443c0592009-01-16 15:21:05 +00003342** Read the content for page pPg out of the database file and into
3343** pPg->pData. A shared lock or greater must be held on the database
3344** file before this function is called.
3345**
3346** If page 1 is read, then the value of Pager.dbFileVers[] is set to
3347** the value read from the database file.
3348**
3349** If an IO error occurs, then the IO error is returned to the caller.
3350** Otherwise, SQLITE_OK is returned.
danielk1977e180dd92007-04-05 17:15:52 +00003351*/
danielk1977443c0592009-01-16 15:21:05 +00003352static int readDbPage(PgHdr *pPg){
3353 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
3354 Pgno pgno = pPg->pgno; /* Page number to read */
3355 int rc; /* Return code */
3356 i64 iOffset; /* Byte offset of file to read from */
3357
3358 assert( pPager->state>=PAGER_SHARED && !MEMDB );
3359
3360 if( !isOpen(pPager->fd) ){
3361 assert( pPager->tempFile );
danielk19777a2b1ee2007-08-21 14:27:01 +00003362 return SQLITE_IOERR_SHORT_READ;
3363 }
danielk1977443c0592009-01-16 15:21:05 +00003364 iOffset = (pgno-1)*(i64)pPager->pageSize;
3365 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
3366 if( pgno==1 ){
3367 u8 *dbFileVers = &((u8*)pPg->pData)[24];
3368 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
3369 }
3370 CODEC1(pPager, pPg->pData, pgno, 3);
3371
drh538f5702007-04-13 02:14:30 +00003372 PAGER_INCR(sqlite3_pager_readdb_count);
3373 PAGER_INCR(pPager->nRead);
3374 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh30d53702009-01-06 15:58:57 +00003375 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
danielk1977443c0592009-01-16 15:21:05 +00003376 PAGERID(pPager), pgno, pager_pagehash(pPg)));
3377
danielk1977e180dd92007-04-05 17:15:52 +00003378 return rc;
3379}
3380
danielk1977e180dd92007-04-05 17:15:52 +00003381/*
danielk1977e277be02007-03-23 18:12:06 +00003382** This function is called to obtain the shared lock required before
3383** data may be read from the pager cache. If the shared lock has already
3384** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003385**
3386** Immediately after obtaining the shared lock (if required), this function
3387** checks for a hot-journal file. If one is found, an emergency rollback
3388** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003389*/
3390static int pagerSharedLock(Pager *pPager){
danielk1977443c0592009-01-16 15:21:05 +00003391 int rc = SQLITE_OK; /* Return code */
3392 int isErrorReset = 0; /* True if recovering from error state */
danielk1977e277be02007-03-23 18:12:06 +00003393
danielk1977ae72d982007-10-03 08:46:44 +00003394 /* If this database is opened for exclusive access, has no outstanding
danielk1977443c0592009-01-16 15:21:05 +00003395 ** page references and is in an error-state, this is a chance to clear
danielk1977ae72d982007-10-03 08:46:44 +00003396 ** the error. Discard the contents of the pager-cache and treat any
3397 ** open journal file as a hot-journal.
3398 */
danielk19778c0a7912008-08-20 14:49:23 +00003399 if( !MEMDB && pPager->exclusiveMode
3400 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
3401 ){
danielk1977443c0592009-01-16 15:21:05 +00003402 if( isOpen(pPager->jfd) ){
danielk1977d300b8a2008-08-01 10:50:23 +00003403 isErrorReset = 1;
danielk1977ae72d982007-10-03 08:46:44 +00003404 }
danielk1977ae72d982007-10-03 08:46:44 +00003405 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00003406 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00003407 }
3408
3409 /* If the pager is still in an error state, do not proceed. The error
3410 ** state will be cleared at some point in the future when all page
3411 ** references are dropped and the cache can be discarded.
3412 */
3413 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3414 return pPager->errCode;
3415 }
3416
danielk1977d300b8a2008-08-01 10:50:23 +00003417 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
danielk1977443c0592009-01-16 15:21:05 +00003418 sqlite3_vfs * const pVfs = pPager->pVfs;
drh4f21c4a2008-12-10 22:15:00 +00003419 int isHotJournal = 0;
shane049fc212008-10-22 16:26:47 +00003420 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00003421 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
3422 if( !pPager->noReadlock ){
3423 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3424 if( rc!=SQLITE_OK ){
3425 assert( pPager->state==PAGER_UNLOCK );
3426 return pager_error(pPager, rc);
danielk1977e277be02007-03-23 18:12:06 +00003427 }
danielk19770371f1b2009-01-09 17:11:04 +00003428 }else if( pPager->state==PAGER_UNLOCK ){
3429 pPager->state = PAGER_SHARED;
danielk1977b3175382008-10-17 18:51:52 +00003430 }
danielk19770371f1b2009-01-09 17:11:04 +00003431 assert( pPager->state>=SHARED_LOCK );
danielk1977b3175382008-10-17 18:51:52 +00003432
3433 /* If a journal file exists, and there is no RESERVED lock on the
3434 ** database file, then it either needs to be played back or deleted.
3435 */
3436 if( !isErrorReset ){
3437 rc = hasHotJournal(pPager, &isHotJournal);
3438 if( rc!=SQLITE_OK ){
3439 goto failed;
3440 }
3441 }
3442 if( isErrorReset || isHotJournal ){
3443 /* Get an EXCLUSIVE lock on the database file. At this point it is
3444 ** important that a RESERVED lock is not obtained on the way to the
3445 ** EXCLUSIVE lock. If it were, another process might open the
3446 ** database file, detect the RESERVED lock, and conclude that the
danielk1977443c0592009-01-16 15:21:05 +00003447 ** database is safe to read while this process is still rolling the
3448 ** hot-journal back.
danielk1977b3175382008-10-17 18:51:52 +00003449 **
danielk1977443c0592009-01-16 15:21:05 +00003450 ** Because the intermediate RESERVED lock is not requested, any
3451 ** other process attempting to access the database file will get to
3452 ** this point in the code and fail to obtain its own EXCLUSIVE lock
3453 ** on the database file.
danielk1977e277be02007-03-23 18:12:06 +00003454 */
danielk1977b3175382008-10-17 18:51:52 +00003455 if( pPager->state<EXCLUSIVE_LOCK ){
3456 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
danielk1977e277be02007-03-23 18:12:06 +00003457 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003458 rc = pager_error(pPager, rc);
3459 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003460 }
danielk1977b3175382008-10-17 18:51:52 +00003461 pPager->state = PAGER_EXCLUSIVE;
3462 }
3463
3464 /* Open the journal for read/write access. This is because in
3465 ** exclusive-access mode the file descriptor will be kept open and
3466 ** possibly used for a transaction later on. On some systems, the
3467 ** OsTruncate() call used in exclusive-access mode also requires
3468 ** a read/write file handle.
3469 */
danielk1977443c0592009-01-16 15:21:05 +00003470 if( !isOpen(pPager->jfd) ){
danielk1977b3175382008-10-17 18:51:52 +00003471 int res;
3472 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
3473 if( rc==SQLITE_OK ){
3474 if( res ){
3475 int fout = 0;
3476 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3477 assert( !pPager->tempFile );
3478 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
danielk1977443c0592009-01-16 15:21:05 +00003479 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
danielk1977281d8bd2008-10-29 07:01:56 +00003480 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
3481 rc = SQLITE_CANTOPEN;
danielk1977b3175382008-10-17 18:51:52 +00003482 sqlite3OsClose(pPager->jfd);
3483 }
3484 }else{
3485 /* If the journal does not exist, that means some other process
3486 ** has already rolled it back */
3487 rc = SQLITE_BUSY;
3488 }
3489 }
3490 }
3491 if( rc!=SQLITE_OK ){
danielk1977b3175382008-10-17 18:51:52 +00003492 goto failed;
3493 }
danielk1977443c0592009-01-16 15:21:05 +00003494
3495 /* TODO: Why are these cleared here? Is it necessary? */
danielk1977b3175382008-10-17 18:51:52 +00003496 pPager->journalStarted = 0;
3497 pPager->journalOff = 0;
3498 pPager->setMaster = 0;
3499 pPager->journalHdr = 0;
3500
3501 /* Playback and delete the journal. Drop the database write
danielk1977112f7522009-01-08 17:50:45 +00003502 ** lock and reacquire the read lock. Purge the cache before
3503 ** playing back the hot-journal so that we don't end up with
danielk1977ad0ea222009-01-08 18:04:13 +00003504 ** an inconsistent cache.
danielk1977b3175382008-10-17 18:51:52 +00003505 */
danielk1977112f7522009-01-08 17:50:45 +00003506 sqlite3PcacheClear(pPager->pPCache);
danielk1977b3175382008-10-17 18:51:52 +00003507 rc = pager_playback(pPager, 1);
3508 if( rc!=SQLITE_OK ){
3509 rc = pager_error(pPager, rc);
3510 goto failed;
3511 }
danielk1977443c0592009-01-16 15:21:05 +00003512 assert( (pPager->state==PAGER_SHARED)
3513 || (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
danielk1977b3175382008-10-17 18:51:52 +00003514 );
3515 }
3516
3517 if( sqlite3PcachePagecount(pPager->pPCache)>0 ){
3518 /* The shared-lock has just been acquired on the database file
3519 ** and there are already pages in the cache (from a previous
3520 ** read or write transaction). Check to see if the database
3521 ** has been modified. If the database has changed, flush the
3522 ** cache.
3523 **
3524 ** Database changes is detected by looking at 15 bytes beginning
3525 ** at offset 24 into the file. The first 4 of these 16 bytes are
3526 ** a 32-bit counter that is incremented with each change. The
3527 ** other bytes change randomly with each file change when
3528 ** a codec is in use.
3529 **
3530 ** There is a vanishingly small chance that a change will not be
3531 ** detected. The chance of an undetected change is so small that
3532 ** it can be neglected.
3533 */
3534 char dbFileVers[sizeof(pPager->dbFileVers)];
3535 sqlite3PagerPagecount(pPager, 0);
3536
3537 if( pPager->errCode ){
3538 rc = pPager->errCode;
3539 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003540 }
3541
danielk1977d92db532008-11-17 04:56:24 +00003542 assert( pPager->dbSizeValid );
danielk1977b3175382008-10-17 18:51:52 +00003543 if( pPager->dbSize>0 ){
3544 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
3545 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
3546 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003547 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003548 }
danielk1977b3175382008-10-17 18:51:52 +00003549 }else{
3550 memset(dbFileVers, 0, sizeof(dbFileVers));
3551 }
danielk1977e277be02007-03-23 18:12:06 +00003552
danielk1977b3175382008-10-17 18:51:52 +00003553 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
3554 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003555 }
3556 }
danielk19770371f1b2009-01-09 17:11:04 +00003557 assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED );
danielk1977e277be02007-03-23 18:12:06 +00003558 }
3559
danielk197752b472a2008-05-05 16:23:55 +00003560 failed:
3561 if( rc!=SQLITE_OK ){
3562 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
3563 pager_unlock(pPager);
3564 }
danielk1977e277be02007-03-23 18:12:06 +00003565 return rc;
3566}
3567
3568/*
danielk19778c0a7912008-08-20 14:49:23 +00003569** If the reference count has reached zero, and the pager is not in the
3570** middle of a write transaction or opened in exclusive mode, unlock it.
3571*/
3572static void pagerUnlockIfUnused(Pager *pPager){
3573 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
3574 && (!pPager->exclusiveMode || pPager->journalOff>0)
3575 ){
3576 pagerUnlockAndRollback(pPager);
3577 }
3578}
3579
3580/*
3581** Drop a page from the cache using sqlite3PcacheDrop().
3582**
3583** If this means there are now no pages with references to them, a rollback
3584** occurs and the lock on the database is removed.
3585*/
3586static void pagerDropPage(DbPage *pPg){
3587 Pager *pPager = pPg->pPager;
3588 sqlite3PcacheDrop(pPg);
3589 pagerUnlockIfUnused(pPager);
3590}
3591
3592/*
drhd9b02572001-04-15 00:37:09 +00003593** Acquire a page.
3594**
drh58a11682001-11-10 13:51:08 +00003595** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003596** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003597**
drhd33d5a82007-04-26 12:11:28 +00003598** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003599** file is smaller than the requested page, then no actual disk
3600** read occurs and the memory image of the page is initialized to
3601** all zeros. The extra data appended to a page is always initialized
3602** to zeros the first time a page is loaded into memory.
3603**
drhd9b02572001-04-15 00:37:09 +00003604** The acquisition might fail for several reasons. In all cases,
3605** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003606**
drhd33d5a82007-04-26 12:11:28 +00003607** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003608** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003609** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003610** just returns 0. This routine acquires a read-lock the first time it
3611** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003612** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003613** or journal files.
drh0787db62007-03-04 13:15:27 +00003614**
drh538f5702007-04-13 02:14:30 +00003615** If noContent is false, the page contents are actually read from disk.
3616** If noContent is true, it means that we do not care about the contents
danielk1977443c0592009-01-16 15:21:05 +00003617** of the page. This occurs in two seperate scenarios:
3618**
3619** a) When reading a free-list leaf page from the database, and
3620**
3621** b) When a savepoint is being rolled back and we need to load
3622** a new page into the cache to populate with the data read
3623** from the savepoint journal.
3624**
3625** If noContent is true, then the data returned is zeroed instead of
3626** being read from the database. Additionally, the bits corresponding
3627** to pgno in Pager.pInJournal (bitvec of pages already written to the
3628** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
3629** savepoints are set. This means if the page is made writable at any
3630** point in the future, using a call to sqlite3PagerWrite(), its contents
3631** will not be journaled. This saves IO.
drhed7c8552001-04-11 14:29:21 +00003632*/
danielk197765e0ff32008-09-19 09:14:43 +00003633int sqlite3PagerAcquire(
drh538f5702007-04-13 02:14:30 +00003634 Pager *pPager, /* The pager open on the database file */
3635 Pgno pgno, /* Page number to fetch */
3636 DbPage **ppPage, /* Write a pointer to the page here */
3637 int noContent /* Do not bother reading content from disk if true */
3638){
danielk19778c0a7912008-08-20 14:49:23 +00003639 PgHdr *pPg = 0;
drh165ffe92005-03-15 17:09:30 +00003640 int rc;
drhed7c8552001-04-11 14:29:21 +00003641
danielk19778c0a7912008-08-20 14:49:23 +00003642 assert( pPager->state==PAGER_UNLOCK
3643 || sqlite3PcacheRefCount(pPager->pPCache)>0
danielk1977b3175382008-10-17 18:51:52 +00003644 || pgno==1
danielk19778c0a7912008-08-20 14:49:23 +00003645 );
danielk1977e277be02007-03-23 18:12:06 +00003646
danielk197726836652005-01-17 01:33:13 +00003647 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3648 ** number greater than this, or zero, is requested.
3649 */
drh267cb322005-09-16 17:16:52 +00003650 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003651 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003652 }
3653
drhd9b02572001-04-15 00:37:09 +00003654 /* Make sure we have not hit any critical errors.
3655 */
drh836faa42003-01-11 13:30:57 +00003656 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003657 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003658
danielk197713adf8a2004-06-03 16:08:41 +00003659 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003660 ** on the database file. pagerSharedLock() is a no-op if
3661 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003662 */
danielk1977e277be02007-03-23 18:12:06 +00003663 rc = pagerSharedLock(pPager);
3664 if( rc!=SQLITE_OK ){
3665 return rc;
drhed7c8552001-04-11 14:29:21 +00003666 }
danielk1977e277be02007-03-23 18:12:06 +00003667 assert( pPager->state!=PAGER_UNLOCK );
3668
danielk19778c0a7912008-08-20 14:49:23 +00003669 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
3670 if( rc!=SQLITE_OK ){
3671 return rc;
3672 }
danielk19778c0a7912008-08-20 14:49:23 +00003673 if( pPg->pPager==0 ){
3674 /* The pager cache has created a new page. Its content needs to
3675 ** be initialized.
3676 */
danielk1977979f38e2007-03-27 16:19:51 +00003677 int nMax;
drh538f5702007-04-13 02:14:30 +00003678 PAGER_INCR(pPager->nMiss);
danielk19778c0a7912008-08-20 14:49:23 +00003679 pPg->pPager = pPager;
danielk19778c0a7912008-08-20 14:49:23 +00003680 memset(pPg->pExtra, 0, pPager->nExtra);
danielk197724168722007-04-02 05:07:47 +00003681
danielk1977ad0132d2008-06-07 08:58:22 +00003682 rc = sqlite3PagerPagecount(pPager, &nMax);
3683 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00003684 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003685 return rc;
3686 }
danielk197775bab7d2006-01-23 13:09:45 +00003687
danielk1977a1fa00d2008-08-27 15:16:33 +00003688 if( nMax<(int)pgno || MEMDB || noContent ){
drhf8e632b2007-05-08 14:51:36 +00003689 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003690 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003691 return SQLITE_FULL;
3692 }
danielk19778c0a7912008-08-20 14:49:23 +00003693 memset(pPg->pData, 0, pPager->pageSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00003694 if( noContent ){
danielk1977443c0592009-01-16 15:21:05 +00003695 /* Failure to set the bits in the InJournal bit-vectors is benign.
3696 ** It merely means that we might do some extra work to journal a
3697 ** page that does not need to be journaled. Nevertheless, be sure
3698 ** to test the case where a malloc error occurs while trying to set
3699 ** a bit in a bit vector.
3700 */
3701 sqlite3BeginBenignMalloc();
3702 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3703 testcase( rc==SQLITE_NOMEM );
3704 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pPg->pgno);
3705 testcase( rc==SQLITE_NOMEM );
3706 sqlite3EndBenignMalloc();
danielk19778c0a7912008-08-20 14:49:23 +00003707 }
drh538f5702007-04-13 02:14:30 +00003708 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003709 }else{
danielk1977443c0592009-01-16 15:21:05 +00003710 assert( pPg->pPager==pPager && pPg->pgno==pgno );
3711 rc = readDbPage(pPg);
drh551b7732006-11-06 21:20:25 +00003712 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
danielk19778c0a7912008-08-20 14:49:23 +00003713 pagerDropPage(pPg);
drh551b7732006-11-06 21:20:25 +00003714 return rc;
drh81a20f22001-10-12 17:30:04 +00003715 }
drh306dc212001-05-21 13:45:10 +00003716 }
danielk19773c407372005-02-15 02:54:14 +00003717#ifdef SQLITE_CHECK_PAGES
3718 pPg->pageHash = pager_pagehash(pPg);
3719#endif
drhed7c8552001-04-11 14:29:21 +00003720 }else{
drhd9b02572001-04-15 00:37:09 +00003721 /* The requested page is in the page cache. */
drh538f5702007-04-13 02:14:30 +00003722 PAGER_INCR(pPager->nHit);
drhed7c8552001-04-11 14:29:21 +00003723 }
danielk19778c0a7912008-08-20 14:49:23 +00003724
danielk19773b8a05f2007-03-19 17:44:26 +00003725 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003726 return SQLITE_OK;
3727}
danielk19778c0a7912008-08-20 14:49:23 +00003728
drhed7c8552001-04-11 14:29:21 +00003729/*
drh7e3b0a02001-04-28 16:52:40 +00003730** Acquire a page if it is already in the in-memory cache. Do
3731** not read the page from disk. Return a pointer to the page,
3732** or 0 if the page is not in cache.
3733**
danielk19773b8a05f2007-03-19 17:44:26 +00003734** See also sqlite3PagerGet(). The difference between this routine
3735** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003736** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003737** returns NULL if the page is not in cache or if a disk I/O error
3738** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003739*/
danielk19773b8a05f2007-03-19 17:44:26 +00003740DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003741 PgHdr *pPg = 0;
drh836faa42003-01-11 13:30:57 +00003742 assert( pPager!=0 );
3743 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003744
danielk19778c0a7912008-08-20 14:49:23 +00003745 if( (pPager->state!=PAGER_UNLOCK)
3746 && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL)
3747 ){
3748 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
danielk1977e277be02007-03-23 18:12:06 +00003749 }
danielk19778c0a7912008-08-20 14:49:23 +00003750
danielk19773b8a05f2007-03-19 17:44:26 +00003751 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003752}
3753
3754/*
drhed7c8552001-04-11 14:29:21 +00003755** Release a page.
3756**
3757** If the number of references to the page drop to zero, then the
3758** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003759** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003760** removed.
3761*/
danielk1977443c0592009-01-16 15:21:05 +00003762void sqlite3PagerUnref(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003763 if( pPg ){
3764 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00003765 sqlite3PcacheRelease(pPg);
3766 pagerUnlockIfUnused(pPager);
drhed7c8552001-04-11 14:29:21 +00003767 }
drhed7c8552001-04-11 14:29:21 +00003768}
3769
danielk19779153d852009-01-07 10:52:56 +00003770/*
3771** If the main journal file has already been opened, ensure that the
3772** sub-journal file is open too. If the main journal is not open,
3773** this function is a no-op.
3774**
3775** SQLITE_OK is returned if everything goes according to plan. An
3776** SQLITE_IOERR_XXX error code is returned if the call to
3777** sqlite3OsOpen() fails.
3778*/
danielk1977fd7f0452008-12-17 17:30:26 +00003779static int openSubJournal(Pager *pPager){
3780 int rc = SQLITE_OK;
danielk1977443c0592009-01-16 15:21:05 +00003781 if( isOpen(pPager->jfd) && !isOpen(pPager->sjfd) ){
danielk1977fd7f0452008-12-17 17:30:26 +00003782 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3783 sqlite3MemJournalOpen(pPager->sjfd);
3784 }else{
danielk1977443c0592009-01-16 15:21:05 +00003785 rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
danielk1977fd7f0452008-12-17 17:30:26 +00003786 }
3787 }
3788 return rc;
3789}
3790
drhed7c8552001-04-11 14:29:21 +00003791/*
drha6abd042004-06-09 17:37:22 +00003792** Create a journal file for pPager. There should already be a RESERVED
3793** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003794**
3795** Return SQLITE_OK if everything. Return an error code and release the
3796** write lock if anything goes wrong.
3797*/
3798static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003799 sqlite3_vfs *pVfs = pPager->pVfs;
3800 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3801
drhda47d772002-12-02 04:25:19 +00003802 int rc;
drha6abd042004-06-09 17:37:22 +00003803 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003804 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003805 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003806 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003807 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00003808 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003809 rc = SQLITE_NOMEM;
3810 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003811 }
danielk1977b4b47412007-08-17 15:53:36 +00003812
danielk1977443c0592009-01-16 15:21:05 +00003813 if( !isOpen(pPager->jfd) ){
drhfdc40e92008-04-17 20:59:37 +00003814 if( pPager->tempFile ){
3815 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3816 }else{
3817 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003818 }
danielk1977b3175382008-10-17 18:51:52 +00003819 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3820 sqlite3MemJournalOpen(pPager->jfd);
3821 rc = SQLITE_OK;
3822 }else{
drhfdc40e92008-04-17 20:59:37 +00003823#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977b3175382008-10-17 18:51:52 +00003824 rc = sqlite3JournalOpen(
3825 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3826 );
drhfdc40e92008-04-17 20:59:37 +00003827#else
danielk1977b3175382008-10-17 18:51:52 +00003828 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
drhfdc40e92008-04-17 20:59:37 +00003829#endif
danielk1977b3175382008-10-17 18:51:52 +00003830 }
danielk1977443c0592009-01-16 15:21:05 +00003831 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
drhfdc40e92008-04-17 20:59:37 +00003832 pPager->journalOff = 0;
3833 pPager->setMaster = 0;
3834 pPager->journalHdr = 0;
3835 if( rc!=SQLITE_OK ){
3836 if( rc==SQLITE_NOMEM ){
3837 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3838 }
3839 goto failed_to_open_journal;
3840 }
drhda47d772002-12-02 04:25:19 +00003841 }
drhdb48ee02003-01-16 13:42:43 +00003842 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003843 pPager->needSync = 0;
drh968af522003-02-11 14:55:40 +00003844 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003845 if( pPager->errCode ){
3846 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003847 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003848 }
danielk19773460d192008-12-27 15:23:13 +00003849 pPager->dbOrigSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003850
danielk197776572402004-06-25 02:38:54 +00003851 rc = writeJournalHdr(pPager);
3852
danielk1977fd7f0452008-12-17 17:30:26 +00003853 if( pPager->nSavepoint && rc==SQLITE_OK ){
3854 rc = openSubJournal(pPager);
drhda47d772002-12-02 04:25:19 +00003855 }
danielk1977ae72d982007-10-03 08:46:44 +00003856 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003857 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003858 if( rc==SQLITE_OK ){
3859 rc = SQLITE_FULL;
3860 }
3861 }
drh9c105bb2004-10-02 20:38:28 +00003862 return rc;
3863
3864failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003865 sqlite3BitvecDestroy(pPager->pInJournal);
3866 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003867 return rc;
drhda47d772002-12-02 04:25:19 +00003868}
3869
3870/*
drh4b845d72002-03-05 12:41:19 +00003871** Acquire a write-lock on the database. The lock is removed when
3872** the any of the following happen:
3873**
drh80e35f42007-03-30 14:06:34 +00003874** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003875** * sqlite3PagerRollback() is called.
3876** * sqlite3PagerClose() is called.
3877** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003878**
danielk197713adf8a2004-06-03 16:08:41 +00003879** The first parameter to this routine is a pointer to any open page of the
3880** database file. Nothing changes about the page - it is used merely to
3881** acquire a pointer to the Pager structure and as proof that there is
3882** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003883**
danielk197713adf8a2004-06-03 16:08:41 +00003884** The second parameter indicates how much space in bytes to reserve for a
3885** master journal file-name at the start of the journal when it is created.
3886**
3887** A journal file is opened if this is not a temporary file. For temporary
3888** files, the opening of the journal file is deferred until there is an
3889** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003890**
drha6abd042004-06-09 17:37:22 +00003891** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003892**
3893** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3894** immediately instead of waiting until we try to flush the cache. The
3895** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003896*/
danielk19773b8a05f2007-03-19 17:44:26 +00003897int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003898 Pager *pPager = pPg->pPager;
3899 int rc = SQLITE_OK;
3900 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003901 assert( pPager->state!=PAGER_UNLOCK );
3902 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003903 assert( pPager->pInJournal==0 );
danielk1977b3175382008-10-17 18:51:52 +00003904 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00003905 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
3906 if( rc==SQLITE_OK ){
3907 pPager->state = PAGER_RESERVED;
3908 if( exFlag ){
3909 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh684917c2004-10-05 02:41:42 +00003910 }
danielk1977b3175382008-10-17 18:51:52 +00003911 }
3912 if( rc!=SQLITE_OK ){
3913 return rc;
3914 }
drh30d53702009-01-06 15:58:57 +00003915 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00003916 if( pPager->useJournal && !pPager->tempFile
3917 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
3918 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00003919 }
danielk1977443c0592009-01-16 15:21:05 +00003920 }else if( isOpen(pPager->jfd) && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00003921 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00003922 ** time a (read or write) transaction was successfully concluded
3923 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00003924 ** kept open and either was truncated to 0 bytes or its header was
3925 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00003926 */
3927 assert( pPager->nRec==0 );
danielk19773460d192008-12-27 15:23:13 +00003928 assert( pPager->dbOrigSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003929 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003930 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003931 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhf5e7bb52008-02-18 14:47:33 +00003932 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003933 rc = SQLITE_NOMEM;
3934 }else{
danielk19773460d192008-12-27 15:23:13 +00003935 pPager->dbOrigSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003936 rc = writeJournalHdr(pPager);
3937 }
drh4b845d72002-03-05 12:41:19 +00003938 }
danielk1977443c0592009-01-16 15:21:05 +00003939 assert( !isOpen(pPager->jfd) || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003940 return rc;
3941}
3942
3943/*
drhed7c8552001-04-11 14:29:21 +00003944** Mark a data page as writeable. The page is written into the journal
3945** if it is not there already. This routine must be called before making
3946** changes to a page.
3947**
3948** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003949** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003950** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003951** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003952** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003953**
3954** If the journal file could not be written because the disk is full,
3955** then this routine returns SQLITE_FULL and does an immediate rollback.
3956** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003957** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003958** reset.
drhed7c8552001-04-11 14:29:21 +00003959*/
danielk19773b8a05f2007-03-19 17:44:26 +00003960static int pager_write(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003961 void *pData = pPg->pData;
drh69688d52001-04-14 16:38:23 +00003962 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003963 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003964
drh6446c4d2001-12-15 14:22:18 +00003965 /* Check for errors
3966 */
danielk1977efaaf572006-01-16 11:29:19 +00003967 if( pPager->errCode ){
3968 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003969 }
drh5e00f6c2001-09-13 13:46:56 +00003970 if( pPager->readOnly ){
3971 return SQLITE_PERM;
3972 }
drh6446c4d2001-12-15 14:22:18 +00003973
danielk197776572402004-06-25 02:38:54 +00003974 assert( !pPager->setMaster );
3975
danielk19773c407372005-02-15 02:54:14 +00003976 CHECK_PAGE(pPg);
3977
drh6446c4d2001-12-15 14:22:18 +00003978 /* Mark the page as dirty. If the page has already been written
3979 ** to the journal then we can return right away.
3980 */
drhc047b9f2008-11-21 03:23:43 +00003981 sqlite3PcacheMakeDirty(pPg);
danielk19773460d192008-12-27 15:23:13 +00003982 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
drhd138c012008-05-13 00:58:18 +00003983 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003984 }else{
drh6446c4d2001-12-15 14:22:18 +00003985
danielk1977a0bf2652004-11-04 14:30:04 +00003986 /* If we get this far, it means that the page needs to be
3987 ** written to the transaction journal or the ckeckpoint journal
3988 ** or both.
3989 **
3990 ** First check to see that the transaction journal exists and
3991 ** create it if it does not.
3992 */
3993 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003994 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003995 if( rc!=SQLITE_OK ){
3996 return rc;
3997 }
3998 assert( pPager->state>=PAGER_RESERVED );
danielk1977443c0592009-01-16 15:21:05 +00003999 if( !isOpen(pPager->jfd) && pPager->useJournal
drhfdc40e92008-04-17 20:59:37 +00004000 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00004001 rc = pager_open_journal(pPager);
4002 if( rc!=SQLITE_OK ) return rc;
4003 }
drhd138c012008-05-13 00:58:18 +00004004 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004005
4006 /* The transaction journal now exists and we have a RESERVED or an
4007 ** EXCLUSIVE lock on the main database file. Write the current page to
4008 ** the transaction journal if it is not there already.
4009 */
danielk1977443c0592009-01-16 15:21:05 +00004010 if( !pageInJournal(pPg) && isOpen(pPager->jfd) ){
danielk19773460d192008-12-27 15:23:13 +00004011 if( pPg->pgno<=pPager->dbOrigSize ){
danielk1977b3175382008-10-17 18:51:52 +00004012 u32 cksum;
4013 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00004014
danielk1977b3175382008-10-17 18:51:52 +00004015 /* We should never write to the journal file the page that
4016 ** contains the database locks. The following assert verifies
4017 ** that we do not. */
4018 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
4019 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4020 cksum = pager_cksum(pPager, (u8*)pData2);
4021 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4022 if( rc==SQLITE_OK ){
4023 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4024 pPager->journalOff + 4);
4025 pPager->journalOff += pPager->pageSize+4;
4026 }
4027 if( rc==SQLITE_OK ){
4028 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4029 pPager->journalOff += 4;
4030 }
4031 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
4032 pPager->journalOff, pPager->pageSize));
4033 PAGER_INCR(sqlite3_pager_writej_count);
drh30d53702009-01-06 15:58:57 +00004034 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
danielk1977b3175382008-10-17 18:51:52 +00004035 PAGERID(pPager), pPg->pgno,
drh30d53702009-01-06 15:58:57 +00004036 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
danielk197707cb5602006-01-20 10:55:05 +00004037
danielk1977f3107512008-12-22 10:58:46 +00004038 /* Even if an IO or diskfull error occurred while journalling the
4039 ** page in the block above, set the need-sync flag for the page.
4040 ** Otherwise, when the transaction is rolled back, the logic in
4041 ** playback_one_page() will think that the page needs to be restored
4042 ** in the database file. And if an IO error occurs while doing so,
4043 ** then corruption may follow.
4044 */
4045 if( !pPager->noSync ){
4046 pPg->flags |= PGHDR_NEED_SYNC;
danielk1977a4124bd2008-12-23 10:37:47 +00004047 pPager->needSync = 1;
danielk1977f3107512008-12-22 10:58:46 +00004048 }
4049
danielk1977b3175382008-10-17 18:51:52 +00004050 /* An error has occured writing to the journal file. The
4051 ** transaction will be rolled back by the layer above.
4052 */
4053 if( rc!=SQLITE_OK ){
4054 return rc;
4055 }
danielk197707cb5602006-01-20 10:55:05 +00004056
danielk1977b3175382008-10-17 18:51:52 +00004057 pPager->nRec++;
4058 assert( pPager->pInJournal!=0 );
drh7539b6b2009-01-02 21:39:39 +00004059 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
4060 testcase( rc==SQLITE_NOMEM );
4061 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4062 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
4063 if( rc!=SQLITE_OK ){
4064 assert( rc==SQLITE_NOMEM );
4065 return rc;
4066 }
danielk197713adf8a2004-06-03 16:08:41 +00004067 }else{
danielk19778c0a7912008-08-20 14:49:23 +00004068 if( !pPager->journalStarted && !pPager->noSync ){
4069 pPg->flags |= PGHDR_NEED_SYNC;
danielk1977a4124bd2008-12-23 10:37:47 +00004070 pPager->needSync = 1;
danielk19778c0a7912008-08-20 14:49:23 +00004071 }
drh30d53702009-01-06 15:58:57 +00004072 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
danielk19778c0a7912008-08-20 14:49:23 +00004073 PAGERID(pPager), pPg->pgno,
drh30d53702009-01-06 15:58:57 +00004074 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
danielk1977a0bf2652004-11-04 14:30:04 +00004075 }
danielk1977a0bf2652004-11-04 14:30:04 +00004076 }
4077
4078 /* If the statement journal is open and the page is not in it,
4079 ** then write the current page to the statement journal. Note that
4080 ** the statement journal format differs from the standard journal format
4081 ** in that it omits the checksums and the header.
4082 */
danielk19773460d192008-12-27 15:23:13 +00004083 if( subjRequiresPage(pPg) ){
danielk1977f2c31ad2009-01-06 13:40:08 +00004084 rc = subjournalPage(pPg);
drhd9b02572001-04-15 00:37:09 +00004085 }
drhfa86c412002-02-02 15:01:15 +00004086 }
4087
4088 /* Update the database size and return.
4089 */
drh1aa2d8b2007-01-03 15:34:29 +00004090 assert( pPager->state>=PAGER_SHARED );
danielk1977d92db532008-11-17 04:56:24 +00004091 if( pPager->dbSize<pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004092 pPager->dbSize = pPg->pgno;
danielk1977d92db532008-11-17 04:56:24 +00004093 if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){
drh1f595712004-06-15 01:40:29 +00004094 pPager->dbSize++;
4095 }
drh306dc212001-05-21 13:45:10 +00004096 }
drh69688d52001-04-14 16:38:23 +00004097 return rc;
drhed7c8552001-04-11 14:29:21 +00004098}
4099
4100/*
danielk19774099f6e2007-03-19 11:25:20 +00004101** This function is used to mark a data-page as writable. It uses
4102** pager_write() to open a journal file (if it is not already open)
4103** and write the page *pData to the journal.
4104**
4105** The difference between this function and pager_write() is that this
4106** function also deals with the special case where 2 or more pages
4107** fit on a single disk sector. In this case all co-resident pages
4108** must have been written to the journal file before returning.
4109*/
danielk19773b8a05f2007-03-19 17:44:26 +00004110int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004111 int rc = SQLITE_OK;
4112
danielk19773b8a05f2007-03-19 17:44:26 +00004113 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004114 Pager *pPager = pPg->pPager;
4115 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4116
danielk1977b3175382008-10-17 18:51:52 +00004117 if( nPagePerSector>1 ){
danielk19774099f6e2007-03-19 11:25:20 +00004118 Pgno nPageCount; /* Total number of pages in database file */
4119 Pgno pg1; /* First page of the sector pPg is located on. */
4120 int nPage; /* Number of pages starting at pg1 to journal */
4121 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004122 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004123
4124 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4125 ** header to be written between the pages journaled by this function.
4126 */
danielk1977b3175382008-10-17 18:51:52 +00004127 assert( !MEMDB );
danielk19774099f6e2007-03-19 11:25:20 +00004128 assert( pPager->doNotSync==0 );
4129 pPager->doNotSync = 1;
4130
4131 /* This trick assumes that both the page-size and sector-size are
4132 ** an integer power of 2. It sets variable pg1 to the identifier
4133 ** of the first page of the sector pPg is located on.
4134 */
4135 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4136
danielk1977ad0132d2008-06-07 08:58:22 +00004137 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00004138 if( pPg->pgno>nPageCount ){
4139 nPage = (pPg->pgno - pg1)+1;
4140 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4141 nPage = nPageCount+1-pg1;
4142 }else{
4143 nPage = nPagePerSector;
4144 }
4145 assert(nPage>0);
4146 assert(pg1<=pPg->pgno);
4147 assert((pg1+nPage)>pPg->pgno);
4148
4149 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4150 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004151 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00004152 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00004153 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004154 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004155 if( rc==SQLITE_OK ){
4156 rc = pager_write(pPage);
danielk19778c0a7912008-08-20 14:49:23 +00004157 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00004158 needSync = 1;
danielk1977a4124bd2008-12-23 10:37:47 +00004159 assert(pPager->needSync);
danielk1977dd97a492007-08-22 18:54:32 +00004160 }
danielk19773b8a05f2007-03-19 17:44:26 +00004161 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004162 }
4163 }
drhc81945e2008-03-10 14:12:53 +00004164 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk19778c0a7912008-08-20 14:49:23 +00004165 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00004166 needSync = 1;
4167 }
danielk19778c0a7912008-08-20 14:49:23 +00004168 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004169 }
4170 }
4171
drhee03d622009-01-07 15:33:45 +00004172 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
danielk1977dd97a492007-08-22 18:54:32 +00004173 ** starting at pg1, then it needs to be set for all of them. Because
4174 ** writing to any of these nPage pages may damage the others, the
4175 ** journal file must contain sync()ed copies of all of them
4176 ** before any of them can be written out to the database file.
4177 */
4178 if( needSync ){
drhb3df2e12008-09-17 20:06:26 +00004179 assert( !MEMDB && pPager->noSync==0 );
danielk1977dd97a492007-08-22 18:54:32 +00004180 for(ii=0; ii<nPage && needSync; ii++){
4181 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
drhee03d622009-01-07 15:33:45 +00004182 if( pPage ){
drhee03d622009-01-07 15:33:45 +00004183 pPage->flags |= PGHDR_NEED_SYNC;
4184 sqlite3PagerUnref(pPage);
4185 }
danielk1977dd97a492007-08-22 18:54:32 +00004186 }
4187 assert(pPager->needSync);
4188 }
4189
danielk19774099f6e2007-03-19 11:25:20 +00004190 assert( pPager->doNotSync==1 );
4191 pPager->doNotSync = 0;
4192 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004193 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004194 }
4195 return rc;
4196}
4197
4198/*
drhaacc5432002-01-06 17:07:40 +00004199** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004200** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004201** to change the content of the page.
4202*/
danielk19777d3a6662006-01-23 16:21:05 +00004203#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004204int sqlite3PagerIswriteable(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00004205 return pPg->flags&PGHDR_DIRTY;
drh6019e162001-07-02 17:51:45 +00004206}
danielk19777d3a6662006-01-23 16:21:05 +00004207#endif
drh6019e162001-07-02 17:51:45 +00004208
drh001bbcb2003-03-19 03:14:00 +00004209/*
drh30e58752002-03-02 20:41:57 +00004210** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004211** write the information on page pPg back to the disk, even though
drhdfe88ec2008-11-03 20:55:06 +00004212** that page might be marked as dirty. This happens, for example, when
4213** the page has been added as a leaf of the freelist and so its
4214** content no longer matters.
drh30e58752002-03-02 20:41:57 +00004215**
4216** The overlying software layer calls this routine when all of the data
4217** on the given page is unused. The pager marks the page as clean so
4218** that it does not get written to disk.
4219**
4220** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004221** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004222** of large INSERT operations and quadruple the speed of large DELETEs.
4223*/
danielk1977443c0592009-01-16 15:21:05 +00004224void sqlite3PagerDontWrite(PgHdr *pPg){
drh538f5702007-04-13 02:14:30 +00004225 Pager *pPager = pPg->pPager;
danielk1977443c0592009-01-16 15:21:05 +00004226 if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
4227 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
4228 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
4229 pPg->flags |= PGHDR_DONT_WRITE;
danielk19773c407372005-02-15 02:54:14 +00004230#ifdef SQLITE_CHECK_PAGES
danielk1977443c0592009-01-16 15:21:05 +00004231 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00004232#endif
drh30e58752002-03-02 20:41:57 +00004233 }
4234}
4235
4236/*
drh80e35f42007-03-30 14:06:34 +00004237** This routine is called to increment the database file change-counter,
4238** stored at byte 24 of the pager file.
4239*/
danielk1977c7b60172007-08-22 11:22:03 +00004240static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004241 PgHdr *pPgHdr;
4242 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004243 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004244
drh701bb3b2008-08-02 03:50:39 +00004245#ifndef SQLITE_ENABLE_ATOMIC_WRITE
4246 assert( isDirect==0 ); /* isDirect is only true for atomic writes */
4247#endif
danielk197712dd5492008-12-18 15:45:07 +00004248 if( !pPager->changeCountDone && pPager->dbSize>0 ){
drh80e35f42007-03-30 14:06:34 +00004249 /* Open page 1 of the file for writing. */
4250 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4251 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004252
4253 if( !isDirect ){
4254 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00004255 if( rc!=SQLITE_OK ){
4256 sqlite3PagerUnref(pPgHdr);
4257 return rc;
4258 }
danielk1977c7b60172007-08-22 11:22:03 +00004259 }
4260
drh80e35f42007-03-30 14:06:34 +00004261 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004262 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004263 change_counter++;
danielk19778c0a7912008-08-20 14:49:23 +00004264 put32bits(((char*)pPgHdr->pData)+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004265
drh701bb3b2008-08-02 03:50:39 +00004266#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977443c0592009-01-16 15:21:05 +00004267 if( isDirect && isOpen(pPager->fd) ){
danielk19778c0a7912008-08-20 14:49:23 +00004268 const void *zBuf = pPgHdr->pData;
danielk19773460d192008-12-27 15:23:13 +00004269 assert( pPager->dbFileSize>0 );
danielk1977c7b60172007-08-22 11:22:03 +00004270 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4271 }
drh701bb3b2008-08-02 03:50:39 +00004272#endif
danielk1977c7b60172007-08-22 11:22:03 +00004273
drh80e35f42007-03-30 14:06:34 +00004274 /* Release the page reference. */
4275 sqlite3PagerUnref(pPgHdr);
4276 pPager->changeCountDone = 1;
4277 }
danielk1977c7b60172007-08-22 11:22:03 +00004278 return rc;
drh80e35f42007-03-30 14:06:34 +00004279}
4280
4281/*
danielk1977f653d782008-03-20 11:04:21 +00004282** Sync the pager file to disk.
4283*/
4284int sqlite3PagerSync(Pager *pPager){
4285 int rc;
drh7426f862008-08-26 21:07:26 +00004286 if( MEMDB ){
4287 rc = SQLITE_OK;
4288 }else{
4289 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4290 }
danielk1977f653d782008-03-20 11:04:21 +00004291 return rc;
4292}
4293
4294/*
drh80e35f42007-03-30 14:06:34 +00004295** Sync the database file for the pager pPager. zMaster points to the name
4296** of a master journal file that should be written into the individual
4297** journal file. zMaster may be NULL, which is interpreted as no master
4298** journal (a single database transaction).
4299**
4300** This routine ensures that the journal is synced, all dirty pages written
4301** to the database file and the database file synced. The only thing that
4302** remains to commit the transaction is to delete the journal file (or
4303** master journal file if specified).
4304**
4305** Note that if zMaster==NULL, this does not overwrite a previous value
4306** passed to an sqlite3PagerCommitPhaseOne() call.
4307**
danielk1977f653d782008-03-20 11:04:21 +00004308** If the final parameter - noSync - is true, then the database file itself
4309** is not synced. The caller must call sqlite3PagerSync() directly to
4310** sync the database file before calling CommitPhaseTwo() to delete the
4311** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00004312*/
danielk1977f653d782008-03-20 11:04:21 +00004313int sqlite3PagerCommitPhaseOne(
4314 Pager *pPager,
4315 const char *zMaster,
danielk1977f653d782008-03-20 11:04:21 +00004316 int noSync
4317){
drh80e35f42007-03-30 14:06:34 +00004318 int rc = SQLITE_OK;
4319
danielk1977dad31b52008-05-15 11:08:07 +00004320 if( pPager->errCode ){
4321 return pPager->errCode;
4322 }
4323
drhd138c012008-05-13 00:58:18 +00004324 /* If no changes have been made, we can leave the transaction early.
4325 */
4326 if( pPager->dbModified==0 &&
4327 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
danielk1977443c0592009-01-16 15:21:05 +00004328 pPager->exclusiveMode) ){
4329 assert( pPager->dbModified==0 || !isOpen(pPager->jfd) );
drhd138c012008-05-13 00:58:18 +00004330 return SQLITE_OK;
4331 }
4332
drh30d53702009-01-06 15:58:57 +00004333 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
4334 pPager->zFilename, zMaster, pPager->dbSize));
drh80e35f42007-03-30 14:06:34 +00004335
4336 /* If this is an in-memory db, or no pages have been written to, or this
4337 ** function has already been called, it is a no-op.
4338 */
danielk1977443c0592009-01-16 15:21:05 +00004339 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dbModified ){
drh80e35f42007-03-30 14:06:34 +00004340 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004341
danielk1977c7b60172007-08-22 11:22:03 +00004342#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4343 /* The atomic-write optimization can be used if all of the
4344 ** following are true:
4345 **
4346 ** + The file-system supports the atomic-write property for
4347 ** blocks of size page-size, and
4348 ** + This commit is not part of a multi-file transaction, and
4349 ** + Exactly one page has been modified and store in the journal file.
4350 **
4351 ** If the optimization can be used, then the journal file will never
4352 ** be created for this transaction.
4353 */
danielk19774d60af92008-09-08 15:35:06 +00004354 int useAtomicWrite;
danielk19778c0a7912008-08-20 14:49:23 +00004355 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
danielk19774d60af92008-09-08 15:35:06 +00004356 useAtomicWrite = (
danielk1977f55b8992007-08-24 08:15:53 +00004357 !zMaster &&
danielk1977443c0592009-01-16 15:21:05 +00004358 isOpen(pPager->jfd) &&
danielk1977f55b8992007-08-24 08:15:53 +00004359 pPager->journalOff==jrnlBufferSize(pPager) &&
danielk19773460d192008-12-27 15:23:13 +00004360 pPager->dbSize>=pPager->dbFileSize &&
danielk19778c0a7912008-08-20 14:49:23 +00004361 (pPg==0 || pPg->pDirty==0)
danielk1977f55b8992007-08-24 08:15:53 +00004362 );
danielk1977443c0592009-01-16 15:21:05 +00004363 assert( isOpen(pPager->jfd) || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00004364 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004365 /* Update the nRec field in the journal file. */
4366 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4367 assert(pPager->nRec==1);
4368 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4369
4370 /* Update the db file change counter. The following call will modify
4371 ** the in-memory representation of page 1 to include the updated
4372 ** change counter and then write page 1 directly to the database
4373 ** file. Because of the atomic-write property of the host file-system,
4374 ** this is safe.
4375 */
danielk1977ae72d982007-10-03 08:46:44 +00004376 if( rc==SQLITE_OK ){
4377 rc = pager_incr_changecounter(pPager, 1);
4378 }
danielk1977f55b8992007-08-24 08:15:53 +00004379 }else{
4380 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00004381 }
4382
danielk1977ae72d982007-10-03 08:46:44 +00004383 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00004384#endif
4385
drh80e35f42007-03-30 14:06:34 +00004386 /* If a master journal file name has already been written to the
4387 ** journal file, then no sync is required. This happens when it is
4388 ** written, then the process fails to upgrade from a RESERVED to an
4389 ** EXCLUSIVE lock. The next time the process tries to commit the
4390 ** transaction the m-j name will have already been written.
4391 */
4392 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00004393 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004394 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00004395 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00004396#ifndef SQLITE_OMIT_AUTOVACUUM
danielk19773460d192008-12-27 15:23:13 +00004397 if( pPager->dbSize<pPager->dbOrigSize ){
danielk197771aa7ff2008-05-20 07:05:09 +00004398 /* If this transaction has made the database smaller, then all pages
4399 ** being discarded by the truncation must be written to the journal
4400 ** file.
4401 */
4402 Pgno i;
danielk1977d92db532008-11-17 04:56:24 +00004403 Pgno iSkip = PAGER_MJ_PGNO(pPager);
danielk19773460d192008-12-27 15:23:13 +00004404 Pgno dbSize = pPager->dbSize;
danielk1977f9bce3c2009-01-06 15:20:58 +00004405 pPager->dbSize = pPager->dbOrigSize;
danielk1977f70c1fe2009-01-07 18:08:48 +00004406 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
danielk197771aa7ff2008-05-20 07:05:09 +00004407 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
4408 rc = sqlite3PagerGet(pPager, i, &pPg);
4409 if( rc!=SQLITE_OK ) goto sync_exit;
4410 rc = sqlite3PagerWrite(pPg);
4411 sqlite3PagerUnref(pPg);
4412 if( rc!=SQLITE_OK ) goto sync_exit;
4413 }
4414 }
danielk19773460d192008-12-27 15:23:13 +00004415 pPager->dbSize = dbSize;
danielk197771aa7ff2008-05-20 07:05:09 +00004416 }
drh80e35f42007-03-30 14:06:34 +00004417#endif
danielk197771aa7ff2008-05-20 07:05:09 +00004418 rc = writeMasterJournal(pPager, zMaster);
4419 if( rc!=SQLITE_OK ) goto sync_exit;
4420 rc = syncJournal(pPager);
4421 }
drh80e35f42007-03-30 14:06:34 +00004422 }
danielk1977c7b60172007-08-22 11:22:03 +00004423 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004424
drh80e35f42007-03-30 14:06:34 +00004425 /* Write all dirty pages to the database file */
danielk19778c0a7912008-08-20 14:49:23 +00004426 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00004427 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004428 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004429 assert( rc!=SQLITE_IOERR_BLOCKED );
4430 /* The error might have left the dirty list all fouled up here,
4431 ** but that does not matter because if the if the dirty list did
4432 ** get corrupted, then the transaction will roll back and
4433 ** discard the dirty list. There is an assert in
4434 ** pager_get_all_dirty_pages() that verifies that no attempt
4435 ** is made to use an invalid dirty list.
4436 */
drh153c62c2007-08-24 03:51:33 +00004437 goto sync_exit;
4438 }
danielk19778c0a7912008-08-20 14:49:23 +00004439 sqlite3PcacheCleanAll(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00004440
danielk1977443c0592009-01-16 15:21:05 +00004441 if( pPager->dbSize!=pPager->dbFileSize ){
4442 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
danielk1977f90b7262009-01-07 15:18:20 +00004443 assert( pPager->state>=PAGER_EXCLUSIVE );
danielk1977443c0592009-01-16 15:21:05 +00004444 rc = pager_truncate(pPager, nNew);
danielk1977f90b7262009-01-07 15:18:20 +00004445 if( rc!=SQLITE_OK ) goto sync_exit;
4446 }
4447
drh80e35f42007-03-30 14:06:34 +00004448 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004449 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004450 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004451 }
4452 IOTRACE(("DBSYNC %p\n", pPager))
4453
4454 pPager->state = PAGER_SYNCED;
drh80e35f42007-03-30 14:06:34 +00004455 }
4456
4457sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004458 if( rc==SQLITE_IOERR_BLOCKED ){
4459 /* pager_incr_changecounter() may attempt to obtain an exclusive
4460 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004461 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004462 * better to return SQLITE_BUSY.
4463 */
4464 rc = SQLITE_BUSY;
4465 }
drh80e35f42007-03-30 14:06:34 +00004466 return rc;
4467}
4468
4469
4470/*
drhed7c8552001-04-11 14:29:21 +00004471** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004472**
4473** If the commit fails for any reason, a rollback attempt is made
4474** and an error code is returned. If the commit worked, SQLITE_OK
4475** is returned.
drhed7c8552001-04-11 14:29:21 +00004476*/
drh80e35f42007-03-30 14:06:34 +00004477int sqlite3PagerCommitPhaseTwo(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004478 int rc = SQLITE_OK;
drhd9b02572001-04-15 00:37:09 +00004479
danielk1977efaaf572006-01-16 11:29:19 +00004480 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004481 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004482 }
drha6abd042004-06-09 17:37:22 +00004483 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004484 return SQLITE_ERROR;
4485 }
drhd138c012008-05-13 00:58:18 +00004486 if( pPager->dbModified==0 &&
4487 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
4488 pPager->exclusiveMode!=0) ){
danielk1977443c0592009-01-16 15:21:05 +00004489 assert( pPager->dbModified==0 || isOpen(pPager->jfd)==0 );
drhd138c012008-05-13 00:58:18 +00004490 return SQLITE_OK;
4491 }
drh30d53702009-01-06 15:58:57 +00004492 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
danielk1977443c0592009-01-16 15:21:05 +00004493 assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dbModified );
danielk1977b3175382008-10-17 18:51:52 +00004494 rc = pager_end_transaction(pPager, pPager->setMaster);
4495 rc = pager_error(pPager, rc);
drh86f8c192007-08-22 00:39:19 +00004496 return rc;
drhed7c8552001-04-11 14:29:21 +00004497}
4498
4499/*
drha6abd042004-06-09 17:37:22 +00004500** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004501** All in-memory cache pages revert to their original data contents.
4502** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004503**
4504** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004505** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004506** process is writing trash into the journal file (SQLITE_CORRUPT) or
4507** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4508** codes are returned for all these occasions. Otherwise,
4509** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004510*/
danielk19773b8a05f2007-03-19 17:44:26 +00004511int sqlite3PagerRollback(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004512 int rc = SQLITE_OK;
drh30d53702009-01-06 15:58:57 +00004513 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
danielk1977443c0592009-01-16 15:21:05 +00004514 if( !pPager->dbModified || !isOpen(pPager->jfd) ){
danielk1977df2566a2008-05-07 19:11:03 +00004515 rc = pager_end_transaction(pPager, pPager->setMaster);
danielk19778c0a7912008-08-20 14:49:23 +00004516 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004517 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004518 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004519 }
danielk19778c0a7912008-08-20 14:49:23 +00004520 rc = pPager->errCode;
drha6abd042004-06-09 17:37:22 +00004521 }else{
danielk19778c0a7912008-08-20 14:49:23 +00004522 if( pPager->state==PAGER_RESERVED ){
4523 int rc2;
4524 rc = pager_playback(pPager, 0);
4525 rc2 = pager_end_transaction(pPager, pPager->setMaster);
4526 if( rc==SQLITE_OK ){
4527 rc = rc2;
4528 }
4529 }else{
4530 rc = pager_playback(pPager, 0);
4531 }
danielk197707cb5602006-01-20 10:55:05 +00004532
danielk1977b3175382008-10-17 18:51:52 +00004533 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00004534 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00004535 }
danielk19778c0a7912008-08-20 14:49:23 +00004536
4537 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4538 ** cache. So call pager_error() on the way out to make any error
4539 ** persistent.
4540 */
4541 rc = pager_error(pPager, rc);
4542 }
drh86f8c192007-08-22 00:39:19 +00004543 return rc;
drh98808ba2001-10-18 12:34:46 +00004544}
drhd9b02572001-04-15 00:37:09 +00004545
4546/*
drh5e00f6c2001-09-13 13:46:56 +00004547** Return TRUE if the database file is opened read-only. Return FALSE
4548** if the database is (in theory) writable.
4549*/
drhf49661a2008-12-10 16:45:50 +00004550u8 sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004551 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004552}
4553
4554/*
drh0f7eb612006-08-08 13:51:43 +00004555** Return the number of references to the pager.
4556*/
danielk19773b8a05f2007-03-19 17:44:26 +00004557int sqlite3PagerRefcount(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004558 return sqlite3PcacheRefCount(pPager->pPCache);
drh0f7eb612006-08-08 13:51:43 +00004559}
4560
danielk197771d5d2c2008-09-29 11:49:47 +00004561/*
4562** Return the number of references to the specified page.
4563*/
4564int sqlite3PagerPageRefcount(DbPage *pPage){
4565 return sqlite3PcachePageRefcount(pPage);
4566}
4567
drh0f7eb612006-08-08 13:51:43 +00004568#ifdef SQLITE_TEST
4569/*
drhd9b02572001-04-15 00:37:09 +00004570** This routine is used for testing and analysis only.
4571*/
danielk19773b8a05f2007-03-19 17:44:26 +00004572int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004573 static int a[11];
danielk19778c0a7912008-08-20 14:49:23 +00004574 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
4575 a[1] = sqlite3PcachePagecount(pPager->pPCache);
4576 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
danielk1977d92db532008-11-17 04:56:24 +00004577 a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
drhd9b02572001-04-15 00:37:09 +00004578 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004579 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004580 a[6] = pPager->nHit;
4581 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004582 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004583 a[9] = pPager->nRead;
4584 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004585 return a;
4586}
danielk197717b90b52008-06-06 11:11:25 +00004587int sqlite3PagerIsMemdb(Pager *pPager){
4588 return MEMDB;
4589}
drh0f7eb612006-08-08 13:51:43 +00004590#endif
drhdd793422001-06-28 01:54:48 +00004591
drhfa86c412002-02-02 15:01:15 +00004592/*
danielk1977fd7f0452008-12-17 17:30:26 +00004593** Ensure that there are at least nSavepoint savepoints open.
drhfa86c412002-02-02 15:01:15 +00004594*/
danielk1977fd7f0452008-12-17 17:30:26 +00004595int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
4596 int rc = SQLITE_OK;
4597
danielk197712dd5492008-12-18 15:45:07 +00004598 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
danielk1977fd7f0452008-12-17 17:30:26 +00004599 int ii;
drh49b9d332009-01-02 18:10:42 +00004600 PagerSavepoint *aNew;
danielk1977fd7f0452008-12-17 17:30:26 +00004601
drhc0731c92009-01-11 17:00:02 +00004602 /* Either there is no active journal or the sub-journal is open or
4603 ** the journal is always stored in memory */
danielk1977443c0592009-01-16 15:21:05 +00004604 assert( pPager->nSavepoint==0 || isOpen(pPager->sjfd) ||
drhc0731c92009-01-11 17:00:02 +00004605 pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
danielk1977fd7f0452008-12-17 17:30:26 +00004606
4607 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
4608 ** if the allocation fails. Otherwise, zero the new portion in case a
4609 ** malloc failure occurs while populating it in the for(...) loop below.
4610 */
drh49b9d332009-01-02 18:10:42 +00004611 aNew = (PagerSavepoint *)sqlite3Realloc(
danielk1977fd7f0452008-12-17 17:30:26 +00004612 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
4613 );
4614 if( !aNew ){
4615 return SQLITE_NOMEM;
4616 }
4617 memset(&aNew[pPager->nSavepoint], 0,
4618 (nSavepoint - pPager->nSavepoint) * sizeof(PagerSavepoint)
4619 );
4620 pPager->aSavepoint = aNew;
4621 ii = pPager->nSavepoint;
4622 pPager->nSavepoint = nSavepoint;
4623
4624 /* Populate the PagerSavepoint structures just allocated. */
4625 for(/* no-op */; ii<nSavepoint; ii++){
danielk197712dd5492008-12-18 15:45:07 +00004626 assert( pPager->dbSizeValid );
danielk1977fd7f0452008-12-17 17:30:26 +00004627 aNew[ii].nOrig = pPager->dbSize;
danielk1977443c0592009-01-16 15:21:05 +00004628 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
danielk197767ddef62008-12-23 19:15:56 +00004629 aNew[ii].iOffset = pPager->journalOff;
4630 }else{
4631 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
4632 }
danielk1977443c0592009-01-16 15:21:05 +00004633 aNew[ii].iSubRec = pPager->nSubRec;
danielk1977fd7f0452008-12-17 17:30:26 +00004634 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
4635 if( !aNew[ii].pInSavepoint ){
4636 return SQLITE_NOMEM;
danielk1977b3175382008-10-17 18:51:52 +00004637 }
drh334b2992007-09-06 23:28:23 +00004638 }
danielk1977fd7f0452008-12-17 17:30:26 +00004639
4640 /* Open the sub-journal, if it is not already opened. */
4641 rc = openSubJournal(pPager);
drh0f892532002-05-30 12:27:03 +00004642 }
danielk1977fd7f0452008-12-17 17:30:26 +00004643
drh86f8c192007-08-22 00:39:19 +00004644 return rc;
4645}
drhfa86c412002-02-02 15:01:15 +00004646
4647/*
danielk1977fd7f0452008-12-17 17:30:26 +00004648** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
4649** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
4650** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
4651** that have occured since savepoint iSavepoint was created.
4652**
4653** In either case, all savepoints with an index greater than iSavepoint
4654** are destroyed.
4655**
4656** If there are less than (iSavepoint+1) active savepoints when this
4657** function is called it is a no-op.
4658*/
4659int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
4660 int rc = SQLITE_OK;
4661
4662 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
4663
4664 if( iSavepoint<pPager->nSavepoint ){
4665 int ii;
4666 int nNew = iSavepoint + (op==SAVEPOINT_ROLLBACK);
4667 for(ii=nNew; ii<pPager->nSavepoint; ii++){
4668 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
4669 }
4670 pPager->nSavepoint = nNew;
4671
danielk1977443c0592009-01-16 15:21:05 +00004672 if( op==SAVEPOINT_ROLLBACK && isOpen(pPager->jfd) ){
danielk1977f3107512008-12-22 10:58:46 +00004673 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
danielk1977fd7f0452008-12-17 17:30:26 +00004674 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
4675 assert(rc!=SQLITE_DONE);
4676 }
danielk1977f3107512008-12-22 10:58:46 +00004677
danielk1977fd7f0452008-12-17 17:30:26 +00004678 /* If this is a release of the outermost savepoint, truncate
4679 ** the sub-journal. */
danielk1977443c0592009-01-16 15:21:05 +00004680 if( nNew==0 && op==SAVEPOINT_RELEASE && isOpen(pPager->sjfd) ){
danielk1977fd7f0452008-12-17 17:30:26 +00004681 assert( rc==SQLITE_OK );
4682 rc = sqlite3OsTruncate(pPager->sjfd, 0);
danielk1977443c0592009-01-16 15:21:05 +00004683 pPager->nSubRec = 0;
danielk1977b3175382008-10-17 18:51:52 +00004684 }
drh663fc632002-02-02 18:49:19 +00004685 }
drhfa86c412002-02-02 15:01:15 +00004686 return rc;
4687}
4688
drh73509ee2003-04-06 20:44:45 +00004689/*
4690** Return the full pathname of the database file.
4691*/
danielk19773b8a05f2007-03-19 17:44:26 +00004692const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004693 return pPager->zFilename;
4694}
4695
drhb20ea9d2004-02-09 01:20:36 +00004696/*
drhd0679ed2007-08-28 22:24:34 +00004697** Return the VFS structure for the pager.
4698*/
4699const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4700 return pPager->pVfs;
4701}
4702
4703/*
drhcc6bb3e2007-08-31 16:11:35 +00004704** Return the file handle for the database file associated
4705** with the pager. This might return NULL if the file has
4706** not yet been opened.
4707*/
4708sqlite3_file *sqlite3PagerFile(Pager *pPager){
4709 return pPager->fd;
4710}
4711
4712/*
danielk19775865e3d2004-06-14 06:03:57 +00004713** Return the full pathname of the journal file.
4714*/
danielk19773b8a05f2007-03-19 17:44:26 +00004715const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004716 return pPager->zJournal;
4717}
4718
4719/*
drh2c8997b2005-08-27 16:36:48 +00004720** Return true if fsync() calls are disabled for this pager. Return FALSE
4721** if fsync()s are executed normally.
4722*/
danielk19773b8a05f2007-03-19 17:44:26 +00004723int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004724 return pPager->noSync;
4725}
4726
drh7c4ac0c2007-04-05 11:25:58 +00004727#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004728/*
drhb20ea9d2004-02-09 01:20:36 +00004729** Set the codec for this pager
4730*/
danielk19773b8a05f2007-03-19 17:44:26 +00004731void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004732 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004733 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004734 void *pCodecArg
4735){
4736 pPager->xCodec = xCodec;
4737 pPager->pCodecArg = pCodecArg;
4738}
drh7c4ac0c2007-04-05 11:25:58 +00004739#endif
drhb20ea9d2004-02-09 01:20:36 +00004740
danielk1977687566d2004-11-02 12:56:41 +00004741#ifndef SQLITE_OMIT_AUTOVACUUM
4742/*
danielk197787c29a92008-01-18 11:33:16 +00004743** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004744**
drh5e385312007-06-16 04:42:12 +00004745** There must be no references to the page previously located at
4746** pgno (which we call pPgOld) though that page is allowed to be
drhb3df2e12008-09-17 20:06:26 +00004747** in cache. If the page previously located at pgno is not already
drh5e385312007-06-16 04:42:12 +00004748** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004749**
drh5e385312007-06-16 04:42:12 +00004750** References to the page pPg remain valid. Updating any
4751** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004752** allocated along with the page) is the responsibility of the caller.
4753**
danielk19775fd057a2005-03-09 13:09:43 +00004754** A transaction must be active when this routine is called. It used to be
4755** required that a statement transaction was not active, but this restriction
4756** has been removed (CREATE INDEX needs to move a page when a statement
4757** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00004758**
4759** If the fourth argument, isCommit, is non-zero, then this page is being
4760** moved as part of a database reorganization just before the transaction
4761** is being committed. In this case, it is guaranteed that the database page
4762** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00004763*/
danielk19774c999992008-07-16 18:17:55 +00004764int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00004765 PgHdr *pPgOld; /* The page being overwritten. */
danielk197794daf7f2004-11-08 09:26:09 +00004766 Pgno needSyncPgno = 0;
danielk19771fab7b62009-01-07 10:35:18 +00004767 int rc;
danielk1977687566d2004-11-02 12:56:41 +00004768
danielk1977687566d2004-11-02 12:56:41 +00004769 assert( pPg->nRef>0 );
4770
danielk19771fab7b62009-01-07 10:35:18 +00004771 /* If the page being moved is dirty and has not been saved by the latest
4772 ** savepoint, then save the current contents of the page into the
4773 ** sub-journal now. This is required to handle the following scenario:
4774 **
4775 ** BEGIN;
4776 ** <journal page X, then modify it in memory>
4777 ** SAVEPOINT one;
4778 ** <Move page X to location Y>
4779 ** ROLLBACK TO one;
4780 **
4781 ** If page X were not written to the sub-journal here, it would not
4782 ** be possible to restore its contents when the "ROLLBACK TO one"
4783 ** statement were processed.
4784 */
4785 if( pPg->flags&PGHDR_DIRTY
4786 && subjRequiresPage(pPg)
4787 && SQLITE_OK!=(rc = subjournalPage(pPg))
4788 ){
4789 return rc;
4790 }
4791
drh30d53702009-01-06 15:58:57 +00004792 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
4793 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
drhb0603412007-02-28 04:47:26 +00004794 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004795
danielk19774c999992008-07-16 18:17:55 +00004796 /* If the journal needs to be sync()ed before page pPg->pgno can
4797 ** be written to, store pPg->pgno in local variable needSyncPgno.
4798 **
4799 ** If the isCommit flag is set, there is no need to remember that
4800 ** the journal needs to be sync()ed before database page pPg->pgno
4801 ** can be written to. The caller has already promised not to write to it.
4802 */
danielk19778c0a7912008-08-20 14:49:23 +00004803 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00004804 needSyncPgno = pPg->pgno;
danielk19773460d192008-12-27 15:23:13 +00004805 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
danielk19778c0a7912008-08-20 14:49:23 +00004806 assert( pPg->flags&PGHDR_DIRTY );
danielk1977ae825582004-11-23 09:06:55 +00004807 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004808 }
4809
danielk1977ef73ee92004-11-06 12:26:07 +00004810 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00004811 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00004812 ** page pgno before the 'move' operation, it needs to be retained
4813 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004814 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00004815 pPg->flags &= ~PGHDR_NEED_SYNC;
danielk1977687566d2004-11-02 12:56:41 +00004816 pPgOld = pager_lookup(pPager, pgno);
danielk19778c0a7912008-08-20 14:49:23 +00004817 assert( !pPgOld || pPgOld->nRef==1 );
danielk1977687566d2004-11-02 12:56:41 +00004818 if( pPgOld ){
danielk19778c0a7912008-08-20 14:49:23 +00004819 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
drh5e385312007-06-16 04:42:12 +00004820 }
danielk1977687566d2004-11-02 12:56:41 +00004821
danielk19778c0a7912008-08-20 14:49:23 +00004822 sqlite3PcacheMove(pPg, pgno);
4823 if( pPgOld ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00004824 sqlite3PcacheDrop(pPgOld);
danielk1977f5fdda82004-11-03 08:44:05 +00004825 }
danielk1977f5fdda82004-11-03 08:44:05 +00004826
drhc047b9f2008-11-21 03:23:43 +00004827 sqlite3PcacheMakeDirty(pPg);
drhd138c012008-05-13 00:58:18 +00004828 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00004829
danielk197794daf7f2004-11-08 09:26:09 +00004830 if( needSyncPgno ){
4831 /* If needSyncPgno is non-zero, then the journal file needs to be
4832 ** sync()ed before any data is written to database file page needSyncPgno.
4833 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00004834 ** "is journaled" bitvec flag has been set. This needs to be remedied by
4835 ** loading the page into the pager-cache and setting the PgHdr.needSync
4836 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00004837 **
danielk1977a98d7b42008-01-18 13:42:54 +00004838 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00004839 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00004840 ** array. Otherwise, if the page is loaded and written again in
4841 ** this transaction, it may be written to the database file before
4842 ** it is synced into the journal file. This way, it may end up in
4843 ** the journal file twice, but that is not a problem.
4844 **
danielk19773b8a05f2007-03-19 17:44:26 +00004845 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004846 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004847 */
danielk19773b8a05f2007-03-19 17:44:26 +00004848 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004849 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004850 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00004851 if( rc!=SQLITE_OK ){
danielk19773460d192008-12-27 15:23:13 +00004852 if( pPager->pInJournal && needSyncPgno<=pPager->dbOrigSize ){
drhf5e7bb52008-02-18 14:47:33 +00004853 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00004854 }
danielk197787c29a92008-01-18 11:33:16 +00004855 return rc;
4856 }
danielk1977ae825582004-11-23 09:06:55 +00004857 pPager->needSync = 1;
drhb3df2e12008-09-17 20:06:26 +00004858 assert( pPager->noSync==0 && !MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00004859 pPgHdr->flags |= PGHDR_NEED_SYNC;
drhc047b9f2008-11-21 03:23:43 +00004860 sqlite3PcacheMakeDirty(pPgHdr);
danielk19773b8a05f2007-03-19 17:44:26 +00004861 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004862 }
4863
danielk1977687566d2004-11-02 12:56:41 +00004864 return SQLITE_OK;
4865}
4866#endif
4867
danielk19773b8a05f2007-03-19 17:44:26 +00004868/*
4869** Return a pointer to the data for the specified page.
4870*/
4871void *sqlite3PagerGetData(DbPage *pPg){
danielk197771d5d2c2008-09-29 11:49:47 +00004872 assert( pPg->nRef>0 || pPg->pPager->memDb );
danielk19778c0a7912008-08-20 14:49:23 +00004873 return pPg->pData;
danielk19773b8a05f2007-03-19 17:44:26 +00004874}
4875
4876/*
4877** Return a pointer to the Pager.nExtra bytes of "extra" space
4878** allocated along with the specified page.
4879*/
4880void *sqlite3PagerGetExtra(DbPage *pPg){
4881 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00004882 return (pPager?pPg->pExtra:0);
danielk19773b8a05f2007-03-19 17:44:26 +00004883}
4884
danielk197741483462007-03-24 16:45:04 +00004885/*
4886** Get/set the locking-mode for this pager. Parameter eMode must be one
4887** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4888** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4889** the locking-mode is set to the value specified.
4890**
4891** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4892** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4893** locking-mode.
4894*/
4895int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004896 assert( eMode==PAGER_LOCKINGMODE_QUERY
4897 || eMode==PAGER_LOCKINGMODE_NORMAL
4898 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4899 assert( PAGER_LOCKINGMODE_QUERY<0 );
4900 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4901 if( eMode>=0 && !pPager->tempFile ){
drh1bd10f82008-12-10 21:19:56 +00004902 pPager->exclusiveMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +00004903 }
4904 return (int)pPager->exclusiveMode;
4905}
4906
drh3b020132008-04-17 17:02:01 +00004907/*
drh04335882008-09-26 21:08:08 +00004908** Get/set the journal-mode for this pager. Parameter eMode must be one of:
drh3b020132008-04-17 17:02:01 +00004909**
drh04335882008-09-26 21:08:08 +00004910** PAGER_JOURNALMODE_QUERY
4911** PAGER_JOURNALMODE_DELETE
4912** PAGER_JOURNALMODE_TRUNCATE
4913** PAGER_JOURNALMODE_PERSIST
4914** PAGER_JOURNALMODE_OFF
4915**
4916** If the parameter is not _QUERY, then the journal-mode is set to the
4917** value specified.
4918**
danielk1977443c0592009-01-16 15:21:05 +00004919** The returned indicate the current (possibly updated) journal-mode.
drh3b020132008-04-17 17:02:01 +00004920*/
4921int sqlite3PagerJournalMode(Pager *pPager, int eMode){
danielk1977b3175382008-10-17 18:51:52 +00004922 if( !MEMDB ){
4923 assert( eMode==PAGER_JOURNALMODE_QUERY
4924 || eMode==PAGER_JOURNALMODE_DELETE
4925 || eMode==PAGER_JOURNALMODE_TRUNCATE
4926 || eMode==PAGER_JOURNALMODE_PERSIST
4927 || eMode==PAGER_JOURNALMODE_OFF
4928 || eMode==PAGER_JOURNALMODE_MEMORY );
4929 assert( PAGER_JOURNALMODE_QUERY<0 );
4930 if( eMode>=0 ){
drh1bd10f82008-12-10 21:19:56 +00004931 pPager->journalMode = (u8)eMode;
danielk1977b3175382008-10-17 18:51:52 +00004932 }else{
4933 assert( eMode==PAGER_JOURNALMODE_QUERY );
4934 }
drh3b020132008-04-17 17:02:01 +00004935 }
drhfdc40e92008-04-17 20:59:37 +00004936 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00004937}
4938
danielk1977b53e4962008-06-04 06:45:59 +00004939/*
4940** Get/set the size-limit used for persistent journal files.
4941*/
4942i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
4943 if( iLimit>=-1 ){
4944 pPager->journalSizeLimit = iLimit;
4945 }
4946 return pPager->journalSizeLimit;
4947}
4948
drh2e66f0b2005-04-28 17:18:48 +00004949#endif /* SQLITE_OMIT_DISKIO */