blob: baae60ec225e40b01a9869130ed789e0b5b416e4 [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**
danielk19777cbd5892009-01-10 16:15:09 +000021** @(#) $Id: pager.c,v 1.545 2009/01/10 16:15:09 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**
drh4f0ee682007-03-30 20:43:40 +0000152** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000153** 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,
danielk19773b8a05f2007-03-19 17:44:26 +0000157** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000158** APIs, they may still be used successfully.
danielk19773460d192008-12-27 15:23:13 +0000159**
160** 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 */
drh603240c2002-03-05 01:11:12 +0000174 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000175 u8 journalStarted; /* True if header of journal is synced */
176 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 */
drha6abd042004-06-09 17:37:22 +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 */
184 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000185 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000186 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000187 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000188 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000189 u8 doNotSync; /* Boolean. While true, do not spill the cache */
190 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000191 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drhd138c012008-05-13 00:58:18 +0000192 u8 dbModified; /* True if there are any changes to the Db */
drh80e35f42007-03-30 14:06:34 +0000193 u8 changeCountDone; /* Set after incrementing the change-counter */
danielk1977d92db532008-11-17 04:56:24 +0000194 u8 dbSizeValid; /* Set when dbSize is correct */
danielk19773460d192008-12-27 15:23:13 +0000195 Pgno dbSize; /* Number of pages in the database */
196 Pgno dbOrigSize; /* dbSize before the current transaction */
197 Pgno dbFileSize; /* Number of pages in the database file */
drh33f4e022007-09-03 15:19:34 +0000198 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000199 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000200 int nRec; /* Number of pages written to the journal */
201 u32 cksumInit; /* Quasi-random value added to every checksum */
202 int stmtNRec; /* Number of records in stmt subjournal */
203 int nExtra; /* Add this many bytes to each in-memory page */
204 int pageSize; /* Number of bytes in a page */
205 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000206 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000207 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000208 Bitvec *pInJournal; /* One bit for each page in the database file */
danielk1977a1fa00d2008-08-27 15:16:33 +0000209 Bitvec *pAlwaysRollback; /* One bit for each page marked always-rollback */
drhfcd35c72005-05-21 02:48:08 +0000210 char *zFilename; /* Name of the database file */
211 char *zJournal; /* Name of the journal file */
212 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000213 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
danielk1977fd7f0452008-12-17 17:30:26 +0000214 sqlite3_file *sjfd; /* File descriptor for the sub-journal*/
danielk19771ceedd32008-11-19 10:22:33 +0000215 int (*xBusyHandler)(void*); /* Function to call when busy */
216 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
drheb206252004-10-01 02:00:31 +0000217 i64 journalOff; /* Current byte offset in the journal file */
218 i64 journalHdr; /* Byte offset to previous journal header */
drh98c58352008-11-07 00:24:53 +0000219 u32 sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000220#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000221 int nHit, nMiss; /* Cache hits and missing */
222 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000223#endif
danielk1977eaa06f62008-09-18 17:34:44 +0000224 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000225#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000226 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000227 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000228#endif
danielk19778186df82007-03-06 13:45:59 +0000229 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000230 char dbFileVers[16]; /* Changes whenever database file changes */
danielk1977b53e4962008-06-04 06:45:59 +0000231 i64 journalSizeLimit; /* Size limit for persistent journal files */
danielk19778c0a7912008-08-20 14:49:23 +0000232 PCache *pPCache; /* Pointer to page cache object */
drh9f0bbf92009-01-02 21:08:09 +0000233 PagerSavepoint *aSavepoint; /* Array of active savepoints */
234 int nSavepoint; /* Number of elements in aSavepoint[] */
drhd9b02572001-04-15 00:37:09 +0000235};
236
237/*
drh538f5702007-04-13 02:14:30 +0000238** The following global variables hold counters used for
239** testing purposes only. These variables do not exist in
240** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000241*/
242#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000243int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
244int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
245int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
drh538f5702007-04-13 02:14:30 +0000246# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000247#else
drh538f5702007-04-13 02:14:30 +0000248# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000249#endif
250
drh538f5702007-04-13 02:14:30 +0000251
252
drhfcd35c72005-05-21 02:48:08 +0000253/*
drh5e00f6c2001-09-13 13:46:56 +0000254** Journal files begin with the following magic string. The data
255** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000256**
drhae2b40c2004-06-09 19:03:54 +0000257** Since version 2.8.0, the journal format contains additional sanity
drh30d53702009-01-06 15:58:57 +0000258** checking information. If the power fails while the journal is being
drhae2b40c2004-06-09 19:03:54 +0000259** written, semi-random garbage data might appear in the journal
260** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000261** to roll the journal back, the database could be corrupted. The additional
262** sanity checking data is an attempt to discover the garbage in the
263** journal and ignore it.
264**
drhae2b40c2004-06-09 19:03:54 +0000265** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000266** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000267** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000268** This cksum is initialized to a 32-bit random value that appears in the
269** journal file right after the header. The random initializer is important,
270** because garbage data that appears at the end of a journal is likely
271** data that was once in other files that have now been deleted. If the
272** garbage data came from an obsolete journal file, the checksums might
273** be correct. But by initializing the checksum to random value which
274** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000275*/
drhae2b40c2004-06-09 19:03:54 +0000276static const unsigned char aJournalMagic[] = {
277 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000278};
279
280/*
drh726de592004-06-10 23:35:50 +0000281** The size of the header and of each page in the journal is determined
282** by the following macros.
drh968af522003-02-11 14:55:40 +0000283*/
drhae2b40c2004-06-09 19:03:54 +0000284#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000285
danielk197776572402004-06-25 02:38:54 +0000286/*
287** The journal header size for this pager. In the future, this could be
288** set to some value read from the disk controller. The important
289** characteristic is that it is the same size as a disk sector.
290*/
291#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
292
drhb7f91642004-10-31 02:22:47 +0000293/*
294** The macro MEMDB is true if we are dealing with an in-memory database.
295** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
296** the value of MEMDB will be a constant and the compiler will optimize
297** out code that would never execute.
298*/
299#ifdef SQLITE_OMIT_MEMORYDB
300# define MEMDB 0
301#else
302# define MEMDB pPager->memDb
303#endif
304
305/*
danielk197776572402004-06-25 02:38:54 +0000306** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
307** reserved for working around a windows/posix incompatibility). It is
308** used in the journal to signify that the remainder of the journal file
309** is devoted to storing a master journal name - there are no more pages to
310** roll back. See comments for function writeMasterJournal() for details.
311*/
danielk1977599fcba2004-11-08 07:13:13 +0000312/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
danielk1977d92db532008-11-17 04:56:24 +0000313#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
danielk197713adf8a2004-06-03 16:08:41 +0000314
drh968af522003-02-11 14:55:40 +0000315/*
danielk197726836652005-01-17 01:33:13 +0000316** The maximum legal page number is (2^31 - 1).
317*/
318#define PAGER_MAX_PGNO 2147483647
319
320/*
danielk19773460d192008-12-27 15:23:13 +0000321** Return true if it is necessary to write page *pPg into the sub-journal.
322** A page needs to be written into the sub-journal if there exists one
323** or more open savepoints for which:
danielk1977fd7f0452008-12-17 17:30:26 +0000324**
danielk19773460d192008-12-27 15:23:13 +0000325** * The page-number is less than or equal to PagerSavepoint.nOrig, and
326** * The bit corresponding to the page-number is not set in
327** PagerSavepoint.pInSavepoint.
danielk1977f35843b2007-04-07 15:03:17 +0000328*/
danielk19773460d192008-12-27 15:23:13 +0000329static int subjRequiresPage(PgHdr *pPg){
330 Pgno pgno = pPg->pgno;
danielk1977f35843b2007-04-07 15:03:17 +0000331 Pager *pPager = pPg->pPager;
danielk19773460d192008-12-27 15:23:13 +0000332 int i;
333 for(i=0; i<pPager->nSavepoint; i++){
334 PagerSavepoint *p = &pPager->aSavepoint[i];
335 if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
336 return 1;
337 }
danielk1977fd7f0452008-12-17 17:30:26 +0000338 }
danielk19773460d192008-12-27 15:23:13 +0000339 return 0;
danielk1977f35843b2007-04-07 15:03:17 +0000340}
drh8ca0c722006-05-07 17:49:38 +0000341
danielk19773460d192008-12-27 15:23:13 +0000342/*
343** Return true if the page is already in the journal file.
344*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000345static int pageInJournal(PgHdr *pPg){
346 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
347}
348
drh8ca0c722006-05-07 17:49:38 +0000349/*
drh34e79ce2004-02-08 06:05:46 +0000350** Read a 32-bit integer from the given file descriptor. Store the integer
351** that is read in *pRes. Return SQLITE_OK if everything worked, or an
352** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000353**
354** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000355*/
danielk197762079062007-08-15 17:08:46 +0000356static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000357 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000358 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000359 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000360 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000361 }
drh94f33312002-08-12 12:29:56 +0000362 return rc;
363}
364
365/*
drh97b57482006-01-10 20:32:32 +0000366** Write a 32-bit integer into a string buffer in big-endian byte order.
367*/
drha3152892007-05-05 11:48:52 +0000368#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000369
370/*
drh34e79ce2004-02-08 06:05:46 +0000371** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
372** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000373*/
danielk197762079062007-08-15 17:08:46 +0000374static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000375 char ac[4];
drh97b57482006-01-10 20:32:32 +0000376 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000377 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000378}
379
drh2554f8b2003-01-22 01:26:44 +0000380/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000381** If file pFd is open, call sqlite3OsUnlock() on it.
382*/
383static int osUnlock(sqlite3_file *pFd, int eLock){
384 if( !pFd->pMethods ){
385 return SQLITE_OK;
386 }
387 return sqlite3OsUnlock(pFd, eLock);
388}
389
390/*
danielk1977c7b60172007-08-22 11:22:03 +0000391** This function determines whether or not the atomic-write optimization
392** can be used with this pager. The optimization can be used if:
393**
394** (a) the value returned by OsDeviceCharacteristics() indicates that
395** a database page may be written atomically, and
396** (b) the value returned by OsSectorSize() is less than or equal
397** to the page size.
398**
399** If the optimization cannot be used, 0 is returned. If it can be used,
400** then the value returned is the size of the journal file when it
401** contains rollback data for exactly one page.
402*/
403#ifdef SQLITE_ENABLE_ATOMIC_WRITE
404static int jrnlBufferSize(Pager *pPager){
405 int dc; /* Device characteristics */
406 int nSector; /* Sector size */
drhfacf0302008-06-17 15:12:00 +0000407 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000408 sqlite3_file *fd = pPager->fd;
409
410 if( fd->pMethods ){
411 dc = sqlite3OsDeviceCharacteristics(fd);
danielk19777cbd5892009-01-10 16:15:09 +0000412 nSector = pPager->sectorSize;
drhfacf0302008-06-17 15:12:00 +0000413 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000414 }
415
416 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
417 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
418
drhfacf0302008-06-17 15:12:00 +0000419 if( !fd->pMethods ||
420 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000421 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
422 }
423 return 0;
424}
425#endif
426
427/*
danielk1977aef0bf62005-12-30 16:28:01 +0000428** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000429** code. The first argument is a pointer to the pager structure, the
430** second the error-code about to be returned by a pager API function.
431** The value returned is a copy of the second argument to this function.
432**
drh4f0ee682007-03-30 20:43:40 +0000433** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000434** the error becomes persistent. Until the persisten error is cleared,
435** subsequent API calls on this Pager will immediately return the same
436** error code.
437**
438** A persistent error indicates that the contents of the pager-cache
439** cannot be trusted. This state can be cleared by completely discarding
440** the contents of the pager-cache. If a transaction was active when
441** the persistent error occured, then the rollback journal may need
442** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000443*/
danielk1977ae72d982007-10-03 08:46:44 +0000444static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000445static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000446 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000447 assert(
448 pPager->errCode==SQLITE_FULL ||
449 pPager->errCode==SQLITE_OK ||
450 (pPager->errCode & 0xff)==SQLITE_IOERR
451 );
danielk1977979f38e2007-03-27 16:19:51 +0000452 if(
drh4ac285a2006-09-15 07:28:50 +0000453 rc2==SQLITE_FULL ||
454 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000455 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000456 ){
457 pPager->errCode = rc;
danielk19778c0a7912008-08-20 14:49:23 +0000458 if( pPager->state==PAGER_UNLOCK
459 && sqlite3PcacheRefCount(pPager->pPCache)==0
460 ){
danielk1977ae72d982007-10-03 08:46:44 +0000461 /* If the pager is already unlocked, call pager_unlock() now to
462 ** clear the error state and ensure that the pager-cache is
463 ** completely empty.
464 */
465 pager_unlock(pPager);
466 }
danielk1977aef0bf62005-12-30 16:28:01 +0000467 }
468 return rc;
469}
470
drh477731b2007-06-16 03:06:27 +0000471/*
472** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
473** on the cache using a hash function. This is used for testing
474** and debugging only.
475*/
danielk19773c407372005-02-15 02:54:14 +0000476#ifdef SQLITE_CHECK_PAGES
477/*
478** Return a 32-bit hash of the page data for pPage.
479*/
drh477731b2007-06-16 03:06:27 +0000480static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000481 u32 hash = 0;
482 int i;
drh477731b2007-06-16 03:06:27 +0000483 for(i=0; i<nByte; i++){
484 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000485 }
486 return hash;
487}
drh477731b2007-06-16 03:06:27 +0000488static u32 pager_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000489 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
490}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000491static void pager_set_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000492 pPage->pageHash = pager_pagehash(pPage);
drh477731b2007-06-16 03:06:27 +0000493}
danielk19773c407372005-02-15 02:54:14 +0000494
495/*
496** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
497** is defined, and NDEBUG is not defined, an assert() statement checks
498** that the page is either dirty or still matches the calculated page-hash.
499*/
500#define CHECK_PAGE(x) checkPage(x)
501static void checkPage(PgHdr *pPg){
502 Pager *pPager = pPg->pPager;
danielk1977b3175382008-10-17 18:51:52 +0000503 assert( !pPg->pageHash || pPager->errCode
danielk19778c0a7912008-08-20 14:49:23 +0000504 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
danielk19773c407372005-02-15 02:54:14 +0000505}
506
507#else
drh8ffa8172007-06-18 17:25:17 +0000508#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000509#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000510#define CHECK_PAGE(x)
drh41d30272008-08-20 21:47:45 +0000511#endif /* SQLITE_CHECK_PAGES */
danielk19773c407372005-02-15 02:54:14 +0000512
drhed7c8552001-04-11 14:29:21 +0000513/*
danielk197776572402004-06-25 02:38:54 +0000514** When this is called the journal file for pager pPager must be open.
515** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000516** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000517**
danielk197765839c62007-08-30 08:08:17 +0000518** zMaster must point to a buffer of at least nMaster bytes allocated by
519** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
520** enough space to write the master journal name). If the master journal
521** name in the journal is longer than nMaster bytes (including a
522** nul-terminator), then this is handled as if no master journal name
523** were present in the journal.
524**
525** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000526** SQLITE_OK returned.
527*/
danielk1977d92db532008-11-17 04:56:24 +0000528static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
danielk197776572402004-06-25 02:38:54 +0000529 int rc;
530 u32 len;
drheb206252004-10-01 02:00:31 +0000531 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000532 u32 cksum;
shane0b8d2762008-07-22 05:18:00 +0000533 u32 u; /* Unsigned loop counter */
danielk197776572402004-06-25 02:38:54 +0000534 unsigned char aMagic[8]; /* A buffer to hold the magic header */
535
danielk197765839c62007-08-30 08:08:17 +0000536 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000537
drh054889e2005-11-30 03:20:31 +0000538 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000539 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000540
danielk197762079062007-08-15 17:08:46 +0000541 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000542 if( rc!=SQLITE_OK ) return rc;
543
danielk197765839c62007-08-30 08:08:17 +0000544 if( len>=nMaster ){
545 return SQLITE_OK;
546 }
547
danielk197762079062007-08-15 17:08:46 +0000548 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000549 if( rc!=SQLITE_OK ) return rc;
550
danielk197762079062007-08-15 17:08:46 +0000551 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000552 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
553
danielk197765839c62007-08-30 08:08:17 +0000554 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000555 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000556 return rc;
557 }
danielk197765839c62007-08-30 08:08:17 +0000558 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000559
560 /* See if the checksum matches the master journal name */
shane0b8d2762008-07-22 05:18:00 +0000561 for(u=0; u<len; u++){
562 cksum -= zMaster[u];
danielk197765839c62007-08-30 08:08:17 +0000563 }
danielk19778191bff2004-06-28 04:52:30 +0000564 if( cksum ){
565 /* If the checksum doesn't add up, then one or more of the disk sectors
566 ** containing the master journal filename is corrupted. This means
567 ** definitely roll back, so just return SQLITE_OK and report a (nul)
568 ** master-journal filename.
569 */
danielk197765839c62007-08-30 08:08:17 +0000570 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000571 }
danielk197776572402004-06-25 02:38:54 +0000572
573 return SQLITE_OK;
574}
575
576/*
577** Seek the journal file descriptor to the next sector boundary where a
578** journal header may be read or written. Pager.journalOff is updated with
579** the new seek offset.
580**
581** i.e for a sector size of 512:
582**
583** Input Offset Output Offset
584** ---------------------------------------
585** 0 0
586** 512 512
587** 100 512
588** 2000 2048
589**
590*/
danielk1977112f7522009-01-08 17:50:45 +0000591static i64 journalHdrOffset(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000592 i64 offset = 0;
593 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000594 if( c ){
595 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
596 }
597 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
598 assert( offset>=c );
599 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
danielk1977112f7522009-01-08 17:50:45 +0000600 return offset;
601}
602static void seekJournalHdr(Pager *pPager){
603 pPager->journalOff = journalHdrOffset(pPager);
danielk197776572402004-06-25 02:38:54 +0000604}
605
606/*
drhf3a87622008-04-17 14:16:42 +0000607** Write zeros over the header of the journal file. This has the
608** effect of invalidating the journal file and committing the
609** transaction.
610*/
danielk1977df2566a2008-05-07 19:11:03 +0000611static int zeroJournalHdr(Pager *pPager, int doTruncate){
612 int rc = SQLITE_OK;
danielk197755a25a12008-09-11 10:29:15 +0000613 static const char zeroHdr[28] = {0};
drhf3a87622008-04-17 14:16:42 +0000614
danielk1977df2566a2008-05-07 19:11:03 +0000615 if( pPager->journalOff ){
danielk1977b53e4962008-06-04 06:45:59 +0000616 i64 iLimit = pPager->journalSizeLimit;
617
danielk1977df2566a2008-05-07 19:11:03 +0000618 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000619 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000620 rc = sqlite3OsTruncate(pPager->jfd, 0);
621 }else{
622 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
623 }
danielk197781620542008-06-07 05:19:37 +0000624 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000625 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
626 }
danielk1977b53e4962008-06-04 06:45:59 +0000627
628 /* At this point the transaction is committed but the write lock
629 ** is still held on the file. If there is a size limit configured for
630 ** the persistent journal and the journal file currently consumes more
631 ** space than that limit allows for, truncate it now. There is no need
632 ** to sync the file following this operation.
633 */
634 if( rc==SQLITE_OK && iLimit>0 ){
635 i64 sz;
636 rc = sqlite3OsFileSize(pPager->jfd, &sz);
637 if( rc==SQLITE_OK && sz>iLimit ){
638 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
639 }
640 }
drha06ecba2008-04-22 17:15:17 +0000641 }
drhf3a87622008-04-17 14:16:42 +0000642 return rc;
643}
644
645/*
danielk197776572402004-06-25 02:38:54 +0000646** The journal file must be open when this routine is called. A journal
647** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
648** current location.
649**
650** The format for the journal header is as follows:
651** - 8 bytes: Magic identifying journal format.
652** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
653** - 4 bytes: Random number used for page hash.
654** - 4 bytes: Initial database page count.
655** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000656** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000657**
danielk197767c007b2008-03-20 04:45:49 +0000658** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000659*/
660static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +0000661 int rc = SQLITE_OK;
662 char *zHeader = pPager->pTmpSpace;
danielk1977d92db532008-11-17 04:56:24 +0000663 u32 nHeader = pPager->pageSize;
664 u32 nWrite;
danielk1977fd7f0452008-12-17 17:30:26 +0000665 int ii;
danielk1977a664f8e2008-04-22 14:31:48 +0000666
667 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
668 nHeader = JOURNAL_HDR_SZ(pPager);
669 }
danielk197776572402004-06-25 02:38:54 +0000670
danielk1977fd7f0452008-12-17 17:30:26 +0000671 /* If there are active savepoints and any of them were created since the
672 ** most recent journal header was written, update the PagerSavepoint.iHdrOff
673 ** fields now.
674 */
675 for(ii=0; ii<pPager->nSavepoint; ii++){
676 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
677 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
678 }
danielk19774099f6e2007-03-19 11:25:20 +0000679 }
680
danielk197762079062007-08-15 17:08:46 +0000681 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000682 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000683
drh97b57482006-01-10 20:32:32 +0000684 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000685
686 /*
687 ** Write the nRec Field - the number of page records that follow this
688 ** journal header. Normally, zero is written to this value at this time.
689 ** After the records are added to the journal (and the journal synced,
690 ** if in full-sync mode), the zero is overwritten with the true number
691 ** of records (see syncJournal()).
692 **
693 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
694 ** reading the journal this value tells SQLite to assume that the
695 ** rest of the journal file contains valid page records. This assumption
696 ** is dangerous, as if a failure occured whilst writing to the journal
697 ** file it may contain some garbage data. There are two scenarios
698 ** where this risk can be ignored:
699 **
700 ** * When the pager is in no-sync mode. Corruption can follow a
701 ** power failure in this case anyway.
702 **
703 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
704 ** that garbage data is never appended to the journal file.
705 */
706 assert(pPager->fd->pMethods||pPager->noSync);
danielk1977b3175382008-10-17 18:51:52 +0000707 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
danielk19774cd2cd52007-08-23 14:48:23 +0000708 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
709 ){
710 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
711 }else{
712 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
713 }
714
drh97b57482006-01-10 20:32:32 +0000715 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +0000716 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +0000717 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
718 /* The initial database size */
danielk19773460d192008-12-27 15:23:13 +0000719 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
drh97b57482006-01-10 20:32:32 +0000720 /* The assumed sector size for this process */
721 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drh08609ce2008-11-29 22:49:23 +0000722
723 /* Initializing the tail of the buffer is not necessary. Everything
724 ** works find if the following memset() is omitted. But initializing
725 ** the memory prevents valgrind from complaining, so we are willing to
726 ** take the performance hit.
727 */
728 memset(&zHeader[sizeof(aJournalMagic)+16], 0,
729 nHeader-(sizeof(aJournalMagic)+16));
730
danielk197767c007b2008-03-20 04:45:49 +0000731 if( pPager->journalHdr==0 ){
732 /* The page size */
733 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
734 }
danielk197776572402004-06-25 02:38:54 +0000735
danielk1977a664f8e2008-04-22 14:31:48 +0000736 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
737 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
738 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
739 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +0000740 }
danielk1977a664f8e2008-04-22 14:31:48 +0000741
danielk197776572402004-06-25 02:38:54 +0000742 return rc;
743}
744
745/*
746** The journal file must be open when this is called. A journal header file
747** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
drhd6e5e092009-01-07 02:03:35 +0000748** file. The current location in the journal file is given by
749** pPager->journalOff. See comments above function writeJournalHdr() for
750** a description of the journal header format.
danielk197776572402004-06-25 02:38:54 +0000751**
752** If the header is read successfully, *nRec is set to the number of
753** page records following this header and *dbSize is set to the size of the
754** database before the transaction began, in pages. Also, pPager->cksumInit
755** is set to the value read from the journal header. SQLITE_OK is returned
756** in this case.
757**
758** If the journal header file appears to be corrupted, SQLITE_DONE is
drhd6e5e092009-01-07 02:03:35 +0000759** returned and *nRec and *dbSize are undefined. If JOURNAL_HDR_SZ bytes
danielk197776572402004-06-25 02:38:54 +0000760** cannot be read from the journal file an error code is returned.
761*/
762static int readJournalHdr(
763 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000764 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000765 u32 *pNRec,
766 u32 *pDbSize
767){
768 int rc;
769 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000770 i64 jrnlOff;
danielk19777cbd5892009-01-10 16:15:09 +0000771 u32 iPageSize;
772 u32 iSectorSize;
danielk197776572402004-06-25 02:38:54 +0000773
danielk197762079062007-08-15 17:08:46 +0000774 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000775 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
776 return SQLITE_DONE;
777 }
danielk197762079062007-08-15 17:08:46 +0000778 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000779
danielk197762079062007-08-15 17:08:46 +0000780 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000781 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000782 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000783
784 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
785 return SQLITE_DONE;
786 }
787
danielk197762079062007-08-15 17:08:46 +0000788 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000789 if( rc ) return rc;
790
danielk197762079062007-08-15 17:08:46 +0000791 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000792 if( rc ) return rc;
793
danielk197762079062007-08-15 17:08:46 +0000794 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000795 if( rc ) return rc;
796
danielk19777cbd5892009-01-10 16:15:09 +0000797 if( pPager->journalOff==0 ){
798 rc = read32bits(pPager->jfd, jrnlOff+16, &iPageSize);
799 if( rc ) return rc;
danielk197767c007b2008-03-20 04:45:49 +0000800
danielk19777cbd5892009-01-10 16:15:09 +0000801 if( iPageSize<512
802 || iPageSize>SQLITE_MAX_PAGE_SIZE
803 || ((iPageSize-1)&iPageSize)!=0
804 ){
805 /* If the page-size in the journal-header is invalid, then the process
806 ** that wrote the journal-header must have crashed before the header
807 ** was synced. In this case stop reading the journal file here.
808 */
809 rc = SQLITE_DONE;
810 }else{
811 u16 pagesize = (u16)iPageSize;
812 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
813 assert( rc!=SQLITE_OK || pagesize==(u16)iPageSize );
814 }
815 if( rc ) return rc;
816
817 /* Update the assumed sector-size to match the value used by
818 ** the process that created this journal. If this journal was
819 ** created by a process other than this one, then this routine
820 ** is being called from within pager_playback(). The local value
821 ** of Pager.sectorSize is restored at the end of that routine.
822 */
823 rc = read32bits(pPager->jfd, jrnlOff+12, &iSectorSize);
824 if( rc ) return rc;
825 if( (iSectorSize&(iSectorSize-1))
826 || iSectorSize<512
827 || iSectorSize>MAX_SECTOR_SIZE
828 ){
829 return SQLITE_DONE;
830 }
831 pPager->sectorSize = iSectorSize;
drh98c58352008-11-07 00:24:53 +0000832 }
danielk197776572402004-06-25 02:38:54 +0000833
834 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000835 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000836}
837
838
839/*
840** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000841** pPager at the current location. The master journal name must be the last
842** thing written to a journal file. If the pager is in full-sync mode, the
843** journal file descriptor is advanced to the next sector boundary before
844** anything is written. The format is:
845**
846** + 4 bytes: PAGER_MJ_PGNO.
847** + N bytes: length of master journal name.
848** + 4 bytes: N
849** + 4 bytes: Master journal name checksum.
850** + 8 bytes: aJournalMagic[].
851**
852** The master journal page checksum is the sum of the bytes in the master
853** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000854**
855** If zMaster is a NULL pointer (occurs for a single database transaction),
856** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000857*/
858static int writeMasterJournal(Pager *pPager, const char *zMaster){
859 int rc;
860 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000861 int i;
danielk197762079062007-08-15 17:08:46 +0000862 i64 jrnlOff;
danielk1977df2566a2008-05-07 19:11:03 +0000863 i64 jrnlSize;
drh97b57482006-01-10 20:32:32 +0000864 u32 cksum = 0;
865 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000866
danielk1977b3175382008-10-17 18:51:52 +0000867 if( !zMaster || pPager->setMaster ) return SQLITE_OK;
868 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ) return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000869 pPager->setMaster = 1;
870
drhea678832008-12-10 19:26:22 +0000871 len = sqlite3Strlen30(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000872 for(i=0; i<len; i++){
873 cksum += zMaster[i];
874 }
danielk197776572402004-06-25 02:38:54 +0000875
876 /* If in full-sync mode, advance to the next disk sector before writing
877 ** the master journal name. This is in case the previous page written to
878 ** the journal has already been synced.
879 */
880 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000881 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000882 }
danielk197762079062007-08-15 17:08:46 +0000883 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000884 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000885
danielk197762079062007-08-15 17:08:46 +0000886 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000887 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000888 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000889
danielk197762079062007-08-15 17:08:46 +0000890 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000891 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000892 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000893
drh97b57482006-01-10 20:32:32 +0000894 put32bits(zBuf, len);
895 put32bits(&zBuf[4], cksum);
896 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000897 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +0000898 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +0000899 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +0000900
901 /* If the pager is in peristent-journal mode, then the physical
902 ** journal-file may extend past the end of the master-journal name
903 ** and 8 bytes of magic data just written to the file. This is
904 ** dangerous because the code to rollback a hot-journal file
905 ** will not be able to find the master-journal name to determine
906 ** whether or not the journal is hot.
907 **
908 ** Easiest thing to do in this scenario is to truncate the journal
909 ** file to the required size.
910 */
911 if( (rc==SQLITE_OK)
912 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
913 && jrnlSize>jrnlOff
914 ){
915 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
916 }
danielk197776572402004-06-25 02:38:54 +0000917 return rc;
918}
919
920/*
drhed7c8552001-04-11 14:29:21 +0000921** Find a page in the hash table given its page number. Return
922** a pointer to the page or NULL if not found.
923*/
drhd9b02572001-04-15 00:37:09 +0000924static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000925 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000926 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
drhed7c8552001-04-11 14:29:21 +0000927 return p;
928}
929
930/*
danielk1977e180dd92007-04-05 17:15:52 +0000931** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +0000932** sets the state of the pager back to what it was when it was first
933** opened. Any outstanding pages are invalidated and subsequent attempts
934** to access those pages will likely result in a coredump.
935*/
drhd9b02572001-04-15 00:37:09 +0000936static void pager_reset(Pager *pPager){
danielk1977efaaf572006-01-16 11:29:19 +0000937 if( pPager->errCode ) return;
danielk19778c0a7912008-08-20 14:49:23 +0000938 sqlite3PcacheClear(pPager->pPCache);
danielk1977e277be02007-03-23 18:12:06 +0000939}
940
danielk197734cf35d2008-12-18 18:31:38 +0000941/*
942** Free all structures in the Pager.aSavepoint[] array and set both
943** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
944** if it is open and the pager is not in exclusive mode.
945*/
danielk1977fd7f0452008-12-17 17:30:26 +0000946static void releaseAllSavepoint(Pager *pPager){
947 int ii;
948 for(ii=0; ii<pPager->nSavepoint; ii++){
949 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
950 }
951 if( !pPager->exclusiveMode ){
952 sqlite3OsClose(pPager->sjfd);
953 }
954 sqlite3_free(pPager->aSavepoint);
955 pPager->aSavepoint = 0;
956 pPager->nSavepoint = 0;
danielk197767ddef62008-12-23 19:15:56 +0000957 pPager->stmtNRec = 0;
danielk1977fd7f0452008-12-17 17:30:26 +0000958}
959
danielk197734cf35d2008-12-18 18:31:38 +0000960/*
961** Set the bit number pgno in the PagerSavepoint.pInSavepoint bitvecs of
962** all open savepoints.
963*/
danielk1977fd7f0452008-12-17 17:30:26 +0000964static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
drh7539b6b2009-01-02 21:39:39 +0000965 int ii; /* Loop counter */
966 int rc = SQLITE_OK; /* Result code */
967
danielk1977fd7f0452008-12-17 17:30:26 +0000968 for(ii=0; ii<pPager->nSavepoint; ii++){
969 PagerSavepoint *p = &pPager->aSavepoint[ii];
970 if( pgno<=p->nOrig ){
drh7539b6b2009-01-02 21:39:39 +0000971 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
972 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
danielk1977fd7f0452008-12-17 17:30:26 +0000973 }
974 }
drh7539b6b2009-01-02 21:39:39 +0000975 return rc;
danielk1977fd7f0452008-12-17 17:30:26 +0000976}
977
danielk1977e277be02007-03-23 18:12:06 +0000978/*
danielk1977ae72d982007-10-03 08:46:44 +0000979** Unlock the database file.
980**
981** If the pager is currently in error state, discard the contents of
982** the cache and reset the Pager structure internal state. If there is
983** an open journal-file, then the next time a shared-lock is obtained
984** on the pager file (by this or any other process), it will be
985** treated as a hot-journal and rolled back.
986*/
987static void pager_unlock(Pager *pPager){
988 if( !pPager->exclusiveMode ){
danielk1977b3175382008-10-17 18:51:52 +0000989 int rc = osUnlock(pPager->fd, NO_LOCK);
990 if( rc ) pPager->errCode = rc;
danielk1977d92db532008-11-17 04:56:24 +0000991 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +0000992 IOTRACE(("UNLOCK %p\n", pPager))
danielk1977ae72d982007-10-03 08:46:44 +0000993
danielk1977b3175382008-10-17 18:51:52 +0000994 /* Always close the journal file when dropping the database lock.
995 ** Otherwise, another connection with journal_mode=delete might
996 ** delete the file out from under us.
997 */
998 if( pPager->journalOpen ){
999 sqlite3OsClose(pPager->jfd);
1000 pPager->journalOpen = 0;
1001 sqlite3BitvecDestroy(pPager->pInJournal);
1002 pPager->pInJournal = 0;
1003 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
1004 pPager->pAlwaysRollback = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001005 }
1006
danielk1977b3175382008-10-17 18:51:52 +00001007 /* If Pager.errCode is set, the contents of the pager cache cannot be
1008 ** trusted. Now that the pager file is unlocked, the contents of the
1009 ** cache can be discarded and the error code safely cleared.
1010 */
1011 if( pPager->errCode ){
1012 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
1013 pager_reset(pPager);
danielk1977fd7f0452008-12-17 17:30:26 +00001014 releaseAllSavepoint(pPager);
danielk1977b3175382008-10-17 18:51:52 +00001015 pPager->journalOff = 0;
1016 pPager->journalStarted = 0;
danielk19773460d192008-12-27 15:23:13 +00001017 pPager->dbOrigSize = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001018 }
danielk1977b3175382008-10-17 18:51:52 +00001019
1020 pPager->state = PAGER_UNLOCK;
1021 pPager->changeCountDone = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001022 }
1023}
1024
1025/*
1026** Execute a rollback if a transaction is active and unlock the
1027** database file. If the pager has already entered the error state,
1028** do not attempt the rollback.
1029*/
1030static void pagerUnlockAndRollback(Pager *p){
danielk1977ae72d982007-10-03 08:46:44 +00001031 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +00001032 sqlite3BeginBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001033 sqlite3PagerRollback(p);
danielk19772d1d86f2008-06-20 14:59:51 +00001034 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001035 }
1036 pager_unlock(p);
danielk1977ae72d982007-10-03 08:46:44 +00001037}
1038
1039/*
drh80e35f42007-03-30 14:06:34 +00001040** This routine ends a transaction. A transaction is ended by either
1041** a COMMIT or a ROLLBACK.
1042**
drhed7c8552001-04-11 14:29:21 +00001043** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001044** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1045** the database lock and acquires a SHARED lock in its place if that is
1046** the appropriate thing to do. Release locks usually is appropriate,
1047** unless we are in exclusive access mode or unless this is a
1048** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1049**
1050** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001051**
1052** TODO: Consider keeping the journal file open for temporary databases.
1053** This might give a performance improvement on windows where opening
1054** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001055*/
danielk1977df2566a2008-05-07 19:11:03 +00001056static int pager_end_transaction(Pager *pPager, int hasMaster){
danielk197741483462007-03-24 16:45:04 +00001057 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001058 int rc2 = SQLITE_OK;
drha6abd042004-06-09 17:37:22 +00001059 if( pPager->state<PAGER_RESERVED ){
1060 return SQLITE_OK;
1061 }
danielk1977fd7f0452008-12-17 17:30:26 +00001062 releaseAllSavepoint(pPager);
drhda47d772002-12-02 04:25:19 +00001063 if( pPager->journalOpen ){
danielk1977b3175382008-10-17 18:51:52 +00001064 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
1065 int isMemoryJournal = sqlite3IsMemJournal(pPager->jfd);
1066 sqlite3OsClose(pPager->jfd);
1067 pPager->journalOpen = 0;
1068 if( !isMemoryJournal ){
1069 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
1070 }
1071 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE
drh04335882008-09-26 21:08:08 +00001072 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){
1073 pPager->journalOff = 0;
1074 pPager->journalStarted = 0;
1075 }else if( pPager->exclusiveMode
danielk197793f7af92008-05-09 16:57:50 +00001076 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1077 ){
1078 rc = zeroJournalHdr(pPager, hasMaster);
1079 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +00001080 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001081 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001082 }else{
drh04335882008-09-26 21:08:08 +00001083 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || rc );
danielk1977b4b47412007-08-17 15:53:36 +00001084 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001085 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +00001086 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00001087 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001088 }
danielk197741483462007-03-24 16:45:04 +00001089 }
drhf5e7bb52008-02-18 14:47:33 +00001090 sqlite3BitvecDestroy(pPager->pInJournal);
1091 pPager->pInJournal = 0;
danielk1977a1fa00d2008-08-27 15:16:33 +00001092 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
1093 pPager->pAlwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001094#ifdef SQLITE_CHECK_PAGES
danielk1977bc2ca9e2008-11-13 14:28:28 +00001095 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
danielk19773c407372005-02-15 02:54:14 +00001096#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +00001097 sqlite3PcacheCleanAll(pPager->pPCache);
danielk1977ef317ab2004-06-23 10:43:10 +00001098 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001099 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001100 }else{
drhf5e7bb52008-02-18 14:47:33 +00001101 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001102 }
danielk1977979f38e2007-03-27 16:19:51 +00001103
danielk197741483462007-03-24 16:45:04 +00001104 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001105 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001106 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001107 }else if( pPager->state==PAGER_SYNCED ){
1108 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001109 }
danielk19773460d192008-12-27 15:23:13 +00001110 pPager->dbOrigSize = 0;
danielk197776572402004-06-25 02:38:54 +00001111 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001112 pPager->needSync = 0;
danielk19778c0a7912008-08-20 14:49:23 +00001113 /* lruListSetFirstSynced(pPager); */
danielk1977f90b7262009-01-07 15:18:20 +00001114 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
danielk1977b3175382008-10-17 18:51:52 +00001115 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00001116 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00001117 }
drhd138c012008-05-13 00:58:18 +00001118 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001119
1120 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001121}
1122
drhed7c8552001-04-11 14:29:21 +00001123/*
drh968af522003-02-11 14:55:40 +00001124** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001125**
1126** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001127** random initial value and the page number. We experimented with
1128** a checksum of the entire data, but that was found to be too slow.
1129**
1130** Note that the page number is stored at the beginning of data and
1131** the checksum is stored at the end. This is important. If journal
1132** corruption occurs due to a power failure, the most likely scenario
1133** is that one end or the other of the record will be changed. It is
1134** much less likely that the two ends of the journal record will be
1135** correct and the middle be corrupt. Thus, this "checksum" scheme,
1136** though fast and simple, catches the mostly likely kind of corruption.
1137**
1138** FIX ME: Consider adding every 200th (or so) byte of the data to the
1139** checksum. That way if a single page spans 3 or more disk sectors and
1140** only the middle sector is corrupt, we will still have a reasonable
1141** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001142*/
drh74161702006-02-24 02:53:49 +00001143static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001144 u32 cksum = pPager->cksumInit;
1145 int i = pPager->pageSize-200;
1146 while( i>0 ){
1147 cksum += aData[i];
1148 i -= 200;
1149 }
drh968af522003-02-11 14:55:40 +00001150 return cksum;
1151}
1152
1153/*
drhd6e5e092009-01-07 02:03:35 +00001154** Read a single page from either the journal file (if isMainJrnl==1) or
1155** from the sub-journal (if isMainJrnl==0) and playback that page.
1156** The page begins at offset *pOffset into the file. The *pOffset
1157** value is increased to the start of the next page in the journal.
drh968af522003-02-11 14:55:40 +00001158**
drhc13148f2008-08-27 18:03:20 +00001159** The isMainJrnl flag is true if this is the main rollback journal and
1160** false for the statement journal. The main rollback journal uses
1161** checksums - the statement journal does not.
drhd6e5e092009-01-07 02:03:35 +00001162**
1163** If pDone is not NULL, then it is a record of pages that have already
1164** been played back. If the page at *pOffset has already been played back
1165** (if the corresponding pDone bit is set) then skip the playback.
1166** Make sure the pDone bit corresponding to the *pOffset page is set
1167** prior to returning.
drhfa86c412002-02-02 15:01:15 +00001168*/
danielk197762079062007-08-15 17:08:46 +00001169static int pager_playback_one_page(
danielk1977fd7f0452008-12-17 17:30:26 +00001170 Pager *pPager, /* The pager being played back */
1171 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
drhd6e5e092009-01-07 02:03:35 +00001172 i64 *pOffset, /* Offset of record to playback */
danielk1977ecfef982008-12-20 08:39:57 +00001173 int isSavepnt, /* True for a savepoint rollback */
danielk1977fd7f0452008-12-17 17:30:26 +00001174 Bitvec *pDone /* Bitvec of pages already played back */
danielk197762079062007-08-15 17:08:46 +00001175){
drhfa86c412002-02-02 15:01:15 +00001176 int rc;
drhae2b40c2004-06-09 19:03:54 +00001177 PgHdr *pPg; /* An existing page in the cache */
1178 Pgno pgno; /* The page number of a page in journal */
1179 u32 cksum; /* Checksum used for sanity checking */
drhd6e5e092009-01-07 02:03:35 +00001180 u8 *aData; /* Temporary storage for the page */
1181 sqlite3_file *jfd; /* The file descriptor for the journal file */
drhfa86c412002-02-02 15:01:15 +00001182
drhd6e5e092009-01-07 02:03:35 +00001183 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
1184 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
1185 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
1186 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
drh96362842005-03-20 22:47:56 +00001187
drhd6e5e092009-01-07 02:03:35 +00001188 aData = (u8*)pPager->pTmpSpace;
1189 assert( aData ); /* Temp storage must have already been allocated */
1190
1191 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
1192
1193 rc = read32bits(jfd, *pOffset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001194 if( rc!=SQLITE_OK ) return rc;
drhd6e5e092009-01-07 02:03:35 +00001195 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, (*pOffset)+4);
drh99ee3602003-02-16 19:13:36 +00001196 if( rc!=SQLITE_OK ) return rc;
drhd6e5e092009-01-07 02:03:35 +00001197 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
drhfa86c412002-02-02 15:01:15 +00001198
drh968af522003-02-11 14:55:40 +00001199 /* Sanity checking on the page. This is more important that I originally
1200 ** thought. If a power failure occurs while the journal is being written,
1201 ** it could cause invalid data to be written into the journal. We need to
1202 ** detect this invalid data (with high probability) and ignore it.
1203 */
danielk197775edc162004-06-26 01:48:18 +00001204 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001205 return SQLITE_DONE;
1206 }
danielk1977fd7f0452008-12-17 17:30:26 +00001207 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
drh968af522003-02-11 14:55:40 +00001208 return SQLITE_OK;
1209 }
drhc13148f2008-08-27 18:03:20 +00001210 if( isMainJrnl ){
drhd6e5e092009-01-07 02:03:35 +00001211 rc = read32bits(jfd, (*pOffset)-4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001212 if( rc ) return rc;
danielk1977ecfef982008-12-20 08:39:57 +00001213 if( !isSavepnt && pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001214 return SQLITE_DONE;
1215 }
1216 }
danielk1977fd7f0452008-12-17 17:30:26 +00001217 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno)) ){
1218 return rc;
1219 }
drhfa86c412002-02-02 15:01:15 +00001220
danielk1977aa5ccdf2004-06-14 05:10:42 +00001221 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001222
1223 /* If the pager is in RESERVED state, then there must be a copy of this
1224 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001225 ** not the database file. The page is left marked dirty in this case.
1226 **
danielk19772df71c72007-05-24 07:22:42 +00001227 ** An exception to the above rule: If the database is in no-sync mode
1228 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001229 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1230 ** during a Movepage() call, then the page may not be in the cache
1231 ** either. So the condition described in the above paragraph is not
1232 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001233 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001234 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1235 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001236 **
1237 ** Ticket #1171: The statement journal might contain page content that is
1238 ** different from the page content at the start of the transaction.
1239 ** This occurs when a page is changed prior to the start of a statement
1240 ** then changed again within the statement. When rolling back such a
1241 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001242 ** for certain that original page contents are synced into the main rollback
1243 ** journal. Otherwise, a power loss might leave modified data in the
1244 ** database file without an entry in the rollback journal that can
1245 ** restore the database to its original form. Two conditions must be
1246 ** met before writing to the database files. (1) the database must be
1247 ** locked. (2) we know that the original page content is fully synced
1248 ** in the main journal either because the page is not in cache or else
1249 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001250 **
1251 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1252 ** is possible to fail a statement on a database that does not yet exist.
1253 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001254 */
drhae2b40c2004-06-09 19:03:54 +00001255 pPg = pager_lookup(pPager, pgno);
drh30d53702009-01-06 15:58:57 +00001256 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
danielk1977ecfef982008-12-20 08:39:57 +00001257 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData),
1258 (isMainJrnl?"main-journal":"sub-journal")
drh30d53702009-01-06 15:58:57 +00001259 ));
danielk19778c0a7912008-08-20 14:49:23 +00001260 if( (pPager->state>=PAGER_EXCLUSIVE)
1261 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
1262 && (pPager->fd->pMethods)
1263 ){
drh281b21d2008-08-22 12:57:08 +00001264 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
1265 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
danielk19773460d192008-12-27 15:23:13 +00001266 if( pgno>pPager->dbFileSize ){
1267 pPager->dbFileSize = pgno;
1268 }
danielk1977f2c31ad2009-01-06 13:40:08 +00001269 }else if( !isMainJrnl && pPg==0 ){
1270 /* If this is a rollback of a savepoint and data was not written to
1271 ** the database and the page is not in-memory, there is a potential
1272 ** problem. When the page is next fetched by the b-tree layer, it
1273 ** will be read from the database file, which may or may not be
1274 ** current.
1275 **
1276 ** There are a couple of different ways this can happen. All are quite
danielk1977401b65e2009-01-06 14:34:34 +00001277 ** obscure. When running in synchronous mode, this can only happen
danielk1977f2c31ad2009-01-06 13:40:08 +00001278 ** if the page is on the free-list at the start of the transaction, then
1279 ** populated, then moved using sqlite3PagerMovepage().
1280 **
1281 ** The solution is to add an in-memory page to the cache containing
1282 ** the data just read from the sub-journal. Mark the page as dirty
1283 ** and if the pager requires a journal-sync, then mark the page as
1284 ** requiring a journal-sync before it is written.
1285 */
1286 assert( isSavepnt );
1287 if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1)) ){
1288 return rc;
1289 }
1290 pPg->flags &= ~PGHDR_NEED_READ;
1291 sqlite3PcacheMakeDirty(pPg);
danielk1977a3f3a5f2004-06-10 04:32:16 +00001292 }
drhfa86c412002-02-02 15:01:15 +00001293 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001294 /* No page should ever be explicitly rolled back that is in use, except
1295 ** for page 1 which is held in use in order to keep the lock on the
1296 ** database active. However such a page may be rolled back as a result
1297 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001298 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001299 */
drhb6f41482004-05-14 01:58:11 +00001300 void *pData;
danielk19778c0a7912008-08-20 14:49:23 +00001301 pData = pPg->pData;
drhae2b40c2004-06-09 19:03:54 +00001302 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001303 if( pPager->xReiniter ){
danielk1977eaa06f62008-09-18 17:34:44 +00001304 pPager->xReiniter(pPg);
drhde647132004-05-07 17:57:49 +00001305 }
danielk1977ecfef982008-12-20 08:39:57 +00001306 if( isMainJrnl && (!isSavepnt || pPager->journalOff<=pPager->journalHdr) ){
danielk1977488af092008-12-19 16:31:11 +00001307 /* If the contents of this page were just restored from the main
1308 ** journal file, then its content must be as they were when the
1309 ** transaction was first opened. In this case we can mark the page
1310 ** as clean, since there will be no need to write it out to the.
1311 **
1312 ** There is one exception to this rule. If the page is being rolled
1313 ** back as part of a savepoint (or statement) rollback from an
1314 ** unsynced portion of the main journal file, then it is not safe
1315 ** to mark the page as clean. This is because marking the page as
1316 ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
1317 ** already in the journal file (recorded in Pager.pInJournal) and
1318 ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
1319 ** again within this transaction, it will be marked as dirty but
1320 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
1321 ** be written out into the database file before its journal file
1322 ** segment is synced. If a crash occurs during or following this,
1323 ** database corruption may ensue.
1324 */
drhc047b9f2008-11-21 03:23:43 +00001325 sqlite3PcacheMakeClean(pPg);
1326 }
danielk19773c407372005-02-15 02:54:14 +00001327#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001328 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001329#endif
drh86a88112007-04-16 15:02:19 +00001330 /* If this was page 1, then restore the value of Pager.dbFileVers.
1331 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001332 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001333 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001334 }
drh86a88112007-04-16 15:02:19 +00001335
1336 /* Decode the page just read from disk */
1337 CODEC1(pPager, pData, pPg->pgno, 3);
danielk19778c0a7912008-08-20 14:49:23 +00001338 sqlite3PcacheRelease(pPg);
drhfa86c412002-02-02 15:01:15 +00001339 }
1340 return rc;
1341}
1342
drhee03d622009-01-07 15:33:45 +00001343#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
drhd6e5e092009-01-07 02:03:35 +00001344/*
1345** This routine looks ahead into the main journal file and determines
1346** whether or not the next record (the record that begins at file
1347** offset pPager->journalOff) is a well-formed page record consisting
1348** of a valid page number, pPage->pageSize bytes of content, followed
1349** by a valid checksum.
1350**
1351** The pager never needs to know this in order to do its job. This
1352** routine is only used from with assert() and testcase() macros.
1353*/
1354static int pagerNextJournalPageIsValid(Pager *pPager){
1355 Pgno pgno; /* The page number of the page */
1356 u32 cksum; /* The page checksum */
1357 int rc; /* Return code from read operations */
1358 sqlite3_file *fd; /* The file descriptor from which we are reading */
1359 u8 *aData; /* Content of the page */
1360
1361 /* Read the page number header */
1362 fd = pPager->jfd;
1363 rc = read32bits(fd, pPager->journalOff, &pgno);
1364 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1365 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ return 0; } /*NO_TEST*/
1366 if( pgno>(Pgno)pPager->dbSize ){ return 0; } /*NO_TEST*/
1367
1368 /* Read the checksum */
1369 rc = read32bits(fd, pPager->journalOff+pPager->pageSize+4, &cksum);
1370 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1371
1372 /* Read the data and verify the checksum */
1373 aData = (u8*)pPager->pTmpSpace;
1374 rc = sqlite3OsRead(fd, aData, pPager->pageSize, pPager->journalOff+4);
1375 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1376 if( pager_cksum(pPager, aData)!=cksum ){ return 0; } /*NO_TEST*/
1377
1378 /* Reach this point only if the page is valid */
1379 return 1;
1380}
1381#endif /* !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST) */
1382
drhfa86c412002-02-02 15:01:15 +00001383/*
danielk197713adf8a2004-06-03 16:08:41 +00001384** Parameter zMaster is the name of a master journal file. A single journal
1385** file that referred to the master journal file has just been rolled back.
1386** This routine checks if it is possible to delete the master journal file,
1387** and does so if it is.
drh726de592004-06-10 23:35:50 +00001388**
danielk197765839c62007-08-30 08:08:17 +00001389** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1390** available for use within this function.
1391**
1392**
drh726de592004-06-10 23:35:50 +00001393** The master journal file contains the names of all child journals.
1394** To tell if a master journal can be deleted, check to each of the
1395** children. If all children are either missing or do not refer to
1396** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001397*/
danielk1977b4b47412007-08-17 15:53:36 +00001398static int pager_delmaster(Pager *pPager, const char *zMaster){
1399 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001400 int rc;
1401 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001402 sqlite3_file *pMaster;
1403 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001404 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001405 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001406
1407 /* Open the master journal file exclusively in case some other process
1408 ** is running this routine also. Not that it makes too much difference.
1409 */
drhe5ae5732008-06-15 02:51:47 +00001410 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001411 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001412 if( !pMaster ){
1413 rc = SQLITE_NOMEM;
1414 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001415 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1416 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001417 }
danielk197713adf8a2004-06-03 16:08:41 +00001418 if( rc!=SQLITE_OK ) goto delmaster_out;
1419 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001420
1421 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001422 if( rc!=SQLITE_OK ) goto delmaster_out;
1423
1424 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001425 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001426 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001427 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001428
1429 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001430 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001431 */
drh4f21c4a2008-12-10 22:15:00 +00001432 zMasterJournal = (char *)sqlite3Malloc((int)nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001433 if( !zMasterJournal ){
1434 rc = SQLITE_NOMEM;
1435 goto delmaster_out;
1436 }
danielk197765839c62007-08-30 08:08:17 +00001437 zMasterPtr = &zMasterJournal[nMasterJournal];
drh4f21c4a2008-12-10 22:15:00 +00001438 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001439 if( rc!=SQLITE_OK ) goto delmaster_out;
1440
danielk19775865e3d2004-06-14 06:03:57 +00001441 zJournal = zMasterJournal;
1442 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001443 int exists;
1444 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1445 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001446 goto delmaster_out;
1447 }
danielk1977861f7452008-06-05 11:39:11 +00001448 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001449 /* One of the journals pointed to by the master journal exists.
1450 ** Open it and check if it points at the master journal. If
1451 ** so, return without deleting the master journal file.
1452 */
drh3b7b78b2004-11-24 01:16:43 +00001453 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001454 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1455 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001456 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001457 goto delmaster_out;
1458 }
danielk197713adf8a2004-06-03 16:08:41 +00001459
danielk197765839c62007-08-30 08:08:17 +00001460 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001461 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001462 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001463 goto delmaster_out;
1464 }
danielk197776572402004-06-25 02:38:54 +00001465
danielk197765839c62007-08-30 08:08:17 +00001466 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001467 if( c ){
danielk197776572402004-06-25 02:38:54 +00001468 /* We have a match. Do not delete the master journal file. */
1469 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001470 }
1471 }
drhea678832008-12-10 19:26:22 +00001472 zJournal += (sqlite3Strlen30(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001473 }
1474 }
1475
danielk1977fee2d252007-08-18 10:59:19 +00001476 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001477
1478delmaster_out:
1479 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001480 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001481 }
1482 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001483 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001484 }
danielk1977b4b47412007-08-17 15:53:36 +00001485 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001486 return rc;
1487}
1488
drha6abd042004-06-09 17:37:22 +00001489
drha6abd042004-06-09 17:37:22 +00001490/*
danielk1977f90b7262009-01-07 15:18:20 +00001491** If the main database file is open and an exclusive lock is held,
1492** truncate the main file of the given pager to the specified number
1493** of pages.
drh7fe3f7e2007-11-29 18:44:27 +00001494**
danielk1977f90b7262009-01-07 15:18:20 +00001495** It might might be the case that the file on disk is smaller than nPage.
drh7fe3f7e2007-11-29 18:44:27 +00001496** This can happen, for example, if we are in the middle of a transaction
1497** which has extended the file size and the new pages are still all held
1498** in cache, then an INSERT or UPDATE does a statement rollback. Some
1499** operating system implementations can get confused if you try to
1500** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001501** so detect this case and write a single zero byte to the end of the new
1502** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001503*/
danielk1977d92db532008-11-17 04:56:24 +00001504static int pager_truncate(Pager *pPager, Pgno nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001505 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001506 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001507 i64 currentSize, newSize;
1508 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1509 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001510 if( rc==SQLITE_OK && currentSize!=newSize ){
1511 if( currentSize>newSize ){
1512 rc = sqlite3OsTruncate(pPager->fd, newSize);
1513 }else{
1514 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1515 }
danielk19773460d192008-12-27 15:23:13 +00001516 if( rc==SQLITE_OK ){
1517 pPager->dbFileSize = nPage;
1518 }
drh7fe3f7e2007-11-29 18:44:27 +00001519 }
danielk1977e180dd92007-04-05 17:15:52 +00001520 }
danielk1977e180dd92007-04-05 17:15:52 +00001521 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001522}
1523
1524/*
drhc80f0582007-05-01 16:59:48 +00001525** Set the sectorSize for the given pager.
1526**
drh334c80d2008-03-28 17:41:13 +00001527** The sector size is at least as big as the sector size reported
danielk19777cbd5892009-01-10 16:15:09 +00001528** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001529*/
1530static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001531 assert(pPager->fd->pMethods||pPager->tempFile);
1532 if( !pPager->tempFile ){
1533 /* Sector size doesn't matter for temporary files. Also, the file
1534 ** may not have been opened yet, in whcih case the OsSectorSize()
1535 ** call will segfault.
1536 */
1537 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1538 }
drh334c80d2008-03-28 17:41:13 +00001539 if( pPager->sectorSize<512 ){
1540 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001541 }
danielk19777cbd5892009-01-10 16:15:09 +00001542 if( pPager->sectorSize>MAX_SECTOR_SIZE ){
1543 pPager->sectorSize = MAX_SECTOR_SIZE;
1544 }
drhc80f0582007-05-01 16:59:48 +00001545}
1546
1547/*
drhed7c8552001-04-11 14:29:21 +00001548** Playback the journal and thus restore the database file to
1549** the state it was in before we started making changes.
1550**
drh34e79ce2004-02-08 06:05:46 +00001551** The journal file format is as follows:
1552**
drhae2b40c2004-06-09 19:03:54 +00001553** (1) 8 byte prefix. A copy of aJournalMagic[].
1554** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001555** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001556** number of page records from the journal size.
1557** (3) 4 byte big-endian integer which is the initial value for the
1558** sanity checksum.
1559** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001560** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001561** (5) 4 byte big-endian integer which is the sector size. The header
1562** is this many bytes in size.
1563** (6) 4 byte big-endian integer which is the page case.
1564** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001565** name. The value may be zero (indicate that there is no master
1566** journal.)
drh334c80d2008-03-28 17:41:13 +00001567** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001568** and might be shorter than the value read from (5). If the first byte
1569** of the name is \000 then there is no master journal. The master
1570** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001571** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001572** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001573** + pPager->pageSize bytes of data.
1574** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001575**
drh334c80d2008-03-28 17:41:13 +00001576** When we speak of the journal header, we mean the first 8 items above.
1577** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001578**
1579** Call the value from the second bullet "nRec". nRec is the number of
1580** valid page entries in the journal. In most cases, you can compute the
1581** value of nRec from the size of the journal file. But if a power
1582** failure occurred while the journal was being written, it could be the
1583** case that the size of the journal file had already been increased but
1584** the extra entries had not yet made it safely to disk. In such a case,
1585** the value of nRec computed from the file size would be too large. For
1586** that reason, we always use the nRec value in the header.
1587**
1588** If the nRec value is 0xffffffff it means that nRec should be computed
1589** from the file size. This value is used when the user selects the
1590** no-sync option for the journal. A power failure could lead to corruption
1591** in this case. But for things like temporary table (which will be
1592** deleted when the power is restored) we don't care.
1593**
drhd9b02572001-04-15 00:37:09 +00001594** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001595** journal file then all pages up to the first corrupted page are rolled
1596** back (or no pages if the journal header is corrupted). The journal file
1597** is then deleted and SQLITE_OK returned, just as if no corruption had
1598** been encountered.
1599**
1600** If an I/O or malloc() error occurs, the journal-file is not deleted
1601** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001602*/
danielk1977e277be02007-03-23 18:12:06 +00001603static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001604 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001605 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001606 u32 nRec; /* Number of Records in the journal */
shane0b8d2762008-07-22 05:18:00 +00001607 u32 u; /* Unsigned loop counter */
drhd9b02572001-04-15 00:37:09 +00001608 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001609 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001610 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001611 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001612
drhc3a64ba2001-11-22 00:01:27 +00001613 /* Figure out how many records are in the journal. Abort early if
1614 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001615 */
drh8cfbf082001-09-19 13:22:39 +00001616 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001617 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001618 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001619 goto end_playback;
1620 }
drh240c5792004-02-08 00:40:52 +00001621
danielk197776572402004-06-25 02:38:54 +00001622 /* Read the master journal name from the journal, if it is present.
1623 ** If a master journal file name is specified, but the file is not
1624 ** present on disk, then the journal is not hot and does not need to be
1625 ** played back.
drh240c5792004-02-08 00:40:52 +00001626 */
danielk197765839c62007-08-30 08:08:17 +00001627 zMaster = pPager->pTmpSpace;
1628 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001629 if( rc==SQLITE_OK && zMaster[0] ){
1630 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001631 }
danielk197765839c62007-08-30 08:08:17 +00001632 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001633 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001634 goto end_playback;
1635 }
1636 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001637
danielk197776572402004-06-25 02:38:54 +00001638 /* This loop terminates either when the readJournalHdr() call returns
1639 ** SQLITE_DONE or an IO error occurs. */
1640 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001641
danielk197776572402004-06-25 02:38:54 +00001642 /* Read the next journal header from the journal file. If there are
1643 ** not enough bytes left in the journal file for a complete header, or
1644 ** it is corrupted, then a process must of failed while writing it.
1645 ** This indicates nothing more needs to be rolled back.
1646 */
1647 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1648 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001649 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001650 rc = SQLITE_OK;
1651 }
danielk197776572402004-06-25 02:38:54 +00001652 goto end_playback;
1653 }
1654
1655 /* If nRec is 0xffffffff, then this journal was created by a process
1656 ** working in no-sync mode. This means that the rest of the journal
1657 ** file consists of pages, there are no more journal headers. Compute
1658 ** the value of nRec based on this assumption.
1659 */
1660 if( nRec==0xffffffff ){
1661 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
drh4f21c4a2008-12-10 22:15:00 +00001662 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
danielk197776572402004-06-25 02:38:54 +00001663 }
1664
danielk1977e277be02007-03-23 18:12:06 +00001665 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001666 ** process and if this is the final header in the journal, then it means
1667 ** that this part of the journal was being filled but has not yet been
1668 ** synced to disk. Compute the number of pages based on the remaining
1669 ** size of the file.
1670 **
1671 ** The third term of the test was added to fix ticket #2565.
drhd6e5e092009-01-07 02:03:35 +00001672 ** When rolling back a hot journal, nRec==0 always means that the next
1673 ** chunk of the journal contains zero pages to be rolled back. But
1674 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
1675 ** the journal, it means that the journal might contain additional
1676 ** pages that need to be rolled back and that the number of pages
drhee03d622009-01-07 15:33:45 +00001677 ** should be computed based on the journal file size.
danielk1977e277be02007-03-23 18:12:06 +00001678 */
drh4fd18c42009-01-08 15:24:01 +00001679 testcase( nRec==0 && !isHot
drhd6e5e092009-01-07 02:03:35 +00001680 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)!=pPager->journalOff
1681 && ((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager))>0
drh4fd18c42009-01-08 15:24:01 +00001682 && pagerNextJournalPageIsValid(pPager)
drhd6e5e092009-01-07 02:03:35 +00001683 );
drh8940f4e2007-08-11 00:26:20 +00001684 if( nRec==0 && !isHot &&
1685 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
drh4f21c4a2008-12-10 22:15:00 +00001686 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
danielk1977e277be02007-03-23 18:12:06 +00001687 }
1688
danielk197776572402004-06-25 02:38:54 +00001689 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001690 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001691 */
danielk1977e180dd92007-04-05 17:15:52 +00001692 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001693 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001694 if( rc!=SQLITE_OK ){
1695 goto end_playback;
1696 }
danielk1977f90b7262009-01-07 15:18:20 +00001697 pPager->dbSize = mxPg;
danielk197776572402004-06-25 02:38:54 +00001698 }
1699
danielk197776572402004-06-25 02:38:54 +00001700 /* Copy original pages out of the journal and back into the database file.
1701 */
shane0b8d2762008-07-22 05:18:00 +00001702 for(u=0; u<nRec; u++){
drhd6e5e092009-01-07 02:03:35 +00001703 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 0, 0);
danielk197776572402004-06-25 02:38:54 +00001704 if( rc!=SQLITE_OK ){
1705 if( rc==SQLITE_DONE ){
1706 rc = SQLITE_OK;
1707 pPager->journalOff = szJ;
1708 break;
1709 }else{
drha9625ea2008-09-03 00:08:29 +00001710 /* If we are unable to rollback, then the database is probably
1711 ** going to end up being corrupt. It is corrupt to us, anyhow.
1712 ** Perhaps the next process to come along can fix it....
1713 */
danielk197798c21902008-09-23 16:41:29 +00001714 rc = SQLITE_CORRUPT_BKPT;
danielk197776572402004-06-25 02:38:54 +00001715 goto end_playback;
1716 }
1717 }
drh968af522003-02-11 14:55:40 +00001718 }
drhed7c8552001-04-11 14:29:21 +00001719 }
drh580eeaf2006-02-24 03:09:37 +00001720 /*NOTREACHED*/
1721 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001722
1723end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001724 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001725 zMaster = pPager->pTmpSpace;
1726 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1727 }
1728 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001729 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001730 }
danielk1977c56774e2008-10-07 11:51:20 +00001731 if( rc==SQLITE_OK && zMaster[0] && res ){
danielk1977979f38e2007-03-27 16:19:51 +00001732 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001733 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001734 */
danielk197765839c62007-08-30 08:08:17 +00001735 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001736 }
danielk197776572402004-06-25 02:38:54 +00001737
1738 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001739 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001740 ** value. Reset it to the correct value for this process.
1741 */
drhc80f0582007-05-01 16:59:48 +00001742 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001743 return rc;
drhed7c8552001-04-11 14:29:21 +00001744}
1745
1746/*
drhd6e5e092009-01-07 02:03:35 +00001747** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
1748** the entire master journal file.
1749**
1750** The case pSavepoint==NULL occurs when a ROLLBACK TO command is invoked
1751** on a SAVEPOINT that is a transaction savepoint.
drhfa86c412002-02-02 15:01:15 +00001752*/
danielk1977fd7f0452008-12-17 17:30:26 +00001753static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
drhd6e5e092009-01-07 02:03:35 +00001754 i64 szJ; /* Effective size of the main journal */
danielk1977fd7f0452008-12-17 17:30:26 +00001755 i64 iHdrOff; /* End of first segment of main-journal records */
1756 Pgno ii; /* Loop counter */
danielk1977f2c31ad2009-01-06 13:40:08 +00001757 int rc = SQLITE_OK; /* Return code */
danielk1977fd7f0452008-12-17 17:30:26 +00001758 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
drhfa86c412002-02-02 15:01:15 +00001759
danielk1977fd7f0452008-12-17 17:30:26 +00001760 /* Allocate a bitvec to use to store the set of pages rolled back */
1761 if( pSavepoint ){
1762 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
1763 if( !pDone ){
1764 return SQLITE_NOMEM;
1765 }
danielk197776572402004-06-25 02:38:54 +00001766 }
danielk1977fd7f0452008-12-17 17:30:26 +00001767
1768 /* Truncate the database back to the size it was before the
1769 ** savepoint being reverted was opened.
drhfa86c412002-02-02 15:01:15 +00001770 */
drhd6e5e092009-01-07 02:03:35 +00001771 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
drh1aa2d8b2007-01-03 15:34:29 +00001772 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001773
drhd6e5e092009-01-07 02:03:35 +00001774 /* Use pPager->journalOff as the effective size of the main rollback
1775 ** journal. The actual file might be larger than this in
1776 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
1777 ** past pPager->journalOff is off-limits to us.
drhfa86c412002-02-02 15:01:15 +00001778 */
danielk1977fd7f0452008-12-17 17:30:26 +00001779 szJ = pPager->journalOff;
drhd6e5e092009-01-07 02:03:35 +00001780
1781 /* Begin by rolling back records from the main journal starting at
1782 ** PagerSavepoint.iOffset and continuing to the next journal header.
1783 ** There might be records in the main journal that have a page number
1784 ** greater than the current database size (pPager->dbSize) but those
1785 ** will be skipped automatically. Pages are added to pDone as they
1786 ** are played back.
1787 */
danielk1977fd7f0452008-12-17 17:30:26 +00001788 if( pSavepoint ){
1789 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
1790 pPager->journalOff = pSavepoint->iOffset;
1791 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
drhd6e5e092009-01-07 02:03:35 +00001792 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone);
danielk1977fd7f0452008-12-17 17:30:26 +00001793 assert( rc!=SQLITE_DONE );
1794 }
1795 }else{
1796 pPager->journalOff = 0;
drhfa86c412002-02-02 15:01:15 +00001797 }
drhd6e5e092009-01-07 02:03:35 +00001798
1799 /* Continue rolling back records out of the main journal starting at
1800 ** the first journal header seen and continuing until the effective end
1801 ** of the main journal file. Continue to skip out-of-range pages and
1802 ** continue adding pages rolled back to pDone.
1803 */
danielk1977fd7f0452008-12-17 17:30:26 +00001804 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
danielk1977c81806f2009-01-01 15:20:37 +00001805 u32 nJRec = 0; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001806 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001807 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
danielk1977fd7f0452008-12-17 17:30:26 +00001808 assert( rc!=SQLITE_DONE );
drhd6e5e092009-01-07 02:03:35 +00001809
1810 /*
1811 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
1812 ** test is related to ticket #2565. See the discussion in the
1813 ** pager_playback() function for additional information.
1814 */
drhee03d622009-01-07 15:33:45 +00001815 assert( !(nJRec==0
drhd6e5e092009-01-07 02:03:35 +00001816 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)!=pPager->journalOff
1817 && ((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager))>0
drhee03d622009-01-07 15:33:45 +00001818 && pagerNextJournalPageIsValid(pPager))
drhd6e5e092009-01-07 02:03:35 +00001819 );
1820 if( nJRec==0
1821 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
1822 ){
1823 nJRec = (szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager);
danielk197775edc162004-06-26 01:48:18 +00001824 }
danielk197712dd5492008-12-18 15:45:07 +00001825 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
drhd6e5e092009-01-07 02:03:35 +00001826 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone);
danielk197776572402004-06-25 02:38:54 +00001827 assert( rc!=SQLITE_DONE );
danielk1977fd7f0452008-12-17 17:30:26 +00001828 }
1829 }
1830 assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
1831
drhd6e5e092009-01-07 02:03:35 +00001832 /* Finally, rollback pages from the sub-journal. Page that were
1833 ** previously rolled back out of the main journal (and are hence in pDone)
1834 ** will be skipped. Out-of-range pages are also skipped.
1835 */
danielk1977fd7f0452008-12-17 17:30:26 +00001836 if( pSavepoint ){
drhd6e5e092009-01-07 02:03:35 +00001837 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
drh49b9d332009-01-02 18:10:42 +00001838 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK&&ii<(u32)pPager->stmtNRec; ii++){
drhd6e5e092009-01-07 02:03:35 +00001839 assert( offset == ii*(4+pPager->pageSize) );
1840 rc = pager_playback_one_page(pPager, 0, &offset, 1, pDone);
danielk1977fd7f0452008-12-17 17:30:26 +00001841 assert( rc!=SQLITE_DONE );
danielk197776572402004-06-25 02:38:54 +00001842 }
drhfa86c412002-02-02 15:01:15 +00001843 }
danielk197776572402004-06-25 02:38:54 +00001844
danielk1977fd7f0452008-12-17 17:30:26 +00001845 sqlite3BitvecDestroy(pDone);
1846 if( rc==SQLITE_OK ){
danielk197775edc162004-06-26 01:48:18 +00001847 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001848 }
1849 return rc;
1850}
1851
1852/*
drhf57b14a2001-09-14 18:54:08 +00001853** Change the maximum number of in-memory pages that are allowed.
1854*/
danielk19773b8a05f2007-03-19 17:44:26 +00001855void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +00001856 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001857}
1858
1859/*
drh973b6e32003-02-12 14:09:42 +00001860** Adjust the robustness of the database to damage due to OS crashes
1861** or power failures by changing the number of syncs()s when writing
1862** the rollback journal. There are three levels:
1863**
drh054889e2005-11-30 03:20:31 +00001864** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001865** for temporary and transient files.
1866**
1867** NORMAL The journal is synced once before writes begin on the
1868** database. This is normally adequate protection, but
1869** it is theoretically possible, though very unlikely,
1870** that an inopertune power failure could leave the journal
1871** in a state which would cause damage to the database
1872** when it is rolled back.
1873**
1874** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001875** database (with some additional information - the nRec field
1876** of the journal header - being written in between the two
1877** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001878** single disk sector is atomic, then this mode provides
1879** assurance that the journal will not be corrupted to the
1880** point of causing damage to the database during rollback.
1881**
1882** Numeric values associated with these states are OFF==1, NORMAL=2,
1883** and FULL=3.
1884*/
danielk197793758c82005-01-21 08:13:14 +00001885#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh281b21d2008-08-22 12:57:08 +00001886void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
drh4f21c4a2008-12-10 22:15:00 +00001887 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
1888 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
drh281b21d2008-08-22 12:57:08 +00001889 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001890 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001891}
danielk197793758c82005-01-21 08:13:14 +00001892#endif
drh973b6e32003-02-12 14:09:42 +00001893
1894/*
drhaf6df112005-06-07 02:12:30 +00001895** The following global variable is incremented whenever the library
1896** attempts to open a temporary file. This information is used for
1897** testing and analysis only.
1898*/
drh0f7eb612006-08-08 13:51:43 +00001899#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001900int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001901#endif
drhaf6df112005-06-07 02:12:30 +00001902
1903/*
drh3f56e6e2007-03-15 01:16:47 +00001904** Open a temporary file.
1905**
1906** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001907** other error code if we fail. The OS will automatically delete the temporary
1908** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001909*/
danielk1977b4b47412007-08-17 15:53:36 +00001910static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00001911 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00001912 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00001913 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00001914){
drhfa86c412002-02-02 15:01:15 +00001915 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001916
drh0f7eb612006-08-08 13:51:43 +00001917#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001918 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001919#endif
danielk1977b4b47412007-08-17 15:53:36 +00001920
drh33f4e022007-09-03 15:19:34 +00001921 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1922 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00001923 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00001924 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00001925 return rc;
1926}
1927
danielk1977a858aa22008-08-22 16:22:17 +00001928static int pagerStress(void *,PgHdr *);
danielk19778c0a7912008-08-20 14:49:23 +00001929
drhfa86c412002-02-02 15:01:15 +00001930/*
drhed7c8552001-04-11 14:29:21 +00001931** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001932** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001933** the first call to sqlite3PagerGet() and is only held open until the
1934** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001935**
drh6446c4d2001-12-15 14:22:18 +00001936** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00001937** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00001938** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001939**
1940** If zFilename is ":memory:" then all information is held in cache.
1941** It is never written to disk. This can be used to implement an
1942** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001943*/
danielk19773b8a05f2007-03-19 17:44:26 +00001944int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001945 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001946 Pager **ppPager, /* Return the Pager structure here */
1947 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001948 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00001949 int flags, /* flags controlling this file */
1950 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00001951){
danielk1977b4b47412007-08-17 15:53:36 +00001952 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001953 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001954 int rc = SQLITE_OK;
1955 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001956 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001957 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001958 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001959 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1960 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977b3175382008-10-17 18:51:52 +00001961 int journalFileSize;
danielk19778c0a7912008-08-20 14:49:23 +00001962 int pcacheSize = sqlite3PcacheSize();
drhfacf0302008-06-17 15:12:00 +00001963 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
danielk197717b90b52008-06-06 11:11:25 +00001964 char *zPathname = 0;
1965 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001966
danielk1977b3175382008-10-17 18:51:52 +00001967 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
1968 journalFileSize = sqlite3JournalSize(pVfs);
1969 }else{
1970 journalFileSize = sqlite3MemJournalSize();
1971 }
1972
drh86f8c192007-08-22 00:39:19 +00001973 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001974 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001975
danielk197717b90b52008-06-06 11:11:25 +00001976 /* Compute and store the full pathname in an allocated buffer pointed
1977 ** to by zPathname, length nPathname. Or, if this is a temporary file,
1978 ** leave both nPathname and zPathname set to 0.
1979 */
drh1cc8c442007-08-24 16:08:29 +00001980 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00001981 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001982 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00001983 if( zPathname==0 ){
1984 return SQLITE_NOMEM;
1985 }
drh1cc8c442007-08-24 16:08:29 +00001986#ifndef SQLITE_OMIT_MEMORYDB
1987 if( strcmp(zFilename,":memory:")==0 ){
1988 memDb = 1;
1989 zPathname[0] = 0;
1990 }else
1991#endif
1992 {
danielk1977adfb9b02007-09-17 07:02:56 +00001993 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00001994 }
danielk197717b90b52008-06-06 11:11:25 +00001995 if( rc!=SQLITE_OK ){
1996 sqlite3_free(zPathname);
1997 return rc;
1998 }
drhea678832008-12-10 19:26:22 +00001999 nPathname = sqlite3Strlen30(zPathname);
drhae28c012007-08-24 16:29:23 +00002000 }
drh99b90c32008-03-17 13:50:58 +00002001
danielk1977b4b47412007-08-17 15:53:36 +00002002 /* Allocate memory for the pager structure */
2003 pPager = sqlite3MallocZero(
2004 sizeof(*pPager) + /* Pager structure */
danielk19778c0a7912008-08-20 14:49:23 +00002005 pcacheSize + /* PCache object */
danielk1977c7b60172007-08-22 11:22:03 +00002006 journalFileSize + /* The journal file structure */
danielk1977b3175382008-10-17 18:51:52 +00002007 pVfs->szOsFile + /* The main db file */
2008 journalFileSize * 2 + /* The two journal files */
danielk197717b90b52008-06-06 11:11:25 +00002009 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00002010 );
2011 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002012 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002013 return SQLITE_NOMEM;
2014 }
danielk19778c0a7912008-08-20 14:49:23 +00002015 pPager->pPCache = (PCache *)&pPager[1];
2016 pPtr = ((u8 *)&pPager[1]) + pcacheSize;
drh33f4e022007-09-03 15:19:34 +00002017 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002018 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977fd7f0452008-12-17 17:30:26 +00002019 pPager->sjfd = (sqlite3_file*)&pPtr[pVfs->szOsFile];
danielk1977b3175382008-10-17 18:51:52 +00002020 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile+journalFileSize];
2021 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile+2*journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002022 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2023 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00002024 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00002025 if( zPathname ){
2026 memcpy(pPager->zFilename, zPathname, nPathname+1);
2027 sqlite3_free(zPathname);
2028 }
danielk1977b4b47412007-08-17 15:53:36 +00002029
drh153c62c2007-08-24 03:51:33 +00002030 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002031 */
drhae28c012007-08-24 16:29:23 +00002032 if( zFilename && zFilename[0] && !memDb ){
danielk1977d92db532008-11-17 04:56:24 +00002033 if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){
drh1cc8c442007-08-24 16:08:29 +00002034 rc = SQLITE_CANTOPEN;
2035 }else{
drh1cc8c442007-08-24 16:08:29 +00002036 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002037 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2038 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002039 readOnly = (fout&SQLITE_OPEN_READONLY);
2040
2041 /* If the file was successfully opened for read/write access,
2042 ** choose a default page size in case we have to create the
2043 ** database file. The default page size is the maximum of:
2044 **
2045 ** + SQLITE_DEFAULT_PAGE_SIZE,
2046 ** + The value returned by sqlite3OsSectorSize()
2047 ** + The largest page size that can be written atomically.
2048 */
2049 if( rc==SQLITE_OK && !readOnly ){
danielk19777cbd5892009-01-10 16:15:09 +00002050 setSectorSize(pPager);
2051 if( szPageDflt<pPager->sectorSize ){
2052 szPageDflt = pPager->sectorSize;
drh1cc8c442007-08-24 16:08:29 +00002053 }
danielk19779663b8f2007-08-24 11:52:28 +00002054#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002055 {
2056 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2057 int ii;
2058 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2059 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2060 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
drhfacf0302008-06-17 15:12:00 +00002061 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2062 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002063 }
danielk1977b4b47412007-08-17 15:53:36 +00002064 }
drh1cc8c442007-08-24 16:08:29 +00002065#endif
drhfacf0302008-06-17 15:12:00 +00002066 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2067 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
drh1cc8c442007-08-24 16:08:29 +00002068 }
danielk19778def5ea2004-06-16 10:39:52 +00002069 }
drhac69b052004-05-12 13:30:07 +00002070 }
danielk1977b3175382008-10-17 18:51:52 +00002071 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00002072 /* If a temporary file is requested, it is not opened immediately.
2073 ** In this case we accept the default page size and delay actually
2074 ** opening the file until the first call to OsWrite().
danielk1977b3175382008-10-17 18:51:52 +00002075 **
2076 ** This branch is also run for an in-memory database. An in-memory
2077 ** database is the same as a temp-file that is never written out to
2078 ** disk and uses an in-memory rollback journal.
danielk19777a2b1ee2007-08-21 14:27:01 +00002079 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002080 tempFile = 1;
2081 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002082 }
danielk1977aef0bf62005-12-30 16:28:01 +00002083
danielk1977b4b47412007-08-17 15:53:36 +00002084 if( pPager && rc==SQLITE_OK ){
drhfacf0302008-06-17 15:12:00 +00002085 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00002086 }
danielk1977aef0bf62005-12-30 16:28:01 +00002087
drh153c62c2007-08-24 03:51:33 +00002088 /* If an error occured in either of the blocks above.
2089 ** Free the Pager structure and close the file.
2090 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002091 ** any Pager.errMask variables.
2092 */
danielk1977b4b47412007-08-17 15:53:36 +00002093 if( !pPager || !pPager->pTmpSpace ){
2094 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002095 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002096 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002097 }
danielk19778c0a7912008-08-20 14:49:23 +00002098 nExtra = FORCE_ALIGNMENT(nExtra);
danielk197771d5d2c2008-09-29 11:49:47 +00002099 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
drh41d30272008-08-20 21:47:45 +00002100 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
danielk1977aef0bf62005-12-30 16:28:01 +00002101
drh30d53702009-01-06 15:58:57 +00002102 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
drh153c62c2007-08-24 03:51:33 +00002103 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002104
danielk1977b4b47412007-08-17 15:53:36 +00002105 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002106 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
drhea678832008-12-10 19:26:22 +00002107 for(i=sqlite3Strlen30(pPager->zDirectory);
2108 i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002109 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002110
drh99b90c32008-03-17 13:50:58 +00002111 /* Fill in Pager.zJournal[] */
danielk197717b90b52008-06-06 11:11:25 +00002112 if( zPathname ){
2113 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2114 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
2115 }else{
2116 pPager->zJournal = 0;
2117 }
danielk1977b4b47412007-08-17 15:53:36 +00002118
drh3b59a5c2006-01-15 20:28:28 +00002119 /* pPager->journalOpen = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002120 pPager->useJournal = (u8)useJournal;
2121 pPager->noReadlock = (noReadlock && readOnly) ?1:0;
drh3b59a5c2006-01-15 20:28:28 +00002122 /* pPager->stmtOpen = 0; */
2123 /* pPager->stmtInUse = 0; */
2124 /* pPager->nRef = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002125 pPager->dbSizeValid = (u8)memDb;
drhfacf0302008-06-17 15:12:00 +00002126 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00002127 /* pPager->stmtSize = 0; */
2128 /* pPager->stmtJSize = 0; */
2129 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002130 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002131 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002132 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002133 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002134 /* pPager->errMask = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002135 pPager->tempFile = (u8)tempFile;
drh369339d2007-03-30 16:01:55 +00002136 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2137 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2138 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
drh4f21c4a2008-12-10 22:15:00 +00002139 pPager->exclusiveMode = (u8)tempFile;
2140 pPager->memDb = (u8)memDb;
2141 pPager->readOnly = (u8)readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002142 /* pPager->needSync = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002143 pPager->noSync = (pPager->tempFile || !useJournal) ?1:0;
2144 pPager->fullSync = pPager->noSync ?0:1;
danielk1977f036aef2007-08-20 05:36:51 +00002145 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002146 /* pPager->pFirst = 0; */
2147 /* pPager->pFirstSynced = 0; */
2148 /* pPager->pLast = 0; */
danielk19778c0a7912008-08-20 14:49:23 +00002149 pPager->nExtra = nExtra;
danielk1977b53e4962008-06-04 06:45:59 +00002150 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk1977b3175382008-10-17 18:51:52 +00002151 assert(pPager->fd->pMethods||tempFile);
2152 setSectorSize(pPager);
2153 if( memDb ){
2154 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
danielk1977b4721172007-03-19 05:54:48 +00002155 }
danielk19771ceedd32008-11-19 10:22:33 +00002156 /* pPager->xBusyHandler = 0; */
2157 /* pPager->pBusyHandlerArg = 0; */
drh3b59a5c2006-01-15 20:28:28 +00002158 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002159 *ppPager = pPager;
2160 return SQLITE_OK;
2161}
2162
2163/*
drh90f5ecb2004-07-22 01:19:35 +00002164** Set the busy handler function.
2165*/
danielk19771ceedd32008-11-19 10:22:33 +00002166void sqlite3PagerSetBusyhandler(
2167 Pager *pPager,
2168 int (*xBusyHandler)(void *),
2169 void *pBusyHandlerArg
2170){
2171 pPager->xBusyHandler = xBusyHandler;
2172 pPager->pBusyHandlerArg = pBusyHandlerArg;
drh90f5ecb2004-07-22 01:19:35 +00002173}
2174
2175/*
drha6abd042004-06-09 17:37:22 +00002176** Set the reinitializer for this pager. If not NULL, the reinitializer
2177** is called when the content of a page in cache is restored to its original
2178** value as a result of a rollback. The callback gives higher-level code
2179** an opportunity to restore the EXTRA section to agree with the restored
2180** page data.
2181*/
danielk1977eaa06f62008-09-18 17:34:44 +00002182void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
drha6abd042004-06-09 17:37:22 +00002183 pPager->xReiniter = xReinit;
2184}
2185
2186/*
danielk1977a1644fd2007-08-29 12:31:25 +00002187** Set the page size to *pPageSize. If the suggest new page size is
2188** inappropriate, then an alternative page size is set to that
2189** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002190*/
danielk1977a1644fd2007-08-29 12:31:25 +00002191int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
danielk19771357d9f2008-09-16 05:12:24 +00002192 int rc = pPager->errCode;
2193 if( rc==SQLITE_OK ){
2194 u16 pageSize = *pPageSize;
2195 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
2196 if( pageSize && pageSize!=pPager->pageSize
2197 && (pPager->memDb==0 || pPager->dbSize==0)
2198 && sqlite3PcacheRefCount(pPager->pPCache)==0
2199 ){
2200 char *pNew = (char *)sqlite3PageMalloc(pageSize);
2201 if( !pNew ){
2202 rc = SQLITE_NOMEM;
2203 }else{
2204 pager_reset(pPager);
2205 pPager->pageSize = pageSize;
2206 if( !pPager->memDb ) setSectorSize(pPager);
2207 sqlite3PageFree(pPager->pTmpSpace);
2208 pPager->pTmpSpace = pNew;
2209 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
2210 }
danielk1977a1644fd2007-08-29 12:31:25 +00002211 }
drh4f21c4a2008-12-10 22:15:00 +00002212 *pPageSize = (u16)pPager->pageSize;
drh1c7880e2005-05-20 20:01:55 +00002213 }
danielk1977a1644fd2007-08-29 12:31:25 +00002214 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002215}
2216
2217/*
drh26b79942007-11-28 16:19:56 +00002218** Return a pointer to the "temporary page" buffer held internally
2219** by the pager. This is a buffer that is big enough to hold the
2220** entire content of a database page. This buffer is used internally
2221** during rollback and will be overwritten whenever a rollback
2222** occurs. But other modules are free to use it too, as long as
2223** no rollbacks are happening.
2224*/
2225void *sqlite3PagerTempSpace(Pager *pPager){
2226 return pPager->pTmpSpace;
2227}
2228
2229/*
drhf8e632b2007-05-08 14:51:36 +00002230** Attempt to set the maximum database page count if mxPage is positive.
2231** Make no changes if mxPage is zero or negative. And never reduce the
2232** maximum page count below the current size of the database.
2233**
2234** Regardless of mxPage, return the current maximum page count.
2235*/
2236int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2237 if( mxPage>0 ){
2238 pPager->mxPgno = mxPage;
2239 }
danielk1977ad0132d2008-06-07 08:58:22 +00002240 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002241 return pPager->mxPgno;
2242}
2243
2244/*
drhc9ac5ca2005-11-04 22:03:30 +00002245** The following set of routines are used to disable the simulated
2246** I/O error mechanism. These routines are used to avoid simulated
2247** errors in places where we do not care about errors.
2248**
2249** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2250** and generate no code.
2251*/
2252#ifdef SQLITE_TEST
2253extern int sqlite3_io_error_pending;
2254extern int sqlite3_io_error_hit;
2255static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002256void disable_simulated_io_errors(void){
2257 saved_cnt = sqlite3_io_error_pending;
2258 sqlite3_io_error_pending = -1;
2259}
2260void enable_simulated_io_errors(void){
2261 sqlite3_io_error_pending = saved_cnt;
2262}
2263#else
drh152410f2005-11-05 15:11:22 +00002264# define disable_simulated_io_errors()
2265# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002266#endif
2267
2268/*
drh90f5ecb2004-07-22 01:19:35 +00002269** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002270** that pDest points to.
2271**
2272** No error checking is done. The rational for this is that this function
2273** may be called even if the file does not exist or contain a header. In
2274** these cases sqlite3OsRead() will return an error, to which the correct
2275** response is to zero the memory at pDest and continue. A real IO error
2276** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002277*/
danielk19773b8a05f2007-03-19 17:44:26 +00002278int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002279 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002280 memset(pDest, 0, N);
danielk1977b3175382008-10-17 18:51:52 +00002281 assert(pPager->fd->pMethods||pPager->tempFile);
danielk19777a2b1ee2007-08-21 14:27:01 +00002282 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002283 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002284 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002285 if( rc==SQLITE_IOERR_SHORT_READ ){
2286 rc = SQLITE_OK;
2287 }
drh90f5ecb2004-07-22 01:19:35 +00002288 }
drh551b7732006-11-06 21:20:25 +00002289 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002290}
2291
2292/*
drh5e00f6c2001-09-13 13:46:56 +00002293** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002294** pPager.
2295**
2296** If the PENDING_BYTE lies on the page directly after the end of the
2297** file, then consider this page part of the file too. For example, if
2298** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2299** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002300*/
danielk1977ad0132d2008-06-07 08:58:22 +00002301int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk19777a2b1ee2007-08-21 14:27:01 +00002302 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002303 int rc;
drhd9b02572001-04-15 00:37:09 +00002304 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002305 if( pPager->errCode ){
danielk19778c0a7912008-08-20 14:49:23 +00002306 rc = pPager->errCode;
danielk19778c0a7912008-08-20 14:49:23 +00002307 return rc;
drha7aea3d2007-03-15 12:51:16 +00002308 }
danielk1977d92db532008-11-17 04:56:24 +00002309 if( pPager->dbSizeValid ){
danielk197715f411d2005-09-16 10:18:45 +00002310 n = pPager->dbSize;
2311 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002312 assert(pPager->fd->pMethods||pPager->tempFile);
2313 if( (pPager->fd->pMethods)
2314 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002315 pager_error(pPager, rc);
danielk1977ad0132d2008-06-07 08:58:22 +00002316 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002317 }
2318 if( n>0 && n<pPager->pageSize ){
2319 n = 1;
2320 }else{
2321 n /= pPager->pageSize;
2322 }
2323 if( pPager->state!=PAGER_UNLOCK ){
danielk19773460d192008-12-27 15:23:13 +00002324 pPager->dbSize = (Pgno)n;
2325 pPager->dbFileSize = (Pgno)n;
danielk1977d92db532008-11-17 04:56:24 +00002326 pPager->dbSizeValid = 1;
danielk197715f411d2005-09-16 10:18:45 +00002327 }
drhed7c8552001-04-11 14:29:21 +00002328 }
danielk197715f411d2005-09-16 10:18:45 +00002329 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002330 n++;
2331 }
drhf8e632b2007-05-08 14:51:36 +00002332 if( n>pPager->mxPgno ){
drh4f21c4a2008-12-10 22:15:00 +00002333 pPager->mxPgno = (Pgno)n;
drhf8e632b2007-05-08 14:51:36 +00002334 }
danielk1977ad0132d2008-06-07 08:58:22 +00002335 if( pnPage ){
drh4f21c4a2008-12-10 22:15:00 +00002336 *pnPage = (int)n;
danielk1977ad0132d2008-06-07 08:58:22 +00002337 }
2338 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002339}
2340
2341/*
drhf7c57532003-04-25 13:22:51 +00002342** Forward declaration
2343*/
danielk197776572402004-06-25 02:38:54 +00002344static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002345
2346/*
danielk197717221812005-02-15 03:38:05 +00002347** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002348** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002349** false or until the lock succeeds.
2350**
2351** Return SQLITE_OK on success and an error code if we cannot obtain
2352** the lock.
2353*/
2354static int pager_wait_on_lock(Pager *pPager, int locktype){
2355 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002356
2357 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002358 assert( PAGER_SHARED==SHARED_LOCK );
2359 assert( PAGER_RESERVED==RESERVED_LOCK );
2360 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002361
2362 /* If the file is currently unlocked then the size must be unknown */
danielk1977d92db532008-11-17 04:56:24 +00002363 assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
drh1aa2d8b2007-01-03 15:34:29 +00002364
danielk197717221812005-02-15 03:38:05 +00002365 if( pPager->state>=locktype ){
2366 rc = SQLITE_OK;
2367 }else{
danielk197717221812005-02-15 03:38:05 +00002368 do {
drh054889e2005-11-30 03:20:31 +00002369 rc = sqlite3OsLock(pPager->fd, locktype);
danielk19771ceedd32008-11-19 10:22:33 +00002370 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
danielk197717221812005-02-15 03:38:05 +00002371 if( rc==SQLITE_OK ){
drh4f21c4a2008-12-10 22:15:00 +00002372 pPager->state = (u8)locktype;
drhb0603412007-02-28 04:47:26 +00002373 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002374 }
2375 }
2376 return rc;
2377}
2378
danielk19773460d192008-12-27 15:23:13 +00002379#ifndef SQLITE_OMIT_AUTOVACUUM
2380/*
danielk1977f90b7262009-01-07 15:18:20 +00002381** Truncate the in-memory database file image to nPage pages. This
2382** function does not actually modify the database file on disk. It
2383** just sets the internal state of the pager object so that the
2384** truncation will be done when the current transaction is committed.
danielk19773460d192008-12-27 15:23:13 +00002385*/
2386void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
2387 assert( pPager->dbSizeValid );
2388 assert( pPager->dbSize>=nPage );
2389 pPager->dbSize = nPage;
2390}
2391
2392/*
2393** Return the current size of the database file image in pages. This
2394** function differs from sqlite3PagerPagecount() in two ways:
2395**
2396** a) It may only be called when at least one reference to a database
2397** page is held. This guarantees that the database size is already
2398** known and a call to sqlite3OsFileSize() is not required.
2399**
2400** b) The return value is not adjusted for the locking page.
2401*/
2402Pgno sqlite3PagerImageSize(Pager *pPager){
2403 assert( pPager->dbSizeValid );
2404 return pPager->dbSize;
2405}
2406#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
2407
drhf7c57532003-04-25 13:22:51 +00002408/*
drhed7c8552001-04-11 14:29:21 +00002409** Shutdown the page cache. Free all memory and close all files.
2410**
2411** If a transaction was in progress when this routine is called, that
2412** transaction is rolled back. All outstanding pages are invalidated
2413** and their memory is freed. Any attempt to use a page associated
2414** with this page cache after this function returns will likely
2415** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002416**
2417** This function always succeeds. If a transaction is active an attempt
2418** is made to roll it back. If an error occurs during the rollback
2419** a hot journal may be left in the filesystem but no error is returned
2420** to the caller.
drhed7c8552001-04-11 14:29:21 +00002421*/
danielk19773b8a05f2007-03-19 17:44:26 +00002422int sqlite3PagerClose(Pager *pPager){
danielk197713f72992005-12-18 08:51:22 +00002423
drhbafda092007-01-03 23:36:22 +00002424 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002425 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002426 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002427 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002428 pager_reset(pPager);
danielk1977b3175382008-10-17 18:51:52 +00002429 if( !MEMDB ){
danielk1977f2c31ad2009-01-06 13:40:08 +00002430 /* Set Pager.journalHdr to -1 for the benefit of the pager_playback()
2431 ** call which may be made from within pagerUnlockAndRollback(). If it
2432 ** is not -1, then the unsynced portion of an open journal file may
2433 ** be played back into the database. If a power failure occurs while
2434 ** this is happening, the database may become corrupt.
2435 */
2436 pPager->journalHdr = -1;
danielk1977b3175382008-10-17 18:51:52 +00002437 pagerUnlockAndRollback(pPager);
2438 }
drhbafda092007-01-03 23:36:22 +00002439 enable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002440 sqlite3EndBenignMalloc();
drh30d53702009-01-06 15:58:57 +00002441 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
drhb0603412007-02-28 04:47:26 +00002442 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002443 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002444 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002445 }
drhf5e7bb52008-02-18 14:47:33 +00002446 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977a1fa00d2008-08-27 15:16:33 +00002447 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
danielk1977fd7f0452008-12-17 17:30:26 +00002448 releaseAllSavepoint(pPager);
danielk1977b4b47412007-08-17 15:53:36 +00002449 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002450 /* Temp files are automatically deleted by the OS
2451 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002452 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002453 ** }
2454 */
danielk1977aca790a2005-01-13 11:07:52 +00002455
drhfacf0302008-06-17 15:12:00 +00002456 sqlite3PageFree(pPager->pTmpSpace);
danielk19778c0a7912008-08-20 14:49:23 +00002457 sqlite3PcacheClose(pPager->pPCache);
drh17435752007-08-16 04:30:38 +00002458 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002459 return SQLITE_OK;
2460}
2461
drh87cc3b32007-05-08 21:45:27 +00002462#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002463/*
drh5e00f6c2001-09-13 13:46:56 +00002464** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002465*/
danielk19773b8a05f2007-03-19 17:44:26 +00002466Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002467 return p->pgno;
2468}
drh87cc3b32007-05-08 21:45:27 +00002469#endif
drhed7c8552001-04-11 14:29:21 +00002470
2471/*
drhdf0b3b02001-06-23 11:36:20 +00002472** Increment the reference count for a page. The input pointer is
2473** a reference to the page data.
2474*/
danielk19773b8a05f2007-03-19 17:44:26 +00002475int sqlite3PagerRef(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002476 sqlite3PcacheRef(pPg);
drh8c42ca92001-06-22 19:15:00 +00002477 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002478}
2479
2480/*
drh34e79ce2004-02-08 06:05:46 +00002481** Sync the journal. In other words, make sure all the pages that have
2482** been written to the journal have actually reached the surface of the
2483** disk. It is not safe to modify the original database file until after
2484** the journal has been synced. If the original database is modified before
2485** the journal is synced and a power failure occurs, the unsynced journal
2486** data would be lost and we would be unable to completely rollback the
2487** database changes. Database corruption would occur.
2488**
2489** This routine also updates the nRec field in the header of the journal.
2490** (See comments on the pager_playback() routine for additional information.)
2491** If the sync mode is FULL, two syncs will occur. First the whole journal
2492** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002493**
drh34e79ce2004-02-08 06:05:46 +00002494** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002495** after a power failure, so no sync occurs.
2496**
2497** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2498** the database is stored, then OsSync() is never called on the journal
2499** file. In this case all that is required is to update the nRec field in
2500** the journal header.
drhfa86c412002-02-02 15:01:15 +00002501**
drh34e79ce2004-02-08 06:05:46 +00002502** This routine clears the needSync field of every page current held in
2503** memory.
drh50e5dad2001-09-15 00:57:28 +00002504*/
danielk197776572402004-06-25 02:38:54 +00002505static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002506 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002507
2508 /* Sync the journal before modifying the main database
2509 ** (assuming there is a journal and it needs to be synced.)
2510 */
danielk197776572402004-06-25 02:38:54 +00002511 if( pPager->needSync ){
danielk1977b3175382008-10-17 18:51:52 +00002512 assert( !pPager->tempFile );
2513 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
danielk19774cd2cd52007-08-23 14:48:23 +00002514 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002515 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002516
danielk19774cd2cd52007-08-23 14:48:23 +00002517 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk1977112f7522009-01-08 17:50:45 +00002518 i64 jrnlOff = journalHdrOffset(pPager);
2519 u8 zMagic[8];
2520
2521 /* This block deals with an obscure problem. If the last connection
2522 ** that wrote to this database was operating in persistent-journal
2523 ** mode, then the journal file may at this point actually be larger
2524 ** than Pager.journalOff bytes. If the next thing in the journal
2525 ** file happens to be a journal-header (written as part of the
2526 ** previous connections transaction), and a crash or power-failure
2527 ** occurs after nRec is updated but before this connection writes
2528 ** anything else to the journal file (or commits/rolls back its
2529 ** transaction), then SQLite may become confused when doing the
2530 ** hot-journal rollback following recovery. It may roll back all
2531 ** of this connections data, then proceed to rolling back the old,
2532 ** out-of-date data that follows it. Database corruption.
2533 **
2534 ** To work around this, if the journal file does appear to contain
2535 ** a valid header following Pager.journalOff, then write a 0x00
2536 ** byte to the start of it to prevent it from being recognized.
2537 */
2538 rc = sqlite3OsRead(pPager->jfd, zMagic, 8, jrnlOff);
2539 if( rc==SQLITE_OK && 0==memcmp(zMagic, aJournalMagic, 8) ){
2540 static const u8 zerobyte = 0;
2541 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, jrnlOff);
2542 }
2543 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
2544 return rc;
2545 }
2546
danielk197776572402004-06-25 02:38:54 +00002547 /* Write the nRec value into the journal file header. If in
2548 ** full-synchronous mode, sync the journal first. This ensures that
2549 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002550 ** it as a candidate for rollback.
2551 **
2552 ** This is not required if the persistent media supports the
2553 ** SAFE_APPEND property. Because in this case it is not possible
2554 ** for garbage data to be appended to the file, the nRec field
2555 ** is populated with 0xFFFFFFFF when the journal header is written
2556 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002557 */
danielk19774cd2cd52007-08-23 14:48:23 +00002558 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh30d53702009-01-06 15:58:57 +00002559 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
drhb0603412007-02-28 04:47:26 +00002560 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002561 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002562 if( rc!=0 ) return rc;
2563 }
danielk197713adf8a2004-06-03 16:08:41 +00002564
danielk197762079062007-08-15 17:08:46 +00002565 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2566 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2567 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002568 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002569 }
danielk19774cd2cd52007-08-23 14:48:23 +00002570 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh30d53702009-01-06 15:58:57 +00002571 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
danielk19774cd2cd52007-08-23 14:48:23 +00002572 IOTRACE(("JSYNC %p\n", pPager))
2573 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2574 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2575 );
2576 if( rc!=0 ) return rc;
2577 }
drhdb48ee02003-01-16 13:42:43 +00002578 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002579 }
drh50e5dad2001-09-15 00:57:28 +00002580 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002581
2582 /* Erase the needSync flag from every page.
2583 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00002584 sqlite3PcacheClearSyncFlags(pPager->pPCache);
drh50e5dad2001-09-15 00:57:28 +00002585 }
drh03eb96a2002-11-10 23:32:56 +00002586
drh81a20f22001-10-12 17:30:04 +00002587 return rc;
drh50e5dad2001-09-15 00:57:28 +00002588}
2589
2590/*
drh2554f8b2003-01-22 01:26:44 +00002591** Given a list of pages (connected by the PgHdr.pDirty pointer) write
danielk1977a858aa22008-08-22 16:22:17 +00002592** every one of those pages out to the database file. No calls are made
2593** to the page-cache to mark the pages as clean. It is the responsibility
2594** of the caller to use PcacheCleanAll() or PcacheMakeClean() to mark
2595** the pages as clean.
drh2554f8b2003-01-22 01:26:44 +00002596*/
2597static int pager_write_pagelist(PgHdr *pList){
2598 Pager *pPager;
2599 int rc;
2600
2601 if( pList==0 ) return SQLITE_OK;
2602 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002603
2604 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2605 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002606 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002607 **
drha6abd042004-06-09 17:37:22 +00002608 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2609 ** through an intermediate state PENDING. A PENDING lock prevents new
2610 ** readers from attaching to the database but is unsufficient for us to
2611 ** write. The idea of a PENDING lock is to prevent new readers from
2612 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002613 **
drha6abd042004-06-09 17:37:22 +00002614 ** While the pager is in the RESERVED state, the original database file
2615 ** is unchanged and we can rollback without having to playback the
2616 ** journal into the original database file. Once we transition to
2617 ** EXCLUSIVE, it means the database file has been changed and any rollback
2618 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002619 */
drh684917c2004-10-05 02:41:42 +00002620 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002621 if( rc!=SQLITE_OK ){
2622 return rc;
2623 }
2624
drh2554f8b2003-01-22 01:26:44 +00002625 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002626
2627 /* If the file has not yet been opened, open it now. */
2628 if( !pPager->fd->pMethods ){
2629 assert(pPager->tempFile);
danielk197717b90b52008-06-06 11:11:25 +00002630 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002631 if( rc ) return rc;
2632 }
2633
danielk1977687566d2004-11-02 12:56:41 +00002634 /* If there are dirty pages in the page cache with page numbers greater
danielk1977f90b7262009-01-07 15:18:20 +00002635 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
danielk1977687566d2004-11-02 12:56:41 +00002636 ** make the file smaller (presumably by auto-vacuum code). Do not write
2637 ** any such pages to the file.
2638 */
danielk197733e32162008-08-23 18:53:08 +00002639 if( pList->pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
danielk197762079062007-08-15 17:08:46 +00002640 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002641 char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6);
danielk197712dd5492008-12-18 15:45:07 +00002642
drh30d53702009-01-06 15:58:57 +00002643 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
2644 PAGERID(pPager), pList->pgno, pager_pagehash(pList)));
drh538f5702007-04-13 02:14:30 +00002645 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002646 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002647 PAGER_INCR(sqlite3_pager_writedb_count);
2648 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002649 if( pList->pgno==1 ){
2650 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2651 }
danielk19773460d192008-12-27 15:23:13 +00002652 if( pList->pgno>pPager->dbFileSize ){
2653 pPager->dbFileSize = pList->pgno;
2654 }
danielk1977687566d2004-11-02 12:56:41 +00002655 }
2656#ifndef NDEBUG
2657 else{
drh30d53702009-01-06 15:58:57 +00002658 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno));
danielk1977687566d2004-11-02 12:56:41 +00002659 }
2660#endif
drh2554f8b2003-01-22 01:26:44 +00002661 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002662#ifdef SQLITE_CHECK_PAGES
2663 pList->pageHash = pager_pagehash(pList);
2664#endif
drh2554f8b2003-01-22 01:26:44 +00002665 pList = pList->pDirty;
2666 }
danielk19778c0a7912008-08-20 14:49:23 +00002667
drh2554f8b2003-01-22 01:26:44 +00002668 return SQLITE_OK;
2669}
2670
2671/*
danielk1977f2c31ad2009-01-06 13:40:08 +00002672** Add the page to the sub-journal. It is the callers responsibility to
2673** use subjRequiresPage() to check that it is really required before
2674** calling this function.
2675*/
2676static int subjournalPage(PgHdr *pPg){
2677 int rc;
2678 void *pData = pPg->pData;
2679 Pager *pPager = pPg->pPager;
2680 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
2681 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
2682
drh30d53702009-01-06 15:58:57 +00002683 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
danielk1977f2c31ad2009-01-06 13:40:08 +00002684
2685 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
2686 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
2687 if( rc==SQLITE_OK ){
2688 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
2689 }
2690 if( rc==SQLITE_OK ){
2691 pPager->stmtNRec++;
2692 assert( pPager->nSavepoint>0 );
2693 rc = addToSavepointBitvecs(pPager, pPg->pgno);
2694 }
2695 return rc;
2696}
2697
2698
2699/*
danielk19778c0a7912008-08-20 14:49:23 +00002700** This function is called by the pcache layer when it has reached some
2701** soft memory limit. The argument is a pointer to a purgeable Pager
2702** object. This function attempts to make a single dirty page that has no
2703** outstanding references (if one exists) clean so that it can be recycled
2704** by the pcache layer.
drh2554f8b2003-01-22 01:26:44 +00002705*/
danielk1977a858aa22008-08-22 16:22:17 +00002706static int pagerStress(void *p, PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002707 Pager *pPager = (Pager *)p;
danielk19778c0a7912008-08-20 14:49:23 +00002708 int rc = SQLITE_OK;
drh8f2e9a12008-02-26 14:46:04 +00002709
danielk19778c200142008-08-25 07:12:28 +00002710 if( pPager->doNotSync ){
2711 return SQLITE_OK;
2712 }
2713
danielk1977a858aa22008-08-22 16:22:17 +00002714 assert( pPg->flags&PGHDR_DIRTY );
2715 if( pPager->errCode==SQLITE_OK ){
2716 if( pPg->flags&PGHDR_NEED_SYNC ){
2717 rc = syncJournal(pPager);
2718 if( rc==SQLITE_OK && pPager->fullSync &&
danielk1977b3175382008-10-17 18:51:52 +00002719 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
danielk1977a858aa22008-08-22 16:22:17 +00002720 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
2721 ){
2722 pPager->nRec = 0;
2723 rc = writeJournalHdr(pPager);
danielk19778c0a7912008-08-20 14:49:23 +00002724 }
danielk19778c0a7912008-08-20 14:49:23 +00002725 }
danielk1977a858aa22008-08-22 16:22:17 +00002726 if( rc==SQLITE_OK ){
2727 pPg->pDirty = 0;
danielk1977f2c31ad2009-01-06 13:40:08 +00002728 if( pPg->pgno>pPager->dbSize && subjRequiresPage(pPg) ){
2729 rc = subjournalPage(pPg);
2730 }
2731 if( rc==SQLITE_OK ){
2732 rc = pager_write_pagelist(pPg);
2733 }
danielk1977a858aa22008-08-22 16:22:17 +00002734 }
2735 if( rc!=SQLITE_OK ){
2736 pager_error(pPager, rc);
2737 }
2738 }
2739
2740 if( rc==SQLITE_OK ){
drh30d53702009-01-06 15:58:57 +00002741 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
danielk1977a858aa22008-08-22 16:22:17 +00002742 sqlite3PcacheMakeClean(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00002743 }
2744 return rc;
drh2554f8b2003-01-22 01:26:44 +00002745}
2746
danielk19778c0a7912008-08-20 14:49:23 +00002747
drh2554f8b2003-01-22 01:26:44 +00002748/*
drh19db9352008-03-27 22:42:51 +00002749** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00002750** A hot journal is one that needs to be played back.
2751**
2752** If the current size of the database file is 0 but a journal file
2753** exists, that is probably an old journal left over from a prior
2754** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00002755**
2756** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00002757**
2758** This routine does not open the journal file to examine its
2759** content. Hence, the journal might contain the name of a master
2760** journal file that has been deleted, and hence not be hot. Or
2761** the header of the journal might be zeroed out. This routine
2762** does not discover these cases of a non-hot journal - if the
2763** journal file exists and is not empty this routine assumes it
2764** is hot. The pager_playback() routine will discover that the
2765** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00002766*/
danielk1977d300b8a2008-08-01 10:50:23 +00002767static int hasHotJournal(Pager *pPager, int *pExists){
danielk1977b4b47412007-08-17 15:53:36 +00002768 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977d300b8a2008-08-01 10:50:23 +00002769 int rc = SQLITE_OK;
drhea678832008-12-10 19:26:22 +00002770 int exists = 0;
2771 int locked = 0;
drh0a846f92008-08-25 17:23:29 +00002772 assert( pPager!=0 );
2773 assert( pPager->useJournal );
2774 assert( pPager->fd->pMethods );
danielk1977d300b8a2008-08-01 10:50:23 +00002775 *pExists = 0;
drh0a846f92008-08-25 17:23:29 +00002776 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
2777 if( rc==SQLITE_OK && exists ){
2778 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
2779 }
2780 if( rc==SQLITE_OK && exists && !locked ){
2781 int nPage;
2782 rc = sqlite3PagerPagecount(pPager, &nPage);
2783 if( rc==SQLITE_OK ){
2784 if( nPage==0 ){
2785 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2786 }else{
2787 *pExists = 1;
danielk1977861f7452008-06-05 11:39:11 +00002788 }
2789 }
drhbb5f18d2007-04-06 18:23:17 +00002790 }
danielk1977d300b8a2008-08-01 10:50:23 +00002791 return rc;
drh165ffe92005-03-15 17:09:30 +00002792}
2793
2794/*
danielk1977e180dd92007-04-05 17:15:52 +00002795** Read the content of page pPg out of the database file.
2796*/
2797static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2798 int rc;
danielk197762079062007-08-15 17:08:46 +00002799 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002800 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002801 assert(pPager->fd->pMethods||pPager->tempFile);
2802 if( !pPager->fd->pMethods ){
2803 return SQLITE_IOERR_SHORT_READ;
2804 }
danielk197762079062007-08-15 17:08:46 +00002805 offset = (pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002806 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002807 PAGER_INCR(sqlite3_pager_readdb_count);
2808 PAGER_INCR(pPager->nRead);
2809 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002810 if( pgno==1 ){
danielk19778c0a7912008-08-20 14:49:23 +00002811 memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24],
drh86a88112007-04-16 15:02:19 +00002812 sizeof(pPager->dbFileVers));
2813 }
danielk197730849522008-08-21 04:35:18 +00002814 CODEC1(pPager, pPg->pData, pPg->pgno, 3);
drh30d53702009-01-06 15:58:57 +00002815 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
2816 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
danielk1977e180dd92007-04-05 17:15:52 +00002817 return rc;
2818}
2819
2820
2821/*
danielk1977e277be02007-03-23 18:12:06 +00002822** This function is called to obtain the shared lock required before
2823** data may be read from the pager cache. If the shared lock has already
2824** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002825**
2826** Immediately after obtaining the shared lock (if required), this function
2827** checks for a hot-journal file. If one is found, an emergency rollback
2828** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002829*/
2830static int pagerSharedLock(Pager *pPager){
2831 int rc = SQLITE_OK;
danielk1977d300b8a2008-08-01 10:50:23 +00002832 int isErrorReset = 0;
danielk1977e277be02007-03-23 18:12:06 +00002833
danielk1977ae72d982007-10-03 08:46:44 +00002834 /* If this database is opened for exclusive access, has no outstanding
2835 ** page references and is in an error-state, now is the chance to clear
2836 ** the error. Discard the contents of the pager-cache and treat any
2837 ** open journal file as a hot-journal.
2838 */
danielk19778c0a7912008-08-20 14:49:23 +00002839 if( !MEMDB && pPager->exclusiveMode
2840 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
2841 ){
danielk1977ae72d982007-10-03 08:46:44 +00002842 if( pPager->journalOpen ){
danielk1977d300b8a2008-08-01 10:50:23 +00002843 isErrorReset = 1;
danielk1977ae72d982007-10-03 08:46:44 +00002844 }
danielk1977ae72d982007-10-03 08:46:44 +00002845 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00002846 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00002847 }
2848
2849 /* If the pager is still in an error state, do not proceed. The error
2850 ** state will be cleared at some point in the future when all page
2851 ** references are dropped and the cache can be discarded.
2852 */
2853 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
2854 return pPager->errCode;
2855 }
2856
danielk1977d300b8a2008-08-01 10:50:23 +00002857 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
danielk1977b4b47412007-08-17 15:53:36 +00002858 sqlite3_vfs *pVfs = pPager->pVfs;
drh4f21c4a2008-12-10 22:15:00 +00002859 int isHotJournal = 0;
shane049fc212008-10-22 16:26:47 +00002860 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00002861 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
2862 if( !pPager->noReadlock ){
2863 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2864 if( rc!=SQLITE_OK ){
2865 assert( pPager->state==PAGER_UNLOCK );
2866 return pager_error(pPager, rc);
danielk1977e277be02007-03-23 18:12:06 +00002867 }
danielk19770371f1b2009-01-09 17:11:04 +00002868 }else if( pPager->state==PAGER_UNLOCK ){
2869 pPager->state = PAGER_SHARED;
danielk1977b3175382008-10-17 18:51:52 +00002870 }
danielk19770371f1b2009-01-09 17:11:04 +00002871 assert( pPager->state>=SHARED_LOCK );
danielk1977b3175382008-10-17 18:51:52 +00002872
2873 /* If a journal file exists, and there is no RESERVED lock on the
2874 ** database file, then it either needs to be played back or deleted.
2875 */
2876 if( !isErrorReset ){
2877 rc = hasHotJournal(pPager, &isHotJournal);
2878 if( rc!=SQLITE_OK ){
2879 goto failed;
2880 }
2881 }
2882 if( isErrorReset || isHotJournal ){
2883 /* Get an EXCLUSIVE lock on the database file. At this point it is
2884 ** important that a RESERVED lock is not obtained on the way to the
2885 ** EXCLUSIVE lock. If it were, another process might open the
2886 ** database file, detect the RESERVED lock, and conclude that the
2887 ** database is safe to read while this process is still rolling it
2888 ** back.
2889 **
2890 ** Because the intermediate RESERVED lock is not requested, the
2891 ** second process will get to this point in the code and fail to
2892 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00002893 */
danielk1977b3175382008-10-17 18:51:52 +00002894 if( pPager->state<EXCLUSIVE_LOCK ){
2895 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
danielk1977e277be02007-03-23 18:12:06 +00002896 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002897 rc = pager_error(pPager, rc);
2898 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002899 }
danielk1977b3175382008-10-17 18:51:52 +00002900 pPager->state = PAGER_EXCLUSIVE;
2901 }
2902
2903 /* Open the journal for read/write access. This is because in
2904 ** exclusive-access mode the file descriptor will be kept open and
2905 ** possibly used for a transaction later on. On some systems, the
2906 ** OsTruncate() call used in exclusive-access mode also requires
2907 ** a read/write file handle.
2908 */
2909 if( !isErrorReset && pPager->journalOpen==0 ){
2910 int res;
2911 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
2912 if( rc==SQLITE_OK ){
2913 if( res ){
2914 int fout = 0;
2915 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
2916 assert( !pPager->tempFile );
2917 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
2918 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk1977281d8bd2008-10-29 07:01:56 +00002919 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
2920 rc = SQLITE_CANTOPEN;
danielk1977b3175382008-10-17 18:51:52 +00002921 sqlite3OsClose(pPager->jfd);
2922 }
2923 }else{
2924 /* If the journal does not exist, that means some other process
2925 ** has already rolled it back */
2926 rc = SQLITE_BUSY;
2927 }
2928 }
2929 }
2930 if( rc!=SQLITE_OK ){
danielk1977b3175382008-10-17 18:51:52 +00002931 goto failed;
2932 }
2933 pPager->journalOpen = 1;
2934 pPager->journalStarted = 0;
2935 pPager->journalOff = 0;
2936 pPager->setMaster = 0;
2937 pPager->journalHdr = 0;
2938
2939 /* Playback and delete the journal. Drop the database write
danielk1977112f7522009-01-08 17:50:45 +00002940 ** lock and reacquire the read lock. Purge the cache before
2941 ** playing back the hot-journal so that we don't end up with
danielk1977ad0ea222009-01-08 18:04:13 +00002942 ** an inconsistent cache.
danielk1977b3175382008-10-17 18:51:52 +00002943 */
danielk1977112f7522009-01-08 17:50:45 +00002944 sqlite3PcacheClear(pPager->pPCache);
danielk1977b3175382008-10-17 18:51:52 +00002945 rc = pager_playback(pPager, 1);
2946 if( rc!=SQLITE_OK ){
2947 rc = pager_error(pPager, rc);
2948 goto failed;
2949 }
2950 assert(pPager->state==PAGER_SHARED ||
2951 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
2952 );
2953 }
2954
2955 if( sqlite3PcachePagecount(pPager->pPCache)>0 ){
2956 /* The shared-lock has just been acquired on the database file
2957 ** and there are already pages in the cache (from a previous
2958 ** read or write transaction). Check to see if the database
2959 ** has been modified. If the database has changed, flush the
2960 ** cache.
2961 **
2962 ** Database changes is detected by looking at 15 bytes beginning
2963 ** at offset 24 into the file. The first 4 of these 16 bytes are
2964 ** a 32-bit counter that is incremented with each change. The
2965 ** other bytes change randomly with each file change when
2966 ** a codec is in use.
2967 **
2968 ** There is a vanishingly small chance that a change will not be
2969 ** detected. The chance of an undetected change is so small that
2970 ** it can be neglected.
2971 */
2972 char dbFileVers[sizeof(pPager->dbFileVers)];
2973 sqlite3PagerPagecount(pPager, 0);
2974
2975 if( pPager->errCode ){
2976 rc = pPager->errCode;
2977 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002978 }
2979
danielk1977d92db532008-11-17 04:56:24 +00002980 assert( pPager->dbSizeValid );
danielk1977b3175382008-10-17 18:51:52 +00002981 if( pPager->dbSize>0 ){
2982 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
2983 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
2984 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002985 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002986 }
danielk1977b3175382008-10-17 18:51:52 +00002987 }else{
2988 memset(dbFileVers, 0, sizeof(dbFileVers));
2989 }
danielk1977e277be02007-03-23 18:12:06 +00002990
danielk1977b3175382008-10-17 18:51:52 +00002991 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
2992 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002993 }
2994 }
danielk19770371f1b2009-01-09 17:11:04 +00002995 assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED );
danielk1977e277be02007-03-23 18:12:06 +00002996 }
2997
danielk197752b472a2008-05-05 16:23:55 +00002998 failed:
2999 if( rc!=SQLITE_OK ){
3000 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
3001 pager_unlock(pPager);
3002 }
danielk1977e277be02007-03-23 18:12:06 +00003003 return rc;
3004}
3005
3006/*
drhd33d5a82007-04-26 12:11:28 +00003007** Make sure we have the content for a page. If the page was
3008** previously acquired with noContent==1, then the content was
3009** just initialized to zeros instead of being read from disk.
3010** But now we need the real data off of disk. So make sure we
3011** have it. Read it in if we do not have it already.
3012*/
3013static int pager_get_content(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003014 if( pPg->flags&PGHDR_NEED_READ ){
drhd33d5a82007-04-26 12:11:28 +00003015 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3016 if( rc==SQLITE_OK ){
danielk19778c0a7912008-08-20 14:49:23 +00003017 pPg->flags &= ~PGHDR_NEED_READ;
drhd33d5a82007-04-26 12:11:28 +00003018 }else{
3019 return rc;
3020 }
3021 }
3022 return SQLITE_OK;
3023}
3024
3025/*
danielk19778c0a7912008-08-20 14:49:23 +00003026** If the reference count has reached zero, and the pager is not in the
3027** middle of a write transaction or opened in exclusive mode, unlock it.
3028*/
3029static void pagerUnlockIfUnused(Pager *pPager){
3030 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
3031 && (!pPager->exclusiveMode || pPager->journalOff>0)
3032 ){
3033 pagerUnlockAndRollback(pPager);
3034 }
3035}
3036
3037/*
3038** Drop a page from the cache using sqlite3PcacheDrop().
3039**
3040** If this means there are now no pages with references to them, a rollback
3041** occurs and the lock on the database is removed.
3042*/
3043static void pagerDropPage(DbPage *pPg){
3044 Pager *pPager = pPg->pPager;
3045 sqlite3PcacheDrop(pPg);
3046 pagerUnlockIfUnused(pPager);
3047}
3048
3049/*
drhd9b02572001-04-15 00:37:09 +00003050** Acquire a page.
3051**
drh58a11682001-11-10 13:51:08 +00003052** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003053** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003054**
drhd33d5a82007-04-26 12:11:28 +00003055** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003056** file is smaller than the requested page, then no actual disk
3057** read occurs and the memory image of the page is initialized to
3058** all zeros. The extra data appended to a page is always initialized
3059** to zeros the first time a page is loaded into memory.
3060**
drhd9b02572001-04-15 00:37:09 +00003061** The acquisition might fail for several reasons. In all cases,
3062** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003063**
drhd33d5a82007-04-26 12:11:28 +00003064** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003065** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003066** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003067** just returns 0. This routine acquires a read-lock the first time it
3068** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003069** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003070** or journal files.
drh0787db62007-03-04 13:15:27 +00003071**
drh538f5702007-04-13 02:14:30 +00003072** If noContent is false, the page contents are actually read from disk.
3073** If noContent is true, it means that we do not care about the contents
3074** of the page at this time, so do not do a disk read. Just fill in the
3075** page content with zeros. But mark the fact that we have not read the
3076** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003077** sqlite3PagerWrite() is called on this page or if this routine is
3078** called again with noContent==0, that means that the content is needed
3079** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003080*/
danielk197765e0ff32008-09-19 09:14:43 +00003081int sqlite3PagerAcquire(
drh538f5702007-04-13 02:14:30 +00003082 Pager *pPager, /* The pager open on the database file */
3083 Pgno pgno, /* Page number to fetch */
3084 DbPage **ppPage, /* Write a pointer to the page here */
3085 int noContent /* Do not bother reading content from disk if true */
3086){
danielk19778c0a7912008-08-20 14:49:23 +00003087 PgHdr *pPg = 0;
drh165ffe92005-03-15 17:09:30 +00003088 int rc;
drhed7c8552001-04-11 14:29:21 +00003089
danielk19778c0a7912008-08-20 14:49:23 +00003090 assert( pPager->state==PAGER_UNLOCK
3091 || sqlite3PcacheRefCount(pPager->pPCache)>0
danielk1977b3175382008-10-17 18:51:52 +00003092 || pgno==1
danielk19778c0a7912008-08-20 14:49:23 +00003093 );
danielk1977e277be02007-03-23 18:12:06 +00003094
danielk197726836652005-01-17 01:33:13 +00003095 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3096 ** number greater than this, or zero, is requested.
3097 */
drh267cb322005-09-16 17:16:52 +00003098 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003099 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003100 }
3101
drhd9b02572001-04-15 00:37:09 +00003102 /* Make sure we have not hit any critical errors.
3103 */
drh836faa42003-01-11 13:30:57 +00003104 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003105 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003106
danielk197713adf8a2004-06-03 16:08:41 +00003107 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003108 ** on the database file. pagerSharedLock() is a no-op if
3109 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003110 */
danielk1977e277be02007-03-23 18:12:06 +00003111 rc = pagerSharedLock(pPager);
3112 if( rc!=SQLITE_OK ){
3113 return rc;
drhed7c8552001-04-11 14:29:21 +00003114 }
danielk1977e277be02007-03-23 18:12:06 +00003115 assert( pPager->state!=PAGER_UNLOCK );
3116
danielk19778c0a7912008-08-20 14:49:23 +00003117 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
3118 if( rc!=SQLITE_OK ){
3119 return rc;
3120 }
danielk19778c0a7912008-08-20 14:49:23 +00003121 if( pPg->pPager==0 ){
3122 /* The pager cache has created a new page. Its content needs to
3123 ** be initialized.
3124 */
danielk1977979f38e2007-03-27 16:19:51 +00003125 int nMax;
drh538f5702007-04-13 02:14:30 +00003126 PAGER_INCR(pPager->nMiss);
danielk19778c0a7912008-08-20 14:49:23 +00003127 pPg->pPager = pPager;
danielk19778c0a7912008-08-20 14:49:23 +00003128 memset(pPg->pExtra, 0, pPager->nExtra);
danielk197724168722007-04-02 05:07:47 +00003129
danielk1977ad0132d2008-06-07 08:58:22 +00003130 rc = sqlite3PagerPagecount(pPager, &nMax);
3131 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00003132 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003133 return rc;
3134 }
danielk197775bab7d2006-01-23 13:09:45 +00003135
danielk1977a1fa00d2008-08-27 15:16:33 +00003136 if( nMax<(int)pgno || MEMDB || noContent ){
drhf8e632b2007-05-08 14:51:36 +00003137 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003138 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003139 return SQLITE_FULL;
3140 }
danielk19778c0a7912008-08-20 14:49:23 +00003141 memset(pPg->pData, 0, pPager->pageSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00003142 if( noContent ){
danielk19778c0a7912008-08-20 14:49:23 +00003143 pPg->flags |= PGHDR_NEED_READ;
3144 }
drh538f5702007-04-13 02:14:30 +00003145 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003146 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003147 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003148 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
danielk19778c0a7912008-08-20 14:49:23 +00003149 /* sqlite3PagerUnref(pPg); */
3150 pagerDropPage(pPg);
drh551b7732006-11-06 21:20:25 +00003151 return rc;
drh81a20f22001-10-12 17:30:04 +00003152 }
drh306dc212001-05-21 13:45:10 +00003153 }
danielk19773c407372005-02-15 02:54:14 +00003154#ifdef SQLITE_CHECK_PAGES
3155 pPg->pageHash = pager_pagehash(pPg);
3156#endif
drhed7c8552001-04-11 14:29:21 +00003157 }else{
drhd9b02572001-04-15 00:37:09 +00003158 /* The requested page is in the page cache. */
danielk19778c0a7912008-08-20 14:49:23 +00003159 assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003160 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003161 if( !noContent ){
3162 rc = pager_get_content(pPg);
3163 if( rc ){
danielk19778c0a7912008-08-20 14:49:23 +00003164 sqlite3PagerUnref(pPg);
drhd33d5a82007-04-26 12:11:28 +00003165 return rc;
3166 }
3167 }
drhed7c8552001-04-11 14:29:21 +00003168 }
danielk19778c0a7912008-08-20 14:49:23 +00003169
danielk19773b8a05f2007-03-19 17:44:26 +00003170 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003171 return SQLITE_OK;
3172}
danielk19778c0a7912008-08-20 14:49:23 +00003173
drhed7c8552001-04-11 14:29:21 +00003174/*
drh7e3b0a02001-04-28 16:52:40 +00003175** Acquire a page if it is already in the in-memory cache. Do
3176** not read the page from disk. Return a pointer to the page,
3177** or 0 if the page is not in cache.
3178**
danielk19773b8a05f2007-03-19 17:44:26 +00003179** See also sqlite3PagerGet(). The difference between this routine
3180** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003181** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003182** returns NULL if the page is not in cache or if a disk I/O error
3183** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003184*/
danielk19773b8a05f2007-03-19 17:44:26 +00003185DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003186 PgHdr *pPg = 0;
drh836faa42003-01-11 13:30:57 +00003187 assert( pPager!=0 );
3188 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003189
danielk19778c0a7912008-08-20 14:49:23 +00003190 if( (pPager->state!=PAGER_UNLOCK)
3191 && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL)
3192 ){
3193 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
danielk1977e277be02007-03-23 18:12:06 +00003194 }
danielk19778c0a7912008-08-20 14:49:23 +00003195
danielk19773b8a05f2007-03-19 17:44:26 +00003196 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003197}
3198
3199/*
drhed7c8552001-04-11 14:29:21 +00003200** Release a page.
3201**
3202** If the number of references to the page drop to zero, then the
3203** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003204** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003205** removed.
3206*/
danielk19773b8a05f2007-03-19 17:44:26 +00003207int sqlite3PagerUnref(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003208 if( pPg ){
3209 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00003210 sqlite3PcacheRelease(pPg);
3211 pagerUnlockIfUnused(pPager);
drhed7c8552001-04-11 14:29:21 +00003212 }
drhd9b02572001-04-15 00:37:09 +00003213 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003214}
3215
danielk19779153d852009-01-07 10:52:56 +00003216/*
3217** If the main journal file has already been opened, ensure that the
3218** sub-journal file is open too. If the main journal is not open,
3219** this function is a no-op.
3220**
3221** SQLITE_OK is returned if everything goes according to plan. An
3222** SQLITE_IOERR_XXX error code is returned if the call to
3223** sqlite3OsOpen() fails.
3224*/
danielk1977fd7f0452008-12-17 17:30:26 +00003225static int openSubJournal(Pager *pPager){
3226 int rc = SQLITE_OK;
3227 if( pPager->journalOpen && !pPager->sjfd->pMethods ){
3228 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3229 sqlite3MemJournalOpen(pPager->sjfd);
3230 }else{
3231 rc = sqlite3PagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
3232 }
3233 }
3234 return rc;
3235}
3236
drhed7c8552001-04-11 14:29:21 +00003237/*
drha6abd042004-06-09 17:37:22 +00003238** Create a journal file for pPager. There should already be a RESERVED
3239** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003240**
3241** Return SQLITE_OK if everything. Return an error code and release the
3242** write lock if anything goes wrong.
3243*/
3244static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003245 sqlite3_vfs *pVfs = pPager->pVfs;
3246 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3247
drhda47d772002-12-02 04:25:19 +00003248 int rc;
drha6abd042004-06-09 17:37:22 +00003249 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003250 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003251 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003252 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003253 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00003254 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003255 rc = SQLITE_NOMEM;
3256 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003257 }
danielk1977b4b47412007-08-17 15:53:36 +00003258
drhfdc40e92008-04-17 20:59:37 +00003259 if( pPager->journalOpen==0 ){
3260 if( pPager->tempFile ){
3261 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3262 }else{
3263 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003264 }
danielk1977b3175382008-10-17 18:51:52 +00003265 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3266 sqlite3MemJournalOpen(pPager->jfd);
3267 rc = SQLITE_OK;
3268 }else{
drhfdc40e92008-04-17 20:59:37 +00003269#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977b3175382008-10-17 18:51:52 +00003270 rc = sqlite3JournalOpen(
3271 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3272 );
drhfdc40e92008-04-17 20:59:37 +00003273#else
danielk1977b3175382008-10-17 18:51:52 +00003274 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
drhfdc40e92008-04-17 20:59:37 +00003275#endif
danielk1977b3175382008-10-17 18:51:52 +00003276 }
drhfdc40e92008-04-17 20:59:37 +00003277 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3278 pPager->journalOff = 0;
3279 pPager->setMaster = 0;
3280 pPager->journalHdr = 0;
3281 if( rc!=SQLITE_OK ){
3282 if( rc==SQLITE_NOMEM ){
3283 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3284 }
3285 goto failed_to_open_journal;
3286 }
drhda47d772002-12-02 04:25:19 +00003287 }
3288 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003289 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003290 pPager->needSync = 0;
drh968af522003-02-11 14:55:40 +00003291 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003292 if( pPager->errCode ){
3293 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003294 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003295 }
danielk19773460d192008-12-27 15:23:13 +00003296 pPager->dbOrigSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003297
danielk197776572402004-06-25 02:38:54 +00003298 rc = writeJournalHdr(pPager);
3299
danielk1977fd7f0452008-12-17 17:30:26 +00003300 if( pPager->nSavepoint && rc==SQLITE_OK ){
3301 rc = openSubJournal(pPager);
drhda47d772002-12-02 04:25:19 +00003302 }
danielk1977ae72d982007-10-03 08:46:44 +00003303 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003304 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003305 if( rc==SQLITE_OK ){
3306 rc = SQLITE_FULL;
3307 }
3308 }
drh9c105bb2004-10-02 20:38:28 +00003309 return rc;
3310
3311failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003312 sqlite3BitvecDestroy(pPager->pInJournal);
3313 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003314 return rc;
drhda47d772002-12-02 04:25:19 +00003315}
3316
3317/*
drh4b845d72002-03-05 12:41:19 +00003318** Acquire a write-lock on the database. The lock is removed when
3319** the any of the following happen:
3320**
drh80e35f42007-03-30 14:06:34 +00003321** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003322** * sqlite3PagerRollback() is called.
3323** * sqlite3PagerClose() is called.
3324** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003325**
danielk197713adf8a2004-06-03 16:08:41 +00003326** The first parameter to this routine is a pointer to any open page of the
3327** database file. Nothing changes about the page - it is used merely to
3328** acquire a pointer to the Pager structure and as proof that there is
3329** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003330**
danielk197713adf8a2004-06-03 16:08:41 +00003331** The second parameter indicates how much space in bytes to reserve for a
3332** master journal file-name at the start of the journal when it is created.
3333**
3334** A journal file is opened if this is not a temporary file. For temporary
3335** files, the opening of the journal file is deferred until there is an
3336** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003337**
drha6abd042004-06-09 17:37:22 +00003338** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003339**
3340** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3341** immediately instead of waiting until we try to flush the cache. The
3342** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003343*/
danielk19773b8a05f2007-03-19 17:44:26 +00003344int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003345 Pager *pPager = pPg->pPager;
3346 int rc = SQLITE_OK;
3347 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003348 assert( pPager->state!=PAGER_UNLOCK );
3349 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003350 assert( pPager->pInJournal==0 );
danielk1977b3175382008-10-17 18:51:52 +00003351 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00003352 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
3353 if( rc==SQLITE_OK ){
3354 pPager->state = PAGER_RESERVED;
3355 if( exFlag ){
3356 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh684917c2004-10-05 02:41:42 +00003357 }
danielk1977b3175382008-10-17 18:51:52 +00003358 }
3359 if( rc!=SQLITE_OK ){
3360 return rc;
3361 }
3362 pPager->dirtyCache = 0;
drh30d53702009-01-06 15:58:57 +00003363 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00003364 if( pPager->useJournal && !pPager->tempFile
3365 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
3366 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00003367 }
danielk1977334cdb62007-03-26 08:05:12 +00003368 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00003369 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00003370 ** time a (read or write) transaction was successfully concluded
3371 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00003372 ** kept open and either was truncated to 0 bytes or its header was
3373 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00003374 */
3375 assert( pPager->nRec==0 );
danielk19773460d192008-12-27 15:23:13 +00003376 assert( pPager->dbOrigSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003377 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003378 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003379 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhf5e7bb52008-02-18 14:47:33 +00003380 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003381 rc = SQLITE_NOMEM;
3382 }else{
danielk19773460d192008-12-27 15:23:13 +00003383 pPager->dbOrigSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003384 rc = writeJournalHdr(pPager);
3385 }
drh4b845d72002-03-05 12:41:19 +00003386 }
danielk1977334cdb62007-03-26 08:05:12 +00003387 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003388 return rc;
3389}
3390
3391/*
drhed7c8552001-04-11 14:29:21 +00003392** Mark a data page as writeable. The page is written into the journal
3393** if it is not there already. This routine must be called before making
3394** changes to a page.
3395**
3396** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003397** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003398** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003399** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003400** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003401**
3402** If the journal file could not be written because the disk is full,
3403** then this routine returns SQLITE_FULL and does an immediate rollback.
3404** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003405** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003406** reset.
drhed7c8552001-04-11 14:29:21 +00003407*/
danielk19773b8a05f2007-03-19 17:44:26 +00003408static int pager_write(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003409 void *pData = pPg->pData;
drh69688d52001-04-14 16:38:23 +00003410 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003411 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003412
drh6446c4d2001-12-15 14:22:18 +00003413 /* Check for errors
3414 */
danielk1977efaaf572006-01-16 11:29:19 +00003415 if( pPager->errCode ){
3416 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003417 }
drh5e00f6c2001-09-13 13:46:56 +00003418 if( pPager->readOnly ){
3419 return SQLITE_PERM;
3420 }
drh6446c4d2001-12-15 14:22:18 +00003421
danielk197776572402004-06-25 02:38:54 +00003422 assert( !pPager->setMaster );
3423
danielk19773c407372005-02-15 02:54:14 +00003424 CHECK_PAGE(pPg);
3425
drh538f5702007-04-13 02:14:30 +00003426 /* If this page was previously acquired with noContent==1, that means
3427 ** we didn't really read in the content of the page. This can happen
3428 ** (for example) when the page is being moved to the freelist. But
3429 ** now we are (perhaps) moving the page off of the freelist for
3430 ** reuse and we need to know its original content so that content
3431 ** can be stored in the rollback journal. So do the read at this
3432 ** time.
3433 */
drhd33d5a82007-04-26 12:11:28 +00003434 rc = pager_get_content(pPg);
3435 if( rc ){
3436 return rc;
drh538f5702007-04-13 02:14:30 +00003437 }
3438
drh6446c4d2001-12-15 14:22:18 +00003439 /* Mark the page as dirty. If the page has already been written
3440 ** to the journal then we can return right away.
3441 */
drhc047b9f2008-11-21 03:23:43 +00003442 sqlite3PcacheMakeDirty(pPg);
danielk19773460d192008-12-27 15:23:13 +00003443 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
drha6abd042004-06-09 17:37:22 +00003444 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003445 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003446 }else{
drh6446c4d2001-12-15 14:22:18 +00003447
danielk1977a0bf2652004-11-04 14:30:04 +00003448 /* If we get this far, it means that the page needs to be
3449 ** written to the transaction journal or the ckeckpoint journal
3450 ** or both.
3451 **
3452 ** First check to see that the transaction journal exists and
3453 ** create it if it does not.
3454 */
3455 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003456 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003457 if( rc!=SQLITE_OK ){
3458 return rc;
3459 }
3460 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00003461 if( !pPager->journalOpen && pPager->useJournal
3462 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00003463 rc = pager_open_journal(pPager);
3464 if( rc!=SQLITE_OK ) return rc;
3465 }
danielk1977a0bf2652004-11-04 14:30:04 +00003466 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003467 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003468
3469 /* The transaction journal now exists and we have a RESERVED or an
3470 ** EXCLUSIVE lock on the main database file. Write the current page to
3471 ** the transaction journal if it is not there already.
3472 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00003473 if( !pageInJournal(pPg) && pPager->journalOpen ){
danielk19773460d192008-12-27 15:23:13 +00003474 if( pPg->pgno<=pPager->dbOrigSize ){
danielk1977b3175382008-10-17 18:51:52 +00003475 u32 cksum;
3476 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00003477
danielk1977b3175382008-10-17 18:51:52 +00003478 /* We should never write to the journal file the page that
3479 ** contains the database locks. The following assert verifies
3480 ** that we do not. */
3481 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
3482 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
3483 cksum = pager_cksum(pPager, (u8*)pData2);
3484 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
3485 if( rc==SQLITE_OK ){
3486 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
3487 pPager->journalOff + 4);
3488 pPager->journalOff += pPager->pageSize+4;
3489 }
3490 if( rc==SQLITE_OK ){
3491 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
3492 pPager->journalOff += 4;
3493 }
3494 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
3495 pPager->journalOff, pPager->pageSize));
3496 PAGER_INCR(sqlite3_pager_writej_count);
drh30d53702009-01-06 15:58:57 +00003497 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
danielk1977b3175382008-10-17 18:51:52 +00003498 PAGERID(pPager), pPg->pgno,
drh30d53702009-01-06 15:58:57 +00003499 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
danielk197707cb5602006-01-20 10:55:05 +00003500
danielk1977f3107512008-12-22 10:58:46 +00003501 /* Even if an IO or diskfull error occurred while journalling the
3502 ** page in the block above, set the need-sync flag for the page.
3503 ** Otherwise, when the transaction is rolled back, the logic in
3504 ** playback_one_page() will think that the page needs to be restored
3505 ** in the database file. And if an IO error occurs while doing so,
3506 ** then corruption may follow.
3507 */
3508 if( !pPager->noSync ){
3509 pPg->flags |= PGHDR_NEED_SYNC;
danielk1977a4124bd2008-12-23 10:37:47 +00003510 pPager->needSync = 1;
danielk1977f3107512008-12-22 10:58:46 +00003511 }
3512
danielk1977b3175382008-10-17 18:51:52 +00003513 /* An error has occured writing to the journal file. The
3514 ** transaction will be rolled back by the layer above.
3515 */
3516 if( rc!=SQLITE_OK ){
3517 return rc;
3518 }
danielk197707cb5602006-01-20 10:55:05 +00003519
danielk1977b3175382008-10-17 18:51:52 +00003520 pPager->nRec++;
3521 assert( pPager->pInJournal!=0 );
drh7539b6b2009-01-02 21:39:39 +00003522 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3523 testcase( rc==SQLITE_NOMEM );
3524 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
3525 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
3526 if( rc!=SQLITE_OK ){
3527 assert( rc==SQLITE_NOMEM );
3528 return rc;
3529 }
danielk197713adf8a2004-06-03 16:08:41 +00003530 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003531 if( !pPager->journalStarted && !pPager->noSync ){
3532 pPg->flags |= PGHDR_NEED_SYNC;
danielk1977a4124bd2008-12-23 10:37:47 +00003533 pPager->needSync = 1;
danielk19778c0a7912008-08-20 14:49:23 +00003534 }
drh30d53702009-01-06 15:58:57 +00003535 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
danielk19778c0a7912008-08-20 14:49:23 +00003536 PAGERID(pPager), pPg->pgno,
drh30d53702009-01-06 15:58:57 +00003537 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
danielk1977a0bf2652004-11-04 14:30:04 +00003538 }
danielk1977a0bf2652004-11-04 14:30:04 +00003539 }
3540
3541 /* If the statement journal is open and the page is not in it,
3542 ** then write the current page to the statement journal. Note that
3543 ** the statement journal format differs from the standard journal format
3544 ** in that it omits the checksums and the header.
3545 */
danielk19773460d192008-12-27 15:23:13 +00003546 if( subjRequiresPage(pPg) ){
danielk1977f2c31ad2009-01-06 13:40:08 +00003547 rc = subjournalPage(pPg);
drhd9b02572001-04-15 00:37:09 +00003548 }
drhfa86c412002-02-02 15:01:15 +00003549 }
3550
3551 /* Update the database size and return.
3552 */
drh1aa2d8b2007-01-03 15:34:29 +00003553 assert( pPager->state>=PAGER_SHARED );
danielk1977d92db532008-11-17 04:56:24 +00003554 if( pPager->dbSize<pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003555 pPager->dbSize = pPg->pgno;
danielk1977d92db532008-11-17 04:56:24 +00003556 if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){
drh1f595712004-06-15 01:40:29 +00003557 pPager->dbSize++;
3558 }
drh306dc212001-05-21 13:45:10 +00003559 }
drh69688d52001-04-14 16:38:23 +00003560 return rc;
drhed7c8552001-04-11 14:29:21 +00003561}
3562
3563/*
danielk19774099f6e2007-03-19 11:25:20 +00003564** This function is used to mark a data-page as writable. It uses
3565** pager_write() to open a journal file (if it is not already open)
3566** and write the page *pData to the journal.
3567**
3568** The difference between this function and pager_write() is that this
3569** function also deals with the special case where 2 or more pages
3570** fit on a single disk sector. In this case all co-resident pages
3571** must have been written to the journal file before returning.
3572*/
danielk19773b8a05f2007-03-19 17:44:26 +00003573int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003574 int rc = SQLITE_OK;
3575
danielk19773b8a05f2007-03-19 17:44:26 +00003576 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003577 Pager *pPager = pPg->pPager;
3578 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3579
danielk1977b3175382008-10-17 18:51:52 +00003580 if( nPagePerSector>1 ){
danielk19774099f6e2007-03-19 11:25:20 +00003581 Pgno nPageCount; /* Total number of pages in database file */
3582 Pgno pg1; /* First page of the sector pPg is located on. */
3583 int nPage; /* Number of pages starting at pg1 to journal */
3584 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003585 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003586
3587 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3588 ** header to be written between the pages journaled by this function.
3589 */
danielk1977b3175382008-10-17 18:51:52 +00003590 assert( !MEMDB );
danielk19774099f6e2007-03-19 11:25:20 +00003591 assert( pPager->doNotSync==0 );
3592 pPager->doNotSync = 1;
3593
3594 /* This trick assumes that both the page-size and sector-size are
3595 ** an integer power of 2. It sets variable pg1 to the identifier
3596 ** of the first page of the sector pPg is located on.
3597 */
3598 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3599
danielk1977ad0132d2008-06-07 08:58:22 +00003600 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00003601 if( pPg->pgno>nPageCount ){
3602 nPage = (pPg->pgno - pg1)+1;
3603 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3604 nPage = nPageCount+1-pg1;
3605 }else{
3606 nPage = nPagePerSector;
3607 }
3608 assert(nPage>0);
3609 assert(pg1<=pPg->pgno);
3610 assert((pg1+nPage)>pPg->pgno);
3611
3612 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3613 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003614 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00003615 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00003616 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003617 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003618 if( rc==SQLITE_OK ){
3619 rc = pager_write(pPage);
danielk19778c0a7912008-08-20 14:49:23 +00003620 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003621 needSync = 1;
danielk1977a4124bd2008-12-23 10:37:47 +00003622 assert(pPager->needSync);
danielk1977dd97a492007-08-22 18:54:32 +00003623 }
danielk19773b8a05f2007-03-19 17:44:26 +00003624 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003625 }
3626 }
drhc81945e2008-03-10 14:12:53 +00003627 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk19778c0a7912008-08-20 14:49:23 +00003628 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003629 needSync = 1;
3630 }
danielk19778c0a7912008-08-20 14:49:23 +00003631 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003632 }
3633 }
3634
drhee03d622009-01-07 15:33:45 +00003635 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
danielk1977dd97a492007-08-22 18:54:32 +00003636 ** starting at pg1, then it needs to be set for all of them. Because
3637 ** writing to any of these nPage pages may damage the others, the
3638 ** journal file must contain sync()ed copies of all of them
3639 ** before any of them can be written out to the database file.
drhee03d622009-01-07 15:33:45 +00003640 **
3641 ** 2009-01-07: This block of code appears to be a no-op. I do not
3642 ** believe it is possible for any page on the sector to not have
3643 ** the PGHDR_NEED_SYNC flag set. The "pPage->flags |= PGHDR_NEED_SYNC"
3644 ** line below does nothing, I think. But it does no harm to leave
3645 ** this code in place until we can definitively prove this is the case.
danielk1977dd97a492007-08-22 18:54:32 +00003646 */
3647 if( needSync ){
drhb3df2e12008-09-17 20:06:26 +00003648 assert( !MEMDB && pPager->noSync==0 );
danielk1977dd97a492007-08-22 18:54:32 +00003649 for(ii=0; ii<nPage && needSync; ii++){
3650 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
drhee03d622009-01-07 15:33:45 +00003651 if( pPage ){
3652 assert( pPage->flags & PGHDR_NEED_SYNC ); /* 2009-01-07 conjecture */
3653 pPage->flags |= PGHDR_NEED_SYNC;
3654 sqlite3PagerUnref(pPage);
3655 }
danielk1977dd97a492007-08-22 18:54:32 +00003656 }
3657 assert(pPager->needSync);
3658 }
3659
danielk19774099f6e2007-03-19 11:25:20 +00003660 assert( pPager->doNotSync==1 );
3661 pPager->doNotSync = 0;
3662 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003663 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003664 }
3665 return rc;
3666}
3667
3668/*
drhaacc5432002-01-06 17:07:40 +00003669** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003670** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003671** to change the content of the page.
3672*/
danielk19777d3a6662006-01-23 16:21:05 +00003673#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003674int sqlite3PagerIswriteable(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003675 return pPg->flags&PGHDR_DIRTY;
drh6019e162001-07-02 17:51:45 +00003676}
danielk19777d3a6662006-01-23 16:21:05 +00003677#endif
drh6019e162001-07-02 17:51:45 +00003678
drh001bbcb2003-03-19 03:14:00 +00003679/*
drh30e58752002-03-02 20:41:57 +00003680** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003681** write the information on page pPg back to the disk, even though
drhdfe88ec2008-11-03 20:55:06 +00003682** that page might be marked as dirty. This happens, for example, when
3683** the page has been added as a leaf of the freelist and so its
3684** content no longer matters.
drh30e58752002-03-02 20:41:57 +00003685**
3686** The overlying software layer calls this routine when all of the data
3687** on the given page is unused. The pager marks the page as clean so
3688** that it does not get written to disk.
3689**
3690** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003691** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003692** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003693**
3694** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003695** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003696** will thereafter be ignored. This is necessary to avoid a problem
3697** where a page with data is added to the freelist during one part of
3698** a transaction then removed from the freelist during a later part
3699** of the same transaction and reused for some other purpose. When it
3700** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003701** the sqlite3PagerDontRollback() routine is called. But because the
3702** page contains critical data, we still need to be sure it gets
3703** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003704*/
danielk1977a1fa00d2008-08-27 15:16:33 +00003705int sqlite3PagerDontWrite(DbPage *pDbPage){
drh538f5702007-04-13 02:14:30 +00003706 PgHdr *pPg = pDbPage;
3707 Pager *pPager = pPg->pPager;
danielk1977a1fa00d2008-08-27 15:16:33 +00003708 int rc;
drh8e298f92002-07-06 16:28:47 +00003709
danielk19773460d192008-12-27 15:23:13 +00003710 if( pPg->pgno>pPager->dbOrigSize ){
danielk1977a1fa00d2008-08-27 15:16:33 +00003711 return SQLITE_OK;
3712 }
3713 if( pPager->pAlwaysRollback==0 ){
3714 assert( pPager->pInJournal );
danielk19773460d192008-12-27 15:23:13 +00003715 pPager->pAlwaysRollback = sqlite3BitvecCreate(pPager->dbOrigSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00003716 if( !pPager->pAlwaysRollback ){
3717 return SQLITE_NOMEM;
3718 }
3719 }
3720 rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno);
3721
danielk1977fd7f0452008-12-17 17:30:26 +00003722 if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
drh1aa2d8b2007-01-03 15:34:29 +00003723 assert( pPager->state>=PAGER_SHARED );
danielk19773460d192008-12-27 15:23:13 +00003724 if( pPager->dbSize==pPg->pgno && pPager->dbOrigSize<pPager->dbSize ){
drh8124a302002-06-25 14:43:57 +00003725 /* If this pages is the last page in the file and the file has grown
3726 ** during the current transaction, then do NOT mark the page as clean.
3727 ** When the database file grows, we must make sure that the last page
3728 ** gets written at least once so that the disk file will be the correct
3729 ** size. If you do not write this page and the size of the file
3730 ** on the disk ends up being too small, that can lead to database
3731 ** corruption during the next transaction.
3732 */
3733 }else{
drh30d53702009-01-06 15:58:57 +00003734 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
drh538f5702007-04-13 02:14:30 +00003735 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
danielk197733e32162008-08-23 18:53:08 +00003736 pPg->flags |= PGHDR_DONT_WRITE;
danielk19773c407372005-02-15 02:54:14 +00003737#ifdef SQLITE_CHECK_PAGES
3738 pPg->pageHash = pager_pagehash(pPg);
3739#endif
drh8124a302002-06-25 14:43:57 +00003740 }
drh30e58752002-03-02 20:41:57 +00003741 }
danielk1977a1fa00d2008-08-27 15:16:33 +00003742 return rc;
drh30e58752002-03-02 20:41:57 +00003743}
3744
3745/*
3746** A call to this routine tells the pager that if a rollback occurs,
3747** it is not necessary to restore the data on the given page. This
3748** means that the pager does not have to record the given page in the
3749** rollback journal.
drh538f5702007-04-13 02:14:30 +00003750**
3751** If we have not yet actually read the content of this page (if
3752** the PgHdr.needRead flag is set) then this routine acts as a promise
3753** that we will never need to read the page content in the future.
3754** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00003755*/
danielk19773b8a05f2007-03-19 17:44:26 +00003756void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003757 Pager *pPager = pPg->pPager;
drh7539b6b2009-01-02 21:39:39 +00003758 TESTONLY( int rc; ) /* Return value from sqlite3BitvecSet() */
drh30e58752002-03-02 20:41:57 +00003759
drhd3627af2006-12-18 18:34:51 +00003760 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00003761
3762 /* If the journal file is not open, or DontWrite() has been called on
3763 ** this page (DontWrite() sets the alwaysRollback flag), then this
3764 ** function is a no-op.
3765 */
danielk1977a1fa00d2008-08-27 15:16:33 +00003766 if( pPager->journalOpen==0
3767 || sqlite3BitvecTest(pPager->pAlwaysRollback, pPg->pgno)
danielk19773460d192008-12-27 15:23:13 +00003768 || pPg->pgno>pPager->dbOrigSize
danielk19778c0a7912008-08-20 14:49:23 +00003769 ){
danielk197787c29a92008-01-18 11:33:16 +00003770 return;
3771 }
danielk1977a55e9352008-01-21 13:04:34 +00003772
drhc5d0bd92008-04-14 01:00:57 +00003773#ifdef SQLITE_SECURE_DELETE
drh1feb7dd2008-11-19 18:30:29 +00003774 if( sqlite3BitvecTest(pPager->pInJournal, pPg->pgno)!=0
danielk19773460d192008-12-27 15:23:13 +00003775 || pPg->pgno>pPager->dbOrigSize ){
drhc5d0bd92008-04-14 01:00:57 +00003776 return;
3777 }
3778#endif
3779
3780 /* If SECURE_DELETE is disabled, then there is no way that this
3781 ** routine can be called on a page for which sqlite3PagerDontWrite()
3782 ** has not been previously called during the same transaction.
3783 ** And if DontWrite() has previously been called, the following
3784 ** conditions must be met.
drh10131482008-07-11 03:34:09 +00003785 **
3786 ** (Later:) Not true. If the database is corrupted by having duplicate
3787 ** pages on the freelist (ex: corrupt9.test) then the following is not
3788 ** necessarily true:
danielk1977a55e9352008-01-21 13:04:34 +00003789 */
danielk19773460d192008-12-27 15:23:13 +00003790 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->dbOrigSize ); */
danielk1977a55e9352008-01-21 13:04:34 +00003791
drhf5e7bb52008-02-18 14:47:33 +00003792 assert( pPager->pInJournal!=0 );
danielk19778c0a7912008-08-20 14:49:23 +00003793 pPg->flags &= ~PGHDR_NEED_READ;
drh7539b6b2009-01-02 21:39:39 +00003794
3795 /* Failure to set the bits in the InJournal bit-vectors is benign.
3796 ** It merely means that we might do some extra work to journal a page
drha8a71ba2009-01-03 12:55:17 +00003797 ** that does not need to be journaled. Nevertheless, be sure to test the
drh7539b6b2009-01-02 21:39:39 +00003798 ** case where a malloc error occurs while trying to set a bit in a
3799 ** bit vector.
3800 */
3801 sqlite3BeginBenignMalloc();
3802 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3803 testcase( rc==SQLITE_NOMEM );
3804 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pPg->pgno);
3805 testcase( rc==SQLITE_NOMEM );
3806 sqlite3EndBenignMalloc();
3807
3808
drh30d53702009-01-06 15:58:57 +00003809 PAGERTRACE(("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)));
danielk1977a55e9352008-01-21 13:04:34 +00003810 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00003811}
3812
drhac69b052004-05-12 13:30:07 +00003813
drh30e58752002-03-02 20:41:57 +00003814/*
drh80e35f42007-03-30 14:06:34 +00003815** This routine is called to increment the database file change-counter,
3816** stored at byte 24 of the pager file.
3817*/
danielk1977c7b60172007-08-22 11:22:03 +00003818static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00003819 PgHdr *pPgHdr;
3820 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00003821 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00003822
drh701bb3b2008-08-02 03:50:39 +00003823#ifndef SQLITE_ENABLE_ATOMIC_WRITE
3824 assert( isDirect==0 ); /* isDirect is only true for atomic writes */
3825#endif
danielk197712dd5492008-12-18 15:45:07 +00003826 if( !pPager->changeCountDone && pPager->dbSize>0 ){
drh80e35f42007-03-30 14:06:34 +00003827 /* Open page 1 of the file for writing. */
3828 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3829 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00003830
3831 if( !isDirect ){
3832 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00003833 if( rc!=SQLITE_OK ){
3834 sqlite3PagerUnref(pPgHdr);
3835 return rc;
3836 }
danielk1977c7b60172007-08-22 11:22:03 +00003837 }
3838
drh80e35f42007-03-30 14:06:34 +00003839 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00003840 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00003841 change_counter++;
danielk19778c0a7912008-08-20 14:49:23 +00003842 put32bits(((char*)pPgHdr->pData)+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00003843
drh701bb3b2008-08-02 03:50:39 +00003844#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977c7b60172007-08-22 11:22:03 +00003845 if( isDirect && pPager->fd->pMethods ){
danielk19778c0a7912008-08-20 14:49:23 +00003846 const void *zBuf = pPgHdr->pData;
danielk19773460d192008-12-27 15:23:13 +00003847 assert( pPager->dbFileSize>0 );
danielk1977c7b60172007-08-22 11:22:03 +00003848 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
3849 }
drh701bb3b2008-08-02 03:50:39 +00003850#endif
danielk1977c7b60172007-08-22 11:22:03 +00003851
drh80e35f42007-03-30 14:06:34 +00003852 /* Release the page reference. */
3853 sqlite3PagerUnref(pPgHdr);
3854 pPager->changeCountDone = 1;
3855 }
danielk1977c7b60172007-08-22 11:22:03 +00003856 return rc;
drh80e35f42007-03-30 14:06:34 +00003857}
3858
3859/*
danielk1977f653d782008-03-20 11:04:21 +00003860** Sync the pager file to disk.
3861*/
3862int sqlite3PagerSync(Pager *pPager){
3863 int rc;
drh7426f862008-08-26 21:07:26 +00003864 if( MEMDB ){
3865 rc = SQLITE_OK;
3866 }else{
3867 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3868 }
danielk1977f653d782008-03-20 11:04:21 +00003869 return rc;
3870}
3871
3872/*
drh80e35f42007-03-30 14:06:34 +00003873** Sync the database file for the pager pPager. zMaster points to the name
3874** of a master journal file that should be written into the individual
3875** journal file. zMaster may be NULL, which is interpreted as no master
3876** journal (a single database transaction).
3877**
3878** This routine ensures that the journal is synced, all dirty pages written
3879** to the database file and the database file synced. The only thing that
3880** remains to commit the transaction is to delete the journal file (or
3881** master journal file if specified).
3882**
3883** Note that if zMaster==NULL, this does not overwrite a previous value
3884** passed to an sqlite3PagerCommitPhaseOne() call.
3885**
danielk1977f653d782008-03-20 11:04:21 +00003886** If the final parameter - noSync - is true, then the database file itself
3887** is not synced. The caller must call sqlite3PagerSync() directly to
3888** sync the database file before calling CommitPhaseTwo() to delete the
3889** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00003890*/
danielk1977f653d782008-03-20 11:04:21 +00003891int sqlite3PagerCommitPhaseOne(
3892 Pager *pPager,
3893 const char *zMaster,
danielk1977f653d782008-03-20 11:04:21 +00003894 int noSync
3895){
drh80e35f42007-03-30 14:06:34 +00003896 int rc = SQLITE_OK;
3897
danielk1977dad31b52008-05-15 11:08:07 +00003898 if( pPager->errCode ){
3899 return pPager->errCode;
3900 }
3901
drhd138c012008-05-13 00:58:18 +00003902 /* If no changes have been made, we can leave the transaction early.
3903 */
3904 if( pPager->dbModified==0 &&
3905 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3906 pPager->exclusiveMode!=0) ){
3907 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3908 return SQLITE_OK;
3909 }
3910
drh30d53702009-01-06 15:58:57 +00003911 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
3912 pPager->zFilename, zMaster, pPager->dbSize));
drh80e35f42007-03-30 14:06:34 +00003913
3914 /* If this is an in-memory db, or no pages have been written to, or this
3915 ** function has already been called, it is a no-op.
3916 */
3917 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
3918 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00003919
danielk1977c7b60172007-08-22 11:22:03 +00003920#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3921 /* The atomic-write optimization can be used if all of the
3922 ** following are true:
3923 **
3924 ** + The file-system supports the atomic-write property for
3925 ** blocks of size page-size, and
3926 ** + This commit is not part of a multi-file transaction, and
3927 ** + Exactly one page has been modified and store in the journal file.
3928 **
3929 ** If the optimization can be used, then the journal file will never
3930 ** be created for this transaction.
3931 */
danielk19774d60af92008-09-08 15:35:06 +00003932 int useAtomicWrite;
danielk19778c0a7912008-08-20 14:49:23 +00003933 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
danielk19774d60af92008-09-08 15:35:06 +00003934 useAtomicWrite = (
danielk1977f55b8992007-08-24 08:15:53 +00003935 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00003936 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00003937 pPager->journalOff==jrnlBufferSize(pPager) &&
danielk19773460d192008-12-27 15:23:13 +00003938 pPager->dbSize>=pPager->dbFileSize &&
danielk19778c0a7912008-08-20 14:49:23 +00003939 (pPg==0 || pPg->pDirty==0)
danielk1977f55b8992007-08-24 08:15:53 +00003940 );
danielk1977700b9c52008-04-24 12:37:40 +00003941 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00003942 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00003943 /* Update the nRec field in the journal file. */
3944 int offset = pPager->journalHdr + sizeof(aJournalMagic);
3945 assert(pPager->nRec==1);
3946 rc = write32bits(pPager->jfd, offset, pPager->nRec);
3947
3948 /* Update the db file change counter. The following call will modify
3949 ** the in-memory representation of page 1 to include the updated
3950 ** change counter and then write page 1 directly to the database
3951 ** file. Because of the atomic-write property of the host file-system,
3952 ** this is safe.
3953 */
danielk1977ae72d982007-10-03 08:46:44 +00003954 if( rc==SQLITE_OK ){
3955 rc = pager_incr_changecounter(pPager, 1);
3956 }
danielk1977f55b8992007-08-24 08:15:53 +00003957 }else{
3958 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00003959 }
3960
danielk1977ae72d982007-10-03 08:46:44 +00003961 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00003962#endif
3963
drh80e35f42007-03-30 14:06:34 +00003964 /* If a master journal file name has already been written to the
3965 ** journal file, then no sync is required. This happens when it is
3966 ** written, then the process fails to upgrade from a RESERVED to an
3967 ** EXCLUSIVE lock. The next time the process tries to commit the
3968 ** transaction the m-j name will have already been written.
3969 */
3970 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00003971 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00003972 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00003973 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00003974#ifndef SQLITE_OMIT_AUTOVACUUM
danielk19773460d192008-12-27 15:23:13 +00003975 if( pPager->dbSize<pPager->dbOrigSize ){
danielk197771aa7ff2008-05-20 07:05:09 +00003976 /* If this transaction has made the database smaller, then all pages
3977 ** being discarded by the truncation must be written to the journal
3978 ** file.
3979 */
3980 Pgno i;
danielk1977d92db532008-11-17 04:56:24 +00003981 Pgno iSkip = PAGER_MJ_PGNO(pPager);
danielk19773460d192008-12-27 15:23:13 +00003982 Pgno dbSize = pPager->dbSize;
danielk1977f9bce3c2009-01-06 15:20:58 +00003983 pPager->dbSize = pPager->dbOrigSize;
danielk1977f70c1fe2009-01-07 18:08:48 +00003984 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
danielk197771aa7ff2008-05-20 07:05:09 +00003985 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
3986 rc = sqlite3PagerGet(pPager, i, &pPg);
3987 if( rc!=SQLITE_OK ) goto sync_exit;
3988 rc = sqlite3PagerWrite(pPg);
3989 sqlite3PagerUnref(pPg);
3990 if( rc!=SQLITE_OK ) goto sync_exit;
3991 }
3992 }
danielk19773460d192008-12-27 15:23:13 +00003993 pPager->dbSize = dbSize;
danielk197771aa7ff2008-05-20 07:05:09 +00003994 }
drh80e35f42007-03-30 14:06:34 +00003995#endif
danielk197771aa7ff2008-05-20 07:05:09 +00003996 rc = writeMasterJournal(pPager, zMaster);
3997 if( rc!=SQLITE_OK ) goto sync_exit;
3998 rc = syncJournal(pPager);
3999 }
drh80e35f42007-03-30 14:06:34 +00004000 }
danielk1977c7b60172007-08-22 11:22:03 +00004001 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004002
drh80e35f42007-03-30 14:06:34 +00004003 /* Write all dirty pages to the database file */
danielk19778c0a7912008-08-20 14:49:23 +00004004 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00004005 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004006 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004007 assert( rc!=SQLITE_IOERR_BLOCKED );
4008 /* The error might have left the dirty list all fouled up here,
4009 ** but that does not matter because if the if the dirty list did
4010 ** get corrupted, then the transaction will roll back and
4011 ** discard the dirty list. There is an assert in
4012 ** pager_get_all_dirty_pages() that verifies that no attempt
4013 ** is made to use an invalid dirty list.
4014 */
drh153c62c2007-08-24 03:51:33 +00004015 goto sync_exit;
4016 }
danielk19778c0a7912008-08-20 14:49:23 +00004017 sqlite3PcacheCleanAll(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00004018
danielk1977f90b7262009-01-07 15:18:20 +00004019 if( pPager->dbSize<pPager->dbFileSize ){
4020 assert( pPager->state>=PAGER_EXCLUSIVE );
4021 rc = pager_truncate(pPager, pPager->dbSize);
4022 if( rc!=SQLITE_OK ) goto sync_exit;
4023 }
4024
drh80e35f42007-03-30 14:06:34 +00004025 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004026 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004027 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004028 }
4029 IOTRACE(("DBSYNC %p\n", pPager))
4030
4031 pPager->state = PAGER_SYNCED;
drh80e35f42007-03-30 14:06:34 +00004032 }
4033
4034sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004035 if( rc==SQLITE_IOERR_BLOCKED ){
4036 /* pager_incr_changecounter() may attempt to obtain an exclusive
4037 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004038 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004039 * better to return SQLITE_BUSY.
4040 */
4041 rc = SQLITE_BUSY;
4042 }
drh80e35f42007-03-30 14:06:34 +00004043 return rc;
4044}
4045
4046
4047/*
drhed7c8552001-04-11 14:29:21 +00004048** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004049**
4050** If the commit fails for any reason, a rollback attempt is made
4051** and an error code is returned. If the commit worked, SQLITE_OK
4052** is returned.
drhed7c8552001-04-11 14:29:21 +00004053*/
drh80e35f42007-03-30 14:06:34 +00004054int sqlite3PagerCommitPhaseTwo(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004055 int rc = SQLITE_OK;
drhd9b02572001-04-15 00:37:09 +00004056
danielk1977efaaf572006-01-16 11:29:19 +00004057 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004058 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004059 }
drha6abd042004-06-09 17:37:22 +00004060 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004061 return SQLITE_ERROR;
4062 }
drhd138c012008-05-13 00:58:18 +00004063 if( pPager->dbModified==0 &&
4064 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
4065 pPager->exclusiveMode!=0) ){
4066 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
4067 return SQLITE_OK;
4068 }
drh30d53702009-01-06 15:58:57 +00004069 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00004070 assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dirtyCache );
4071 rc = pager_end_transaction(pPager, pPager->setMaster);
4072 rc = pager_error(pPager, rc);
drh86f8c192007-08-22 00:39:19 +00004073 return rc;
drhed7c8552001-04-11 14:29:21 +00004074}
4075
4076/*
drha6abd042004-06-09 17:37:22 +00004077** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004078** All in-memory cache pages revert to their original data contents.
4079** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004080**
4081** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004082** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004083** process is writing trash into the journal file (SQLITE_CORRUPT) or
4084** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4085** codes are returned for all these occasions. Otherwise,
4086** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004087*/
danielk19773b8a05f2007-03-19 17:44:26 +00004088int sqlite3PagerRollback(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004089 int rc = SQLITE_OK;
drh30d53702009-01-06 15:58:57 +00004090 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00004091 if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00004092 rc = pager_end_transaction(pPager, pPager->setMaster);
danielk19778c0a7912008-08-20 14:49:23 +00004093 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004094 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004095 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004096 }
danielk19778c0a7912008-08-20 14:49:23 +00004097 rc = pPager->errCode;
drha6abd042004-06-09 17:37:22 +00004098 }else{
danielk19778c0a7912008-08-20 14:49:23 +00004099 if( pPager->state==PAGER_RESERVED ){
4100 int rc2;
4101 rc = pager_playback(pPager, 0);
4102 rc2 = pager_end_transaction(pPager, pPager->setMaster);
4103 if( rc==SQLITE_OK ){
4104 rc = rc2;
4105 }
4106 }else{
4107 rc = pager_playback(pPager, 0);
4108 }
danielk197707cb5602006-01-20 10:55:05 +00004109
danielk1977b3175382008-10-17 18:51:52 +00004110 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00004111 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00004112 }
danielk19778c0a7912008-08-20 14:49:23 +00004113
4114 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4115 ** cache. So call pager_error() on the way out to make any error
4116 ** persistent.
4117 */
4118 rc = pager_error(pPager, rc);
4119 }
drh86f8c192007-08-22 00:39:19 +00004120 return rc;
drh98808ba2001-10-18 12:34:46 +00004121}
drhd9b02572001-04-15 00:37:09 +00004122
4123/*
drh5e00f6c2001-09-13 13:46:56 +00004124** Return TRUE if the database file is opened read-only. Return FALSE
4125** if the database is (in theory) writable.
4126*/
drhf49661a2008-12-10 16:45:50 +00004127u8 sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004128 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004129}
4130
4131/*
drh0f7eb612006-08-08 13:51:43 +00004132** Return the number of references to the pager.
4133*/
danielk19773b8a05f2007-03-19 17:44:26 +00004134int sqlite3PagerRefcount(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004135 return sqlite3PcacheRefCount(pPager->pPCache);
drh0f7eb612006-08-08 13:51:43 +00004136}
4137
danielk197771d5d2c2008-09-29 11:49:47 +00004138/*
4139** Return the number of references to the specified page.
4140*/
4141int sqlite3PagerPageRefcount(DbPage *pPage){
4142 return sqlite3PcachePageRefcount(pPage);
4143}
4144
drh0f7eb612006-08-08 13:51:43 +00004145#ifdef SQLITE_TEST
4146/*
drhd9b02572001-04-15 00:37:09 +00004147** This routine is used for testing and analysis only.
4148*/
danielk19773b8a05f2007-03-19 17:44:26 +00004149int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004150 static int a[11];
danielk19778c0a7912008-08-20 14:49:23 +00004151 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
4152 a[1] = sqlite3PcachePagecount(pPager->pPCache);
4153 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
danielk1977d92db532008-11-17 04:56:24 +00004154 a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
drhd9b02572001-04-15 00:37:09 +00004155 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004156 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004157 a[6] = pPager->nHit;
4158 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004159 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004160 a[9] = pPager->nRead;
4161 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004162 return a;
4163}
danielk197717b90b52008-06-06 11:11:25 +00004164int sqlite3PagerIsMemdb(Pager *pPager){
4165 return MEMDB;
4166}
drh0f7eb612006-08-08 13:51:43 +00004167#endif
drhdd793422001-06-28 01:54:48 +00004168
drhfa86c412002-02-02 15:01:15 +00004169/*
danielk1977fd7f0452008-12-17 17:30:26 +00004170** Ensure that there are at least nSavepoint savepoints open.
drhfa86c412002-02-02 15:01:15 +00004171*/
danielk1977fd7f0452008-12-17 17:30:26 +00004172int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
4173 int rc = SQLITE_OK;
4174
danielk197712dd5492008-12-18 15:45:07 +00004175 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
danielk1977fd7f0452008-12-17 17:30:26 +00004176 int ii;
drh49b9d332009-01-02 18:10:42 +00004177 PagerSavepoint *aNew;
danielk1977fd7f0452008-12-17 17:30:26 +00004178
4179 /* Either the sub-journal is open or there are no active savepoints. */
4180 assert( pPager->nSavepoint==0 || pPager->sjfd->pMethods );
4181
4182 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
4183 ** if the allocation fails. Otherwise, zero the new portion in case a
4184 ** malloc failure occurs while populating it in the for(...) loop below.
4185 */
drh49b9d332009-01-02 18:10:42 +00004186 aNew = (PagerSavepoint *)sqlite3Realloc(
danielk1977fd7f0452008-12-17 17:30:26 +00004187 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
4188 );
4189 if( !aNew ){
4190 return SQLITE_NOMEM;
4191 }
4192 memset(&aNew[pPager->nSavepoint], 0,
4193 (nSavepoint - pPager->nSavepoint) * sizeof(PagerSavepoint)
4194 );
4195 pPager->aSavepoint = aNew;
4196 ii = pPager->nSavepoint;
4197 pPager->nSavepoint = nSavepoint;
4198
4199 /* Populate the PagerSavepoint structures just allocated. */
4200 for(/* no-op */; ii<nSavepoint; ii++){
danielk197712dd5492008-12-18 15:45:07 +00004201 assert( pPager->dbSizeValid );
danielk1977fd7f0452008-12-17 17:30:26 +00004202 aNew[ii].nOrig = pPager->dbSize;
danielk197767ddef62008-12-23 19:15:56 +00004203 if( pPager->journalOpen && pPager->journalOff>0 ){
4204 aNew[ii].iOffset = pPager->journalOff;
4205 }else{
4206 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
4207 }
danielk1977fd7f0452008-12-17 17:30:26 +00004208 aNew[ii].iSubRec = pPager->stmtNRec;
4209 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
4210 if( !aNew[ii].pInSavepoint ){
4211 return SQLITE_NOMEM;
danielk1977b3175382008-10-17 18:51:52 +00004212 }
drh334b2992007-09-06 23:28:23 +00004213 }
danielk1977fd7f0452008-12-17 17:30:26 +00004214
4215 /* Open the sub-journal, if it is not already opened. */
4216 rc = openSubJournal(pPager);
drh0f892532002-05-30 12:27:03 +00004217 }
danielk1977fd7f0452008-12-17 17:30:26 +00004218
drh86f8c192007-08-22 00:39:19 +00004219 return rc;
4220}
drhfa86c412002-02-02 15:01:15 +00004221
4222/*
danielk1977fd7f0452008-12-17 17:30:26 +00004223** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
4224** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
4225** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
4226** that have occured since savepoint iSavepoint was created.
4227**
4228** In either case, all savepoints with an index greater than iSavepoint
4229** are destroyed.
4230**
4231** If there are less than (iSavepoint+1) active savepoints when this
4232** function is called it is a no-op.
4233*/
4234int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
4235 int rc = SQLITE_OK;
4236
4237 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
4238
4239 if( iSavepoint<pPager->nSavepoint ){
4240 int ii;
4241 int nNew = iSavepoint + (op==SAVEPOINT_ROLLBACK);
4242 for(ii=nNew; ii<pPager->nSavepoint; ii++){
4243 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
4244 }
4245 pPager->nSavepoint = nNew;
4246
danielk19778a7adb02008-12-22 11:43:35 +00004247 if( op==SAVEPOINT_ROLLBACK && pPager->jfd->pMethods ){
danielk1977f3107512008-12-22 10:58:46 +00004248 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
danielk1977fd7f0452008-12-17 17:30:26 +00004249 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
4250 assert(rc!=SQLITE_DONE);
4251 }
danielk1977f3107512008-12-22 10:58:46 +00004252
danielk1977fd7f0452008-12-17 17:30:26 +00004253 /* If this is a release of the outermost savepoint, truncate
4254 ** the sub-journal. */
4255 if( nNew==0 && op==SAVEPOINT_RELEASE && pPager->sjfd->pMethods ){
4256 assert( rc==SQLITE_OK );
4257 rc = sqlite3OsTruncate(pPager->sjfd, 0);
4258 pPager->stmtNRec = 0;
danielk1977b3175382008-10-17 18:51:52 +00004259 }
drh663fc632002-02-02 18:49:19 +00004260 }
drhfa86c412002-02-02 15:01:15 +00004261 return rc;
4262}
4263
drh73509ee2003-04-06 20:44:45 +00004264/*
4265** Return the full pathname of the database file.
4266*/
danielk19773b8a05f2007-03-19 17:44:26 +00004267const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004268 return pPager->zFilename;
4269}
4270
drhb20ea9d2004-02-09 01:20:36 +00004271/*
drhd0679ed2007-08-28 22:24:34 +00004272** Return the VFS structure for the pager.
4273*/
4274const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4275 return pPager->pVfs;
4276}
4277
4278/*
drhcc6bb3e2007-08-31 16:11:35 +00004279** Return the file handle for the database file associated
4280** with the pager. This might return NULL if the file has
4281** not yet been opened.
4282*/
4283sqlite3_file *sqlite3PagerFile(Pager *pPager){
4284 return pPager->fd;
4285}
4286
4287/*
danielk19775865e3d2004-06-14 06:03:57 +00004288** Return the directory of the database file.
4289*/
danielk19773b8a05f2007-03-19 17:44:26 +00004290const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004291 return pPager->zDirectory;
4292}
4293
4294/*
4295** Return the full pathname of the journal file.
4296*/
danielk19773b8a05f2007-03-19 17:44:26 +00004297const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004298 return pPager->zJournal;
4299}
4300
4301/*
drh2c8997b2005-08-27 16:36:48 +00004302** Return true if fsync() calls are disabled for this pager. Return FALSE
4303** if fsync()s are executed normally.
4304*/
danielk19773b8a05f2007-03-19 17:44:26 +00004305int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004306 return pPager->noSync;
4307}
4308
drh7c4ac0c2007-04-05 11:25:58 +00004309#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004310/*
drhb20ea9d2004-02-09 01:20:36 +00004311** Set the codec for this pager
4312*/
danielk19773b8a05f2007-03-19 17:44:26 +00004313void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004314 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004315 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004316 void *pCodecArg
4317){
4318 pPager->xCodec = xCodec;
4319 pPager->pCodecArg = pCodecArg;
4320}
drh7c4ac0c2007-04-05 11:25:58 +00004321#endif
drhb20ea9d2004-02-09 01:20:36 +00004322
danielk1977687566d2004-11-02 12:56:41 +00004323#ifndef SQLITE_OMIT_AUTOVACUUM
4324/*
danielk197787c29a92008-01-18 11:33:16 +00004325** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004326**
drh5e385312007-06-16 04:42:12 +00004327** There must be no references to the page previously located at
4328** pgno (which we call pPgOld) though that page is allowed to be
drhb3df2e12008-09-17 20:06:26 +00004329** in cache. If the page previously located at pgno is not already
drh5e385312007-06-16 04:42:12 +00004330** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004331**
drh5e385312007-06-16 04:42:12 +00004332** References to the page pPg remain valid. Updating any
4333** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004334** allocated along with the page) is the responsibility of the caller.
4335**
danielk19775fd057a2005-03-09 13:09:43 +00004336** A transaction must be active when this routine is called. It used to be
4337** required that a statement transaction was not active, but this restriction
4338** has been removed (CREATE INDEX needs to move a page when a statement
4339** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00004340**
4341** If the fourth argument, isCommit, is non-zero, then this page is being
4342** moved as part of a database reorganization just before the transaction
4343** is being committed. In this case, it is guaranteed that the database page
4344** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00004345*/
danielk19774c999992008-07-16 18:17:55 +00004346int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00004347 PgHdr *pPgOld; /* The page being overwritten. */
danielk197794daf7f2004-11-08 09:26:09 +00004348 Pgno needSyncPgno = 0;
danielk19771fab7b62009-01-07 10:35:18 +00004349 int rc;
danielk1977687566d2004-11-02 12:56:41 +00004350
danielk1977687566d2004-11-02 12:56:41 +00004351 assert( pPg->nRef>0 );
4352
danielk19771fab7b62009-01-07 10:35:18 +00004353 /* If the page being moved is dirty and has not been saved by the latest
4354 ** savepoint, then save the current contents of the page into the
4355 ** sub-journal now. This is required to handle the following scenario:
4356 **
4357 ** BEGIN;
4358 ** <journal page X, then modify it in memory>
4359 ** SAVEPOINT one;
4360 ** <Move page X to location Y>
4361 ** ROLLBACK TO one;
4362 **
4363 ** If page X were not written to the sub-journal here, it would not
4364 ** be possible to restore its contents when the "ROLLBACK TO one"
4365 ** statement were processed.
4366 */
4367 if( pPg->flags&PGHDR_DIRTY
4368 && subjRequiresPage(pPg)
4369 && SQLITE_OK!=(rc = subjournalPage(pPg))
4370 ){
4371 return rc;
4372 }
4373
drh30d53702009-01-06 15:58:57 +00004374 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
4375 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
drhb0603412007-02-28 04:47:26 +00004376 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004377
danielk1977b4626a32007-04-28 15:47:43 +00004378 pager_get_content(pPg);
danielk19774c999992008-07-16 18:17:55 +00004379
4380 /* If the journal needs to be sync()ed before page pPg->pgno can
4381 ** be written to, store pPg->pgno in local variable needSyncPgno.
4382 **
4383 ** If the isCommit flag is set, there is no need to remember that
4384 ** the journal needs to be sync()ed before database page pPg->pgno
4385 ** can be written to. The caller has already promised not to write to it.
4386 */
danielk19778c0a7912008-08-20 14:49:23 +00004387 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00004388 needSyncPgno = pPg->pgno;
danielk19773460d192008-12-27 15:23:13 +00004389 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
danielk19778c0a7912008-08-20 14:49:23 +00004390 assert( pPg->flags&PGHDR_DIRTY );
danielk1977ae825582004-11-23 09:06:55 +00004391 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004392 }
4393
danielk1977ef73ee92004-11-06 12:26:07 +00004394 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00004395 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00004396 ** page pgno before the 'move' operation, it needs to be retained
4397 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004398 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00004399 pPg->flags &= ~PGHDR_NEED_SYNC;
danielk1977687566d2004-11-02 12:56:41 +00004400 pPgOld = pager_lookup(pPager, pgno);
danielk19778c0a7912008-08-20 14:49:23 +00004401 assert( !pPgOld || pPgOld->nRef==1 );
danielk1977687566d2004-11-02 12:56:41 +00004402 if( pPgOld ){
danielk19778c0a7912008-08-20 14:49:23 +00004403 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
drh5e385312007-06-16 04:42:12 +00004404 }
danielk1977687566d2004-11-02 12:56:41 +00004405
danielk19778c0a7912008-08-20 14:49:23 +00004406 sqlite3PcacheMove(pPg, pgno);
4407 if( pPgOld ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00004408 sqlite3PcacheDrop(pPgOld);
danielk1977f5fdda82004-11-03 08:44:05 +00004409 }
danielk1977f5fdda82004-11-03 08:44:05 +00004410
drhc047b9f2008-11-21 03:23:43 +00004411 sqlite3PcacheMakeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004412 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004413 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00004414
danielk197794daf7f2004-11-08 09:26:09 +00004415 if( needSyncPgno ){
4416 /* If needSyncPgno is non-zero, then the journal file needs to be
4417 ** sync()ed before any data is written to database file page needSyncPgno.
4418 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00004419 ** "is journaled" bitvec flag has been set. This needs to be remedied by
4420 ** loading the page into the pager-cache and setting the PgHdr.needSync
4421 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00004422 **
danielk1977a98d7b42008-01-18 13:42:54 +00004423 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00004424 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00004425 ** array. Otherwise, if the page is loaded and written again in
4426 ** this transaction, it may be written to the database file before
4427 ** it is synced into the journal file. This way, it may end up in
4428 ** the journal file twice, but that is not a problem.
4429 **
danielk19773b8a05f2007-03-19 17:44:26 +00004430 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004431 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004432 */
danielk19773b8a05f2007-03-19 17:44:26 +00004433 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004434 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004435 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00004436 if( rc!=SQLITE_OK ){
danielk19773460d192008-12-27 15:23:13 +00004437 if( pPager->pInJournal && needSyncPgno<=pPager->dbOrigSize ){
drhf5e7bb52008-02-18 14:47:33 +00004438 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00004439 }
danielk197787c29a92008-01-18 11:33:16 +00004440 return rc;
4441 }
danielk1977ae825582004-11-23 09:06:55 +00004442 pPager->needSync = 1;
drhb3df2e12008-09-17 20:06:26 +00004443 assert( pPager->noSync==0 && !MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00004444 pPgHdr->flags |= PGHDR_NEED_SYNC;
drhc047b9f2008-11-21 03:23:43 +00004445 sqlite3PcacheMakeDirty(pPgHdr);
danielk19773b8a05f2007-03-19 17:44:26 +00004446 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004447 }
4448
danielk1977687566d2004-11-02 12:56:41 +00004449 return SQLITE_OK;
4450}
4451#endif
4452
danielk19773b8a05f2007-03-19 17:44:26 +00004453/*
4454** Return a pointer to the data for the specified page.
4455*/
4456void *sqlite3PagerGetData(DbPage *pPg){
danielk197771d5d2c2008-09-29 11:49:47 +00004457 assert( pPg->nRef>0 || pPg->pPager->memDb );
danielk19778c0a7912008-08-20 14:49:23 +00004458 return pPg->pData;
danielk19773b8a05f2007-03-19 17:44:26 +00004459}
4460
4461/*
4462** Return a pointer to the Pager.nExtra bytes of "extra" space
4463** allocated along with the specified page.
4464*/
4465void *sqlite3PagerGetExtra(DbPage *pPg){
4466 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00004467 return (pPager?pPg->pExtra:0);
danielk19773b8a05f2007-03-19 17:44:26 +00004468}
4469
danielk197741483462007-03-24 16:45:04 +00004470/*
4471** Get/set the locking-mode for this pager. Parameter eMode must be one
4472** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4473** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4474** the locking-mode is set to the value specified.
4475**
4476** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4477** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4478** locking-mode.
4479*/
4480int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004481 assert( eMode==PAGER_LOCKINGMODE_QUERY
4482 || eMode==PAGER_LOCKINGMODE_NORMAL
4483 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4484 assert( PAGER_LOCKINGMODE_QUERY<0 );
4485 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4486 if( eMode>=0 && !pPager->tempFile ){
drh1bd10f82008-12-10 21:19:56 +00004487 pPager->exclusiveMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +00004488 }
4489 return (int)pPager->exclusiveMode;
4490}
4491
drh3b020132008-04-17 17:02:01 +00004492/*
drh04335882008-09-26 21:08:08 +00004493** Get/set the journal-mode for this pager. Parameter eMode must be one of:
drh3b020132008-04-17 17:02:01 +00004494**
drh04335882008-09-26 21:08:08 +00004495** PAGER_JOURNALMODE_QUERY
4496** PAGER_JOURNALMODE_DELETE
4497** PAGER_JOURNALMODE_TRUNCATE
4498** PAGER_JOURNALMODE_PERSIST
4499** PAGER_JOURNALMODE_OFF
4500**
4501** If the parameter is not _QUERY, then the journal-mode is set to the
4502** value specified.
4503**
4504** The returned indicate the current (possibly updated)
drh3b020132008-04-17 17:02:01 +00004505** journal-mode.
4506*/
4507int sqlite3PagerJournalMode(Pager *pPager, int eMode){
danielk1977b3175382008-10-17 18:51:52 +00004508 if( !MEMDB ){
4509 assert( eMode==PAGER_JOURNALMODE_QUERY
4510 || eMode==PAGER_JOURNALMODE_DELETE
4511 || eMode==PAGER_JOURNALMODE_TRUNCATE
4512 || eMode==PAGER_JOURNALMODE_PERSIST
4513 || eMode==PAGER_JOURNALMODE_OFF
4514 || eMode==PAGER_JOURNALMODE_MEMORY );
4515 assert( PAGER_JOURNALMODE_QUERY<0 );
4516 if( eMode>=0 ){
drh1bd10f82008-12-10 21:19:56 +00004517 pPager->journalMode = (u8)eMode;
danielk1977b3175382008-10-17 18:51:52 +00004518 }else{
4519 assert( eMode==PAGER_JOURNALMODE_QUERY );
4520 }
drh3b020132008-04-17 17:02:01 +00004521 }
drhfdc40e92008-04-17 20:59:37 +00004522 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00004523}
4524
danielk1977b53e4962008-06-04 06:45:59 +00004525/*
4526** Get/set the size-limit used for persistent journal files.
4527*/
4528i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
4529 if( iLimit>=-1 ){
4530 pPager->journalSizeLimit = iLimit;
4531 }
4532 return pPager->journalSizeLimit;
4533}
4534
drh2e66f0b2005-04-28 17:18:48 +00004535#endif /* SQLITE_OMIT_DISKIO */