blob: 4d24a36eab449f791c0dcbc8a749e704db85a40c [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**
drh98c58352008-11-07 00:24:53 +000021** @(#) $Id: pager.c,v 1.502 2008/11/07 00:24:54 drh 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
drhd3627af2006-12-18 18:34:51 +000030#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000031#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
32#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
33#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
34#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
35#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000036#else
drh4f0c5872007-03-26 22:05:01 +000037#define PAGERTRACE1(X)
38#define PAGERTRACE2(X,Y)
39#define PAGERTRACE3(X,Y,Z)
40#define PAGERTRACE4(X,Y,Z,W)
41#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000042#endif
43
danielk1977599fcba2004-11-08 07:13:13 +000044/*
drh4f0c5872007-03-26 22:05:01 +000045** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000046** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000047**
drh85b623f2007-12-13 21:54:09 +000048** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000049** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000050** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000051*/
drhc001c582006-03-06 18:23:16 +000052#define PAGERID(p) ((int)(p->fd))
53#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000054
55/*
drhed7c8552001-04-11 14:29:21 +000056** The page cache as a whole is always in one of the following
57** states:
58**
drha6abd042004-06-09 17:37:22 +000059** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000060** writing the database file. There is no
61** data held in memory. This is the initial
62** state.
63**
drha6abd042004-06-09 17:37:22 +000064** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000065** Writing is not permitted. There can be
66** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000067** file at the same time.
drhed7c8552001-04-11 14:29:21 +000068**
drh726de592004-06-10 23:35:50 +000069** PAGER_RESERVED This process has reserved the database for writing
70** but has not yet made any changes. Only one process
71** at a time can reserve the database. The original
72** database file has not been modified so other
73** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000074** database file.
75**
76** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000077** Access is exclusive. No other processes or
78** threads can be reading or writing while one
79** process is writing.
80**
danielk1977aa5ccdf2004-06-14 05:10:42 +000081** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
82** after all dirty pages have been written to the
83** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000084** disk. All that remains to do is to remove or
85** truncate the journal file and the transaction
86** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000087**
drha6abd042004-06-09 17:37:22 +000088** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000089** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000090** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000091** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000092** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000093** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000094** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000095** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000096** PAGER_RESERVED means that there is an open rollback journal.
97** The transition to PAGER_EXCLUSIVE occurs before any changes
98** are made to the database file, though writes to the rollback
99** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
100** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
101** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000102*/
drha6abd042004-06-09 17:37:22 +0000103#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000104#define PAGER_SHARED 1 /* same as SHARED_LOCK */
105#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
106#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
107#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000108
drh684917c2004-10-05 02:41:42 +0000109/*
110** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
111** then failed attempts to get a reserved lock will invoke the busy callback.
112** This is off by default. To see why, consider the following scenario:
113**
114** Suppose thread A already has a shared lock and wants a reserved lock.
115** Thread B already has a reserved lock and wants an exclusive lock. If
116** both threads are using their busy callbacks, it might be a long time
117** be for one of the threads give up and allows the other to proceed.
118** But if the thread trying to get the reserved lock gives up quickly
119** (if it never invokes its busy callback) then the contention will be
120** resolved quickly.
121*/
122#ifndef SQLITE_BUSY_RESERVED_LOCK
123# define SQLITE_BUSY_RESERVED_LOCK 0
124#endif
drhd9b02572001-04-15 00:37:09 +0000125
drhed7c8552001-04-11 14:29:21 +0000126/*
drh887dc4c2004-10-22 16:22:57 +0000127** This macro rounds values up so that if the value is an address it
128** is guaranteed to be an address that is aligned to an 8-byte boundary.
129*/
130#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
131
drh9eb9e262004-02-11 02:18:05 +0000132/*
133** A macro used for invoking the codec if there is one
134*/
135#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000136# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
137# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000138#else
drhc001c582006-03-06 18:23:16 +0000139# define CODEC1(P,D,N,X) /* NO-OP */
140# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000141#endif
142
drhed7c8552001-04-11 14:29:21 +0000143/*
drhed7c8552001-04-11 14:29:21 +0000144** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000145**
drh4f0ee682007-03-30 20:43:40 +0000146** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000147** or SQLITE_FULL. Once one of the first three errors occurs, it persists
148** and is returned as the result of every major pager API call. The
149** SQLITE_FULL return code is slightly different. It persists only until the
150** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000151** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000152** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000153*/
154struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000155 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000156 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000157 u8 journalStarted; /* True if header of journal is synced */
158 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000159 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000160 u8 stmtOpen; /* True if the statement subjournal is open */
161 u8 stmtInUse; /* True we are in a statement subtransaction */
162 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000163 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000164 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000165 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000166 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000167 u8 tempFile; /* zFilename is a temporary file */
168 u8 readOnly; /* True for a read-only database */
169 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000170 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000171 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000172 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000173 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000174 u8 doNotSync; /* Boolean. While true, do not spill the cache */
175 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000176 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drhd138c012008-05-13 00:58:18 +0000177 u8 dbModified; /* True if there are any changes to the Db */
drh80e35f42007-03-30 14:06:34 +0000178 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000179 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000180 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000181 int dbSize; /* Number of pages in the file */
182 int origDbSize; /* dbSize before the current change */
183 int stmtSize; /* Size of database (in pages) at stmt_begin() */
184 int nRec; /* Number of pages written to the journal */
185 u32 cksumInit; /* Quasi-random value added to every checksum */
186 int stmtNRec; /* Number of records in stmt subjournal */
187 int nExtra; /* Add this many bytes to each in-memory page */
188 int pageSize; /* Number of bytes in a page */
189 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000190 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000191 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000192 Bitvec *pInJournal; /* One bit for each page in the database file */
193 Bitvec *pInStmt; /* One bit for each page in the database */
danielk1977a1fa00d2008-08-27 15:16:33 +0000194 Bitvec *pAlwaysRollback; /* One bit for each page marked always-rollback */
drhfcd35c72005-05-21 02:48:08 +0000195 char *zFilename; /* Name of the database file */
196 char *zJournal; /* Name of the journal file */
197 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000198 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
199 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000200 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
drheb206252004-10-01 02:00:31 +0000201 i64 journalOff; /* Current byte offset in the journal file */
202 i64 journalHdr; /* Byte offset to previous journal header */
203 i64 stmtHdrOff; /* First journal header written this statement */
204 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000205 i64 stmtJSize; /* Size of journal at stmt_begin() */
drh98c58352008-11-07 00:24:53 +0000206 u32 sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000207#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000208 int nHit, nMiss; /* Cache hits and missing */
209 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000210#endif
danielk1977eaa06f62008-09-18 17:34:44 +0000211 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000212#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000213 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000214 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000215#endif
danielk19778186df82007-03-06 13:45:59 +0000216 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000217 char dbFileVers[16]; /* Changes whenever database file changes */
danielk1977b53e4962008-06-04 06:45:59 +0000218 i64 journalSizeLimit; /* Size limit for persistent journal files */
danielk19778c0a7912008-08-20 14:49:23 +0000219 PCache *pPCache; /* Pointer to page cache object */
drhd9b02572001-04-15 00:37:09 +0000220};
221
222/*
drh538f5702007-04-13 02:14:30 +0000223** The following global variables hold counters used for
224** testing purposes only. These variables do not exist in
225** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000226*/
227#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000228int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
229int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
230int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
drh538f5702007-04-13 02:14:30 +0000231# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000232#else
drh538f5702007-04-13 02:14:30 +0000233# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000234#endif
235
drh538f5702007-04-13 02:14:30 +0000236
237
drhfcd35c72005-05-21 02:48:08 +0000238/*
drh5e00f6c2001-09-13 13:46:56 +0000239** Journal files begin with the following magic string. The data
240** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000241**
drhae2b40c2004-06-09 19:03:54 +0000242** Since version 2.8.0, the journal format contains additional sanity
243** checking information. If the power fails while the journal is begin
244** written, semi-random garbage data might appear in the journal
245** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000246** to roll the journal back, the database could be corrupted. The additional
247** sanity checking data is an attempt to discover the garbage in the
248** journal and ignore it.
249**
drhae2b40c2004-06-09 19:03:54 +0000250** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000251** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000252** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000253** This cksum is initialized to a 32-bit random value that appears in the
254** journal file right after the header. The random initializer is important,
255** because garbage data that appears at the end of a journal is likely
256** data that was once in other files that have now been deleted. If the
257** garbage data came from an obsolete journal file, the checksums might
258** be correct. But by initializing the checksum to random value which
259** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000260*/
drhae2b40c2004-06-09 19:03:54 +0000261static const unsigned char aJournalMagic[] = {
262 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000263};
264
265/*
drh726de592004-06-10 23:35:50 +0000266** The size of the header and of each page in the journal is determined
267** by the following macros.
drh968af522003-02-11 14:55:40 +0000268*/
drhae2b40c2004-06-09 19:03:54 +0000269#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000270
danielk197776572402004-06-25 02:38:54 +0000271/*
272** The journal header size for this pager. In the future, this could be
273** set to some value read from the disk controller. The important
274** characteristic is that it is the same size as a disk sector.
275*/
276#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
277
drhb7f91642004-10-31 02:22:47 +0000278/*
279** The macro MEMDB is true if we are dealing with an in-memory database.
280** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
281** the value of MEMDB will be a constant and the compiler will optimize
282** out code that would never execute.
283*/
284#ifdef SQLITE_OMIT_MEMORYDB
285# define MEMDB 0
286#else
287# define MEMDB pPager->memDb
288#endif
289
290/*
danielk197776572402004-06-25 02:38:54 +0000291** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
292** reserved for working around a windows/posix incompatibility). It is
293** used in the journal to signify that the remainder of the journal file
294** is devoted to storing a master journal name - there are no more pages to
295** roll back. See comments for function writeMasterJournal() for details.
296*/
danielk1977599fcba2004-11-08 07:13:13 +0000297/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
298#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000299
drh968af522003-02-11 14:55:40 +0000300/*
danielk197726836652005-01-17 01:33:13 +0000301** The maximum legal page number is (2^31 - 1).
302*/
303#define PAGER_MAX_PGNO 2147483647
304
305/*
danielk1977f35843b2007-04-07 15:03:17 +0000306** Return true if page *pPg has already been written to the statement
307** journal (or statement snapshot has been created, if *pPg is part
308** of an in-memory database).
309*/
310static int pageInStatement(PgHdr *pPg){
311 Pager *pPager = pPg->pPager;
danielk1977b3175382008-10-17 18:51:52 +0000312 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000313}
drh8ca0c722006-05-07 17:49:38 +0000314
315/*
drh34e79ce2004-02-08 06:05:46 +0000316** Read a 32-bit integer from the given file descriptor. Store the integer
317** that is read in *pRes. Return SQLITE_OK if everything worked, or an
318** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000319**
320** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000321*/
danielk197762079062007-08-15 17:08:46 +0000322static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000323 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000324 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000325 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000326 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000327 }
drh94f33312002-08-12 12:29:56 +0000328 return rc;
329}
330
331/*
drh97b57482006-01-10 20:32:32 +0000332** Write a 32-bit integer into a string buffer in big-endian byte order.
333*/
drha3152892007-05-05 11:48:52 +0000334#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000335
336/*
drh34e79ce2004-02-08 06:05:46 +0000337** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
338** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000339*/
danielk197762079062007-08-15 17:08:46 +0000340static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000341 char ac[4];
drh97b57482006-01-10 20:32:32 +0000342 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000343 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000344}
345
drh2554f8b2003-01-22 01:26:44 +0000346/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000347** If file pFd is open, call sqlite3OsUnlock() on it.
348*/
349static int osUnlock(sqlite3_file *pFd, int eLock){
350 if( !pFd->pMethods ){
351 return SQLITE_OK;
352 }
353 return sqlite3OsUnlock(pFd, eLock);
354}
355
356/*
danielk1977c7b60172007-08-22 11:22:03 +0000357** This function determines whether or not the atomic-write optimization
358** can be used with this pager. The optimization can be used if:
359**
360** (a) the value returned by OsDeviceCharacteristics() indicates that
361** a database page may be written atomically, and
362** (b) the value returned by OsSectorSize() is less than or equal
363** to the page size.
364**
365** If the optimization cannot be used, 0 is returned. If it can be used,
366** then the value returned is the size of the journal file when it
367** contains rollback data for exactly one page.
368*/
369#ifdef SQLITE_ENABLE_ATOMIC_WRITE
370static int jrnlBufferSize(Pager *pPager){
371 int dc; /* Device characteristics */
372 int nSector; /* Sector size */
drhfacf0302008-06-17 15:12:00 +0000373 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000374 sqlite3_file *fd = pPager->fd;
375
376 if( fd->pMethods ){
377 dc = sqlite3OsDeviceCharacteristics(fd);
378 nSector = sqlite3OsSectorSize(fd);
drhfacf0302008-06-17 15:12:00 +0000379 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000380 }
381
382 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
383 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
384
drhfacf0302008-06-17 15:12:00 +0000385 if( !fd->pMethods ||
386 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000387 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
388 }
389 return 0;
390}
391#endif
392
393/*
danielk1977aef0bf62005-12-30 16:28:01 +0000394** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000395** code. The first argument is a pointer to the pager structure, the
396** second the error-code about to be returned by a pager API function.
397** The value returned is a copy of the second argument to this function.
398**
drh4f0ee682007-03-30 20:43:40 +0000399** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000400** the error becomes persistent. Until the persisten error is cleared,
401** subsequent API calls on this Pager will immediately return the same
402** error code.
403**
404** A persistent error indicates that the contents of the pager-cache
405** cannot be trusted. This state can be cleared by completely discarding
406** the contents of the pager-cache. If a transaction was active when
407** the persistent error occured, then the rollback journal may need
408** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000409*/
danielk1977ae72d982007-10-03 08:46:44 +0000410static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000411static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000412 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000413 assert(
414 pPager->errCode==SQLITE_FULL ||
415 pPager->errCode==SQLITE_OK ||
416 (pPager->errCode & 0xff)==SQLITE_IOERR
417 );
danielk1977979f38e2007-03-27 16:19:51 +0000418 if(
drh4ac285a2006-09-15 07:28:50 +0000419 rc2==SQLITE_FULL ||
420 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000421 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000422 ){
423 pPager->errCode = rc;
danielk19778c0a7912008-08-20 14:49:23 +0000424 if( pPager->state==PAGER_UNLOCK
425 && sqlite3PcacheRefCount(pPager->pPCache)==0
426 ){
danielk1977ae72d982007-10-03 08:46:44 +0000427 /* If the pager is already unlocked, call pager_unlock() now to
428 ** clear the error state and ensure that the pager-cache is
429 ** completely empty.
430 */
431 pager_unlock(pPager);
432 }
danielk1977aef0bf62005-12-30 16:28:01 +0000433 }
434 return rc;
435}
436
drh477731b2007-06-16 03:06:27 +0000437/*
438** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
439** on the cache using a hash function. This is used for testing
440** and debugging only.
441*/
danielk19773c407372005-02-15 02:54:14 +0000442#ifdef SQLITE_CHECK_PAGES
443/*
444** Return a 32-bit hash of the page data for pPage.
445*/
drh477731b2007-06-16 03:06:27 +0000446static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000447 u32 hash = 0;
448 int i;
drh477731b2007-06-16 03:06:27 +0000449 for(i=0; i<nByte; i++){
450 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000451 }
452 return hash;
453}
drh477731b2007-06-16 03:06:27 +0000454static u32 pager_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000455 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
456}
457static u32 pager_set_pagehash(PgHdr *pPage){
458 pPage->pageHash = pager_pagehash(pPage);
drh477731b2007-06-16 03:06:27 +0000459}
danielk19773c407372005-02-15 02:54:14 +0000460
461/*
462** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
463** is defined, and NDEBUG is not defined, an assert() statement checks
464** that the page is either dirty or still matches the calculated page-hash.
465*/
466#define CHECK_PAGE(x) checkPage(x)
467static void checkPage(PgHdr *pPg){
468 Pager *pPager = pPg->pPager;
danielk1977b3175382008-10-17 18:51:52 +0000469 assert( !pPg->pageHash || pPager->errCode
danielk19778c0a7912008-08-20 14:49:23 +0000470 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
danielk19773c407372005-02-15 02:54:14 +0000471}
472
473#else
drh8ffa8172007-06-18 17:25:17 +0000474#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000475#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000476#define CHECK_PAGE(x)
drh41d30272008-08-20 21:47:45 +0000477#endif /* SQLITE_CHECK_PAGES */
danielk19773c407372005-02-15 02:54:14 +0000478
drhed7c8552001-04-11 14:29:21 +0000479/*
danielk197776572402004-06-25 02:38:54 +0000480** When this is called the journal file for pager pPager must be open.
481** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000482** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000483**
danielk197765839c62007-08-30 08:08:17 +0000484** zMaster must point to a buffer of at least nMaster bytes allocated by
485** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
486** enough space to write the master journal name). If the master journal
487** name in the journal is longer than nMaster bytes (including a
488** nul-terminator), then this is handled as if no master journal name
489** were present in the journal.
490**
491** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000492** SQLITE_OK returned.
493*/
danielk197765839c62007-08-30 08:08:17 +0000494static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000495 int rc;
496 u32 len;
drheb206252004-10-01 02:00:31 +0000497 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000498 u32 cksum;
shane0b8d2762008-07-22 05:18:00 +0000499 u32 u; /* Unsigned loop counter */
danielk197776572402004-06-25 02:38:54 +0000500 unsigned char aMagic[8]; /* A buffer to hold the magic header */
501
danielk197765839c62007-08-30 08:08:17 +0000502 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000503
drh054889e2005-11-30 03:20:31 +0000504 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000505 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000506
danielk197762079062007-08-15 17:08:46 +0000507 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000508 if( rc!=SQLITE_OK ) return rc;
509
danielk197765839c62007-08-30 08:08:17 +0000510 if( len>=nMaster ){
511 return SQLITE_OK;
512 }
513
danielk197762079062007-08-15 17:08:46 +0000514 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000515 if( rc!=SQLITE_OK ) return rc;
516
danielk197762079062007-08-15 17:08:46 +0000517 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000518 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
519
danielk197765839c62007-08-30 08:08:17 +0000520 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000521 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000522 return rc;
523 }
danielk197765839c62007-08-30 08:08:17 +0000524 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000525
526 /* See if the checksum matches the master journal name */
shane0b8d2762008-07-22 05:18:00 +0000527 for(u=0; u<len; u++){
528 cksum -= zMaster[u];
danielk197765839c62007-08-30 08:08:17 +0000529 }
danielk19778191bff2004-06-28 04:52:30 +0000530 if( cksum ){
531 /* If the checksum doesn't add up, then one or more of the disk sectors
532 ** containing the master journal filename is corrupted. This means
533 ** definitely roll back, so just return SQLITE_OK and report a (nul)
534 ** master-journal filename.
535 */
danielk197765839c62007-08-30 08:08:17 +0000536 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000537 }
danielk197776572402004-06-25 02:38:54 +0000538
539 return SQLITE_OK;
540}
541
542/*
543** Seek the journal file descriptor to the next sector boundary where a
544** journal header may be read or written. Pager.journalOff is updated with
545** the new seek offset.
546**
547** i.e for a sector size of 512:
548**
549** Input Offset Output Offset
550** ---------------------------------------
551** 0 0
552** 512 512
553** 100 512
554** 2000 2048
555**
556*/
danielk197762079062007-08-15 17:08:46 +0000557static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000558 i64 offset = 0;
559 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000560 if( c ){
561 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
562 }
563 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
564 assert( offset>=c );
565 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
566 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000567}
568
569/*
drhf3a87622008-04-17 14:16:42 +0000570** Write zeros over the header of the journal file. This has the
571** effect of invalidating the journal file and committing the
572** transaction.
573*/
danielk1977df2566a2008-05-07 19:11:03 +0000574static int zeroJournalHdr(Pager *pPager, int doTruncate){
575 int rc = SQLITE_OK;
danielk197755a25a12008-09-11 10:29:15 +0000576 static const char zeroHdr[28] = {0};
drhf3a87622008-04-17 14:16:42 +0000577
danielk1977df2566a2008-05-07 19:11:03 +0000578 if( pPager->journalOff ){
danielk1977b53e4962008-06-04 06:45:59 +0000579 i64 iLimit = pPager->journalSizeLimit;
580
danielk1977df2566a2008-05-07 19:11:03 +0000581 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000582 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000583 rc = sqlite3OsTruncate(pPager->jfd, 0);
584 }else{
585 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
586 }
danielk197781620542008-06-07 05:19:37 +0000587 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000588 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
589 }
danielk1977b53e4962008-06-04 06:45:59 +0000590
591 /* At this point the transaction is committed but the write lock
592 ** is still held on the file. If there is a size limit configured for
593 ** the persistent journal and the journal file currently consumes more
594 ** space than that limit allows for, truncate it now. There is no need
595 ** to sync the file following this operation.
596 */
597 if( rc==SQLITE_OK && iLimit>0 ){
598 i64 sz;
599 rc = sqlite3OsFileSize(pPager->jfd, &sz);
600 if( rc==SQLITE_OK && sz>iLimit ){
601 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
602 }
603 }
drha06ecba2008-04-22 17:15:17 +0000604 }
drhf3a87622008-04-17 14:16:42 +0000605 return rc;
606}
607
608/*
danielk197776572402004-06-25 02:38:54 +0000609** The journal file must be open when this routine is called. A journal
610** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
611** current location.
612**
613** The format for the journal header is as follows:
614** - 8 bytes: Magic identifying journal format.
615** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
616** - 4 bytes: Random number used for page hash.
617** - 4 bytes: Initial database page count.
618** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000619** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000620**
danielk197767c007b2008-03-20 04:45:49 +0000621** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000622*/
623static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +0000624 int rc = SQLITE_OK;
625 char *zHeader = pPager->pTmpSpace;
626 int nHeader = pPager->pageSize;
627 int nWrite;
628
629 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
630 nHeader = JOURNAL_HDR_SZ(pPager);
631 }
danielk197776572402004-06-25 02:38:54 +0000632
danielk19774099f6e2007-03-19 11:25:20 +0000633 if( pPager->stmtHdrOff==0 ){
634 pPager->stmtHdrOff = pPager->journalOff;
635 }
636
danielk197762079062007-08-15 17:08:46 +0000637 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000638 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000639
drh97b57482006-01-10 20:32:32 +0000640 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000641
642 /*
643 ** Write the nRec Field - the number of page records that follow this
644 ** journal header. Normally, zero is written to this value at this time.
645 ** After the records are added to the journal (and the journal synced,
646 ** if in full-sync mode), the zero is overwritten with the true number
647 ** of records (see syncJournal()).
648 **
649 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
650 ** reading the journal this value tells SQLite to assume that the
651 ** rest of the journal file contains valid page records. This assumption
652 ** is dangerous, as if a failure occured whilst writing to the journal
653 ** file it may contain some garbage data. There are two scenarios
654 ** where this risk can be ignored:
655 **
656 ** * When the pager is in no-sync mode. Corruption can follow a
657 ** power failure in this case anyway.
658 **
659 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
660 ** that garbage data is never appended to the journal file.
661 */
662 assert(pPager->fd->pMethods||pPager->noSync);
danielk1977b3175382008-10-17 18:51:52 +0000663 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
danielk19774cd2cd52007-08-23 14:48:23 +0000664 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
665 ){
666 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
667 }else{
668 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
669 }
670
drh97b57482006-01-10 20:32:32 +0000671 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +0000672 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +0000673 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
674 /* The initial database size */
675 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
676 /* The assumed sector size for this process */
677 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
danielk197767c007b2008-03-20 04:45:49 +0000678 if( pPager->journalHdr==0 ){
679 /* The page size */
680 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
681 }
danielk197776572402004-06-25 02:38:54 +0000682
danielk1977a664f8e2008-04-22 14:31:48 +0000683 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
684 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
685 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
686 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +0000687 }
danielk1977a664f8e2008-04-22 14:31:48 +0000688
danielk197776572402004-06-25 02:38:54 +0000689 return rc;
690}
691
692/*
693** The journal file must be open when this is called. A journal header file
694** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
695** file. See comments above function writeJournalHdr() for a description of
696** the journal header format.
697**
698** If the header is read successfully, *nRec is set to the number of
699** page records following this header and *dbSize is set to the size of the
700** database before the transaction began, in pages. Also, pPager->cksumInit
701** is set to the value read from the journal header. SQLITE_OK is returned
702** in this case.
703**
704** If the journal header file appears to be corrupted, SQLITE_DONE is
705** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
706** cannot be read from the journal file an error code is returned.
707*/
708static int readJournalHdr(
709 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000710 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000711 u32 *pNRec,
712 u32 *pDbSize
713){
714 int rc;
715 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000716 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +0000717 int iPageSize;
danielk197776572402004-06-25 02:38:54 +0000718
danielk197762079062007-08-15 17:08:46 +0000719 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000720 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
721 return SQLITE_DONE;
722 }
danielk197762079062007-08-15 17:08:46 +0000723 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000724
danielk197762079062007-08-15 17:08:46 +0000725 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000726 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000727 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000728
729 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
730 return SQLITE_DONE;
731 }
732
danielk197762079062007-08-15 17:08:46 +0000733 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000734 if( rc ) return rc;
735
danielk197762079062007-08-15 17:08:46 +0000736 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000737 if( rc ) return rc;
738
danielk197762079062007-08-15 17:08:46 +0000739 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000740 if( rc ) return rc;
741
danielk197767c007b2008-03-20 04:45:49 +0000742 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
743 if( rc==SQLITE_OK
744 && iPageSize>=512
745 && iPageSize<=SQLITE_MAX_PAGE_SIZE
746 && ((iPageSize-1)&iPageSize)==0
747 ){
748 u16 pagesize = iPageSize;
749 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
750 }
751 if( rc ) return rc;
752
danielk197776572402004-06-25 02:38:54 +0000753 /* Update the assumed sector-size to match the value used by
754 ** the process that created this journal. If this journal was
755 ** created by a process other than this one, then this routine
756 ** is being called from within pager_playback(). The local value
757 ** of Pager.sectorSize is restored at the end of that routine.
758 */
drh98c58352008-11-07 00:24:53 +0000759 rc = read32bits(pPager->jfd, jrnlOff+12, &pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000760 if( rc ) return rc;
drh98c58352008-11-07 00:24:53 +0000761 if( (pPager->sectorSize & (pPager->sectorSize-1))!=0
762 || pPager->sectorSize>0x1000000 ){
763 return SQLITE_DONE;
764 }
danielk197776572402004-06-25 02:38:54 +0000765
766 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000767 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000768}
769
770
771/*
772** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000773** pPager at the current location. The master journal name must be the last
774** thing written to a journal file. If the pager is in full-sync mode, the
775** journal file descriptor is advanced to the next sector boundary before
776** anything is written. The format is:
777**
778** + 4 bytes: PAGER_MJ_PGNO.
779** + N bytes: length of master journal name.
780** + 4 bytes: N
781** + 4 bytes: Master journal name checksum.
782** + 8 bytes: aJournalMagic[].
783**
784** The master journal page checksum is the sum of the bytes in the master
785** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000786**
787** If zMaster is a NULL pointer (occurs for a single database transaction),
788** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000789*/
790static int writeMasterJournal(Pager *pPager, const char *zMaster){
791 int rc;
792 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000793 int i;
danielk197762079062007-08-15 17:08:46 +0000794 i64 jrnlOff;
danielk1977df2566a2008-05-07 19:11:03 +0000795 i64 jrnlSize;
drh97b57482006-01-10 20:32:32 +0000796 u32 cksum = 0;
797 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000798
danielk1977b3175382008-10-17 18:51:52 +0000799 if( !zMaster || pPager->setMaster ) return SQLITE_OK;
800 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ) return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000801 pPager->setMaster = 1;
802
803 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000804 for(i=0; i<len; i++){
805 cksum += zMaster[i];
806 }
danielk197776572402004-06-25 02:38:54 +0000807
808 /* If in full-sync mode, advance to the next disk sector before writing
809 ** the master journal name. This is in case the previous page written to
810 ** the journal has already been synced.
811 */
812 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000813 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000814 }
danielk197762079062007-08-15 17:08:46 +0000815 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000816 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000817
danielk197762079062007-08-15 17:08:46 +0000818 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000819 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000820 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000821
danielk197762079062007-08-15 17:08:46 +0000822 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000823 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000824 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000825
drh97b57482006-01-10 20:32:32 +0000826 put32bits(zBuf, len);
827 put32bits(&zBuf[4], cksum);
828 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000829 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +0000830 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +0000831 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +0000832
833 /* If the pager is in peristent-journal mode, then the physical
834 ** journal-file may extend past the end of the master-journal name
835 ** and 8 bytes of magic data just written to the file. This is
836 ** dangerous because the code to rollback a hot-journal file
837 ** will not be able to find the master-journal name to determine
838 ** whether or not the journal is hot.
839 **
840 ** Easiest thing to do in this scenario is to truncate the journal
841 ** file to the required size.
842 */
843 if( (rc==SQLITE_OK)
844 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
845 && jrnlSize>jrnlOff
846 ){
847 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
848 }
danielk197776572402004-06-25 02:38:54 +0000849 return rc;
850}
851
852/*
drhed7c8552001-04-11 14:29:21 +0000853** Find a page in the hash table given its page number. Return
854** a pointer to the page or NULL if not found.
855*/
drhd9b02572001-04-15 00:37:09 +0000856static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000857 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000858 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
drhed7c8552001-04-11 14:29:21 +0000859 return p;
860}
861
862/*
danielk1977e180dd92007-04-05 17:15:52 +0000863** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +0000864** sets the state of the pager back to what it was when it was first
865** opened. Any outstanding pages are invalidated and subsequent attempts
866** to access those pages will likely result in a coredump.
867*/
drhd9b02572001-04-15 00:37:09 +0000868static void pager_reset(Pager *pPager){
danielk1977efaaf572006-01-16 11:29:19 +0000869 if( pPager->errCode ) return;
danielk19778c0a7912008-08-20 14:49:23 +0000870 sqlite3PcacheClear(pPager->pPCache);
danielk1977e277be02007-03-23 18:12:06 +0000871}
872
873/*
danielk1977ae72d982007-10-03 08:46:44 +0000874** Unlock the database file.
875**
876** If the pager is currently in error state, discard the contents of
877** the cache and reset the Pager structure internal state. If there is
878** an open journal-file, then the next time a shared-lock is obtained
879** on the pager file (by this or any other process), it will be
880** treated as a hot-journal and rolled back.
881*/
882static void pager_unlock(Pager *pPager){
883 if( !pPager->exclusiveMode ){
danielk1977b3175382008-10-17 18:51:52 +0000884 int rc = osUnlock(pPager->fd, NO_LOCK);
885 if( rc ) pPager->errCode = rc;
886 pPager->dbSize = -1;
887 IOTRACE(("UNLOCK %p\n", pPager))
danielk1977ae72d982007-10-03 08:46:44 +0000888
danielk1977b3175382008-10-17 18:51:52 +0000889 /* Always close the journal file when dropping the database lock.
890 ** Otherwise, another connection with journal_mode=delete might
891 ** delete the file out from under us.
892 */
893 if( pPager->journalOpen ){
894 sqlite3OsClose(pPager->jfd);
895 pPager->journalOpen = 0;
896 sqlite3BitvecDestroy(pPager->pInJournal);
897 pPager->pInJournal = 0;
898 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
899 pPager->pAlwaysRollback = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000900 }
901
danielk1977b3175382008-10-17 18:51:52 +0000902 /* If Pager.errCode is set, the contents of the pager cache cannot be
903 ** trusted. Now that the pager file is unlocked, the contents of the
904 ** cache can be discarded and the error code safely cleared.
905 */
906 if( pPager->errCode ){
907 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
908 pager_reset(pPager);
909 if( pPager->stmtOpen ){
910 sqlite3OsClose(pPager->stfd);
911 sqlite3BitvecDestroy(pPager->pInStmt);
912 pPager->pInStmt = 0;
913 }
914 pPager->stmtOpen = 0;
915 pPager->stmtInUse = 0;
916 pPager->journalOff = 0;
917 pPager->journalStarted = 0;
918 pPager->stmtAutoopen = 0;
919 pPager->origDbSize = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000920 }
danielk1977b3175382008-10-17 18:51:52 +0000921
922 pPager->state = PAGER_UNLOCK;
923 pPager->changeCountDone = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000924 }
925}
926
927/*
928** Execute a rollback if a transaction is active and unlock the
929** database file. If the pager has already entered the error state,
930** do not attempt the rollback.
931*/
932static void pagerUnlockAndRollback(Pager *p){
danielk1977ae72d982007-10-03 08:46:44 +0000933 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +0000934 sqlite3BeginBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +0000935 sqlite3PagerRollback(p);
danielk19772d1d86f2008-06-20 14:59:51 +0000936 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +0000937 }
938 pager_unlock(p);
danielk1977ae72d982007-10-03 08:46:44 +0000939}
940
941/*
drh80e35f42007-03-30 14:06:34 +0000942** This routine ends a transaction. A transaction is ended by either
943** a COMMIT or a ROLLBACK.
944**
drhed7c8552001-04-11 14:29:21 +0000945** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +0000946** a RESERVED or EXCLUSIVE lock on the database. This routine will release
947** the database lock and acquires a SHARED lock in its place if that is
948** the appropriate thing to do. Release locks usually is appropriate,
949** unless we are in exclusive access mode or unless this is a
950** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
951**
952** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +0000953**
954** TODO: Consider keeping the journal file open for temporary databases.
955** This might give a performance improvement on windows where opening
956** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +0000957*/
danielk1977df2566a2008-05-07 19:11:03 +0000958static int pager_end_transaction(Pager *pPager, int hasMaster){
danielk197741483462007-03-24 16:45:04 +0000959 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +0000960 int rc2 = SQLITE_OK;
drha6abd042004-06-09 17:37:22 +0000961 if( pPager->state<PAGER_RESERVED ){
962 return SQLITE_OK;
963 }
danielk19773b8a05f2007-03-19 17:44:26 +0000964 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +0000965 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +0000966 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +0000967 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +0000968 }
drhda47d772002-12-02 04:25:19 +0000969 if( pPager->journalOpen ){
danielk1977b3175382008-10-17 18:51:52 +0000970 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
971 int isMemoryJournal = sqlite3IsMemJournal(pPager->jfd);
972 sqlite3OsClose(pPager->jfd);
973 pPager->journalOpen = 0;
974 if( !isMemoryJournal ){
975 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
976 }
977 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE
drh04335882008-09-26 21:08:08 +0000978 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){
979 pPager->journalOff = 0;
980 pPager->journalStarted = 0;
981 }else if( pPager->exclusiveMode
danielk197793f7af92008-05-09 16:57:50 +0000982 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
983 ){
984 rc = zeroJournalHdr(pPager, hasMaster);
985 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +0000986 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +0000987 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +0000988 }else{
drh04335882008-09-26 21:08:08 +0000989 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || rc );
danielk1977b4b47412007-08-17 15:53:36 +0000990 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +0000991 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +0000992 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +0000993 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +0000994 }
danielk197741483462007-03-24 16:45:04 +0000995 }
drhf5e7bb52008-02-18 14:47:33 +0000996 sqlite3BitvecDestroy(pPager->pInJournal);
997 pPager->pInJournal = 0;
danielk1977a1fa00d2008-08-27 15:16:33 +0000998 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
999 pPager->pAlwaysRollback = 0;
danielk19778c0a7912008-08-20 14:49:23 +00001000 sqlite3PcacheCleanAll(pPager->pPCache);
danielk19773c407372005-02-15 02:54:14 +00001001#ifdef SQLITE_CHECK_PAGES
danielk19778c0a7912008-08-20 14:49:23 +00001002 sqlite3PcacheIterate(pPager->pPCache, pager_set_pagehash);
danielk19773c407372005-02-15 02:54:14 +00001003#endif
drhb3df2e12008-09-17 20:06:26 +00001004 sqlite3PcacheClearFlags(pPager->pPCache,
1005 PGHDR_IN_JOURNAL | PGHDR_NEED_SYNC
danielk19778c0a7912008-08-20 14:49:23 +00001006 );
danielk1977ef317ab2004-06-23 10:43:10 +00001007 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001008 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001009 }else{
drhf5e7bb52008-02-18 14:47:33 +00001010 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001011 }
danielk1977979f38e2007-03-27 16:19:51 +00001012
danielk197741483462007-03-24 16:45:04 +00001013 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001014 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001015 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001016 }else if( pPager->state==PAGER_SYNCED ){
1017 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001018 }
danielk1977334cdb62007-03-26 08:05:12 +00001019 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001020 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001021 pPager->needSync = 0;
danielk19778c0a7912008-08-20 14:49:23 +00001022 /* lruListSetFirstSynced(pPager); */
danielk1977b3175382008-10-17 18:51:52 +00001023 if( !MEMDB ){
1024 pPager->dbSize = -1;
1025 }
drhd138c012008-05-13 00:58:18 +00001026 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001027
1028 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001029}
1030
drhed7c8552001-04-11 14:29:21 +00001031/*
drh968af522003-02-11 14:55:40 +00001032** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001033**
1034** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001035** random initial value and the page number. We experimented with
1036** a checksum of the entire data, but that was found to be too slow.
1037**
1038** Note that the page number is stored at the beginning of data and
1039** the checksum is stored at the end. This is important. If journal
1040** corruption occurs due to a power failure, the most likely scenario
1041** is that one end or the other of the record will be changed. It is
1042** much less likely that the two ends of the journal record will be
1043** correct and the middle be corrupt. Thus, this "checksum" scheme,
1044** though fast and simple, catches the mostly likely kind of corruption.
1045**
1046** FIX ME: Consider adding every 200th (or so) byte of the data to the
1047** checksum. That way if a single page spans 3 or more disk sectors and
1048** only the middle sector is corrupt, we will still have a reasonable
1049** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001050*/
drh74161702006-02-24 02:53:49 +00001051static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001052 u32 cksum = pPager->cksumInit;
1053 int i = pPager->pageSize-200;
1054 while( i>0 ){
1055 cksum += aData[i];
1056 i -= 200;
1057 }
drh968af522003-02-11 14:55:40 +00001058 return cksum;
1059}
1060
drh605ad8f2006-05-03 23:34:05 +00001061/* Forward declaration */
1062static void makeClean(PgHdr*);
1063
drh968af522003-02-11 14:55:40 +00001064/*
drhfa86c412002-02-02 15:01:15 +00001065** Read a single page from the journal file opened on file descriptor
1066** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001067**
drhc13148f2008-08-27 18:03:20 +00001068** The isMainJrnl flag is true if this is the main rollback journal and
1069** false for the statement journal. The main rollback journal uses
1070** checksums - the statement journal does not.
drhfa86c412002-02-02 15:01:15 +00001071*/
danielk197762079062007-08-15 17:08:46 +00001072static int pager_playback_one_page(
drhc13148f2008-08-27 18:03:20 +00001073 Pager *pPager, /* The pager being played back */
1074 sqlite3_file *jfd, /* The file that is the journal being rolled back */
1075 i64 offset, /* Offset of the page within the journal */
1076 int isMainJrnl /* True for main rollback journal. False for Stmt jrnl */
danielk197762079062007-08-15 17:08:46 +00001077){
drhfa86c412002-02-02 15:01:15 +00001078 int rc;
drhae2b40c2004-06-09 19:03:54 +00001079 PgHdr *pPg; /* An existing page in the cache */
1080 Pgno pgno; /* The page number of a page in journal */
1081 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001082 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001083
drhc13148f2008-08-27 18:03:20 +00001084 /* isMainJrnl should be true for the main journal and false for
drh96362842005-03-20 22:47:56 +00001085 ** statement journals. Verify that this is always the case
1086 */
drhc13148f2008-08-27 18:03:20 +00001087 assert( jfd == (isMainJrnl ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001088 assert( aData );
drh96362842005-03-20 22:47:56 +00001089
danielk197762079062007-08-15 17:08:46 +00001090 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001091 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001092 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001093 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001094 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001095
drh968af522003-02-11 14:55:40 +00001096 /* Sanity checking on the page. This is more important that I originally
1097 ** thought. If a power failure occurs while the journal is being written,
1098 ** it could cause invalid data to be written into the journal. We need to
1099 ** detect this invalid data (with high probability) and ignore it.
1100 */
danielk197775edc162004-06-26 01:48:18 +00001101 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001102 return SQLITE_DONE;
1103 }
drhae2b40c2004-06-09 19:03:54 +00001104 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001105 return SQLITE_OK;
1106 }
drhc13148f2008-08-27 18:03:20 +00001107 if( isMainJrnl ){
danielk197762079062007-08-15 17:08:46 +00001108 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001109 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001110 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001111 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001112 return SQLITE_DONE;
1113 }
1114 }
drhfa86c412002-02-02 15:01:15 +00001115
danielk1977aa5ccdf2004-06-14 05:10:42 +00001116 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001117
1118 /* If the pager is in RESERVED state, then there must be a copy of this
1119 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001120 ** not the database file. The page is left marked dirty in this case.
1121 **
danielk19772df71c72007-05-24 07:22:42 +00001122 ** An exception to the above rule: If the database is in no-sync mode
1123 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001124 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1125 ** during a Movepage() call, then the page may not be in the cache
1126 ** either. So the condition described in the above paragraph is not
1127 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001128 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001129 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1130 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001131 **
1132 ** Ticket #1171: The statement journal might contain page content that is
1133 ** different from the page content at the start of the transaction.
1134 ** This occurs when a page is changed prior to the start of a statement
1135 ** then changed again within the statement. When rolling back such a
1136 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001137 ** for certain that original page contents are synced into the main rollback
1138 ** journal. Otherwise, a power loss might leave modified data in the
1139 ** database file without an entry in the rollback journal that can
1140 ** restore the database to its original form. Two conditions must be
1141 ** met before writing to the database files. (1) the database must be
1142 ** locked. (2) we know that the original page content is fully synced
1143 ** in the main journal either because the page is not in cache or else
1144 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001145 **
1146 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1147 ** is possible to fail a statement on a database that does not yet exist.
1148 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001149 */
drhae2b40c2004-06-09 19:03:54 +00001150 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001151 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1152 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
danielk19778c0a7912008-08-20 14:49:23 +00001153 if( (pPager->state>=PAGER_EXCLUSIVE)
1154 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
1155 && (pPager->fd->pMethods)
1156 ){
drh281b21d2008-08-22 12:57:08 +00001157 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
1158 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
danielk1977a3f3a5f2004-06-10 04:32:16 +00001159 }
drhfa86c412002-02-02 15:01:15 +00001160 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001161 /* No page should ever be explicitly rolled back that is in use, except
1162 ** for page 1 which is held in use in order to keep the lock on the
1163 ** database active. However such a page may be rolled back as a result
1164 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001165 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001166 */
drhb6f41482004-05-14 01:58:11 +00001167 void *pData;
danielk19778c0a7912008-08-20 14:49:23 +00001168 pData = pPg->pData;
drhae2b40c2004-06-09 19:03:54 +00001169 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001170 if( pPager->xReiniter ){
danielk1977eaa06f62008-09-18 17:34:44 +00001171 pPager->xReiniter(pPg);
drhde647132004-05-07 17:57:49 +00001172 }
drhc13148f2008-08-27 18:03:20 +00001173 if( isMainJrnl ) makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00001174#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001175 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001176#endif
drh86a88112007-04-16 15:02:19 +00001177 /* If this was page 1, then restore the value of Pager.dbFileVers.
1178 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001179 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001180 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001181 }
drh86a88112007-04-16 15:02:19 +00001182
1183 /* Decode the page just read from disk */
1184 CODEC1(pPager, pData, pPg->pgno, 3);
danielk19778c0a7912008-08-20 14:49:23 +00001185 sqlite3PcacheRelease(pPg);
drhfa86c412002-02-02 15:01:15 +00001186 }
1187 return rc;
1188}
1189
1190/*
danielk197713adf8a2004-06-03 16:08:41 +00001191** Parameter zMaster is the name of a master journal file. A single journal
1192** file that referred to the master journal file has just been rolled back.
1193** This routine checks if it is possible to delete the master journal file,
1194** and does so if it is.
drh726de592004-06-10 23:35:50 +00001195**
danielk197765839c62007-08-30 08:08:17 +00001196** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1197** available for use within this function.
1198**
1199**
drh726de592004-06-10 23:35:50 +00001200** The master journal file contains the names of all child journals.
1201** To tell if a master journal can be deleted, check to each of the
1202** children. If all children are either missing or do not refer to
1203** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001204*/
danielk1977b4b47412007-08-17 15:53:36 +00001205static int pager_delmaster(Pager *pPager, const char *zMaster){
1206 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001207 int rc;
1208 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001209 sqlite3_file *pMaster;
1210 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001211 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001212 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001213
1214 /* Open the master journal file exclusively in case some other process
1215 ** is running this routine also. Not that it makes too much difference.
1216 */
drhe5ae5732008-06-15 02:51:47 +00001217 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001218 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001219 if( !pMaster ){
1220 rc = SQLITE_NOMEM;
1221 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001222 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1223 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001224 }
danielk197713adf8a2004-06-03 16:08:41 +00001225 if( rc!=SQLITE_OK ) goto delmaster_out;
1226 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001227
1228 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001229 if( rc!=SQLITE_OK ) goto delmaster_out;
1230
1231 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001232 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001233 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001234 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001235
1236 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001237 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001238 */
drhe5ae5732008-06-15 02:51:47 +00001239 zMasterJournal = (char *)sqlite3Malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001240 if( !zMasterJournal ){
1241 rc = SQLITE_NOMEM;
1242 goto delmaster_out;
1243 }
danielk197765839c62007-08-30 08:08:17 +00001244 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001245 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001246 if( rc!=SQLITE_OK ) goto delmaster_out;
1247
danielk19775865e3d2004-06-14 06:03:57 +00001248 zJournal = zMasterJournal;
1249 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001250 int exists;
1251 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1252 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001253 goto delmaster_out;
1254 }
danielk1977861f7452008-06-05 11:39:11 +00001255 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001256 /* One of the journals pointed to by the master journal exists.
1257 ** Open it and check if it points at the master journal. If
1258 ** so, return without deleting the master journal file.
1259 */
drh3b7b78b2004-11-24 01:16:43 +00001260 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001261 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1262 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001263 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001264 goto delmaster_out;
1265 }
danielk197713adf8a2004-06-03 16:08:41 +00001266
danielk197765839c62007-08-30 08:08:17 +00001267 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001268 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001269 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001270 goto delmaster_out;
1271 }
danielk197776572402004-06-25 02:38:54 +00001272
danielk197765839c62007-08-30 08:08:17 +00001273 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001274 if( c ){
danielk197776572402004-06-25 02:38:54 +00001275 /* We have a match. Do not delete the master journal file. */
1276 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001277 }
1278 }
danielk19775865e3d2004-06-14 06:03:57 +00001279 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001280 }
1281 }
1282
danielk1977fee2d252007-08-18 10:59:19 +00001283 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001284
1285delmaster_out:
1286 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001287 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001288 }
1289 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001290 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001291 }
danielk1977b4b47412007-08-17 15:53:36 +00001292 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001293 return rc;
1294}
1295
drha6abd042004-06-09 17:37:22 +00001296
danielk1977e180dd92007-04-05 17:15:52 +00001297static void pager_truncate_cache(Pager *pPager);
1298
drha6abd042004-06-09 17:37:22 +00001299/*
drhcb4c40b2004-08-18 19:09:43 +00001300** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001301** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001302**
1303** Might might be the case that the file on disk is smaller than nPage.
1304** This can happen, for example, if we are in the middle of a transaction
1305** which has extended the file size and the new pages are still all held
1306** in cache, then an INSERT or UPDATE does a statement rollback. Some
1307** operating system implementations can get confused if you try to
1308** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001309** so detect this case and write a single zero byte to the end of the new
1310** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001311*/
1312static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001313 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001314 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001315 i64 currentSize, newSize;
1316 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1317 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001318 if( rc==SQLITE_OK && currentSize!=newSize ){
1319 if( currentSize>newSize ){
1320 rc = sqlite3OsTruncate(pPager->fd, newSize);
1321 }else{
1322 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1323 }
drh7fe3f7e2007-11-29 18:44:27 +00001324 }
danielk1977e180dd92007-04-05 17:15:52 +00001325 }
1326 if( rc==SQLITE_OK ){
1327 pPager->dbSize = nPage;
1328 pager_truncate_cache(pPager);
1329 }
1330 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001331}
1332
1333/*
drhc80f0582007-05-01 16:59:48 +00001334** Set the sectorSize for the given pager.
1335**
drh334c80d2008-03-28 17:41:13 +00001336** The sector size is at least as big as the sector size reported
1337** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001338*/
1339static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001340 assert(pPager->fd->pMethods||pPager->tempFile);
1341 if( !pPager->tempFile ){
1342 /* Sector size doesn't matter for temporary files. Also, the file
1343 ** may not have been opened yet, in whcih case the OsSectorSize()
1344 ** call will segfault.
1345 */
1346 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1347 }
drh334c80d2008-03-28 17:41:13 +00001348 if( pPager->sectorSize<512 ){
1349 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001350 }
1351}
1352
1353/*
drhed7c8552001-04-11 14:29:21 +00001354** Playback the journal and thus restore the database file to
1355** the state it was in before we started making changes.
1356**
drh34e79ce2004-02-08 06:05:46 +00001357** The journal file format is as follows:
1358**
drhae2b40c2004-06-09 19:03:54 +00001359** (1) 8 byte prefix. A copy of aJournalMagic[].
1360** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001361** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001362** number of page records from the journal size.
1363** (3) 4 byte big-endian integer which is the initial value for the
1364** sanity checksum.
1365** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001366** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001367** (5) 4 byte big-endian integer which is the sector size. The header
1368** is this many bytes in size.
1369** (6) 4 byte big-endian integer which is the page case.
1370** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001371** name. The value may be zero (indicate that there is no master
1372** journal.)
drh334c80d2008-03-28 17:41:13 +00001373** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001374** and might be shorter than the value read from (5). If the first byte
1375** of the name is \000 then there is no master journal. The master
1376** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001377** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001378** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001379** + pPager->pageSize bytes of data.
1380** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001381**
drh334c80d2008-03-28 17:41:13 +00001382** When we speak of the journal header, we mean the first 8 items above.
1383** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001384**
1385** Call the value from the second bullet "nRec". nRec is the number of
1386** valid page entries in the journal. In most cases, you can compute the
1387** value of nRec from the size of the journal file. But if a power
1388** failure occurred while the journal was being written, it could be the
1389** case that the size of the journal file had already been increased but
1390** the extra entries had not yet made it safely to disk. In such a case,
1391** the value of nRec computed from the file size would be too large. For
1392** that reason, we always use the nRec value in the header.
1393**
1394** If the nRec value is 0xffffffff it means that nRec should be computed
1395** from the file size. This value is used when the user selects the
1396** no-sync option for the journal. A power failure could lead to corruption
1397** in this case. But for things like temporary table (which will be
1398** deleted when the power is restored) we don't care.
1399**
drhd9b02572001-04-15 00:37:09 +00001400** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001401** journal file then all pages up to the first corrupted page are rolled
1402** back (or no pages if the journal header is corrupted). The journal file
1403** is then deleted and SQLITE_OK returned, just as if no corruption had
1404** been encountered.
1405**
1406** If an I/O or malloc() error occurs, the journal-file is not deleted
1407** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001408*/
danielk1977e277be02007-03-23 18:12:06 +00001409static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001410 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001411 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001412 u32 nRec; /* Number of Records in the journal */
shane0b8d2762008-07-22 05:18:00 +00001413 u32 u; /* Unsigned loop counter */
drhd9b02572001-04-15 00:37:09 +00001414 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001415 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001416 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001417 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001418
drhc3a64ba2001-11-22 00:01:27 +00001419 /* Figure out how many records are in the journal. Abort early if
1420 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001421 */
drh8cfbf082001-09-19 13:22:39 +00001422 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001423 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001424 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001425 goto end_playback;
1426 }
drh240c5792004-02-08 00:40:52 +00001427
danielk197776572402004-06-25 02:38:54 +00001428 /* Read the master journal name from the journal, if it is present.
1429 ** If a master journal file name is specified, but the file is not
1430 ** present on disk, then the journal is not hot and does not need to be
1431 ** played back.
drh240c5792004-02-08 00:40:52 +00001432 */
danielk197765839c62007-08-30 08:08:17 +00001433 zMaster = pPager->pTmpSpace;
1434 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001435 if( rc==SQLITE_OK && zMaster[0] ){
1436 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001437 }
danielk197765839c62007-08-30 08:08:17 +00001438 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001439 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001440 goto end_playback;
1441 }
1442 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001443
danielk197776572402004-06-25 02:38:54 +00001444 /* This loop terminates either when the readJournalHdr() call returns
1445 ** SQLITE_DONE or an IO error occurs. */
1446 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001447
danielk197776572402004-06-25 02:38:54 +00001448 /* Read the next journal header from the journal file. If there are
1449 ** not enough bytes left in the journal file for a complete header, or
1450 ** it is corrupted, then a process must of failed while writing it.
1451 ** This indicates nothing more needs to be rolled back.
1452 */
1453 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1454 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001455 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001456 rc = SQLITE_OK;
1457 }
danielk197776572402004-06-25 02:38:54 +00001458 goto end_playback;
1459 }
1460
1461 /* If nRec is 0xffffffff, then this journal was created by a process
1462 ** working in no-sync mode. This means that the rest of the journal
1463 ** file consists of pages, there are no more journal headers. Compute
1464 ** the value of nRec based on this assumption.
1465 */
1466 if( nRec==0xffffffff ){
1467 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1468 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1469 }
1470
danielk1977e277be02007-03-23 18:12:06 +00001471 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001472 ** process and if this is the final header in the journal, then it means
1473 ** that this part of the journal was being filled but has not yet been
1474 ** synced to disk. Compute the number of pages based on the remaining
1475 ** size of the file.
1476 **
1477 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001478 */
drh8940f4e2007-08-11 00:26:20 +00001479 if( nRec==0 && !isHot &&
1480 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001481 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1482 }
1483
danielk197776572402004-06-25 02:38:54 +00001484 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001485 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001486 */
danielk1977e180dd92007-04-05 17:15:52 +00001487 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001488 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001489 if( rc!=SQLITE_OK ){
1490 goto end_playback;
1491 }
danielk197776572402004-06-25 02:38:54 +00001492 }
1493
danielk197776572402004-06-25 02:38:54 +00001494 /* Copy original pages out of the journal and back into the database file.
1495 */
shane0b8d2762008-07-22 05:18:00 +00001496 for(u=0; u<nRec; u++){
danielk197762079062007-08-15 17:08:46 +00001497 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001498 if( rc!=SQLITE_OK ){
1499 if( rc==SQLITE_DONE ){
1500 rc = SQLITE_OK;
1501 pPager->journalOff = szJ;
1502 break;
1503 }else{
drha9625ea2008-09-03 00:08:29 +00001504 /* If we are unable to rollback, then the database is probably
1505 ** going to end up being corrupt. It is corrupt to us, anyhow.
1506 ** Perhaps the next process to come along can fix it....
1507 */
danielk197798c21902008-09-23 16:41:29 +00001508 rc = SQLITE_CORRUPT_BKPT;
danielk197776572402004-06-25 02:38:54 +00001509 goto end_playback;
1510 }
1511 }
drh968af522003-02-11 14:55:40 +00001512 }
drhed7c8552001-04-11 14:29:21 +00001513 }
drh580eeaf2006-02-24 03:09:37 +00001514 /*NOTREACHED*/
1515 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001516
1517end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001518 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001519 zMaster = pPager->pTmpSpace;
1520 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1521 }
1522 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001523 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001524 }
danielk1977c56774e2008-10-07 11:51:20 +00001525 if( rc==SQLITE_OK && zMaster[0] && res ){
danielk1977979f38e2007-03-27 16:19:51 +00001526 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001527 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001528 */
danielk197765839c62007-08-30 08:08:17 +00001529 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001530 }
danielk197776572402004-06-25 02:38:54 +00001531
1532 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001533 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001534 ** value. Reset it to the correct value for this process.
1535 */
drhc80f0582007-05-01 16:59:48 +00001536 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001537 return rc;
drhed7c8552001-04-11 14:29:21 +00001538}
1539
1540/*
drhac69b052004-05-12 13:30:07 +00001541** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001542**
1543** This is similar to playing back the transaction journal but with
1544** a few extra twists.
1545**
drh663fc632002-02-02 18:49:19 +00001546** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001547** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001548** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001549**
drhac69b052004-05-12 13:30:07 +00001550** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001551** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001552** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001553*/
drh3aac2dd2004-04-26 14:10:20 +00001554static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001555 i64 szJ; /* Size of the full journal */
1556 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001557 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001558 int i; /* Loop counter */
1559 int rc;
1560
danielk197776572402004-06-25 02:38:54 +00001561 szJ = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001562
danielk19774099f6e2007-03-19 11:25:20 +00001563 /* Set hdrOff to be the offset just after the end of the last journal
1564 ** page written before the first journal-header for this statement
1565 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001566 ** header was written.
1567 */
1568 hdrOff = pPager->stmtHdrOff;
1569 assert( pPager->fullSync || !hdrOff );
1570 if( !hdrOff ){
1571 hdrOff = szJ;
1572 }
1573
drhfa86c412002-02-02 15:01:15 +00001574 /* Truncate the database back to its original size.
1575 */
danielk1977e180dd92007-04-05 17:15:52 +00001576 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001577 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001578
drhac69b052004-05-12 13:30:07 +00001579 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001580 */
drhac69b052004-05-12 13:30:07 +00001581 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001582 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001583
drhac69b052004-05-12 13:30:07 +00001584 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001585 ** database file. Note that the statement journal omits checksums from
1586 ** each record since power-failure recovery is not important to statement
1587 ** journals.
drhfa86c412002-02-02 15:01:15 +00001588 */
danielk197762079062007-08-15 17:08:46 +00001589 for(i=0; i<nRec; i++){
1590 i64 offset = i*(4+pPager->pageSize);
1591 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001592 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001593 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001594 }
1595
danielk197776572402004-06-25 02:38:54 +00001596 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1597 ** was the size of the journal file when this statement was started, so
1598 ** everything after that needs to be rolled back, either into the
1599 ** database, the memory cache, or both.
1600 **
1601 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1602 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001603 */
danielk197776572402004-06-25 02:38:54 +00001604 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001605 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001606 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001607 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001608 assert( rc!=SQLITE_DONE );
1609 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1610 }
1611
1612 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001613 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001614 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001615 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001616 if( rc!=SQLITE_OK ){
1617 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001618 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001619 }
danielk1977f0113002006-01-24 12:09:17 +00001620 if( nJRec==0 ){
1621 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001622 }
danielk1977f0113002006-01-24 12:09:17 +00001623 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001624 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001625 assert( rc!=SQLITE_DONE );
1626 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1627 }
drhfa86c412002-02-02 15:01:15 +00001628 }
danielk197776572402004-06-25 02:38:54 +00001629
1630 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001631
drh3aac2dd2004-04-26 14:10:20 +00001632end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001633 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001634 pPager->journalOff = szJ;
1635 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001636 }
1637 return rc;
1638}
1639
1640/*
drhf57b14a2001-09-14 18:54:08 +00001641** Change the maximum number of in-memory pages that are allowed.
1642*/
danielk19773b8a05f2007-03-19 17:44:26 +00001643void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +00001644 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001645}
1646
1647/*
drh973b6e32003-02-12 14:09:42 +00001648** Adjust the robustness of the database to damage due to OS crashes
1649** or power failures by changing the number of syncs()s when writing
1650** the rollback journal. There are three levels:
1651**
drh054889e2005-11-30 03:20:31 +00001652** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001653** for temporary and transient files.
1654**
1655** NORMAL The journal is synced once before writes begin on the
1656** database. This is normally adequate protection, but
1657** it is theoretically possible, though very unlikely,
1658** that an inopertune power failure could leave the journal
1659** in a state which would cause damage to the database
1660** when it is rolled back.
1661**
1662** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001663** database (with some additional information - the nRec field
1664** of the journal header - being written in between the two
1665** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001666** single disk sector is atomic, then this mode provides
1667** assurance that the journal will not be corrupted to the
1668** point of causing damage to the database during rollback.
1669**
1670** Numeric values associated with these states are OFF==1, NORMAL=2,
1671** and FULL=3.
1672*/
danielk197793758c82005-01-21 08:13:14 +00001673#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh281b21d2008-08-22 12:57:08 +00001674void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
danielk1977b3175382008-10-17 18:51:52 +00001675 pPager->noSync = level==1 || pPager->tempFile;
drh973b6e32003-02-12 14:09:42 +00001676 pPager->fullSync = level==3 && !pPager->tempFile;
drh281b21d2008-08-22 12:57:08 +00001677 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001678 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001679}
danielk197793758c82005-01-21 08:13:14 +00001680#endif
drh973b6e32003-02-12 14:09:42 +00001681
1682/*
drhaf6df112005-06-07 02:12:30 +00001683** The following global variable is incremented whenever the library
1684** attempts to open a temporary file. This information is used for
1685** testing and analysis only.
1686*/
drh0f7eb612006-08-08 13:51:43 +00001687#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001688int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001689#endif
drhaf6df112005-06-07 02:12:30 +00001690
1691/*
drh3f56e6e2007-03-15 01:16:47 +00001692** Open a temporary file.
1693**
1694** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001695** other error code if we fail. The OS will automatically delete the temporary
1696** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001697*/
danielk1977b4b47412007-08-17 15:53:36 +00001698static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00001699 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00001700 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00001701 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00001702){
drhfa86c412002-02-02 15:01:15 +00001703 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001704
drh0f7eb612006-08-08 13:51:43 +00001705#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001706 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001707#endif
danielk1977b4b47412007-08-17 15:53:36 +00001708
drh33f4e022007-09-03 15:19:34 +00001709 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1710 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00001711 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00001712 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00001713 return rc;
1714}
1715
danielk1977a858aa22008-08-22 16:22:17 +00001716static int pagerStress(void *,PgHdr *);
danielk19778c0a7912008-08-20 14:49:23 +00001717
drhfa86c412002-02-02 15:01:15 +00001718/*
drhed7c8552001-04-11 14:29:21 +00001719** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001720** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001721** the first call to sqlite3PagerGet() and is only held open until the
1722** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001723**
drh6446c4d2001-12-15 14:22:18 +00001724** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00001725** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00001726** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001727**
1728** If zFilename is ":memory:" then all information is held in cache.
1729** It is never written to disk. This can be used to implement an
1730** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001731*/
danielk19773b8a05f2007-03-19 17:44:26 +00001732int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001733 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001734 Pager **ppPager, /* Return the Pager structure here */
1735 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001736 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00001737 int flags, /* flags controlling this file */
1738 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00001739){
danielk1977b4b47412007-08-17 15:53:36 +00001740 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001741 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001742 int rc = SQLITE_OK;
1743 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001744 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001745 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001746 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001747 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1748 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977b3175382008-10-17 18:51:52 +00001749 int journalFileSize;
danielk19778c0a7912008-08-20 14:49:23 +00001750 int pcacheSize = sqlite3PcacheSize();
drhfacf0302008-06-17 15:12:00 +00001751 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
danielk197717b90b52008-06-06 11:11:25 +00001752 char *zPathname = 0;
1753 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001754
danielk1977b3175382008-10-17 18:51:52 +00001755 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
1756 journalFileSize = sqlite3JournalSize(pVfs);
1757 }else{
1758 journalFileSize = sqlite3MemJournalSize();
1759 }
1760
drh86f8c192007-08-22 00:39:19 +00001761 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001762 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001763
danielk197717b90b52008-06-06 11:11:25 +00001764 /* Compute and store the full pathname in an allocated buffer pointed
1765 ** to by zPathname, length nPathname. Or, if this is a temporary file,
1766 ** leave both nPathname and zPathname set to 0.
1767 */
drh1cc8c442007-08-24 16:08:29 +00001768 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00001769 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001770 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00001771 if( zPathname==0 ){
1772 return SQLITE_NOMEM;
1773 }
drh1cc8c442007-08-24 16:08:29 +00001774#ifndef SQLITE_OMIT_MEMORYDB
1775 if( strcmp(zFilename,":memory:")==0 ){
1776 memDb = 1;
1777 zPathname[0] = 0;
1778 }else
1779#endif
1780 {
danielk1977adfb9b02007-09-17 07:02:56 +00001781 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00001782 }
danielk197717b90b52008-06-06 11:11:25 +00001783 if( rc!=SQLITE_OK ){
1784 sqlite3_free(zPathname);
1785 return rc;
1786 }
1787 nPathname = strlen(zPathname);
drhae28c012007-08-24 16:29:23 +00001788 }
drh99b90c32008-03-17 13:50:58 +00001789
danielk1977b4b47412007-08-17 15:53:36 +00001790 /* Allocate memory for the pager structure */
1791 pPager = sqlite3MallocZero(
1792 sizeof(*pPager) + /* Pager structure */
danielk19778c0a7912008-08-20 14:49:23 +00001793 pcacheSize + /* PCache object */
danielk1977c7b60172007-08-22 11:22:03 +00001794 journalFileSize + /* The journal file structure */
danielk1977b3175382008-10-17 18:51:52 +00001795 pVfs->szOsFile + /* The main db file */
1796 journalFileSize * 2 + /* The two journal files */
danielk197717b90b52008-06-06 11:11:25 +00001797 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00001798 );
1799 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00001800 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00001801 return SQLITE_NOMEM;
1802 }
danielk19778c0a7912008-08-20 14:49:23 +00001803 pPager->pPCache = (PCache *)&pPager[1];
1804 pPtr = ((u8 *)&pPager[1]) + pcacheSize;
drh33f4e022007-09-03 15:19:34 +00001805 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00001806 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977b3175382008-10-17 18:51:52 +00001807 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile];
1808 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile+journalFileSize];
1809 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile+2*journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00001810 pPager->zDirectory = &pPager->zFilename[nPathname+1];
1811 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00001812 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00001813 if( zPathname ){
1814 memcpy(pPager->zFilename, zPathname, nPathname+1);
1815 sqlite3_free(zPathname);
1816 }
danielk1977b4b47412007-08-17 15:53:36 +00001817
drh153c62c2007-08-24 03:51:33 +00001818 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00001819 */
drhae28c012007-08-24 16:29:23 +00001820 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00001821 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
1822 rc = SQLITE_CANTOPEN;
1823 }else{
drh1cc8c442007-08-24 16:08:29 +00001824 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00001825 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
1826 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00001827 readOnly = (fout&SQLITE_OPEN_READONLY);
1828
1829 /* If the file was successfully opened for read/write access,
1830 ** choose a default page size in case we have to create the
1831 ** database file. The default page size is the maximum of:
1832 **
1833 ** + SQLITE_DEFAULT_PAGE_SIZE,
1834 ** + The value returned by sqlite3OsSectorSize()
1835 ** + The largest page size that can be written atomically.
1836 */
1837 if( rc==SQLITE_OK && !readOnly ){
1838 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
drhfacf0302008-06-17 15:12:00 +00001839 if( szPageDflt<iSectorSize ){
1840 szPageDflt = iSectorSize;
drh1cc8c442007-08-24 16:08:29 +00001841 }
danielk19779663b8f2007-08-24 11:52:28 +00001842#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00001843 {
1844 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
1845 int ii;
1846 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
1847 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
1848 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
drhfacf0302008-06-17 15:12:00 +00001849 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
1850 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00001851 }
danielk1977b4b47412007-08-17 15:53:36 +00001852 }
drh1cc8c442007-08-24 16:08:29 +00001853#endif
drhfacf0302008-06-17 15:12:00 +00001854 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
1855 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
drh1cc8c442007-08-24 16:08:29 +00001856 }
danielk19778def5ea2004-06-16 10:39:52 +00001857 }
drhac69b052004-05-12 13:30:07 +00001858 }
danielk1977b3175382008-10-17 18:51:52 +00001859 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00001860 /* If a temporary file is requested, it is not opened immediately.
1861 ** In this case we accept the default page size and delay actually
1862 ** opening the file until the first call to OsWrite().
danielk1977b3175382008-10-17 18:51:52 +00001863 **
1864 ** This branch is also run for an in-memory database. An in-memory
1865 ** database is the same as a temp-file that is never written out to
1866 ** disk and uses an in-memory rollback journal.
danielk19777a2b1ee2007-08-21 14:27:01 +00001867 */
danielk19777a2b1ee2007-08-21 14:27:01 +00001868 tempFile = 1;
1869 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00001870 }
danielk1977aef0bf62005-12-30 16:28:01 +00001871
danielk1977b4b47412007-08-17 15:53:36 +00001872 if( pPager && rc==SQLITE_OK ){
drhfacf0302008-06-17 15:12:00 +00001873 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00001874 }
danielk1977aef0bf62005-12-30 16:28:01 +00001875
drh153c62c2007-08-24 03:51:33 +00001876 /* If an error occured in either of the blocks above.
1877 ** Free the Pager structure and close the file.
1878 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00001879 ** any Pager.errMask variables.
1880 */
danielk1977b4b47412007-08-17 15:53:36 +00001881 if( !pPager || !pPager->pTmpSpace ){
1882 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00001883 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00001884 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001885 }
danielk19778c0a7912008-08-20 14:49:23 +00001886 nExtra = FORCE_ALIGNMENT(nExtra);
danielk197771d5d2c2008-09-29 11:49:47 +00001887 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
drh41d30272008-08-20 21:47:45 +00001888 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
danielk1977aef0bf62005-12-30 16:28:01 +00001889
drh153c62c2007-08-24 03:51:33 +00001890 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
1891 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00001892
danielk1977b4b47412007-08-17 15:53:36 +00001893 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00001894 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00001895 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00001896 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001897
drh99b90c32008-03-17 13:50:58 +00001898 /* Fill in Pager.zJournal[] */
danielk197717b90b52008-06-06 11:11:25 +00001899 if( zPathname ){
1900 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
1901 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
1902 }else{
1903 pPager->zJournal = 0;
1904 }
danielk1977b4b47412007-08-17 15:53:36 +00001905
drh3b59a5c2006-01-15 20:28:28 +00001906 /* pPager->journalOpen = 0; */
drhb3df2e12008-09-17 20:06:26 +00001907 pPager->useJournal = useJournal;
drh7bec5052005-02-06 02:45:41 +00001908 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001909 /* pPager->stmtOpen = 0; */
1910 /* pPager->stmtInUse = 0; */
1911 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00001912 pPager->dbSize = memDb-1;
drhfacf0302008-06-17 15:12:00 +00001913 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00001914 /* pPager->stmtSize = 0; */
1915 /* pPager->stmtJSize = 0; */
1916 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00001917 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00001918 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00001919 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00001920 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00001921 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00001922 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00001923 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
1924 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
1925 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
1926 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00001927 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001928 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001929 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00001930 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00001931 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00001932 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00001933 /* pPager->pFirst = 0; */
1934 /* pPager->pFirstSynced = 0; */
1935 /* pPager->pLast = 0; */
danielk19778c0a7912008-08-20 14:49:23 +00001936 pPager->nExtra = nExtra;
danielk1977b53e4962008-06-04 06:45:59 +00001937 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk1977b3175382008-10-17 18:51:52 +00001938 assert(pPager->fd->pMethods||tempFile);
1939 setSectorSize(pPager);
1940 if( memDb ){
1941 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
danielk1977b4721172007-03-19 05:54:48 +00001942 }
drh3b59a5c2006-01-15 20:28:28 +00001943 /* pPager->pBusyHandler = 0; */
1944 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00001945 *ppPager = pPager;
1946 return SQLITE_OK;
1947}
1948
1949/*
drh90f5ecb2004-07-22 01:19:35 +00001950** Set the busy handler function.
1951*/
danielk19773b8a05f2007-03-19 17:44:26 +00001952void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00001953 pPager->pBusyHandler = pBusyHandler;
1954}
1955
1956/*
drha6abd042004-06-09 17:37:22 +00001957** Set the reinitializer for this pager. If not NULL, the reinitializer
1958** is called when the content of a page in cache is restored to its original
1959** value as a result of a rollback. The callback gives higher-level code
1960** an opportunity to restore the EXTRA section to agree with the restored
1961** page data.
1962*/
danielk1977eaa06f62008-09-18 17:34:44 +00001963void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
drha6abd042004-06-09 17:37:22 +00001964 pPager->xReiniter = xReinit;
1965}
1966
1967/*
danielk1977a1644fd2007-08-29 12:31:25 +00001968** Set the page size to *pPageSize. If the suggest new page size is
1969** inappropriate, then an alternative page size is set to that
1970** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00001971*/
danielk1977a1644fd2007-08-29 12:31:25 +00001972int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
danielk19771357d9f2008-09-16 05:12:24 +00001973 int rc = pPager->errCode;
1974 if( rc==SQLITE_OK ){
1975 u16 pageSize = *pPageSize;
1976 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
1977 if( pageSize && pageSize!=pPager->pageSize
1978 && (pPager->memDb==0 || pPager->dbSize==0)
1979 && sqlite3PcacheRefCount(pPager->pPCache)==0
1980 ){
1981 char *pNew = (char *)sqlite3PageMalloc(pageSize);
1982 if( !pNew ){
1983 rc = SQLITE_NOMEM;
1984 }else{
1985 pager_reset(pPager);
1986 pPager->pageSize = pageSize;
1987 if( !pPager->memDb ) setSectorSize(pPager);
1988 sqlite3PageFree(pPager->pTmpSpace);
1989 pPager->pTmpSpace = pNew;
1990 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
1991 }
danielk1977a1644fd2007-08-29 12:31:25 +00001992 }
danielk19771357d9f2008-09-16 05:12:24 +00001993 *pPageSize = pPager->pageSize;
drh1c7880e2005-05-20 20:01:55 +00001994 }
danielk1977a1644fd2007-08-29 12:31:25 +00001995 return rc;
drh90f5ecb2004-07-22 01:19:35 +00001996}
1997
1998/*
drh26b79942007-11-28 16:19:56 +00001999** Return a pointer to the "temporary page" buffer held internally
2000** by the pager. This is a buffer that is big enough to hold the
2001** entire content of a database page. This buffer is used internally
2002** during rollback and will be overwritten whenever a rollback
2003** occurs. But other modules are free to use it too, as long as
2004** no rollbacks are happening.
2005*/
2006void *sqlite3PagerTempSpace(Pager *pPager){
2007 return pPager->pTmpSpace;
2008}
2009
2010/*
drhf8e632b2007-05-08 14:51:36 +00002011** Attempt to set the maximum database page count if mxPage is positive.
2012** Make no changes if mxPage is zero or negative. And never reduce the
2013** maximum page count below the current size of the database.
2014**
2015** Regardless of mxPage, return the current maximum page count.
2016*/
2017int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2018 if( mxPage>0 ){
2019 pPager->mxPgno = mxPage;
2020 }
danielk1977ad0132d2008-06-07 08:58:22 +00002021 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002022 return pPager->mxPgno;
2023}
2024
2025/*
drhc9ac5ca2005-11-04 22:03:30 +00002026** The following set of routines are used to disable the simulated
2027** I/O error mechanism. These routines are used to avoid simulated
2028** errors in places where we do not care about errors.
2029**
2030** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2031** and generate no code.
2032*/
2033#ifdef SQLITE_TEST
2034extern int sqlite3_io_error_pending;
2035extern int sqlite3_io_error_hit;
2036static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002037void disable_simulated_io_errors(void){
2038 saved_cnt = sqlite3_io_error_pending;
2039 sqlite3_io_error_pending = -1;
2040}
2041void enable_simulated_io_errors(void){
2042 sqlite3_io_error_pending = saved_cnt;
2043}
2044#else
drh152410f2005-11-05 15:11:22 +00002045# define disable_simulated_io_errors()
2046# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002047#endif
2048
2049/*
drh90f5ecb2004-07-22 01:19:35 +00002050** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002051** that pDest points to.
2052**
2053** No error checking is done. The rational for this is that this function
2054** may be called even if the file does not exist or contain a header. In
2055** these cases sqlite3OsRead() will return an error, to which the correct
2056** response is to zero the memory at pDest and continue. A real IO error
2057** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002058*/
danielk19773b8a05f2007-03-19 17:44:26 +00002059int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002060 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002061 memset(pDest, 0, N);
danielk1977b3175382008-10-17 18:51:52 +00002062 assert(pPager->fd->pMethods||pPager->tempFile);
danielk19777a2b1ee2007-08-21 14:27:01 +00002063 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002064 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002065 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002066 if( rc==SQLITE_IOERR_SHORT_READ ){
2067 rc = SQLITE_OK;
2068 }
drh90f5ecb2004-07-22 01:19:35 +00002069 }
drh551b7732006-11-06 21:20:25 +00002070 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002071}
2072
2073/*
drh5e00f6c2001-09-13 13:46:56 +00002074** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002075** pPager.
2076**
2077** If the PENDING_BYTE lies on the page directly after the end of the
2078** file, then consider this page part of the file too. For example, if
2079** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2080** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002081*/
danielk1977ad0132d2008-06-07 08:58:22 +00002082int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk19777a2b1ee2007-08-21 14:27:01 +00002083 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002084 int rc;
drhd9b02572001-04-15 00:37:09 +00002085 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002086 if( pPager->errCode ){
danielk19778c0a7912008-08-20 14:49:23 +00002087 rc = pPager->errCode;
danielk19778c0a7912008-08-20 14:49:23 +00002088 return rc;
drha7aea3d2007-03-15 12:51:16 +00002089 }
drhed7c8552001-04-11 14:29:21 +00002090 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002091 n = pPager->dbSize;
2092 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002093 assert(pPager->fd->pMethods||pPager->tempFile);
2094 if( (pPager->fd->pMethods)
2095 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002096 pager_error(pPager, rc);
danielk1977ad0132d2008-06-07 08:58:22 +00002097 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002098 }
2099 if( n>0 && n<pPager->pageSize ){
2100 n = 1;
2101 }else{
2102 n /= pPager->pageSize;
2103 }
2104 if( pPager->state!=PAGER_UNLOCK ){
2105 pPager->dbSize = n;
2106 }
drhed7c8552001-04-11 14:29:21 +00002107 }
danielk197715f411d2005-09-16 10:18:45 +00002108 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002109 n++;
2110 }
drhf8e632b2007-05-08 14:51:36 +00002111 if( n>pPager->mxPgno ){
2112 pPager->mxPgno = n;
2113 }
danielk1977ad0132d2008-06-07 08:58:22 +00002114 if( pnPage ){
2115 *pnPage = n;
2116 }
2117 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002118}
2119
2120/*
drhf7c57532003-04-25 13:22:51 +00002121** Forward declaration
2122*/
danielk197776572402004-06-25 02:38:54 +00002123static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002124
2125/*
danielk1977e180dd92007-04-05 17:15:52 +00002126** This routine is used to truncate the cache when a database
2127** is truncated. Drop from the cache all pages whose pgno is
2128** larger than pPager->dbSize and is unreferenced.
2129**
drhac69b052004-05-12 13:30:07 +00002130** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002131**
2132** Actually, at the point this routine is called, it would be
2133** an error to have a referenced page. But rather than delete
2134** that page and guarantee a subsequent segfault, it seems better
2135** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002136*/
danielk1977e180dd92007-04-05 17:15:52 +00002137static void pager_truncate_cache(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00002138 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
drhac69b052004-05-12 13:30:07 +00002139}
2140
drhf7c57532003-04-25 13:22:51 +00002141/*
danielk197717221812005-02-15 03:38:05 +00002142** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002143** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002144** false or until the lock succeeds.
2145**
2146** Return SQLITE_OK on success and an error code if we cannot obtain
2147** the lock.
2148*/
2149static int pager_wait_on_lock(Pager *pPager, int locktype){
2150 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002151
2152 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002153 assert( PAGER_SHARED==SHARED_LOCK );
2154 assert( PAGER_RESERVED==RESERVED_LOCK );
2155 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002156
2157 /* If the file is currently unlocked then the size must be unknown */
danielk1977b3175382008-10-17 18:51:52 +00002158 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 );
drh1aa2d8b2007-01-03 15:34:29 +00002159
danielk197717221812005-02-15 03:38:05 +00002160 if( pPager->state>=locktype ){
2161 rc = SQLITE_OK;
2162 }else{
drhbefdf832008-03-14 19:33:05 +00002163 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002164 do {
drh054889e2005-11-30 03:20:31 +00002165 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002166 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002167 if( rc==SQLITE_OK ){
2168 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002169 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002170 }
2171 }
2172 return rc;
2173}
2174
2175/*
drhf7c57532003-04-25 13:22:51 +00002176** Truncate the file to the number of pages specified.
2177*/
danielk19773b8a05f2007-03-19 17:44:26 +00002178int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
danielk19778c0a7912008-08-20 14:49:23 +00002179 int rc = SQLITE_OK;
danielk1977b3175382008-10-17 18:51:52 +00002180 assert( pPager->state>=PAGER_SHARED );
danielk19778c0a7912008-08-20 14:49:23 +00002181
danielk1977ad0132d2008-06-07 08:58:22 +00002182 sqlite3PagerPagecount(pPager, 0);
danielk1977efaaf572006-01-16 11:29:19 +00002183 if( pPager->errCode ){
2184 rc = pPager->errCode;
danielk19778c0a7912008-08-20 14:49:23 +00002185 }else if( nPage<(unsigned)pPager->dbSize ){
danielk1977b3175382008-10-17 18:51:52 +00002186 rc = syncJournal(pPager);
2187 if( rc==SQLITE_OK ){
2188 /* Get an exclusive lock on the database before truncating. */
2189 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2190 }
2191 if( rc==SQLITE_OK ){
2192 rc = pager_truncate(pPager, nPage);
danielk19778c0a7912008-08-20 14:49:23 +00002193 }
danielk1977369f27e2004-06-15 11:40:04 +00002194 }
danielk197717221812005-02-15 03:38:05 +00002195
drhf7c57532003-04-25 13:22:51 +00002196 return rc;
2197}
2198
2199/*
drhed7c8552001-04-11 14:29:21 +00002200** Shutdown the page cache. Free all memory and close all files.
2201**
2202** If a transaction was in progress when this routine is called, that
2203** transaction is rolled back. All outstanding pages are invalidated
2204** and their memory is freed. Any attempt to use a page associated
2205** with this page cache after this function returns will likely
2206** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002207**
2208** This function always succeeds. If a transaction is active an attempt
2209** is made to roll it back. If an error occurs during the rollback
2210** a hot journal may be left in the filesystem but no error is returned
2211** to the caller.
drhed7c8552001-04-11 14:29:21 +00002212*/
danielk19773b8a05f2007-03-19 17:44:26 +00002213int sqlite3PagerClose(Pager *pPager){
danielk197713f72992005-12-18 08:51:22 +00002214
drhbafda092007-01-03 23:36:22 +00002215 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002216 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002217 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002218 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002219 pager_reset(pPager);
danielk1977b3175382008-10-17 18:51:52 +00002220 if( !MEMDB ){
2221 pagerUnlockAndRollback(pPager);
2222 }
drhbafda092007-01-03 23:36:22 +00002223 enable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002224 sqlite3EndBenignMalloc();
drh4f0c5872007-03-26 22:05:01 +00002225 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002226 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002227 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002228 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002229 }
drhf5e7bb52008-02-18 14:47:33 +00002230 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977a1fa00d2008-08-27 15:16:33 +00002231 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
danielk1977e94ddc92005-03-21 03:53:38 +00002232 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002233 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002234 }
danielk1977b4b47412007-08-17 15:53:36 +00002235 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002236 /* Temp files are automatically deleted by the OS
2237 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002238 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002239 ** }
2240 */
danielk1977aca790a2005-01-13 11:07:52 +00002241
drhfacf0302008-06-17 15:12:00 +00002242 sqlite3PageFree(pPager->pTmpSpace);
danielk19778c0a7912008-08-20 14:49:23 +00002243 sqlite3PcacheClose(pPager->pPCache);
drh17435752007-08-16 04:30:38 +00002244 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002245 return SQLITE_OK;
2246}
2247
drh87cc3b32007-05-08 21:45:27 +00002248#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002249/*
drh5e00f6c2001-09-13 13:46:56 +00002250** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002251*/
danielk19773b8a05f2007-03-19 17:44:26 +00002252Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002253 return p->pgno;
2254}
drh87cc3b32007-05-08 21:45:27 +00002255#endif
drhed7c8552001-04-11 14:29:21 +00002256
2257/*
drhdf0b3b02001-06-23 11:36:20 +00002258** Increment the reference count for a page. The input pointer is
2259** a reference to the page data.
2260*/
danielk19773b8a05f2007-03-19 17:44:26 +00002261int sqlite3PagerRef(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002262 sqlite3PcacheRef(pPg);
drh8c42ca92001-06-22 19:15:00 +00002263 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002264}
2265
2266/*
drh34e79ce2004-02-08 06:05:46 +00002267** Sync the journal. In other words, make sure all the pages that have
2268** been written to the journal have actually reached the surface of the
2269** disk. It is not safe to modify the original database file until after
2270** the journal has been synced. If the original database is modified before
2271** the journal is synced and a power failure occurs, the unsynced journal
2272** data would be lost and we would be unable to completely rollback the
2273** database changes. Database corruption would occur.
2274**
2275** This routine also updates the nRec field in the header of the journal.
2276** (See comments on the pager_playback() routine for additional information.)
2277** If the sync mode is FULL, two syncs will occur. First the whole journal
2278** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002279**
drh34e79ce2004-02-08 06:05:46 +00002280** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002281** after a power failure, so no sync occurs.
2282**
2283** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2284** the database is stored, then OsSync() is never called on the journal
2285** file. In this case all that is required is to update the nRec field in
2286** the journal header.
drhfa86c412002-02-02 15:01:15 +00002287**
drh34e79ce2004-02-08 06:05:46 +00002288** This routine clears the needSync field of every page current held in
2289** memory.
drh50e5dad2001-09-15 00:57:28 +00002290*/
danielk197776572402004-06-25 02:38:54 +00002291static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002292 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002293
2294 /* Sync the journal before modifying the main database
2295 ** (assuming there is a journal and it needs to be synced.)
2296 */
danielk197776572402004-06-25 02:38:54 +00002297 if( pPager->needSync ){
danielk1977b3175382008-10-17 18:51:52 +00002298 assert( !pPager->tempFile );
2299 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
danielk19774cd2cd52007-08-23 14:48:23 +00002300 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002301 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002302
danielk19774cd2cd52007-08-23 14:48:23 +00002303 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002304 /* Write the nRec value into the journal file header. If in
2305 ** full-synchronous mode, sync the journal first. This ensures that
2306 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002307 ** it as a candidate for rollback.
2308 **
2309 ** This is not required if the persistent media supports the
2310 ** SAFE_APPEND property. Because in this case it is not possible
2311 ** for garbage data to be appended to the file, the nRec field
2312 ** is populated with 0xFFFFFFFF when the journal header is written
2313 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002314 */
danielk19774cd2cd52007-08-23 14:48:23 +00002315 i64 jrnlOff;
2316 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002317 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002318 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002319 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002320 if( rc!=0 ) return rc;
2321 }
danielk197713adf8a2004-06-03 16:08:41 +00002322
danielk197762079062007-08-15 17:08:46 +00002323 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2324 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2325 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002326 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002327 }
danielk19774cd2cd52007-08-23 14:48:23 +00002328 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2329 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2330 IOTRACE(("JSYNC %p\n", pPager))
2331 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2332 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2333 );
2334 if( rc!=0 ) return rc;
2335 }
drhdb48ee02003-01-16 13:42:43 +00002336 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002337 }
drh50e5dad2001-09-15 00:57:28 +00002338 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002339
2340 /* Erase the needSync flag from every page.
2341 */
drhb3df2e12008-09-17 20:06:26 +00002342 sqlite3PcacheClearFlags(pPager->pPCache, PGHDR_NEED_SYNC);
drh50e5dad2001-09-15 00:57:28 +00002343 }
drh03eb96a2002-11-10 23:32:56 +00002344
drh341eae82003-01-21 02:39:36 +00002345#ifndef NDEBUG
2346 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2347 ** flag must also be clear for all pages. Verify that this
2348 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002349 */
drh341eae82003-01-21 02:39:36 +00002350 else{
danielk19778c0a7912008-08-20 14:49:23 +00002351 sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_NEED_SYNC);
drh03eb96a2002-11-10 23:32:56 +00002352 }
drh341eae82003-01-21 02:39:36 +00002353#endif
drhdb48ee02003-01-16 13:42:43 +00002354
drh81a20f22001-10-12 17:30:04 +00002355 return rc;
drh50e5dad2001-09-15 00:57:28 +00002356}
2357
2358/*
drh2554f8b2003-01-22 01:26:44 +00002359** Given a list of pages (connected by the PgHdr.pDirty pointer) write
danielk1977a858aa22008-08-22 16:22:17 +00002360** every one of those pages out to the database file. No calls are made
2361** to the page-cache to mark the pages as clean. It is the responsibility
2362** of the caller to use PcacheCleanAll() or PcacheMakeClean() to mark
2363** the pages as clean.
drh2554f8b2003-01-22 01:26:44 +00002364*/
2365static int pager_write_pagelist(PgHdr *pList){
2366 Pager *pPager;
2367 int rc;
2368
2369 if( pList==0 ) return SQLITE_OK;
2370 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002371
2372 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2373 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002374 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002375 **
drha6abd042004-06-09 17:37:22 +00002376 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2377 ** through an intermediate state PENDING. A PENDING lock prevents new
2378 ** readers from attaching to the database but is unsufficient for us to
2379 ** write. The idea of a PENDING lock is to prevent new readers from
2380 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002381 **
drha6abd042004-06-09 17:37:22 +00002382 ** While the pager is in the RESERVED state, the original database file
2383 ** is unchanged and we can rollback without having to playback the
2384 ** journal into the original database file. Once we transition to
2385 ** EXCLUSIVE, it means the database file has been changed and any rollback
2386 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002387 */
drh684917c2004-10-05 02:41:42 +00002388 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002389 if( rc!=SQLITE_OK ){
2390 return rc;
2391 }
2392
drh2554f8b2003-01-22 01:26:44 +00002393 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002394
2395 /* If the file has not yet been opened, open it now. */
2396 if( !pPager->fd->pMethods ){
2397 assert(pPager->tempFile);
danielk197717b90b52008-06-06 11:11:25 +00002398 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002399 if( rc ) return rc;
2400 }
2401
danielk1977687566d2004-11-02 12:56:41 +00002402 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002403 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002404 ** make the file smaller (presumably by auto-vacuum code). Do not write
2405 ** any such pages to the file.
2406 */
danielk197733e32162008-08-23 18:53:08 +00002407 if( pList->pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
danielk197762079062007-08-15 17:08:46 +00002408 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002409 char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002410 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2411 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002412 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002413 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002414 PAGER_INCR(sqlite3_pager_writedb_count);
2415 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002416 if( pList->pgno==1 ){
2417 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2418 }
danielk1977687566d2004-11-02 12:56:41 +00002419 }
2420#ifndef NDEBUG
2421 else{
drh4f0c5872007-03-26 22:05:01 +00002422 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002423 }
2424#endif
drh2554f8b2003-01-22 01:26:44 +00002425 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002426#ifdef SQLITE_CHECK_PAGES
2427 pList->pageHash = pager_pagehash(pList);
2428#endif
drh2554f8b2003-01-22 01:26:44 +00002429 pList = pList->pDirty;
2430 }
danielk19778c0a7912008-08-20 14:49:23 +00002431
drh2554f8b2003-01-22 01:26:44 +00002432 return SQLITE_OK;
2433}
2434
2435/*
danielk19778c0a7912008-08-20 14:49:23 +00002436** This function is called by the pcache layer when it has reached some
2437** soft memory limit. The argument is a pointer to a purgeable Pager
2438** object. This function attempts to make a single dirty page that has no
2439** outstanding references (if one exists) clean so that it can be recycled
2440** by the pcache layer.
drh2554f8b2003-01-22 01:26:44 +00002441*/
danielk1977a858aa22008-08-22 16:22:17 +00002442static int pagerStress(void *p, PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002443 Pager *pPager = (Pager *)p;
danielk19778c0a7912008-08-20 14:49:23 +00002444 int rc = SQLITE_OK;
drh8f2e9a12008-02-26 14:46:04 +00002445
danielk19778c200142008-08-25 07:12:28 +00002446 if( pPager->doNotSync ){
2447 return SQLITE_OK;
2448 }
2449
danielk1977a858aa22008-08-22 16:22:17 +00002450 assert( pPg->flags&PGHDR_DIRTY );
2451 if( pPager->errCode==SQLITE_OK ){
2452 if( pPg->flags&PGHDR_NEED_SYNC ){
2453 rc = syncJournal(pPager);
2454 if( rc==SQLITE_OK && pPager->fullSync &&
danielk1977b3175382008-10-17 18:51:52 +00002455 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
danielk1977a858aa22008-08-22 16:22:17 +00002456 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
2457 ){
2458 pPager->nRec = 0;
2459 rc = writeJournalHdr(pPager);
danielk19778c0a7912008-08-20 14:49:23 +00002460 }
danielk19778c0a7912008-08-20 14:49:23 +00002461 }
danielk1977a858aa22008-08-22 16:22:17 +00002462 if( rc==SQLITE_OK ){
2463 pPg->pDirty = 0;
2464 rc = pager_write_pagelist(pPg);
2465 }
2466 if( rc!=SQLITE_OK ){
2467 pager_error(pPager, rc);
2468 }
2469 }
2470
2471 if( rc==SQLITE_OK ){
2472 sqlite3PcacheMakeClean(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00002473 }
2474 return rc;
drh2554f8b2003-01-22 01:26:44 +00002475}
2476
danielk19778c0a7912008-08-20 14:49:23 +00002477
drh2554f8b2003-01-22 01:26:44 +00002478/*
drh19db9352008-03-27 22:42:51 +00002479** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00002480** A hot journal is one that needs to be played back.
2481**
2482** If the current size of the database file is 0 but a journal file
2483** exists, that is probably an old journal left over from a prior
2484** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00002485**
2486** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00002487**
2488** This routine does not open the journal file to examine its
2489** content. Hence, the journal might contain the name of a master
2490** journal file that has been deleted, and hence not be hot. Or
2491** the header of the journal might be zeroed out. This routine
2492** does not discover these cases of a non-hot journal - if the
2493** journal file exists and is not empty this routine assumes it
2494** is hot. The pager_playback() routine will discover that the
2495** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00002496*/
danielk1977d300b8a2008-08-01 10:50:23 +00002497static int hasHotJournal(Pager *pPager, int *pExists){
danielk1977b4b47412007-08-17 15:53:36 +00002498 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977d300b8a2008-08-01 10:50:23 +00002499 int rc = SQLITE_OK;
drh0a846f92008-08-25 17:23:29 +00002500 int exists;
2501 int locked;
2502 assert( pPager!=0 );
2503 assert( pPager->useJournal );
2504 assert( pPager->fd->pMethods );
danielk1977d300b8a2008-08-01 10:50:23 +00002505 *pExists = 0;
drh0a846f92008-08-25 17:23:29 +00002506 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
2507 if( rc==SQLITE_OK && exists ){
2508 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
2509 }
2510 if( rc==SQLITE_OK && exists && !locked ){
2511 int nPage;
2512 rc = sqlite3PagerPagecount(pPager, &nPage);
2513 if( rc==SQLITE_OK ){
2514 if( nPage==0 ){
2515 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2516 }else{
2517 *pExists = 1;
danielk1977861f7452008-06-05 11:39:11 +00002518 }
2519 }
drhbb5f18d2007-04-06 18:23:17 +00002520 }
danielk1977d300b8a2008-08-01 10:50:23 +00002521 return rc;
drh165ffe92005-03-15 17:09:30 +00002522}
2523
2524/*
danielk1977e180dd92007-04-05 17:15:52 +00002525** Read the content of page pPg out of the database file.
2526*/
2527static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2528 int rc;
danielk197762079062007-08-15 17:08:46 +00002529 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002530 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002531 assert(pPager->fd->pMethods||pPager->tempFile);
2532 if( !pPager->fd->pMethods ){
2533 return SQLITE_IOERR_SHORT_READ;
2534 }
danielk197762079062007-08-15 17:08:46 +00002535 offset = (pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002536 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002537 PAGER_INCR(sqlite3_pager_readdb_count);
2538 PAGER_INCR(pPager->nRead);
2539 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002540 if( pgno==1 ){
danielk19778c0a7912008-08-20 14:49:23 +00002541 memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24],
drh86a88112007-04-16 15:02:19 +00002542 sizeof(pPager->dbFileVers));
2543 }
danielk197730849522008-08-21 04:35:18 +00002544 CODEC1(pPager, pPg->pData, pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00002545 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
2546 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00002547 return rc;
2548}
2549
2550
2551/*
danielk1977e277be02007-03-23 18:12:06 +00002552** This function is called to obtain the shared lock required before
2553** data may be read from the pager cache. If the shared lock has already
2554** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002555**
2556** Immediately after obtaining the shared lock (if required), this function
2557** checks for a hot-journal file. If one is found, an emergency rollback
2558** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002559*/
2560static int pagerSharedLock(Pager *pPager){
2561 int rc = SQLITE_OK;
danielk1977d300b8a2008-08-01 10:50:23 +00002562 int isErrorReset = 0;
danielk1977e277be02007-03-23 18:12:06 +00002563
danielk1977ae72d982007-10-03 08:46:44 +00002564 /* If this database is opened for exclusive access, has no outstanding
2565 ** page references and is in an error-state, now is the chance to clear
2566 ** the error. Discard the contents of the pager-cache and treat any
2567 ** open journal file as a hot-journal.
2568 */
danielk19778c0a7912008-08-20 14:49:23 +00002569 if( !MEMDB && pPager->exclusiveMode
2570 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
2571 ){
danielk1977ae72d982007-10-03 08:46:44 +00002572 if( pPager->journalOpen ){
danielk1977d300b8a2008-08-01 10:50:23 +00002573 isErrorReset = 1;
danielk1977ae72d982007-10-03 08:46:44 +00002574 }
danielk1977ae72d982007-10-03 08:46:44 +00002575 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00002576 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00002577 }
2578
2579 /* If the pager is still in an error state, do not proceed. The error
2580 ** state will be cleared at some point in the future when all page
2581 ** references are dropped and the cache can be discarded.
2582 */
2583 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
2584 return pPager->errCode;
2585 }
2586
danielk1977d300b8a2008-08-01 10:50:23 +00002587 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
danielk1977b4b47412007-08-17 15:53:36 +00002588 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977b3175382008-10-17 18:51:52 +00002589 int isHotJournal;
shane049fc212008-10-22 16:26:47 +00002590 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00002591 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
2592 if( !pPager->noReadlock ){
2593 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2594 if( rc!=SQLITE_OK ){
2595 assert( pPager->state==PAGER_UNLOCK );
2596 return pager_error(pPager, rc);
danielk1977e277be02007-03-23 18:12:06 +00002597 }
danielk1977b3175382008-10-17 18:51:52 +00002598 assert( pPager->state>=SHARED_LOCK );
2599 }
2600
2601 /* If a journal file exists, and there is no RESERVED lock on the
2602 ** database file, then it either needs to be played back or deleted.
2603 */
2604 if( !isErrorReset ){
2605 rc = hasHotJournal(pPager, &isHotJournal);
2606 if( rc!=SQLITE_OK ){
2607 goto failed;
2608 }
2609 }
2610 if( isErrorReset || isHotJournal ){
2611 /* Get an EXCLUSIVE lock on the database file. At this point it is
2612 ** important that a RESERVED lock is not obtained on the way to the
2613 ** EXCLUSIVE lock. If it were, another process might open the
2614 ** database file, detect the RESERVED lock, and conclude that the
2615 ** database is safe to read while this process is still rolling it
2616 ** back.
2617 **
2618 ** Because the intermediate RESERVED lock is not requested, the
2619 ** second process will get to this point in the code and fail to
2620 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00002621 */
danielk1977b3175382008-10-17 18:51:52 +00002622 if( pPager->state<EXCLUSIVE_LOCK ){
2623 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
danielk1977e277be02007-03-23 18:12:06 +00002624 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002625 rc = pager_error(pPager, rc);
2626 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002627 }
danielk1977b3175382008-10-17 18:51:52 +00002628 pPager->state = PAGER_EXCLUSIVE;
2629 }
2630
2631 /* Open the journal for read/write access. This is because in
2632 ** exclusive-access mode the file descriptor will be kept open and
2633 ** possibly used for a transaction later on. On some systems, the
2634 ** OsTruncate() call used in exclusive-access mode also requires
2635 ** a read/write file handle.
2636 */
2637 if( !isErrorReset && pPager->journalOpen==0 ){
2638 int res;
2639 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
2640 if( rc==SQLITE_OK ){
2641 if( res ){
2642 int fout = 0;
2643 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
2644 assert( !pPager->tempFile );
2645 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
2646 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk1977281d8bd2008-10-29 07:01:56 +00002647 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
2648 rc = SQLITE_CANTOPEN;
danielk1977b3175382008-10-17 18:51:52 +00002649 sqlite3OsClose(pPager->jfd);
2650 }
2651 }else{
2652 /* If the journal does not exist, that means some other process
2653 ** has already rolled it back */
2654 rc = SQLITE_BUSY;
2655 }
2656 }
2657 }
2658 if( rc!=SQLITE_OK ){
danielk1977b3175382008-10-17 18:51:52 +00002659 goto failed;
2660 }
2661 pPager->journalOpen = 1;
2662 pPager->journalStarted = 0;
2663 pPager->journalOff = 0;
2664 pPager->setMaster = 0;
2665 pPager->journalHdr = 0;
2666
2667 /* Playback and delete the journal. Drop the database write
2668 ** lock and reacquire the read lock.
2669 */
2670 rc = pager_playback(pPager, 1);
2671 if( rc!=SQLITE_OK ){
2672 rc = pager_error(pPager, rc);
2673 goto failed;
2674 }
2675 assert(pPager->state==PAGER_SHARED ||
2676 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
2677 );
2678 }
2679
2680 if( sqlite3PcachePagecount(pPager->pPCache)>0 ){
2681 /* The shared-lock has just been acquired on the database file
2682 ** and there are already pages in the cache (from a previous
2683 ** read or write transaction). Check to see if the database
2684 ** has been modified. If the database has changed, flush the
2685 ** cache.
2686 **
2687 ** Database changes is detected by looking at 15 bytes beginning
2688 ** at offset 24 into the file. The first 4 of these 16 bytes are
2689 ** a 32-bit counter that is incremented with each change. The
2690 ** other bytes change randomly with each file change when
2691 ** a codec is in use.
2692 **
2693 ** There is a vanishingly small chance that a change will not be
2694 ** detected. The chance of an undetected change is so small that
2695 ** it can be neglected.
2696 */
2697 char dbFileVers[sizeof(pPager->dbFileVers)];
2698 sqlite3PagerPagecount(pPager, 0);
2699
2700 if( pPager->errCode ){
2701 rc = pPager->errCode;
2702 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002703 }
2704
danielk1977b3175382008-10-17 18:51:52 +00002705 if( pPager->dbSize>0 ){
2706 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
2707 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
2708 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002709 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002710 }
danielk1977b3175382008-10-17 18:51:52 +00002711 }else{
2712 memset(dbFileVers, 0, sizeof(dbFileVers));
2713 }
danielk1977e277be02007-03-23 18:12:06 +00002714
danielk1977b3175382008-10-17 18:51:52 +00002715 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
2716 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002717 }
2718 }
danielk1977c5859712007-03-26 12:26:27 +00002719 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
2720 if( pPager->state==PAGER_UNLOCK ){
2721 pPager->state = PAGER_SHARED;
2722 }
danielk1977e277be02007-03-23 18:12:06 +00002723 }
2724
danielk197752b472a2008-05-05 16:23:55 +00002725 failed:
2726 if( rc!=SQLITE_OK ){
2727 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
2728 pager_unlock(pPager);
2729 }
danielk1977e277be02007-03-23 18:12:06 +00002730 return rc;
2731}
2732
2733/*
drhd33d5a82007-04-26 12:11:28 +00002734** Make sure we have the content for a page. If the page was
2735** previously acquired with noContent==1, then the content was
2736** just initialized to zeros instead of being read from disk.
2737** But now we need the real data off of disk. So make sure we
2738** have it. Read it in if we do not have it already.
2739*/
2740static int pager_get_content(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002741 if( pPg->flags&PGHDR_NEED_READ ){
drhd33d5a82007-04-26 12:11:28 +00002742 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
2743 if( rc==SQLITE_OK ){
danielk19778c0a7912008-08-20 14:49:23 +00002744 pPg->flags &= ~PGHDR_NEED_READ;
drhd33d5a82007-04-26 12:11:28 +00002745 }else{
2746 return rc;
2747 }
2748 }
2749 return SQLITE_OK;
2750}
2751
2752/*
danielk19778c0a7912008-08-20 14:49:23 +00002753** If the reference count has reached zero, and the pager is not in the
2754** middle of a write transaction or opened in exclusive mode, unlock it.
2755*/
2756static void pagerUnlockIfUnused(Pager *pPager){
2757 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
2758 && (!pPager->exclusiveMode || pPager->journalOff>0)
2759 ){
2760 pagerUnlockAndRollback(pPager);
2761 }
2762}
2763
2764/*
2765** Drop a page from the cache using sqlite3PcacheDrop().
2766**
2767** If this means there are now no pages with references to them, a rollback
2768** occurs and the lock on the database is removed.
2769*/
2770static void pagerDropPage(DbPage *pPg){
2771 Pager *pPager = pPg->pPager;
2772 sqlite3PcacheDrop(pPg);
2773 pagerUnlockIfUnused(pPager);
2774}
2775
2776/*
drhd9b02572001-04-15 00:37:09 +00002777** Acquire a page.
2778**
drh58a11682001-11-10 13:51:08 +00002779** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00002780** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00002781**
drhd33d5a82007-04-26 12:11:28 +00002782** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00002783** file is smaller than the requested page, then no actual disk
2784** read occurs and the memory image of the page is initialized to
2785** all zeros. The extra data appended to a page is always initialized
2786** to zeros the first time a page is loaded into memory.
2787**
drhd9b02572001-04-15 00:37:09 +00002788** The acquisition might fail for several reasons. In all cases,
2789** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00002790**
drhd33d5a82007-04-26 12:11:28 +00002791** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00002792** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00002793** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00002794** just returns 0. This routine acquires a read-lock the first time it
2795** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00002796** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00002797** or journal files.
drh0787db62007-03-04 13:15:27 +00002798**
drh538f5702007-04-13 02:14:30 +00002799** If noContent is false, the page contents are actually read from disk.
2800** If noContent is true, it means that we do not care about the contents
2801** of the page at this time, so do not do a disk read. Just fill in the
2802** page content with zeros. But mark the fact that we have not read the
2803** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00002804** sqlite3PagerWrite() is called on this page or if this routine is
2805** called again with noContent==0, that means that the content is needed
2806** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00002807*/
danielk197765e0ff32008-09-19 09:14:43 +00002808int sqlite3PagerAcquire(
drh538f5702007-04-13 02:14:30 +00002809 Pager *pPager, /* The pager open on the database file */
2810 Pgno pgno, /* Page number to fetch */
2811 DbPage **ppPage, /* Write a pointer to the page here */
2812 int noContent /* Do not bother reading content from disk if true */
2813){
danielk19778c0a7912008-08-20 14:49:23 +00002814 PgHdr *pPg = 0;
drh165ffe92005-03-15 17:09:30 +00002815 int rc;
drhed7c8552001-04-11 14:29:21 +00002816
danielk19778c0a7912008-08-20 14:49:23 +00002817 assert( pPager->state==PAGER_UNLOCK
2818 || sqlite3PcacheRefCount(pPager->pPCache)>0
danielk1977b3175382008-10-17 18:51:52 +00002819 || pgno==1
danielk19778c0a7912008-08-20 14:49:23 +00002820 );
danielk1977e277be02007-03-23 18:12:06 +00002821
danielk197726836652005-01-17 01:33:13 +00002822 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
2823 ** number greater than this, or zero, is requested.
2824 */
drh267cb322005-09-16 17:16:52 +00002825 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00002826 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00002827 }
2828
drhd9b02572001-04-15 00:37:09 +00002829 /* Make sure we have not hit any critical errors.
2830 */
drh836faa42003-01-11 13:30:57 +00002831 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00002832 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00002833
danielk197713adf8a2004-06-03 16:08:41 +00002834 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00002835 ** on the database file. pagerSharedLock() is a no-op if
2836 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00002837 */
danielk1977e277be02007-03-23 18:12:06 +00002838 rc = pagerSharedLock(pPager);
2839 if( rc!=SQLITE_OK ){
2840 return rc;
drhed7c8552001-04-11 14:29:21 +00002841 }
danielk1977e277be02007-03-23 18:12:06 +00002842 assert( pPager->state!=PAGER_UNLOCK );
2843
danielk19778c0a7912008-08-20 14:49:23 +00002844 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
2845 if( rc!=SQLITE_OK ){
2846 return rc;
2847 }
danielk19778c0a7912008-08-20 14:49:23 +00002848 if( pPg->pPager==0 ){
2849 /* The pager cache has created a new page. Its content needs to
2850 ** be initialized.
2851 */
danielk1977979f38e2007-03-27 16:19:51 +00002852 int nMax;
drh538f5702007-04-13 02:14:30 +00002853 PAGER_INCR(pPager->nMiss);
danielk19778c0a7912008-08-20 14:49:23 +00002854 pPg->pPager = pPager;
2855 if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){
2856 pPg->flags |= PGHDR_IN_JOURNAL;
drhed7c8552001-04-11 14:29:21 +00002857 }
danielk19778c0a7912008-08-20 14:49:23 +00002858 memset(pPg->pExtra, 0, pPager->nExtra);
danielk197724168722007-04-02 05:07:47 +00002859
danielk1977ad0132d2008-06-07 08:58:22 +00002860 rc = sqlite3PagerPagecount(pPager, &nMax);
2861 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002862 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00002863 return rc;
2864 }
danielk197775bab7d2006-01-23 13:09:45 +00002865
danielk1977a1fa00d2008-08-27 15:16:33 +00002866 if( nMax<(int)pgno || MEMDB || noContent ){
drhf8e632b2007-05-08 14:51:36 +00002867 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00002868 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00002869 return SQLITE_FULL;
2870 }
danielk19778c0a7912008-08-20 14:49:23 +00002871 memset(pPg->pData, 0, pPager->pageSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00002872 if( noContent ){
danielk19778c0a7912008-08-20 14:49:23 +00002873 pPg->flags |= PGHDR_NEED_READ;
2874 }
drh538f5702007-04-13 02:14:30 +00002875 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00002876 }else{
danielk1977e180dd92007-04-05 17:15:52 +00002877 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00002878 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
danielk19778c0a7912008-08-20 14:49:23 +00002879 /* sqlite3PagerUnref(pPg); */
2880 pagerDropPage(pPg);
drh551b7732006-11-06 21:20:25 +00002881 return rc;
drh81a20f22001-10-12 17:30:04 +00002882 }
drh306dc212001-05-21 13:45:10 +00002883 }
danielk19773c407372005-02-15 02:54:14 +00002884#ifdef SQLITE_CHECK_PAGES
2885 pPg->pageHash = pager_pagehash(pPg);
2886#endif
drhed7c8552001-04-11 14:29:21 +00002887 }else{
drhd9b02572001-04-15 00:37:09 +00002888 /* The requested page is in the page cache. */
danielk19778c0a7912008-08-20 14:49:23 +00002889 assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00002890 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00002891 if( !noContent ){
2892 rc = pager_get_content(pPg);
2893 if( rc ){
danielk19778c0a7912008-08-20 14:49:23 +00002894 sqlite3PagerUnref(pPg);
drhd33d5a82007-04-26 12:11:28 +00002895 return rc;
2896 }
2897 }
drhed7c8552001-04-11 14:29:21 +00002898 }
danielk19778c0a7912008-08-20 14:49:23 +00002899
danielk19773b8a05f2007-03-19 17:44:26 +00002900 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00002901 return SQLITE_OK;
2902}
danielk19778c0a7912008-08-20 14:49:23 +00002903
drhed7c8552001-04-11 14:29:21 +00002904/*
drh7e3b0a02001-04-28 16:52:40 +00002905** Acquire a page if it is already in the in-memory cache. Do
2906** not read the page from disk. Return a pointer to the page,
2907** or 0 if the page is not in cache.
2908**
danielk19773b8a05f2007-03-19 17:44:26 +00002909** See also sqlite3PagerGet(). The difference between this routine
2910** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00002911** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00002912** returns NULL if the page is not in cache or if a disk I/O error
2913** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00002914*/
danielk19773b8a05f2007-03-19 17:44:26 +00002915DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00002916 PgHdr *pPg = 0;
drh836faa42003-01-11 13:30:57 +00002917 assert( pPager!=0 );
2918 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00002919
danielk19778c0a7912008-08-20 14:49:23 +00002920 if( (pPager->state!=PAGER_UNLOCK)
2921 && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL)
2922 ){
2923 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
danielk1977e277be02007-03-23 18:12:06 +00002924 }
danielk19778c0a7912008-08-20 14:49:23 +00002925
danielk19773b8a05f2007-03-19 17:44:26 +00002926 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00002927}
2928
2929/*
drhed7c8552001-04-11 14:29:21 +00002930** Release a page.
2931**
2932** If the number of references to the page drop to zero, then the
2933** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00002934** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00002935** removed.
2936*/
danielk19773b8a05f2007-03-19 17:44:26 +00002937int sqlite3PagerUnref(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002938 if( pPg ){
2939 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00002940 sqlite3PcacheRelease(pPg);
2941 pagerUnlockIfUnused(pPager);
drhed7c8552001-04-11 14:29:21 +00002942 }
drhd9b02572001-04-15 00:37:09 +00002943 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002944}
2945
2946/*
drha6abd042004-06-09 17:37:22 +00002947** Create a journal file for pPager. There should already be a RESERVED
2948** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00002949**
2950** Return SQLITE_OK if everything. Return an error code and release the
2951** write lock if anything goes wrong.
2952*/
2953static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00002954 sqlite3_vfs *pVfs = pPager->pVfs;
2955 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
2956
drhda47d772002-12-02 04:25:19 +00002957 int rc;
drha6abd042004-06-09 17:37:22 +00002958 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00002959 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00002960 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00002961 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00002962 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00002963 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00002964 rc = SQLITE_NOMEM;
2965 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00002966 }
danielk1977b4b47412007-08-17 15:53:36 +00002967
drhfdc40e92008-04-17 20:59:37 +00002968 if( pPager->journalOpen==0 ){
2969 if( pPager->tempFile ){
2970 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
2971 }else{
2972 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00002973 }
danielk1977b3175382008-10-17 18:51:52 +00002974 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
2975 sqlite3MemJournalOpen(pPager->jfd);
2976 rc = SQLITE_OK;
2977 }else{
drhfdc40e92008-04-17 20:59:37 +00002978#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977b3175382008-10-17 18:51:52 +00002979 rc = sqlite3JournalOpen(
2980 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
2981 );
drhfdc40e92008-04-17 20:59:37 +00002982#else
danielk1977b3175382008-10-17 18:51:52 +00002983 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
drhfdc40e92008-04-17 20:59:37 +00002984#endif
danielk1977b3175382008-10-17 18:51:52 +00002985 }
drhfdc40e92008-04-17 20:59:37 +00002986 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
2987 pPager->journalOff = 0;
2988 pPager->setMaster = 0;
2989 pPager->journalHdr = 0;
2990 if( rc!=SQLITE_OK ){
2991 if( rc==SQLITE_NOMEM ){
2992 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2993 }
2994 goto failed_to_open_journal;
2995 }
drhda47d772002-12-02 04:25:19 +00002996 }
2997 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00002998 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00002999 pPager->needSync = 0;
drh968af522003-02-11 14:55:40 +00003000 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003001 if( pPager->errCode ){
3002 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003003 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003004 }
drhda47d772002-12-02 04:25:19 +00003005 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003006
danielk197776572402004-06-25 02:38:54 +00003007 rc = writeJournalHdr(pPager);
3008
drhac69b052004-05-12 13:30:07 +00003009 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003010 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003011 }
danielk1977ae72d982007-10-03 08:46:44 +00003012 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003013 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003014 if( rc==SQLITE_OK ){
3015 rc = SQLITE_FULL;
3016 }
3017 }
drh9c105bb2004-10-02 20:38:28 +00003018 return rc;
3019
3020failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003021 sqlite3BitvecDestroy(pPager->pInJournal);
3022 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003023 return rc;
drhda47d772002-12-02 04:25:19 +00003024}
3025
3026/*
drh4b845d72002-03-05 12:41:19 +00003027** Acquire a write-lock on the database. The lock is removed when
3028** the any of the following happen:
3029**
drh80e35f42007-03-30 14:06:34 +00003030** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003031** * sqlite3PagerRollback() is called.
3032** * sqlite3PagerClose() is called.
3033** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003034**
danielk197713adf8a2004-06-03 16:08:41 +00003035** The first parameter to this routine is a pointer to any open page of the
3036** database file. Nothing changes about the page - it is used merely to
3037** acquire a pointer to the Pager structure and as proof that there is
3038** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003039**
danielk197713adf8a2004-06-03 16:08:41 +00003040** The second parameter indicates how much space in bytes to reserve for a
3041** master journal file-name at the start of the journal when it is created.
3042**
3043** A journal file is opened if this is not a temporary file. For temporary
3044** files, the opening of the journal file is deferred until there is an
3045** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003046**
drha6abd042004-06-09 17:37:22 +00003047** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003048**
3049** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3050** immediately instead of waiting until we try to flush the cache. The
3051** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003052*/
danielk19773b8a05f2007-03-19 17:44:26 +00003053int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003054 Pager *pPager = pPg->pPager;
3055 int rc = SQLITE_OK;
3056 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003057 assert( pPager->state!=PAGER_UNLOCK );
3058 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003059 assert( pPager->pInJournal==0 );
danielk1977b3175382008-10-17 18:51:52 +00003060 assert( !MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00003061 sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_IN_JOURNAL);
danielk1977b3175382008-10-17 18:51:52 +00003062 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
3063 if( rc==SQLITE_OK ){
3064 pPager->state = PAGER_RESERVED;
3065 if( exFlag ){
3066 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh684917c2004-10-05 02:41:42 +00003067 }
danielk1977b3175382008-10-17 18:51:52 +00003068 }
3069 if( rc!=SQLITE_OK ){
3070 return rc;
3071 }
3072 pPager->dirtyCache = 0;
3073 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
3074 if( pPager->useJournal && !pPager->tempFile
3075 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
3076 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00003077 }
danielk1977334cdb62007-03-26 08:05:12 +00003078 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00003079 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00003080 ** time a (read or write) transaction was successfully concluded
3081 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00003082 ** kept open and either was truncated to 0 bytes or its header was
3083 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00003084 */
3085 assert( pPager->nRec==0 );
3086 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003087 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003088 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003089 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhf5e7bb52008-02-18 14:47:33 +00003090 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003091 rc = SQLITE_NOMEM;
3092 }else{
drh369339d2007-03-30 16:01:55 +00003093 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003094 rc = writeJournalHdr(pPager);
3095 }
drh4b845d72002-03-05 12:41:19 +00003096 }
danielk1977334cdb62007-03-26 08:05:12 +00003097 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003098 return rc;
3099}
3100
3101/*
drh605ad8f2006-05-03 23:34:05 +00003102** Make a page dirty. Set its dirty flag and add it to the dirty
3103** page list.
3104*/
3105static void makeDirty(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003106 sqlite3PcacheMakeDirty(pPg);
drh605ad8f2006-05-03 23:34:05 +00003107}
3108
3109/*
3110** Make a page clean. Clear its dirty bit and remove it from the
3111** dirty page list.
3112*/
3113static void makeClean(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003114 sqlite3PcacheMakeClean(pPg);
drh605ad8f2006-05-03 23:34:05 +00003115}
3116
3117
3118/*
drhed7c8552001-04-11 14:29:21 +00003119** Mark a data page as writeable. The page is written into the journal
3120** if it is not there already. This routine must be called before making
3121** changes to a page.
3122**
3123** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003124** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003125** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003126** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003127** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003128**
3129** If the journal file could not be written because the disk is full,
3130** then this routine returns SQLITE_FULL and does an immediate rollback.
3131** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003132** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003133** reset.
drhed7c8552001-04-11 14:29:21 +00003134*/
danielk19773b8a05f2007-03-19 17:44:26 +00003135static int pager_write(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003136 void *pData = pPg->pData;
drh69688d52001-04-14 16:38:23 +00003137 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003138 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003139
drh6446c4d2001-12-15 14:22:18 +00003140 /* Check for errors
3141 */
danielk1977efaaf572006-01-16 11:29:19 +00003142 if( pPager->errCode ){
3143 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003144 }
drh5e00f6c2001-09-13 13:46:56 +00003145 if( pPager->readOnly ){
3146 return SQLITE_PERM;
3147 }
drh6446c4d2001-12-15 14:22:18 +00003148
danielk197776572402004-06-25 02:38:54 +00003149 assert( !pPager->setMaster );
3150
danielk19773c407372005-02-15 02:54:14 +00003151 CHECK_PAGE(pPg);
3152
drh538f5702007-04-13 02:14:30 +00003153 /* If this page was previously acquired with noContent==1, that means
3154 ** we didn't really read in the content of the page. This can happen
3155 ** (for example) when the page is being moved to the freelist. But
3156 ** now we are (perhaps) moving the page off of the freelist for
3157 ** reuse and we need to know its original content so that content
3158 ** can be stored in the rollback journal. So do the read at this
3159 ** time.
3160 */
drhd33d5a82007-04-26 12:11:28 +00003161 rc = pager_get_content(pPg);
3162 if( rc ){
3163 return rc;
drh538f5702007-04-13 02:14:30 +00003164 }
3165
drh6446c4d2001-12-15 14:22:18 +00003166 /* Mark the page as dirty. If the page has already been written
3167 ** to the journal then we can return right away.
3168 */
drh605ad8f2006-05-03 23:34:05 +00003169 makeDirty(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00003170 if( (pPg->flags&PGHDR_IN_JOURNAL)
3171 && (pageInStatement(pPg) || pPager->stmtInUse==0)
3172 ){
drha6abd042004-06-09 17:37:22 +00003173 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003174 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003175 }else{
drh6446c4d2001-12-15 14:22:18 +00003176
danielk1977a0bf2652004-11-04 14:30:04 +00003177 /* If we get this far, it means that the page needs to be
3178 ** written to the transaction journal or the ckeckpoint journal
3179 ** or both.
3180 **
3181 ** First check to see that the transaction journal exists and
3182 ** create it if it does not.
3183 */
3184 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003185 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003186 if( rc!=SQLITE_OK ){
3187 return rc;
3188 }
3189 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00003190 if( !pPager->journalOpen && pPager->useJournal
3191 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00003192 rc = pager_open_journal(pPager);
3193 if( rc!=SQLITE_OK ) return rc;
3194 }
danielk1977a0bf2652004-11-04 14:30:04 +00003195 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003196 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003197
3198 /* The transaction journal now exists and we have a RESERVED or an
3199 ** EXCLUSIVE lock on the main database file. Write the current page to
3200 ** the transaction journal if it is not there already.
3201 */
danielk1977b3175382008-10-17 18:51:52 +00003202 if( !(pPg->flags&PGHDR_IN_JOURNAL) && pPager->journalOpen ){
danielk1977a0bf2652004-11-04 14:30:04 +00003203 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977b3175382008-10-17 18:51:52 +00003204 u32 cksum;
3205 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00003206
danielk1977b3175382008-10-17 18:51:52 +00003207 /* We should never write to the journal file the page that
3208 ** contains the database locks. The following assert verifies
3209 ** that we do not. */
3210 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
3211 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
3212 cksum = pager_cksum(pPager, (u8*)pData2);
3213 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
3214 if( rc==SQLITE_OK ){
3215 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
3216 pPager->journalOff + 4);
3217 pPager->journalOff += pPager->pageSize+4;
3218 }
3219 if( rc==SQLITE_OK ){
3220 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
3221 pPager->journalOff += 4;
3222 }
3223 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
3224 pPager->journalOff, pPager->pageSize));
3225 PAGER_INCR(sqlite3_pager_writej_count);
3226 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
3227 PAGERID(pPager), pPg->pgno,
3228 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00003229
danielk1977b3175382008-10-17 18:51:52 +00003230 /* An error has occured writing to the journal file. The
3231 ** transaction will be rolled back by the layer above.
3232 */
3233 if( rc!=SQLITE_OK ){
3234 return rc;
3235 }
danielk197707cb5602006-01-20 10:55:05 +00003236
danielk1977b3175382008-10-17 18:51:52 +00003237 pPager->nRec++;
3238 assert( pPager->pInJournal!=0 );
3239 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3240 if( !pPager->noSync ){
3241 pPg->flags |= PGHDR_NEED_SYNC;
3242 }
3243 if( pPager->stmtInUse ){
3244 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhac69b052004-05-12 13:30:07 +00003245 }
danielk197713adf8a2004-06-03 16:08:41 +00003246 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003247 if( !pPager->journalStarted && !pPager->noSync ){
3248 pPg->flags |= PGHDR_NEED_SYNC;
3249 }
drh4f0c5872007-03-26 22:05:01 +00003250 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk19778c0a7912008-08-20 14:49:23 +00003251 PAGERID(pPager), pPg->pgno,
3252 ((pPg->flags&PGHDR_NEED_SYNC)?1:0));
danielk1977a0bf2652004-11-04 14:30:04 +00003253 }
danielk19778c0a7912008-08-20 14:49:23 +00003254 if( pPg->flags&PGHDR_NEED_SYNC ){
danielk1977a0bf2652004-11-04 14:30:04 +00003255 pPager->needSync = 1;
3256 }
danielk19778c0a7912008-08-20 14:49:23 +00003257 pPg->flags |= PGHDR_IN_JOURNAL;
danielk1977a0bf2652004-11-04 14:30:04 +00003258 }
3259
3260 /* If the statement journal is open and the page is not in it,
3261 ** then write the current page to the statement journal. Note that
3262 ** the statement journal format differs from the standard journal format
3263 ** in that it omits the checksums and the header.
3264 */
danielk1977f35843b2007-04-07 15:03:17 +00003265 if( pPager->stmtInUse
3266 && !pageInStatement(pPg)
3267 && (int)pPg->pgno<=pPager->stmtSize
3268 ){
danielk1977b3175382008-10-17 18:51:52 +00003269 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
3270 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
shane049fc212008-10-22 16:26:47 +00003271 assert( (pPg->flags&PGHDR_IN_JOURNAL)
3272 || (int)pPg->pgno>pPager->origDbSize );
danielk1977b3175382008-10-17 18:51:52 +00003273 rc = write32bits(pPager->stfd, offset, pPg->pgno);
3274 if( rc==SQLITE_OK ){
3275 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
drhdb48ee02003-01-16 13:42:43 +00003276 }
danielk1977b3175382008-10-17 18:51:52 +00003277 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
3278 if( rc!=SQLITE_OK ){
3279 return rc;
3280 }
3281 pPager->stmtNRec++;
3282 assert( pPager->pInStmt!=0 );
3283 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhd9b02572001-04-15 00:37:09 +00003284 }
drhfa86c412002-02-02 15:01:15 +00003285 }
3286
3287 /* Update the database size and return.
3288 */
drh1aa2d8b2007-01-03 15:34:29 +00003289 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00003290 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003291 pPager->dbSize = pPg->pgno;
danielk1977b3175382008-10-17 18:51:52 +00003292 if( pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00003293 pPager->dbSize++;
3294 }
drh306dc212001-05-21 13:45:10 +00003295 }
drh69688d52001-04-14 16:38:23 +00003296 return rc;
drhed7c8552001-04-11 14:29:21 +00003297}
3298
3299/*
danielk19774099f6e2007-03-19 11:25:20 +00003300** This function is used to mark a data-page as writable. It uses
3301** pager_write() to open a journal file (if it is not already open)
3302** and write the page *pData to the journal.
3303**
3304** The difference between this function and pager_write() is that this
3305** function also deals with the special case where 2 or more pages
3306** fit on a single disk sector. In this case all co-resident pages
3307** must have been written to the journal file before returning.
3308*/
danielk19773b8a05f2007-03-19 17:44:26 +00003309int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003310 int rc = SQLITE_OK;
3311
danielk19773b8a05f2007-03-19 17:44:26 +00003312 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003313 Pager *pPager = pPg->pPager;
3314 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3315
danielk1977b3175382008-10-17 18:51:52 +00003316 if( nPagePerSector>1 ){
danielk19774099f6e2007-03-19 11:25:20 +00003317 Pgno nPageCount; /* Total number of pages in database file */
3318 Pgno pg1; /* First page of the sector pPg is located on. */
3319 int nPage; /* Number of pages starting at pg1 to journal */
3320 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003321 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003322
3323 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3324 ** header to be written between the pages journaled by this function.
3325 */
danielk1977b3175382008-10-17 18:51:52 +00003326 assert( !MEMDB );
danielk19774099f6e2007-03-19 11:25:20 +00003327 assert( pPager->doNotSync==0 );
3328 pPager->doNotSync = 1;
3329
3330 /* This trick assumes that both the page-size and sector-size are
3331 ** an integer power of 2. It sets variable pg1 to the identifier
3332 ** of the first page of the sector pPg is located on.
3333 */
3334 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3335
danielk1977ad0132d2008-06-07 08:58:22 +00003336 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00003337 if( pPg->pgno>nPageCount ){
3338 nPage = (pPg->pgno - pg1)+1;
3339 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3340 nPage = nPageCount+1-pg1;
3341 }else{
3342 nPage = nPagePerSector;
3343 }
3344 assert(nPage>0);
3345 assert(pg1<=pPg->pgno);
3346 assert((pg1+nPage)>pPg->pgno);
3347
3348 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3349 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003350 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00003351 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00003352 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003353 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003354 if( rc==SQLITE_OK ){
3355 rc = pager_write(pPage);
danielk19778c0a7912008-08-20 14:49:23 +00003356 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003357 needSync = 1;
3358 }
danielk19773b8a05f2007-03-19 17:44:26 +00003359 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003360 }
3361 }
drhc81945e2008-03-10 14:12:53 +00003362 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk19778c0a7912008-08-20 14:49:23 +00003363 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003364 needSync = 1;
3365 }
danielk19778c0a7912008-08-20 14:49:23 +00003366 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003367 }
3368 }
3369
danielk1977dd97a492007-08-22 18:54:32 +00003370 /* If the PgHdr.needSync flag is set for any of the nPage pages
3371 ** starting at pg1, then it needs to be set for all of them. Because
3372 ** writing to any of these nPage pages may damage the others, the
3373 ** journal file must contain sync()ed copies of all of them
3374 ** before any of them can be written out to the database file.
3375 */
3376 if( needSync ){
drhb3df2e12008-09-17 20:06:26 +00003377 assert( !MEMDB && pPager->noSync==0 );
danielk1977dd97a492007-08-22 18:54:32 +00003378 for(ii=0; ii<nPage && needSync; ii++){
3379 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
danielk19778c0a7912008-08-20 14:49:23 +00003380 if( pPage ) pPage->flags |= PGHDR_NEED_SYNC;
3381 sqlite3PagerUnref(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00003382 }
3383 assert(pPager->needSync);
3384 }
3385
danielk19774099f6e2007-03-19 11:25:20 +00003386 assert( pPager->doNotSync==1 );
3387 pPager->doNotSync = 0;
3388 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003389 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003390 }
3391 return rc;
3392}
3393
3394/*
drhaacc5432002-01-06 17:07:40 +00003395** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003396** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003397** to change the content of the page.
3398*/
danielk19777d3a6662006-01-23 16:21:05 +00003399#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003400int sqlite3PagerIswriteable(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003401 return pPg->flags&PGHDR_DIRTY;
drh6019e162001-07-02 17:51:45 +00003402}
danielk19777d3a6662006-01-23 16:21:05 +00003403#endif
drh6019e162001-07-02 17:51:45 +00003404
drh001bbcb2003-03-19 03:14:00 +00003405/*
drh30e58752002-03-02 20:41:57 +00003406** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003407** write the information on page pPg back to the disk, even though
drhdfe88ec2008-11-03 20:55:06 +00003408** that page might be marked as dirty. This happens, for example, when
3409** the page has been added as a leaf of the freelist and so its
3410** content no longer matters.
drh30e58752002-03-02 20:41:57 +00003411**
3412** The overlying software layer calls this routine when all of the data
3413** on the given page is unused. The pager marks the page as clean so
3414** that it does not get written to disk.
3415**
3416** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003417** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003418** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003419**
3420** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003421** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003422** will thereafter be ignored. This is necessary to avoid a problem
3423** where a page with data is added to the freelist during one part of
3424** a transaction then removed from the freelist during a later part
3425** of the same transaction and reused for some other purpose. When it
3426** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003427** the sqlite3PagerDontRollback() routine is called. But because the
3428** page contains critical data, we still need to be sure it gets
3429** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003430*/
danielk1977a1fa00d2008-08-27 15:16:33 +00003431int sqlite3PagerDontWrite(DbPage *pDbPage){
drh538f5702007-04-13 02:14:30 +00003432 PgHdr *pPg = pDbPage;
3433 Pager *pPager = pPg->pPager;
danielk1977a1fa00d2008-08-27 15:16:33 +00003434 int rc;
drh8e298f92002-07-06 16:28:47 +00003435
danielk1977b3175382008-10-17 18:51:52 +00003436 if( pPg->pgno>pPager->origDbSize ){
danielk1977a1fa00d2008-08-27 15:16:33 +00003437 return SQLITE_OK;
3438 }
3439 if( pPager->pAlwaysRollback==0 ){
3440 assert( pPager->pInJournal );
3441 pPager->pAlwaysRollback = sqlite3BitvecCreate(pPager->origDbSize);
3442 if( !pPager->pAlwaysRollback ){
3443 return SQLITE_NOMEM;
3444 }
3445 }
3446 rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno);
3447
3448 if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00003449 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00003450 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
3451 /* If this pages is the last page in the file and the file has grown
3452 ** during the current transaction, then do NOT mark the page as clean.
3453 ** When the database file grows, we must make sure that the last page
3454 ** gets written at least once so that the disk file will be the correct
3455 ** size. If you do not write this page and the size of the file
3456 ** on the disk ends up being too small, that can lead to database
3457 ** corruption during the next transaction.
3458 */
3459 }else{
drh538f5702007-04-13 02:14:30 +00003460 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
3461 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
danielk197733e32162008-08-23 18:53:08 +00003462 pPg->flags |= PGHDR_DONT_WRITE;
danielk19773c407372005-02-15 02:54:14 +00003463#ifdef SQLITE_CHECK_PAGES
3464 pPg->pageHash = pager_pagehash(pPg);
3465#endif
drh8124a302002-06-25 14:43:57 +00003466 }
drh30e58752002-03-02 20:41:57 +00003467 }
danielk1977a1fa00d2008-08-27 15:16:33 +00003468 return rc;
drh30e58752002-03-02 20:41:57 +00003469}
3470
3471/*
3472** A call to this routine tells the pager that if a rollback occurs,
3473** it is not necessary to restore the data on the given page. This
3474** means that the pager does not have to record the given page in the
3475** rollback journal.
drh538f5702007-04-13 02:14:30 +00003476**
3477** If we have not yet actually read the content of this page (if
3478** the PgHdr.needRead flag is set) then this routine acts as a promise
3479** that we will never need to read the page content in the future.
3480** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00003481*/
danielk19773b8a05f2007-03-19 17:44:26 +00003482void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003483 Pager *pPager = pPg->pPager;
3484
drhd3627af2006-12-18 18:34:51 +00003485 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00003486
3487 /* If the journal file is not open, or DontWrite() has been called on
3488 ** this page (DontWrite() sets the alwaysRollback flag), then this
3489 ** function is a no-op.
3490 */
danielk1977a1fa00d2008-08-27 15:16:33 +00003491 if( pPager->journalOpen==0
3492 || sqlite3BitvecTest(pPager->pAlwaysRollback, pPg->pgno)
3493 || pPg->pgno>pPager->origDbSize
danielk19778c0a7912008-08-20 14:49:23 +00003494 ){
danielk197787c29a92008-01-18 11:33:16 +00003495 return;
3496 }
danielk1977a55e9352008-01-21 13:04:34 +00003497
drhc5d0bd92008-04-14 01:00:57 +00003498#ifdef SQLITE_SECURE_DELETE
drha85f7e32008-08-28 02:26:07 +00003499 if( (pPg->flags & PGHDR_IN_JOURNAL)!=0 || (int)pPg->pgno>pPager->origDbSize ){
drhc5d0bd92008-04-14 01:00:57 +00003500 return;
3501 }
3502#endif
3503
3504 /* If SECURE_DELETE is disabled, then there is no way that this
3505 ** routine can be called on a page for which sqlite3PagerDontWrite()
3506 ** has not been previously called during the same transaction.
3507 ** And if DontWrite() has previously been called, the following
3508 ** conditions must be met.
drh10131482008-07-11 03:34:09 +00003509 **
3510 ** (Later:) Not true. If the database is corrupted by having duplicate
3511 ** pages on the freelist (ex: corrupt9.test) then the following is not
3512 ** necessarily true:
danielk1977a55e9352008-01-21 13:04:34 +00003513 */
drh10131482008-07-11 03:34:09 +00003514 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); */
danielk1977a55e9352008-01-21 13:04:34 +00003515
drhf5e7bb52008-02-18 14:47:33 +00003516 assert( pPager->pInJournal!=0 );
3517 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003518 pPg->flags |= PGHDR_IN_JOURNAL;
3519 pPg->flags &= ~PGHDR_NEED_READ;
danielk1977a55e9352008-01-21 13:04:34 +00003520 if( pPager->stmtInUse ){
drhc5d0bd92008-04-14 01:00:57 +00003521 assert( pPager->stmtSize >= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00003522 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00003523 }
danielk1977a55e9352008-01-21 13:04:34 +00003524 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
3525 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00003526}
3527
drhac69b052004-05-12 13:30:07 +00003528
drh30e58752002-03-02 20:41:57 +00003529/*
drh80e35f42007-03-30 14:06:34 +00003530** This routine is called to increment the database file change-counter,
3531** stored at byte 24 of the pager file.
3532*/
danielk1977c7b60172007-08-22 11:22:03 +00003533static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00003534 PgHdr *pPgHdr;
3535 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00003536 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00003537
drh701bb3b2008-08-02 03:50:39 +00003538#ifndef SQLITE_ENABLE_ATOMIC_WRITE
3539 assert( isDirect==0 ); /* isDirect is only true for atomic writes */
3540#endif
drh80e35f42007-03-30 14:06:34 +00003541 if( !pPager->changeCountDone ){
3542 /* Open page 1 of the file for writing. */
3543 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3544 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00003545
3546 if( !isDirect ){
3547 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00003548 if( rc!=SQLITE_OK ){
3549 sqlite3PagerUnref(pPgHdr);
3550 return rc;
3551 }
danielk1977c7b60172007-08-22 11:22:03 +00003552 }
3553
drh80e35f42007-03-30 14:06:34 +00003554 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00003555 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00003556 change_counter++;
danielk19778c0a7912008-08-20 14:49:23 +00003557 put32bits(((char*)pPgHdr->pData)+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00003558
drh701bb3b2008-08-02 03:50:39 +00003559#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977c7b60172007-08-22 11:22:03 +00003560 if( isDirect && pPager->fd->pMethods ){
danielk19778c0a7912008-08-20 14:49:23 +00003561 const void *zBuf = pPgHdr->pData;
danielk1977c7b60172007-08-22 11:22:03 +00003562 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
3563 }
drh701bb3b2008-08-02 03:50:39 +00003564#endif
danielk1977c7b60172007-08-22 11:22:03 +00003565
drh80e35f42007-03-30 14:06:34 +00003566 /* Release the page reference. */
3567 sqlite3PagerUnref(pPgHdr);
3568 pPager->changeCountDone = 1;
3569 }
danielk1977c7b60172007-08-22 11:22:03 +00003570 return rc;
drh80e35f42007-03-30 14:06:34 +00003571}
3572
3573/*
danielk1977f653d782008-03-20 11:04:21 +00003574** Sync the pager file to disk.
3575*/
3576int sqlite3PagerSync(Pager *pPager){
3577 int rc;
drh7426f862008-08-26 21:07:26 +00003578 if( MEMDB ){
3579 rc = SQLITE_OK;
3580 }else{
3581 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3582 }
danielk1977f653d782008-03-20 11:04:21 +00003583 return rc;
3584}
3585
3586/*
drh80e35f42007-03-30 14:06:34 +00003587** Sync the database file for the pager pPager. zMaster points to the name
3588** of a master journal file that should be written into the individual
3589** journal file. zMaster may be NULL, which is interpreted as no master
3590** journal (a single database transaction).
3591**
3592** This routine ensures that the journal is synced, all dirty pages written
3593** to the database file and the database file synced. The only thing that
3594** remains to commit the transaction is to delete the journal file (or
3595** master journal file if specified).
3596**
3597** Note that if zMaster==NULL, this does not overwrite a previous value
3598** passed to an sqlite3PagerCommitPhaseOne() call.
3599**
3600** If parameter nTrunc is non-zero, then the pager file is truncated to
3601** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00003602**
3603** If the final parameter - noSync - is true, then the database file itself
3604** is not synced. The caller must call sqlite3PagerSync() directly to
3605** sync the database file before calling CommitPhaseTwo() to delete the
3606** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00003607*/
danielk1977f653d782008-03-20 11:04:21 +00003608int sqlite3PagerCommitPhaseOne(
3609 Pager *pPager,
3610 const char *zMaster,
3611 Pgno nTrunc,
3612 int noSync
3613){
drh80e35f42007-03-30 14:06:34 +00003614 int rc = SQLITE_OK;
3615
danielk1977dad31b52008-05-15 11:08:07 +00003616 if( pPager->errCode ){
3617 return pPager->errCode;
3618 }
3619
drhd138c012008-05-13 00:58:18 +00003620 /* If no changes have been made, we can leave the transaction early.
3621 */
3622 if( pPager->dbModified==0 &&
3623 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3624 pPager->exclusiveMode!=0) ){
3625 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3626 return SQLITE_OK;
3627 }
3628
drh80e35f42007-03-30 14:06:34 +00003629 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
3630 pPager->zFilename, zMaster, nTrunc);
3631
3632 /* If this is an in-memory db, or no pages have been written to, or this
3633 ** function has already been called, it is a no-op.
3634 */
3635 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
3636 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00003637
danielk1977c7b60172007-08-22 11:22:03 +00003638#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3639 /* The atomic-write optimization can be used if all of the
3640 ** following are true:
3641 **
3642 ** + The file-system supports the atomic-write property for
3643 ** blocks of size page-size, and
3644 ** + This commit is not part of a multi-file transaction, and
3645 ** + Exactly one page has been modified and store in the journal file.
3646 **
3647 ** If the optimization can be used, then the journal file will never
3648 ** be created for this transaction.
3649 */
danielk19774d60af92008-09-08 15:35:06 +00003650 int useAtomicWrite;
danielk19778c0a7912008-08-20 14:49:23 +00003651 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
danielk19774d60af92008-09-08 15:35:06 +00003652 useAtomicWrite = (
danielk1977f55b8992007-08-24 08:15:53 +00003653 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00003654 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00003655 pPager->journalOff==jrnlBufferSize(pPager) &&
3656 nTrunc==0 &&
danielk19778c0a7912008-08-20 14:49:23 +00003657 (pPg==0 || pPg->pDirty==0)
danielk1977f55b8992007-08-24 08:15:53 +00003658 );
danielk1977700b9c52008-04-24 12:37:40 +00003659 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00003660 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00003661 /* Update the nRec field in the journal file. */
3662 int offset = pPager->journalHdr + sizeof(aJournalMagic);
3663 assert(pPager->nRec==1);
3664 rc = write32bits(pPager->jfd, offset, pPager->nRec);
3665
3666 /* Update the db file change counter. The following call will modify
3667 ** the in-memory representation of page 1 to include the updated
3668 ** change counter and then write page 1 directly to the database
3669 ** file. Because of the atomic-write property of the host file-system,
3670 ** this is safe.
3671 */
danielk1977ae72d982007-10-03 08:46:44 +00003672 if( rc==SQLITE_OK ){
3673 rc = pager_incr_changecounter(pPager, 1);
3674 }
danielk1977f55b8992007-08-24 08:15:53 +00003675 }else{
3676 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00003677 }
3678
danielk1977ae72d982007-10-03 08:46:44 +00003679 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00003680#endif
3681
drh80e35f42007-03-30 14:06:34 +00003682 /* If a master journal file name has already been written to the
3683 ** journal file, then no sync is required. This happens when it is
3684 ** written, then the process fails to upgrade from a RESERVED to an
3685 ** EXCLUSIVE lock. The next time the process tries to commit the
3686 ** transaction the m-j name will have already been written.
3687 */
3688 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00003689 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00003690 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00003691 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00003692#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197771aa7ff2008-05-20 07:05:09 +00003693 if( nTrunc!=0 ){
3694 /* If this transaction has made the database smaller, then all pages
3695 ** being discarded by the truncation must be written to the journal
3696 ** file.
3697 */
3698 Pgno i;
3699 int iSkip = PAGER_MJ_PGNO(pPager);
3700 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
3701 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
3702 rc = sqlite3PagerGet(pPager, i, &pPg);
3703 if( rc!=SQLITE_OK ) goto sync_exit;
3704 rc = sqlite3PagerWrite(pPg);
3705 sqlite3PagerUnref(pPg);
3706 if( rc!=SQLITE_OK ) goto sync_exit;
3707 }
3708 }
3709 }
drh80e35f42007-03-30 14:06:34 +00003710#endif
danielk197771aa7ff2008-05-20 07:05:09 +00003711 rc = writeMasterJournal(pPager, zMaster);
3712 if( rc!=SQLITE_OK ) goto sync_exit;
3713 rc = syncJournal(pPager);
3714 }
drh80e35f42007-03-30 14:06:34 +00003715 }
danielk1977c7b60172007-08-22 11:22:03 +00003716 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00003717
3718#ifndef SQLITE_OMIT_AUTOVACUUM
3719 if( nTrunc!=0 ){
3720 rc = sqlite3PagerTruncate(pPager, nTrunc);
3721 if( rc!=SQLITE_OK ) goto sync_exit;
3722 }
3723#endif
3724
3725 /* Write all dirty pages to the database file */
danielk19778c0a7912008-08-20 14:49:23 +00003726 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003727 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00003728 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00003729 assert( rc!=SQLITE_IOERR_BLOCKED );
3730 /* The error might have left the dirty list all fouled up here,
3731 ** but that does not matter because if the if the dirty list did
3732 ** get corrupted, then the transaction will roll back and
3733 ** discard the dirty list. There is an assert in
3734 ** pager_get_all_dirty_pages() that verifies that no attempt
3735 ** is made to use an invalid dirty list.
3736 */
drh153c62c2007-08-24 03:51:33 +00003737 goto sync_exit;
3738 }
danielk19778c0a7912008-08-20 14:49:23 +00003739 sqlite3PcacheCleanAll(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003740
3741 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00003742 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00003743 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00003744 }
3745 IOTRACE(("DBSYNC %p\n", pPager))
3746
3747 pPager->state = PAGER_SYNCED;
3748 }else if( MEMDB && nTrunc!=0 ){
3749 rc = sqlite3PagerTruncate(pPager, nTrunc);
3750 }
3751
3752sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00003753 if( rc==SQLITE_IOERR_BLOCKED ){
3754 /* pager_incr_changecounter() may attempt to obtain an exclusive
3755 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00003756 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00003757 * better to return SQLITE_BUSY.
3758 */
3759 rc = SQLITE_BUSY;
3760 }
drh80e35f42007-03-30 14:06:34 +00003761 return rc;
3762}
3763
3764
3765/*
drhed7c8552001-04-11 14:29:21 +00003766** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00003767**
3768** If the commit fails for any reason, a rollback attempt is made
3769** and an error code is returned. If the commit worked, SQLITE_OK
3770** is returned.
drhed7c8552001-04-11 14:29:21 +00003771*/
drh80e35f42007-03-30 14:06:34 +00003772int sqlite3PagerCommitPhaseTwo(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003773 int rc = SQLITE_OK;
drhd9b02572001-04-15 00:37:09 +00003774
danielk1977efaaf572006-01-16 11:29:19 +00003775 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00003776 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003777 }
drha6abd042004-06-09 17:37:22 +00003778 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00003779 return SQLITE_ERROR;
3780 }
drhd138c012008-05-13 00:58:18 +00003781 if( pPager->dbModified==0 &&
3782 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3783 pPager->exclusiveMode!=0) ){
3784 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3785 return SQLITE_OK;
3786 }
drh4f0c5872007-03-26 22:05:01 +00003787 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
danielk1977b3175382008-10-17 18:51:52 +00003788 assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dirtyCache );
3789 rc = pager_end_transaction(pPager, pPager->setMaster);
3790 rc = pager_error(pPager, rc);
drh86f8c192007-08-22 00:39:19 +00003791 return rc;
drhed7c8552001-04-11 14:29:21 +00003792}
3793
3794/*
drha6abd042004-06-09 17:37:22 +00003795** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00003796** All in-memory cache pages revert to their original data contents.
3797** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00003798**
3799** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00003800** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00003801** process is writing trash into the journal file (SQLITE_CORRUPT) or
3802** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
3803** codes are returned for all these occasions. Otherwise,
3804** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00003805*/
danielk19773b8a05f2007-03-19 17:44:26 +00003806int sqlite3PagerRollback(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003807 int rc = SQLITE_OK;
drh4f0c5872007-03-26 22:05:01 +00003808 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
danielk1977b3175382008-10-17 18:51:52 +00003809 if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00003810 rc = pager_end_transaction(pPager, pPager->setMaster);
danielk19778c0a7912008-08-20 14:49:23 +00003811 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00003812 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00003813 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00003814 }
danielk19778c0a7912008-08-20 14:49:23 +00003815 rc = pPager->errCode;
drha6abd042004-06-09 17:37:22 +00003816 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003817 if( pPager->state==PAGER_RESERVED ){
3818 int rc2;
3819 rc = pager_playback(pPager, 0);
3820 rc2 = pager_end_transaction(pPager, pPager->setMaster);
3821 if( rc==SQLITE_OK ){
3822 rc = rc2;
3823 }
3824 }else{
3825 rc = pager_playback(pPager, 0);
3826 }
danielk197707cb5602006-01-20 10:55:05 +00003827
danielk1977b3175382008-10-17 18:51:52 +00003828 if( !MEMDB ){
3829 pPager->dbSize = -1;
3830 }
danielk19778c0a7912008-08-20 14:49:23 +00003831
3832 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
3833 ** cache. So call pager_error() on the way out to make any error
3834 ** persistent.
3835 */
3836 rc = pager_error(pPager, rc);
3837 }
drh86f8c192007-08-22 00:39:19 +00003838 return rc;
drh98808ba2001-10-18 12:34:46 +00003839}
drhd9b02572001-04-15 00:37:09 +00003840
3841/*
drh5e00f6c2001-09-13 13:46:56 +00003842** Return TRUE if the database file is opened read-only. Return FALSE
3843** if the database is (in theory) writable.
3844*/
danielk19773b8a05f2007-03-19 17:44:26 +00003845int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00003846 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00003847}
3848
3849/*
drh0f7eb612006-08-08 13:51:43 +00003850** Return the number of references to the pager.
3851*/
danielk19773b8a05f2007-03-19 17:44:26 +00003852int sqlite3PagerRefcount(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003853 return sqlite3PcacheRefCount(pPager->pPCache);
drh0f7eb612006-08-08 13:51:43 +00003854}
3855
danielk197771d5d2c2008-09-29 11:49:47 +00003856/*
3857** Return the number of references to the specified page.
3858*/
3859int sqlite3PagerPageRefcount(DbPage *pPage){
3860 return sqlite3PcachePageRefcount(pPage);
3861}
3862
drh0f7eb612006-08-08 13:51:43 +00003863#ifdef SQLITE_TEST
3864/*
drhd9b02572001-04-15 00:37:09 +00003865** This routine is used for testing and analysis only.
3866*/
danielk19773b8a05f2007-03-19 17:44:26 +00003867int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00003868 static int a[11];
danielk19778c0a7912008-08-20 14:49:23 +00003869 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
3870 a[1] = sqlite3PcachePagecount(pPager->pPCache);
3871 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
drhd9b02572001-04-15 00:37:09 +00003872 a[3] = pPager->dbSize;
3873 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00003874 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003875 a[6] = pPager->nHit;
3876 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00003877 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00003878 a[9] = pPager->nRead;
3879 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00003880 return a;
3881}
danielk197717b90b52008-06-06 11:11:25 +00003882int sqlite3PagerIsMemdb(Pager *pPager){
3883 return MEMDB;
3884}
drh0f7eb612006-08-08 13:51:43 +00003885#endif
drhdd793422001-06-28 01:54:48 +00003886
drhfa86c412002-02-02 15:01:15 +00003887/*
drhac69b052004-05-12 13:30:07 +00003888** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00003889**
3890** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00003891** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00003892** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00003893*/
drh86f8c192007-08-22 00:39:19 +00003894static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00003895 int rc;
drhac69b052004-05-12 13:30:07 +00003896 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00003897 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00003898 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00003899 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhda47d772002-12-02 04:25:19 +00003900 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00003901 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00003902 return SQLITE_OK;
3903 }
drhfa86c412002-02-02 15:01:15 +00003904 assert( pPager->journalOpen );
drhf5e7bb52008-02-18 14:47:33 +00003905 assert( pPager->pInStmt==0 );
3906 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00003907 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003908 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00003909 return SQLITE_NOMEM;
3910 }
danielk197776572402004-06-25 02:38:54 +00003911 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00003912 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00003913 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00003914 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00003915 if( !pPager->stmtOpen ){
danielk1977b3175382008-10-17 18:51:52 +00003916 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3917 sqlite3MemJournalOpen(pPager->stfd);
3918 }else{
3919 rc = sqlite3PagerOpentemp(pPager, pPager->stfd, SQLITE_OPEN_SUBJOURNAL);
3920 if( rc ){
3921 goto stmt_begin_failed;
3922 }
drh334b2992007-09-06 23:28:23 +00003923 }
drhac69b052004-05-12 13:30:07 +00003924 pPager->stmtOpen = 1;
3925 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00003926 }
drhac69b052004-05-12 13:30:07 +00003927 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00003928 return SQLITE_OK;
3929
drhac69b052004-05-12 13:30:07 +00003930stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00003931 if( pPager->pInStmt ){
3932 sqlite3BitvecDestroy(pPager->pInStmt);
3933 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00003934 }
3935 return rc;
3936}
drh86f8c192007-08-22 00:39:19 +00003937int sqlite3PagerStmtBegin(Pager *pPager){
3938 int rc;
drh86f8c192007-08-22 00:39:19 +00003939 rc = pagerStmtBegin(pPager);
drh86f8c192007-08-22 00:39:19 +00003940 return rc;
3941}
drhfa86c412002-02-02 15:01:15 +00003942
3943/*
drhac69b052004-05-12 13:30:07 +00003944** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00003945*/
danielk19773b8a05f2007-03-19 17:44:26 +00003946int sqlite3PagerStmtCommit(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00003947 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00003948 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
danielk1977b3175382008-10-17 18:51:52 +00003949 sqlite3BitvecDestroy(pPager->pInStmt);
3950 pPager->pInStmt = 0;
drhac69b052004-05-12 13:30:07 +00003951 pPager->stmtNRec = 0;
3952 pPager->stmtInUse = 0;
danielk1977b3175382008-10-17 18:51:52 +00003953 if( sqlite3IsMemJournal(pPager->stfd) ){
3954 sqlite3OsTruncate(pPager->stfd, 0);
3955 }
drh663fc632002-02-02 18:49:19 +00003956 }
drhac69b052004-05-12 13:30:07 +00003957 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00003958 return SQLITE_OK;
3959}
3960
3961/*
drhac69b052004-05-12 13:30:07 +00003962** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00003963*/
danielk19773b8a05f2007-03-19 17:44:26 +00003964int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00003965 int rc;
drhac69b052004-05-12 13:30:07 +00003966 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00003967 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
danielk1977b3175382008-10-17 18:51:52 +00003968 rc = pager_stmt_playback(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003969 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00003970 }else{
3971 rc = SQLITE_OK;
3972 }
drhac69b052004-05-12 13:30:07 +00003973 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00003974 return rc;
3975}
3976
drh73509ee2003-04-06 20:44:45 +00003977/*
3978** Return the full pathname of the database file.
3979*/
danielk19773b8a05f2007-03-19 17:44:26 +00003980const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00003981 return pPager->zFilename;
3982}
3983
drhb20ea9d2004-02-09 01:20:36 +00003984/*
drhd0679ed2007-08-28 22:24:34 +00003985** Return the VFS structure for the pager.
3986*/
3987const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
3988 return pPager->pVfs;
3989}
3990
3991/*
drhcc6bb3e2007-08-31 16:11:35 +00003992** Return the file handle for the database file associated
3993** with the pager. This might return NULL if the file has
3994** not yet been opened.
3995*/
3996sqlite3_file *sqlite3PagerFile(Pager *pPager){
3997 return pPager->fd;
3998}
3999
4000/*
danielk19775865e3d2004-06-14 06:03:57 +00004001** Return the directory of the database file.
4002*/
danielk19773b8a05f2007-03-19 17:44:26 +00004003const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004004 return pPager->zDirectory;
4005}
4006
4007/*
4008** Return the full pathname of the journal file.
4009*/
danielk19773b8a05f2007-03-19 17:44:26 +00004010const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004011 return pPager->zJournal;
4012}
4013
4014/*
drh2c8997b2005-08-27 16:36:48 +00004015** Return true if fsync() calls are disabled for this pager. Return FALSE
4016** if fsync()s are executed normally.
4017*/
danielk19773b8a05f2007-03-19 17:44:26 +00004018int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004019 return pPager->noSync;
4020}
4021
drh7c4ac0c2007-04-05 11:25:58 +00004022#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004023/*
drhb20ea9d2004-02-09 01:20:36 +00004024** Set the codec for this pager
4025*/
danielk19773b8a05f2007-03-19 17:44:26 +00004026void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004027 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004028 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004029 void *pCodecArg
4030){
4031 pPager->xCodec = xCodec;
4032 pPager->pCodecArg = pCodecArg;
4033}
drh7c4ac0c2007-04-05 11:25:58 +00004034#endif
drhb20ea9d2004-02-09 01:20:36 +00004035
danielk1977687566d2004-11-02 12:56:41 +00004036#ifndef SQLITE_OMIT_AUTOVACUUM
4037/*
danielk197787c29a92008-01-18 11:33:16 +00004038** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004039**
drh5e385312007-06-16 04:42:12 +00004040** There must be no references to the page previously located at
4041** pgno (which we call pPgOld) though that page is allowed to be
drhb3df2e12008-09-17 20:06:26 +00004042** in cache. If the page previously located at pgno is not already
drh5e385312007-06-16 04:42:12 +00004043** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004044**
drh5e385312007-06-16 04:42:12 +00004045** References to the page pPg remain valid. Updating any
4046** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004047** allocated along with the page) is the responsibility of the caller.
4048**
danielk19775fd057a2005-03-09 13:09:43 +00004049** A transaction must be active when this routine is called. It used to be
4050** required that a statement transaction was not active, but this restriction
4051** has been removed (CREATE INDEX needs to move a page when a statement
4052** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00004053**
4054** If the fourth argument, isCommit, is non-zero, then this page is being
4055** moved as part of a database reorganization just before the transaction
4056** is being committed. In this case, it is guaranteed that the database page
4057** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00004058*/
danielk19774c999992008-07-16 18:17:55 +00004059int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00004060 PgHdr *pPgOld; /* The page being overwritten. */
danielk197794daf7f2004-11-08 09:26:09 +00004061 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004062
danielk1977687566d2004-11-02 12:56:41 +00004063 assert( pPg->nRef>0 );
4064
drh4f0c5872007-03-26 22:05:01 +00004065 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk19778c0a7912008-08-20 14:49:23 +00004066 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno);
drhb0603412007-02-28 04:47:26 +00004067 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004068
danielk1977b4626a32007-04-28 15:47:43 +00004069 pager_get_content(pPg);
danielk19774c999992008-07-16 18:17:55 +00004070
4071 /* If the journal needs to be sync()ed before page pPg->pgno can
4072 ** be written to, store pPg->pgno in local variable needSyncPgno.
4073 **
4074 ** If the isCommit flag is set, there is no need to remember that
4075 ** the journal needs to be sync()ed before database page pPg->pgno
4076 ** can be written to. The caller has already promised not to write to it.
4077 */
danielk19778c0a7912008-08-20 14:49:23 +00004078 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00004079 needSyncPgno = pPg->pgno;
danielk19778c0a7912008-08-20 14:49:23 +00004080 assert( (pPg->flags&PGHDR_IN_JOURNAL) || (int)pgno>pPager->origDbSize );
4081 assert( pPg->flags&PGHDR_DIRTY );
danielk1977ae825582004-11-23 09:06:55 +00004082 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004083 }
4084
danielk1977ef73ee92004-11-06 12:26:07 +00004085 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00004086 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00004087 ** page pgno before the 'move' operation, it needs to be retained
4088 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004089 */
danielk19778c0a7912008-08-20 14:49:23 +00004090 pPg->flags &= ~(PGHDR_NEED_SYNC|PGHDR_IN_JOURNAL);
danielk1977687566d2004-11-02 12:56:41 +00004091 pPgOld = pager_lookup(pPager, pgno);
danielk19778c0a7912008-08-20 14:49:23 +00004092 assert( !pPgOld || pPgOld->nRef==1 );
danielk1977687566d2004-11-02 12:56:41 +00004093 if( pPgOld ){
danielk19778c0a7912008-08-20 14:49:23 +00004094 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
drh5e385312007-06-16 04:42:12 +00004095 }
danielk19778c0a7912008-08-20 14:49:23 +00004096 if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){
4097 pPg->flags |= PGHDR_IN_JOURNAL;
4098 }
danielk1977687566d2004-11-02 12:56:41 +00004099
danielk19778c0a7912008-08-20 14:49:23 +00004100 sqlite3PcacheMove(pPg, pgno);
4101 if( pPgOld ){
4102 sqlite3PcacheMove(pPgOld, 0);
4103 sqlite3PcacheRelease(pPgOld);
danielk1977f5fdda82004-11-03 08:44:05 +00004104 }
danielk1977f5fdda82004-11-03 08:44:05 +00004105
drh605ad8f2006-05-03 23:34:05 +00004106 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004107 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004108 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00004109
danielk197794daf7f2004-11-08 09:26:09 +00004110 if( needSyncPgno ){
4111 /* If needSyncPgno is non-zero, then the journal file needs to be
4112 ** sync()ed before any data is written to database file page needSyncPgno.
4113 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00004114 ** "is journaled" bitvec flag has been set. This needs to be remedied by
4115 ** loading the page into the pager-cache and setting the PgHdr.needSync
4116 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00004117 **
danielk1977a98d7b42008-01-18 13:42:54 +00004118 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00004119 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00004120 ** array. Otherwise, if the page is loaded and written again in
4121 ** this transaction, it may be written to the database file before
4122 ** it is synced into the journal file. This way, it may end up in
4123 ** the journal file twice, but that is not a problem.
4124 **
danielk19773b8a05f2007-03-19 17:44:26 +00004125 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004126 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004127 */
4128 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004129 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004130 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004131 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00004132 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00004133 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
4134 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00004135 }
danielk197787c29a92008-01-18 11:33:16 +00004136 return rc;
4137 }
danielk1977ae825582004-11-23 09:06:55 +00004138 pPager->needSync = 1;
drhb3df2e12008-09-17 20:06:26 +00004139 assert( pPager->noSync==0 && !MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00004140 pPgHdr->flags |= PGHDR_NEED_SYNC;
4141 pPgHdr->flags |= PGHDR_IN_JOURNAL;
danielk19773b8a05f2007-03-19 17:44:26 +00004142 makeDirty(pPgHdr);
4143 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004144 }
4145
danielk1977687566d2004-11-02 12:56:41 +00004146 return SQLITE_OK;
4147}
4148#endif
4149
danielk19773b8a05f2007-03-19 17:44:26 +00004150/*
4151** Return a pointer to the data for the specified page.
4152*/
4153void *sqlite3PagerGetData(DbPage *pPg){
danielk197771d5d2c2008-09-29 11:49:47 +00004154 assert( pPg->nRef>0 || pPg->pPager->memDb );
danielk19778c0a7912008-08-20 14:49:23 +00004155 return pPg->pData;
danielk19773b8a05f2007-03-19 17:44:26 +00004156}
4157
4158/*
4159** Return a pointer to the Pager.nExtra bytes of "extra" space
4160** allocated along with the specified page.
4161*/
4162void *sqlite3PagerGetExtra(DbPage *pPg){
4163 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00004164 return (pPager?pPg->pExtra:0);
danielk19773b8a05f2007-03-19 17:44:26 +00004165}
4166
danielk197741483462007-03-24 16:45:04 +00004167/*
4168** Get/set the locking-mode for this pager. Parameter eMode must be one
4169** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4170** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4171** the locking-mode is set to the value specified.
4172**
4173** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4174** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4175** locking-mode.
4176*/
4177int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004178 assert( eMode==PAGER_LOCKINGMODE_QUERY
4179 || eMode==PAGER_LOCKINGMODE_NORMAL
4180 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4181 assert( PAGER_LOCKINGMODE_QUERY<0 );
4182 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4183 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00004184 pPager->exclusiveMode = eMode;
4185 }
4186 return (int)pPager->exclusiveMode;
4187}
4188
drh3b020132008-04-17 17:02:01 +00004189/*
drh04335882008-09-26 21:08:08 +00004190** Get/set the journal-mode for this pager. Parameter eMode must be one of:
drh3b020132008-04-17 17:02:01 +00004191**
drh04335882008-09-26 21:08:08 +00004192** PAGER_JOURNALMODE_QUERY
4193** PAGER_JOURNALMODE_DELETE
4194** PAGER_JOURNALMODE_TRUNCATE
4195** PAGER_JOURNALMODE_PERSIST
4196** PAGER_JOURNALMODE_OFF
4197**
4198** If the parameter is not _QUERY, then the journal-mode is set to the
4199** value specified.
4200**
4201** The returned indicate the current (possibly updated)
drh3b020132008-04-17 17:02:01 +00004202** journal-mode.
4203*/
4204int sqlite3PagerJournalMode(Pager *pPager, int eMode){
danielk1977b3175382008-10-17 18:51:52 +00004205 if( !MEMDB ){
4206 assert( eMode==PAGER_JOURNALMODE_QUERY
4207 || eMode==PAGER_JOURNALMODE_DELETE
4208 || eMode==PAGER_JOURNALMODE_TRUNCATE
4209 || eMode==PAGER_JOURNALMODE_PERSIST
4210 || eMode==PAGER_JOURNALMODE_OFF
4211 || eMode==PAGER_JOURNALMODE_MEMORY );
4212 assert( PAGER_JOURNALMODE_QUERY<0 );
4213 if( eMode>=0 ){
4214 pPager->journalMode = eMode;
4215 }else{
4216 assert( eMode==PAGER_JOURNALMODE_QUERY );
4217 }
drh3b020132008-04-17 17:02:01 +00004218 }
drhfdc40e92008-04-17 20:59:37 +00004219 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00004220}
4221
danielk1977b53e4962008-06-04 06:45:59 +00004222/*
4223** Get/set the size-limit used for persistent journal files.
4224*/
4225i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
4226 if( iLimit>=-1 ){
4227 pPager->journalSizeLimit = iLimit;
4228 }
4229 return pPager->journalSizeLimit;
4230}
4231
drh2e66f0b2005-04-28 17:18:48 +00004232#endif /* SQLITE_OMIT_DISKIO */