blob: 5ea075e583f5480d1bed7bdec3a0ce59ab41b415 [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**
drh08609ce2008-11-29 22:49:23 +000021** @(#) $Id: pager.c,v 1.510 2008/11/29 22:49:23 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025
26/*
drhdb48ee02003-01-16 13:42:43 +000027** Macros for troubleshooting. Normally turned off
28*/
danielk1977466be562004-06-10 02:16:01 +000029#if 0
drhd3627af2006-12-18 18:34:51 +000030#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000031#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
32#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
33#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
34#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
35#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000036#else
drh4f0c5872007-03-26 22:05:01 +000037#define PAGERTRACE1(X)
38#define PAGERTRACE2(X,Y)
39#define PAGERTRACE3(X,Y,Z)
40#define PAGERTRACE4(X,Y,Z,W)
41#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000042#endif
43
danielk1977599fcba2004-11-08 07:13:13 +000044/*
drh4f0c5872007-03-26 22:05:01 +000045** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000046** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000047**
drh85b623f2007-12-13 21:54:09 +000048** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000049** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000050** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000051*/
drhc001c582006-03-06 18:23:16 +000052#define PAGERID(p) ((int)(p->fd))
53#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000054
55/*
drhed7c8552001-04-11 14:29:21 +000056** The page cache as a whole is always in one of the following
57** states:
58**
drha6abd042004-06-09 17:37:22 +000059** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000060** writing the database file. There is no
61** data held in memory. This is the initial
62** state.
63**
drha6abd042004-06-09 17:37:22 +000064** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000065** Writing is not permitted. There can be
66** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000067** file at the same time.
drhed7c8552001-04-11 14:29:21 +000068**
drh726de592004-06-10 23:35:50 +000069** PAGER_RESERVED This process has reserved the database for writing
70** but has not yet made any changes. Only one process
71** at a time can reserve the database. The original
72** database file has not been modified so other
73** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000074** database file.
75**
76** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000077** Access is exclusive. No other processes or
78** threads can be reading or writing while one
79** process is writing.
80**
danielk1977aa5ccdf2004-06-14 05:10:42 +000081** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
82** after all dirty pages have been written to the
83** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000084** disk. All that remains to do is to remove or
85** truncate the journal file and the transaction
86** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000087**
drha6abd042004-06-09 17:37:22 +000088** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000089** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000090** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000091** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000092** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000093** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000094** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000095** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000096** PAGER_RESERVED means that there is an open rollback journal.
97** The transition to PAGER_EXCLUSIVE occurs before any changes
98** are made to the database file, though writes to the rollback
99** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
100** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
101** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000102*/
drha6abd042004-06-09 17:37:22 +0000103#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000104#define PAGER_SHARED 1 /* same as SHARED_LOCK */
105#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
106#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
107#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000108
drh684917c2004-10-05 02:41:42 +0000109/*
110** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
111** then failed attempts to get a reserved lock will invoke the busy callback.
112** This is off by default. To see why, consider the following scenario:
113**
114** Suppose thread A already has a shared lock and wants a reserved lock.
115** Thread B already has a reserved lock and wants an exclusive lock. If
116** both threads are using their busy callbacks, it might be a long time
117** be for one of the threads give up and allows the other to proceed.
118** But if the thread trying to get the reserved lock gives up quickly
119** (if it never invokes its busy callback) then the contention will be
120** resolved quickly.
121*/
122#ifndef SQLITE_BUSY_RESERVED_LOCK
123# define SQLITE_BUSY_RESERVED_LOCK 0
124#endif
drhd9b02572001-04-15 00:37:09 +0000125
drhed7c8552001-04-11 14:29:21 +0000126/*
drh887dc4c2004-10-22 16:22:57 +0000127** This macro rounds values up so that if the value is an address it
128** is guaranteed to be an address that is aligned to an 8-byte boundary.
129*/
130#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
131
drh9eb9e262004-02-11 02:18:05 +0000132/*
133** A macro used for invoking the codec if there is one
134*/
135#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000136# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
137# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000138#else
drhc001c582006-03-06 18:23:16 +0000139# define CODEC1(P,D,N,X) /* NO-OP */
140# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000141#endif
142
drhed7c8552001-04-11 14:29:21 +0000143/*
drhed7c8552001-04-11 14:29:21 +0000144** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000145**
drh4f0ee682007-03-30 20:43:40 +0000146** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000147** or SQLITE_FULL. Once one of the first three errors occurs, it persists
148** and is returned as the result of every major pager API call. The
149** SQLITE_FULL return code is slightly different. It persists only until the
150** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000151** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000152** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000153*/
154struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000155 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000156 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000157 u8 journalStarted; /* True if header of journal is synced */
158 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000159 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000160 u8 stmtOpen; /* True if the statement subjournal is open */
161 u8 stmtInUse; /* True we are in a statement subtransaction */
162 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000163 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000164 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000165 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000166 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000167 u8 tempFile; /* zFilename is a temporary file */
168 u8 readOnly; /* True for a read-only database */
169 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000170 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000171 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000172 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000173 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000174 u8 doNotSync; /* Boolean. While true, do not spill the cache */
175 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000176 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drhd138c012008-05-13 00:58:18 +0000177 u8 dbModified; /* True if there are any changes to the Db */
drh80e35f42007-03-30 14:06:34 +0000178 u8 changeCountDone; /* Set after incrementing the change-counter */
danielk1977d92db532008-11-17 04:56:24 +0000179 u8 dbSizeValid; /* Set when dbSize is correct */
drh33f4e022007-09-03 15:19:34 +0000180 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000181 int errCode; /* One of several kinds of errors */
danielk1977d92db532008-11-17 04:56:24 +0000182 Pgno dbSize; /* Number of pages in the file */
183 Pgno origDbSize; /* dbSize before the current change */
184 Pgno stmtSize; /* Size of database (in pages) at stmt_begin() */
drhfcd35c72005-05-21 02:48:08 +0000185 int nRec; /* Number of pages written to the journal */
186 u32 cksumInit; /* Quasi-random value added to every checksum */
187 int stmtNRec; /* Number of records in stmt subjournal */
188 int nExtra; /* Add this many bytes to each in-memory page */
189 int pageSize; /* Number of bytes in a page */
190 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000191 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000192 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000193 Bitvec *pInJournal; /* One bit for each page in the database file */
194 Bitvec *pInStmt; /* One bit for each page in the database */
danielk1977a1fa00d2008-08-27 15:16:33 +0000195 Bitvec *pAlwaysRollback; /* One bit for each page marked always-rollback */
drhfcd35c72005-05-21 02:48:08 +0000196 char *zFilename; /* Name of the database file */
197 char *zJournal; /* Name of the journal file */
198 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000199 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
200 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
danielk19771ceedd32008-11-19 10:22:33 +0000201 int (*xBusyHandler)(void*); /* Function to call when busy */
202 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
drheb206252004-10-01 02:00:31 +0000203 i64 journalOff; /* Current byte offset in the journal file */
204 i64 journalHdr; /* Byte offset to previous journal header */
205 i64 stmtHdrOff; /* First journal header written this statement */
206 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000207 i64 stmtJSize; /* Size of journal at stmt_begin() */
drh98c58352008-11-07 00:24:53 +0000208 u32 sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000209#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000210 int nHit, nMiss; /* Cache hits and missing */
211 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000212#endif
danielk1977eaa06f62008-09-18 17:34:44 +0000213 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000214#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000215 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000216 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000217#endif
danielk19778186df82007-03-06 13:45:59 +0000218 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000219 char dbFileVers[16]; /* Changes whenever database file changes */
danielk1977b53e4962008-06-04 06:45:59 +0000220 i64 journalSizeLimit; /* Size limit for persistent journal files */
danielk19778c0a7912008-08-20 14:49:23 +0000221 PCache *pPCache; /* Pointer to page cache object */
drhd9b02572001-04-15 00:37:09 +0000222};
223
224/*
drh538f5702007-04-13 02:14:30 +0000225** The following global variables hold counters used for
226** testing purposes only. These variables do not exist in
227** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000228*/
229#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000230int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
231int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
232int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
drh538f5702007-04-13 02:14:30 +0000233# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000234#else
drh538f5702007-04-13 02:14:30 +0000235# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000236#endif
237
drh538f5702007-04-13 02:14:30 +0000238
239
drhfcd35c72005-05-21 02:48:08 +0000240/*
drh5e00f6c2001-09-13 13:46:56 +0000241** Journal files begin with the following magic string. The data
242** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000243**
drhae2b40c2004-06-09 19:03:54 +0000244** Since version 2.8.0, the journal format contains additional sanity
245** checking information. If the power fails while the journal is begin
246** written, semi-random garbage data might appear in the journal
247** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000248** to roll the journal back, the database could be corrupted. The additional
249** sanity checking data is an attempt to discover the garbage in the
250** journal and ignore it.
251**
drhae2b40c2004-06-09 19:03:54 +0000252** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000253** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000254** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000255** This cksum is initialized to a 32-bit random value that appears in the
256** journal file right after the header. The random initializer is important,
257** because garbage data that appears at the end of a journal is likely
258** data that was once in other files that have now been deleted. If the
259** garbage data came from an obsolete journal file, the checksums might
260** be correct. But by initializing the checksum to random value which
261** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000262*/
drhae2b40c2004-06-09 19:03:54 +0000263static const unsigned char aJournalMagic[] = {
264 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000265};
266
267/*
drh726de592004-06-10 23:35:50 +0000268** The size of the header and of each page in the journal is determined
269** by the following macros.
drh968af522003-02-11 14:55:40 +0000270*/
drhae2b40c2004-06-09 19:03:54 +0000271#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000272
danielk197776572402004-06-25 02:38:54 +0000273/*
274** The journal header size for this pager. In the future, this could be
275** set to some value read from the disk controller. The important
276** characteristic is that it is the same size as a disk sector.
277*/
278#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
279
drhb7f91642004-10-31 02:22:47 +0000280/*
281** The macro MEMDB is true if we are dealing with an in-memory database.
282** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
283** the value of MEMDB will be a constant and the compiler will optimize
284** out code that would never execute.
285*/
286#ifdef SQLITE_OMIT_MEMORYDB
287# define MEMDB 0
288#else
289# define MEMDB pPager->memDb
290#endif
291
292/*
danielk197776572402004-06-25 02:38:54 +0000293** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
294** reserved for working around a windows/posix incompatibility). It is
295** used in the journal to signify that the remainder of the journal file
296** is devoted to storing a master journal name - there are no more pages to
297** roll back. See comments for function writeMasterJournal() for details.
298*/
danielk1977599fcba2004-11-08 07:13:13 +0000299/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
danielk1977d92db532008-11-17 04:56:24 +0000300#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
danielk197713adf8a2004-06-03 16:08:41 +0000301
drh968af522003-02-11 14:55:40 +0000302/*
danielk197726836652005-01-17 01:33:13 +0000303** The maximum legal page number is (2^31 - 1).
304*/
305#define PAGER_MAX_PGNO 2147483647
306
307/*
danielk1977f35843b2007-04-07 15:03:17 +0000308** Return true if page *pPg has already been written to the statement
309** journal (or statement snapshot has been created, if *pPg is part
310** of an in-memory database).
311*/
312static int pageInStatement(PgHdr *pPg){
313 Pager *pPager = pPg->pPager;
danielk1977b3175382008-10-17 18:51:52 +0000314 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000315}
drh8ca0c722006-05-07 17:49:38 +0000316
danielk1977bc2ca9e2008-11-13 14:28:28 +0000317static int pageInJournal(PgHdr *pPg){
318 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
319}
320
drh8ca0c722006-05-07 17:49:38 +0000321/*
drh34e79ce2004-02-08 06:05:46 +0000322** Read a 32-bit integer from the given file descriptor. Store the integer
323** that is read in *pRes. Return SQLITE_OK if everything worked, or an
324** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000325**
326** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000327*/
danielk197762079062007-08-15 17:08:46 +0000328static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000329 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000330 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000331 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000332 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000333 }
drh94f33312002-08-12 12:29:56 +0000334 return rc;
335}
336
337/*
drh97b57482006-01-10 20:32:32 +0000338** Write a 32-bit integer into a string buffer in big-endian byte order.
339*/
drha3152892007-05-05 11:48:52 +0000340#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000341
342/*
drh34e79ce2004-02-08 06:05:46 +0000343** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
344** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000345*/
danielk197762079062007-08-15 17:08:46 +0000346static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000347 char ac[4];
drh97b57482006-01-10 20:32:32 +0000348 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000349 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000350}
351
drh2554f8b2003-01-22 01:26:44 +0000352/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000353** If file pFd is open, call sqlite3OsUnlock() on it.
354*/
355static int osUnlock(sqlite3_file *pFd, int eLock){
356 if( !pFd->pMethods ){
357 return SQLITE_OK;
358 }
359 return sqlite3OsUnlock(pFd, eLock);
360}
361
362/*
danielk1977c7b60172007-08-22 11:22:03 +0000363** This function determines whether or not the atomic-write optimization
364** can be used with this pager. The optimization can be used if:
365**
366** (a) the value returned by OsDeviceCharacteristics() indicates that
367** a database page may be written atomically, and
368** (b) the value returned by OsSectorSize() is less than or equal
369** to the page size.
370**
371** If the optimization cannot be used, 0 is returned. If it can be used,
372** then the value returned is the size of the journal file when it
373** contains rollback data for exactly one page.
374*/
375#ifdef SQLITE_ENABLE_ATOMIC_WRITE
376static int jrnlBufferSize(Pager *pPager){
377 int dc; /* Device characteristics */
378 int nSector; /* Sector size */
drhfacf0302008-06-17 15:12:00 +0000379 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000380 sqlite3_file *fd = pPager->fd;
381
382 if( fd->pMethods ){
383 dc = sqlite3OsDeviceCharacteristics(fd);
384 nSector = sqlite3OsSectorSize(fd);
drhfacf0302008-06-17 15:12:00 +0000385 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000386 }
387
388 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
389 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
390
drhfacf0302008-06-17 15:12:00 +0000391 if( !fd->pMethods ||
392 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000393 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
394 }
395 return 0;
396}
397#endif
398
399/*
danielk1977aef0bf62005-12-30 16:28:01 +0000400** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000401** code. The first argument is a pointer to the pager structure, the
402** second the error-code about to be returned by a pager API function.
403** The value returned is a copy of the second argument to this function.
404**
drh4f0ee682007-03-30 20:43:40 +0000405** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000406** the error becomes persistent. Until the persisten error is cleared,
407** subsequent API calls on this Pager will immediately return the same
408** error code.
409**
410** A persistent error indicates that the contents of the pager-cache
411** cannot be trusted. This state can be cleared by completely discarding
412** the contents of the pager-cache. If a transaction was active when
413** the persistent error occured, then the rollback journal may need
414** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000415*/
danielk1977ae72d982007-10-03 08:46:44 +0000416static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000417static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000418 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000419 assert(
420 pPager->errCode==SQLITE_FULL ||
421 pPager->errCode==SQLITE_OK ||
422 (pPager->errCode & 0xff)==SQLITE_IOERR
423 );
danielk1977979f38e2007-03-27 16:19:51 +0000424 if(
drh4ac285a2006-09-15 07:28:50 +0000425 rc2==SQLITE_FULL ||
426 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000427 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000428 ){
429 pPager->errCode = rc;
danielk19778c0a7912008-08-20 14:49:23 +0000430 if( pPager->state==PAGER_UNLOCK
431 && sqlite3PcacheRefCount(pPager->pPCache)==0
432 ){
danielk1977ae72d982007-10-03 08:46:44 +0000433 /* If the pager is already unlocked, call pager_unlock() now to
434 ** clear the error state and ensure that the pager-cache is
435 ** completely empty.
436 */
437 pager_unlock(pPager);
438 }
danielk1977aef0bf62005-12-30 16:28:01 +0000439 }
440 return rc;
441}
442
drh477731b2007-06-16 03:06:27 +0000443/*
444** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
445** on the cache using a hash function. This is used for testing
446** and debugging only.
447*/
danielk19773c407372005-02-15 02:54:14 +0000448#ifdef SQLITE_CHECK_PAGES
449/*
450** Return a 32-bit hash of the page data for pPage.
451*/
drh477731b2007-06-16 03:06:27 +0000452static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000453 u32 hash = 0;
454 int i;
drh477731b2007-06-16 03:06:27 +0000455 for(i=0; i<nByte; i++){
456 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000457 }
458 return hash;
459}
drh477731b2007-06-16 03:06:27 +0000460static u32 pager_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000461 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
462}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000463static void pager_set_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000464 pPage->pageHash = pager_pagehash(pPage);
drh477731b2007-06-16 03:06:27 +0000465}
danielk19773c407372005-02-15 02:54:14 +0000466
467/*
468** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
469** is defined, and NDEBUG is not defined, an assert() statement checks
470** that the page is either dirty or still matches the calculated page-hash.
471*/
472#define CHECK_PAGE(x) checkPage(x)
473static void checkPage(PgHdr *pPg){
474 Pager *pPager = pPg->pPager;
danielk1977b3175382008-10-17 18:51:52 +0000475 assert( !pPg->pageHash || pPager->errCode
danielk19778c0a7912008-08-20 14:49:23 +0000476 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
danielk19773c407372005-02-15 02:54:14 +0000477}
478
479#else
drh8ffa8172007-06-18 17:25:17 +0000480#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000481#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000482#define CHECK_PAGE(x)
drh41d30272008-08-20 21:47:45 +0000483#endif /* SQLITE_CHECK_PAGES */
danielk19773c407372005-02-15 02:54:14 +0000484
drhed7c8552001-04-11 14:29:21 +0000485/*
danielk197776572402004-06-25 02:38:54 +0000486** When this is called the journal file for pager pPager must be open.
487** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000488** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000489**
danielk197765839c62007-08-30 08:08:17 +0000490** zMaster must point to a buffer of at least nMaster bytes allocated by
491** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
492** enough space to write the master journal name). If the master journal
493** name in the journal is longer than nMaster bytes (including a
494** nul-terminator), then this is handled as if no master journal name
495** were present in the journal.
496**
497** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000498** SQLITE_OK returned.
499*/
danielk1977d92db532008-11-17 04:56:24 +0000500static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
danielk197776572402004-06-25 02:38:54 +0000501 int rc;
502 u32 len;
drheb206252004-10-01 02:00:31 +0000503 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000504 u32 cksum;
shane0b8d2762008-07-22 05:18:00 +0000505 u32 u; /* Unsigned loop counter */
danielk197776572402004-06-25 02:38:54 +0000506 unsigned char aMagic[8]; /* A buffer to hold the magic header */
507
danielk197765839c62007-08-30 08:08:17 +0000508 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000509
drh054889e2005-11-30 03:20:31 +0000510 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000511 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000512
danielk197762079062007-08-15 17:08:46 +0000513 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000514 if( rc!=SQLITE_OK ) return rc;
515
danielk197765839c62007-08-30 08:08:17 +0000516 if( len>=nMaster ){
517 return SQLITE_OK;
518 }
519
danielk197762079062007-08-15 17:08:46 +0000520 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000521 if( rc!=SQLITE_OK ) return rc;
522
danielk197762079062007-08-15 17:08:46 +0000523 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000524 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
525
danielk197765839c62007-08-30 08:08:17 +0000526 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000527 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000528 return rc;
529 }
danielk197765839c62007-08-30 08:08:17 +0000530 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000531
532 /* See if the checksum matches the master journal name */
shane0b8d2762008-07-22 05:18:00 +0000533 for(u=0; u<len; u++){
534 cksum -= zMaster[u];
danielk197765839c62007-08-30 08:08:17 +0000535 }
danielk19778191bff2004-06-28 04:52:30 +0000536 if( cksum ){
537 /* If the checksum doesn't add up, then one or more of the disk sectors
538 ** containing the master journal filename is corrupted. This means
539 ** definitely roll back, so just return SQLITE_OK and report a (nul)
540 ** master-journal filename.
541 */
danielk197765839c62007-08-30 08:08:17 +0000542 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000543 }
danielk197776572402004-06-25 02:38:54 +0000544
545 return SQLITE_OK;
546}
547
548/*
549** Seek the journal file descriptor to the next sector boundary where a
550** journal header may be read or written. Pager.journalOff is updated with
551** the new seek offset.
552**
553** i.e for a sector size of 512:
554**
555** Input Offset Output Offset
556** ---------------------------------------
557** 0 0
558** 512 512
559** 100 512
560** 2000 2048
561**
562*/
danielk197762079062007-08-15 17:08:46 +0000563static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000564 i64 offset = 0;
565 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000566 if( c ){
567 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
568 }
569 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
570 assert( offset>=c );
571 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
572 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000573}
574
575/*
drhf3a87622008-04-17 14:16:42 +0000576** Write zeros over the header of the journal file. This has the
577** effect of invalidating the journal file and committing the
578** transaction.
579*/
danielk1977df2566a2008-05-07 19:11:03 +0000580static int zeroJournalHdr(Pager *pPager, int doTruncate){
581 int rc = SQLITE_OK;
danielk197755a25a12008-09-11 10:29:15 +0000582 static const char zeroHdr[28] = {0};
drhf3a87622008-04-17 14:16:42 +0000583
danielk1977df2566a2008-05-07 19:11:03 +0000584 if( pPager->journalOff ){
danielk1977b53e4962008-06-04 06:45:59 +0000585 i64 iLimit = pPager->journalSizeLimit;
586
danielk1977df2566a2008-05-07 19:11:03 +0000587 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000588 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000589 rc = sqlite3OsTruncate(pPager->jfd, 0);
590 }else{
591 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
592 }
danielk197781620542008-06-07 05:19:37 +0000593 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000594 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
595 }
danielk1977b53e4962008-06-04 06:45:59 +0000596
597 /* At this point the transaction is committed but the write lock
598 ** is still held on the file. If there is a size limit configured for
599 ** the persistent journal and the journal file currently consumes more
600 ** space than that limit allows for, truncate it now. There is no need
601 ** to sync the file following this operation.
602 */
603 if( rc==SQLITE_OK && iLimit>0 ){
604 i64 sz;
605 rc = sqlite3OsFileSize(pPager->jfd, &sz);
606 if( rc==SQLITE_OK && sz>iLimit ){
607 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
608 }
609 }
drha06ecba2008-04-22 17:15:17 +0000610 }
drhf3a87622008-04-17 14:16:42 +0000611 return rc;
612}
613
614/*
danielk197776572402004-06-25 02:38:54 +0000615** The journal file must be open when this routine is called. A journal
616** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
617** current location.
618**
619** The format for the journal header is as follows:
620** - 8 bytes: Magic identifying journal format.
621** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
622** - 4 bytes: Random number used for page hash.
623** - 4 bytes: Initial database page count.
624** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000625** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000626**
danielk197767c007b2008-03-20 04:45:49 +0000627** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000628*/
629static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +0000630 int rc = SQLITE_OK;
631 char *zHeader = pPager->pTmpSpace;
danielk1977d92db532008-11-17 04:56:24 +0000632 u32 nHeader = pPager->pageSize;
633 u32 nWrite;
danielk1977a664f8e2008-04-22 14:31:48 +0000634
635 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
636 nHeader = JOURNAL_HDR_SZ(pPager);
637 }
danielk197776572402004-06-25 02:38:54 +0000638
danielk19774099f6e2007-03-19 11:25:20 +0000639 if( pPager->stmtHdrOff==0 ){
640 pPager->stmtHdrOff = pPager->journalOff;
641 }
642
danielk197762079062007-08-15 17:08:46 +0000643 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000644 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000645
drh97b57482006-01-10 20:32:32 +0000646 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000647
648 /*
649 ** Write the nRec Field - the number of page records that follow this
650 ** journal header. Normally, zero is written to this value at this time.
651 ** After the records are added to the journal (and the journal synced,
652 ** if in full-sync mode), the zero is overwritten with the true number
653 ** of records (see syncJournal()).
654 **
655 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
656 ** reading the journal this value tells SQLite to assume that the
657 ** rest of the journal file contains valid page records. This assumption
658 ** is dangerous, as if a failure occured whilst writing to the journal
659 ** file it may contain some garbage data. There are two scenarios
660 ** where this risk can be ignored:
661 **
662 ** * When the pager is in no-sync mode. Corruption can follow a
663 ** power failure in this case anyway.
664 **
665 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
666 ** that garbage data is never appended to the journal file.
667 */
668 assert(pPager->fd->pMethods||pPager->noSync);
danielk1977b3175382008-10-17 18:51:52 +0000669 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
danielk19774cd2cd52007-08-23 14:48:23 +0000670 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
671 ){
672 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
673 }else{
674 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
675 }
676
drh97b57482006-01-10 20:32:32 +0000677 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +0000678 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +0000679 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
680 /* The initial database size */
681 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
682 /* The assumed sector size for this process */
683 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drh08609ce2008-11-29 22:49:23 +0000684
685 /* Initializing the tail of the buffer is not necessary. Everything
686 ** works find if the following memset() is omitted. But initializing
687 ** the memory prevents valgrind from complaining, so we are willing to
688 ** take the performance hit.
689 */
690 memset(&zHeader[sizeof(aJournalMagic)+16], 0,
691 nHeader-(sizeof(aJournalMagic)+16));
692
danielk197767c007b2008-03-20 04:45:49 +0000693 if( pPager->journalHdr==0 ){
694 /* The page size */
695 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
696 }
danielk197776572402004-06-25 02:38:54 +0000697
danielk1977a664f8e2008-04-22 14:31:48 +0000698 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
699 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
700 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
701 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +0000702 }
danielk1977a664f8e2008-04-22 14:31:48 +0000703
danielk197776572402004-06-25 02:38:54 +0000704 return rc;
705}
706
707/*
708** The journal file must be open when this is called. A journal header file
709** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
710** file. See comments above function writeJournalHdr() for a description of
711** the journal header format.
712**
713** If the header is read successfully, *nRec is set to the number of
714** page records following this header and *dbSize is set to the size of the
715** database before the transaction began, in pages. Also, pPager->cksumInit
716** is set to the value read from the journal header. SQLITE_OK is returned
717** in this case.
718**
719** If the journal header file appears to be corrupted, SQLITE_DONE is
720** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
721** cannot be read from the journal file an error code is returned.
722*/
723static int readJournalHdr(
724 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000725 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000726 u32 *pNRec,
727 u32 *pDbSize
728){
729 int rc;
730 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000731 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +0000732 int iPageSize;
danielk197776572402004-06-25 02:38:54 +0000733
danielk197762079062007-08-15 17:08:46 +0000734 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000735 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
736 return SQLITE_DONE;
737 }
danielk197762079062007-08-15 17:08:46 +0000738 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000739
danielk197762079062007-08-15 17:08:46 +0000740 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000741 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000742 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000743
744 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
745 return SQLITE_DONE;
746 }
747
danielk197762079062007-08-15 17:08:46 +0000748 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000749 if( rc ) return rc;
750
danielk197762079062007-08-15 17:08:46 +0000751 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000752 if( rc ) return rc;
753
danielk197762079062007-08-15 17:08:46 +0000754 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000755 if( rc ) return rc;
756
danielk197767c007b2008-03-20 04:45:49 +0000757 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
758 if( rc==SQLITE_OK
759 && iPageSize>=512
760 && iPageSize<=SQLITE_MAX_PAGE_SIZE
761 && ((iPageSize-1)&iPageSize)==0
762 ){
763 u16 pagesize = iPageSize;
764 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
765 }
766 if( rc ) return rc;
767
danielk197776572402004-06-25 02:38:54 +0000768 /* Update the assumed sector-size to match the value used by
769 ** the process that created this journal. If this journal was
770 ** created by a process other than this one, then this routine
771 ** is being called from within pager_playback(). The local value
772 ** of Pager.sectorSize is restored at the end of that routine.
773 */
drh98c58352008-11-07 00:24:53 +0000774 rc = read32bits(pPager->jfd, jrnlOff+12, &pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000775 if( rc ) return rc;
drh98c58352008-11-07 00:24:53 +0000776 if( (pPager->sectorSize & (pPager->sectorSize-1))!=0
777 || pPager->sectorSize>0x1000000 ){
778 return SQLITE_DONE;
779 }
danielk197776572402004-06-25 02:38:54 +0000780
781 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000782 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000783}
784
785
786/*
787** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000788** pPager at the current location. The master journal name must be the last
789** thing written to a journal file. If the pager is in full-sync mode, the
790** journal file descriptor is advanced to the next sector boundary before
791** anything is written. The format is:
792**
793** + 4 bytes: PAGER_MJ_PGNO.
794** + N bytes: length of master journal name.
795** + 4 bytes: N
796** + 4 bytes: Master journal name checksum.
797** + 8 bytes: aJournalMagic[].
798**
799** The master journal page checksum is the sum of the bytes in the master
800** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000801**
802** If zMaster is a NULL pointer (occurs for a single database transaction),
803** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000804*/
805static int writeMasterJournal(Pager *pPager, const char *zMaster){
806 int rc;
807 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000808 int i;
danielk197762079062007-08-15 17:08:46 +0000809 i64 jrnlOff;
danielk1977df2566a2008-05-07 19:11:03 +0000810 i64 jrnlSize;
drh97b57482006-01-10 20:32:32 +0000811 u32 cksum = 0;
812 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000813
danielk1977b3175382008-10-17 18:51:52 +0000814 if( !zMaster || pPager->setMaster ) return SQLITE_OK;
815 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ) return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000816 pPager->setMaster = 1;
817
818 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000819 for(i=0; i<len; i++){
820 cksum += zMaster[i];
821 }
danielk197776572402004-06-25 02:38:54 +0000822
823 /* If in full-sync mode, advance to the next disk sector before writing
824 ** the master journal name. This is in case the previous page written to
825 ** the journal has already been synced.
826 */
827 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000828 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000829 }
danielk197762079062007-08-15 17:08:46 +0000830 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000831 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000832
danielk197762079062007-08-15 17:08:46 +0000833 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000834 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000835 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000836
danielk197762079062007-08-15 17:08:46 +0000837 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000838 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000839 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000840
drh97b57482006-01-10 20:32:32 +0000841 put32bits(zBuf, len);
842 put32bits(&zBuf[4], cksum);
843 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000844 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +0000845 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +0000846 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +0000847
848 /* If the pager is in peristent-journal mode, then the physical
849 ** journal-file may extend past the end of the master-journal name
850 ** and 8 bytes of magic data just written to the file. This is
851 ** dangerous because the code to rollback a hot-journal file
852 ** will not be able to find the master-journal name to determine
853 ** whether or not the journal is hot.
854 **
855 ** Easiest thing to do in this scenario is to truncate the journal
856 ** file to the required size.
857 */
858 if( (rc==SQLITE_OK)
859 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
860 && jrnlSize>jrnlOff
861 ){
862 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
863 }
danielk197776572402004-06-25 02:38:54 +0000864 return rc;
865}
866
867/*
drhed7c8552001-04-11 14:29:21 +0000868** Find a page in the hash table given its page number. Return
869** a pointer to the page or NULL if not found.
870*/
drhd9b02572001-04-15 00:37:09 +0000871static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000872 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000873 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
drhed7c8552001-04-11 14:29:21 +0000874 return p;
875}
876
877/*
danielk1977e180dd92007-04-05 17:15:52 +0000878** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +0000879** sets the state of the pager back to what it was when it was first
880** opened. Any outstanding pages are invalidated and subsequent attempts
881** to access those pages will likely result in a coredump.
882*/
drhd9b02572001-04-15 00:37:09 +0000883static void pager_reset(Pager *pPager){
danielk1977efaaf572006-01-16 11:29:19 +0000884 if( pPager->errCode ) return;
danielk19778c0a7912008-08-20 14:49:23 +0000885 sqlite3PcacheClear(pPager->pPCache);
danielk1977e277be02007-03-23 18:12:06 +0000886}
887
888/*
danielk1977ae72d982007-10-03 08:46:44 +0000889** Unlock the database file.
890**
891** If the pager is currently in error state, discard the contents of
892** the cache and reset the Pager structure internal state. If there is
893** an open journal-file, then the next time a shared-lock is obtained
894** on the pager file (by this or any other process), it will be
895** treated as a hot-journal and rolled back.
896*/
897static void pager_unlock(Pager *pPager){
898 if( !pPager->exclusiveMode ){
danielk1977b3175382008-10-17 18:51:52 +0000899 int rc = osUnlock(pPager->fd, NO_LOCK);
900 if( rc ) pPager->errCode = rc;
danielk1977d92db532008-11-17 04:56:24 +0000901 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +0000902 IOTRACE(("UNLOCK %p\n", pPager))
danielk1977ae72d982007-10-03 08:46:44 +0000903
danielk1977b3175382008-10-17 18:51:52 +0000904 /* Always close the journal file when dropping the database lock.
905 ** Otherwise, another connection with journal_mode=delete might
906 ** delete the file out from under us.
907 */
908 if( pPager->journalOpen ){
909 sqlite3OsClose(pPager->jfd);
910 pPager->journalOpen = 0;
911 sqlite3BitvecDestroy(pPager->pInJournal);
912 pPager->pInJournal = 0;
913 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
914 pPager->pAlwaysRollback = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000915 }
916
danielk1977b3175382008-10-17 18:51:52 +0000917 /* If Pager.errCode is set, the contents of the pager cache cannot be
918 ** trusted. Now that the pager file is unlocked, the contents of the
919 ** cache can be discarded and the error code safely cleared.
920 */
921 if( pPager->errCode ){
922 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
923 pager_reset(pPager);
924 if( pPager->stmtOpen ){
925 sqlite3OsClose(pPager->stfd);
926 sqlite3BitvecDestroy(pPager->pInStmt);
927 pPager->pInStmt = 0;
928 }
929 pPager->stmtOpen = 0;
930 pPager->stmtInUse = 0;
931 pPager->journalOff = 0;
932 pPager->journalStarted = 0;
933 pPager->stmtAutoopen = 0;
934 pPager->origDbSize = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000935 }
danielk1977b3175382008-10-17 18:51:52 +0000936
937 pPager->state = PAGER_UNLOCK;
938 pPager->changeCountDone = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000939 }
940}
941
942/*
943** Execute a rollback if a transaction is active and unlock the
944** database file. If the pager has already entered the error state,
945** do not attempt the rollback.
946*/
947static void pagerUnlockAndRollback(Pager *p){
danielk1977ae72d982007-10-03 08:46:44 +0000948 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +0000949 sqlite3BeginBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +0000950 sqlite3PagerRollback(p);
danielk19772d1d86f2008-06-20 14:59:51 +0000951 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +0000952 }
953 pager_unlock(p);
danielk1977ae72d982007-10-03 08:46:44 +0000954}
955
956/*
drh80e35f42007-03-30 14:06:34 +0000957** This routine ends a transaction. A transaction is ended by either
958** a COMMIT or a ROLLBACK.
959**
drhed7c8552001-04-11 14:29:21 +0000960** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +0000961** a RESERVED or EXCLUSIVE lock on the database. This routine will release
962** the database lock and acquires a SHARED lock in its place if that is
963** the appropriate thing to do. Release locks usually is appropriate,
964** unless we are in exclusive access mode or unless this is a
965** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
966**
967** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +0000968**
969** TODO: Consider keeping the journal file open for temporary databases.
970** This might give a performance improvement on windows where opening
971** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +0000972*/
danielk1977df2566a2008-05-07 19:11:03 +0000973static int pager_end_transaction(Pager *pPager, int hasMaster){
danielk197741483462007-03-24 16:45:04 +0000974 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +0000975 int rc2 = SQLITE_OK;
drha6abd042004-06-09 17:37:22 +0000976 if( pPager->state<PAGER_RESERVED ){
977 return SQLITE_OK;
978 }
danielk19773b8a05f2007-03-19 17:44:26 +0000979 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +0000980 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +0000981 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +0000982 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +0000983 }
drhda47d772002-12-02 04:25:19 +0000984 if( pPager->journalOpen ){
danielk1977b3175382008-10-17 18:51:52 +0000985 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
986 int isMemoryJournal = sqlite3IsMemJournal(pPager->jfd);
987 sqlite3OsClose(pPager->jfd);
988 pPager->journalOpen = 0;
989 if( !isMemoryJournal ){
990 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
991 }
992 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE
drh04335882008-09-26 21:08:08 +0000993 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){
994 pPager->journalOff = 0;
995 pPager->journalStarted = 0;
996 }else if( pPager->exclusiveMode
danielk197793f7af92008-05-09 16:57:50 +0000997 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
998 ){
999 rc = zeroJournalHdr(pPager, hasMaster);
1000 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +00001001 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001002 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001003 }else{
drh04335882008-09-26 21:08:08 +00001004 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || rc );
danielk1977b4b47412007-08-17 15:53:36 +00001005 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001006 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +00001007 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00001008 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001009 }
danielk197741483462007-03-24 16:45:04 +00001010 }
drhf5e7bb52008-02-18 14:47:33 +00001011 sqlite3BitvecDestroy(pPager->pInJournal);
1012 pPager->pInJournal = 0;
danielk1977a1fa00d2008-08-27 15:16:33 +00001013 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
1014 pPager->pAlwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001015#ifdef SQLITE_CHECK_PAGES
danielk1977bc2ca9e2008-11-13 14:28:28 +00001016 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
danielk19773c407372005-02-15 02:54:14 +00001017#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +00001018 sqlite3PcacheCleanAll(pPager->pPCache);
danielk1977ef317ab2004-06-23 10:43:10 +00001019 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001020 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001021 }else{
drhf5e7bb52008-02-18 14:47:33 +00001022 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001023 }
danielk1977979f38e2007-03-27 16:19:51 +00001024
danielk197741483462007-03-24 16:45:04 +00001025 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001026 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001027 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001028 }else if( pPager->state==PAGER_SYNCED ){
1029 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001030 }
danielk1977334cdb62007-03-26 08:05:12 +00001031 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001032 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001033 pPager->needSync = 0;
danielk19778c0a7912008-08-20 14:49:23 +00001034 /* lruListSetFirstSynced(pPager); */
danielk1977b3175382008-10-17 18:51:52 +00001035 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00001036 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00001037 }
drhd138c012008-05-13 00:58:18 +00001038 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001039
1040 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001041}
1042
drhed7c8552001-04-11 14:29:21 +00001043/*
drh968af522003-02-11 14:55:40 +00001044** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001045**
1046** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001047** random initial value and the page number. We experimented with
1048** a checksum of the entire data, but that was found to be too slow.
1049**
1050** Note that the page number is stored at the beginning of data and
1051** the checksum is stored at the end. This is important. If journal
1052** corruption occurs due to a power failure, the most likely scenario
1053** is that one end or the other of the record will be changed. It is
1054** much less likely that the two ends of the journal record will be
1055** correct and the middle be corrupt. Thus, this "checksum" scheme,
1056** though fast and simple, catches the mostly likely kind of corruption.
1057**
1058** FIX ME: Consider adding every 200th (or so) byte of the data to the
1059** checksum. That way if a single page spans 3 or more disk sectors and
1060** only the middle sector is corrupt, we will still have a reasonable
1061** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001062*/
drh74161702006-02-24 02:53:49 +00001063static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001064 u32 cksum = pPager->cksumInit;
1065 int i = pPager->pageSize-200;
1066 while( i>0 ){
1067 cksum += aData[i];
1068 i -= 200;
1069 }
drh968af522003-02-11 14:55:40 +00001070 return cksum;
1071}
1072
1073/*
drhfa86c412002-02-02 15:01:15 +00001074** Read a single page from the journal file opened on file descriptor
1075** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001076**
drhc13148f2008-08-27 18:03:20 +00001077** The isMainJrnl flag is true if this is the main rollback journal and
1078** false for the statement journal. The main rollback journal uses
1079** checksums - the statement journal does not.
drhfa86c412002-02-02 15:01:15 +00001080*/
danielk197762079062007-08-15 17:08:46 +00001081static int pager_playback_one_page(
drhc13148f2008-08-27 18:03:20 +00001082 Pager *pPager, /* The pager being played back */
1083 sqlite3_file *jfd, /* The file that is the journal being rolled back */
1084 i64 offset, /* Offset of the page within the journal */
1085 int isMainJrnl /* True for main rollback journal. False for Stmt jrnl */
danielk197762079062007-08-15 17:08:46 +00001086){
drhfa86c412002-02-02 15:01:15 +00001087 int rc;
drhae2b40c2004-06-09 19:03:54 +00001088 PgHdr *pPg; /* An existing page in the cache */
1089 Pgno pgno; /* The page number of a page in journal */
1090 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001091 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001092
drhc13148f2008-08-27 18:03:20 +00001093 /* isMainJrnl should be true for the main journal and false for
drh96362842005-03-20 22:47:56 +00001094 ** statement journals. Verify that this is always the case
1095 */
drhc13148f2008-08-27 18:03:20 +00001096 assert( jfd == (isMainJrnl ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001097 assert( aData );
drh96362842005-03-20 22:47:56 +00001098
danielk197762079062007-08-15 17:08:46 +00001099 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001100 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001101 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001102 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001103 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001104
drh968af522003-02-11 14:55:40 +00001105 /* Sanity checking on the page. This is more important that I originally
1106 ** thought. If a power failure occurs while the journal is being written,
1107 ** it could cause invalid data to be written into the journal. We need to
1108 ** detect this invalid data (with high probability) and ignore it.
1109 */
danielk197775edc162004-06-26 01:48:18 +00001110 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001111 return SQLITE_DONE;
1112 }
drhae2b40c2004-06-09 19:03:54 +00001113 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001114 return SQLITE_OK;
1115 }
drhc13148f2008-08-27 18:03:20 +00001116 if( isMainJrnl ){
danielk197762079062007-08-15 17:08:46 +00001117 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001118 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001119 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001120 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001121 return SQLITE_DONE;
1122 }
1123 }
drhfa86c412002-02-02 15:01:15 +00001124
danielk1977aa5ccdf2004-06-14 05:10:42 +00001125 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001126
1127 /* If the pager is in RESERVED state, then there must be a copy of this
1128 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001129 ** not the database file. The page is left marked dirty in this case.
1130 **
danielk19772df71c72007-05-24 07:22:42 +00001131 ** An exception to the above rule: If the database is in no-sync mode
1132 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001133 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1134 ** during a Movepage() call, then the page may not be in the cache
1135 ** either. So the condition described in the above paragraph is not
1136 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001137 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001138 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1139 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001140 **
1141 ** Ticket #1171: The statement journal might contain page content that is
1142 ** different from the page content at the start of the transaction.
1143 ** This occurs when a page is changed prior to the start of a statement
1144 ** then changed again within the statement. When rolling back such a
1145 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001146 ** for certain that original page contents are synced into the main rollback
1147 ** journal. Otherwise, a power loss might leave modified data in the
1148 ** database file without an entry in the rollback journal that can
1149 ** restore the database to its original form. Two conditions must be
1150 ** met before writing to the database files. (1) the database must be
1151 ** locked. (2) we know that the original page content is fully synced
1152 ** in the main journal either because the page is not in cache or else
1153 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001154 **
1155 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1156 ** is possible to fail a statement on a database that does not yet exist.
1157 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001158 */
drhae2b40c2004-06-09 19:03:54 +00001159 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001160 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1161 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
danielk19778c0a7912008-08-20 14:49:23 +00001162 if( (pPager->state>=PAGER_EXCLUSIVE)
1163 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
1164 && (pPager->fd->pMethods)
1165 ){
drh281b21d2008-08-22 12:57:08 +00001166 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
1167 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
danielk1977a3f3a5f2004-06-10 04:32:16 +00001168 }
drhfa86c412002-02-02 15:01:15 +00001169 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001170 /* No page should ever be explicitly rolled back that is in use, except
1171 ** for page 1 which is held in use in order to keep the lock on the
1172 ** database active. However such a page may be rolled back as a result
1173 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001174 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001175 */
drhb6f41482004-05-14 01:58:11 +00001176 void *pData;
danielk19778c0a7912008-08-20 14:49:23 +00001177 pData = pPg->pData;
drhae2b40c2004-06-09 19:03:54 +00001178 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001179 if( pPager->xReiniter ){
danielk1977eaa06f62008-09-18 17:34:44 +00001180 pPager->xReiniter(pPg);
drhde647132004-05-07 17:57:49 +00001181 }
drhc047b9f2008-11-21 03:23:43 +00001182 if( isMainJrnl ){
1183 sqlite3PcacheMakeClean(pPg);
1184 }
danielk19773c407372005-02-15 02:54:14 +00001185#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001186 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001187#endif
drh86a88112007-04-16 15:02:19 +00001188 /* If this was page 1, then restore the value of Pager.dbFileVers.
1189 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001190 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001191 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001192 }
drh86a88112007-04-16 15:02:19 +00001193
1194 /* Decode the page just read from disk */
1195 CODEC1(pPager, pData, pPg->pgno, 3);
danielk19778c0a7912008-08-20 14:49:23 +00001196 sqlite3PcacheRelease(pPg);
drhfa86c412002-02-02 15:01:15 +00001197 }
1198 return rc;
1199}
1200
1201/*
danielk197713adf8a2004-06-03 16:08:41 +00001202** Parameter zMaster is the name of a master journal file. A single journal
1203** file that referred to the master journal file has just been rolled back.
1204** This routine checks if it is possible to delete the master journal file,
1205** and does so if it is.
drh726de592004-06-10 23:35:50 +00001206**
danielk197765839c62007-08-30 08:08:17 +00001207** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1208** available for use within this function.
1209**
1210**
drh726de592004-06-10 23:35:50 +00001211** The master journal file contains the names of all child journals.
1212** To tell if a master journal can be deleted, check to each of the
1213** children. If all children are either missing or do not refer to
1214** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001215*/
danielk1977b4b47412007-08-17 15:53:36 +00001216static int pager_delmaster(Pager *pPager, const char *zMaster){
1217 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001218 int rc;
1219 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001220 sqlite3_file *pMaster;
1221 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001222 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001223 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001224
1225 /* Open the master journal file exclusively in case some other process
1226 ** is running this routine also. Not that it makes too much difference.
1227 */
drhe5ae5732008-06-15 02:51:47 +00001228 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001229 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001230 if( !pMaster ){
1231 rc = SQLITE_NOMEM;
1232 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001233 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1234 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001235 }
danielk197713adf8a2004-06-03 16:08:41 +00001236 if( rc!=SQLITE_OK ) goto delmaster_out;
1237 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001238
1239 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001240 if( rc!=SQLITE_OK ) goto delmaster_out;
1241
1242 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001243 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001244 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001245 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001246
1247 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001248 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001249 */
drhe5ae5732008-06-15 02:51:47 +00001250 zMasterJournal = (char *)sqlite3Malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001251 if( !zMasterJournal ){
1252 rc = SQLITE_NOMEM;
1253 goto delmaster_out;
1254 }
danielk197765839c62007-08-30 08:08:17 +00001255 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001256 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001257 if( rc!=SQLITE_OK ) goto delmaster_out;
1258
danielk19775865e3d2004-06-14 06:03:57 +00001259 zJournal = zMasterJournal;
1260 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001261 int exists;
1262 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1263 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001264 goto delmaster_out;
1265 }
danielk1977861f7452008-06-05 11:39:11 +00001266 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001267 /* One of the journals pointed to by the master journal exists.
1268 ** Open it and check if it points at the master journal. If
1269 ** so, return without deleting the master journal file.
1270 */
drh3b7b78b2004-11-24 01:16:43 +00001271 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001272 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1273 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001274 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001275 goto delmaster_out;
1276 }
danielk197713adf8a2004-06-03 16:08:41 +00001277
danielk197765839c62007-08-30 08:08:17 +00001278 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001279 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001280 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001281 goto delmaster_out;
1282 }
danielk197776572402004-06-25 02:38:54 +00001283
danielk197765839c62007-08-30 08:08:17 +00001284 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001285 if( c ){
danielk197776572402004-06-25 02:38:54 +00001286 /* We have a match. Do not delete the master journal file. */
1287 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001288 }
1289 }
danielk19775865e3d2004-06-14 06:03:57 +00001290 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001291 }
1292 }
1293
danielk1977fee2d252007-08-18 10:59:19 +00001294 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001295
1296delmaster_out:
1297 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001298 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001299 }
1300 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001301 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001302 }
danielk1977b4b47412007-08-17 15:53:36 +00001303 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001304 return rc;
1305}
1306
drha6abd042004-06-09 17:37:22 +00001307
danielk1977e180dd92007-04-05 17:15:52 +00001308static void pager_truncate_cache(Pager *pPager);
1309
drha6abd042004-06-09 17:37:22 +00001310/*
drhcb4c40b2004-08-18 19:09:43 +00001311** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001312** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001313**
1314** Might might be the case that the file on disk is smaller than nPage.
1315** This can happen, for example, if we are in the middle of a transaction
1316** which has extended the file size and the new pages are still all held
1317** in cache, then an INSERT or UPDATE does a statement rollback. Some
1318** operating system implementations can get confused if you try to
1319** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001320** so detect this case and write a single zero byte to the end of the new
1321** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001322*/
danielk1977d92db532008-11-17 04:56:24 +00001323static int pager_truncate(Pager *pPager, Pgno nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001324 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001325 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001326 i64 currentSize, newSize;
1327 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1328 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001329 if( rc==SQLITE_OK && currentSize!=newSize ){
1330 if( currentSize>newSize ){
1331 rc = sqlite3OsTruncate(pPager->fd, newSize);
1332 }else{
1333 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1334 }
drh7fe3f7e2007-11-29 18:44:27 +00001335 }
danielk1977e180dd92007-04-05 17:15:52 +00001336 }
1337 if( rc==SQLITE_OK ){
1338 pPager->dbSize = nPage;
1339 pager_truncate_cache(pPager);
1340 }
1341 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001342}
1343
1344/*
drhc80f0582007-05-01 16:59:48 +00001345** Set the sectorSize for the given pager.
1346**
drh334c80d2008-03-28 17:41:13 +00001347** The sector size is at least as big as the sector size reported
1348** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001349*/
1350static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001351 assert(pPager->fd->pMethods||pPager->tempFile);
1352 if( !pPager->tempFile ){
1353 /* Sector size doesn't matter for temporary files. Also, the file
1354 ** may not have been opened yet, in whcih case the OsSectorSize()
1355 ** call will segfault.
1356 */
1357 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1358 }
drh334c80d2008-03-28 17:41:13 +00001359 if( pPager->sectorSize<512 ){
1360 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001361 }
1362}
1363
1364/*
drhed7c8552001-04-11 14:29:21 +00001365** Playback the journal and thus restore the database file to
1366** the state it was in before we started making changes.
1367**
drh34e79ce2004-02-08 06:05:46 +00001368** The journal file format is as follows:
1369**
drhae2b40c2004-06-09 19:03:54 +00001370** (1) 8 byte prefix. A copy of aJournalMagic[].
1371** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001372** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001373** number of page records from the journal size.
1374** (3) 4 byte big-endian integer which is the initial value for the
1375** sanity checksum.
1376** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001377** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001378** (5) 4 byte big-endian integer which is the sector size. The header
1379** is this many bytes in size.
1380** (6) 4 byte big-endian integer which is the page case.
1381** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001382** name. The value may be zero (indicate that there is no master
1383** journal.)
drh334c80d2008-03-28 17:41:13 +00001384** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001385** and might be shorter than the value read from (5). If the first byte
1386** of the name is \000 then there is no master journal. The master
1387** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001388** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001389** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001390** + pPager->pageSize bytes of data.
1391** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001392**
drh334c80d2008-03-28 17:41:13 +00001393** When we speak of the journal header, we mean the first 8 items above.
1394** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001395**
1396** Call the value from the second bullet "nRec". nRec is the number of
1397** valid page entries in the journal. In most cases, you can compute the
1398** value of nRec from the size of the journal file. But if a power
1399** failure occurred while the journal was being written, it could be the
1400** case that the size of the journal file had already been increased but
1401** the extra entries had not yet made it safely to disk. In such a case,
1402** the value of nRec computed from the file size would be too large. For
1403** that reason, we always use the nRec value in the header.
1404**
1405** If the nRec value is 0xffffffff it means that nRec should be computed
1406** from the file size. This value is used when the user selects the
1407** no-sync option for the journal. A power failure could lead to corruption
1408** in this case. But for things like temporary table (which will be
1409** deleted when the power is restored) we don't care.
1410**
drhd9b02572001-04-15 00:37:09 +00001411** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001412** journal file then all pages up to the first corrupted page are rolled
1413** back (or no pages if the journal header is corrupted). The journal file
1414** is then deleted and SQLITE_OK returned, just as if no corruption had
1415** been encountered.
1416**
1417** If an I/O or malloc() error occurs, the journal-file is not deleted
1418** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001419*/
danielk1977e277be02007-03-23 18:12:06 +00001420static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001421 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001422 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001423 u32 nRec; /* Number of Records in the journal */
shane0b8d2762008-07-22 05:18:00 +00001424 u32 u; /* Unsigned loop counter */
drhd9b02572001-04-15 00:37:09 +00001425 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001426 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001427 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001428 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001429
drhc3a64ba2001-11-22 00:01:27 +00001430 /* Figure out how many records are in the journal. Abort early if
1431 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001432 */
drh8cfbf082001-09-19 13:22:39 +00001433 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001434 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001435 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001436 goto end_playback;
1437 }
drh240c5792004-02-08 00:40:52 +00001438
danielk197776572402004-06-25 02:38:54 +00001439 /* Read the master journal name from the journal, if it is present.
1440 ** If a master journal file name is specified, but the file is not
1441 ** present on disk, then the journal is not hot and does not need to be
1442 ** played back.
drh240c5792004-02-08 00:40:52 +00001443 */
danielk197765839c62007-08-30 08:08:17 +00001444 zMaster = pPager->pTmpSpace;
1445 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001446 if( rc==SQLITE_OK && zMaster[0] ){
1447 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001448 }
danielk197765839c62007-08-30 08:08:17 +00001449 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001450 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001451 goto end_playback;
1452 }
1453 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001454
danielk197776572402004-06-25 02:38:54 +00001455 /* This loop terminates either when the readJournalHdr() call returns
1456 ** SQLITE_DONE or an IO error occurs. */
1457 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001458
danielk197776572402004-06-25 02:38:54 +00001459 /* Read the next journal header from the journal file. If there are
1460 ** not enough bytes left in the journal file for a complete header, or
1461 ** it is corrupted, then a process must of failed while writing it.
1462 ** This indicates nothing more needs to be rolled back.
1463 */
1464 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1465 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001466 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001467 rc = SQLITE_OK;
1468 }
danielk197776572402004-06-25 02:38:54 +00001469 goto end_playback;
1470 }
1471
1472 /* If nRec is 0xffffffff, then this journal was created by a process
1473 ** working in no-sync mode. This means that the rest of the journal
1474 ** file consists of pages, there are no more journal headers. Compute
1475 ** the value of nRec based on this assumption.
1476 */
1477 if( nRec==0xffffffff ){
1478 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1479 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1480 }
1481
danielk1977e277be02007-03-23 18:12:06 +00001482 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001483 ** process and if this is the final header in the journal, then it means
1484 ** that this part of the journal was being filled but has not yet been
1485 ** synced to disk. Compute the number of pages based on the remaining
1486 ** size of the file.
1487 **
1488 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001489 */
drh8940f4e2007-08-11 00:26:20 +00001490 if( nRec==0 && !isHot &&
1491 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001492 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1493 }
1494
danielk197776572402004-06-25 02:38:54 +00001495 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001496 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001497 */
danielk1977e180dd92007-04-05 17:15:52 +00001498 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001499 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001500 if( rc!=SQLITE_OK ){
1501 goto end_playback;
1502 }
danielk197776572402004-06-25 02:38:54 +00001503 }
1504
danielk197776572402004-06-25 02:38:54 +00001505 /* Copy original pages out of the journal and back into the database file.
1506 */
shane0b8d2762008-07-22 05:18:00 +00001507 for(u=0; u<nRec; u++){
danielk197762079062007-08-15 17:08:46 +00001508 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001509 if( rc!=SQLITE_OK ){
1510 if( rc==SQLITE_DONE ){
1511 rc = SQLITE_OK;
1512 pPager->journalOff = szJ;
1513 break;
1514 }else{
drha9625ea2008-09-03 00:08:29 +00001515 /* If we are unable to rollback, then the database is probably
1516 ** going to end up being corrupt. It is corrupt to us, anyhow.
1517 ** Perhaps the next process to come along can fix it....
1518 */
danielk197798c21902008-09-23 16:41:29 +00001519 rc = SQLITE_CORRUPT_BKPT;
danielk197776572402004-06-25 02:38:54 +00001520 goto end_playback;
1521 }
1522 }
drh968af522003-02-11 14:55:40 +00001523 }
drhed7c8552001-04-11 14:29:21 +00001524 }
drh580eeaf2006-02-24 03:09:37 +00001525 /*NOTREACHED*/
1526 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001527
1528end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001529 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001530 zMaster = pPager->pTmpSpace;
1531 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1532 }
1533 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001534 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001535 }
danielk1977c56774e2008-10-07 11:51:20 +00001536 if( rc==SQLITE_OK && zMaster[0] && res ){
danielk1977979f38e2007-03-27 16:19:51 +00001537 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001538 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001539 */
danielk197765839c62007-08-30 08:08:17 +00001540 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001541 }
danielk197776572402004-06-25 02:38:54 +00001542
1543 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001544 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001545 ** value. Reset it to the correct value for this process.
1546 */
drhc80f0582007-05-01 16:59:48 +00001547 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001548 return rc;
drhed7c8552001-04-11 14:29:21 +00001549}
1550
1551/*
drhac69b052004-05-12 13:30:07 +00001552** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001553**
1554** This is similar to playing back the transaction journal but with
1555** a few extra twists.
1556**
drh663fc632002-02-02 18:49:19 +00001557** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001558** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001559** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001560**
drhac69b052004-05-12 13:30:07 +00001561** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001562** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001563** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001564*/
drh3aac2dd2004-04-26 14:10:20 +00001565static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001566 i64 szJ; /* Size of the full journal */
1567 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001568 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001569 int i; /* Loop counter */
1570 int rc;
1571
danielk197776572402004-06-25 02:38:54 +00001572 szJ = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001573
danielk19774099f6e2007-03-19 11:25:20 +00001574 /* Set hdrOff to be the offset just after the end of the last journal
1575 ** page written before the first journal-header for this statement
1576 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001577 ** header was written.
1578 */
1579 hdrOff = pPager->stmtHdrOff;
1580 assert( pPager->fullSync || !hdrOff );
1581 if( !hdrOff ){
1582 hdrOff = szJ;
1583 }
1584
drhfa86c412002-02-02 15:01:15 +00001585 /* Truncate the database back to its original size.
1586 */
danielk1977e180dd92007-04-05 17:15:52 +00001587 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001588 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001589
drhac69b052004-05-12 13:30:07 +00001590 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001591 */
drhac69b052004-05-12 13:30:07 +00001592 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001593 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001594
drhac69b052004-05-12 13:30:07 +00001595 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001596 ** database file. Note that the statement journal omits checksums from
1597 ** each record since power-failure recovery is not important to statement
1598 ** journals.
drhfa86c412002-02-02 15:01:15 +00001599 */
danielk197762079062007-08-15 17:08:46 +00001600 for(i=0; i<nRec; i++){
1601 i64 offset = i*(4+pPager->pageSize);
1602 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001603 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001604 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001605 }
1606
danielk197776572402004-06-25 02:38:54 +00001607 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1608 ** was the size of the journal file when this statement was started, so
1609 ** everything after that needs to be rolled back, either into the
1610 ** database, the memory cache, or both.
1611 **
1612 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1613 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001614 */
danielk197776572402004-06-25 02:38:54 +00001615 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001616 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001617 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001618 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001619 assert( rc!=SQLITE_DONE );
1620 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1621 }
1622
1623 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001624 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001625 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001626 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001627 if( rc!=SQLITE_OK ){
1628 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001629 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001630 }
danielk1977f0113002006-01-24 12:09:17 +00001631 if( nJRec==0 ){
1632 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001633 }
danielk1977f0113002006-01-24 12:09:17 +00001634 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001635 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001636 assert( rc!=SQLITE_DONE );
1637 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1638 }
drhfa86c412002-02-02 15:01:15 +00001639 }
danielk197776572402004-06-25 02:38:54 +00001640
1641 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001642
drh3aac2dd2004-04-26 14:10:20 +00001643end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001644 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001645 pPager->journalOff = szJ;
1646 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001647 }
1648 return rc;
1649}
1650
1651/*
drhf57b14a2001-09-14 18:54:08 +00001652** Change the maximum number of in-memory pages that are allowed.
1653*/
danielk19773b8a05f2007-03-19 17:44:26 +00001654void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +00001655 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001656}
1657
1658/*
drh973b6e32003-02-12 14:09:42 +00001659** Adjust the robustness of the database to damage due to OS crashes
1660** or power failures by changing the number of syncs()s when writing
1661** the rollback journal. There are three levels:
1662**
drh054889e2005-11-30 03:20:31 +00001663** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001664** for temporary and transient files.
1665**
1666** NORMAL The journal is synced once before writes begin on the
1667** database. This is normally adequate protection, but
1668** it is theoretically possible, though very unlikely,
1669** that an inopertune power failure could leave the journal
1670** in a state which would cause damage to the database
1671** when it is rolled back.
1672**
1673** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001674** database (with some additional information - the nRec field
1675** of the journal header - being written in between the two
1676** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001677** single disk sector is atomic, then this mode provides
1678** assurance that the journal will not be corrupted to the
1679** point of causing damage to the database during rollback.
1680**
1681** Numeric values associated with these states are OFF==1, NORMAL=2,
1682** and FULL=3.
1683*/
danielk197793758c82005-01-21 08:13:14 +00001684#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh281b21d2008-08-22 12:57:08 +00001685void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
danielk1977b3175382008-10-17 18:51:52 +00001686 pPager->noSync = level==1 || pPager->tempFile;
drh973b6e32003-02-12 14:09:42 +00001687 pPager->fullSync = level==3 && !pPager->tempFile;
drh281b21d2008-08-22 12:57:08 +00001688 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001689 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001690}
danielk197793758c82005-01-21 08:13:14 +00001691#endif
drh973b6e32003-02-12 14:09:42 +00001692
1693/*
drhaf6df112005-06-07 02:12:30 +00001694** The following global variable is incremented whenever the library
1695** attempts to open a temporary file. This information is used for
1696** testing and analysis only.
1697*/
drh0f7eb612006-08-08 13:51:43 +00001698#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001699int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001700#endif
drhaf6df112005-06-07 02:12:30 +00001701
1702/*
drh3f56e6e2007-03-15 01:16:47 +00001703** Open a temporary file.
1704**
1705** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001706** other error code if we fail. The OS will automatically delete the temporary
1707** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001708*/
danielk1977b4b47412007-08-17 15:53:36 +00001709static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00001710 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00001711 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00001712 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00001713){
drhfa86c412002-02-02 15:01:15 +00001714 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001715
drh0f7eb612006-08-08 13:51:43 +00001716#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001717 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001718#endif
danielk1977b4b47412007-08-17 15:53:36 +00001719
drh33f4e022007-09-03 15:19:34 +00001720 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1721 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00001722 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00001723 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00001724 return rc;
1725}
1726
danielk1977a858aa22008-08-22 16:22:17 +00001727static int pagerStress(void *,PgHdr *);
danielk19778c0a7912008-08-20 14:49:23 +00001728
drhfa86c412002-02-02 15:01:15 +00001729/*
drhed7c8552001-04-11 14:29:21 +00001730** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001731** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001732** the first call to sqlite3PagerGet() and is only held open until the
1733** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001734**
drh6446c4d2001-12-15 14:22:18 +00001735** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00001736** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00001737** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001738**
1739** If zFilename is ":memory:" then all information is held in cache.
1740** It is never written to disk. This can be used to implement an
1741** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001742*/
danielk19773b8a05f2007-03-19 17:44:26 +00001743int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001744 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001745 Pager **ppPager, /* Return the Pager structure here */
1746 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001747 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00001748 int flags, /* flags controlling this file */
1749 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00001750){
danielk1977b4b47412007-08-17 15:53:36 +00001751 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001752 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001753 int rc = SQLITE_OK;
1754 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001755 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001756 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001757 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001758 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1759 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977b3175382008-10-17 18:51:52 +00001760 int journalFileSize;
danielk19778c0a7912008-08-20 14:49:23 +00001761 int pcacheSize = sqlite3PcacheSize();
drhfacf0302008-06-17 15:12:00 +00001762 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
danielk197717b90b52008-06-06 11:11:25 +00001763 char *zPathname = 0;
1764 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001765
danielk1977b3175382008-10-17 18:51:52 +00001766 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
1767 journalFileSize = sqlite3JournalSize(pVfs);
1768 }else{
1769 journalFileSize = sqlite3MemJournalSize();
1770 }
1771
drh86f8c192007-08-22 00:39:19 +00001772 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001773 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001774
danielk197717b90b52008-06-06 11:11:25 +00001775 /* Compute and store the full pathname in an allocated buffer pointed
1776 ** to by zPathname, length nPathname. Or, if this is a temporary file,
1777 ** leave both nPathname and zPathname set to 0.
1778 */
drh1cc8c442007-08-24 16:08:29 +00001779 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00001780 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001781 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00001782 if( zPathname==0 ){
1783 return SQLITE_NOMEM;
1784 }
drh1cc8c442007-08-24 16:08:29 +00001785#ifndef SQLITE_OMIT_MEMORYDB
1786 if( strcmp(zFilename,":memory:")==0 ){
1787 memDb = 1;
1788 zPathname[0] = 0;
1789 }else
1790#endif
1791 {
danielk1977adfb9b02007-09-17 07:02:56 +00001792 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00001793 }
danielk197717b90b52008-06-06 11:11:25 +00001794 if( rc!=SQLITE_OK ){
1795 sqlite3_free(zPathname);
1796 return rc;
1797 }
1798 nPathname = strlen(zPathname);
drhae28c012007-08-24 16:29:23 +00001799 }
drh99b90c32008-03-17 13:50:58 +00001800
danielk1977b4b47412007-08-17 15:53:36 +00001801 /* Allocate memory for the pager structure */
1802 pPager = sqlite3MallocZero(
1803 sizeof(*pPager) + /* Pager structure */
danielk19778c0a7912008-08-20 14:49:23 +00001804 pcacheSize + /* PCache object */
danielk1977c7b60172007-08-22 11:22:03 +00001805 journalFileSize + /* The journal file structure */
danielk1977b3175382008-10-17 18:51:52 +00001806 pVfs->szOsFile + /* The main db file */
1807 journalFileSize * 2 + /* The two journal files */
danielk197717b90b52008-06-06 11:11:25 +00001808 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00001809 );
1810 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00001811 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00001812 return SQLITE_NOMEM;
1813 }
danielk19778c0a7912008-08-20 14:49:23 +00001814 pPager->pPCache = (PCache *)&pPager[1];
1815 pPtr = ((u8 *)&pPager[1]) + pcacheSize;
drh33f4e022007-09-03 15:19:34 +00001816 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00001817 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977b3175382008-10-17 18:51:52 +00001818 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile];
1819 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile+journalFileSize];
1820 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile+2*journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00001821 pPager->zDirectory = &pPager->zFilename[nPathname+1];
1822 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00001823 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00001824 if( zPathname ){
1825 memcpy(pPager->zFilename, zPathname, nPathname+1);
1826 sqlite3_free(zPathname);
1827 }
danielk1977b4b47412007-08-17 15:53:36 +00001828
drh153c62c2007-08-24 03:51:33 +00001829 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00001830 */
drhae28c012007-08-24 16:29:23 +00001831 if( zFilename && zFilename[0] && !memDb ){
danielk1977d92db532008-11-17 04:56:24 +00001832 if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){
drh1cc8c442007-08-24 16:08:29 +00001833 rc = SQLITE_CANTOPEN;
1834 }else{
drh1cc8c442007-08-24 16:08:29 +00001835 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00001836 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
1837 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00001838 readOnly = (fout&SQLITE_OPEN_READONLY);
1839
1840 /* If the file was successfully opened for read/write access,
1841 ** choose a default page size in case we have to create the
1842 ** database file. The default page size is the maximum of:
1843 **
1844 ** + SQLITE_DEFAULT_PAGE_SIZE,
1845 ** + The value returned by sqlite3OsSectorSize()
1846 ** + The largest page size that can be written atomically.
1847 */
1848 if( rc==SQLITE_OK && !readOnly ){
1849 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
drhfacf0302008-06-17 15:12:00 +00001850 if( szPageDflt<iSectorSize ){
1851 szPageDflt = iSectorSize;
drh1cc8c442007-08-24 16:08:29 +00001852 }
danielk19779663b8f2007-08-24 11:52:28 +00001853#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00001854 {
1855 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
1856 int ii;
1857 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
1858 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
1859 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
drhfacf0302008-06-17 15:12:00 +00001860 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
1861 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00001862 }
danielk1977b4b47412007-08-17 15:53:36 +00001863 }
drh1cc8c442007-08-24 16:08:29 +00001864#endif
drhfacf0302008-06-17 15:12:00 +00001865 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
1866 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
drh1cc8c442007-08-24 16:08:29 +00001867 }
danielk19778def5ea2004-06-16 10:39:52 +00001868 }
drhac69b052004-05-12 13:30:07 +00001869 }
danielk1977b3175382008-10-17 18:51:52 +00001870 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00001871 /* If a temporary file is requested, it is not opened immediately.
1872 ** In this case we accept the default page size and delay actually
1873 ** opening the file until the first call to OsWrite().
danielk1977b3175382008-10-17 18:51:52 +00001874 **
1875 ** This branch is also run for an in-memory database. An in-memory
1876 ** database is the same as a temp-file that is never written out to
1877 ** disk and uses an in-memory rollback journal.
danielk19777a2b1ee2007-08-21 14:27:01 +00001878 */
danielk19777a2b1ee2007-08-21 14:27:01 +00001879 tempFile = 1;
1880 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00001881 }
danielk1977aef0bf62005-12-30 16:28:01 +00001882
danielk1977b4b47412007-08-17 15:53:36 +00001883 if( pPager && rc==SQLITE_OK ){
drhfacf0302008-06-17 15:12:00 +00001884 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00001885 }
danielk1977aef0bf62005-12-30 16:28:01 +00001886
drh153c62c2007-08-24 03:51:33 +00001887 /* If an error occured in either of the blocks above.
1888 ** Free the Pager structure and close the file.
1889 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00001890 ** any Pager.errMask variables.
1891 */
danielk1977b4b47412007-08-17 15:53:36 +00001892 if( !pPager || !pPager->pTmpSpace ){
1893 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00001894 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00001895 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001896 }
danielk19778c0a7912008-08-20 14:49:23 +00001897 nExtra = FORCE_ALIGNMENT(nExtra);
danielk197771d5d2c2008-09-29 11:49:47 +00001898 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
drh41d30272008-08-20 21:47:45 +00001899 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
danielk1977aef0bf62005-12-30 16:28:01 +00001900
drh153c62c2007-08-24 03:51:33 +00001901 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
1902 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00001903
danielk1977b4b47412007-08-17 15:53:36 +00001904 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00001905 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00001906 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00001907 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001908
drh99b90c32008-03-17 13:50:58 +00001909 /* Fill in Pager.zJournal[] */
danielk197717b90b52008-06-06 11:11:25 +00001910 if( zPathname ){
1911 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
1912 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
1913 }else{
1914 pPager->zJournal = 0;
1915 }
danielk1977b4b47412007-08-17 15:53:36 +00001916
drh3b59a5c2006-01-15 20:28:28 +00001917 /* pPager->journalOpen = 0; */
drhb3df2e12008-09-17 20:06:26 +00001918 pPager->useJournal = useJournal;
drh7bec5052005-02-06 02:45:41 +00001919 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001920 /* pPager->stmtOpen = 0; */
1921 /* pPager->stmtInUse = 0; */
1922 /* pPager->nRef = 0; */
danielk1977d92db532008-11-17 04:56:24 +00001923 pPager->dbSizeValid = memDb;
drhfacf0302008-06-17 15:12:00 +00001924 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00001925 /* pPager->stmtSize = 0; */
1926 /* pPager->stmtJSize = 0; */
1927 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00001928 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00001929 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00001930 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00001931 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00001932 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00001933 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00001934 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
1935 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
1936 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
1937 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00001938 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001939 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001940 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00001941 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00001942 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00001943 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00001944 /* pPager->pFirst = 0; */
1945 /* pPager->pFirstSynced = 0; */
1946 /* pPager->pLast = 0; */
danielk19778c0a7912008-08-20 14:49:23 +00001947 pPager->nExtra = nExtra;
danielk1977b53e4962008-06-04 06:45:59 +00001948 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk1977b3175382008-10-17 18:51:52 +00001949 assert(pPager->fd->pMethods||tempFile);
1950 setSectorSize(pPager);
1951 if( memDb ){
1952 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
danielk1977b4721172007-03-19 05:54:48 +00001953 }
danielk19771ceedd32008-11-19 10:22:33 +00001954 /* pPager->xBusyHandler = 0; */
1955 /* pPager->pBusyHandlerArg = 0; */
drh3b59a5c2006-01-15 20:28:28 +00001956 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00001957 *ppPager = pPager;
1958 return SQLITE_OK;
1959}
1960
1961/*
drh90f5ecb2004-07-22 01:19:35 +00001962** Set the busy handler function.
1963*/
danielk19771ceedd32008-11-19 10:22:33 +00001964void sqlite3PagerSetBusyhandler(
1965 Pager *pPager,
1966 int (*xBusyHandler)(void *),
1967 void *pBusyHandlerArg
1968){
1969 pPager->xBusyHandler = xBusyHandler;
1970 pPager->pBusyHandlerArg = pBusyHandlerArg;
drh90f5ecb2004-07-22 01:19:35 +00001971}
1972
1973/*
drha6abd042004-06-09 17:37:22 +00001974** Set the reinitializer for this pager. If not NULL, the reinitializer
1975** is called when the content of a page in cache is restored to its original
1976** value as a result of a rollback. The callback gives higher-level code
1977** an opportunity to restore the EXTRA section to agree with the restored
1978** page data.
1979*/
danielk1977eaa06f62008-09-18 17:34:44 +00001980void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
drha6abd042004-06-09 17:37:22 +00001981 pPager->xReiniter = xReinit;
1982}
1983
1984/*
danielk1977a1644fd2007-08-29 12:31:25 +00001985** Set the page size to *pPageSize. If the suggest new page size is
1986** inappropriate, then an alternative page size is set to that
1987** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00001988*/
danielk1977a1644fd2007-08-29 12:31:25 +00001989int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
danielk19771357d9f2008-09-16 05:12:24 +00001990 int rc = pPager->errCode;
1991 if( rc==SQLITE_OK ){
1992 u16 pageSize = *pPageSize;
1993 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
1994 if( pageSize && pageSize!=pPager->pageSize
1995 && (pPager->memDb==0 || pPager->dbSize==0)
1996 && sqlite3PcacheRefCount(pPager->pPCache)==0
1997 ){
1998 char *pNew = (char *)sqlite3PageMalloc(pageSize);
1999 if( !pNew ){
2000 rc = SQLITE_NOMEM;
2001 }else{
2002 pager_reset(pPager);
2003 pPager->pageSize = pageSize;
2004 if( !pPager->memDb ) setSectorSize(pPager);
2005 sqlite3PageFree(pPager->pTmpSpace);
2006 pPager->pTmpSpace = pNew;
2007 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
2008 }
danielk1977a1644fd2007-08-29 12:31:25 +00002009 }
danielk19771357d9f2008-09-16 05:12:24 +00002010 *pPageSize = pPager->pageSize;
drh1c7880e2005-05-20 20:01:55 +00002011 }
danielk1977a1644fd2007-08-29 12:31:25 +00002012 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002013}
2014
2015/*
drh26b79942007-11-28 16:19:56 +00002016** Return a pointer to the "temporary page" buffer held internally
2017** by the pager. This is a buffer that is big enough to hold the
2018** entire content of a database page. This buffer is used internally
2019** during rollback and will be overwritten whenever a rollback
2020** occurs. But other modules are free to use it too, as long as
2021** no rollbacks are happening.
2022*/
2023void *sqlite3PagerTempSpace(Pager *pPager){
2024 return pPager->pTmpSpace;
2025}
2026
2027/*
drhf8e632b2007-05-08 14:51:36 +00002028** Attempt to set the maximum database page count if mxPage is positive.
2029** Make no changes if mxPage is zero or negative. And never reduce the
2030** maximum page count below the current size of the database.
2031**
2032** Regardless of mxPage, return the current maximum page count.
2033*/
2034int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2035 if( mxPage>0 ){
2036 pPager->mxPgno = mxPage;
2037 }
danielk1977ad0132d2008-06-07 08:58:22 +00002038 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002039 return pPager->mxPgno;
2040}
2041
2042/*
drhc9ac5ca2005-11-04 22:03:30 +00002043** The following set of routines are used to disable the simulated
2044** I/O error mechanism. These routines are used to avoid simulated
2045** errors in places where we do not care about errors.
2046**
2047** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2048** and generate no code.
2049*/
2050#ifdef SQLITE_TEST
2051extern int sqlite3_io_error_pending;
2052extern int sqlite3_io_error_hit;
2053static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002054void disable_simulated_io_errors(void){
2055 saved_cnt = sqlite3_io_error_pending;
2056 sqlite3_io_error_pending = -1;
2057}
2058void enable_simulated_io_errors(void){
2059 sqlite3_io_error_pending = saved_cnt;
2060}
2061#else
drh152410f2005-11-05 15:11:22 +00002062# define disable_simulated_io_errors()
2063# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002064#endif
2065
2066/*
drh90f5ecb2004-07-22 01:19:35 +00002067** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002068** that pDest points to.
2069**
2070** No error checking is done. The rational for this is that this function
2071** may be called even if the file does not exist or contain a header. In
2072** these cases sqlite3OsRead() will return an error, to which the correct
2073** response is to zero the memory at pDest and continue. A real IO error
2074** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002075*/
danielk19773b8a05f2007-03-19 17:44:26 +00002076int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002077 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002078 memset(pDest, 0, N);
danielk1977b3175382008-10-17 18:51:52 +00002079 assert(pPager->fd->pMethods||pPager->tempFile);
danielk19777a2b1ee2007-08-21 14:27:01 +00002080 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002081 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002082 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002083 if( rc==SQLITE_IOERR_SHORT_READ ){
2084 rc = SQLITE_OK;
2085 }
drh90f5ecb2004-07-22 01:19:35 +00002086 }
drh551b7732006-11-06 21:20:25 +00002087 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002088}
2089
2090/*
drh5e00f6c2001-09-13 13:46:56 +00002091** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002092** pPager.
2093**
2094** If the PENDING_BYTE lies on the page directly after the end of the
2095** file, then consider this page part of the file too. For example, if
2096** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2097** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002098*/
danielk1977ad0132d2008-06-07 08:58:22 +00002099int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk19777a2b1ee2007-08-21 14:27:01 +00002100 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002101 int rc;
drhd9b02572001-04-15 00:37:09 +00002102 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002103 if( pPager->errCode ){
danielk19778c0a7912008-08-20 14:49:23 +00002104 rc = pPager->errCode;
danielk19778c0a7912008-08-20 14:49:23 +00002105 return rc;
drha7aea3d2007-03-15 12:51:16 +00002106 }
danielk1977d92db532008-11-17 04:56:24 +00002107 if( pPager->dbSizeValid ){
danielk197715f411d2005-09-16 10:18:45 +00002108 n = pPager->dbSize;
2109 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002110 assert(pPager->fd->pMethods||pPager->tempFile);
2111 if( (pPager->fd->pMethods)
2112 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002113 pager_error(pPager, rc);
danielk1977ad0132d2008-06-07 08:58:22 +00002114 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002115 }
2116 if( n>0 && n<pPager->pageSize ){
2117 n = 1;
2118 }else{
2119 n /= pPager->pageSize;
2120 }
2121 if( pPager->state!=PAGER_UNLOCK ){
2122 pPager->dbSize = n;
danielk1977d92db532008-11-17 04:56:24 +00002123 pPager->dbSizeValid = 1;
danielk197715f411d2005-09-16 10:18:45 +00002124 }
drhed7c8552001-04-11 14:29:21 +00002125 }
danielk197715f411d2005-09-16 10:18:45 +00002126 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002127 n++;
2128 }
drhf8e632b2007-05-08 14:51:36 +00002129 if( n>pPager->mxPgno ){
2130 pPager->mxPgno = n;
2131 }
danielk1977ad0132d2008-06-07 08:58:22 +00002132 if( pnPage ){
2133 *pnPage = n;
2134 }
2135 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002136}
2137
2138/*
drhf7c57532003-04-25 13:22:51 +00002139** Forward declaration
2140*/
danielk197776572402004-06-25 02:38:54 +00002141static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002142
2143/*
danielk1977e180dd92007-04-05 17:15:52 +00002144** This routine is used to truncate the cache when a database
2145** is truncated. Drop from the cache all pages whose pgno is
2146** larger than pPager->dbSize and is unreferenced.
2147**
drhac69b052004-05-12 13:30:07 +00002148** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002149**
2150** Actually, at the point this routine is called, it would be
2151** an error to have a referenced page. But rather than delete
2152** that page and guarantee a subsequent segfault, it seems better
2153** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002154*/
danielk1977e180dd92007-04-05 17:15:52 +00002155static void pager_truncate_cache(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00002156 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
drhac69b052004-05-12 13:30:07 +00002157}
2158
drhf7c57532003-04-25 13:22:51 +00002159/*
danielk197717221812005-02-15 03:38:05 +00002160** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002161** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002162** false or until the lock succeeds.
2163**
2164** Return SQLITE_OK on success and an error code if we cannot obtain
2165** the lock.
2166*/
2167static int pager_wait_on_lock(Pager *pPager, int locktype){
2168 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002169
2170 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002171 assert( PAGER_SHARED==SHARED_LOCK );
2172 assert( PAGER_RESERVED==RESERVED_LOCK );
2173 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002174
2175 /* If the file is currently unlocked then the size must be unknown */
danielk1977d92db532008-11-17 04:56:24 +00002176 assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
drh1aa2d8b2007-01-03 15:34:29 +00002177
danielk197717221812005-02-15 03:38:05 +00002178 if( pPager->state>=locktype ){
2179 rc = SQLITE_OK;
2180 }else{
danielk197717221812005-02-15 03:38:05 +00002181 do {
drh054889e2005-11-30 03:20:31 +00002182 rc = sqlite3OsLock(pPager->fd, locktype);
danielk19771ceedd32008-11-19 10:22:33 +00002183 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
danielk197717221812005-02-15 03:38:05 +00002184 if( rc==SQLITE_OK ){
2185 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002186 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002187 }
2188 }
2189 return rc;
2190}
2191
2192/*
drhf7c57532003-04-25 13:22:51 +00002193** Truncate the file to the number of pages specified.
2194*/
danielk19773b8a05f2007-03-19 17:44:26 +00002195int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
danielk19778c0a7912008-08-20 14:49:23 +00002196 int rc = SQLITE_OK;
danielk1977b3175382008-10-17 18:51:52 +00002197 assert( pPager->state>=PAGER_SHARED );
danielk19778c0a7912008-08-20 14:49:23 +00002198
danielk1977ad0132d2008-06-07 08:58:22 +00002199 sqlite3PagerPagecount(pPager, 0);
danielk1977efaaf572006-01-16 11:29:19 +00002200 if( pPager->errCode ){
2201 rc = pPager->errCode;
danielk1977d92db532008-11-17 04:56:24 +00002202 }else if( nPage<pPager->dbSize ){
danielk1977b3175382008-10-17 18:51:52 +00002203 rc = syncJournal(pPager);
2204 if( rc==SQLITE_OK ){
2205 /* Get an exclusive lock on the database before truncating. */
2206 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2207 }
2208 if( rc==SQLITE_OK ){
2209 rc = pager_truncate(pPager, nPage);
danielk19778c0a7912008-08-20 14:49:23 +00002210 }
danielk1977369f27e2004-06-15 11:40:04 +00002211 }
danielk197717221812005-02-15 03:38:05 +00002212
drhf7c57532003-04-25 13:22:51 +00002213 return rc;
2214}
2215
2216/*
drhed7c8552001-04-11 14:29:21 +00002217** Shutdown the page cache. Free all memory and close all files.
2218**
2219** If a transaction was in progress when this routine is called, that
2220** transaction is rolled back. All outstanding pages are invalidated
2221** and their memory is freed. Any attempt to use a page associated
2222** with this page cache after this function returns will likely
2223** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002224**
2225** This function always succeeds. If a transaction is active an attempt
2226** is made to roll it back. If an error occurs during the rollback
2227** a hot journal may be left in the filesystem but no error is returned
2228** to the caller.
drhed7c8552001-04-11 14:29:21 +00002229*/
danielk19773b8a05f2007-03-19 17:44:26 +00002230int sqlite3PagerClose(Pager *pPager){
danielk197713f72992005-12-18 08:51:22 +00002231
drhbafda092007-01-03 23:36:22 +00002232 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002233 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002234 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002235 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002236 pager_reset(pPager);
danielk1977b3175382008-10-17 18:51:52 +00002237 if( !MEMDB ){
2238 pagerUnlockAndRollback(pPager);
2239 }
drhbafda092007-01-03 23:36:22 +00002240 enable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002241 sqlite3EndBenignMalloc();
drh4f0c5872007-03-26 22:05:01 +00002242 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002243 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002244 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002245 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002246 }
drhf5e7bb52008-02-18 14:47:33 +00002247 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977a1fa00d2008-08-27 15:16:33 +00002248 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
danielk1977e94ddc92005-03-21 03:53:38 +00002249 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002250 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002251 }
danielk1977b4b47412007-08-17 15:53:36 +00002252 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002253 /* Temp files are automatically deleted by the OS
2254 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002255 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002256 ** }
2257 */
danielk1977aca790a2005-01-13 11:07:52 +00002258
drhfacf0302008-06-17 15:12:00 +00002259 sqlite3PageFree(pPager->pTmpSpace);
danielk19778c0a7912008-08-20 14:49:23 +00002260 sqlite3PcacheClose(pPager->pPCache);
drh17435752007-08-16 04:30:38 +00002261 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002262 return SQLITE_OK;
2263}
2264
drh87cc3b32007-05-08 21:45:27 +00002265#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002266/*
drh5e00f6c2001-09-13 13:46:56 +00002267** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002268*/
danielk19773b8a05f2007-03-19 17:44:26 +00002269Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002270 return p->pgno;
2271}
drh87cc3b32007-05-08 21:45:27 +00002272#endif
drhed7c8552001-04-11 14:29:21 +00002273
2274/*
drhdf0b3b02001-06-23 11:36:20 +00002275** Increment the reference count for a page. The input pointer is
2276** a reference to the page data.
2277*/
danielk19773b8a05f2007-03-19 17:44:26 +00002278int sqlite3PagerRef(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002279 sqlite3PcacheRef(pPg);
drh8c42ca92001-06-22 19:15:00 +00002280 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002281}
2282
2283/*
drh34e79ce2004-02-08 06:05:46 +00002284** Sync the journal. In other words, make sure all the pages that have
2285** been written to the journal have actually reached the surface of the
2286** disk. It is not safe to modify the original database file until after
2287** the journal has been synced. If the original database is modified before
2288** the journal is synced and a power failure occurs, the unsynced journal
2289** data would be lost and we would be unable to completely rollback the
2290** database changes. Database corruption would occur.
2291**
2292** This routine also updates the nRec field in the header of the journal.
2293** (See comments on the pager_playback() routine for additional information.)
2294** If the sync mode is FULL, two syncs will occur. First the whole journal
2295** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002296**
drh34e79ce2004-02-08 06:05:46 +00002297** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002298** after a power failure, so no sync occurs.
2299**
2300** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2301** the database is stored, then OsSync() is never called on the journal
2302** file. In this case all that is required is to update the nRec field in
2303** the journal header.
drhfa86c412002-02-02 15:01:15 +00002304**
drh34e79ce2004-02-08 06:05:46 +00002305** This routine clears the needSync field of every page current held in
2306** memory.
drh50e5dad2001-09-15 00:57:28 +00002307*/
danielk197776572402004-06-25 02:38:54 +00002308static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002309 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002310
2311 /* Sync the journal before modifying the main database
2312 ** (assuming there is a journal and it needs to be synced.)
2313 */
danielk197776572402004-06-25 02:38:54 +00002314 if( pPager->needSync ){
danielk1977b3175382008-10-17 18:51:52 +00002315 assert( !pPager->tempFile );
2316 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
danielk19774cd2cd52007-08-23 14:48:23 +00002317 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002318 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002319
danielk19774cd2cd52007-08-23 14:48:23 +00002320 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002321 /* Write the nRec value into the journal file header. If in
2322 ** full-synchronous mode, sync the journal first. This ensures that
2323 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002324 ** it as a candidate for rollback.
2325 **
2326 ** This is not required if the persistent media supports the
2327 ** SAFE_APPEND property. Because in this case it is not possible
2328 ** for garbage data to be appended to the file, the nRec field
2329 ** is populated with 0xFFFFFFFF when the journal header is written
2330 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002331 */
danielk19774cd2cd52007-08-23 14:48:23 +00002332 i64 jrnlOff;
2333 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002334 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002335 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002336 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002337 if( rc!=0 ) return rc;
2338 }
danielk197713adf8a2004-06-03 16:08:41 +00002339
danielk197762079062007-08-15 17:08:46 +00002340 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2341 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2342 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002343 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002344 }
danielk19774cd2cd52007-08-23 14:48:23 +00002345 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2346 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2347 IOTRACE(("JSYNC %p\n", pPager))
2348 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2349 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2350 );
2351 if( rc!=0 ) return rc;
2352 }
drhdb48ee02003-01-16 13:42:43 +00002353 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002354 }
drh50e5dad2001-09-15 00:57:28 +00002355 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002356
2357 /* Erase the needSync flag from every page.
2358 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00002359 sqlite3PcacheClearSyncFlags(pPager->pPCache);
drh50e5dad2001-09-15 00:57:28 +00002360 }
drh03eb96a2002-11-10 23:32:56 +00002361
drh81a20f22001-10-12 17:30:04 +00002362 return rc;
drh50e5dad2001-09-15 00:57:28 +00002363}
2364
2365/*
drh2554f8b2003-01-22 01:26:44 +00002366** Given a list of pages (connected by the PgHdr.pDirty pointer) write
danielk1977a858aa22008-08-22 16:22:17 +00002367** every one of those pages out to the database file. No calls are made
2368** to the page-cache to mark the pages as clean. It is the responsibility
2369** of the caller to use PcacheCleanAll() or PcacheMakeClean() to mark
2370** the pages as clean.
drh2554f8b2003-01-22 01:26:44 +00002371*/
2372static int pager_write_pagelist(PgHdr *pList){
2373 Pager *pPager;
2374 int rc;
2375
2376 if( pList==0 ) return SQLITE_OK;
2377 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002378
2379 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2380 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002381 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002382 **
drha6abd042004-06-09 17:37:22 +00002383 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2384 ** through an intermediate state PENDING. A PENDING lock prevents new
2385 ** readers from attaching to the database but is unsufficient for us to
2386 ** write. The idea of a PENDING lock is to prevent new readers from
2387 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002388 **
drha6abd042004-06-09 17:37:22 +00002389 ** While the pager is in the RESERVED state, the original database file
2390 ** is unchanged and we can rollback without having to playback the
2391 ** journal into the original database file. Once we transition to
2392 ** EXCLUSIVE, it means the database file has been changed and any rollback
2393 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002394 */
drh684917c2004-10-05 02:41:42 +00002395 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002396 if( rc!=SQLITE_OK ){
2397 return rc;
2398 }
2399
drh2554f8b2003-01-22 01:26:44 +00002400 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002401
2402 /* If the file has not yet been opened, open it now. */
2403 if( !pPager->fd->pMethods ){
2404 assert(pPager->tempFile);
danielk197717b90b52008-06-06 11:11:25 +00002405 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002406 if( rc ) return rc;
2407 }
2408
danielk1977687566d2004-11-02 12:56:41 +00002409 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002410 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002411 ** make the file smaller (presumably by auto-vacuum code). Do not write
2412 ** any such pages to the file.
2413 */
danielk197733e32162008-08-23 18:53:08 +00002414 if( pList->pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
danielk197762079062007-08-15 17:08:46 +00002415 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002416 char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002417 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2418 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002419 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002420 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002421 PAGER_INCR(sqlite3_pager_writedb_count);
2422 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002423 if( pList->pgno==1 ){
2424 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2425 }
danielk1977687566d2004-11-02 12:56:41 +00002426 }
2427#ifndef NDEBUG
2428 else{
drh4f0c5872007-03-26 22:05:01 +00002429 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002430 }
2431#endif
drh2554f8b2003-01-22 01:26:44 +00002432 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002433#ifdef SQLITE_CHECK_PAGES
2434 pList->pageHash = pager_pagehash(pList);
2435#endif
drh2554f8b2003-01-22 01:26:44 +00002436 pList = pList->pDirty;
2437 }
danielk19778c0a7912008-08-20 14:49:23 +00002438
drh2554f8b2003-01-22 01:26:44 +00002439 return SQLITE_OK;
2440}
2441
2442/*
danielk19778c0a7912008-08-20 14:49:23 +00002443** This function is called by the pcache layer when it has reached some
2444** soft memory limit. The argument is a pointer to a purgeable Pager
2445** object. This function attempts to make a single dirty page that has no
2446** outstanding references (if one exists) clean so that it can be recycled
2447** by the pcache layer.
drh2554f8b2003-01-22 01:26:44 +00002448*/
danielk1977a858aa22008-08-22 16:22:17 +00002449static int pagerStress(void *p, PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002450 Pager *pPager = (Pager *)p;
danielk19778c0a7912008-08-20 14:49:23 +00002451 int rc = SQLITE_OK;
drh8f2e9a12008-02-26 14:46:04 +00002452
danielk19778c200142008-08-25 07:12:28 +00002453 if( pPager->doNotSync ){
2454 return SQLITE_OK;
2455 }
2456
danielk1977a858aa22008-08-22 16:22:17 +00002457 assert( pPg->flags&PGHDR_DIRTY );
2458 if( pPager->errCode==SQLITE_OK ){
2459 if( pPg->flags&PGHDR_NEED_SYNC ){
2460 rc = syncJournal(pPager);
2461 if( rc==SQLITE_OK && pPager->fullSync &&
danielk1977b3175382008-10-17 18:51:52 +00002462 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
danielk1977a858aa22008-08-22 16:22:17 +00002463 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
2464 ){
2465 pPager->nRec = 0;
2466 rc = writeJournalHdr(pPager);
danielk19778c0a7912008-08-20 14:49:23 +00002467 }
danielk19778c0a7912008-08-20 14:49:23 +00002468 }
danielk1977a858aa22008-08-22 16:22:17 +00002469 if( rc==SQLITE_OK ){
2470 pPg->pDirty = 0;
2471 rc = pager_write_pagelist(pPg);
2472 }
2473 if( rc!=SQLITE_OK ){
2474 pager_error(pPager, rc);
2475 }
2476 }
2477
2478 if( rc==SQLITE_OK ){
2479 sqlite3PcacheMakeClean(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00002480 }
2481 return rc;
drh2554f8b2003-01-22 01:26:44 +00002482}
2483
danielk19778c0a7912008-08-20 14:49:23 +00002484
drh2554f8b2003-01-22 01:26:44 +00002485/*
drh19db9352008-03-27 22:42:51 +00002486** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00002487** A hot journal is one that needs to be played back.
2488**
2489** If the current size of the database file is 0 but a journal file
2490** exists, that is probably an old journal left over from a prior
2491** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00002492**
2493** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00002494**
2495** This routine does not open the journal file to examine its
2496** content. Hence, the journal might contain the name of a master
2497** journal file that has been deleted, and hence not be hot. Or
2498** the header of the journal might be zeroed out. This routine
2499** does not discover these cases of a non-hot journal - if the
2500** journal file exists and is not empty this routine assumes it
2501** is hot. The pager_playback() routine will discover that the
2502** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00002503*/
danielk1977d300b8a2008-08-01 10:50:23 +00002504static int hasHotJournal(Pager *pPager, int *pExists){
danielk1977b4b47412007-08-17 15:53:36 +00002505 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977d300b8a2008-08-01 10:50:23 +00002506 int rc = SQLITE_OK;
drh0a846f92008-08-25 17:23:29 +00002507 int exists;
2508 int locked;
2509 assert( pPager!=0 );
2510 assert( pPager->useJournal );
2511 assert( pPager->fd->pMethods );
danielk1977d300b8a2008-08-01 10:50:23 +00002512 *pExists = 0;
drh0a846f92008-08-25 17:23:29 +00002513 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
2514 if( rc==SQLITE_OK && exists ){
2515 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
2516 }
2517 if( rc==SQLITE_OK && exists && !locked ){
2518 int nPage;
2519 rc = sqlite3PagerPagecount(pPager, &nPage);
2520 if( rc==SQLITE_OK ){
2521 if( nPage==0 ){
2522 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2523 }else{
2524 *pExists = 1;
danielk1977861f7452008-06-05 11:39:11 +00002525 }
2526 }
drhbb5f18d2007-04-06 18:23:17 +00002527 }
danielk1977d300b8a2008-08-01 10:50:23 +00002528 return rc;
drh165ffe92005-03-15 17:09:30 +00002529}
2530
2531/*
danielk1977e180dd92007-04-05 17:15:52 +00002532** Read the content of page pPg out of the database file.
2533*/
2534static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2535 int rc;
danielk197762079062007-08-15 17:08:46 +00002536 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002537 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002538 assert(pPager->fd->pMethods||pPager->tempFile);
2539 if( !pPager->fd->pMethods ){
2540 return SQLITE_IOERR_SHORT_READ;
2541 }
danielk197762079062007-08-15 17:08:46 +00002542 offset = (pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002543 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002544 PAGER_INCR(sqlite3_pager_readdb_count);
2545 PAGER_INCR(pPager->nRead);
2546 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002547 if( pgno==1 ){
danielk19778c0a7912008-08-20 14:49:23 +00002548 memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24],
drh86a88112007-04-16 15:02:19 +00002549 sizeof(pPager->dbFileVers));
2550 }
danielk197730849522008-08-21 04:35:18 +00002551 CODEC1(pPager, pPg->pData, pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00002552 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
2553 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00002554 return rc;
2555}
2556
2557
2558/*
danielk1977e277be02007-03-23 18:12:06 +00002559** This function is called to obtain the shared lock required before
2560** data may be read from the pager cache. If the shared lock has already
2561** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002562**
2563** Immediately after obtaining the shared lock (if required), this function
2564** checks for a hot-journal file. If one is found, an emergency rollback
2565** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002566*/
2567static int pagerSharedLock(Pager *pPager){
2568 int rc = SQLITE_OK;
danielk1977d300b8a2008-08-01 10:50:23 +00002569 int isErrorReset = 0;
danielk1977e277be02007-03-23 18:12:06 +00002570
danielk1977ae72d982007-10-03 08:46:44 +00002571 /* If this database is opened for exclusive access, has no outstanding
2572 ** page references and is in an error-state, now is the chance to clear
2573 ** the error. Discard the contents of the pager-cache and treat any
2574 ** open journal file as a hot-journal.
2575 */
danielk19778c0a7912008-08-20 14:49:23 +00002576 if( !MEMDB && pPager->exclusiveMode
2577 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
2578 ){
danielk1977ae72d982007-10-03 08:46:44 +00002579 if( pPager->journalOpen ){
danielk1977d300b8a2008-08-01 10:50:23 +00002580 isErrorReset = 1;
danielk1977ae72d982007-10-03 08:46:44 +00002581 }
danielk1977ae72d982007-10-03 08:46:44 +00002582 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00002583 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00002584 }
2585
2586 /* If the pager is still in an error state, do not proceed. The error
2587 ** state will be cleared at some point in the future when all page
2588 ** references are dropped and the cache can be discarded.
2589 */
2590 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
2591 return pPager->errCode;
2592 }
2593
danielk1977d300b8a2008-08-01 10:50:23 +00002594 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
danielk1977b4b47412007-08-17 15:53:36 +00002595 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977b3175382008-10-17 18:51:52 +00002596 int isHotJournal;
shane049fc212008-10-22 16:26:47 +00002597 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00002598 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
2599 if( !pPager->noReadlock ){
2600 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2601 if( rc!=SQLITE_OK ){
2602 assert( pPager->state==PAGER_UNLOCK );
2603 return pager_error(pPager, rc);
danielk1977e277be02007-03-23 18:12:06 +00002604 }
danielk1977b3175382008-10-17 18:51:52 +00002605 assert( pPager->state>=SHARED_LOCK );
2606 }
2607
2608 /* If a journal file exists, and there is no RESERVED lock on the
2609 ** database file, then it either needs to be played back or deleted.
2610 */
2611 if( !isErrorReset ){
2612 rc = hasHotJournal(pPager, &isHotJournal);
2613 if( rc!=SQLITE_OK ){
2614 goto failed;
2615 }
2616 }
2617 if( isErrorReset || isHotJournal ){
2618 /* Get an EXCLUSIVE lock on the database file. At this point it is
2619 ** important that a RESERVED lock is not obtained on the way to the
2620 ** EXCLUSIVE lock. If it were, another process might open the
2621 ** database file, detect the RESERVED lock, and conclude that the
2622 ** database is safe to read while this process is still rolling it
2623 ** back.
2624 **
2625 ** Because the intermediate RESERVED lock is not requested, the
2626 ** second process will get to this point in the code and fail to
2627 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00002628 */
danielk1977b3175382008-10-17 18:51:52 +00002629 if( pPager->state<EXCLUSIVE_LOCK ){
2630 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
danielk1977e277be02007-03-23 18:12:06 +00002631 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002632 rc = pager_error(pPager, rc);
2633 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002634 }
danielk1977b3175382008-10-17 18:51:52 +00002635 pPager->state = PAGER_EXCLUSIVE;
2636 }
2637
2638 /* Open the journal for read/write access. This is because in
2639 ** exclusive-access mode the file descriptor will be kept open and
2640 ** possibly used for a transaction later on. On some systems, the
2641 ** OsTruncate() call used in exclusive-access mode also requires
2642 ** a read/write file handle.
2643 */
2644 if( !isErrorReset && pPager->journalOpen==0 ){
2645 int res;
2646 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
2647 if( rc==SQLITE_OK ){
2648 if( res ){
2649 int fout = 0;
2650 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
2651 assert( !pPager->tempFile );
2652 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
2653 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk1977281d8bd2008-10-29 07:01:56 +00002654 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
2655 rc = SQLITE_CANTOPEN;
danielk1977b3175382008-10-17 18:51:52 +00002656 sqlite3OsClose(pPager->jfd);
2657 }
2658 }else{
2659 /* If the journal does not exist, that means some other process
2660 ** has already rolled it back */
2661 rc = SQLITE_BUSY;
2662 }
2663 }
2664 }
2665 if( rc!=SQLITE_OK ){
danielk1977b3175382008-10-17 18:51:52 +00002666 goto failed;
2667 }
2668 pPager->journalOpen = 1;
2669 pPager->journalStarted = 0;
2670 pPager->journalOff = 0;
2671 pPager->setMaster = 0;
2672 pPager->journalHdr = 0;
2673
2674 /* Playback and delete the journal. Drop the database write
2675 ** lock and reacquire the read lock.
2676 */
2677 rc = pager_playback(pPager, 1);
2678 if( rc!=SQLITE_OK ){
2679 rc = pager_error(pPager, rc);
2680 goto failed;
2681 }
2682 assert(pPager->state==PAGER_SHARED ||
2683 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
2684 );
2685 }
2686
2687 if( sqlite3PcachePagecount(pPager->pPCache)>0 ){
2688 /* The shared-lock has just been acquired on the database file
2689 ** and there are already pages in the cache (from a previous
2690 ** read or write transaction). Check to see if the database
2691 ** has been modified. If the database has changed, flush the
2692 ** cache.
2693 **
2694 ** Database changes is detected by looking at 15 bytes beginning
2695 ** at offset 24 into the file. The first 4 of these 16 bytes are
2696 ** a 32-bit counter that is incremented with each change. The
2697 ** other bytes change randomly with each file change when
2698 ** a codec is in use.
2699 **
2700 ** There is a vanishingly small chance that a change will not be
2701 ** detected. The chance of an undetected change is so small that
2702 ** it can be neglected.
2703 */
2704 char dbFileVers[sizeof(pPager->dbFileVers)];
2705 sqlite3PagerPagecount(pPager, 0);
2706
2707 if( pPager->errCode ){
2708 rc = pPager->errCode;
2709 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002710 }
2711
danielk1977d92db532008-11-17 04:56:24 +00002712 assert( pPager->dbSizeValid );
danielk1977b3175382008-10-17 18:51:52 +00002713 if( pPager->dbSize>0 ){
2714 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
2715 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
2716 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002717 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002718 }
danielk1977b3175382008-10-17 18:51:52 +00002719 }else{
2720 memset(dbFileVers, 0, sizeof(dbFileVers));
2721 }
danielk1977e277be02007-03-23 18:12:06 +00002722
danielk1977b3175382008-10-17 18:51:52 +00002723 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
2724 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002725 }
2726 }
danielk1977c5859712007-03-26 12:26:27 +00002727 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
2728 if( pPager->state==PAGER_UNLOCK ){
2729 pPager->state = PAGER_SHARED;
2730 }
danielk1977e277be02007-03-23 18:12:06 +00002731 }
2732
danielk197752b472a2008-05-05 16:23:55 +00002733 failed:
2734 if( rc!=SQLITE_OK ){
2735 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
2736 pager_unlock(pPager);
2737 }
danielk1977e277be02007-03-23 18:12:06 +00002738 return rc;
2739}
2740
2741/*
drhd33d5a82007-04-26 12:11:28 +00002742** Make sure we have the content for a page. If the page was
2743** previously acquired with noContent==1, then the content was
2744** just initialized to zeros instead of being read from disk.
2745** But now we need the real data off of disk. So make sure we
2746** have it. Read it in if we do not have it already.
2747*/
2748static int pager_get_content(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002749 if( pPg->flags&PGHDR_NEED_READ ){
drhd33d5a82007-04-26 12:11:28 +00002750 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
2751 if( rc==SQLITE_OK ){
danielk19778c0a7912008-08-20 14:49:23 +00002752 pPg->flags &= ~PGHDR_NEED_READ;
drhd33d5a82007-04-26 12:11:28 +00002753 }else{
2754 return rc;
2755 }
2756 }
2757 return SQLITE_OK;
2758}
2759
2760/*
danielk19778c0a7912008-08-20 14:49:23 +00002761** If the reference count has reached zero, and the pager is not in the
2762** middle of a write transaction or opened in exclusive mode, unlock it.
2763*/
2764static void pagerUnlockIfUnused(Pager *pPager){
2765 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
2766 && (!pPager->exclusiveMode || pPager->journalOff>0)
2767 ){
2768 pagerUnlockAndRollback(pPager);
2769 }
2770}
2771
2772/*
2773** Drop a page from the cache using sqlite3PcacheDrop().
2774**
2775** If this means there are now no pages with references to them, a rollback
2776** occurs and the lock on the database is removed.
2777*/
2778static void pagerDropPage(DbPage *pPg){
2779 Pager *pPager = pPg->pPager;
2780 sqlite3PcacheDrop(pPg);
2781 pagerUnlockIfUnused(pPager);
2782}
2783
2784/*
drhd9b02572001-04-15 00:37:09 +00002785** Acquire a page.
2786**
drh58a11682001-11-10 13:51:08 +00002787** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00002788** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00002789**
drhd33d5a82007-04-26 12:11:28 +00002790** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00002791** file is smaller than the requested page, then no actual disk
2792** read occurs and the memory image of the page is initialized to
2793** all zeros. The extra data appended to a page is always initialized
2794** to zeros the first time a page is loaded into memory.
2795**
drhd9b02572001-04-15 00:37:09 +00002796** The acquisition might fail for several reasons. In all cases,
2797** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00002798**
drhd33d5a82007-04-26 12:11:28 +00002799** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00002800** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00002801** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00002802** just returns 0. This routine acquires a read-lock the first time it
2803** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00002804** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00002805** or journal files.
drh0787db62007-03-04 13:15:27 +00002806**
drh538f5702007-04-13 02:14:30 +00002807** If noContent is false, the page contents are actually read from disk.
2808** If noContent is true, it means that we do not care about the contents
2809** of the page at this time, so do not do a disk read. Just fill in the
2810** page content with zeros. But mark the fact that we have not read the
2811** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00002812** sqlite3PagerWrite() is called on this page or if this routine is
2813** called again with noContent==0, that means that the content is needed
2814** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00002815*/
danielk197765e0ff32008-09-19 09:14:43 +00002816int sqlite3PagerAcquire(
drh538f5702007-04-13 02:14:30 +00002817 Pager *pPager, /* The pager open on the database file */
2818 Pgno pgno, /* Page number to fetch */
2819 DbPage **ppPage, /* Write a pointer to the page here */
2820 int noContent /* Do not bother reading content from disk if true */
2821){
danielk19778c0a7912008-08-20 14:49:23 +00002822 PgHdr *pPg = 0;
drh165ffe92005-03-15 17:09:30 +00002823 int rc;
drhed7c8552001-04-11 14:29:21 +00002824
danielk19778c0a7912008-08-20 14:49:23 +00002825 assert( pPager->state==PAGER_UNLOCK
2826 || sqlite3PcacheRefCount(pPager->pPCache)>0
danielk1977b3175382008-10-17 18:51:52 +00002827 || pgno==1
danielk19778c0a7912008-08-20 14:49:23 +00002828 );
danielk1977e277be02007-03-23 18:12:06 +00002829
danielk197726836652005-01-17 01:33:13 +00002830 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
2831 ** number greater than this, or zero, is requested.
2832 */
drh267cb322005-09-16 17:16:52 +00002833 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00002834 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00002835 }
2836
drhd9b02572001-04-15 00:37:09 +00002837 /* Make sure we have not hit any critical errors.
2838 */
drh836faa42003-01-11 13:30:57 +00002839 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00002840 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00002841
danielk197713adf8a2004-06-03 16:08:41 +00002842 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00002843 ** on the database file. pagerSharedLock() is a no-op if
2844 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00002845 */
danielk1977e277be02007-03-23 18:12:06 +00002846 rc = pagerSharedLock(pPager);
2847 if( rc!=SQLITE_OK ){
2848 return rc;
drhed7c8552001-04-11 14:29:21 +00002849 }
danielk1977e277be02007-03-23 18:12:06 +00002850 assert( pPager->state!=PAGER_UNLOCK );
2851
danielk19778c0a7912008-08-20 14:49:23 +00002852 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
2853 if( rc!=SQLITE_OK ){
2854 return rc;
2855 }
danielk19778c0a7912008-08-20 14:49:23 +00002856 if( pPg->pPager==0 ){
2857 /* The pager cache has created a new page. Its content needs to
2858 ** be initialized.
2859 */
danielk1977979f38e2007-03-27 16:19:51 +00002860 int nMax;
drh538f5702007-04-13 02:14:30 +00002861 PAGER_INCR(pPager->nMiss);
danielk19778c0a7912008-08-20 14:49:23 +00002862 pPg->pPager = pPager;
danielk19778c0a7912008-08-20 14:49:23 +00002863 memset(pPg->pExtra, 0, pPager->nExtra);
danielk197724168722007-04-02 05:07:47 +00002864
danielk1977ad0132d2008-06-07 08:58:22 +00002865 rc = sqlite3PagerPagecount(pPager, &nMax);
2866 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002867 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00002868 return rc;
2869 }
danielk197775bab7d2006-01-23 13:09:45 +00002870
danielk1977a1fa00d2008-08-27 15:16:33 +00002871 if( nMax<(int)pgno || MEMDB || noContent ){
drhf8e632b2007-05-08 14:51:36 +00002872 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00002873 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00002874 return SQLITE_FULL;
2875 }
danielk19778c0a7912008-08-20 14:49:23 +00002876 memset(pPg->pData, 0, pPager->pageSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00002877 if( noContent ){
danielk19778c0a7912008-08-20 14:49:23 +00002878 pPg->flags |= PGHDR_NEED_READ;
2879 }
drh538f5702007-04-13 02:14:30 +00002880 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00002881 }else{
danielk1977e180dd92007-04-05 17:15:52 +00002882 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00002883 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
danielk19778c0a7912008-08-20 14:49:23 +00002884 /* sqlite3PagerUnref(pPg); */
2885 pagerDropPage(pPg);
drh551b7732006-11-06 21:20:25 +00002886 return rc;
drh81a20f22001-10-12 17:30:04 +00002887 }
drh306dc212001-05-21 13:45:10 +00002888 }
danielk19773c407372005-02-15 02:54:14 +00002889#ifdef SQLITE_CHECK_PAGES
2890 pPg->pageHash = pager_pagehash(pPg);
2891#endif
drhed7c8552001-04-11 14:29:21 +00002892 }else{
drhd9b02572001-04-15 00:37:09 +00002893 /* The requested page is in the page cache. */
danielk19778c0a7912008-08-20 14:49:23 +00002894 assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00002895 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00002896 if( !noContent ){
2897 rc = pager_get_content(pPg);
2898 if( rc ){
danielk19778c0a7912008-08-20 14:49:23 +00002899 sqlite3PagerUnref(pPg);
drhd33d5a82007-04-26 12:11:28 +00002900 return rc;
2901 }
2902 }
drhed7c8552001-04-11 14:29:21 +00002903 }
danielk19778c0a7912008-08-20 14:49:23 +00002904
danielk19773b8a05f2007-03-19 17:44:26 +00002905 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00002906 return SQLITE_OK;
2907}
danielk19778c0a7912008-08-20 14:49:23 +00002908
drhed7c8552001-04-11 14:29:21 +00002909/*
drh7e3b0a02001-04-28 16:52:40 +00002910** Acquire a page if it is already in the in-memory cache. Do
2911** not read the page from disk. Return a pointer to the page,
2912** or 0 if the page is not in cache.
2913**
danielk19773b8a05f2007-03-19 17:44:26 +00002914** See also sqlite3PagerGet(). The difference between this routine
2915** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00002916** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00002917** returns NULL if the page is not in cache or if a disk I/O error
2918** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00002919*/
danielk19773b8a05f2007-03-19 17:44:26 +00002920DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00002921 PgHdr *pPg = 0;
drh836faa42003-01-11 13:30:57 +00002922 assert( pPager!=0 );
2923 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00002924
danielk19778c0a7912008-08-20 14:49:23 +00002925 if( (pPager->state!=PAGER_UNLOCK)
2926 && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL)
2927 ){
2928 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
danielk1977e277be02007-03-23 18:12:06 +00002929 }
danielk19778c0a7912008-08-20 14:49:23 +00002930
danielk19773b8a05f2007-03-19 17:44:26 +00002931 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00002932}
2933
2934/*
drhed7c8552001-04-11 14:29:21 +00002935** Release a page.
2936**
2937** If the number of references to the page drop to zero, then the
2938** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00002939** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00002940** removed.
2941*/
danielk19773b8a05f2007-03-19 17:44:26 +00002942int sqlite3PagerUnref(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002943 if( pPg ){
2944 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00002945 sqlite3PcacheRelease(pPg);
2946 pagerUnlockIfUnused(pPager);
drhed7c8552001-04-11 14:29:21 +00002947 }
drhd9b02572001-04-15 00:37:09 +00002948 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002949}
2950
2951/*
drha6abd042004-06-09 17:37:22 +00002952** Create a journal file for pPager. There should already be a RESERVED
2953** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00002954**
2955** Return SQLITE_OK if everything. Return an error code and release the
2956** write lock if anything goes wrong.
2957*/
2958static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00002959 sqlite3_vfs *pVfs = pPager->pVfs;
2960 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
2961
drhda47d772002-12-02 04:25:19 +00002962 int rc;
drha6abd042004-06-09 17:37:22 +00002963 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00002964 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00002965 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00002966 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00002967 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00002968 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00002969 rc = SQLITE_NOMEM;
2970 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00002971 }
danielk1977b4b47412007-08-17 15:53:36 +00002972
drhfdc40e92008-04-17 20:59:37 +00002973 if( pPager->journalOpen==0 ){
2974 if( pPager->tempFile ){
2975 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
2976 }else{
2977 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00002978 }
danielk1977b3175382008-10-17 18:51:52 +00002979 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
2980 sqlite3MemJournalOpen(pPager->jfd);
2981 rc = SQLITE_OK;
2982 }else{
drhfdc40e92008-04-17 20:59:37 +00002983#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977b3175382008-10-17 18:51:52 +00002984 rc = sqlite3JournalOpen(
2985 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
2986 );
drhfdc40e92008-04-17 20:59:37 +00002987#else
danielk1977b3175382008-10-17 18:51:52 +00002988 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
drhfdc40e92008-04-17 20:59:37 +00002989#endif
danielk1977b3175382008-10-17 18:51:52 +00002990 }
drhfdc40e92008-04-17 20:59:37 +00002991 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
2992 pPager->journalOff = 0;
2993 pPager->setMaster = 0;
2994 pPager->journalHdr = 0;
2995 if( rc!=SQLITE_OK ){
2996 if( rc==SQLITE_NOMEM ){
2997 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2998 }
2999 goto failed_to_open_journal;
3000 }
drhda47d772002-12-02 04:25:19 +00003001 }
3002 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003003 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003004 pPager->needSync = 0;
drh968af522003-02-11 14:55:40 +00003005 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003006 if( pPager->errCode ){
3007 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003008 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003009 }
drhda47d772002-12-02 04:25:19 +00003010 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003011
danielk197776572402004-06-25 02:38:54 +00003012 rc = writeJournalHdr(pPager);
3013
drhac69b052004-05-12 13:30:07 +00003014 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003015 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003016 }
danielk1977ae72d982007-10-03 08:46:44 +00003017 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003018 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003019 if( rc==SQLITE_OK ){
3020 rc = SQLITE_FULL;
3021 }
3022 }
drh9c105bb2004-10-02 20:38:28 +00003023 return rc;
3024
3025failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003026 sqlite3BitvecDestroy(pPager->pInJournal);
3027 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003028 return rc;
drhda47d772002-12-02 04:25:19 +00003029}
3030
3031/*
drh4b845d72002-03-05 12:41:19 +00003032** Acquire a write-lock on the database. The lock is removed when
3033** the any of the following happen:
3034**
drh80e35f42007-03-30 14:06:34 +00003035** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003036** * sqlite3PagerRollback() is called.
3037** * sqlite3PagerClose() is called.
3038** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003039**
danielk197713adf8a2004-06-03 16:08:41 +00003040** The first parameter to this routine is a pointer to any open page of the
3041** database file. Nothing changes about the page - it is used merely to
3042** acquire a pointer to the Pager structure and as proof that there is
3043** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003044**
danielk197713adf8a2004-06-03 16:08:41 +00003045** The second parameter indicates how much space in bytes to reserve for a
3046** master journal file-name at the start of the journal when it is created.
3047**
3048** A journal file is opened if this is not a temporary file. For temporary
3049** files, the opening of the journal file is deferred until there is an
3050** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003051**
drha6abd042004-06-09 17:37:22 +00003052** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003053**
3054** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3055** immediately instead of waiting until we try to flush the cache. The
3056** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003057*/
danielk19773b8a05f2007-03-19 17:44:26 +00003058int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003059 Pager *pPager = pPg->pPager;
3060 int rc = SQLITE_OK;
3061 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003062 assert( pPager->state!=PAGER_UNLOCK );
3063 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003064 assert( pPager->pInJournal==0 );
danielk1977b3175382008-10-17 18:51:52 +00003065 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00003066 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
3067 if( rc==SQLITE_OK ){
3068 pPager->state = PAGER_RESERVED;
3069 if( exFlag ){
3070 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh684917c2004-10-05 02:41:42 +00003071 }
danielk1977b3175382008-10-17 18:51:52 +00003072 }
3073 if( rc!=SQLITE_OK ){
3074 return rc;
3075 }
3076 pPager->dirtyCache = 0;
3077 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
3078 if( pPager->useJournal && !pPager->tempFile
3079 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
3080 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00003081 }
danielk1977334cdb62007-03-26 08:05:12 +00003082 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00003083 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00003084 ** time a (read or write) transaction was successfully concluded
3085 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00003086 ** kept open and either was truncated to 0 bytes or its header was
3087 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00003088 */
3089 assert( pPager->nRec==0 );
3090 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003091 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003092 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003093 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhf5e7bb52008-02-18 14:47:33 +00003094 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003095 rc = SQLITE_NOMEM;
3096 }else{
drh369339d2007-03-30 16:01:55 +00003097 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003098 rc = writeJournalHdr(pPager);
3099 }
drh4b845d72002-03-05 12:41:19 +00003100 }
danielk1977334cdb62007-03-26 08:05:12 +00003101 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003102 return rc;
3103}
3104
3105/*
drhed7c8552001-04-11 14:29:21 +00003106** Mark a data page as writeable. The page is written into the journal
3107** if it is not there already. This routine must be called before making
3108** changes to a page.
3109**
3110** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003111** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003112** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003113** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003114** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003115**
3116** If the journal file could not be written because the disk is full,
3117** then this routine returns SQLITE_FULL and does an immediate rollback.
3118** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003119** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003120** reset.
drhed7c8552001-04-11 14:29:21 +00003121*/
danielk19773b8a05f2007-03-19 17:44:26 +00003122static int pager_write(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003123 void *pData = pPg->pData;
drh69688d52001-04-14 16:38:23 +00003124 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003125 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003126
drh6446c4d2001-12-15 14:22:18 +00003127 /* Check for errors
3128 */
danielk1977efaaf572006-01-16 11:29:19 +00003129 if( pPager->errCode ){
3130 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003131 }
drh5e00f6c2001-09-13 13:46:56 +00003132 if( pPager->readOnly ){
3133 return SQLITE_PERM;
3134 }
drh6446c4d2001-12-15 14:22:18 +00003135
danielk197776572402004-06-25 02:38:54 +00003136 assert( !pPager->setMaster );
3137
danielk19773c407372005-02-15 02:54:14 +00003138 CHECK_PAGE(pPg);
3139
drh538f5702007-04-13 02:14:30 +00003140 /* If this page was previously acquired with noContent==1, that means
3141 ** we didn't really read in the content of the page. This can happen
3142 ** (for example) when the page is being moved to the freelist. But
3143 ** now we are (perhaps) moving the page off of the freelist for
3144 ** reuse and we need to know its original content so that content
3145 ** can be stored in the rollback journal. So do the read at this
3146 ** time.
3147 */
drhd33d5a82007-04-26 12:11:28 +00003148 rc = pager_get_content(pPg);
3149 if( rc ){
3150 return rc;
drh538f5702007-04-13 02:14:30 +00003151 }
3152
drh6446c4d2001-12-15 14:22:18 +00003153 /* Mark the page as dirty. If the page has already been written
3154 ** to the journal then we can return right away.
3155 */
drhc047b9f2008-11-21 03:23:43 +00003156 sqlite3PcacheMakeDirty(pPg);
danielk1977bc2ca9e2008-11-13 14:28:28 +00003157 if( pageInJournal(pPg) && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00003158 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003159 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003160 }else{
drh6446c4d2001-12-15 14:22:18 +00003161
danielk1977a0bf2652004-11-04 14:30:04 +00003162 /* If we get this far, it means that the page needs to be
3163 ** written to the transaction journal or the ckeckpoint journal
3164 ** or both.
3165 **
3166 ** First check to see that the transaction journal exists and
3167 ** create it if it does not.
3168 */
3169 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003170 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003171 if( rc!=SQLITE_OK ){
3172 return rc;
3173 }
3174 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00003175 if( !pPager->journalOpen && pPager->useJournal
3176 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00003177 rc = pager_open_journal(pPager);
3178 if( rc!=SQLITE_OK ) return rc;
3179 }
danielk1977a0bf2652004-11-04 14:30:04 +00003180 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003181 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003182
3183 /* The transaction journal now exists and we have a RESERVED or an
3184 ** EXCLUSIVE lock on the main database file. Write the current page to
3185 ** the transaction journal if it is not there already.
3186 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00003187 if( !pageInJournal(pPg) && pPager->journalOpen ){
danielk1977d92db532008-11-17 04:56:24 +00003188 if( pPg->pgno<=pPager->origDbSize ){
danielk1977b3175382008-10-17 18:51:52 +00003189 u32 cksum;
3190 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00003191
danielk1977b3175382008-10-17 18:51:52 +00003192 /* We should never write to the journal file the page that
3193 ** contains the database locks. The following assert verifies
3194 ** that we do not. */
3195 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
3196 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
3197 cksum = pager_cksum(pPager, (u8*)pData2);
3198 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
3199 if( rc==SQLITE_OK ){
3200 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
3201 pPager->journalOff + 4);
3202 pPager->journalOff += pPager->pageSize+4;
3203 }
3204 if( rc==SQLITE_OK ){
3205 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
3206 pPager->journalOff += 4;
3207 }
3208 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
3209 pPager->journalOff, pPager->pageSize));
3210 PAGER_INCR(sqlite3_pager_writej_count);
3211 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
3212 PAGERID(pPager), pPg->pgno,
3213 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00003214
danielk1977b3175382008-10-17 18:51:52 +00003215 /* An error has occured writing to the journal file. The
3216 ** transaction will be rolled back by the layer above.
3217 */
3218 if( rc!=SQLITE_OK ){
3219 return rc;
3220 }
danielk197707cb5602006-01-20 10:55:05 +00003221
danielk1977b3175382008-10-17 18:51:52 +00003222 pPager->nRec++;
3223 assert( pPager->pInJournal!=0 );
3224 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3225 if( !pPager->noSync ){
3226 pPg->flags |= PGHDR_NEED_SYNC;
3227 }
3228 if( pPager->stmtInUse ){
3229 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhac69b052004-05-12 13:30:07 +00003230 }
danielk197713adf8a2004-06-03 16:08:41 +00003231 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003232 if( !pPager->journalStarted && !pPager->noSync ){
3233 pPg->flags |= PGHDR_NEED_SYNC;
3234 }
drh4f0c5872007-03-26 22:05:01 +00003235 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk19778c0a7912008-08-20 14:49:23 +00003236 PAGERID(pPager), pPg->pgno,
3237 ((pPg->flags&PGHDR_NEED_SYNC)?1:0));
danielk1977a0bf2652004-11-04 14:30:04 +00003238 }
danielk19778c0a7912008-08-20 14:49:23 +00003239 if( pPg->flags&PGHDR_NEED_SYNC ){
danielk1977a0bf2652004-11-04 14:30:04 +00003240 pPager->needSync = 1;
3241 }
danielk1977a0bf2652004-11-04 14:30:04 +00003242 }
3243
3244 /* If the statement journal is open and the page is not in it,
3245 ** then write the current page to the statement journal. Note that
3246 ** the statement journal format differs from the standard journal format
3247 ** in that it omits the checksums and the header.
3248 */
danielk1977f35843b2007-04-07 15:03:17 +00003249 if( pPager->stmtInUse
3250 && !pageInStatement(pPg)
danielk1977d92db532008-11-17 04:56:24 +00003251 && pPg->pgno<=pPager->stmtSize
danielk1977f35843b2007-04-07 15:03:17 +00003252 ){
danielk1977b3175382008-10-17 18:51:52 +00003253 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
3254 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
danielk1977d92db532008-11-17 04:56:24 +00003255 assert( pageInJournal(pPg) || pPg->pgno>pPager->origDbSize );
danielk1977b3175382008-10-17 18:51:52 +00003256 rc = write32bits(pPager->stfd, offset, pPg->pgno);
3257 if( rc==SQLITE_OK ){
3258 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
drhdb48ee02003-01-16 13:42:43 +00003259 }
danielk1977b3175382008-10-17 18:51:52 +00003260 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
3261 if( rc!=SQLITE_OK ){
3262 return rc;
3263 }
3264 pPager->stmtNRec++;
3265 assert( pPager->pInStmt!=0 );
3266 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhd9b02572001-04-15 00:37:09 +00003267 }
drhfa86c412002-02-02 15:01:15 +00003268 }
3269
3270 /* Update the database size and return.
3271 */
drh1aa2d8b2007-01-03 15:34:29 +00003272 assert( pPager->state>=PAGER_SHARED );
danielk1977d92db532008-11-17 04:56:24 +00003273 if( pPager->dbSize<pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003274 pPager->dbSize = pPg->pgno;
danielk1977d92db532008-11-17 04:56:24 +00003275 if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){
drh1f595712004-06-15 01:40:29 +00003276 pPager->dbSize++;
3277 }
drh306dc212001-05-21 13:45:10 +00003278 }
drh69688d52001-04-14 16:38:23 +00003279 return rc;
drhed7c8552001-04-11 14:29:21 +00003280}
3281
3282/*
danielk19774099f6e2007-03-19 11:25:20 +00003283** This function is used to mark a data-page as writable. It uses
3284** pager_write() to open a journal file (if it is not already open)
3285** and write the page *pData to the journal.
3286**
3287** The difference between this function and pager_write() is that this
3288** function also deals with the special case where 2 or more pages
3289** fit on a single disk sector. In this case all co-resident pages
3290** must have been written to the journal file before returning.
3291*/
danielk19773b8a05f2007-03-19 17:44:26 +00003292int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003293 int rc = SQLITE_OK;
3294
danielk19773b8a05f2007-03-19 17:44:26 +00003295 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003296 Pager *pPager = pPg->pPager;
3297 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3298
danielk1977b3175382008-10-17 18:51:52 +00003299 if( nPagePerSector>1 ){
danielk19774099f6e2007-03-19 11:25:20 +00003300 Pgno nPageCount; /* Total number of pages in database file */
3301 Pgno pg1; /* First page of the sector pPg is located on. */
3302 int nPage; /* Number of pages starting at pg1 to journal */
3303 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003304 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003305
3306 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3307 ** header to be written between the pages journaled by this function.
3308 */
danielk1977b3175382008-10-17 18:51:52 +00003309 assert( !MEMDB );
danielk19774099f6e2007-03-19 11:25:20 +00003310 assert( pPager->doNotSync==0 );
3311 pPager->doNotSync = 1;
3312
3313 /* This trick assumes that both the page-size and sector-size are
3314 ** an integer power of 2. It sets variable pg1 to the identifier
3315 ** of the first page of the sector pPg is located on.
3316 */
3317 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3318
danielk1977ad0132d2008-06-07 08:58:22 +00003319 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00003320 if( pPg->pgno>nPageCount ){
3321 nPage = (pPg->pgno - pg1)+1;
3322 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3323 nPage = nPageCount+1-pg1;
3324 }else{
3325 nPage = nPagePerSector;
3326 }
3327 assert(nPage>0);
3328 assert(pg1<=pPg->pgno);
3329 assert((pg1+nPage)>pPg->pgno);
3330
3331 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3332 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003333 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00003334 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00003335 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003336 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003337 if( rc==SQLITE_OK ){
3338 rc = pager_write(pPage);
danielk19778c0a7912008-08-20 14:49:23 +00003339 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003340 needSync = 1;
3341 }
danielk19773b8a05f2007-03-19 17:44:26 +00003342 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003343 }
3344 }
drhc81945e2008-03-10 14:12:53 +00003345 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk19778c0a7912008-08-20 14:49:23 +00003346 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003347 needSync = 1;
3348 }
danielk19778c0a7912008-08-20 14:49:23 +00003349 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003350 }
3351 }
3352
danielk1977dd97a492007-08-22 18:54:32 +00003353 /* If the PgHdr.needSync flag is set for any of the nPage pages
3354 ** starting at pg1, then it needs to be set for all of them. Because
3355 ** writing to any of these nPage pages may damage the others, the
3356 ** journal file must contain sync()ed copies of all of them
3357 ** before any of them can be written out to the database file.
3358 */
3359 if( needSync ){
drhb3df2e12008-09-17 20:06:26 +00003360 assert( !MEMDB && pPager->noSync==0 );
danielk1977dd97a492007-08-22 18:54:32 +00003361 for(ii=0; ii<nPage && needSync; ii++){
3362 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
danielk19778c0a7912008-08-20 14:49:23 +00003363 if( pPage ) pPage->flags |= PGHDR_NEED_SYNC;
3364 sqlite3PagerUnref(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00003365 }
3366 assert(pPager->needSync);
3367 }
3368
danielk19774099f6e2007-03-19 11:25:20 +00003369 assert( pPager->doNotSync==1 );
3370 pPager->doNotSync = 0;
3371 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003372 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003373 }
3374 return rc;
3375}
3376
3377/*
drhaacc5432002-01-06 17:07:40 +00003378** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003379** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003380** to change the content of the page.
3381*/
danielk19777d3a6662006-01-23 16:21:05 +00003382#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003383int sqlite3PagerIswriteable(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003384 return pPg->flags&PGHDR_DIRTY;
drh6019e162001-07-02 17:51:45 +00003385}
danielk19777d3a6662006-01-23 16:21:05 +00003386#endif
drh6019e162001-07-02 17:51:45 +00003387
drh001bbcb2003-03-19 03:14:00 +00003388/*
drh30e58752002-03-02 20:41:57 +00003389** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003390** write the information on page pPg back to the disk, even though
drhdfe88ec2008-11-03 20:55:06 +00003391** that page might be marked as dirty. This happens, for example, when
3392** the page has been added as a leaf of the freelist and so its
3393** content no longer matters.
drh30e58752002-03-02 20:41:57 +00003394**
3395** The overlying software layer calls this routine when all of the data
3396** on the given page is unused. The pager marks the page as clean so
3397** that it does not get written to disk.
3398**
3399** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003400** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003401** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003402**
3403** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003404** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003405** will thereafter be ignored. This is necessary to avoid a problem
3406** where a page with data is added to the freelist during one part of
3407** a transaction then removed from the freelist during a later part
3408** of the same transaction and reused for some other purpose. When it
3409** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003410** the sqlite3PagerDontRollback() routine is called. But because the
3411** page contains critical data, we still need to be sure it gets
3412** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003413*/
danielk1977a1fa00d2008-08-27 15:16:33 +00003414int sqlite3PagerDontWrite(DbPage *pDbPage){
drh538f5702007-04-13 02:14:30 +00003415 PgHdr *pPg = pDbPage;
3416 Pager *pPager = pPg->pPager;
danielk1977a1fa00d2008-08-27 15:16:33 +00003417 int rc;
drh8e298f92002-07-06 16:28:47 +00003418
danielk1977b3175382008-10-17 18:51:52 +00003419 if( pPg->pgno>pPager->origDbSize ){
danielk1977a1fa00d2008-08-27 15:16:33 +00003420 return SQLITE_OK;
3421 }
3422 if( pPager->pAlwaysRollback==0 ){
3423 assert( pPager->pInJournal );
3424 pPager->pAlwaysRollback = sqlite3BitvecCreate(pPager->origDbSize);
3425 if( !pPager->pAlwaysRollback ){
3426 return SQLITE_NOMEM;
3427 }
3428 }
3429 rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno);
3430
3431 if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00003432 assert( pPager->state>=PAGER_SHARED );
danielk1977d92db532008-11-17 04:56:24 +00003433 if( pPager->dbSize==pPg->pgno && pPager->origDbSize<pPager->dbSize ){
drh8124a302002-06-25 14:43:57 +00003434 /* If this pages is the last page in the file and the file has grown
3435 ** during the current transaction, then do NOT mark the page as clean.
3436 ** When the database file grows, we must make sure that the last page
3437 ** gets written at least once so that the disk file will be the correct
3438 ** size. If you do not write this page and the size of the file
3439 ** on the disk ends up being too small, that can lead to database
3440 ** corruption during the next transaction.
3441 */
3442 }else{
drh538f5702007-04-13 02:14:30 +00003443 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
3444 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
danielk197733e32162008-08-23 18:53:08 +00003445 pPg->flags |= PGHDR_DONT_WRITE;
danielk19773c407372005-02-15 02:54:14 +00003446#ifdef SQLITE_CHECK_PAGES
3447 pPg->pageHash = pager_pagehash(pPg);
3448#endif
drh8124a302002-06-25 14:43:57 +00003449 }
drh30e58752002-03-02 20:41:57 +00003450 }
danielk1977a1fa00d2008-08-27 15:16:33 +00003451 return rc;
drh30e58752002-03-02 20:41:57 +00003452}
3453
3454/*
3455** A call to this routine tells the pager that if a rollback occurs,
3456** it is not necessary to restore the data on the given page. This
3457** means that the pager does not have to record the given page in the
3458** rollback journal.
drh538f5702007-04-13 02:14:30 +00003459**
3460** If we have not yet actually read the content of this page (if
3461** the PgHdr.needRead flag is set) then this routine acts as a promise
3462** that we will never need to read the page content in the future.
3463** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00003464*/
danielk19773b8a05f2007-03-19 17:44:26 +00003465void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003466 Pager *pPager = pPg->pPager;
3467
drhd3627af2006-12-18 18:34:51 +00003468 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00003469
3470 /* If the journal file is not open, or DontWrite() has been called on
3471 ** this page (DontWrite() sets the alwaysRollback flag), then this
3472 ** function is a no-op.
3473 */
danielk1977a1fa00d2008-08-27 15:16:33 +00003474 if( pPager->journalOpen==0
3475 || sqlite3BitvecTest(pPager->pAlwaysRollback, pPg->pgno)
3476 || pPg->pgno>pPager->origDbSize
danielk19778c0a7912008-08-20 14:49:23 +00003477 ){
danielk197787c29a92008-01-18 11:33:16 +00003478 return;
3479 }
danielk1977a55e9352008-01-21 13:04:34 +00003480
drhc5d0bd92008-04-14 01:00:57 +00003481#ifdef SQLITE_SECURE_DELETE
drh1feb7dd2008-11-19 18:30:29 +00003482 if( sqlite3BitvecTest(pPager->pInJournal, pPg->pgno)!=0
3483 || pPg->pgno>pPager->origDbSize ){
drhc5d0bd92008-04-14 01:00:57 +00003484 return;
3485 }
3486#endif
3487
3488 /* If SECURE_DELETE is disabled, then there is no way that this
3489 ** routine can be called on a page for which sqlite3PagerDontWrite()
3490 ** has not been previously called during the same transaction.
3491 ** And if DontWrite() has previously been called, the following
3492 ** conditions must be met.
drh10131482008-07-11 03:34:09 +00003493 **
3494 ** (Later:) Not true. If the database is corrupted by having duplicate
3495 ** pages on the freelist (ex: corrupt9.test) then the following is not
3496 ** necessarily true:
danielk1977a55e9352008-01-21 13:04:34 +00003497 */
drh10131482008-07-11 03:34:09 +00003498 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); */
danielk1977a55e9352008-01-21 13:04:34 +00003499
drhf5e7bb52008-02-18 14:47:33 +00003500 assert( pPager->pInJournal!=0 );
3501 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003502 pPg->flags &= ~PGHDR_NEED_READ;
danielk1977a55e9352008-01-21 13:04:34 +00003503 if( pPager->stmtInUse ){
drhc5d0bd92008-04-14 01:00:57 +00003504 assert( pPager->stmtSize >= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00003505 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00003506 }
danielk1977a55e9352008-01-21 13:04:34 +00003507 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
3508 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00003509}
3510
drhac69b052004-05-12 13:30:07 +00003511
drh30e58752002-03-02 20:41:57 +00003512/*
drh80e35f42007-03-30 14:06:34 +00003513** This routine is called to increment the database file change-counter,
3514** stored at byte 24 of the pager file.
3515*/
danielk1977c7b60172007-08-22 11:22:03 +00003516static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00003517 PgHdr *pPgHdr;
3518 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00003519 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00003520
drh701bb3b2008-08-02 03:50:39 +00003521#ifndef SQLITE_ENABLE_ATOMIC_WRITE
3522 assert( isDirect==0 ); /* isDirect is only true for atomic writes */
3523#endif
drh80e35f42007-03-30 14:06:34 +00003524 if( !pPager->changeCountDone ){
3525 /* Open page 1 of the file for writing. */
3526 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3527 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00003528
3529 if( !isDirect ){
3530 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00003531 if( rc!=SQLITE_OK ){
3532 sqlite3PagerUnref(pPgHdr);
3533 return rc;
3534 }
danielk1977c7b60172007-08-22 11:22:03 +00003535 }
3536
drh80e35f42007-03-30 14:06:34 +00003537 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00003538 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00003539 change_counter++;
danielk19778c0a7912008-08-20 14:49:23 +00003540 put32bits(((char*)pPgHdr->pData)+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00003541
drh701bb3b2008-08-02 03:50:39 +00003542#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977c7b60172007-08-22 11:22:03 +00003543 if( isDirect && pPager->fd->pMethods ){
danielk19778c0a7912008-08-20 14:49:23 +00003544 const void *zBuf = pPgHdr->pData;
danielk1977c7b60172007-08-22 11:22:03 +00003545 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
3546 }
drh701bb3b2008-08-02 03:50:39 +00003547#endif
danielk1977c7b60172007-08-22 11:22:03 +00003548
drh80e35f42007-03-30 14:06:34 +00003549 /* Release the page reference. */
3550 sqlite3PagerUnref(pPgHdr);
3551 pPager->changeCountDone = 1;
3552 }
danielk1977c7b60172007-08-22 11:22:03 +00003553 return rc;
drh80e35f42007-03-30 14:06:34 +00003554}
3555
3556/*
danielk1977f653d782008-03-20 11:04:21 +00003557** Sync the pager file to disk.
3558*/
3559int sqlite3PagerSync(Pager *pPager){
3560 int rc;
drh7426f862008-08-26 21:07:26 +00003561 if( MEMDB ){
3562 rc = SQLITE_OK;
3563 }else{
3564 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3565 }
danielk1977f653d782008-03-20 11:04:21 +00003566 return rc;
3567}
3568
3569/*
drh80e35f42007-03-30 14:06:34 +00003570** Sync the database file for the pager pPager. zMaster points to the name
3571** of a master journal file that should be written into the individual
3572** journal file. zMaster may be NULL, which is interpreted as no master
3573** journal (a single database transaction).
3574**
3575** This routine ensures that the journal is synced, all dirty pages written
3576** to the database file and the database file synced. The only thing that
3577** remains to commit the transaction is to delete the journal file (or
3578** master journal file if specified).
3579**
3580** Note that if zMaster==NULL, this does not overwrite a previous value
3581** passed to an sqlite3PagerCommitPhaseOne() call.
3582**
3583** If parameter nTrunc is non-zero, then the pager file is truncated to
3584** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00003585**
3586** If the final parameter - noSync - is true, then the database file itself
3587** is not synced. The caller must call sqlite3PagerSync() directly to
3588** sync the database file before calling CommitPhaseTwo() to delete the
3589** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00003590*/
danielk1977f653d782008-03-20 11:04:21 +00003591int sqlite3PagerCommitPhaseOne(
3592 Pager *pPager,
3593 const char *zMaster,
3594 Pgno nTrunc,
3595 int noSync
3596){
drh80e35f42007-03-30 14:06:34 +00003597 int rc = SQLITE_OK;
3598
danielk1977dad31b52008-05-15 11:08:07 +00003599 if( pPager->errCode ){
3600 return pPager->errCode;
3601 }
3602
drhd138c012008-05-13 00:58:18 +00003603 /* If no changes have been made, we can leave the transaction early.
3604 */
3605 if( pPager->dbModified==0 &&
3606 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3607 pPager->exclusiveMode!=0) ){
3608 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3609 return SQLITE_OK;
3610 }
3611
drh80e35f42007-03-30 14:06:34 +00003612 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
3613 pPager->zFilename, zMaster, nTrunc);
3614
3615 /* If this is an in-memory db, or no pages have been written to, or this
3616 ** function has already been called, it is a no-op.
3617 */
3618 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
3619 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00003620
danielk1977c7b60172007-08-22 11:22:03 +00003621#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3622 /* The atomic-write optimization can be used if all of the
3623 ** following are true:
3624 **
3625 ** + The file-system supports the atomic-write property for
3626 ** blocks of size page-size, and
3627 ** + This commit is not part of a multi-file transaction, and
3628 ** + Exactly one page has been modified and store in the journal file.
3629 **
3630 ** If the optimization can be used, then the journal file will never
3631 ** be created for this transaction.
3632 */
danielk19774d60af92008-09-08 15:35:06 +00003633 int useAtomicWrite;
danielk19778c0a7912008-08-20 14:49:23 +00003634 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
danielk19774d60af92008-09-08 15:35:06 +00003635 useAtomicWrite = (
danielk1977f55b8992007-08-24 08:15:53 +00003636 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00003637 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00003638 pPager->journalOff==jrnlBufferSize(pPager) &&
3639 nTrunc==0 &&
danielk19778c0a7912008-08-20 14:49:23 +00003640 (pPg==0 || pPg->pDirty==0)
danielk1977f55b8992007-08-24 08:15:53 +00003641 );
danielk1977700b9c52008-04-24 12:37:40 +00003642 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00003643 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00003644 /* Update the nRec field in the journal file. */
3645 int offset = pPager->journalHdr + sizeof(aJournalMagic);
3646 assert(pPager->nRec==1);
3647 rc = write32bits(pPager->jfd, offset, pPager->nRec);
3648
3649 /* Update the db file change counter. The following call will modify
3650 ** the in-memory representation of page 1 to include the updated
3651 ** change counter and then write page 1 directly to the database
3652 ** file. Because of the atomic-write property of the host file-system,
3653 ** this is safe.
3654 */
danielk1977ae72d982007-10-03 08:46:44 +00003655 if( rc==SQLITE_OK ){
3656 rc = pager_incr_changecounter(pPager, 1);
3657 }
danielk1977f55b8992007-08-24 08:15:53 +00003658 }else{
3659 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00003660 }
3661
danielk1977ae72d982007-10-03 08:46:44 +00003662 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00003663#endif
3664
drh80e35f42007-03-30 14:06:34 +00003665 /* If a master journal file name has already been written to the
3666 ** journal file, then no sync is required. This happens when it is
3667 ** written, then the process fails to upgrade from a RESERVED to an
3668 ** EXCLUSIVE lock. The next time the process tries to commit the
3669 ** transaction the m-j name will have already been written.
3670 */
3671 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00003672 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00003673 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00003674 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00003675#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197771aa7ff2008-05-20 07:05:09 +00003676 if( nTrunc!=0 ){
3677 /* If this transaction has made the database smaller, then all pages
3678 ** being discarded by the truncation must be written to the journal
3679 ** file.
3680 */
3681 Pgno i;
danielk1977d92db532008-11-17 04:56:24 +00003682 Pgno iSkip = PAGER_MJ_PGNO(pPager);
danielk197771aa7ff2008-05-20 07:05:09 +00003683 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
3684 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
3685 rc = sqlite3PagerGet(pPager, i, &pPg);
3686 if( rc!=SQLITE_OK ) goto sync_exit;
3687 rc = sqlite3PagerWrite(pPg);
3688 sqlite3PagerUnref(pPg);
3689 if( rc!=SQLITE_OK ) goto sync_exit;
3690 }
3691 }
3692 }
drh80e35f42007-03-30 14:06:34 +00003693#endif
danielk197771aa7ff2008-05-20 07:05:09 +00003694 rc = writeMasterJournal(pPager, zMaster);
3695 if( rc!=SQLITE_OK ) goto sync_exit;
3696 rc = syncJournal(pPager);
3697 }
drh80e35f42007-03-30 14:06:34 +00003698 }
danielk1977c7b60172007-08-22 11:22:03 +00003699 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00003700
3701#ifndef SQLITE_OMIT_AUTOVACUUM
3702 if( nTrunc!=0 ){
3703 rc = sqlite3PagerTruncate(pPager, nTrunc);
3704 if( rc!=SQLITE_OK ) goto sync_exit;
3705 }
3706#endif
3707
3708 /* Write all dirty pages to the database file */
danielk19778c0a7912008-08-20 14:49:23 +00003709 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003710 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00003711 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00003712 assert( rc!=SQLITE_IOERR_BLOCKED );
3713 /* The error might have left the dirty list all fouled up here,
3714 ** but that does not matter because if the if the dirty list did
3715 ** get corrupted, then the transaction will roll back and
3716 ** discard the dirty list. There is an assert in
3717 ** pager_get_all_dirty_pages() that verifies that no attempt
3718 ** is made to use an invalid dirty list.
3719 */
drh153c62c2007-08-24 03:51:33 +00003720 goto sync_exit;
3721 }
danielk19778c0a7912008-08-20 14:49:23 +00003722 sqlite3PcacheCleanAll(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003723
3724 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00003725 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00003726 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00003727 }
3728 IOTRACE(("DBSYNC %p\n", pPager))
3729
3730 pPager->state = PAGER_SYNCED;
3731 }else if( MEMDB && nTrunc!=0 ){
3732 rc = sqlite3PagerTruncate(pPager, nTrunc);
3733 }
3734
3735sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00003736 if( rc==SQLITE_IOERR_BLOCKED ){
3737 /* pager_incr_changecounter() may attempt to obtain an exclusive
3738 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00003739 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00003740 * better to return SQLITE_BUSY.
3741 */
3742 rc = SQLITE_BUSY;
3743 }
drh80e35f42007-03-30 14:06:34 +00003744 return rc;
3745}
3746
3747
3748/*
drhed7c8552001-04-11 14:29:21 +00003749** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00003750**
3751** If the commit fails for any reason, a rollback attempt is made
3752** and an error code is returned. If the commit worked, SQLITE_OK
3753** is returned.
drhed7c8552001-04-11 14:29:21 +00003754*/
drh80e35f42007-03-30 14:06:34 +00003755int sqlite3PagerCommitPhaseTwo(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003756 int rc = SQLITE_OK;
drhd9b02572001-04-15 00:37:09 +00003757
danielk1977efaaf572006-01-16 11:29:19 +00003758 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00003759 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003760 }
drha6abd042004-06-09 17:37:22 +00003761 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00003762 return SQLITE_ERROR;
3763 }
drhd138c012008-05-13 00:58:18 +00003764 if( pPager->dbModified==0 &&
3765 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3766 pPager->exclusiveMode!=0) ){
3767 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3768 return SQLITE_OK;
3769 }
drh4f0c5872007-03-26 22:05:01 +00003770 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
danielk1977b3175382008-10-17 18:51:52 +00003771 assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dirtyCache );
3772 rc = pager_end_transaction(pPager, pPager->setMaster);
3773 rc = pager_error(pPager, rc);
drh86f8c192007-08-22 00:39:19 +00003774 return rc;
drhed7c8552001-04-11 14:29:21 +00003775}
3776
3777/*
drha6abd042004-06-09 17:37:22 +00003778** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00003779** All in-memory cache pages revert to their original data contents.
3780** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00003781**
3782** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00003783** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00003784** process is writing trash into the journal file (SQLITE_CORRUPT) or
3785** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
3786** codes are returned for all these occasions. Otherwise,
3787** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00003788*/
danielk19773b8a05f2007-03-19 17:44:26 +00003789int sqlite3PagerRollback(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003790 int rc = SQLITE_OK;
drh4f0c5872007-03-26 22:05:01 +00003791 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
danielk1977b3175382008-10-17 18:51:52 +00003792 if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00003793 rc = pager_end_transaction(pPager, pPager->setMaster);
danielk19778c0a7912008-08-20 14:49:23 +00003794 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00003795 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00003796 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00003797 }
danielk19778c0a7912008-08-20 14:49:23 +00003798 rc = pPager->errCode;
drha6abd042004-06-09 17:37:22 +00003799 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003800 if( pPager->state==PAGER_RESERVED ){
3801 int rc2;
3802 rc = pager_playback(pPager, 0);
3803 rc2 = pager_end_transaction(pPager, pPager->setMaster);
3804 if( rc==SQLITE_OK ){
3805 rc = rc2;
3806 }
3807 }else{
3808 rc = pager_playback(pPager, 0);
3809 }
danielk197707cb5602006-01-20 10:55:05 +00003810
danielk1977b3175382008-10-17 18:51:52 +00003811 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00003812 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00003813 }
danielk19778c0a7912008-08-20 14:49:23 +00003814
3815 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
3816 ** cache. So call pager_error() on the way out to make any error
3817 ** persistent.
3818 */
3819 rc = pager_error(pPager, rc);
3820 }
drh86f8c192007-08-22 00:39:19 +00003821 return rc;
drh98808ba2001-10-18 12:34:46 +00003822}
drhd9b02572001-04-15 00:37:09 +00003823
3824/*
drh5e00f6c2001-09-13 13:46:56 +00003825** Return TRUE if the database file is opened read-only. Return FALSE
3826** if the database is (in theory) writable.
3827*/
danielk19773b8a05f2007-03-19 17:44:26 +00003828int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00003829 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00003830}
3831
3832/*
drh0f7eb612006-08-08 13:51:43 +00003833** Return the number of references to the pager.
3834*/
danielk19773b8a05f2007-03-19 17:44:26 +00003835int sqlite3PagerRefcount(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003836 return sqlite3PcacheRefCount(pPager->pPCache);
drh0f7eb612006-08-08 13:51:43 +00003837}
3838
danielk197771d5d2c2008-09-29 11:49:47 +00003839/*
3840** Return the number of references to the specified page.
3841*/
3842int sqlite3PagerPageRefcount(DbPage *pPage){
3843 return sqlite3PcachePageRefcount(pPage);
3844}
3845
drh0f7eb612006-08-08 13:51:43 +00003846#ifdef SQLITE_TEST
3847/*
drhd9b02572001-04-15 00:37:09 +00003848** This routine is used for testing and analysis only.
3849*/
danielk19773b8a05f2007-03-19 17:44:26 +00003850int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00003851 static int a[11];
danielk19778c0a7912008-08-20 14:49:23 +00003852 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
3853 a[1] = sqlite3PcachePagecount(pPager->pPCache);
3854 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
danielk1977d92db532008-11-17 04:56:24 +00003855 a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
drhd9b02572001-04-15 00:37:09 +00003856 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00003857 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003858 a[6] = pPager->nHit;
3859 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00003860 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00003861 a[9] = pPager->nRead;
3862 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00003863 return a;
3864}
danielk197717b90b52008-06-06 11:11:25 +00003865int sqlite3PagerIsMemdb(Pager *pPager){
3866 return MEMDB;
3867}
drh0f7eb612006-08-08 13:51:43 +00003868#endif
drhdd793422001-06-28 01:54:48 +00003869
drhfa86c412002-02-02 15:01:15 +00003870/*
drhac69b052004-05-12 13:30:07 +00003871** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00003872**
3873** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00003874** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00003875** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00003876*/
drh86f8c192007-08-22 00:39:19 +00003877static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00003878 int rc;
drhac69b052004-05-12 13:30:07 +00003879 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00003880 assert( pPager->state>=PAGER_SHARED );
danielk1977d92db532008-11-17 04:56:24 +00003881 assert( pPager->dbSizeValid );
drh4f0c5872007-03-26 22:05:01 +00003882 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhda47d772002-12-02 04:25:19 +00003883 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00003884 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00003885 return SQLITE_OK;
3886 }
drhfa86c412002-02-02 15:01:15 +00003887 assert( pPager->journalOpen );
drhf5e7bb52008-02-18 14:47:33 +00003888 assert( pPager->pInStmt==0 );
3889 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00003890 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003891 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00003892 return SQLITE_NOMEM;
3893 }
danielk197776572402004-06-25 02:38:54 +00003894 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00003895 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00003896 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00003897 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00003898 if( !pPager->stmtOpen ){
danielk1977b3175382008-10-17 18:51:52 +00003899 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3900 sqlite3MemJournalOpen(pPager->stfd);
3901 }else{
3902 rc = sqlite3PagerOpentemp(pPager, pPager->stfd, SQLITE_OPEN_SUBJOURNAL);
3903 if( rc ){
3904 goto stmt_begin_failed;
3905 }
drh334b2992007-09-06 23:28:23 +00003906 }
drhac69b052004-05-12 13:30:07 +00003907 pPager->stmtOpen = 1;
3908 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00003909 }
drhac69b052004-05-12 13:30:07 +00003910 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00003911 return SQLITE_OK;
3912
drhac69b052004-05-12 13:30:07 +00003913stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00003914 if( pPager->pInStmt ){
3915 sqlite3BitvecDestroy(pPager->pInStmt);
3916 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00003917 }
3918 return rc;
3919}
drh86f8c192007-08-22 00:39:19 +00003920int sqlite3PagerStmtBegin(Pager *pPager){
3921 int rc;
drh86f8c192007-08-22 00:39:19 +00003922 rc = pagerStmtBegin(pPager);
drh86f8c192007-08-22 00:39:19 +00003923 return rc;
3924}
drhfa86c412002-02-02 15:01:15 +00003925
3926/*
drhac69b052004-05-12 13:30:07 +00003927** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00003928*/
danielk19773b8a05f2007-03-19 17:44:26 +00003929int sqlite3PagerStmtCommit(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00003930 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00003931 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
danielk1977b3175382008-10-17 18:51:52 +00003932 sqlite3BitvecDestroy(pPager->pInStmt);
3933 pPager->pInStmt = 0;
drhac69b052004-05-12 13:30:07 +00003934 pPager->stmtNRec = 0;
3935 pPager->stmtInUse = 0;
danielk1977b3175382008-10-17 18:51:52 +00003936 if( sqlite3IsMemJournal(pPager->stfd) ){
3937 sqlite3OsTruncate(pPager->stfd, 0);
3938 }
drh663fc632002-02-02 18:49:19 +00003939 }
drhac69b052004-05-12 13:30:07 +00003940 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00003941 return SQLITE_OK;
3942}
3943
3944/*
drhac69b052004-05-12 13:30:07 +00003945** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00003946*/
danielk19773b8a05f2007-03-19 17:44:26 +00003947int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00003948 int rc;
drhac69b052004-05-12 13:30:07 +00003949 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00003950 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
danielk1977b3175382008-10-17 18:51:52 +00003951 rc = pager_stmt_playback(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003952 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00003953 }else{
3954 rc = SQLITE_OK;
3955 }
drhac69b052004-05-12 13:30:07 +00003956 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00003957 return rc;
3958}
3959
drh73509ee2003-04-06 20:44:45 +00003960/*
3961** Return the full pathname of the database file.
3962*/
danielk19773b8a05f2007-03-19 17:44:26 +00003963const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00003964 return pPager->zFilename;
3965}
3966
drhb20ea9d2004-02-09 01:20:36 +00003967/*
drhd0679ed2007-08-28 22:24:34 +00003968** Return the VFS structure for the pager.
3969*/
3970const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
3971 return pPager->pVfs;
3972}
3973
3974/*
drhcc6bb3e2007-08-31 16:11:35 +00003975** Return the file handle for the database file associated
3976** with the pager. This might return NULL if the file has
3977** not yet been opened.
3978*/
3979sqlite3_file *sqlite3PagerFile(Pager *pPager){
3980 return pPager->fd;
3981}
3982
3983/*
danielk19775865e3d2004-06-14 06:03:57 +00003984** Return the directory of the database file.
3985*/
danielk19773b8a05f2007-03-19 17:44:26 +00003986const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00003987 return pPager->zDirectory;
3988}
3989
3990/*
3991** Return the full pathname of the journal file.
3992*/
danielk19773b8a05f2007-03-19 17:44:26 +00003993const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00003994 return pPager->zJournal;
3995}
3996
3997/*
drh2c8997b2005-08-27 16:36:48 +00003998** Return true if fsync() calls are disabled for this pager. Return FALSE
3999** if fsync()s are executed normally.
4000*/
danielk19773b8a05f2007-03-19 17:44:26 +00004001int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004002 return pPager->noSync;
4003}
4004
drh7c4ac0c2007-04-05 11:25:58 +00004005#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004006/*
drhb20ea9d2004-02-09 01:20:36 +00004007** Set the codec for this pager
4008*/
danielk19773b8a05f2007-03-19 17:44:26 +00004009void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004010 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004011 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004012 void *pCodecArg
4013){
4014 pPager->xCodec = xCodec;
4015 pPager->pCodecArg = pCodecArg;
4016}
drh7c4ac0c2007-04-05 11:25:58 +00004017#endif
drhb20ea9d2004-02-09 01:20:36 +00004018
danielk1977687566d2004-11-02 12:56:41 +00004019#ifndef SQLITE_OMIT_AUTOVACUUM
4020/*
danielk197787c29a92008-01-18 11:33:16 +00004021** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004022**
drh5e385312007-06-16 04:42:12 +00004023** There must be no references to the page previously located at
4024** pgno (which we call pPgOld) though that page is allowed to be
drhb3df2e12008-09-17 20:06:26 +00004025** in cache. If the page previously located at pgno is not already
drh5e385312007-06-16 04:42:12 +00004026** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004027**
drh5e385312007-06-16 04:42:12 +00004028** References to the page pPg remain valid. Updating any
4029** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004030** allocated along with the page) is the responsibility of the caller.
4031**
danielk19775fd057a2005-03-09 13:09:43 +00004032** A transaction must be active when this routine is called. It used to be
4033** required that a statement transaction was not active, but this restriction
4034** has been removed (CREATE INDEX needs to move a page when a statement
4035** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00004036**
4037** If the fourth argument, isCommit, is non-zero, then this page is being
4038** moved as part of a database reorganization just before the transaction
4039** is being committed. In this case, it is guaranteed that the database page
4040** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00004041*/
danielk19774c999992008-07-16 18:17:55 +00004042int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00004043 PgHdr *pPgOld; /* The page being overwritten. */
danielk197794daf7f2004-11-08 09:26:09 +00004044 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004045
danielk1977687566d2004-11-02 12:56:41 +00004046 assert( pPg->nRef>0 );
4047
drh4f0c5872007-03-26 22:05:01 +00004048 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk19778c0a7912008-08-20 14:49:23 +00004049 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno);
drhb0603412007-02-28 04:47:26 +00004050 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004051
danielk1977b4626a32007-04-28 15:47:43 +00004052 pager_get_content(pPg);
danielk19774c999992008-07-16 18:17:55 +00004053
4054 /* If the journal needs to be sync()ed before page pPg->pgno can
4055 ** be written to, store pPg->pgno in local variable needSyncPgno.
4056 **
4057 ** If the isCommit flag is set, there is no need to remember that
4058 ** the journal needs to be sync()ed before database page pPg->pgno
4059 ** can be written to. The caller has already promised not to write to it.
4060 */
danielk19778c0a7912008-08-20 14:49:23 +00004061 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00004062 needSyncPgno = pPg->pgno;
danielk19770cd1bbd2008-11-26 07:25:52 +00004063 assert( pageInJournal(pPg) || pPg->pgno>pPager->origDbSize );
danielk19778c0a7912008-08-20 14:49:23 +00004064 assert( pPg->flags&PGHDR_DIRTY );
danielk1977ae825582004-11-23 09:06:55 +00004065 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004066 }
4067
danielk1977ef73ee92004-11-06 12:26:07 +00004068 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00004069 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00004070 ** page pgno before the 'move' operation, it needs to be retained
4071 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004072 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00004073 pPg->flags &= ~PGHDR_NEED_SYNC;
danielk1977687566d2004-11-02 12:56:41 +00004074 pPgOld = pager_lookup(pPager, pgno);
danielk19778c0a7912008-08-20 14:49:23 +00004075 assert( !pPgOld || pPgOld->nRef==1 );
danielk1977687566d2004-11-02 12:56:41 +00004076 if( pPgOld ){
danielk19778c0a7912008-08-20 14:49:23 +00004077 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
drh5e385312007-06-16 04:42:12 +00004078 }
danielk1977687566d2004-11-02 12:56:41 +00004079
danielk19778c0a7912008-08-20 14:49:23 +00004080 sqlite3PcacheMove(pPg, pgno);
4081 if( pPgOld ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00004082 sqlite3PcacheDrop(pPgOld);
danielk1977f5fdda82004-11-03 08:44:05 +00004083 }
danielk1977f5fdda82004-11-03 08:44:05 +00004084
drhc047b9f2008-11-21 03:23:43 +00004085 sqlite3PcacheMakeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004086 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004087 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00004088
danielk197794daf7f2004-11-08 09:26:09 +00004089 if( needSyncPgno ){
4090 /* If needSyncPgno is non-zero, then the journal file needs to be
4091 ** sync()ed before any data is written to database file page needSyncPgno.
4092 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00004093 ** "is journaled" bitvec flag has been set. This needs to be remedied by
4094 ** loading the page into the pager-cache and setting the PgHdr.needSync
4095 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00004096 **
danielk1977a98d7b42008-01-18 13:42:54 +00004097 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00004098 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00004099 ** array. Otherwise, if the page is loaded and written again in
4100 ** this transaction, it may be written to the database file before
4101 ** it is synced into the journal file. This way, it may end up in
4102 ** the journal file twice, but that is not a problem.
4103 **
danielk19773b8a05f2007-03-19 17:44:26 +00004104 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004105 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004106 */
4107 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004108 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004109 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004110 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00004111 if( rc!=SQLITE_OK ){
danielk1977d92db532008-11-17 04:56:24 +00004112 if( pPager->pInJournal && needSyncPgno<=pPager->origDbSize ){
drhf5e7bb52008-02-18 14:47:33 +00004113 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00004114 }
danielk197787c29a92008-01-18 11:33:16 +00004115 return rc;
4116 }
danielk1977ae825582004-11-23 09:06:55 +00004117 pPager->needSync = 1;
drhb3df2e12008-09-17 20:06:26 +00004118 assert( pPager->noSync==0 && !MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00004119 pPgHdr->flags |= PGHDR_NEED_SYNC;
drhc047b9f2008-11-21 03:23:43 +00004120 sqlite3PcacheMakeDirty(pPgHdr);
danielk19773b8a05f2007-03-19 17:44:26 +00004121 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004122 }
4123
danielk1977687566d2004-11-02 12:56:41 +00004124 return SQLITE_OK;
4125}
4126#endif
4127
danielk19773b8a05f2007-03-19 17:44:26 +00004128/*
4129** Return a pointer to the data for the specified page.
4130*/
4131void *sqlite3PagerGetData(DbPage *pPg){
danielk197771d5d2c2008-09-29 11:49:47 +00004132 assert( pPg->nRef>0 || pPg->pPager->memDb );
danielk19778c0a7912008-08-20 14:49:23 +00004133 return pPg->pData;
danielk19773b8a05f2007-03-19 17:44:26 +00004134}
4135
4136/*
4137** Return a pointer to the Pager.nExtra bytes of "extra" space
4138** allocated along with the specified page.
4139*/
4140void *sqlite3PagerGetExtra(DbPage *pPg){
4141 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00004142 return (pPager?pPg->pExtra:0);
danielk19773b8a05f2007-03-19 17:44:26 +00004143}
4144
danielk197741483462007-03-24 16:45:04 +00004145/*
4146** Get/set the locking-mode for this pager. Parameter eMode must be one
4147** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4148** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4149** the locking-mode is set to the value specified.
4150**
4151** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4152** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4153** locking-mode.
4154*/
4155int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004156 assert( eMode==PAGER_LOCKINGMODE_QUERY
4157 || eMode==PAGER_LOCKINGMODE_NORMAL
4158 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4159 assert( PAGER_LOCKINGMODE_QUERY<0 );
4160 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4161 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00004162 pPager->exclusiveMode = eMode;
4163 }
4164 return (int)pPager->exclusiveMode;
4165}
4166
drh3b020132008-04-17 17:02:01 +00004167/*
drh04335882008-09-26 21:08:08 +00004168** Get/set the journal-mode for this pager. Parameter eMode must be one of:
drh3b020132008-04-17 17:02:01 +00004169**
drh04335882008-09-26 21:08:08 +00004170** PAGER_JOURNALMODE_QUERY
4171** PAGER_JOURNALMODE_DELETE
4172** PAGER_JOURNALMODE_TRUNCATE
4173** PAGER_JOURNALMODE_PERSIST
4174** PAGER_JOURNALMODE_OFF
4175**
4176** If the parameter is not _QUERY, then the journal-mode is set to the
4177** value specified.
4178**
4179** The returned indicate the current (possibly updated)
drh3b020132008-04-17 17:02:01 +00004180** journal-mode.
4181*/
4182int sqlite3PagerJournalMode(Pager *pPager, int eMode){
danielk1977b3175382008-10-17 18:51:52 +00004183 if( !MEMDB ){
4184 assert( eMode==PAGER_JOURNALMODE_QUERY
4185 || eMode==PAGER_JOURNALMODE_DELETE
4186 || eMode==PAGER_JOURNALMODE_TRUNCATE
4187 || eMode==PAGER_JOURNALMODE_PERSIST
4188 || eMode==PAGER_JOURNALMODE_OFF
4189 || eMode==PAGER_JOURNALMODE_MEMORY );
4190 assert( PAGER_JOURNALMODE_QUERY<0 );
4191 if( eMode>=0 ){
4192 pPager->journalMode = eMode;
4193 }else{
4194 assert( eMode==PAGER_JOURNALMODE_QUERY );
4195 }
drh3b020132008-04-17 17:02:01 +00004196 }
drhfdc40e92008-04-17 20:59:37 +00004197 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00004198}
4199
danielk1977b53e4962008-06-04 06:45:59 +00004200/*
4201** Get/set the size-limit used for persistent journal files.
4202*/
4203i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
4204 if( iLimit>=-1 ){
4205 pPager->journalSizeLimit = iLimit;
4206 }
4207 return pPager->journalSizeLimit;
4208}
4209
drh2e66f0b2005-04-28 17:18:48 +00004210#endif /* SQLITE_OMIT_DISKIO */