blob: eac099ec1dd42fc60caf350ba64eca8baa0bc84c [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**
danielk1977488af092008-12-19 16:31:11 +000021** @(#) $Id: pager.c,v 1.518 2008/12/19 16:31:11 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025
26/*
drhdb48ee02003-01-16 13:42:43 +000027** Macros for troubleshooting. Normally turned off
28*/
danielk1977466be562004-06-10 02:16:01 +000029#if 0
drhd3627af2006-12-18 18:34:51 +000030#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000031#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
32#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
33#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
34#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
35#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000036#else
drh4f0c5872007-03-26 22:05:01 +000037#define PAGERTRACE1(X)
38#define PAGERTRACE2(X,Y)
39#define PAGERTRACE3(X,Y,Z)
40#define PAGERTRACE4(X,Y,Z,W)
41#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000042#endif
43
danielk1977599fcba2004-11-08 07:13:13 +000044/*
drh4f0c5872007-03-26 22:05:01 +000045** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000046** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000047**
drh85b623f2007-12-13 21:54:09 +000048** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000049** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000050** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000051*/
drhc001c582006-03-06 18:23:16 +000052#define PAGERID(p) ((int)(p->fd))
53#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000054
55/*
drhed7c8552001-04-11 14:29:21 +000056** The page cache as a whole is always in one of the following
57** states:
58**
drha6abd042004-06-09 17:37:22 +000059** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000060** writing the database file. There is no
61** data held in memory. This is the initial
62** state.
63**
drha6abd042004-06-09 17:37:22 +000064** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000065** Writing is not permitted. There can be
66** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000067** file at the same time.
drhed7c8552001-04-11 14:29:21 +000068**
drh726de592004-06-10 23:35:50 +000069** PAGER_RESERVED This process has reserved the database for writing
70** but has not yet made any changes. Only one process
71** at a time can reserve the database. The original
72** database file has not been modified so other
73** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000074** database file.
75**
76** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000077** Access is exclusive. No other processes or
78** threads can be reading or writing while one
79** process is writing.
80**
danielk1977aa5ccdf2004-06-14 05:10:42 +000081** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
82** after all dirty pages have been written to the
83** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000084** disk. All that remains to do is to remove or
85** truncate the journal file and the transaction
86** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000087**
drha6abd042004-06-09 17:37:22 +000088** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000089** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000090** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000091** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000092** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000093** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000094** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000095** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000096** PAGER_RESERVED means that there is an open rollback journal.
97** The transition to PAGER_EXCLUSIVE occurs before any changes
98** are made to the database file, though writes to the rollback
99** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
100** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
101** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000102*/
drha6abd042004-06-09 17:37:22 +0000103#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000104#define PAGER_SHARED 1 /* same as SHARED_LOCK */
105#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
106#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
107#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000108
drh684917c2004-10-05 02:41:42 +0000109/*
110** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
111** then failed attempts to get a reserved lock will invoke the busy callback.
112** This is off by default. To see why, consider the following scenario:
113**
114** Suppose thread A already has a shared lock and wants a reserved lock.
115** Thread B already has a reserved lock and wants an exclusive lock. If
116** both threads are using their busy callbacks, it might be a long time
117** be for one of the threads give up and allows the other to proceed.
118** But if the thread trying to get the reserved lock gives up quickly
119** (if it never invokes its busy callback) then the contention will be
120** resolved quickly.
121*/
122#ifndef SQLITE_BUSY_RESERVED_LOCK
123# define SQLITE_BUSY_RESERVED_LOCK 0
124#endif
drhd9b02572001-04-15 00:37:09 +0000125
drhed7c8552001-04-11 14:29:21 +0000126/*
drh887dc4c2004-10-22 16:22:57 +0000127** This macro rounds values up so that if the value is an address it
128** is guaranteed to be an address that is aligned to an 8-byte boundary.
129*/
130#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
131
drh9eb9e262004-02-11 02:18:05 +0000132/*
133** A macro used for invoking the codec if there is one
134*/
135#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000136# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
137# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000138#else
drhc001c582006-03-06 18:23:16 +0000139# define CODEC1(P,D,N,X) /* NO-OP */
140# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000141#endif
142
drhed7c8552001-04-11 14:29:21 +0000143/*
danielk1977fd7f0452008-12-17 17:30:26 +0000144** An instance of the following structure is allocated for each active
145** savepoint and statement transaction in the system. All such structures
146** are stored in the Pager.aSavepoint[] array, which is allocated and
147** resized using sqlite3Realloc().
148**
149** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
150** set to 0. If a journal-header is written into the main journal while
151** the savepoint is active, then iHdrOffset is set to the byte offset
152** immediately following the last journal record written into the main
153** journal before the journal-header. This is required during savepoint
154** rollback (see pagerPlaybackSavepoint()).
155*/
156typedef struct PagerSavepoint PagerSavepoint;
157struct PagerSavepoint {
158 i64 iOffset; /* Starting offset in main journal */
159 i64 iHdrOffset; /* See above */
160 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
161 Pgno nOrig; /* Original number of pages in file */
162 Pgno iSubRec; /* Index of first record in sub-journal */
163};
164
165/*
drhed7c8552001-04-11 14:29:21 +0000166** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000167**
drh4f0ee682007-03-30 20:43:40 +0000168** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000169** or SQLITE_FULL. Once one of the first three errors occurs, it persists
170** and is returned as the result of every major pager API call. The
171** SQLITE_FULL return code is slightly different. It persists only until the
172** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000173** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000174** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000175*/
176struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000177 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000178 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000179 u8 journalStarted; /* True if header of journal is synced */
180 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000181 u8 noReadlock; /* Do not bother to obtain readlocks */
drh603240c2002-03-05 01:11:12 +0000182 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000183 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000184 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000185 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000186 u8 tempFile; /* zFilename is a temporary file */
187 u8 readOnly; /* True for a read-only database */
188 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000189 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000190 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000191 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000192 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000193 u8 doNotSync; /* Boolean. While true, do not spill the cache */
194 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000195 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drhd138c012008-05-13 00:58:18 +0000196 u8 dbModified; /* True if there are any changes to the Db */
drh80e35f42007-03-30 14:06:34 +0000197 u8 changeCountDone; /* Set after incrementing the change-counter */
danielk1977d92db532008-11-17 04:56:24 +0000198 u8 dbSizeValid; /* Set when dbSize is correct */
drh33f4e022007-09-03 15:19:34 +0000199 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000200 int errCode; /* One of several kinds of errors */
danielk1977d92db532008-11-17 04:56:24 +0000201 Pgno dbSize; /* Number of pages in the file */
202 Pgno origDbSize; /* dbSize before the current change */
drhfcd35c72005-05-21 02:48:08 +0000203 int nRec; /* Number of pages written to the journal */
204 u32 cksumInit; /* Quasi-random value added to every checksum */
205 int stmtNRec; /* Number of records in stmt subjournal */
206 int nExtra; /* Add this many bytes to each in-memory page */
207 int pageSize; /* Number of bytes in a page */
208 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000209 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000210 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000211 Bitvec *pInJournal; /* One bit for each page in the database file */
danielk1977a1fa00d2008-08-27 15:16:33 +0000212 Bitvec *pAlwaysRollback; /* One bit for each page marked always-rollback */
drhfcd35c72005-05-21 02:48:08 +0000213 char *zFilename; /* Name of the database file */
214 char *zJournal; /* Name of the journal file */
215 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000216 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
danielk1977fd7f0452008-12-17 17:30:26 +0000217 sqlite3_file *sjfd; /* File descriptor for the sub-journal*/
danielk19771ceedd32008-11-19 10:22:33 +0000218 int (*xBusyHandler)(void*); /* Function to call when busy */
219 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
drheb206252004-10-01 02:00:31 +0000220 i64 journalOff; /* Current byte offset in the journal file */
221 i64 journalHdr; /* Byte offset to previous journal header */
drh98c58352008-11-07 00:24:53 +0000222 u32 sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000223#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000224 int nHit, nMiss; /* Cache hits and missing */
225 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000226#endif
danielk1977eaa06f62008-09-18 17:34:44 +0000227 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000228#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000229 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000230 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000231#endif
danielk19778186df82007-03-06 13:45:59 +0000232 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000233 char dbFileVers[16]; /* Changes whenever database file changes */
danielk1977b53e4962008-06-04 06:45:59 +0000234 i64 journalSizeLimit; /* Size limit for persistent journal files */
danielk19778c0a7912008-08-20 14:49:23 +0000235 PCache *pPCache; /* Pointer to page cache object */
danielk1977fd7f0452008-12-17 17:30:26 +0000236
237 PagerSavepoint *aSavepoint;
238 int nSavepoint;
drhd9b02572001-04-15 00:37:09 +0000239};
240
241/*
drh538f5702007-04-13 02:14:30 +0000242** The following global variables hold counters used for
243** testing purposes only. These variables do not exist in
244** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000245*/
246#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000247int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
248int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
249int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
drh538f5702007-04-13 02:14:30 +0000250# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000251#else
drh538f5702007-04-13 02:14:30 +0000252# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000253#endif
254
drh538f5702007-04-13 02:14:30 +0000255
256
drhfcd35c72005-05-21 02:48:08 +0000257/*
drh5e00f6c2001-09-13 13:46:56 +0000258** Journal files begin with the following magic string. The data
259** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000260**
drhae2b40c2004-06-09 19:03:54 +0000261** Since version 2.8.0, the journal format contains additional sanity
262** checking information. If the power fails while the journal is begin
263** written, semi-random garbage data might appear in the journal
264** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000265** to roll the journal back, the database could be corrupted. The additional
266** sanity checking data is an attempt to discover the garbage in the
267** journal and ignore it.
268**
drhae2b40c2004-06-09 19:03:54 +0000269** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000270** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000271** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000272** This cksum is initialized to a 32-bit random value that appears in the
273** journal file right after the header. The random initializer is important,
274** because garbage data that appears at the end of a journal is likely
275** data that was once in other files that have now been deleted. If the
276** garbage data came from an obsolete journal file, the checksums might
277** be correct. But by initializing the checksum to random value which
278** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000279*/
drhae2b40c2004-06-09 19:03:54 +0000280static const unsigned char aJournalMagic[] = {
281 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000282};
283
284/*
drh726de592004-06-10 23:35:50 +0000285** The size of the header and of each page in the journal is determined
286** by the following macros.
drh968af522003-02-11 14:55:40 +0000287*/
drhae2b40c2004-06-09 19:03:54 +0000288#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000289
danielk197776572402004-06-25 02:38:54 +0000290/*
291** The journal header size for this pager. In the future, this could be
292** set to some value read from the disk controller. The important
293** characteristic is that it is the same size as a disk sector.
294*/
295#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
296
drhb7f91642004-10-31 02:22:47 +0000297/*
298** The macro MEMDB is true if we are dealing with an in-memory database.
299** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
300** the value of MEMDB will be a constant and the compiler will optimize
301** out code that would never execute.
302*/
303#ifdef SQLITE_OMIT_MEMORYDB
304# define MEMDB 0
305#else
306# define MEMDB pPager->memDb
307#endif
308
309/*
danielk197776572402004-06-25 02:38:54 +0000310** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
311** reserved for working around a windows/posix incompatibility). It is
312** used in the journal to signify that the remainder of the journal file
313** is devoted to storing a master journal name - there are no more pages to
314** roll back. See comments for function writeMasterJournal() for details.
315*/
danielk1977599fcba2004-11-08 07:13:13 +0000316/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
danielk1977d92db532008-11-17 04:56:24 +0000317#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
danielk197713adf8a2004-06-03 16:08:41 +0000318
drh968af522003-02-11 14:55:40 +0000319/*
danielk197726836652005-01-17 01:33:13 +0000320** The maximum legal page number is (2^31 - 1).
321*/
322#define PAGER_MAX_PGNO 2147483647
323
324/*
danielk1977fd7f0452008-12-17 17:30:26 +0000325** Return false if it is necessary to write page *pPg into the sub-journal.
326** More accurately, true is returned if either:
327**
328** * No savepoints are open, or
329** * The page has been saved to the sub-journal since the most recent
330** savepoint was opened.
331**
332** TODO: There's a bug here. To do with PagerSavepoint.nOrig. Also consider
333** the idea that the page may not be required by the outermost savepoint
334** but may be required by some earlier savepoint, due to an incremental
335** vacuum operation.
danielk1977f35843b2007-04-07 15:03:17 +0000336*/
danielk1977fd7f0452008-12-17 17:30:26 +0000337static int pageInSavepoint(PgHdr *pPg){
danielk1977f35843b2007-04-07 15:03:17 +0000338 Pager *pPager = pPg->pPager;
danielk1977fd7f0452008-12-17 17:30:26 +0000339 if( pPager->nSavepoint==0 ){
340 return 1;
341 }
342 return sqlite3BitvecTest(
343 pPager->aSavepoint[pPager->nSavepoint-1].pInSavepoint, pPg->pgno
344 );
danielk1977f35843b2007-04-07 15:03:17 +0000345}
drh8ca0c722006-05-07 17:49:38 +0000346
danielk1977bc2ca9e2008-11-13 14:28:28 +0000347static int pageInJournal(PgHdr *pPg){
348 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
349}
350
drh8ca0c722006-05-07 17:49:38 +0000351/*
drh34e79ce2004-02-08 06:05:46 +0000352** Read a 32-bit integer from the given file descriptor. Store the integer
353** that is read in *pRes. Return SQLITE_OK if everything worked, or an
354** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000355**
356** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000357*/
danielk197762079062007-08-15 17:08:46 +0000358static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000359 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000360 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000361 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000362 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000363 }
drh94f33312002-08-12 12:29:56 +0000364 return rc;
365}
366
367/*
drh97b57482006-01-10 20:32:32 +0000368** Write a 32-bit integer into a string buffer in big-endian byte order.
369*/
drha3152892007-05-05 11:48:52 +0000370#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000371
372/*
drh34e79ce2004-02-08 06:05:46 +0000373** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
374** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000375*/
danielk197762079062007-08-15 17:08:46 +0000376static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000377 char ac[4];
drh97b57482006-01-10 20:32:32 +0000378 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000379 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000380}
381
drh2554f8b2003-01-22 01:26:44 +0000382/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000383** If file pFd is open, call sqlite3OsUnlock() on it.
384*/
385static int osUnlock(sqlite3_file *pFd, int eLock){
386 if( !pFd->pMethods ){
387 return SQLITE_OK;
388 }
389 return sqlite3OsUnlock(pFd, eLock);
390}
391
392/*
danielk1977c7b60172007-08-22 11:22:03 +0000393** This function determines whether or not the atomic-write optimization
394** can be used with this pager. The optimization can be used if:
395**
396** (a) the value returned by OsDeviceCharacteristics() indicates that
397** a database page may be written atomically, and
398** (b) the value returned by OsSectorSize() is less than or equal
399** to the page size.
400**
401** If the optimization cannot be used, 0 is returned. If it can be used,
402** then the value returned is the size of the journal file when it
403** contains rollback data for exactly one page.
404*/
405#ifdef SQLITE_ENABLE_ATOMIC_WRITE
406static int jrnlBufferSize(Pager *pPager){
407 int dc; /* Device characteristics */
408 int nSector; /* Sector size */
drhfacf0302008-06-17 15:12:00 +0000409 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000410 sqlite3_file *fd = pPager->fd;
411
412 if( fd->pMethods ){
413 dc = sqlite3OsDeviceCharacteristics(fd);
414 nSector = sqlite3OsSectorSize(fd);
drhfacf0302008-06-17 15:12:00 +0000415 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000416 }
417
418 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
419 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
420
drhfacf0302008-06-17 15:12:00 +0000421 if( !fd->pMethods ||
422 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000423 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
424 }
425 return 0;
426}
427#endif
428
429/*
danielk1977aef0bf62005-12-30 16:28:01 +0000430** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000431** code. The first argument is a pointer to the pager structure, the
432** second the error-code about to be returned by a pager API function.
433** The value returned is a copy of the second argument to this function.
434**
drh4f0ee682007-03-30 20:43:40 +0000435** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000436** the error becomes persistent. Until the persisten error is cleared,
437** subsequent API calls on this Pager will immediately return the same
438** error code.
439**
440** A persistent error indicates that the contents of the pager-cache
441** cannot be trusted. This state can be cleared by completely discarding
442** the contents of the pager-cache. If a transaction was active when
443** the persistent error occured, then the rollback journal may need
444** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000445*/
danielk1977ae72d982007-10-03 08:46:44 +0000446static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000447static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000448 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000449 assert(
450 pPager->errCode==SQLITE_FULL ||
451 pPager->errCode==SQLITE_OK ||
452 (pPager->errCode & 0xff)==SQLITE_IOERR
453 );
danielk1977979f38e2007-03-27 16:19:51 +0000454 if(
drh4ac285a2006-09-15 07:28:50 +0000455 rc2==SQLITE_FULL ||
456 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000457 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000458 ){
459 pPager->errCode = rc;
danielk19778c0a7912008-08-20 14:49:23 +0000460 if( pPager->state==PAGER_UNLOCK
461 && sqlite3PcacheRefCount(pPager->pPCache)==0
462 ){
danielk1977ae72d982007-10-03 08:46:44 +0000463 /* If the pager is already unlocked, call pager_unlock() now to
464 ** clear the error state and ensure that the pager-cache is
465 ** completely empty.
466 */
467 pager_unlock(pPager);
468 }
danielk1977aef0bf62005-12-30 16:28:01 +0000469 }
470 return rc;
471}
472
drh477731b2007-06-16 03:06:27 +0000473/*
474** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
475** on the cache using a hash function. This is used for testing
476** and debugging only.
477*/
danielk19773c407372005-02-15 02:54:14 +0000478#ifdef SQLITE_CHECK_PAGES
479/*
480** Return a 32-bit hash of the page data for pPage.
481*/
drh477731b2007-06-16 03:06:27 +0000482static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000483 u32 hash = 0;
484 int i;
drh477731b2007-06-16 03:06:27 +0000485 for(i=0; i<nByte; i++){
486 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000487 }
488 return hash;
489}
drh477731b2007-06-16 03:06:27 +0000490static u32 pager_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000491 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
492}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000493static void pager_set_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000494 pPage->pageHash = pager_pagehash(pPage);
drh477731b2007-06-16 03:06:27 +0000495}
danielk19773c407372005-02-15 02:54:14 +0000496
497/*
498** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
499** is defined, and NDEBUG is not defined, an assert() statement checks
500** that the page is either dirty or still matches the calculated page-hash.
501*/
502#define CHECK_PAGE(x) checkPage(x)
503static void checkPage(PgHdr *pPg){
504 Pager *pPager = pPg->pPager;
danielk1977b3175382008-10-17 18:51:52 +0000505 assert( !pPg->pageHash || pPager->errCode
danielk19778c0a7912008-08-20 14:49:23 +0000506 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
danielk19773c407372005-02-15 02:54:14 +0000507}
508
509#else
drh8ffa8172007-06-18 17:25:17 +0000510#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000511#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000512#define CHECK_PAGE(x)
drh41d30272008-08-20 21:47:45 +0000513#endif /* SQLITE_CHECK_PAGES */
danielk19773c407372005-02-15 02:54:14 +0000514
drhed7c8552001-04-11 14:29:21 +0000515/*
danielk197776572402004-06-25 02:38:54 +0000516** When this is called the journal file for pager pPager must be open.
517** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000518** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000519**
danielk197765839c62007-08-30 08:08:17 +0000520** zMaster must point to a buffer of at least nMaster bytes allocated by
521** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
522** enough space to write the master journal name). If the master journal
523** name in the journal is longer than nMaster bytes (including a
524** nul-terminator), then this is handled as if no master journal name
525** were present in the journal.
526**
527** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000528** SQLITE_OK returned.
529*/
danielk1977d92db532008-11-17 04:56:24 +0000530static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
danielk197776572402004-06-25 02:38:54 +0000531 int rc;
532 u32 len;
drheb206252004-10-01 02:00:31 +0000533 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000534 u32 cksum;
shane0b8d2762008-07-22 05:18:00 +0000535 u32 u; /* Unsigned loop counter */
danielk197776572402004-06-25 02:38:54 +0000536 unsigned char aMagic[8]; /* A buffer to hold the magic header */
537
danielk197765839c62007-08-30 08:08:17 +0000538 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000539
drh054889e2005-11-30 03:20:31 +0000540 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000541 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000542
danielk197762079062007-08-15 17:08:46 +0000543 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000544 if( rc!=SQLITE_OK ) return rc;
545
danielk197765839c62007-08-30 08:08:17 +0000546 if( len>=nMaster ){
547 return SQLITE_OK;
548 }
549
danielk197762079062007-08-15 17:08:46 +0000550 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000551 if( rc!=SQLITE_OK ) return rc;
552
danielk197762079062007-08-15 17:08:46 +0000553 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000554 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
555
danielk197765839c62007-08-30 08:08:17 +0000556 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000557 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000558 return rc;
559 }
danielk197765839c62007-08-30 08:08:17 +0000560 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000561
562 /* See if the checksum matches the master journal name */
shane0b8d2762008-07-22 05:18:00 +0000563 for(u=0; u<len; u++){
564 cksum -= zMaster[u];
danielk197765839c62007-08-30 08:08:17 +0000565 }
danielk19778191bff2004-06-28 04:52:30 +0000566 if( cksum ){
567 /* If the checksum doesn't add up, then one or more of the disk sectors
568 ** containing the master journal filename is corrupted. This means
569 ** definitely roll back, so just return SQLITE_OK and report a (nul)
570 ** master-journal filename.
571 */
danielk197765839c62007-08-30 08:08:17 +0000572 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000573 }
danielk197776572402004-06-25 02:38:54 +0000574
575 return SQLITE_OK;
576}
577
578/*
579** Seek the journal file descriptor to the next sector boundary where a
580** journal header may be read or written. Pager.journalOff is updated with
581** the new seek offset.
582**
583** i.e for a sector size of 512:
584**
585** Input Offset Output Offset
586** ---------------------------------------
587** 0 0
588** 512 512
589** 100 512
590** 2000 2048
591**
592*/
danielk197762079062007-08-15 17:08:46 +0000593static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000594 i64 offset = 0;
595 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000596 if( c ){
597 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
598 }
599 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
600 assert( offset>=c );
601 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
602 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000603}
604
605/*
drhf3a87622008-04-17 14:16:42 +0000606** Write zeros over the header of the journal file. This has the
607** effect of invalidating the journal file and committing the
608** transaction.
609*/
danielk1977df2566a2008-05-07 19:11:03 +0000610static int zeroJournalHdr(Pager *pPager, int doTruncate){
611 int rc = SQLITE_OK;
danielk197755a25a12008-09-11 10:29:15 +0000612 static const char zeroHdr[28] = {0};
drhf3a87622008-04-17 14:16:42 +0000613
danielk1977df2566a2008-05-07 19:11:03 +0000614 if( pPager->journalOff ){
danielk1977b53e4962008-06-04 06:45:59 +0000615 i64 iLimit = pPager->journalSizeLimit;
616
danielk1977df2566a2008-05-07 19:11:03 +0000617 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000618 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000619 rc = sqlite3OsTruncate(pPager->jfd, 0);
620 }else{
621 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
622 }
danielk197781620542008-06-07 05:19:37 +0000623 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000624 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
625 }
danielk1977b53e4962008-06-04 06:45:59 +0000626
627 /* At this point the transaction is committed but the write lock
628 ** is still held on the file. If there is a size limit configured for
629 ** the persistent journal and the journal file currently consumes more
630 ** space than that limit allows for, truncate it now. There is no need
631 ** to sync the file following this operation.
632 */
633 if( rc==SQLITE_OK && iLimit>0 ){
634 i64 sz;
635 rc = sqlite3OsFileSize(pPager->jfd, &sz);
636 if( rc==SQLITE_OK && sz>iLimit ){
637 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
638 }
639 }
drha06ecba2008-04-22 17:15:17 +0000640 }
drhf3a87622008-04-17 14:16:42 +0000641 return rc;
642}
643
644/*
danielk197776572402004-06-25 02:38:54 +0000645** The journal file must be open when this routine is called. A journal
646** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
647** current location.
648**
649** The format for the journal header is as follows:
650** - 8 bytes: Magic identifying journal format.
651** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
652** - 4 bytes: Random number used for page hash.
653** - 4 bytes: Initial database page count.
654** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000655** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000656**
danielk197767c007b2008-03-20 04:45:49 +0000657** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000658*/
659static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +0000660 int rc = SQLITE_OK;
661 char *zHeader = pPager->pTmpSpace;
danielk1977d92db532008-11-17 04:56:24 +0000662 u32 nHeader = pPager->pageSize;
663 u32 nWrite;
danielk1977fd7f0452008-12-17 17:30:26 +0000664 int ii;
danielk1977a664f8e2008-04-22 14:31:48 +0000665
666 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
667 nHeader = JOURNAL_HDR_SZ(pPager);
668 }
danielk197776572402004-06-25 02:38:54 +0000669
danielk1977fd7f0452008-12-17 17:30:26 +0000670 /* If there are active savepoints and any of them were created since the
671 ** most recent journal header was written, update the PagerSavepoint.iHdrOff
672 ** fields now.
673 */
674 for(ii=0; ii<pPager->nSavepoint; ii++){
675 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
676 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
677 }
danielk19774099f6e2007-03-19 11:25:20 +0000678 }
679
danielk197762079062007-08-15 17:08:46 +0000680 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000681 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000682
drh97b57482006-01-10 20:32:32 +0000683 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000684
685 /*
686 ** Write the nRec Field - the number of page records that follow this
687 ** journal header. Normally, zero is written to this value at this time.
688 ** After the records are added to the journal (and the journal synced,
689 ** if in full-sync mode), the zero is overwritten with the true number
690 ** of records (see syncJournal()).
691 **
692 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
693 ** reading the journal this value tells SQLite to assume that the
694 ** rest of the journal file contains valid page records. This assumption
695 ** is dangerous, as if a failure occured whilst writing to the journal
696 ** file it may contain some garbage data. There are two scenarios
697 ** where this risk can be ignored:
698 **
699 ** * When the pager is in no-sync mode. Corruption can follow a
700 ** power failure in this case anyway.
701 **
702 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
703 ** that garbage data is never appended to the journal file.
704 */
705 assert(pPager->fd->pMethods||pPager->noSync);
danielk1977b3175382008-10-17 18:51:52 +0000706 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
danielk19774cd2cd52007-08-23 14:48:23 +0000707 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
708 ){
709 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
710 }else{
711 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
712 }
713
drh97b57482006-01-10 20:32:32 +0000714 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +0000715 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +0000716 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
717 /* The initial database size */
718 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
719 /* The assumed sector size for this process */
720 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drh08609ce2008-11-29 22:49:23 +0000721
722 /* Initializing the tail of the buffer is not necessary. Everything
723 ** works find if the following memset() is omitted. But initializing
724 ** the memory prevents valgrind from complaining, so we are willing to
725 ** take the performance hit.
726 */
727 memset(&zHeader[sizeof(aJournalMagic)+16], 0,
728 nHeader-(sizeof(aJournalMagic)+16));
729
danielk197767c007b2008-03-20 04:45:49 +0000730 if( pPager->journalHdr==0 ){
731 /* The page size */
732 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
733 }
danielk197776572402004-06-25 02:38:54 +0000734
danielk1977a664f8e2008-04-22 14:31:48 +0000735 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
736 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
737 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
738 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +0000739 }
danielk1977a664f8e2008-04-22 14:31:48 +0000740
danielk197776572402004-06-25 02:38:54 +0000741 return rc;
742}
743
744/*
745** The journal file must be open when this is called. A journal header file
746** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
747** file. See comments above function writeJournalHdr() for a description of
748** the journal header format.
749**
750** If the header is read successfully, *nRec is set to the number of
751** page records following this header and *dbSize is set to the size of the
752** database before the transaction began, in pages. Also, pPager->cksumInit
753** is set to the value read from the journal header. SQLITE_OK is returned
754** in this case.
755**
756** If the journal header file appears to be corrupted, SQLITE_DONE is
757** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
758** cannot be read from the journal file an error code is returned.
759*/
760static int readJournalHdr(
761 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000762 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000763 u32 *pNRec,
764 u32 *pDbSize
765){
766 int rc;
767 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000768 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +0000769 int iPageSize;
danielk197776572402004-06-25 02:38:54 +0000770
danielk197762079062007-08-15 17:08:46 +0000771 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000772 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
773 return SQLITE_DONE;
774 }
danielk197762079062007-08-15 17:08:46 +0000775 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000776
danielk197762079062007-08-15 17:08:46 +0000777 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000778 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000779 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000780
781 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
782 return SQLITE_DONE;
783 }
784
danielk197762079062007-08-15 17:08:46 +0000785 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000786 if( rc ) return rc;
787
danielk197762079062007-08-15 17:08:46 +0000788 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000789 if( rc ) return rc;
790
danielk197762079062007-08-15 17:08:46 +0000791 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000792 if( rc ) return rc;
793
danielk197767c007b2008-03-20 04:45:49 +0000794 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
795 if( rc==SQLITE_OK
796 && iPageSize>=512
797 && iPageSize<=SQLITE_MAX_PAGE_SIZE
798 && ((iPageSize-1)&iPageSize)==0
799 ){
drh4f21c4a2008-12-10 22:15:00 +0000800 u16 pagesize = (u16)iPageSize;
danielk197767c007b2008-03-20 04:45:49 +0000801 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
802 }
803 if( rc ) return rc;
804
danielk197776572402004-06-25 02:38:54 +0000805 /* Update the assumed sector-size to match the value used by
806 ** the process that created this journal. If this journal was
807 ** created by a process other than this one, then this routine
808 ** is being called from within pager_playback(). The local value
809 ** of Pager.sectorSize is restored at the end of that routine.
810 */
drh98c58352008-11-07 00:24:53 +0000811 rc = read32bits(pPager->jfd, jrnlOff+12, &pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000812 if( rc ) return rc;
drh98c58352008-11-07 00:24:53 +0000813 if( (pPager->sectorSize & (pPager->sectorSize-1))!=0
814 || pPager->sectorSize>0x1000000 ){
815 return SQLITE_DONE;
816 }
danielk197776572402004-06-25 02:38:54 +0000817
818 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000819 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000820}
821
822
823/*
824** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000825** pPager at the current location. The master journal name must be the last
826** thing written to a journal file. If the pager is in full-sync mode, the
827** journal file descriptor is advanced to the next sector boundary before
828** anything is written. The format is:
829**
830** + 4 bytes: PAGER_MJ_PGNO.
831** + N bytes: length of master journal name.
832** + 4 bytes: N
833** + 4 bytes: Master journal name checksum.
834** + 8 bytes: aJournalMagic[].
835**
836** The master journal page checksum is the sum of the bytes in the master
837** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000838**
839** If zMaster is a NULL pointer (occurs for a single database transaction),
840** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000841*/
842static int writeMasterJournal(Pager *pPager, const char *zMaster){
843 int rc;
844 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000845 int i;
danielk197762079062007-08-15 17:08:46 +0000846 i64 jrnlOff;
danielk1977df2566a2008-05-07 19:11:03 +0000847 i64 jrnlSize;
drh97b57482006-01-10 20:32:32 +0000848 u32 cksum = 0;
849 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000850
danielk1977b3175382008-10-17 18:51:52 +0000851 if( !zMaster || pPager->setMaster ) return SQLITE_OK;
852 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ) return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000853 pPager->setMaster = 1;
854
drhea678832008-12-10 19:26:22 +0000855 len = sqlite3Strlen30(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000856 for(i=0; i<len; i++){
857 cksum += zMaster[i];
858 }
danielk197776572402004-06-25 02:38:54 +0000859
860 /* If in full-sync mode, advance to the next disk sector before writing
861 ** the master journal name. This is in case the previous page written to
862 ** the journal has already been synced.
863 */
864 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000865 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000866 }
danielk197762079062007-08-15 17:08:46 +0000867 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000868 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000869
danielk197762079062007-08-15 17:08:46 +0000870 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000871 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000872 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000873
danielk197762079062007-08-15 17:08:46 +0000874 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000875 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000876 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000877
drh97b57482006-01-10 20:32:32 +0000878 put32bits(zBuf, len);
879 put32bits(&zBuf[4], cksum);
880 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000881 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +0000882 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +0000883 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +0000884
885 /* If the pager is in peristent-journal mode, then the physical
886 ** journal-file may extend past the end of the master-journal name
887 ** and 8 bytes of magic data just written to the file. This is
888 ** dangerous because the code to rollback a hot-journal file
889 ** will not be able to find the master-journal name to determine
890 ** whether or not the journal is hot.
891 **
892 ** Easiest thing to do in this scenario is to truncate the journal
893 ** file to the required size.
894 */
895 if( (rc==SQLITE_OK)
896 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
897 && jrnlSize>jrnlOff
898 ){
899 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
900 }
danielk197776572402004-06-25 02:38:54 +0000901 return rc;
902}
903
904/*
drhed7c8552001-04-11 14:29:21 +0000905** Find a page in the hash table given its page number. Return
906** a pointer to the page or NULL if not found.
907*/
drhd9b02572001-04-15 00:37:09 +0000908static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000909 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000910 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
drhed7c8552001-04-11 14:29:21 +0000911 return p;
912}
913
914/*
danielk1977e180dd92007-04-05 17:15:52 +0000915** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +0000916** sets the state of the pager back to what it was when it was first
917** opened. Any outstanding pages are invalidated and subsequent attempts
918** to access those pages will likely result in a coredump.
919*/
drhd9b02572001-04-15 00:37:09 +0000920static void pager_reset(Pager *pPager){
danielk1977efaaf572006-01-16 11:29:19 +0000921 if( pPager->errCode ) return;
danielk19778c0a7912008-08-20 14:49:23 +0000922 sqlite3PcacheClear(pPager->pPCache);
danielk1977e277be02007-03-23 18:12:06 +0000923}
924
danielk197734cf35d2008-12-18 18:31:38 +0000925/*
926** Free all structures in the Pager.aSavepoint[] array and set both
927** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
928** if it is open and the pager is not in exclusive mode.
929*/
danielk1977fd7f0452008-12-17 17:30:26 +0000930static void releaseAllSavepoint(Pager *pPager){
931 int ii;
932 for(ii=0; ii<pPager->nSavepoint; ii++){
933 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
934 }
935 if( !pPager->exclusiveMode ){
936 sqlite3OsClose(pPager->sjfd);
937 }
938 sqlite3_free(pPager->aSavepoint);
939 pPager->aSavepoint = 0;
940 pPager->nSavepoint = 0;
941}
942
danielk197734cf35d2008-12-18 18:31:38 +0000943/*
944** Set the bit number pgno in the PagerSavepoint.pInSavepoint bitvecs of
945** all open savepoints.
946*/
danielk1977fd7f0452008-12-17 17:30:26 +0000947static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
948 int ii;
949 for(ii=0; ii<pPager->nSavepoint; ii++){
950 PagerSavepoint *p = &pPager->aSavepoint[ii];
951 if( pgno<=p->nOrig ){
952 /* TODO: malloc() failure handling */
953 sqlite3BitvecSet(p->pInSavepoint, pgno);
954 }
955 }
956 return SQLITE_OK;
957}
958
danielk1977e277be02007-03-23 18:12:06 +0000959/*
danielk1977ae72d982007-10-03 08:46:44 +0000960** Unlock the database file.
961**
962** If the pager is currently in error state, discard the contents of
963** the cache and reset the Pager structure internal state. If there is
964** an open journal-file, then the next time a shared-lock is obtained
965** on the pager file (by this or any other process), it will be
966** treated as a hot-journal and rolled back.
967*/
968static void pager_unlock(Pager *pPager){
969 if( !pPager->exclusiveMode ){
danielk1977b3175382008-10-17 18:51:52 +0000970 int rc = osUnlock(pPager->fd, NO_LOCK);
971 if( rc ) pPager->errCode = rc;
danielk1977d92db532008-11-17 04:56:24 +0000972 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +0000973 IOTRACE(("UNLOCK %p\n", pPager))
danielk1977ae72d982007-10-03 08:46:44 +0000974
danielk1977b3175382008-10-17 18:51:52 +0000975 /* Always close the journal file when dropping the database lock.
976 ** Otherwise, another connection with journal_mode=delete might
977 ** delete the file out from under us.
978 */
979 if( pPager->journalOpen ){
980 sqlite3OsClose(pPager->jfd);
981 pPager->journalOpen = 0;
982 sqlite3BitvecDestroy(pPager->pInJournal);
983 pPager->pInJournal = 0;
984 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
985 pPager->pAlwaysRollback = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000986 }
987
danielk1977b3175382008-10-17 18:51:52 +0000988 /* If Pager.errCode is set, the contents of the pager cache cannot be
989 ** trusted. Now that the pager file is unlocked, the contents of the
990 ** cache can be discarded and the error code safely cleared.
991 */
992 if( pPager->errCode ){
993 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
994 pager_reset(pPager);
danielk1977fd7f0452008-12-17 17:30:26 +0000995 releaseAllSavepoint(pPager);
danielk1977b3175382008-10-17 18:51:52 +0000996 pPager->journalOff = 0;
997 pPager->journalStarted = 0;
danielk1977b3175382008-10-17 18:51:52 +0000998 pPager->origDbSize = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000999 }
danielk1977b3175382008-10-17 18:51:52 +00001000
1001 pPager->state = PAGER_UNLOCK;
1002 pPager->changeCountDone = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001003 }
1004}
1005
1006/*
1007** Execute a rollback if a transaction is active and unlock the
1008** database file. If the pager has already entered the error state,
1009** do not attempt the rollback.
1010*/
1011static void pagerUnlockAndRollback(Pager *p){
danielk1977ae72d982007-10-03 08:46:44 +00001012 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +00001013 sqlite3BeginBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001014 sqlite3PagerRollback(p);
danielk19772d1d86f2008-06-20 14:59:51 +00001015 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001016 }
1017 pager_unlock(p);
danielk1977ae72d982007-10-03 08:46:44 +00001018}
1019
1020/*
drh80e35f42007-03-30 14:06:34 +00001021** This routine ends a transaction. A transaction is ended by either
1022** a COMMIT or a ROLLBACK.
1023**
drhed7c8552001-04-11 14:29:21 +00001024** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001025** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1026** the database lock and acquires a SHARED lock in its place if that is
1027** the appropriate thing to do. Release locks usually is appropriate,
1028** unless we are in exclusive access mode or unless this is a
1029** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1030**
1031** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001032**
1033** TODO: Consider keeping the journal file open for temporary databases.
1034** This might give a performance improvement on windows where opening
1035** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001036*/
danielk1977df2566a2008-05-07 19:11:03 +00001037static int pager_end_transaction(Pager *pPager, int hasMaster){
danielk197741483462007-03-24 16:45:04 +00001038 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001039 int rc2 = SQLITE_OK;
drha6abd042004-06-09 17:37:22 +00001040 if( pPager->state<PAGER_RESERVED ){
1041 return SQLITE_OK;
1042 }
danielk1977fd7f0452008-12-17 17:30:26 +00001043 releaseAllSavepoint(pPager);
drhda47d772002-12-02 04:25:19 +00001044 if( pPager->journalOpen ){
danielk1977b3175382008-10-17 18:51:52 +00001045 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
1046 int isMemoryJournal = sqlite3IsMemJournal(pPager->jfd);
1047 sqlite3OsClose(pPager->jfd);
1048 pPager->journalOpen = 0;
1049 if( !isMemoryJournal ){
1050 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
1051 }
1052 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE
drh04335882008-09-26 21:08:08 +00001053 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){
1054 pPager->journalOff = 0;
1055 pPager->journalStarted = 0;
1056 }else if( pPager->exclusiveMode
danielk197793f7af92008-05-09 16:57:50 +00001057 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1058 ){
1059 rc = zeroJournalHdr(pPager, hasMaster);
1060 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +00001061 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001062 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001063 }else{
drh04335882008-09-26 21:08:08 +00001064 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || rc );
danielk1977b4b47412007-08-17 15:53:36 +00001065 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001066 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +00001067 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00001068 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001069 }
danielk197741483462007-03-24 16:45:04 +00001070 }
drhf5e7bb52008-02-18 14:47:33 +00001071 sqlite3BitvecDestroy(pPager->pInJournal);
1072 pPager->pInJournal = 0;
danielk1977a1fa00d2008-08-27 15:16:33 +00001073 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
1074 pPager->pAlwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001075#ifdef SQLITE_CHECK_PAGES
danielk1977bc2ca9e2008-11-13 14:28:28 +00001076 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
danielk19773c407372005-02-15 02:54:14 +00001077#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +00001078 sqlite3PcacheCleanAll(pPager->pPCache);
danielk1977ef317ab2004-06-23 10:43:10 +00001079 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001080 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001081 }else{
drhf5e7bb52008-02-18 14:47:33 +00001082 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001083 }
danielk1977979f38e2007-03-27 16:19:51 +00001084
danielk197741483462007-03-24 16:45:04 +00001085 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001086 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001087 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001088 }else if( pPager->state==PAGER_SYNCED ){
1089 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001090 }
danielk1977334cdb62007-03-26 08:05:12 +00001091 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001092 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001093 pPager->needSync = 0;
danielk19778c0a7912008-08-20 14:49:23 +00001094 /* lruListSetFirstSynced(pPager); */
danielk1977b3175382008-10-17 18:51:52 +00001095 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00001096 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00001097 }
drhd138c012008-05-13 00:58:18 +00001098 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001099
1100 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001101}
1102
drhed7c8552001-04-11 14:29:21 +00001103/*
drh968af522003-02-11 14:55:40 +00001104** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001105**
1106** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001107** random initial value and the page number. We experimented with
1108** a checksum of the entire data, but that was found to be too slow.
1109**
1110** Note that the page number is stored at the beginning of data and
1111** the checksum is stored at the end. This is important. If journal
1112** corruption occurs due to a power failure, the most likely scenario
1113** is that one end or the other of the record will be changed. It is
1114** much less likely that the two ends of the journal record will be
1115** correct and the middle be corrupt. Thus, this "checksum" scheme,
1116** though fast and simple, catches the mostly likely kind of corruption.
1117**
1118** FIX ME: Consider adding every 200th (or so) byte of the data to the
1119** checksum. That way if a single page spans 3 or more disk sectors and
1120** only the middle sector is corrupt, we will still have a reasonable
1121** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001122*/
drh74161702006-02-24 02:53:49 +00001123static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001124 u32 cksum = pPager->cksumInit;
1125 int i = pPager->pageSize-200;
1126 while( i>0 ){
1127 cksum += aData[i];
1128 i -= 200;
1129 }
drh968af522003-02-11 14:55:40 +00001130 return cksum;
1131}
1132
1133/*
drhfa86c412002-02-02 15:01:15 +00001134** Read a single page from the journal file opened on file descriptor
1135** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001136**
drhc13148f2008-08-27 18:03:20 +00001137** The isMainJrnl flag is true if this is the main rollback journal and
1138** false for the statement journal. The main rollback journal uses
1139** checksums - the statement journal does not.
drhfa86c412002-02-02 15:01:15 +00001140*/
danielk197762079062007-08-15 17:08:46 +00001141static int pager_playback_one_page(
danielk1977fd7f0452008-12-17 17:30:26 +00001142 Pager *pPager, /* The pager being played back */
1143 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
1144 i64 offset, /* Offset of record to playback */
1145 Bitvec *pDone /* Bitvec of pages already played back */
danielk197762079062007-08-15 17:08:46 +00001146){
drhfa86c412002-02-02 15:01:15 +00001147 int rc;
drhae2b40c2004-06-09 19:03:54 +00001148 PgHdr *pPg; /* An existing page in the cache */
1149 Pgno pgno; /* The page number of a page in journal */
1150 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001151 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
danielk1977fd7f0452008-12-17 17:30:26 +00001152 sqlite3_file *jfd = (isMainJrnl ? pPager->jfd : pPager->sjfd);
drhfa86c412002-02-02 15:01:15 +00001153
danielk1977fd7f0452008-12-17 17:30:26 +00001154 /* The temp storage must be allocated at this point */
danielk19778186df82007-03-06 13:45:59 +00001155 assert( aData );
danielk1977488af092008-12-19 16:31:11 +00001156 assert( isMainJrnl || pDone );
drh96362842005-03-20 22:47:56 +00001157
danielk197762079062007-08-15 17:08:46 +00001158 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001159 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001160 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001161 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001162 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001163
drh968af522003-02-11 14:55:40 +00001164 /* Sanity checking on the page. This is more important that I originally
1165 ** thought. If a power failure occurs while the journal is being written,
1166 ** it could cause invalid data to be written into the journal. We need to
1167 ** detect this invalid data (with high probability) and ignore it.
1168 */
danielk197775edc162004-06-26 01:48:18 +00001169 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001170 return SQLITE_DONE;
1171 }
danielk1977fd7f0452008-12-17 17:30:26 +00001172 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
drh968af522003-02-11 14:55:40 +00001173 return SQLITE_OK;
1174 }
drhc13148f2008-08-27 18:03:20 +00001175 if( isMainJrnl ){
danielk197762079062007-08-15 17:08:46 +00001176 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001177 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001178 pPager->journalOff += 4;
danielk1977fd7f0452008-12-17 17:30:26 +00001179 if( !pDone && pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001180 return SQLITE_DONE;
1181 }
1182 }
danielk1977fd7f0452008-12-17 17:30:26 +00001183 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno)) ){
1184 return rc;
1185 }
drhfa86c412002-02-02 15:01:15 +00001186
danielk1977aa5ccdf2004-06-14 05:10:42 +00001187 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001188
1189 /* If the pager is in RESERVED state, then there must be a copy of this
1190 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001191 ** not the database file. The page is left marked dirty in this case.
1192 **
danielk19772df71c72007-05-24 07:22:42 +00001193 ** An exception to the above rule: If the database is in no-sync mode
1194 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001195 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1196 ** during a Movepage() call, then the page may not be in the cache
1197 ** either. So the condition described in the above paragraph is not
1198 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001199 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001200 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1201 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001202 **
1203 ** Ticket #1171: The statement journal might contain page content that is
1204 ** different from the page content at the start of the transaction.
1205 ** This occurs when a page is changed prior to the start of a statement
1206 ** then changed again within the statement. When rolling back such a
1207 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001208 ** for certain that original page contents are synced into the main rollback
1209 ** journal. Otherwise, a power loss might leave modified data in the
1210 ** database file without an entry in the rollback journal that can
1211 ** restore the database to its original form. Two conditions must be
1212 ** met before writing to the database files. (1) the database must be
1213 ** locked. (2) we know that the original page content is fully synced
1214 ** in the main journal either because the page is not in cache or else
1215 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001216 **
1217 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1218 ** is possible to fail a statement on a database that does not yet exist.
1219 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001220 */
drhae2b40c2004-06-09 19:03:54 +00001221 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001222 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1223 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
danielk19778c0a7912008-08-20 14:49:23 +00001224 if( (pPager->state>=PAGER_EXCLUSIVE)
1225 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
1226 && (pPager->fd->pMethods)
1227 ){
drh281b21d2008-08-22 12:57:08 +00001228 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
1229 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
danielk1977a3f3a5f2004-06-10 04:32:16 +00001230 }
drhfa86c412002-02-02 15:01:15 +00001231 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001232 /* No page should ever be explicitly rolled back that is in use, except
1233 ** for page 1 which is held in use in order to keep the lock on the
1234 ** database active. However such a page may be rolled back as a result
1235 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001236 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001237 */
drhb6f41482004-05-14 01:58:11 +00001238 void *pData;
danielk19778c0a7912008-08-20 14:49:23 +00001239 pData = pPg->pData;
drhae2b40c2004-06-09 19:03:54 +00001240 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001241 if( pPager->xReiniter ){
danielk1977eaa06f62008-09-18 17:34:44 +00001242 pPager->xReiniter(pPg);
drhde647132004-05-07 17:57:49 +00001243 }
danielk1977488af092008-12-19 16:31:11 +00001244 if( isMainJrnl && (!pDone || pPager->journalOff<=pPager->journalHdr) ){
1245 /* If the contents of this page were just restored from the main
1246 ** journal file, then its content must be as they were when the
1247 ** transaction was first opened. In this case we can mark the page
1248 ** as clean, since there will be no need to write it out to the.
1249 **
1250 ** There is one exception to this rule. If the page is being rolled
1251 ** back as part of a savepoint (or statement) rollback from an
1252 ** unsynced portion of the main journal file, then it is not safe
1253 ** to mark the page as clean. This is because marking the page as
1254 ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
1255 ** already in the journal file (recorded in Pager.pInJournal) and
1256 ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
1257 ** again within this transaction, it will be marked as dirty but
1258 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
1259 ** be written out into the database file before its journal file
1260 ** segment is synced. If a crash occurs during or following this,
1261 ** database corruption may ensue.
1262 */
drhc047b9f2008-11-21 03:23:43 +00001263 sqlite3PcacheMakeClean(pPg);
1264 }
danielk19773c407372005-02-15 02:54:14 +00001265#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001266 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001267#endif
drh86a88112007-04-16 15:02:19 +00001268 /* If this was page 1, then restore the value of Pager.dbFileVers.
1269 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001270 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001271 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001272 }
drh86a88112007-04-16 15:02:19 +00001273
1274 /* Decode the page just read from disk */
1275 CODEC1(pPager, pData, pPg->pgno, 3);
danielk19778c0a7912008-08-20 14:49:23 +00001276 sqlite3PcacheRelease(pPg);
drhfa86c412002-02-02 15:01:15 +00001277 }
1278 return rc;
1279}
1280
1281/*
danielk197713adf8a2004-06-03 16:08:41 +00001282** Parameter zMaster is the name of a master journal file. A single journal
1283** file that referred to the master journal file has just been rolled back.
1284** This routine checks if it is possible to delete the master journal file,
1285** and does so if it is.
drh726de592004-06-10 23:35:50 +00001286**
danielk197765839c62007-08-30 08:08:17 +00001287** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1288** available for use within this function.
1289**
1290**
drh726de592004-06-10 23:35:50 +00001291** The master journal file contains the names of all child journals.
1292** To tell if a master journal can be deleted, check to each of the
1293** children. If all children are either missing or do not refer to
1294** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001295*/
danielk1977b4b47412007-08-17 15:53:36 +00001296static int pager_delmaster(Pager *pPager, const char *zMaster){
1297 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001298 int rc;
1299 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001300 sqlite3_file *pMaster;
1301 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001302 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001303 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001304
1305 /* Open the master journal file exclusively in case some other process
1306 ** is running this routine also. Not that it makes too much difference.
1307 */
drhe5ae5732008-06-15 02:51:47 +00001308 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001309 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001310 if( !pMaster ){
1311 rc = SQLITE_NOMEM;
1312 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001313 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1314 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001315 }
danielk197713adf8a2004-06-03 16:08:41 +00001316 if( rc!=SQLITE_OK ) goto delmaster_out;
1317 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001318
1319 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001320 if( rc!=SQLITE_OK ) goto delmaster_out;
1321
1322 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001323 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001324 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001325 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001326
1327 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001328 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001329 */
drh4f21c4a2008-12-10 22:15:00 +00001330 zMasterJournal = (char *)sqlite3Malloc((int)nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001331 if( !zMasterJournal ){
1332 rc = SQLITE_NOMEM;
1333 goto delmaster_out;
1334 }
danielk197765839c62007-08-30 08:08:17 +00001335 zMasterPtr = &zMasterJournal[nMasterJournal];
drh4f21c4a2008-12-10 22:15:00 +00001336 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001337 if( rc!=SQLITE_OK ) goto delmaster_out;
1338
danielk19775865e3d2004-06-14 06:03:57 +00001339 zJournal = zMasterJournal;
1340 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001341 int exists;
1342 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1343 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001344 goto delmaster_out;
1345 }
danielk1977861f7452008-06-05 11:39:11 +00001346 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001347 /* One of the journals pointed to by the master journal exists.
1348 ** Open it and check if it points at the master journal. If
1349 ** so, return without deleting the master journal file.
1350 */
drh3b7b78b2004-11-24 01:16:43 +00001351 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001352 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1353 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001354 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001355 goto delmaster_out;
1356 }
danielk197713adf8a2004-06-03 16:08:41 +00001357
danielk197765839c62007-08-30 08:08:17 +00001358 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001359 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001360 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001361 goto delmaster_out;
1362 }
danielk197776572402004-06-25 02:38:54 +00001363
danielk197765839c62007-08-30 08:08:17 +00001364 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001365 if( c ){
danielk197776572402004-06-25 02:38:54 +00001366 /* We have a match. Do not delete the master journal file. */
1367 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001368 }
1369 }
drhea678832008-12-10 19:26:22 +00001370 zJournal += (sqlite3Strlen30(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001371 }
1372 }
1373
danielk1977fee2d252007-08-18 10:59:19 +00001374 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001375
1376delmaster_out:
1377 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001378 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001379 }
1380 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001381 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001382 }
danielk1977b4b47412007-08-17 15:53:36 +00001383 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001384 return rc;
1385}
1386
drha6abd042004-06-09 17:37:22 +00001387
danielk1977e180dd92007-04-05 17:15:52 +00001388static void pager_truncate_cache(Pager *pPager);
1389
drha6abd042004-06-09 17:37:22 +00001390/*
drhcb4c40b2004-08-18 19:09:43 +00001391** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001392** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001393**
1394** Might might be the case that the file on disk is smaller than nPage.
1395** This can happen, for example, if we are in the middle of a transaction
1396** which has extended the file size and the new pages are still all held
1397** in cache, then an INSERT or UPDATE does a statement rollback. Some
1398** operating system implementations can get confused if you try to
1399** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001400** so detect this case and write a single zero byte to the end of the new
1401** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001402*/
danielk1977d92db532008-11-17 04:56:24 +00001403static int pager_truncate(Pager *pPager, Pgno nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001404 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001405 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001406 i64 currentSize, newSize;
1407 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1408 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001409 if( rc==SQLITE_OK && currentSize!=newSize ){
1410 if( currentSize>newSize ){
1411 rc = sqlite3OsTruncate(pPager->fd, newSize);
1412 }else{
1413 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1414 }
drh7fe3f7e2007-11-29 18:44:27 +00001415 }
danielk1977e180dd92007-04-05 17:15:52 +00001416 }
1417 if( rc==SQLITE_OK ){
1418 pPager->dbSize = nPage;
1419 pager_truncate_cache(pPager);
1420 }
1421 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001422}
1423
1424/*
drhc80f0582007-05-01 16:59:48 +00001425** Set the sectorSize for the given pager.
1426**
drh334c80d2008-03-28 17:41:13 +00001427** The sector size is at least as big as the sector size reported
1428** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001429*/
1430static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001431 assert(pPager->fd->pMethods||pPager->tempFile);
1432 if( !pPager->tempFile ){
1433 /* Sector size doesn't matter for temporary files. Also, the file
1434 ** may not have been opened yet, in whcih case the OsSectorSize()
1435 ** call will segfault.
1436 */
1437 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1438 }
drh334c80d2008-03-28 17:41:13 +00001439 if( pPager->sectorSize<512 ){
1440 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001441 }
1442}
1443
1444/*
drhed7c8552001-04-11 14:29:21 +00001445** Playback the journal and thus restore the database file to
1446** the state it was in before we started making changes.
1447**
drh34e79ce2004-02-08 06:05:46 +00001448** The journal file format is as follows:
1449**
drhae2b40c2004-06-09 19:03:54 +00001450** (1) 8 byte prefix. A copy of aJournalMagic[].
1451** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001452** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001453** number of page records from the journal size.
1454** (3) 4 byte big-endian integer which is the initial value for the
1455** sanity checksum.
1456** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001457** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001458** (5) 4 byte big-endian integer which is the sector size. The header
1459** is this many bytes in size.
1460** (6) 4 byte big-endian integer which is the page case.
1461** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001462** name. The value may be zero (indicate that there is no master
1463** journal.)
drh334c80d2008-03-28 17:41:13 +00001464** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001465** and might be shorter than the value read from (5). If the first byte
1466** of the name is \000 then there is no master journal. The master
1467** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001468** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001469** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001470** + pPager->pageSize bytes of data.
1471** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001472**
drh334c80d2008-03-28 17:41:13 +00001473** When we speak of the journal header, we mean the first 8 items above.
1474** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001475**
1476** Call the value from the second bullet "nRec". nRec is the number of
1477** valid page entries in the journal. In most cases, you can compute the
1478** value of nRec from the size of the journal file. But if a power
1479** failure occurred while the journal was being written, it could be the
1480** case that the size of the journal file had already been increased but
1481** the extra entries had not yet made it safely to disk. In such a case,
1482** the value of nRec computed from the file size would be too large. For
1483** that reason, we always use the nRec value in the header.
1484**
1485** If the nRec value is 0xffffffff it means that nRec should be computed
1486** from the file size. This value is used when the user selects the
1487** no-sync option for the journal. A power failure could lead to corruption
1488** in this case. But for things like temporary table (which will be
1489** deleted when the power is restored) we don't care.
1490**
drhd9b02572001-04-15 00:37:09 +00001491** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001492** journal file then all pages up to the first corrupted page are rolled
1493** back (or no pages if the journal header is corrupted). The journal file
1494** is then deleted and SQLITE_OK returned, just as if no corruption had
1495** been encountered.
1496**
1497** If an I/O or malloc() error occurs, the journal-file is not deleted
1498** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001499*/
danielk1977e277be02007-03-23 18:12:06 +00001500static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001501 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001502 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001503 u32 nRec; /* Number of Records in the journal */
shane0b8d2762008-07-22 05:18:00 +00001504 u32 u; /* Unsigned loop counter */
drhd9b02572001-04-15 00:37:09 +00001505 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001506 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001507 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001508 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001509
drhc3a64ba2001-11-22 00:01:27 +00001510 /* Figure out how many records are in the journal. Abort early if
1511 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001512 */
drh8cfbf082001-09-19 13:22:39 +00001513 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001514 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001515 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001516 goto end_playback;
1517 }
drh240c5792004-02-08 00:40:52 +00001518
danielk197776572402004-06-25 02:38:54 +00001519 /* Read the master journal name from the journal, if it is present.
1520 ** If a master journal file name is specified, but the file is not
1521 ** present on disk, then the journal is not hot and does not need to be
1522 ** played back.
drh240c5792004-02-08 00:40:52 +00001523 */
danielk197765839c62007-08-30 08:08:17 +00001524 zMaster = pPager->pTmpSpace;
1525 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001526 if( rc==SQLITE_OK && zMaster[0] ){
1527 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001528 }
danielk197765839c62007-08-30 08:08:17 +00001529 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001530 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001531 goto end_playback;
1532 }
1533 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001534
danielk197776572402004-06-25 02:38:54 +00001535 /* This loop terminates either when the readJournalHdr() call returns
1536 ** SQLITE_DONE or an IO error occurs. */
1537 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001538
danielk197776572402004-06-25 02:38:54 +00001539 /* Read the next journal header from the journal file. If there are
1540 ** not enough bytes left in the journal file for a complete header, or
1541 ** it is corrupted, then a process must of failed while writing it.
1542 ** This indicates nothing more needs to be rolled back.
1543 */
1544 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1545 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001546 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001547 rc = SQLITE_OK;
1548 }
danielk197776572402004-06-25 02:38:54 +00001549 goto end_playback;
1550 }
1551
1552 /* If nRec is 0xffffffff, then this journal was created by a process
1553 ** working in no-sync mode. This means that the rest of the journal
1554 ** file consists of pages, there are no more journal headers. Compute
1555 ** the value of nRec based on this assumption.
1556 */
1557 if( nRec==0xffffffff ){
1558 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
drh4f21c4a2008-12-10 22:15:00 +00001559 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
danielk197776572402004-06-25 02:38:54 +00001560 }
1561
danielk1977e277be02007-03-23 18:12:06 +00001562 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001563 ** process and if this is the final header in the journal, then it means
1564 ** that this part of the journal was being filled but has not yet been
1565 ** synced to disk. Compute the number of pages based on the remaining
1566 ** size of the file.
1567 **
1568 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001569 */
drh8940f4e2007-08-11 00:26:20 +00001570 if( nRec==0 && !isHot &&
1571 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
drh4f21c4a2008-12-10 22:15:00 +00001572 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
danielk1977e277be02007-03-23 18:12:06 +00001573 }
1574
danielk197776572402004-06-25 02:38:54 +00001575 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001576 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001577 */
danielk1977e180dd92007-04-05 17:15:52 +00001578 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001579 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001580 if( rc!=SQLITE_OK ){
1581 goto end_playback;
1582 }
danielk197776572402004-06-25 02:38:54 +00001583 }
1584
danielk197776572402004-06-25 02:38:54 +00001585 /* Copy original pages out of the journal and back into the database file.
1586 */
shane0b8d2762008-07-22 05:18:00 +00001587 for(u=0; u<nRec; u++){
danielk1977fd7f0452008-12-17 17:30:26 +00001588 rc = pager_playback_one_page(pPager, 1, pPager->journalOff, 0);
danielk197776572402004-06-25 02:38:54 +00001589 if( rc!=SQLITE_OK ){
1590 if( rc==SQLITE_DONE ){
1591 rc = SQLITE_OK;
1592 pPager->journalOff = szJ;
1593 break;
1594 }else{
drha9625ea2008-09-03 00:08:29 +00001595 /* If we are unable to rollback, then the database is probably
1596 ** going to end up being corrupt. It is corrupt to us, anyhow.
1597 ** Perhaps the next process to come along can fix it....
1598 */
danielk197798c21902008-09-23 16:41:29 +00001599 rc = SQLITE_CORRUPT_BKPT;
danielk197776572402004-06-25 02:38:54 +00001600 goto end_playback;
1601 }
1602 }
drh968af522003-02-11 14:55:40 +00001603 }
drhed7c8552001-04-11 14:29:21 +00001604 }
drh580eeaf2006-02-24 03:09:37 +00001605 /*NOTREACHED*/
1606 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001607
1608end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001609 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001610 zMaster = pPager->pTmpSpace;
1611 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1612 }
1613 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001614 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001615 }
danielk1977c56774e2008-10-07 11:51:20 +00001616 if( rc==SQLITE_OK && zMaster[0] && res ){
danielk1977979f38e2007-03-27 16:19:51 +00001617 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001618 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001619 */
danielk197765839c62007-08-30 08:08:17 +00001620 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001621 }
danielk197776572402004-06-25 02:38:54 +00001622
1623 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001624 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001625 ** value. Reset it to the correct value for this process.
1626 */
drhc80f0582007-05-01 16:59:48 +00001627 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001628 return rc;
drhed7c8552001-04-11 14:29:21 +00001629}
1630
1631/*
danielk1977fd7f0452008-12-17 17:30:26 +00001632** Playback a savepoint.
drhfa86c412002-02-02 15:01:15 +00001633*/
danielk1977fd7f0452008-12-17 17:30:26 +00001634static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
drheb206252004-10-01 02:00:31 +00001635 i64 szJ; /* Size of the full journal */
danielk1977fd7f0452008-12-17 17:30:26 +00001636 i64 iHdrOff; /* End of first segment of main-journal records */
1637 Pgno ii; /* Loop counter */
1638 int rc; /* Return code */
1639 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
drhfa86c412002-02-02 15:01:15 +00001640
danielk1977fd7f0452008-12-17 17:30:26 +00001641 /* Allocate a bitvec to use to store the set of pages rolled back */
1642 if( pSavepoint ){
1643 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
1644 if( !pDone ){
1645 return SQLITE_NOMEM;
1646 }
danielk197776572402004-06-25 02:38:54 +00001647 }
danielk1977fd7f0452008-12-17 17:30:26 +00001648
1649 /* Truncate the database back to the size it was before the
1650 ** savepoint being reverted was opened.
drhfa86c412002-02-02 15:01:15 +00001651 */
danielk1977fd7f0452008-12-17 17:30:26 +00001652 rc = pager_truncate(pPager, pSavepoint?pSavepoint->nOrig:pPager->origDbSize);
drh1aa2d8b2007-01-03 15:34:29 +00001653 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001654
danielk1977fd7f0452008-12-17 17:30:26 +00001655 /* Now roll back all main journal file records that occur after byte
1656 ** byte offset PagerSavepoint.iOffset that have a page number less than
1657 ** or equal to PagerSavepoint.nOrig. As each record is played back,
1658 ** the corresponding bit in bitvec PagerSavepoint.pInSavepoint is
1659 ** cleared.
drhfa86c412002-02-02 15:01:15 +00001660 */
danielk1977fd7f0452008-12-17 17:30:26 +00001661 szJ = pPager->journalOff;
1662 if( pSavepoint ){
1663 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
1664 pPager->journalOff = pSavepoint->iOffset;
1665 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
1666 rc = pager_playback_one_page(pPager, 1, pPager->journalOff, pDone);
1667 assert( rc!=SQLITE_DONE );
1668 }
1669 }else{
1670 pPager->journalOff = 0;
drhfa86c412002-02-02 15:01:15 +00001671 }
danielk1977fd7f0452008-12-17 17:30:26 +00001672 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001673 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001674 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001675 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
danielk1977fd7f0452008-12-17 17:30:26 +00001676 assert( rc!=SQLITE_DONE );
danielk1977f0113002006-01-24 12:09:17 +00001677 if( nJRec==0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00001678 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001679 }
danielk197712dd5492008-12-18 15:45:07 +00001680 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00001681 rc = pager_playback_one_page(pPager, 1, pPager->journalOff, pDone);
danielk197776572402004-06-25 02:38:54 +00001682 assert( rc!=SQLITE_DONE );
danielk1977fd7f0452008-12-17 17:30:26 +00001683 }
1684 }
1685 assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
1686
1687 /* Now roll back pages from the sub-journal. */
1688 if( pSavepoint ){
1689 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->stmtNRec; ii++){
1690 i64 offset = ii*(4+pPager->pageSize);
1691 rc = pager_playback_one_page(pPager, 0, offset, pDone);
1692 assert( rc!=SQLITE_DONE );
danielk197776572402004-06-25 02:38:54 +00001693 }
drhfa86c412002-02-02 15:01:15 +00001694 }
danielk197776572402004-06-25 02:38:54 +00001695
danielk1977fd7f0452008-12-17 17:30:26 +00001696 sqlite3BitvecDestroy(pDone);
1697 if( rc==SQLITE_OK ){
danielk197775edc162004-06-26 01:48:18 +00001698 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001699 }
1700 return rc;
1701}
1702
1703/*
drhf57b14a2001-09-14 18:54:08 +00001704** Change the maximum number of in-memory pages that are allowed.
1705*/
danielk19773b8a05f2007-03-19 17:44:26 +00001706void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +00001707 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001708}
1709
1710/*
drh973b6e32003-02-12 14:09:42 +00001711** Adjust the robustness of the database to damage due to OS crashes
1712** or power failures by changing the number of syncs()s when writing
1713** the rollback journal. There are three levels:
1714**
drh054889e2005-11-30 03:20:31 +00001715** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001716** for temporary and transient files.
1717**
1718** NORMAL The journal is synced once before writes begin on the
1719** database. This is normally adequate protection, but
1720** it is theoretically possible, though very unlikely,
1721** that an inopertune power failure could leave the journal
1722** in a state which would cause damage to the database
1723** when it is rolled back.
1724**
1725** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001726** database (with some additional information - the nRec field
1727** of the journal header - being written in between the two
1728** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001729** single disk sector is atomic, then this mode provides
1730** assurance that the journal will not be corrupted to the
1731** point of causing damage to the database during rollback.
1732**
1733** Numeric values associated with these states are OFF==1, NORMAL=2,
1734** and FULL=3.
1735*/
danielk197793758c82005-01-21 08:13:14 +00001736#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh281b21d2008-08-22 12:57:08 +00001737void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
drh4f21c4a2008-12-10 22:15:00 +00001738 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
1739 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
drh281b21d2008-08-22 12:57:08 +00001740 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001741 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001742}
danielk197793758c82005-01-21 08:13:14 +00001743#endif
drh973b6e32003-02-12 14:09:42 +00001744
1745/*
drhaf6df112005-06-07 02:12:30 +00001746** The following global variable is incremented whenever the library
1747** attempts to open a temporary file. This information is used for
1748** testing and analysis only.
1749*/
drh0f7eb612006-08-08 13:51:43 +00001750#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001751int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001752#endif
drhaf6df112005-06-07 02:12:30 +00001753
1754/*
drh3f56e6e2007-03-15 01:16:47 +00001755** Open a temporary file.
1756**
1757** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001758** other error code if we fail. The OS will automatically delete the temporary
1759** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001760*/
danielk1977b4b47412007-08-17 15:53:36 +00001761static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00001762 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00001763 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00001764 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00001765){
drhfa86c412002-02-02 15:01:15 +00001766 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001767
drh0f7eb612006-08-08 13:51:43 +00001768#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001769 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001770#endif
danielk1977b4b47412007-08-17 15:53:36 +00001771
drh33f4e022007-09-03 15:19:34 +00001772 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1773 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00001774 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00001775 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00001776 return rc;
1777}
1778
danielk1977a858aa22008-08-22 16:22:17 +00001779static int pagerStress(void *,PgHdr *);
danielk19778c0a7912008-08-20 14:49:23 +00001780
drhfa86c412002-02-02 15:01:15 +00001781/*
drhed7c8552001-04-11 14:29:21 +00001782** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001783** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001784** the first call to sqlite3PagerGet() and is only held open until the
1785** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001786**
drh6446c4d2001-12-15 14:22:18 +00001787** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00001788** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00001789** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001790**
1791** If zFilename is ":memory:" then all information is held in cache.
1792** It is never written to disk. This can be used to implement an
1793** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001794*/
danielk19773b8a05f2007-03-19 17:44:26 +00001795int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001796 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001797 Pager **ppPager, /* Return the Pager structure here */
1798 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001799 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00001800 int flags, /* flags controlling this file */
1801 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00001802){
danielk1977b4b47412007-08-17 15:53:36 +00001803 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001804 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001805 int rc = SQLITE_OK;
1806 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001807 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001808 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001809 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001810 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1811 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977b3175382008-10-17 18:51:52 +00001812 int journalFileSize;
danielk19778c0a7912008-08-20 14:49:23 +00001813 int pcacheSize = sqlite3PcacheSize();
drhfacf0302008-06-17 15:12:00 +00001814 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
danielk197717b90b52008-06-06 11:11:25 +00001815 char *zPathname = 0;
1816 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001817
danielk1977b3175382008-10-17 18:51:52 +00001818 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
1819 journalFileSize = sqlite3JournalSize(pVfs);
1820 }else{
1821 journalFileSize = sqlite3MemJournalSize();
1822 }
1823
drh86f8c192007-08-22 00:39:19 +00001824 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001825 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001826
danielk197717b90b52008-06-06 11:11:25 +00001827 /* Compute and store the full pathname in an allocated buffer pointed
1828 ** to by zPathname, length nPathname. Or, if this is a temporary file,
1829 ** leave both nPathname and zPathname set to 0.
1830 */
drh1cc8c442007-08-24 16:08:29 +00001831 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00001832 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001833 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00001834 if( zPathname==0 ){
1835 return SQLITE_NOMEM;
1836 }
drh1cc8c442007-08-24 16:08:29 +00001837#ifndef SQLITE_OMIT_MEMORYDB
1838 if( strcmp(zFilename,":memory:")==0 ){
1839 memDb = 1;
1840 zPathname[0] = 0;
1841 }else
1842#endif
1843 {
danielk1977adfb9b02007-09-17 07:02:56 +00001844 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00001845 }
danielk197717b90b52008-06-06 11:11:25 +00001846 if( rc!=SQLITE_OK ){
1847 sqlite3_free(zPathname);
1848 return rc;
1849 }
drhea678832008-12-10 19:26:22 +00001850 nPathname = sqlite3Strlen30(zPathname);
drhae28c012007-08-24 16:29:23 +00001851 }
drh99b90c32008-03-17 13:50:58 +00001852
danielk1977b4b47412007-08-17 15:53:36 +00001853 /* Allocate memory for the pager structure */
1854 pPager = sqlite3MallocZero(
1855 sizeof(*pPager) + /* Pager structure */
danielk19778c0a7912008-08-20 14:49:23 +00001856 pcacheSize + /* PCache object */
danielk1977c7b60172007-08-22 11:22:03 +00001857 journalFileSize + /* The journal file structure */
danielk1977b3175382008-10-17 18:51:52 +00001858 pVfs->szOsFile + /* The main db file */
1859 journalFileSize * 2 + /* The two journal files */
danielk197717b90b52008-06-06 11:11:25 +00001860 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00001861 );
1862 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00001863 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00001864 return SQLITE_NOMEM;
1865 }
danielk19778c0a7912008-08-20 14:49:23 +00001866 pPager->pPCache = (PCache *)&pPager[1];
1867 pPtr = ((u8 *)&pPager[1]) + pcacheSize;
drh33f4e022007-09-03 15:19:34 +00001868 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00001869 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977fd7f0452008-12-17 17:30:26 +00001870 pPager->sjfd = (sqlite3_file*)&pPtr[pVfs->szOsFile];
danielk1977b3175382008-10-17 18:51:52 +00001871 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile+journalFileSize];
1872 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile+2*journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00001873 pPager->zDirectory = &pPager->zFilename[nPathname+1];
1874 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00001875 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00001876 if( zPathname ){
1877 memcpy(pPager->zFilename, zPathname, nPathname+1);
1878 sqlite3_free(zPathname);
1879 }
danielk1977b4b47412007-08-17 15:53:36 +00001880
drh153c62c2007-08-24 03:51:33 +00001881 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00001882 */
drhae28c012007-08-24 16:29:23 +00001883 if( zFilename && zFilename[0] && !memDb ){
danielk1977d92db532008-11-17 04:56:24 +00001884 if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){
drh1cc8c442007-08-24 16:08:29 +00001885 rc = SQLITE_CANTOPEN;
1886 }else{
drh1cc8c442007-08-24 16:08:29 +00001887 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00001888 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
1889 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00001890 readOnly = (fout&SQLITE_OPEN_READONLY);
1891
1892 /* If the file was successfully opened for read/write access,
1893 ** choose a default page size in case we have to create the
1894 ** database file. The default page size is the maximum of:
1895 **
1896 ** + SQLITE_DEFAULT_PAGE_SIZE,
1897 ** + The value returned by sqlite3OsSectorSize()
1898 ** + The largest page size that can be written atomically.
1899 */
1900 if( rc==SQLITE_OK && !readOnly ){
1901 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
drhfacf0302008-06-17 15:12:00 +00001902 if( szPageDflt<iSectorSize ){
1903 szPageDflt = iSectorSize;
drh1cc8c442007-08-24 16:08:29 +00001904 }
danielk19779663b8f2007-08-24 11:52:28 +00001905#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00001906 {
1907 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
1908 int ii;
1909 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
1910 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
1911 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
drhfacf0302008-06-17 15:12:00 +00001912 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
1913 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00001914 }
danielk1977b4b47412007-08-17 15:53:36 +00001915 }
drh1cc8c442007-08-24 16:08:29 +00001916#endif
drhfacf0302008-06-17 15:12:00 +00001917 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
1918 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
drh1cc8c442007-08-24 16:08:29 +00001919 }
danielk19778def5ea2004-06-16 10:39:52 +00001920 }
drhac69b052004-05-12 13:30:07 +00001921 }
danielk1977b3175382008-10-17 18:51:52 +00001922 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00001923 /* If a temporary file is requested, it is not opened immediately.
1924 ** In this case we accept the default page size and delay actually
1925 ** opening the file until the first call to OsWrite().
danielk1977b3175382008-10-17 18:51:52 +00001926 **
1927 ** This branch is also run for an in-memory database. An in-memory
1928 ** database is the same as a temp-file that is never written out to
1929 ** disk and uses an in-memory rollback journal.
danielk19777a2b1ee2007-08-21 14:27:01 +00001930 */
danielk19777a2b1ee2007-08-21 14:27:01 +00001931 tempFile = 1;
1932 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00001933 }
danielk1977aef0bf62005-12-30 16:28:01 +00001934
danielk1977b4b47412007-08-17 15:53:36 +00001935 if( pPager && rc==SQLITE_OK ){
drhfacf0302008-06-17 15:12:00 +00001936 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00001937 }
danielk1977aef0bf62005-12-30 16:28:01 +00001938
drh153c62c2007-08-24 03:51:33 +00001939 /* If an error occured in either of the blocks above.
1940 ** Free the Pager structure and close the file.
1941 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00001942 ** any Pager.errMask variables.
1943 */
danielk1977b4b47412007-08-17 15:53:36 +00001944 if( !pPager || !pPager->pTmpSpace ){
1945 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00001946 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00001947 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001948 }
danielk19778c0a7912008-08-20 14:49:23 +00001949 nExtra = FORCE_ALIGNMENT(nExtra);
danielk197771d5d2c2008-09-29 11:49:47 +00001950 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
drh41d30272008-08-20 21:47:45 +00001951 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
danielk1977aef0bf62005-12-30 16:28:01 +00001952
drh153c62c2007-08-24 03:51:33 +00001953 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
1954 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00001955
danielk1977b4b47412007-08-17 15:53:36 +00001956 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00001957 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
drhea678832008-12-10 19:26:22 +00001958 for(i=sqlite3Strlen30(pPager->zDirectory);
1959 i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00001960 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001961
drh99b90c32008-03-17 13:50:58 +00001962 /* Fill in Pager.zJournal[] */
danielk197717b90b52008-06-06 11:11:25 +00001963 if( zPathname ){
1964 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
1965 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
1966 }else{
1967 pPager->zJournal = 0;
1968 }
danielk1977b4b47412007-08-17 15:53:36 +00001969
drh3b59a5c2006-01-15 20:28:28 +00001970 /* pPager->journalOpen = 0; */
drh4f21c4a2008-12-10 22:15:00 +00001971 pPager->useJournal = (u8)useJournal;
1972 pPager->noReadlock = (noReadlock && readOnly) ?1:0;
drh3b59a5c2006-01-15 20:28:28 +00001973 /* pPager->stmtOpen = 0; */
1974 /* pPager->stmtInUse = 0; */
1975 /* pPager->nRef = 0; */
drh4f21c4a2008-12-10 22:15:00 +00001976 pPager->dbSizeValid = (u8)memDb;
drhfacf0302008-06-17 15:12:00 +00001977 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00001978 /* pPager->stmtSize = 0; */
1979 /* pPager->stmtJSize = 0; */
1980 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00001981 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00001982 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00001983 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00001984 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00001985 /* pPager->errMask = 0; */
drh4f21c4a2008-12-10 22:15:00 +00001986 pPager->tempFile = (u8)tempFile;
drh369339d2007-03-30 16:01:55 +00001987 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
1988 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
1989 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
drh4f21c4a2008-12-10 22:15:00 +00001990 pPager->exclusiveMode = (u8)tempFile;
1991 pPager->memDb = (u8)memDb;
1992 pPager->readOnly = (u8)readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001993 /* pPager->needSync = 0; */
drh4f21c4a2008-12-10 22:15:00 +00001994 pPager->noSync = (pPager->tempFile || !useJournal) ?1:0;
1995 pPager->fullSync = pPager->noSync ?0:1;
danielk1977f036aef2007-08-20 05:36:51 +00001996 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00001997 /* pPager->pFirst = 0; */
1998 /* pPager->pFirstSynced = 0; */
1999 /* pPager->pLast = 0; */
danielk19778c0a7912008-08-20 14:49:23 +00002000 pPager->nExtra = nExtra;
danielk1977b53e4962008-06-04 06:45:59 +00002001 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk1977b3175382008-10-17 18:51:52 +00002002 assert(pPager->fd->pMethods||tempFile);
2003 setSectorSize(pPager);
2004 if( memDb ){
2005 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
danielk1977b4721172007-03-19 05:54:48 +00002006 }
danielk19771ceedd32008-11-19 10:22:33 +00002007 /* pPager->xBusyHandler = 0; */
2008 /* pPager->pBusyHandlerArg = 0; */
drh3b59a5c2006-01-15 20:28:28 +00002009 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002010 *ppPager = pPager;
2011 return SQLITE_OK;
2012}
2013
2014/*
drh90f5ecb2004-07-22 01:19:35 +00002015** Set the busy handler function.
2016*/
danielk19771ceedd32008-11-19 10:22:33 +00002017void sqlite3PagerSetBusyhandler(
2018 Pager *pPager,
2019 int (*xBusyHandler)(void *),
2020 void *pBusyHandlerArg
2021){
2022 pPager->xBusyHandler = xBusyHandler;
2023 pPager->pBusyHandlerArg = pBusyHandlerArg;
drh90f5ecb2004-07-22 01:19:35 +00002024}
2025
2026/*
drha6abd042004-06-09 17:37:22 +00002027** Set the reinitializer for this pager. If not NULL, the reinitializer
2028** is called when the content of a page in cache is restored to its original
2029** value as a result of a rollback. The callback gives higher-level code
2030** an opportunity to restore the EXTRA section to agree with the restored
2031** page data.
2032*/
danielk1977eaa06f62008-09-18 17:34:44 +00002033void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
drha6abd042004-06-09 17:37:22 +00002034 pPager->xReiniter = xReinit;
2035}
2036
2037/*
danielk1977a1644fd2007-08-29 12:31:25 +00002038** Set the page size to *pPageSize. If the suggest new page size is
2039** inappropriate, then an alternative page size is set to that
2040** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002041*/
danielk1977a1644fd2007-08-29 12:31:25 +00002042int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
danielk19771357d9f2008-09-16 05:12:24 +00002043 int rc = pPager->errCode;
2044 if( rc==SQLITE_OK ){
2045 u16 pageSize = *pPageSize;
2046 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
2047 if( pageSize && pageSize!=pPager->pageSize
2048 && (pPager->memDb==0 || pPager->dbSize==0)
2049 && sqlite3PcacheRefCount(pPager->pPCache)==0
2050 ){
2051 char *pNew = (char *)sqlite3PageMalloc(pageSize);
2052 if( !pNew ){
2053 rc = SQLITE_NOMEM;
2054 }else{
2055 pager_reset(pPager);
2056 pPager->pageSize = pageSize;
2057 if( !pPager->memDb ) setSectorSize(pPager);
2058 sqlite3PageFree(pPager->pTmpSpace);
2059 pPager->pTmpSpace = pNew;
2060 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
2061 }
danielk1977a1644fd2007-08-29 12:31:25 +00002062 }
drh4f21c4a2008-12-10 22:15:00 +00002063 *pPageSize = (u16)pPager->pageSize;
drh1c7880e2005-05-20 20:01:55 +00002064 }
danielk1977a1644fd2007-08-29 12:31:25 +00002065 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002066}
2067
2068/*
drh26b79942007-11-28 16:19:56 +00002069** Return a pointer to the "temporary page" buffer held internally
2070** by the pager. This is a buffer that is big enough to hold the
2071** entire content of a database page. This buffer is used internally
2072** during rollback and will be overwritten whenever a rollback
2073** occurs. But other modules are free to use it too, as long as
2074** no rollbacks are happening.
2075*/
2076void *sqlite3PagerTempSpace(Pager *pPager){
2077 return pPager->pTmpSpace;
2078}
2079
2080/*
drhf8e632b2007-05-08 14:51:36 +00002081** Attempt to set the maximum database page count if mxPage is positive.
2082** Make no changes if mxPage is zero or negative. And never reduce the
2083** maximum page count below the current size of the database.
2084**
2085** Regardless of mxPage, return the current maximum page count.
2086*/
2087int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2088 if( mxPage>0 ){
2089 pPager->mxPgno = mxPage;
2090 }
danielk1977ad0132d2008-06-07 08:58:22 +00002091 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002092 return pPager->mxPgno;
2093}
2094
2095/*
drhc9ac5ca2005-11-04 22:03:30 +00002096** The following set of routines are used to disable the simulated
2097** I/O error mechanism. These routines are used to avoid simulated
2098** errors in places where we do not care about errors.
2099**
2100** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2101** and generate no code.
2102*/
2103#ifdef SQLITE_TEST
2104extern int sqlite3_io_error_pending;
2105extern int sqlite3_io_error_hit;
2106static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002107void disable_simulated_io_errors(void){
2108 saved_cnt = sqlite3_io_error_pending;
2109 sqlite3_io_error_pending = -1;
2110}
2111void enable_simulated_io_errors(void){
2112 sqlite3_io_error_pending = saved_cnt;
2113}
2114#else
drh152410f2005-11-05 15:11:22 +00002115# define disable_simulated_io_errors()
2116# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002117#endif
2118
2119/*
drh90f5ecb2004-07-22 01:19:35 +00002120** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002121** that pDest points to.
2122**
2123** No error checking is done. The rational for this is that this function
2124** may be called even if the file does not exist or contain a header. In
2125** these cases sqlite3OsRead() will return an error, to which the correct
2126** response is to zero the memory at pDest and continue. A real IO error
2127** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002128*/
danielk19773b8a05f2007-03-19 17:44:26 +00002129int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002130 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002131 memset(pDest, 0, N);
danielk1977b3175382008-10-17 18:51:52 +00002132 assert(pPager->fd->pMethods||pPager->tempFile);
danielk19777a2b1ee2007-08-21 14:27:01 +00002133 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002134 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002135 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002136 if( rc==SQLITE_IOERR_SHORT_READ ){
2137 rc = SQLITE_OK;
2138 }
drh90f5ecb2004-07-22 01:19:35 +00002139 }
drh551b7732006-11-06 21:20:25 +00002140 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002141}
2142
2143/*
drh5e00f6c2001-09-13 13:46:56 +00002144** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002145** pPager.
2146**
2147** If the PENDING_BYTE lies on the page directly after the end of the
2148** file, then consider this page part of the file too. For example, if
2149** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2150** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002151*/
danielk1977ad0132d2008-06-07 08:58:22 +00002152int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk19777a2b1ee2007-08-21 14:27:01 +00002153 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002154 int rc;
drhd9b02572001-04-15 00:37:09 +00002155 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002156 if( pPager->errCode ){
danielk19778c0a7912008-08-20 14:49:23 +00002157 rc = pPager->errCode;
danielk19778c0a7912008-08-20 14:49:23 +00002158 return rc;
drha7aea3d2007-03-15 12:51:16 +00002159 }
danielk1977d92db532008-11-17 04:56:24 +00002160 if( pPager->dbSizeValid ){
danielk197715f411d2005-09-16 10:18:45 +00002161 n = pPager->dbSize;
2162 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002163 assert(pPager->fd->pMethods||pPager->tempFile);
2164 if( (pPager->fd->pMethods)
2165 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002166 pager_error(pPager, rc);
danielk1977ad0132d2008-06-07 08:58:22 +00002167 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002168 }
2169 if( n>0 && n<pPager->pageSize ){
2170 n = 1;
2171 }else{
2172 n /= pPager->pageSize;
2173 }
2174 if( pPager->state!=PAGER_UNLOCK ){
drh4f21c4a2008-12-10 22:15:00 +00002175 pPager->dbSize = (int)n;
danielk1977d92db532008-11-17 04:56:24 +00002176 pPager->dbSizeValid = 1;
danielk197715f411d2005-09-16 10:18:45 +00002177 }
drhed7c8552001-04-11 14:29:21 +00002178 }
danielk197715f411d2005-09-16 10:18:45 +00002179 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002180 n++;
2181 }
drhf8e632b2007-05-08 14:51:36 +00002182 if( n>pPager->mxPgno ){
drh4f21c4a2008-12-10 22:15:00 +00002183 pPager->mxPgno = (Pgno)n;
drhf8e632b2007-05-08 14:51:36 +00002184 }
danielk1977ad0132d2008-06-07 08:58:22 +00002185 if( pnPage ){
drh4f21c4a2008-12-10 22:15:00 +00002186 *pnPage = (int)n;
danielk1977ad0132d2008-06-07 08:58:22 +00002187 }
2188 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002189}
2190
2191/*
drhf7c57532003-04-25 13:22:51 +00002192** Forward declaration
2193*/
danielk197776572402004-06-25 02:38:54 +00002194static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002195
2196/*
danielk1977e180dd92007-04-05 17:15:52 +00002197** This routine is used to truncate the cache when a database
2198** is truncated. Drop from the cache all pages whose pgno is
2199** larger than pPager->dbSize and is unreferenced.
2200**
drhac69b052004-05-12 13:30:07 +00002201** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002202**
2203** Actually, at the point this routine is called, it would be
2204** an error to have a referenced page. But rather than delete
2205** that page and guarantee a subsequent segfault, it seems better
2206** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002207*/
danielk1977e180dd92007-04-05 17:15:52 +00002208static void pager_truncate_cache(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00002209 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
drhac69b052004-05-12 13:30:07 +00002210}
2211
drhf7c57532003-04-25 13:22:51 +00002212/*
danielk197717221812005-02-15 03:38:05 +00002213** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002214** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002215** false or until the lock succeeds.
2216**
2217** Return SQLITE_OK on success and an error code if we cannot obtain
2218** the lock.
2219*/
2220static int pager_wait_on_lock(Pager *pPager, int locktype){
2221 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002222
2223 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002224 assert( PAGER_SHARED==SHARED_LOCK );
2225 assert( PAGER_RESERVED==RESERVED_LOCK );
2226 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002227
2228 /* If the file is currently unlocked then the size must be unknown */
danielk1977d92db532008-11-17 04:56:24 +00002229 assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
drh1aa2d8b2007-01-03 15:34:29 +00002230
danielk197717221812005-02-15 03:38:05 +00002231 if( pPager->state>=locktype ){
2232 rc = SQLITE_OK;
2233 }else{
danielk197717221812005-02-15 03:38:05 +00002234 do {
drh054889e2005-11-30 03:20:31 +00002235 rc = sqlite3OsLock(pPager->fd, locktype);
danielk19771ceedd32008-11-19 10:22:33 +00002236 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
danielk197717221812005-02-15 03:38:05 +00002237 if( rc==SQLITE_OK ){
drh4f21c4a2008-12-10 22:15:00 +00002238 pPager->state = (u8)locktype;
drhb0603412007-02-28 04:47:26 +00002239 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002240 }
2241 }
2242 return rc;
2243}
2244
2245/*
drhf7c57532003-04-25 13:22:51 +00002246** Truncate the file to the number of pages specified.
2247*/
danielk19773b8a05f2007-03-19 17:44:26 +00002248int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
danielk19778c0a7912008-08-20 14:49:23 +00002249 int rc = SQLITE_OK;
danielk1977b3175382008-10-17 18:51:52 +00002250 assert( pPager->state>=PAGER_SHARED );
danielk19778c0a7912008-08-20 14:49:23 +00002251
danielk1977ad0132d2008-06-07 08:58:22 +00002252 sqlite3PagerPagecount(pPager, 0);
danielk1977efaaf572006-01-16 11:29:19 +00002253 if( pPager->errCode ){
2254 rc = pPager->errCode;
danielk1977d92db532008-11-17 04:56:24 +00002255 }else if( nPage<pPager->dbSize ){
danielk1977b3175382008-10-17 18:51:52 +00002256 rc = syncJournal(pPager);
2257 if( rc==SQLITE_OK ){
2258 /* Get an exclusive lock on the database before truncating. */
2259 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2260 }
2261 if( rc==SQLITE_OK ){
2262 rc = pager_truncate(pPager, nPage);
danielk19778c0a7912008-08-20 14:49:23 +00002263 }
danielk1977369f27e2004-06-15 11:40:04 +00002264 }
danielk197717221812005-02-15 03:38:05 +00002265
drhf7c57532003-04-25 13:22:51 +00002266 return rc;
2267}
2268
2269/*
drhed7c8552001-04-11 14:29:21 +00002270** Shutdown the page cache. Free all memory and close all files.
2271**
2272** If a transaction was in progress when this routine is called, that
2273** transaction is rolled back. All outstanding pages are invalidated
2274** and their memory is freed. Any attempt to use a page associated
2275** with this page cache after this function returns will likely
2276** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002277**
2278** This function always succeeds. If a transaction is active an attempt
2279** is made to roll it back. If an error occurs during the rollback
2280** a hot journal may be left in the filesystem but no error is returned
2281** to the caller.
drhed7c8552001-04-11 14:29:21 +00002282*/
danielk19773b8a05f2007-03-19 17:44:26 +00002283int sqlite3PagerClose(Pager *pPager){
danielk197713f72992005-12-18 08:51:22 +00002284
drhbafda092007-01-03 23:36:22 +00002285 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002286 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002287 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002288 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002289 pager_reset(pPager);
danielk1977b3175382008-10-17 18:51:52 +00002290 if( !MEMDB ){
2291 pagerUnlockAndRollback(pPager);
2292 }
drhbafda092007-01-03 23:36:22 +00002293 enable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002294 sqlite3EndBenignMalloc();
drh4f0c5872007-03-26 22:05:01 +00002295 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002296 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002297 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002298 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002299 }
drhf5e7bb52008-02-18 14:47:33 +00002300 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977a1fa00d2008-08-27 15:16:33 +00002301 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
danielk1977fd7f0452008-12-17 17:30:26 +00002302 releaseAllSavepoint(pPager);
danielk1977b4b47412007-08-17 15:53:36 +00002303 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002304 /* Temp files are automatically deleted by the OS
2305 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002306 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002307 ** }
2308 */
danielk1977aca790a2005-01-13 11:07:52 +00002309
drhfacf0302008-06-17 15:12:00 +00002310 sqlite3PageFree(pPager->pTmpSpace);
danielk19778c0a7912008-08-20 14:49:23 +00002311 sqlite3PcacheClose(pPager->pPCache);
drh17435752007-08-16 04:30:38 +00002312 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002313 return SQLITE_OK;
2314}
2315
drh87cc3b32007-05-08 21:45:27 +00002316#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002317/*
drh5e00f6c2001-09-13 13:46:56 +00002318** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002319*/
danielk19773b8a05f2007-03-19 17:44:26 +00002320Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002321 return p->pgno;
2322}
drh87cc3b32007-05-08 21:45:27 +00002323#endif
drhed7c8552001-04-11 14:29:21 +00002324
2325/*
drhdf0b3b02001-06-23 11:36:20 +00002326** Increment the reference count for a page. The input pointer is
2327** a reference to the page data.
2328*/
danielk19773b8a05f2007-03-19 17:44:26 +00002329int sqlite3PagerRef(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002330 sqlite3PcacheRef(pPg);
drh8c42ca92001-06-22 19:15:00 +00002331 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002332}
2333
2334/*
drh34e79ce2004-02-08 06:05:46 +00002335** Sync the journal. In other words, make sure all the pages that have
2336** been written to the journal have actually reached the surface of the
2337** disk. It is not safe to modify the original database file until after
2338** the journal has been synced. If the original database is modified before
2339** the journal is synced and a power failure occurs, the unsynced journal
2340** data would be lost and we would be unable to completely rollback the
2341** database changes. Database corruption would occur.
2342**
2343** This routine also updates the nRec field in the header of the journal.
2344** (See comments on the pager_playback() routine for additional information.)
2345** If the sync mode is FULL, two syncs will occur. First the whole journal
2346** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002347**
drh34e79ce2004-02-08 06:05:46 +00002348** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002349** after a power failure, so no sync occurs.
2350**
2351** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2352** the database is stored, then OsSync() is never called on the journal
2353** file. In this case all that is required is to update the nRec field in
2354** the journal header.
drhfa86c412002-02-02 15:01:15 +00002355**
drh34e79ce2004-02-08 06:05:46 +00002356** This routine clears the needSync field of every page current held in
2357** memory.
drh50e5dad2001-09-15 00:57:28 +00002358*/
danielk197776572402004-06-25 02:38:54 +00002359static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002360 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002361
2362 /* Sync the journal before modifying the main database
2363 ** (assuming there is a journal and it needs to be synced.)
2364 */
danielk197776572402004-06-25 02:38:54 +00002365 if( pPager->needSync ){
danielk1977b3175382008-10-17 18:51:52 +00002366 assert( !pPager->tempFile );
2367 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
danielk19774cd2cd52007-08-23 14:48:23 +00002368 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002369 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002370
danielk19774cd2cd52007-08-23 14:48:23 +00002371 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002372 /* Write the nRec value into the journal file header. If in
2373 ** full-synchronous mode, sync the journal first. This ensures that
2374 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002375 ** it as a candidate for rollback.
2376 **
2377 ** This is not required if the persistent media supports the
2378 ** SAFE_APPEND property. Because in this case it is not possible
2379 ** for garbage data to be appended to the file, the nRec field
2380 ** is populated with 0xFFFFFFFF when the journal header is written
2381 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002382 */
danielk19774cd2cd52007-08-23 14:48:23 +00002383 i64 jrnlOff;
2384 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002385 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002386 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002387 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002388 if( rc!=0 ) return rc;
2389 }
danielk197713adf8a2004-06-03 16:08:41 +00002390
danielk197762079062007-08-15 17:08:46 +00002391 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2392 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2393 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002394 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002395 }
danielk19774cd2cd52007-08-23 14:48:23 +00002396 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2397 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2398 IOTRACE(("JSYNC %p\n", pPager))
2399 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2400 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2401 );
2402 if( rc!=0 ) return rc;
2403 }
drhdb48ee02003-01-16 13:42:43 +00002404 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002405 }
drh50e5dad2001-09-15 00:57:28 +00002406 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002407
2408 /* Erase the needSync flag from every page.
2409 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00002410 sqlite3PcacheClearSyncFlags(pPager->pPCache);
drh50e5dad2001-09-15 00:57:28 +00002411 }
drh03eb96a2002-11-10 23:32:56 +00002412
drh81a20f22001-10-12 17:30:04 +00002413 return rc;
drh50e5dad2001-09-15 00:57:28 +00002414}
2415
2416/*
drh2554f8b2003-01-22 01:26:44 +00002417** Given a list of pages (connected by the PgHdr.pDirty pointer) write
danielk1977a858aa22008-08-22 16:22:17 +00002418** every one of those pages out to the database file. No calls are made
2419** to the page-cache to mark the pages as clean. It is the responsibility
2420** of the caller to use PcacheCleanAll() or PcacheMakeClean() to mark
2421** the pages as clean.
drh2554f8b2003-01-22 01:26:44 +00002422*/
2423static int pager_write_pagelist(PgHdr *pList){
2424 Pager *pPager;
2425 int rc;
2426
2427 if( pList==0 ) return SQLITE_OK;
2428 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002429
2430 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2431 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002432 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002433 **
drha6abd042004-06-09 17:37:22 +00002434 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2435 ** through an intermediate state PENDING. A PENDING lock prevents new
2436 ** readers from attaching to the database but is unsufficient for us to
2437 ** write. The idea of a PENDING lock is to prevent new readers from
2438 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002439 **
drha6abd042004-06-09 17:37:22 +00002440 ** While the pager is in the RESERVED state, the original database file
2441 ** is unchanged and we can rollback without having to playback the
2442 ** journal into the original database file. Once we transition to
2443 ** EXCLUSIVE, it means the database file has been changed and any rollback
2444 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002445 */
drh684917c2004-10-05 02:41:42 +00002446 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002447 if( rc!=SQLITE_OK ){
2448 return rc;
2449 }
2450
drh2554f8b2003-01-22 01:26:44 +00002451 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002452
2453 /* If the file has not yet been opened, open it now. */
2454 if( !pPager->fd->pMethods ){
2455 assert(pPager->tempFile);
danielk197717b90b52008-06-06 11:11:25 +00002456 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002457 if( rc ) return rc;
2458 }
2459
danielk1977687566d2004-11-02 12:56:41 +00002460 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002461 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002462 ** make the file smaller (presumably by auto-vacuum code). Do not write
2463 ** any such pages to the file.
2464 */
danielk197733e32162008-08-23 18:53:08 +00002465 if( pList->pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
danielk197762079062007-08-15 17:08:46 +00002466 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002467 char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6);
danielk197712dd5492008-12-18 15:45:07 +00002468
drh477731b2007-06-16 03:06:27 +00002469 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2470 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002471 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002472 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002473 PAGER_INCR(sqlite3_pager_writedb_count);
2474 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002475 if( pList->pgno==1 ){
2476 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2477 }
danielk1977687566d2004-11-02 12:56:41 +00002478 }
2479#ifndef NDEBUG
2480 else{
drh4f0c5872007-03-26 22:05:01 +00002481 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002482 }
2483#endif
drh2554f8b2003-01-22 01:26:44 +00002484 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002485#ifdef SQLITE_CHECK_PAGES
2486 pList->pageHash = pager_pagehash(pList);
2487#endif
drh2554f8b2003-01-22 01:26:44 +00002488 pList = pList->pDirty;
2489 }
danielk19778c0a7912008-08-20 14:49:23 +00002490
drh2554f8b2003-01-22 01:26:44 +00002491 return SQLITE_OK;
2492}
2493
2494/*
danielk19778c0a7912008-08-20 14:49:23 +00002495** This function is called by the pcache layer when it has reached some
2496** soft memory limit. The argument is a pointer to a purgeable Pager
2497** object. This function attempts to make a single dirty page that has no
2498** outstanding references (if one exists) clean so that it can be recycled
2499** by the pcache layer.
drh2554f8b2003-01-22 01:26:44 +00002500*/
danielk1977a858aa22008-08-22 16:22:17 +00002501static int pagerStress(void *p, PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002502 Pager *pPager = (Pager *)p;
danielk19778c0a7912008-08-20 14:49:23 +00002503 int rc = SQLITE_OK;
drh8f2e9a12008-02-26 14:46:04 +00002504
danielk19778c200142008-08-25 07:12:28 +00002505 if( pPager->doNotSync ){
2506 return SQLITE_OK;
2507 }
2508
danielk1977a858aa22008-08-22 16:22:17 +00002509 assert( pPg->flags&PGHDR_DIRTY );
2510 if( pPager->errCode==SQLITE_OK ){
2511 if( pPg->flags&PGHDR_NEED_SYNC ){
2512 rc = syncJournal(pPager);
2513 if( rc==SQLITE_OK && pPager->fullSync &&
danielk1977b3175382008-10-17 18:51:52 +00002514 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
danielk1977a858aa22008-08-22 16:22:17 +00002515 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
2516 ){
2517 pPager->nRec = 0;
2518 rc = writeJournalHdr(pPager);
danielk19778c0a7912008-08-20 14:49:23 +00002519 }
danielk19778c0a7912008-08-20 14:49:23 +00002520 }
danielk1977a858aa22008-08-22 16:22:17 +00002521 if( rc==SQLITE_OK ){
2522 pPg->pDirty = 0;
2523 rc = pager_write_pagelist(pPg);
2524 }
2525 if( rc!=SQLITE_OK ){
2526 pager_error(pPager, rc);
2527 }
2528 }
2529
2530 if( rc==SQLITE_OK ){
2531 sqlite3PcacheMakeClean(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00002532 }
2533 return rc;
drh2554f8b2003-01-22 01:26:44 +00002534}
2535
danielk19778c0a7912008-08-20 14:49:23 +00002536
drh2554f8b2003-01-22 01:26:44 +00002537/*
drh19db9352008-03-27 22:42:51 +00002538** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00002539** A hot journal is one that needs to be played back.
2540**
2541** If the current size of the database file is 0 but a journal file
2542** exists, that is probably an old journal left over from a prior
2543** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00002544**
2545** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00002546**
2547** This routine does not open the journal file to examine its
2548** content. Hence, the journal might contain the name of a master
2549** journal file that has been deleted, and hence not be hot. Or
2550** the header of the journal might be zeroed out. This routine
2551** does not discover these cases of a non-hot journal - if the
2552** journal file exists and is not empty this routine assumes it
2553** is hot. The pager_playback() routine will discover that the
2554** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00002555*/
danielk1977d300b8a2008-08-01 10:50:23 +00002556static int hasHotJournal(Pager *pPager, int *pExists){
danielk1977b4b47412007-08-17 15:53:36 +00002557 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977d300b8a2008-08-01 10:50:23 +00002558 int rc = SQLITE_OK;
drhea678832008-12-10 19:26:22 +00002559 int exists = 0;
2560 int locked = 0;
drh0a846f92008-08-25 17:23:29 +00002561 assert( pPager!=0 );
2562 assert( pPager->useJournal );
2563 assert( pPager->fd->pMethods );
danielk1977d300b8a2008-08-01 10:50:23 +00002564 *pExists = 0;
drh0a846f92008-08-25 17:23:29 +00002565 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
2566 if( rc==SQLITE_OK && exists ){
2567 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
2568 }
2569 if( rc==SQLITE_OK && exists && !locked ){
2570 int nPage;
2571 rc = sqlite3PagerPagecount(pPager, &nPage);
2572 if( rc==SQLITE_OK ){
2573 if( nPage==0 ){
2574 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2575 }else{
2576 *pExists = 1;
danielk1977861f7452008-06-05 11:39:11 +00002577 }
2578 }
drhbb5f18d2007-04-06 18:23:17 +00002579 }
danielk1977d300b8a2008-08-01 10:50:23 +00002580 return rc;
drh165ffe92005-03-15 17:09:30 +00002581}
2582
2583/*
danielk1977e180dd92007-04-05 17:15:52 +00002584** Read the content of page pPg out of the database file.
2585*/
2586static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2587 int rc;
danielk197762079062007-08-15 17:08:46 +00002588 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002589 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002590 assert(pPager->fd->pMethods||pPager->tempFile);
2591 if( !pPager->fd->pMethods ){
2592 return SQLITE_IOERR_SHORT_READ;
2593 }
danielk197762079062007-08-15 17:08:46 +00002594 offset = (pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002595 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002596 PAGER_INCR(sqlite3_pager_readdb_count);
2597 PAGER_INCR(pPager->nRead);
2598 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002599 if( pgno==1 ){
danielk19778c0a7912008-08-20 14:49:23 +00002600 memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24],
drh86a88112007-04-16 15:02:19 +00002601 sizeof(pPager->dbFileVers));
2602 }
danielk197730849522008-08-21 04:35:18 +00002603 CODEC1(pPager, pPg->pData, pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00002604 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
2605 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00002606 return rc;
2607}
2608
2609
2610/*
danielk1977e277be02007-03-23 18:12:06 +00002611** This function is called to obtain the shared lock required before
2612** data may be read from the pager cache. If the shared lock has already
2613** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002614**
2615** Immediately after obtaining the shared lock (if required), this function
2616** checks for a hot-journal file. If one is found, an emergency rollback
2617** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002618*/
2619static int pagerSharedLock(Pager *pPager){
2620 int rc = SQLITE_OK;
danielk1977d300b8a2008-08-01 10:50:23 +00002621 int isErrorReset = 0;
danielk1977e277be02007-03-23 18:12:06 +00002622
danielk1977ae72d982007-10-03 08:46:44 +00002623 /* If this database is opened for exclusive access, has no outstanding
2624 ** page references and is in an error-state, now is the chance to clear
2625 ** the error. Discard the contents of the pager-cache and treat any
2626 ** open journal file as a hot-journal.
2627 */
danielk19778c0a7912008-08-20 14:49:23 +00002628 if( !MEMDB && pPager->exclusiveMode
2629 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
2630 ){
danielk1977ae72d982007-10-03 08:46:44 +00002631 if( pPager->journalOpen ){
danielk1977d300b8a2008-08-01 10:50:23 +00002632 isErrorReset = 1;
danielk1977ae72d982007-10-03 08:46:44 +00002633 }
danielk1977ae72d982007-10-03 08:46:44 +00002634 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00002635 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00002636 }
2637
2638 /* If the pager is still in an error state, do not proceed. The error
2639 ** state will be cleared at some point in the future when all page
2640 ** references are dropped and the cache can be discarded.
2641 */
2642 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
2643 return pPager->errCode;
2644 }
2645
danielk1977d300b8a2008-08-01 10:50:23 +00002646 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
danielk1977b4b47412007-08-17 15:53:36 +00002647 sqlite3_vfs *pVfs = pPager->pVfs;
drh4f21c4a2008-12-10 22:15:00 +00002648 int isHotJournal = 0;
shane049fc212008-10-22 16:26:47 +00002649 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00002650 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
2651 if( !pPager->noReadlock ){
2652 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2653 if( rc!=SQLITE_OK ){
2654 assert( pPager->state==PAGER_UNLOCK );
2655 return pager_error(pPager, rc);
danielk1977e277be02007-03-23 18:12:06 +00002656 }
danielk1977b3175382008-10-17 18:51:52 +00002657 assert( pPager->state>=SHARED_LOCK );
2658 }
2659
2660 /* If a journal file exists, and there is no RESERVED lock on the
2661 ** database file, then it either needs to be played back or deleted.
2662 */
2663 if( !isErrorReset ){
2664 rc = hasHotJournal(pPager, &isHotJournal);
2665 if( rc!=SQLITE_OK ){
2666 goto failed;
2667 }
2668 }
2669 if( isErrorReset || isHotJournal ){
2670 /* Get an EXCLUSIVE lock on the database file. At this point it is
2671 ** important that a RESERVED lock is not obtained on the way to the
2672 ** EXCLUSIVE lock. If it were, another process might open the
2673 ** database file, detect the RESERVED lock, and conclude that the
2674 ** database is safe to read while this process is still rolling it
2675 ** back.
2676 **
2677 ** Because the intermediate RESERVED lock is not requested, the
2678 ** second process will get to this point in the code and fail to
2679 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00002680 */
danielk1977b3175382008-10-17 18:51:52 +00002681 if( pPager->state<EXCLUSIVE_LOCK ){
2682 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
danielk1977e277be02007-03-23 18:12:06 +00002683 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002684 rc = pager_error(pPager, rc);
2685 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002686 }
danielk1977b3175382008-10-17 18:51:52 +00002687 pPager->state = PAGER_EXCLUSIVE;
2688 }
2689
2690 /* Open the journal for read/write access. This is because in
2691 ** exclusive-access mode the file descriptor will be kept open and
2692 ** possibly used for a transaction later on. On some systems, the
2693 ** OsTruncate() call used in exclusive-access mode also requires
2694 ** a read/write file handle.
2695 */
2696 if( !isErrorReset && pPager->journalOpen==0 ){
2697 int res;
2698 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
2699 if( rc==SQLITE_OK ){
2700 if( res ){
2701 int fout = 0;
2702 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
2703 assert( !pPager->tempFile );
2704 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
2705 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk1977281d8bd2008-10-29 07:01:56 +00002706 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
2707 rc = SQLITE_CANTOPEN;
danielk1977b3175382008-10-17 18:51:52 +00002708 sqlite3OsClose(pPager->jfd);
2709 }
2710 }else{
2711 /* If the journal does not exist, that means some other process
2712 ** has already rolled it back */
2713 rc = SQLITE_BUSY;
2714 }
2715 }
2716 }
2717 if( rc!=SQLITE_OK ){
danielk1977b3175382008-10-17 18:51:52 +00002718 goto failed;
2719 }
2720 pPager->journalOpen = 1;
2721 pPager->journalStarted = 0;
2722 pPager->journalOff = 0;
2723 pPager->setMaster = 0;
2724 pPager->journalHdr = 0;
2725
2726 /* Playback and delete the journal. Drop the database write
2727 ** lock and reacquire the read lock.
2728 */
2729 rc = pager_playback(pPager, 1);
2730 if( rc!=SQLITE_OK ){
2731 rc = pager_error(pPager, rc);
2732 goto failed;
2733 }
2734 assert(pPager->state==PAGER_SHARED ||
2735 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
2736 );
2737 }
2738
2739 if( sqlite3PcachePagecount(pPager->pPCache)>0 ){
2740 /* The shared-lock has just been acquired on the database file
2741 ** and there are already pages in the cache (from a previous
2742 ** read or write transaction). Check to see if the database
2743 ** has been modified. If the database has changed, flush the
2744 ** cache.
2745 **
2746 ** Database changes is detected by looking at 15 bytes beginning
2747 ** at offset 24 into the file. The first 4 of these 16 bytes are
2748 ** a 32-bit counter that is incremented with each change. The
2749 ** other bytes change randomly with each file change when
2750 ** a codec is in use.
2751 **
2752 ** There is a vanishingly small chance that a change will not be
2753 ** detected. The chance of an undetected change is so small that
2754 ** it can be neglected.
2755 */
2756 char dbFileVers[sizeof(pPager->dbFileVers)];
2757 sqlite3PagerPagecount(pPager, 0);
2758
2759 if( pPager->errCode ){
2760 rc = pPager->errCode;
2761 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002762 }
2763
danielk1977d92db532008-11-17 04:56:24 +00002764 assert( pPager->dbSizeValid );
danielk1977b3175382008-10-17 18:51:52 +00002765 if( pPager->dbSize>0 ){
2766 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
2767 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
2768 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002769 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002770 }
danielk1977b3175382008-10-17 18:51:52 +00002771 }else{
2772 memset(dbFileVers, 0, sizeof(dbFileVers));
2773 }
danielk1977e277be02007-03-23 18:12:06 +00002774
danielk1977b3175382008-10-17 18:51:52 +00002775 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
2776 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002777 }
2778 }
danielk1977c5859712007-03-26 12:26:27 +00002779 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
2780 if( pPager->state==PAGER_UNLOCK ){
2781 pPager->state = PAGER_SHARED;
2782 }
danielk1977e277be02007-03-23 18:12:06 +00002783 }
2784
danielk197752b472a2008-05-05 16:23:55 +00002785 failed:
2786 if( rc!=SQLITE_OK ){
2787 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
2788 pager_unlock(pPager);
2789 }
danielk1977e277be02007-03-23 18:12:06 +00002790 return rc;
2791}
2792
2793/*
drhd33d5a82007-04-26 12:11:28 +00002794** Make sure we have the content for a page. If the page was
2795** previously acquired with noContent==1, then the content was
2796** just initialized to zeros instead of being read from disk.
2797** But now we need the real data off of disk. So make sure we
2798** have it. Read it in if we do not have it already.
2799*/
2800static int pager_get_content(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002801 if( pPg->flags&PGHDR_NEED_READ ){
drhd33d5a82007-04-26 12:11:28 +00002802 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
2803 if( rc==SQLITE_OK ){
danielk19778c0a7912008-08-20 14:49:23 +00002804 pPg->flags &= ~PGHDR_NEED_READ;
drhd33d5a82007-04-26 12:11:28 +00002805 }else{
2806 return rc;
2807 }
2808 }
2809 return SQLITE_OK;
2810}
2811
2812/*
danielk19778c0a7912008-08-20 14:49:23 +00002813** If the reference count has reached zero, and the pager is not in the
2814** middle of a write transaction or opened in exclusive mode, unlock it.
2815*/
2816static void pagerUnlockIfUnused(Pager *pPager){
2817 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
2818 && (!pPager->exclusiveMode || pPager->journalOff>0)
2819 ){
2820 pagerUnlockAndRollback(pPager);
2821 }
2822}
2823
2824/*
2825** Drop a page from the cache using sqlite3PcacheDrop().
2826**
2827** If this means there are now no pages with references to them, a rollback
2828** occurs and the lock on the database is removed.
2829*/
2830static void pagerDropPage(DbPage *pPg){
2831 Pager *pPager = pPg->pPager;
2832 sqlite3PcacheDrop(pPg);
2833 pagerUnlockIfUnused(pPager);
2834}
2835
2836/*
drhd9b02572001-04-15 00:37:09 +00002837** Acquire a page.
2838**
drh58a11682001-11-10 13:51:08 +00002839** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00002840** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00002841**
drhd33d5a82007-04-26 12:11:28 +00002842** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00002843** file is smaller than the requested page, then no actual disk
2844** read occurs and the memory image of the page is initialized to
2845** all zeros. The extra data appended to a page is always initialized
2846** to zeros the first time a page is loaded into memory.
2847**
drhd9b02572001-04-15 00:37:09 +00002848** The acquisition might fail for several reasons. In all cases,
2849** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00002850**
drhd33d5a82007-04-26 12:11:28 +00002851** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00002852** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00002853** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00002854** just returns 0. This routine acquires a read-lock the first time it
2855** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00002856** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00002857** or journal files.
drh0787db62007-03-04 13:15:27 +00002858**
drh538f5702007-04-13 02:14:30 +00002859** If noContent is false, the page contents are actually read from disk.
2860** If noContent is true, it means that we do not care about the contents
2861** of the page at this time, so do not do a disk read. Just fill in the
2862** page content with zeros. But mark the fact that we have not read the
2863** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00002864** sqlite3PagerWrite() is called on this page or if this routine is
2865** called again with noContent==0, that means that the content is needed
2866** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00002867*/
danielk197765e0ff32008-09-19 09:14:43 +00002868int sqlite3PagerAcquire(
drh538f5702007-04-13 02:14:30 +00002869 Pager *pPager, /* The pager open on the database file */
2870 Pgno pgno, /* Page number to fetch */
2871 DbPage **ppPage, /* Write a pointer to the page here */
2872 int noContent /* Do not bother reading content from disk if true */
2873){
danielk19778c0a7912008-08-20 14:49:23 +00002874 PgHdr *pPg = 0;
drh165ffe92005-03-15 17:09:30 +00002875 int rc;
drhed7c8552001-04-11 14:29:21 +00002876
danielk19778c0a7912008-08-20 14:49:23 +00002877 assert( pPager->state==PAGER_UNLOCK
2878 || sqlite3PcacheRefCount(pPager->pPCache)>0
danielk1977b3175382008-10-17 18:51:52 +00002879 || pgno==1
danielk19778c0a7912008-08-20 14:49:23 +00002880 );
danielk1977e277be02007-03-23 18:12:06 +00002881
danielk197726836652005-01-17 01:33:13 +00002882 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
2883 ** number greater than this, or zero, is requested.
2884 */
drh267cb322005-09-16 17:16:52 +00002885 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00002886 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00002887 }
2888
drhd9b02572001-04-15 00:37:09 +00002889 /* Make sure we have not hit any critical errors.
2890 */
drh836faa42003-01-11 13:30:57 +00002891 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00002892 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00002893
danielk197713adf8a2004-06-03 16:08:41 +00002894 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00002895 ** on the database file. pagerSharedLock() is a no-op if
2896 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00002897 */
danielk1977e277be02007-03-23 18:12:06 +00002898 rc = pagerSharedLock(pPager);
2899 if( rc!=SQLITE_OK ){
2900 return rc;
drhed7c8552001-04-11 14:29:21 +00002901 }
danielk1977e277be02007-03-23 18:12:06 +00002902 assert( pPager->state!=PAGER_UNLOCK );
2903
danielk19778c0a7912008-08-20 14:49:23 +00002904 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
2905 if( rc!=SQLITE_OK ){
2906 return rc;
2907 }
danielk19778c0a7912008-08-20 14:49:23 +00002908 if( pPg->pPager==0 ){
2909 /* The pager cache has created a new page. Its content needs to
2910 ** be initialized.
2911 */
danielk1977979f38e2007-03-27 16:19:51 +00002912 int nMax;
drh538f5702007-04-13 02:14:30 +00002913 PAGER_INCR(pPager->nMiss);
danielk19778c0a7912008-08-20 14:49:23 +00002914 pPg->pPager = pPager;
danielk19778c0a7912008-08-20 14:49:23 +00002915 memset(pPg->pExtra, 0, pPager->nExtra);
danielk197724168722007-04-02 05:07:47 +00002916
danielk1977ad0132d2008-06-07 08:58:22 +00002917 rc = sqlite3PagerPagecount(pPager, &nMax);
2918 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002919 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00002920 return rc;
2921 }
danielk197775bab7d2006-01-23 13:09:45 +00002922
danielk1977a1fa00d2008-08-27 15:16:33 +00002923 if( nMax<(int)pgno || MEMDB || noContent ){
drhf8e632b2007-05-08 14:51:36 +00002924 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00002925 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00002926 return SQLITE_FULL;
2927 }
danielk19778c0a7912008-08-20 14:49:23 +00002928 memset(pPg->pData, 0, pPager->pageSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00002929 if( noContent ){
danielk19778c0a7912008-08-20 14:49:23 +00002930 pPg->flags |= PGHDR_NEED_READ;
2931 }
drh538f5702007-04-13 02:14:30 +00002932 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00002933 }else{
danielk1977e180dd92007-04-05 17:15:52 +00002934 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00002935 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
danielk19778c0a7912008-08-20 14:49:23 +00002936 /* sqlite3PagerUnref(pPg); */
2937 pagerDropPage(pPg);
drh551b7732006-11-06 21:20:25 +00002938 return rc;
drh81a20f22001-10-12 17:30:04 +00002939 }
drh306dc212001-05-21 13:45:10 +00002940 }
danielk19773c407372005-02-15 02:54:14 +00002941#ifdef SQLITE_CHECK_PAGES
2942 pPg->pageHash = pager_pagehash(pPg);
2943#endif
drhed7c8552001-04-11 14:29:21 +00002944 }else{
drhd9b02572001-04-15 00:37:09 +00002945 /* The requested page is in the page cache. */
danielk19778c0a7912008-08-20 14:49:23 +00002946 assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00002947 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00002948 if( !noContent ){
2949 rc = pager_get_content(pPg);
2950 if( rc ){
danielk19778c0a7912008-08-20 14:49:23 +00002951 sqlite3PagerUnref(pPg);
drhd33d5a82007-04-26 12:11:28 +00002952 return rc;
2953 }
2954 }
drhed7c8552001-04-11 14:29:21 +00002955 }
danielk19778c0a7912008-08-20 14:49:23 +00002956
danielk19773b8a05f2007-03-19 17:44:26 +00002957 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00002958 return SQLITE_OK;
2959}
danielk19778c0a7912008-08-20 14:49:23 +00002960
drhed7c8552001-04-11 14:29:21 +00002961/*
drh7e3b0a02001-04-28 16:52:40 +00002962** Acquire a page if it is already in the in-memory cache. Do
2963** not read the page from disk. Return a pointer to the page,
2964** or 0 if the page is not in cache.
2965**
danielk19773b8a05f2007-03-19 17:44:26 +00002966** See also sqlite3PagerGet(). The difference between this routine
2967** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00002968** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00002969** returns NULL if the page is not in cache or if a disk I/O error
2970** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00002971*/
danielk19773b8a05f2007-03-19 17:44:26 +00002972DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00002973 PgHdr *pPg = 0;
drh836faa42003-01-11 13:30:57 +00002974 assert( pPager!=0 );
2975 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00002976
danielk19778c0a7912008-08-20 14:49:23 +00002977 if( (pPager->state!=PAGER_UNLOCK)
2978 && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL)
2979 ){
2980 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
danielk1977e277be02007-03-23 18:12:06 +00002981 }
danielk19778c0a7912008-08-20 14:49:23 +00002982
danielk19773b8a05f2007-03-19 17:44:26 +00002983 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00002984}
2985
2986/*
drhed7c8552001-04-11 14:29:21 +00002987** Release a page.
2988**
2989** If the number of references to the page drop to zero, then the
2990** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00002991** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00002992** removed.
2993*/
danielk19773b8a05f2007-03-19 17:44:26 +00002994int sqlite3PagerUnref(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002995 if( pPg ){
2996 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00002997 sqlite3PcacheRelease(pPg);
2998 pagerUnlockIfUnused(pPager);
drhed7c8552001-04-11 14:29:21 +00002999 }
drhd9b02572001-04-15 00:37:09 +00003000 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003001}
3002
danielk1977fd7f0452008-12-17 17:30:26 +00003003static int openSubJournal(Pager *pPager){
3004 int rc = SQLITE_OK;
3005 if( pPager->journalOpen && !pPager->sjfd->pMethods ){
3006 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3007 sqlite3MemJournalOpen(pPager->sjfd);
3008 }else{
3009 rc = sqlite3PagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
3010 }
3011 }
3012 return rc;
3013}
3014
drhed7c8552001-04-11 14:29:21 +00003015/*
drha6abd042004-06-09 17:37:22 +00003016** Create a journal file for pPager. There should already be a RESERVED
3017** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003018**
3019** Return SQLITE_OK if everything. Return an error code and release the
3020** write lock if anything goes wrong.
3021*/
3022static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003023 sqlite3_vfs *pVfs = pPager->pVfs;
3024 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3025
drhda47d772002-12-02 04:25:19 +00003026 int rc;
drha6abd042004-06-09 17:37:22 +00003027 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003028 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003029 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003030 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003031 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00003032 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003033 rc = SQLITE_NOMEM;
3034 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003035 }
danielk1977b4b47412007-08-17 15:53:36 +00003036
drhfdc40e92008-04-17 20:59:37 +00003037 if( pPager->journalOpen==0 ){
3038 if( pPager->tempFile ){
3039 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3040 }else{
3041 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003042 }
danielk1977b3175382008-10-17 18:51:52 +00003043 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3044 sqlite3MemJournalOpen(pPager->jfd);
3045 rc = SQLITE_OK;
3046 }else{
drhfdc40e92008-04-17 20:59:37 +00003047#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977b3175382008-10-17 18:51:52 +00003048 rc = sqlite3JournalOpen(
3049 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3050 );
drhfdc40e92008-04-17 20:59:37 +00003051#else
danielk1977b3175382008-10-17 18:51:52 +00003052 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
drhfdc40e92008-04-17 20:59:37 +00003053#endif
danielk1977b3175382008-10-17 18:51:52 +00003054 }
drhfdc40e92008-04-17 20:59:37 +00003055 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3056 pPager->journalOff = 0;
3057 pPager->setMaster = 0;
3058 pPager->journalHdr = 0;
3059 if( rc!=SQLITE_OK ){
3060 if( rc==SQLITE_NOMEM ){
3061 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3062 }
3063 goto failed_to_open_journal;
3064 }
drhda47d772002-12-02 04:25:19 +00003065 }
3066 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003067 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003068 pPager->needSync = 0;
drh968af522003-02-11 14:55:40 +00003069 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003070 if( pPager->errCode ){
3071 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003072 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003073 }
drhda47d772002-12-02 04:25:19 +00003074 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003075
danielk197776572402004-06-25 02:38:54 +00003076 rc = writeJournalHdr(pPager);
3077
danielk1977fd7f0452008-12-17 17:30:26 +00003078 if( pPager->nSavepoint && rc==SQLITE_OK ){
3079 rc = openSubJournal(pPager);
drhda47d772002-12-02 04:25:19 +00003080 }
danielk1977ae72d982007-10-03 08:46:44 +00003081 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003082 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003083 if( rc==SQLITE_OK ){
3084 rc = SQLITE_FULL;
3085 }
3086 }
drh9c105bb2004-10-02 20:38:28 +00003087 return rc;
3088
3089failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003090 sqlite3BitvecDestroy(pPager->pInJournal);
3091 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003092 return rc;
drhda47d772002-12-02 04:25:19 +00003093}
3094
3095/*
drh4b845d72002-03-05 12:41:19 +00003096** Acquire a write-lock on the database. The lock is removed when
3097** the any of the following happen:
3098**
drh80e35f42007-03-30 14:06:34 +00003099** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003100** * sqlite3PagerRollback() is called.
3101** * sqlite3PagerClose() is called.
3102** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003103**
danielk197713adf8a2004-06-03 16:08:41 +00003104** The first parameter to this routine is a pointer to any open page of the
3105** database file. Nothing changes about the page - it is used merely to
3106** acquire a pointer to the Pager structure and as proof that there is
3107** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003108**
danielk197713adf8a2004-06-03 16:08:41 +00003109** The second parameter indicates how much space in bytes to reserve for a
3110** master journal file-name at the start of the journal when it is created.
3111**
3112** A journal file is opened if this is not a temporary file. For temporary
3113** files, the opening of the journal file is deferred until there is an
3114** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003115**
drha6abd042004-06-09 17:37:22 +00003116** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003117**
3118** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3119** immediately instead of waiting until we try to flush the cache. The
3120** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003121*/
danielk19773b8a05f2007-03-19 17:44:26 +00003122int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003123 Pager *pPager = pPg->pPager;
3124 int rc = SQLITE_OK;
3125 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003126 assert( pPager->state!=PAGER_UNLOCK );
3127 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003128 assert( pPager->pInJournal==0 );
danielk1977b3175382008-10-17 18:51:52 +00003129 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00003130 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
3131 if( rc==SQLITE_OK ){
3132 pPager->state = PAGER_RESERVED;
3133 if( exFlag ){
3134 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh684917c2004-10-05 02:41:42 +00003135 }
danielk1977b3175382008-10-17 18:51:52 +00003136 }
3137 if( rc!=SQLITE_OK ){
3138 return rc;
3139 }
3140 pPager->dirtyCache = 0;
3141 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
3142 if( pPager->useJournal && !pPager->tempFile
3143 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
3144 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00003145 }
danielk1977334cdb62007-03-26 08:05:12 +00003146 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00003147 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00003148 ** time a (read or write) transaction was successfully concluded
3149 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00003150 ** kept open and either was truncated to 0 bytes or its header was
3151 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00003152 */
3153 assert( pPager->nRec==0 );
3154 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003155 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003156 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003157 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhf5e7bb52008-02-18 14:47:33 +00003158 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003159 rc = SQLITE_NOMEM;
3160 }else{
drh369339d2007-03-30 16:01:55 +00003161 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003162 rc = writeJournalHdr(pPager);
3163 }
drh4b845d72002-03-05 12:41:19 +00003164 }
danielk1977334cdb62007-03-26 08:05:12 +00003165 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003166 return rc;
3167}
3168
3169/*
drhed7c8552001-04-11 14:29:21 +00003170** Mark a data page as writeable. The page is written into the journal
3171** if it is not there already. This routine must be called before making
3172** changes to a page.
3173**
3174** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003175** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003176** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003177** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003178** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003179**
3180** If the journal file could not be written because the disk is full,
3181** then this routine returns SQLITE_FULL and does an immediate rollback.
3182** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003183** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003184** reset.
drhed7c8552001-04-11 14:29:21 +00003185*/
danielk19773b8a05f2007-03-19 17:44:26 +00003186static int pager_write(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003187 void *pData = pPg->pData;
drh69688d52001-04-14 16:38:23 +00003188 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003189 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003190
drh6446c4d2001-12-15 14:22:18 +00003191 /* Check for errors
3192 */
danielk1977efaaf572006-01-16 11:29:19 +00003193 if( pPager->errCode ){
3194 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003195 }
drh5e00f6c2001-09-13 13:46:56 +00003196 if( pPager->readOnly ){
3197 return SQLITE_PERM;
3198 }
drh6446c4d2001-12-15 14:22:18 +00003199
danielk197776572402004-06-25 02:38:54 +00003200 assert( !pPager->setMaster );
3201
danielk19773c407372005-02-15 02:54:14 +00003202 CHECK_PAGE(pPg);
3203
drh538f5702007-04-13 02:14:30 +00003204 /* If this page was previously acquired with noContent==1, that means
3205 ** we didn't really read in the content of the page. This can happen
3206 ** (for example) when the page is being moved to the freelist. But
3207 ** now we are (perhaps) moving the page off of the freelist for
3208 ** reuse and we need to know its original content so that content
3209 ** can be stored in the rollback journal. So do the read at this
3210 ** time.
3211 */
drhd33d5a82007-04-26 12:11:28 +00003212 rc = pager_get_content(pPg);
3213 if( rc ){
3214 return rc;
drh538f5702007-04-13 02:14:30 +00003215 }
3216
drh6446c4d2001-12-15 14:22:18 +00003217 /* Mark the page as dirty. If the page has already been written
3218 ** to the journal then we can return right away.
3219 */
drhc047b9f2008-11-21 03:23:43 +00003220 sqlite3PcacheMakeDirty(pPg);
danielk1977fd7f0452008-12-17 17:30:26 +00003221 if( pageInJournal(pPg) && pageInSavepoint(pPg) ){
drha6abd042004-06-09 17:37:22 +00003222 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003223 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003224 }else{
drh6446c4d2001-12-15 14:22:18 +00003225
danielk1977a0bf2652004-11-04 14:30:04 +00003226 /* If we get this far, it means that the page needs to be
3227 ** written to the transaction journal or the ckeckpoint journal
3228 ** or both.
3229 **
3230 ** First check to see that the transaction journal exists and
3231 ** create it if it does not.
3232 */
3233 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003234 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003235 if( rc!=SQLITE_OK ){
3236 return rc;
3237 }
3238 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00003239 if( !pPager->journalOpen && pPager->useJournal
3240 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00003241 rc = pager_open_journal(pPager);
3242 if( rc!=SQLITE_OK ) return rc;
3243 }
danielk1977a0bf2652004-11-04 14:30:04 +00003244 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003245 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003246
3247 /* The transaction journal now exists and we have a RESERVED or an
3248 ** EXCLUSIVE lock on the main database file. Write the current page to
3249 ** the transaction journal if it is not there already.
3250 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00003251 if( !pageInJournal(pPg) && pPager->journalOpen ){
danielk1977d92db532008-11-17 04:56:24 +00003252 if( pPg->pgno<=pPager->origDbSize ){
danielk1977b3175382008-10-17 18:51:52 +00003253 u32 cksum;
3254 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00003255
danielk1977b3175382008-10-17 18:51:52 +00003256 /* We should never write to the journal file the page that
3257 ** contains the database locks. The following assert verifies
3258 ** that we do not. */
3259 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
3260 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
3261 cksum = pager_cksum(pPager, (u8*)pData2);
3262 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
3263 if( rc==SQLITE_OK ){
3264 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
3265 pPager->journalOff + 4);
3266 pPager->journalOff += pPager->pageSize+4;
3267 }
3268 if( rc==SQLITE_OK ){
3269 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
3270 pPager->journalOff += 4;
3271 }
3272 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
3273 pPager->journalOff, pPager->pageSize));
3274 PAGER_INCR(sqlite3_pager_writej_count);
3275 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
3276 PAGERID(pPager), pPg->pgno,
3277 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00003278
danielk1977b3175382008-10-17 18:51:52 +00003279 /* An error has occured writing to the journal file. The
3280 ** transaction will be rolled back by the layer above.
3281 */
3282 if( rc!=SQLITE_OK ){
3283 return rc;
3284 }
danielk197707cb5602006-01-20 10:55:05 +00003285
danielk1977b3175382008-10-17 18:51:52 +00003286 pPager->nRec++;
3287 assert( pPager->pInJournal!=0 );
3288 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3289 if( !pPager->noSync ){
3290 pPg->flags |= PGHDR_NEED_SYNC;
3291 }
danielk1977fd7f0452008-12-17 17:30:26 +00003292 addToSavepointBitvecs(pPager, pPg->pgno);
danielk197713adf8a2004-06-03 16:08:41 +00003293 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003294 if( !pPager->journalStarted && !pPager->noSync ){
3295 pPg->flags |= PGHDR_NEED_SYNC;
3296 }
drh4f0c5872007-03-26 22:05:01 +00003297 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk19778c0a7912008-08-20 14:49:23 +00003298 PAGERID(pPager), pPg->pgno,
3299 ((pPg->flags&PGHDR_NEED_SYNC)?1:0));
danielk1977a0bf2652004-11-04 14:30:04 +00003300 }
danielk19778c0a7912008-08-20 14:49:23 +00003301 if( pPg->flags&PGHDR_NEED_SYNC ){
danielk1977a0bf2652004-11-04 14:30:04 +00003302 pPager->needSync = 1;
3303 }
danielk1977a0bf2652004-11-04 14:30:04 +00003304 }
3305
3306 /* If the statement journal is open and the page is not in it,
3307 ** then write the current page to the statement journal. Note that
3308 ** the statement journal format differs from the standard journal format
3309 ** in that it omits the checksums and the header.
3310 */
danielk1977fd7f0452008-12-17 17:30:26 +00003311 if( !pageInSavepoint(pPg) ){
danielk1977b3175382008-10-17 18:51:52 +00003312 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
3313 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
danielk1977d92db532008-11-17 04:56:24 +00003314 assert( pageInJournal(pPg) || pPg->pgno>pPager->origDbSize );
danielk1977fd7f0452008-12-17 17:30:26 +00003315 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
danielk1977b3175382008-10-17 18:51:52 +00003316 if( rc==SQLITE_OK ){
danielk1977fd7f0452008-12-17 17:30:26 +00003317 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
drhdb48ee02003-01-16 13:42:43 +00003318 }
danielk1977b3175382008-10-17 18:51:52 +00003319 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
3320 if( rc!=SQLITE_OK ){
3321 return rc;
3322 }
3323 pPager->stmtNRec++;
danielk1977fd7f0452008-12-17 17:30:26 +00003324 assert( pPager->nSavepoint>0 );
3325 addToSavepointBitvecs(pPager, pPg->pgno);
drhd9b02572001-04-15 00:37:09 +00003326 }
drhfa86c412002-02-02 15:01:15 +00003327 }
3328
3329 /* Update the database size and return.
3330 */
drh1aa2d8b2007-01-03 15:34:29 +00003331 assert( pPager->state>=PAGER_SHARED );
danielk1977d92db532008-11-17 04:56:24 +00003332 if( pPager->dbSize<pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003333 pPager->dbSize = pPg->pgno;
danielk1977d92db532008-11-17 04:56:24 +00003334 if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){
drh1f595712004-06-15 01:40:29 +00003335 pPager->dbSize++;
3336 }
drh306dc212001-05-21 13:45:10 +00003337 }
drh69688d52001-04-14 16:38:23 +00003338 return rc;
drhed7c8552001-04-11 14:29:21 +00003339}
3340
3341/*
danielk19774099f6e2007-03-19 11:25:20 +00003342** This function is used to mark a data-page as writable. It uses
3343** pager_write() to open a journal file (if it is not already open)
3344** and write the page *pData to the journal.
3345**
3346** The difference between this function and pager_write() is that this
3347** function also deals with the special case where 2 or more pages
3348** fit on a single disk sector. In this case all co-resident pages
3349** must have been written to the journal file before returning.
3350*/
danielk19773b8a05f2007-03-19 17:44:26 +00003351int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003352 int rc = SQLITE_OK;
3353
danielk19773b8a05f2007-03-19 17:44:26 +00003354 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003355 Pager *pPager = pPg->pPager;
3356 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3357
danielk1977b3175382008-10-17 18:51:52 +00003358 if( nPagePerSector>1 ){
danielk19774099f6e2007-03-19 11:25:20 +00003359 Pgno nPageCount; /* Total number of pages in database file */
3360 Pgno pg1; /* First page of the sector pPg is located on. */
3361 int nPage; /* Number of pages starting at pg1 to journal */
3362 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003363 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003364
3365 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3366 ** header to be written between the pages journaled by this function.
3367 */
danielk1977b3175382008-10-17 18:51:52 +00003368 assert( !MEMDB );
danielk19774099f6e2007-03-19 11:25:20 +00003369 assert( pPager->doNotSync==0 );
3370 pPager->doNotSync = 1;
3371
3372 /* This trick assumes that both the page-size and sector-size are
3373 ** an integer power of 2. It sets variable pg1 to the identifier
3374 ** of the first page of the sector pPg is located on.
3375 */
3376 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3377
danielk1977ad0132d2008-06-07 08:58:22 +00003378 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00003379 if( pPg->pgno>nPageCount ){
3380 nPage = (pPg->pgno - pg1)+1;
3381 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3382 nPage = nPageCount+1-pg1;
3383 }else{
3384 nPage = nPagePerSector;
3385 }
3386 assert(nPage>0);
3387 assert(pg1<=pPg->pgno);
3388 assert((pg1+nPage)>pPg->pgno);
3389
3390 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3391 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003392 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00003393 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00003394 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003395 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003396 if( rc==SQLITE_OK ){
3397 rc = pager_write(pPage);
danielk19778c0a7912008-08-20 14:49:23 +00003398 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003399 needSync = 1;
3400 }
danielk19773b8a05f2007-03-19 17:44:26 +00003401 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003402 }
3403 }
drhc81945e2008-03-10 14:12:53 +00003404 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk19778c0a7912008-08-20 14:49:23 +00003405 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003406 needSync = 1;
3407 }
danielk19778c0a7912008-08-20 14:49:23 +00003408 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003409 }
3410 }
3411
danielk1977dd97a492007-08-22 18:54:32 +00003412 /* If the PgHdr.needSync flag is set for any of the nPage pages
3413 ** starting at pg1, then it needs to be set for all of them. Because
3414 ** writing to any of these nPage pages may damage the others, the
3415 ** journal file must contain sync()ed copies of all of them
3416 ** before any of them can be written out to the database file.
3417 */
3418 if( needSync ){
drhb3df2e12008-09-17 20:06:26 +00003419 assert( !MEMDB && pPager->noSync==0 );
danielk1977dd97a492007-08-22 18:54:32 +00003420 for(ii=0; ii<nPage && needSync; ii++){
3421 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
danielk19778c0a7912008-08-20 14:49:23 +00003422 if( pPage ) pPage->flags |= PGHDR_NEED_SYNC;
3423 sqlite3PagerUnref(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00003424 }
3425 assert(pPager->needSync);
3426 }
3427
danielk19774099f6e2007-03-19 11:25:20 +00003428 assert( pPager->doNotSync==1 );
3429 pPager->doNotSync = 0;
3430 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003431 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003432 }
3433 return rc;
3434}
3435
3436/*
drhaacc5432002-01-06 17:07:40 +00003437** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003438** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003439** to change the content of the page.
3440*/
danielk19777d3a6662006-01-23 16:21:05 +00003441#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003442int sqlite3PagerIswriteable(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003443 return pPg->flags&PGHDR_DIRTY;
drh6019e162001-07-02 17:51:45 +00003444}
danielk19777d3a6662006-01-23 16:21:05 +00003445#endif
drh6019e162001-07-02 17:51:45 +00003446
drh001bbcb2003-03-19 03:14:00 +00003447/*
drh30e58752002-03-02 20:41:57 +00003448** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003449** write the information on page pPg back to the disk, even though
drhdfe88ec2008-11-03 20:55:06 +00003450** that page might be marked as dirty. This happens, for example, when
3451** the page has been added as a leaf of the freelist and so its
3452** content no longer matters.
drh30e58752002-03-02 20:41:57 +00003453**
3454** The overlying software layer calls this routine when all of the data
3455** on the given page is unused. The pager marks the page as clean so
3456** that it does not get written to disk.
3457**
3458** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003459** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003460** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003461**
3462** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003463** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003464** will thereafter be ignored. This is necessary to avoid a problem
3465** where a page with data is added to the freelist during one part of
3466** a transaction then removed from the freelist during a later part
3467** of the same transaction and reused for some other purpose. When it
3468** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003469** the sqlite3PagerDontRollback() routine is called. But because the
3470** page contains critical data, we still need to be sure it gets
3471** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003472*/
danielk1977a1fa00d2008-08-27 15:16:33 +00003473int sqlite3PagerDontWrite(DbPage *pDbPage){
drh538f5702007-04-13 02:14:30 +00003474 PgHdr *pPg = pDbPage;
3475 Pager *pPager = pPg->pPager;
danielk1977a1fa00d2008-08-27 15:16:33 +00003476 int rc;
drh8e298f92002-07-06 16:28:47 +00003477
danielk1977b3175382008-10-17 18:51:52 +00003478 if( pPg->pgno>pPager->origDbSize ){
danielk1977a1fa00d2008-08-27 15:16:33 +00003479 return SQLITE_OK;
3480 }
3481 if( pPager->pAlwaysRollback==0 ){
3482 assert( pPager->pInJournal );
3483 pPager->pAlwaysRollback = sqlite3BitvecCreate(pPager->origDbSize);
3484 if( !pPager->pAlwaysRollback ){
3485 return SQLITE_NOMEM;
3486 }
3487 }
3488 rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno);
3489
danielk1977fd7f0452008-12-17 17:30:26 +00003490 if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
drh1aa2d8b2007-01-03 15:34:29 +00003491 assert( pPager->state>=PAGER_SHARED );
danielk1977d92db532008-11-17 04:56:24 +00003492 if( pPager->dbSize==pPg->pgno && pPager->origDbSize<pPager->dbSize ){
drh8124a302002-06-25 14:43:57 +00003493 /* If this pages is the last page in the file and the file has grown
3494 ** during the current transaction, then do NOT mark the page as clean.
3495 ** When the database file grows, we must make sure that the last page
3496 ** gets written at least once so that the disk file will be the correct
3497 ** size. If you do not write this page and the size of the file
3498 ** on the disk ends up being too small, that can lead to database
3499 ** corruption during the next transaction.
3500 */
3501 }else{
drh538f5702007-04-13 02:14:30 +00003502 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
3503 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
danielk197733e32162008-08-23 18:53:08 +00003504 pPg->flags |= PGHDR_DONT_WRITE;
danielk19773c407372005-02-15 02:54:14 +00003505#ifdef SQLITE_CHECK_PAGES
3506 pPg->pageHash = pager_pagehash(pPg);
3507#endif
drh8124a302002-06-25 14:43:57 +00003508 }
drh30e58752002-03-02 20:41:57 +00003509 }
danielk1977a1fa00d2008-08-27 15:16:33 +00003510 return rc;
drh30e58752002-03-02 20:41:57 +00003511}
3512
3513/*
3514** A call to this routine tells the pager that if a rollback occurs,
3515** it is not necessary to restore the data on the given page. This
3516** means that the pager does not have to record the given page in the
3517** rollback journal.
drh538f5702007-04-13 02:14:30 +00003518**
3519** If we have not yet actually read the content of this page (if
3520** the PgHdr.needRead flag is set) then this routine acts as a promise
3521** that we will never need to read the page content in the future.
3522** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00003523*/
danielk19773b8a05f2007-03-19 17:44:26 +00003524void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003525 Pager *pPager = pPg->pPager;
3526
drhd3627af2006-12-18 18:34:51 +00003527 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00003528
3529 /* If the journal file is not open, or DontWrite() has been called on
3530 ** this page (DontWrite() sets the alwaysRollback flag), then this
3531 ** function is a no-op.
3532 */
danielk1977a1fa00d2008-08-27 15:16:33 +00003533 if( pPager->journalOpen==0
3534 || sqlite3BitvecTest(pPager->pAlwaysRollback, pPg->pgno)
3535 || pPg->pgno>pPager->origDbSize
danielk19778c0a7912008-08-20 14:49:23 +00003536 ){
danielk197787c29a92008-01-18 11:33:16 +00003537 return;
3538 }
danielk1977a55e9352008-01-21 13:04:34 +00003539
drhc5d0bd92008-04-14 01:00:57 +00003540#ifdef SQLITE_SECURE_DELETE
drh1feb7dd2008-11-19 18:30:29 +00003541 if( sqlite3BitvecTest(pPager->pInJournal, pPg->pgno)!=0
3542 || pPg->pgno>pPager->origDbSize ){
drhc5d0bd92008-04-14 01:00:57 +00003543 return;
3544 }
3545#endif
3546
3547 /* If SECURE_DELETE is disabled, then there is no way that this
3548 ** routine can be called on a page for which sqlite3PagerDontWrite()
3549 ** has not been previously called during the same transaction.
3550 ** And if DontWrite() has previously been called, the following
3551 ** conditions must be met.
drh10131482008-07-11 03:34:09 +00003552 **
3553 ** (Later:) Not true. If the database is corrupted by having duplicate
3554 ** pages on the freelist (ex: corrupt9.test) then the following is not
3555 ** necessarily true:
danielk1977a55e9352008-01-21 13:04:34 +00003556 */
drh10131482008-07-11 03:34:09 +00003557 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); */
danielk1977a55e9352008-01-21 13:04:34 +00003558
drhf5e7bb52008-02-18 14:47:33 +00003559 assert( pPager->pInJournal!=0 );
3560 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003561 pPg->flags &= ~PGHDR_NEED_READ;
danielk1977fd7f0452008-12-17 17:30:26 +00003562 addToSavepointBitvecs(pPager, pPg->pgno);
danielk1977a55e9352008-01-21 13:04:34 +00003563 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
3564 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00003565}
3566
drhac69b052004-05-12 13:30:07 +00003567
drh30e58752002-03-02 20:41:57 +00003568/*
drh80e35f42007-03-30 14:06:34 +00003569** This routine is called to increment the database file change-counter,
3570** stored at byte 24 of the pager file.
3571*/
danielk1977c7b60172007-08-22 11:22:03 +00003572static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00003573 PgHdr *pPgHdr;
3574 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00003575 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00003576
drh701bb3b2008-08-02 03:50:39 +00003577#ifndef SQLITE_ENABLE_ATOMIC_WRITE
3578 assert( isDirect==0 ); /* isDirect is only true for atomic writes */
3579#endif
danielk197712dd5492008-12-18 15:45:07 +00003580 if( !pPager->changeCountDone && pPager->dbSize>0 ){
drh80e35f42007-03-30 14:06:34 +00003581 /* Open page 1 of the file for writing. */
3582 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3583 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00003584
3585 if( !isDirect ){
3586 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00003587 if( rc!=SQLITE_OK ){
3588 sqlite3PagerUnref(pPgHdr);
3589 return rc;
3590 }
danielk1977c7b60172007-08-22 11:22:03 +00003591 }
3592
drh80e35f42007-03-30 14:06:34 +00003593 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00003594 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00003595 change_counter++;
danielk19778c0a7912008-08-20 14:49:23 +00003596 put32bits(((char*)pPgHdr->pData)+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00003597
drh701bb3b2008-08-02 03:50:39 +00003598#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977c7b60172007-08-22 11:22:03 +00003599 if( isDirect && pPager->fd->pMethods ){
danielk19778c0a7912008-08-20 14:49:23 +00003600 const void *zBuf = pPgHdr->pData;
danielk1977c7b60172007-08-22 11:22:03 +00003601 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
3602 }
drh701bb3b2008-08-02 03:50:39 +00003603#endif
danielk1977c7b60172007-08-22 11:22:03 +00003604
drh80e35f42007-03-30 14:06:34 +00003605 /* Release the page reference. */
3606 sqlite3PagerUnref(pPgHdr);
3607 pPager->changeCountDone = 1;
3608 }
danielk1977c7b60172007-08-22 11:22:03 +00003609 return rc;
drh80e35f42007-03-30 14:06:34 +00003610}
3611
3612/*
danielk1977f653d782008-03-20 11:04:21 +00003613** Sync the pager file to disk.
3614*/
3615int sqlite3PagerSync(Pager *pPager){
3616 int rc;
drh7426f862008-08-26 21:07:26 +00003617 if( MEMDB ){
3618 rc = SQLITE_OK;
3619 }else{
3620 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3621 }
danielk1977f653d782008-03-20 11:04:21 +00003622 return rc;
3623}
3624
3625/*
drh80e35f42007-03-30 14:06:34 +00003626** Sync the database file for the pager pPager. zMaster points to the name
3627** of a master journal file that should be written into the individual
3628** journal file. zMaster may be NULL, which is interpreted as no master
3629** journal (a single database transaction).
3630**
3631** This routine ensures that the journal is synced, all dirty pages written
3632** to the database file and the database file synced. The only thing that
3633** remains to commit the transaction is to delete the journal file (or
3634** master journal file if specified).
3635**
3636** Note that if zMaster==NULL, this does not overwrite a previous value
3637** passed to an sqlite3PagerCommitPhaseOne() call.
3638**
3639** If parameter nTrunc is non-zero, then the pager file is truncated to
3640** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00003641**
3642** If the final parameter - noSync - is true, then the database file itself
3643** is not synced. The caller must call sqlite3PagerSync() directly to
3644** sync the database file before calling CommitPhaseTwo() to delete the
3645** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00003646*/
danielk1977f653d782008-03-20 11:04:21 +00003647int sqlite3PagerCommitPhaseOne(
3648 Pager *pPager,
3649 const char *zMaster,
3650 Pgno nTrunc,
3651 int noSync
3652){
drh80e35f42007-03-30 14:06:34 +00003653 int rc = SQLITE_OK;
3654
danielk1977dad31b52008-05-15 11:08:07 +00003655 if( pPager->errCode ){
3656 return pPager->errCode;
3657 }
3658
drhd138c012008-05-13 00:58:18 +00003659 /* If no changes have been made, we can leave the transaction early.
3660 */
3661 if( pPager->dbModified==0 &&
3662 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3663 pPager->exclusiveMode!=0) ){
3664 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3665 return SQLITE_OK;
3666 }
3667
drh80e35f42007-03-30 14:06:34 +00003668 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
3669 pPager->zFilename, zMaster, nTrunc);
3670
3671 /* If this is an in-memory db, or no pages have been written to, or this
3672 ** function has already been called, it is a no-op.
3673 */
3674 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
3675 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00003676
danielk1977c7b60172007-08-22 11:22:03 +00003677#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3678 /* The atomic-write optimization can be used if all of the
3679 ** following are true:
3680 **
3681 ** + The file-system supports the atomic-write property for
3682 ** blocks of size page-size, and
3683 ** + This commit is not part of a multi-file transaction, and
3684 ** + Exactly one page has been modified and store in the journal file.
3685 **
3686 ** If the optimization can be used, then the journal file will never
3687 ** be created for this transaction.
3688 */
danielk19774d60af92008-09-08 15:35:06 +00003689 int useAtomicWrite;
danielk19778c0a7912008-08-20 14:49:23 +00003690 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
danielk19774d60af92008-09-08 15:35:06 +00003691 useAtomicWrite = (
danielk1977f55b8992007-08-24 08:15:53 +00003692 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00003693 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00003694 pPager->journalOff==jrnlBufferSize(pPager) &&
3695 nTrunc==0 &&
danielk19778c0a7912008-08-20 14:49:23 +00003696 (pPg==0 || pPg->pDirty==0)
danielk1977f55b8992007-08-24 08:15:53 +00003697 );
danielk1977700b9c52008-04-24 12:37:40 +00003698 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00003699 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00003700 /* Update the nRec field in the journal file. */
3701 int offset = pPager->journalHdr + sizeof(aJournalMagic);
3702 assert(pPager->nRec==1);
3703 rc = write32bits(pPager->jfd, offset, pPager->nRec);
3704
3705 /* Update the db file change counter. The following call will modify
3706 ** the in-memory representation of page 1 to include the updated
3707 ** change counter and then write page 1 directly to the database
3708 ** file. Because of the atomic-write property of the host file-system,
3709 ** this is safe.
3710 */
danielk1977ae72d982007-10-03 08:46:44 +00003711 if( rc==SQLITE_OK ){
3712 rc = pager_incr_changecounter(pPager, 1);
3713 }
danielk1977f55b8992007-08-24 08:15:53 +00003714 }else{
3715 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00003716 }
3717
danielk1977ae72d982007-10-03 08:46:44 +00003718 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00003719#endif
3720
drh80e35f42007-03-30 14:06:34 +00003721 /* If a master journal file name has already been written to the
3722 ** journal file, then no sync is required. This happens when it is
3723 ** written, then the process fails to upgrade from a RESERVED to an
3724 ** EXCLUSIVE lock. The next time the process tries to commit the
3725 ** transaction the m-j name will have already been written.
3726 */
3727 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00003728 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00003729 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00003730 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00003731#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197771aa7ff2008-05-20 07:05:09 +00003732 if( nTrunc!=0 ){
3733 /* If this transaction has made the database smaller, then all pages
3734 ** being discarded by the truncation must be written to the journal
3735 ** file.
3736 */
3737 Pgno i;
danielk1977d92db532008-11-17 04:56:24 +00003738 Pgno iSkip = PAGER_MJ_PGNO(pPager);
danielk197771aa7ff2008-05-20 07:05:09 +00003739 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
3740 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
3741 rc = sqlite3PagerGet(pPager, i, &pPg);
3742 if( rc!=SQLITE_OK ) goto sync_exit;
3743 rc = sqlite3PagerWrite(pPg);
3744 sqlite3PagerUnref(pPg);
3745 if( rc!=SQLITE_OK ) goto sync_exit;
3746 }
3747 }
3748 }
drh80e35f42007-03-30 14:06:34 +00003749#endif
danielk197771aa7ff2008-05-20 07:05:09 +00003750 rc = writeMasterJournal(pPager, zMaster);
3751 if( rc!=SQLITE_OK ) goto sync_exit;
3752 rc = syncJournal(pPager);
3753 }
drh80e35f42007-03-30 14:06:34 +00003754 }
danielk1977c7b60172007-08-22 11:22:03 +00003755 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00003756
3757#ifndef SQLITE_OMIT_AUTOVACUUM
3758 if( nTrunc!=0 ){
3759 rc = sqlite3PagerTruncate(pPager, nTrunc);
3760 if( rc!=SQLITE_OK ) goto sync_exit;
3761 }
3762#endif
3763
3764 /* Write all dirty pages to the database file */
danielk19778c0a7912008-08-20 14:49:23 +00003765 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003766 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00003767 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00003768 assert( rc!=SQLITE_IOERR_BLOCKED );
3769 /* The error might have left the dirty list all fouled up here,
3770 ** but that does not matter because if the if the dirty list did
3771 ** get corrupted, then the transaction will roll back and
3772 ** discard the dirty list. There is an assert in
3773 ** pager_get_all_dirty_pages() that verifies that no attempt
3774 ** is made to use an invalid dirty list.
3775 */
drh153c62c2007-08-24 03:51:33 +00003776 goto sync_exit;
3777 }
danielk19778c0a7912008-08-20 14:49:23 +00003778 sqlite3PcacheCleanAll(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003779
3780 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00003781 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00003782 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00003783 }
3784 IOTRACE(("DBSYNC %p\n", pPager))
3785
3786 pPager->state = PAGER_SYNCED;
3787 }else if( MEMDB && nTrunc!=0 ){
3788 rc = sqlite3PagerTruncate(pPager, nTrunc);
3789 }
3790
3791sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00003792 if( rc==SQLITE_IOERR_BLOCKED ){
3793 /* pager_incr_changecounter() may attempt to obtain an exclusive
3794 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00003795 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00003796 * better to return SQLITE_BUSY.
3797 */
3798 rc = SQLITE_BUSY;
3799 }
drh80e35f42007-03-30 14:06:34 +00003800 return rc;
3801}
3802
3803
3804/*
drhed7c8552001-04-11 14:29:21 +00003805** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00003806**
3807** If the commit fails for any reason, a rollback attempt is made
3808** and an error code is returned. If the commit worked, SQLITE_OK
3809** is returned.
drhed7c8552001-04-11 14:29:21 +00003810*/
drh80e35f42007-03-30 14:06:34 +00003811int sqlite3PagerCommitPhaseTwo(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003812 int rc = SQLITE_OK;
drhd9b02572001-04-15 00:37:09 +00003813
danielk1977efaaf572006-01-16 11:29:19 +00003814 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00003815 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003816 }
drha6abd042004-06-09 17:37:22 +00003817 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00003818 return SQLITE_ERROR;
3819 }
drhd138c012008-05-13 00:58:18 +00003820 if( pPager->dbModified==0 &&
3821 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3822 pPager->exclusiveMode!=0) ){
3823 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3824 return SQLITE_OK;
3825 }
drh4f0c5872007-03-26 22:05:01 +00003826 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
danielk1977b3175382008-10-17 18:51:52 +00003827 assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dirtyCache );
3828 rc = pager_end_transaction(pPager, pPager->setMaster);
3829 rc = pager_error(pPager, rc);
drh86f8c192007-08-22 00:39:19 +00003830 return rc;
drhed7c8552001-04-11 14:29:21 +00003831}
3832
3833/*
drha6abd042004-06-09 17:37:22 +00003834** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00003835** All in-memory cache pages revert to their original data contents.
3836** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00003837**
3838** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00003839** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00003840** process is writing trash into the journal file (SQLITE_CORRUPT) or
3841** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
3842** codes are returned for all these occasions. Otherwise,
3843** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00003844*/
danielk19773b8a05f2007-03-19 17:44:26 +00003845int sqlite3PagerRollback(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003846 int rc = SQLITE_OK;
drh4f0c5872007-03-26 22:05:01 +00003847 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
danielk1977b3175382008-10-17 18:51:52 +00003848 if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00003849 rc = pager_end_transaction(pPager, pPager->setMaster);
danielk19778c0a7912008-08-20 14:49:23 +00003850 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00003851 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00003852 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00003853 }
danielk19778c0a7912008-08-20 14:49:23 +00003854 rc = pPager->errCode;
drha6abd042004-06-09 17:37:22 +00003855 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003856 if( pPager->state==PAGER_RESERVED ){
3857 int rc2;
3858 rc = pager_playback(pPager, 0);
3859 rc2 = pager_end_transaction(pPager, pPager->setMaster);
3860 if( rc==SQLITE_OK ){
3861 rc = rc2;
3862 }
3863 }else{
3864 rc = pager_playback(pPager, 0);
3865 }
danielk197707cb5602006-01-20 10:55:05 +00003866
danielk1977b3175382008-10-17 18:51:52 +00003867 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00003868 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00003869 }
danielk19778c0a7912008-08-20 14:49:23 +00003870
3871 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
3872 ** cache. So call pager_error() on the way out to make any error
3873 ** persistent.
3874 */
3875 rc = pager_error(pPager, rc);
3876 }
drh86f8c192007-08-22 00:39:19 +00003877 return rc;
drh98808ba2001-10-18 12:34:46 +00003878}
drhd9b02572001-04-15 00:37:09 +00003879
3880/*
drh5e00f6c2001-09-13 13:46:56 +00003881** Return TRUE if the database file is opened read-only. Return FALSE
3882** if the database is (in theory) writable.
3883*/
drhf49661a2008-12-10 16:45:50 +00003884u8 sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00003885 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00003886}
3887
3888/*
drh0f7eb612006-08-08 13:51:43 +00003889** Return the number of references to the pager.
3890*/
danielk19773b8a05f2007-03-19 17:44:26 +00003891int sqlite3PagerRefcount(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003892 return sqlite3PcacheRefCount(pPager->pPCache);
drh0f7eb612006-08-08 13:51:43 +00003893}
3894
danielk197771d5d2c2008-09-29 11:49:47 +00003895/*
3896** Return the number of references to the specified page.
3897*/
3898int sqlite3PagerPageRefcount(DbPage *pPage){
3899 return sqlite3PcachePageRefcount(pPage);
3900}
3901
drh0f7eb612006-08-08 13:51:43 +00003902#ifdef SQLITE_TEST
3903/*
drhd9b02572001-04-15 00:37:09 +00003904** This routine is used for testing and analysis only.
3905*/
danielk19773b8a05f2007-03-19 17:44:26 +00003906int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00003907 static int a[11];
danielk19778c0a7912008-08-20 14:49:23 +00003908 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
3909 a[1] = sqlite3PcachePagecount(pPager->pPCache);
3910 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
danielk1977d92db532008-11-17 04:56:24 +00003911 a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
drhd9b02572001-04-15 00:37:09 +00003912 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00003913 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003914 a[6] = pPager->nHit;
3915 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00003916 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00003917 a[9] = pPager->nRead;
3918 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00003919 return a;
3920}
danielk197717b90b52008-06-06 11:11:25 +00003921int sqlite3PagerIsMemdb(Pager *pPager){
3922 return MEMDB;
3923}
drh0f7eb612006-08-08 13:51:43 +00003924#endif
drhdd793422001-06-28 01:54:48 +00003925
drhfa86c412002-02-02 15:01:15 +00003926/*
danielk1977fd7f0452008-12-17 17:30:26 +00003927** Ensure that there are at least nSavepoint savepoints open.
drhfa86c412002-02-02 15:01:15 +00003928*/
danielk1977fd7f0452008-12-17 17:30:26 +00003929int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
3930 int rc = SQLITE_OK;
3931
danielk197712dd5492008-12-18 15:45:07 +00003932 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
danielk1977fd7f0452008-12-17 17:30:26 +00003933 int ii;
3934
3935 /* Either the sub-journal is open or there are no active savepoints. */
3936 assert( pPager->nSavepoint==0 || pPager->sjfd->pMethods );
3937
3938 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
3939 ** if the allocation fails. Otherwise, zero the new portion in case a
3940 ** malloc failure occurs while populating it in the for(...) loop below.
3941 */
3942 PagerSavepoint *aNew = (PagerSavepoint *)sqlite3Realloc(
3943 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
3944 );
3945 if( !aNew ){
3946 return SQLITE_NOMEM;
3947 }
3948 memset(&aNew[pPager->nSavepoint], 0,
3949 (nSavepoint - pPager->nSavepoint) * sizeof(PagerSavepoint)
3950 );
3951 pPager->aSavepoint = aNew;
3952 ii = pPager->nSavepoint;
3953 pPager->nSavepoint = nSavepoint;
3954
3955 /* Populate the PagerSavepoint structures just allocated. */
3956 for(/* no-op */; ii<nSavepoint; ii++){
danielk197712dd5492008-12-18 15:45:07 +00003957 assert( pPager->dbSizeValid );
danielk1977fd7f0452008-12-17 17:30:26 +00003958 aNew[ii].nOrig = pPager->dbSize;
3959 aNew[ii].iOffset = (pPager->journalOpen ? pPager->journalOff : 0);
3960 aNew[ii].iSubRec = pPager->stmtNRec;
3961 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
3962 if( !aNew[ii].pInSavepoint ){
3963 return SQLITE_NOMEM;
danielk1977b3175382008-10-17 18:51:52 +00003964 }
drh334b2992007-09-06 23:28:23 +00003965 }
danielk1977fd7f0452008-12-17 17:30:26 +00003966
3967 /* Open the sub-journal, if it is not already opened. */
3968 rc = openSubJournal(pPager);
drh0f892532002-05-30 12:27:03 +00003969 }
danielk1977fd7f0452008-12-17 17:30:26 +00003970
drh86f8c192007-08-22 00:39:19 +00003971 return rc;
3972}
drhfa86c412002-02-02 15:01:15 +00003973
3974/*
danielk1977fd7f0452008-12-17 17:30:26 +00003975** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
3976** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
3977** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
3978** that have occured since savepoint iSavepoint was created.
3979**
3980** In either case, all savepoints with an index greater than iSavepoint
3981** are destroyed.
3982**
3983** If there are less than (iSavepoint+1) active savepoints when this
3984** function is called it is a no-op.
3985*/
3986int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
3987 int rc = SQLITE_OK;
3988
3989 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
3990
3991 if( iSavepoint<pPager->nSavepoint ){
3992 int ii;
3993 int nNew = iSavepoint + (op==SAVEPOINT_ROLLBACK);
3994 for(ii=nNew; ii<pPager->nSavepoint; ii++){
3995 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
3996 }
3997 pPager->nSavepoint = nNew;
3998
3999 if( op==SAVEPOINT_ROLLBACK ){
4000 PagerSavepoint *pSavepoint = (nNew==0) ? 0 : &pPager->aSavepoint[nNew-1];
4001 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
4002 assert(rc!=SQLITE_DONE);
4003 }
4004
4005 /* If this is a release of the outermost savepoint, truncate
4006 ** the sub-journal. */
4007 if( nNew==0 && op==SAVEPOINT_RELEASE && pPager->sjfd->pMethods ){
4008 assert( rc==SQLITE_OK );
4009 rc = sqlite3OsTruncate(pPager->sjfd, 0);
4010 pPager->stmtNRec = 0;
danielk1977b3175382008-10-17 18:51:52 +00004011 }
drh663fc632002-02-02 18:49:19 +00004012 }
drhfa86c412002-02-02 15:01:15 +00004013 return rc;
4014}
4015
drh73509ee2003-04-06 20:44:45 +00004016/*
4017** Return the full pathname of the database file.
4018*/
danielk19773b8a05f2007-03-19 17:44:26 +00004019const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004020 return pPager->zFilename;
4021}
4022
drhb20ea9d2004-02-09 01:20:36 +00004023/*
drhd0679ed2007-08-28 22:24:34 +00004024** Return the VFS structure for the pager.
4025*/
4026const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4027 return pPager->pVfs;
4028}
4029
4030/*
drhcc6bb3e2007-08-31 16:11:35 +00004031** Return the file handle for the database file associated
4032** with the pager. This might return NULL if the file has
4033** not yet been opened.
4034*/
4035sqlite3_file *sqlite3PagerFile(Pager *pPager){
4036 return pPager->fd;
4037}
4038
4039/*
danielk19775865e3d2004-06-14 06:03:57 +00004040** Return the directory of the database file.
4041*/
danielk19773b8a05f2007-03-19 17:44:26 +00004042const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004043 return pPager->zDirectory;
4044}
4045
4046/*
4047** Return the full pathname of the journal file.
4048*/
danielk19773b8a05f2007-03-19 17:44:26 +00004049const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004050 return pPager->zJournal;
4051}
4052
4053/*
drh2c8997b2005-08-27 16:36:48 +00004054** Return true if fsync() calls are disabled for this pager. Return FALSE
4055** if fsync()s are executed normally.
4056*/
danielk19773b8a05f2007-03-19 17:44:26 +00004057int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004058 return pPager->noSync;
4059}
4060
drh7c4ac0c2007-04-05 11:25:58 +00004061#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004062/*
drhb20ea9d2004-02-09 01:20:36 +00004063** Set the codec for this pager
4064*/
danielk19773b8a05f2007-03-19 17:44:26 +00004065void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004066 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004067 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004068 void *pCodecArg
4069){
4070 pPager->xCodec = xCodec;
4071 pPager->pCodecArg = pCodecArg;
4072}
drh7c4ac0c2007-04-05 11:25:58 +00004073#endif
drhb20ea9d2004-02-09 01:20:36 +00004074
danielk1977687566d2004-11-02 12:56:41 +00004075#ifndef SQLITE_OMIT_AUTOVACUUM
4076/*
danielk197787c29a92008-01-18 11:33:16 +00004077** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004078**
drh5e385312007-06-16 04:42:12 +00004079** There must be no references to the page previously located at
4080** pgno (which we call pPgOld) though that page is allowed to be
drhb3df2e12008-09-17 20:06:26 +00004081** in cache. If the page previously located at pgno is not already
drh5e385312007-06-16 04:42:12 +00004082** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004083**
drh5e385312007-06-16 04:42:12 +00004084** References to the page pPg remain valid. Updating any
4085** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004086** allocated along with the page) is the responsibility of the caller.
4087**
danielk19775fd057a2005-03-09 13:09:43 +00004088** A transaction must be active when this routine is called. It used to be
4089** required that a statement transaction was not active, but this restriction
4090** has been removed (CREATE INDEX needs to move a page when a statement
4091** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00004092**
4093** If the fourth argument, isCommit, is non-zero, then this page is being
4094** moved as part of a database reorganization just before the transaction
4095** is being committed. In this case, it is guaranteed that the database page
4096** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00004097*/
danielk19774c999992008-07-16 18:17:55 +00004098int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00004099 PgHdr *pPgOld; /* The page being overwritten. */
danielk197794daf7f2004-11-08 09:26:09 +00004100 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004101
danielk1977687566d2004-11-02 12:56:41 +00004102 assert( pPg->nRef>0 );
4103
drh4f0c5872007-03-26 22:05:01 +00004104 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk19778c0a7912008-08-20 14:49:23 +00004105 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno);
drhb0603412007-02-28 04:47:26 +00004106 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004107
danielk1977b4626a32007-04-28 15:47:43 +00004108 pager_get_content(pPg);
danielk19774c999992008-07-16 18:17:55 +00004109
4110 /* If the journal needs to be sync()ed before page pPg->pgno can
4111 ** be written to, store pPg->pgno in local variable needSyncPgno.
4112 **
4113 ** If the isCommit flag is set, there is no need to remember that
4114 ** the journal needs to be sync()ed before database page pPg->pgno
4115 ** can be written to. The caller has already promised not to write to it.
4116 */
danielk19778c0a7912008-08-20 14:49:23 +00004117 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00004118 needSyncPgno = pPg->pgno;
danielk19770cd1bbd2008-11-26 07:25:52 +00004119 assert( pageInJournal(pPg) || pPg->pgno>pPager->origDbSize );
danielk19778c0a7912008-08-20 14:49:23 +00004120 assert( pPg->flags&PGHDR_DIRTY );
danielk1977ae825582004-11-23 09:06:55 +00004121 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004122 }
4123
danielk1977ef73ee92004-11-06 12:26:07 +00004124 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00004125 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00004126 ** page pgno before the 'move' operation, it needs to be retained
4127 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004128 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00004129 pPg->flags &= ~PGHDR_NEED_SYNC;
danielk1977687566d2004-11-02 12:56:41 +00004130 pPgOld = pager_lookup(pPager, pgno);
danielk19778c0a7912008-08-20 14:49:23 +00004131 assert( !pPgOld || pPgOld->nRef==1 );
danielk1977687566d2004-11-02 12:56:41 +00004132 if( pPgOld ){
danielk19778c0a7912008-08-20 14:49:23 +00004133 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
drh5e385312007-06-16 04:42:12 +00004134 }
danielk1977687566d2004-11-02 12:56:41 +00004135
danielk19778c0a7912008-08-20 14:49:23 +00004136 sqlite3PcacheMove(pPg, pgno);
4137 if( pPgOld ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00004138 sqlite3PcacheDrop(pPgOld);
danielk1977f5fdda82004-11-03 08:44:05 +00004139 }
danielk1977f5fdda82004-11-03 08:44:05 +00004140
drhc047b9f2008-11-21 03:23:43 +00004141 sqlite3PcacheMakeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004142 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004143 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00004144
danielk197794daf7f2004-11-08 09:26:09 +00004145 if( needSyncPgno ){
4146 /* If needSyncPgno is non-zero, then the journal file needs to be
4147 ** sync()ed before any data is written to database file page needSyncPgno.
4148 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00004149 ** "is journaled" bitvec flag has been set. This needs to be remedied by
4150 ** loading the page into the pager-cache and setting the PgHdr.needSync
4151 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00004152 **
danielk1977a98d7b42008-01-18 13:42:54 +00004153 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00004154 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00004155 ** array. Otherwise, if the page is loaded and written again in
4156 ** this transaction, it may be written to the database file before
4157 ** it is synced into the journal file. This way, it may end up in
4158 ** the journal file twice, but that is not a problem.
4159 **
danielk19773b8a05f2007-03-19 17:44:26 +00004160 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004161 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004162 */
4163 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004164 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004165 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004166 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00004167 if( rc!=SQLITE_OK ){
danielk1977d92db532008-11-17 04:56:24 +00004168 if( pPager->pInJournal && needSyncPgno<=pPager->origDbSize ){
drhf5e7bb52008-02-18 14:47:33 +00004169 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00004170 }
danielk197787c29a92008-01-18 11:33:16 +00004171 return rc;
4172 }
danielk1977ae825582004-11-23 09:06:55 +00004173 pPager->needSync = 1;
drhb3df2e12008-09-17 20:06:26 +00004174 assert( pPager->noSync==0 && !MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00004175 pPgHdr->flags |= PGHDR_NEED_SYNC;
drhc047b9f2008-11-21 03:23:43 +00004176 sqlite3PcacheMakeDirty(pPgHdr);
danielk19773b8a05f2007-03-19 17:44:26 +00004177 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004178 }
4179
danielk1977687566d2004-11-02 12:56:41 +00004180 return SQLITE_OK;
4181}
4182#endif
4183
danielk19773b8a05f2007-03-19 17:44:26 +00004184/*
4185** Return a pointer to the data for the specified page.
4186*/
4187void *sqlite3PagerGetData(DbPage *pPg){
danielk197771d5d2c2008-09-29 11:49:47 +00004188 assert( pPg->nRef>0 || pPg->pPager->memDb );
danielk19778c0a7912008-08-20 14:49:23 +00004189 return pPg->pData;
danielk19773b8a05f2007-03-19 17:44:26 +00004190}
4191
4192/*
4193** Return a pointer to the Pager.nExtra bytes of "extra" space
4194** allocated along with the specified page.
4195*/
4196void *sqlite3PagerGetExtra(DbPage *pPg){
4197 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00004198 return (pPager?pPg->pExtra:0);
danielk19773b8a05f2007-03-19 17:44:26 +00004199}
4200
danielk197741483462007-03-24 16:45:04 +00004201/*
4202** Get/set the locking-mode for this pager. Parameter eMode must be one
4203** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4204** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4205** the locking-mode is set to the value specified.
4206**
4207** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4208** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4209** locking-mode.
4210*/
4211int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004212 assert( eMode==PAGER_LOCKINGMODE_QUERY
4213 || eMode==PAGER_LOCKINGMODE_NORMAL
4214 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4215 assert( PAGER_LOCKINGMODE_QUERY<0 );
4216 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4217 if( eMode>=0 && !pPager->tempFile ){
drh1bd10f82008-12-10 21:19:56 +00004218 pPager->exclusiveMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +00004219 }
4220 return (int)pPager->exclusiveMode;
4221}
4222
drh3b020132008-04-17 17:02:01 +00004223/*
drh04335882008-09-26 21:08:08 +00004224** Get/set the journal-mode for this pager. Parameter eMode must be one of:
drh3b020132008-04-17 17:02:01 +00004225**
drh04335882008-09-26 21:08:08 +00004226** PAGER_JOURNALMODE_QUERY
4227** PAGER_JOURNALMODE_DELETE
4228** PAGER_JOURNALMODE_TRUNCATE
4229** PAGER_JOURNALMODE_PERSIST
4230** PAGER_JOURNALMODE_OFF
4231**
4232** If the parameter is not _QUERY, then the journal-mode is set to the
4233** value specified.
4234**
4235** The returned indicate the current (possibly updated)
drh3b020132008-04-17 17:02:01 +00004236** journal-mode.
4237*/
4238int sqlite3PagerJournalMode(Pager *pPager, int eMode){
danielk1977b3175382008-10-17 18:51:52 +00004239 if( !MEMDB ){
4240 assert( eMode==PAGER_JOURNALMODE_QUERY
4241 || eMode==PAGER_JOURNALMODE_DELETE
4242 || eMode==PAGER_JOURNALMODE_TRUNCATE
4243 || eMode==PAGER_JOURNALMODE_PERSIST
4244 || eMode==PAGER_JOURNALMODE_OFF
4245 || eMode==PAGER_JOURNALMODE_MEMORY );
4246 assert( PAGER_JOURNALMODE_QUERY<0 );
4247 if( eMode>=0 ){
drh1bd10f82008-12-10 21:19:56 +00004248 pPager->journalMode = (u8)eMode;
danielk1977b3175382008-10-17 18:51:52 +00004249 }else{
4250 assert( eMode==PAGER_JOURNALMODE_QUERY );
4251 }
drh3b020132008-04-17 17:02:01 +00004252 }
drhfdc40e92008-04-17 20:59:37 +00004253 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00004254}
4255
danielk1977b53e4962008-06-04 06:45:59 +00004256/*
4257** Get/set the size-limit used for persistent journal files.
4258*/
4259i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
4260 if( iLimit>=-1 ){
4261 pPager->journalSizeLimit = iLimit;
4262 }
4263 return pPager->journalSizeLimit;
4264}
4265
drh2e66f0b2005-04-28 17:18:48 +00004266#endif /* SQLITE_OMIT_DISKIO */