blob: 6a65c09e8cafd615e53da989f8ed3a5646baf7ed [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**
danielk1977112f7522009-01-08 17:50:45 +000021** @(#) $Id: pager.c,v 1.542 2009/01/08 17:50:46 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/*
danielk1977fd7f0452008-12-17 17:30:26 +0000120** An instance of the following structure is allocated for each active
121** savepoint and statement transaction in the system. All such structures
122** are stored in the Pager.aSavepoint[] array, which is allocated and
123** resized using sqlite3Realloc().
124**
125** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
126** set to 0. If a journal-header is written into the main journal while
127** the savepoint is active, then iHdrOffset is set to the byte offset
128** immediately following the last journal record written into the main
129** journal before the journal-header. This is required during savepoint
130** rollback (see pagerPlaybackSavepoint()).
131*/
132typedef struct PagerSavepoint PagerSavepoint;
133struct PagerSavepoint {
134 i64 iOffset; /* Starting offset in main journal */
135 i64 iHdrOffset; /* See above */
136 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
137 Pgno nOrig; /* Original number of pages in file */
138 Pgno iSubRec; /* Index of first record in sub-journal */
139};
140
141/*
drhed7c8552001-04-11 14:29:21 +0000142** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000143**
drh4f0ee682007-03-30 20:43:40 +0000144** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000145** or SQLITE_FULL. Once one of the first three errors occurs, it persists
146** and is returned as the result of every major pager API call. The
147** SQLITE_FULL return code is slightly different. It persists only until the
148** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000149** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000150** APIs, they may still be used successfully.
danielk19773460d192008-12-27 15:23:13 +0000151**
152** Managing the size of the database file in pages is a little complicated.
153** The variable Pager.dbSize contains the number of pages that the database
154** image currently contains. As the database image grows or shrinks this
155** variable is updated. The variable Pager.dbFileSize contains the number
156** of pages in the database file. This may be different from Pager.dbSize
157** if some pages have been appended to the database image but not yet written
158** out from the cache to the actual file on disk. Or if the image has been
159** truncated by an incremental-vacuum operation. The Pager.dbOrigSize variable
160** contains the number of pages in the database image when the current
161** transaction was opened. The contents of all three of these variables is
162** only guaranteed to be correct if the boolean Pager.dbSizeValid is true.
drhed7c8552001-04-11 14:29:21 +0000163*/
164struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000165 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000166 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000167 u8 journalStarted; /* True if header of journal is synced */
168 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000169 u8 noReadlock; /* Do not bother to obtain readlocks */
drh603240c2002-03-05 01:11:12 +0000170 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000171 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000172 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000173 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000174 u8 tempFile; /* zFilename is a temporary file */
175 u8 readOnly; /* True for a read-only database */
176 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000177 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000178 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000179 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000180 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000181 u8 doNotSync; /* Boolean. While true, do not spill the cache */
182 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000183 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drhd138c012008-05-13 00:58:18 +0000184 u8 dbModified; /* True if there are any changes to the Db */
drh80e35f42007-03-30 14:06:34 +0000185 u8 changeCountDone; /* Set after incrementing the change-counter */
danielk1977d92db532008-11-17 04:56:24 +0000186 u8 dbSizeValid; /* Set when dbSize is correct */
danielk19773460d192008-12-27 15:23:13 +0000187 Pgno dbSize; /* Number of pages in the database */
188 Pgno dbOrigSize; /* dbSize before the current transaction */
189 Pgno dbFileSize; /* Number of pages in the database file */
drh33f4e022007-09-03 15:19:34 +0000190 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000191 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000192 int nRec; /* Number of pages written to the journal */
193 u32 cksumInit; /* Quasi-random value added to every checksum */
194 int stmtNRec; /* Number of records in stmt subjournal */
195 int nExtra; /* Add this many bytes to each in-memory page */
196 int pageSize; /* Number of bytes in a page */
197 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000198 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000199 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000200 Bitvec *pInJournal; /* One bit for each page in the database file */
danielk1977a1fa00d2008-08-27 15:16:33 +0000201 Bitvec *pAlwaysRollback; /* One bit for each page marked always-rollback */
drhfcd35c72005-05-21 02:48:08 +0000202 char *zFilename; /* Name of the database file */
203 char *zJournal; /* Name of the journal file */
204 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000205 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
danielk1977fd7f0452008-12-17 17:30:26 +0000206 sqlite3_file *sjfd; /* File descriptor for the sub-journal*/
danielk19771ceedd32008-11-19 10:22:33 +0000207 int (*xBusyHandler)(void*); /* Function to call when busy */
208 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
drheb206252004-10-01 02:00:31 +0000209 i64 journalOff; /* Current byte offset in the journal file */
210 i64 journalHdr; /* Byte offset to previous journal header */
drh98c58352008-11-07 00:24:53 +0000211 u32 sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000212#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000213 int nHit, nMiss; /* Cache hits and missing */
214 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000215#endif
danielk1977eaa06f62008-09-18 17:34:44 +0000216 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000217#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000218 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000219 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000220#endif
danielk19778186df82007-03-06 13:45:59 +0000221 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000222 char dbFileVers[16]; /* Changes whenever database file changes */
danielk1977b53e4962008-06-04 06:45:59 +0000223 i64 journalSizeLimit; /* Size limit for persistent journal files */
danielk19778c0a7912008-08-20 14:49:23 +0000224 PCache *pPCache; /* Pointer to page cache object */
drh9f0bbf92009-01-02 21:08:09 +0000225 PagerSavepoint *aSavepoint; /* Array of active savepoints */
226 int nSavepoint; /* Number of elements in aSavepoint[] */
drhd9b02572001-04-15 00:37:09 +0000227};
228
229/*
drh538f5702007-04-13 02:14:30 +0000230** The following global variables hold counters used for
231** testing purposes only. These variables do not exist in
232** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000233*/
234#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000235int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
236int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
237int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
drh538f5702007-04-13 02:14:30 +0000238# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000239#else
drh538f5702007-04-13 02:14:30 +0000240# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000241#endif
242
drh538f5702007-04-13 02:14:30 +0000243
244
drhfcd35c72005-05-21 02:48:08 +0000245/*
drh5e00f6c2001-09-13 13:46:56 +0000246** Journal files begin with the following magic string. The data
247** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000248**
drhae2b40c2004-06-09 19:03:54 +0000249** Since version 2.8.0, the journal format contains additional sanity
drh30d53702009-01-06 15:58:57 +0000250** checking information. If the power fails while the journal is being
drhae2b40c2004-06-09 19:03:54 +0000251** written, semi-random garbage data might appear in the journal
252** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000253** to roll the journal back, the database could be corrupted. The additional
254** sanity checking data is an attempt to discover the garbage in the
255** journal and ignore it.
256**
drhae2b40c2004-06-09 19:03:54 +0000257** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000258** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000259** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000260** This cksum is initialized to a 32-bit random value that appears in the
261** journal file right after the header. The random initializer is important,
262** because garbage data that appears at the end of a journal is likely
263** data that was once in other files that have now been deleted. If the
264** garbage data came from an obsolete journal file, the checksums might
265** be correct. But by initializing the checksum to random value which
266** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000267*/
drhae2b40c2004-06-09 19:03:54 +0000268static const unsigned char aJournalMagic[] = {
269 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000270};
271
272/*
drh726de592004-06-10 23:35:50 +0000273** The size of the header and of each page in the journal is determined
274** by the following macros.
drh968af522003-02-11 14:55:40 +0000275*/
drhae2b40c2004-06-09 19:03:54 +0000276#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000277
danielk197776572402004-06-25 02:38:54 +0000278/*
279** The journal header size for this pager. In the future, this could be
280** set to some value read from the disk controller. The important
281** characteristic is that it is the same size as a disk sector.
282*/
283#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
284
drhb7f91642004-10-31 02:22:47 +0000285/*
286** The macro MEMDB is true if we are dealing with an in-memory database.
287** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
288** the value of MEMDB will be a constant and the compiler will optimize
289** out code that would never execute.
290*/
291#ifdef SQLITE_OMIT_MEMORYDB
292# define MEMDB 0
293#else
294# define MEMDB pPager->memDb
295#endif
296
297/*
danielk197776572402004-06-25 02:38:54 +0000298** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
299** reserved for working around a windows/posix incompatibility). It is
300** used in the journal to signify that the remainder of the journal file
301** is devoted to storing a master journal name - there are no more pages to
302** roll back. See comments for function writeMasterJournal() for details.
303*/
danielk1977599fcba2004-11-08 07:13:13 +0000304/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
danielk1977d92db532008-11-17 04:56:24 +0000305#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
danielk197713adf8a2004-06-03 16:08:41 +0000306
drh968af522003-02-11 14:55:40 +0000307/*
danielk197726836652005-01-17 01:33:13 +0000308** The maximum legal page number is (2^31 - 1).
309*/
310#define PAGER_MAX_PGNO 2147483647
311
312/*
danielk19773460d192008-12-27 15:23:13 +0000313** Return true if it is necessary to write page *pPg into the sub-journal.
314** A page needs to be written into the sub-journal if there exists one
315** or more open savepoints for which:
danielk1977fd7f0452008-12-17 17:30:26 +0000316**
danielk19773460d192008-12-27 15:23:13 +0000317** * The page-number is less than or equal to PagerSavepoint.nOrig, and
318** * The bit corresponding to the page-number is not set in
319** PagerSavepoint.pInSavepoint.
danielk1977f35843b2007-04-07 15:03:17 +0000320*/
danielk19773460d192008-12-27 15:23:13 +0000321static int subjRequiresPage(PgHdr *pPg){
322 Pgno pgno = pPg->pgno;
danielk1977f35843b2007-04-07 15:03:17 +0000323 Pager *pPager = pPg->pPager;
danielk19773460d192008-12-27 15:23:13 +0000324 int i;
325 for(i=0; i<pPager->nSavepoint; i++){
326 PagerSavepoint *p = &pPager->aSavepoint[i];
327 if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
328 return 1;
329 }
danielk1977fd7f0452008-12-17 17:30:26 +0000330 }
danielk19773460d192008-12-27 15:23:13 +0000331 return 0;
danielk1977f35843b2007-04-07 15:03:17 +0000332}
drh8ca0c722006-05-07 17:49:38 +0000333
danielk19773460d192008-12-27 15:23:13 +0000334/*
335** Return true if the page is already in the journal file.
336*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000337static int pageInJournal(PgHdr *pPg){
338 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
339}
340
drh8ca0c722006-05-07 17:49:38 +0000341/*
drh34e79ce2004-02-08 06:05:46 +0000342** Read a 32-bit integer from the given file descriptor. Store the integer
343** that is read in *pRes. Return SQLITE_OK if everything worked, or an
344** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000345**
346** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000347*/
danielk197762079062007-08-15 17:08:46 +0000348static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000349 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000350 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000351 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000352 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000353 }
drh94f33312002-08-12 12:29:56 +0000354 return rc;
355}
356
357/*
drh97b57482006-01-10 20:32:32 +0000358** Write a 32-bit integer into a string buffer in big-endian byte order.
359*/
drha3152892007-05-05 11:48:52 +0000360#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000361
362/*
drh34e79ce2004-02-08 06:05:46 +0000363** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
364** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000365*/
danielk197762079062007-08-15 17:08:46 +0000366static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000367 char ac[4];
drh97b57482006-01-10 20:32:32 +0000368 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000369 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000370}
371
drh2554f8b2003-01-22 01:26:44 +0000372/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000373** If file pFd is open, call sqlite3OsUnlock() on it.
374*/
375static int osUnlock(sqlite3_file *pFd, int eLock){
376 if( !pFd->pMethods ){
377 return SQLITE_OK;
378 }
379 return sqlite3OsUnlock(pFd, eLock);
380}
381
382/*
danielk1977c7b60172007-08-22 11:22:03 +0000383** This function determines whether or not the atomic-write optimization
384** can be used with this pager. The optimization can be used if:
385**
386** (a) the value returned by OsDeviceCharacteristics() indicates that
387** a database page may be written atomically, and
388** (b) the value returned by OsSectorSize() is less than or equal
389** to the page size.
390**
391** If the optimization cannot be used, 0 is returned. If it can be used,
392** then the value returned is the size of the journal file when it
393** contains rollback data for exactly one page.
394*/
395#ifdef SQLITE_ENABLE_ATOMIC_WRITE
396static int jrnlBufferSize(Pager *pPager){
397 int dc; /* Device characteristics */
398 int nSector; /* Sector size */
drhfacf0302008-06-17 15:12:00 +0000399 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000400 sqlite3_file *fd = pPager->fd;
401
402 if( fd->pMethods ){
403 dc = sqlite3OsDeviceCharacteristics(fd);
404 nSector = sqlite3OsSectorSize(fd);
drhfacf0302008-06-17 15:12:00 +0000405 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000406 }
407
408 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
409 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
410
drhfacf0302008-06-17 15:12:00 +0000411 if( !fd->pMethods ||
412 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000413 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
414 }
415 return 0;
416}
417#endif
418
419/*
danielk1977aef0bf62005-12-30 16:28:01 +0000420** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000421** code. The first argument is a pointer to the pager structure, the
422** second the error-code about to be returned by a pager API function.
423** The value returned is a copy of the second argument to this function.
424**
drh4f0ee682007-03-30 20:43:40 +0000425** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000426** the error becomes persistent. Until the persisten error is cleared,
427** subsequent API calls on this Pager will immediately return the same
428** error code.
429**
430** A persistent error indicates that the contents of the pager-cache
431** cannot be trusted. This state can be cleared by completely discarding
432** the contents of the pager-cache. If a transaction was active when
433** the persistent error occured, then the rollback journal may need
434** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000435*/
danielk1977ae72d982007-10-03 08:46:44 +0000436static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000437static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000438 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000439 assert(
440 pPager->errCode==SQLITE_FULL ||
441 pPager->errCode==SQLITE_OK ||
442 (pPager->errCode & 0xff)==SQLITE_IOERR
443 );
danielk1977979f38e2007-03-27 16:19:51 +0000444 if(
drh4ac285a2006-09-15 07:28:50 +0000445 rc2==SQLITE_FULL ||
446 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000447 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000448 ){
449 pPager->errCode = rc;
danielk19778c0a7912008-08-20 14:49:23 +0000450 if( pPager->state==PAGER_UNLOCK
451 && sqlite3PcacheRefCount(pPager->pPCache)==0
452 ){
danielk1977ae72d982007-10-03 08:46:44 +0000453 /* If the pager is already unlocked, call pager_unlock() now to
454 ** clear the error state and ensure that the pager-cache is
455 ** completely empty.
456 */
457 pager_unlock(pPager);
458 }
danielk1977aef0bf62005-12-30 16:28:01 +0000459 }
460 return rc;
461}
462
drh477731b2007-06-16 03:06:27 +0000463/*
464** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
465** on the cache using a hash function. This is used for testing
466** and debugging only.
467*/
danielk19773c407372005-02-15 02:54:14 +0000468#ifdef SQLITE_CHECK_PAGES
469/*
470** Return a 32-bit hash of the page data for pPage.
471*/
drh477731b2007-06-16 03:06:27 +0000472static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000473 u32 hash = 0;
474 int i;
drh477731b2007-06-16 03:06:27 +0000475 for(i=0; i<nByte; i++){
476 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000477 }
478 return hash;
479}
drh477731b2007-06-16 03:06:27 +0000480static u32 pager_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000481 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
482}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000483static void pager_set_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000484 pPage->pageHash = pager_pagehash(pPage);
drh477731b2007-06-16 03:06:27 +0000485}
danielk19773c407372005-02-15 02:54:14 +0000486
487/*
488** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
489** is defined, and NDEBUG is not defined, an assert() statement checks
490** that the page is either dirty or still matches the calculated page-hash.
491*/
492#define CHECK_PAGE(x) checkPage(x)
493static void checkPage(PgHdr *pPg){
494 Pager *pPager = pPg->pPager;
danielk1977b3175382008-10-17 18:51:52 +0000495 assert( !pPg->pageHash || pPager->errCode
danielk19778c0a7912008-08-20 14:49:23 +0000496 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
danielk19773c407372005-02-15 02:54:14 +0000497}
498
499#else
drh8ffa8172007-06-18 17:25:17 +0000500#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000501#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000502#define CHECK_PAGE(x)
drh41d30272008-08-20 21:47:45 +0000503#endif /* SQLITE_CHECK_PAGES */
danielk19773c407372005-02-15 02:54:14 +0000504
drhed7c8552001-04-11 14:29:21 +0000505/*
danielk197776572402004-06-25 02:38:54 +0000506** When this is called the journal file for pager pPager must be open.
507** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000508** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000509**
danielk197765839c62007-08-30 08:08:17 +0000510** zMaster must point to a buffer of at least nMaster bytes allocated by
511** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
512** enough space to write the master journal name). If the master journal
513** name in the journal is longer than nMaster bytes (including a
514** nul-terminator), then this is handled as if no master journal name
515** were present in the journal.
516**
517** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000518** SQLITE_OK returned.
519*/
danielk1977d92db532008-11-17 04:56:24 +0000520static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
danielk197776572402004-06-25 02:38:54 +0000521 int rc;
522 u32 len;
drheb206252004-10-01 02:00:31 +0000523 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000524 u32 cksum;
shane0b8d2762008-07-22 05:18:00 +0000525 u32 u; /* Unsigned loop counter */
danielk197776572402004-06-25 02:38:54 +0000526 unsigned char aMagic[8]; /* A buffer to hold the magic header */
527
danielk197765839c62007-08-30 08:08:17 +0000528 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000529
drh054889e2005-11-30 03:20:31 +0000530 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000531 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000532
danielk197762079062007-08-15 17:08:46 +0000533 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000534 if( rc!=SQLITE_OK ) return rc;
535
danielk197765839c62007-08-30 08:08:17 +0000536 if( len>=nMaster ){
537 return SQLITE_OK;
538 }
539
danielk197762079062007-08-15 17:08:46 +0000540 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000541 if( rc!=SQLITE_OK ) return rc;
542
danielk197762079062007-08-15 17:08:46 +0000543 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000544 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
545
danielk197765839c62007-08-30 08:08:17 +0000546 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000547 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000548 return rc;
549 }
danielk197765839c62007-08-30 08:08:17 +0000550 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000551
552 /* See if the checksum matches the master journal name */
shane0b8d2762008-07-22 05:18:00 +0000553 for(u=0; u<len; u++){
554 cksum -= zMaster[u];
danielk197765839c62007-08-30 08:08:17 +0000555 }
danielk19778191bff2004-06-28 04:52:30 +0000556 if( cksum ){
557 /* If the checksum doesn't add up, then one or more of the disk sectors
558 ** containing the master journal filename is corrupted. This means
559 ** definitely roll back, so just return SQLITE_OK and report a (nul)
560 ** master-journal filename.
561 */
danielk197765839c62007-08-30 08:08:17 +0000562 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000563 }
danielk197776572402004-06-25 02:38:54 +0000564
565 return SQLITE_OK;
566}
567
568/*
569** Seek the journal file descriptor to the next sector boundary where a
570** journal header may be read or written. Pager.journalOff is updated with
571** the new seek offset.
572**
573** i.e for a sector size of 512:
574**
575** Input Offset Output Offset
576** ---------------------------------------
577** 0 0
578** 512 512
579** 100 512
580** 2000 2048
581**
582*/
danielk1977112f7522009-01-08 17:50:45 +0000583static i64 journalHdrOffset(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000584 i64 offset = 0;
585 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000586 if( c ){
587 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
588 }
589 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
590 assert( offset>=c );
591 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
danielk1977112f7522009-01-08 17:50:45 +0000592 return offset;
593}
594static void seekJournalHdr(Pager *pPager){
595 pPager->journalOff = journalHdrOffset(pPager);
danielk197776572402004-06-25 02:38:54 +0000596}
597
598/*
drhf3a87622008-04-17 14:16:42 +0000599** Write zeros over the header of the journal file. This has the
600** effect of invalidating the journal file and committing the
601** transaction.
602*/
danielk1977df2566a2008-05-07 19:11:03 +0000603static int zeroJournalHdr(Pager *pPager, int doTruncate){
604 int rc = SQLITE_OK;
danielk197755a25a12008-09-11 10:29:15 +0000605 static const char zeroHdr[28] = {0};
drhf3a87622008-04-17 14:16:42 +0000606
danielk1977df2566a2008-05-07 19:11:03 +0000607 if( pPager->journalOff ){
danielk1977b53e4962008-06-04 06:45:59 +0000608 i64 iLimit = pPager->journalSizeLimit;
609
danielk1977df2566a2008-05-07 19:11:03 +0000610 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000611 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000612 rc = sqlite3OsTruncate(pPager->jfd, 0);
613 }else{
614 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
615 }
danielk197781620542008-06-07 05:19:37 +0000616 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000617 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
618 }
danielk1977b53e4962008-06-04 06:45:59 +0000619
620 /* At this point the transaction is committed but the write lock
621 ** is still held on the file. If there is a size limit configured for
622 ** the persistent journal and the journal file currently consumes more
623 ** space than that limit allows for, truncate it now. There is no need
624 ** to sync the file following this operation.
625 */
626 if( rc==SQLITE_OK && iLimit>0 ){
627 i64 sz;
628 rc = sqlite3OsFileSize(pPager->jfd, &sz);
629 if( rc==SQLITE_OK && sz>iLimit ){
630 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
631 }
632 }
drha06ecba2008-04-22 17:15:17 +0000633 }
drhf3a87622008-04-17 14:16:42 +0000634 return rc;
635}
636
637/*
danielk197776572402004-06-25 02:38:54 +0000638** The journal file must be open when this routine is called. A journal
639** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
640** current location.
641**
642** The format for the journal header is as follows:
643** - 8 bytes: Magic identifying journal format.
644** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
645** - 4 bytes: Random number used for page hash.
646** - 4 bytes: Initial database page count.
647** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000648** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000649**
danielk197767c007b2008-03-20 04:45:49 +0000650** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000651*/
652static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +0000653 int rc = SQLITE_OK;
654 char *zHeader = pPager->pTmpSpace;
danielk1977d92db532008-11-17 04:56:24 +0000655 u32 nHeader = pPager->pageSize;
656 u32 nWrite;
danielk1977fd7f0452008-12-17 17:30:26 +0000657 int ii;
danielk1977a664f8e2008-04-22 14:31:48 +0000658
659 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
660 nHeader = JOURNAL_HDR_SZ(pPager);
661 }
danielk197776572402004-06-25 02:38:54 +0000662
danielk1977fd7f0452008-12-17 17:30:26 +0000663 /* If there are active savepoints and any of them were created since the
664 ** most recent journal header was written, update the PagerSavepoint.iHdrOff
665 ** fields now.
666 */
667 for(ii=0; ii<pPager->nSavepoint; ii++){
668 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
669 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
670 }
danielk19774099f6e2007-03-19 11:25:20 +0000671 }
672
danielk197762079062007-08-15 17:08:46 +0000673 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000674 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000675
drh97b57482006-01-10 20:32:32 +0000676 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000677
678 /*
679 ** Write the nRec Field - the number of page records that follow this
680 ** journal header. Normally, zero is written to this value at this time.
681 ** After the records are added to the journal (and the journal synced,
682 ** if in full-sync mode), the zero is overwritten with the true number
683 ** of records (see syncJournal()).
684 **
685 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
686 ** reading the journal this value tells SQLite to assume that the
687 ** rest of the journal file contains valid page records. This assumption
688 ** is dangerous, as if a failure occured whilst writing to the journal
689 ** file it may contain some garbage data. There are two scenarios
690 ** where this risk can be ignored:
691 **
692 ** * When the pager is in no-sync mode. Corruption can follow a
693 ** power failure in this case anyway.
694 **
695 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
696 ** that garbage data is never appended to the journal file.
697 */
698 assert(pPager->fd->pMethods||pPager->noSync);
danielk1977b3175382008-10-17 18:51:52 +0000699 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
danielk19774cd2cd52007-08-23 14:48:23 +0000700 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
701 ){
702 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
703 }else{
704 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
705 }
706
drh97b57482006-01-10 20:32:32 +0000707 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +0000708 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +0000709 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
710 /* The initial database size */
danielk19773460d192008-12-27 15:23:13 +0000711 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
drh97b57482006-01-10 20:32:32 +0000712 /* The assumed sector size for this process */
713 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drh08609ce2008-11-29 22:49:23 +0000714
715 /* Initializing the tail of the buffer is not necessary. Everything
716 ** works find if the following memset() is omitted. But initializing
717 ** the memory prevents valgrind from complaining, so we are willing to
718 ** take the performance hit.
719 */
720 memset(&zHeader[sizeof(aJournalMagic)+16], 0,
721 nHeader-(sizeof(aJournalMagic)+16));
722
danielk197767c007b2008-03-20 04:45:49 +0000723 if( pPager->journalHdr==0 ){
724 /* The page size */
725 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
726 }
danielk197776572402004-06-25 02:38:54 +0000727
danielk1977a664f8e2008-04-22 14:31:48 +0000728 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
729 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
730 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
731 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +0000732 }
danielk1977a664f8e2008-04-22 14:31:48 +0000733
danielk197776572402004-06-25 02:38:54 +0000734 return rc;
735}
736
737/*
738** The journal file must be open when this is called. A journal header file
739** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
drhd6e5e092009-01-07 02:03:35 +0000740** file. The current location in the journal file is given by
741** pPager->journalOff. See comments above function writeJournalHdr() for
742** a description of the journal header format.
danielk197776572402004-06-25 02:38:54 +0000743**
744** If the header is read successfully, *nRec is set to the number of
745** page records following this header and *dbSize is set to the size of the
746** database before the transaction began, in pages. Also, pPager->cksumInit
747** is set to the value read from the journal header. SQLITE_OK is returned
748** in this case.
749**
750** If the journal header file appears to be corrupted, SQLITE_DONE is
drhd6e5e092009-01-07 02:03:35 +0000751** returned and *nRec and *dbSize are undefined. If JOURNAL_HDR_SZ bytes
danielk197776572402004-06-25 02:38:54 +0000752** cannot be read from the journal file an error code is returned.
753*/
754static int readJournalHdr(
755 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000756 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000757 u32 *pNRec,
758 u32 *pDbSize
759){
760 int rc;
761 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000762 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +0000763 int iPageSize;
danielk197776572402004-06-25 02:38:54 +0000764
danielk197762079062007-08-15 17:08:46 +0000765 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000766 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
767 return SQLITE_DONE;
768 }
danielk197762079062007-08-15 17:08:46 +0000769 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000770
danielk197762079062007-08-15 17:08:46 +0000771 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000772 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000773 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000774
775 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
776 return SQLITE_DONE;
777 }
778
danielk197762079062007-08-15 17:08:46 +0000779 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000780 if( rc ) return rc;
781
danielk197762079062007-08-15 17:08:46 +0000782 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000783 if( rc ) return rc;
784
danielk197762079062007-08-15 17:08:46 +0000785 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000786 if( rc ) return rc;
787
danielk197767c007b2008-03-20 04:45:49 +0000788 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
789 if( rc==SQLITE_OK
790 && iPageSize>=512
791 && iPageSize<=SQLITE_MAX_PAGE_SIZE
792 && ((iPageSize-1)&iPageSize)==0
793 ){
drh4f21c4a2008-12-10 22:15:00 +0000794 u16 pagesize = (u16)iPageSize;
danielk197767c007b2008-03-20 04:45:49 +0000795 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
796 }
797 if( rc ) return rc;
798
danielk197776572402004-06-25 02:38:54 +0000799 /* Update the assumed sector-size to match the value used by
800 ** the process that created this journal. If this journal was
801 ** created by a process other than this one, then this routine
802 ** is being called from within pager_playback(). The local value
803 ** of Pager.sectorSize is restored at the end of that routine.
804 */
drh98c58352008-11-07 00:24:53 +0000805 rc = read32bits(pPager->jfd, jrnlOff+12, &pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000806 if( rc ) return rc;
drh98c58352008-11-07 00:24:53 +0000807 if( (pPager->sectorSize & (pPager->sectorSize-1))!=0
808 || pPager->sectorSize>0x1000000 ){
809 return SQLITE_DONE;
810 }
danielk197776572402004-06-25 02:38:54 +0000811
812 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000813 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000814}
815
816
817/*
818** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000819** pPager at the current location. The master journal name must be the last
820** thing written to a journal file. If the pager is in full-sync mode, the
821** journal file descriptor is advanced to the next sector boundary before
822** anything is written. The format is:
823**
824** + 4 bytes: PAGER_MJ_PGNO.
825** + N bytes: length of master journal name.
826** + 4 bytes: N
827** + 4 bytes: Master journal name checksum.
828** + 8 bytes: aJournalMagic[].
829**
830** The master journal page checksum is the sum of the bytes in the master
831** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000832**
833** If zMaster is a NULL pointer (occurs for a single database transaction),
834** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000835*/
836static int writeMasterJournal(Pager *pPager, const char *zMaster){
837 int rc;
838 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000839 int i;
danielk197762079062007-08-15 17:08:46 +0000840 i64 jrnlOff;
danielk1977df2566a2008-05-07 19:11:03 +0000841 i64 jrnlSize;
drh97b57482006-01-10 20:32:32 +0000842 u32 cksum = 0;
843 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000844
danielk1977b3175382008-10-17 18:51:52 +0000845 if( !zMaster || pPager->setMaster ) return SQLITE_OK;
846 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ) return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000847 pPager->setMaster = 1;
848
drhea678832008-12-10 19:26:22 +0000849 len = sqlite3Strlen30(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000850 for(i=0; i<len; i++){
851 cksum += zMaster[i];
852 }
danielk197776572402004-06-25 02:38:54 +0000853
854 /* If in full-sync mode, advance to the next disk sector before writing
855 ** the master journal name. This is in case the previous page written to
856 ** the journal has already been synced.
857 */
858 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000859 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000860 }
danielk197762079062007-08-15 17:08:46 +0000861 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000862 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000863
danielk197762079062007-08-15 17:08:46 +0000864 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000865 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000866 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000867
danielk197762079062007-08-15 17:08:46 +0000868 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000869 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000870 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000871
drh97b57482006-01-10 20:32:32 +0000872 put32bits(zBuf, len);
873 put32bits(&zBuf[4], cksum);
874 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000875 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +0000876 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +0000877 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +0000878
879 /* If the pager is in peristent-journal mode, then the physical
880 ** journal-file may extend past the end of the master-journal name
881 ** and 8 bytes of magic data just written to the file. This is
882 ** dangerous because the code to rollback a hot-journal file
883 ** will not be able to find the master-journal name to determine
884 ** whether or not the journal is hot.
885 **
886 ** Easiest thing to do in this scenario is to truncate the journal
887 ** file to the required size.
888 */
889 if( (rc==SQLITE_OK)
890 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
891 && jrnlSize>jrnlOff
892 ){
893 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
894 }
danielk197776572402004-06-25 02:38:54 +0000895 return rc;
896}
897
898/*
drhed7c8552001-04-11 14:29:21 +0000899** Find a page in the hash table given its page number. Return
900** a pointer to the page or NULL if not found.
901*/
drhd9b02572001-04-15 00:37:09 +0000902static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000903 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000904 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
drhed7c8552001-04-11 14:29:21 +0000905 return p;
906}
907
908/*
danielk1977e180dd92007-04-05 17:15:52 +0000909** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +0000910** sets the state of the pager back to what it was when it was first
911** opened. Any outstanding pages are invalidated and subsequent attempts
912** to access those pages will likely result in a coredump.
913*/
drhd9b02572001-04-15 00:37:09 +0000914static void pager_reset(Pager *pPager){
danielk1977efaaf572006-01-16 11:29:19 +0000915 if( pPager->errCode ) return;
danielk19778c0a7912008-08-20 14:49:23 +0000916 sqlite3PcacheClear(pPager->pPCache);
danielk1977e277be02007-03-23 18:12:06 +0000917}
918
danielk197734cf35d2008-12-18 18:31:38 +0000919/*
920** Free all structures in the Pager.aSavepoint[] array and set both
921** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
922** if it is open and the pager is not in exclusive mode.
923*/
danielk1977fd7f0452008-12-17 17:30:26 +0000924static void releaseAllSavepoint(Pager *pPager){
925 int ii;
926 for(ii=0; ii<pPager->nSavepoint; ii++){
927 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
928 }
929 if( !pPager->exclusiveMode ){
930 sqlite3OsClose(pPager->sjfd);
931 }
932 sqlite3_free(pPager->aSavepoint);
933 pPager->aSavepoint = 0;
934 pPager->nSavepoint = 0;
danielk197767ddef62008-12-23 19:15:56 +0000935 pPager->stmtNRec = 0;
danielk1977fd7f0452008-12-17 17:30:26 +0000936}
937
danielk197734cf35d2008-12-18 18:31:38 +0000938/*
939** Set the bit number pgno in the PagerSavepoint.pInSavepoint bitvecs of
940** all open savepoints.
941*/
danielk1977fd7f0452008-12-17 17:30:26 +0000942static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
drh7539b6b2009-01-02 21:39:39 +0000943 int ii; /* Loop counter */
944 int rc = SQLITE_OK; /* Result code */
945
danielk1977fd7f0452008-12-17 17:30:26 +0000946 for(ii=0; ii<pPager->nSavepoint; ii++){
947 PagerSavepoint *p = &pPager->aSavepoint[ii];
948 if( pgno<=p->nOrig ){
drh7539b6b2009-01-02 21:39:39 +0000949 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
950 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
danielk1977fd7f0452008-12-17 17:30:26 +0000951 }
952 }
drh7539b6b2009-01-02 21:39:39 +0000953 return rc;
danielk1977fd7f0452008-12-17 17:30:26 +0000954}
955
danielk1977e277be02007-03-23 18:12:06 +0000956/*
danielk1977ae72d982007-10-03 08:46:44 +0000957** Unlock the database file.
958**
959** If the pager is currently in error state, discard the contents of
960** the cache and reset the Pager structure internal state. If there is
961** an open journal-file, then the next time a shared-lock is obtained
962** on the pager file (by this or any other process), it will be
963** treated as a hot-journal and rolled back.
964*/
965static void pager_unlock(Pager *pPager){
966 if( !pPager->exclusiveMode ){
danielk1977b3175382008-10-17 18:51:52 +0000967 int rc = osUnlock(pPager->fd, NO_LOCK);
968 if( rc ) pPager->errCode = rc;
danielk1977d92db532008-11-17 04:56:24 +0000969 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +0000970 IOTRACE(("UNLOCK %p\n", pPager))
danielk1977ae72d982007-10-03 08:46:44 +0000971
danielk1977b3175382008-10-17 18:51:52 +0000972 /* Always close the journal file when dropping the database lock.
973 ** Otherwise, another connection with journal_mode=delete might
974 ** delete the file out from under us.
975 */
976 if( pPager->journalOpen ){
977 sqlite3OsClose(pPager->jfd);
978 pPager->journalOpen = 0;
979 sqlite3BitvecDestroy(pPager->pInJournal);
980 pPager->pInJournal = 0;
981 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
982 pPager->pAlwaysRollback = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000983 }
984
danielk1977b3175382008-10-17 18:51:52 +0000985 /* If Pager.errCode is set, the contents of the pager cache cannot be
986 ** trusted. Now that the pager file is unlocked, the contents of the
987 ** cache can be discarded and the error code safely cleared.
988 */
989 if( pPager->errCode ){
990 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
991 pager_reset(pPager);
danielk1977fd7f0452008-12-17 17:30:26 +0000992 releaseAllSavepoint(pPager);
danielk1977b3175382008-10-17 18:51:52 +0000993 pPager->journalOff = 0;
994 pPager->journalStarted = 0;
danielk19773460d192008-12-27 15:23:13 +0000995 pPager->dbOrigSize = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000996 }
danielk1977b3175382008-10-17 18:51:52 +0000997
998 pPager->state = PAGER_UNLOCK;
999 pPager->changeCountDone = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001000 }
1001}
1002
1003/*
1004** Execute a rollback if a transaction is active and unlock the
1005** database file. If the pager has already entered the error state,
1006** do not attempt the rollback.
1007*/
1008static void pagerUnlockAndRollback(Pager *p){
danielk1977ae72d982007-10-03 08:46:44 +00001009 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +00001010 sqlite3BeginBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001011 sqlite3PagerRollback(p);
danielk19772d1d86f2008-06-20 14:59:51 +00001012 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001013 }
1014 pager_unlock(p);
danielk1977ae72d982007-10-03 08:46:44 +00001015}
1016
1017/*
drh80e35f42007-03-30 14:06:34 +00001018** This routine ends a transaction. A transaction is ended by either
1019** a COMMIT or a ROLLBACK.
1020**
drhed7c8552001-04-11 14:29:21 +00001021** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001022** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1023** the database lock and acquires a SHARED lock in its place if that is
1024** the appropriate thing to do. Release locks usually is appropriate,
1025** unless we are in exclusive access mode or unless this is a
1026** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1027**
1028** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001029**
1030** TODO: Consider keeping the journal file open for temporary databases.
1031** This might give a performance improvement on windows where opening
1032** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001033*/
danielk1977df2566a2008-05-07 19:11:03 +00001034static int pager_end_transaction(Pager *pPager, int hasMaster){
danielk197741483462007-03-24 16:45:04 +00001035 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001036 int rc2 = SQLITE_OK;
drha6abd042004-06-09 17:37:22 +00001037 if( pPager->state<PAGER_RESERVED ){
1038 return SQLITE_OK;
1039 }
danielk1977fd7f0452008-12-17 17:30:26 +00001040 releaseAllSavepoint(pPager);
drhda47d772002-12-02 04:25:19 +00001041 if( pPager->journalOpen ){
danielk1977b3175382008-10-17 18:51:52 +00001042 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
1043 int isMemoryJournal = sqlite3IsMemJournal(pPager->jfd);
1044 sqlite3OsClose(pPager->jfd);
1045 pPager->journalOpen = 0;
1046 if( !isMemoryJournal ){
1047 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
1048 }
1049 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE
drh04335882008-09-26 21:08:08 +00001050 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){
1051 pPager->journalOff = 0;
1052 pPager->journalStarted = 0;
1053 }else if( pPager->exclusiveMode
danielk197793f7af92008-05-09 16:57:50 +00001054 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1055 ){
1056 rc = zeroJournalHdr(pPager, hasMaster);
1057 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +00001058 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001059 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001060 }else{
drh04335882008-09-26 21:08:08 +00001061 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || rc );
danielk1977b4b47412007-08-17 15:53:36 +00001062 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001063 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +00001064 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00001065 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001066 }
danielk197741483462007-03-24 16:45:04 +00001067 }
drhf5e7bb52008-02-18 14:47:33 +00001068 sqlite3BitvecDestroy(pPager->pInJournal);
1069 pPager->pInJournal = 0;
danielk1977a1fa00d2008-08-27 15:16:33 +00001070 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
1071 pPager->pAlwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001072#ifdef SQLITE_CHECK_PAGES
danielk1977bc2ca9e2008-11-13 14:28:28 +00001073 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
danielk19773c407372005-02-15 02:54:14 +00001074#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +00001075 sqlite3PcacheCleanAll(pPager->pPCache);
danielk1977ef317ab2004-06-23 10:43:10 +00001076 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001077 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001078 }else{
drhf5e7bb52008-02-18 14:47:33 +00001079 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001080 }
danielk1977979f38e2007-03-27 16:19:51 +00001081
danielk197741483462007-03-24 16:45:04 +00001082 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001083 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001084 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001085 }else if( pPager->state==PAGER_SYNCED ){
1086 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001087 }
danielk19773460d192008-12-27 15:23:13 +00001088 pPager->dbOrigSize = 0;
danielk197776572402004-06-25 02:38:54 +00001089 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001090 pPager->needSync = 0;
danielk19778c0a7912008-08-20 14:49:23 +00001091 /* lruListSetFirstSynced(pPager); */
danielk1977f90b7262009-01-07 15:18:20 +00001092 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
danielk1977b3175382008-10-17 18:51:52 +00001093 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00001094 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00001095 }
drhd138c012008-05-13 00:58:18 +00001096 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001097
1098 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001099}
1100
drhed7c8552001-04-11 14:29:21 +00001101/*
drh968af522003-02-11 14:55:40 +00001102** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001103**
1104** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001105** random initial value and the page number. We experimented with
1106** a checksum of the entire data, but that was found to be too slow.
1107**
1108** Note that the page number is stored at the beginning of data and
1109** the checksum is stored at the end. This is important. If journal
1110** corruption occurs due to a power failure, the most likely scenario
1111** is that one end or the other of the record will be changed. It is
1112** much less likely that the two ends of the journal record will be
1113** correct and the middle be corrupt. Thus, this "checksum" scheme,
1114** though fast and simple, catches the mostly likely kind of corruption.
1115**
1116** FIX ME: Consider adding every 200th (or so) byte of the data to the
1117** checksum. That way if a single page spans 3 or more disk sectors and
1118** only the middle sector is corrupt, we will still have a reasonable
1119** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001120*/
drh74161702006-02-24 02:53:49 +00001121static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001122 u32 cksum = pPager->cksumInit;
1123 int i = pPager->pageSize-200;
1124 while( i>0 ){
1125 cksum += aData[i];
1126 i -= 200;
1127 }
drh968af522003-02-11 14:55:40 +00001128 return cksum;
1129}
1130
1131/*
drhd6e5e092009-01-07 02:03:35 +00001132** Read a single page from either the journal file (if isMainJrnl==1) or
1133** from the sub-journal (if isMainJrnl==0) and playback that page.
1134** The page begins at offset *pOffset into the file. The *pOffset
1135** value is increased to the start of the next page in the journal.
drh968af522003-02-11 14:55:40 +00001136**
drhc13148f2008-08-27 18:03:20 +00001137** The isMainJrnl flag is true if this is the main rollback journal and
1138** false for the statement journal. The main rollback journal uses
1139** checksums - the statement journal does not.
drhd6e5e092009-01-07 02:03:35 +00001140**
1141** If pDone is not NULL, then it is a record of pages that have already
1142** been played back. If the page at *pOffset has already been played back
1143** (if the corresponding pDone bit is set) then skip the playback.
1144** Make sure the pDone bit corresponding to the *pOffset page is set
1145** prior to returning.
drhfa86c412002-02-02 15:01:15 +00001146*/
danielk197762079062007-08-15 17:08:46 +00001147static int pager_playback_one_page(
danielk1977fd7f0452008-12-17 17:30:26 +00001148 Pager *pPager, /* The pager being played back */
1149 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
drhd6e5e092009-01-07 02:03:35 +00001150 i64 *pOffset, /* Offset of record to playback */
danielk1977ecfef982008-12-20 08:39:57 +00001151 int isSavepnt, /* True for a savepoint rollback */
danielk1977fd7f0452008-12-17 17:30:26 +00001152 Bitvec *pDone /* Bitvec of pages already played back */
danielk197762079062007-08-15 17:08:46 +00001153){
drhfa86c412002-02-02 15:01:15 +00001154 int rc;
drhae2b40c2004-06-09 19:03:54 +00001155 PgHdr *pPg; /* An existing page in the cache */
1156 Pgno pgno; /* The page number of a page in journal */
1157 u32 cksum; /* Checksum used for sanity checking */
drhd6e5e092009-01-07 02:03:35 +00001158 u8 *aData; /* Temporary storage for the page */
1159 sqlite3_file *jfd; /* The file descriptor for the journal file */
drhfa86c412002-02-02 15:01:15 +00001160
drhd6e5e092009-01-07 02:03:35 +00001161 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
1162 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
1163 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
1164 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
drh96362842005-03-20 22:47:56 +00001165
drhd6e5e092009-01-07 02:03:35 +00001166 aData = (u8*)pPager->pTmpSpace;
1167 assert( aData ); /* Temp storage must have already been allocated */
1168
1169 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
1170
1171 rc = read32bits(jfd, *pOffset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001172 if( rc!=SQLITE_OK ) return rc;
drhd6e5e092009-01-07 02:03:35 +00001173 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, (*pOffset)+4);
drh99ee3602003-02-16 19:13:36 +00001174 if( rc!=SQLITE_OK ) return rc;
drhd6e5e092009-01-07 02:03:35 +00001175 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
drhfa86c412002-02-02 15:01:15 +00001176
drh968af522003-02-11 14:55:40 +00001177 /* Sanity checking on the page. This is more important that I originally
1178 ** thought. If a power failure occurs while the journal is being written,
1179 ** it could cause invalid data to be written into the journal. We need to
1180 ** detect this invalid data (with high probability) and ignore it.
1181 */
danielk197775edc162004-06-26 01:48:18 +00001182 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001183 return SQLITE_DONE;
1184 }
danielk1977fd7f0452008-12-17 17:30:26 +00001185 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
drh968af522003-02-11 14:55:40 +00001186 return SQLITE_OK;
1187 }
drhc13148f2008-08-27 18:03:20 +00001188 if( isMainJrnl ){
drhd6e5e092009-01-07 02:03:35 +00001189 rc = read32bits(jfd, (*pOffset)-4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001190 if( rc ) return rc;
danielk1977ecfef982008-12-20 08:39:57 +00001191 if( !isSavepnt && pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001192 return SQLITE_DONE;
1193 }
1194 }
danielk1977fd7f0452008-12-17 17:30:26 +00001195 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno)) ){
1196 return rc;
1197 }
drhfa86c412002-02-02 15:01:15 +00001198
danielk1977aa5ccdf2004-06-14 05:10:42 +00001199 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001200
1201 /* If the pager is in RESERVED state, then there must be a copy of this
1202 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001203 ** not the database file. The page is left marked dirty in this case.
1204 **
danielk19772df71c72007-05-24 07:22:42 +00001205 ** An exception to the above rule: If the database is in no-sync mode
1206 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001207 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1208 ** during a Movepage() call, then the page may not be in the cache
1209 ** either. So the condition described in the above paragraph is not
1210 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001211 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001212 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1213 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001214 **
1215 ** Ticket #1171: The statement journal might contain page content that is
1216 ** different from the page content at the start of the transaction.
1217 ** This occurs when a page is changed prior to the start of a statement
1218 ** then changed again within the statement. When rolling back such a
1219 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001220 ** for certain that original page contents are synced into the main rollback
1221 ** journal. Otherwise, a power loss might leave modified data in the
1222 ** database file without an entry in the rollback journal that can
1223 ** restore the database to its original form. Two conditions must be
1224 ** met before writing to the database files. (1) the database must be
1225 ** locked. (2) we know that the original page content is fully synced
1226 ** in the main journal either because the page is not in cache or else
1227 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001228 **
1229 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1230 ** is possible to fail a statement on a database that does not yet exist.
1231 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001232 */
drhae2b40c2004-06-09 19:03:54 +00001233 pPg = pager_lookup(pPager, pgno);
drh30d53702009-01-06 15:58:57 +00001234 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
danielk1977ecfef982008-12-20 08:39:57 +00001235 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData),
1236 (isMainJrnl?"main-journal":"sub-journal")
drh30d53702009-01-06 15:58:57 +00001237 ));
danielk19778c0a7912008-08-20 14:49:23 +00001238 if( (pPager->state>=PAGER_EXCLUSIVE)
1239 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
1240 && (pPager->fd->pMethods)
1241 ){
drh281b21d2008-08-22 12:57:08 +00001242 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
1243 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
danielk19773460d192008-12-27 15:23:13 +00001244 if( pgno>pPager->dbFileSize ){
1245 pPager->dbFileSize = pgno;
1246 }
danielk1977f2c31ad2009-01-06 13:40:08 +00001247 }else if( !isMainJrnl && pPg==0 ){
1248 /* If this is a rollback of a savepoint and data was not written to
1249 ** the database and the page is not in-memory, there is a potential
1250 ** problem. When the page is next fetched by the b-tree layer, it
1251 ** will be read from the database file, which may or may not be
1252 ** current.
1253 **
1254 ** There are a couple of different ways this can happen. All are quite
danielk1977401b65e2009-01-06 14:34:34 +00001255 ** obscure. When running in synchronous mode, this can only happen
danielk1977f2c31ad2009-01-06 13:40:08 +00001256 ** if the page is on the free-list at the start of the transaction, then
1257 ** populated, then moved using sqlite3PagerMovepage().
1258 **
1259 ** The solution is to add an in-memory page to the cache containing
1260 ** the data just read from the sub-journal. Mark the page as dirty
1261 ** and if the pager requires a journal-sync, then mark the page as
1262 ** requiring a journal-sync before it is written.
1263 */
1264 assert( isSavepnt );
1265 if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1)) ){
1266 return rc;
1267 }
1268 pPg->flags &= ~PGHDR_NEED_READ;
1269 sqlite3PcacheMakeDirty(pPg);
danielk1977a3f3a5f2004-06-10 04:32:16 +00001270 }
drhfa86c412002-02-02 15:01:15 +00001271 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001272 /* No page should ever be explicitly rolled back that is in use, except
1273 ** for page 1 which is held in use in order to keep the lock on the
1274 ** database active. However such a page may be rolled back as a result
1275 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001276 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001277 */
drhb6f41482004-05-14 01:58:11 +00001278 void *pData;
danielk19778c0a7912008-08-20 14:49:23 +00001279 pData = pPg->pData;
drhae2b40c2004-06-09 19:03:54 +00001280 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001281 if( pPager->xReiniter ){
danielk1977eaa06f62008-09-18 17:34:44 +00001282 pPager->xReiniter(pPg);
drhde647132004-05-07 17:57:49 +00001283 }
danielk1977ecfef982008-12-20 08:39:57 +00001284 if( isMainJrnl && (!isSavepnt || pPager->journalOff<=pPager->journalHdr) ){
danielk1977488af092008-12-19 16:31:11 +00001285 /* If the contents of this page were just restored from the main
1286 ** journal file, then its content must be as they were when the
1287 ** transaction was first opened. In this case we can mark the page
1288 ** as clean, since there will be no need to write it out to the.
1289 **
1290 ** There is one exception to this rule. If the page is being rolled
1291 ** back as part of a savepoint (or statement) rollback from an
1292 ** unsynced portion of the main journal file, then it is not safe
1293 ** to mark the page as clean. This is because marking the page as
1294 ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
1295 ** already in the journal file (recorded in Pager.pInJournal) and
1296 ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
1297 ** again within this transaction, it will be marked as dirty but
1298 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
1299 ** be written out into the database file before its journal file
1300 ** segment is synced. If a crash occurs during or following this,
1301 ** database corruption may ensue.
1302 */
drhc047b9f2008-11-21 03:23:43 +00001303 sqlite3PcacheMakeClean(pPg);
1304 }
danielk19773c407372005-02-15 02:54:14 +00001305#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001306 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001307#endif
drh86a88112007-04-16 15:02:19 +00001308 /* If this was page 1, then restore the value of Pager.dbFileVers.
1309 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001310 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001311 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001312 }
drh86a88112007-04-16 15:02:19 +00001313
1314 /* Decode the page just read from disk */
1315 CODEC1(pPager, pData, pPg->pgno, 3);
danielk19778c0a7912008-08-20 14:49:23 +00001316 sqlite3PcacheRelease(pPg);
drhfa86c412002-02-02 15:01:15 +00001317 }
1318 return rc;
1319}
1320
drhee03d622009-01-07 15:33:45 +00001321#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
drhd6e5e092009-01-07 02:03:35 +00001322/*
1323** This routine looks ahead into the main journal file and determines
1324** whether or not the next record (the record that begins at file
1325** offset pPager->journalOff) is a well-formed page record consisting
1326** of a valid page number, pPage->pageSize bytes of content, followed
1327** by a valid checksum.
1328**
1329** The pager never needs to know this in order to do its job. This
1330** routine is only used from with assert() and testcase() macros.
1331*/
1332static int pagerNextJournalPageIsValid(Pager *pPager){
1333 Pgno pgno; /* The page number of the page */
1334 u32 cksum; /* The page checksum */
1335 int rc; /* Return code from read operations */
1336 sqlite3_file *fd; /* The file descriptor from which we are reading */
1337 u8 *aData; /* Content of the page */
1338
1339 /* Read the page number header */
1340 fd = pPager->jfd;
1341 rc = read32bits(fd, pPager->journalOff, &pgno);
1342 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1343 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ return 0; } /*NO_TEST*/
1344 if( pgno>(Pgno)pPager->dbSize ){ return 0; } /*NO_TEST*/
1345
1346 /* Read the checksum */
1347 rc = read32bits(fd, pPager->journalOff+pPager->pageSize+4, &cksum);
1348 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1349
1350 /* Read the data and verify the checksum */
1351 aData = (u8*)pPager->pTmpSpace;
1352 rc = sqlite3OsRead(fd, aData, pPager->pageSize, pPager->journalOff+4);
1353 if( rc!=SQLITE_OK ){ return 0; } /*NO_TEST*/
1354 if( pager_cksum(pPager, aData)!=cksum ){ return 0; } /*NO_TEST*/
1355
1356 /* Reach this point only if the page is valid */
1357 return 1;
1358}
1359#endif /* !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST) */
1360
drhfa86c412002-02-02 15:01:15 +00001361/*
danielk197713adf8a2004-06-03 16:08:41 +00001362** Parameter zMaster is the name of a master journal file. A single journal
1363** file that referred to the master journal file has just been rolled back.
1364** This routine checks if it is possible to delete the master journal file,
1365** and does so if it is.
drh726de592004-06-10 23:35:50 +00001366**
danielk197765839c62007-08-30 08:08:17 +00001367** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1368** available for use within this function.
1369**
1370**
drh726de592004-06-10 23:35:50 +00001371** The master journal file contains the names of all child journals.
1372** To tell if a master journal can be deleted, check to each of the
1373** children. If all children are either missing or do not refer to
1374** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001375*/
danielk1977b4b47412007-08-17 15:53:36 +00001376static int pager_delmaster(Pager *pPager, const char *zMaster){
1377 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001378 int rc;
1379 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001380 sqlite3_file *pMaster;
1381 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001382 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001383 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001384
1385 /* Open the master journal file exclusively in case some other process
1386 ** is running this routine also. Not that it makes too much difference.
1387 */
drhe5ae5732008-06-15 02:51:47 +00001388 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001389 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001390 if( !pMaster ){
1391 rc = SQLITE_NOMEM;
1392 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001393 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1394 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001395 }
danielk197713adf8a2004-06-03 16:08:41 +00001396 if( rc!=SQLITE_OK ) goto delmaster_out;
1397 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001398
1399 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001400 if( rc!=SQLITE_OK ) goto delmaster_out;
1401
1402 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001403 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001404 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001405 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001406
1407 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001408 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001409 */
drh4f21c4a2008-12-10 22:15:00 +00001410 zMasterJournal = (char *)sqlite3Malloc((int)nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001411 if( !zMasterJournal ){
1412 rc = SQLITE_NOMEM;
1413 goto delmaster_out;
1414 }
danielk197765839c62007-08-30 08:08:17 +00001415 zMasterPtr = &zMasterJournal[nMasterJournal];
drh4f21c4a2008-12-10 22:15:00 +00001416 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001417 if( rc!=SQLITE_OK ) goto delmaster_out;
1418
danielk19775865e3d2004-06-14 06:03:57 +00001419 zJournal = zMasterJournal;
1420 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001421 int exists;
1422 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1423 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001424 goto delmaster_out;
1425 }
danielk1977861f7452008-06-05 11:39:11 +00001426 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001427 /* One of the journals pointed to by the master journal exists.
1428 ** Open it and check if it points at the master journal. If
1429 ** so, return without deleting the master journal file.
1430 */
drh3b7b78b2004-11-24 01:16:43 +00001431 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001432 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1433 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001434 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001435 goto delmaster_out;
1436 }
danielk197713adf8a2004-06-03 16:08:41 +00001437
danielk197765839c62007-08-30 08:08:17 +00001438 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001439 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001440 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001441 goto delmaster_out;
1442 }
danielk197776572402004-06-25 02:38:54 +00001443
danielk197765839c62007-08-30 08:08:17 +00001444 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001445 if( c ){
danielk197776572402004-06-25 02:38:54 +00001446 /* We have a match. Do not delete the master journal file. */
1447 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001448 }
1449 }
drhea678832008-12-10 19:26:22 +00001450 zJournal += (sqlite3Strlen30(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001451 }
1452 }
1453
danielk1977fee2d252007-08-18 10:59:19 +00001454 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001455
1456delmaster_out:
1457 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001458 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001459 }
1460 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001461 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001462 }
danielk1977b4b47412007-08-17 15:53:36 +00001463 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001464 return rc;
1465}
1466
drha6abd042004-06-09 17:37:22 +00001467
drha6abd042004-06-09 17:37:22 +00001468/*
danielk1977f90b7262009-01-07 15:18:20 +00001469** If the main database file is open and an exclusive lock is held,
1470** truncate the main file of the given pager to the specified number
1471** of pages.
drh7fe3f7e2007-11-29 18:44:27 +00001472**
danielk1977f90b7262009-01-07 15:18:20 +00001473** It might might be the case that the file on disk is smaller than nPage.
drh7fe3f7e2007-11-29 18:44:27 +00001474** This can happen, for example, if we are in the middle of a transaction
1475** which has extended the file size and the new pages are still all held
1476** in cache, then an INSERT or UPDATE does a statement rollback. Some
1477** operating system implementations can get confused if you try to
1478** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001479** so detect this case and write a single zero byte to the end of the new
1480** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001481*/
danielk1977d92db532008-11-17 04:56:24 +00001482static int pager_truncate(Pager *pPager, Pgno nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001483 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001484 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001485 i64 currentSize, newSize;
1486 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1487 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001488 if( rc==SQLITE_OK && currentSize!=newSize ){
1489 if( currentSize>newSize ){
1490 rc = sqlite3OsTruncate(pPager->fd, newSize);
1491 }else{
1492 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1493 }
danielk19773460d192008-12-27 15:23:13 +00001494 if( rc==SQLITE_OK ){
1495 pPager->dbFileSize = nPage;
1496 }
drh7fe3f7e2007-11-29 18:44:27 +00001497 }
danielk1977e180dd92007-04-05 17:15:52 +00001498 }
danielk1977e180dd92007-04-05 17:15:52 +00001499 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001500}
1501
1502/*
drhc80f0582007-05-01 16:59:48 +00001503** Set the sectorSize for the given pager.
1504**
drh334c80d2008-03-28 17:41:13 +00001505** The sector size is at least as big as the sector size reported
1506** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001507*/
1508static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001509 assert(pPager->fd->pMethods||pPager->tempFile);
1510 if( !pPager->tempFile ){
1511 /* Sector size doesn't matter for temporary files. Also, the file
1512 ** may not have been opened yet, in whcih case the OsSectorSize()
1513 ** call will segfault.
1514 */
1515 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1516 }
drh334c80d2008-03-28 17:41:13 +00001517 if( pPager->sectorSize<512 ){
1518 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001519 }
1520}
1521
1522/*
drhed7c8552001-04-11 14:29:21 +00001523** Playback the journal and thus restore the database file to
1524** the state it was in before we started making changes.
1525**
drh34e79ce2004-02-08 06:05:46 +00001526** The journal file format is as follows:
1527**
drhae2b40c2004-06-09 19:03:54 +00001528** (1) 8 byte prefix. A copy of aJournalMagic[].
1529** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001530** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001531** number of page records from the journal size.
1532** (3) 4 byte big-endian integer which is the initial value for the
1533** sanity checksum.
1534** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001535** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001536** (5) 4 byte big-endian integer which is the sector size. The header
1537** is this many bytes in size.
1538** (6) 4 byte big-endian integer which is the page case.
1539** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001540** name. The value may be zero (indicate that there is no master
1541** journal.)
drh334c80d2008-03-28 17:41:13 +00001542** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001543** and might be shorter than the value read from (5). If the first byte
1544** of the name is \000 then there is no master journal. The master
1545** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001546** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001547** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001548** + pPager->pageSize bytes of data.
1549** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001550**
drh334c80d2008-03-28 17:41:13 +00001551** When we speak of the journal header, we mean the first 8 items above.
1552** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001553**
1554** Call the value from the second bullet "nRec". nRec is the number of
1555** valid page entries in the journal. In most cases, you can compute the
1556** value of nRec from the size of the journal file. But if a power
1557** failure occurred while the journal was being written, it could be the
1558** case that the size of the journal file had already been increased but
1559** the extra entries had not yet made it safely to disk. In such a case,
1560** the value of nRec computed from the file size would be too large. For
1561** that reason, we always use the nRec value in the header.
1562**
1563** If the nRec value is 0xffffffff it means that nRec should be computed
1564** from the file size. This value is used when the user selects the
1565** no-sync option for the journal. A power failure could lead to corruption
1566** in this case. But for things like temporary table (which will be
1567** deleted when the power is restored) we don't care.
1568**
drhd9b02572001-04-15 00:37:09 +00001569** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001570** journal file then all pages up to the first corrupted page are rolled
1571** back (or no pages if the journal header is corrupted). The journal file
1572** is then deleted and SQLITE_OK returned, just as if no corruption had
1573** been encountered.
1574**
1575** If an I/O or malloc() error occurs, the journal-file is not deleted
1576** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001577*/
danielk1977e277be02007-03-23 18:12:06 +00001578static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001579 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001580 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001581 u32 nRec; /* Number of Records in the journal */
shane0b8d2762008-07-22 05:18:00 +00001582 u32 u; /* Unsigned loop counter */
drhd9b02572001-04-15 00:37:09 +00001583 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001584 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001585 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001586 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001587
drhc3a64ba2001-11-22 00:01:27 +00001588 /* Figure out how many records are in the journal. Abort early if
1589 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001590 */
drh8cfbf082001-09-19 13:22:39 +00001591 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001592 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001593 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001594 goto end_playback;
1595 }
drh240c5792004-02-08 00:40:52 +00001596
danielk197776572402004-06-25 02:38:54 +00001597 /* Read the master journal name from the journal, if it is present.
1598 ** If a master journal file name is specified, but the file is not
1599 ** present on disk, then the journal is not hot and does not need to be
1600 ** played back.
drh240c5792004-02-08 00:40:52 +00001601 */
danielk197765839c62007-08-30 08:08:17 +00001602 zMaster = pPager->pTmpSpace;
1603 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001604 if( rc==SQLITE_OK && zMaster[0] ){
1605 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001606 }
danielk197765839c62007-08-30 08:08:17 +00001607 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001608 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001609 goto end_playback;
1610 }
1611 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001612
danielk197776572402004-06-25 02:38:54 +00001613 /* This loop terminates either when the readJournalHdr() call returns
1614 ** SQLITE_DONE or an IO error occurs. */
1615 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001616
danielk197776572402004-06-25 02:38:54 +00001617 /* Read the next journal header from the journal file. If there are
1618 ** not enough bytes left in the journal file for a complete header, or
1619 ** it is corrupted, then a process must of failed while writing it.
1620 ** This indicates nothing more needs to be rolled back.
1621 */
1622 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1623 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001624 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001625 rc = SQLITE_OK;
1626 }
danielk197776572402004-06-25 02:38:54 +00001627 goto end_playback;
1628 }
1629
1630 /* If nRec is 0xffffffff, then this journal was created by a process
1631 ** working in no-sync mode. This means that the rest of the journal
1632 ** file consists of pages, there are no more journal headers. Compute
1633 ** the value of nRec based on this assumption.
1634 */
1635 if( nRec==0xffffffff ){
1636 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
drh4f21c4a2008-12-10 22:15:00 +00001637 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
danielk197776572402004-06-25 02:38:54 +00001638 }
1639
danielk1977e277be02007-03-23 18:12:06 +00001640 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001641 ** process and if this is the final header in the journal, then it means
1642 ** that this part of the journal was being filled but has not yet been
1643 ** synced to disk. Compute the number of pages based on the remaining
1644 ** size of the file.
1645 **
1646 ** The third term of the test was added to fix ticket #2565.
drhd6e5e092009-01-07 02:03:35 +00001647 ** When rolling back a hot journal, nRec==0 always means that the next
1648 ** chunk of the journal contains zero pages to be rolled back. But
1649 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
1650 ** the journal, it means that the journal might contain additional
1651 ** pages that need to be rolled back and that the number of pages
drhee03d622009-01-07 15:33:45 +00001652 ** should be computed based on the journal file size.
danielk1977e277be02007-03-23 18:12:06 +00001653 */
drh4fd18c42009-01-08 15:24:01 +00001654 testcase( nRec==0 && !isHot
drhd6e5e092009-01-07 02:03:35 +00001655 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)!=pPager->journalOff
1656 && ((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager))>0
drh4fd18c42009-01-08 15:24:01 +00001657 && pagerNextJournalPageIsValid(pPager)
drhd6e5e092009-01-07 02:03:35 +00001658 );
drh8940f4e2007-08-11 00:26:20 +00001659 if( nRec==0 && !isHot &&
1660 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
drh4f21c4a2008-12-10 22:15:00 +00001661 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
danielk1977e277be02007-03-23 18:12:06 +00001662 }
1663
danielk197776572402004-06-25 02:38:54 +00001664 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001665 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001666 */
danielk1977e180dd92007-04-05 17:15:52 +00001667 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001668 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001669 if( rc!=SQLITE_OK ){
1670 goto end_playback;
1671 }
danielk1977f90b7262009-01-07 15:18:20 +00001672 pPager->dbSize = mxPg;
danielk197776572402004-06-25 02:38:54 +00001673 }
1674
danielk197776572402004-06-25 02:38:54 +00001675 /* Copy original pages out of the journal and back into the database file.
1676 */
shane0b8d2762008-07-22 05:18:00 +00001677 for(u=0; u<nRec; u++){
drhd6e5e092009-01-07 02:03:35 +00001678 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 0, 0);
danielk197776572402004-06-25 02:38:54 +00001679 if( rc!=SQLITE_OK ){
1680 if( rc==SQLITE_DONE ){
1681 rc = SQLITE_OK;
1682 pPager->journalOff = szJ;
1683 break;
1684 }else{
drha9625ea2008-09-03 00:08:29 +00001685 /* If we are unable to rollback, then the database is probably
1686 ** going to end up being corrupt. It is corrupt to us, anyhow.
1687 ** Perhaps the next process to come along can fix it....
1688 */
danielk197798c21902008-09-23 16:41:29 +00001689 rc = SQLITE_CORRUPT_BKPT;
danielk197776572402004-06-25 02:38:54 +00001690 goto end_playback;
1691 }
1692 }
drh968af522003-02-11 14:55:40 +00001693 }
drhed7c8552001-04-11 14:29:21 +00001694 }
drh580eeaf2006-02-24 03:09:37 +00001695 /*NOTREACHED*/
1696 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001697
1698end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001699 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001700 zMaster = pPager->pTmpSpace;
1701 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1702 }
1703 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001704 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001705 }
danielk1977c56774e2008-10-07 11:51:20 +00001706 if( rc==SQLITE_OK && zMaster[0] && res ){
danielk1977979f38e2007-03-27 16:19:51 +00001707 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001708 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001709 */
danielk197765839c62007-08-30 08:08:17 +00001710 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001711 }
danielk197776572402004-06-25 02:38:54 +00001712
1713 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001714 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001715 ** value. Reset it to the correct value for this process.
1716 */
drhc80f0582007-05-01 16:59:48 +00001717 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001718 return rc;
drhed7c8552001-04-11 14:29:21 +00001719}
1720
1721/*
drhd6e5e092009-01-07 02:03:35 +00001722** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
1723** the entire master journal file.
1724**
1725** The case pSavepoint==NULL occurs when a ROLLBACK TO command is invoked
1726** on a SAVEPOINT that is a transaction savepoint.
drhfa86c412002-02-02 15:01:15 +00001727*/
danielk1977fd7f0452008-12-17 17:30:26 +00001728static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
drhd6e5e092009-01-07 02:03:35 +00001729 i64 szJ; /* Effective size of the main journal */
danielk1977fd7f0452008-12-17 17:30:26 +00001730 i64 iHdrOff; /* End of first segment of main-journal records */
1731 Pgno ii; /* Loop counter */
danielk1977f2c31ad2009-01-06 13:40:08 +00001732 int rc = SQLITE_OK; /* Return code */
danielk1977fd7f0452008-12-17 17:30:26 +00001733 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
drhfa86c412002-02-02 15:01:15 +00001734
danielk1977fd7f0452008-12-17 17:30:26 +00001735 /* Allocate a bitvec to use to store the set of pages rolled back */
1736 if( pSavepoint ){
1737 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
1738 if( !pDone ){
1739 return SQLITE_NOMEM;
1740 }
danielk197776572402004-06-25 02:38:54 +00001741 }
danielk1977fd7f0452008-12-17 17:30:26 +00001742
1743 /* Truncate the database back to the size it was before the
1744 ** savepoint being reverted was opened.
drhfa86c412002-02-02 15:01:15 +00001745 */
drhd6e5e092009-01-07 02:03:35 +00001746 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
drh1aa2d8b2007-01-03 15:34:29 +00001747 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001748
drhd6e5e092009-01-07 02:03:35 +00001749 /* Use pPager->journalOff as the effective size of the main rollback
1750 ** journal. The actual file might be larger than this in
1751 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
1752 ** past pPager->journalOff is off-limits to us.
drhfa86c412002-02-02 15:01:15 +00001753 */
danielk1977fd7f0452008-12-17 17:30:26 +00001754 szJ = pPager->journalOff;
drhd6e5e092009-01-07 02:03:35 +00001755
1756 /* Begin by rolling back records from the main journal starting at
1757 ** PagerSavepoint.iOffset and continuing to the next journal header.
1758 ** There might be records in the main journal that have a page number
1759 ** greater than the current database size (pPager->dbSize) but those
1760 ** will be skipped automatically. Pages are added to pDone as they
1761 ** are played back.
1762 */
danielk1977fd7f0452008-12-17 17:30:26 +00001763 if( pSavepoint ){
1764 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
1765 pPager->journalOff = pSavepoint->iOffset;
1766 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
drhd6e5e092009-01-07 02:03:35 +00001767 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone);
danielk1977fd7f0452008-12-17 17:30:26 +00001768 assert( rc!=SQLITE_DONE );
1769 }
1770 }else{
1771 pPager->journalOff = 0;
drhfa86c412002-02-02 15:01:15 +00001772 }
drhd6e5e092009-01-07 02:03:35 +00001773
1774 /* Continue rolling back records out of the main journal starting at
1775 ** the first journal header seen and continuing until the effective end
1776 ** of the main journal file. Continue to skip out-of-range pages and
1777 ** continue adding pages rolled back to pDone.
1778 */
danielk1977fd7f0452008-12-17 17:30:26 +00001779 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
danielk1977c81806f2009-01-01 15:20:37 +00001780 u32 nJRec = 0; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001781 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001782 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
danielk1977fd7f0452008-12-17 17:30:26 +00001783 assert( rc!=SQLITE_DONE );
drhd6e5e092009-01-07 02:03:35 +00001784
1785 /*
1786 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
1787 ** test is related to ticket #2565. See the discussion in the
1788 ** pager_playback() function for additional information.
1789 */
drhee03d622009-01-07 15:33:45 +00001790 assert( !(nJRec==0
drhd6e5e092009-01-07 02:03:35 +00001791 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)!=pPager->journalOff
1792 && ((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager))>0
drhee03d622009-01-07 15:33:45 +00001793 && pagerNextJournalPageIsValid(pPager))
drhd6e5e092009-01-07 02:03:35 +00001794 );
1795 if( nJRec==0
1796 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
1797 ){
1798 nJRec = (szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager);
danielk197775edc162004-06-26 01:48:18 +00001799 }
danielk197712dd5492008-12-18 15:45:07 +00001800 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
drhd6e5e092009-01-07 02:03:35 +00001801 rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone);
danielk197776572402004-06-25 02:38:54 +00001802 assert( rc!=SQLITE_DONE );
danielk1977fd7f0452008-12-17 17:30:26 +00001803 }
1804 }
1805 assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
1806
drhd6e5e092009-01-07 02:03:35 +00001807 /* Finally, rollback pages from the sub-journal. Page that were
1808 ** previously rolled back out of the main journal (and are hence in pDone)
1809 ** will be skipped. Out-of-range pages are also skipped.
1810 */
danielk1977fd7f0452008-12-17 17:30:26 +00001811 if( pSavepoint ){
drhd6e5e092009-01-07 02:03:35 +00001812 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
drh49b9d332009-01-02 18:10:42 +00001813 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK&&ii<(u32)pPager->stmtNRec; ii++){
drhd6e5e092009-01-07 02:03:35 +00001814 assert( offset == ii*(4+pPager->pageSize) );
1815 rc = pager_playback_one_page(pPager, 0, &offset, 1, pDone);
danielk1977fd7f0452008-12-17 17:30:26 +00001816 assert( rc!=SQLITE_DONE );
danielk197776572402004-06-25 02:38:54 +00001817 }
drhfa86c412002-02-02 15:01:15 +00001818 }
danielk197776572402004-06-25 02:38:54 +00001819
danielk1977fd7f0452008-12-17 17:30:26 +00001820 sqlite3BitvecDestroy(pDone);
1821 if( rc==SQLITE_OK ){
danielk197775edc162004-06-26 01:48:18 +00001822 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001823 }
1824 return rc;
1825}
1826
1827/*
drhf57b14a2001-09-14 18:54:08 +00001828** Change the maximum number of in-memory pages that are allowed.
1829*/
danielk19773b8a05f2007-03-19 17:44:26 +00001830void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +00001831 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001832}
1833
1834/*
drh973b6e32003-02-12 14:09:42 +00001835** Adjust the robustness of the database to damage due to OS crashes
1836** or power failures by changing the number of syncs()s when writing
1837** the rollback journal. There are three levels:
1838**
drh054889e2005-11-30 03:20:31 +00001839** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001840** for temporary and transient files.
1841**
1842** NORMAL The journal is synced once before writes begin on the
1843** database. This is normally adequate protection, but
1844** it is theoretically possible, though very unlikely,
1845** that an inopertune power failure could leave the journal
1846** in a state which would cause damage to the database
1847** when it is rolled back.
1848**
1849** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001850** database (with some additional information - the nRec field
1851** of the journal header - being written in between the two
1852** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001853** single disk sector is atomic, then this mode provides
1854** assurance that the journal will not be corrupted to the
1855** point of causing damage to the database during rollback.
1856**
1857** Numeric values associated with these states are OFF==1, NORMAL=2,
1858** and FULL=3.
1859*/
danielk197793758c82005-01-21 08:13:14 +00001860#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh281b21d2008-08-22 12:57:08 +00001861void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
drh4f21c4a2008-12-10 22:15:00 +00001862 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
1863 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
drh281b21d2008-08-22 12:57:08 +00001864 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001865 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001866}
danielk197793758c82005-01-21 08:13:14 +00001867#endif
drh973b6e32003-02-12 14:09:42 +00001868
1869/*
drhaf6df112005-06-07 02:12:30 +00001870** The following global variable is incremented whenever the library
1871** attempts to open a temporary file. This information is used for
1872** testing and analysis only.
1873*/
drh0f7eb612006-08-08 13:51:43 +00001874#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001875int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001876#endif
drhaf6df112005-06-07 02:12:30 +00001877
1878/*
drh3f56e6e2007-03-15 01:16:47 +00001879** Open a temporary file.
1880**
1881** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001882** other error code if we fail. The OS will automatically delete the temporary
1883** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001884*/
danielk1977b4b47412007-08-17 15:53:36 +00001885static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00001886 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00001887 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00001888 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00001889){
drhfa86c412002-02-02 15:01:15 +00001890 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001891
drh0f7eb612006-08-08 13:51:43 +00001892#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001893 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001894#endif
danielk1977b4b47412007-08-17 15:53:36 +00001895
drh33f4e022007-09-03 15:19:34 +00001896 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1897 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00001898 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00001899 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00001900 return rc;
1901}
1902
danielk1977a858aa22008-08-22 16:22:17 +00001903static int pagerStress(void *,PgHdr *);
danielk19778c0a7912008-08-20 14:49:23 +00001904
drhfa86c412002-02-02 15:01:15 +00001905/*
drhed7c8552001-04-11 14:29:21 +00001906** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001907** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001908** the first call to sqlite3PagerGet() and is only held open until the
1909** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001910**
drh6446c4d2001-12-15 14:22:18 +00001911** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00001912** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00001913** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001914**
1915** If zFilename is ":memory:" then all information is held in cache.
1916** It is never written to disk. This can be used to implement an
1917** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001918*/
danielk19773b8a05f2007-03-19 17:44:26 +00001919int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001920 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001921 Pager **ppPager, /* Return the Pager structure here */
1922 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001923 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00001924 int flags, /* flags controlling this file */
1925 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00001926){
danielk1977b4b47412007-08-17 15:53:36 +00001927 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001928 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001929 int rc = SQLITE_OK;
1930 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001931 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001932 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001933 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001934 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1935 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977b3175382008-10-17 18:51:52 +00001936 int journalFileSize;
danielk19778c0a7912008-08-20 14:49:23 +00001937 int pcacheSize = sqlite3PcacheSize();
drhfacf0302008-06-17 15:12:00 +00001938 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
danielk197717b90b52008-06-06 11:11:25 +00001939 char *zPathname = 0;
1940 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001941
danielk1977b3175382008-10-17 18:51:52 +00001942 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
1943 journalFileSize = sqlite3JournalSize(pVfs);
1944 }else{
1945 journalFileSize = sqlite3MemJournalSize();
1946 }
1947
drh86f8c192007-08-22 00:39:19 +00001948 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001949 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001950
danielk197717b90b52008-06-06 11:11:25 +00001951 /* Compute and store the full pathname in an allocated buffer pointed
1952 ** to by zPathname, length nPathname. Or, if this is a temporary file,
1953 ** leave both nPathname and zPathname set to 0.
1954 */
drh1cc8c442007-08-24 16:08:29 +00001955 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00001956 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001957 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00001958 if( zPathname==0 ){
1959 return SQLITE_NOMEM;
1960 }
drh1cc8c442007-08-24 16:08:29 +00001961#ifndef SQLITE_OMIT_MEMORYDB
1962 if( strcmp(zFilename,":memory:")==0 ){
1963 memDb = 1;
1964 zPathname[0] = 0;
1965 }else
1966#endif
1967 {
danielk1977adfb9b02007-09-17 07:02:56 +00001968 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00001969 }
danielk197717b90b52008-06-06 11:11:25 +00001970 if( rc!=SQLITE_OK ){
1971 sqlite3_free(zPathname);
1972 return rc;
1973 }
drhea678832008-12-10 19:26:22 +00001974 nPathname = sqlite3Strlen30(zPathname);
drhae28c012007-08-24 16:29:23 +00001975 }
drh99b90c32008-03-17 13:50:58 +00001976
danielk1977b4b47412007-08-17 15:53:36 +00001977 /* Allocate memory for the pager structure */
1978 pPager = sqlite3MallocZero(
1979 sizeof(*pPager) + /* Pager structure */
danielk19778c0a7912008-08-20 14:49:23 +00001980 pcacheSize + /* PCache object */
danielk1977c7b60172007-08-22 11:22:03 +00001981 journalFileSize + /* The journal file structure */
danielk1977b3175382008-10-17 18:51:52 +00001982 pVfs->szOsFile + /* The main db file */
1983 journalFileSize * 2 + /* The two journal files */
danielk197717b90b52008-06-06 11:11:25 +00001984 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00001985 );
1986 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00001987 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00001988 return SQLITE_NOMEM;
1989 }
danielk19778c0a7912008-08-20 14:49:23 +00001990 pPager->pPCache = (PCache *)&pPager[1];
1991 pPtr = ((u8 *)&pPager[1]) + pcacheSize;
drh33f4e022007-09-03 15:19:34 +00001992 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00001993 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977fd7f0452008-12-17 17:30:26 +00001994 pPager->sjfd = (sqlite3_file*)&pPtr[pVfs->szOsFile];
danielk1977b3175382008-10-17 18:51:52 +00001995 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile+journalFileSize];
1996 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile+2*journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00001997 pPager->zDirectory = &pPager->zFilename[nPathname+1];
1998 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00001999 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00002000 if( zPathname ){
2001 memcpy(pPager->zFilename, zPathname, nPathname+1);
2002 sqlite3_free(zPathname);
2003 }
danielk1977b4b47412007-08-17 15:53:36 +00002004
drh153c62c2007-08-24 03:51:33 +00002005 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002006 */
drhae28c012007-08-24 16:29:23 +00002007 if( zFilename && zFilename[0] && !memDb ){
danielk1977d92db532008-11-17 04:56:24 +00002008 if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){
drh1cc8c442007-08-24 16:08:29 +00002009 rc = SQLITE_CANTOPEN;
2010 }else{
drh1cc8c442007-08-24 16:08:29 +00002011 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002012 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2013 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002014 readOnly = (fout&SQLITE_OPEN_READONLY);
2015
2016 /* If the file was successfully opened for read/write access,
2017 ** choose a default page size in case we have to create the
2018 ** database file. The default page size is the maximum of:
2019 **
2020 ** + SQLITE_DEFAULT_PAGE_SIZE,
2021 ** + The value returned by sqlite3OsSectorSize()
2022 ** + The largest page size that can be written atomically.
2023 */
2024 if( rc==SQLITE_OK && !readOnly ){
2025 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
drhfacf0302008-06-17 15:12:00 +00002026 if( szPageDflt<iSectorSize ){
2027 szPageDflt = iSectorSize;
drh1cc8c442007-08-24 16:08:29 +00002028 }
danielk19779663b8f2007-08-24 11:52:28 +00002029#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002030 {
2031 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2032 int ii;
2033 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2034 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2035 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
drhfacf0302008-06-17 15:12:00 +00002036 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2037 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002038 }
danielk1977b4b47412007-08-17 15:53:36 +00002039 }
drh1cc8c442007-08-24 16:08:29 +00002040#endif
drhfacf0302008-06-17 15:12:00 +00002041 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2042 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
drh1cc8c442007-08-24 16:08:29 +00002043 }
danielk19778def5ea2004-06-16 10:39:52 +00002044 }
drhac69b052004-05-12 13:30:07 +00002045 }
danielk1977b3175382008-10-17 18:51:52 +00002046 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00002047 /* If a temporary file is requested, it is not opened immediately.
2048 ** In this case we accept the default page size and delay actually
2049 ** opening the file until the first call to OsWrite().
danielk1977b3175382008-10-17 18:51:52 +00002050 **
2051 ** This branch is also run for an in-memory database. An in-memory
2052 ** database is the same as a temp-file that is never written out to
2053 ** disk and uses an in-memory rollback journal.
danielk19777a2b1ee2007-08-21 14:27:01 +00002054 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002055 tempFile = 1;
2056 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002057 }
danielk1977aef0bf62005-12-30 16:28:01 +00002058
danielk1977b4b47412007-08-17 15:53:36 +00002059 if( pPager && rc==SQLITE_OK ){
drhfacf0302008-06-17 15:12:00 +00002060 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00002061 }
danielk1977aef0bf62005-12-30 16:28:01 +00002062
drh153c62c2007-08-24 03:51:33 +00002063 /* If an error occured in either of the blocks above.
2064 ** Free the Pager structure and close the file.
2065 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002066 ** any Pager.errMask variables.
2067 */
danielk1977b4b47412007-08-17 15:53:36 +00002068 if( !pPager || !pPager->pTmpSpace ){
2069 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002070 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002071 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002072 }
danielk19778c0a7912008-08-20 14:49:23 +00002073 nExtra = FORCE_ALIGNMENT(nExtra);
danielk197771d5d2c2008-09-29 11:49:47 +00002074 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
drh41d30272008-08-20 21:47:45 +00002075 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
danielk1977aef0bf62005-12-30 16:28:01 +00002076
drh30d53702009-01-06 15:58:57 +00002077 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
drh153c62c2007-08-24 03:51:33 +00002078 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002079
danielk1977b4b47412007-08-17 15:53:36 +00002080 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002081 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
drhea678832008-12-10 19:26:22 +00002082 for(i=sqlite3Strlen30(pPager->zDirectory);
2083 i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002084 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002085
drh99b90c32008-03-17 13:50:58 +00002086 /* Fill in Pager.zJournal[] */
danielk197717b90b52008-06-06 11:11:25 +00002087 if( zPathname ){
2088 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2089 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
2090 }else{
2091 pPager->zJournal = 0;
2092 }
danielk1977b4b47412007-08-17 15:53:36 +00002093
drh3b59a5c2006-01-15 20:28:28 +00002094 /* pPager->journalOpen = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002095 pPager->useJournal = (u8)useJournal;
2096 pPager->noReadlock = (noReadlock && readOnly) ?1:0;
drh3b59a5c2006-01-15 20:28:28 +00002097 /* pPager->stmtOpen = 0; */
2098 /* pPager->stmtInUse = 0; */
2099 /* pPager->nRef = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002100 pPager->dbSizeValid = (u8)memDb;
drhfacf0302008-06-17 15:12:00 +00002101 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00002102 /* pPager->stmtSize = 0; */
2103 /* pPager->stmtJSize = 0; */
2104 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002105 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002106 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002107 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002108 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002109 /* pPager->errMask = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002110 pPager->tempFile = (u8)tempFile;
drh369339d2007-03-30 16:01:55 +00002111 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2112 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2113 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
drh4f21c4a2008-12-10 22:15:00 +00002114 pPager->exclusiveMode = (u8)tempFile;
2115 pPager->memDb = (u8)memDb;
2116 pPager->readOnly = (u8)readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002117 /* pPager->needSync = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002118 pPager->noSync = (pPager->tempFile || !useJournal) ?1:0;
2119 pPager->fullSync = pPager->noSync ?0:1;
danielk1977f036aef2007-08-20 05:36:51 +00002120 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002121 /* pPager->pFirst = 0; */
2122 /* pPager->pFirstSynced = 0; */
2123 /* pPager->pLast = 0; */
danielk19778c0a7912008-08-20 14:49:23 +00002124 pPager->nExtra = nExtra;
danielk1977b53e4962008-06-04 06:45:59 +00002125 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk1977b3175382008-10-17 18:51:52 +00002126 assert(pPager->fd->pMethods||tempFile);
2127 setSectorSize(pPager);
2128 if( memDb ){
2129 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
danielk1977b4721172007-03-19 05:54:48 +00002130 }
danielk19771ceedd32008-11-19 10:22:33 +00002131 /* pPager->xBusyHandler = 0; */
2132 /* pPager->pBusyHandlerArg = 0; */
drh3b59a5c2006-01-15 20:28:28 +00002133 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002134 *ppPager = pPager;
2135 return SQLITE_OK;
2136}
2137
2138/*
drh90f5ecb2004-07-22 01:19:35 +00002139** Set the busy handler function.
2140*/
danielk19771ceedd32008-11-19 10:22:33 +00002141void sqlite3PagerSetBusyhandler(
2142 Pager *pPager,
2143 int (*xBusyHandler)(void *),
2144 void *pBusyHandlerArg
2145){
2146 pPager->xBusyHandler = xBusyHandler;
2147 pPager->pBusyHandlerArg = pBusyHandlerArg;
drh90f5ecb2004-07-22 01:19:35 +00002148}
2149
2150/*
drha6abd042004-06-09 17:37:22 +00002151** Set the reinitializer for this pager. If not NULL, the reinitializer
2152** is called when the content of a page in cache is restored to its original
2153** value as a result of a rollback. The callback gives higher-level code
2154** an opportunity to restore the EXTRA section to agree with the restored
2155** page data.
2156*/
danielk1977eaa06f62008-09-18 17:34:44 +00002157void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
drha6abd042004-06-09 17:37:22 +00002158 pPager->xReiniter = xReinit;
2159}
2160
2161/*
danielk1977a1644fd2007-08-29 12:31:25 +00002162** Set the page size to *pPageSize. If the suggest new page size is
2163** inappropriate, then an alternative page size is set to that
2164** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002165*/
danielk1977a1644fd2007-08-29 12:31:25 +00002166int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
danielk19771357d9f2008-09-16 05:12:24 +00002167 int rc = pPager->errCode;
2168 if( rc==SQLITE_OK ){
2169 u16 pageSize = *pPageSize;
2170 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
2171 if( pageSize && pageSize!=pPager->pageSize
2172 && (pPager->memDb==0 || pPager->dbSize==0)
2173 && sqlite3PcacheRefCount(pPager->pPCache)==0
2174 ){
2175 char *pNew = (char *)sqlite3PageMalloc(pageSize);
2176 if( !pNew ){
2177 rc = SQLITE_NOMEM;
2178 }else{
2179 pager_reset(pPager);
2180 pPager->pageSize = pageSize;
2181 if( !pPager->memDb ) setSectorSize(pPager);
2182 sqlite3PageFree(pPager->pTmpSpace);
2183 pPager->pTmpSpace = pNew;
2184 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
2185 }
danielk1977a1644fd2007-08-29 12:31:25 +00002186 }
drh4f21c4a2008-12-10 22:15:00 +00002187 *pPageSize = (u16)pPager->pageSize;
drh1c7880e2005-05-20 20:01:55 +00002188 }
danielk1977a1644fd2007-08-29 12:31:25 +00002189 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002190}
2191
2192/*
drh26b79942007-11-28 16:19:56 +00002193** Return a pointer to the "temporary page" buffer held internally
2194** by the pager. This is a buffer that is big enough to hold the
2195** entire content of a database page. This buffer is used internally
2196** during rollback and will be overwritten whenever a rollback
2197** occurs. But other modules are free to use it too, as long as
2198** no rollbacks are happening.
2199*/
2200void *sqlite3PagerTempSpace(Pager *pPager){
2201 return pPager->pTmpSpace;
2202}
2203
2204/*
drhf8e632b2007-05-08 14:51:36 +00002205** Attempt to set the maximum database page count if mxPage is positive.
2206** Make no changes if mxPage is zero or negative. And never reduce the
2207** maximum page count below the current size of the database.
2208**
2209** Regardless of mxPage, return the current maximum page count.
2210*/
2211int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2212 if( mxPage>0 ){
2213 pPager->mxPgno = mxPage;
2214 }
danielk1977ad0132d2008-06-07 08:58:22 +00002215 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002216 return pPager->mxPgno;
2217}
2218
2219/*
drhc9ac5ca2005-11-04 22:03:30 +00002220** The following set of routines are used to disable the simulated
2221** I/O error mechanism. These routines are used to avoid simulated
2222** errors in places where we do not care about errors.
2223**
2224** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2225** and generate no code.
2226*/
2227#ifdef SQLITE_TEST
2228extern int sqlite3_io_error_pending;
2229extern int sqlite3_io_error_hit;
2230static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002231void disable_simulated_io_errors(void){
2232 saved_cnt = sqlite3_io_error_pending;
2233 sqlite3_io_error_pending = -1;
2234}
2235void enable_simulated_io_errors(void){
2236 sqlite3_io_error_pending = saved_cnt;
2237}
2238#else
drh152410f2005-11-05 15:11:22 +00002239# define disable_simulated_io_errors()
2240# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002241#endif
2242
2243/*
drh90f5ecb2004-07-22 01:19:35 +00002244** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002245** that pDest points to.
2246**
2247** No error checking is done. The rational for this is that this function
2248** may be called even if the file does not exist or contain a header. In
2249** these cases sqlite3OsRead() will return an error, to which the correct
2250** response is to zero the memory at pDest and continue. A real IO error
2251** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002252*/
danielk19773b8a05f2007-03-19 17:44:26 +00002253int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002254 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002255 memset(pDest, 0, N);
danielk1977b3175382008-10-17 18:51:52 +00002256 assert(pPager->fd->pMethods||pPager->tempFile);
danielk19777a2b1ee2007-08-21 14:27:01 +00002257 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002258 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002259 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002260 if( rc==SQLITE_IOERR_SHORT_READ ){
2261 rc = SQLITE_OK;
2262 }
drh90f5ecb2004-07-22 01:19:35 +00002263 }
drh551b7732006-11-06 21:20:25 +00002264 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002265}
2266
2267/*
drh5e00f6c2001-09-13 13:46:56 +00002268** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002269** pPager.
2270**
2271** If the PENDING_BYTE lies on the page directly after the end of the
2272** file, then consider this page part of the file too. For example, if
2273** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2274** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002275*/
danielk1977ad0132d2008-06-07 08:58:22 +00002276int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk19777a2b1ee2007-08-21 14:27:01 +00002277 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002278 int rc;
drhd9b02572001-04-15 00:37:09 +00002279 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002280 if( pPager->errCode ){
danielk19778c0a7912008-08-20 14:49:23 +00002281 rc = pPager->errCode;
danielk19778c0a7912008-08-20 14:49:23 +00002282 return rc;
drha7aea3d2007-03-15 12:51:16 +00002283 }
danielk1977d92db532008-11-17 04:56:24 +00002284 if( pPager->dbSizeValid ){
danielk197715f411d2005-09-16 10:18:45 +00002285 n = pPager->dbSize;
2286 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002287 assert(pPager->fd->pMethods||pPager->tempFile);
2288 if( (pPager->fd->pMethods)
2289 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002290 pager_error(pPager, rc);
danielk1977ad0132d2008-06-07 08:58:22 +00002291 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002292 }
2293 if( n>0 && n<pPager->pageSize ){
2294 n = 1;
2295 }else{
2296 n /= pPager->pageSize;
2297 }
2298 if( pPager->state!=PAGER_UNLOCK ){
danielk19773460d192008-12-27 15:23:13 +00002299 pPager->dbSize = (Pgno)n;
2300 pPager->dbFileSize = (Pgno)n;
danielk1977d92db532008-11-17 04:56:24 +00002301 pPager->dbSizeValid = 1;
danielk197715f411d2005-09-16 10:18:45 +00002302 }
drhed7c8552001-04-11 14:29:21 +00002303 }
danielk197715f411d2005-09-16 10:18:45 +00002304 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002305 n++;
2306 }
drhf8e632b2007-05-08 14:51:36 +00002307 if( n>pPager->mxPgno ){
drh4f21c4a2008-12-10 22:15:00 +00002308 pPager->mxPgno = (Pgno)n;
drhf8e632b2007-05-08 14:51:36 +00002309 }
danielk1977ad0132d2008-06-07 08:58:22 +00002310 if( pnPage ){
drh4f21c4a2008-12-10 22:15:00 +00002311 *pnPage = (int)n;
danielk1977ad0132d2008-06-07 08:58:22 +00002312 }
2313 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002314}
2315
2316/*
drhf7c57532003-04-25 13:22:51 +00002317** Forward declaration
2318*/
danielk197776572402004-06-25 02:38:54 +00002319static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002320
2321/*
danielk197717221812005-02-15 03:38:05 +00002322** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002323** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002324** false or until the lock succeeds.
2325**
2326** Return SQLITE_OK on success and an error code if we cannot obtain
2327** the lock.
2328*/
2329static int pager_wait_on_lock(Pager *pPager, int locktype){
2330 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002331
2332 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002333 assert( PAGER_SHARED==SHARED_LOCK );
2334 assert( PAGER_RESERVED==RESERVED_LOCK );
2335 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002336
2337 /* If the file is currently unlocked then the size must be unknown */
danielk1977d92db532008-11-17 04:56:24 +00002338 assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
drh1aa2d8b2007-01-03 15:34:29 +00002339
danielk197717221812005-02-15 03:38:05 +00002340 if( pPager->state>=locktype ){
2341 rc = SQLITE_OK;
2342 }else{
danielk197717221812005-02-15 03:38:05 +00002343 do {
drh054889e2005-11-30 03:20:31 +00002344 rc = sqlite3OsLock(pPager->fd, locktype);
danielk19771ceedd32008-11-19 10:22:33 +00002345 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
danielk197717221812005-02-15 03:38:05 +00002346 if( rc==SQLITE_OK ){
drh4f21c4a2008-12-10 22:15:00 +00002347 pPager->state = (u8)locktype;
drhb0603412007-02-28 04:47:26 +00002348 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002349 }
2350 }
2351 return rc;
2352}
2353
danielk19773460d192008-12-27 15:23:13 +00002354#ifndef SQLITE_OMIT_AUTOVACUUM
2355/*
danielk1977f90b7262009-01-07 15:18:20 +00002356** Truncate the in-memory database file image to nPage pages. This
2357** function does not actually modify the database file on disk. It
2358** just sets the internal state of the pager object so that the
2359** truncation will be done when the current transaction is committed.
danielk19773460d192008-12-27 15:23:13 +00002360*/
2361void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
2362 assert( pPager->dbSizeValid );
2363 assert( pPager->dbSize>=nPage );
2364 pPager->dbSize = nPage;
2365}
2366
2367/*
2368** Return the current size of the database file image in pages. This
2369** function differs from sqlite3PagerPagecount() in two ways:
2370**
2371** a) It may only be called when at least one reference to a database
2372** page is held. This guarantees that the database size is already
2373** known and a call to sqlite3OsFileSize() is not required.
2374**
2375** b) The return value is not adjusted for the locking page.
2376*/
2377Pgno sqlite3PagerImageSize(Pager *pPager){
2378 assert( pPager->dbSizeValid );
2379 return pPager->dbSize;
2380}
2381#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
2382
drhf7c57532003-04-25 13:22:51 +00002383/*
drhed7c8552001-04-11 14:29:21 +00002384** Shutdown the page cache. Free all memory and close all files.
2385**
2386** If a transaction was in progress when this routine is called, that
2387** transaction is rolled back. All outstanding pages are invalidated
2388** and their memory is freed. Any attempt to use a page associated
2389** with this page cache after this function returns will likely
2390** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002391**
2392** This function always succeeds. If a transaction is active an attempt
2393** is made to roll it back. If an error occurs during the rollback
2394** a hot journal may be left in the filesystem but no error is returned
2395** to the caller.
drhed7c8552001-04-11 14:29:21 +00002396*/
danielk19773b8a05f2007-03-19 17:44:26 +00002397int sqlite3PagerClose(Pager *pPager){
danielk197713f72992005-12-18 08:51:22 +00002398
drhbafda092007-01-03 23:36:22 +00002399 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002400 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002401 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002402 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002403 pager_reset(pPager);
danielk1977b3175382008-10-17 18:51:52 +00002404 if( !MEMDB ){
danielk1977f2c31ad2009-01-06 13:40:08 +00002405 /* Set Pager.journalHdr to -1 for the benefit of the pager_playback()
2406 ** call which may be made from within pagerUnlockAndRollback(). If it
2407 ** is not -1, then the unsynced portion of an open journal file may
2408 ** be played back into the database. If a power failure occurs while
2409 ** this is happening, the database may become corrupt.
2410 */
2411 pPager->journalHdr = -1;
danielk1977b3175382008-10-17 18:51:52 +00002412 pagerUnlockAndRollback(pPager);
2413 }
drhbafda092007-01-03 23:36:22 +00002414 enable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002415 sqlite3EndBenignMalloc();
drh30d53702009-01-06 15:58:57 +00002416 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
drhb0603412007-02-28 04:47:26 +00002417 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002418 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002419 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002420 }
drhf5e7bb52008-02-18 14:47:33 +00002421 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977a1fa00d2008-08-27 15:16:33 +00002422 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
danielk1977fd7f0452008-12-17 17:30:26 +00002423 releaseAllSavepoint(pPager);
danielk1977b4b47412007-08-17 15:53:36 +00002424 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002425 /* Temp files are automatically deleted by the OS
2426 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002427 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002428 ** }
2429 */
danielk1977aca790a2005-01-13 11:07:52 +00002430
drhfacf0302008-06-17 15:12:00 +00002431 sqlite3PageFree(pPager->pTmpSpace);
danielk19778c0a7912008-08-20 14:49:23 +00002432 sqlite3PcacheClose(pPager->pPCache);
drh17435752007-08-16 04:30:38 +00002433 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002434 return SQLITE_OK;
2435}
2436
drh87cc3b32007-05-08 21:45:27 +00002437#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002438/*
drh5e00f6c2001-09-13 13:46:56 +00002439** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002440*/
danielk19773b8a05f2007-03-19 17:44:26 +00002441Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002442 return p->pgno;
2443}
drh87cc3b32007-05-08 21:45:27 +00002444#endif
drhed7c8552001-04-11 14:29:21 +00002445
2446/*
drhdf0b3b02001-06-23 11:36:20 +00002447** Increment the reference count for a page. The input pointer is
2448** a reference to the page data.
2449*/
danielk19773b8a05f2007-03-19 17:44:26 +00002450int sqlite3PagerRef(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002451 sqlite3PcacheRef(pPg);
drh8c42ca92001-06-22 19:15:00 +00002452 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002453}
2454
2455/*
drh34e79ce2004-02-08 06:05:46 +00002456** Sync the journal. In other words, make sure all the pages that have
2457** been written to the journal have actually reached the surface of the
2458** disk. It is not safe to modify the original database file until after
2459** the journal has been synced. If the original database is modified before
2460** the journal is synced and a power failure occurs, the unsynced journal
2461** data would be lost and we would be unable to completely rollback the
2462** database changes. Database corruption would occur.
2463**
2464** This routine also updates the nRec field in the header of the journal.
2465** (See comments on the pager_playback() routine for additional information.)
2466** If the sync mode is FULL, two syncs will occur. First the whole journal
2467** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002468**
drh34e79ce2004-02-08 06:05:46 +00002469** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002470** after a power failure, so no sync occurs.
2471**
2472** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2473** the database is stored, then OsSync() is never called on the journal
2474** file. In this case all that is required is to update the nRec field in
2475** the journal header.
drhfa86c412002-02-02 15:01:15 +00002476**
drh34e79ce2004-02-08 06:05:46 +00002477** This routine clears the needSync field of every page current held in
2478** memory.
drh50e5dad2001-09-15 00:57:28 +00002479*/
danielk197776572402004-06-25 02:38:54 +00002480static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002481 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002482
2483 /* Sync the journal before modifying the main database
2484 ** (assuming there is a journal and it needs to be synced.)
2485 */
danielk197776572402004-06-25 02:38:54 +00002486 if( pPager->needSync ){
danielk1977b3175382008-10-17 18:51:52 +00002487 assert( !pPager->tempFile );
2488 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
danielk19774cd2cd52007-08-23 14:48:23 +00002489 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002490 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002491
danielk19774cd2cd52007-08-23 14:48:23 +00002492 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk1977112f7522009-01-08 17:50:45 +00002493 i64 jrnlOff = journalHdrOffset(pPager);
2494 u8 zMagic[8];
2495
2496 /* This block deals with an obscure problem. If the last connection
2497 ** that wrote to this database was operating in persistent-journal
2498 ** mode, then the journal file may at this point actually be larger
2499 ** than Pager.journalOff bytes. If the next thing in the journal
2500 ** file happens to be a journal-header (written as part of the
2501 ** previous connections transaction), and a crash or power-failure
2502 ** occurs after nRec is updated but before this connection writes
2503 ** anything else to the journal file (or commits/rolls back its
2504 ** transaction), then SQLite may become confused when doing the
2505 ** hot-journal rollback following recovery. It may roll back all
2506 ** of this connections data, then proceed to rolling back the old,
2507 ** out-of-date data that follows it. Database corruption.
2508 **
2509 ** To work around this, if the journal file does appear to contain
2510 ** a valid header following Pager.journalOff, then write a 0x00
2511 ** byte to the start of it to prevent it from being recognized.
2512 */
2513 rc = sqlite3OsRead(pPager->jfd, zMagic, 8, jrnlOff);
2514 if( rc==SQLITE_OK && 0==memcmp(zMagic, aJournalMagic, 8) ){
2515 static const u8 zerobyte = 0;
2516 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, jrnlOff);
2517 }
2518 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
2519 return rc;
2520 }
2521
danielk197776572402004-06-25 02:38:54 +00002522 /* Write the nRec value into the journal file header. If in
2523 ** full-synchronous mode, sync the journal first. This ensures that
2524 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002525 ** it as a candidate for rollback.
2526 **
2527 ** This is not required if the persistent media supports the
2528 ** SAFE_APPEND property. Because in this case it is not possible
2529 ** for garbage data to be appended to the file, the nRec field
2530 ** is populated with 0xFFFFFFFF when the journal header is written
2531 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002532 */
danielk19774cd2cd52007-08-23 14:48:23 +00002533 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh30d53702009-01-06 15:58:57 +00002534 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
drhb0603412007-02-28 04:47:26 +00002535 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002536 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002537 if( rc!=0 ) return rc;
2538 }
danielk197713adf8a2004-06-03 16:08:41 +00002539
danielk197762079062007-08-15 17:08:46 +00002540 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2541 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2542 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002543 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002544 }
danielk19774cd2cd52007-08-23 14:48:23 +00002545 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh30d53702009-01-06 15:58:57 +00002546 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
danielk19774cd2cd52007-08-23 14:48:23 +00002547 IOTRACE(("JSYNC %p\n", pPager))
2548 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2549 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2550 );
2551 if( rc!=0 ) return rc;
2552 }
drhdb48ee02003-01-16 13:42:43 +00002553 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002554 }
drh50e5dad2001-09-15 00:57:28 +00002555 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002556
2557 /* Erase the needSync flag from every page.
2558 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00002559 sqlite3PcacheClearSyncFlags(pPager->pPCache);
drh50e5dad2001-09-15 00:57:28 +00002560 }
drh03eb96a2002-11-10 23:32:56 +00002561
drh81a20f22001-10-12 17:30:04 +00002562 return rc;
drh50e5dad2001-09-15 00:57:28 +00002563}
2564
2565/*
drh2554f8b2003-01-22 01:26:44 +00002566** Given a list of pages (connected by the PgHdr.pDirty pointer) write
danielk1977a858aa22008-08-22 16:22:17 +00002567** every one of those pages out to the database file. No calls are made
2568** to the page-cache to mark the pages as clean. It is the responsibility
2569** of the caller to use PcacheCleanAll() or PcacheMakeClean() to mark
2570** the pages as clean.
drh2554f8b2003-01-22 01:26:44 +00002571*/
2572static int pager_write_pagelist(PgHdr *pList){
2573 Pager *pPager;
2574 int rc;
2575
2576 if( pList==0 ) return SQLITE_OK;
2577 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002578
2579 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2580 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002581 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002582 **
drha6abd042004-06-09 17:37:22 +00002583 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2584 ** through an intermediate state PENDING. A PENDING lock prevents new
2585 ** readers from attaching to the database but is unsufficient for us to
2586 ** write. The idea of a PENDING lock is to prevent new readers from
2587 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002588 **
drha6abd042004-06-09 17:37:22 +00002589 ** While the pager is in the RESERVED state, the original database file
2590 ** is unchanged and we can rollback without having to playback the
2591 ** journal into the original database file. Once we transition to
2592 ** EXCLUSIVE, it means the database file has been changed and any rollback
2593 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002594 */
drh684917c2004-10-05 02:41:42 +00002595 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002596 if( rc!=SQLITE_OK ){
2597 return rc;
2598 }
2599
drh2554f8b2003-01-22 01:26:44 +00002600 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002601
2602 /* If the file has not yet been opened, open it now. */
2603 if( !pPager->fd->pMethods ){
2604 assert(pPager->tempFile);
danielk197717b90b52008-06-06 11:11:25 +00002605 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002606 if( rc ) return rc;
2607 }
2608
danielk1977687566d2004-11-02 12:56:41 +00002609 /* If there are dirty pages in the page cache with page numbers greater
danielk1977f90b7262009-01-07 15:18:20 +00002610 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
danielk1977687566d2004-11-02 12:56:41 +00002611 ** make the file smaller (presumably by auto-vacuum code). Do not write
2612 ** any such pages to the file.
2613 */
danielk197733e32162008-08-23 18:53:08 +00002614 if( pList->pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
danielk197762079062007-08-15 17:08:46 +00002615 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002616 char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6);
danielk197712dd5492008-12-18 15:45:07 +00002617
drh30d53702009-01-06 15:58:57 +00002618 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
2619 PAGERID(pPager), pList->pgno, pager_pagehash(pList)));
drh538f5702007-04-13 02:14:30 +00002620 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002621 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002622 PAGER_INCR(sqlite3_pager_writedb_count);
2623 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002624 if( pList->pgno==1 ){
2625 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2626 }
danielk19773460d192008-12-27 15:23:13 +00002627 if( pList->pgno>pPager->dbFileSize ){
2628 pPager->dbFileSize = pList->pgno;
2629 }
danielk1977687566d2004-11-02 12:56:41 +00002630 }
2631#ifndef NDEBUG
2632 else{
drh30d53702009-01-06 15:58:57 +00002633 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno));
danielk1977687566d2004-11-02 12:56:41 +00002634 }
2635#endif
drh2554f8b2003-01-22 01:26:44 +00002636 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002637#ifdef SQLITE_CHECK_PAGES
2638 pList->pageHash = pager_pagehash(pList);
2639#endif
drh2554f8b2003-01-22 01:26:44 +00002640 pList = pList->pDirty;
2641 }
danielk19778c0a7912008-08-20 14:49:23 +00002642
drh2554f8b2003-01-22 01:26:44 +00002643 return SQLITE_OK;
2644}
2645
2646/*
danielk1977f2c31ad2009-01-06 13:40:08 +00002647** Add the page to the sub-journal. It is the callers responsibility to
2648** use subjRequiresPage() to check that it is really required before
2649** calling this function.
2650*/
2651static int subjournalPage(PgHdr *pPg){
2652 int rc;
2653 void *pData = pPg->pData;
2654 Pager *pPager = pPg->pPager;
2655 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
2656 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
2657
drh30d53702009-01-06 15:58:57 +00002658 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
danielk1977f2c31ad2009-01-06 13:40:08 +00002659
2660 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
2661 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
2662 if( rc==SQLITE_OK ){
2663 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
2664 }
2665 if( rc==SQLITE_OK ){
2666 pPager->stmtNRec++;
2667 assert( pPager->nSavepoint>0 );
2668 rc = addToSavepointBitvecs(pPager, pPg->pgno);
2669 }
2670 return rc;
2671}
2672
2673
2674/*
danielk19778c0a7912008-08-20 14:49:23 +00002675** This function is called by the pcache layer when it has reached some
2676** soft memory limit. The argument is a pointer to a purgeable Pager
2677** object. This function attempts to make a single dirty page that has no
2678** outstanding references (if one exists) clean so that it can be recycled
2679** by the pcache layer.
drh2554f8b2003-01-22 01:26:44 +00002680*/
danielk1977a858aa22008-08-22 16:22:17 +00002681static int pagerStress(void *p, PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002682 Pager *pPager = (Pager *)p;
danielk19778c0a7912008-08-20 14:49:23 +00002683 int rc = SQLITE_OK;
drh8f2e9a12008-02-26 14:46:04 +00002684
danielk19778c200142008-08-25 07:12:28 +00002685 if( pPager->doNotSync ){
2686 return SQLITE_OK;
2687 }
2688
danielk1977a858aa22008-08-22 16:22:17 +00002689 assert( pPg->flags&PGHDR_DIRTY );
2690 if( pPager->errCode==SQLITE_OK ){
2691 if( pPg->flags&PGHDR_NEED_SYNC ){
2692 rc = syncJournal(pPager);
2693 if( rc==SQLITE_OK && pPager->fullSync &&
danielk1977b3175382008-10-17 18:51:52 +00002694 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
danielk1977a858aa22008-08-22 16:22:17 +00002695 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
2696 ){
2697 pPager->nRec = 0;
2698 rc = writeJournalHdr(pPager);
danielk19778c0a7912008-08-20 14:49:23 +00002699 }
danielk19778c0a7912008-08-20 14:49:23 +00002700 }
danielk1977a858aa22008-08-22 16:22:17 +00002701 if( rc==SQLITE_OK ){
2702 pPg->pDirty = 0;
danielk1977f2c31ad2009-01-06 13:40:08 +00002703 if( pPg->pgno>pPager->dbSize && subjRequiresPage(pPg) ){
2704 rc = subjournalPage(pPg);
2705 }
2706 if( rc==SQLITE_OK ){
2707 rc = pager_write_pagelist(pPg);
2708 }
danielk1977a858aa22008-08-22 16:22:17 +00002709 }
2710 if( rc!=SQLITE_OK ){
2711 pager_error(pPager, rc);
2712 }
2713 }
2714
2715 if( rc==SQLITE_OK ){
drh30d53702009-01-06 15:58:57 +00002716 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
danielk1977a858aa22008-08-22 16:22:17 +00002717 sqlite3PcacheMakeClean(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00002718 }
2719 return rc;
drh2554f8b2003-01-22 01:26:44 +00002720}
2721
danielk19778c0a7912008-08-20 14:49:23 +00002722
drh2554f8b2003-01-22 01:26:44 +00002723/*
drh19db9352008-03-27 22:42:51 +00002724** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00002725** A hot journal is one that needs to be played back.
2726**
2727** If the current size of the database file is 0 but a journal file
2728** exists, that is probably an old journal left over from a prior
2729** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00002730**
2731** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00002732**
2733** This routine does not open the journal file to examine its
2734** content. Hence, the journal might contain the name of a master
2735** journal file that has been deleted, and hence not be hot. Or
2736** the header of the journal might be zeroed out. This routine
2737** does not discover these cases of a non-hot journal - if the
2738** journal file exists and is not empty this routine assumes it
2739** is hot. The pager_playback() routine will discover that the
2740** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00002741*/
danielk1977d300b8a2008-08-01 10:50:23 +00002742static int hasHotJournal(Pager *pPager, int *pExists){
danielk1977b4b47412007-08-17 15:53:36 +00002743 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977d300b8a2008-08-01 10:50:23 +00002744 int rc = SQLITE_OK;
drhea678832008-12-10 19:26:22 +00002745 int exists = 0;
2746 int locked = 0;
drh0a846f92008-08-25 17:23:29 +00002747 assert( pPager!=0 );
2748 assert( pPager->useJournal );
2749 assert( pPager->fd->pMethods );
danielk1977d300b8a2008-08-01 10:50:23 +00002750 *pExists = 0;
drh0a846f92008-08-25 17:23:29 +00002751 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
2752 if( rc==SQLITE_OK && exists ){
2753 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
2754 }
2755 if( rc==SQLITE_OK && exists && !locked ){
2756 int nPage;
2757 rc = sqlite3PagerPagecount(pPager, &nPage);
2758 if( rc==SQLITE_OK ){
2759 if( nPage==0 ){
2760 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2761 }else{
2762 *pExists = 1;
danielk1977861f7452008-06-05 11:39:11 +00002763 }
2764 }
drhbb5f18d2007-04-06 18:23:17 +00002765 }
danielk1977d300b8a2008-08-01 10:50:23 +00002766 return rc;
drh165ffe92005-03-15 17:09:30 +00002767}
2768
2769/*
danielk1977e180dd92007-04-05 17:15:52 +00002770** Read the content of page pPg out of the database file.
2771*/
2772static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2773 int rc;
danielk197762079062007-08-15 17:08:46 +00002774 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002775 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002776 assert(pPager->fd->pMethods||pPager->tempFile);
2777 if( !pPager->fd->pMethods ){
2778 return SQLITE_IOERR_SHORT_READ;
2779 }
danielk197762079062007-08-15 17:08:46 +00002780 offset = (pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002781 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002782 PAGER_INCR(sqlite3_pager_readdb_count);
2783 PAGER_INCR(pPager->nRead);
2784 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002785 if( pgno==1 ){
danielk19778c0a7912008-08-20 14:49:23 +00002786 memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24],
drh86a88112007-04-16 15:02:19 +00002787 sizeof(pPager->dbFileVers));
2788 }
danielk197730849522008-08-21 04:35:18 +00002789 CODEC1(pPager, pPg->pData, pPg->pgno, 3);
drh30d53702009-01-06 15:58:57 +00002790 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
2791 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
danielk1977e180dd92007-04-05 17:15:52 +00002792 return rc;
2793}
2794
2795
2796/*
danielk1977e277be02007-03-23 18:12:06 +00002797** This function is called to obtain the shared lock required before
2798** data may be read from the pager cache. If the shared lock has already
2799** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002800**
2801** Immediately after obtaining the shared lock (if required), this function
2802** checks for a hot-journal file. If one is found, an emergency rollback
2803** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002804*/
2805static int pagerSharedLock(Pager *pPager){
2806 int rc = SQLITE_OK;
danielk1977d300b8a2008-08-01 10:50:23 +00002807 int isErrorReset = 0;
danielk1977e277be02007-03-23 18:12:06 +00002808
danielk1977ae72d982007-10-03 08:46:44 +00002809 /* If this database is opened for exclusive access, has no outstanding
2810 ** page references and is in an error-state, now is the chance to clear
2811 ** the error. Discard the contents of the pager-cache and treat any
2812 ** open journal file as a hot-journal.
2813 */
danielk19778c0a7912008-08-20 14:49:23 +00002814 if( !MEMDB && pPager->exclusiveMode
2815 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
2816 ){
danielk1977ae72d982007-10-03 08:46:44 +00002817 if( pPager->journalOpen ){
danielk1977d300b8a2008-08-01 10:50:23 +00002818 isErrorReset = 1;
danielk1977ae72d982007-10-03 08:46:44 +00002819 }
danielk1977ae72d982007-10-03 08:46:44 +00002820 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00002821 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00002822 }
2823
2824 /* If the pager is still in an error state, do not proceed. The error
2825 ** state will be cleared at some point in the future when all page
2826 ** references are dropped and the cache can be discarded.
2827 */
2828 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
2829 return pPager->errCode;
2830 }
2831
danielk1977d300b8a2008-08-01 10:50:23 +00002832 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
danielk1977b4b47412007-08-17 15:53:36 +00002833 sqlite3_vfs *pVfs = pPager->pVfs;
drh4f21c4a2008-12-10 22:15:00 +00002834 int isHotJournal = 0;
shane049fc212008-10-22 16:26:47 +00002835 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00002836 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
2837 if( !pPager->noReadlock ){
2838 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2839 if( rc!=SQLITE_OK ){
2840 assert( pPager->state==PAGER_UNLOCK );
2841 return pager_error(pPager, rc);
danielk1977e277be02007-03-23 18:12:06 +00002842 }
danielk1977b3175382008-10-17 18:51:52 +00002843 assert( pPager->state>=SHARED_LOCK );
2844 }
2845
2846 /* If a journal file exists, and there is no RESERVED lock on the
2847 ** database file, then it either needs to be played back or deleted.
2848 */
2849 if( !isErrorReset ){
2850 rc = hasHotJournal(pPager, &isHotJournal);
2851 if( rc!=SQLITE_OK ){
2852 goto failed;
2853 }
2854 }
2855 if( isErrorReset || isHotJournal ){
2856 /* Get an EXCLUSIVE lock on the database file. At this point it is
2857 ** important that a RESERVED lock is not obtained on the way to the
2858 ** EXCLUSIVE lock. If it were, another process might open the
2859 ** database file, detect the RESERVED lock, and conclude that the
2860 ** database is safe to read while this process is still rolling it
2861 ** back.
2862 **
2863 ** Because the intermediate RESERVED lock is not requested, the
2864 ** second process will get to this point in the code and fail to
2865 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00002866 */
danielk1977b3175382008-10-17 18:51:52 +00002867 if( pPager->state<EXCLUSIVE_LOCK ){
2868 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
danielk1977e277be02007-03-23 18:12:06 +00002869 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002870 rc = pager_error(pPager, rc);
2871 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002872 }
danielk1977b3175382008-10-17 18:51:52 +00002873 pPager->state = PAGER_EXCLUSIVE;
2874 }
2875
2876 /* Open the journal for read/write access. This is because in
2877 ** exclusive-access mode the file descriptor will be kept open and
2878 ** possibly used for a transaction later on. On some systems, the
2879 ** OsTruncate() call used in exclusive-access mode also requires
2880 ** a read/write file handle.
2881 */
2882 if( !isErrorReset && pPager->journalOpen==0 ){
2883 int res;
2884 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
2885 if( rc==SQLITE_OK ){
2886 if( res ){
2887 int fout = 0;
2888 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
2889 assert( !pPager->tempFile );
2890 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
2891 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk1977281d8bd2008-10-29 07:01:56 +00002892 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
2893 rc = SQLITE_CANTOPEN;
danielk1977b3175382008-10-17 18:51:52 +00002894 sqlite3OsClose(pPager->jfd);
2895 }
2896 }else{
2897 /* If the journal does not exist, that means some other process
2898 ** has already rolled it back */
2899 rc = SQLITE_BUSY;
2900 }
2901 }
2902 }
2903 if( rc!=SQLITE_OK ){
danielk1977b3175382008-10-17 18:51:52 +00002904 goto failed;
2905 }
2906 pPager->journalOpen = 1;
2907 pPager->journalStarted = 0;
2908 pPager->journalOff = 0;
2909 pPager->setMaster = 0;
2910 pPager->journalHdr = 0;
2911
2912 /* Playback and delete the journal. Drop the database write
danielk1977112f7522009-01-08 17:50:45 +00002913 ** lock and reacquire the read lock. Purge the cache before
2914 ** playing back the hot-journal so that we don't end up with
danielk1977b3175382008-10-17 18:51:52 +00002915 */
danielk1977112f7522009-01-08 17:50:45 +00002916 sqlite3PcacheClear(pPager->pPCache);
danielk1977b3175382008-10-17 18:51:52 +00002917 rc = pager_playback(pPager, 1);
2918 if( rc!=SQLITE_OK ){
2919 rc = pager_error(pPager, rc);
2920 goto failed;
2921 }
2922 assert(pPager->state==PAGER_SHARED ||
2923 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
2924 );
2925 }
2926
2927 if( sqlite3PcachePagecount(pPager->pPCache)>0 ){
2928 /* The shared-lock has just been acquired on the database file
2929 ** and there are already pages in the cache (from a previous
2930 ** read or write transaction). Check to see if the database
2931 ** has been modified. If the database has changed, flush the
2932 ** cache.
2933 **
2934 ** Database changes is detected by looking at 15 bytes beginning
2935 ** at offset 24 into the file. The first 4 of these 16 bytes are
2936 ** a 32-bit counter that is incremented with each change. The
2937 ** other bytes change randomly with each file change when
2938 ** a codec is in use.
2939 **
2940 ** There is a vanishingly small chance that a change will not be
2941 ** detected. The chance of an undetected change is so small that
2942 ** it can be neglected.
2943 */
2944 char dbFileVers[sizeof(pPager->dbFileVers)];
2945 sqlite3PagerPagecount(pPager, 0);
2946
2947 if( pPager->errCode ){
2948 rc = pPager->errCode;
2949 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002950 }
2951
danielk1977d92db532008-11-17 04:56:24 +00002952 assert( pPager->dbSizeValid );
danielk1977b3175382008-10-17 18:51:52 +00002953 if( pPager->dbSize>0 ){
2954 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
2955 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
2956 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002957 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002958 }
danielk1977b3175382008-10-17 18:51:52 +00002959 }else{
2960 memset(dbFileVers, 0, sizeof(dbFileVers));
2961 }
danielk1977e277be02007-03-23 18:12:06 +00002962
danielk1977b3175382008-10-17 18:51:52 +00002963 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
2964 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002965 }
2966 }
danielk1977c5859712007-03-26 12:26:27 +00002967 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
2968 if( pPager->state==PAGER_UNLOCK ){
2969 pPager->state = PAGER_SHARED;
2970 }
danielk1977e277be02007-03-23 18:12:06 +00002971 }
2972
danielk197752b472a2008-05-05 16:23:55 +00002973 failed:
2974 if( rc!=SQLITE_OK ){
2975 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
2976 pager_unlock(pPager);
2977 }
danielk1977e277be02007-03-23 18:12:06 +00002978 return rc;
2979}
2980
2981/*
drhd33d5a82007-04-26 12:11:28 +00002982** Make sure we have the content for a page. If the page was
2983** previously acquired with noContent==1, then the content was
2984** just initialized to zeros instead of being read from disk.
2985** But now we need the real data off of disk. So make sure we
2986** have it. Read it in if we do not have it already.
2987*/
2988static int pager_get_content(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002989 if( pPg->flags&PGHDR_NEED_READ ){
drhd33d5a82007-04-26 12:11:28 +00002990 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
2991 if( rc==SQLITE_OK ){
danielk19778c0a7912008-08-20 14:49:23 +00002992 pPg->flags &= ~PGHDR_NEED_READ;
drhd33d5a82007-04-26 12:11:28 +00002993 }else{
2994 return rc;
2995 }
2996 }
2997 return SQLITE_OK;
2998}
2999
3000/*
danielk19778c0a7912008-08-20 14:49:23 +00003001** If the reference count has reached zero, and the pager is not in the
3002** middle of a write transaction or opened in exclusive mode, unlock it.
3003*/
3004static void pagerUnlockIfUnused(Pager *pPager){
3005 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
3006 && (!pPager->exclusiveMode || pPager->journalOff>0)
3007 ){
3008 pagerUnlockAndRollback(pPager);
3009 }
3010}
3011
3012/*
3013** Drop a page from the cache using sqlite3PcacheDrop().
3014**
3015** If this means there are now no pages with references to them, a rollback
3016** occurs and the lock on the database is removed.
3017*/
3018static void pagerDropPage(DbPage *pPg){
3019 Pager *pPager = pPg->pPager;
3020 sqlite3PcacheDrop(pPg);
3021 pagerUnlockIfUnused(pPager);
3022}
3023
3024/*
drhd9b02572001-04-15 00:37:09 +00003025** Acquire a page.
3026**
drh58a11682001-11-10 13:51:08 +00003027** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003028** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003029**
drhd33d5a82007-04-26 12:11:28 +00003030** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003031** file is smaller than the requested page, then no actual disk
3032** read occurs and the memory image of the page is initialized to
3033** all zeros. The extra data appended to a page is always initialized
3034** to zeros the first time a page is loaded into memory.
3035**
drhd9b02572001-04-15 00:37:09 +00003036** The acquisition might fail for several reasons. In all cases,
3037** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003038**
drhd33d5a82007-04-26 12:11:28 +00003039** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003040** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003041** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003042** just returns 0. This routine acquires a read-lock the first time it
3043** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003044** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003045** or journal files.
drh0787db62007-03-04 13:15:27 +00003046**
drh538f5702007-04-13 02:14:30 +00003047** If noContent is false, the page contents are actually read from disk.
3048** If noContent is true, it means that we do not care about the contents
3049** of the page at this time, so do not do a disk read. Just fill in the
3050** page content with zeros. But mark the fact that we have not read the
3051** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003052** sqlite3PagerWrite() is called on this page or if this routine is
3053** called again with noContent==0, that means that the content is needed
3054** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003055*/
danielk197765e0ff32008-09-19 09:14:43 +00003056int sqlite3PagerAcquire(
drh538f5702007-04-13 02:14:30 +00003057 Pager *pPager, /* The pager open on the database file */
3058 Pgno pgno, /* Page number to fetch */
3059 DbPage **ppPage, /* Write a pointer to the page here */
3060 int noContent /* Do not bother reading content from disk if true */
3061){
danielk19778c0a7912008-08-20 14:49:23 +00003062 PgHdr *pPg = 0;
drh165ffe92005-03-15 17:09:30 +00003063 int rc;
drhed7c8552001-04-11 14:29:21 +00003064
danielk19778c0a7912008-08-20 14:49:23 +00003065 assert( pPager->state==PAGER_UNLOCK
3066 || sqlite3PcacheRefCount(pPager->pPCache)>0
danielk1977b3175382008-10-17 18:51:52 +00003067 || pgno==1
danielk19778c0a7912008-08-20 14:49:23 +00003068 );
danielk1977e277be02007-03-23 18:12:06 +00003069
danielk197726836652005-01-17 01:33:13 +00003070 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3071 ** number greater than this, or zero, is requested.
3072 */
drh267cb322005-09-16 17:16:52 +00003073 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003074 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003075 }
3076
drhd9b02572001-04-15 00:37:09 +00003077 /* Make sure we have not hit any critical errors.
3078 */
drh836faa42003-01-11 13:30:57 +00003079 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003080 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003081
danielk197713adf8a2004-06-03 16:08:41 +00003082 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003083 ** on the database file. pagerSharedLock() is a no-op if
3084 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003085 */
danielk1977e277be02007-03-23 18:12:06 +00003086 rc = pagerSharedLock(pPager);
3087 if( rc!=SQLITE_OK ){
3088 return rc;
drhed7c8552001-04-11 14:29:21 +00003089 }
danielk1977e277be02007-03-23 18:12:06 +00003090 assert( pPager->state!=PAGER_UNLOCK );
3091
danielk19778c0a7912008-08-20 14:49:23 +00003092 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
3093 if( rc!=SQLITE_OK ){
3094 return rc;
3095 }
danielk19778c0a7912008-08-20 14:49:23 +00003096 if( pPg->pPager==0 ){
3097 /* The pager cache has created a new page. Its content needs to
3098 ** be initialized.
3099 */
danielk1977979f38e2007-03-27 16:19:51 +00003100 int nMax;
drh538f5702007-04-13 02:14:30 +00003101 PAGER_INCR(pPager->nMiss);
danielk19778c0a7912008-08-20 14:49:23 +00003102 pPg->pPager = pPager;
danielk19778c0a7912008-08-20 14:49:23 +00003103 memset(pPg->pExtra, 0, pPager->nExtra);
danielk197724168722007-04-02 05:07:47 +00003104
danielk1977ad0132d2008-06-07 08:58:22 +00003105 rc = sqlite3PagerPagecount(pPager, &nMax);
3106 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00003107 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003108 return rc;
3109 }
danielk197775bab7d2006-01-23 13:09:45 +00003110
danielk1977a1fa00d2008-08-27 15:16:33 +00003111 if( nMax<(int)pgno || MEMDB || noContent ){
drhf8e632b2007-05-08 14:51:36 +00003112 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003113 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003114 return SQLITE_FULL;
3115 }
danielk19778c0a7912008-08-20 14:49:23 +00003116 memset(pPg->pData, 0, pPager->pageSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00003117 if( noContent ){
danielk19778c0a7912008-08-20 14:49:23 +00003118 pPg->flags |= PGHDR_NEED_READ;
3119 }
drh538f5702007-04-13 02:14:30 +00003120 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003121 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003122 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003123 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
danielk19778c0a7912008-08-20 14:49:23 +00003124 /* sqlite3PagerUnref(pPg); */
3125 pagerDropPage(pPg);
drh551b7732006-11-06 21:20:25 +00003126 return rc;
drh81a20f22001-10-12 17:30:04 +00003127 }
drh306dc212001-05-21 13:45:10 +00003128 }
danielk19773c407372005-02-15 02:54:14 +00003129#ifdef SQLITE_CHECK_PAGES
3130 pPg->pageHash = pager_pagehash(pPg);
3131#endif
drhed7c8552001-04-11 14:29:21 +00003132 }else{
drhd9b02572001-04-15 00:37:09 +00003133 /* The requested page is in the page cache. */
danielk19778c0a7912008-08-20 14:49:23 +00003134 assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003135 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003136 if( !noContent ){
3137 rc = pager_get_content(pPg);
3138 if( rc ){
danielk19778c0a7912008-08-20 14:49:23 +00003139 sqlite3PagerUnref(pPg);
drhd33d5a82007-04-26 12:11:28 +00003140 return rc;
3141 }
3142 }
drhed7c8552001-04-11 14:29:21 +00003143 }
danielk19778c0a7912008-08-20 14:49:23 +00003144
danielk19773b8a05f2007-03-19 17:44:26 +00003145 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003146 return SQLITE_OK;
3147}
danielk19778c0a7912008-08-20 14:49:23 +00003148
drhed7c8552001-04-11 14:29:21 +00003149/*
drh7e3b0a02001-04-28 16:52:40 +00003150** Acquire a page if it is already in the in-memory cache. Do
3151** not read the page from disk. Return a pointer to the page,
3152** or 0 if the page is not in cache.
3153**
danielk19773b8a05f2007-03-19 17:44:26 +00003154** See also sqlite3PagerGet(). The difference between this routine
3155** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003156** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003157** returns NULL if the page is not in cache or if a disk I/O error
3158** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003159*/
danielk19773b8a05f2007-03-19 17:44:26 +00003160DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003161 PgHdr *pPg = 0;
drh836faa42003-01-11 13:30:57 +00003162 assert( pPager!=0 );
3163 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003164
danielk19778c0a7912008-08-20 14:49:23 +00003165 if( (pPager->state!=PAGER_UNLOCK)
3166 && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL)
3167 ){
3168 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
danielk1977e277be02007-03-23 18:12:06 +00003169 }
danielk19778c0a7912008-08-20 14:49:23 +00003170
danielk19773b8a05f2007-03-19 17:44:26 +00003171 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003172}
3173
3174/*
drhed7c8552001-04-11 14:29:21 +00003175** Release a page.
3176**
3177** If the number of references to the page drop to zero, then the
3178** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003179** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003180** removed.
3181*/
danielk19773b8a05f2007-03-19 17:44:26 +00003182int sqlite3PagerUnref(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003183 if( pPg ){
3184 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00003185 sqlite3PcacheRelease(pPg);
3186 pagerUnlockIfUnused(pPager);
drhed7c8552001-04-11 14:29:21 +00003187 }
drhd9b02572001-04-15 00:37:09 +00003188 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003189}
3190
danielk19779153d852009-01-07 10:52:56 +00003191/*
3192** If the main journal file has already been opened, ensure that the
3193** sub-journal file is open too. If the main journal is not open,
3194** this function is a no-op.
3195**
3196** SQLITE_OK is returned if everything goes according to plan. An
3197** SQLITE_IOERR_XXX error code is returned if the call to
3198** sqlite3OsOpen() fails.
3199*/
danielk1977fd7f0452008-12-17 17:30:26 +00003200static int openSubJournal(Pager *pPager){
3201 int rc = SQLITE_OK;
3202 if( pPager->journalOpen && !pPager->sjfd->pMethods ){
3203 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3204 sqlite3MemJournalOpen(pPager->sjfd);
3205 }else{
3206 rc = sqlite3PagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
3207 }
3208 }
3209 return rc;
3210}
3211
drhed7c8552001-04-11 14:29:21 +00003212/*
drha6abd042004-06-09 17:37:22 +00003213** Create a journal file for pPager. There should already be a RESERVED
3214** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003215**
3216** Return SQLITE_OK if everything. Return an error code and release the
3217** write lock if anything goes wrong.
3218*/
3219static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003220 sqlite3_vfs *pVfs = pPager->pVfs;
3221 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3222
drhda47d772002-12-02 04:25:19 +00003223 int rc;
drha6abd042004-06-09 17:37:22 +00003224 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003225 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003226 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003227 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003228 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00003229 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003230 rc = SQLITE_NOMEM;
3231 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003232 }
danielk1977b4b47412007-08-17 15:53:36 +00003233
drhfdc40e92008-04-17 20:59:37 +00003234 if( pPager->journalOpen==0 ){
3235 if( pPager->tempFile ){
3236 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3237 }else{
3238 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003239 }
danielk1977b3175382008-10-17 18:51:52 +00003240 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3241 sqlite3MemJournalOpen(pPager->jfd);
3242 rc = SQLITE_OK;
3243 }else{
drhfdc40e92008-04-17 20:59:37 +00003244#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977b3175382008-10-17 18:51:52 +00003245 rc = sqlite3JournalOpen(
3246 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3247 );
drhfdc40e92008-04-17 20:59:37 +00003248#else
danielk1977b3175382008-10-17 18:51:52 +00003249 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
drhfdc40e92008-04-17 20:59:37 +00003250#endif
danielk1977b3175382008-10-17 18:51:52 +00003251 }
drhfdc40e92008-04-17 20:59:37 +00003252 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3253 pPager->journalOff = 0;
3254 pPager->setMaster = 0;
3255 pPager->journalHdr = 0;
3256 if( rc!=SQLITE_OK ){
3257 if( rc==SQLITE_NOMEM ){
3258 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3259 }
3260 goto failed_to_open_journal;
3261 }
drhda47d772002-12-02 04:25:19 +00003262 }
3263 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003264 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003265 pPager->needSync = 0;
drh968af522003-02-11 14:55:40 +00003266 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003267 if( pPager->errCode ){
3268 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003269 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003270 }
danielk19773460d192008-12-27 15:23:13 +00003271 pPager->dbOrigSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003272
danielk197776572402004-06-25 02:38:54 +00003273 rc = writeJournalHdr(pPager);
3274
danielk1977fd7f0452008-12-17 17:30:26 +00003275 if( pPager->nSavepoint && rc==SQLITE_OK ){
3276 rc = openSubJournal(pPager);
drhda47d772002-12-02 04:25:19 +00003277 }
danielk1977ae72d982007-10-03 08:46:44 +00003278 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003279 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003280 if( rc==SQLITE_OK ){
3281 rc = SQLITE_FULL;
3282 }
3283 }
drh9c105bb2004-10-02 20:38:28 +00003284 return rc;
3285
3286failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003287 sqlite3BitvecDestroy(pPager->pInJournal);
3288 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003289 return rc;
drhda47d772002-12-02 04:25:19 +00003290}
3291
3292/*
drh4b845d72002-03-05 12:41:19 +00003293** Acquire a write-lock on the database. The lock is removed when
3294** the any of the following happen:
3295**
drh80e35f42007-03-30 14:06:34 +00003296** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003297** * sqlite3PagerRollback() is called.
3298** * sqlite3PagerClose() is called.
3299** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003300**
danielk197713adf8a2004-06-03 16:08:41 +00003301** The first parameter to this routine is a pointer to any open page of the
3302** database file. Nothing changes about the page - it is used merely to
3303** acquire a pointer to the Pager structure and as proof that there is
3304** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003305**
danielk197713adf8a2004-06-03 16:08:41 +00003306** The second parameter indicates how much space in bytes to reserve for a
3307** master journal file-name at the start of the journal when it is created.
3308**
3309** A journal file is opened if this is not a temporary file. For temporary
3310** files, the opening of the journal file is deferred until there is an
3311** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003312**
drha6abd042004-06-09 17:37:22 +00003313** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003314**
3315** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3316** immediately instead of waiting until we try to flush the cache. The
3317** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003318*/
danielk19773b8a05f2007-03-19 17:44:26 +00003319int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003320 Pager *pPager = pPg->pPager;
3321 int rc = SQLITE_OK;
3322 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003323 assert( pPager->state!=PAGER_UNLOCK );
3324 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003325 assert( pPager->pInJournal==0 );
danielk1977b3175382008-10-17 18:51:52 +00003326 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00003327 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
3328 if( rc==SQLITE_OK ){
3329 pPager->state = PAGER_RESERVED;
3330 if( exFlag ){
3331 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh684917c2004-10-05 02:41:42 +00003332 }
danielk1977b3175382008-10-17 18:51:52 +00003333 }
3334 if( rc!=SQLITE_OK ){
3335 return rc;
3336 }
3337 pPager->dirtyCache = 0;
drh30d53702009-01-06 15:58:57 +00003338 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00003339 if( pPager->useJournal && !pPager->tempFile
3340 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
3341 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00003342 }
danielk1977334cdb62007-03-26 08:05:12 +00003343 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00003344 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00003345 ** time a (read or write) transaction was successfully concluded
3346 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00003347 ** kept open and either was truncated to 0 bytes or its header was
3348 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00003349 */
3350 assert( pPager->nRec==0 );
danielk19773460d192008-12-27 15:23:13 +00003351 assert( pPager->dbOrigSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003352 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003353 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003354 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhf5e7bb52008-02-18 14:47:33 +00003355 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003356 rc = SQLITE_NOMEM;
3357 }else{
danielk19773460d192008-12-27 15:23:13 +00003358 pPager->dbOrigSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003359 rc = writeJournalHdr(pPager);
3360 }
drh4b845d72002-03-05 12:41:19 +00003361 }
danielk1977334cdb62007-03-26 08:05:12 +00003362 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003363 return rc;
3364}
3365
3366/*
drhed7c8552001-04-11 14:29:21 +00003367** Mark a data page as writeable. The page is written into the journal
3368** if it is not there already. This routine must be called before making
3369** changes to a page.
3370**
3371** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003372** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003373** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003374** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003375** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003376**
3377** If the journal file could not be written because the disk is full,
3378** then this routine returns SQLITE_FULL and does an immediate rollback.
3379** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003380** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003381** reset.
drhed7c8552001-04-11 14:29:21 +00003382*/
danielk19773b8a05f2007-03-19 17:44:26 +00003383static int pager_write(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003384 void *pData = pPg->pData;
drh69688d52001-04-14 16:38:23 +00003385 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003386 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003387
drh6446c4d2001-12-15 14:22:18 +00003388 /* Check for errors
3389 */
danielk1977efaaf572006-01-16 11:29:19 +00003390 if( pPager->errCode ){
3391 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003392 }
drh5e00f6c2001-09-13 13:46:56 +00003393 if( pPager->readOnly ){
3394 return SQLITE_PERM;
3395 }
drh6446c4d2001-12-15 14:22:18 +00003396
danielk197776572402004-06-25 02:38:54 +00003397 assert( !pPager->setMaster );
3398
danielk19773c407372005-02-15 02:54:14 +00003399 CHECK_PAGE(pPg);
3400
drh538f5702007-04-13 02:14:30 +00003401 /* If this page was previously acquired with noContent==1, that means
3402 ** we didn't really read in the content of the page. This can happen
3403 ** (for example) when the page is being moved to the freelist. But
3404 ** now we are (perhaps) moving the page off of the freelist for
3405 ** reuse and we need to know its original content so that content
3406 ** can be stored in the rollback journal. So do the read at this
3407 ** time.
3408 */
drhd33d5a82007-04-26 12:11:28 +00003409 rc = pager_get_content(pPg);
3410 if( rc ){
3411 return rc;
drh538f5702007-04-13 02:14:30 +00003412 }
3413
drh6446c4d2001-12-15 14:22:18 +00003414 /* Mark the page as dirty. If the page has already been written
3415 ** to the journal then we can return right away.
3416 */
drhc047b9f2008-11-21 03:23:43 +00003417 sqlite3PcacheMakeDirty(pPg);
danielk19773460d192008-12-27 15:23:13 +00003418 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
drha6abd042004-06-09 17:37:22 +00003419 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003420 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003421 }else{
drh6446c4d2001-12-15 14:22:18 +00003422
danielk1977a0bf2652004-11-04 14:30:04 +00003423 /* If we get this far, it means that the page needs to be
3424 ** written to the transaction journal or the ckeckpoint journal
3425 ** or both.
3426 **
3427 ** First check to see that the transaction journal exists and
3428 ** create it if it does not.
3429 */
3430 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003431 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003432 if( rc!=SQLITE_OK ){
3433 return rc;
3434 }
3435 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00003436 if( !pPager->journalOpen && pPager->useJournal
3437 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00003438 rc = pager_open_journal(pPager);
3439 if( rc!=SQLITE_OK ) return rc;
3440 }
danielk1977a0bf2652004-11-04 14:30:04 +00003441 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003442 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003443
3444 /* The transaction journal now exists and we have a RESERVED or an
3445 ** EXCLUSIVE lock on the main database file. Write the current page to
3446 ** the transaction journal if it is not there already.
3447 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00003448 if( !pageInJournal(pPg) && pPager->journalOpen ){
danielk19773460d192008-12-27 15:23:13 +00003449 if( pPg->pgno<=pPager->dbOrigSize ){
danielk1977b3175382008-10-17 18:51:52 +00003450 u32 cksum;
3451 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00003452
danielk1977b3175382008-10-17 18:51:52 +00003453 /* We should never write to the journal file the page that
3454 ** contains the database locks. The following assert verifies
3455 ** that we do not. */
3456 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
3457 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
3458 cksum = pager_cksum(pPager, (u8*)pData2);
3459 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
3460 if( rc==SQLITE_OK ){
3461 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
3462 pPager->journalOff + 4);
3463 pPager->journalOff += pPager->pageSize+4;
3464 }
3465 if( rc==SQLITE_OK ){
3466 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
3467 pPager->journalOff += 4;
3468 }
3469 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
3470 pPager->journalOff, pPager->pageSize));
3471 PAGER_INCR(sqlite3_pager_writej_count);
drh30d53702009-01-06 15:58:57 +00003472 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
danielk1977b3175382008-10-17 18:51:52 +00003473 PAGERID(pPager), pPg->pgno,
drh30d53702009-01-06 15:58:57 +00003474 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
danielk197707cb5602006-01-20 10:55:05 +00003475
danielk1977f3107512008-12-22 10:58:46 +00003476 /* Even if an IO or diskfull error occurred while journalling the
3477 ** page in the block above, set the need-sync flag for the page.
3478 ** Otherwise, when the transaction is rolled back, the logic in
3479 ** playback_one_page() will think that the page needs to be restored
3480 ** in the database file. And if an IO error occurs while doing so,
3481 ** then corruption may follow.
3482 */
3483 if( !pPager->noSync ){
3484 pPg->flags |= PGHDR_NEED_SYNC;
danielk1977a4124bd2008-12-23 10:37:47 +00003485 pPager->needSync = 1;
danielk1977f3107512008-12-22 10:58:46 +00003486 }
3487
danielk1977b3175382008-10-17 18:51:52 +00003488 /* An error has occured writing to the journal file. The
3489 ** transaction will be rolled back by the layer above.
3490 */
3491 if( rc!=SQLITE_OK ){
3492 return rc;
3493 }
danielk197707cb5602006-01-20 10:55:05 +00003494
danielk1977b3175382008-10-17 18:51:52 +00003495 pPager->nRec++;
3496 assert( pPager->pInJournal!=0 );
drh7539b6b2009-01-02 21:39:39 +00003497 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3498 testcase( rc==SQLITE_NOMEM );
3499 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
3500 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
3501 if( rc!=SQLITE_OK ){
3502 assert( rc==SQLITE_NOMEM );
3503 return rc;
3504 }
danielk197713adf8a2004-06-03 16:08:41 +00003505 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003506 if( !pPager->journalStarted && !pPager->noSync ){
3507 pPg->flags |= PGHDR_NEED_SYNC;
danielk1977a4124bd2008-12-23 10:37:47 +00003508 pPager->needSync = 1;
danielk19778c0a7912008-08-20 14:49:23 +00003509 }
drh30d53702009-01-06 15:58:57 +00003510 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
danielk19778c0a7912008-08-20 14:49:23 +00003511 PAGERID(pPager), pPg->pgno,
drh30d53702009-01-06 15:58:57 +00003512 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
danielk1977a0bf2652004-11-04 14:30:04 +00003513 }
danielk1977a0bf2652004-11-04 14:30:04 +00003514 }
3515
3516 /* If the statement journal is open and the page is not in it,
3517 ** then write the current page to the statement journal. Note that
3518 ** the statement journal format differs from the standard journal format
3519 ** in that it omits the checksums and the header.
3520 */
danielk19773460d192008-12-27 15:23:13 +00003521 if( subjRequiresPage(pPg) ){
danielk1977f2c31ad2009-01-06 13:40:08 +00003522 rc = subjournalPage(pPg);
drhd9b02572001-04-15 00:37:09 +00003523 }
drhfa86c412002-02-02 15:01:15 +00003524 }
3525
3526 /* Update the database size and return.
3527 */
drh1aa2d8b2007-01-03 15:34:29 +00003528 assert( pPager->state>=PAGER_SHARED );
danielk1977d92db532008-11-17 04:56:24 +00003529 if( pPager->dbSize<pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003530 pPager->dbSize = pPg->pgno;
danielk1977d92db532008-11-17 04:56:24 +00003531 if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){
drh1f595712004-06-15 01:40:29 +00003532 pPager->dbSize++;
3533 }
drh306dc212001-05-21 13:45:10 +00003534 }
drh69688d52001-04-14 16:38:23 +00003535 return rc;
drhed7c8552001-04-11 14:29:21 +00003536}
3537
3538/*
danielk19774099f6e2007-03-19 11:25:20 +00003539** This function is used to mark a data-page as writable. It uses
3540** pager_write() to open a journal file (if it is not already open)
3541** and write the page *pData to the journal.
3542**
3543** The difference between this function and pager_write() is that this
3544** function also deals with the special case where 2 or more pages
3545** fit on a single disk sector. In this case all co-resident pages
3546** must have been written to the journal file before returning.
3547*/
danielk19773b8a05f2007-03-19 17:44:26 +00003548int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003549 int rc = SQLITE_OK;
3550
danielk19773b8a05f2007-03-19 17:44:26 +00003551 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003552 Pager *pPager = pPg->pPager;
3553 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3554
danielk1977b3175382008-10-17 18:51:52 +00003555 if( nPagePerSector>1 ){
danielk19774099f6e2007-03-19 11:25:20 +00003556 Pgno nPageCount; /* Total number of pages in database file */
3557 Pgno pg1; /* First page of the sector pPg is located on. */
3558 int nPage; /* Number of pages starting at pg1 to journal */
3559 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003560 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003561
3562 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3563 ** header to be written between the pages journaled by this function.
3564 */
danielk1977b3175382008-10-17 18:51:52 +00003565 assert( !MEMDB );
danielk19774099f6e2007-03-19 11:25:20 +00003566 assert( pPager->doNotSync==0 );
3567 pPager->doNotSync = 1;
3568
3569 /* This trick assumes that both the page-size and sector-size are
3570 ** an integer power of 2. It sets variable pg1 to the identifier
3571 ** of the first page of the sector pPg is located on.
3572 */
3573 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3574
danielk1977ad0132d2008-06-07 08:58:22 +00003575 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00003576 if( pPg->pgno>nPageCount ){
3577 nPage = (pPg->pgno - pg1)+1;
3578 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3579 nPage = nPageCount+1-pg1;
3580 }else{
3581 nPage = nPagePerSector;
3582 }
3583 assert(nPage>0);
3584 assert(pg1<=pPg->pgno);
3585 assert((pg1+nPage)>pPg->pgno);
3586
3587 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3588 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003589 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00003590 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00003591 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003592 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003593 if( rc==SQLITE_OK ){
3594 rc = pager_write(pPage);
danielk19778c0a7912008-08-20 14:49:23 +00003595 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003596 needSync = 1;
danielk1977a4124bd2008-12-23 10:37:47 +00003597 assert(pPager->needSync);
danielk1977dd97a492007-08-22 18:54:32 +00003598 }
danielk19773b8a05f2007-03-19 17:44:26 +00003599 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003600 }
3601 }
drhc81945e2008-03-10 14:12:53 +00003602 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk19778c0a7912008-08-20 14:49:23 +00003603 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003604 needSync = 1;
3605 }
danielk19778c0a7912008-08-20 14:49:23 +00003606 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003607 }
3608 }
3609
drhee03d622009-01-07 15:33:45 +00003610 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
danielk1977dd97a492007-08-22 18:54:32 +00003611 ** starting at pg1, then it needs to be set for all of them. Because
3612 ** writing to any of these nPage pages may damage the others, the
3613 ** journal file must contain sync()ed copies of all of them
3614 ** before any of them can be written out to the database file.
drhee03d622009-01-07 15:33:45 +00003615 **
3616 ** 2009-01-07: This block of code appears to be a no-op. I do not
3617 ** believe it is possible for any page on the sector to not have
3618 ** the PGHDR_NEED_SYNC flag set. The "pPage->flags |= PGHDR_NEED_SYNC"
3619 ** line below does nothing, I think. But it does no harm to leave
3620 ** this code in place until we can definitively prove this is the case.
danielk1977dd97a492007-08-22 18:54:32 +00003621 */
3622 if( needSync ){
drhb3df2e12008-09-17 20:06:26 +00003623 assert( !MEMDB && pPager->noSync==0 );
danielk1977dd97a492007-08-22 18:54:32 +00003624 for(ii=0; ii<nPage && needSync; ii++){
3625 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
drhee03d622009-01-07 15:33:45 +00003626 if( pPage ){
3627 assert( pPage->flags & PGHDR_NEED_SYNC ); /* 2009-01-07 conjecture */
3628 pPage->flags |= PGHDR_NEED_SYNC;
3629 sqlite3PagerUnref(pPage);
3630 }
danielk1977dd97a492007-08-22 18:54:32 +00003631 }
3632 assert(pPager->needSync);
3633 }
3634
danielk19774099f6e2007-03-19 11:25:20 +00003635 assert( pPager->doNotSync==1 );
3636 pPager->doNotSync = 0;
3637 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003638 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003639 }
3640 return rc;
3641}
3642
3643/*
drhaacc5432002-01-06 17:07:40 +00003644** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003645** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003646** to change the content of the page.
3647*/
danielk19777d3a6662006-01-23 16:21:05 +00003648#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003649int sqlite3PagerIswriteable(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003650 return pPg->flags&PGHDR_DIRTY;
drh6019e162001-07-02 17:51:45 +00003651}
danielk19777d3a6662006-01-23 16:21:05 +00003652#endif
drh6019e162001-07-02 17:51:45 +00003653
drh001bbcb2003-03-19 03:14:00 +00003654/*
drh30e58752002-03-02 20:41:57 +00003655** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003656** write the information on page pPg back to the disk, even though
drhdfe88ec2008-11-03 20:55:06 +00003657** that page might be marked as dirty. This happens, for example, when
3658** the page has been added as a leaf of the freelist and so its
3659** content no longer matters.
drh30e58752002-03-02 20:41:57 +00003660**
3661** The overlying software layer calls this routine when all of the data
3662** on the given page is unused. The pager marks the page as clean so
3663** that it does not get written to disk.
3664**
3665** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003666** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003667** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003668**
3669** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003670** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003671** will thereafter be ignored. This is necessary to avoid a problem
3672** where a page with data is added to the freelist during one part of
3673** a transaction then removed from the freelist during a later part
3674** of the same transaction and reused for some other purpose. When it
3675** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003676** the sqlite3PagerDontRollback() routine is called. But because the
3677** page contains critical data, we still need to be sure it gets
3678** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003679*/
danielk1977a1fa00d2008-08-27 15:16:33 +00003680int sqlite3PagerDontWrite(DbPage *pDbPage){
drh538f5702007-04-13 02:14:30 +00003681 PgHdr *pPg = pDbPage;
3682 Pager *pPager = pPg->pPager;
danielk1977a1fa00d2008-08-27 15:16:33 +00003683 int rc;
drh8e298f92002-07-06 16:28:47 +00003684
danielk19773460d192008-12-27 15:23:13 +00003685 if( pPg->pgno>pPager->dbOrigSize ){
danielk1977a1fa00d2008-08-27 15:16:33 +00003686 return SQLITE_OK;
3687 }
3688 if( pPager->pAlwaysRollback==0 ){
3689 assert( pPager->pInJournal );
danielk19773460d192008-12-27 15:23:13 +00003690 pPager->pAlwaysRollback = sqlite3BitvecCreate(pPager->dbOrigSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00003691 if( !pPager->pAlwaysRollback ){
3692 return SQLITE_NOMEM;
3693 }
3694 }
3695 rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno);
3696
danielk1977fd7f0452008-12-17 17:30:26 +00003697 if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
drh1aa2d8b2007-01-03 15:34:29 +00003698 assert( pPager->state>=PAGER_SHARED );
danielk19773460d192008-12-27 15:23:13 +00003699 if( pPager->dbSize==pPg->pgno && pPager->dbOrigSize<pPager->dbSize ){
drh8124a302002-06-25 14:43:57 +00003700 /* If this pages is the last page in the file and the file has grown
3701 ** during the current transaction, then do NOT mark the page as clean.
3702 ** When the database file grows, we must make sure that the last page
3703 ** gets written at least once so that the disk file will be the correct
3704 ** size. If you do not write this page and the size of the file
3705 ** on the disk ends up being too small, that can lead to database
3706 ** corruption during the next transaction.
3707 */
3708 }else{
drh30d53702009-01-06 15:58:57 +00003709 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
drh538f5702007-04-13 02:14:30 +00003710 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
danielk197733e32162008-08-23 18:53:08 +00003711 pPg->flags |= PGHDR_DONT_WRITE;
danielk19773c407372005-02-15 02:54:14 +00003712#ifdef SQLITE_CHECK_PAGES
3713 pPg->pageHash = pager_pagehash(pPg);
3714#endif
drh8124a302002-06-25 14:43:57 +00003715 }
drh30e58752002-03-02 20:41:57 +00003716 }
danielk1977a1fa00d2008-08-27 15:16:33 +00003717 return rc;
drh30e58752002-03-02 20:41:57 +00003718}
3719
3720/*
3721** A call to this routine tells the pager that if a rollback occurs,
3722** it is not necessary to restore the data on the given page. This
3723** means that the pager does not have to record the given page in the
3724** rollback journal.
drh538f5702007-04-13 02:14:30 +00003725**
3726** If we have not yet actually read the content of this page (if
3727** the PgHdr.needRead flag is set) then this routine acts as a promise
3728** that we will never need to read the page content in the future.
3729** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00003730*/
danielk19773b8a05f2007-03-19 17:44:26 +00003731void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003732 Pager *pPager = pPg->pPager;
drh7539b6b2009-01-02 21:39:39 +00003733 TESTONLY( int rc; ) /* Return value from sqlite3BitvecSet() */
drh30e58752002-03-02 20:41:57 +00003734
drhd3627af2006-12-18 18:34:51 +00003735 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00003736
3737 /* If the journal file is not open, or DontWrite() has been called on
3738 ** this page (DontWrite() sets the alwaysRollback flag), then this
3739 ** function is a no-op.
3740 */
danielk1977a1fa00d2008-08-27 15:16:33 +00003741 if( pPager->journalOpen==0
3742 || sqlite3BitvecTest(pPager->pAlwaysRollback, pPg->pgno)
danielk19773460d192008-12-27 15:23:13 +00003743 || pPg->pgno>pPager->dbOrigSize
danielk19778c0a7912008-08-20 14:49:23 +00003744 ){
danielk197787c29a92008-01-18 11:33:16 +00003745 return;
3746 }
danielk1977a55e9352008-01-21 13:04:34 +00003747
drhc5d0bd92008-04-14 01:00:57 +00003748#ifdef SQLITE_SECURE_DELETE
drh1feb7dd2008-11-19 18:30:29 +00003749 if( sqlite3BitvecTest(pPager->pInJournal, pPg->pgno)!=0
danielk19773460d192008-12-27 15:23:13 +00003750 || pPg->pgno>pPager->dbOrigSize ){
drhc5d0bd92008-04-14 01:00:57 +00003751 return;
3752 }
3753#endif
3754
3755 /* If SECURE_DELETE is disabled, then there is no way that this
3756 ** routine can be called on a page for which sqlite3PagerDontWrite()
3757 ** has not been previously called during the same transaction.
3758 ** And if DontWrite() has previously been called, the following
3759 ** conditions must be met.
drh10131482008-07-11 03:34:09 +00003760 **
3761 ** (Later:) Not true. If the database is corrupted by having duplicate
3762 ** pages on the freelist (ex: corrupt9.test) then the following is not
3763 ** necessarily true:
danielk1977a55e9352008-01-21 13:04:34 +00003764 */
danielk19773460d192008-12-27 15:23:13 +00003765 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->dbOrigSize ); */
danielk1977a55e9352008-01-21 13:04:34 +00003766
drhf5e7bb52008-02-18 14:47:33 +00003767 assert( pPager->pInJournal!=0 );
danielk19778c0a7912008-08-20 14:49:23 +00003768 pPg->flags &= ~PGHDR_NEED_READ;
drh7539b6b2009-01-02 21:39:39 +00003769
3770 /* Failure to set the bits in the InJournal bit-vectors is benign.
3771 ** It merely means that we might do some extra work to journal a page
drha8a71ba2009-01-03 12:55:17 +00003772 ** that does not need to be journaled. Nevertheless, be sure to test the
drh7539b6b2009-01-02 21:39:39 +00003773 ** case where a malloc error occurs while trying to set a bit in a
3774 ** bit vector.
3775 */
3776 sqlite3BeginBenignMalloc();
3777 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3778 testcase( rc==SQLITE_NOMEM );
3779 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pPg->pgno);
3780 testcase( rc==SQLITE_NOMEM );
3781 sqlite3EndBenignMalloc();
3782
3783
drh30d53702009-01-06 15:58:57 +00003784 PAGERTRACE(("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)));
danielk1977a55e9352008-01-21 13:04:34 +00003785 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00003786}
3787
drhac69b052004-05-12 13:30:07 +00003788
drh30e58752002-03-02 20:41:57 +00003789/*
drh80e35f42007-03-30 14:06:34 +00003790** This routine is called to increment the database file change-counter,
3791** stored at byte 24 of the pager file.
3792*/
danielk1977c7b60172007-08-22 11:22:03 +00003793static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00003794 PgHdr *pPgHdr;
3795 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00003796 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00003797
drh701bb3b2008-08-02 03:50:39 +00003798#ifndef SQLITE_ENABLE_ATOMIC_WRITE
3799 assert( isDirect==0 ); /* isDirect is only true for atomic writes */
3800#endif
danielk197712dd5492008-12-18 15:45:07 +00003801 if( !pPager->changeCountDone && pPager->dbSize>0 ){
drh80e35f42007-03-30 14:06:34 +00003802 /* Open page 1 of the file for writing. */
3803 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3804 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00003805
3806 if( !isDirect ){
3807 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00003808 if( rc!=SQLITE_OK ){
3809 sqlite3PagerUnref(pPgHdr);
3810 return rc;
3811 }
danielk1977c7b60172007-08-22 11:22:03 +00003812 }
3813
drh80e35f42007-03-30 14:06:34 +00003814 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00003815 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00003816 change_counter++;
danielk19778c0a7912008-08-20 14:49:23 +00003817 put32bits(((char*)pPgHdr->pData)+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00003818
drh701bb3b2008-08-02 03:50:39 +00003819#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977c7b60172007-08-22 11:22:03 +00003820 if( isDirect && pPager->fd->pMethods ){
danielk19778c0a7912008-08-20 14:49:23 +00003821 const void *zBuf = pPgHdr->pData;
danielk19773460d192008-12-27 15:23:13 +00003822 assert( pPager->dbFileSize>0 );
danielk1977c7b60172007-08-22 11:22:03 +00003823 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
3824 }
drh701bb3b2008-08-02 03:50:39 +00003825#endif
danielk1977c7b60172007-08-22 11:22:03 +00003826
drh80e35f42007-03-30 14:06:34 +00003827 /* Release the page reference. */
3828 sqlite3PagerUnref(pPgHdr);
3829 pPager->changeCountDone = 1;
3830 }
danielk1977c7b60172007-08-22 11:22:03 +00003831 return rc;
drh80e35f42007-03-30 14:06:34 +00003832}
3833
3834/*
danielk1977f653d782008-03-20 11:04:21 +00003835** Sync the pager file to disk.
3836*/
3837int sqlite3PagerSync(Pager *pPager){
3838 int rc;
drh7426f862008-08-26 21:07:26 +00003839 if( MEMDB ){
3840 rc = SQLITE_OK;
3841 }else{
3842 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3843 }
danielk1977f653d782008-03-20 11:04:21 +00003844 return rc;
3845}
3846
3847/*
drh80e35f42007-03-30 14:06:34 +00003848** Sync the database file for the pager pPager. zMaster points to the name
3849** of a master journal file that should be written into the individual
3850** journal file. zMaster may be NULL, which is interpreted as no master
3851** journal (a single database transaction).
3852**
3853** This routine ensures that the journal is synced, all dirty pages written
3854** to the database file and the database file synced. The only thing that
3855** remains to commit the transaction is to delete the journal file (or
3856** master journal file if specified).
3857**
3858** Note that if zMaster==NULL, this does not overwrite a previous value
3859** passed to an sqlite3PagerCommitPhaseOne() call.
3860**
danielk1977f653d782008-03-20 11:04:21 +00003861** If the final parameter - noSync - is true, then the database file itself
3862** is not synced. The caller must call sqlite3PagerSync() directly to
3863** sync the database file before calling CommitPhaseTwo() to delete the
3864** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00003865*/
danielk1977f653d782008-03-20 11:04:21 +00003866int sqlite3PagerCommitPhaseOne(
3867 Pager *pPager,
3868 const char *zMaster,
danielk1977f653d782008-03-20 11:04:21 +00003869 int noSync
3870){
drh80e35f42007-03-30 14:06:34 +00003871 int rc = SQLITE_OK;
3872
danielk1977dad31b52008-05-15 11:08:07 +00003873 if( pPager->errCode ){
3874 return pPager->errCode;
3875 }
3876
drhd138c012008-05-13 00:58:18 +00003877 /* If no changes have been made, we can leave the transaction early.
3878 */
3879 if( pPager->dbModified==0 &&
3880 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3881 pPager->exclusiveMode!=0) ){
3882 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3883 return SQLITE_OK;
3884 }
3885
drh30d53702009-01-06 15:58:57 +00003886 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
3887 pPager->zFilename, zMaster, pPager->dbSize));
drh80e35f42007-03-30 14:06:34 +00003888
3889 /* If this is an in-memory db, or no pages have been written to, or this
3890 ** function has already been called, it is a no-op.
3891 */
3892 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
3893 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00003894
danielk1977c7b60172007-08-22 11:22:03 +00003895#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3896 /* The atomic-write optimization can be used if all of the
3897 ** following are true:
3898 **
3899 ** + The file-system supports the atomic-write property for
3900 ** blocks of size page-size, and
3901 ** + This commit is not part of a multi-file transaction, and
3902 ** + Exactly one page has been modified and store in the journal file.
3903 **
3904 ** If the optimization can be used, then the journal file will never
3905 ** be created for this transaction.
3906 */
danielk19774d60af92008-09-08 15:35:06 +00003907 int useAtomicWrite;
danielk19778c0a7912008-08-20 14:49:23 +00003908 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
danielk19774d60af92008-09-08 15:35:06 +00003909 useAtomicWrite = (
danielk1977f55b8992007-08-24 08:15:53 +00003910 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00003911 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00003912 pPager->journalOff==jrnlBufferSize(pPager) &&
danielk19773460d192008-12-27 15:23:13 +00003913 pPager->dbSize>=pPager->dbFileSize &&
danielk19778c0a7912008-08-20 14:49:23 +00003914 (pPg==0 || pPg->pDirty==0)
danielk1977f55b8992007-08-24 08:15:53 +00003915 );
danielk1977700b9c52008-04-24 12:37:40 +00003916 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00003917 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00003918 /* Update the nRec field in the journal file. */
3919 int offset = pPager->journalHdr + sizeof(aJournalMagic);
3920 assert(pPager->nRec==1);
3921 rc = write32bits(pPager->jfd, offset, pPager->nRec);
3922
3923 /* Update the db file change counter. The following call will modify
3924 ** the in-memory representation of page 1 to include the updated
3925 ** change counter and then write page 1 directly to the database
3926 ** file. Because of the atomic-write property of the host file-system,
3927 ** this is safe.
3928 */
danielk1977ae72d982007-10-03 08:46:44 +00003929 if( rc==SQLITE_OK ){
3930 rc = pager_incr_changecounter(pPager, 1);
3931 }
danielk1977f55b8992007-08-24 08:15:53 +00003932 }else{
3933 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00003934 }
3935
danielk1977ae72d982007-10-03 08:46:44 +00003936 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00003937#endif
3938
drh80e35f42007-03-30 14:06:34 +00003939 /* If a master journal file name has already been written to the
3940 ** journal file, then no sync is required. This happens when it is
3941 ** written, then the process fails to upgrade from a RESERVED to an
3942 ** EXCLUSIVE lock. The next time the process tries to commit the
3943 ** transaction the m-j name will have already been written.
3944 */
3945 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00003946 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00003947 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00003948 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00003949#ifndef SQLITE_OMIT_AUTOVACUUM
danielk19773460d192008-12-27 15:23:13 +00003950 if( pPager->dbSize<pPager->dbOrigSize ){
danielk197771aa7ff2008-05-20 07:05:09 +00003951 /* If this transaction has made the database smaller, then all pages
3952 ** being discarded by the truncation must be written to the journal
3953 ** file.
3954 */
3955 Pgno i;
danielk1977d92db532008-11-17 04:56:24 +00003956 Pgno iSkip = PAGER_MJ_PGNO(pPager);
danielk19773460d192008-12-27 15:23:13 +00003957 Pgno dbSize = pPager->dbSize;
danielk1977f9bce3c2009-01-06 15:20:58 +00003958 pPager->dbSize = pPager->dbOrigSize;
danielk1977f70c1fe2009-01-07 18:08:48 +00003959 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
danielk197771aa7ff2008-05-20 07:05:09 +00003960 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
3961 rc = sqlite3PagerGet(pPager, i, &pPg);
3962 if( rc!=SQLITE_OK ) goto sync_exit;
3963 rc = sqlite3PagerWrite(pPg);
3964 sqlite3PagerUnref(pPg);
3965 if( rc!=SQLITE_OK ) goto sync_exit;
3966 }
3967 }
danielk19773460d192008-12-27 15:23:13 +00003968 pPager->dbSize = dbSize;
danielk197771aa7ff2008-05-20 07:05:09 +00003969 }
drh80e35f42007-03-30 14:06:34 +00003970#endif
danielk197771aa7ff2008-05-20 07:05:09 +00003971 rc = writeMasterJournal(pPager, zMaster);
3972 if( rc!=SQLITE_OK ) goto sync_exit;
3973 rc = syncJournal(pPager);
3974 }
drh80e35f42007-03-30 14:06:34 +00003975 }
danielk1977c7b60172007-08-22 11:22:03 +00003976 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00003977
drh80e35f42007-03-30 14:06:34 +00003978 /* Write all dirty pages to the database file */
danielk19778c0a7912008-08-20 14:49:23 +00003979 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003980 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00003981 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00003982 assert( rc!=SQLITE_IOERR_BLOCKED );
3983 /* The error might have left the dirty list all fouled up here,
3984 ** but that does not matter because if the if the dirty list did
3985 ** get corrupted, then the transaction will roll back and
3986 ** discard the dirty list. There is an assert in
3987 ** pager_get_all_dirty_pages() that verifies that no attempt
3988 ** is made to use an invalid dirty list.
3989 */
drh153c62c2007-08-24 03:51:33 +00003990 goto sync_exit;
3991 }
danielk19778c0a7912008-08-20 14:49:23 +00003992 sqlite3PcacheCleanAll(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003993
danielk1977f90b7262009-01-07 15:18:20 +00003994 if( pPager->dbSize<pPager->dbFileSize ){
3995 assert( pPager->state>=PAGER_EXCLUSIVE );
3996 rc = pager_truncate(pPager, pPager->dbSize);
3997 if( rc!=SQLITE_OK ) goto sync_exit;
3998 }
3999
drh80e35f42007-03-30 14:06:34 +00004000 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004001 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004002 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004003 }
4004 IOTRACE(("DBSYNC %p\n", pPager))
4005
4006 pPager->state = PAGER_SYNCED;
drh80e35f42007-03-30 14:06:34 +00004007 }
4008
4009sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004010 if( rc==SQLITE_IOERR_BLOCKED ){
4011 /* pager_incr_changecounter() may attempt to obtain an exclusive
4012 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004013 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004014 * better to return SQLITE_BUSY.
4015 */
4016 rc = SQLITE_BUSY;
4017 }
drh80e35f42007-03-30 14:06:34 +00004018 return rc;
4019}
4020
4021
4022/*
drhed7c8552001-04-11 14:29:21 +00004023** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004024**
4025** If the commit fails for any reason, a rollback attempt is made
4026** and an error code is returned. If the commit worked, SQLITE_OK
4027** is returned.
drhed7c8552001-04-11 14:29:21 +00004028*/
drh80e35f42007-03-30 14:06:34 +00004029int sqlite3PagerCommitPhaseTwo(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004030 int rc = SQLITE_OK;
drhd9b02572001-04-15 00:37:09 +00004031
danielk1977efaaf572006-01-16 11:29:19 +00004032 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004033 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004034 }
drha6abd042004-06-09 17:37:22 +00004035 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004036 return SQLITE_ERROR;
4037 }
drhd138c012008-05-13 00:58:18 +00004038 if( pPager->dbModified==0 &&
4039 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
4040 pPager->exclusiveMode!=0) ){
4041 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
4042 return SQLITE_OK;
4043 }
drh30d53702009-01-06 15:58:57 +00004044 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00004045 assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dirtyCache );
4046 rc = pager_end_transaction(pPager, pPager->setMaster);
4047 rc = pager_error(pPager, rc);
drh86f8c192007-08-22 00:39:19 +00004048 return rc;
drhed7c8552001-04-11 14:29:21 +00004049}
4050
4051/*
drha6abd042004-06-09 17:37:22 +00004052** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004053** All in-memory cache pages revert to their original data contents.
4054** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004055**
4056** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004057** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004058** process is writing trash into the journal file (SQLITE_CORRUPT) or
4059** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4060** codes are returned for all these occasions. Otherwise,
4061** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004062*/
danielk19773b8a05f2007-03-19 17:44:26 +00004063int sqlite3PagerRollback(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004064 int rc = SQLITE_OK;
drh30d53702009-01-06 15:58:57 +00004065 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00004066 if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00004067 rc = pager_end_transaction(pPager, pPager->setMaster);
danielk19778c0a7912008-08-20 14:49:23 +00004068 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004069 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004070 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004071 }
danielk19778c0a7912008-08-20 14:49:23 +00004072 rc = pPager->errCode;
drha6abd042004-06-09 17:37:22 +00004073 }else{
danielk19778c0a7912008-08-20 14:49:23 +00004074 if( pPager->state==PAGER_RESERVED ){
4075 int rc2;
4076 rc = pager_playback(pPager, 0);
4077 rc2 = pager_end_transaction(pPager, pPager->setMaster);
4078 if( rc==SQLITE_OK ){
4079 rc = rc2;
4080 }
4081 }else{
4082 rc = pager_playback(pPager, 0);
4083 }
danielk197707cb5602006-01-20 10:55:05 +00004084
danielk1977b3175382008-10-17 18:51:52 +00004085 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00004086 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00004087 }
danielk19778c0a7912008-08-20 14:49:23 +00004088
4089 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4090 ** cache. So call pager_error() on the way out to make any error
4091 ** persistent.
4092 */
4093 rc = pager_error(pPager, rc);
4094 }
drh86f8c192007-08-22 00:39:19 +00004095 return rc;
drh98808ba2001-10-18 12:34:46 +00004096}
drhd9b02572001-04-15 00:37:09 +00004097
4098/*
drh5e00f6c2001-09-13 13:46:56 +00004099** Return TRUE if the database file is opened read-only. Return FALSE
4100** if the database is (in theory) writable.
4101*/
drhf49661a2008-12-10 16:45:50 +00004102u8 sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004103 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004104}
4105
4106/*
drh0f7eb612006-08-08 13:51:43 +00004107** Return the number of references to the pager.
4108*/
danielk19773b8a05f2007-03-19 17:44:26 +00004109int sqlite3PagerRefcount(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00004110 return sqlite3PcacheRefCount(pPager->pPCache);
drh0f7eb612006-08-08 13:51:43 +00004111}
4112
danielk197771d5d2c2008-09-29 11:49:47 +00004113/*
4114** Return the number of references to the specified page.
4115*/
4116int sqlite3PagerPageRefcount(DbPage *pPage){
4117 return sqlite3PcachePageRefcount(pPage);
4118}
4119
drh0f7eb612006-08-08 13:51:43 +00004120#ifdef SQLITE_TEST
4121/*
drhd9b02572001-04-15 00:37:09 +00004122** This routine is used for testing and analysis only.
4123*/
danielk19773b8a05f2007-03-19 17:44:26 +00004124int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004125 static int a[11];
danielk19778c0a7912008-08-20 14:49:23 +00004126 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
4127 a[1] = sqlite3PcachePagecount(pPager->pPCache);
4128 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
danielk1977d92db532008-11-17 04:56:24 +00004129 a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
drhd9b02572001-04-15 00:37:09 +00004130 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004131 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004132 a[6] = pPager->nHit;
4133 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004134 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004135 a[9] = pPager->nRead;
4136 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004137 return a;
4138}
danielk197717b90b52008-06-06 11:11:25 +00004139int sqlite3PagerIsMemdb(Pager *pPager){
4140 return MEMDB;
4141}
drh0f7eb612006-08-08 13:51:43 +00004142#endif
drhdd793422001-06-28 01:54:48 +00004143
drhfa86c412002-02-02 15:01:15 +00004144/*
danielk1977fd7f0452008-12-17 17:30:26 +00004145** Ensure that there are at least nSavepoint savepoints open.
drhfa86c412002-02-02 15:01:15 +00004146*/
danielk1977fd7f0452008-12-17 17:30:26 +00004147int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
4148 int rc = SQLITE_OK;
4149
danielk197712dd5492008-12-18 15:45:07 +00004150 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
danielk1977fd7f0452008-12-17 17:30:26 +00004151 int ii;
drh49b9d332009-01-02 18:10:42 +00004152 PagerSavepoint *aNew;
danielk1977fd7f0452008-12-17 17:30:26 +00004153
4154 /* Either the sub-journal is open or there are no active savepoints. */
4155 assert( pPager->nSavepoint==0 || pPager->sjfd->pMethods );
4156
4157 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
4158 ** if the allocation fails. Otherwise, zero the new portion in case a
4159 ** malloc failure occurs while populating it in the for(...) loop below.
4160 */
drh49b9d332009-01-02 18:10:42 +00004161 aNew = (PagerSavepoint *)sqlite3Realloc(
danielk1977fd7f0452008-12-17 17:30:26 +00004162 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
4163 );
4164 if( !aNew ){
4165 return SQLITE_NOMEM;
4166 }
4167 memset(&aNew[pPager->nSavepoint], 0,
4168 (nSavepoint - pPager->nSavepoint) * sizeof(PagerSavepoint)
4169 );
4170 pPager->aSavepoint = aNew;
4171 ii = pPager->nSavepoint;
4172 pPager->nSavepoint = nSavepoint;
4173
4174 /* Populate the PagerSavepoint structures just allocated. */
4175 for(/* no-op */; ii<nSavepoint; ii++){
danielk197712dd5492008-12-18 15:45:07 +00004176 assert( pPager->dbSizeValid );
danielk1977fd7f0452008-12-17 17:30:26 +00004177 aNew[ii].nOrig = pPager->dbSize;
danielk197767ddef62008-12-23 19:15:56 +00004178 if( pPager->journalOpen && pPager->journalOff>0 ){
4179 aNew[ii].iOffset = pPager->journalOff;
4180 }else{
4181 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
4182 }
danielk1977fd7f0452008-12-17 17:30:26 +00004183 aNew[ii].iSubRec = pPager->stmtNRec;
4184 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
4185 if( !aNew[ii].pInSavepoint ){
4186 return SQLITE_NOMEM;
danielk1977b3175382008-10-17 18:51:52 +00004187 }
drh334b2992007-09-06 23:28:23 +00004188 }
danielk1977fd7f0452008-12-17 17:30:26 +00004189
4190 /* Open the sub-journal, if it is not already opened. */
4191 rc = openSubJournal(pPager);
drh0f892532002-05-30 12:27:03 +00004192 }
danielk1977fd7f0452008-12-17 17:30:26 +00004193
drh86f8c192007-08-22 00:39:19 +00004194 return rc;
4195}
drhfa86c412002-02-02 15:01:15 +00004196
4197/*
danielk1977fd7f0452008-12-17 17:30:26 +00004198** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
4199** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
4200** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
4201** that have occured since savepoint iSavepoint was created.
4202**
4203** In either case, all savepoints with an index greater than iSavepoint
4204** are destroyed.
4205**
4206** If there are less than (iSavepoint+1) active savepoints when this
4207** function is called it is a no-op.
4208*/
4209int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
4210 int rc = SQLITE_OK;
4211
4212 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
4213
4214 if( iSavepoint<pPager->nSavepoint ){
4215 int ii;
4216 int nNew = iSavepoint + (op==SAVEPOINT_ROLLBACK);
4217 for(ii=nNew; ii<pPager->nSavepoint; ii++){
4218 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
4219 }
4220 pPager->nSavepoint = nNew;
4221
danielk19778a7adb02008-12-22 11:43:35 +00004222 if( op==SAVEPOINT_ROLLBACK && pPager->jfd->pMethods ){
danielk1977f3107512008-12-22 10:58:46 +00004223 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
danielk1977fd7f0452008-12-17 17:30:26 +00004224 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
4225 assert(rc!=SQLITE_DONE);
4226 }
danielk1977f3107512008-12-22 10:58:46 +00004227
danielk1977fd7f0452008-12-17 17:30:26 +00004228 /* If this is a release of the outermost savepoint, truncate
4229 ** the sub-journal. */
4230 if( nNew==0 && op==SAVEPOINT_RELEASE && pPager->sjfd->pMethods ){
4231 assert( rc==SQLITE_OK );
4232 rc = sqlite3OsTruncate(pPager->sjfd, 0);
4233 pPager->stmtNRec = 0;
danielk1977b3175382008-10-17 18:51:52 +00004234 }
drh663fc632002-02-02 18:49:19 +00004235 }
drhfa86c412002-02-02 15:01:15 +00004236 return rc;
4237}
4238
drh73509ee2003-04-06 20:44:45 +00004239/*
4240** Return the full pathname of the database file.
4241*/
danielk19773b8a05f2007-03-19 17:44:26 +00004242const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004243 return pPager->zFilename;
4244}
4245
drhb20ea9d2004-02-09 01:20:36 +00004246/*
drhd0679ed2007-08-28 22:24:34 +00004247** Return the VFS structure for the pager.
4248*/
4249const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4250 return pPager->pVfs;
4251}
4252
4253/*
drhcc6bb3e2007-08-31 16:11:35 +00004254** Return the file handle for the database file associated
4255** with the pager. This might return NULL if the file has
4256** not yet been opened.
4257*/
4258sqlite3_file *sqlite3PagerFile(Pager *pPager){
4259 return pPager->fd;
4260}
4261
4262/*
danielk19775865e3d2004-06-14 06:03:57 +00004263** Return the directory of the database file.
4264*/
danielk19773b8a05f2007-03-19 17:44:26 +00004265const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004266 return pPager->zDirectory;
4267}
4268
4269/*
4270** Return the full pathname of the journal file.
4271*/
danielk19773b8a05f2007-03-19 17:44:26 +00004272const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004273 return pPager->zJournal;
4274}
4275
4276/*
drh2c8997b2005-08-27 16:36:48 +00004277** Return true if fsync() calls are disabled for this pager. Return FALSE
4278** if fsync()s are executed normally.
4279*/
danielk19773b8a05f2007-03-19 17:44:26 +00004280int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004281 return pPager->noSync;
4282}
4283
drh7c4ac0c2007-04-05 11:25:58 +00004284#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004285/*
drhb20ea9d2004-02-09 01:20:36 +00004286** Set the codec for this pager
4287*/
danielk19773b8a05f2007-03-19 17:44:26 +00004288void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004289 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004290 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004291 void *pCodecArg
4292){
4293 pPager->xCodec = xCodec;
4294 pPager->pCodecArg = pCodecArg;
4295}
drh7c4ac0c2007-04-05 11:25:58 +00004296#endif
drhb20ea9d2004-02-09 01:20:36 +00004297
danielk1977687566d2004-11-02 12:56:41 +00004298#ifndef SQLITE_OMIT_AUTOVACUUM
4299/*
danielk197787c29a92008-01-18 11:33:16 +00004300** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004301**
drh5e385312007-06-16 04:42:12 +00004302** There must be no references to the page previously located at
4303** pgno (which we call pPgOld) though that page is allowed to be
drhb3df2e12008-09-17 20:06:26 +00004304** in cache. If the page previously located at pgno is not already
drh5e385312007-06-16 04:42:12 +00004305** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004306**
drh5e385312007-06-16 04:42:12 +00004307** References to the page pPg remain valid. Updating any
4308** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004309** allocated along with the page) is the responsibility of the caller.
4310**
danielk19775fd057a2005-03-09 13:09:43 +00004311** A transaction must be active when this routine is called. It used to be
4312** required that a statement transaction was not active, but this restriction
4313** has been removed (CREATE INDEX needs to move a page when a statement
4314** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00004315**
4316** If the fourth argument, isCommit, is non-zero, then this page is being
4317** moved as part of a database reorganization just before the transaction
4318** is being committed. In this case, it is guaranteed that the database page
4319** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00004320*/
danielk19774c999992008-07-16 18:17:55 +00004321int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00004322 PgHdr *pPgOld; /* The page being overwritten. */
danielk197794daf7f2004-11-08 09:26:09 +00004323 Pgno needSyncPgno = 0;
danielk19771fab7b62009-01-07 10:35:18 +00004324 int rc;
danielk1977687566d2004-11-02 12:56:41 +00004325
danielk1977687566d2004-11-02 12:56:41 +00004326 assert( pPg->nRef>0 );
4327
danielk19771fab7b62009-01-07 10:35:18 +00004328 /* If the page being moved is dirty and has not been saved by the latest
4329 ** savepoint, then save the current contents of the page into the
4330 ** sub-journal now. This is required to handle the following scenario:
4331 **
4332 ** BEGIN;
4333 ** <journal page X, then modify it in memory>
4334 ** SAVEPOINT one;
4335 ** <Move page X to location Y>
4336 ** ROLLBACK TO one;
4337 **
4338 ** If page X were not written to the sub-journal here, it would not
4339 ** be possible to restore its contents when the "ROLLBACK TO one"
4340 ** statement were processed.
4341 */
4342 if( pPg->flags&PGHDR_DIRTY
4343 && subjRequiresPage(pPg)
4344 && SQLITE_OK!=(rc = subjournalPage(pPg))
4345 ){
4346 return rc;
4347 }
4348
drh30d53702009-01-06 15:58:57 +00004349 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
4350 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
drhb0603412007-02-28 04:47:26 +00004351 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004352
danielk1977b4626a32007-04-28 15:47:43 +00004353 pager_get_content(pPg);
danielk19774c999992008-07-16 18:17:55 +00004354
4355 /* If the journal needs to be sync()ed before page pPg->pgno can
4356 ** be written to, store pPg->pgno in local variable needSyncPgno.
4357 **
4358 ** If the isCommit flag is set, there is no need to remember that
4359 ** the journal needs to be sync()ed before database page pPg->pgno
4360 ** can be written to. The caller has already promised not to write to it.
4361 */
danielk19778c0a7912008-08-20 14:49:23 +00004362 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00004363 needSyncPgno = pPg->pgno;
danielk19773460d192008-12-27 15:23:13 +00004364 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
danielk19778c0a7912008-08-20 14:49:23 +00004365 assert( pPg->flags&PGHDR_DIRTY );
danielk1977ae825582004-11-23 09:06:55 +00004366 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004367 }
4368
danielk1977ef73ee92004-11-06 12:26:07 +00004369 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00004370 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00004371 ** page pgno before the 'move' operation, it needs to be retained
4372 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004373 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00004374 pPg->flags &= ~PGHDR_NEED_SYNC;
danielk1977687566d2004-11-02 12:56:41 +00004375 pPgOld = pager_lookup(pPager, pgno);
danielk19778c0a7912008-08-20 14:49:23 +00004376 assert( !pPgOld || pPgOld->nRef==1 );
danielk1977687566d2004-11-02 12:56:41 +00004377 if( pPgOld ){
danielk19778c0a7912008-08-20 14:49:23 +00004378 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
drh5e385312007-06-16 04:42:12 +00004379 }
danielk1977687566d2004-11-02 12:56:41 +00004380
danielk19778c0a7912008-08-20 14:49:23 +00004381 sqlite3PcacheMove(pPg, pgno);
4382 if( pPgOld ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00004383 sqlite3PcacheDrop(pPgOld);
danielk1977f5fdda82004-11-03 08:44:05 +00004384 }
danielk1977f5fdda82004-11-03 08:44:05 +00004385
drhc047b9f2008-11-21 03:23:43 +00004386 sqlite3PcacheMakeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004387 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004388 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00004389
danielk197794daf7f2004-11-08 09:26:09 +00004390 if( needSyncPgno ){
4391 /* If needSyncPgno is non-zero, then the journal file needs to be
4392 ** sync()ed before any data is written to database file page needSyncPgno.
4393 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00004394 ** "is journaled" bitvec flag has been set. This needs to be remedied by
4395 ** loading the page into the pager-cache and setting the PgHdr.needSync
4396 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00004397 **
danielk1977a98d7b42008-01-18 13:42:54 +00004398 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00004399 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00004400 ** array. Otherwise, if the page is loaded and written again in
4401 ** this transaction, it may be written to the database file before
4402 ** it is synced into the journal file. This way, it may end up in
4403 ** the journal file twice, but that is not a problem.
4404 **
danielk19773b8a05f2007-03-19 17:44:26 +00004405 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004406 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004407 */
danielk19773b8a05f2007-03-19 17:44:26 +00004408 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004409 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004410 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00004411 if( rc!=SQLITE_OK ){
danielk19773460d192008-12-27 15:23:13 +00004412 if( pPager->pInJournal && needSyncPgno<=pPager->dbOrigSize ){
drhf5e7bb52008-02-18 14:47:33 +00004413 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00004414 }
danielk197787c29a92008-01-18 11:33:16 +00004415 return rc;
4416 }
danielk1977ae825582004-11-23 09:06:55 +00004417 pPager->needSync = 1;
drhb3df2e12008-09-17 20:06:26 +00004418 assert( pPager->noSync==0 && !MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00004419 pPgHdr->flags |= PGHDR_NEED_SYNC;
drhc047b9f2008-11-21 03:23:43 +00004420 sqlite3PcacheMakeDirty(pPgHdr);
danielk19773b8a05f2007-03-19 17:44:26 +00004421 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004422 }
4423
danielk1977687566d2004-11-02 12:56:41 +00004424 return SQLITE_OK;
4425}
4426#endif
4427
danielk19773b8a05f2007-03-19 17:44:26 +00004428/*
4429** Return a pointer to the data for the specified page.
4430*/
4431void *sqlite3PagerGetData(DbPage *pPg){
danielk197771d5d2c2008-09-29 11:49:47 +00004432 assert( pPg->nRef>0 || pPg->pPager->memDb );
danielk19778c0a7912008-08-20 14:49:23 +00004433 return pPg->pData;
danielk19773b8a05f2007-03-19 17:44:26 +00004434}
4435
4436/*
4437** Return a pointer to the Pager.nExtra bytes of "extra" space
4438** allocated along with the specified page.
4439*/
4440void *sqlite3PagerGetExtra(DbPage *pPg){
4441 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00004442 return (pPager?pPg->pExtra:0);
danielk19773b8a05f2007-03-19 17:44:26 +00004443}
4444
danielk197741483462007-03-24 16:45:04 +00004445/*
4446** Get/set the locking-mode for this pager. Parameter eMode must be one
4447** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4448** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4449** the locking-mode is set to the value specified.
4450**
4451** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4452** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4453** locking-mode.
4454*/
4455int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004456 assert( eMode==PAGER_LOCKINGMODE_QUERY
4457 || eMode==PAGER_LOCKINGMODE_NORMAL
4458 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4459 assert( PAGER_LOCKINGMODE_QUERY<0 );
4460 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4461 if( eMode>=0 && !pPager->tempFile ){
drh1bd10f82008-12-10 21:19:56 +00004462 pPager->exclusiveMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +00004463 }
4464 return (int)pPager->exclusiveMode;
4465}
4466
drh3b020132008-04-17 17:02:01 +00004467/*
drh04335882008-09-26 21:08:08 +00004468** Get/set the journal-mode for this pager. Parameter eMode must be one of:
drh3b020132008-04-17 17:02:01 +00004469**
drh04335882008-09-26 21:08:08 +00004470** PAGER_JOURNALMODE_QUERY
4471** PAGER_JOURNALMODE_DELETE
4472** PAGER_JOURNALMODE_TRUNCATE
4473** PAGER_JOURNALMODE_PERSIST
4474** PAGER_JOURNALMODE_OFF
4475**
4476** If the parameter is not _QUERY, then the journal-mode is set to the
4477** value specified.
4478**
4479** The returned indicate the current (possibly updated)
drh3b020132008-04-17 17:02:01 +00004480** journal-mode.
4481*/
4482int sqlite3PagerJournalMode(Pager *pPager, int eMode){
danielk1977b3175382008-10-17 18:51:52 +00004483 if( !MEMDB ){
4484 assert( eMode==PAGER_JOURNALMODE_QUERY
4485 || eMode==PAGER_JOURNALMODE_DELETE
4486 || eMode==PAGER_JOURNALMODE_TRUNCATE
4487 || eMode==PAGER_JOURNALMODE_PERSIST
4488 || eMode==PAGER_JOURNALMODE_OFF
4489 || eMode==PAGER_JOURNALMODE_MEMORY );
4490 assert( PAGER_JOURNALMODE_QUERY<0 );
4491 if( eMode>=0 ){
drh1bd10f82008-12-10 21:19:56 +00004492 pPager->journalMode = (u8)eMode;
danielk1977b3175382008-10-17 18:51:52 +00004493 }else{
4494 assert( eMode==PAGER_JOURNALMODE_QUERY );
4495 }
drh3b020132008-04-17 17:02:01 +00004496 }
drhfdc40e92008-04-17 20:59:37 +00004497 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00004498}
4499
danielk1977b53e4962008-06-04 06:45:59 +00004500/*
4501** Get/set the size-limit used for persistent journal files.
4502*/
4503i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
4504 if( iLimit>=-1 ){
4505 pPager->journalSizeLimit = iLimit;
4506 }
4507 return pPager->journalSizeLimit;
4508}
4509
drh2e66f0b2005-04-28 17:18:48 +00004510#endif /* SQLITE_OMIT_DISKIO */