blob: 0df2b594fca7e1c013caf25fc1df89977027fe44 [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**
danielk197745d68822009-01-16 16:23:38 +000021** @(#) $Id: pager.c,v 1.553 2009/01/16 16:23:38 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**
danielk197745d68822009-01-16 16:23:38 +0000152** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
153** or SQLITE_FULL. Once one of the first three errors occurs, it persists
154** and is returned as the result of every major pager API call. The
155** SQLITE_FULL return code is slightly different. It persists only until the
156** next successful rollback is performed on the pager cache. Also,
157** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
158** APIs, they may still be used successfully.
danielk19773460d192008-12-27 15:23:13 +0000159**
danielk197745d68822009-01-16 16:23:38 +0000160** Managing the size of the database file in pages is a little complicated.
161** The variable Pager.dbSize contains the number of pages that the database
162** image currently contains. As the database image grows or shrinks this
163** variable is updated. The variable Pager.dbFileSize contains the number
164** of pages in the database file. This may be different from Pager.dbSize
165** if some pages have been appended to the database image but not yet written
166** out from the cache to the actual file on disk. Or if the image has been
167** truncated by an incremental-vacuum operation. The Pager.dbOrigSize variable
168** contains the number of pages in the database image when the current
169** transaction was opened. The contents of all three of these variables is
170** only guaranteed to be correct if the boolean Pager.dbSizeValid is true.
drhed7c8552001-04-11 14:29:21 +0000171*/
172struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000173 sqlite3_vfs *pVfs; /* OS functions to use for IO */
danielk197745d68822009-01-16 16:23:38 +0000174 u8 journalOpen; /* True if journal file descriptors is valid */
175 u8 journalStarted; /* True if header of journal is synced */
drh34e79ce2004-02-08 06:05:46 +0000176 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000177 u8 noReadlock; /* Do not bother to obtain readlocks */
drh603240c2002-03-05 01:11:12 +0000178 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000179 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000180 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
danielk197745d68822009-01-16 16:23:38 +0000181 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000182 u8 tempFile; /* zFilename is a temporary file */
183 u8 readOnly; /* True for a read-only database */
danielk1977443c0592009-01-16 15:21:05 +0000184 u8 needSync; /* True if an fsync() is needed on the journal */
danielk197745d68822009-01-16 16:23:38 +0000185 u8 dirtyCache; /* True if cached pages have changed */
186 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000187 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000188 u8 doNotSync; /* Boolean. While true, do not spill the cache */
danielk197745d68822009-01-16 16:23:38 +0000189 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
190 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
191 u8 dbModified; /* True if there are any changes to the Db */
192 u8 changeCountDone; /* Set after incrementing the change-counter */
danielk1977d92db532008-11-17 04:56:24 +0000193 u8 dbSizeValid; /* Set when dbSize is correct */
danielk19773460d192008-12-27 15:23:13 +0000194 Pgno dbSize; /* Number of pages in the database */
195 Pgno dbOrigSize; /* dbSize before the current transaction */
196 Pgno dbFileSize; /* Number of pages in the database file */
danielk1977443c0592009-01-16 15:21:05 +0000197 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
danielk197745d68822009-01-16 16:23:38 +0000198 int errCode; /* One of several kinds of errors */
199 int nRec; /* Number of pages written to the journal */
200 u32 cksumInit; /* Quasi-random value added to every checksum */
201 int stmtNRec; /* Number of records in stmt subjournal */
202 int nExtra; /* Add this many bytes to each in-memory page */
danielk1977443c0592009-01-16 15:21:05 +0000203 int pageSize; /* Number of bytes in a page */
danielk197745d68822009-01-16 16:23:38 +0000204 int nPage; /* Total number of in-memory pages */
205 int mxPage; /* Maximum number of pages to hold in cache */
danielk1977443c0592009-01-16 15:21:05 +0000206 Pgno mxPgno; /* Maximum allowed size of the database */
danielk197745d68822009-01-16 16:23:38 +0000207 Bitvec *pInJournal; /* One bit for each page in the database file */
208 Bitvec *pAlwaysRollback; /* One bit for each page marked always-rollback */
drhfcd35c72005-05-21 02:48:08 +0000209 char *zFilename; /* Name of the database file */
210 char *zJournal; /* Name of the journal file */
danielk197745d68822009-01-16 16:23:38 +0000211 char *zDirectory; /* Directory hold database and journal files */
212 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
213 sqlite3_file *sjfd; /* File descriptor for the sub-journal*/
danielk19771ceedd32008-11-19 10:22:33 +0000214 int (*xBusyHandler)(void*); /* Function to call when busy */
215 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
danielk197745d68822009-01-16 16:23:38 +0000216 i64 journalOff; /* Current byte offset in the journal file */
217 i64 journalHdr; /* Byte offset to previous journal header */
218 u32 sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000219#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000220 int nHit, nMiss; /* Cache hits and missing */
221 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000222#endif
danielk1977eaa06f62008-09-18 17:34:44 +0000223 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000224#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000225 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000226 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000227#endif
danielk19778186df82007-03-06 13:45:59 +0000228 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
danielk197745d68822009-01-16 16:23:38 +0000229 char dbFileVers[16]; /* Changes whenever database file changes */
danielk1977b53e4962008-06-04 06:45:59 +0000230 i64 journalSizeLimit; /* Size limit for persistent journal files */
danielk19778c0a7912008-08-20 14:49:23 +0000231 PCache *pPCache; /* Pointer to page cache object */
danielk197745d68822009-01-16 16:23:38 +0000232 PagerSavepoint *aSavepoint; /* Array of active savepoints */
233 int nSavepoint; /* Number of elements in aSavepoint[] */
drhd9b02572001-04-15 00:37:09 +0000234};
235
236/*
drh538f5702007-04-13 02:14:30 +0000237** The following global variables hold counters used for
238** testing purposes only. These variables do not exist in
239** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000240*/
241#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000242int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
243int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
244int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
drh538f5702007-04-13 02:14:30 +0000245# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000246#else
drh538f5702007-04-13 02:14:30 +0000247# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000248#endif
249
drh538f5702007-04-13 02:14:30 +0000250
251
drhfcd35c72005-05-21 02:48:08 +0000252/*
drh5e00f6c2001-09-13 13:46:56 +0000253** Journal files begin with the following magic string. The data
254** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000255**
drhae2b40c2004-06-09 19:03:54 +0000256** Since version 2.8.0, the journal format contains additional sanity
drh30d53702009-01-06 15:58:57 +0000257** checking information. If the power fails while the journal is being
drhae2b40c2004-06-09 19:03:54 +0000258** written, semi-random garbage data might appear in the journal
259** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000260** to roll the journal back, the database could be corrupted. The additional
261** sanity checking data is an attempt to discover the garbage in the
262** journal and ignore it.
263**
drhae2b40c2004-06-09 19:03:54 +0000264** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000265** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000266** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000267** This cksum is initialized to a 32-bit random value that appears in the
268** journal file right after the header. The random initializer is important,
269** because garbage data that appears at the end of a journal is likely
270** data that was once in other files that have now been deleted. If the
271** garbage data came from an obsolete journal file, the checksums might
272** be correct. But by initializing the checksum to random value which
273** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000274*/
drhae2b40c2004-06-09 19:03:54 +0000275static const unsigned char aJournalMagic[] = {
276 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000277};
278
279/*
danielk197745d68822009-01-16 16:23:38 +0000280** The size of the header and of each page in the journal is determined
281** by the following macros.
drh968af522003-02-11 14:55:40 +0000282*/
drhae2b40c2004-06-09 19:03:54 +0000283#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000284
danielk197776572402004-06-25 02:38:54 +0000285/*
danielk197745d68822009-01-16 16:23:38 +0000286** The journal header size for this pager. In the future, this could be
287** set to some value read from the disk controller. The important
288** characteristic is that it is the same size as a disk sector.
danielk197776572402004-06-25 02:38:54 +0000289*/
290#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
291
drhb7f91642004-10-31 02:22:47 +0000292/*
293** The macro MEMDB is true if we are dealing with an in-memory database.
294** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
295** the value of MEMDB will be a constant and the compiler will optimize
296** out code that would never execute.
297*/
298#ifdef SQLITE_OMIT_MEMORYDB
299# define MEMDB 0
300#else
301# define MEMDB pPager->memDb
302#endif
303
304/*
danielk197745d68822009-01-16 16:23:38 +0000305** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
306** reserved for working around a windows/posix incompatibility). It is
307** used in the journal to signify that the remainder of the journal file
308** is devoted to storing a master journal name - there are no more pages to
309** roll back. See comments for function writeMasterJournal() for details.
310*/
311/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
312#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
313
314/*
danielk197726836652005-01-17 01:33:13 +0000315** The maximum legal page number is (2^31 - 1).
316*/
317#define PAGER_MAX_PGNO 2147483647
318
319/*
danielk19773460d192008-12-27 15:23:13 +0000320** Return true if it is necessary to write page *pPg into the sub-journal.
321** A page needs to be written into the sub-journal if there exists one
322** or more open savepoints for which:
danielk1977fd7f0452008-12-17 17:30:26 +0000323**
danielk19773460d192008-12-27 15:23:13 +0000324** * The page-number is less than or equal to PagerSavepoint.nOrig, and
325** * The bit corresponding to the page-number is not set in
326** PagerSavepoint.pInSavepoint.
danielk1977f35843b2007-04-07 15:03:17 +0000327*/
danielk19773460d192008-12-27 15:23:13 +0000328static int subjRequiresPage(PgHdr *pPg){
329 Pgno pgno = pPg->pgno;
danielk1977f35843b2007-04-07 15:03:17 +0000330 Pager *pPager = pPg->pPager;
danielk19773460d192008-12-27 15:23:13 +0000331 int i;
332 for(i=0; i<pPager->nSavepoint; i++){
333 PagerSavepoint *p = &pPager->aSavepoint[i];
334 if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
335 return 1;
336 }
danielk1977fd7f0452008-12-17 17:30:26 +0000337 }
danielk19773460d192008-12-27 15:23:13 +0000338 return 0;
danielk1977f35843b2007-04-07 15:03:17 +0000339}
drh8ca0c722006-05-07 17:49:38 +0000340
danielk19773460d192008-12-27 15:23:13 +0000341/*
342** Return true if the page is already in the journal file.
343*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000344static int pageInJournal(PgHdr *pPg){
345 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
346}
347
drh8ca0c722006-05-07 17:49:38 +0000348/*
drh34e79ce2004-02-08 06:05:46 +0000349** Read a 32-bit integer from the given file descriptor. Store the integer
350** that is read in *pRes. Return SQLITE_OK if everything worked, or an
351** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000352**
353** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000354*/
danielk197762079062007-08-15 17:08:46 +0000355static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000356 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000357 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000358 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000359 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000360 }
drh94f33312002-08-12 12:29:56 +0000361 return rc;
362}
363
364/*
drh97b57482006-01-10 20:32:32 +0000365** Write a 32-bit integer into a string buffer in big-endian byte order.
366*/
drha3152892007-05-05 11:48:52 +0000367#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000368
369/*
drh34e79ce2004-02-08 06:05:46 +0000370** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
371** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000372*/
danielk197762079062007-08-15 17:08:46 +0000373static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000374 char ac[4];
drh97b57482006-01-10 20:32:32 +0000375 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000376 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000377}
378
drh2554f8b2003-01-22 01:26:44 +0000379/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000380** If file pFd is open, call sqlite3OsUnlock() on it.
381*/
382static int osUnlock(sqlite3_file *pFd, int eLock){
danielk197745d68822009-01-16 16:23:38 +0000383 if( !pFd->pMethods ){
danielk19777a2b1ee2007-08-21 14:27:01 +0000384 return SQLITE_OK;
385 }
386 return sqlite3OsUnlock(pFd, eLock);
387}
388
389/*
danielk1977c7b60172007-08-22 11:22:03 +0000390** This function determines whether or not the atomic-write optimization
391** can be used with this pager. The optimization can be used if:
392**
393** (a) the value returned by OsDeviceCharacteristics() indicates that
394** a database page may be written atomically, and
395** (b) the value returned by OsSectorSize() is less than or equal
396** to the page size.
397**
398** If the optimization cannot be used, 0 is returned. If it can be used,
399** then the value returned is the size of the journal file when it
400** contains rollback data for exactly one page.
401*/
402#ifdef SQLITE_ENABLE_ATOMIC_WRITE
403static int jrnlBufferSize(Pager *pPager){
danielk197745d68822009-01-16 16:23:38 +0000404 int dc; /* Device characteristics */
405 int nSector; /* Sector size */
406 int szPage; /* Page size */
407 sqlite3_file *fd = pPager->fd;
danielk1977c7b60172007-08-22 11:22:03 +0000408
danielk197745d68822009-01-16 16:23:38 +0000409 if( fd->pMethods ){
410 dc = sqlite3OsDeviceCharacteristics(fd);
danielk19777cbd5892009-01-10 16:15:09 +0000411 nSector = pPager->sectorSize;
drhfacf0302008-06-17 15:12:00 +0000412 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000413 }
414
danielk197745d68822009-01-16 16:23:38 +0000415 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
416 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
417
418 if( !fd->pMethods ||
419 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
420 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
421 }
422 return 0;
danielk1977c7b60172007-08-22 11:22:03 +0000423}
424#endif
425
426/*
danielk197745d68822009-01-16 16:23:38 +0000427** This function should be called when an error occurs within the pager
428** code. The first argument is a pointer to the pager structure, the
429** second the error-code about to be returned by a pager API function.
430** The value returned is a copy of the second argument to this function.
danielk1977a96a7102006-01-16 12:46:41 +0000431**
drh4f0ee682007-03-30 20:43:40 +0000432** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000433** the error becomes persistent. Until the persisten error is cleared,
434** subsequent API calls on this Pager will immediately return the same
435** error code.
436**
437** A persistent error indicates that the contents of the pager-cache
438** cannot be trusted. This state can be cleared by completely discarding
439** the contents of the pager-cache. If a transaction was active when
440** the persistent error occured, then the rollback journal may need
danielk197745d68822009-01-16 16:23:38 +0000441** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000442*/
danielk1977ae72d982007-10-03 08:46:44 +0000443static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000444static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000445 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000446 assert(
447 pPager->errCode==SQLITE_FULL ||
448 pPager->errCode==SQLITE_OK ||
449 (pPager->errCode & 0xff)==SQLITE_IOERR
450 );
danielk1977979f38e2007-03-27 16:19:51 +0000451 if(
drh4ac285a2006-09-15 07:28:50 +0000452 rc2==SQLITE_FULL ||
453 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000454 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000455 ){
456 pPager->errCode = rc;
danielk19778c0a7912008-08-20 14:49:23 +0000457 if( pPager->state==PAGER_UNLOCK
458 && sqlite3PcacheRefCount(pPager->pPCache)==0
459 ){
danielk1977ae72d982007-10-03 08:46:44 +0000460 /* If the pager is already unlocked, call pager_unlock() now to
461 ** clear the error state and ensure that the pager-cache is
462 ** completely empty.
463 */
464 pager_unlock(pPager);
465 }
danielk1977aef0bf62005-12-30 16:28:01 +0000466 }
467 return rc;
468}
469
drh477731b2007-06-16 03:06:27 +0000470/*
471** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
472** on the cache using a hash function. This is used for testing
473** and debugging only.
474*/
danielk19773c407372005-02-15 02:54:14 +0000475#ifdef SQLITE_CHECK_PAGES
476/*
477** Return a 32-bit hash of the page data for pPage.
478*/
drh477731b2007-06-16 03:06:27 +0000479static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000480 u32 hash = 0;
481 int i;
drh477731b2007-06-16 03:06:27 +0000482 for(i=0; i<nByte; i++){
483 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000484 }
485 return hash;
486}
drh477731b2007-06-16 03:06:27 +0000487static u32 pager_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000488 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
489}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000490static void pager_set_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000491 pPage->pageHash = pager_pagehash(pPage);
drh477731b2007-06-16 03:06:27 +0000492}
danielk19773c407372005-02-15 02:54:14 +0000493
494/*
495** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
496** is defined, and NDEBUG is not defined, an assert() statement checks
497** that the page is either dirty or still matches the calculated page-hash.
498*/
499#define CHECK_PAGE(x) checkPage(x)
500static void checkPage(PgHdr *pPg){
501 Pager *pPager = pPg->pPager;
danielk1977b3175382008-10-17 18:51:52 +0000502 assert( !pPg->pageHash || pPager->errCode
danielk19778c0a7912008-08-20 14:49:23 +0000503 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
danielk19773c407372005-02-15 02:54:14 +0000504}
505
506#else
drh8ffa8172007-06-18 17:25:17 +0000507#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000508#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000509#define CHECK_PAGE(x)
drh41d30272008-08-20 21:47:45 +0000510#endif /* SQLITE_CHECK_PAGES */
danielk19773c407372005-02-15 02:54:14 +0000511
drhed7c8552001-04-11 14:29:21 +0000512/*
danielk197776572402004-06-25 02:38:54 +0000513** When this is called the journal file for pager pPager must be open.
danielk197745d68822009-01-16 16:23:38 +0000514** The master journal file name is read from the end of the file and
515** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000516**
danielk197765839c62007-08-30 08:08:17 +0000517** zMaster must point to a buffer of at least nMaster bytes allocated by
518** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
519** enough space to write the master journal name). If the master journal
520** name in the journal is longer than nMaster bytes (including a
521** nul-terminator), then this is handled as if no master journal name
522** were present in the journal.
523**
danielk197745d68822009-01-16 16:23:38 +0000524** If no master journal file name is present zMaster[0] is set to 0 and
525** SQLITE_OK returned.
danielk197776572402004-06-25 02:38:54 +0000526*/
danielk1977d92db532008-11-17 04:56:24 +0000527static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
danielk197745d68822009-01-16 16:23:38 +0000528 int rc;
529 u32 len;
530 i64 szJ;
531 u32 cksum;
532 u32 u; /* Unsigned loop counter */
533 unsigned char aMagic[8]; /* A buffer to hold the magic header */
534
danielk197765839c62007-08-30 08:08:17 +0000535 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000536
danielk197745d68822009-01-16 16:23:38 +0000537 rc = sqlite3OsFileSize(pJrnl, &szJ);
538 if( rc!=SQLITE_OK || szJ<16 ) return rc;
539
540 rc = read32bits(pJrnl, szJ-16, &len);
541 if( rc!=SQLITE_OK ) return rc;
542
543 if( len>=nMaster ){
544 return SQLITE_OK;
545 }
546
547 rc = read32bits(pJrnl, szJ-12, &cksum);
548 if( rc!=SQLITE_OK ) return rc;
549
550 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
551 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
552
553 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
554 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000555 return rc;
556 }
danielk197745d68822009-01-16 16:23:38 +0000557 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000558
559 /* See if the checksum matches the master journal name */
shane0b8d2762008-07-22 05:18:00 +0000560 for(u=0; u<len; u++){
561 cksum -= zMaster[u];
danielk197745d68822009-01-16 16:23:38 +0000562 }
danielk19778191bff2004-06-28 04:52:30 +0000563 if( cksum ){
564 /* If the checksum doesn't add up, then one or more of the disk sectors
565 ** containing the master journal filename is corrupted. This means
566 ** definitely roll back, so just return SQLITE_OK and report a (nul)
567 ** master-journal filename.
568 */
danielk197745d68822009-01-16 16:23:38 +0000569 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000570 }
danielk197776572402004-06-25 02:38:54 +0000571
572 return SQLITE_OK;
573}
574
575/*
danielk197745d68822009-01-16 16:23:38 +0000576** Seek the journal file descriptor to the next sector boundary where a
577** journal header may be read or written. Pager.journalOff is updated with
578** the new seek offset.
danielk197776572402004-06-25 02:38:54 +0000579**
580** i.e for a sector size of 512:
581**
danielk197745d68822009-01-16 16:23:38 +0000582** Input Offset Output Offset
583** ---------------------------------------
584** 0 0
585** 512 512
586** 100 512
587** 2000 2048
danielk197776572402004-06-25 02:38:54 +0000588**
589*/
danielk1977112f7522009-01-08 17:50:45 +0000590static i64 journalHdrOffset(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000591 i64 offset = 0;
592 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000593 if( c ){
594 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
595 }
596 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
597 assert( offset>=c );
598 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
danielk1977112f7522009-01-08 17:50:45 +0000599 return offset;
600}
danielk197745d68822009-01-16 16:23:38 +0000601static void seekJournalHdr(Pager *pPager){
602 pPager->journalOff = journalHdrOffset(pPager);
603}
danielk197776572402004-06-25 02:38:54 +0000604
605/*
danielk197745d68822009-01-16 16:23:38 +0000606** Write zeros over the header of the journal file. This has the
607** effect of invalidating the journal file and committing the
608** transaction.
drhf3a87622008-04-17 14:16:42 +0000609*/
danielk1977df2566a2008-05-07 19:11:03 +0000610static int zeroJournalHdr(Pager *pPager, int doTruncate){
danielk197745d68822009-01-16 16:23:38 +0000611 int rc = SQLITE_OK;
612 static const char zeroHdr[28] = {0};
613
danielk1977df2566a2008-05-07 19:11:03 +0000614 if( pPager->journalOff ){
danielk197745d68822009-01-16 16:23:38 +0000615 i64 iLimit = pPager->journalSizeLimit;
danielk1977b53e4962008-06-04 06:45:59 +0000616
danielk1977df2566a2008-05-07 19:11:03 +0000617 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000618 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000619 rc = sqlite3OsTruncate(pPager->jfd, 0);
620 }else{
621 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
622 }
danielk197781620542008-06-07 05:19:37 +0000623 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000624 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
625 }
danielk1977b53e4962008-06-04 06:45:59 +0000626
627 /* At this point the transaction is committed but the write lock
628 ** is still held on the file. If there is a size limit configured for
629 ** the persistent journal and the journal file currently consumes more
630 ** space than that limit allows for, truncate it now. There is no need
631 ** to sync the file following this operation.
632 */
633 if( rc==SQLITE_OK && iLimit>0 ){
634 i64 sz;
635 rc = sqlite3OsFileSize(pPager->jfd, &sz);
636 if( rc==SQLITE_OK && sz>iLimit ){
637 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
638 }
639 }
drha06ecba2008-04-22 17:15:17 +0000640 }
drhf3a87622008-04-17 14:16:42 +0000641 return rc;
642}
643
644/*
danielk197776572402004-06-25 02:38:54 +0000645** The journal file must be open when this routine is called. A journal
646** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
647** current location.
648**
649** The format for the journal header is as follows:
650** - 8 bytes: Magic identifying journal format.
651** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
652** - 4 bytes: Random number used for page hash.
653** - 4 bytes: Initial database page count.
654** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000655** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000656**
danielk197767c007b2008-03-20 04:45:49 +0000657** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000658*/
659static int writeJournalHdr(Pager *pPager){
danielk197745d68822009-01-16 16:23:38 +0000660 int rc = SQLITE_OK;
661 char *zHeader = pPager->pTmpSpace;
662 u32 nHeader = pPager->pageSize;
663 u32 nWrite;
664 int ii;
danielk1977a664f8e2008-04-22 14:31:48 +0000665
666 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
667 nHeader = JOURNAL_HDR_SZ(pPager);
668 }
danielk197776572402004-06-25 02:38:54 +0000669
danielk197745d68822009-01-16 16:23:38 +0000670 /* If there are active savepoints and any of them were created since the
671 ** most recent journal header was written, update the PagerSavepoint.iHdrOff
672 ** fields now.
danielk1977fd7f0452008-12-17 17:30:26 +0000673 */
674 for(ii=0; ii<pPager->nSavepoint; ii++){
675 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
676 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
677 }
danielk19774099f6e2007-03-19 11:25:20 +0000678 }
679
danielk197745d68822009-01-16 16:23:38 +0000680 seekJournalHdr(pPager);
681 pPager->journalHdr = pPager->journalOff;
682
drh97b57482006-01-10 20:32:32 +0000683 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000684
685 /*
686 ** Write the nRec Field - the number of page records that follow this
687 ** journal header. Normally, zero is written to this value at this time.
688 ** After the records are added to the journal (and the journal synced,
689 ** if in full-sync mode), the zero is overwritten with the true number
690 ** of records (see syncJournal()).
691 **
692 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
693 ** reading the journal this value tells SQLite to assume that the
694 ** rest of the journal file contains valid page records. This assumption
695 ** is dangerous, as if a failure occured whilst writing to the journal
696 ** file it may contain some garbage data. There are two scenarios
697 ** where this risk can be ignored:
698 **
699 ** * When the pager is in no-sync mode. Corruption can follow a
700 ** power failure in this case anyway.
701 **
702 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
703 ** that garbage data is never appended to the journal file.
704 */
danielk197745d68822009-01-16 16:23:38 +0000705 assert(pPager->fd->pMethods||pPager->noSync);
danielk1977b3175382008-10-17 18:51:52 +0000706 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
danielk19774cd2cd52007-08-23 14:48:23 +0000707 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
708 ){
709 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
710 }else{
711 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
712 }
713
drh97b57482006-01-10 20:32:32 +0000714 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +0000715 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +0000716 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
717 /* The initial database size */
danielk19773460d192008-12-27 15:23:13 +0000718 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
drh97b57482006-01-10 20:32:32 +0000719 /* The assumed sector size for this process */
720 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drh08609ce2008-11-29 22:49:23 +0000721
722 /* Initializing the tail of the buffer is not necessary. Everything
723 ** works find if the following memset() is omitted. But initializing
724 ** the memory prevents valgrind from complaining, so we are willing to
725 ** take the performance hit.
726 */
danielk197745d68822009-01-16 16:23:38 +0000727 memset(&zHeader[sizeof(aJournalMagic)+16], 0,
728 nHeader-(sizeof(aJournalMagic)+16));
drh08609ce2008-11-29 22:49:23 +0000729
danielk197745d68822009-01-16 16:23:38 +0000730 if( pPager->journalHdr==0 ){
731 /* The page size */
732 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
733 }
734
danielk1977a664f8e2008-04-22 14:31:48 +0000735 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
736 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
737 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
738 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +0000739 }
danielk1977a664f8e2008-04-22 14:31:48 +0000740
danielk197776572402004-06-25 02:38:54 +0000741 return rc;
742}
743
744/*
745** The journal file must be open when this is called. A journal header file
746** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
drhd6e5e092009-01-07 02:03:35 +0000747** file. The current location in the journal file is given by
danielk197745d68822009-01-16 16:23:38 +0000748** pPager->journalOff. See comments above function writeJournalHdr() for
drhd6e5e092009-01-07 02:03:35 +0000749** a description of the journal header format.
danielk197776572402004-06-25 02:38:54 +0000750**
danielk197745d68822009-01-16 16:23:38 +0000751** If the header is read successfully, *nRec is set to the number of
752** page records following this header and *dbSize is set to the size of the
danielk197776572402004-06-25 02:38:54 +0000753** database before the transaction began, in pages. Also, pPager->cksumInit
754** is set to the value read from the journal header. SQLITE_OK is returned
755** in this case.
756**
757** If the journal header file appears to be corrupted, SQLITE_DONE is
danielk197745d68822009-01-16 16:23:38 +0000758** returned and *nRec and *dbSize are undefined. If JOURNAL_HDR_SZ bytes
danielk197776572402004-06-25 02:38:54 +0000759** cannot be read from the journal file an error code is returned.
760*/
761static int readJournalHdr(
danielk197745d68822009-01-16 16:23:38 +0000762 Pager *pPager,
763 i64 journalSize,
764 u32 *pNRec,
765 u32 *pDbSize
danielk197776572402004-06-25 02:38:54 +0000766){
danielk197745d68822009-01-16 16:23:38 +0000767 int rc;
768 unsigned char aMagic[8]; /* A buffer to hold the magic header */
769 i64 jrnlOff;
770 u32 iPageSize;
771 u32 iSectorSize;
danielk197776572402004-06-25 02:38:54 +0000772
danielk197745d68822009-01-16 16:23:38 +0000773 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000774 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
775 return SQLITE_DONE;
776 }
danielk197745d68822009-01-16 16:23:38 +0000777 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000778
danielk197745d68822009-01-16 16:23:38 +0000779 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
780 if( rc ) return rc;
781 jrnlOff += sizeof(aMagic);
782
danielk197776572402004-06-25 02:38:54 +0000783 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
784 return SQLITE_DONE;
785 }
786
danielk197745d68822009-01-16 16:23:38 +0000787 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
788 if( rc ) return rc;
789
790 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
791 if( rc ) return rc;
792
793 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
794 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +0000795
danielk19777cbd5892009-01-10 16:15:09 +0000796 if( pPager->journalOff==0 ){
danielk197745d68822009-01-16 16:23:38 +0000797 rc = read32bits(pPager->jfd, jrnlOff+16, &iPageSize);
798 if( rc ) return rc;
danielk197767c007b2008-03-20 04:45:49 +0000799
danielk197745d68822009-01-16 16:23:38 +0000800 if( iPageSize<512
801 || iPageSize>SQLITE_MAX_PAGE_SIZE
802 || ((iPageSize-1)&iPageSize)!=0
danielk19777cbd5892009-01-10 16:15:09 +0000803 ){
danielk197745d68822009-01-16 16:23:38 +0000804 /* If the page-size in the journal-header is invalid, then the process
805 ** that wrote the journal-header must have crashed before the header
806 ** was synced. In this case stop reading the journal file here.
danielk1977443c0592009-01-16 15:21:05 +0000807 */
danielk197745d68822009-01-16 16:23:38 +0000808 rc = SQLITE_DONE;
809 }else{
810 u16 pagesize = (u16)iPageSize;
811 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
812 assert( rc!=SQLITE_OK || pagesize==(u16)iPageSize );
danielk1977443c0592009-01-16 15:21:05 +0000813 }
danielk197745d68822009-01-16 16:23:38 +0000814 if( rc ) return rc;
815
danielk19777cbd5892009-01-10 16:15:09 +0000816 /* Update the assumed sector-size to match the value used by
817 ** the process that created this journal. If this journal was
818 ** created by a process other than this one, then this routine
819 ** is being called from within pager_playback(). The local value
820 ** of Pager.sectorSize is restored at the end of that routine.
821 */
danielk197745d68822009-01-16 16:23:38 +0000822 rc = read32bits(pPager->jfd, jrnlOff+12, &iSectorSize);
823 if( rc ) return rc;
824 if( (iSectorSize&(iSectorSize-1))
825 || iSectorSize<512
826 || iSectorSize>MAX_SECTOR_SIZE
827 ){
828 return SQLITE_DONE;
829 }
danielk19777cbd5892009-01-10 16:15:09 +0000830 pPager->sectorSize = iSectorSize;
drh98c58352008-11-07 00:24:53 +0000831 }
danielk197776572402004-06-25 02:38:54 +0000832
833 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197745d68822009-01-16 16:23:38 +0000834 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000835}
836
837
838/*
839** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000840** pPager at the current location. The master journal name must be the last
841** thing written to a journal file. If the pager is in full-sync mode, the
842** journal file descriptor is advanced to the next sector boundary before
843** anything is written. The format is:
844**
danielk197745d68822009-01-16 16:23:38 +0000845** + 4 bytes: PAGER_MJ_PGNO.
846** + N bytes: length of master journal name.
847** + 4 bytes: N
848** + 4 bytes: Master journal name checksum.
849** + 8 bytes: aJournalMagic[].
danielk1977cafadba2004-06-25 11:11:54 +0000850**
851** The master journal page checksum is the sum of the bytes in the master
danielk197745d68822009-01-16 16:23:38 +0000852** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000853**
854** If zMaster is a NULL pointer (occurs for a single database transaction),
855** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000856*/
857static int writeMasterJournal(Pager *pPager, const char *zMaster){
danielk197745d68822009-01-16 16:23:38 +0000858 int rc;
859 int len;
860 int i;
861 i64 jrnlOff;
862 i64 jrnlSize;
863 u32 cksum = 0;
864 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000865
danielk197745d68822009-01-16 16:23:38 +0000866 if( !zMaster || pPager->setMaster ) return SQLITE_OK;
danielk1977b3175382008-10-17 18:51:52 +0000867 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ) return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000868 pPager->setMaster = 1;
869
danielk197745d68822009-01-16 16:23:38 +0000870 len = sqlite3Strlen30(zMaster);
871 for(i=0; i<len; i++){
872 cksum += zMaster[i];
danielk1977cafadba2004-06-25 11:11:54 +0000873 }
danielk197776572402004-06-25 02:38:54 +0000874
875 /* If in full-sync mode, advance to the next disk sector before writing
876 ** the master journal name. This is in case the previous page written to
877 ** the journal has already been synced.
878 */
879 if( pPager->fullSync ){
danielk197745d68822009-01-16 16:23:38 +0000880 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000881 }
danielk197745d68822009-01-16 16:23:38 +0000882 jrnlOff = pPager->journalOff;
883 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000884
danielk197745d68822009-01-16 16:23:38 +0000885 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
886 if( rc!=SQLITE_OK ) return rc;
887 jrnlOff += 4;
888
889 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
890 if( rc!=SQLITE_OK ) return rc;
891 jrnlOff += len;
892
893 put32bits(zBuf, len);
894 put32bits(&zBuf[4], cksum);
895 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
896 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
897 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +0000898 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +0000899
900 /* If the pager is in peristent-journal mode, then the physical
901 ** journal-file may extend past the end of the master-journal name
902 ** and 8 bytes of magic data just written to the file. This is
903 ** dangerous because the code to rollback a hot-journal file
904 ** will not be able to find the master-journal name to determine
905 ** whether or not the journal is hot.
906 **
907 ** Easiest thing to do in this scenario is to truncate the journal
908 ** file to the required size.
909 */
danielk197745d68822009-01-16 16:23:38 +0000910 if( (rc==SQLITE_OK)
911 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
912 && jrnlSize>jrnlOff
danielk1977df2566a2008-05-07 19:11:03 +0000913 ){
danielk197745d68822009-01-16 16:23:38 +0000914 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +0000915 }
danielk197776572402004-06-25 02:38:54 +0000916 return rc;
917}
918
919/*
danielk197745d68822009-01-16 16:23:38 +0000920** Find a page in the hash table given its page number. Return
921** a pointer to the page or NULL if not found.
drhed7c8552001-04-11 14:29:21 +0000922*/
drhd9b02572001-04-15 00:37:09 +0000923static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
danielk197745d68822009-01-16 16:23:38 +0000924 PgHdr *p;
925 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
drhed7c8552001-04-11 14:29:21 +0000926 return p;
927}
928
929/*
danielk197745d68822009-01-16 16:23:38 +0000930** Clear the in-memory cache. This routine
931** sets the state of the pager back to what it was when it was first
932** opened. Any outstanding pages are invalidated and subsequent attempts
933** to access those pages will likely result in a coredump.
drhed7c8552001-04-11 14:29:21 +0000934*/
drhd9b02572001-04-15 00:37:09 +0000935static void pager_reset(Pager *pPager){
danielk197745d68822009-01-16 16:23:38 +0000936 if( pPager->errCode ) return;
937 sqlite3PcacheClear(pPager->pPCache);
danielk1977e277be02007-03-23 18:12:06 +0000938}
939
danielk197734cf35d2008-12-18 18:31:38 +0000940/*
941** Free all structures in the Pager.aSavepoint[] array and set both
942** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
943** if it is open and the pager is not in exclusive mode.
944*/
danielk197745d68822009-01-16 16:23:38 +0000945static void releaseAllSavepoint(Pager *pPager){
946 int ii;
danielk1977fd7f0452008-12-17 17:30:26 +0000947 for(ii=0; ii<pPager->nSavepoint; ii++){
948 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
949 }
950 if( !pPager->exclusiveMode ){
951 sqlite3OsClose(pPager->sjfd);
952 }
953 sqlite3_free(pPager->aSavepoint);
954 pPager->aSavepoint = 0;
955 pPager->nSavepoint = 0;
danielk197745d68822009-01-16 16:23:38 +0000956 pPager->stmtNRec = 0;
danielk1977fd7f0452008-12-17 17:30:26 +0000957}
958
danielk197734cf35d2008-12-18 18:31:38 +0000959/*
danielk197745d68822009-01-16 16:23:38 +0000960** Set the bit number pgno in the PagerSavepoint.pInSavepoint bitvecs of
961** all open savepoints.
danielk197734cf35d2008-12-18 18:31:38 +0000962*/
danielk1977fd7f0452008-12-17 17:30:26 +0000963static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
drh7539b6b2009-01-02 21:39:39 +0000964 int ii; /* Loop counter */
965 int rc = SQLITE_OK; /* Result code */
966
danielk1977fd7f0452008-12-17 17:30:26 +0000967 for(ii=0; ii<pPager->nSavepoint; ii++){
968 PagerSavepoint *p = &pPager->aSavepoint[ii];
969 if( pgno<=p->nOrig ){
drh7539b6b2009-01-02 21:39:39 +0000970 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
971 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
danielk1977fd7f0452008-12-17 17:30:26 +0000972 }
973 }
drh7539b6b2009-01-02 21:39:39 +0000974 return rc;
danielk1977fd7f0452008-12-17 17:30:26 +0000975}
976
danielk1977e277be02007-03-23 18:12:06 +0000977/*
danielk197745d68822009-01-16 16:23:38 +0000978** Unlock the database file.
danielk1977ae72d982007-10-03 08:46:44 +0000979**
980** If the pager is currently in error state, discard the contents of
981** the cache and reset the Pager structure internal state. If there is
982** an open journal-file, then the next time a shared-lock is obtained
983** on the pager file (by this or any other process), it will be
984** treated as a hot-journal and rolled back.
985*/
986static void pager_unlock(Pager *pPager){
987 if( !pPager->exclusiveMode ){
danielk197745d68822009-01-16 16:23:38 +0000988 int rc;
danielk1977ae72d982007-10-03 08:46:44 +0000989
danielk1977b3175382008-10-17 18:51:52 +0000990 /* Always close the journal file when dropping the database lock.
991 ** Otherwise, another connection with journal_mode=delete might
992 ** delete the file out from under us.
993 */
danielk197745d68822009-01-16 16:23:38 +0000994 if( pPager->journalOpen ){
995 sqlite3OsClose(pPager->jfd);
996 pPager->journalOpen = 0;
997 sqlite3BitvecDestroy(pPager->pInJournal);
998 pPager->pInJournal = 0;
999 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
1000 pPager->pAlwaysRollback = 0;
1001 }
danielk1977ae72d982007-10-03 08:46:44 +00001002
danielk19775f2d46b2009-01-13 16:03:44 +00001003 rc = osUnlock(pPager->fd, NO_LOCK);
danielk197745d68822009-01-16 16:23:38 +00001004 if( rc ) pPager->errCode = rc;
1005 pPager->dbSizeValid = 0;
danielk19775f2d46b2009-01-13 16:03:44 +00001006 IOTRACE(("UNLOCK %p\n", pPager))
1007
danielk1977b3175382008-10-17 18:51:52 +00001008 /* If Pager.errCode is set, the contents of the pager cache cannot be
1009 ** trusted. Now that the pager file is unlocked, the contents of the
1010 ** cache can be discarded and the error code safely cleared.
1011 */
1012 if( pPager->errCode ){
danielk197745d68822009-01-16 16:23:38 +00001013 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977b3175382008-10-17 18:51:52 +00001014 pager_reset(pPager);
danielk197745d68822009-01-16 16:23:38 +00001015 releaseAllSavepoint(pPager);
1016 pPager->journalOff = 0;
1017 pPager->journalStarted = 0;
1018 pPager->dbOrigSize = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001019 }
danielk1977b3175382008-10-17 18:51:52 +00001020
danielk1977443c0592009-01-16 15:21:05 +00001021 pPager->state = PAGER_UNLOCK;
danielk197745d68822009-01-16 16:23:38 +00001022 pPager->changeCountDone = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001023 }
1024}
1025
1026/*
1027** Execute a rollback if a transaction is active and unlock the
danielk197745d68822009-01-16 16:23:38 +00001028** database file. If the pager has already entered the error state,
1029** do not attempt the rollback.
danielk1977ae72d982007-10-03 08:46:44 +00001030*/
danielk197745d68822009-01-16 16:23:38 +00001031static void pagerUnlockAndRollback(Pager *p){
1032 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +00001033 sqlite3BeginBenignMalloc();
danielk197745d68822009-01-16 16:23:38 +00001034 sqlite3PagerRollback(p);
danielk19772d1d86f2008-06-20 14:59:51 +00001035 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001036 }
danielk197745d68822009-01-16 16:23:38 +00001037 pager_unlock(p);
danielk1977ae72d982007-10-03 08:46:44 +00001038}
1039
1040/*
danielk197745d68822009-01-16 16:23:38 +00001041** This routine ends a transaction. A transaction is ended by either
1042** a COMMIT or a ROLLBACK.
drh80e35f42007-03-30 14:06:34 +00001043**
danielk197745d68822009-01-16 16:23:38 +00001044** When this routine is called, the pager has the journal file open and
1045** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1046** the database lock and acquires a SHARED lock in its place if that is
1047** the appropriate thing to do. Release locks usually is appropriate,
1048** unless we are in exclusive access mode or unless this is a
1049** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
drh80e35f42007-03-30 14:06:34 +00001050**
danielk197745d68822009-01-16 16:23:38 +00001051** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001052**
danielk197745d68822009-01-16 16:23:38 +00001053** TODO: Consider keeping the journal file open for temporary databases.
1054** This might give a performance improvement on windows where opening
1055** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001056*/
danielk1977df2566a2008-05-07 19:11:03 +00001057static int pager_end_transaction(Pager *pPager, int hasMaster){
danielk197745d68822009-01-16 16:23:38 +00001058 int rc = SQLITE_OK;
1059 int rc2 = SQLITE_OK;
drha6abd042004-06-09 17:37:22 +00001060 if( pPager->state<PAGER_RESERVED ){
1061 return SQLITE_OK;
1062 }
danielk197745d68822009-01-16 16:23:38 +00001063 releaseAllSavepoint(pPager);
1064 if( pPager->journalOpen ){
danielk1977b3175382008-10-17 18:51:52 +00001065 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
1066 int isMemoryJournal = sqlite3IsMemJournal(pPager->jfd);
1067 sqlite3OsClose(pPager->jfd);
danielk197745d68822009-01-16 16:23:38 +00001068 pPager->journalOpen = 0;
danielk1977b3175382008-10-17 18:51:52 +00001069 if( !isMemoryJournal ){
1070 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
1071 }
1072 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE
drh04335882008-09-26 21:08:08 +00001073 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){
1074 pPager->journalOff = 0;
1075 pPager->journalStarted = 0;
1076 }else if( pPager->exclusiveMode
danielk197793f7af92008-05-09 16:57:50 +00001077 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1078 ){
1079 rc = zeroJournalHdr(pPager, hasMaster);
1080 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +00001081 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001082 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001083 }else{
drh04335882008-09-26 21:08:08 +00001084 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || rc );
danielk1977b4b47412007-08-17 15:53:36 +00001085 sqlite3OsClose(pPager->jfd);
danielk197745d68822009-01-16 16:23:38 +00001086 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +00001087 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00001088 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001089 }
danielk197741483462007-03-24 16:45:04 +00001090 }
danielk197745d68822009-01-16 16:23:38 +00001091 sqlite3BitvecDestroy(pPager->pInJournal);
1092 pPager->pInJournal = 0;
1093 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
1094 pPager->pAlwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001095#ifdef SQLITE_CHECK_PAGES
danielk1977bc2ca9e2008-11-13 14:28:28 +00001096 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
danielk19773c407372005-02-15 02:54:14 +00001097#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +00001098 sqlite3PcacheCleanAll(pPager->pPCache);
danielk197745d68822009-01-16 16:23:38 +00001099 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001100 pPager->nRec = 0;
danielk197745d68822009-01-16 16:23:38 +00001101 }else{
1102 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001103 }
danielk1977979f38e2007-03-27 16:19:51 +00001104
danielk197741483462007-03-24 16:45:04 +00001105 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001106 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001107 pPager->state = PAGER_SHARED;
danielk1977104f1fe2009-01-14 17:45:57 +00001108 pPager->changeCountDone = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001109 }else if( pPager->state==PAGER_SYNCED ){
1110 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001111 }
danielk197745d68822009-01-16 16:23:38 +00001112 pPager->dbOrigSize = 0;
danielk197776572402004-06-25 02:38:54 +00001113 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001114 pPager->needSync = 0;
danielk197745d68822009-01-16 16:23:38 +00001115 /* lruListSetFirstSynced(pPager); */
danielk1977f90b7262009-01-07 15:18:20 +00001116 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
danielk1977b3175382008-10-17 18:51:52 +00001117 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00001118 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00001119 }
danielk197745d68822009-01-16 16:23:38 +00001120 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001121
1122 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001123}
1124
drhed7c8552001-04-11 14:29:21 +00001125/*
danielk197745d68822009-01-16 16:23:38 +00001126** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001127**
danielk197745d68822009-01-16 16:23:38 +00001128** This is not a real checksum. It is really just the sum of the
1129** random initial value and the page number. We experimented with
1130** a checksum of the entire data, but that was found to be too slow.
drh726de592004-06-10 23:35:50 +00001131**
danielk197745d68822009-01-16 16:23:38 +00001132** Note that the page number is stored at the beginning of data and
1133** the checksum is stored at the end. This is important. If journal
1134** corruption occurs due to a power failure, the most likely scenario
1135** is that one end or the other of the record will be changed. It is
1136** much less likely that the two ends of the journal record will be
drh726de592004-06-10 23:35:50 +00001137** correct and the middle be corrupt. Thus, this "checksum" scheme,
1138** though fast and simple, catches the mostly likely kind of corruption.
danielk197745d68822009-01-16 16:23:38 +00001139**
1140** FIX ME: Consider adding every 200th (or so) byte of the data to the
1141** checksum. That way if a single page spans 3 or more disk sectors and
1142** only the middle sector is corrupt, we will still have a reasonable
1143** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001144*/
drh74161702006-02-24 02:53:49 +00001145static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk197745d68822009-01-16 16:23:38 +00001146 u32 cksum = pPager->cksumInit;
1147 int i = pPager->pageSize-200;
danielk1977ef317ab2004-06-23 10:43:10 +00001148 while( i>0 ){
1149 cksum += aData[i];
1150 i -= 200;
1151 }
drh968af522003-02-11 14:55:40 +00001152 return cksum;
1153}
1154
1155/*
drhd6e5e092009-01-07 02:03:35 +00001156** Read a single page from either the journal file (if isMainJrnl==1) or
1157** from the sub-journal (if isMainJrnl==0) and playback that page.
danielk197745d68822009-01-16 16:23:38 +00001158** The page begins at offset *pOffset into the file. The *pOffset
drhd6e5e092009-01-07 02:03:35 +00001159** value is increased to the start of the next page in the journal.
drh968af522003-02-11 14:55:40 +00001160**
drhc13148f2008-08-27 18:03:20 +00001161** The isMainJrnl flag is true if this is the main rollback journal and
1162** false for the statement journal. The main rollback journal uses
1163** checksums - the statement journal does not.
drhd6e5e092009-01-07 02:03:35 +00001164**
1165** If pDone is not NULL, then it is a record of pages that have already
1166** been played back. If the page at *pOffset has already been played back
1167** (if the corresponding pDone bit is set) then skip the playback.
1168** Make sure the pDone bit corresponding to the *pOffset page is set
1169** prior to returning.
drhfa86c412002-02-02 15:01:15 +00001170*/
danielk197762079062007-08-15 17:08:46 +00001171static int pager_playback_one_page(
danielk1977fd7f0452008-12-17 17:30:26 +00001172 Pager *pPager, /* The pager being played back */
1173 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
drhd6e5e092009-01-07 02:03:35 +00001174 i64 *pOffset, /* Offset of record to playback */
danielk1977ecfef982008-12-20 08:39:57 +00001175 int isSavepnt, /* True for a savepoint rollback */
danielk1977fd7f0452008-12-17 17:30:26 +00001176 Bitvec *pDone /* Bitvec of pages already played back */
danielk197762079062007-08-15 17:08:46 +00001177){
drhfa86c412002-02-02 15:01:15 +00001178 int rc;
drhae2b40c2004-06-09 19:03:54 +00001179 PgHdr *pPg; /* An existing page in the cache */
1180 Pgno pgno; /* The page number of a page in journal */
1181 u32 cksum; /* Checksum used for sanity checking */
drhd6e5e092009-01-07 02:03:35 +00001182 u8 *aData; /* Temporary storage for the page */
1183 sqlite3_file *jfd; /* The file descriptor for the journal file */
drhfa86c412002-02-02 15:01:15 +00001184
drhd6e5e092009-01-07 02:03:35 +00001185 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
1186 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
1187 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
1188 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
drh96362842005-03-20 22:47:56 +00001189
drhd6e5e092009-01-07 02:03:35 +00001190 aData = (u8*)pPager->pTmpSpace;
1191 assert( aData ); /* Temp storage must have already been allocated */
1192
1193 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
danielk197745d68822009-01-16 16:23:38 +00001194
drhd6e5e092009-01-07 02:03:35 +00001195 rc = read32bits(jfd, *pOffset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001196 if( rc!=SQLITE_OK ) return rc;
drhd6e5e092009-01-07 02:03:35 +00001197 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, (*pOffset)+4);
drh99ee3602003-02-16 19:13:36 +00001198 if( rc!=SQLITE_OK ) return rc;
drhd6e5e092009-01-07 02:03:35 +00001199 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
drhfa86c412002-02-02 15:01:15 +00001200
drh968af522003-02-11 14:55:40 +00001201 /* Sanity checking on the page. This is more important that I originally
1202 ** thought. If a power failure occurs while the journal is being written,
1203 ** it could cause invalid data to be written into the journal. We need to
1204 ** detect this invalid data (with high probability) and ignore it.
1205 */
danielk197775edc162004-06-26 01:48:18 +00001206 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001207 return SQLITE_DONE;
1208 }
danielk1977fd7f0452008-12-17 17:30:26 +00001209 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
drh968af522003-02-11 14:55:40 +00001210 return SQLITE_OK;
1211 }
drhc13148f2008-08-27 18:03:20 +00001212 if( isMainJrnl ){
drhd6e5e092009-01-07 02:03:35 +00001213 rc = read32bits(jfd, (*pOffset)-4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001214 if( rc ) return rc;
danielk1977ecfef982008-12-20 08:39:57 +00001215 if( !isSavepnt && pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001216 return SQLITE_DONE;
1217 }
1218 }
danielk1977fd7f0452008-12-17 17:30:26 +00001219 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno)) ){
1220 return rc;
1221 }
drhfa86c412002-02-02 15:01:15 +00001222
danielk1977aa5ccdf2004-06-14 05:10:42 +00001223 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001224
1225 /* If the pager is in RESERVED state, then there must be a copy of this
1226 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001227 ** not the database file. The page is left marked dirty in this case.
1228 **
danielk19772df71c72007-05-24 07:22:42 +00001229 ** An exception to the above rule: If the database is in no-sync mode
1230 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001231 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1232 ** during a Movepage() call, then the page may not be in the cache
1233 ** either. So the condition described in the above paragraph is not
1234 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001235 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001236 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1237 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001238 **
1239 ** Ticket #1171: The statement journal might contain page content that is
1240 ** different from the page content at the start of the transaction.
1241 ** This occurs when a page is changed prior to the start of a statement
1242 ** then changed again within the statement. When rolling back such a
1243 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001244 ** for certain that original page contents are synced into the main rollback
1245 ** journal. Otherwise, a power loss might leave modified data in the
1246 ** database file without an entry in the rollback journal that can
1247 ** restore the database to its original form. Two conditions must be
1248 ** met before writing to the database files. (1) the database must be
1249 ** locked. (2) we know that the original page content is fully synced
1250 ** in the main journal either because the page is not in cache or else
1251 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001252 **
1253 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1254 ** is possible to fail a statement on a database that does not yet exist.
1255 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001256 */
drhae2b40c2004-06-09 19:03:54 +00001257 pPg = pager_lookup(pPager, pgno);
drh30d53702009-01-06 15:58:57 +00001258 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
danielk1977ecfef982008-12-20 08:39:57 +00001259 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData),
1260 (isMainJrnl?"main-journal":"sub-journal")
drh30d53702009-01-06 15:58:57 +00001261 ));
danielk19778c0a7912008-08-20 14:49:23 +00001262 if( (pPager->state>=PAGER_EXCLUSIVE)
1263 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
danielk197745d68822009-01-16 16:23:38 +00001264 && (pPager->fd->pMethods)
danielk19778c0a7912008-08-20 14:49:23 +00001265 ){
drh281b21d2008-08-22 12:57:08 +00001266 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
1267 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
danielk19773460d192008-12-27 15:23:13 +00001268 if( pgno>pPager->dbFileSize ){
1269 pPager->dbFileSize = pgno;
1270 }
danielk1977f2c31ad2009-01-06 13:40:08 +00001271 }else if( !isMainJrnl && pPg==0 ){
1272 /* If this is a rollback of a savepoint and data was not written to
1273 ** the database and the page is not in-memory, there is a potential
1274 ** problem. When the page is next fetched by the b-tree layer, it
1275 ** will be read from the database file, which may or may not be
1276 ** current.
1277 **
1278 ** There are a couple of different ways this can happen. All are quite
danielk1977401b65e2009-01-06 14:34:34 +00001279 ** obscure. When running in synchronous mode, this can only happen
danielk1977f2c31ad2009-01-06 13:40:08 +00001280 ** if the page is on the free-list at the start of the transaction, then
1281 ** populated, then moved using sqlite3PagerMovepage().
1282 **
1283 ** The solution is to add an in-memory page to the cache containing
1284 ** the data just read from the sub-journal. Mark the page as dirty
1285 ** and if the pager requires a journal-sync, then mark the page as
1286 ** requiring a journal-sync before it is written.
1287 */
1288 assert( isSavepnt );
1289 if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1)) ){
1290 return rc;
1291 }
1292 pPg->flags &= ~PGHDR_NEED_READ;
1293 sqlite3PcacheMakeDirty(pPg);
danielk1977a3f3a5f2004-06-10 04:32:16 +00001294 }
drhfa86c412002-02-02 15:01:15 +00001295 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001296 /* No page should ever be explicitly rolled back that is in use, except
1297 ** for page 1 which is held in use in order to keep the lock on the
1298 ** database active. However such a page may be rolled back as a result
1299 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001300 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001301 */
drhb6f41482004-05-14 01:58:11 +00001302 void *pData;
danielk19778c0a7912008-08-20 14:49:23 +00001303 pData = pPg->pData;
drhae2b40c2004-06-09 19:03:54 +00001304 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001305 if( pPager->xReiniter ){
danielk1977eaa06f62008-09-18 17:34:44 +00001306 pPager->xReiniter(pPg);
drhde647132004-05-07 17:57:49 +00001307 }
danielk197745d68822009-01-16 16:23:38 +00001308 if( isMainJrnl && (!isSavepnt || pPager->journalOff<=pPager->journalHdr) ){
danielk1977488af092008-12-19 16:31:11 +00001309 /* If the contents of this page were just restored from the main
1310 ** journal file, then its content must be as they were when the
1311 ** transaction was first opened. In this case we can mark the page
1312 ** as clean, since there will be no need to write it out to the.
1313 **
1314 ** There is one exception to this rule. If the page is being rolled
1315 ** back as part of a savepoint (or statement) rollback from an
1316 ** unsynced portion of the main journal file, then it is not safe
1317 ** to mark the page as clean. This is because marking the page as
1318 ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
1319 ** already in the journal file (recorded in Pager.pInJournal) and
1320 ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
1321 ** again within this transaction, it will be marked as dirty but
1322 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
1323 ** be written out into the database file before its journal file
1324 ** segment is synced. If a crash occurs during or following this,
1325 ** database corruption may ensue.
1326 */
drhc047b9f2008-11-21 03:23:43 +00001327 sqlite3PcacheMakeClean(pPg);
1328 }
danielk19773c407372005-02-15 02:54:14 +00001329#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001330 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001331#endif
drh86a88112007-04-16 15:02:19 +00001332 /* If this was page 1, then restore the value of Pager.dbFileVers.
1333 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001334 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001335 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001336 }
drh86a88112007-04-16 15:02:19 +00001337
1338 /* Decode the page just read from disk */
1339 CODEC1(pPager, pData, pPg->pgno, 3);
danielk19778c0a7912008-08-20 14:49:23 +00001340 sqlite3PcacheRelease(pPg);
drhfa86c412002-02-02 15:01:15 +00001341 }
1342 return rc;
1343}
1344
drhee03d622009-01-07 15:33:45 +00001345#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
drhd6e5e092009-01-07 02:03:35 +00001346/*
1347** This routine looks ahead into the main journal file and determines
1348** whether or not the next record (the record that begins at file
1349** offset pPager->journalOff) is a well-formed page record consisting
1350** of a valid page number, pPage->pageSize bytes of content, followed
1351** by a valid checksum.
1352**
1353** The pager never needs to know this in order to do its job. This
1354** routine is only used from with assert() and testcase() macros.
1355*/
1356static int pagerNextJournalPageIsValid(Pager *pPager){
1357 Pgno pgno; /* The page number of the page */
1358 u32 cksum; /* The page checksum */
1359 int rc; /* Return code from read operations */
1360 sqlite3_file *fd; /* The file descriptor from which we are reading */
1361 u8 *aData; /* Content of the page */
1362
1363 /* Read the page number header */
1364 fd = pPager->jfd;
1365 rc = read32bits(fd, pPager->journalOff, &pgno);
1366 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1367 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ return 0; } /*NO_TEST*/
1368 if( pgno>(Pgno)pPager->dbSize ){ return 0; } /*NO_TEST*/
1369
1370 /* Read the checksum */
1371 rc = read32bits(fd, pPager->journalOff+pPager->pageSize+4, &cksum);
1372 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1373
1374 /* Read the data and verify the checksum */
1375 aData = (u8*)pPager->pTmpSpace;
1376 rc = sqlite3OsRead(fd, aData, pPager->pageSize, pPager->journalOff+4);
1377 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1378 if( pager_cksum(pPager, aData)!=cksum ){ return 0; } /*NO_TEST*/
1379
1380 /* Reach this point only if the page is valid */
1381 return 1;
1382}
1383#endif /* !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST) */
1384
drhfa86c412002-02-02 15:01:15 +00001385/*
danielk197713adf8a2004-06-03 16:08:41 +00001386** Parameter zMaster is the name of a master journal file. A single journal
1387** file that referred to the master journal file has just been rolled back.
1388** This routine checks if it is possible to delete the master journal file,
1389** and does so if it is.
drh726de592004-06-10 23:35:50 +00001390**
danielk197765839c62007-08-30 08:08:17 +00001391** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1392** available for use within this function.
1393**
1394**
danielk197745d68822009-01-16 16:23:38 +00001395** The master journal file contains the names of all child journals.
1396** To tell if a master journal can be deleted, check to each of the
1397** children. If all children are either missing or do not refer to
1398** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001399*/
danielk1977b4b47412007-08-17 15:53:36 +00001400static int pager_delmaster(Pager *pPager, const char *zMaster){
1401 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197745d68822009-01-16 16:23:38 +00001402 int rc;
1403 int master_open = 0;
1404 sqlite3_file *pMaster;
1405 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001406 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001407 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001408
1409 /* Open the master journal file exclusively in case some other process
1410 ** is running this routine also. Not that it makes too much difference.
1411 */
drhe5ae5732008-06-15 02:51:47 +00001412 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001413 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001414 if( !pMaster ){
1415 rc = SQLITE_NOMEM;
1416 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001417 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1418 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001419 }
danielk197713adf8a2004-06-03 16:08:41 +00001420 if( rc!=SQLITE_OK ) goto delmaster_out;
danielk197745d68822009-01-16 16:23:38 +00001421 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001422
1423 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001424 if( rc!=SQLITE_OK ) goto delmaster_out;
1425
1426 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001427 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001428 char *zMasterPtr = 0;
danielk197745d68822009-01-16 16:23:38 +00001429 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001430
1431 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001432 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001433 */
drh4f21c4a2008-12-10 22:15:00 +00001434 zMasterJournal = (char *)sqlite3Malloc((int)nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001435 if( !zMasterJournal ){
1436 rc = SQLITE_NOMEM;
1437 goto delmaster_out;
1438 }
danielk197765839c62007-08-30 08:08:17 +00001439 zMasterPtr = &zMasterJournal[nMasterJournal];
drh4f21c4a2008-12-10 22:15:00 +00001440 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001441 if( rc!=SQLITE_OK ) goto delmaster_out;
1442
danielk19775865e3d2004-06-14 06:03:57 +00001443 zJournal = zMasterJournal;
1444 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001445 int exists;
1446 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1447 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001448 goto delmaster_out;
1449 }
danielk1977861f7452008-06-05 11:39:11 +00001450 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001451 /* One of the journals pointed to by the master journal exists.
1452 ** Open it and check if it points at the master journal. If
1453 ** so, return without deleting the master journal file.
1454 */
drh3b7b78b2004-11-24 01:16:43 +00001455 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001456 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1457 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001458 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001459 goto delmaster_out;
1460 }
danielk197713adf8a2004-06-03 16:08:41 +00001461
danielk197765839c62007-08-30 08:08:17 +00001462 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001463 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001464 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001465 goto delmaster_out;
1466 }
danielk197776572402004-06-25 02:38:54 +00001467
danielk197765839c62007-08-30 08:08:17 +00001468 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001469 if( c ){
danielk197776572402004-06-25 02:38:54 +00001470 /* We have a match. Do not delete the master journal file. */
1471 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001472 }
1473 }
drhea678832008-12-10 19:26:22 +00001474 zJournal += (sqlite3Strlen30(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001475 }
1476 }
1477
danielk1977fee2d252007-08-18 10:59:19 +00001478 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001479
1480delmaster_out:
1481 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001482 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001483 }
danielk197745d68822009-01-16 16:23:38 +00001484 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001485 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001486 }
danielk1977b4b47412007-08-17 15:53:36 +00001487 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001488 return rc;
1489}
1490
drha6abd042004-06-09 17:37:22 +00001491
drha6abd042004-06-09 17:37:22 +00001492/*
danielk197745d68822009-01-16 16:23:38 +00001493** If the main database file is open and an exclusive lock is held,
1494** truncate the main file of the given pager to the specified number
1495** of pages.
drh7fe3f7e2007-11-29 18:44:27 +00001496**
danielk197745d68822009-01-16 16:23:38 +00001497** It might might be the case that the file on disk is smaller than nPage.
1498** This can happen, for example, if we are in the middle of a transaction
1499** which has extended the file size and the new pages are still all held
1500** in cache, then an INSERT or UPDATE does a statement rollback. Some
1501** operating system implementations can get confused if you try to
1502** truncate a file to some size that is larger than it currently is,
1503** so detect this case and write a single zero byte to the end of the new
1504** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001505*/
danielk1977d92db532008-11-17 04:56:24 +00001506static int pager_truncate(Pager *pPager, Pgno nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001507 int rc = SQLITE_OK;
danielk197745d68822009-01-16 16:23:38 +00001508 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001509 i64 currentSize, newSize;
1510 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1511 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001512 if( rc==SQLITE_OK && currentSize!=newSize ){
1513 if( currentSize>newSize ){
1514 rc = sqlite3OsTruncate(pPager->fd, newSize);
1515 }else{
1516 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1517 }
danielk19773460d192008-12-27 15:23:13 +00001518 if( rc==SQLITE_OK ){
1519 pPager->dbFileSize = nPage;
1520 }
drh7fe3f7e2007-11-29 18:44:27 +00001521 }
danielk1977e180dd92007-04-05 17:15:52 +00001522 }
danielk1977e180dd92007-04-05 17:15:52 +00001523 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001524}
1525
1526/*
danielk197745d68822009-01-16 16:23:38 +00001527** Set the sectorSize for the given pager.
drhc80f0582007-05-01 16:59:48 +00001528**
danielk197745d68822009-01-16 16:23:38 +00001529** The sector size is at least as big as the sector size reported
1530** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001531*/
1532static void setSectorSize(Pager *pPager){
danielk197745d68822009-01-16 16:23:38 +00001533 assert(pPager->fd->pMethods||pPager->tempFile);
danielk19777a2b1ee2007-08-21 14:27:01 +00001534 if( !pPager->tempFile ){
1535 /* Sector size doesn't matter for temporary files. Also, the file
danielk197745d68822009-01-16 16:23:38 +00001536 ** may not have been opened yet, in whcih case the OsSectorSize()
danielk19777a2b1ee2007-08-21 14:27:01 +00001537 ** call will segfault.
1538 */
1539 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1540 }
drh334c80d2008-03-28 17:41:13 +00001541 if( pPager->sectorSize<512 ){
1542 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001543 }
danielk19777cbd5892009-01-10 16:15:09 +00001544 if( pPager->sectorSize>MAX_SECTOR_SIZE ){
1545 pPager->sectorSize = MAX_SECTOR_SIZE;
1546 }
drhc80f0582007-05-01 16:59:48 +00001547}
1548
1549/*
drhed7c8552001-04-11 14:29:21 +00001550** Playback the journal and thus restore the database file to
1551** the state it was in before we started making changes.
1552**
drh34e79ce2004-02-08 06:05:46 +00001553** The journal file format is as follows:
1554**
drhae2b40c2004-06-09 19:03:54 +00001555** (1) 8 byte prefix. A copy of aJournalMagic[].
1556** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001557** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001558** number of page records from the journal size.
1559** (3) 4 byte big-endian integer which is the initial value for the
1560** sanity checksum.
1561** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001562** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001563** (5) 4 byte big-endian integer which is the sector size. The header
1564** is this many bytes in size.
1565** (6) 4 byte big-endian integer which is the page case.
1566** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001567** name. The value may be zero (indicate that there is no master
1568** journal.)
drh334c80d2008-03-28 17:41:13 +00001569** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001570** and might be shorter than the value read from (5). If the first byte
1571** of the name is \000 then there is no master journal. The master
1572** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001573** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001574** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001575** + pPager->pageSize bytes of data.
1576** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001577**
drh334c80d2008-03-28 17:41:13 +00001578** When we speak of the journal header, we mean the first 8 items above.
1579** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001580**
1581** Call the value from the second bullet "nRec". nRec is the number of
1582** valid page entries in the journal. In most cases, you can compute the
1583** value of nRec from the size of the journal file. But if a power
1584** failure occurred while the journal was being written, it could be the
1585** case that the size of the journal file had already been increased but
1586** the extra entries had not yet made it safely to disk. In such a case,
1587** the value of nRec computed from the file size would be too large. For
1588** that reason, we always use the nRec value in the header.
1589**
1590** If the nRec value is 0xffffffff it means that nRec should be computed
1591** from the file size. This value is used when the user selects the
1592** no-sync option for the journal. A power failure could lead to corruption
1593** in this case. But for things like temporary table (which will be
1594** deleted when the power is restored) we don't care.
1595**
drhd9b02572001-04-15 00:37:09 +00001596** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001597** journal file then all pages up to the first corrupted page are rolled
1598** back (or no pages if the journal header is corrupted). The journal file
1599** is then deleted and SQLITE_OK returned, just as if no corruption had
1600** been encountered.
1601**
1602** If an I/O or malloc() error occurs, the journal-file is not deleted
1603** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001604*/
danielk1977e277be02007-03-23 18:12:06 +00001605static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001606 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001607 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001608 u32 nRec; /* Number of Records in the journal */
shane0b8d2762008-07-22 05:18:00 +00001609 u32 u; /* Unsigned loop counter */
drhd9b02572001-04-15 00:37:09 +00001610 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001611 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001612 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001613 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001614
drhc3a64ba2001-11-22 00:01:27 +00001615 /* Figure out how many records are in the journal. Abort early if
1616 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001617 */
danielk197745d68822009-01-16 16:23:38 +00001618 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001619 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001620 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001621 goto end_playback;
1622 }
drh240c5792004-02-08 00:40:52 +00001623
danielk197776572402004-06-25 02:38:54 +00001624 /* Read the master journal name from the journal, if it is present.
1625 ** If a master journal file name is specified, but the file is not
1626 ** present on disk, then the journal is not hot and does not need to be
1627 ** played back.
drh240c5792004-02-08 00:40:52 +00001628 */
danielk197765839c62007-08-30 08:08:17 +00001629 zMaster = pPager->pTmpSpace;
1630 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001631 if( rc==SQLITE_OK && zMaster[0] ){
1632 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001633 }
danielk197765839c62007-08-30 08:08:17 +00001634 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001635 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001636 goto end_playback;
1637 }
1638 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001639
danielk197745d68822009-01-16 16:23:38 +00001640 /* This loop terminates either when the readJournalHdr() call returns
1641 ** SQLITE_DONE or an IO error occurs. */
danielk197776572402004-06-25 02:38:54 +00001642 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001643
danielk197776572402004-06-25 02:38:54 +00001644 /* Read the next journal header from the journal file. If there are
1645 ** not enough bytes left in the journal file for a complete header, or
1646 ** it is corrupted, then a process must of failed while writing it.
1647 ** This indicates nothing more needs to be rolled back.
1648 */
1649 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1650 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001651 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001652 rc = SQLITE_OK;
1653 }
danielk197776572402004-06-25 02:38:54 +00001654 goto end_playback;
1655 }
1656
1657 /* If nRec is 0xffffffff, then this journal was created by a process
1658 ** working in no-sync mode. This means that the rest of the journal
1659 ** file consists of pages, there are no more journal headers. Compute
1660 ** the value of nRec based on this assumption.
1661 */
1662 if( nRec==0xffffffff ){
1663 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
drh4f21c4a2008-12-10 22:15:00 +00001664 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
danielk197776572402004-06-25 02:38:54 +00001665 }
1666
danielk1977e277be02007-03-23 18:12:06 +00001667 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001668 ** process and if this is the final header in the journal, then it means
1669 ** that this part of the journal was being filled but has not yet been
1670 ** synced to disk. Compute the number of pages based on the remaining
1671 ** size of the file.
1672 **
1673 ** The third term of the test was added to fix ticket #2565.
drhd6e5e092009-01-07 02:03:35 +00001674 ** When rolling back a hot journal, nRec==0 always means that the next
1675 ** chunk of the journal contains zero pages to be rolled back. But
1676 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
1677 ** the journal, it means that the journal might contain additional
1678 ** pages that need to be rolled back and that the number of pages
drhee03d622009-01-07 15:33:45 +00001679 ** should be computed based on the journal file size.
danielk1977e277be02007-03-23 18:12:06 +00001680 */
drh4fd18c42009-01-08 15:24:01 +00001681 testcase( nRec==0 && !isHot
drhd6e5e092009-01-07 02:03:35 +00001682 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)!=pPager->journalOff
1683 && ((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager))>0
drh4fd18c42009-01-08 15:24:01 +00001684 && pagerNextJournalPageIsValid(pPager)
drhd6e5e092009-01-07 02:03:35 +00001685 );
drh8940f4e2007-08-11 00:26:20 +00001686 if( nRec==0 && !isHot &&
1687 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
drh4f21c4a2008-12-10 22:15:00 +00001688 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
danielk1977e277be02007-03-23 18:12:06 +00001689 }
1690
danielk197776572402004-06-25 02:38:54 +00001691 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001692 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001693 */
danielk1977e180dd92007-04-05 17:15:52 +00001694 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001695 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001696 if( rc!=SQLITE_OK ){
1697 goto end_playback;
1698 }
danielk1977f90b7262009-01-07 15:18:20 +00001699 pPager->dbSize = mxPg;
danielk197776572402004-06-25 02:38:54 +00001700 }
1701
danielk197745d68822009-01-16 16:23:38 +00001702 /* Copy original pages out of the journal and back into the database file.
danielk197776572402004-06-25 02:38:54 +00001703 */
shane0b8d2762008-07-22 05:18:00 +00001704 for(u=0; u<nRec; u++){
drhd6e5e092009-01-07 02:03:35 +00001705 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 0, 0);
danielk197776572402004-06-25 02:38:54 +00001706 if( rc!=SQLITE_OK ){
1707 if( rc==SQLITE_DONE ){
1708 rc = SQLITE_OK;
1709 pPager->journalOff = szJ;
1710 break;
1711 }else{
drha9625ea2008-09-03 00:08:29 +00001712 /* If we are unable to rollback, then the database is probably
1713 ** going to end up being corrupt. It is corrupt to us, anyhow.
1714 ** Perhaps the next process to come along can fix it....
1715 */
danielk197798c21902008-09-23 16:41:29 +00001716 rc = SQLITE_CORRUPT_BKPT;
danielk197776572402004-06-25 02:38:54 +00001717 goto end_playback;
1718 }
1719 }
drh968af522003-02-11 14:55:40 +00001720 }
drhed7c8552001-04-11 14:29:21 +00001721 }
drh580eeaf2006-02-24 03:09:37 +00001722 /*NOTREACHED*/
1723 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001724
1725end_playback:
drh8f941bc2009-01-14 23:03:40 +00001726 /* Following a rollback, the database file should be back in its original
1727 ** state prior to the start of the transaction, so invoke the
1728 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
1729 ** assertion that the transaction counter was modified.
1730 */
1731 assert(
1732 pPager->fd->pMethods==0 ||
1733 sqlite3OsFileControl(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0)>=SQLITE_OK
1734 );
1735
danielk19778191bff2004-06-28 04:52:30 +00001736 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001737 zMaster = pPager->pTmpSpace;
1738 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1739 }
1740 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001741 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001742 }
danielk1977c56774e2008-10-07 11:51:20 +00001743 if( rc==SQLITE_OK && zMaster[0] && res ){
danielk1977979f38e2007-03-27 16:19:51 +00001744 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001745 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001746 */
danielk197765839c62007-08-30 08:08:17 +00001747 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001748 }
danielk197776572402004-06-25 02:38:54 +00001749
1750 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001751 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001752 ** value. Reset it to the correct value for this process.
1753 */
drhc80f0582007-05-01 16:59:48 +00001754 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001755 return rc;
drhed7c8552001-04-11 14:29:21 +00001756}
1757
1758/*
danielk197745d68822009-01-16 16:23:38 +00001759** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
1760** the entire master journal file.
drhd6e5e092009-01-07 02:03:35 +00001761**
danielk197745d68822009-01-16 16:23:38 +00001762** The case pSavepoint==NULL occurs when a ROLLBACK TO command is invoked
1763** on a SAVEPOINT that is a transaction savepoint.
drhfa86c412002-02-02 15:01:15 +00001764*/
danielk1977fd7f0452008-12-17 17:30:26 +00001765static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
drhd6e5e092009-01-07 02:03:35 +00001766 i64 szJ; /* Effective size of the main journal */
danielk1977fd7f0452008-12-17 17:30:26 +00001767 i64 iHdrOff; /* End of first segment of main-journal records */
danielk197745d68822009-01-16 16:23:38 +00001768 Pgno ii; /* Loop counter */
danielk1977f2c31ad2009-01-06 13:40:08 +00001769 int rc = SQLITE_OK; /* Return code */
danielk1977fd7f0452008-12-17 17:30:26 +00001770 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
drhfa86c412002-02-02 15:01:15 +00001771
danielk1977fd7f0452008-12-17 17:30:26 +00001772 /* Allocate a bitvec to use to store the set of pages rolled back */
1773 if( pSavepoint ){
1774 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
1775 if( !pDone ){
1776 return SQLITE_NOMEM;
1777 }
danielk197776572402004-06-25 02:38:54 +00001778 }
danielk1977fd7f0452008-12-17 17:30:26 +00001779
danielk197745d68822009-01-16 16:23:38 +00001780 /* Truncate the database back to the size it was before the
1781 ** savepoint being reverted was opened.
drhfa86c412002-02-02 15:01:15 +00001782 */
drhd6e5e092009-01-07 02:03:35 +00001783 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
danielk197745d68822009-01-16 16:23:38 +00001784 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001785
drhd6e5e092009-01-07 02:03:35 +00001786 /* Use pPager->journalOff as the effective size of the main rollback
1787 ** journal. The actual file might be larger than this in
1788 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
1789 ** past pPager->journalOff is off-limits to us.
drhfa86c412002-02-02 15:01:15 +00001790 */
danielk1977fd7f0452008-12-17 17:30:26 +00001791 szJ = pPager->journalOff;
drhd6e5e092009-01-07 02:03:35 +00001792
1793 /* Begin by rolling back records from the main journal starting at
1794 ** PagerSavepoint.iOffset and continuing to the next journal header.
1795 ** There might be records in the main journal that have a page number
1796 ** greater than the current database size (pPager->dbSize) but those
1797 ** will be skipped automatically. Pages are added to pDone as they
1798 ** are played back.
1799 */
danielk1977fd7f0452008-12-17 17:30:26 +00001800 if( pSavepoint ){
1801 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
1802 pPager->journalOff = pSavepoint->iOffset;
1803 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
drhd6e5e092009-01-07 02:03:35 +00001804 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone);
danielk197745d68822009-01-16 16:23:38 +00001805 assert( rc!=SQLITE_DONE );
danielk1977fd7f0452008-12-17 17:30:26 +00001806 }
1807 }else{
1808 pPager->journalOff = 0;
drhfa86c412002-02-02 15:01:15 +00001809 }
drhd6e5e092009-01-07 02:03:35 +00001810
1811 /* Continue rolling back records out of the main journal starting at
1812 ** the first journal header seen and continuing until the effective end
1813 ** of the main journal file. Continue to skip out-of-range pages and
1814 ** continue adding pages rolled back to pDone.
1815 */
danielk1977fd7f0452008-12-17 17:30:26 +00001816 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
danielk1977c81806f2009-01-01 15:20:37 +00001817 u32 nJRec = 0; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001818 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001819 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
danielk1977fd7f0452008-12-17 17:30:26 +00001820 assert( rc!=SQLITE_DONE );
drhd6e5e092009-01-07 02:03:35 +00001821
1822 /*
1823 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
1824 ** test is related to ticket #2565. See the discussion in the
1825 ** pager_playback() function for additional information.
1826 */
drhee03d622009-01-07 15:33:45 +00001827 assert( !(nJRec==0
drhd6e5e092009-01-07 02:03:35 +00001828 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)!=pPager->journalOff
1829 && ((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager))>0
drhee03d622009-01-07 15:33:45 +00001830 && pagerNextJournalPageIsValid(pPager))
drhd6e5e092009-01-07 02:03:35 +00001831 );
1832 if( nJRec==0
1833 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
1834 ){
1835 nJRec = (szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager);
danielk197775edc162004-06-26 01:48:18 +00001836 }
danielk197712dd5492008-12-18 15:45:07 +00001837 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
drhd6e5e092009-01-07 02:03:35 +00001838 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone);
danielk197745d68822009-01-16 16:23:38 +00001839 assert( rc!=SQLITE_DONE );
danielk1977fd7f0452008-12-17 17:30:26 +00001840 }
1841 }
1842 assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
1843
drhd6e5e092009-01-07 02:03:35 +00001844 /* Finally, rollback pages from the sub-journal. Page that were
1845 ** previously rolled back out of the main journal (and are hence in pDone)
1846 ** will be skipped. Out-of-range pages are also skipped.
1847 */
danielk1977fd7f0452008-12-17 17:30:26 +00001848 if( pSavepoint ){
drhd6e5e092009-01-07 02:03:35 +00001849 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
danielk197745d68822009-01-16 16:23:38 +00001850 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK&&ii<(u32)pPager->stmtNRec; ii++){
1851 assert( offset == ii*(4+pPager->pageSize) );
drhd6e5e092009-01-07 02:03:35 +00001852 rc = pager_playback_one_page(pPager, 0, &offset, 1, pDone);
danielk197745d68822009-01-16 16:23:38 +00001853 assert( rc!=SQLITE_DONE );
danielk197776572402004-06-25 02:38:54 +00001854 }
drhfa86c412002-02-02 15:01:15 +00001855 }
danielk197776572402004-06-25 02:38:54 +00001856
danielk1977fd7f0452008-12-17 17:30:26 +00001857 sqlite3BitvecDestroy(pDone);
1858 if( rc==SQLITE_OK ){
danielk197775edc162004-06-26 01:48:18 +00001859 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001860 }
1861 return rc;
1862}
1863
1864/*
drhf57b14a2001-09-14 18:54:08 +00001865** Change the maximum number of in-memory pages that are allowed.
1866*/
danielk19773b8a05f2007-03-19 17:44:26 +00001867void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +00001868 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001869}
1870
1871/*
drh973b6e32003-02-12 14:09:42 +00001872** Adjust the robustness of the database to damage due to OS crashes
1873** or power failures by changing the number of syncs()s when writing
1874** the rollback journal. There are three levels:
1875**
drh054889e2005-11-30 03:20:31 +00001876** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001877** for temporary and transient files.
1878**
1879** NORMAL The journal is synced once before writes begin on the
1880** database. This is normally adequate protection, but
1881** it is theoretically possible, though very unlikely,
1882** that an inopertune power failure could leave the journal
1883** in a state which would cause damage to the database
1884** when it is rolled back.
1885**
1886** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001887** database (with some additional information - the nRec field
1888** of the journal header - being written in between the two
1889** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001890** single disk sector is atomic, then this mode provides
1891** assurance that the journal will not be corrupted to the
1892** point of causing damage to the database during rollback.
1893**
1894** Numeric values associated with these states are OFF==1, NORMAL=2,
1895** and FULL=3.
1896*/
danielk197793758c82005-01-21 08:13:14 +00001897#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh281b21d2008-08-22 12:57:08 +00001898void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
drh4f21c4a2008-12-10 22:15:00 +00001899 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
1900 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
drh281b21d2008-08-22 12:57:08 +00001901 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001902 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001903}
danielk197793758c82005-01-21 08:13:14 +00001904#endif
drh973b6e32003-02-12 14:09:42 +00001905
1906/*
drhaf6df112005-06-07 02:12:30 +00001907** The following global variable is incremented whenever the library
1908** attempts to open a temporary file. This information is used for
1909** testing and analysis only.
1910*/
drh0f7eb612006-08-08 13:51:43 +00001911#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001912int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001913#endif
drhaf6df112005-06-07 02:12:30 +00001914
1915/*
danielk197745d68822009-01-16 16:23:38 +00001916** Open a temporary file.
drh3f56e6e2007-03-15 01:16:47 +00001917**
danielk197745d68822009-01-16 16:23:38 +00001918** Write the file descriptor into *fd. Return SQLITE_OK on success or some
1919** other error code if we fail. The OS will automatically delete the temporary
1920** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001921*/
danielk197745d68822009-01-16 16:23:38 +00001922static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00001923 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00001924 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00001925 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00001926){
danielk197745d68822009-01-16 16:23:38 +00001927 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001928
drh0f7eb612006-08-08 13:51:43 +00001929#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001930 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001931#endif
danielk1977b4b47412007-08-17 15:53:36 +00001932
drh33f4e022007-09-03 15:19:34 +00001933 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1934 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00001935 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
danielk197745d68822009-01-16 16:23:38 +00001936 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00001937 return rc;
1938}
1939
danielk1977a858aa22008-08-22 16:22:17 +00001940static int pagerStress(void *,PgHdr *);
danielk19778c0a7912008-08-20 14:49:23 +00001941
drhfa86c412002-02-02 15:01:15 +00001942/*
danielk197745d68822009-01-16 16:23:38 +00001943** Create a new page cache and put a pointer to the page cache in *ppPager.
1944** The file to be cached need not exist. The file is not locked until
1945** the first call to sqlite3PagerGet() and is only held open until the
1946** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001947**
drh6446c4d2001-12-15 14:22:18 +00001948** If zFilename is NULL then a randomly-named temporary file is created
danielk197745d68822009-01-16 16:23:38 +00001949** and used as the file to be cached. The file will be deleted
1950** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001951**
danielk197745d68822009-01-16 16:23:38 +00001952** If zFilename is ":memory:" then all information is held in cache.
1953** It is never written to disk. This can be used to implement an
1954** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001955*/
danielk19773b8a05f2007-03-19 17:44:26 +00001956int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001957 sqlite3_vfs *pVfs, /* The virtual file system to use */
danielk197745d68822009-01-16 16:23:38 +00001958 Pager **ppPager, /* Return the Pager structure here */
drh7e3b0a02001-04-28 16:52:40 +00001959 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001960 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00001961 int flags, /* flags controlling this file */
1962 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00001963){
danielk1977b4b47412007-08-17 15:53:36 +00001964 u8 *pPtr;
danielk197745d68822009-01-16 16:23:38 +00001965 Pager *pPager = 0;
1966 int rc = SQLITE_OK;
1967 int i;
1968 int tempFile = 0;
1969 int memDb = 0;
1970 int readOnly = 0;
1971 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1972 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
1973 int journalFileSize;
1974 int pcacheSize = sqlite3PcacheSize();
1975 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
1976 char *zPathname = 0;
1977 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001978
danielk1977b3175382008-10-17 18:51:52 +00001979 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
1980 journalFileSize = sqlite3JournalSize(pVfs);
1981 }else{
1982 journalFileSize = sqlite3MemJournalSize();
1983 }
1984
danielk197745d68822009-01-16 16:23:38 +00001985 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001986 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001987
danielk197717b90b52008-06-06 11:11:25 +00001988 /* Compute and store the full pathname in an allocated buffer pointed
1989 ** to by zPathname, length nPathname. Or, if this is a temporary file,
1990 ** leave both nPathname and zPathname set to 0.
1991 */
drh1cc8c442007-08-24 16:08:29 +00001992 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00001993 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001994 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00001995 if( zPathname==0 ){
1996 return SQLITE_NOMEM;
1997 }
drh1cc8c442007-08-24 16:08:29 +00001998#ifndef SQLITE_OMIT_MEMORYDB
1999 if( strcmp(zFilename,":memory:")==0 ){
2000 memDb = 1;
2001 zPathname[0] = 0;
2002 }else
2003#endif
2004 {
danielk1977adfb9b02007-09-17 07:02:56 +00002005 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002006 }
danielk197717b90b52008-06-06 11:11:25 +00002007 if( rc!=SQLITE_OK ){
2008 sqlite3_free(zPathname);
2009 return rc;
2010 }
danielk197745d68822009-01-16 16:23:38 +00002011 nPathname = sqlite3Strlen30(zPathname);
drhae28c012007-08-24 16:29:23 +00002012 }
drh99b90c32008-03-17 13:50:58 +00002013
danielk197745d68822009-01-16 16:23:38 +00002014 /* Allocate memory for the pager structure */
2015 pPager = sqlite3MallocZero(
danielk1977b4b47412007-08-17 15:53:36 +00002016 sizeof(*pPager) + /* Pager structure */
danielk19778c0a7912008-08-20 14:49:23 +00002017 pcacheSize + /* PCache object */
danielk197745d68822009-01-16 16:23:38 +00002018 journalFileSize + /* The journal file structure */
danielk1977b3175382008-10-17 18:51:52 +00002019 pVfs->szOsFile + /* The main db file */
2020 journalFileSize * 2 + /* The two journal files */
danielk197745d68822009-01-16 16:23:38 +00002021 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00002022 );
danielk197745d68822009-01-16 16:23:38 +00002023 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002024 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002025 return SQLITE_NOMEM;
2026 }
danielk197745d68822009-01-16 16:23:38 +00002027 pPager->pPCache = (PCache *)&pPager[1];
2028 pPtr = ((u8 *)&pPager[1]) + pcacheSize;
2029 pPager->vfsFlags = vfsFlags;
2030 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
2031 pPager->sjfd = (sqlite3_file*)&pPtr[pVfs->szOsFile];
2032 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile+journalFileSize];
2033 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile+2*journalFileSize];
2034 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2035 pPager->zJournal = &pPager->zDirectory[nPathname+1];
2036 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00002037 if( zPathname ){
danielk197745d68822009-01-16 16:23:38 +00002038 memcpy(pPager->zFilename, zPathname, nPathname+1);
danielk197717b90b52008-06-06 11:11:25 +00002039 sqlite3_free(zPathname);
2040 }
danielk1977b4b47412007-08-17 15:53:36 +00002041
drh153c62c2007-08-24 03:51:33 +00002042 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002043 */
drhae28c012007-08-24 16:29:23 +00002044 if( zFilename && zFilename[0] && !memDb ){
danielk197745d68822009-01-16 16:23:38 +00002045 if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){
2046 rc = SQLITE_CANTOPEN;
2047 }else{
2048 int fout = 0;
2049 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2050 pPager->vfsFlags, &fout);
2051 readOnly = (fout&SQLITE_OPEN_READONLY);
drh1cc8c442007-08-24 16:08:29 +00002052
danielk197745d68822009-01-16 16:23:38 +00002053 /* If the file was successfully opened for read/write access,
2054 ** choose a default page size in case we have to create the
2055 ** database file. The default page size is the maximum of:
2056 **
2057 ** + SQLITE_DEFAULT_PAGE_SIZE,
2058 ** + The value returned by sqlite3OsSectorSize()
2059 ** + The largest page size that can be written atomically.
2060 */
2061 if( rc==SQLITE_OK && !readOnly ){
2062 setSectorSize(pPager);
2063 if( szPageDflt<pPager->sectorSize ){
2064 szPageDflt = pPager->sectorSize;
2065 }
danielk19779663b8f2007-08-24 11:52:28 +00002066#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk197745d68822009-01-16 16:23:38 +00002067 {
2068 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2069 int ii;
2070 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2071 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2072 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2073 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2074 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002075 }
danielk1977b4b47412007-08-17 15:53:36 +00002076 }
drh1cc8c442007-08-24 16:08:29 +00002077#endif
danielk197745d68822009-01-16 16:23:38 +00002078 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2079 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2080 }
danielk19778def5ea2004-06-16 10:39:52 +00002081 }
drhac69b052004-05-12 13:30:07 +00002082 }
danielk1977b3175382008-10-17 18:51:52 +00002083 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00002084 /* If a temporary file is requested, it is not opened immediately.
2085 ** In this case we accept the default page size and delay actually
2086 ** opening the file until the first call to OsWrite().
danielk1977b3175382008-10-17 18:51:52 +00002087 **
2088 ** This branch is also run for an in-memory database. An in-memory
2089 ** database is the same as a temp-file that is never written out to
2090 ** disk and uses an in-memory rollback journal.
danielk19777a2b1ee2007-08-21 14:27:01 +00002091 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002092 tempFile = 1;
2093 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002094 }
danielk1977aef0bf62005-12-30 16:28:01 +00002095
danielk197745d68822009-01-16 16:23:38 +00002096 if( pPager && rc==SQLITE_OK ){
2097 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00002098 }
danielk1977aef0bf62005-12-30 16:28:01 +00002099
danielk197745d68822009-01-16 16:23:38 +00002100 /* If an error occured in either of the blocks above.
2101 ** Free the Pager structure and close the file.
2102 ** Since the pager is not allocated there is no need to set
2103 ** any Pager.errMask variables.
danielk1977aef0bf62005-12-30 16:28:01 +00002104 */
danielk197745d68822009-01-16 16:23:38 +00002105 if( !pPager || !pPager->pTmpSpace ){
danielk1977b4b47412007-08-17 15:53:36 +00002106 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002107 sqlite3_free(pPager);
danielk197745d68822009-01-16 16:23:38 +00002108 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002109 }
danielk19778c0a7912008-08-20 14:49:23 +00002110 nExtra = FORCE_ALIGNMENT(nExtra);
danielk197771d5d2c2008-09-29 11:49:47 +00002111 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
drh41d30272008-08-20 21:47:45 +00002112 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
danielk1977aef0bf62005-12-30 16:28:01 +00002113
drh30d53702009-01-06 15:58:57 +00002114 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
drh153c62c2007-08-24 03:51:33 +00002115 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002116
danielk197745d68822009-01-16 16:23:38 +00002117 /* Fill in Pager.zDirectory[] */
2118 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
2119 for(i=sqlite3Strlen30(pPager->zDirectory);
2120 i>0 && pPager->zDirectory[i-1]!='/'; i--){}
2121 if( i>0 ) pPager->zDirectory[i-1] = 0;
2122
2123 /* Fill in Pager.zJournal[] */
2124 if( zPathname ){
2125 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2126 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
2127 }else{
2128 pPager->zJournal = 0;
2129 }
2130
2131 /* pPager->journalOpen = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002132 pPager->useJournal = (u8)useJournal;
2133 pPager->noReadlock = (noReadlock && readOnly) ?1:0;
drh3b59a5c2006-01-15 20:28:28 +00002134 /* pPager->stmtOpen = 0; */
2135 /* pPager->stmtInUse = 0; */
2136 /* pPager->nRef = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002137 pPager->dbSizeValid = (u8)memDb;
danielk197745d68822009-01-16 16:23:38 +00002138 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00002139 /* pPager->stmtSize = 0; */
2140 /* pPager->stmtJSize = 0; */
2141 /* pPager->nPage = 0; */
danielk197745d68822009-01-16 16:23:38 +00002142 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002143 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002144 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002145 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002146 /* pPager->errMask = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002147 pPager->tempFile = (u8)tempFile;
drh369339d2007-03-30 16:01:55 +00002148 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2149 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2150 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
drh4f21c4a2008-12-10 22:15:00 +00002151 pPager->exclusiveMode = (u8)tempFile;
2152 pPager->memDb = (u8)memDb;
2153 pPager->readOnly = (u8)readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002154 /* pPager->needSync = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002155 pPager->noSync = (pPager->tempFile || !useJournal) ?1:0;
2156 pPager->fullSync = pPager->noSync ?0:1;
danielk1977f036aef2007-08-20 05:36:51 +00002157 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002158 /* pPager->pFirst = 0; */
2159 /* pPager->pFirstSynced = 0; */
2160 /* pPager->pLast = 0; */
danielk19778c0a7912008-08-20 14:49:23 +00002161 pPager->nExtra = nExtra;
danielk1977b53e4962008-06-04 06:45:59 +00002162 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk197745d68822009-01-16 16:23:38 +00002163 assert(pPager->fd->pMethods||tempFile);
danielk1977b3175382008-10-17 18:51:52 +00002164 setSectorSize(pPager);
2165 if( memDb ){
2166 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
danielk1977b4721172007-03-19 05:54:48 +00002167 }
danielk19771ceedd32008-11-19 10:22:33 +00002168 /* pPager->xBusyHandler = 0; */
2169 /* pPager->pBusyHandlerArg = 0; */
drh3b59a5c2006-01-15 20:28:28 +00002170 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002171 *ppPager = pPager;
2172 return SQLITE_OK;
2173}
2174
2175/*
drh90f5ecb2004-07-22 01:19:35 +00002176** Set the busy handler function.
2177*/
danielk19771ceedd32008-11-19 10:22:33 +00002178void sqlite3PagerSetBusyhandler(
danielk197745d68822009-01-16 16:23:38 +00002179 Pager *pPager,
2180 int (*xBusyHandler)(void *),
2181 void *pBusyHandlerArg
danielk19771ceedd32008-11-19 10:22:33 +00002182){
2183 pPager->xBusyHandler = xBusyHandler;
2184 pPager->pBusyHandlerArg = pBusyHandlerArg;
drh90f5ecb2004-07-22 01:19:35 +00002185}
2186
2187/*
danielk197745d68822009-01-16 16:23:38 +00002188** Set the reinitializer for this pager. If not NULL, the reinitializer
2189** is called when the content of a page in cache is restored to its original
2190** value as a result of a rollback. The callback gives higher-level code
2191** an opportunity to restore the EXTRA section to agree with the restored
2192** page data.
drha6abd042004-06-09 17:37:22 +00002193*/
danielk1977eaa06f62008-09-18 17:34:44 +00002194void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
drha6abd042004-06-09 17:37:22 +00002195 pPager->xReiniter = xReinit;
2196}
2197
2198/*
danielk197745d68822009-01-16 16:23:38 +00002199** Set the page size to *pPageSize. If the suggest new page size is
2200** inappropriate, then an alternative page size is set to that
2201** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002202*/
danielk1977a1644fd2007-08-29 12:31:25 +00002203int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
danielk19771357d9f2008-09-16 05:12:24 +00002204 int rc = pPager->errCode;
2205 if( rc==SQLITE_OK ){
2206 u16 pageSize = *pPageSize;
2207 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
2208 if( pageSize && pageSize!=pPager->pageSize
2209 && (pPager->memDb==0 || pPager->dbSize==0)
2210 && sqlite3PcacheRefCount(pPager->pPCache)==0
2211 ){
2212 char *pNew = (char *)sqlite3PageMalloc(pageSize);
2213 if( !pNew ){
2214 rc = SQLITE_NOMEM;
2215 }else{
2216 pager_reset(pPager);
2217 pPager->pageSize = pageSize;
danielk197745d68822009-01-16 16:23:38 +00002218 if( !pPager->memDb ) setSectorSize(pPager);
danielk19771357d9f2008-09-16 05:12:24 +00002219 sqlite3PageFree(pPager->pTmpSpace);
2220 pPager->pTmpSpace = pNew;
2221 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
2222 }
danielk1977a1644fd2007-08-29 12:31:25 +00002223 }
drh4f21c4a2008-12-10 22:15:00 +00002224 *pPageSize = (u16)pPager->pageSize;
drh1c7880e2005-05-20 20:01:55 +00002225 }
danielk1977a1644fd2007-08-29 12:31:25 +00002226 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002227}
2228
2229/*
drh26b79942007-11-28 16:19:56 +00002230** Return a pointer to the "temporary page" buffer held internally
2231** by the pager. This is a buffer that is big enough to hold the
2232** entire content of a database page. This buffer is used internally
2233** during rollback and will be overwritten whenever a rollback
2234** occurs. But other modules are free to use it too, as long as
2235** no rollbacks are happening.
2236*/
2237void *sqlite3PagerTempSpace(Pager *pPager){
2238 return pPager->pTmpSpace;
2239}
2240
2241/*
drhf8e632b2007-05-08 14:51:36 +00002242** Attempt to set the maximum database page count if mxPage is positive.
2243** Make no changes if mxPage is zero or negative. And never reduce the
2244** maximum page count below the current size of the database.
2245**
2246** Regardless of mxPage, return the current maximum page count.
2247*/
2248int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2249 if( mxPage>0 ){
2250 pPager->mxPgno = mxPage;
2251 }
danielk1977ad0132d2008-06-07 08:58:22 +00002252 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002253 return pPager->mxPgno;
2254}
2255
2256/*
drhc9ac5ca2005-11-04 22:03:30 +00002257** The following set of routines are used to disable the simulated
2258** I/O error mechanism. These routines are used to avoid simulated
2259** errors in places where we do not care about errors.
2260**
2261** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2262** and generate no code.
2263*/
2264#ifdef SQLITE_TEST
2265extern int sqlite3_io_error_pending;
2266extern int sqlite3_io_error_hit;
2267static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002268void disable_simulated_io_errors(void){
2269 saved_cnt = sqlite3_io_error_pending;
2270 sqlite3_io_error_pending = -1;
2271}
2272void enable_simulated_io_errors(void){
2273 sqlite3_io_error_pending = saved_cnt;
2274}
2275#else
drh152410f2005-11-05 15:11:22 +00002276# define disable_simulated_io_errors()
2277# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002278#endif
2279
2280/*
drh90f5ecb2004-07-22 01:19:35 +00002281** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002282** that pDest points to.
2283**
danielk197745d68822009-01-16 16:23:38 +00002284** No error checking is done. The rational for this is that this function
2285** may be called even if the file does not exist or contain a header. In
2286** these cases sqlite3OsRead() will return an error, to which the correct
2287** response is to zero the memory at pDest and continue. A real IO error
2288** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002289*/
danielk19773b8a05f2007-03-19 17:44:26 +00002290int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002291 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002292 memset(pDest, 0, N);
danielk197745d68822009-01-16 16:23:38 +00002293 assert(pPager->fd->pMethods||pPager->tempFile);
2294 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002295 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002296 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002297 if( rc==SQLITE_IOERR_SHORT_READ ){
2298 rc = SQLITE_OK;
2299 }
drh90f5ecb2004-07-22 01:19:35 +00002300 }
drh551b7732006-11-06 21:20:25 +00002301 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002302}
2303
2304/*
danielk197745d68822009-01-16 16:23:38 +00002305** Return the total number of pages in the disk file associated with
2306** pPager.
danielk197715f411d2005-09-16 10:18:45 +00002307**
danielk197745d68822009-01-16 16:23:38 +00002308** If the PENDING_BYTE lies on the page directly after the end of the
2309** file, then consider this page part of the file too. For example, if
2310** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2311** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002312*/
danielk1977ad0132d2008-06-07 08:58:22 +00002313int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk197745d68822009-01-16 16:23:38 +00002314 i64 n = 0;
2315 int rc;
2316 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002317 if( pPager->errCode ){
danielk197745d68822009-01-16 16:23:38 +00002318 rc = pPager->errCode;
2319 return rc;
drha7aea3d2007-03-15 12:51:16 +00002320 }
danielk1977d92db532008-11-17 04:56:24 +00002321 if( pPager->dbSizeValid ){
danielk197745d68822009-01-16 16:23:38 +00002322 n = pPager->dbSize;
2323 } else {
2324 assert(pPager->fd->pMethods||pPager->tempFile);
2325 if( (pPager->fd->pMethods)
2326 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002327 pager_error(pPager, rc);
danielk1977ad0132d2008-06-07 08:58:22 +00002328 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002329 }
2330 if( n>0 && n<pPager->pageSize ){
danielk197745d68822009-01-16 16:23:38 +00002331 n = 1;
danielk197715f411d2005-09-16 10:18:45 +00002332 }else{
danielk197745d68822009-01-16 16:23:38 +00002333 n /= pPager->pageSize;
danielk197715f411d2005-09-16 10:18:45 +00002334 }
2335 if( pPager->state!=PAGER_UNLOCK ){
danielk197745d68822009-01-16 16:23:38 +00002336 pPager->dbSize = (Pgno)n;
2337 pPager->dbFileSize = (Pgno)n;
danielk1977d92db532008-11-17 04:56:24 +00002338 pPager->dbSizeValid = 1;
danielk197715f411d2005-09-16 10:18:45 +00002339 }
drhed7c8552001-04-11 14:29:21 +00002340 }
danielk197745d68822009-01-16 16:23:38 +00002341 if( n==(PENDING_BYTE/pPager->pageSize) ){
2342 n++;
drh1f595712004-06-15 01:40:29 +00002343 }
danielk197745d68822009-01-16 16:23:38 +00002344 if( n>pPager->mxPgno ){
2345 pPager->mxPgno = (Pgno)n;
2346 }
danielk1977ad0132d2008-06-07 08:58:22 +00002347 if( pnPage ){
danielk197745d68822009-01-16 16:23:38 +00002348 *pnPage = (int)n;
danielk1977ad0132d2008-06-07 08:58:22 +00002349 }
2350 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002351}
2352
2353/*
danielk197745d68822009-01-16 16:23:38 +00002354** Forward declaration
drhf7c57532003-04-25 13:22:51 +00002355*/
danielk197776572402004-06-25 02:38:54 +00002356static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002357
2358/*
danielk197745d68822009-01-16 16:23:38 +00002359** Try to obtain a lock on a file. Invoke the busy callback if the lock
2360** is currently not available. Repeat until the busy callback returns
2361** false or until the lock succeeds.
danielk197717221812005-02-15 03:38:05 +00002362**
2363** Return SQLITE_OK on success and an error code if we cannot obtain
danielk197745d68822009-01-16 16:23:38 +00002364** the lock.
danielk197717221812005-02-15 03:38:05 +00002365*/
2366static int pager_wait_on_lock(Pager *pPager, int locktype){
danielk197745d68822009-01-16 16:23:38 +00002367 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002368
2369 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002370 assert( PAGER_SHARED==SHARED_LOCK );
2371 assert( PAGER_RESERVED==RESERVED_LOCK );
2372 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002373
2374 /* If the file is currently unlocked then the size must be unknown */
danielk1977d92db532008-11-17 04:56:24 +00002375 assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
drh1aa2d8b2007-01-03 15:34:29 +00002376
danielk197717221812005-02-15 03:38:05 +00002377 if( pPager->state>=locktype ){
2378 rc = SQLITE_OK;
2379 }else{
danielk197717221812005-02-15 03:38:05 +00002380 do {
drh054889e2005-11-30 03:20:31 +00002381 rc = sqlite3OsLock(pPager->fd, locktype);
danielk19771ceedd32008-11-19 10:22:33 +00002382 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
danielk197717221812005-02-15 03:38:05 +00002383 if( rc==SQLITE_OK ){
drh4f21c4a2008-12-10 22:15:00 +00002384 pPager->state = (u8)locktype;
drhb0603412007-02-28 04:47:26 +00002385 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002386 }
2387 }
2388 return rc;
2389}
2390
danielk19773460d192008-12-27 15:23:13 +00002391#ifndef SQLITE_OMIT_AUTOVACUUM
2392/*
danielk1977f90b7262009-01-07 15:18:20 +00002393** Truncate the in-memory database file image to nPage pages. This
2394** function does not actually modify the database file on disk. It
2395** just sets the internal state of the pager object so that the
2396** truncation will be done when the current transaction is committed.
danielk19773460d192008-12-27 15:23:13 +00002397*/
2398void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
2399 assert( pPager->dbSizeValid );
2400 assert( pPager->dbSize>=nPage );
2401 pPager->dbSize = nPage;
2402}
danielk197745d68822009-01-16 16:23:38 +00002403
2404/*
2405** Return the current size of the database file image in pages. This
2406** function differs from sqlite3PagerPagecount() in two ways:
2407**
2408** a) It may only be called when at least one reference to a database
2409** page is held. This guarantees that the database size is already
2410** known and a call to sqlite3OsFileSize() is not required.
2411**
2412** b) The return value is not adjusted for the locking page.
2413*/
2414Pgno sqlite3PagerImageSize(Pager *pPager){
2415 assert( pPager->dbSizeValid );
2416 return pPager->dbSize;
2417}
danielk19773460d192008-12-27 15:23:13 +00002418#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
2419
drhf7c57532003-04-25 13:22:51 +00002420/*
drhed7c8552001-04-11 14:29:21 +00002421** Shutdown the page cache. Free all memory and close all files.
2422**
2423** If a transaction was in progress when this routine is called, that
2424** transaction is rolled back. All outstanding pages are invalidated
2425** and their memory is freed. Any attempt to use a page associated
2426** with this page cache after this function returns will likely
2427** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002428**
2429** This function always succeeds. If a transaction is active an attempt
2430** is made to roll it back. If an error occurs during the rollback
2431** a hot journal may be left in the filesystem but no error is returned
2432** to the caller.
drhed7c8552001-04-11 14:29:21 +00002433*/
danielk19773b8a05f2007-03-19 17:44:26 +00002434int sqlite3PagerClose(Pager *pPager){
danielk197745d68822009-01-16 16:23:38 +00002435
drhbafda092007-01-03 23:36:22 +00002436 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002437 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002438 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002439 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002440 pager_reset(pPager);
danielk197745d68822009-01-16 16:23:38 +00002441 if( !MEMDB ){
danielk1977f2c31ad2009-01-06 13:40:08 +00002442 /* Set Pager.journalHdr to -1 for the benefit of the pager_playback()
2443 ** call which may be made from within pagerUnlockAndRollback(). If it
2444 ** is not -1, then the unsynced portion of an open journal file may
2445 ** be played back into the database. If a power failure occurs while
2446 ** this is happening, the database may become corrupt.
2447 */
2448 pPager->journalHdr = -1;
danielk1977b3175382008-10-17 18:51:52 +00002449 pagerUnlockAndRollback(pPager);
2450 }
danielk1977443c0592009-01-16 15:21:05 +00002451 enable_simulated_io_errors();
danielk197745d68822009-01-16 16:23:38 +00002452 sqlite3EndBenignMalloc();
drh30d53702009-01-06 15:58:57 +00002453 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
drhb0603412007-02-28 04:47:26 +00002454 IOTRACE(("CLOSE %p\n", pPager))
danielk197745d68822009-01-16 16:23:38 +00002455 if( pPager->journalOpen ){
2456 sqlite3OsClose(pPager->jfd);
2457 }
2458 sqlite3BitvecDestroy(pPager->pInJournal);
2459 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
2460 releaseAllSavepoint(pPager);
danielk1977b4b47412007-08-17 15:53:36 +00002461 sqlite3OsClose(pPager->fd);
danielk197745d68822009-01-16 16:23:38 +00002462 /* Temp files are automatically deleted by the OS
2463 ** if( pPager->tempFile ){
2464 ** sqlite3OsDelete(pPager->zFilename);
2465 ** }
2466 */
2467
drhfacf0302008-06-17 15:12:00 +00002468 sqlite3PageFree(pPager->pTmpSpace);
danielk19778c0a7912008-08-20 14:49:23 +00002469 sqlite3PcacheClose(pPager->pPCache);
drh17435752007-08-16 04:30:38 +00002470 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002471 return SQLITE_OK;
2472}
2473
drh87cc3b32007-05-08 21:45:27 +00002474#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002475/*
danielk197745d68822009-01-16 16:23:38 +00002476** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002477*/
danielk197745d68822009-01-16 16:23:38 +00002478Pgno sqlite3PagerPagenumber(DbPage *p){
2479 return p->pgno;
drhed7c8552001-04-11 14:29:21 +00002480}
drh87cc3b32007-05-08 21:45:27 +00002481#endif
drhed7c8552001-04-11 14:29:21 +00002482
2483/*
danielk197745d68822009-01-16 16:23:38 +00002484** Increment the reference count for a page. The input pointer is
2485** a reference to the page data.
drhdf0b3b02001-06-23 11:36:20 +00002486*/
danielk197745d68822009-01-16 16:23:38 +00002487int sqlite3PagerRef(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002488 sqlite3PcacheRef(pPg);
danielk197745d68822009-01-16 16:23:38 +00002489 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002490}
2491
2492/*
danielk197745d68822009-01-16 16:23:38 +00002493** Sync the journal. In other words, make sure all the pages that have
drh34e79ce2004-02-08 06:05:46 +00002494** been written to the journal have actually reached the surface of the
danielk197745d68822009-01-16 16:23:38 +00002495** disk. It is not safe to modify the original database file until after
2496** the journal has been synced. If the original database is modified before
2497** the journal is synced and a power failure occurs, the unsynced journal
2498** data would be lost and we would be unable to completely rollback the
2499** database changes. Database corruption would occur.
2500**
2501** This routine also updates the nRec field in the header of the journal.
2502** (See comments on the pager_playback() routine for additional information.)
2503** If the sync mode is FULL, two syncs will occur. First the whole journal
2504** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002505**
danielk197745d68822009-01-16 16:23:38 +00002506** For temporary databases, we do not care if we are able to rollback
2507** after a power failure, so no sync occurs.
danielk19774cd2cd52007-08-23 14:48:23 +00002508**
danielk197745d68822009-01-16 16:23:38 +00002509** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2510** the database is stored, then OsSync() is never called on the journal
2511** file. In this case all that is required is to update the nRec field in
2512** the journal header.
drhfa86c412002-02-02 15:01:15 +00002513**
danielk197745d68822009-01-16 16:23:38 +00002514** This routine clears the needSync field of every page current held in
2515** memory.
drh50e5dad2001-09-15 00:57:28 +00002516*/
danielk197776572402004-06-25 02:38:54 +00002517static int syncJournal(Pager *pPager){
danielk197745d68822009-01-16 16:23:38 +00002518 int rc = SQLITE_OK;
2519
2520 /* Sync the journal before modifying the main database
2521 ** (assuming there is a journal and it needs to be synced.)
2522 */
danielk197776572402004-06-25 02:38:54 +00002523 if( pPager->needSync ){
danielk1977b3175382008-10-17 18:51:52 +00002524 assert( !pPager->tempFile );
2525 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
danielk197745d68822009-01-16 16:23:38 +00002526 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2527 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002528
danielk19774cd2cd52007-08-23 14:48:23 +00002529 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197745d68822009-01-16 16:23:38 +00002530 i64 jrnlOff = journalHdrOffset(pPager);
2531 u8 zMagic[8];
danielk1977112f7522009-01-08 17:50:45 +00002532
2533 /* This block deals with an obscure problem. If the last connection
2534 ** that wrote to this database was operating in persistent-journal
2535 ** mode, then the journal file may at this point actually be larger
2536 ** than Pager.journalOff bytes. If the next thing in the journal
2537 ** file happens to be a journal-header (written as part of the
2538 ** previous connections transaction), and a crash or power-failure
2539 ** occurs after nRec is updated but before this connection writes
2540 ** anything else to the journal file (or commits/rolls back its
2541 ** transaction), then SQLite may become confused when doing the
2542 ** hot-journal rollback following recovery. It may roll back all
2543 ** of this connections data, then proceed to rolling back the old,
2544 ** out-of-date data that follows it. Database corruption.
2545 **
2546 ** To work around this, if the journal file does appear to contain
2547 ** a valid header following Pager.journalOff, then write a 0x00
2548 ** byte to the start of it to prevent it from being recognized.
2549 */
danielk197745d68822009-01-16 16:23:38 +00002550 rc = sqlite3OsRead(pPager->jfd, zMagic, 8, jrnlOff);
2551 if( rc==SQLITE_OK && 0==memcmp(zMagic, aJournalMagic, 8) ){
danielk1977112f7522009-01-08 17:50:45 +00002552 static const u8 zerobyte = 0;
danielk197745d68822009-01-16 16:23:38 +00002553 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, jrnlOff);
danielk1977112f7522009-01-08 17:50:45 +00002554 }
2555 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
2556 return rc;
2557 }
2558
danielk197776572402004-06-25 02:38:54 +00002559 /* Write the nRec value into the journal file header. If in
2560 ** full-synchronous mode, sync the journal first. This ensures that
2561 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002562 ** it as a candidate for rollback.
2563 **
2564 ** This is not required if the persistent media supports the
2565 ** SAFE_APPEND property. Because in this case it is not possible
2566 ** for garbage data to be appended to the file, the nRec field
2567 ** is populated with 0xFFFFFFFF when the journal header is written
2568 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002569 */
danielk19774cd2cd52007-08-23 14:48:23 +00002570 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh30d53702009-01-06 15:58:57 +00002571 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
drhb0603412007-02-28 04:47:26 +00002572 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002573 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
danielk197745d68822009-01-16 16:23:38 +00002574 if( rc!=0 ) return rc;
drhd8d66e82003-02-12 02:10:15 +00002575 }
danielk197745d68822009-01-16 16:23:38 +00002576
2577 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2578 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2579 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
2580 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002581 }
danielk19774cd2cd52007-08-23 14:48:23 +00002582 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh30d53702009-01-06 15:58:57 +00002583 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
danielk19774cd2cd52007-08-23 14:48:23 +00002584 IOTRACE(("JSYNC %p\n", pPager))
2585 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2586 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2587 );
danielk197745d68822009-01-16 16:23:38 +00002588 if( rc!=0 ) return rc;
danielk19774cd2cd52007-08-23 14:48:23 +00002589 }
danielk197745d68822009-01-16 16:23:38 +00002590 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002591 }
danielk1977443c0592009-01-16 15:21:05 +00002592 pPager->needSync = 0;
danielk197745d68822009-01-16 16:23:38 +00002593
2594 /* Erase the needSync flag from every page.
2595 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00002596 sqlite3PcacheClearSyncFlags(pPager->pPCache);
drh50e5dad2001-09-15 00:57:28 +00002597 }
drh03eb96a2002-11-10 23:32:56 +00002598
danielk197745d68822009-01-16 16:23:38 +00002599 return rc;
drh50e5dad2001-09-15 00:57:28 +00002600}
2601
2602/*
danielk197745d68822009-01-16 16:23:38 +00002603** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2604** every one of those pages out to the database file. No calls are made
2605** to the page-cache to mark the pages as clean. It is the responsibility
2606** of the caller to use PcacheCleanAll() or PcacheMakeClean() to mark
2607** the pages as clean.
drh2554f8b2003-01-22 01:26:44 +00002608*/
2609static int pager_write_pagelist(PgHdr *pList){
danielk197745d68822009-01-16 16:23:38 +00002610 Pager *pPager;
2611 int rc;
drh2554f8b2003-01-22 01:26:44 +00002612
2613 if( pList==0 ) return SQLITE_OK;
2614 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002615
2616 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2617 ** database file. If there is already an EXCLUSIVE lock, the following
danielk197745d68822009-01-16 16:23:38 +00002618 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002619 **
drha6abd042004-06-09 17:37:22 +00002620 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2621 ** through an intermediate state PENDING. A PENDING lock prevents new
2622 ** readers from attaching to the database but is unsufficient for us to
2623 ** write. The idea of a PENDING lock is to prevent new readers from
2624 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002625 **
drha6abd042004-06-09 17:37:22 +00002626 ** While the pager is in the RESERVED state, the original database file
2627 ** is unchanged and we can rollback without having to playback the
2628 ** journal into the original database file. Once we transition to
2629 ** EXCLUSIVE, it means the database file has been changed and any rollback
2630 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002631 */
drh684917c2004-10-05 02:41:42 +00002632 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk197745d68822009-01-16 16:23:38 +00002633 if( rc!=SQLITE_OK ){
2634 return rc;
danielk19779eed5052004-06-04 10:38:30 +00002635 }
2636
danielk197745d68822009-01-16 16:23:38 +00002637 while( pList ){
2638
2639 /* If the file has not yet been opened, open it now. */
2640 if( !pPager->fd->pMethods ){
2641 assert(pPager->tempFile);
2642 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
2643 if( rc ) return rc;
2644 }
danielk19777a2b1ee2007-08-21 14:27:01 +00002645
danielk1977687566d2004-11-02 12:56:41 +00002646 /* If there are dirty pages in the page cache with page numbers greater
danielk1977f90b7262009-01-07 15:18:20 +00002647 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
danielk1977687566d2004-11-02 12:56:41 +00002648 ** make the file smaller (presumably by auto-vacuum code). Do not write
2649 ** any such pages to the file.
2650 */
danielk197745d68822009-01-16 16:23:38 +00002651 if( pList->pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
2652 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
2653 char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6);
danielk1977443c0592009-01-16 15:21:05 +00002654
2655 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
danielk197745d68822009-01-16 16:23:38 +00002656 PAGERID(pPager), pList->pgno, pager_pagehash(pList)));
2657 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
2658 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
danielk1977443c0592009-01-16 15:21:05 +00002659 PAGER_INCR(sqlite3_pager_writedb_count);
2660 PAGER_INCR(pPager->nWrite);
danielk197745d68822009-01-16 16:23:38 +00002661 if( pList->pgno==1 ){
2662 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2663 }
2664 if( pList->pgno>pPager->dbFileSize ){
2665 pPager->dbFileSize = pList->pgno;
2666 }
danielk1977687566d2004-11-02 12:56:41 +00002667 }
danielk197745d68822009-01-16 16:23:38 +00002668#ifndef NDEBUG
2669 else{
2670 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno));
2671 }
2672#endif
2673 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002674#ifdef SQLITE_CHECK_PAGES
2675 pList->pageHash = pager_pagehash(pList);
2676#endif
drh2554f8b2003-01-22 01:26:44 +00002677 pList = pList->pDirty;
2678 }
danielk19778c0a7912008-08-20 14:49:23 +00002679
danielk197745d68822009-01-16 16:23:38 +00002680 return SQLITE_OK;
drh2554f8b2003-01-22 01:26:44 +00002681}
2682
2683/*
danielk197745d68822009-01-16 16:23:38 +00002684** Add the page to the sub-journal. It is the callers responsibility to
2685** use subjRequiresPage() to check that it is really required before
2686** calling this function.
danielk1977f2c31ad2009-01-06 13:40:08 +00002687*/
2688static int subjournalPage(PgHdr *pPg){
2689 int rc;
2690 void *pData = pPg->pData;
2691 Pager *pPager = pPg->pPager;
danielk197745d68822009-01-16 16:23:38 +00002692 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
danielk1977f2c31ad2009-01-06 13:40:08 +00002693 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
2694
drh30d53702009-01-06 15:58:57 +00002695 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
danielk1977f2c31ad2009-01-06 13:40:08 +00002696
2697 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
2698 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
2699 if( rc==SQLITE_OK ){
2700 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
2701 }
2702 if( rc==SQLITE_OK ){
danielk197745d68822009-01-16 16:23:38 +00002703 pPager->stmtNRec++;
danielk1977f2c31ad2009-01-06 13:40:08 +00002704 assert( pPager->nSavepoint>0 );
2705 rc = addToSavepointBitvecs(pPager, pPg->pgno);
2706 }
2707 return rc;
2708}
2709
2710
2711/*
danielk19778c0a7912008-08-20 14:49:23 +00002712** This function is called by the pcache layer when it has reached some
danielk197745d68822009-01-16 16:23:38 +00002713** soft memory limit. The argument is a pointer to a purgeable Pager
2714** object. This function attempts to make a single dirty page that has no
2715** outstanding references (if one exists) clean so that it can be recycled
2716** by the pcache layer.
drh2554f8b2003-01-22 01:26:44 +00002717*/
danielk1977a858aa22008-08-22 16:22:17 +00002718static int pagerStress(void *p, PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002719 Pager *pPager = (Pager *)p;
danielk19778c0a7912008-08-20 14:49:23 +00002720 int rc = SQLITE_OK;
drh8f2e9a12008-02-26 14:46:04 +00002721
danielk197745d68822009-01-16 16:23:38 +00002722 if( pPager->doNotSync ){
danielk19778c200142008-08-25 07:12:28 +00002723 return SQLITE_OK;
2724 }
2725
danielk197745d68822009-01-16 16:23:38 +00002726 assert( pPg->flags&PGHDR_DIRTY );
2727 if( pPager->errCode==SQLITE_OK ){
2728 if( pPg->flags&PGHDR_NEED_SYNC ){
2729 rc = syncJournal(pPager);
2730 if( rc==SQLITE_OK && pPager->fullSync &&
2731 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
2732 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
2733 ){
2734 pPager->nRec = 0;
2735 rc = writeJournalHdr(pPager);
2736 }
2737 }
2738 if( rc==SQLITE_OK ){
2739 pPg->pDirty = 0;
2740 if( pPg->pgno>pPager->dbSize && subjRequiresPage(pPg) ){
2741 rc = subjournalPage(pPg);
2742 }
2743 if( rc==SQLITE_OK ){
2744 rc = pager_write_pagelist(pPg);
2745 }
2746 }
2747 if( rc!=SQLITE_OK ){
2748 pager_error(pPager, rc);
danielk1977a858aa22008-08-22 16:22:17 +00002749 }
2750 }
2751
2752 if( rc==SQLITE_OK ){
drh30d53702009-01-06 15:58:57 +00002753 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
danielk1977a858aa22008-08-22 16:22:17 +00002754 sqlite3PcacheMakeClean(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00002755 }
danielk197745d68822009-01-16 16:23:38 +00002756 return rc;
drh2554f8b2003-01-22 01:26:44 +00002757}
2758
danielk19778c0a7912008-08-20 14:49:23 +00002759
drh2554f8b2003-01-22 01:26:44 +00002760/*
danielk197745d68822009-01-16 16:23:38 +00002761** Return 1 if there is a hot journal on the given pager.
2762** A hot journal is one that needs to be played back.
drh165ffe92005-03-15 17:09:30 +00002763**
2764** If the current size of the database file is 0 but a journal file
2765** exists, that is probably an old journal left over from a prior
danielk197745d68822009-01-16 16:23:38 +00002766** database with the same name. Just delete the journal.
2767**
2768** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00002769**
2770** This routine does not open the journal file to examine its
2771** content. Hence, the journal might contain the name of a master
2772** journal file that has been deleted, and hence not be hot. Or
2773** the header of the journal might be zeroed out. This routine
2774** does not discover these cases of a non-hot journal - if the
2775** journal file exists and is not empty this routine assumes it
2776** is hot. The pager_playback() routine will discover that the
2777** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00002778*/
danielk1977d300b8a2008-08-01 10:50:23 +00002779static int hasHotJournal(Pager *pPager, int *pExists){
danielk197745d68822009-01-16 16:23:38 +00002780 sqlite3_vfs *pVfs = pPager->pVfs;
2781 int rc = SQLITE_OK;
2782 int exists = 0;
2783 int locked = 0;
drh0a846f92008-08-25 17:23:29 +00002784 assert( pPager!=0 );
2785 assert( pPager->useJournal );
danielk197745d68822009-01-16 16:23:38 +00002786 assert( pPager->fd->pMethods );
danielk1977d300b8a2008-08-01 10:50:23 +00002787 *pExists = 0;
drh0a846f92008-08-25 17:23:29 +00002788 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
2789 if( rc==SQLITE_OK && exists ){
2790 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
danielk197745d68822009-01-16 16:23:38 +00002791 }
2792 if( rc==SQLITE_OK && exists && !locked ){
2793 int nPage;
2794 rc = sqlite3PagerPagecount(pPager, &nPage);
2795 if( rc==SQLITE_OK ){
2796 if( nPage==0 ){
2797 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2798 }else{
2799 *pExists = 1;
danielk1977861f7452008-06-05 11:39:11 +00002800 }
2801 }
drhbb5f18d2007-04-06 18:23:17 +00002802 }
danielk1977d300b8a2008-08-01 10:50:23 +00002803 return rc;
drh165ffe92005-03-15 17:09:30 +00002804}
2805
2806/*
danielk197745d68822009-01-16 16:23:38 +00002807** Read the content of page pPg out of the database file.
danielk1977e180dd92007-04-05 17:15:52 +00002808*/
danielk197745d68822009-01-16 16:23:38 +00002809static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2810 int rc;
2811 i64 offset;
2812 assert( MEMDB==0 );
2813 assert(pPager->fd->pMethods||pPager->tempFile);
2814 if( !pPager->fd->pMethods ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002815 return SQLITE_IOERR_SHORT_READ;
2816 }
danielk197745d68822009-01-16 16:23:38 +00002817 offset = (pgno-1)*(i64)pPager->pageSize;
2818 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002819 PAGER_INCR(sqlite3_pager_readdb_count);
2820 PAGER_INCR(pPager->nRead);
2821 IOTRACE(("PGIN %p %d\n", pPager, pgno));
danielk197745d68822009-01-16 16:23:38 +00002822 if( pgno==1 ){
2823 memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24],
2824 sizeof(pPager->dbFileVers));
2825 }
2826 CODEC1(pPager, pPg->pData, pPg->pgno, 3);
drh30d53702009-01-06 15:58:57 +00002827 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
danielk197745d68822009-01-16 16:23:38 +00002828 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
danielk1977e180dd92007-04-05 17:15:52 +00002829 return rc;
2830}
2831
danielk197745d68822009-01-16 16:23:38 +00002832
danielk1977e180dd92007-04-05 17:15:52 +00002833/*
danielk1977e277be02007-03-23 18:12:06 +00002834** This function is called to obtain the shared lock required before
2835** data may be read from the pager cache. If the shared lock has already
2836** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002837**
2838** Immediately after obtaining the shared lock (if required), this function
2839** checks for a hot-journal file. If one is found, an emergency rollback
2840** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002841*/
2842static int pagerSharedLock(Pager *pPager){
danielk197745d68822009-01-16 16:23:38 +00002843 int rc = SQLITE_OK;
2844 int isErrorReset = 0;
danielk1977e277be02007-03-23 18:12:06 +00002845
danielk1977ae72d982007-10-03 08:46:44 +00002846 /* If this database is opened for exclusive access, has no outstanding
danielk197745d68822009-01-16 16:23:38 +00002847 ** page references and is in an error-state, now is the chance to clear
danielk1977ae72d982007-10-03 08:46:44 +00002848 ** the error. Discard the contents of the pager-cache and treat any
2849 ** open journal file as a hot-journal.
2850 */
danielk19778c0a7912008-08-20 14:49:23 +00002851 if( !MEMDB && pPager->exclusiveMode
2852 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
2853 ){
danielk197745d68822009-01-16 16:23:38 +00002854 if( pPager->journalOpen ){
danielk1977d300b8a2008-08-01 10:50:23 +00002855 isErrorReset = 1;
danielk1977ae72d982007-10-03 08:46:44 +00002856 }
danielk1977ae72d982007-10-03 08:46:44 +00002857 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00002858 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00002859 }
2860
2861 /* If the pager is still in an error state, do not proceed. The error
2862 ** state will be cleared at some point in the future when all page
2863 ** references are dropped and the cache can be discarded.
2864 */
2865 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
2866 return pPager->errCode;
2867 }
2868
danielk1977d300b8a2008-08-01 10:50:23 +00002869 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
danielk197745d68822009-01-16 16:23:38 +00002870 sqlite3_vfs *pVfs = pPager->pVfs;
drh4f21c4a2008-12-10 22:15:00 +00002871 int isHotJournal = 0;
shane049fc212008-10-22 16:26:47 +00002872 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00002873 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
2874 if( !pPager->noReadlock ){
2875 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2876 if( rc!=SQLITE_OK ){
2877 assert( pPager->state==PAGER_UNLOCK );
2878 return pager_error(pPager, rc);
danielk1977e277be02007-03-23 18:12:06 +00002879 }
danielk19770371f1b2009-01-09 17:11:04 +00002880 }else if( pPager->state==PAGER_UNLOCK ){
2881 pPager->state = PAGER_SHARED;
danielk1977b3175382008-10-17 18:51:52 +00002882 }
danielk19770371f1b2009-01-09 17:11:04 +00002883 assert( pPager->state>=SHARED_LOCK );
danielk1977b3175382008-10-17 18:51:52 +00002884
2885 /* If a journal file exists, and there is no RESERVED lock on the
2886 ** database file, then it either needs to be played back or deleted.
2887 */
2888 if( !isErrorReset ){
2889 rc = hasHotJournal(pPager, &isHotJournal);
2890 if( rc!=SQLITE_OK ){
2891 goto failed;
2892 }
2893 }
2894 if( isErrorReset || isHotJournal ){
2895 /* Get an EXCLUSIVE lock on the database file. At this point it is
2896 ** important that a RESERVED lock is not obtained on the way to the
2897 ** EXCLUSIVE lock. If it were, another process might open the
2898 ** database file, detect the RESERVED lock, and conclude that the
danielk197745d68822009-01-16 16:23:38 +00002899 ** database is safe to read while this process is still rolling it
2900 ** back.
danielk1977b3175382008-10-17 18:51:52 +00002901 **
danielk197745d68822009-01-16 16:23:38 +00002902 ** Because the intermediate RESERVED lock is not requested, the
2903 ** second process will get to this point in the code and fail to
2904 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00002905 */
danielk1977b3175382008-10-17 18:51:52 +00002906 if( pPager->state<EXCLUSIVE_LOCK ){
2907 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
danielk1977e277be02007-03-23 18:12:06 +00002908 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002909 rc = pager_error(pPager, rc);
2910 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002911 }
danielk1977b3175382008-10-17 18:51:52 +00002912 pPager->state = PAGER_EXCLUSIVE;
2913 }
2914
2915 /* Open the journal for read/write access. This is because in
2916 ** exclusive-access mode the file descriptor will be kept open and
2917 ** possibly used for a transaction later on. On some systems, the
2918 ** OsTruncate() call used in exclusive-access mode also requires
2919 ** a read/write file handle.
2920 */
danielk197745d68822009-01-16 16:23:38 +00002921 if( !isErrorReset && pPager->journalOpen==0 ){
danielk1977b3175382008-10-17 18:51:52 +00002922 int res;
2923 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
2924 if( rc==SQLITE_OK ){
2925 if( res ){
2926 int fout = 0;
2927 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
2928 assert( !pPager->tempFile );
2929 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
danielk197745d68822009-01-16 16:23:38 +00002930 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk1977281d8bd2008-10-29 07:01:56 +00002931 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
2932 rc = SQLITE_CANTOPEN;
danielk1977b3175382008-10-17 18:51:52 +00002933 sqlite3OsClose(pPager->jfd);
2934 }
2935 }else{
2936 /* If the journal does not exist, that means some other process
2937 ** has already rolled it back */
2938 rc = SQLITE_BUSY;
2939 }
2940 }
2941 }
2942 if( rc!=SQLITE_OK ){
danielk1977b3175382008-10-17 18:51:52 +00002943 goto failed;
2944 }
danielk197745d68822009-01-16 16:23:38 +00002945 pPager->journalOpen = 1;
danielk1977b3175382008-10-17 18:51:52 +00002946 pPager->journalStarted = 0;
2947 pPager->journalOff = 0;
2948 pPager->setMaster = 0;
2949 pPager->journalHdr = 0;
2950
2951 /* Playback and delete the journal. Drop the database write
danielk1977112f7522009-01-08 17:50:45 +00002952 ** lock and reacquire the read lock. Purge the cache before
2953 ** playing back the hot-journal so that we don't end up with
danielk1977ad0ea222009-01-08 18:04:13 +00002954 ** an inconsistent cache.
danielk1977b3175382008-10-17 18:51:52 +00002955 */
danielk1977112f7522009-01-08 17:50:45 +00002956 sqlite3PcacheClear(pPager->pPCache);
danielk1977b3175382008-10-17 18:51:52 +00002957 rc = pager_playback(pPager, 1);
2958 if( rc!=SQLITE_OK ){
2959 rc = pager_error(pPager, rc);
2960 goto failed;
2961 }
danielk197745d68822009-01-16 16:23:38 +00002962 assert(pPager->state==PAGER_SHARED ||
2963 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
danielk1977b3175382008-10-17 18:51:52 +00002964 );
2965 }
2966
2967 if( sqlite3PcachePagecount(pPager->pPCache)>0 ){
2968 /* The shared-lock has just been acquired on the database file
2969 ** and there are already pages in the cache (from a previous
2970 ** read or write transaction). Check to see if the database
2971 ** has been modified. If the database has changed, flush the
2972 ** cache.
2973 **
2974 ** Database changes is detected by looking at 15 bytes beginning
2975 ** at offset 24 into the file. The first 4 of these 16 bytes are
2976 ** a 32-bit counter that is incremented with each change. The
2977 ** other bytes change randomly with each file change when
2978 ** a codec is in use.
2979 **
2980 ** There is a vanishingly small chance that a change will not be
2981 ** detected. The chance of an undetected change is so small that
2982 ** it can be neglected.
2983 */
2984 char dbFileVers[sizeof(pPager->dbFileVers)];
2985 sqlite3PagerPagecount(pPager, 0);
2986
2987 if( pPager->errCode ){
2988 rc = pPager->errCode;
2989 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002990 }
2991
danielk1977d92db532008-11-17 04:56:24 +00002992 assert( pPager->dbSizeValid );
danielk1977b3175382008-10-17 18:51:52 +00002993 if( pPager->dbSize>0 ){
2994 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
2995 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
2996 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002997 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002998 }
danielk1977b3175382008-10-17 18:51:52 +00002999 }else{
3000 memset(dbFileVers, 0, sizeof(dbFileVers));
3001 }
danielk1977e277be02007-03-23 18:12:06 +00003002
danielk1977b3175382008-10-17 18:51:52 +00003003 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
3004 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003005 }
3006 }
danielk19770371f1b2009-01-09 17:11:04 +00003007 assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED );
danielk1977e277be02007-03-23 18:12:06 +00003008 }
3009
danielk197752b472a2008-05-05 16:23:55 +00003010 failed:
3011 if( rc!=SQLITE_OK ){
3012 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
3013 pager_unlock(pPager);
3014 }
danielk1977e277be02007-03-23 18:12:06 +00003015 return rc;
3016}
3017
3018/*
danielk197745d68822009-01-16 16:23:38 +00003019** Make sure we have the content for a page. If the page was
3020** previously acquired with noContent==1, then the content was
3021** just initialized to zeros instead of being read from disk.
3022** But now we need the real data off of disk. So make sure we
3023** have it. Read it in if we do not have it already.
3024*/
3025static int pager_get_content(PgHdr *pPg){
3026 if( pPg->flags&PGHDR_NEED_READ ){
3027 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3028 if( rc==SQLITE_OK ){
3029 pPg->flags &= ~PGHDR_NEED_READ;
3030 }else{
3031 return rc;
3032 }
3033 }
3034 return SQLITE_OK;
3035}
3036
3037/*
danielk19778c0a7912008-08-20 14:49:23 +00003038** If the reference count has reached zero, and the pager is not in the
3039** middle of a write transaction or opened in exclusive mode, unlock it.
3040*/
3041static void pagerUnlockIfUnused(Pager *pPager){
3042 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
3043 && (!pPager->exclusiveMode || pPager->journalOff>0)
3044 ){
3045 pagerUnlockAndRollback(pPager);
3046 }
3047}
3048
3049/*
3050** Drop a page from the cache using sqlite3PcacheDrop().
3051**
3052** If this means there are now no pages with references to them, a rollback
3053** occurs and the lock on the database is removed.
3054*/
3055static void pagerDropPage(DbPage *pPg){
3056 Pager *pPager = pPg->pPager;
3057 sqlite3PcacheDrop(pPg);
3058 pagerUnlockIfUnused(pPager);
3059}
3060
3061/*
drhd9b02572001-04-15 00:37:09 +00003062** Acquire a page.
3063**
drh58a11682001-11-10 13:51:08 +00003064** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003065** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003066**
drhd33d5a82007-04-26 12:11:28 +00003067** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003068** file is smaller than the requested page, then no actual disk
3069** read occurs and the memory image of the page is initialized to
3070** all zeros. The extra data appended to a page is always initialized
3071** to zeros the first time a page is loaded into memory.
3072**
drhd9b02572001-04-15 00:37:09 +00003073** The acquisition might fail for several reasons. In all cases,
3074** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003075**
drhd33d5a82007-04-26 12:11:28 +00003076** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003077** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003078** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003079** just returns 0. This routine acquires a read-lock the first time it
3080** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003081** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003082** or journal files.
drh0787db62007-03-04 13:15:27 +00003083**
drh538f5702007-04-13 02:14:30 +00003084** If noContent is false, the page contents are actually read from disk.
3085** If noContent is true, it means that we do not care about the contents
danielk197745d68822009-01-16 16:23:38 +00003086** of the page at this time, so do not do a disk read. Just fill in the
3087** page content with zeros. But mark the fact that we have not read the
3088** content by setting the PgHdr.needRead flag. Later on, if
3089** sqlite3PagerWrite() is called on this page or if this routine is
3090** called again with noContent==0, that means that the content is needed
3091** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003092*/
danielk197765e0ff32008-09-19 09:14:43 +00003093int sqlite3PagerAcquire(
drh538f5702007-04-13 02:14:30 +00003094 Pager *pPager, /* The pager open on the database file */
3095 Pgno pgno, /* Page number to fetch */
3096 DbPage **ppPage, /* Write a pointer to the page here */
3097 int noContent /* Do not bother reading content from disk if true */
3098){
danielk19778c0a7912008-08-20 14:49:23 +00003099 PgHdr *pPg = 0;
drh165ffe92005-03-15 17:09:30 +00003100 int rc;
drhed7c8552001-04-11 14:29:21 +00003101
danielk19778c0a7912008-08-20 14:49:23 +00003102 assert( pPager->state==PAGER_UNLOCK
3103 || sqlite3PcacheRefCount(pPager->pPCache)>0
danielk1977b3175382008-10-17 18:51:52 +00003104 || pgno==1
danielk19778c0a7912008-08-20 14:49:23 +00003105 );
danielk1977e277be02007-03-23 18:12:06 +00003106
danielk197726836652005-01-17 01:33:13 +00003107 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3108 ** number greater than this, or zero, is requested.
3109 */
drh267cb322005-09-16 17:16:52 +00003110 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003111 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003112 }
3113
drhd9b02572001-04-15 00:37:09 +00003114 /* Make sure we have not hit any critical errors.
3115 */
drh836faa42003-01-11 13:30:57 +00003116 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003117 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003118
danielk197713adf8a2004-06-03 16:08:41 +00003119 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003120 ** on the database file. pagerSharedLock() is a no-op if
3121 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003122 */
danielk1977e277be02007-03-23 18:12:06 +00003123 rc = pagerSharedLock(pPager);
3124 if( rc!=SQLITE_OK ){
3125 return rc;
drhed7c8552001-04-11 14:29:21 +00003126 }
danielk1977e277be02007-03-23 18:12:06 +00003127 assert( pPager->state!=PAGER_UNLOCK );
3128
danielk19778c0a7912008-08-20 14:49:23 +00003129 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
3130 if( rc!=SQLITE_OK ){
3131 return rc;
3132 }
danielk19778c0a7912008-08-20 14:49:23 +00003133 if( pPg->pPager==0 ){
3134 /* The pager cache has created a new page. Its content needs to
3135 ** be initialized.
3136 */
danielk1977979f38e2007-03-27 16:19:51 +00003137 int nMax;
drh538f5702007-04-13 02:14:30 +00003138 PAGER_INCR(pPager->nMiss);
danielk19778c0a7912008-08-20 14:49:23 +00003139 pPg->pPager = pPager;
danielk19778c0a7912008-08-20 14:49:23 +00003140 memset(pPg->pExtra, 0, pPager->nExtra);
danielk197724168722007-04-02 05:07:47 +00003141
danielk1977ad0132d2008-06-07 08:58:22 +00003142 rc = sqlite3PagerPagecount(pPager, &nMax);
3143 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00003144 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003145 return rc;
3146 }
danielk197775bab7d2006-01-23 13:09:45 +00003147
danielk1977a1fa00d2008-08-27 15:16:33 +00003148 if( nMax<(int)pgno || MEMDB || noContent ){
drhf8e632b2007-05-08 14:51:36 +00003149 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003150 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003151 return SQLITE_FULL;
3152 }
danielk19778c0a7912008-08-20 14:49:23 +00003153 memset(pPg->pData, 0, pPager->pageSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00003154 if( noContent ){
danielk197745d68822009-01-16 16:23:38 +00003155 pPg->flags |= PGHDR_NEED_READ;
danielk19778c0a7912008-08-20 14:49:23 +00003156 }
drh538f5702007-04-13 02:14:30 +00003157 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003158 }else{
danielk197745d68822009-01-16 16:23:38 +00003159 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003160 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
danielk197745d68822009-01-16 16:23:38 +00003161 /* sqlite3PagerUnref(pPg); */
danielk19778c0a7912008-08-20 14:49:23 +00003162 pagerDropPage(pPg);
drh551b7732006-11-06 21:20:25 +00003163 return rc;
drh81a20f22001-10-12 17:30:04 +00003164 }
drh306dc212001-05-21 13:45:10 +00003165 }
danielk19773c407372005-02-15 02:54:14 +00003166#ifdef SQLITE_CHECK_PAGES
3167 pPg->pageHash = pager_pagehash(pPg);
3168#endif
drhed7c8552001-04-11 14:29:21 +00003169 }else{
drhd9b02572001-04-15 00:37:09 +00003170 /* The requested page is in the page cache. */
danielk197745d68822009-01-16 16:23:38 +00003171 assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003172 PAGER_INCR(pPager->nHit);
danielk197745d68822009-01-16 16:23:38 +00003173 if( !noContent ){
3174 rc = pager_get_content(pPg);
3175 if( rc ){
3176 sqlite3PagerUnref(pPg);
3177 return rc;
3178 }
3179 }
drhed7c8552001-04-11 14:29:21 +00003180 }
danielk19778c0a7912008-08-20 14:49:23 +00003181
danielk19773b8a05f2007-03-19 17:44:26 +00003182 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003183 return SQLITE_OK;
3184}
danielk19778c0a7912008-08-20 14:49:23 +00003185
drhed7c8552001-04-11 14:29:21 +00003186/*
drh7e3b0a02001-04-28 16:52:40 +00003187** Acquire a page if it is already in the in-memory cache. Do
3188** not read the page from disk. Return a pointer to the page,
3189** or 0 if the page is not in cache.
3190**
danielk19773b8a05f2007-03-19 17:44:26 +00003191** See also sqlite3PagerGet(). The difference between this routine
3192** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003193** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003194** returns NULL if the page is not in cache or if a disk I/O error
3195** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003196*/
danielk19773b8a05f2007-03-19 17:44:26 +00003197DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003198 PgHdr *pPg = 0;
drh836faa42003-01-11 13:30:57 +00003199 assert( pPager!=0 );
3200 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003201
danielk19778c0a7912008-08-20 14:49:23 +00003202 if( (pPager->state!=PAGER_UNLOCK)
3203 && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL)
3204 ){
3205 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
danielk1977e277be02007-03-23 18:12:06 +00003206 }
danielk19778c0a7912008-08-20 14:49:23 +00003207
danielk19773b8a05f2007-03-19 17:44:26 +00003208 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003209}
3210
3211/*
drhed7c8552001-04-11 14:29:21 +00003212** Release a page.
3213**
3214** If the number of references to the page drop to zero, then the
3215** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003216** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003217** removed.
3218*/
danielk197745d68822009-01-16 16:23:38 +00003219int sqlite3PagerUnref(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003220 if( pPg ){
3221 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00003222 sqlite3PcacheRelease(pPg);
3223 pagerUnlockIfUnused(pPager);
drhed7c8552001-04-11 14:29:21 +00003224 }
danielk197745d68822009-01-16 16:23:38 +00003225 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003226}
3227
danielk19779153d852009-01-07 10:52:56 +00003228/*
3229** If the main journal file has already been opened, ensure that the
3230** sub-journal file is open too. If the main journal is not open,
3231** this function is a no-op.
3232**
3233** SQLITE_OK is returned if everything goes according to plan. An
3234** SQLITE_IOERR_XXX error code is returned if the call to
3235** sqlite3OsOpen() fails.
3236*/
danielk1977fd7f0452008-12-17 17:30:26 +00003237static int openSubJournal(Pager *pPager){
3238 int rc = SQLITE_OK;
danielk197745d68822009-01-16 16:23:38 +00003239 if( pPager->journalOpen && !pPager->sjfd->pMethods ){
danielk1977fd7f0452008-12-17 17:30:26 +00003240 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3241 sqlite3MemJournalOpen(pPager->sjfd);
3242 }else{
danielk197745d68822009-01-16 16:23:38 +00003243 rc = sqlite3PagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
danielk1977fd7f0452008-12-17 17:30:26 +00003244 }
3245 }
3246 return rc;
3247}
3248
drhed7c8552001-04-11 14:29:21 +00003249/*
drha6abd042004-06-09 17:37:22 +00003250** Create a journal file for pPager. There should already be a RESERVED
3251** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003252**
3253** Return SQLITE_OK if everything. Return an error code and release the
3254** write lock if anything goes wrong.
3255*/
3256static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003257 sqlite3_vfs *pVfs = pPager->pVfs;
3258 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3259
drhda47d772002-12-02 04:25:19 +00003260 int rc;
drha6abd042004-06-09 17:37:22 +00003261 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003262 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003263 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003264 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003265 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00003266 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003267 rc = SQLITE_NOMEM;
3268 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003269 }
danielk1977b4b47412007-08-17 15:53:36 +00003270
danielk197745d68822009-01-16 16:23:38 +00003271 if( pPager->journalOpen==0 ){
drhfdc40e92008-04-17 20:59:37 +00003272 if( pPager->tempFile ){
3273 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3274 }else{
3275 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003276 }
danielk1977b3175382008-10-17 18:51:52 +00003277 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3278 sqlite3MemJournalOpen(pPager->jfd);
3279 rc = SQLITE_OK;
3280 }else{
drhfdc40e92008-04-17 20:59:37 +00003281#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977b3175382008-10-17 18:51:52 +00003282 rc = sqlite3JournalOpen(
3283 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3284 );
drhfdc40e92008-04-17 20:59:37 +00003285#else
danielk1977b3175382008-10-17 18:51:52 +00003286 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
drhfdc40e92008-04-17 20:59:37 +00003287#endif
danielk1977b3175382008-10-17 18:51:52 +00003288 }
danielk197745d68822009-01-16 16:23:38 +00003289 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
drhfdc40e92008-04-17 20:59:37 +00003290 pPager->journalOff = 0;
3291 pPager->setMaster = 0;
3292 pPager->journalHdr = 0;
3293 if( rc!=SQLITE_OK ){
3294 if( rc==SQLITE_NOMEM ){
3295 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3296 }
3297 goto failed_to_open_journal;
3298 }
drhda47d772002-12-02 04:25:19 +00003299 }
danielk197745d68822009-01-16 16:23:38 +00003300 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003301 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003302 pPager->needSync = 0;
drh968af522003-02-11 14:55:40 +00003303 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003304 if( pPager->errCode ){
3305 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003306 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003307 }
danielk19773460d192008-12-27 15:23:13 +00003308 pPager->dbOrigSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003309
danielk197776572402004-06-25 02:38:54 +00003310 rc = writeJournalHdr(pPager);
3311
danielk1977fd7f0452008-12-17 17:30:26 +00003312 if( pPager->nSavepoint && rc==SQLITE_OK ){
3313 rc = openSubJournal(pPager);
drhda47d772002-12-02 04:25:19 +00003314 }
danielk1977ae72d982007-10-03 08:46:44 +00003315 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003316 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003317 if( rc==SQLITE_OK ){
3318 rc = SQLITE_FULL;
3319 }
3320 }
drh9c105bb2004-10-02 20:38:28 +00003321 return rc;
3322
3323failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003324 sqlite3BitvecDestroy(pPager->pInJournal);
3325 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003326 return rc;
drhda47d772002-12-02 04:25:19 +00003327}
3328
3329/*
drh4b845d72002-03-05 12:41:19 +00003330** Acquire a write-lock on the database. The lock is removed when
3331** the any of the following happen:
3332**
drh80e35f42007-03-30 14:06:34 +00003333** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003334** * sqlite3PagerRollback() is called.
3335** * sqlite3PagerClose() is called.
3336** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003337**
danielk197713adf8a2004-06-03 16:08:41 +00003338** The first parameter to this routine is a pointer to any open page of the
3339** database file. Nothing changes about the page - it is used merely to
3340** acquire a pointer to the Pager structure and as proof that there is
3341** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003342**
danielk197713adf8a2004-06-03 16:08:41 +00003343** The second parameter indicates how much space in bytes to reserve for a
3344** master journal file-name at the start of the journal when it is created.
3345**
3346** A journal file is opened if this is not a temporary file. For temporary
3347** files, the opening of the journal file is deferred until there is an
3348** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003349**
drha6abd042004-06-09 17:37:22 +00003350** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003351**
3352** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3353** immediately instead of waiting until we try to flush the cache. The
3354** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003355*/
danielk19773b8a05f2007-03-19 17:44:26 +00003356int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003357 Pager *pPager = pPg->pPager;
3358 int rc = SQLITE_OK;
3359 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003360 assert( pPager->state!=PAGER_UNLOCK );
3361 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003362 assert( pPager->pInJournal==0 );
danielk1977b3175382008-10-17 18:51:52 +00003363 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00003364 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
3365 if( rc==SQLITE_OK ){
3366 pPager->state = PAGER_RESERVED;
3367 if( exFlag ){
3368 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh684917c2004-10-05 02:41:42 +00003369 }
danielk1977b3175382008-10-17 18:51:52 +00003370 }
3371 if( rc!=SQLITE_OK ){
3372 return rc;
3373 }
danielk197745d68822009-01-16 16:23:38 +00003374 pPager->dirtyCache = 0;
drh30d53702009-01-06 15:58:57 +00003375 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00003376 if( pPager->useJournal && !pPager->tempFile
3377 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
3378 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00003379 }
danielk197745d68822009-01-16 16:23:38 +00003380 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00003381 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00003382 ** time a (read or write) transaction was successfully concluded
3383 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00003384 ** kept open and either was truncated to 0 bytes or its header was
3385 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00003386 */
3387 assert( pPager->nRec==0 );
danielk19773460d192008-12-27 15:23:13 +00003388 assert( pPager->dbOrigSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003389 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003390 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003391 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhf5e7bb52008-02-18 14:47:33 +00003392 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003393 rc = SQLITE_NOMEM;
3394 }else{
danielk19773460d192008-12-27 15:23:13 +00003395 pPager->dbOrigSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003396 rc = writeJournalHdr(pPager);
3397 }
drh4b845d72002-03-05 12:41:19 +00003398 }
danielk197745d68822009-01-16 16:23:38 +00003399 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003400 return rc;
3401}
3402
3403/*
drhed7c8552001-04-11 14:29:21 +00003404** Mark a data page as writeable. The page is written into the journal
3405** if it is not there already. This routine must be called before making
3406** changes to a page.
3407**
3408** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003409** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003410** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003411** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003412** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003413**
3414** If the journal file could not be written because the disk is full,
3415** then this routine returns SQLITE_FULL and does an immediate rollback.
3416** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003417** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003418** reset.
drhed7c8552001-04-11 14:29:21 +00003419*/
danielk19773b8a05f2007-03-19 17:44:26 +00003420static int pager_write(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003421 void *pData = pPg->pData;
drh69688d52001-04-14 16:38:23 +00003422 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003423 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003424
drh6446c4d2001-12-15 14:22:18 +00003425 /* Check for errors
3426 */
danielk1977efaaf572006-01-16 11:29:19 +00003427 if( pPager->errCode ){
3428 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003429 }
drh5e00f6c2001-09-13 13:46:56 +00003430 if( pPager->readOnly ){
3431 return SQLITE_PERM;
3432 }
drh6446c4d2001-12-15 14:22:18 +00003433
danielk197776572402004-06-25 02:38:54 +00003434 assert( !pPager->setMaster );
3435
danielk19773c407372005-02-15 02:54:14 +00003436 CHECK_PAGE(pPg);
3437
danielk197745d68822009-01-16 16:23:38 +00003438 /* If this page was previously acquired with noContent==1, that means
3439 ** we didn't really read in the content of the page. This can happen
3440 ** (for example) when the page is being moved to the freelist. But
3441 ** now we are (perhaps) moving the page off of the freelist for
3442 ** reuse and we need to know its original content so that content
3443 ** can be stored in the rollback journal. So do the read at this
3444 ** time.
3445 */
3446 rc = pager_get_content(pPg);
3447 if( rc ){
3448 return rc;
3449 }
3450
drh6446c4d2001-12-15 14:22:18 +00003451 /* Mark the page as dirty. If the page has already been written
3452 ** to the journal then we can return right away.
3453 */
drhc047b9f2008-11-21 03:23:43 +00003454 sqlite3PcacheMakeDirty(pPg);
danielk19773460d192008-12-27 15:23:13 +00003455 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
danielk197745d68822009-01-16 16:23:38 +00003456 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003457 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003458 }else{
drh6446c4d2001-12-15 14:22:18 +00003459
danielk1977a0bf2652004-11-04 14:30:04 +00003460 /* If we get this far, it means that the page needs to be
3461 ** written to the transaction journal or the ckeckpoint journal
3462 ** or both.
3463 **
3464 ** First check to see that the transaction journal exists and
3465 ** create it if it does not.
3466 */
3467 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003468 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003469 if( rc!=SQLITE_OK ){
3470 return rc;
3471 }
3472 assert( pPager->state>=PAGER_RESERVED );
danielk197745d68822009-01-16 16:23:38 +00003473 if( !pPager->journalOpen && pPager->useJournal
drhfdc40e92008-04-17 20:59:37 +00003474 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00003475 rc = pager_open_journal(pPager);
3476 if( rc!=SQLITE_OK ) return rc;
3477 }
danielk197745d68822009-01-16 16:23:38 +00003478 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003479 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003480
3481 /* The transaction journal now exists and we have a RESERVED or an
3482 ** EXCLUSIVE lock on the main database file. Write the current page to
3483 ** the transaction journal if it is not there already.
3484 */
danielk197745d68822009-01-16 16:23:38 +00003485 if( !pageInJournal(pPg) && pPager->journalOpen ){
danielk19773460d192008-12-27 15:23:13 +00003486 if( pPg->pgno<=pPager->dbOrigSize ){
danielk1977b3175382008-10-17 18:51:52 +00003487 u32 cksum;
3488 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00003489
danielk1977b3175382008-10-17 18:51:52 +00003490 /* We should never write to the journal file the page that
3491 ** contains the database locks. The following assert verifies
3492 ** that we do not. */
3493 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
3494 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
3495 cksum = pager_cksum(pPager, (u8*)pData2);
3496 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
3497 if( rc==SQLITE_OK ){
3498 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
3499 pPager->journalOff + 4);
3500 pPager->journalOff += pPager->pageSize+4;
3501 }
3502 if( rc==SQLITE_OK ){
3503 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
3504 pPager->journalOff += 4;
3505 }
3506 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
3507 pPager->journalOff, pPager->pageSize));
3508 PAGER_INCR(sqlite3_pager_writej_count);
drh30d53702009-01-06 15:58:57 +00003509 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
danielk1977b3175382008-10-17 18:51:52 +00003510 PAGERID(pPager), pPg->pgno,
drh30d53702009-01-06 15:58:57 +00003511 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
danielk197707cb5602006-01-20 10:55:05 +00003512
danielk1977f3107512008-12-22 10:58:46 +00003513 /* Even if an IO or diskfull error occurred while journalling the
3514 ** page in the block above, set the need-sync flag for the page.
3515 ** Otherwise, when the transaction is rolled back, the logic in
3516 ** playback_one_page() will think that the page needs to be restored
3517 ** in the database file. And if an IO error occurs while doing so,
3518 ** then corruption may follow.
3519 */
3520 if( !pPager->noSync ){
3521 pPg->flags |= PGHDR_NEED_SYNC;
danielk1977a4124bd2008-12-23 10:37:47 +00003522 pPager->needSync = 1;
danielk1977f3107512008-12-22 10:58:46 +00003523 }
3524
danielk1977b3175382008-10-17 18:51:52 +00003525 /* An error has occured writing to the journal file. The
3526 ** transaction will be rolled back by the layer above.
3527 */
3528 if( rc!=SQLITE_OK ){
3529 return rc;
3530 }
danielk197707cb5602006-01-20 10:55:05 +00003531
danielk1977b3175382008-10-17 18:51:52 +00003532 pPager->nRec++;
3533 assert( pPager->pInJournal!=0 );
drh7539b6b2009-01-02 21:39:39 +00003534 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3535 testcase( rc==SQLITE_NOMEM );
3536 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
3537 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
3538 if( rc!=SQLITE_OK ){
3539 assert( rc==SQLITE_NOMEM );
3540 return rc;
3541 }
danielk197713adf8a2004-06-03 16:08:41 +00003542 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003543 if( !pPager->journalStarted && !pPager->noSync ){
3544 pPg->flags |= PGHDR_NEED_SYNC;
danielk1977a4124bd2008-12-23 10:37:47 +00003545 pPager->needSync = 1;
danielk19778c0a7912008-08-20 14:49:23 +00003546 }
drh30d53702009-01-06 15:58:57 +00003547 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
danielk19778c0a7912008-08-20 14:49:23 +00003548 PAGERID(pPager), pPg->pgno,
drh30d53702009-01-06 15:58:57 +00003549 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
danielk1977a0bf2652004-11-04 14:30:04 +00003550 }
danielk1977a0bf2652004-11-04 14:30:04 +00003551 }
3552
3553 /* If the statement journal is open and the page is not in it,
3554 ** then write the current page to the statement journal. Note that
3555 ** the statement journal format differs from the standard journal format
3556 ** in that it omits the checksums and the header.
3557 */
danielk19773460d192008-12-27 15:23:13 +00003558 if( subjRequiresPage(pPg) ){
danielk1977f2c31ad2009-01-06 13:40:08 +00003559 rc = subjournalPage(pPg);
drhd9b02572001-04-15 00:37:09 +00003560 }
drhfa86c412002-02-02 15:01:15 +00003561 }
3562
3563 /* Update the database size and return.
3564 */
drh1aa2d8b2007-01-03 15:34:29 +00003565 assert( pPager->state>=PAGER_SHARED );
danielk1977d92db532008-11-17 04:56:24 +00003566 if( pPager->dbSize<pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003567 pPager->dbSize = pPg->pgno;
danielk1977d92db532008-11-17 04:56:24 +00003568 if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){
drh1f595712004-06-15 01:40:29 +00003569 pPager->dbSize++;
3570 }
drh306dc212001-05-21 13:45:10 +00003571 }
drh69688d52001-04-14 16:38:23 +00003572 return rc;
drhed7c8552001-04-11 14:29:21 +00003573}
3574
3575/*
danielk19774099f6e2007-03-19 11:25:20 +00003576** This function is used to mark a data-page as writable. It uses
3577** pager_write() to open a journal file (if it is not already open)
3578** and write the page *pData to the journal.
3579**
3580** The difference between this function and pager_write() is that this
3581** function also deals with the special case where 2 or more pages
3582** fit on a single disk sector. In this case all co-resident pages
3583** must have been written to the journal file before returning.
3584*/
danielk19773b8a05f2007-03-19 17:44:26 +00003585int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003586 int rc = SQLITE_OK;
3587
danielk19773b8a05f2007-03-19 17:44:26 +00003588 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003589 Pager *pPager = pPg->pPager;
3590 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3591
danielk1977b3175382008-10-17 18:51:52 +00003592 if( nPagePerSector>1 ){
danielk19774099f6e2007-03-19 11:25:20 +00003593 Pgno nPageCount; /* Total number of pages in database file */
3594 Pgno pg1; /* First page of the sector pPg is located on. */
3595 int nPage; /* Number of pages starting at pg1 to journal */
3596 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003597 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003598
3599 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3600 ** header to be written between the pages journaled by this function.
3601 */
danielk1977b3175382008-10-17 18:51:52 +00003602 assert( !MEMDB );
danielk19774099f6e2007-03-19 11:25:20 +00003603 assert( pPager->doNotSync==0 );
3604 pPager->doNotSync = 1;
3605
3606 /* This trick assumes that both the page-size and sector-size are
3607 ** an integer power of 2. It sets variable pg1 to the identifier
3608 ** of the first page of the sector pPg is located on.
3609 */
3610 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3611
danielk1977ad0132d2008-06-07 08:58:22 +00003612 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00003613 if( pPg->pgno>nPageCount ){
3614 nPage = (pPg->pgno - pg1)+1;
3615 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3616 nPage = nPageCount+1-pg1;
3617 }else{
3618 nPage = nPagePerSector;
3619 }
3620 assert(nPage>0);
3621 assert(pg1<=pPg->pgno);
3622 assert((pg1+nPage)>pPg->pgno);
3623
3624 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3625 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003626 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00003627 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00003628 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003629 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003630 if( rc==SQLITE_OK ){
3631 rc = pager_write(pPage);
danielk19778c0a7912008-08-20 14:49:23 +00003632 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003633 needSync = 1;
danielk1977a4124bd2008-12-23 10:37:47 +00003634 assert(pPager->needSync);
danielk1977dd97a492007-08-22 18:54:32 +00003635 }
danielk19773b8a05f2007-03-19 17:44:26 +00003636 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003637 }
3638 }
drhc81945e2008-03-10 14:12:53 +00003639 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk19778c0a7912008-08-20 14:49:23 +00003640 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003641 needSync = 1;
3642 }
danielk19778c0a7912008-08-20 14:49:23 +00003643 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003644 }
3645 }
3646
drhee03d622009-01-07 15:33:45 +00003647 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
danielk1977dd97a492007-08-22 18:54:32 +00003648 ** starting at pg1, then it needs to be set for all of them. Because
3649 ** writing to any of these nPage pages may damage the others, the
3650 ** journal file must contain sync()ed copies of all of them
3651 ** before any of them can be written out to the database file.
3652 */
3653 if( needSync ){
drhb3df2e12008-09-17 20:06:26 +00003654 assert( !MEMDB && pPager->noSync==0 );
danielk1977dd97a492007-08-22 18:54:32 +00003655 for(ii=0; ii<nPage && needSync; ii++){
3656 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
drhee03d622009-01-07 15:33:45 +00003657 if( pPage ){
drhee03d622009-01-07 15:33:45 +00003658 pPage->flags |= PGHDR_NEED_SYNC;
3659 sqlite3PagerUnref(pPage);
3660 }
danielk1977dd97a492007-08-22 18:54:32 +00003661 }
3662 assert(pPager->needSync);
3663 }
3664
danielk19774099f6e2007-03-19 11:25:20 +00003665 assert( pPager->doNotSync==1 );
3666 pPager->doNotSync = 0;
3667 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003668 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003669 }
3670 return rc;
3671}
3672
3673/*
drhaacc5432002-01-06 17:07:40 +00003674** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003675** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003676** to change the content of the page.
3677*/
danielk19777d3a6662006-01-23 16:21:05 +00003678#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003679int sqlite3PagerIswriteable(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003680 return pPg->flags&PGHDR_DIRTY;
drh6019e162001-07-02 17:51:45 +00003681}
danielk19777d3a6662006-01-23 16:21:05 +00003682#endif
drh6019e162001-07-02 17:51:45 +00003683
drh001bbcb2003-03-19 03:14:00 +00003684/*
drh30e58752002-03-02 20:41:57 +00003685** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003686** write the information on page pPg back to the disk, even though
drhdfe88ec2008-11-03 20:55:06 +00003687** that page might be marked as dirty. This happens, for example, when
3688** the page has been added as a leaf of the freelist and so its
3689** content no longer matters.
drh30e58752002-03-02 20:41:57 +00003690**
3691** The overlying software layer calls this routine when all of the data
3692** on the given page is unused. The pager marks the page as clean so
3693** that it does not get written to disk.
3694**
3695** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003696** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003697** of large INSERT operations and quadruple the speed of large DELETEs.
danielk197745d68822009-01-16 16:23:38 +00003698**
3699** When this routine is called, set the bit corresponding to pDbPage in
3700** the Pager.pAlwaysRollback bitvec. Subsequent calls to
3701** sqlite3PagerDontRollback() for the same page will thereafter be ignored.
3702** This is necessary to avoid a problem where a page with data is added to
3703** the freelist during one part of a transaction then removed from the
3704** freelist during a later part of the same transaction and reused for some
3705** other purpose. When it is first added to the freelist, this routine is
3706** called. When reused, the sqlite3PagerDontRollback() routine is called.
3707** But because the page contains critical data, we still need to be sure it
3708** gets rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003709*/
danielk197745d68822009-01-16 16:23:38 +00003710int sqlite3PagerDontWrite(DbPage *pDbPage){
3711 PgHdr *pPg = pDbPage;
drh538f5702007-04-13 02:14:30 +00003712 Pager *pPager = pPg->pPager;
danielk197745d68822009-01-16 16:23:38 +00003713 int rc;
3714
3715 if( pPg->pgno>pPager->dbOrigSize ){
3716 return SQLITE_OK;
drh30e58752002-03-02 20:41:57 +00003717 }
danielk197745d68822009-01-16 16:23:38 +00003718 if( pPager->pAlwaysRollback==0 ){
3719 assert( pPager->pInJournal );
3720 pPager->pAlwaysRollback = sqlite3BitvecCreate(pPager->dbOrigSize);
3721 if( !pPager->pAlwaysRollback ){
3722 return SQLITE_NOMEM;
3723 }
3724 }
3725 rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno);
3726
3727 if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
3728 assert( pPager->state>=PAGER_SHARED );
3729 if( pPager->dbSize==pPg->pgno && pPager->dbOrigSize<pPager->dbSize ){
3730 /* If this pages is the last page in the file and the file has grown
3731 ** during the current transaction, then do NOT mark the page as clean.
3732 ** When the database file grows, we must make sure that the last page
3733 ** gets written at least once so that the disk file will be the correct
3734 ** size. If you do not write this page and the size of the file
3735 ** on the disk ends up being too small, that can lead to database
3736 ** corruption during the next transaction.
3737 */
3738 }else{
3739 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
3740 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
3741 pPg->flags |= PGHDR_DONT_WRITE;
3742#ifdef SQLITE_CHECK_PAGES
3743 pPg->pageHash = pager_pagehash(pPg);
3744#endif
3745 }
3746 }
3747 return rc;
drh30e58752002-03-02 20:41:57 +00003748}
3749
3750/*
danielk197745d68822009-01-16 16:23:38 +00003751** A call to this routine tells the pager that if a rollback occurs,
3752** it is not necessary to restore the data on the given page. This
3753** means that the pager does not have to record the given page in the
3754** rollback journal.
3755**
3756** If we have not yet actually read the content of this page (if
3757** the PgHdr.needRead flag is set) then this routine acts as a promise
3758** that we will never need to read the page content in the future.
3759** so the needRead flag can be cleared at this point.
3760*/
3761void sqlite3PagerDontRollback(DbPage *pPg){
3762 Pager *pPager = pPg->pPager;
3763 TESTONLY( int rc; ) /* Return value from sqlite3BitvecSet() */
3764
3765 assert( pPager->state>=PAGER_RESERVED );
3766
3767 /* If the journal file is not open, or DontWrite() has been called on
3768 ** this page (DontWrite() sets the Pager.pAlwaysRollback bit), then this
3769 ** function is a no-op.
3770 */
3771 if( pPager->journalOpen==0
3772 || sqlite3BitvecTest(pPager->pAlwaysRollback, pPg->pgno)
3773 || pPg->pgno>pPager->dbOrigSize
3774 ){
3775 return;
3776 }
3777
3778#ifdef SQLITE_SECURE_DELETE
3779 if( sqlite3BitvecTest(pPager->pInJournal, pPg->pgno)!=0
3780 || pPg->pgno>pPager->dbOrigSize ){
3781 return;
3782 }
3783#endif
3784
3785 /* If SECURE_DELETE is disabled, then there is no way that this
3786 ** routine can be called on a page for which sqlite3PagerDontWrite()
3787 ** has not been previously called during the same transaction.
3788 ** And if DontWrite() has previously been called, the following
3789 ** conditions must be met.
3790 **
3791 ** (Later:) Not true. If the database is corrupted by having duplicate
3792 ** pages on the freelist (ex: corrupt9.test) then the following is not
3793 ** necessarily true:
3794 */
3795 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->dbOrigSize ); */
3796
3797 assert( pPager->pInJournal!=0 );
3798 pPg->flags &= ~PGHDR_NEED_READ;
3799
3800 /* Failure to set the bits in the InJournal bit-vectors is benign.
3801 ** It merely means that we might do some extra work to journal a page
3802 ** that does not need to be journaled. Nevertheless, be sure to test the
3803 ** case where a malloc error occurs while trying to set a bit in a
3804 ** bit vector.
3805 */
3806 sqlite3BeginBenignMalloc();
3807 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3808 testcase( rc==SQLITE_NOMEM );
3809 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pPg->pgno);
3810 testcase( rc==SQLITE_NOMEM );
3811 sqlite3EndBenignMalloc();
3812
3813
3814 PAGERTRACE(("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)));
3815 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
3816}
3817
3818
3819/*
drh80e35f42007-03-30 14:06:34 +00003820** This routine is called to increment the database file change-counter,
3821** stored at byte 24 of the pager file.
3822*/
danielk1977c7b60172007-08-22 11:22:03 +00003823static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00003824 PgHdr *pPgHdr;
3825 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00003826 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00003827
drh701bb3b2008-08-02 03:50:39 +00003828#ifndef SQLITE_ENABLE_ATOMIC_WRITE
3829 assert( isDirect==0 ); /* isDirect is only true for atomic writes */
3830#endif
danielk197712dd5492008-12-18 15:45:07 +00003831 if( !pPager->changeCountDone && pPager->dbSize>0 ){
drh80e35f42007-03-30 14:06:34 +00003832 /* Open page 1 of the file for writing. */
3833 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3834 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00003835
3836 if( !isDirect ){
3837 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00003838 if( rc!=SQLITE_OK ){
3839 sqlite3PagerUnref(pPgHdr);
3840 return rc;
3841 }
danielk1977c7b60172007-08-22 11:22:03 +00003842 }
3843
drh80e35f42007-03-30 14:06:34 +00003844 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00003845 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00003846 change_counter++;
danielk19778c0a7912008-08-20 14:49:23 +00003847 put32bits(((char*)pPgHdr->pData)+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00003848
drh701bb3b2008-08-02 03:50:39 +00003849#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk197745d68822009-01-16 16:23:38 +00003850 if( isDirect && pPager->fd->pMethods ){
danielk19778c0a7912008-08-20 14:49:23 +00003851 const void *zBuf = pPgHdr->pData;
danielk19773460d192008-12-27 15:23:13 +00003852 assert( pPager->dbFileSize>0 );
danielk1977c7b60172007-08-22 11:22:03 +00003853 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
3854 }
drh701bb3b2008-08-02 03:50:39 +00003855#endif
danielk1977c7b60172007-08-22 11:22:03 +00003856
drh80e35f42007-03-30 14:06:34 +00003857 /* Release the page reference. */
3858 sqlite3PagerUnref(pPgHdr);
3859 pPager->changeCountDone = 1;
3860 }
danielk1977c7b60172007-08-22 11:22:03 +00003861 return rc;
drh80e35f42007-03-30 14:06:34 +00003862}
3863
3864/*
danielk1977f653d782008-03-20 11:04:21 +00003865** Sync the pager file to disk.
3866*/
3867int sqlite3PagerSync(Pager *pPager){
3868 int rc;
drh7426f862008-08-26 21:07:26 +00003869 if( MEMDB ){
3870 rc = SQLITE_OK;
3871 }else{
3872 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3873 }
danielk1977f653d782008-03-20 11:04:21 +00003874 return rc;
3875}
3876
3877/*
drh80e35f42007-03-30 14:06:34 +00003878** Sync the database file for the pager pPager. zMaster points to the name
3879** of a master journal file that should be written into the individual
3880** journal file. zMaster may be NULL, which is interpreted as no master
3881** journal (a single database transaction).
3882**
3883** This routine ensures that the journal is synced, all dirty pages written
3884** to the database file and the database file synced. The only thing that
3885** remains to commit the transaction is to delete the journal file (or
3886** master journal file if specified).
3887**
3888** Note that if zMaster==NULL, this does not overwrite a previous value
3889** passed to an sqlite3PagerCommitPhaseOne() call.
3890**
danielk1977f653d782008-03-20 11:04:21 +00003891** If the final parameter - noSync - is true, then the database file itself
3892** is not synced. The caller must call sqlite3PagerSync() directly to
3893** sync the database file before calling CommitPhaseTwo() to delete the
3894** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00003895*/
danielk1977f653d782008-03-20 11:04:21 +00003896int sqlite3PagerCommitPhaseOne(
3897 Pager *pPager,
3898 const char *zMaster,
danielk1977f653d782008-03-20 11:04:21 +00003899 int noSync
3900){
drh80e35f42007-03-30 14:06:34 +00003901 int rc = SQLITE_OK;
3902
danielk1977dad31b52008-05-15 11:08:07 +00003903 if( pPager->errCode ){
3904 return pPager->errCode;
3905 }
3906
drhd138c012008-05-13 00:58:18 +00003907 /* If no changes have been made, we can leave the transaction early.
3908 */
3909 if( pPager->dbModified==0 &&
3910 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
danielk197745d68822009-01-16 16:23:38 +00003911 pPager->exclusiveMode!=0) ){
3912 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
drhd138c012008-05-13 00:58:18 +00003913 return SQLITE_OK;
3914 }
3915
drh30d53702009-01-06 15:58:57 +00003916 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
3917 pPager->zFilename, zMaster, pPager->dbSize));
drh80e35f42007-03-30 14:06:34 +00003918
3919 /* If this is an in-memory db, or no pages have been written to, or this
3920 ** function has already been called, it is a no-op.
3921 */
danielk197745d68822009-01-16 16:23:38 +00003922 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
drh80e35f42007-03-30 14:06:34 +00003923 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00003924
danielk1977c7b60172007-08-22 11:22:03 +00003925#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3926 /* The atomic-write optimization can be used if all of the
3927 ** following are true:
3928 **
3929 ** + The file-system supports the atomic-write property for
3930 ** blocks of size page-size, and
3931 ** + This commit is not part of a multi-file transaction, and
3932 ** + Exactly one page has been modified and store in the journal file.
3933 **
3934 ** If the optimization can be used, then the journal file will never
3935 ** be created for this transaction.
3936 */
danielk19774d60af92008-09-08 15:35:06 +00003937 int useAtomicWrite;
danielk19778c0a7912008-08-20 14:49:23 +00003938 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
danielk19774d60af92008-09-08 15:35:06 +00003939 useAtomicWrite = (
danielk1977f55b8992007-08-24 08:15:53 +00003940 !zMaster &&
danielk197745d68822009-01-16 16:23:38 +00003941 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00003942 pPager->journalOff==jrnlBufferSize(pPager) &&
danielk19773460d192008-12-27 15:23:13 +00003943 pPager->dbSize>=pPager->dbFileSize &&
danielk19778c0a7912008-08-20 14:49:23 +00003944 (pPg==0 || pPg->pDirty==0)
danielk1977f55b8992007-08-24 08:15:53 +00003945 );
danielk197745d68822009-01-16 16:23:38 +00003946 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00003947 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00003948 /* Update the nRec field in the journal file. */
3949 int offset = pPager->journalHdr + sizeof(aJournalMagic);
3950 assert(pPager->nRec==1);
3951 rc = write32bits(pPager->jfd, offset, pPager->nRec);
3952
3953 /* Update the db file change counter. The following call will modify
3954 ** the in-memory representation of page 1 to include the updated
3955 ** change counter and then write page 1 directly to the database
3956 ** file. Because of the atomic-write property of the host file-system,
3957 ** this is safe.
3958 */
danielk1977ae72d982007-10-03 08:46:44 +00003959 if( rc==SQLITE_OK ){
3960 rc = pager_incr_changecounter(pPager, 1);
3961 }
danielk1977f55b8992007-08-24 08:15:53 +00003962 }else{
3963 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00003964 }
3965
danielk1977ae72d982007-10-03 08:46:44 +00003966 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00003967#endif
3968
drh80e35f42007-03-30 14:06:34 +00003969 /* If a master journal file name has already been written to the
3970 ** journal file, then no sync is required. This happens when it is
3971 ** written, then the process fails to upgrade from a RESERVED to an
3972 ** EXCLUSIVE lock. The next time the process tries to commit the
3973 ** transaction the m-j name will have already been written.
3974 */
3975 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00003976 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00003977 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00003978 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00003979#ifndef SQLITE_OMIT_AUTOVACUUM
danielk19773460d192008-12-27 15:23:13 +00003980 if( pPager->dbSize<pPager->dbOrigSize ){
danielk197771aa7ff2008-05-20 07:05:09 +00003981 /* If this transaction has made the database smaller, then all pages
3982 ** being discarded by the truncation must be written to the journal
3983 ** file.
3984 */
3985 Pgno i;
danielk1977d92db532008-11-17 04:56:24 +00003986 Pgno iSkip = PAGER_MJ_PGNO(pPager);
danielk19773460d192008-12-27 15:23:13 +00003987 Pgno dbSize = pPager->dbSize;
danielk1977f9bce3c2009-01-06 15:20:58 +00003988 pPager->dbSize = pPager->dbOrigSize;
danielk1977f70c1fe2009-01-07 18:08:48 +00003989 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
danielk197771aa7ff2008-05-20 07:05:09 +00003990 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
3991 rc = sqlite3PagerGet(pPager, i, &pPg);
3992 if( rc!=SQLITE_OK ) goto sync_exit;
3993 rc = sqlite3PagerWrite(pPg);
3994 sqlite3PagerUnref(pPg);
3995 if( rc!=SQLITE_OK ) goto sync_exit;
3996 }
3997 }
danielk19773460d192008-12-27 15:23:13 +00003998 pPager->dbSize = dbSize;
danielk197771aa7ff2008-05-20 07:05:09 +00003999 }
drh80e35f42007-03-30 14:06:34 +00004000#endif
danielk197771aa7ff2008-05-20 07:05:09 +00004001 rc = writeMasterJournal(pPager, zMaster);
4002 if( rc!=SQLITE_OK ) goto sync_exit;
4003 rc = syncJournal(pPager);
4004 }
drh80e35f42007-03-30 14:06:34 +00004005 }
danielk1977c7b60172007-08-22 11:22:03 +00004006 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004007
drh80e35f42007-03-30 14:06:34 +00004008 /* Write all dirty pages to the database file */
danielk19778c0a7912008-08-20 14:49:23 +00004009 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00004010 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004011 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004012 assert( rc!=SQLITE_IOERR_BLOCKED );
4013 /* The error might have left the dirty list all fouled up here,
4014 ** but that does not matter because if the if the dirty list did
4015 ** get corrupted, then the transaction will roll back and
4016 ** discard the dirty list. There is an assert in
4017 ** pager_get_all_dirty_pages() that verifies that no attempt
4018 ** is made to use an invalid dirty list.
4019 */
drh153c62c2007-08-24 03:51:33 +00004020 goto sync_exit;
4021 }
danielk19778c0a7912008-08-20 14:49:23 +00004022 sqlite3PcacheCleanAll(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00004023
danielk197745d68822009-01-16 16:23:38 +00004024 if( pPager->dbSize<pPager->dbFileSize ){
danielk1977f90b7262009-01-07 15:18:20 +00004025 assert( pPager->state>=PAGER_EXCLUSIVE );
danielk197745d68822009-01-16 16:23:38 +00004026 rc = pager_truncate(pPager, pPager->dbSize);
danielk1977f90b7262009-01-07 15:18:20 +00004027 if( rc!=SQLITE_OK ) goto sync_exit;
4028 }
4029
drh80e35f42007-03-30 14:06:34 +00004030 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004031 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004032 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004033 }
4034 IOTRACE(("DBSYNC %p\n", pPager))
4035
4036 pPager->state = PAGER_SYNCED;
drh80e35f42007-03-30 14:06:34 +00004037 }
4038
4039sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004040 if( rc==SQLITE_IOERR_BLOCKED ){
4041 /* pager_incr_changecounter() may attempt to obtain an exclusive
4042 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004043 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004044 * better to return SQLITE_BUSY.
4045 */
4046 rc = SQLITE_BUSY;
4047 }
drh80e35f42007-03-30 14:06:34 +00004048 return rc;
4049}
4050
4051
4052/*
drhed7c8552001-04-11 14:29:21 +00004053** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004054**
4055** If the commit fails for any reason, a rollback attempt is made
4056** and an error code is returned. If the commit worked, SQLITE_OK
4057** is returned.
drhed7c8552001-04-11 14:29:21 +00004058*/
drh80e35f42007-03-30 14:06:34 +00004059int sqlite3PagerCommitPhaseTwo(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004060 int rc = SQLITE_OK;
drhd9b02572001-04-15 00:37:09 +00004061
danielk1977efaaf572006-01-16 11:29:19 +00004062 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004063 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004064 }
drha6abd042004-06-09 17:37:22 +00004065 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004066 return SQLITE_ERROR;
4067 }
drhd138c012008-05-13 00:58:18 +00004068 if( pPager->dbModified==0 &&
4069 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
4070 pPager->exclusiveMode!=0) ){
danielk197745d68822009-01-16 16:23:38 +00004071 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
drhd138c012008-05-13 00:58:18 +00004072 return SQLITE_OK;
4073 }
drh30d53702009-01-06 15:58:57 +00004074 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
danielk197745d68822009-01-16 16:23:38 +00004075 assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dirtyCache );
danielk1977b3175382008-10-17 18:51:52 +00004076 rc = pager_end_transaction(pPager, pPager->setMaster);
4077 rc = pager_error(pPager, rc);
drh86f8c192007-08-22 00:39:19 +00004078 return rc;
drhed7c8552001-04-11 14:29:21 +00004079}
4080
4081/*
drha6abd042004-06-09 17:37:22 +00004082** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004083** All in-memory cache pages revert to their original data contents.
4084** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004085**
4086** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004087** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004088** process is writing trash into the journal file (SQLITE_CORRUPT) or
4089** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4090** codes are returned for all these occasions. Otherwise,
4091** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004092*/
danielk19773b8a05f2007-03-19 17:44:26 +00004093int sqlite3PagerRollback(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004094 int rc = SQLITE_OK;
drh30d53702009-01-06 15:58:57 +00004095 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
danielk197745d68822009-01-16 16:23:38 +00004096 if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00004097 rc = pager_end_transaction(pPager, pPager->setMaster);
danielk19778c0a7912008-08-20 14:49:23 +00004098 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004099 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004100 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004101 }
danielk19778c0a7912008-08-20 14:49:23 +00004102 rc = pPager->errCode;
drha6abd042004-06-09 17:37:22 +00004103 }else{
danielk19778c0a7912008-08-20 14:49:23 +00004104 if( pPager->state==PAGER_RESERVED ){
4105 int rc2;
4106 rc = pager_playback(pPager, 0);
4107 rc2 = pager_end_transaction(pPager, pPager->setMaster);
4108 if( rc==SQLITE_OK ){
4109 rc = rc2;
4110 }
4111 }else{
4112 rc = pager_playback(pPager, 0);
4113 }
danielk197707cb5602006-01-20 10:55:05 +00004114
danielk1977b3175382008-10-17 18:51:52 +00004115 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00004116 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00004117 }
danielk19778c0a7912008-08-20 14:49:23 +00004118
4119 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4120 ** cache. So call pager_error() on the way out to make any error
4121 ** persistent.
4122 */
4123 rc = pager_error(pPager, rc);
4124 }
drh86f8c192007-08-22 00:39:19 +00004125 return rc;
drh98808ba2001-10-18 12:34:46 +00004126}
drhd9b02572001-04-15 00:37:09 +00004127
4128/*
drh5e00f6c2001-09-13 13:46:56 +00004129** Return TRUE if the database file is opened read-only. Return FALSE
4130** if the database is (in theory) writable.
4131*/
drhf49661a2008-12-10 16:45:50 +00004132u8 sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004133 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004134}
4135
4136/*
drh0f7eb612006-08-08 13:51:43 +00004137** Return the number of references to the pager.
4138*/
danielk19773b8a05f2007-03-19 17:44:26 +00004139int sqlite3PagerRefcount(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004140 return sqlite3PcacheRefCount(pPager->pPCache);
drh0f7eb612006-08-08 13:51:43 +00004141}
4142
danielk197771d5d2c2008-09-29 11:49:47 +00004143/*
4144** Return the number of references to the specified page.
4145*/
4146int sqlite3PagerPageRefcount(DbPage *pPage){
4147 return sqlite3PcachePageRefcount(pPage);
4148}
4149
drh0f7eb612006-08-08 13:51:43 +00004150#ifdef SQLITE_TEST
4151/*
drhd9b02572001-04-15 00:37:09 +00004152** This routine is used for testing and analysis only.
4153*/
danielk19773b8a05f2007-03-19 17:44:26 +00004154int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004155 static int a[11];
danielk19778c0a7912008-08-20 14:49:23 +00004156 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
4157 a[1] = sqlite3PcachePagecount(pPager->pPCache);
4158 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
danielk1977d92db532008-11-17 04:56:24 +00004159 a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
drhd9b02572001-04-15 00:37:09 +00004160 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004161 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004162 a[6] = pPager->nHit;
4163 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004164 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004165 a[9] = pPager->nRead;
4166 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004167 return a;
4168}
danielk197717b90b52008-06-06 11:11:25 +00004169int sqlite3PagerIsMemdb(Pager *pPager){
4170 return MEMDB;
4171}
drh0f7eb612006-08-08 13:51:43 +00004172#endif
drhdd793422001-06-28 01:54:48 +00004173
drhfa86c412002-02-02 15:01:15 +00004174/*
danielk1977fd7f0452008-12-17 17:30:26 +00004175** Ensure that there are at least nSavepoint savepoints open.
drhfa86c412002-02-02 15:01:15 +00004176*/
danielk1977fd7f0452008-12-17 17:30:26 +00004177int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
4178 int rc = SQLITE_OK;
4179
danielk197712dd5492008-12-18 15:45:07 +00004180 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
danielk1977fd7f0452008-12-17 17:30:26 +00004181 int ii;
drh49b9d332009-01-02 18:10:42 +00004182 PagerSavepoint *aNew;
danielk1977fd7f0452008-12-17 17:30:26 +00004183
drhc0731c92009-01-11 17:00:02 +00004184 /* Either there is no active journal or the sub-journal is open or
4185 ** the journal is always stored in memory */
danielk197745d68822009-01-16 16:23:38 +00004186 assert( pPager->nSavepoint==0 || pPager->sjfd->pMethods ||
drhc0731c92009-01-11 17:00:02 +00004187 pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
danielk1977fd7f0452008-12-17 17:30:26 +00004188
4189 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
4190 ** if the allocation fails. Otherwise, zero the new portion in case a
4191 ** malloc failure occurs while populating it in the for(...) loop below.
4192 */
drh49b9d332009-01-02 18:10:42 +00004193 aNew = (PagerSavepoint *)sqlite3Realloc(
danielk1977fd7f0452008-12-17 17:30:26 +00004194 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
4195 );
4196 if( !aNew ){
4197 return SQLITE_NOMEM;
4198 }
4199 memset(&aNew[pPager->nSavepoint], 0,
4200 (nSavepoint - pPager->nSavepoint) * sizeof(PagerSavepoint)
4201 );
4202 pPager->aSavepoint = aNew;
4203 ii = pPager->nSavepoint;
4204 pPager->nSavepoint = nSavepoint;
4205
4206 /* Populate the PagerSavepoint structures just allocated. */
4207 for(/* no-op */; ii<nSavepoint; ii++){
danielk197712dd5492008-12-18 15:45:07 +00004208 assert( pPager->dbSizeValid );
danielk1977fd7f0452008-12-17 17:30:26 +00004209 aNew[ii].nOrig = pPager->dbSize;
danielk197745d68822009-01-16 16:23:38 +00004210 if( pPager->journalOpen && pPager->journalOff>0 ){
danielk197767ddef62008-12-23 19:15:56 +00004211 aNew[ii].iOffset = pPager->journalOff;
4212 }else{
4213 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
4214 }
danielk197745d68822009-01-16 16:23:38 +00004215 aNew[ii].iSubRec = pPager->stmtNRec;
danielk1977fd7f0452008-12-17 17:30:26 +00004216 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
4217 if( !aNew[ii].pInSavepoint ){
4218 return SQLITE_NOMEM;
danielk1977b3175382008-10-17 18:51:52 +00004219 }
drh334b2992007-09-06 23:28:23 +00004220 }
danielk1977fd7f0452008-12-17 17:30:26 +00004221
4222 /* Open the sub-journal, if it is not already opened. */
4223 rc = openSubJournal(pPager);
drh0f892532002-05-30 12:27:03 +00004224 }
danielk1977fd7f0452008-12-17 17:30:26 +00004225
drh86f8c192007-08-22 00:39:19 +00004226 return rc;
4227}
drhfa86c412002-02-02 15:01:15 +00004228
4229/*
danielk1977fd7f0452008-12-17 17:30:26 +00004230** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
4231** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
4232** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
4233** that have occured since savepoint iSavepoint was created.
4234**
4235** In either case, all savepoints with an index greater than iSavepoint
4236** are destroyed.
4237**
4238** If there are less than (iSavepoint+1) active savepoints when this
4239** function is called it is a no-op.
4240*/
4241int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
4242 int rc = SQLITE_OK;
4243
4244 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
4245
4246 if( iSavepoint<pPager->nSavepoint ){
4247 int ii;
4248 int nNew = iSavepoint + (op==SAVEPOINT_ROLLBACK);
4249 for(ii=nNew; ii<pPager->nSavepoint; ii++){
4250 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
4251 }
4252 pPager->nSavepoint = nNew;
4253
danielk197745d68822009-01-16 16:23:38 +00004254 if( op==SAVEPOINT_ROLLBACK && pPager->jfd->pMethods ){
danielk1977f3107512008-12-22 10:58:46 +00004255 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
danielk1977fd7f0452008-12-17 17:30:26 +00004256 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
4257 assert(rc!=SQLITE_DONE);
4258 }
danielk1977f3107512008-12-22 10:58:46 +00004259
danielk1977fd7f0452008-12-17 17:30:26 +00004260 /* If this is a release of the outermost savepoint, truncate
4261 ** the sub-journal. */
danielk197745d68822009-01-16 16:23:38 +00004262 if( nNew==0 && op==SAVEPOINT_RELEASE && pPager->sjfd->pMethods ){
danielk1977fd7f0452008-12-17 17:30:26 +00004263 assert( rc==SQLITE_OK );
4264 rc = sqlite3OsTruncate(pPager->sjfd, 0);
danielk197745d68822009-01-16 16:23:38 +00004265 pPager->stmtNRec = 0;
danielk1977b3175382008-10-17 18:51:52 +00004266 }
drh663fc632002-02-02 18:49:19 +00004267 }
drhfa86c412002-02-02 15:01:15 +00004268 return rc;
4269}
4270
drh73509ee2003-04-06 20:44:45 +00004271/*
4272** Return the full pathname of the database file.
4273*/
danielk19773b8a05f2007-03-19 17:44:26 +00004274const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004275 return pPager->zFilename;
4276}
4277
drhb20ea9d2004-02-09 01:20:36 +00004278/*
drhd0679ed2007-08-28 22:24:34 +00004279** Return the VFS structure for the pager.
4280*/
4281const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4282 return pPager->pVfs;
4283}
4284
4285/*
drhcc6bb3e2007-08-31 16:11:35 +00004286** Return the file handle for the database file associated
4287** with the pager. This might return NULL if the file has
4288** not yet been opened.
4289*/
4290sqlite3_file *sqlite3PagerFile(Pager *pPager){
4291 return pPager->fd;
4292}
4293
4294/*
danielk197745d68822009-01-16 16:23:38 +00004295** Return the directory of the database file.
4296*/
4297const char *sqlite3PagerDirname(Pager *pPager){
4298 return pPager->zDirectory;
4299}
4300
4301/*
danielk19775865e3d2004-06-14 06:03:57 +00004302** Return the full pathname of the journal file.
4303*/
danielk19773b8a05f2007-03-19 17:44:26 +00004304const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004305 return pPager->zJournal;
4306}
4307
4308/*
drh2c8997b2005-08-27 16:36:48 +00004309** Return true if fsync() calls are disabled for this pager. Return FALSE
4310** if fsync()s are executed normally.
4311*/
danielk19773b8a05f2007-03-19 17:44:26 +00004312int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004313 return pPager->noSync;
4314}
4315
drh7c4ac0c2007-04-05 11:25:58 +00004316#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004317/*
drhb20ea9d2004-02-09 01:20:36 +00004318** Set the codec for this pager
4319*/
danielk19773b8a05f2007-03-19 17:44:26 +00004320void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004321 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004322 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004323 void *pCodecArg
4324){
4325 pPager->xCodec = xCodec;
4326 pPager->pCodecArg = pCodecArg;
4327}
drh7c4ac0c2007-04-05 11:25:58 +00004328#endif
drhb20ea9d2004-02-09 01:20:36 +00004329
danielk1977687566d2004-11-02 12:56:41 +00004330#ifndef SQLITE_OMIT_AUTOVACUUM
4331/*
danielk197787c29a92008-01-18 11:33:16 +00004332** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004333**
drh5e385312007-06-16 04:42:12 +00004334** There must be no references to the page previously located at
4335** pgno (which we call pPgOld) though that page is allowed to be
drhb3df2e12008-09-17 20:06:26 +00004336** in cache. If the page previously located at pgno is not already
drh5e385312007-06-16 04:42:12 +00004337** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004338**
drh5e385312007-06-16 04:42:12 +00004339** References to the page pPg remain valid. Updating any
4340** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004341** allocated along with the page) is the responsibility of the caller.
4342**
danielk19775fd057a2005-03-09 13:09:43 +00004343** A transaction must be active when this routine is called. It used to be
4344** required that a statement transaction was not active, but this restriction
4345** has been removed (CREATE INDEX needs to move a page when a statement
4346** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00004347**
4348** If the fourth argument, isCommit, is non-zero, then this page is being
4349** moved as part of a database reorganization just before the transaction
4350** is being committed. In this case, it is guaranteed that the database page
4351** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00004352*/
danielk19774c999992008-07-16 18:17:55 +00004353int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00004354 PgHdr *pPgOld; /* The page being overwritten. */
danielk197794daf7f2004-11-08 09:26:09 +00004355 Pgno needSyncPgno = 0;
danielk19771fab7b62009-01-07 10:35:18 +00004356 int rc;
danielk1977687566d2004-11-02 12:56:41 +00004357
danielk1977687566d2004-11-02 12:56:41 +00004358 assert( pPg->nRef>0 );
4359
danielk19771fab7b62009-01-07 10:35:18 +00004360 /* If the page being moved is dirty and has not been saved by the latest
4361 ** savepoint, then save the current contents of the page into the
4362 ** sub-journal now. This is required to handle the following scenario:
4363 **
4364 ** BEGIN;
4365 ** <journal page X, then modify it in memory>
4366 ** SAVEPOINT one;
4367 ** <Move page X to location Y>
4368 ** ROLLBACK TO one;
4369 **
4370 ** If page X were not written to the sub-journal here, it would not
4371 ** be possible to restore its contents when the "ROLLBACK TO one"
4372 ** statement were processed.
4373 */
4374 if( pPg->flags&PGHDR_DIRTY
4375 && subjRequiresPage(pPg)
4376 && SQLITE_OK!=(rc = subjournalPage(pPg))
4377 ){
4378 return rc;
4379 }
4380
drh30d53702009-01-06 15:58:57 +00004381 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
4382 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
drhb0603412007-02-28 04:47:26 +00004383 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004384
danielk197745d68822009-01-16 16:23:38 +00004385 pager_get_content(pPg);
4386
danielk19774c999992008-07-16 18:17:55 +00004387 /* If the journal needs to be sync()ed before page pPg->pgno can
4388 ** be written to, store pPg->pgno in local variable needSyncPgno.
4389 **
4390 ** If the isCommit flag is set, there is no need to remember that
4391 ** the journal needs to be sync()ed before database page pPg->pgno
4392 ** can be written to. The caller has already promised not to write to it.
4393 */
danielk19778c0a7912008-08-20 14:49:23 +00004394 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00004395 needSyncPgno = pPg->pgno;
danielk19773460d192008-12-27 15:23:13 +00004396 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
danielk19778c0a7912008-08-20 14:49:23 +00004397 assert( pPg->flags&PGHDR_DIRTY );
danielk1977ae825582004-11-23 09:06:55 +00004398 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004399 }
4400
danielk1977ef73ee92004-11-06 12:26:07 +00004401 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00004402 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00004403 ** page pgno before the 'move' operation, it needs to be retained
4404 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004405 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00004406 pPg->flags &= ~PGHDR_NEED_SYNC;
danielk1977687566d2004-11-02 12:56:41 +00004407 pPgOld = pager_lookup(pPager, pgno);
danielk19778c0a7912008-08-20 14:49:23 +00004408 assert( !pPgOld || pPgOld->nRef==1 );
danielk1977687566d2004-11-02 12:56:41 +00004409 if( pPgOld ){
danielk19778c0a7912008-08-20 14:49:23 +00004410 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
drh5e385312007-06-16 04:42:12 +00004411 }
danielk1977687566d2004-11-02 12:56:41 +00004412
danielk19778c0a7912008-08-20 14:49:23 +00004413 sqlite3PcacheMove(pPg, pgno);
4414 if( pPgOld ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00004415 sqlite3PcacheDrop(pPgOld);
danielk1977f5fdda82004-11-03 08:44:05 +00004416 }
danielk1977f5fdda82004-11-03 08:44:05 +00004417
drhc047b9f2008-11-21 03:23:43 +00004418 sqlite3PcacheMakeDirty(pPg);
danielk197745d68822009-01-16 16:23:38 +00004419 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004420 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00004421
danielk197794daf7f2004-11-08 09:26:09 +00004422 if( needSyncPgno ){
4423 /* If needSyncPgno is non-zero, then the journal file needs to be
4424 ** sync()ed before any data is written to database file page needSyncPgno.
4425 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00004426 ** "is journaled" bitvec flag has been set. This needs to be remedied by
4427 ** loading the page into the pager-cache and setting the PgHdr.needSync
4428 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00004429 **
danielk1977a98d7b42008-01-18 13:42:54 +00004430 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00004431 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00004432 ** array. Otherwise, if the page is loaded and written again in
4433 ** this transaction, it may be written to the database file before
4434 ** it is synced into the journal file. This way, it may end up in
4435 ** the journal file twice, but that is not a problem.
4436 **
danielk19773b8a05f2007-03-19 17:44:26 +00004437 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004438 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004439 */
danielk19773b8a05f2007-03-19 17:44:26 +00004440 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004441 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004442 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00004443 if( rc!=SQLITE_OK ){
danielk19773460d192008-12-27 15:23:13 +00004444 if( pPager->pInJournal && needSyncPgno<=pPager->dbOrigSize ){
drhf5e7bb52008-02-18 14:47:33 +00004445 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00004446 }
danielk197787c29a92008-01-18 11:33:16 +00004447 return rc;
4448 }
danielk1977ae825582004-11-23 09:06:55 +00004449 pPager->needSync = 1;
drhb3df2e12008-09-17 20:06:26 +00004450 assert( pPager->noSync==0 && !MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00004451 pPgHdr->flags |= PGHDR_NEED_SYNC;
drhc047b9f2008-11-21 03:23:43 +00004452 sqlite3PcacheMakeDirty(pPgHdr);
danielk19773b8a05f2007-03-19 17:44:26 +00004453 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004454 }
4455
danielk1977687566d2004-11-02 12:56:41 +00004456 return SQLITE_OK;
4457}
4458#endif
4459
danielk19773b8a05f2007-03-19 17:44:26 +00004460/*
4461** Return a pointer to the data for the specified page.
4462*/
4463void *sqlite3PagerGetData(DbPage *pPg){
danielk197771d5d2c2008-09-29 11:49:47 +00004464 assert( pPg->nRef>0 || pPg->pPager->memDb );
danielk19778c0a7912008-08-20 14:49:23 +00004465 return pPg->pData;
danielk19773b8a05f2007-03-19 17:44:26 +00004466}
4467
4468/*
4469** Return a pointer to the Pager.nExtra bytes of "extra" space
4470** allocated along with the specified page.
4471*/
4472void *sqlite3PagerGetExtra(DbPage *pPg){
4473 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00004474 return (pPager?pPg->pExtra:0);
danielk19773b8a05f2007-03-19 17:44:26 +00004475}
4476
danielk197741483462007-03-24 16:45:04 +00004477/*
4478** Get/set the locking-mode for this pager. Parameter eMode must be one
4479** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4480** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4481** the locking-mode is set to the value specified.
4482**
4483** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4484** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4485** locking-mode.
4486*/
4487int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004488 assert( eMode==PAGER_LOCKINGMODE_QUERY
4489 || eMode==PAGER_LOCKINGMODE_NORMAL
4490 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4491 assert( PAGER_LOCKINGMODE_QUERY<0 );
4492 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4493 if( eMode>=0 && !pPager->tempFile ){
drh1bd10f82008-12-10 21:19:56 +00004494 pPager->exclusiveMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +00004495 }
4496 return (int)pPager->exclusiveMode;
4497}
4498
drh3b020132008-04-17 17:02:01 +00004499/*
drh04335882008-09-26 21:08:08 +00004500** Get/set the journal-mode for this pager. Parameter eMode must be one of:
drh3b020132008-04-17 17:02:01 +00004501**
drh04335882008-09-26 21:08:08 +00004502** PAGER_JOURNALMODE_QUERY
4503** PAGER_JOURNALMODE_DELETE
4504** PAGER_JOURNALMODE_TRUNCATE
4505** PAGER_JOURNALMODE_PERSIST
4506** PAGER_JOURNALMODE_OFF
4507**
4508** If the parameter is not _QUERY, then the journal-mode is set to the
4509** value specified.
4510**
danielk197745d68822009-01-16 16:23:38 +00004511** The returned indicate the current (possibly updated)
4512** journal-mode.
drh3b020132008-04-17 17:02:01 +00004513*/
4514int sqlite3PagerJournalMode(Pager *pPager, int eMode){
danielk1977b3175382008-10-17 18:51:52 +00004515 if( !MEMDB ){
4516 assert( eMode==PAGER_JOURNALMODE_QUERY
4517 || eMode==PAGER_JOURNALMODE_DELETE
4518 || eMode==PAGER_JOURNALMODE_TRUNCATE
4519 || eMode==PAGER_JOURNALMODE_PERSIST
4520 || eMode==PAGER_JOURNALMODE_OFF
4521 || eMode==PAGER_JOURNALMODE_MEMORY );
4522 assert( PAGER_JOURNALMODE_QUERY<0 );
4523 if( eMode>=0 ){
drh1bd10f82008-12-10 21:19:56 +00004524 pPager->journalMode = (u8)eMode;
danielk1977b3175382008-10-17 18:51:52 +00004525 }else{
4526 assert( eMode==PAGER_JOURNALMODE_QUERY );
4527 }
drh3b020132008-04-17 17:02:01 +00004528 }
drhfdc40e92008-04-17 20:59:37 +00004529 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00004530}
4531
danielk1977b53e4962008-06-04 06:45:59 +00004532/*
4533** Get/set the size-limit used for persistent journal files.
4534*/
4535i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
4536 if( iLimit>=-1 ){
4537 pPager->journalSizeLimit = iLimit;
4538 }
4539 return pPager->journalSizeLimit;
4540}
4541
drh2e66f0b2005-04-28 17:18:48 +00004542#endif /* SQLITE_OMIT_DISKIO */