blob: 7d122aee0cc494aaafed6b864b125b9a2671ef40 [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**
danielk19774abdfa42008-08-27 09:44:39 +000021** @(#) $Id: pager.c,v 1.482 2008/08/27 09:44:40 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025
26/*
drhdb48ee02003-01-16 13:42:43 +000027** Macros for troubleshooting. Normally turned off
28*/
danielk1977466be562004-06-10 02:16:01 +000029#if 0
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 */
drhfcd35c72005-05-21 02:48:08 +0000194 char *zFilename; /* Name of the database file */
195 char *zJournal; /* Name of the journal file */
196 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000197 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
198 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000199 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
drheb206252004-10-01 02:00:31 +0000200 i64 journalOff; /* Current byte offset in the journal file */
201 i64 journalHdr; /* Byte offset to previous journal header */
202 i64 stmtHdrOff; /* First journal header written this statement */
203 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000204 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000205 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000206#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000207 int nHit, nMiss; /* Cache hits and missing */
208 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000209#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000210 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
211 void (*xReiniter)(DbPage*,int); /* 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;
312 if( MEMDB ){
danielk19778c0a7912008-08-20 14:49:23 +0000313 return pPg->apSave[1]!=0;
danielk1977f35843b2007-04-07 15:03:17 +0000314 }else{
drhf5e7bb52008-02-18 14:47:33 +0000315 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000316 }
317}
drh8ca0c722006-05-07 17:49:38 +0000318
319/*
drh34e79ce2004-02-08 06:05:46 +0000320** Read a 32-bit integer from the given file descriptor. Store the integer
321** that is read in *pRes. Return SQLITE_OK if everything worked, or an
322** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000323**
324** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000325*/
danielk197762079062007-08-15 17:08:46 +0000326static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000327 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000328 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000329 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000330 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000331 }
drh94f33312002-08-12 12:29:56 +0000332 return rc;
333}
334
335/*
drh97b57482006-01-10 20:32:32 +0000336** Write a 32-bit integer into a string buffer in big-endian byte order.
337*/
drha3152892007-05-05 11:48:52 +0000338#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000339
340/*
drh34e79ce2004-02-08 06:05:46 +0000341** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
342** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000343*/
danielk197762079062007-08-15 17:08:46 +0000344static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000345 char ac[4];
drh97b57482006-01-10 20:32:32 +0000346 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000347 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000348}
349
drh2554f8b2003-01-22 01:26:44 +0000350/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000351** If file pFd is open, call sqlite3OsUnlock() on it.
352*/
353static int osUnlock(sqlite3_file *pFd, int eLock){
354 if( !pFd->pMethods ){
355 return SQLITE_OK;
356 }
357 return sqlite3OsUnlock(pFd, eLock);
358}
359
360/*
danielk1977c7b60172007-08-22 11:22:03 +0000361** This function determines whether or not the atomic-write optimization
362** can be used with this pager. The optimization can be used if:
363**
364** (a) the value returned by OsDeviceCharacteristics() indicates that
365** a database page may be written atomically, and
366** (b) the value returned by OsSectorSize() is less than or equal
367** to the page size.
368**
369** If the optimization cannot be used, 0 is returned. If it can be used,
370** then the value returned is the size of the journal file when it
371** contains rollback data for exactly one page.
372*/
373#ifdef SQLITE_ENABLE_ATOMIC_WRITE
374static int jrnlBufferSize(Pager *pPager){
375 int dc; /* Device characteristics */
376 int nSector; /* Sector size */
drhfacf0302008-06-17 15:12:00 +0000377 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000378 sqlite3_file *fd = pPager->fd;
379
380 if( fd->pMethods ){
381 dc = sqlite3OsDeviceCharacteristics(fd);
382 nSector = sqlite3OsSectorSize(fd);
drhfacf0302008-06-17 15:12:00 +0000383 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000384 }
385
386 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
387 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
388
drhfacf0302008-06-17 15:12:00 +0000389 if( !fd->pMethods ||
390 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000391 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
392 }
393 return 0;
394}
395#endif
396
397/*
danielk1977aef0bf62005-12-30 16:28:01 +0000398** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000399** code. The first argument is a pointer to the pager structure, the
400** second the error-code about to be returned by a pager API function.
401** The value returned is a copy of the second argument to this function.
402**
drh4f0ee682007-03-30 20:43:40 +0000403** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000404** the error becomes persistent. Until the persisten error is cleared,
405** subsequent API calls on this Pager will immediately return the same
406** error code.
407**
408** A persistent error indicates that the contents of the pager-cache
409** cannot be trusted. This state can be cleared by completely discarding
410** the contents of the pager-cache. If a transaction was active when
411** the persistent error occured, then the rollback journal may need
412** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000413*/
danielk1977ae72d982007-10-03 08:46:44 +0000414static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000415static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000416 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000417 assert(
418 pPager->errCode==SQLITE_FULL ||
419 pPager->errCode==SQLITE_OK ||
420 (pPager->errCode & 0xff)==SQLITE_IOERR
421 );
danielk1977979f38e2007-03-27 16:19:51 +0000422 if(
drh4ac285a2006-09-15 07:28:50 +0000423 rc2==SQLITE_FULL ||
424 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000425 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000426 ){
427 pPager->errCode = rc;
danielk19778c0a7912008-08-20 14:49:23 +0000428 if( pPager->state==PAGER_UNLOCK
429 && sqlite3PcacheRefCount(pPager->pPCache)==0
430 ){
danielk1977ae72d982007-10-03 08:46:44 +0000431 /* If the pager is already unlocked, call pager_unlock() now to
432 ** clear the error state and ensure that the pager-cache is
433 ** completely empty.
434 */
435 pager_unlock(pPager);
436 }
danielk1977aef0bf62005-12-30 16:28:01 +0000437 }
438 return rc;
439}
440
drh477731b2007-06-16 03:06:27 +0000441/*
442** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
443** on the cache using a hash function. This is used for testing
444** and debugging only.
445*/
danielk19773c407372005-02-15 02:54:14 +0000446#ifdef SQLITE_CHECK_PAGES
447/*
448** Return a 32-bit hash of the page data for pPage.
449*/
drh477731b2007-06-16 03:06:27 +0000450static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000451 u32 hash = 0;
452 int i;
drh477731b2007-06-16 03:06:27 +0000453 for(i=0; i<nByte; i++){
454 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000455 }
456 return hash;
457}
drh477731b2007-06-16 03:06:27 +0000458static u32 pager_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000459 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
460}
461static u32 pager_set_pagehash(PgHdr *pPage){
462 pPage->pageHash = pager_pagehash(pPage);
drh477731b2007-06-16 03:06:27 +0000463}
danielk19773c407372005-02-15 02:54:14 +0000464
465/*
466** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
467** is defined, and NDEBUG is not defined, an assert() statement checks
468** that the page is either dirty or still matches the calculated page-hash.
469*/
470#define CHECK_PAGE(x) checkPage(x)
471static void checkPage(PgHdr *pPg){
472 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +0000473 assert( !pPg->pageHash || pPager->errCode || MEMDB
474 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
danielk19773c407372005-02-15 02:54:14 +0000475}
476
477#else
drh8ffa8172007-06-18 17:25:17 +0000478#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000479#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000480#define CHECK_PAGE(x)
drh41d30272008-08-20 21:47:45 +0000481#endif /* SQLITE_CHECK_PAGES */
danielk19773c407372005-02-15 02:54:14 +0000482
drhed7c8552001-04-11 14:29:21 +0000483/*
danielk197776572402004-06-25 02:38:54 +0000484** When this is called the journal file for pager pPager must be open.
485** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000486** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000487**
danielk197765839c62007-08-30 08:08:17 +0000488** zMaster must point to a buffer of at least nMaster bytes allocated by
489** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
490** enough space to write the master journal name). If the master journal
491** name in the journal is longer than nMaster bytes (including a
492** nul-terminator), then this is handled as if no master journal name
493** were present in the journal.
494**
495** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000496** SQLITE_OK returned.
497*/
danielk197765839c62007-08-30 08:08:17 +0000498static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000499 int rc;
500 u32 len;
drheb206252004-10-01 02:00:31 +0000501 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000502 u32 cksum;
shane0b8d2762008-07-22 05:18:00 +0000503 u32 u; /* Unsigned loop counter */
danielk197776572402004-06-25 02:38:54 +0000504 unsigned char aMagic[8]; /* A buffer to hold the magic header */
505
danielk197765839c62007-08-30 08:08:17 +0000506 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000507
drh054889e2005-11-30 03:20:31 +0000508 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000509 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000510
danielk197762079062007-08-15 17:08:46 +0000511 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000512 if( rc!=SQLITE_OK ) return rc;
513
danielk197765839c62007-08-30 08:08:17 +0000514 if( len>=nMaster ){
515 return SQLITE_OK;
516 }
517
danielk197762079062007-08-15 17:08:46 +0000518 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000519 if( rc!=SQLITE_OK ) return rc;
520
danielk197762079062007-08-15 17:08:46 +0000521 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000522 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
523
danielk197765839c62007-08-30 08:08:17 +0000524 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000525 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000526 return rc;
527 }
danielk197765839c62007-08-30 08:08:17 +0000528 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000529
530 /* See if the checksum matches the master journal name */
shane0b8d2762008-07-22 05:18:00 +0000531 for(u=0; u<len; u++){
532 cksum -= zMaster[u];
danielk197765839c62007-08-30 08:08:17 +0000533 }
danielk19778191bff2004-06-28 04:52:30 +0000534 if( cksum ){
535 /* If the checksum doesn't add up, then one or more of the disk sectors
536 ** containing the master journal filename is corrupted. This means
537 ** definitely roll back, so just return SQLITE_OK and report a (nul)
538 ** master-journal filename.
539 */
danielk197765839c62007-08-30 08:08:17 +0000540 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000541 }
danielk197776572402004-06-25 02:38:54 +0000542
543 return SQLITE_OK;
544}
545
546/*
547** Seek the journal file descriptor to the next sector boundary where a
548** journal header may be read or written. Pager.journalOff is updated with
549** the new seek offset.
550**
551** i.e for a sector size of 512:
552**
553** Input Offset Output Offset
554** ---------------------------------------
555** 0 0
556** 512 512
557** 100 512
558** 2000 2048
559**
560*/
danielk197762079062007-08-15 17:08:46 +0000561static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000562 i64 offset = 0;
563 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000564 if( c ){
565 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
566 }
567 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
568 assert( offset>=c );
569 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
570 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000571}
572
573/*
drhf3a87622008-04-17 14:16:42 +0000574** Write zeros over the header of the journal file. This has the
575** effect of invalidating the journal file and committing the
576** transaction.
577*/
danielk1977df2566a2008-05-07 19:11:03 +0000578static int zeroJournalHdr(Pager *pPager, int doTruncate){
579 int rc = SQLITE_OK;
drhf3a87622008-04-17 14:16:42 +0000580 static const char zeroHdr[28];
581
danielk1977df2566a2008-05-07 19:11:03 +0000582 if( pPager->journalOff ){
danielk1977b53e4962008-06-04 06:45:59 +0000583 i64 iLimit = pPager->journalSizeLimit;
584
danielk1977df2566a2008-05-07 19:11:03 +0000585 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000586 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000587 rc = sqlite3OsTruncate(pPager->jfd, 0);
588 }else{
589 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
590 }
danielk197781620542008-06-07 05:19:37 +0000591 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000592 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
593 }
danielk1977b53e4962008-06-04 06:45:59 +0000594
595 /* At this point the transaction is committed but the write lock
596 ** is still held on the file. If there is a size limit configured for
597 ** the persistent journal and the journal file currently consumes more
598 ** space than that limit allows for, truncate it now. There is no need
599 ** to sync the file following this operation.
600 */
601 if( rc==SQLITE_OK && iLimit>0 ){
602 i64 sz;
603 rc = sqlite3OsFileSize(pPager->jfd, &sz);
604 if( rc==SQLITE_OK && sz>iLimit ){
605 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
606 }
607 }
drha06ecba2008-04-22 17:15:17 +0000608 }
drhf3a87622008-04-17 14:16:42 +0000609 return rc;
610}
611
612/*
danielk197776572402004-06-25 02:38:54 +0000613** The journal file must be open when this routine is called. A journal
614** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
615** current location.
616**
617** The format for the journal header is as follows:
618** - 8 bytes: Magic identifying journal format.
619** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
620** - 4 bytes: Random number used for page hash.
621** - 4 bytes: Initial database page count.
622** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000623** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000624**
danielk197767c007b2008-03-20 04:45:49 +0000625** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000626*/
627static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +0000628 int rc = SQLITE_OK;
629 char *zHeader = pPager->pTmpSpace;
630 int nHeader = pPager->pageSize;
631 int nWrite;
632
633 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
634 nHeader = JOURNAL_HDR_SZ(pPager);
635 }
danielk197776572402004-06-25 02:38:54 +0000636
danielk19774099f6e2007-03-19 11:25:20 +0000637 if( pPager->stmtHdrOff==0 ){
638 pPager->stmtHdrOff = pPager->journalOff;
639 }
640
danielk197762079062007-08-15 17:08:46 +0000641 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000642 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000643
drh97b57482006-01-10 20:32:32 +0000644 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000645
646 /*
647 ** Write the nRec Field - the number of page records that follow this
648 ** journal header. Normally, zero is written to this value at this time.
649 ** After the records are added to the journal (and the journal synced,
650 ** if in full-sync mode), the zero is overwritten with the true number
651 ** of records (see syncJournal()).
652 **
653 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
654 ** reading the journal this value tells SQLite to assume that the
655 ** rest of the journal file contains valid page records. This assumption
656 ** is dangerous, as if a failure occured whilst writing to the journal
657 ** file it may contain some garbage data. There are two scenarios
658 ** where this risk can be ignored:
659 **
660 ** * When the pager is in no-sync mode. Corruption can follow a
661 ** power failure in this case anyway.
662 **
663 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
664 ** that garbage data is never appended to the journal file.
665 */
666 assert(pPager->fd->pMethods||pPager->noSync);
667 if( (pPager->noSync)
668 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
669 ){
670 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
671 }else{
672 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
673 }
674
drh97b57482006-01-10 20:32:32 +0000675 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +0000676 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +0000677 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
678 /* The initial database size */
679 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
680 /* The assumed sector size for this process */
681 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
danielk197767c007b2008-03-20 04:45:49 +0000682 if( pPager->journalHdr==0 ){
683 /* The page size */
684 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
685 }
danielk197776572402004-06-25 02:38:54 +0000686
danielk1977a664f8e2008-04-22 14:31:48 +0000687 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
688 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
689 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
690 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +0000691 }
danielk1977a664f8e2008-04-22 14:31:48 +0000692
danielk197776572402004-06-25 02:38:54 +0000693 return rc;
694}
695
696/*
697** The journal file must be open when this is called. A journal header file
698** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
699** file. See comments above function writeJournalHdr() for a description of
700** the journal header format.
701**
702** If the header is read successfully, *nRec is set to the number of
703** page records following this header and *dbSize is set to the size of the
704** database before the transaction began, in pages. Also, pPager->cksumInit
705** is set to the value read from the journal header. SQLITE_OK is returned
706** in this case.
707**
708** If the journal header file appears to be corrupted, SQLITE_DONE is
709** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
710** cannot be read from the journal file an error code is returned.
711*/
712static int readJournalHdr(
713 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000714 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000715 u32 *pNRec,
716 u32 *pDbSize
717){
718 int rc;
719 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000720 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +0000721 int iPageSize;
danielk197776572402004-06-25 02:38:54 +0000722
danielk197762079062007-08-15 17:08:46 +0000723 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000724 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
725 return SQLITE_DONE;
726 }
danielk197762079062007-08-15 17:08:46 +0000727 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000728
danielk197762079062007-08-15 17:08:46 +0000729 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000730 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000731 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000732
733 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
734 return SQLITE_DONE;
735 }
736
danielk197762079062007-08-15 17:08:46 +0000737 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000738 if( rc ) return rc;
739
danielk197762079062007-08-15 17:08:46 +0000740 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000741 if( rc ) return rc;
742
danielk197762079062007-08-15 17:08:46 +0000743 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000744 if( rc ) return rc;
745
danielk197767c007b2008-03-20 04:45:49 +0000746 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
747 if( rc==SQLITE_OK
748 && iPageSize>=512
749 && iPageSize<=SQLITE_MAX_PAGE_SIZE
750 && ((iPageSize-1)&iPageSize)==0
751 ){
752 u16 pagesize = iPageSize;
753 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
754 }
755 if( rc ) return rc;
756
danielk197776572402004-06-25 02:38:54 +0000757 /* Update the assumed sector-size to match the value used by
758 ** the process that created this journal. If this journal was
759 ** created by a process other than this one, then this routine
760 ** is being called from within pager_playback(). The local value
761 ** of Pager.sectorSize is restored at the end of that routine.
762 */
danielk197762079062007-08-15 17:08:46 +0000763 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000764 if( rc ) return rc;
765
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
799 if( !zMaster || pPager->setMaster) return SQLITE_OK;
800 pPager->setMaster = 1;
801
802 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000803 for(i=0; i<len; i++){
804 cksum += zMaster[i];
805 }
danielk197776572402004-06-25 02:38:54 +0000806
807 /* If in full-sync mode, advance to the next disk sector before writing
808 ** the master journal name. This is in case the previous page written to
809 ** the journal has already been synced.
810 */
811 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000812 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000813 }
danielk197762079062007-08-15 17:08:46 +0000814 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000815 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000816
danielk197762079062007-08-15 17:08:46 +0000817 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000818 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000819 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000820
danielk197762079062007-08-15 17:08:46 +0000821 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000822 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000823 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000824
drh97b57482006-01-10 20:32:32 +0000825 put32bits(zBuf, len);
826 put32bits(&zBuf[4], cksum);
827 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000828 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +0000829 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +0000830 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +0000831
832 /* If the pager is in peristent-journal mode, then the physical
833 ** journal-file may extend past the end of the master-journal name
834 ** and 8 bytes of magic data just written to the file. This is
835 ** dangerous because the code to rollback a hot-journal file
836 ** will not be able to find the master-journal name to determine
837 ** whether or not the journal is hot.
838 **
839 ** Easiest thing to do in this scenario is to truncate the journal
840 ** file to the required size.
841 */
842 if( (rc==SQLITE_OK)
843 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
844 && jrnlSize>jrnlOff
845 ){
846 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
847 }
danielk197776572402004-06-25 02:38:54 +0000848 return rc;
849}
850
851/*
drhed7c8552001-04-11 14:29:21 +0000852** Find a page in the hash table given its page number. Return
853** a pointer to the page or NULL if not found.
854*/
drhd9b02572001-04-15 00:37:09 +0000855static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000856 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000857 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
drhed7c8552001-04-11 14:29:21 +0000858 return p;
859}
860
861/*
danielk1977e180dd92007-04-05 17:15:52 +0000862** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +0000863** sets the state of the pager back to what it was when it was first
864** opened. Any outstanding pages are invalidated and subsequent attempts
865** to access those pages will likely result in a coredump.
866*/
drhd9b02572001-04-15 00:37:09 +0000867static void pager_reset(Pager *pPager){
danielk1977efaaf572006-01-16 11:29:19 +0000868 if( pPager->errCode ) return;
danielk19778c0a7912008-08-20 14:49:23 +0000869 sqlite3PcacheClear(pPager->pPCache);
danielk1977e277be02007-03-23 18:12:06 +0000870}
871
872/*
danielk1977ae72d982007-10-03 08:46:44 +0000873** Unlock the database file.
874**
875** If the pager is currently in error state, discard the contents of
876** the cache and reset the Pager structure internal state. If there is
877** an open journal-file, then the next time a shared-lock is obtained
878** on the pager file (by this or any other process), it will be
879** treated as a hot-journal and rolled back.
880*/
881static void pager_unlock(Pager *pPager){
882 if( !pPager->exclusiveMode ){
883 if( !MEMDB ){
drh1aa5af12008-03-07 19:51:14 +0000884 int rc = osUnlock(pPager->fd, NO_LOCK);
drh308aa322008-03-07 20:14:38 +0000885 if( rc ) pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000886 pPager->dbSize = -1;
887 IOTRACE(("UNLOCK %p\n", pPager))
888
drh16e45a42008-04-19 20:34:18 +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 }
899
danielk1977ae72d982007-10-03 08:46:44 +0000900 /* If Pager.errCode is set, the contents of the pager cache cannot be
901 ** trusted. Now that the pager file is unlocked, the contents of the
902 ** cache can be discarded and the error code safely cleared.
903 */
904 if( pPager->errCode ){
drh1aa5af12008-03-07 19:51:14 +0000905 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +0000906 pager_reset(pPager);
907 if( pPager->stmtOpen ){
908 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +0000909 sqlite3BitvecDestroy(pPager->pInStmt);
910 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000911 }
danielk1977ae72d982007-10-03 08:46:44 +0000912 pPager->stmtOpen = 0;
913 pPager->stmtInUse = 0;
914 pPager->journalOff = 0;
915 pPager->journalStarted = 0;
916 pPager->stmtAutoopen = 0;
917 pPager->origDbSize = 0;
918 }
919 }
920
921 if( !MEMDB || pPager->errCode==SQLITE_OK ){
922 pPager->state = PAGER_UNLOCK;
923 pPager->changeCountDone = 0;
924 }
925 }
926}
927
928/*
929** Execute a rollback if a transaction is active and unlock the
930** database file. If the pager has already entered the error state,
931** do not attempt the rollback.
932*/
933static void pagerUnlockAndRollback(Pager *p){
danielk1977ae72d982007-10-03 08:46:44 +0000934 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +0000935 sqlite3BeginBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +0000936 sqlite3PagerRollback(p);
danielk19772d1d86f2008-06-20 14:59:51 +0000937 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +0000938 }
939 pager_unlock(p);
danielk1977ae72d982007-10-03 08:46:44 +0000940}
941
942/*
drh80e35f42007-03-30 14:06:34 +0000943** This routine ends a transaction. A transaction is ended by either
944** a COMMIT or a ROLLBACK.
945**
drhed7c8552001-04-11 14:29:21 +0000946** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +0000947** a RESERVED or EXCLUSIVE lock on the database. This routine will release
948** the database lock and acquires a SHARED lock in its place if that is
949** the appropriate thing to do. Release locks usually is appropriate,
950** unless we are in exclusive access mode or unless this is a
951** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
952**
953** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +0000954**
955** TODO: Consider keeping the journal file open for temporary databases.
956** This might give a performance improvement on windows where opening
957** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +0000958*/
danielk1977df2566a2008-05-07 19:11:03 +0000959static int pager_end_transaction(Pager *pPager, int hasMaster){
danielk197741483462007-03-24 16:45:04 +0000960 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +0000961 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +0000962 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +0000963 if( pPager->state<PAGER_RESERVED ){
964 return SQLITE_OK;
965 }
danielk19773b8a05f2007-03-19 17:44:26 +0000966 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +0000967 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +0000968 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +0000969 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +0000970 }
drhda47d772002-12-02 04:25:19 +0000971 if( pPager->journalOpen ){
danielk197793f7af92008-05-09 16:57:50 +0000972 if( pPager->exclusiveMode
973 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
974 ){
975 rc = zeroJournalHdr(pPager, hasMaster);
976 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +0000977 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +0000978 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +0000979 }else{
danielk1977b4b47412007-08-17 15:53:36 +0000980 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +0000981 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +0000982 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +0000983 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +0000984 }
danielk197741483462007-03-24 16:45:04 +0000985 }
drhf5e7bb52008-02-18 14:47:33 +0000986 sqlite3BitvecDestroy(pPager->pInJournal);
987 pPager->pInJournal = 0;
danielk19778c0a7912008-08-20 14:49:23 +0000988 sqlite3PcacheCleanAll(pPager->pPCache);
danielk19773c407372005-02-15 02:54:14 +0000989#ifdef SQLITE_CHECK_PAGES
danielk19778c0a7912008-08-20 14:49:23 +0000990 sqlite3PcacheIterate(pPager->pPCache, pager_set_pagehash);
danielk19773c407372005-02-15 02:54:14 +0000991#endif
danielk19778c0a7912008-08-20 14:49:23 +0000992 sqlite3PcacheSetFlags(pPager->pPCache,
993 ~(PGHDR_IN_JOURNAL | PGHDR_NEED_SYNC | PGHDR_ALWAYS_ROLLBACK), 0
994 );
danielk1977ef317ab2004-06-23 10:43:10 +0000995 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +0000996 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +0000997 }else{
drhf5e7bb52008-02-18 14:47:33 +0000998 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +0000999 }
danielk1977979f38e2007-03-27 16:19:51 +00001000
danielk197741483462007-03-24 16:45:04 +00001001 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001002 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001003 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001004 }else if( pPager->state==PAGER_SYNCED ){
1005 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001006 }
danielk1977334cdb62007-03-26 08:05:12 +00001007 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001008 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001009 pPager->needSync = 0;
danielk19778c0a7912008-08-20 14:49:23 +00001010 /* lruListSetFirstSynced(pPager); */
drh588f5bc2007-01-02 18:41:54 +00001011 pPager->dbSize = -1;
drhd138c012008-05-13 00:58:18 +00001012 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001013
1014 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001015}
1016
drhed7c8552001-04-11 14:29:21 +00001017/*
drh968af522003-02-11 14:55:40 +00001018** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001019**
1020** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001021** random initial value and the page number. We experimented with
1022** a checksum of the entire data, but that was found to be too slow.
1023**
1024** Note that the page number is stored at the beginning of data and
1025** the checksum is stored at the end. This is important. If journal
1026** corruption occurs due to a power failure, the most likely scenario
1027** is that one end or the other of the record will be changed. It is
1028** much less likely that the two ends of the journal record will be
1029** correct and the middle be corrupt. Thus, this "checksum" scheme,
1030** though fast and simple, catches the mostly likely kind of corruption.
1031**
1032** FIX ME: Consider adding every 200th (or so) byte of the data to the
1033** checksum. That way if a single page spans 3 or more disk sectors and
1034** only the middle sector is corrupt, we will still have a reasonable
1035** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001036*/
drh74161702006-02-24 02:53:49 +00001037static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001038 u32 cksum = pPager->cksumInit;
1039 int i = pPager->pageSize-200;
1040 while( i>0 ){
1041 cksum += aData[i];
1042 i -= 200;
1043 }
drh968af522003-02-11 14:55:40 +00001044 return cksum;
1045}
1046
drh605ad8f2006-05-03 23:34:05 +00001047/* Forward declaration */
1048static void makeClean(PgHdr*);
1049
drh968af522003-02-11 14:55:40 +00001050/*
drhfa86c412002-02-02 15:01:15 +00001051** Read a single page from the journal file opened on file descriptor
1052** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001053**
drh726de592004-06-10 23:35:50 +00001054** If useCksum==0 it means this journal does not use checksums. Checksums
1055** are not used in statement journals because statement journals do not
1056** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001057*/
danielk197762079062007-08-15 17:08:46 +00001058static int pager_playback_one_page(
1059 Pager *pPager,
1060 sqlite3_file *jfd,
1061 i64 offset,
1062 int useCksum
1063){
drhfa86c412002-02-02 15:01:15 +00001064 int rc;
drhae2b40c2004-06-09 19:03:54 +00001065 PgHdr *pPg; /* An existing page in the cache */
1066 Pgno pgno; /* The page number of a page in journal */
1067 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001068 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001069
drh96362842005-03-20 22:47:56 +00001070 /* useCksum should be true for the main journal and false for
1071 ** statement journals. Verify that this is always the case
1072 */
drh9cbe6352005-11-29 03:13:21 +00001073 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001074 assert( aData );
drh96362842005-03-20 22:47:56 +00001075
danielk197762079062007-08-15 17:08:46 +00001076 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001077 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001078 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001079 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001080 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001081
drh968af522003-02-11 14:55:40 +00001082 /* Sanity checking on the page. This is more important that I originally
1083 ** thought. If a power failure occurs while the journal is being written,
1084 ** it could cause invalid data to be written into the journal. We need to
1085 ** detect this invalid data (with high probability) and ignore it.
1086 */
danielk197775edc162004-06-26 01:48:18 +00001087 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001088 return SQLITE_DONE;
1089 }
drhae2b40c2004-06-09 19:03:54 +00001090 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001091 return SQLITE_OK;
1092 }
drhae2b40c2004-06-09 19:03:54 +00001093 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001094 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001095 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001096 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001097 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001098 return SQLITE_DONE;
1099 }
1100 }
drhfa86c412002-02-02 15:01:15 +00001101
danielk1977aa5ccdf2004-06-14 05:10:42 +00001102 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001103
1104 /* If the pager is in RESERVED state, then there must be a copy of this
1105 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001106 ** not the database file. The page is left marked dirty in this case.
1107 **
danielk19772df71c72007-05-24 07:22:42 +00001108 ** An exception to the above rule: If the database is in no-sync mode
1109 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001110 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1111 ** during a Movepage() call, then the page may not be in the cache
1112 ** either. So the condition described in the above paragraph is not
1113 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001114 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001115 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1116 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001117 **
1118 ** Ticket #1171: The statement journal might contain page content that is
1119 ** different from the page content at the start of the transaction.
1120 ** This occurs when a page is changed prior to the start of a statement
1121 ** then changed again within the statement. When rolling back such a
1122 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001123 ** for certain that original page contents are synced into the main rollback
1124 ** journal. Otherwise, a power loss might leave modified data in the
1125 ** database file without an entry in the rollback journal that can
1126 ** restore the database to its original form. Two conditions must be
1127 ** met before writing to the database files. (1) the database must be
1128 ** locked. (2) we know that the original page content is fully synced
1129 ** in the main journal either because the page is not in cache or else
1130 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001131 **
1132 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1133 ** is possible to fail a statement on a database that does not yet exist.
1134 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001135 */
drhae2b40c2004-06-09 19:03:54 +00001136 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001137 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1138 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
danielk19778c0a7912008-08-20 14:49:23 +00001139 if( (pPager->state>=PAGER_EXCLUSIVE)
1140 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
1141 && (pPager->fd->pMethods)
1142 ){
drh281b21d2008-08-22 12:57:08 +00001143 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
1144 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
danielk1977a3f3a5f2004-06-10 04:32:16 +00001145 }
drhfa86c412002-02-02 15:01:15 +00001146 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001147 /* No page should ever be explicitly rolled back that is in use, except
1148 ** for page 1 which is held in use in order to keep the lock on the
1149 ** database active. However such a page may be rolled back as a result
1150 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001151 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001152 */
drhb6f41482004-05-14 01:58:11 +00001153 void *pData;
danielk197728129562005-01-11 10:25:06 +00001154 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
danielk19778c0a7912008-08-20 14:49:23 +00001155 pData = pPg->pData;
drhae2b40c2004-06-09 19:03:54 +00001156 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001157 if( pPager->xReiniter ){
1158 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001159 }
danielk19778c0a7912008-08-20 14:49:23 +00001160 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00001161#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001162 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001163#endif
drh86a88112007-04-16 15:02:19 +00001164 /* If this was page 1, then restore the value of Pager.dbFileVers.
1165 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001166 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001167 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001168 }
drh86a88112007-04-16 15:02:19 +00001169
1170 /* Decode the page just read from disk */
1171 CODEC1(pPager, pData, pPg->pgno, 3);
danielk19778c0a7912008-08-20 14:49:23 +00001172 sqlite3PcacheRelease(pPg);
drhfa86c412002-02-02 15:01:15 +00001173 }
1174 return rc;
1175}
1176
1177/*
danielk197713adf8a2004-06-03 16:08:41 +00001178** Parameter zMaster is the name of a master journal file. A single journal
1179** file that referred to the master journal file has just been rolled back.
1180** This routine checks if it is possible to delete the master journal file,
1181** and does so if it is.
drh726de592004-06-10 23:35:50 +00001182**
danielk197765839c62007-08-30 08:08:17 +00001183** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1184** available for use within this function.
1185**
1186**
drh726de592004-06-10 23:35:50 +00001187** The master journal file contains the names of all child journals.
1188** To tell if a master journal can be deleted, check to each of the
1189** children. If all children are either missing or do not refer to
1190** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001191*/
danielk1977b4b47412007-08-17 15:53:36 +00001192static int pager_delmaster(Pager *pPager, const char *zMaster){
1193 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001194 int rc;
1195 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001196 sqlite3_file *pMaster;
1197 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001198 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001199 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001200
1201 /* Open the master journal file exclusively in case some other process
1202 ** is running this routine also. Not that it makes too much difference.
1203 */
drhe5ae5732008-06-15 02:51:47 +00001204 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001205 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001206 if( !pMaster ){
1207 rc = SQLITE_NOMEM;
1208 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001209 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1210 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001211 }
danielk197713adf8a2004-06-03 16:08:41 +00001212 if( rc!=SQLITE_OK ) goto delmaster_out;
1213 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001214
1215 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001216 if( rc!=SQLITE_OK ) goto delmaster_out;
1217
1218 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001219 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001220 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001221 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001222
1223 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001224 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001225 */
drhe5ae5732008-06-15 02:51:47 +00001226 zMasterJournal = (char *)sqlite3Malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001227 if( !zMasterJournal ){
1228 rc = SQLITE_NOMEM;
1229 goto delmaster_out;
1230 }
danielk197765839c62007-08-30 08:08:17 +00001231 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001232 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001233 if( rc!=SQLITE_OK ) goto delmaster_out;
1234
danielk19775865e3d2004-06-14 06:03:57 +00001235 zJournal = zMasterJournal;
1236 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001237 int exists;
1238 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1239 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001240 goto delmaster_out;
1241 }
danielk1977861f7452008-06-05 11:39:11 +00001242 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001243 /* One of the journals pointed to by the master journal exists.
1244 ** Open it and check if it points at the master journal. If
1245 ** so, return without deleting the master journal file.
1246 */
drh3b7b78b2004-11-24 01:16:43 +00001247 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001248 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1249 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001250 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001251 goto delmaster_out;
1252 }
danielk197713adf8a2004-06-03 16:08:41 +00001253
danielk197765839c62007-08-30 08:08:17 +00001254 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001255 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001256 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001257 goto delmaster_out;
1258 }
danielk197776572402004-06-25 02:38:54 +00001259
danielk197765839c62007-08-30 08:08:17 +00001260 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001261 if( c ){
danielk197776572402004-06-25 02:38:54 +00001262 /* We have a match. Do not delete the master journal file. */
1263 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001264 }
1265 }
danielk19775865e3d2004-06-14 06:03:57 +00001266 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001267 }
1268 }
1269
danielk1977fee2d252007-08-18 10:59:19 +00001270 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001271
1272delmaster_out:
1273 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001274 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001275 }
1276 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001277 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001278 }
danielk1977b4b47412007-08-17 15:53:36 +00001279 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001280 return rc;
1281}
1282
drha6abd042004-06-09 17:37:22 +00001283
danielk1977e180dd92007-04-05 17:15:52 +00001284static void pager_truncate_cache(Pager *pPager);
1285
drha6abd042004-06-09 17:37:22 +00001286/*
drhcb4c40b2004-08-18 19:09:43 +00001287** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001288** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001289**
1290** Might might be the case that the file on disk is smaller than nPage.
1291** This can happen, for example, if we are in the middle of a transaction
1292** which has extended the file size and the new pages are still all held
1293** in cache, then an INSERT or UPDATE does a statement rollback. Some
1294** operating system implementations can get confused if you try to
1295** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001296** so detect this case and write a single zero byte to the end of the new
1297** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001298*/
1299static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001300 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001301 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001302 i64 currentSize, newSize;
1303 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1304 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001305 if( rc==SQLITE_OK && currentSize!=newSize ){
1306 if( currentSize>newSize ){
1307 rc = sqlite3OsTruncate(pPager->fd, newSize);
1308 }else{
1309 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1310 }
drh7fe3f7e2007-11-29 18:44:27 +00001311 }
danielk1977e180dd92007-04-05 17:15:52 +00001312 }
1313 if( rc==SQLITE_OK ){
1314 pPager->dbSize = nPage;
1315 pager_truncate_cache(pPager);
1316 }
1317 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001318}
1319
1320/*
drhc80f0582007-05-01 16:59:48 +00001321** Set the sectorSize for the given pager.
1322**
drh334c80d2008-03-28 17:41:13 +00001323** The sector size is at least as big as the sector size reported
1324** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001325*/
1326static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001327 assert(pPager->fd->pMethods||pPager->tempFile);
1328 if( !pPager->tempFile ){
1329 /* Sector size doesn't matter for temporary files. Also, the file
1330 ** may not have been opened yet, in whcih case the OsSectorSize()
1331 ** call will segfault.
1332 */
1333 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1334 }
drh334c80d2008-03-28 17:41:13 +00001335 if( pPager->sectorSize<512 ){
1336 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001337 }
1338}
1339
1340/*
drhed7c8552001-04-11 14:29:21 +00001341** Playback the journal and thus restore the database file to
1342** the state it was in before we started making changes.
1343**
drh34e79ce2004-02-08 06:05:46 +00001344** The journal file format is as follows:
1345**
drhae2b40c2004-06-09 19:03:54 +00001346** (1) 8 byte prefix. A copy of aJournalMagic[].
1347** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001348** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001349** number of page records from the journal size.
1350** (3) 4 byte big-endian integer which is the initial value for the
1351** sanity checksum.
1352** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001353** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001354** (5) 4 byte big-endian integer which is the sector size. The header
1355** is this many bytes in size.
1356** (6) 4 byte big-endian integer which is the page case.
1357** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001358** name. The value may be zero (indicate that there is no master
1359** journal.)
drh334c80d2008-03-28 17:41:13 +00001360** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001361** and might be shorter than the value read from (5). If the first byte
1362** of the name is \000 then there is no master journal. The master
1363** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001364** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001365** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001366** + pPager->pageSize bytes of data.
1367** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001368**
drh334c80d2008-03-28 17:41:13 +00001369** When we speak of the journal header, we mean the first 8 items above.
1370** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001371**
1372** Call the value from the second bullet "nRec". nRec is the number of
1373** valid page entries in the journal. In most cases, you can compute the
1374** value of nRec from the size of the journal file. But if a power
1375** failure occurred while the journal was being written, it could be the
1376** case that the size of the journal file had already been increased but
1377** the extra entries had not yet made it safely to disk. In such a case,
1378** the value of nRec computed from the file size would be too large. For
1379** that reason, we always use the nRec value in the header.
1380**
1381** If the nRec value is 0xffffffff it means that nRec should be computed
1382** from the file size. This value is used when the user selects the
1383** no-sync option for the journal. A power failure could lead to corruption
1384** in this case. But for things like temporary table (which will be
1385** deleted when the power is restored) we don't care.
1386**
drhd9b02572001-04-15 00:37:09 +00001387** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001388** journal file then all pages up to the first corrupted page are rolled
1389** back (or no pages if the journal header is corrupted). The journal file
1390** is then deleted and SQLITE_OK returned, just as if no corruption had
1391** been encountered.
1392**
1393** If an I/O or malloc() error occurs, the journal-file is not deleted
1394** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001395*/
danielk1977e277be02007-03-23 18:12:06 +00001396static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001397 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001398 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001399 u32 nRec; /* Number of Records in the journal */
shane0b8d2762008-07-22 05:18:00 +00001400 u32 u; /* Unsigned loop counter */
drhd9b02572001-04-15 00:37:09 +00001401 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001402 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001403 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001404 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001405
drhc3a64ba2001-11-22 00:01:27 +00001406 /* Figure out how many records are in the journal. Abort early if
1407 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001408 */
drh8cfbf082001-09-19 13:22:39 +00001409 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001410 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001411 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001412 goto end_playback;
1413 }
drh240c5792004-02-08 00:40:52 +00001414
danielk197776572402004-06-25 02:38:54 +00001415 /* Read the master journal name from the journal, if it is present.
1416 ** If a master journal file name is specified, but the file is not
1417 ** present on disk, then the journal is not hot and does not need to be
1418 ** played back.
drh240c5792004-02-08 00:40:52 +00001419 */
danielk197765839c62007-08-30 08:08:17 +00001420 zMaster = pPager->pTmpSpace;
1421 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001422 if( rc==SQLITE_OK && zMaster[0] ){
1423 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001424 }
danielk197765839c62007-08-30 08:08:17 +00001425 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001426 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001427 goto end_playback;
1428 }
1429 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001430
danielk197776572402004-06-25 02:38:54 +00001431 /* This loop terminates either when the readJournalHdr() call returns
1432 ** SQLITE_DONE or an IO error occurs. */
1433 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001434
danielk197776572402004-06-25 02:38:54 +00001435 /* Read the next journal header from the journal file. If there are
1436 ** not enough bytes left in the journal file for a complete header, or
1437 ** it is corrupted, then a process must of failed while writing it.
1438 ** This indicates nothing more needs to be rolled back.
1439 */
1440 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1441 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001442 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001443 rc = SQLITE_OK;
1444 }
danielk197776572402004-06-25 02:38:54 +00001445 goto end_playback;
1446 }
1447
1448 /* If nRec is 0xffffffff, then this journal was created by a process
1449 ** working in no-sync mode. This means that the rest of the journal
1450 ** file consists of pages, there are no more journal headers. Compute
1451 ** the value of nRec based on this assumption.
1452 */
1453 if( nRec==0xffffffff ){
1454 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1455 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1456 }
1457
danielk1977e277be02007-03-23 18:12:06 +00001458 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001459 ** process and if this is the final header in the journal, then it means
1460 ** that this part of the journal was being filled but has not yet been
1461 ** synced to disk. Compute the number of pages based on the remaining
1462 ** size of the file.
1463 **
1464 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001465 */
drh8940f4e2007-08-11 00:26:20 +00001466 if( nRec==0 && !isHot &&
1467 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001468 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1469 }
1470
danielk197776572402004-06-25 02:38:54 +00001471 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001472 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001473 */
danielk1977e180dd92007-04-05 17:15:52 +00001474 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001475 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001476 if( rc!=SQLITE_OK ){
1477 goto end_playback;
1478 }
danielk197776572402004-06-25 02:38:54 +00001479 }
1480
danielk197776572402004-06-25 02:38:54 +00001481 /* Copy original pages out of the journal and back into the database file.
1482 */
shane0b8d2762008-07-22 05:18:00 +00001483 for(u=0; u<nRec; u++){
danielk197762079062007-08-15 17:08:46 +00001484 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001485 if( rc!=SQLITE_OK ){
1486 if( rc==SQLITE_DONE ){
1487 rc = SQLITE_OK;
1488 pPager->journalOff = szJ;
1489 break;
1490 }else{
1491 goto end_playback;
1492 }
1493 }
drh968af522003-02-11 14:55:40 +00001494 }
drhed7c8552001-04-11 14:29:21 +00001495 }
drh580eeaf2006-02-24 03:09:37 +00001496 /*NOTREACHED*/
1497 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001498
1499end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001500 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001501 zMaster = pPager->pTmpSpace;
1502 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1503 }
1504 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001505 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001506 }
danielk197765839c62007-08-30 08:08:17 +00001507 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001508 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001509 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001510 */
danielk197765839c62007-08-30 08:08:17 +00001511 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001512 }
danielk197776572402004-06-25 02:38:54 +00001513
1514 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001515 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001516 ** value. Reset it to the correct value for this process.
1517 */
drhc80f0582007-05-01 16:59:48 +00001518 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001519 return rc;
drhed7c8552001-04-11 14:29:21 +00001520}
1521
1522/*
drhac69b052004-05-12 13:30:07 +00001523** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001524**
1525** This is similar to playing back the transaction journal but with
1526** a few extra twists.
1527**
drh663fc632002-02-02 18:49:19 +00001528** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001529** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001530** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001531**
drhac69b052004-05-12 13:30:07 +00001532** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001533** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001534** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001535*/
drh3aac2dd2004-04-26 14:10:20 +00001536static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001537 i64 szJ; /* Size of the full journal */
1538 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001539 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001540 int i; /* Loop counter */
1541 int rc;
1542
danielk197776572402004-06-25 02:38:54 +00001543 szJ = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001544
danielk19774099f6e2007-03-19 11:25:20 +00001545 /* Set hdrOff to be the offset just after the end of the last journal
1546 ** page written before the first journal-header for this statement
1547 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001548 ** header was written.
1549 */
1550 hdrOff = pPager->stmtHdrOff;
1551 assert( pPager->fullSync || !hdrOff );
1552 if( !hdrOff ){
1553 hdrOff = szJ;
1554 }
1555
drhfa86c412002-02-02 15:01:15 +00001556 /* Truncate the database back to its original size.
1557 */
danielk1977e180dd92007-04-05 17:15:52 +00001558 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001559 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001560
drhac69b052004-05-12 13:30:07 +00001561 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001562 */
drhac69b052004-05-12 13:30:07 +00001563 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001564 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001565
drhac69b052004-05-12 13:30:07 +00001566 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001567 ** database file. Note that the statement journal omits checksums from
1568 ** each record since power-failure recovery is not important to statement
1569 ** journals.
drhfa86c412002-02-02 15:01:15 +00001570 */
danielk197762079062007-08-15 17:08:46 +00001571 for(i=0; i<nRec; i++){
1572 i64 offset = i*(4+pPager->pageSize);
1573 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001574 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001575 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001576 }
1577
danielk197776572402004-06-25 02:38:54 +00001578 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1579 ** was the size of the journal file when this statement was started, so
1580 ** everything after that needs to be rolled back, either into the
1581 ** database, the memory cache, or both.
1582 **
1583 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1584 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001585 */
danielk197776572402004-06-25 02:38:54 +00001586 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001587 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001588 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001589 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001590 assert( rc!=SQLITE_DONE );
1591 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1592 }
1593
1594 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001595 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001596 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001597 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001598 if( rc!=SQLITE_OK ){
1599 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001600 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001601 }
danielk1977f0113002006-01-24 12:09:17 +00001602 if( nJRec==0 ){
1603 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001604 }
danielk1977f0113002006-01-24 12:09:17 +00001605 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001606 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001607 assert( rc!=SQLITE_DONE );
1608 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1609 }
drhfa86c412002-02-02 15:01:15 +00001610 }
danielk197776572402004-06-25 02:38:54 +00001611
1612 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001613
drh3aac2dd2004-04-26 14:10:20 +00001614end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001615 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001616 pPager->journalOff = szJ;
1617 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001618 }
1619 return rc;
1620}
1621
1622/*
drhf57b14a2001-09-14 18:54:08 +00001623** Change the maximum number of in-memory pages that are allowed.
1624*/
danielk19773b8a05f2007-03-19 17:44:26 +00001625void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +00001626 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001627}
1628
1629/*
drh973b6e32003-02-12 14:09:42 +00001630** Adjust the robustness of the database to damage due to OS crashes
1631** or power failures by changing the number of syncs()s when writing
1632** the rollback journal. There are three levels:
1633**
drh054889e2005-11-30 03:20:31 +00001634** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001635** for temporary and transient files.
1636**
1637** NORMAL The journal is synced once before writes begin on the
1638** database. This is normally adequate protection, but
1639** it is theoretically possible, though very unlikely,
1640** that an inopertune power failure could leave the journal
1641** in a state which would cause damage to the database
1642** when it is rolled back.
1643**
1644** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001645** database (with some additional information - the nRec field
1646** of the journal header - being written in between the two
1647** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001648** single disk sector is atomic, then this mode provides
1649** assurance that the journal will not be corrupted to the
1650** point of causing damage to the database during rollback.
1651**
1652** Numeric values associated with these states are OFF==1, NORMAL=2,
1653** and FULL=3.
1654*/
danielk197793758c82005-01-21 08:13:14 +00001655#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh281b21d2008-08-22 12:57:08 +00001656void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
drh973b6e32003-02-12 14:09:42 +00001657 pPager->noSync = level==1 || pPager->tempFile;
1658 pPager->fullSync = level==3 && !pPager->tempFile;
drh281b21d2008-08-22 12:57:08 +00001659 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001660 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001661}
danielk197793758c82005-01-21 08:13:14 +00001662#endif
drh973b6e32003-02-12 14:09:42 +00001663
1664/*
drhaf6df112005-06-07 02:12:30 +00001665** The following global variable is incremented whenever the library
1666** attempts to open a temporary file. This information is used for
1667** testing and analysis only.
1668*/
drh0f7eb612006-08-08 13:51:43 +00001669#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001670int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001671#endif
drhaf6df112005-06-07 02:12:30 +00001672
1673/*
drh3f56e6e2007-03-15 01:16:47 +00001674** Open a temporary file.
1675**
1676** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001677** other error code if we fail. The OS will automatically delete the temporary
1678** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001679*/
danielk1977b4b47412007-08-17 15:53:36 +00001680static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00001681 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00001682 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00001683 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00001684){
drhfa86c412002-02-02 15:01:15 +00001685 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001686
drh0f7eb612006-08-08 13:51:43 +00001687#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001688 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001689#endif
danielk1977b4b47412007-08-17 15:53:36 +00001690
drh33f4e022007-09-03 15:19:34 +00001691 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1692 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00001693 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00001694 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00001695 return rc;
1696}
1697
danielk1977a858aa22008-08-22 16:22:17 +00001698static int pagerStress(void *,PgHdr *);
danielk19778c0a7912008-08-20 14:49:23 +00001699
drhfa86c412002-02-02 15:01:15 +00001700/*
drhed7c8552001-04-11 14:29:21 +00001701** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001702** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001703** the first call to sqlite3PagerGet() and is only held open until the
1704** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001705**
drh6446c4d2001-12-15 14:22:18 +00001706** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00001707** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00001708** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001709**
1710** If zFilename is ":memory:" then all information is held in cache.
1711** It is never written to disk. This can be used to implement an
1712** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001713*/
danielk19773b8a05f2007-03-19 17:44:26 +00001714int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001715 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001716 Pager **ppPager, /* Return the Pager structure here */
1717 const char *zFilename, /* Name of the database file to open */
danielk19778c0a7912008-08-20 14:49:23 +00001718 void (*xDesc)(DbPage*), /* Page destructor function */
drhda47d772002-12-02 04:25:19 +00001719 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00001720 int flags, /* flags controlling this file */
1721 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00001722){
danielk1977b4b47412007-08-17 15:53:36 +00001723 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001724 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001725 int rc = SQLITE_OK;
1726 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001727 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001728 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001729 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001730 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1731 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00001732 int journalFileSize = sqlite3JournalSize(pVfs);
danielk19778c0a7912008-08-20 14:49:23 +00001733 int pcacheSize = sqlite3PcacheSize();
drhfacf0302008-06-17 15:12:00 +00001734 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
danielk197717b90b52008-06-06 11:11:25 +00001735 char *zPathname = 0;
1736 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001737
drh86f8c192007-08-22 00:39:19 +00001738 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001739 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001740
danielk197717b90b52008-06-06 11:11:25 +00001741 /* Compute and store the full pathname in an allocated buffer pointed
1742 ** to by zPathname, length nPathname. Or, if this is a temporary file,
1743 ** leave both nPathname and zPathname set to 0.
1744 */
drh1cc8c442007-08-24 16:08:29 +00001745 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00001746 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001747 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00001748 if( zPathname==0 ){
1749 return SQLITE_NOMEM;
1750 }
drh1cc8c442007-08-24 16:08:29 +00001751#ifndef SQLITE_OMIT_MEMORYDB
1752 if( strcmp(zFilename,":memory:")==0 ){
1753 memDb = 1;
1754 zPathname[0] = 0;
1755 }else
1756#endif
1757 {
danielk1977adfb9b02007-09-17 07:02:56 +00001758 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00001759 }
danielk197717b90b52008-06-06 11:11:25 +00001760 if( rc!=SQLITE_OK ){
1761 sqlite3_free(zPathname);
1762 return rc;
1763 }
1764 nPathname = strlen(zPathname);
drhae28c012007-08-24 16:29:23 +00001765 }
drh99b90c32008-03-17 13:50:58 +00001766
danielk1977b4b47412007-08-17 15:53:36 +00001767 /* Allocate memory for the pager structure */
1768 pPager = sqlite3MallocZero(
1769 sizeof(*pPager) + /* Pager structure */
danielk19778c0a7912008-08-20 14:49:23 +00001770 pcacheSize + /* PCache object */
danielk1977c7b60172007-08-22 11:22:03 +00001771 journalFileSize + /* The journal file structure */
drh17c0ba22008-02-26 18:40:11 +00001772 pVfs->szOsFile * 3 + /* The main db and two journal files */
danielk197717b90b52008-06-06 11:11:25 +00001773 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00001774 );
1775 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00001776 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00001777 return SQLITE_NOMEM;
1778 }
danielk19778c0a7912008-08-20 14:49:23 +00001779 pPager->pPCache = (PCache *)&pPager[1];
1780 pPtr = ((u8 *)&pPager[1]) + pcacheSize;
drh33f4e022007-09-03 15:19:34 +00001781 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00001782 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00001783 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
1784 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
1785 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00001786 pPager->zDirectory = &pPager->zFilename[nPathname+1];
1787 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00001788 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00001789 if( zPathname ){
1790 memcpy(pPager->zFilename, zPathname, nPathname+1);
1791 sqlite3_free(zPathname);
1792 }
danielk1977b4b47412007-08-17 15:53:36 +00001793
drh153c62c2007-08-24 03:51:33 +00001794 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00001795 */
drhae28c012007-08-24 16:29:23 +00001796 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00001797 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
1798 rc = SQLITE_CANTOPEN;
1799 }else{
drh1cc8c442007-08-24 16:08:29 +00001800 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00001801 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
1802 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00001803 readOnly = (fout&SQLITE_OPEN_READONLY);
1804
1805 /* If the file was successfully opened for read/write access,
1806 ** choose a default page size in case we have to create the
1807 ** database file. The default page size is the maximum of:
1808 **
1809 ** + SQLITE_DEFAULT_PAGE_SIZE,
1810 ** + The value returned by sqlite3OsSectorSize()
1811 ** + The largest page size that can be written atomically.
1812 */
1813 if( rc==SQLITE_OK && !readOnly ){
1814 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
drhfacf0302008-06-17 15:12:00 +00001815 if( szPageDflt<iSectorSize ){
1816 szPageDflt = iSectorSize;
drh1cc8c442007-08-24 16:08:29 +00001817 }
danielk19779663b8f2007-08-24 11:52:28 +00001818#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00001819 {
1820 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
1821 int ii;
1822 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
1823 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
1824 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
drhfacf0302008-06-17 15:12:00 +00001825 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
1826 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00001827 }
danielk1977b4b47412007-08-17 15:53:36 +00001828 }
drh1cc8c442007-08-24 16:08:29 +00001829#endif
drhfacf0302008-06-17 15:12:00 +00001830 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
1831 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
drh1cc8c442007-08-24 16:08:29 +00001832 }
danielk19778def5ea2004-06-16 10:39:52 +00001833 }
drhac69b052004-05-12 13:30:07 +00001834 }
drh1cc8c442007-08-24 16:08:29 +00001835 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001836 /* If a temporary file is requested, it is not opened immediately.
1837 ** In this case we accept the default page size and delay actually
1838 ** opening the file until the first call to OsWrite().
1839 */
danielk19777a2b1ee2007-08-21 14:27:01 +00001840 tempFile = 1;
1841 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00001842 }
danielk1977aef0bf62005-12-30 16:28:01 +00001843
danielk1977b4b47412007-08-17 15:53:36 +00001844 if( pPager && rc==SQLITE_OK ){
drhfacf0302008-06-17 15:12:00 +00001845 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00001846 }
danielk1977aef0bf62005-12-30 16:28:01 +00001847
drh153c62c2007-08-24 03:51:33 +00001848 /* If an error occured in either of the blocks above.
1849 ** Free the Pager structure and close the file.
1850 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00001851 ** any Pager.errMask variables.
1852 */
danielk1977b4b47412007-08-17 15:53:36 +00001853 if( !pPager || !pPager->pTmpSpace ){
1854 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00001855 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00001856 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001857 }
danielk19778c0a7912008-08-20 14:49:23 +00001858 nExtra = FORCE_ALIGNMENT(nExtra);
drh41d30272008-08-20 21:47:45 +00001859 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb, xDesc,
1860 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
danielk1977aef0bf62005-12-30 16:28:01 +00001861
drh153c62c2007-08-24 03:51:33 +00001862 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
1863 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00001864
danielk1977b4b47412007-08-17 15:53:36 +00001865 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00001866 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00001867 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00001868 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001869
drh99b90c32008-03-17 13:50:58 +00001870 /* Fill in Pager.zJournal[] */
danielk197717b90b52008-06-06 11:11:25 +00001871 if( zPathname ){
1872 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
1873 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
1874 }else{
1875 pPager->zJournal = 0;
1876 }
danielk1977b4b47412007-08-17 15:53:36 +00001877
drh3b59a5c2006-01-15 20:28:28 +00001878 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00001879 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00001880 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001881 /* pPager->stmtOpen = 0; */
1882 /* pPager->stmtInUse = 0; */
1883 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00001884 pPager->dbSize = memDb-1;
drhfacf0302008-06-17 15:12:00 +00001885 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00001886 /* pPager->stmtSize = 0; */
1887 /* pPager->stmtJSize = 0; */
1888 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00001889 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00001890 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00001891 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00001892 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00001893 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00001894 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00001895 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
1896 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
1897 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
1898 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00001899 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001900 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001901 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00001902 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00001903 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00001904 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00001905 /* pPager->pFirst = 0; */
1906 /* pPager->pFirstSynced = 0; */
1907 /* pPager->pLast = 0; */
danielk19778c0a7912008-08-20 14:49:23 +00001908 pPager->nExtra = nExtra;
danielk1977b53e4962008-06-04 06:45:59 +00001909 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk19777a2b1ee2007-08-21 14:27:01 +00001910 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00001911 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00001912 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00001913 }
drh3b59a5c2006-01-15 20:28:28 +00001914 /* pPager->pBusyHandler = 0; */
1915 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00001916 *ppPager = pPager;
1917 return SQLITE_OK;
1918}
1919
1920/*
drh90f5ecb2004-07-22 01:19:35 +00001921** Set the busy handler function.
1922*/
danielk19773b8a05f2007-03-19 17:44:26 +00001923void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00001924 pPager->pBusyHandler = pBusyHandler;
1925}
1926
1927/*
drha6abd042004-06-09 17:37:22 +00001928** Set the reinitializer for this pager. If not NULL, the reinitializer
1929** is called when the content of a page in cache is restored to its original
1930** value as a result of a rollback. The callback gives higher-level code
1931** an opportunity to restore the EXTRA section to agree with the restored
1932** page data.
1933*/
danielk19773b8a05f2007-03-19 17:44:26 +00001934void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00001935 pPager->xReiniter = xReinit;
1936}
1937
1938/*
danielk1977a1644fd2007-08-29 12:31:25 +00001939** Set the page size to *pPageSize. If the suggest new page size is
1940** inappropriate, then an alternative page size is set to that
1941** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00001942*/
danielk1977a1644fd2007-08-29 12:31:25 +00001943int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
1944 int rc = SQLITE_OK;
1945 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00001946 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00001947 if( pageSize && pageSize!=pPager->pageSize
drh7426f862008-08-26 21:07:26 +00001948 && (pPager->memDb==0 || pPager->dbSize==0)
1949 && sqlite3PcacheRefCount(pPager->pPCache)==0
danielk1977a1644fd2007-08-29 12:31:25 +00001950 ){
drhfacf0302008-06-17 15:12:00 +00001951 char *pNew = (char *)sqlite3PageMalloc(pageSize);
danielk1977a1644fd2007-08-29 12:31:25 +00001952 if( !pNew ){
1953 rc = SQLITE_NOMEM;
1954 }else{
danielk1977a1644fd2007-08-29 12:31:25 +00001955 pager_reset(pPager);
1956 pPager->pageSize = pageSize;
drh7426f862008-08-26 21:07:26 +00001957 if( !pPager->memDb ) setSectorSize(pPager);
drhfacf0302008-06-17 15:12:00 +00001958 sqlite3PageFree(pPager->pTmpSpace);
danielk1977a1644fd2007-08-29 12:31:25 +00001959 pPager->pTmpSpace = pNew;
danielk19778c0a7912008-08-20 14:49:23 +00001960 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
danielk1977a1644fd2007-08-29 12:31:25 +00001961 }
drh1c7880e2005-05-20 20:01:55 +00001962 }
danielk1977a1644fd2007-08-29 12:31:25 +00001963 *pPageSize = pPager->pageSize;
1964 return rc;
drh90f5ecb2004-07-22 01:19:35 +00001965}
1966
1967/*
drh26b79942007-11-28 16:19:56 +00001968** Return a pointer to the "temporary page" buffer held internally
1969** by the pager. This is a buffer that is big enough to hold the
1970** entire content of a database page. This buffer is used internally
1971** during rollback and will be overwritten whenever a rollback
1972** occurs. But other modules are free to use it too, as long as
1973** no rollbacks are happening.
1974*/
1975void *sqlite3PagerTempSpace(Pager *pPager){
1976 return pPager->pTmpSpace;
1977}
1978
1979/*
drhf8e632b2007-05-08 14:51:36 +00001980** Attempt to set the maximum database page count if mxPage is positive.
1981** Make no changes if mxPage is zero or negative. And never reduce the
1982** maximum page count below the current size of the database.
1983**
1984** Regardless of mxPage, return the current maximum page count.
1985*/
1986int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
1987 if( mxPage>0 ){
1988 pPager->mxPgno = mxPage;
1989 }
danielk1977ad0132d2008-06-07 08:58:22 +00001990 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00001991 return pPager->mxPgno;
1992}
1993
1994/*
drhc9ac5ca2005-11-04 22:03:30 +00001995** The following set of routines are used to disable the simulated
1996** I/O error mechanism. These routines are used to avoid simulated
1997** errors in places where we do not care about errors.
1998**
1999** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2000** and generate no code.
2001*/
2002#ifdef SQLITE_TEST
2003extern int sqlite3_io_error_pending;
2004extern int sqlite3_io_error_hit;
2005static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002006void disable_simulated_io_errors(void){
2007 saved_cnt = sqlite3_io_error_pending;
2008 sqlite3_io_error_pending = -1;
2009}
2010void enable_simulated_io_errors(void){
2011 sqlite3_io_error_pending = saved_cnt;
2012}
2013#else
drh152410f2005-11-05 15:11:22 +00002014# define disable_simulated_io_errors()
2015# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002016#endif
2017
2018/*
drh90f5ecb2004-07-22 01:19:35 +00002019** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002020** that pDest points to.
2021**
2022** No error checking is done. The rational for this is that this function
2023** may be called even if the file does not exist or contain a header. In
2024** these cases sqlite3OsRead() will return an error, to which the correct
2025** response is to zero the memory at pDest and continue. A real IO error
2026** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002027*/
danielk19773b8a05f2007-03-19 17:44:26 +00002028int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002029 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002030 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002031 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2032 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002033 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002034 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002035 if( rc==SQLITE_IOERR_SHORT_READ ){
2036 rc = SQLITE_OK;
2037 }
drh90f5ecb2004-07-22 01:19:35 +00002038 }
drh551b7732006-11-06 21:20:25 +00002039 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002040}
2041
2042/*
drh5e00f6c2001-09-13 13:46:56 +00002043** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002044** pPager.
2045**
2046** If the PENDING_BYTE lies on the page directly after the end of the
2047** file, then consider this page part of the file too. For example, if
2048** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2049** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002050*/
danielk1977ad0132d2008-06-07 08:58:22 +00002051int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk19777a2b1ee2007-08-21 14:27:01 +00002052 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002053 int rc;
drhd9b02572001-04-15 00:37:09 +00002054 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002055 if( pPager->errCode ){
danielk19778c0a7912008-08-20 14:49:23 +00002056 rc = pPager->errCode;
danielk19778c0a7912008-08-20 14:49:23 +00002057 return rc;
drha7aea3d2007-03-15 12:51:16 +00002058 }
drhed7c8552001-04-11 14:29:21 +00002059 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002060 n = pPager->dbSize;
2061 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002062 assert(pPager->fd->pMethods||pPager->tempFile);
2063 if( (pPager->fd->pMethods)
2064 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002065 pager_error(pPager, rc);
danielk1977ad0132d2008-06-07 08:58:22 +00002066 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002067 }
2068 if( n>0 && n<pPager->pageSize ){
2069 n = 1;
2070 }else{
2071 n /= pPager->pageSize;
2072 }
2073 if( pPager->state!=PAGER_UNLOCK ){
2074 pPager->dbSize = n;
2075 }
drhed7c8552001-04-11 14:29:21 +00002076 }
danielk197715f411d2005-09-16 10:18:45 +00002077 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002078 n++;
2079 }
drhf8e632b2007-05-08 14:51:36 +00002080 if( n>pPager->mxPgno ){
2081 pPager->mxPgno = n;
2082 }
danielk1977ad0132d2008-06-07 08:58:22 +00002083 if( pnPage ){
2084 *pnPage = n;
2085 }
2086 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002087}
2088
2089/*
drhf7c57532003-04-25 13:22:51 +00002090** Forward declaration
2091*/
danielk197776572402004-06-25 02:38:54 +00002092static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002093
2094/*
danielk1977e180dd92007-04-05 17:15:52 +00002095** This routine is used to truncate the cache when a database
2096** is truncated. Drop from the cache all pages whose pgno is
2097** larger than pPager->dbSize and is unreferenced.
2098**
drhac69b052004-05-12 13:30:07 +00002099** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002100**
2101** Actually, at the point this routine is called, it would be
2102** an error to have a referenced page. But rather than delete
2103** that page and guarantee a subsequent segfault, it seems better
2104** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002105*/
danielk1977e180dd92007-04-05 17:15:52 +00002106static void pager_truncate_cache(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00002107 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
drhac69b052004-05-12 13:30:07 +00002108}
2109
drhf7c57532003-04-25 13:22:51 +00002110/*
danielk197717221812005-02-15 03:38:05 +00002111** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002112** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002113** false or until the lock succeeds.
2114**
2115** Return SQLITE_OK on success and an error code if we cannot obtain
2116** the lock.
2117*/
2118static int pager_wait_on_lock(Pager *pPager, int locktype){
2119 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002120
2121 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002122 assert( PAGER_SHARED==SHARED_LOCK );
2123 assert( PAGER_RESERVED==RESERVED_LOCK );
2124 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002125
2126 /* If the file is currently unlocked then the size must be unknown */
2127 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2128
danielk197717221812005-02-15 03:38:05 +00002129 if( pPager->state>=locktype ){
2130 rc = SQLITE_OK;
2131 }else{
drhbefdf832008-03-14 19:33:05 +00002132 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002133 do {
drh054889e2005-11-30 03:20:31 +00002134 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002135 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002136 if( rc==SQLITE_OK ){
2137 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002138 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002139 }
2140 }
2141 return rc;
2142}
2143
2144/*
drhf7c57532003-04-25 13:22:51 +00002145** Truncate the file to the number of pages specified.
2146*/
danielk19773b8a05f2007-03-19 17:44:26 +00002147int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
danielk19778c0a7912008-08-20 14:49:23 +00002148 int rc = SQLITE_OK;
drh1aa2d8b2007-01-03 15:34:29 +00002149 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00002150
danielk19778c0a7912008-08-20 14:49:23 +00002151
danielk1977ad0132d2008-06-07 08:58:22 +00002152 sqlite3PagerPagecount(pPager, 0);
danielk1977efaaf572006-01-16 11:29:19 +00002153 if( pPager->errCode ){
2154 rc = pPager->errCode;
danielk19778c0a7912008-08-20 14:49:23 +00002155 }else if( nPage<(unsigned)pPager->dbSize ){
2156 if( MEMDB ){
2157 pPager->dbSize = nPage;
2158 pager_truncate_cache(pPager);
2159 }else{
2160 rc = syncJournal(pPager);
2161 if( rc==SQLITE_OK ){
2162 /* Get an exclusive lock on the database before truncating. */
2163 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2164 }
2165 if( rc==SQLITE_OK ){
2166 rc = pager_truncate(pPager, nPage);
2167 }
2168 }
danielk1977369f27e2004-06-15 11:40:04 +00002169 }
danielk197717221812005-02-15 03:38:05 +00002170
drhf7c57532003-04-25 13:22:51 +00002171 return rc;
2172}
2173
2174/*
drhed7c8552001-04-11 14:29:21 +00002175** Shutdown the page cache. Free all memory and close all files.
2176**
2177** If a transaction was in progress when this routine is called, that
2178** transaction is rolled back. All outstanding pages are invalidated
2179** and their memory is freed. Any attempt to use a page associated
2180** with this page cache after this function returns will likely
2181** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002182**
2183** This function always succeeds. If a transaction is active an attempt
2184** is made to roll it back. If an error occurs during the rollback
2185** a hot journal may be left in the filesystem but no error is returned
2186** to the caller.
drhed7c8552001-04-11 14:29:21 +00002187*/
danielk19773b8a05f2007-03-19 17:44:26 +00002188int sqlite3PagerClose(Pager *pPager){
danielk197713f72992005-12-18 08:51:22 +00002189
drhbafda092007-01-03 23:36:22 +00002190 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002191 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002192 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002193 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002194 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002195 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002196 enable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002197 sqlite3EndBenignMalloc();
drh4f0c5872007-03-26 22:05:01 +00002198 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002199 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002200 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002201 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002202 }
drhf5e7bb52008-02-18 14:47:33 +00002203 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002204 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002205 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002206 }
danielk1977b4b47412007-08-17 15:53:36 +00002207 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002208 /* Temp files are automatically deleted by the OS
2209 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002210 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002211 ** }
2212 */
danielk1977aca790a2005-01-13 11:07:52 +00002213
drhfacf0302008-06-17 15:12:00 +00002214 sqlite3PageFree(pPager->pTmpSpace);
danielk19778c0a7912008-08-20 14:49:23 +00002215 sqlite3PcacheClose(pPager->pPCache);
drh17435752007-08-16 04:30:38 +00002216 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002217 return SQLITE_OK;
2218}
2219
drh87cc3b32007-05-08 21:45:27 +00002220#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002221/*
drh5e00f6c2001-09-13 13:46:56 +00002222** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002223*/
danielk19773b8a05f2007-03-19 17:44:26 +00002224Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002225 return p->pgno;
2226}
drh87cc3b32007-05-08 21:45:27 +00002227#endif
drhed7c8552001-04-11 14:29:21 +00002228
2229/*
drhdf0b3b02001-06-23 11:36:20 +00002230** Increment the reference count for a page. The input pointer is
2231** a reference to the page data.
2232*/
danielk19773b8a05f2007-03-19 17:44:26 +00002233int sqlite3PagerRef(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002234 sqlite3PcacheRef(pPg);
drh8c42ca92001-06-22 19:15:00 +00002235 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002236}
2237
2238/*
drh34e79ce2004-02-08 06:05:46 +00002239** Sync the journal. In other words, make sure all the pages that have
2240** been written to the journal have actually reached the surface of the
2241** disk. It is not safe to modify the original database file until after
2242** the journal has been synced. If the original database is modified before
2243** the journal is synced and a power failure occurs, the unsynced journal
2244** data would be lost and we would be unable to completely rollback the
2245** database changes. Database corruption would occur.
2246**
2247** This routine also updates the nRec field in the header of the journal.
2248** (See comments on the pager_playback() routine for additional information.)
2249** If the sync mode is FULL, two syncs will occur. First the whole journal
2250** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002251**
drh34e79ce2004-02-08 06:05:46 +00002252** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002253** after a power failure, so no sync occurs.
2254**
2255** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2256** the database is stored, then OsSync() is never called on the journal
2257** file. In this case all that is required is to update the nRec field in
2258** the journal header.
drhfa86c412002-02-02 15:01:15 +00002259**
drh34e79ce2004-02-08 06:05:46 +00002260** This routine clears the needSync field of every page current held in
2261** memory.
drh50e5dad2001-09-15 00:57:28 +00002262*/
danielk197776572402004-06-25 02:38:54 +00002263static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002264 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002265
2266 /* Sync the journal before modifying the main database
2267 ** (assuming there is a journal and it needs to be synced.)
2268 */
danielk197776572402004-06-25 02:38:54 +00002269 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002270 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002271 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002272 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002273
danielk19774cd2cd52007-08-23 14:48:23 +00002274 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002275 /* Write the nRec value into the journal file header. If in
2276 ** full-synchronous mode, sync the journal first. This ensures that
2277 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002278 ** it as a candidate for rollback.
2279 **
2280 ** This is not required if the persistent media supports the
2281 ** SAFE_APPEND property. Because in this case it is not possible
2282 ** for garbage data to be appended to the file, the nRec field
2283 ** is populated with 0xFFFFFFFF when the journal header is written
2284 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002285 */
danielk19774cd2cd52007-08-23 14:48:23 +00002286 i64 jrnlOff;
2287 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002288 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002289 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002290 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002291 if( rc!=0 ) return rc;
2292 }
danielk197713adf8a2004-06-03 16:08:41 +00002293
danielk197762079062007-08-15 17:08:46 +00002294 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2295 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2296 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002297 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002298 }
danielk19774cd2cd52007-08-23 14:48:23 +00002299 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2300 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2301 IOTRACE(("JSYNC %p\n", pPager))
2302 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2303 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2304 );
2305 if( rc!=0 ) return rc;
2306 }
drhdb48ee02003-01-16 13:42:43 +00002307 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002308 }
drh50e5dad2001-09-15 00:57:28 +00002309 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002310
2311 /* Erase the needSync flag from every page.
2312 */
danielk19778c0a7912008-08-20 14:49:23 +00002313 sqlite3PcacheSetFlags(pPager->pPCache, ~PGHDR_NEED_SYNC, 0);
2314 /* lruListSetFirstSynced(pPager); */
drh50e5dad2001-09-15 00:57:28 +00002315 }
drh03eb96a2002-11-10 23:32:56 +00002316
drh341eae82003-01-21 02:39:36 +00002317#ifndef NDEBUG
2318 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2319 ** flag must also be clear for all pages. Verify that this
2320 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002321 */
drh341eae82003-01-21 02:39:36 +00002322 else{
danielk19778c0a7912008-08-20 14:49:23 +00002323 sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_NEED_SYNC);
2324 /* assert( pPager->lru.pFirstSynced==pPager->lru.pFirst ); */
drh03eb96a2002-11-10 23:32:56 +00002325 }
drh341eae82003-01-21 02:39:36 +00002326#endif
drhdb48ee02003-01-16 13:42:43 +00002327
drh81a20f22001-10-12 17:30:04 +00002328 return rc;
drh50e5dad2001-09-15 00:57:28 +00002329}
2330
2331/*
drh2554f8b2003-01-22 01:26:44 +00002332** Given a list of pages (connected by the PgHdr.pDirty pointer) write
danielk1977a858aa22008-08-22 16:22:17 +00002333** every one of those pages out to the database file. No calls are made
2334** to the page-cache to mark the pages as clean. It is the responsibility
2335** of the caller to use PcacheCleanAll() or PcacheMakeClean() to mark
2336** the pages as clean.
drh2554f8b2003-01-22 01:26:44 +00002337*/
2338static int pager_write_pagelist(PgHdr *pList){
2339 Pager *pPager;
2340 int rc;
2341
2342 if( pList==0 ) return SQLITE_OK;
2343 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002344
2345 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2346 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002347 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002348 **
drha6abd042004-06-09 17:37:22 +00002349 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2350 ** through an intermediate state PENDING. A PENDING lock prevents new
2351 ** readers from attaching to the database but is unsufficient for us to
2352 ** write. The idea of a PENDING lock is to prevent new readers from
2353 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002354 **
drha6abd042004-06-09 17:37:22 +00002355 ** While the pager is in the RESERVED state, the original database file
2356 ** is unchanged and we can rollback without having to playback the
2357 ** journal into the original database file. Once we transition to
2358 ** EXCLUSIVE, it means the database file has been changed and any rollback
2359 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002360 */
drh684917c2004-10-05 02:41:42 +00002361 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002362 if( rc!=SQLITE_OK ){
2363 return rc;
2364 }
2365
drh2554f8b2003-01-22 01:26:44 +00002366 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002367
2368 /* If the file has not yet been opened, open it now. */
2369 if( !pPager->fd->pMethods ){
2370 assert(pPager->tempFile);
danielk197717b90b52008-06-06 11:11:25 +00002371 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002372 if( rc ) return rc;
2373 }
2374
danielk1977687566d2004-11-02 12:56:41 +00002375 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002376 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002377 ** make the file smaller (presumably by auto-vacuum code). Do not write
2378 ** any such pages to the file.
2379 */
danielk197733e32162008-08-23 18:53:08 +00002380 if( pList->pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
danielk197762079062007-08-15 17:08:46 +00002381 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002382 char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002383 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2384 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002385 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002386 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002387 PAGER_INCR(sqlite3_pager_writedb_count);
2388 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002389 if( pList->pgno==1 ){
2390 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2391 }
danielk1977687566d2004-11-02 12:56:41 +00002392 }
2393#ifndef NDEBUG
2394 else{
drh4f0c5872007-03-26 22:05:01 +00002395 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002396 }
2397#endif
drh2554f8b2003-01-22 01:26:44 +00002398 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002399#ifdef SQLITE_CHECK_PAGES
2400 pList->pageHash = pager_pagehash(pList);
2401#endif
danielk1977a858aa22008-08-22 16:22:17 +00002402 /* makeClean(pList); */
drh2554f8b2003-01-22 01:26:44 +00002403 pList = pList->pDirty;
2404 }
danielk19778c0a7912008-08-20 14:49:23 +00002405
2406 /* sqlite3PcacheCleanAll(pPager->pPCache); */
drh2554f8b2003-01-22 01:26:44 +00002407 return SQLITE_OK;
2408}
2409
2410/*
danielk19778c0a7912008-08-20 14:49:23 +00002411** This function is called by the pcache layer when it has reached some
2412** soft memory limit. The argument is a pointer to a purgeable Pager
2413** object. This function attempts to make a single dirty page that has no
2414** outstanding references (if one exists) clean so that it can be recycled
2415** by the pcache layer.
drh2554f8b2003-01-22 01:26:44 +00002416*/
danielk1977a858aa22008-08-22 16:22:17 +00002417static int pagerStress(void *p, PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002418 Pager *pPager = (Pager *)p;
danielk19778c0a7912008-08-20 14:49:23 +00002419 int rc = SQLITE_OK;
drh8f2e9a12008-02-26 14:46:04 +00002420
danielk19778c200142008-08-25 07:12:28 +00002421 if( pPager->doNotSync ){
2422 return SQLITE_OK;
2423 }
2424
danielk1977a858aa22008-08-22 16:22:17 +00002425 assert( pPg->flags&PGHDR_DIRTY );
2426 if( pPager->errCode==SQLITE_OK ){
2427 if( pPg->flags&PGHDR_NEED_SYNC ){
2428 rc = syncJournal(pPager);
2429 if( rc==SQLITE_OK && pPager->fullSync &&
2430 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
2431 ){
2432 pPager->nRec = 0;
2433 rc = writeJournalHdr(pPager);
danielk19778c0a7912008-08-20 14:49:23 +00002434 }
danielk19778c0a7912008-08-20 14:49:23 +00002435 }
danielk1977a858aa22008-08-22 16:22:17 +00002436 if( rc==SQLITE_OK ){
2437 pPg->pDirty = 0;
2438 rc = pager_write_pagelist(pPg);
2439 }
2440 if( rc!=SQLITE_OK ){
2441 pager_error(pPager, rc);
2442 }
2443 }
2444
2445 if( rc==SQLITE_OK ){
2446 sqlite3PcacheMakeClean(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00002447 }
2448 return rc;
drh2554f8b2003-01-22 01:26:44 +00002449}
2450
danielk19778c0a7912008-08-20 14:49:23 +00002451
drh2554f8b2003-01-22 01:26:44 +00002452/*
drh19db9352008-03-27 22:42:51 +00002453** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00002454** A hot journal is one that needs to be played back.
2455**
2456** If the current size of the database file is 0 but a journal file
2457** exists, that is probably an old journal left over from a prior
2458** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00002459**
2460** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00002461**
2462** This routine does not open the journal file to examine its
2463** content. Hence, the journal might contain the name of a master
2464** journal file that has been deleted, and hence not be hot. Or
2465** the header of the journal might be zeroed out. This routine
2466** does not discover these cases of a non-hot journal - if the
2467** journal file exists and is not empty this routine assumes it
2468** is hot. The pager_playback() routine will discover that the
2469** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00002470*/
danielk1977d300b8a2008-08-01 10:50:23 +00002471static int hasHotJournal(Pager *pPager, int *pExists){
danielk1977b4b47412007-08-17 15:53:36 +00002472 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977d300b8a2008-08-01 10:50:23 +00002473 int rc = SQLITE_OK;
drh0a846f92008-08-25 17:23:29 +00002474 int exists;
2475 int locked;
2476 assert( pPager!=0 );
2477 assert( pPager->useJournal );
2478 assert( pPager->fd->pMethods );
danielk1977d300b8a2008-08-01 10:50:23 +00002479 *pExists = 0;
drh0a846f92008-08-25 17:23:29 +00002480 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
2481 if( rc==SQLITE_OK && exists ){
2482 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
2483 }
2484 if( rc==SQLITE_OK && exists && !locked ){
2485 int nPage;
2486 rc = sqlite3PagerPagecount(pPager, &nPage);
2487 if( rc==SQLITE_OK ){
2488 if( nPage==0 ){
2489 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2490 }else{
2491 *pExists = 1;
danielk1977861f7452008-06-05 11:39:11 +00002492 }
2493 }
drhbb5f18d2007-04-06 18:23:17 +00002494 }
danielk1977d300b8a2008-08-01 10:50:23 +00002495 return rc;
drh165ffe92005-03-15 17:09:30 +00002496}
2497
2498/*
danielk1977e180dd92007-04-05 17:15:52 +00002499** Read the content of page pPg out of the database file.
2500*/
2501static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2502 int rc;
danielk197762079062007-08-15 17:08:46 +00002503 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002504 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002505 assert(pPager->fd->pMethods||pPager->tempFile);
2506 if( !pPager->fd->pMethods ){
2507 return SQLITE_IOERR_SHORT_READ;
2508 }
danielk197762079062007-08-15 17:08:46 +00002509 offset = (pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002510 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002511 PAGER_INCR(sqlite3_pager_readdb_count);
2512 PAGER_INCR(pPager->nRead);
2513 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002514 if( pgno==1 ){
danielk19778c0a7912008-08-20 14:49:23 +00002515 memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24],
drh86a88112007-04-16 15:02:19 +00002516 sizeof(pPager->dbFileVers));
2517 }
danielk197730849522008-08-21 04:35:18 +00002518 CODEC1(pPager, pPg->pData, pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00002519 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
2520 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00002521 return rc;
2522}
2523
2524
2525/*
danielk1977e277be02007-03-23 18:12:06 +00002526** This function is called to obtain the shared lock required before
2527** data may be read from the pager cache. If the shared lock has already
2528** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002529**
2530** Immediately after obtaining the shared lock (if required), this function
2531** checks for a hot-journal file. If one is found, an emergency rollback
2532** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002533*/
2534static int pagerSharedLock(Pager *pPager){
2535 int rc = SQLITE_OK;
danielk1977d300b8a2008-08-01 10:50:23 +00002536 int isErrorReset = 0;
danielk1977e277be02007-03-23 18:12:06 +00002537
danielk1977ae72d982007-10-03 08:46:44 +00002538 /* If this database is opened for exclusive access, has no outstanding
2539 ** page references and is in an error-state, now is the chance to clear
2540 ** the error. Discard the contents of the pager-cache and treat any
2541 ** open journal file as a hot-journal.
2542 */
danielk19778c0a7912008-08-20 14:49:23 +00002543 if( !MEMDB && pPager->exclusiveMode
2544 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
2545 ){
danielk1977ae72d982007-10-03 08:46:44 +00002546 if( pPager->journalOpen ){
danielk1977d300b8a2008-08-01 10:50:23 +00002547 isErrorReset = 1;
danielk1977ae72d982007-10-03 08:46:44 +00002548 }
danielk1977ae72d982007-10-03 08:46:44 +00002549 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00002550 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00002551 }
2552
2553 /* If the pager is still in an error state, do not proceed. The error
2554 ** state will be cleared at some point in the future when all page
2555 ** references are dropped and the cache can be discarded.
2556 */
2557 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
2558 return pPager->errCode;
2559 }
2560
danielk1977d300b8a2008-08-01 10:50:23 +00002561 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
danielk1977b4b47412007-08-17 15:53:36 +00002562 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00002563 if( !MEMDB ){
danielk1977d300b8a2008-08-01 10:50:23 +00002564 int isHotJournal;
danielk19778c0a7912008-08-20 14:49:23 +00002565 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
danielk1977e277be02007-03-23 18:12:06 +00002566 if( !pPager->noReadlock ){
2567 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2568 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002569 assert( pPager->state==PAGER_UNLOCK );
danielk1977e277be02007-03-23 18:12:06 +00002570 return pager_error(pPager, rc);
2571 }
2572 assert( pPager->state>=SHARED_LOCK );
2573 }
2574
2575 /* If a journal file exists, and there is no RESERVED lock on the
2576 ** database file, then it either needs to be played back or deleted.
2577 */
danielk1977d300b8a2008-08-01 10:50:23 +00002578 if( !isErrorReset ){
2579 rc = hasHotJournal(pPager, &isHotJournal);
2580 if( rc!=SQLITE_OK ){
2581 goto failed;
2582 }
drh19db9352008-03-27 22:42:51 +00002583 }
danielk1977d300b8a2008-08-01 10:50:23 +00002584 if( isErrorReset || isHotJournal ){
danielk1977e277be02007-03-23 18:12:06 +00002585 /* Get an EXCLUSIVE lock on the database file. At this point it is
2586 ** important that a RESERVED lock is not obtained on the way to the
2587 ** EXCLUSIVE lock. If it were, another process might open the
2588 ** database file, detect the RESERVED lock, and conclude that the
2589 ** database is safe to read while this process is still rolling it
2590 ** back.
2591 **
2592 ** Because the intermediate RESERVED lock is not requested, the
2593 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00002594 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00002595 */
danielk1977ae72d982007-10-03 08:46:44 +00002596 if( pPager->state<EXCLUSIVE_LOCK ){
2597 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
2598 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002599 rc = pager_error(pPager, rc);
2600 goto failed;
danielk1977ae72d982007-10-03 08:46:44 +00002601 }
2602 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00002603 }
danielk1977e277be02007-03-23 18:12:06 +00002604
drh16e45a42008-04-19 20:34:18 +00002605 /* Open the journal for read/write access. This is because in
drhfd131da2007-08-07 17:13:03 +00002606 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00002607 ** possibly used for a transaction later on. On some systems, the
2608 ** OsTruncate() call used in exclusive-access mode also requires
2609 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00002610 */
danielk1977d300b8a2008-08-01 10:50:23 +00002611 if( !isErrorReset && pPager->journalOpen==0 ){
danielk1977861f7452008-06-05 11:39:11 +00002612 int res;
2613 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
2614 if( rc==SQLITE_OK ){
2615 if( res ){
2616 int fout = 0;
2617 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
2618 assert( !pPager->tempFile );
2619 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
2620 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
2621 if( fout&SQLITE_OPEN_READONLY ){
2622 rc = SQLITE_BUSY;
2623 sqlite3OsClose(pPager->jfd);
2624 }
2625 }else{
2626 /* If the journal does not exist, that means some other process
2627 ** has already rolled it back */
danielk1977ae72d982007-10-03 08:46:44 +00002628 rc = SQLITE_BUSY;
danielk1977ae72d982007-10-03 08:46:44 +00002629 }
danielk1977979f38e2007-03-27 16:19:51 +00002630 }
2631 }
danielk1977e277be02007-03-23 18:12:06 +00002632 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002633 if( rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_UNLOCK
2634 && rc!=SQLITE_IOERR_NOMEM
2635 ){
2636 rc = SQLITE_BUSY;
drh1aa5af12008-03-07 19:51:14 +00002637 }
danielk197752b472a2008-05-05 16:23:55 +00002638 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002639 }
2640 pPager->journalOpen = 1;
2641 pPager->journalStarted = 0;
2642 pPager->journalOff = 0;
2643 pPager->setMaster = 0;
2644 pPager->journalHdr = 0;
2645
2646 /* Playback and delete the journal. Drop the database write
2647 ** lock and reacquire the read lock.
2648 */
2649 rc = pager_playback(pPager, 1);
2650 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002651 rc = pager_error(pPager, rc);
2652 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002653 }
danielk1977c5859712007-03-26 12:26:27 +00002654 assert(pPager->state==PAGER_SHARED ||
2655 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
2656 );
danielk1977e277be02007-03-23 18:12:06 +00002657 }
2658
danielk19778c0a7912008-08-20 14:49:23 +00002659 if( sqlite3PcachePagecount(pPager->pPCache)>0 ){
danielk197724168722007-04-02 05:07:47 +00002660 /* The shared-lock has just been acquired on the database file
2661 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00002662 ** read or write transaction). Check to see if the database
2663 ** has been modified. If the database has changed, flush the
2664 ** cache.
2665 **
2666 ** Database changes is detected by looking at 15 bytes beginning
2667 ** at offset 24 into the file. The first 4 of these 16 bytes are
2668 ** a 32-bit counter that is incremented with each change. The
2669 ** other bytes change randomly with each file change when
2670 ** a codec is in use.
2671 **
2672 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00002673 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00002674 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00002675 */
drh86a88112007-04-16 15:02:19 +00002676 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977ad0132d2008-06-07 08:58:22 +00002677 sqlite3PagerPagecount(pPager, 0);
danielk197724168722007-04-02 05:07:47 +00002678
danielk1977e180dd92007-04-05 17:15:52 +00002679 if( pPager->errCode ){
danielk197752b472a2008-05-05 16:23:55 +00002680 rc = pPager->errCode;
2681 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002682 }
2683
danielk1977e180dd92007-04-05 17:15:52 +00002684 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00002685 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00002686 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00002687 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002688 goto failed;
danielk1977e180dd92007-04-05 17:15:52 +00002689 }
drh86a88112007-04-16 15:02:19 +00002690 }else{
2691 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00002692 }
2693
drh86a88112007-04-16 15:02:19 +00002694 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00002695 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002696 }
2697 }
2698 }
danielk1977c5859712007-03-26 12:26:27 +00002699 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
2700 if( pPager->state==PAGER_UNLOCK ){
2701 pPager->state = PAGER_SHARED;
2702 }
danielk1977e277be02007-03-23 18:12:06 +00002703 }
2704
danielk197752b472a2008-05-05 16:23:55 +00002705 failed:
2706 if( rc!=SQLITE_OK ){
2707 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
2708 pager_unlock(pPager);
2709 }
danielk1977e277be02007-03-23 18:12:06 +00002710 return rc;
2711}
2712
2713/*
drhd33d5a82007-04-26 12:11:28 +00002714** Make sure we have the content for a page. If the page was
2715** previously acquired with noContent==1, then the content was
2716** just initialized to zeros instead of being read from disk.
2717** But now we need the real data off of disk. So make sure we
2718** have it. Read it in if we do not have it already.
2719*/
2720static int pager_get_content(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002721 if( pPg->flags&PGHDR_NEED_READ ){
drhd33d5a82007-04-26 12:11:28 +00002722 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
2723 if( rc==SQLITE_OK ){
danielk19778c0a7912008-08-20 14:49:23 +00002724 pPg->flags &= ~PGHDR_NEED_READ;
drhd33d5a82007-04-26 12:11:28 +00002725 }else{
2726 return rc;
2727 }
2728 }
2729 return SQLITE_OK;
2730}
2731
2732/*
danielk19778c0a7912008-08-20 14:49:23 +00002733** If the reference count has reached zero, and the pager is not in the
2734** middle of a write transaction or opened in exclusive mode, unlock it.
2735*/
2736static void pagerUnlockIfUnused(Pager *pPager){
2737 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
2738 && (!pPager->exclusiveMode || pPager->journalOff>0)
2739 ){
2740 pagerUnlockAndRollback(pPager);
2741 }
2742}
2743
2744/*
2745** Drop a page from the cache using sqlite3PcacheDrop().
2746**
2747** If this means there are now no pages with references to them, a rollback
2748** occurs and the lock on the database is removed.
2749*/
2750static void pagerDropPage(DbPage *pPg){
2751 Pager *pPager = pPg->pPager;
2752 sqlite3PcacheDrop(pPg);
2753 pagerUnlockIfUnused(pPager);
2754}
2755
2756/*
drhd9b02572001-04-15 00:37:09 +00002757** Acquire a page.
2758**
drh58a11682001-11-10 13:51:08 +00002759** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00002760** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00002761**
drhd33d5a82007-04-26 12:11:28 +00002762** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00002763** file is smaller than the requested page, then no actual disk
2764** read occurs and the memory image of the page is initialized to
2765** all zeros. The extra data appended to a page is always initialized
2766** to zeros the first time a page is loaded into memory.
2767**
drhd9b02572001-04-15 00:37:09 +00002768** The acquisition might fail for several reasons. In all cases,
2769** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00002770**
drhd33d5a82007-04-26 12:11:28 +00002771** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00002772** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00002773** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00002774** just returns 0. This routine acquires a read-lock the first time it
2775** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00002776** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00002777** or journal files.
drh0787db62007-03-04 13:15:27 +00002778**
drh538f5702007-04-13 02:14:30 +00002779** If noContent is false, the page contents are actually read from disk.
2780** If noContent is true, it means that we do not care about the contents
2781** of the page at this time, so do not do a disk read. Just fill in the
2782** page content with zeros. But mark the fact that we have not read the
2783** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00002784** sqlite3PagerWrite() is called on this page or if this routine is
2785** called again with noContent==0, that means that the content is needed
2786** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00002787*/
drh86f8c192007-08-22 00:39:19 +00002788static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00002789 Pager *pPager, /* The pager open on the database file */
2790 Pgno pgno, /* Page number to fetch */
2791 DbPage **ppPage, /* Write a pointer to the page here */
2792 int noContent /* Do not bother reading content from disk if true */
2793){
danielk19778c0a7912008-08-20 14:49:23 +00002794 PgHdr *pPg = 0;
drh165ffe92005-03-15 17:09:30 +00002795 int rc;
drhed7c8552001-04-11 14:29:21 +00002796
danielk19778c0a7912008-08-20 14:49:23 +00002797 assert( pPager->state==PAGER_UNLOCK
2798 || sqlite3PcacheRefCount(pPager->pPCache)>0
2799 || pgno==1
2800 );
danielk1977e277be02007-03-23 18:12:06 +00002801
danielk197726836652005-01-17 01:33:13 +00002802 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
2803 ** number greater than this, or zero, is requested.
2804 */
drh267cb322005-09-16 17:16:52 +00002805 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00002806 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00002807 }
2808
drhd9b02572001-04-15 00:37:09 +00002809 /* Make sure we have not hit any critical errors.
2810 */
drh836faa42003-01-11 13:30:57 +00002811 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00002812 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00002813
danielk197713adf8a2004-06-03 16:08:41 +00002814 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00002815 ** on the database file. pagerSharedLock() is a no-op if
2816 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00002817 */
danielk1977e277be02007-03-23 18:12:06 +00002818 rc = pagerSharedLock(pPager);
2819 if( rc!=SQLITE_OK ){
2820 return rc;
drhed7c8552001-04-11 14:29:21 +00002821 }
danielk1977e277be02007-03-23 18:12:06 +00002822 assert( pPager->state!=PAGER_UNLOCK );
2823
danielk19778c0a7912008-08-20 14:49:23 +00002824 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
2825 if( rc!=SQLITE_OK ){
2826 return rc;
2827 }
danielk19778c0a7912008-08-20 14:49:23 +00002828 if( pPg->pPager==0 ){
2829 /* The pager cache has created a new page. Its content needs to
2830 ** be initialized.
2831 */
danielk1977979f38e2007-03-27 16:19:51 +00002832 int nMax;
drh538f5702007-04-13 02:14:30 +00002833 PAGER_INCR(pPager->nMiss);
danielk19778c0a7912008-08-20 14:49:23 +00002834 pPg->pPager = pPager;
2835 if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){
2836 pPg->flags |= PGHDR_IN_JOURNAL;
drhed7c8552001-04-11 14:29:21 +00002837 }
danielk19778c0a7912008-08-20 14:49:23 +00002838 memset(pPg->pExtra, 0, pPager->nExtra);
danielk197724168722007-04-02 05:07:47 +00002839
danielk1977ad0132d2008-06-07 08:58:22 +00002840 rc = sqlite3PagerPagecount(pPager, &nMax);
2841 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002842 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00002843 return rc;
2844 }
danielk197775bab7d2006-01-23 13:09:45 +00002845
drhd215acf2007-04-13 04:01:58 +00002846 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00002847 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00002848 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00002849 return SQLITE_FULL;
2850 }
danielk19778c0a7912008-08-20 14:49:23 +00002851 memset(pPg->pData, 0, pPager->pageSize);
2852 if( noContent && !pPager->alwaysRollback ){
2853 pPg->flags |= PGHDR_NEED_READ;
2854 }
drh538f5702007-04-13 02:14:30 +00002855 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00002856 }else{
danielk1977e180dd92007-04-05 17:15:52 +00002857 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00002858 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
danielk19778c0a7912008-08-20 14:49:23 +00002859 /* sqlite3PagerUnref(pPg); */
2860 pagerDropPage(pPg);
drh551b7732006-11-06 21:20:25 +00002861 return rc;
drh81a20f22001-10-12 17:30:04 +00002862 }
drh306dc212001-05-21 13:45:10 +00002863 }
danielk19773c407372005-02-15 02:54:14 +00002864#ifdef SQLITE_CHECK_PAGES
2865 pPg->pageHash = pager_pagehash(pPg);
2866#endif
drhed7c8552001-04-11 14:29:21 +00002867 }else{
drhd9b02572001-04-15 00:37:09 +00002868 /* The requested page is in the page cache. */
danielk19778c0a7912008-08-20 14:49:23 +00002869 assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00002870 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00002871 if( !noContent ){
2872 rc = pager_get_content(pPg);
2873 if( rc ){
danielk19778c0a7912008-08-20 14:49:23 +00002874 sqlite3PagerUnref(pPg);
drhd33d5a82007-04-26 12:11:28 +00002875 return rc;
2876 }
2877 }
drhed7c8552001-04-11 14:29:21 +00002878 }
danielk19778c0a7912008-08-20 14:49:23 +00002879
danielk19773b8a05f2007-03-19 17:44:26 +00002880 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00002881 return SQLITE_OK;
2882}
danielk19778c0a7912008-08-20 14:49:23 +00002883
drh86f8c192007-08-22 00:39:19 +00002884int sqlite3PagerAcquire(
2885 Pager *pPager, /* The pager open on the database file */
2886 Pgno pgno, /* Page number to fetch */
2887 DbPage **ppPage, /* Write a pointer to the page here */
2888 int noContent /* Do not bother reading content from disk if true */
2889){
2890 int rc;
drh86f8c192007-08-22 00:39:19 +00002891 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
drh86f8c192007-08-22 00:39:19 +00002892 return rc;
2893}
2894
drhed7c8552001-04-11 14:29:21 +00002895
2896/*
drh7e3b0a02001-04-28 16:52:40 +00002897** Acquire a page if it is already in the in-memory cache. Do
2898** not read the page from disk. Return a pointer to the page,
2899** or 0 if the page is not in cache.
2900**
danielk19773b8a05f2007-03-19 17:44:26 +00002901** See also sqlite3PagerGet(). The difference between this routine
2902** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00002903** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00002904** returns NULL if the page is not in cache or if a disk I/O error
2905** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00002906*/
danielk19773b8a05f2007-03-19 17:44:26 +00002907DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00002908 PgHdr *pPg = 0;
drh836faa42003-01-11 13:30:57 +00002909 assert( pPager!=0 );
2910 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00002911
danielk19778c0a7912008-08-20 14:49:23 +00002912 if( (pPager->state!=PAGER_UNLOCK)
2913 && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL)
2914 ){
2915 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
danielk1977e277be02007-03-23 18:12:06 +00002916 }
danielk19778c0a7912008-08-20 14:49:23 +00002917
danielk19773b8a05f2007-03-19 17:44:26 +00002918 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00002919}
2920
2921/*
drhed7c8552001-04-11 14:29:21 +00002922** Release a page.
2923**
2924** If the number of references to the page drop to zero, then the
2925** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00002926** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00002927** removed.
2928*/
danielk19773b8a05f2007-03-19 17:44:26 +00002929int sqlite3PagerUnref(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002930 if( pPg ){
2931 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00002932 sqlite3PcacheRelease(pPg);
2933 pagerUnlockIfUnused(pPager);
drhed7c8552001-04-11 14:29:21 +00002934 }
drhd9b02572001-04-15 00:37:09 +00002935 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002936}
2937
2938/*
drha6abd042004-06-09 17:37:22 +00002939** Create a journal file for pPager. There should already be a RESERVED
2940** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00002941**
2942** Return SQLITE_OK if everything. Return an error code and release the
2943** write lock if anything goes wrong.
2944*/
2945static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00002946 sqlite3_vfs *pVfs = pPager->pVfs;
2947 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
2948
drhda47d772002-12-02 04:25:19 +00002949 int rc;
drhb7f91642004-10-31 02:22:47 +00002950 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00002951 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00002952 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00002953 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00002954 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00002955 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00002956 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00002957 rc = SQLITE_NOMEM;
2958 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00002959 }
danielk1977b4b47412007-08-17 15:53:36 +00002960
drhfdc40e92008-04-17 20:59:37 +00002961 if( pPager->journalOpen==0 ){
2962 if( pPager->tempFile ){
2963 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
2964 }else{
2965 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00002966 }
drhfdc40e92008-04-17 20:59:37 +00002967#ifdef SQLITE_ENABLE_ATOMIC_WRITE
2968 rc = sqlite3JournalOpen(
2969 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
2970 );
2971#else
2972 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
2973#endif
2974 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
2975 pPager->journalOff = 0;
2976 pPager->setMaster = 0;
2977 pPager->journalHdr = 0;
2978 if( rc!=SQLITE_OK ){
2979 if( rc==SQLITE_NOMEM ){
2980 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2981 }
2982 goto failed_to_open_journal;
2983 }
drhda47d772002-12-02 04:25:19 +00002984 }
2985 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00002986 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00002987 pPager->needSync = 0;
2988 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00002989 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00002990 if( pPager->errCode ){
2991 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00002992 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00002993 }
drhda47d772002-12-02 04:25:19 +00002994 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00002995
danielk197776572402004-06-25 02:38:54 +00002996 rc = writeJournalHdr(pPager);
2997
drhac69b052004-05-12 13:30:07 +00002998 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00002999 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003000 }
danielk1977ae72d982007-10-03 08:46:44 +00003001 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003002 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003003 if( rc==SQLITE_OK ){
3004 rc = SQLITE_FULL;
3005 }
3006 }
drh9c105bb2004-10-02 20:38:28 +00003007 return rc;
3008
3009failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003010 sqlite3BitvecDestroy(pPager->pInJournal);
3011 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003012 return rc;
drhda47d772002-12-02 04:25:19 +00003013}
3014
3015/*
drh4b845d72002-03-05 12:41:19 +00003016** Acquire a write-lock on the database. The lock is removed when
3017** the any of the following happen:
3018**
drh80e35f42007-03-30 14:06:34 +00003019** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003020** * sqlite3PagerRollback() is called.
3021** * sqlite3PagerClose() is called.
3022** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003023**
danielk197713adf8a2004-06-03 16:08:41 +00003024** The first parameter to this routine is a pointer to any open page of the
3025** database file. Nothing changes about the page - it is used merely to
3026** acquire a pointer to the Pager structure and as proof that there is
3027** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003028**
danielk197713adf8a2004-06-03 16:08:41 +00003029** The second parameter indicates how much space in bytes to reserve for a
3030** master journal file-name at the start of the journal when it is created.
3031**
3032** A journal file is opened if this is not a temporary file. For temporary
3033** files, the opening of the journal file is deferred until there is an
3034** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003035**
drha6abd042004-06-09 17:37:22 +00003036** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003037**
3038** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3039** immediately instead of waiting until we try to flush the cache. The
3040** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003041*/
danielk19773b8a05f2007-03-19 17:44:26 +00003042int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003043 Pager *pPager = pPg->pPager;
3044 int rc = SQLITE_OK;
3045 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003046 assert( pPager->state!=PAGER_UNLOCK );
3047 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003048 assert( pPager->pInJournal==0 );
danielk19778c0a7912008-08-20 14:49:23 +00003049 sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_IN_JOURNAL);
drhb7f91642004-10-31 02:22:47 +00003050 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003051 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003052 pPager->origDbSize = pPager->dbSize;
3053 }else{
drh054889e2005-11-30 03:20:31 +00003054 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003055 if( rc==SQLITE_OK ){
3056 pPager->state = PAGER_RESERVED;
3057 if( exFlag ){
3058 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3059 }
3060 }
danielk19779eed5052004-06-04 10:38:30 +00003061 if( rc!=SQLITE_OK ){
3062 return rc;
drhac69b052004-05-12 13:30:07 +00003063 }
drha6abd042004-06-09 17:37:22 +00003064 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003065 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhfdc40e92008-04-17 20:59:37 +00003066 if( pPager->useJournal && !pPager->tempFile
3067 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drhac69b052004-05-12 13:30:07 +00003068 rc = pager_open_journal(pPager);
3069 }
drh4b845d72002-03-05 12:41:19 +00003070 }
danielk1977334cdb62007-03-26 08:05:12 +00003071 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00003072 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00003073 ** time a (read or write) transaction was successfully concluded
3074 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00003075 ** kept open and either was truncated to 0 bytes or its header was
3076 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00003077 */
3078 assert( pPager->nRec==0 );
3079 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003080 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003081 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003082 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhf5e7bb52008-02-18 14:47:33 +00003083 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003084 rc = SQLITE_NOMEM;
3085 }else{
drh369339d2007-03-30 16:01:55 +00003086 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003087 rc = writeJournalHdr(pPager);
3088 }
drh4b845d72002-03-05 12:41:19 +00003089 }
danielk1977334cdb62007-03-26 08:05:12 +00003090 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003091 return rc;
3092}
3093
3094/*
drh605ad8f2006-05-03 23:34:05 +00003095** Make a page dirty. Set its dirty flag and add it to the dirty
3096** page list.
3097*/
3098static void makeDirty(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003099 sqlite3PcacheMakeDirty(pPg);
drh605ad8f2006-05-03 23:34:05 +00003100}
3101
3102/*
3103** Make a page clean. Clear its dirty bit and remove it from the
3104** dirty page list.
3105*/
3106static void makeClean(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003107 sqlite3PcacheMakeClean(pPg);
drh605ad8f2006-05-03 23:34:05 +00003108}
3109
3110
3111/*
drhed7c8552001-04-11 14:29:21 +00003112** Mark a data page as writeable. The page is written into the journal
3113** if it is not there already. This routine must be called before making
3114** changes to a page.
3115**
3116** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003117** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003118** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003119** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003120** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003121**
3122** If the journal file could not be written because the disk is full,
3123** then this routine returns SQLITE_FULL and does an immediate rollback.
3124** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003125** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003126** reset.
drhed7c8552001-04-11 14:29:21 +00003127*/
danielk19773b8a05f2007-03-19 17:44:26 +00003128static int pager_write(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003129 void *pData = pPg->pData;
drh69688d52001-04-14 16:38:23 +00003130 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003131 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003132
drh6446c4d2001-12-15 14:22:18 +00003133 /* Check for errors
3134 */
danielk1977efaaf572006-01-16 11:29:19 +00003135 if( pPager->errCode ){
3136 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003137 }
drh5e00f6c2001-09-13 13:46:56 +00003138 if( pPager->readOnly ){
3139 return SQLITE_PERM;
3140 }
drh6446c4d2001-12-15 14:22:18 +00003141
danielk197776572402004-06-25 02:38:54 +00003142 assert( !pPager->setMaster );
3143
danielk19773c407372005-02-15 02:54:14 +00003144 CHECK_PAGE(pPg);
3145
drh538f5702007-04-13 02:14:30 +00003146 /* If this page was previously acquired with noContent==1, that means
3147 ** we didn't really read in the content of the page. This can happen
3148 ** (for example) when the page is being moved to the freelist. But
3149 ** now we are (perhaps) moving the page off of the freelist for
3150 ** reuse and we need to know its original content so that content
3151 ** can be stored in the rollback journal. So do the read at this
3152 ** time.
3153 */
drhd33d5a82007-04-26 12:11:28 +00003154 rc = pager_get_content(pPg);
3155 if( rc ){
3156 return rc;
drh538f5702007-04-13 02:14:30 +00003157 }
3158
drh6446c4d2001-12-15 14:22:18 +00003159 /* Mark the page as dirty. If the page has already been written
3160 ** to the journal then we can return right away.
3161 */
drh605ad8f2006-05-03 23:34:05 +00003162 makeDirty(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00003163 if( (pPg->flags&PGHDR_IN_JOURNAL)
3164 && (pageInStatement(pPg) || pPager->stmtInUse==0)
3165 ){
drha6abd042004-06-09 17:37:22 +00003166 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003167 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003168 }else{
drh6446c4d2001-12-15 14:22:18 +00003169
danielk1977a0bf2652004-11-04 14:30:04 +00003170 /* If we get this far, it means that the page needs to be
3171 ** written to the transaction journal or the ckeckpoint journal
3172 ** or both.
3173 **
3174 ** First check to see that the transaction journal exists and
3175 ** create it if it does not.
3176 */
3177 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003178 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003179 if( rc!=SQLITE_OK ){
3180 return rc;
3181 }
3182 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00003183 if( !pPager->journalOpen && pPager->useJournal
3184 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00003185 rc = pager_open_journal(pPager);
3186 if( rc!=SQLITE_OK ) return rc;
3187 }
danielk1977a0bf2652004-11-04 14:30:04 +00003188 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003189 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003190
3191 /* The transaction journal now exists and we have a RESERVED or an
3192 ** EXCLUSIVE lock on the main database file. Write the current page to
3193 ** the transaction journal if it is not there already.
3194 */
danielk19778c0a7912008-08-20 14:49:23 +00003195 if( !(pPg->flags&PGHDR_IN_JOURNAL) && (pPager->journalOpen || MEMDB) ){
danielk1977a0bf2652004-11-04 14:30:04 +00003196 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00003197 if( MEMDB ){
drh4f0c5872007-03-26 22:05:01 +00003198 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003199 rc = sqlite3PcachePreserve(pPg, 0);
3200 if( rc!=SQLITE_OK ){
3201 return rc;
danielk1977a0bf2652004-11-04 14:30:04 +00003202 }
3203 }else{
drhbf4bca52007-09-06 22:19:14 +00003204 u32 cksum;
3205 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00003206
drh267cb322005-09-16 17:16: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) );
drhc001c582006-03-06 18:23:16 +00003211 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00003212 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00003213 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,
danielk1977667a6c92007-09-10 06:12:04 +00003224 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00003225 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00003226 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
danielk19778c0a7912008-08-20 14:49:23 +00003227 PAGERID(pPager), pPg->pgno,
3228 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00003229
drhfd131da2007-08-07 17:13:03 +00003230 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00003231 ** transaction will be rolled back by the layer above.
3232 */
danielk1977a0bf2652004-11-04 14:30:04 +00003233 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00003234 return rc;
3235 }
danielk197707cb5602006-01-20 10:55:05 +00003236
danielk1977a0bf2652004-11-04 14:30:04 +00003237 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00003238 assert( pPager->pInJournal!=0 );
3239 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003240 if( !pPager->noSync ){
3241 pPg->flags |= PGHDR_NEED_SYNC;
3242 }
danielk1977a0bf2652004-11-04 14:30:04 +00003243 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00003244 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00003245 }
drhac69b052004-05-12 13:30:07 +00003246 }
danielk197713adf8a2004-06-03 16:08:41 +00003247 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003248 if( !pPager->journalStarted && !pPager->noSync ){
3249 pPg->flags |= PGHDR_NEED_SYNC;
3250 }
drh4f0c5872007-03-26 22:05:01 +00003251 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk19778c0a7912008-08-20 14:49:23 +00003252 PAGERID(pPager), pPg->pgno,
3253 ((pPg->flags&PGHDR_NEED_SYNC)?1:0));
danielk1977a0bf2652004-11-04 14:30:04 +00003254 }
danielk19778c0a7912008-08-20 14:49:23 +00003255 if( pPg->flags&PGHDR_NEED_SYNC ){
danielk1977a0bf2652004-11-04 14:30:04 +00003256 pPager->needSync = 1;
3257 }
danielk19778c0a7912008-08-20 14:49:23 +00003258 pPg->flags |= PGHDR_IN_JOURNAL;
danielk1977a0bf2652004-11-04 14:30:04 +00003259 }
3260
3261 /* If the statement journal is open and the page is not in it,
3262 ** then write the current page to the statement journal. Note that
3263 ** the statement journal format differs from the standard journal format
3264 ** in that it omits the checksums and the header.
3265 */
danielk1977f35843b2007-04-07 15:03:17 +00003266 if( pPager->stmtInUse
3267 && !pageInStatement(pPg)
3268 && (int)pPg->pgno<=pPager->stmtSize
3269 ){
danielk19778c0a7912008-08-20 14:49:23 +00003270 assert(
3271 (pPg->flags&PGHDR_IN_JOURNAL) || (int)pPg->pgno>pPager->origDbSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003272 if( MEMDB ){
danielk19778c0a7912008-08-20 14:49:23 +00003273 rc = sqlite3PcachePreserve(pPg, 1);
3274 if( rc!=SQLITE_OK ){
3275 return rc;
danielk1977a0bf2652004-11-04 14:30:04 +00003276 }
drh4f0c5872007-03-26 22:05:01 +00003277 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003278 /* page_add_to_stmt_list(pPg); */
danielk1977a0bf2652004-11-04 14:30:04 +00003279 }else{
danielk197762079062007-08-15 17:08:46 +00003280 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00003281 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
3282 rc = write32bits(pPager->stfd, offset, pPg->pgno);
3283 if( rc==SQLITE_OK ){
3284 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
3285 }
drh4f0c5872007-03-26 22:05:01 +00003286 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00003287 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00003288 return rc;
3289 }
danielk1977a0bf2652004-11-04 14:30:04 +00003290 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00003291 assert( pPager->pInStmt!=0 );
3292 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00003293 }
drhd9b02572001-04-15 00:37:09 +00003294 }
drhfa86c412002-02-02 15:01:15 +00003295 }
3296
3297 /* Update the database size and return.
3298 */
drh1aa2d8b2007-01-03 15:34:29 +00003299 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00003300 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003301 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00003302 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00003303 pPager->dbSize++;
3304 }
drh306dc212001-05-21 13:45:10 +00003305 }
drh69688d52001-04-14 16:38:23 +00003306 return rc;
drhed7c8552001-04-11 14:29:21 +00003307}
3308
3309/*
danielk19774099f6e2007-03-19 11:25:20 +00003310** This function is used to mark a data-page as writable. It uses
3311** pager_write() to open a journal file (if it is not already open)
3312** and write the page *pData to the journal.
3313**
3314** The difference between this function and pager_write() is that this
3315** function also deals with the special case where 2 or more pages
3316** fit on a single disk sector. In this case all co-resident pages
3317** must have been written to the journal file before returning.
3318*/
danielk19773b8a05f2007-03-19 17:44:26 +00003319int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003320 int rc = SQLITE_OK;
3321
danielk19773b8a05f2007-03-19 17:44:26 +00003322 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003323 Pager *pPager = pPg->pPager;
3324 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3325
3326 if( !MEMDB && nPagePerSector>1 ){
3327 Pgno nPageCount; /* Total number of pages in database file */
3328 Pgno pg1; /* First page of the sector pPg is located on. */
3329 int nPage; /* Number of pages starting at pg1 to journal */
3330 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003331 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003332
3333 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3334 ** header to be written between the pages journaled by this function.
3335 */
3336 assert( pPager->doNotSync==0 );
3337 pPager->doNotSync = 1;
3338
3339 /* This trick assumes that both the page-size and sector-size are
3340 ** an integer power of 2. It sets variable pg1 to the identifier
3341 ** of the first page of the sector pPg is located on.
3342 */
3343 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3344
danielk1977ad0132d2008-06-07 08:58:22 +00003345 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00003346 if( pPg->pgno>nPageCount ){
3347 nPage = (pPg->pgno - pg1)+1;
3348 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3349 nPage = nPageCount+1-pg1;
3350 }else{
3351 nPage = nPagePerSector;
3352 }
3353 assert(nPage>0);
3354 assert(pg1<=pPg->pgno);
3355 assert((pg1+nPage)>pPg->pgno);
3356
3357 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3358 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003359 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00003360 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00003361 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003362 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003363 if( rc==SQLITE_OK ){
3364 rc = pager_write(pPage);
danielk19778c0a7912008-08-20 14:49:23 +00003365 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003366 needSync = 1;
3367 }
danielk19773b8a05f2007-03-19 17:44:26 +00003368 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003369 }
3370 }
drhc81945e2008-03-10 14:12:53 +00003371 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk19778c0a7912008-08-20 14:49:23 +00003372 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003373 needSync = 1;
3374 }
danielk19778c0a7912008-08-20 14:49:23 +00003375 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003376 }
3377 }
3378
danielk1977dd97a492007-08-22 18:54:32 +00003379 /* If the PgHdr.needSync flag is set for any of the nPage pages
3380 ** starting at pg1, then it needs to be set for all of them. Because
3381 ** writing to any of these nPage pages may damage the others, the
3382 ** journal file must contain sync()ed copies of all of them
3383 ** before any of them can be written out to the database file.
3384 */
3385 if( needSync ){
3386 for(ii=0; ii<nPage && needSync; ii++){
3387 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
danielk19778c0a7912008-08-20 14:49:23 +00003388 if( pPage ) pPage->flags |= PGHDR_NEED_SYNC;
3389 sqlite3PagerUnref(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00003390 }
3391 assert(pPager->needSync);
3392 }
3393
danielk19774099f6e2007-03-19 11:25:20 +00003394 assert( pPager->doNotSync==1 );
3395 pPager->doNotSync = 0;
3396 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003397 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003398 }
3399 return rc;
3400}
3401
3402/*
drhaacc5432002-01-06 17:07:40 +00003403** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003404** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003405** to change the content of the page.
3406*/
danielk19777d3a6662006-01-23 16:21:05 +00003407#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003408int sqlite3PagerIswriteable(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003409 return pPg->flags&PGHDR_DIRTY;
drh6019e162001-07-02 17:51:45 +00003410}
danielk19777d3a6662006-01-23 16:21:05 +00003411#endif
drh6019e162001-07-02 17:51:45 +00003412
drh001bbcb2003-03-19 03:14:00 +00003413/*
drh30e58752002-03-02 20:41:57 +00003414** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003415** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00003416** that page might be marked as dirty.
3417**
3418** The overlying software layer calls this routine when all of the data
3419** on the given page is unused. The pager marks the page as clean so
3420** that it does not get written to disk.
3421**
3422** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003423** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003424** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003425**
3426** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003427** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003428** will thereafter be ignored. This is necessary to avoid a problem
3429** where a page with data is added to the freelist during one part of
3430** a transaction then removed from the freelist during a later part
3431** of the same transaction and reused for some other purpose. When it
3432** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003433** the sqlite3PagerDontRollback() routine is called. But because the
3434** page contains critical data, we still need to be sure it gets
3435** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003436*/
drh538f5702007-04-13 02:14:30 +00003437void sqlite3PagerDontWrite(DbPage *pDbPage){
3438 PgHdr *pPg = pDbPage;
3439 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00003440
drhb7f91642004-10-31 02:22:47 +00003441 if( MEMDB ) return;
danielk19778c0a7912008-08-20 14:49:23 +00003442 pPg->flags |= PGHDR_ALWAYS_ROLLBACK;
3443 if( (pPg->flags&PGHDR_DIRTY) && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00003444 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00003445 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
3446 /* If this pages is the last page in the file and the file has grown
3447 ** during the current transaction, then do NOT mark the page as clean.
3448 ** When the database file grows, we must make sure that the last page
3449 ** gets written at least once so that the disk file will be the correct
3450 ** size. If you do not write this page and the size of the file
3451 ** on the disk ends up being too small, that can lead to database
3452 ** corruption during the next transaction.
3453 */
3454 }else{
drh538f5702007-04-13 02:14:30 +00003455 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
3456 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
danielk197733e32162008-08-23 18:53:08 +00003457 pPg->flags |= PGHDR_DONT_WRITE;
danielk19773c407372005-02-15 02:54:14 +00003458#ifdef SQLITE_CHECK_PAGES
3459 pPg->pageHash = pager_pagehash(pPg);
3460#endif
drh8124a302002-06-25 14:43:57 +00003461 }
drh30e58752002-03-02 20:41:57 +00003462 }
3463}
3464
3465/*
3466** A call to this routine tells the pager that if a rollback occurs,
3467** it is not necessary to restore the data on the given page. This
3468** means that the pager does not have to record the given page in the
3469** rollback journal.
drh538f5702007-04-13 02:14:30 +00003470**
3471** If we have not yet actually read the content of this page (if
3472** the PgHdr.needRead flag is set) then this routine acts as a promise
3473** that we will never need to read the page content in the future.
3474** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00003475*/
danielk19773b8a05f2007-03-19 17:44:26 +00003476void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003477 Pager *pPager = pPg->pPager;
3478
drhd3627af2006-12-18 18:34:51 +00003479 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00003480
3481 /* If the journal file is not open, or DontWrite() has been called on
3482 ** this page (DontWrite() sets the alwaysRollback flag), then this
3483 ** function is a no-op.
3484 */
danielk19778c0a7912008-08-20 14:49:23 +00003485 if( pPager->journalOpen==0 || (pPg->flags&PGHDR_ALWAYS_ROLLBACK)
3486 || pPager->alwaysRollback
3487 ){
danielk197787c29a92008-01-18 11:33:16 +00003488 return;
3489 }
danielk1977a55e9352008-01-21 13:04:34 +00003490 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
3491
drhc5d0bd92008-04-14 01:00:57 +00003492#ifdef SQLITE_SECURE_DELETE
3493 if( pPg->inJournal || (int)pPg->pgno > pPager->origDbSize ){
3494 return;
3495 }
3496#endif
3497
3498 /* If SECURE_DELETE is disabled, then there is no way that this
3499 ** routine can be called on a page for which sqlite3PagerDontWrite()
3500 ** has not been previously called during the same transaction.
3501 ** And if DontWrite() has previously been called, the following
3502 ** conditions must be met.
drh10131482008-07-11 03:34:09 +00003503 **
3504 ** (Later:) Not true. If the database is corrupted by having duplicate
3505 ** pages on the freelist (ex: corrupt9.test) then the following is not
3506 ** necessarily true:
danielk1977a55e9352008-01-21 13:04:34 +00003507 */
drh10131482008-07-11 03:34:09 +00003508 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); */
danielk1977a55e9352008-01-21 13:04:34 +00003509
drhf5e7bb52008-02-18 14:47:33 +00003510 assert( pPager->pInJournal!=0 );
3511 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003512 pPg->flags |= PGHDR_IN_JOURNAL;
3513 pPg->flags &= ~PGHDR_NEED_READ;
danielk1977a55e9352008-01-21 13:04:34 +00003514 if( pPager->stmtInUse ){
drhc5d0bd92008-04-14 01:00:57 +00003515 assert( pPager->stmtSize >= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00003516 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00003517 }
danielk1977a55e9352008-01-21 13:04:34 +00003518 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
3519 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00003520}
3521
drhac69b052004-05-12 13:30:07 +00003522
drh30e58752002-03-02 20:41:57 +00003523/*
drh80e35f42007-03-30 14:06:34 +00003524** This routine is called to increment the database file change-counter,
3525** stored at byte 24 of the pager file.
3526*/
danielk1977c7b60172007-08-22 11:22:03 +00003527static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00003528 PgHdr *pPgHdr;
3529 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00003530 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00003531
drh701bb3b2008-08-02 03:50:39 +00003532#ifndef SQLITE_ENABLE_ATOMIC_WRITE
3533 assert( isDirect==0 ); /* isDirect is only true for atomic writes */
3534#endif
drh80e35f42007-03-30 14:06:34 +00003535 if( !pPager->changeCountDone ){
3536 /* Open page 1 of the file for writing. */
3537 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3538 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00003539
3540 if( !isDirect ){
3541 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00003542 if( rc!=SQLITE_OK ){
3543 sqlite3PagerUnref(pPgHdr);
3544 return rc;
3545 }
danielk1977c7b60172007-08-22 11:22:03 +00003546 }
3547
drh80e35f42007-03-30 14:06:34 +00003548 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00003549 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00003550 change_counter++;
danielk19778c0a7912008-08-20 14:49:23 +00003551 put32bits(((char*)pPgHdr->pData)+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00003552
drh701bb3b2008-08-02 03:50:39 +00003553#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977c7b60172007-08-22 11:22:03 +00003554 if( isDirect && pPager->fd->pMethods ){
danielk19778c0a7912008-08-20 14:49:23 +00003555 const void *zBuf = pPgHdr->pData;
danielk1977c7b60172007-08-22 11:22:03 +00003556 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
3557 }
drh701bb3b2008-08-02 03:50:39 +00003558#endif
danielk1977c7b60172007-08-22 11:22:03 +00003559
drh80e35f42007-03-30 14:06:34 +00003560 /* Release the page reference. */
3561 sqlite3PagerUnref(pPgHdr);
3562 pPager->changeCountDone = 1;
3563 }
danielk1977c7b60172007-08-22 11:22:03 +00003564 return rc;
drh80e35f42007-03-30 14:06:34 +00003565}
3566
3567/*
danielk1977f653d782008-03-20 11:04:21 +00003568** Sync the pager file to disk.
3569*/
3570int sqlite3PagerSync(Pager *pPager){
3571 int rc;
drh7426f862008-08-26 21:07:26 +00003572 if( MEMDB ){
3573 rc = SQLITE_OK;
3574 }else{
3575 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3576 }
danielk1977f653d782008-03-20 11:04:21 +00003577 return rc;
3578}
3579
3580/*
drh80e35f42007-03-30 14:06:34 +00003581** Sync the database file for the pager pPager. zMaster points to the name
3582** of a master journal file that should be written into the individual
3583** journal file. zMaster may be NULL, which is interpreted as no master
3584** journal (a single database transaction).
3585**
3586** This routine ensures that the journal is synced, all dirty pages written
3587** to the database file and the database file synced. The only thing that
3588** remains to commit the transaction is to delete the journal file (or
3589** master journal file if specified).
3590**
3591** Note that if zMaster==NULL, this does not overwrite a previous value
3592** passed to an sqlite3PagerCommitPhaseOne() call.
3593**
3594** If parameter nTrunc is non-zero, then the pager file is truncated to
3595** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00003596**
3597** If the final parameter - noSync - is true, then the database file itself
3598** is not synced. The caller must call sqlite3PagerSync() directly to
3599** sync the database file before calling CommitPhaseTwo() to delete the
3600** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00003601*/
danielk1977f653d782008-03-20 11:04:21 +00003602int sqlite3PagerCommitPhaseOne(
3603 Pager *pPager,
3604 const char *zMaster,
3605 Pgno nTrunc,
3606 int noSync
3607){
drh80e35f42007-03-30 14:06:34 +00003608 int rc = SQLITE_OK;
3609
danielk1977dad31b52008-05-15 11:08:07 +00003610 if( pPager->errCode ){
3611 return pPager->errCode;
3612 }
3613
drhd138c012008-05-13 00:58:18 +00003614 /* If no changes have been made, we can leave the transaction early.
3615 */
3616 if( pPager->dbModified==0 &&
3617 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3618 pPager->exclusiveMode!=0) ){
3619 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3620 return SQLITE_OK;
3621 }
3622
drh80e35f42007-03-30 14:06:34 +00003623 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
3624 pPager->zFilename, zMaster, nTrunc);
3625
3626 /* If this is an in-memory db, or no pages have been written to, or this
3627 ** function has already been called, it is a no-op.
3628 */
3629 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
3630 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00003631
danielk1977c7b60172007-08-22 11:22:03 +00003632#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3633 /* The atomic-write optimization can be used if all of the
3634 ** following are true:
3635 **
3636 ** + The file-system supports the atomic-write property for
3637 ** blocks of size page-size, and
3638 ** + This commit is not part of a multi-file transaction, and
3639 ** + Exactly one page has been modified and store in the journal file.
3640 **
3641 ** If the optimization can be used, then the journal file will never
3642 ** be created for this transaction.
3643 */
danielk19778c0a7912008-08-20 14:49:23 +00003644 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
danielk1977f55b8992007-08-24 08:15:53 +00003645 int useAtomicWrite = (
3646 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00003647 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00003648 pPager->journalOff==jrnlBufferSize(pPager) &&
3649 nTrunc==0 &&
danielk19778c0a7912008-08-20 14:49:23 +00003650 (pPg==0 || pPg->pDirty==0)
danielk1977f55b8992007-08-24 08:15:53 +00003651 );
danielk1977700b9c52008-04-24 12:37:40 +00003652 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00003653 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00003654 /* Update the nRec field in the journal file. */
3655 int offset = pPager->journalHdr + sizeof(aJournalMagic);
3656 assert(pPager->nRec==1);
3657 rc = write32bits(pPager->jfd, offset, pPager->nRec);
3658
3659 /* Update the db file change counter. The following call will modify
3660 ** the in-memory representation of page 1 to include the updated
3661 ** change counter and then write page 1 directly to the database
3662 ** file. Because of the atomic-write property of the host file-system,
3663 ** this is safe.
3664 */
danielk1977ae72d982007-10-03 08:46:44 +00003665 if( rc==SQLITE_OK ){
3666 rc = pager_incr_changecounter(pPager, 1);
3667 }
danielk1977f55b8992007-08-24 08:15:53 +00003668 }else{
3669 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00003670 }
3671
danielk1977ae72d982007-10-03 08:46:44 +00003672 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00003673#endif
3674
drh80e35f42007-03-30 14:06:34 +00003675 /* If a master journal file name has already been written to the
3676 ** journal file, then no sync is required. This happens when it is
3677 ** written, then the process fails to upgrade from a RESERVED to an
3678 ** EXCLUSIVE lock. The next time the process tries to commit the
3679 ** transaction the m-j name will have already been written.
3680 */
3681 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00003682 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00003683 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00003684 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00003685#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197771aa7ff2008-05-20 07:05:09 +00003686 if( nTrunc!=0 ){
3687 /* If this transaction has made the database smaller, then all pages
3688 ** being discarded by the truncation must be written to the journal
3689 ** file.
3690 */
3691 Pgno i;
3692 int iSkip = PAGER_MJ_PGNO(pPager);
3693 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
3694 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
3695 rc = sqlite3PagerGet(pPager, i, &pPg);
3696 if( rc!=SQLITE_OK ) goto sync_exit;
3697 rc = sqlite3PagerWrite(pPg);
3698 sqlite3PagerUnref(pPg);
3699 if( rc!=SQLITE_OK ) goto sync_exit;
3700 }
3701 }
3702 }
drh80e35f42007-03-30 14:06:34 +00003703#endif
danielk197771aa7ff2008-05-20 07:05:09 +00003704 rc = writeMasterJournal(pPager, zMaster);
3705 if( rc!=SQLITE_OK ) goto sync_exit;
3706 rc = syncJournal(pPager);
3707 }
drh80e35f42007-03-30 14:06:34 +00003708 }
danielk1977c7b60172007-08-22 11:22:03 +00003709 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00003710
3711#ifndef SQLITE_OMIT_AUTOVACUUM
3712 if( nTrunc!=0 ){
3713 rc = sqlite3PagerTruncate(pPager, nTrunc);
3714 if( rc!=SQLITE_OK ) goto sync_exit;
3715 }
3716#endif
3717
3718 /* Write all dirty pages to the database file */
danielk19778c0a7912008-08-20 14:49:23 +00003719 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003720 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00003721 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00003722 assert( rc!=SQLITE_IOERR_BLOCKED );
3723 /* The error might have left the dirty list all fouled up here,
3724 ** but that does not matter because if the if the dirty list did
3725 ** get corrupted, then the transaction will roll back and
3726 ** discard the dirty list. There is an assert in
3727 ** pager_get_all_dirty_pages() that verifies that no attempt
3728 ** is made to use an invalid dirty list.
3729 */
drh153c62c2007-08-24 03:51:33 +00003730 goto sync_exit;
3731 }
danielk19778c0a7912008-08-20 14:49:23 +00003732 sqlite3PcacheCleanAll(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003733
3734 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00003735 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00003736 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00003737 }
3738 IOTRACE(("DBSYNC %p\n", pPager))
3739
3740 pPager->state = PAGER_SYNCED;
3741 }else if( MEMDB && nTrunc!=0 ){
3742 rc = sqlite3PagerTruncate(pPager, nTrunc);
3743 }
3744
3745sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00003746 if( rc==SQLITE_IOERR_BLOCKED ){
3747 /* pager_incr_changecounter() may attempt to obtain an exclusive
3748 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00003749 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00003750 * better to return SQLITE_BUSY.
3751 */
3752 rc = SQLITE_BUSY;
3753 }
drh80e35f42007-03-30 14:06:34 +00003754 return rc;
3755}
3756
3757
3758/*
drhed7c8552001-04-11 14:29:21 +00003759** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00003760**
3761** If the commit fails for any reason, a rollback attempt is made
3762** and an error code is returned. If the commit worked, SQLITE_OK
3763** is returned.
drhed7c8552001-04-11 14:29:21 +00003764*/
drh80e35f42007-03-30 14:06:34 +00003765int sqlite3PagerCommitPhaseTwo(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003766 int rc = SQLITE_OK;
drhd9b02572001-04-15 00:37:09 +00003767
danielk1977efaaf572006-01-16 11:29:19 +00003768 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00003769 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003770 }
drha6abd042004-06-09 17:37:22 +00003771 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00003772 return SQLITE_ERROR;
3773 }
drhd138c012008-05-13 00:58:18 +00003774 if( pPager->dbModified==0 &&
3775 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3776 pPager->exclusiveMode!=0) ){
3777 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3778 return SQLITE_OK;
3779 }
drh4f0c5872007-03-26 22:05:01 +00003780 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003781 if( MEMDB ){
danielk19778c0a7912008-08-20 14:49:23 +00003782 sqlite3PcacheCommit(pPager->pPCache, 0);
3783 sqlite3PcacheCleanAll(pPager->pPCache);
3784 sqlite3PcacheSetFlags(pPager->pPCache,
3785 ~(PGHDR_IN_JOURNAL | PGHDR_NEED_SYNC | PGHDR_ALWAYS_ROLLBACK), 0
3786 );
drha6abd042004-06-09 17:37:22 +00003787 pPager->state = PAGER_SHARED;
danielk19778c0a7912008-08-20 14:49:23 +00003788 }else{
3789 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
3790 rc = pager_end_transaction(pPager, pPager->setMaster);
3791 rc = pager_error(pPager, rc);
drhac69b052004-05-12 13:30:07 +00003792 }
drh86f8c192007-08-22 00:39:19 +00003793 return rc;
drhed7c8552001-04-11 14:29:21 +00003794}
3795
3796/*
drha6abd042004-06-09 17:37:22 +00003797** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00003798** All in-memory cache pages revert to their original data contents.
3799** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00003800**
3801** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00003802** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00003803** process is writing trash into the journal file (SQLITE_CORRUPT) or
3804** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
3805** codes are returned for all these occasions. Otherwise,
3806** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00003807*/
danielk19773b8a05f2007-03-19 17:44:26 +00003808int sqlite3PagerRollback(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003809 int rc = SQLITE_OK;
drh4f0c5872007-03-26 22:05:01 +00003810 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003811 if( MEMDB ){
danielk19778c0a7912008-08-20 14:49:23 +00003812 sqlite3PcacheRollback(pPager->pPCache, 1);
3813 sqlite3PcacheRollback(pPager->pPCache, 0);
3814 sqlite3PcacheCleanAll(pPager->pPCache);
3815 sqlite3PcacheSetFlags(pPager->pPCache,
3816 ~(PGHDR_IN_JOURNAL | PGHDR_NEED_SYNC | PGHDR_ALWAYS_ROLLBACK), 0
3817 );
drhac69b052004-05-12 13:30:07 +00003818 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00003819 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00003820 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00003821 pPager->state = PAGER_SHARED;
danielk19778c0a7912008-08-20 14:49:23 +00003822 }else if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00003823 rc = pager_end_transaction(pPager, pPager->setMaster);
danielk19778c0a7912008-08-20 14:49:23 +00003824 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00003825 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00003826 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00003827 }
danielk19778c0a7912008-08-20 14:49:23 +00003828 rc = pPager->errCode;
drha6abd042004-06-09 17:37:22 +00003829 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003830 if( pPager->state==PAGER_RESERVED ){
3831 int rc2;
3832 rc = pager_playback(pPager, 0);
3833 rc2 = pager_end_transaction(pPager, pPager->setMaster);
3834 if( rc==SQLITE_OK ){
3835 rc = rc2;
3836 }
3837 }else{
3838 rc = pager_playback(pPager, 0);
3839 }
danielk197707cb5602006-01-20 10:55:05 +00003840
danielk19778c0a7912008-08-20 14:49:23 +00003841 pPager->dbSize = -1;
3842
3843 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
3844 ** cache. So call pager_error() on the way out to make any error
3845 ** persistent.
3846 */
3847 rc = pager_error(pPager, rc);
3848 }
drh86f8c192007-08-22 00:39:19 +00003849 return rc;
drh98808ba2001-10-18 12:34:46 +00003850}
drhd9b02572001-04-15 00:37:09 +00003851
3852/*
drh5e00f6c2001-09-13 13:46:56 +00003853** Return TRUE if the database file is opened read-only. Return FALSE
3854** if the database is (in theory) writable.
3855*/
danielk19773b8a05f2007-03-19 17:44:26 +00003856int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00003857 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00003858}
3859
3860/*
drh0f7eb612006-08-08 13:51:43 +00003861** Return the number of references to the pager.
3862*/
danielk19773b8a05f2007-03-19 17:44:26 +00003863int sqlite3PagerRefcount(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003864 return sqlite3PcacheRefCount(pPager->pPCache);
drh0f7eb612006-08-08 13:51:43 +00003865}
3866
3867#ifdef SQLITE_TEST
3868/*
drhd9b02572001-04-15 00:37:09 +00003869** This routine is used for testing and analysis only.
3870*/
danielk19773b8a05f2007-03-19 17:44:26 +00003871int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00003872 static int a[11];
danielk19778c0a7912008-08-20 14:49:23 +00003873 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
3874 a[1] = sqlite3PcachePagecount(pPager->pPCache);
3875 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
drhd9b02572001-04-15 00:37:09 +00003876 a[3] = pPager->dbSize;
3877 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00003878 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003879 a[6] = pPager->nHit;
3880 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00003881 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00003882 a[9] = pPager->nRead;
3883 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00003884 return a;
3885}
danielk197717b90b52008-06-06 11:11:25 +00003886int sqlite3PagerIsMemdb(Pager *pPager){
3887 return MEMDB;
3888}
drh0f7eb612006-08-08 13:51:43 +00003889#endif
drhdd793422001-06-28 01:54:48 +00003890
drhfa86c412002-02-02 15:01:15 +00003891/*
drhac69b052004-05-12 13:30:07 +00003892** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00003893**
3894** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00003895** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00003896** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00003897*/
drh86f8c192007-08-22 00:39:19 +00003898static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00003899 int rc;
drhac69b052004-05-12 13:30:07 +00003900 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00003901 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00003902 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00003903 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003904 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00003905 pPager->stmtInUse = 1;
3906 pPager->stmtSize = pPager->dbSize;
3907 return SQLITE_OK;
3908 }
drhda47d772002-12-02 04:25:19 +00003909 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00003910 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00003911 return SQLITE_OK;
3912 }
drhfa86c412002-02-02 15:01:15 +00003913 assert( pPager->journalOpen );
drhf5e7bb52008-02-18 14:47:33 +00003914 assert( pPager->pInStmt==0 );
3915 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00003916 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003917 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00003918 return SQLITE_NOMEM;
3919 }
danielk197776572402004-06-25 02:38:54 +00003920 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00003921 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00003922 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00003923 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00003924 if( !pPager->stmtOpen ){
danielk197717b90b52008-06-06 11:11:25 +00003925 rc = sqlite3PagerOpentemp(pPager, pPager->stfd, SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00003926 if( rc ){
3927 goto stmt_begin_failed;
3928 }
drhac69b052004-05-12 13:30:07 +00003929 pPager->stmtOpen = 1;
3930 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00003931 }
drhac69b052004-05-12 13:30:07 +00003932 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00003933 return SQLITE_OK;
3934
drhac69b052004-05-12 13:30:07 +00003935stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00003936 if( pPager->pInStmt ){
3937 sqlite3BitvecDestroy(pPager->pInStmt);
3938 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00003939 }
3940 return rc;
3941}
drh86f8c192007-08-22 00:39:19 +00003942int sqlite3PagerStmtBegin(Pager *pPager){
3943 int rc;
drh86f8c192007-08-22 00:39:19 +00003944 rc = pagerStmtBegin(pPager);
drh86f8c192007-08-22 00:39:19 +00003945 return rc;
3946}
drhfa86c412002-02-02 15:01:15 +00003947
3948/*
drhac69b052004-05-12 13:30:07 +00003949** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00003950*/
danielk19773b8a05f2007-03-19 17:44:26 +00003951int sqlite3PagerStmtCommit(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00003952 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00003953 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003954 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00003955 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00003956 sqlite3BitvecDestroy(pPager->pInStmt);
3957 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003958 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003959 sqlite3PcacheCommit(pPager->pPCache, 1);
drhac69b052004-05-12 13:30:07 +00003960 }
3961 pPager->stmtNRec = 0;
3962 pPager->stmtInUse = 0;
drh663fc632002-02-02 18:49:19 +00003963 }
drhac69b052004-05-12 13:30:07 +00003964 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00003965 return SQLITE_OK;
3966}
3967
3968/*
drhac69b052004-05-12 13:30:07 +00003969** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00003970*/
danielk19773b8a05f2007-03-19 17:44:26 +00003971int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00003972 int rc;
drhac69b052004-05-12 13:30:07 +00003973 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00003974 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003975 if( MEMDB ){
danielk19778c0a7912008-08-20 14:49:23 +00003976 sqlite3PcacheRollback(pPager->pPCache, 1);
drhac69b052004-05-12 13:30:07 +00003977 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00003978 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00003979 rc = SQLITE_OK;
3980 }else{
3981 rc = pager_stmt_playback(pPager);
3982 }
danielk19773b8a05f2007-03-19 17:44:26 +00003983 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00003984 }else{
3985 rc = SQLITE_OK;
3986 }
drhac69b052004-05-12 13:30:07 +00003987 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00003988 return rc;
3989}
3990
drh73509ee2003-04-06 20:44:45 +00003991/*
3992** Return the full pathname of the database file.
3993*/
danielk19773b8a05f2007-03-19 17:44:26 +00003994const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00003995 return pPager->zFilename;
3996}
3997
drhb20ea9d2004-02-09 01:20:36 +00003998/*
drhd0679ed2007-08-28 22:24:34 +00003999** Return the VFS structure for the pager.
4000*/
4001const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4002 return pPager->pVfs;
4003}
4004
4005/*
drhcc6bb3e2007-08-31 16:11:35 +00004006** Return the file handle for the database file associated
4007** with the pager. This might return NULL if the file has
4008** not yet been opened.
4009*/
4010sqlite3_file *sqlite3PagerFile(Pager *pPager){
4011 return pPager->fd;
4012}
4013
4014/*
danielk19775865e3d2004-06-14 06:03:57 +00004015** Return the directory of the database file.
4016*/
danielk19773b8a05f2007-03-19 17:44:26 +00004017const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004018 return pPager->zDirectory;
4019}
4020
4021/*
4022** Return the full pathname of the journal file.
4023*/
danielk19773b8a05f2007-03-19 17:44:26 +00004024const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004025 return pPager->zJournal;
4026}
4027
4028/*
drh2c8997b2005-08-27 16:36:48 +00004029** Return true if fsync() calls are disabled for this pager. Return FALSE
4030** if fsync()s are executed normally.
4031*/
danielk19773b8a05f2007-03-19 17:44:26 +00004032int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004033 return pPager->noSync;
4034}
4035
drh7c4ac0c2007-04-05 11:25:58 +00004036#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004037/*
drhb20ea9d2004-02-09 01:20:36 +00004038** Set the codec for this pager
4039*/
danielk19773b8a05f2007-03-19 17:44:26 +00004040void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004041 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004042 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004043 void *pCodecArg
4044){
4045 pPager->xCodec = xCodec;
4046 pPager->pCodecArg = pCodecArg;
4047}
drh7c4ac0c2007-04-05 11:25:58 +00004048#endif
drhb20ea9d2004-02-09 01:20:36 +00004049
danielk1977687566d2004-11-02 12:56:41 +00004050#ifndef SQLITE_OMIT_AUTOVACUUM
4051/*
danielk197787c29a92008-01-18 11:33:16 +00004052** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004053**
drh5e385312007-06-16 04:42:12 +00004054** There must be no references to the page previously located at
4055** pgno (which we call pPgOld) though that page is allowed to be
4056** in cache. If the page previous located at pgno is not already
4057** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004058**
drh5e385312007-06-16 04:42:12 +00004059** References to the page pPg remain valid. Updating any
4060** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004061** allocated along with the page) is the responsibility of the caller.
4062**
danielk19775fd057a2005-03-09 13:09:43 +00004063** A transaction must be active when this routine is called. It used to be
4064** required that a statement transaction was not active, but this restriction
4065** has been removed (CREATE INDEX needs to move a page when a statement
4066** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00004067**
4068** If the fourth argument, isCommit, is non-zero, then this page is being
4069** moved as part of a database reorganization just before the transaction
4070** is being committed. In this case, it is guaranteed that the database page
4071** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00004072*/
danielk19774c999992008-07-16 18:17:55 +00004073int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00004074 PgHdr *pPgOld; /* The page being overwritten. */
danielk197794daf7f2004-11-08 09:26:09 +00004075 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004076
danielk1977687566d2004-11-02 12:56:41 +00004077 assert( pPg->nRef>0 );
4078
drh4f0c5872007-03-26 22:05:01 +00004079 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk19778c0a7912008-08-20 14:49:23 +00004080 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno);
drhb0603412007-02-28 04:47:26 +00004081 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004082
danielk1977b4626a32007-04-28 15:47:43 +00004083 pager_get_content(pPg);
danielk19774c999992008-07-16 18:17:55 +00004084
4085 /* If the journal needs to be sync()ed before page pPg->pgno can
4086 ** be written to, store pPg->pgno in local variable needSyncPgno.
4087 **
4088 ** If the isCommit flag is set, there is no need to remember that
4089 ** the journal needs to be sync()ed before database page pPg->pgno
4090 ** can be written to. The caller has already promised not to write to it.
4091 */
danielk19778c0a7912008-08-20 14:49:23 +00004092 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00004093 needSyncPgno = pPg->pgno;
danielk19778c0a7912008-08-20 14:49:23 +00004094 assert( (pPg->flags&PGHDR_IN_JOURNAL) || (int)pgno>pPager->origDbSize );
4095 assert( pPg->flags&PGHDR_DIRTY );
danielk1977ae825582004-11-23 09:06:55 +00004096 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004097 }
4098
danielk1977ef73ee92004-11-06 12:26:07 +00004099 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00004100 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00004101 ** page pgno before the 'move' operation, it needs to be retained
4102 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004103 */
danielk19778c0a7912008-08-20 14:49:23 +00004104 pPg->flags &= ~(PGHDR_NEED_SYNC|PGHDR_IN_JOURNAL);
danielk1977687566d2004-11-02 12:56:41 +00004105 pPgOld = pager_lookup(pPager, pgno);
danielk19778c0a7912008-08-20 14:49:23 +00004106 assert( !pPgOld || pPgOld->nRef==1 );
danielk1977687566d2004-11-02 12:56:41 +00004107 if( pPgOld ){
danielk19778c0a7912008-08-20 14:49:23 +00004108 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
drh5e385312007-06-16 04:42:12 +00004109 }
danielk19778c0a7912008-08-20 14:49:23 +00004110 if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){
4111 pPg->flags |= PGHDR_IN_JOURNAL;
4112 }
danielk1977687566d2004-11-02 12:56:41 +00004113
danielk19778c0a7912008-08-20 14:49:23 +00004114 sqlite3PcacheMove(pPg, pgno);
4115 if( pPgOld ){
4116 sqlite3PcacheMove(pPgOld, 0);
4117 sqlite3PcacheRelease(pPgOld);
danielk1977f5fdda82004-11-03 08:44:05 +00004118 }
danielk1977f5fdda82004-11-03 08:44:05 +00004119
drh605ad8f2006-05-03 23:34:05 +00004120 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004121 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004122 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00004123
danielk197794daf7f2004-11-08 09:26:09 +00004124 if( needSyncPgno ){
4125 /* If needSyncPgno is non-zero, then the journal file needs to be
4126 ** sync()ed before any data is written to database file page needSyncPgno.
4127 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00004128 ** "is journaled" bitvec flag has been set. This needs to be remedied by
4129 ** loading the page into the pager-cache and setting the PgHdr.needSync
4130 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00004131 **
danielk1977a98d7b42008-01-18 13:42:54 +00004132 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00004133 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00004134 ** array. Otherwise, if the page is loaded and written again in
4135 ** this transaction, it may be written to the database file before
4136 ** it is synced into the journal file. This way, it may end up in
4137 ** the journal file twice, but that is not a problem.
4138 **
danielk19773b8a05f2007-03-19 17:44:26 +00004139 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004140 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004141 */
4142 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004143 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004144 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004145 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00004146 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00004147 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
4148 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00004149 }
danielk197787c29a92008-01-18 11:33:16 +00004150 return rc;
4151 }
danielk1977ae825582004-11-23 09:06:55 +00004152 pPager->needSync = 1;
danielk19778c0a7912008-08-20 14:49:23 +00004153 pPgHdr->flags |= PGHDR_NEED_SYNC;
4154 pPgHdr->flags |= PGHDR_IN_JOURNAL;
danielk19773b8a05f2007-03-19 17:44:26 +00004155 makeDirty(pPgHdr);
4156 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004157 }
4158
danielk1977687566d2004-11-02 12:56:41 +00004159 return SQLITE_OK;
4160}
4161#endif
4162
danielk19773b8a05f2007-03-19 17:44:26 +00004163/*
4164** Return a pointer to the data for the specified page.
4165*/
4166void *sqlite3PagerGetData(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00004167 assert( pPg->nRef>0 );
4168 return pPg->pData;
danielk19773b8a05f2007-03-19 17:44:26 +00004169}
4170
4171/*
4172** Return a pointer to the Pager.nExtra bytes of "extra" space
4173** allocated along with the specified page.
4174*/
4175void *sqlite3PagerGetExtra(DbPage *pPg){
4176 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00004177 return (pPager?pPg->pExtra:0);
danielk19773b8a05f2007-03-19 17:44:26 +00004178}
4179
danielk197741483462007-03-24 16:45:04 +00004180/*
4181** Get/set the locking-mode for this pager. Parameter eMode must be one
4182** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4183** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4184** the locking-mode is set to the value specified.
4185**
4186** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4187** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4188** locking-mode.
4189*/
4190int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004191 assert( eMode==PAGER_LOCKINGMODE_QUERY
4192 || eMode==PAGER_LOCKINGMODE_NORMAL
4193 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4194 assert( PAGER_LOCKINGMODE_QUERY<0 );
4195 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4196 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00004197 pPager->exclusiveMode = eMode;
4198 }
4199 return (int)pPager->exclusiveMode;
4200}
4201
drh3b020132008-04-17 17:02:01 +00004202/*
4203** Get/set the journal-mode for this pager. Parameter eMode must be one
4204** of PAGER_JOURNALMODE_QUERY, PAGER_JOURNALMODE_DELETE or
4205** PAGER_JOURNALMODE_PERSIST. If the parameter is not _QUERY, then
4206** the journal-mode is set to the value specified.
4207**
4208** The returned value is either PAGER_JOURNALMODE_DELETE or
4209** PAGER_JOURNALMODE_PERSIST, indicating the current (possibly updated)
4210** journal-mode.
4211*/
4212int sqlite3PagerJournalMode(Pager *pPager, int eMode){
4213 assert( eMode==PAGER_JOURNALMODE_QUERY
4214 || eMode==PAGER_JOURNALMODE_DELETE
drhfdc40e92008-04-17 20:59:37 +00004215 || eMode==PAGER_JOURNALMODE_PERSIST
4216 || eMode==PAGER_JOURNALMODE_OFF );
drh3b020132008-04-17 17:02:01 +00004217 assert( PAGER_JOURNALMODE_QUERY<0 );
4218 assert( PAGER_JOURNALMODE_DELETE>=0 && PAGER_JOURNALMODE_PERSIST>=0 );
drhfdc40e92008-04-17 20:59:37 +00004219 if( eMode>=0 ){
4220 pPager->journalMode = eMode;
drh3b020132008-04-17 17:02:01 +00004221 }
drhfdc40e92008-04-17 20:59:37 +00004222 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00004223}
4224
danielk1977b53e4962008-06-04 06:45:59 +00004225/*
4226** Get/set the size-limit used for persistent journal files.
4227*/
4228i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
4229 if( iLimit>=-1 ){
4230 pPager->journalSizeLimit = iLimit;
4231 }
4232 return pPager->journalSizeLimit;
4233}
4234
danielk19778c0a7912008-08-20 14:49:23 +00004235void sqlite3PagerAlwaysRollback(Pager *pPager){
4236 pPager->alwaysRollback = 1;
4237}
4238
drh2e66f0b2005-04-28 17:18:48 +00004239#endif /* SQLITE_OMIT_DISKIO */