blob: ab4f9167f915d60372303706813d6973cc53a3d3 [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**
drh30d53702009-01-06 15:58:57 +000021** @(#) $Id: pager.c,v 1.534 2009/01/06 15:58:57 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025
26/*
drhdb48ee02003-01-16 13:42:43 +000027** Macros for troubleshooting. Normally turned off
28*/
danielk1977466be562004-06-10 02:16:01 +000029#if 0
danielk1977f2c31ad2009-01-06 13:40:08 +000030int sqlite3PagerTrace=1; /* True to enable tracing */
drhd3627af2006-12-18 18:34:51 +000031#define sqlite3DebugPrintf printf
drh30d53702009-01-06 15:58:57 +000032#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
drhdb48ee02003-01-16 13:42:43 +000033#else
drh30d53702009-01-06 15:58:57 +000034#define PAGERTRACE(X)
drhdb48ee02003-01-16 13:42:43 +000035#endif
36
danielk1977599fcba2004-11-08 07:13:13 +000037/*
drh30d53702009-01-06 15:58:57 +000038** The following two macros are used within the PAGERTRACE() macros above
drhd86959f2005-11-26 03:51:18 +000039** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000040**
drh85b623f2007-12-13 21:54:09 +000041** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000042** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000043** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000044*/
drhc001c582006-03-06 18:23:16 +000045#define PAGERID(p) ((int)(p->fd))
46#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000047
48/*
drhed7c8552001-04-11 14:29:21 +000049** The page cache as a whole is always in one of the following
50** states:
51**
drha6abd042004-06-09 17:37:22 +000052** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000053** writing the database file. There is no
54** data held in memory. This is the initial
55** state.
56**
drha6abd042004-06-09 17:37:22 +000057** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000058** Writing is not permitted. There can be
59** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000060** file at the same time.
drhed7c8552001-04-11 14:29:21 +000061**
drh726de592004-06-10 23:35:50 +000062** PAGER_RESERVED This process has reserved the database for writing
63** but has not yet made any changes. Only one process
64** at a time can reserve the database. The original
65** database file has not been modified so other
66** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000067** database file.
68**
69** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000070** Access is exclusive. No other processes or
71** threads can be reading or writing while one
72** process is writing.
73**
danielk1977aa5ccdf2004-06-14 05:10:42 +000074** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
75** after all dirty pages have been written to the
76** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000077** disk. All that remains to do is to remove or
78** truncate the journal file and the transaction
79** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000080**
drha6abd042004-06-09 17:37:22 +000081** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000082** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000083** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000084** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000085** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000086** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000087** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000088** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000089** PAGER_RESERVED means that there is an open rollback journal.
90** The transition to PAGER_EXCLUSIVE occurs before any changes
91** are made to the database file, though writes to the rollback
92** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
93** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
94** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +000095*/
drha6abd042004-06-09 17:37:22 +000096#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +000097#define PAGER_SHARED 1 /* same as SHARED_LOCK */
98#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
99#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
100#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000101
drh684917c2004-10-05 02:41:42 +0000102/*
drh887dc4c2004-10-22 16:22:57 +0000103** This macro rounds values up so that if the value is an address it
104** is guaranteed to be an address that is aligned to an 8-byte boundary.
105*/
106#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
107
drh9eb9e262004-02-11 02:18:05 +0000108/*
109** A macro used for invoking the codec if there is one
110*/
111#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000112# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
113# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000114#else
drhc001c582006-03-06 18:23:16 +0000115# define CODEC1(P,D,N,X) /* NO-OP */
116# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000117#endif
118
drhed7c8552001-04-11 14:29:21 +0000119/*
danielk1977fd7f0452008-12-17 17:30:26 +0000120** An instance of the following structure is allocated for each active
121** savepoint and statement transaction in the system. All such structures
122** are stored in the Pager.aSavepoint[] array, which is allocated and
123** resized using sqlite3Realloc().
124**
125** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
126** set to 0. If a journal-header is written into the main journal while
127** the savepoint is active, then iHdrOffset is set to the byte offset
128** immediately following the last journal record written into the main
129** journal before the journal-header. This is required during savepoint
130** rollback (see pagerPlaybackSavepoint()).
131*/
132typedef struct PagerSavepoint PagerSavepoint;
133struct PagerSavepoint {
134 i64 iOffset; /* Starting offset in main journal */
135 i64 iHdrOffset; /* See above */
136 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
137 Pgno nOrig; /* Original number of pages in file */
138 Pgno iSubRec; /* Index of first record in sub-journal */
139};
140
141/*
drhed7c8552001-04-11 14:29:21 +0000142** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000143**
drh4f0ee682007-03-30 20:43:40 +0000144** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000145** or SQLITE_FULL. Once one of the first three errors occurs, it persists
146** and is returned as the result of every major pager API call. The
147** SQLITE_FULL return code is slightly different. It persists only until the
148** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000149** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000150** APIs, they may still be used successfully.
danielk19773460d192008-12-27 15:23:13 +0000151**
152** Managing the size of the database file in pages is a little complicated.
153** The variable Pager.dbSize contains the number of pages that the database
154** image currently contains. As the database image grows or shrinks this
155** variable is updated. The variable Pager.dbFileSize contains the number
156** of pages in the database file. This may be different from Pager.dbSize
157** if some pages have been appended to the database image but not yet written
158** out from the cache to the actual file on disk. Or if the image has been
159** truncated by an incremental-vacuum operation. The Pager.dbOrigSize variable
160** contains the number of pages in the database image when the current
161** transaction was opened. The contents of all three of these variables is
162** only guaranteed to be correct if the boolean Pager.dbSizeValid is true.
drhed7c8552001-04-11 14:29:21 +0000163*/
164struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000165 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000166 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000167 u8 journalStarted; /* True if header of journal is synced */
168 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000169 u8 noReadlock; /* Do not bother to obtain readlocks */
drh603240c2002-03-05 01:11:12 +0000170 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000171 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000172 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000173 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000174 u8 tempFile; /* zFilename is a temporary file */
175 u8 readOnly; /* True for a read-only database */
176 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000177 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000178 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000179 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000180 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000181 u8 doNotSync; /* Boolean. While true, do not spill the cache */
182 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000183 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drhd138c012008-05-13 00:58:18 +0000184 u8 dbModified; /* True if there are any changes to the Db */
drh80e35f42007-03-30 14:06:34 +0000185 u8 changeCountDone; /* Set after incrementing the change-counter */
danielk1977d92db532008-11-17 04:56:24 +0000186 u8 dbSizeValid; /* Set when dbSize is correct */
danielk19773460d192008-12-27 15:23:13 +0000187 Pgno dbSize; /* Number of pages in the database */
188 Pgno dbOrigSize; /* dbSize before the current transaction */
189 Pgno dbFileSize; /* Number of pages in the database file */
drh33f4e022007-09-03 15:19:34 +0000190 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000191 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000192 int nRec; /* Number of pages written to the journal */
193 u32 cksumInit; /* Quasi-random value added to every checksum */
194 int stmtNRec; /* Number of records in stmt subjournal */
195 int nExtra; /* Add this many bytes to each in-memory page */
196 int pageSize; /* Number of bytes in a page */
197 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000198 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000199 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000200 Bitvec *pInJournal; /* One bit for each page in the database file */
danielk1977a1fa00d2008-08-27 15:16:33 +0000201 Bitvec *pAlwaysRollback; /* One bit for each page marked always-rollback */
drhfcd35c72005-05-21 02:48:08 +0000202 char *zFilename; /* Name of the database file */
203 char *zJournal; /* Name of the journal file */
204 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000205 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
danielk1977fd7f0452008-12-17 17:30:26 +0000206 sqlite3_file *sjfd; /* File descriptor for the sub-journal*/
danielk19771ceedd32008-11-19 10:22:33 +0000207 int (*xBusyHandler)(void*); /* Function to call when busy */
208 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
drheb206252004-10-01 02:00:31 +0000209 i64 journalOff; /* Current byte offset in the journal file */
210 i64 journalHdr; /* Byte offset to previous journal header */
drh98c58352008-11-07 00:24:53 +0000211 u32 sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000212#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000213 int nHit, nMiss; /* Cache hits and missing */
214 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000215#endif
danielk1977eaa06f62008-09-18 17:34:44 +0000216 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000217#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000218 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000219 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000220#endif
danielk19778186df82007-03-06 13:45:59 +0000221 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000222 char dbFileVers[16]; /* Changes whenever database file changes */
danielk1977b53e4962008-06-04 06:45:59 +0000223 i64 journalSizeLimit; /* Size limit for persistent journal files */
danielk19778c0a7912008-08-20 14:49:23 +0000224 PCache *pPCache; /* Pointer to page cache object */
drh9f0bbf92009-01-02 21:08:09 +0000225 PagerSavepoint *aSavepoint; /* Array of active savepoints */
226 int nSavepoint; /* Number of elements in aSavepoint[] */
drhd9b02572001-04-15 00:37:09 +0000227};
228
229/*
drh538f5702007-04-13 02:14:30 +0000230** The following global variables hold counters used for
231** testing purposes only. These variables do not exist in
232** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000233*/
234#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000235int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
236int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
237int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
drh538f5702007-04-13 02:14:30 +0000238# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000239#else
drh538f5702007-04-13 02:14:30 +0000240# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000241#endif
242
drh538f5702007-04-13 02:14:30 +0000243
244
drhfcd35c72005-05-21 02:48:08 +0000245/*
drh5e00f6c2001-09-13 13:46:56 +0000246** Journal files begin with the following magic string. The data
247** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000248**
drhae2b40c2004-06-09 19:03:54 +0000249** Since version 2.8.0, the journal format contains additional sanity
drh30d53702009-01-06 15:58:57 +0000250** checking information. If the power fails while the journal is being
drhae2b40c2004-06-09 19:03:54 +0000251** written, semi-random garbage data might appear in the journal
252** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000253** to roll the journal back, the database could be corrupted. The additional
254** sanity checking data is an attempt to discover the garbage in the
255** journal and ignore it.
256**
drhae2b40c2004-06-09 19:03:54 +0000257** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000258** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000259** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000260** This cksum is initialized to a 32-bit random value that appears in the
261** journal file right after the header. The random initializer is important,
262** because garbage data that appears at the end of a journal is likely
263** data that was once in other files that have now been deleted. If the
264** garbage data came from an obsolete journal file, the checksums might
265** be correct. But by initializing the checksum to random value which
266** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000267*/
drhae2b40c2004-06-09 19:03:54 +0000268static const unsigned char aJournalMagic[] = {
269 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000270};
271
272/*
drh726de592004-06-10 23:35:50 +0000273** The size of the header and of each page in the journal is determined
274** by the following macros.
drh968af522003-02-11 14:55:40 +0000275*/
drhae2b40c2004-06-09 19:03:54 +0000276#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000277
danielk197776572402004-06-25 02:38:54 +0000278/*
279** The journal header size for this pager. In the future, this could be
280** set to some value read from the disk controller. The important
281** characteristic is that it is the same size as a disk sector.
282*/
283#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
284
drhb7f91642004-10-31 02:22:47 +0000285/*
286** The macro MEMDB is true if we are dealing with an in-memory database.
287** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
288** the value of MEMDB will be a constant and the compiler will optimize
289** out code that would never execute.
290*/
291#ifdef SQLITE_OMIT_MEMORYDB
292# define MEMDB 0
293#else
294# define MEMDB pPager->memDb
295#endif
296
297/*
danielk197776572402004-06-25 02:38:54 +0000298** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
299** reserved for working around a windows/posix incompatibility). It is
300** used in the journal to signify that the remainder of the journal file
301** is devoted to storing a master journal name - there are no more pages to
302** roll back. See comments for function writeMasterJournal() for details.
303*/
danielk1977599fcba2004-11-08 07:13:13 +0000304/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
danielk1977d92db532008-11-17 04:56:24 +0000305#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
danielk197713adf8a2004-06-03 16:08:41 +0000306
drh968af522003-02-11 14:55:40 +0000307/*
danielk197726836652005-01-17 01:33:13 +0000308** The maximum legal page number is (2^31 - 1).
309*/
310#define PAGER_MAX_PGNO 2147483647
311
312/*
danielk19773460d192008-12-27 15:23:13 +0000313** Return true if it is necessary to write page *pPg into the sub-journal.
314** A page needs to be written into the sub-journal if there exists one
315** or more open savepoints for which:
danielk1977fd7f0452008-12-17 17:30:26 +0000316**
danielk19773460d192008-12-27 15:23:13 +0000317** * The page-number is less than or equal to PagerSavepoint.nOrig, and
318** * The bit corresponding to the page-number is not set in
319** PagerSavepoint.pInSavepoint.
danielk1977f35843b2007-04-07 15:03:17 +0000320*/
danielk19773460d192008-12-27 15:23:13 +0000321static int subjRequiresPage(PgHdr *pPg){
322 Pgno pgno = pPg->pgno;
danielk1977f35843b2007-04-07 15:03:17 +0000323 Pager *pPager = pPg->pPager;
danielk19773460d192008-12-27 15:23:13 +0000324 int i;
325 for(i=0; i<pPager->nSavepoint; i++){
326 PagerSavepoint *p = &pPager->aSavepoint[i];
327 if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
328 return 1;
329 }
danielk1977fd7f0452008-12-17 17:30:26 +0000330 }
danielk19773460d192008-12-27 15:23:13 +0000331 return 0;
danielk1977f35843b2007-04-07 15:03:17 +0000332}
drh8ca0c722006-05-07 17:49:38 +0000333
danielk19773460d192008-12-27 15:23:13 +0000334/*
335** Return true if the page is already in the journal file.
336*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000337static int pageInJournal(PgHdr *pPg){
338 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
339}
340
drh8ca0c722006-05-07 17:49:38 +0000341/*
drh34e79ce2004-02-08 06:05:46 +0000342** Read a 32-bit integer from the given file descriptor. Store the integer
343** that is read in *pRes. Return SQLITE_OK if everything worked, or an
344** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000345**
346** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000347*/
danielk197762079062007-08-15 17:08:46 +0000348static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000349 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000350 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000351 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000352 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000353 }
drh94f33312002-08-12 12:29:56 +0000354 return rc;
355}
356
357/*
drh97b57482006-01-10 20:32:32 +0000358** Write a 32-bit integer into a string buffer in big-endian byte order.
359*/
drha3152892007-05-05 11:48:52 +0000360#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000361
362/*
drh34e79ce2004-02-08 06:05:46 +0000363** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
364** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000365*/
danielk197762079062007-08-15 17:08:46 +0000366static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000367 char ac[4];
drh97b57482006-01-10 20:32:32 +0000368 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000369 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000370}
371
drh2554f8b2003-01-22 01:26:44 +0000372/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000373** If file pFd is open, call sqlite3OsUnlock() on it.
374*/
375static int osUnlock(sqlite3_file *pFd, int eLock){
376 if( !pFd->pMethods ){
377 return SQLITE_OK;
378 }
379 return sqlite3OsUnlock(pFd, eLock);
380}
381
382/*
danielk1977c7b60172007-08-22 11:22:03 +0000383** This function determines whether or not the atomic-write optimization
384** can be used with this pager. The optimization can be used if:
385**
386** (a) the value returned by OsDeviceCharacteristics() indicates that
387** a database page may be written atomically, and
388** (b) the value returned by OsSectorSize() is less than or equal
389** to the page size.
390**
391** If the optimization cannot be used, 0 is returned. If it can be used,
392** then the value returned is the size of the journal file when it
393** contains rollback data for exactly one page.
394*/
395#ifdef SQLITE_ENABLE_ATOMIC_WRITE
396static int jrnlBufferSize(Pager *pPager){
397 int dc; /* Device characteristics */
398 int nSector; /* Sector size */
drhfacf0302008-06-17 15:12:00 +0000399 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000400 sqlite3_file *fd = pPager->fd;
401
402 if( fd->pMethods ){
403 dc = sqlite3OsDeviceCharacteristics(fd);
404 nSector = sqlite3OsSectorSize(fd);
drhfacf0302008-06-17 15:12:00 +0000405 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000406 }
407
408 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
409 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
410
drhfacf0302008-06-17 15:12:00 +0000411 if( !fd->pMethods ||
412 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000413 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
414 }
415 return 0;
416}
417#endif
418
419/*
danielk1977aef0bf62005-12-30 16:28:01 +0000420** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000421** code. The first argument is a pointer to the pager structure, the
422** second the error-code about to be returned by a pager API function.
423** The value returned is a copy of the second argument to this function.
424**
drh4f0ee682007-03-30 20:43:40 +0000425** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000426** the error becomes persistent. Until the persisten error is cleared,
427** subsequent API calls on this Pager will immediately return the same
428** error code.
429**
430** A persistent error indicates that the contents of the pager-cache
431** cannot be trusted. This state can be cleared by completely discarding
432** the contents of the pager-cache. If a transaction was active when
433** the persistent error occured, then the rollback journal may need
434** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000435*/
danielk1977ae72d982007-10-03 08:46:44 +0000436static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000437static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000438 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000439 assert(
440 pPager->errCode==SQLITE_FULL ||
441 pPager->errCode==SQLITE_OK ||
442 (pPager->errCode & 0xff)==SQLITE_IOERR
443 );
danielk1977979f38e2007-03-27 16:19:51 +0000444 if(
drh4ac285a2006-09-15 07:28:50 +0000445 rc2==SQLITE_FULL ||
446 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000447 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000448 ){
449 pPager->errCode = rc;
danielk19778c0a7912008-08-20 14:49:23 +0000450 if( pPager->state==PAGER_UNLOCK
451 && sqlite3PcacheRefCount(pPager->pPCache)==0
452 ){
danielk1977ae72d982007-10-03 08:46:44 +0000453 /* If the pager is already unlocked, call pager_unlock() now to
454 ** clear the error state and ensure that the pager-cache is
455 ** completely empty.
456 */
457 pager_unlock(pPager);
458 }
danielk1977aef0bf62005-12-30 16:28:01 +0000459 }
460 return rc;
461}
462
drh477731b2007-06-16 03:06:27 +0000463/*
464** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
465** on the cache using a hash function. This is used for testing
466** and debugging only.
467*/
danielk19773c407372005-02-15 02:54:14 +0000468#ifdef SQLITE_CHECK_PAGES
469/*
470** Return a 32-bit hash of the page data for pPage.
471*/
drh477731b2007-06-16 03:06:27 +0000472static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000473 u32 hash = 0;
474 int i;
drh477731b2007-06-16 03:06:27 +0000475 for(i=0; i<nByte; i++){
476 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000477 }
478 return hash;
479}
drh477731b2007-06-16 03:06:27 +0000480static u32 pager_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000481 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
482}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000483static void pager_set_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000484 pPage->pageHash = pager_pagehash(pPage);
drh477731b2007-06-16 03:06:27 +0000485}
danielk19773c407372005-02-15 02:54:14 +0000486
487/*
488** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
489** is defined, and NDEBUG is not defined, an assert() statement checks
490** that the page is either dirty or still matches the calculated page-hash.
491*/
492#define CHECK_PAGE(x) checkPage(x)
493static void checkPage(PgHdr *pPg){
494 Pager *pPager = pPg->pPager;
danielk1977b3175382008-10-17 18:51:52 +0000495 assert( !pPg->pageHash || pPager->errCode
danielk19778c0a7912008-08-20 14:49:23 +0000496 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
danielk19773c407372005-02-15 02:54:14 +0000497}
498
499#else
drh8ffa8172007-06-18 17:25:17 +0000500#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000501#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000502#define CHECK_PAGE(x)
drh41d30272008-08-20 21:47:45 +0000503#endif /* SQLITE_CHECK_PAGES */
danielk19773c407372005-02-15 02:54:14 +0000504
drhed7c8552001-04-11 14:29:21 +0000505/*
danielk197776572402004-06-25 02:38:54 +0000506** When this is called the journal file for pager pPager must be open.
507** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000508** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000509**
danielk197765839c62007-08-30 08:08:17 +0000510** zMaster must point to a buffer of at least nMaster bytes allocated by
511** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
512** enough space to write the master journal name). If the master journal
513** name in the journal is longer than nMaster bytes (including a
514** nul-terminator), then this is handled as if no master journal name
515** were present in the journal.
516**
517** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000518** SQLITE_OK returned.
519*/
danielk1977d92db532008-11-17 04:56:24 +0000520static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
danielk197776572402004-06-25 02:38:54 +0000521 int rc;
522 u32 len;
drheb206252004-10-01 02:00:31 +0000523 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000524 u32 cksum;
shane0b8d2762008-07-22 05:18:00 +0000525 u32 u; /* Unsigned loop counter */
danielk197776572402004-06-25 02:38:54 +0000526 unsigned char aMagic[8]; /* A buffer to hold the magic header */
527
danielk197765839c62007-08-30 08:08:17 +0000528 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000529
drh054889e2005-11-30 03:20:31 +0000530 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000531 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000532
danielk197762079062007-08-15 17:08:46 +0000533 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000534 if( rc!=SQLITE_OK ) return rc;
535
danielk197765839c62007-08-30 08:08:17 +0000536 if( len>=nMaster ){
537 return SQLITE_OK;
538 }
539
danielk197762079062007-08-15 17:08:46 +0000540 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000541 if( rc!=SQLITE_OK ) return rc;
542
danielk197762079062007-08-15 17:08:46 +0000543 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000544 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
545
danielk197765839c62007-08-30 08:08:17 +0000546 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000547 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000548 return rc;
549 }
danielk197765839c62007-08-30 08:08:17 +0000550 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000551
552 /* See if the checksum matches the master journal name */
shane0b8d2762008-07-22 05:18:00 +0000553 for(u=0; u<len; u++){
554 cksum -= zMaster[u];
danielk197765839c62007-08-30 08:08:17 +0000555 }
danielk19778191bff2004-06-28 04:52:30 +0000556 if( cksum ){
557 /* If the checksum doesn't add up, then one or more of the disk sectors
558 ** containing the master journal filename is corrupted. This means
559 ** definitely roll back, so just return SQLITE_OK and report a (nul)
560 ** master-journal filename.
561 */
danielk197765839c62007-08-30 08:08:17 +0000562 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000563 }
danielk197776572402004-06-25 02:38:54 +0000564
565 return SQLITE_OK;
566}
567
568/*
569** Seek the journal file descriptor to the next sector boundary where a
570** journal header may be read or written. Pager.journalOff is updated with
571** the new seek offset.
572**
573** i.e for a sector size of 512:
574**
575** Input Offset Output Offset
576** ---------------------------------------
577** 0 0
578** 512 512
579** 100 512
580** 2000 2048
581**
582*/
danielk197762079062007-08-15 17:08:46 +0000583static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000584 i64 offset = 0;
585 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000586 if( c ){
587 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
588 }
589 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
590 assert( offset>=c );
591 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
592 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000593}
594
595/*
drhf3a87622008-04-17 14:16:42 +0000596** Write zeros over the header of the journal file. This has the
597** effect of invalidating the journal file and committing the
598** transaction.
599*/
danielk1977df2566a2008-05-07 19:11:03 +0000600static int zeroJournalHdr(Pager *pPager, int doTruncate){
601 int rc = SQLITE_OK;
danielk197755a25a12008-09-11 10:29:15 +0000602 static const char zeroHdr[28] = {0};
drhf3a87622008-04-17 14:16:42 +0000603
danielk1977df2566a2008-05-07 19:11:03 +0000604 if( pPager->journalOff ){
danielk1977b53e4962008-06-04 06:45:59 +0000605 i64 iLimit = pPager->journalSizeLimit;
606
danielk1977df2566a2008-05-07 19:11:03 +0000607 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000608 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000609 rc = sqlite3OsTruncate(pPager->jfd, 0);
610 }else{
611 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
612 }
danielk197781620542008-06-07 05:19:37 +0000613 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000614 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
615 }
danielk1977b53e4962008-06-04 06:45:59 +0000616
617 /* At this point the transaction is committed but the write lock
618 ** is still held on the file. If there is a size limit configured for
619 ** the persistent journal and the journal file currently consumes more
620 ** space than that limit allows for, truncate it now. There is no need
621 ** to sync the file following this operation.
622 */
623 if( rc==SQLITE_OK && iLimit>0 ){
624 i64 sz;
625 rc = sqlite3OsFileSize(pPager->jfd, &sz);
626 if( rc==SQLITE_OK && sz>iLimit ){
627 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
628 }
629 }
drha06ecba2008-04-22 17:15:17 +0000630 }
drhf3a87622008-04-17 14:16:42 +0000631 return rc;
632}
633
634/*
danielk197776572402004-06-25 02:38:54 +0000635** The journal file must be open when this routine is called. A journal
636** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
637** current location.
638**
639** The format for the journal header is as follows:
640** - 8 bytes: Magic identifying journal format.
641** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
642** - 4 bytes: Random number used for page hash.
643** - 4 bytes: Initial database page count.
644** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000645** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000646**
danielk197767c007b2008-03-20 04:45:49 +0000647** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000648*/
649static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +0000650 int rc = SQLITE_OK;
651 char *zHeader = pPager->pTmpSpace;
danielk1977d92db532008-11-17 04:56:24 +0000652 u32 nHeader = pPager->pageSize;
653 u32 nWrite;
danielk1977fd7f0452008-12-17 17:30:26 +0000654 int ii;
danielk1977a664f8e2008-04-22 14:31:48 +0000655
656 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
657 nHeader = JOURNAL_HDR_SZ(pPager);
658 }
danielk197776572402004-06-25 02:38:54 +0000659
danielk1977fd7f0452008-12-17 17:30:26 +0000660 /* If there are active savepoints and any of them were created since the
661 ** most recent journal header was written, update the PagerSavepoint.iHdrOff
662 ** fields now.
663 */
664 for(ii=0; ii<pPager->nSavepoint; ii++){
665 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
666 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
667 }
danielk19774099f6e2007-03-19 11:25:20 +0000668 }
669
danielk197762079062007-08-15 17:08:46 +0000670 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000671 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000672
drh97b57482006-01-10 20:32:32 +0000673 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000674
675 /*
676 ** Write the nRec Field - the number of page records that follow this
677 ** journal header. Normally, zero is written to this value at this time.
678 ** After the records are added to the journal (and the journal synced,
679 ** if in full-sync mode), the zero is overwritten with the true number
680 ** of records (see syncJournal()).
681 **
682 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
683 ** reading the journal this value tells SQLite to assume that the
684 ** rest of the journal file contains valid page records. This assumption
685 ** is dangerous, as if a failure occured whilst writing to the journal
686 ** file it may contain some garbage data. There are two scenarios
687 ** where this risk can be ignored:
688 **
689 ** * When the pager is in no-sync mode. Corruption can follow a
690 ** power failure in this case anyway.
691 **
692 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
693 ** that garbage data is never appended to the journal file.
694 */
695 assert(pPager->fd->pMethods||pPager->noSync);
danielk1977b3175382008-10-17 18:51:52 +0000696 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
danielk19774cd2cd52007-08-23 14:48:23 +0000697 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
698 ){
699 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
700 }else{
701 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
702 }
703
drh97b57482006-01-10 20:32:32 +0000704 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +0000705 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +0000706 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
707 /* The initial database size */
danielk19773460d192008-12-27 15:23:13 +0000708 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
drh97b57482006-01-10 20:32:32 +0000709 /* The assumed sector size for this process */
710 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drh08609ce2008-11-29 22:49:23 +0000711
712 /* Initializing the tail of the buffer is not necessary. Everything
713 ** works find if the following memset() is omitted. But initializing
714 ** the memory prevents valgrind from complaining, so we are willing to
715 ** take the performance hit.
716 */
717 memset(&zHeader[sizeof(aJournalMagic)+16], 0,
718 nHeader-(sizeof(aJournalMagic)+16));
719
danielk197767c007b2008-03-20 04:45:49 +0000720 if( pPager->journalHdr==0 ){
721 /* The page size */
722 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
723 }
danielk197776572402004-06-25 02:38:54 +0000724
danielk1977a664f8e2008-04-22 14:31:48 +0000725 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
726 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
727 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
728 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +0000729 }
danielk1977a664f8e2008-04-22 14:31:48 +0000730
danielk197776572402004-06-25 02:38:54 +0000731 return rc;
732}
733
734/*
735** The journal file must be open when this is called. A journal header file
736** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
737** file. See comments above function writeJournalHdr() for a description of
738** the journal header format.
739**
740** If the header is read successfully, *nRec is set to the number of
741** page records following this header and *dbSize is set to the size of the
742** database before the transaction began, in pages. Also, pPager->cksumInit
743** is set to the value read from the journal header. SQLITE_OK is returned
744** in this case.
745**
746** If the journal header file appears to be corrupted, SQLITE_DONE is
747** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
748** cannot be read from the journal file an error code is returned.
749*/
750static int readJournalHdr(
751 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000752 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000753 u32 *pNRec,
754 u32 *pDbSize
755){
756 int rc;
757 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000758 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +0000759 int iPageSize;
danielk197776572402004-06-25 02:38:54 +0000760
danielk197762079062007-08-15 17:08:46 +0000761 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000762 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
763 return SQLITE_DONE;
764 }
danielk197762079062007-08-15 17:08:46 +0000765 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000766
danielk197762079062007-08-15 17:08:46 +0000767 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000768 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000769 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000770
771 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
772 return SQLITE_DONE;
773 }
774
danielk197762079062007-08-15 17:08:46 +0000775 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000776 if( rc ) return rc;
777
danielk197762079062007-08-15 17:08:46 +0000778 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000779 if( rc ) return rc;
780
danielk197762079062007-08-15 17:08:46 +0000781 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000782 if( rc ) return rc;
783
danielk197767c007b2008-03-20 04:45:49 +0000784 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
785 if( rc==SQLITE_OK
786 && iPageSize>=512
787 && iPageSize<=SQLITE_MAX_PAGE_SIZE
788 && ((iPageSize-1)&iPageSize)==0
789 ){
drh4f21c4a2008-12-10 22:15:00 +0000790 u16 pagesize = (u16)iPageSize;
danielk197767c007b2008-03-20 04:45:49 +0000791 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
792 }
793 if( rc ) return rc;
794
danielk197776572402004-06-25 02:38:54 +0000795 /* Update the assumed sector-size to match the value used by
796 ** the process that created this journal. If this journal was
797 ** created by a process other than this one, then this routine
798 ** is being called from within pager_playback(). The local value
799 ** of Pager.sectorSize is restored at the end of that routine.
800 */
drh98c58352008-11-07 00:24:53 +0000801 rc = read32bits(pPager->jfd, jrnlOff+12, &pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000802 if( rc ) return rc;
drh98c58352008-11-07 00:24:53 +0000803 if( (pPager->sectorSize & (pPager->sectorSize-1))!=0
804 || pPager->sectorSize>0x1000000 ){
805 return SQLITE_DONE;
806 }
danielk197776572402004-06-25 02:38:54 +0000807
808 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000809 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000810}
811
812
813/*
814** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000815** pPager at the current location. The master journal name must be the last
816** thing written to a journal file. If the pager is in full-sync mode, the
817** journal file descriptor is advanced to the next sector boundary before
818** anything is written. The format is:
819**
820** + 4 bytes: PAGER_MJ_PGNO.
821** + N bytes: length of master journal name.
822** + 4 bytes: N
823** + 4 bytes: Master journal name checksum.
824** + 8 bytes: aJournalMagic[].
825**
826** The master journal page checksum is the sum of the bytes in the master
827** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000828**
829** If zMaster is a NULL pointer (occurs for a single database transaction),
830** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000831*/
832static int writeMasterJournal(Pager *pPager, const char *zMaster){
833 int rc;
834 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000835 int i;
danielk197762079062007-08-15 17:08:46 +0000836 i64 jrnlOff;
danielk1977df2566a2008-05-07 19:11:03 +0000837 i64 jrnlSize;
drh97b57482006-01-10 20:32:32 +0000838 u32 cksum = 0;
839 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000840
danielk1977b3175382008-10-17 18:51:52 +0000841 if( !zMaster || pPager->setMaster ) return SQLITE_OK;
842 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ) return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000843 pPager->setMaster = 1;
844
drhea678832008-12-10 19:26:22 +0000845 len = sqlite3Strlen30(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000846 for(i=0; i<len; i++){
847 cksum += zMaster[i];
848 }
danielk197776572402004-06-25 02:38:54 +0000849
850 /* If in full-sync mode, advance to the next disk sector before writing
851 ** the master journal name. This is in case the previous page written to
852 ** the journal has already been synced.
853 */
854 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000855 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000856 }
danielk197762079062007-08-15 17:08:46 +0000857 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000858 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000859
danielk197762079062007-08-15 17:08:46 +0000860 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000861 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000862 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000863
danielk197762079062007-08-15 17:08:46 +0000864 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000865 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000866 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000867
drh97b57482006-01-10 20:32:32 +0000868 put32bits(zBuf, len);
869 put32bits(&zBuf[4], cksum);
870 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000871 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +0000872 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +0000873 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +0000874
875 /* If the pager is in peristent-journal mode, then the physical
876 ** journal-file may extend past the end of the master-journal name
877 ** and 8 bytes of magic data just written to the file. This is
878 ** dangerous because the code to rollback a hot-journal file
879 ** will not be able to find the master-journal name to determine
880 ** whether or not the journal is hot.
881 **
882 ** Easiest thing to do in this scenario is to truncate the journal
883 ** file to the required size.
884 */
885 if( (rc==SQLITE_OK)
886 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
887 && jrnlSize>jrnlOff
888 ){
889 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
890 }
danielk197776572402004-06-25 02:38:54 +0000891 return rc;
892}
893
894/*
drhed7c8552001-04-11 14:29:21 +0000895** Find a page in the hash table given its page number. Return
896** a pointer to the page or NULL if not found.
897*/
drhd9b02572001-04-15 00:37:09 +0000898static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000899 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000900 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
drhed7c8552001-04-11 14:29:21 +0000901 return p;
902}
903
904/*
danielk1977e180dd92007-04-05 17:15:52 +0000905** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +0000906** sets the state of the pager back to what it was when it was first
907** opened. Any outstanding pages are invalidated and subsequent attempts
908** to access those pages will likely result in a coredump.
909*/
drhd9b02572001-04-15 00:37:09 +0000910static void pager_reset(Pager *pPager){
danielk1977efaaf572006-01-16 11:29:19 +0000911 if( pPager->errCode ) return;
danielk19778c0a7912008-08-20 14:49:23 +0000912 sqlite3PcacheClear(pPager->pPCache);
danielk1977e277be02007-03-23 18:12:06 +0000913}
914
danielk197734cf35d2008-12-18 18:31:38 +0000915/*
916** Free all structures in the Pager.aSavepoint[] array and set both
917** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
918** if it is open and the pager is not in exclusive mode.
919*/
danielk1977fd7f0452008-12-17 17:30:26 +0000920static void releaseAllSavepoint(Pager *pPager){
921 int ii;
922 for(ii=0; ii<pPager->nSavepoint; ii++){
923 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
924 }
925 if( !pPager->exclusiveMode ){
926 sqlite3OsClose(pPager->sjfd);
927 }
928 sqlite3_free(pPager->aSavepoint);
929 pPager->aSavepoint = 0;
930 pPager->nSavepoint = 0;
danielk197767ddef62008-12-23 19:15:56 +0000931 pPager->stmtNRec = 0;
danielk1977fd7f0452008-12-17 17:30:26 +0000932}
933
danielk197734cf35d2008-12-18 18:31:38 +0000934/*
935** Set the bit number pgno in the PagerSavepoint.pInSavepoint bitvecs of
936** all open savepoints.
937*/
danielk1977fd7f0452008-12-17 17:30:26 +0000938static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
drh7539b6b2009-01-02 21:39:39 +0000939 int ii; /* Loop counter */
940 int rc = SQLITE_OK; /* Result code */
941
danielk1977fd7f0452008-12-17 17:30:26 +0000942 for(ii=0; ii<pPager->nSavepoint; ii++){
943 PagerSavepoint *p = &pPager->aSavepoint[ii];
944 if( pgno<=p->nOrig ){
drh7539b6b2009-01-02 21:39:39 +0000945 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
946 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
danielk1977fd7f0452008-12-17 17:30:26 +0000947 }
948 }
drh7539b6b2009-01-02 21:39:39 +0000949 return rc;
danielk1977fd7f0452008-12-17 17:30:26 +0000950}
951
danielk1977e277be02007-03-23 18:12:06 +0000952/*
danielk1977ae72d982007-10-03 08:46:44 +0000953** Unlock the database file.
954**
955** If the pager is currently in error state, discard the contents of
956** the cache and reset the Pager structure internal state. If there is
957** an open journal-file, then the next time a shared-lock is obtained
958** on the pager file (by this or any other process), it will be
959** treated as a hot-journal and rolled back.
960*/
961static void pager_unlock(Pager *pPager){
962 if( !pPager->exclusiveMode ){
danielk1977b3175382008-10-17 18:51:52 +0000963 int rc = osUnlock(pPager->fd, NO_LOCK);
964 if( rc ) pPager->errCode = rc;
danielk1977d92db532008-11-17 04:56:24 +0000965 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +0000966 IOTRACE(("UNLOCK %p\n", pPager))
danielk1977ae72d982007-10-03 08:46:44 +0000967
danielk1977b3175382008-10-17 18:51:52 +0000968 /* Always close the journal file when dropping the database lock.
969 ** Otherwise, another connection with journal_mode=delete might
970 ** delete the file out from under us.
971 */
972 if( pPager->journalOpen ){
973 sqlite3OsClose(pPager->jfd);
974 pPager->journalOpen = 0;
975 sqlite3BitvecDestroy(pPager->pInJournal);
976 pPager->pInJournal = 0;
977 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
978 pPager->pAlwaysRollback = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000979 }
980
danielk1977b3175382008-10-17 18:51:52 +0000981 /* If Pager.errCode is set, the contents of the pager cache cannot be
982 ** trusted. Now that the pager file is unlocked, the contents of the
983 ** cache can be discarded and the error code safely cleared.
984 */
985 if( pPager->errCode ){
986 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
987 pager_reset(pPager);
danielk1977fd7f0452008-12-17 17:30:26 +0000988 releaseAllSavepoint(pPager);
danielk1977b3175382008-10-17 18:51:52 +0000989 pPager->journalOff = 0;
990 pPager->journalStarted = 0;
danielk19773460d192008-12-27 15:23:13 +0000991 pPager->dbOrigSize = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000992 }
danielk1977b3175382008-10-17 18:51:52 +0000993
994 pPager->state = PAGER_UNLOCK;
995 pPager->changeCountDone = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000996 }
997}
998
999/*
1000** Execute a rollback if a transaction is active and unlock the
1001** database file. If the pager has already entered the error state,
1002** do not attempt the rollback.
1003*/
1004static void pagerUnlockAndRollback(Pager *p){
danielk1977ae72d982007-10-03 08:46:44 +00001005 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +00001006 sqlite3BeginBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001007 sqlite3PagerRollback(p);
danielk19772d1d86f2008-06-20 14:59:51 +00001008 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001009 }
1010 pager_unlock(p);
danielk1977ae72d982007-10-03 08:46:44 +00001011}
1012
1013/*
drh80e35f42007-03-30 14:06:34 +00001014** This routine ends a transaction. A transaction is ended by either
1015** a COMMIT or a ROLLBACK.
1016**
drhed7c8552001-04-11 14:29:21 +00001017** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001018** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1019** the database lock and acquires a SHARED lock in its place if that is
1020** the appropriate thing to do. Release locks usually is appropriate,
1021** unless we are in exclusive access mode or unless this is a
1022** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1023**
1024** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001025**
1026** TODO: Consider keeping the journal file open for temporary databases.
1027** This might give a performance improvement on windows where opening
1028** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001029*/
danielk1977df2566a2008-05-07 19:11:03 +00001030static int pager_end_transaction(Pager *pPager, int hasMaster){
danielk197741483462007-03-24 16:45:04 +00001031 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001032 int rc2 = SQLITE_OK;
drha6abd042004-06-09 17:37:22 +00001033 if( pPager->state<PAGER_RESERVED ){
1034 return SQLITE_OK;
1035 }
danielk1977fd7f0452008-12-17 17:30:26 +00001036 releaseAllSavepoint(pPager);
drhda47d772002-12-02 04:25:19 +00001037 if( pPager->journalOpen ){
danielk1977b3175382008-10-17 18:51:52 +00001038 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
1039 int isMemoryJournal = sqlite3IsMemJournal(pPager->jfd);
1040 sqlite3OsClose(pPager->jfd);
1041 pPager->journalOpen = 0;
1042 if( !isMemoryJournal ){
1043 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
1044 }
1045 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE
drh04335882008-09-26 21:08:08 +00001046 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){
1047 pPager->journalOff = 0;
1048 pPager->journalStarted = 0;
1049 }else if( pPager->exclusiveMode
danielk197793f7af92008-05-09 16:57:50 +00001050 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1051 ){
1052 rc = zeroJournalHdr(pPager, hasMaster);
1053 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +00001054 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001055 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001056 }else{
drh04335882008-09-26 21:08:08 +00001057 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || rc );
danielk1977b4b47412007-08-17 15:53:36 +00001058 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001059 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +00001060 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00001061 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001062 }
danielk197741483462007-03-24 16:45:04 +00001063 }
drhf5e7bb52008-02-18 14:47:33 +00001064 sqlite3BitvecDestroy(pPager->pInJournal);
1065 pPager->pInJournal = 0;
danielk1977a1fa00d2008-08-27 15:16:33 +00001066 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
1067 pPager->pAlwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001068#ifdef SQLITE_CHECK_PAGES
danielk1977bc2ca9e2008-11-13 14:28:28 +00001069 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
danielk19773c407372005-02-15 02:54:14 +00001070#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +00001071 sqlite3PcacheCleanAll(pPager->pPCache);
danielk1977ef317ab2004-06-23 10:43:10 +00001072 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001073 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001074 }else{
drhf5e7bb52008-02-18 14:47:33 +00001075 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001076 }
danielk1977979f38e2007-03-27 16:19:51 +00001077
danielk197741483462007-03-24 16:45:04 +00001078 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001079 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001080 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001081 }else if( pPager->state==PAGER_SYNCED ){
1082 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001083 }
danielk19773460d192008-12-27 15:23:13 +00001084 pPager->dbOrigSize = 0;
danielk197776572402004-06-25 02:38:54 +00001085 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001086 pPager->needSync = 0;
danielk19778c0a7912008-08-20 14:49:23 +00001087 /* lruListSetFirstSynced(pPager); */
danielk1977b3175382008-10-17 18:51:52 +00001088 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00001089 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00001090 }
drhd138c012008-05-13 00:58:18 +00001091 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001092
1093 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001094}
1095
drhed7c8552001-04-11 14:29:21 +00001096/*
drh968af522003-02-11 14:55:40 +00001097** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001098**
1099** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001100** random initial value and the page number. We experimented with
1101** a checksum of the entire data, but that was found to be too slow.
1102**
1103** Note that the page number is stored at the beginning of data and
1104** the checksum is stored at the end. This is important. If journal
1105** corruption occurs due to a power failure, the most likely scenario
1106** is that one end or the other of the record will be changed. It is
1107** much less likely that the two ends of the journal record will be
1108** correct and the middle be corrupt. Thus, this "checksum" scheme,
1109** though fast and simple, catches the mostly likely kind of corruption.
1110**
1111** FIX ME: Consider adding every 200th (or so) byte of the data to the
1112** checksum. That way if a single page spans 3 or more disk sectors and
1113** only the middle sector is corrupt, we will still have a reasonable
1114** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001115*/
drh74161702006-02-24 02:53:49 +00001116static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001117 u32 cksum = pPager->cksumInit;
1118 int i = pPager->pageSize-200;
1119 while( i>0 ){
1120 cksum += aData[i];
1121 i -= 200;
1122 }
drh968af522003-02-11 14:55:40 +00001123 return cksum;
1124}
1125
1126/*
drhfa86c412002-02-02 15:01:15 +00001127** Read a single page from the journal file opened on file descriptor
1128** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001129**
drhc13148f2008-08-27 18:03:20 +00001130** The isMainJrnl flag is true if this is the main rollback journal and
1131** false for the statement journal. The main rollback journal uses
1132** checksums - the statement journal does not.
drhfa86c412002-02-02 15:01:15 +00001133*/
danielk197762079062007-08-15 17:08:46 +00001134static int pager_playback_one_page(
danielk1977fd7f0452008-12-17 17:30:26 +00001135 Pager *pPager, /* The pager being played back */
1136 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
1137 i64 offset, /* Offset of record to playback */
danielk1977ecfef982008-12-20 08:39:57 +00001138 int isSavepnt, /* True for a savepoint rollback */
danielk1977fd7f0452008-12-17 17:30:26 +00001139 Bitvec *pDone /* Bitvec of pages already played back */
danielk197762079062007-08-15 17:08:46 +00001140){
drhfa86c412002-02-02 15:01:15 +00001141 int rc;
drhae2b40c2004-06-09 19:03:54 +00001142 PgHdr *pPg; /* An existing page in the cache */
1143 Pgno pgno; /* The page number of a page in journal */
1144 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001145 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
danielk1977fd7f0452008-12-17 17:30:26 +00001146 sqlite3_file *jfd = (isMainJrnl ? pPager->jfd : pPager->sjfd);
drhfa86c412002-02-02 15:01:15 +00001147
danielk1977fd7f0452008-12-17 17:30:26 +00001148 /* The temp storage must be allocated at this point */
danielk19778186df82007-03-06 13:45:59 +00001149 assert( aData );
danielk1977488af092008-12-19 16:31:11 +00001150 assert( isMainJrnl || pDone );
danielk1977ecfef982008-12-20 08:39:57 +00001151 assert( isSavepnt || pDone==0 );
drh96362842005-03-20 22:47:56 +00001152
danielk197762079062007-08-15 17:08:46 +00001153 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001154 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001155 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001156 if( rc!=SQLITE_OK ) return rc;
danielk1977f2c31ad2009-01-06 13:40:08 +00001157 pPager->journalOff += pPager->pageSize + 4 + (isMainJrnl?4:0);
drhfa86c412002-02-02 15:01:15 +00001158
drh968af522003-02-11 14:55:40 +00001159 /* Sanity checking on the page. This is more important that I originally
1160 ** thought. If a power failure occurs while the journal is being written,
1161 ** it could cause invalid data to be written into the journal. We need to
1162 ** detect this invalid data (with high probability) and ignore it.
1163 */
danielk197775edc162004-06-26 01:48:18 +00001164 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001165 return SQLITE_DONE;
1166 }
danielk1977fd7f0452008-12-17 17:30:26 +00001167 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
drh968af522003-02-11 14:55:40 +00001168 return SQLITE_OK;
1169 }
drhc13148f2008-08-27 18:03:20 +00001170 if( isMainJrnl ){
danielk197762079062007-08-15 17:08:46 +00001171 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001172 if( rc ) return rc;
danielk1977ecfef982008-12-20 08:39:57 +00001173 if( !isSavepnt && pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001174 return SQLITE_DONE;
1175 }
1176 }
danielk1977fd7f0452008-12-17 17:30:26 +00001177 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno)) ){
1178 return rc;
1179 }
drhfa86c412002-02-02 15:01:15 +00001180
danielk1977aa5ccdf2004-06-14 05:10:42 +00001181 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001182
1183 /* If the pager is in RESERVED state, then there must be a copy of this
1184 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001185 ** not the database file. The page is left marked dirty in this case.
1186 **
danielk19772df71c72007-05-24 07:22:42 +00001187 ** An exception to the above rule: If the database is in no-sync mode
1188 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001189 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1190 ** during a Movepage() call, then the page may not be in the cache
1191 ** either. So the condition described in the above paragraph is not
1192 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001193 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001194 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1195 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001196 **
1197 ** Ticket #1171: The statement journal might contain page content that is
1198 ** different from the page content at the start of the transaction.
1199 ** This occurs when a page is changed prior to the start of a statement
1200 ** then changed again within the statement. When rolling back such a
1201 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001202 ** for certain that original page contents are synced into the main rollback
1203 ** journal. Otherwise, a power loss might leave modified data in the
1204 ** database file without an entry in the rollback journal that can
1205 ** restore the database to its original form. Two conditions must be
1206 ** met before writing to the database files. (1) the database must be
1207 ** locked. (2) we know that the original page content is fully synced
1208 ** in the main journal either because the page is not in cache or else
1209 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001210 **
1211 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1212 ** is possible to fail a statement on a database that does not yet exist.
1213 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001214 */
drhae2b40c2004-06-09 19:03:54 +00001215 pPg = pager_lookup(pPager, pgno);
drh30d53702009-01-06 15:58:57 +00001216 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
danielk1977ecfef982008-12-20 08:39:57 +00001217 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData),
1218 (isMainJrnl?"main-journal":"sub-journal")
drh30d53702009-01-06 15:58:57 +00001219 ));
danielk19778c0a7912008-08-20 14:49:23 +00001220 if( (pPager->state>=PAGER_EXCLUSIVE)
1221 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
1222 && (pPager->fd->pMethods)
1223 ){
drh281b21d2008-08-22 12:57:08 +00001224 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
1225 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
danielk19773460d192008-12-27 15:23:13 +00001226 if( pgno>pPager->dbFileSize ){
1227 pPager->dbFileSize = pgno;
1228 }
danielk1977f2c31ad2009-01-06 13:40:08 +00001229 }else if( !isMainJrnl && pPg==0 ){
1230 /* If this is a rollback of a savepoint and data was not written to
1231 ** the database and the page is not in-memory, there is a potential
1232 ** problem. When the page is next fetched by the b-tree layer, it
1233 ** will be read from the database file, which may or may not be
1234 ** current.
1235 **
1236 ** There are a couple of different ways this can happen. All are quite
danielk1977401b65e2009-01-06 14:34:34 +00001237 ** obscure. When running in synchronous mode, this can only happen
danielk1977f2c31ad2009-01-06 13:40:08 +00001238 ** if the page is on the free-list at the start of the transaction, then
1239 ** populated, then moved using sqlite3PagerMovepage().
1240 **
1241 ** The solution is to add an in-memory page to the cache containing
1242 ** the data just read from the sub-journal. Mark the page as dirty
1243 ** and if the pager requires a journal-sync, then mark the page as
1244 ** requiring a journal-sync before it is written.
1245 */
1246 assert( isSavepnt );
1247 if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1)) ){
1248 return rc;
1249 }
1250 pPg->flags &= ~PGHDR_NEED_READ;
1251 sqlite3PcacheMakeDirty(pPg);
danielk1977a3f3a5f2004-06-10 04:32:16 +00001252 }
drhfa86c412002-02-02 15:01:15 +00001253 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001254 /* No page should ever be explicitly rolled back that is in use, except
1255 ** for page 1 which is held in use in order to keep the lock on the
1256 ** database active. However such a page may be rolled back as a result
1257 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001258 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001259 */
drhb6f41482004-05-14 01:58:11 +00001260 void *pData;
danielk19778c0a7912008-08-20 14:49:23 +00001261 pData = pPg->pData;
drhae2b40c2004-06-09 19:03:54 +00001262 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001263 if( pPager->xReiniter ){
danielk1977eaa06f62008-09-18 17:34:44 +00001264 pPager->xReiniter(pPg);
drhde647132004-05-07 17:57:49 +00001265 }
danielk1977ecfef982008-12-20 08:39:57 +00001266 if( isMainJrnl && (!isSavepnt || pPager->journalOff<=pPager->journalHdr) ){
danielk1977488af092008-12-19 16:31:11 +00001267 /* If the contents of this page were just restored from the main
1268 ** journal file, then its content must be as they were when the
1269 ** transaction was first opened. In this case we can mark the page
1270 ** as clean, since there will be no need to write it out to the.
1271 **
1272 ** There is one exception to this rule. If the page is being rolled
1273 ** back as part of a savepoint (or statement) rollback from an
1274 ** unsynced portion of the main journal file, then it is not safe
1275 ** to mark the page as clean. This is because marking the page as
1276 ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
1277 ** already in the journal file (recorded in Pager.pInJournal) and
1278 ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
1279 ** again within this transaction, it will be marked as dirty but
1280 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
1281 ** be written out into the database file before its journal file
1282 ** segment is synced. If a crash occurs during or following this,
1283 ** database corruption may ensue.
1284 */
drhc047b9f2008-11-21 03:23:43 +00001285 sqlite3PcacheMakeClean(pPg);
1286 }
danielk19773c407372005-02-15 02:54:14 +00001287#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001288 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001289#endif
drh86a88112007-04-16 15:02:19 +00001290 /* If this was page 1, then restore the value of Pager.dbFileVers.
1291 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001292 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001293 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001294 }
drh86a88112007-04-16 15:02:19 +00001295
1296 /* Decode the page just read from disk */
1297 CODEC1(pPager, pData, pPg->pgno, 3);
danielk19778c0a7912008-08-20 14:49:23 +00001298 sqlite3PcacheRelease(pPg);
drhfa86c412002-02-02 15:01:15 +00001299 }
1300 return rc;
1301}
1302
1303/*
danielk197713adf8a2004-06-03 16:08:41 +00001304** Parameter zMaster is the name of a master journal file. A single journal
1305** file that referred to the master journal file has just been rolled back.
1306** This routine checks if it is possible to delete the master journal file,
1307** and does so if it is.
drh726de592004-06-10 23:35:50 +00001308**
danielk197765839c62007-08-30 08:08:17 +00001309** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1310** available for use within this function.
1311**
1312**
drh726de592004-06-10 23:35:50 +00001313** The master journal file contains the names of all child journals.
1314** To tell if a master journal can be deleted, check to each of the
1315** children. If all children are either missing or do not refer to
1316** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001317*/
danielk1977b4b47412007-08-17 15:53:36 +00001318static int pager_delmaster(Pager *pPager, const char *zMaster){
1319 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001320 int rc;
1321 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001322 sqlite3_file *pMaster;
1323 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001324 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001325 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001326
1327 /* Open the master journal file exclusively in case some other process
1328 ** is running this routine also. Not that it makes too much difference.
1329 */
drhe5ae5732008-06-15 02:51:47 +00001330 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001331 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001332 if( !pMaster ){
1333 rc = SQLITE_NOMEM;
1334 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001335 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1336 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001337 }
danielk197713adf8a2004-06-03 16:08:41 +00001338 if( rc!=SQLITE_OK ) goto delmaster_out;
1339 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001340
1341 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001342 if( rc!=SQLITE_OK ) goto delmaster_out;
1343
1344 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001345 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001346 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001347 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001348
1349 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001350 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001351 */
drh4f21c4a2008-12-10 22:15:00 +00001352 zMasterJournal = (char *)sqlite3Malloc((int)nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001353 if( !zMasterJournal ){
1354 rc = SQLITE_NOMEM;
1355 goto delmaster_out;
1356 }
danielk197765839c62007-08-30 08:08:17 +00001357 zMasterPtr = &zMasterJournal[nMasterJournal];
drh4f21c4a2008-12-10 22:15:00 +00001358 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001359 if( rc!=SQLITE_OK ) goto delmaster_out;
1360
danielk19775865e3d2004-06-14 06:03:57 +00001361 zJournal = zMasterJournal;
1362 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001363 int exists;
1364 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1365 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001366 goto delmaster_out;
1367 }
danielk1977861f7452008-06-05 11:39:11 +00001368 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001369 /* One of the journals pointed to by the master journal exists.
1370 ** Open it and check if it points at the master journal. If
1371 ** so, return without deleting the master journal file.
1372 */
drh3b7b78b2004-11-24 01:16:43 +00001373 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001374 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1375 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001376 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001377 goto delmaster_out;
1378 }
danielk197713adf8a2004-06-03 16:08:41 +00001379
danielk197765839c62007-08-30 08:08:17 +00001380 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001381 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001382 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001383 goto delmaster_out;
1384 }
danielk197776572402004-06-25 02:38:54 +00001385
danielk197765839c62007-08-30 08:08:17 +00001386 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001387 if( c ){
danielk197776572402004-06-25 02:38:54 +00001388 /* We have a match. Do not delete the master journal file. */
1389 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001390 }
1391 }
drhea678832008-12-10 19:26:22 +00001392 zJournal += (sqlite3Strlen30(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001393 }
1394 }
1395
danielk1977fee2d252007-08-18 10:59:19 +00001396 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001397
1398delmaster_out:
1399 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001400 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001401 }
1402 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001403 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001404 }
danielk1977b4b47412007-08-17 15:53:36 +00001405 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001406 return rc;
1407}
1408
drha6abd042004-06-09 17:37:22 +00001409
drha6abd042004-06-09 17:37:22 +00001410/*
drhcb4c40b2004-08-18 19:09:43 +00001411** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001412** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001413**
1414** Might might be the case that the file on disk is smaller than nPage.
1415** This can happen, for example, if we are in the middle of a transaction
1416** which has extended the file size and the new pages are still all held
1417** in cache, then an INSERT or UPDATE does a statement rollback. Some
1418** operating system implementations can get confused if you try to
1419** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001420** so detect this case and write a single zero byte to the end of the new
1421** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001422*/
danielk1977d92db532008-11-17 04:56:24 +00001423static int pager_truncate(Pager *pPager, Pgno nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001424 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001425 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001426 i64 currentSize, newSize;
1427 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1428 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001429 if( rc==SQLITE_OK && currentSize!=newSize ){
1430 if( currentSize>newSize ){
1431 rc = sqlite3OsTruncate(pPager->fd, newSize);
1432 }else{
1433 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1434 }
danielk19773460d192008-12-27 15:23:13 +00001435 if( rc==SQLITE_OK ){
1436 pPager->dbFileSize = nPage;
1437 }
drh7fe3f7e2007-11-29 18:44:27 +00001438 }
danielk1977e180dd92007-04-05 17:15:52 +00001439 }
1440 if( rc==SQLITE_OK ){
1441 pPager->dbSize = nPage;
danielk1977401b65e2009-01-06 14:34:34 +00001442 sqlite3PcacheTruncate(pPager->pPCache, nPage);
danielk1977e180dd92007-04-05 17:15:52 +00001443 }
1444 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001445}
1446
1447/*
drhc80f0582007-05-01 16:59:48 +00001448** Set the sectorSize for the given pager.
1449**
drh334c80d2008-03-28 17:41:13 +00001450** The sector size is at least as big as the sector size reported
1451** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001452*/
1453static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001454 assert(pPager->fd->pMethods||pPager->tempFile);
1455 if( !pPager->tempFile ){
1456 /* Sector size doesn't matter for temporary files. Also, the file
1457 ** may not have been opened yet, in whcih case the OsSectorSize()
1458 ** call will segfault.
1459 */
1460 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1461 }
drh334c80d2008-03-28 17:41:13 +00001462 if( pPager->sectorSize<512 ){
1463 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001464 }
1465}
1466
1467/*
drhed7c8552001-04-11 14:29:21 +00001468** Playback the journal and thus restore the database file to
1469** the state it was in before we started making changes.
1470**
drh34e79ce2004-02-08 06:05:46 +00001471** The journal file format is as follows:
1472**
drhae2b40c2004-06-09 19:03:54 +00001473** (1) 8 byte prefix. A copy of aJournalMagic[].
1474** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001475** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001476** number of page records from the journal size.
1477** (3) 4 byte big-endian integer which is the initial value for the
1478** sanity checksum.
1479** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001480** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001481** (5) 4 byte big-endian integer which is the sector size. The header
1482** is this many bytes in size.
1483** (6) 4 byte big-endian integer which is the page case.
1484** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001485** name. The value may be zero (indicate that there is no master
1486** journal.)
drh334c80d2008-03-28 17:41:13 +00001487** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001488** and might be shorter than the value read from (5). If the first byte
1489** of the name is \000 then there is no master journal. The master
1490** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001491** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001492** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001493** + pPager->pageSize bytes of data.
1494** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001495**
drh334c80d2008-03-28 17:41:13 +00001496** When we speak of the journal header, we mean the first 8 items above.
1497** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001498**
1499** Call the value from the second bullet "nRec". nRec is the number of
1500** valid page entries in the journal. In most cases, you can compute the
1501** value of nRec from the size of the journal file. But if a power
1502** failure occurred while the journal was being written, it could be the
1503** case that the size of the journal file had already been increased but
1504** the extra entries had not yet made it safely to disk. In such a case,
1505** the value of nRec computed from the file size would be too large. For
1506** that reason, we always use the nRec value in the header.
1507**
1508** If the nRec value is 0xffffffff it means that nRec should be computed
1509** from the file size. This value is used when the user selects the
1510** no-sync option for the journal. A power failure could lead to corruption
1511** in this case. But for things like temporary table (which will be
1512** deleted when the power is restored) we don't care.
1513**
drhd9b02572001-04-15 00:37:09 +00001514** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001515** journal file then all pages up to the first corrupted page are rolled
1516** back (or no pages if the journal header is corrupted). The journal file
1517** is then deleted and SQLITE_OK returned, just as if no corruption had
1518** been encountered.
1519**
1520** If an I/O or malloc() error occurs, the journal-file is not deleted
1521** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001522*/
danielk1977e277be02007-03-23 18:12:06 +00001523static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001524 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001525 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001526 u32 nRec; /* Number of Records in the journal */
shane0b8d2762008-07-22 05:18:00 +00001527 u32 u; /* Unsigned loop counter */
drhd9b02572001-04-15 00:37:09 +00001528 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001529 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001530 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001531 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001532
drhc3a64ba2001-11-22 00:01:27 +00001533 /* Figure out how many records are in the journal. Abort early if
1534 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001535 */
drh8cfbf082001-09-19 13:22:39 +00001536 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001537 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001538 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001539 goto end_playback;
1540 }
drh240c5792004-02-08 00:40:52 +00001541
danielk197776572402004-06-25 02:38:54 +00001542 /* Read the master journal name from the journal, if it is present.
1543 ** If a master journal file name is specified, but the file is not
1544 ** present on disk, then the journal is not hot and does not need to be
1545 ** played back.
drh240c5792004-02-08 00:40:52 +00001546 */
danielk197765839c62007-08-30 08:08:17 +00001547 zMaster = pPager->pTmpSpace;
1548 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001549 if( rc==SQLITE_OK && zMaster[0] ){
1550 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001551 }
danielk197765839c62007-08-30 08:08:17 +00001552 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001553 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001554 goto end_playback;
1555 }
1556 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001557
danielk197776572402004-06-25 02:38:54 +00001558 /* This loop terminates either when the readJournalHdr() call returns
1559 ** SQLITE_DONE or an IO error occurs. */
1560 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001561
danielk197776572402004-06-25 02:38:54 +00001562 /* Read the next journal header from the journal file. If there are
1563 ** not enough bytes left in the journal file for a complete header, or
1564 ** it is corrupted, then a process must of failed while writing it.
1565 ** This indicates nothing more needs to be rolled back.
1566 */
1567 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1568 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001569 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001570 rc = SQLITE_OK;
1571 }
danielk197776572402004-06-25 02:38:54 +00001572 goto end_playback;
1573 }
1574
1575 /* If nRec is 0xffffffff, then this journal was created by a process
1576 ** working in no-sync mode. This means that the rest of the journal
1577 ** file consists of pages, there are no more journal headers. Compute
1578 ** the value of nRec based on this assumption.
1579 */
1580 if( nRec==0xffffffff ){
1581 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
drh4f21c4a2008-12-10 22:15:00 +00001582 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
danielk197776572402004-06-25 02:38:54 +00001583 }
1584
danielk1977e277be02007-03-23 18:12:06 +00001585 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001586 ** process and if this is the final header in the journal, then it means
1587 ** that this part of the journal was being filled but has not yet been
1588 ** synced to disk. Compute the number of pages based on the remaining
1589 ** size of the file.
1590 **
1591 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001592 */
drh8940f4e2007-08-11 00:26:20 +00001593 if( nRec==0 && !isHot &&
1594 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
drh4f21c4a2008-12-10 22:15:00 +00001595 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
danielk1977e277be02007-03-23 18:12:06 +00001596 }
1597
danielk197776572402004-06-25 02:38:54 +00001598 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001599 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001600 */
danielk1977e180dd92007-04-05 17:15:52 +00001601 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001602 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001603 if( rc!=SQLITE_OK ){
1604 goto end_playback;
1605 }
danielk197776572402004-06-25 02:38:54 +00001606 }
1607
danielk197776572402004-06-25 02:38:54 +00001608 /* Copy original pages out of the journal and back into the database file.
1609 */
shane0b8d2762008-07-22 05:18:00 +00001610 for(u=0; u<nRec; u++){
danielk1977ecfef982008-12-20 08:39:57 +00001611 rc = pager_playback_one_page(pPager, 1, pPager->journalOff, 0, 0);
danielk197776572402004-06-25 02:38:54 +00001612 if( rc!=SQLITE_OK ){
1613 if( rc==SQLITE_DONE ){
1614 rc = SQLITE_OK;
1615 pPager->journalOff = szJ;
1616 break;
1617 }else{
drha9625ea2008-09-03 00:08:29 +00001618 /* If we are unable to rollback, then the database is probably
1619 ** going to end up being corrupt. It is corrupt to us, anyhow.
1620 ** Perhaps the next process to come along can fix it....
1621 */
danielk197798c21902008-09-23 16:41:29 +00001622 rc = SQLITE_CORRUPT_BKPT;
danielk197776572402004-06-25 02:38:54 +00001623 goto end_playback;
1624 }
1625 }
drh968af522003-02-11 14:55:40 +00001626 }
drhed7c8552001-04-11 14:29:21 +00001627 }
drh580eeaf2006-02-24 03:09:37 +00001628 /*NOTREACHED*/
1629 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001630
1631end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001632 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001633 zMaster = pPager->pTmpSpace;
1634 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1635 }
1636 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001637 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001638 }
danielk1977c56774e2008-10-07 11:51:20 +00001639 if( rc==SQLITE_OK && zMaster[0] && res ){
danielk1977979f38e2007-03-27 16:19:51 +00001640 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001641 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001642 */
danielk197765839c62007-08-30 08:08:17 +00001643 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001644 }
danielk197776572402004-06-25 02:38:54 +00001645
1646 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001647 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001648 ** value. Reset it to the correct value for this process.
1649 */
drhc80f0582007-05-01 16:59:48 +00001650 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001651 return rc;
drhed7c8552001-04-11 14:29:21 +00001652}
1653
1654/*
danielk1977fd7f0452008-12-17 17:30:26 +00001655** Playback a savepoint.
drhfa86c412002-02-02 15:01:15 +00001656*/
danielk1977fd7f0452008-12-17 17:30:26 +00001657static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
drheb206252004-10-01 02:00:31 +00001658 i64 szJ; /* Size of the full journal */
danielk1977fd7f0452008-12-17 17:30:26 +00001659 i64 iHdrOff; /* End of first segment of main-journal records */
1660 Pgno ii; /* Loop counter */
danielk1977f2c31ad2009-01-06 13:40:08 +00001661 int rc = SQLITE_OK; /* Return code */
danielk1977fd7f0452008-12-17 17:30:26 +00001662 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
drhfa86c412002-02-02 15:01:15 +00001663
danielk1977fd7f0452008-12-17 17:30:26 +00001664 /* Allocate a bitvec to use to store the set of pages rolled back */
1665 if( pSavepoint ){
1666 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
1667 if( !pDone ){
1668 return SQLITE_NOMEM;
1669 }
danielk197776572402004-06-25 02:38:54 +00001670 }
danielk1977fd7f0452008-12-17 17:30:26 +00001671
1672 /* Truncate the database back to the size it was before the
1673 ** savepoint being reverted was opened.
drhfa86c412002-02-02 15:01:15 +00001674 */
danielk1977f2c31ad2009-01-06 13:40:08 +00001675 pPager->dbSize = pSavepoint?pSavepoint->nOrig:pPager->dbOrigSize;
drh1aa2d8b2007-01-03 15:34:29 +00001676 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001677
danielk1977fd7f0452008-12-17 17:30:26 +00001678 /* Now roll back all main journal file records that occur after byte
1679 ** byte offset PagerSavepoint.iOffset that have a page number less than
1680 ** or equal to PagerSavepoint.nOrig. As each record is played back,
1681 ** the corresponding bit in bitvec PagerSavepoint.pInSavepoint is
1682 ** cleared.
drhfa86c412002-02-02 15:01:15 +00001683 */
danielk1977fd7f0452008-12-17 17:30:26 +00001684 szJ = pPager->journalOff;
1685 if( pSavepoint ){
1686 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
1687 pPager->journalOff = pSavepoint->iOffset;
1688 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
danielk1977ecfef982008-12-20 08:39:57 +00001689 rc = pager_playback_one_page(pPager, 1, pPager->journalOff, 1, pDone);
danielk1977fd7f0452008-12-17 17:30:26 +00001690 assert( rc!=SQLITE_DONE );
1691 }
1692 }else{
1693 pPager->journalOff = 0;
drhfa86c412002-02-02 15:01:15 +00001694 }
danielk1977fd7f0452008-12-17 17:30:26 +00001695 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
danielk1977c81806f2009-01-01 15:20:37 +00001696 u32 nJRec = 0; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001697 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001698 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
danielk1977fd7f0452008-12-17 17:30:26 +00001699 assert( rc!=SQLITE_DONE );
danielk1977f0113002006-01-24 12:09:17 +00001700 if( nJRec==0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00001701 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001702 }
danielk197712dd5492008-12-18 15:45:07 +00001703 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
danielk1977ecfef982008-12-20 08:39:57 +00001704 rc = pager_playback_one_page(pPager, 1, pPager->journalOff, 1, pDone);
danielk197776572402004-06-25 02:38:54 +00001705 assert( rc!=SQLITE_DONE );
danielk1977fd7f0452008-12-17 17:30:26 +00001706 }
1707 }
1708 assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
1709
1710 /* Now roll back pages from the sub-journal. */
1711 if( pSavepoint ){
drh49b9d332009-01-02 18:10:42 +00001712 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK&&ii<(u32)pPager->stmtNRec; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00001713 i64 offset = ii*(4+pPager->pageSize);
danielk1977ecfef982008-12-20 08:39:57 +00001714 rc = pager_playback_one_page(pPager, 0, offset, 1, pDone);
danielk1977fd7f0452008-12-17 17:30:26 +00001715 assert( rc!=SQLITE_DONE );
danielk197776572402004-06-25 02:38:54 +00001716 }
drhfa86c412002-02-02 15:01:15 +00001717 }
danielk197776572402004-06-25 02:38:54 +00001718
danielk1977fd7f0452008-12-17 17:30:26 +00001719 sqlite3BitvecDestroy(pDone);
1720 if( rc==SQLITE_OK ){
danielk197775edc162004-06-26 01:48:18 +00001721 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001722 }
1723 return rc;
1724}
1725
1726/*
drhf57b14a2001-09-14 18:54:08 +00001727** Change the maximum number of in-memory pages that are allowed.
1728*/
danielk19773b8a05f2007-03-19 17:44:26 +00001729void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +00001730 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001731}
1732
1733/*
drh973b6e32003-02-12 14:09:42 +00001734** Adjust the robustness of the database to damage due to OS crashes
1735** or power failures by changing the number of syncs()s when writing
1736** the rollback journal. There are three levels:
1737**
drh054889e2005-11-30 03:20:31 +00001738** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001739** for temporary and transient files.
1740**
1741** NORMAL The journal is synced once before writes begin on the
1742** database. This is normally adequate protection, but
1743** it is theoretically possible, though very unlikely,
1744** that an inopertune power failure could leave the journal
1745** in a state which would cause damage to the database
1746** when it is rolled back.
1747**
1748** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001749** database (with some additional information - the nRec field
1750** of the journal header - being written in between the two
1751** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001752** single disk sector is atomic, then this mode provides
1753** assurance that the journal will not be corrupted to the
1754** point of causing damage to the database during rollback.
1755**
1756** Numeric values associated with these states are OFF==1, NORMAL=2,
1757** and FULL=3.
1758*/
danielk197793758c82005-01-21 08:13:14 +00001759#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh281b21d2008-08-22 12:57:08 +00001760void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
drh4f21c4a2008-12-10 22:15:00 +00001761 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
1762 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
drh281b21d2008-08-22 12:57:08 +00001763 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001764 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001765}
danielk197793758c82005-01-21 08:13:14 +00001766#endif
drh973b6e32003-02-12 14:09:42 +00001767
1768/*
drhaf6df112005-06-07 02:12:30 +00001769** The following global variable is incremented whenever the library
1770** attempts to open a temporary file. This information is used for
1771** testing and analysis only.
1772*/
drh0f7eb612006-08-08 13:51:43 +00001773#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001774int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001775#endif
drhaf6df112005-06-07 02:12:30 +00001776
1777/*
drh3f56e6e2007-03-15 01:16:47 +00001778** Open a temporary file.
1779**
1780** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001781** other error code if we fail. The OS will automatically delete the temporary
1782** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001783*/
danielk1977b4b47412007-08-17 15:53:36 +00001784static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00001785 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00001786 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00001787 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00001788){
drhfa86c412002-02-02 15:01:15 +00001789 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001790
drh0f7eb612006-08-08 13:51:43 +00001791#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001792 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001793#endif
danielk1977b4b47412007-08-17 15:53:36 +00001794
drh33f4e022007-09-03 15:19:34 +00001795 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1796 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00001797 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00001798 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00001799 return rc;
1800}
1801
danielk1977a858aa22008-08-22 16:22:17 +00001802static int pagerStress(void *,PgHdr *);
danielk19778c0a7912008-08-20 14:49:23 +00001803
drhfa86c412002-02-02 15:01:15 +00001804/*
drhed7c8552001-04-11 14:29:21 +00001805** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001806** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001807** the first call to sqlite3PagerGet() and is only held open until the
1808** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001809**
drh6446c4d2001-12-15 14:22:18 +00001810** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00001811** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00001812** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001813**
1814** If zFilename is ":memory:" then all information is held in cache.
1815** It is never written to disk. This can be used to implement an
1816** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001817*/
danielk19773b8a05f2007-03-19 17:44:26 +00001818int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001819 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001820 Pager **ppPager, /* Return the Pager structure here */
1821 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00001822 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00001823 int flags, /* flags controlling this file */
1824 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00001825){
danielk1977b4b47412007-08-17 15:53:36 +00001826 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001827 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001828 int rc = SQLITE_OK;
1829 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001830 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001831 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001832 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001833 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1834 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977b3175382008-10-17 18:51:52 +00001835 int journalFileSize;
danielk19778c0a7912008-08-20 14:49:23 +00001836 int pcacheSize = sqlite3PcacheSize();
drhfacf0302008-06-17 15:12:00 +00001837 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
danielk197717b90b52008-06-06 11:11:25 +00001838 char *zPathname = 0;
1839 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001840
danielk1977b3175382008-10-17 18:51:52 +00001841 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
1842 journalFileSize = sqlite3JournalSize(pVfs);
1843 }else{
1844 journalFileSize = sqlite3MemJournalSize();
1845 }
1846
drh86f8c192007-08-22 00:39:19 +00001847 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001848 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001849
danielk197717b90b52008-06-06 11:11:25 +00001850 /* Compute and store the full pathname in an allocated buffer pointed
1851 ** to by zPathname, length nPathname. Or, if this is a temporary file,
1852 ** leave both nPathname and zPathname set to 0.
1853 */
drh1cc8c442007-08-24 16:08:29 +00001854 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00001855 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001856 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00001857 if( zPathname==0 ){
1858 return SQLITE_NOMEM;
1859 }
drh1cc8c442007-08-24 16:08:29 +00001860#ifndef SQLITE_OMIT_MEMORYDB
1861 if( strcmp(zFilename,":memory:")==0 ){
1862 memDb = 1;
1863 zPathname[0] = 0;
1864 }else
1865#endif
1866 {
danielk1977adfb9b02007-09-17 07:02:56 +00001867 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00001868 }
danielk197717b90b52008-06-06 11:11:25 +00001869 if( rc!=SQLITE_OK ){
1870 sqlite3_free(zPathname);
1871 return rc;
1872 }
drhea678832008-12-10 19:26:22 +00001873 nPathname = sqlite3Strlen30(zPathname);
drhae28c012007-08-24 16:29:23 +00001874 }
drh99b90c32008-03-17 13:50:58 +00001875
danielk1977b4b47412007-08-17 15:53:36 +00001876 /* Allocate memory for the pager structure */
1877 pPager = sqlite3MallocZero(
1878 sizeof(*pPager) + /* Pager structure */
danielk19778c0a7912008-08-20 14:49:23 +00001879 pcacheSize + /* PCache object */
danielk1977c7b60172007-08-22 11:22:03 +00001880 journalFileSize + /* The journal file structure */
danielk1977b3175382008-10-17 18:51:52 +00001881 pVfs->szOsFile + /* The main db file */
1882 journalFileSize * 2 + /* The two journal files */
danielk197717b90b52008-06-06 11:11:25 +00001883 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00001884 );
1885 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00001886 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00001887 return SQLITE_NOMEM;
1888 }
danielk19778c0a7912008-08-20 14:49:23 +00001889 pPager->pPCache = (PCache *)&pPager[1];
1890 pPtr = ((u8 *)&pPager[1]) + pcacheSize;
drh33f4e022007-09-03 15:19:34 +00001891 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00001892 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977fd7f0452008-12-17 17:30:26 +00001893 pPager->sjfd = (sqlite3_file*)&pPtr[pVfs->szOsFile];
danielk1977b3175382008-10-17 18:51:52 +00001894 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile+journalFileSize];
1895 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile+2*journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00001896 pPager->zDirectory = &pPager->zFilename[nPathname+1];
1897 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00001898 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00001899 if( zPathname ){
1900 memcpy(pPager->zFilename, zPathname, nPathname+1);
1901 sqlite3_free(zPathname);
1902 }
danielk1977b4b47412007-08-17 15:53:36 +00001903
drh153c62c2007-08-24 03:51:33 +00001904 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00001905 */
drhae28c012007-08-24 16:29:23 +00001906 if( zFilename && zFilename[0] && !memDb ){
danielk1977d92db532008-11-17 04:56:24 +00001907 if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){
drh1cc8c442007-08-24 16:08:29 +00001908 rc = SQLITE_CANTOPEN;
1909 }else{
drh1cc8c442007-08-24 16:08:29 +00001910 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00001911 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
1912 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00001913 readOnly = (fout&SQLITE_OPEN_READONLY);
1914
1915 /* If the file was successfully opened for read/write access,
1916 ** choose a default page size in case we have to create the
1917 ** database file. The default page size is the maximum of:
1918 **
1919 ** + SQLITE_DEFAULT_PAGE_SIZE,
1920 ** + The value returned by sqlite3OsSectorSize()
1921 ** + The largest page size that can be written atomically.
1922 */
1923 if( rc==SQLITE_OK && !readOnly ){
1924 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
drhfacf0302008-06-17 15:12:00 +00001925 if( szPageDflt<iSectorSize ){
1926 szPageDflt = iSectorSize;
drh1cc8c442007-08-24 16:08:29 +00001927 }
danielk19779663b8f2007-08-24 11:52:28 +00001928#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00001929 {
1930 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
1931 int ii;
1932 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
1933 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
1934 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
drhfacf0302008-06-17 15:12:00 +00001935 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
1936 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00001937 }
danielk1977b4b47412007-08-17 15:53:36 +00001938 }
drh1cc8c442007-08-24 16:08:29 +00001939#endif
drhfacf0302008-06-17 15:12:00 +00001940 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
1941 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
drh1cc8c442007-08-24 16:08:29 +00001942 }
danielk19778def5ea2004-06-16 10:39:52 +00001943 }
drhac69b052004-05-12 13:30:07 +00001944 }
danielk1977b3175382008-10-17 18:51:52 +00001945 }else{
danielk19777a2b1ee2007-08-21 14:27:01 +00001946 /* If a temporary file is requested, it is not opened immediately.
1947 ** In this case we accept the default page size and delay actually
1948 ** opening the file until the first call to OsWrite().
danielk1977b3175382008-10-17 18:51:52 +00001949 **
1950 ** This branch is also run for an in-memory database. An in-memory
1951 ** database is the same as a temp-file that is never written out to
1952 ** disk and uses an in-memory rollback journal.
danielk19777a2b1ee2007-08-21 14:27:01 +00001953 */
danielk19777a2b1ee2007-08-21 14:27:01 +00001954 tempFile = 1;
1955 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00001956 }
danielk1977aef0bf62005-12-30 16:28:01 +00001957
danielk1977b4b47412007-08-17 15:53:36 +00001958 if( pPager && rc==SQLITE_OK ){
drhfacf0302008-06-17 15:12:00 +00001959 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00001960 }
danielk1977aef0bf62005-12-30 16:28:01 +00001961
drh153c62c2007-08-24 03:51:33 +00001962 /* If an error occured in either of the blocks above.
1963 ** Free the Pager structure and close the file.
1964 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00001965 ** any Pager.errMask variables.
1966 */
danielk1977b4b47412007-08-17 15:53:36 +00001967 if( !pPager || !pPager->pTmpSpace ){
1968 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00001969 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00001970 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001971 }
danielk19778c0a7912008-08-20 14:49:23 +00001972 nExtra = FORCE_ALIGNMENT(nExtra);
danielk197771d5d2c2008-09-29 11:49:47 +00001973 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
drh41d30272008-08-20 21:47:45 +00001974 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
danielk1977aef0bf62005-12-30 16:28:01 +00001975
drh30d53702009-01-06 15:58:57 +00001976 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
drh153c62c2007-08-24 03:51:33 +00001977 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00001978
danielk1977b4b47412007-08-17 15:53:36 +00001979 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00001980 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
drhea678832008-12-10 19:26:22 +00001981 for(i=sqlite3Strlen30(pPager->zDirectory);
1982 i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00001983 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001984
drh99b90c32008-03-17 13:50:58 +00001985 /* Fill in Pager.zJournal[] */
danielk197717b90b52008-06-06 11:11:25 +00001986 if( zPathname ){
1987 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
1988 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
1989 }else{
1990 pPager->zJournal = 0;
1991 }
danielk1977b4b47412007-08-17 15:53:36 +00001992
drh3b59a5c2006-01-15 20:28:28 +00001993 /* pPager->journalOpen = 0; */
drh4f21c4a2008-12-10 22:15:00 +00001994 pPager->useJournal = (u8)useJournal;
1995 pPager->noReadlock = (noReadlock && readOnly) ?1:0;
drh3b59a5c2006-01-15 20:28:28 +00001996 /* pPager->stmtOpen = 0; */
1997 /* pPager->stmtInUse = 0; */
1998 /* pPager->nRef = 0; */
drh4f21c4a2008-12-10 22:15:00 +00001999 pPager->dbSizeValid = (u8)memDb;
drhfacf0302008-06-17 15:12:00 +00002000 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00002001 /* pPager->stmtSize = 0; */
2002 /* pPager->stmtJSize = 0; */
2003 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002004 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002005 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002006 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002007 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002008 /* pPager->errMask = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002009 pPager->tempFile = (u8)tempFile;
drh369339d2007-03-30 16:01:55 +00002010 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2011 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2012 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
drh4f21c4a2008-12-10 22:15:00 +00002013 pPager->exclusiveMode = (u8)tempFile;
2014 pPager->memDb = (u8)memDb;
2015 pPager->readOnly = (u8)readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002016 /* pPager->needSync = 0; */
drh4f21c4a2008-12-10 22:15:00 +00002017 pPager->noSync = (pPager->tempFile || !useJournal) ?1:0;
2018 pPager->fullSync = pPager->noSync ?0:1;
danielk1977f036aef2007-08-20 05:36:51 +00002019 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002020 /* pPager->pFirst = 0; */
2021 /* pPager->pFirstSynced = 0; */
2022 /* pPager->pLast = 0; */
danielk19778c0a7912008-08-20 14:49:23 +00002023 pPager->nExtra = nExtra;
danielk1977b53e4962008-06-04 06:45:59 +00002024 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk1977b3175382008-10-17 18:51:52 +00002025 assert(pPager->fd->pMethods||tempFile);
2026 setSectorSize(pPager);
2027 if( memDb ){
2028 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
danielk1977b4721172007-03-19 05:54:48 +00002029 }
danielk19771ceedd32008-11-19 10:22:33 +00002030 /* pPager->xBusyHandler = 0; */
2031 /* pPager->pBusyHandlerArg = 0; */
drh3b59a5c2006-01-15 20:28:28 +00002032 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002033 *ppPager = pPager;
2034 return SQLITE_OK;
2035}
2036
2037/*
drh90f5ecb2004-07-22 01:19:35 +00002038** Set the busy handler function.
2039*/
danielk19771ceedd32008-11-19 10:22:33 +00002040void sqlite3PagerSetBusyhandler(
2041 Pager *pPager,
2042 int (*xBusyHandler)(void *),
2043 void *pBusyHandlerArg
2044){
2045 pPager->xBusyHandler = xBusyHandler;
2046 pPager->pBusyHandlerArg = pBusyHandlerArg;
drh90f5ecb2004-07-22 01:19:35 +00002047}
2048
2049/*
drha6abd042004-06-09 17:37:22 +00002050** Set the reinitializer for this pager. If not NULL, the reinitializer
2051** is called when the content of a page in cache is restored to its original
2052** value as a result of a rollback. The callback gives higher-level code
2053** an opportunity to restore the EXTRA section to agree with the restored
2054** page data.
2055*/
danielk1977eaa06f62008-09-18 17:34:44 +00002056void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
drha6abd042004-06-09 17:37:22 +00002057 pPager->xReiniter = xReinit;
2058}
2059
2060/*
danielk1977a1644fd2007-08-29 12:31:25 +00002061** Set the page size to *pPageSize. If the suggest new page size is
2062** inappropriate, then an alternative page size is set to that
2063** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002064*/
danielk1977a1644fd2007-08-29 12:31:25 +00002065int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
danielk19771357d9f2008-09-16 05:12:24 +00002066 int rc = pPager->errCode;
2067 if( rc==SQLITE_OK ){
2068 u16 pageSize = *pPageSize;
2069 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
2070 if( pageSize && pageSize!=pPager->pageSize
2071 && (pPager->memDb==0 || pPager->dbSize==0)
2072 && sqlite3PcacheRefCount(pPager->pPCache)==0
2073 ){
2074 char *pNew = (char *)sqlite3PageMalloc(pageSize);
2075 if( !pNew ){
2076 rc = SQLITE_NOMEM;
2077 }else{
2078 pager_reset(pPager);
2079 pPager->pageSize = pageSize;
2080 if( !pPager->memDb ) setSectorSize(pPager);
2081 sqlite3PageFree(pPager->pTmpSpace);
2082 pPager->pTmpSpace = pNew;
2083 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
2084 }
danielk1977a1644fd2007-08-29 12:31:25 +00002085 }
drh4f21c4a2008-12-10 22:15:00 +00002086 *pPageSize = (u16)pPager->pageSize;
drh1c7880e2005-05-20 20:01:55 +00002087 }
danielk1977a1644fd2007-08-29 12:31:25 +00002088 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002089}
2090
2091/*
drh26b79942007-11-28 16:19:56 +00002092** Return a pointer to the "temporary page" buffer held internally
2093** by the pager. This is a buffer that is big enough to hold the
2094** entire content of a database page. This buffer is used internally
2095** during rollback and will be overwritten whenever a rollback
2096** occurs. But other modules are free to use it too, as long as
2097** no rollbacks are happening.
2098*/
2099void *sqlite3PagerTempSpace(Pager *pPager){
2100 return pPager->pTmpSpace;
2101}
2102
2103/*
drhf8e632b2007-05-08 14:51:36 +00002104** Attempt to set the maximum database page count if mxPage is positive.
2105** Make no changes if mxPage is zero or negative. And never reduce the
2106** maximum page count below the current size of the database.
2107**
2108** Regardless of mxPage, return the current maximum page count.
2109*/
2110int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2111 if( mxPage>0 ){
2112 pPager->mxPgno = mxPage;
2113 }
danielk1977ad0132d2008-06-07 08:58:22 +00002114 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002115 return pPager->mxPgno;
2116}
2117
2118/*
drhc9ac5ca2005-11-04 22:03:30 +00002119** The following set of routines are used to disable the simulated
2120** I/O error mechanism. These routines are used to avoid simulated
2121** errors in places where we do not care about errors.
2122**
2123** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2124** and generate no code.
2125*/
2126#ifdef SQLITE_TEST
2127extern int sqlite3_io_error_pending;
2128extern int sqlite3_io_error_hit;
2129static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002130void disable_simulated_io_errors(void){
2131 saved_cnt = sqlite3_io_error_pending;
2132 sqlite3_io_error_pending = -1;
2133}
2134void enable_simulated_io_errors(void){
2135 sqlite3_io_error_pending = saved_cnt;
2136}
2137#else
drh152410f2005-11-05 15:11:22 +00002138# define disable_simulated_io_errors()
2139# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002140#endif
2141
2142/*
drh90f5ecb2004-07-22 01:19:35 +00002143** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002144** that pDest points to.
2145**
2146** No error checking is done. The rational for this is that this function
2147** may be called even if the file does not exist or contain a header. In
2148** these cases sqlite3OsRead() will return an error, to which the correct
2149** response is to zero the memory at pDest and continue. A real IO error
2150** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002151*/
danielk19773b8a05f2007-03-19 17:44:26 +00002152int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002153 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002154 memset(pDest, 0, N);
danielk1977b3175382008-10-17 18:51:52 +00002155 assert(pPager->fd->pMethods||pPager->tempFile);
danielk19777a2b1ee2007-08-21 14:27:01 +00002156 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002157 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002158 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002159 if( rc==SQLITE_IOERR_SHORT_READ ){
2160 rc = SQLITE_OK;
2161 }
drh90f5ecb2004-07-22 01:19:35 +00002162 }
drh551b7732006-11-06 21:20:25 +00002163 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002164}
2165
2166/*
drh5e00f6c2001-09-13 13:46:56 +00002167** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002168** pPager.
2169**
2170** If the PENDING_BYTE lies on the page directly after the end of the
2171** file, then consider this page part of the file too. For example, if
2172** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2173** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002174*/
danielk1977ad0132d2008-06-07 08:58:22 +00002175int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk19777a2b1ee2007-08-21 14:27:01 +00002176 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002177 int rc;
drhd9b02572001-04-15 00:37:09 +00002178 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002179 if( pPager->errCode ){
danielk19778c0a7912008-08-20 14:49:23 +00002180 rc = pPager->errCode;
danielk19778c0a7912008-08-20 14:49:23 +00002181 return rc;
drha7aea3d2007-03-15 12:51:16 +00002182 }
danielk1977d92db532008-11-17 04:56:24 +00002183 if( pPager->dbSizeValid ){
danielk197715f411d2005-09-16 10:18:45 +00002184 n = pPager->dbSize;
2185 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002186 assert(pPager->fd->pMethods||pPager->tempFile);
2187 if( (pPager->fd->pMethods)
2188 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002189 pager_error(pPager, rc);
danielk1977ad0132d2008-06-07 08:58:22 +00002190 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002191 }
2192 if( n>0 && n<pPager->pageSize ){
2193 n = 1;
2194 }else{
2195 n /= pPager->pageSize;
2196 }
2197 if( pPager->state!=PAGER_UNLOCK ){
danielk19773460d192008-12-27 15:23:13 +00002198 pPager->dbSize = (Pgno)n;
2199 pPager->dbFileSize = (Pgno)n;
danielk1977d92db532008-11-17 04:56:24 +00002200 pPager->dbSizeValid = 1;
danielk197715f411d2005-09-16 10:18:45 +00002201 }
drhed7c8552001-04-11 14:29:21 +00002202 }
danielk197715f411d2005-09-16 10:18:45 +00002203 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002204 n++;
2205 }
drhf8e632b2007-05-08 14:51:36 +00002206 if( n>pPager->mxPgno ){
drh4f21c4a2008-12-10 22:15:00 +00002207 pPager->mxPgno = (Pgno)n;
drhf8e632b2007-05-08 14:51:36 +00002208 }
danielk1977ad0132d2008-06-07 08:58:22 +00002209 if( pnPage ){
drh4f21c4a2008-12-10 22:15:00 +00002210 *pnPage = (int)n;
danielk1977ad0132d2008-06-07 08:58:22 +00002211 }
2212 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002213}
2214
2215/*
drhf7c57532003-04-25 13:22:51 +00002216** Forward declaration
2217*/
danielk197776572402004-06-25 02:38:54 +00002218static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002219
2220/*
danielk197717221812005-02-15 03:38:05 +00002221** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002222** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002223** false or until the lock succeeds.
2224**
2225** Return SQLITE_OK on success and an error code if we cannot obtain
2226** the lock.
2227*/
2228static int pager_wait_on_lock(Pager *pPager, int locktype){
2229 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002230
2231 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002232 assert( PAGER_SHARED==SHARED_LOCK );
2233 assert( PAGER_RESERVED==RESERVED_LOCK );
2234 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002235
2236 /* If the file is currently unlocked then the size must be unknown */
danielk1977d92db532008-11-17 04:56:24 +00002237 assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
drh1aa2d8b2007-01-03 15:34:29 +00002238
danielk197717221812005-02-15 03:38:05 +00002239 if( pPager->state>=locktype ){
2240 rc = SQLITE_OK;
2241 }else{
danielk197717221812005-02-15 03:38:05 +00002242 do {
drh054889e2005-11-30 03:20:31 +00002243 rc = sqlite3OsLock(pPager->fd, locktype);
danielk19771ceedd32008-11-19 10:22:33 +00002244 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
danielk197717221812005-02-15 03:38:05 +00002245 if( rc==SQLITE_OK ){
drh4f21c4a2008-12-10 22:15:00 +00002246 pPager->state = (u8)locktype;
drhb0603412007-02-28 04:47:26 +00002247 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002248 }
2249 }
2250 return rc;
2251}
2252
2253/*
danielk19773460d192008-12-27 15:23:13 +00002254** Truncate the file to the number of pages specified.
2255**
2256** Unless an IO error occurs, this function is guaranteed to modify the
2257** database file itself. If an exclusive lock is not held when this function
2258** is called, one is obtained before truncating the file.
drhf7c57532003-04-25 13:22:51 +00002259*/
danielk19773b8a05f2007-03-19 17:44:26 +00002260int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
danielk19778c0a7912008-08-20 14:49:23 +00002261 int rc = SQLITE_OK;
danielk1977b3175382008-10-17 18:51:52 +00002262 assert( pPager->state>=PAGER_SHARED );
danielk19778c0a7912008-08-20 14:49:23 +00002263
danielk1977ad0132d2008-06-07 08:58:22 +00002264 sqlite3PagerPagecount(pPager, 0);
danielk1977efaaf572006-01-16 11:29:19 +00002265 if( pPager->errCode ){
2266 rc = pPager->errCode;
danielk19773460d192008-12-27 15:23:13 +00002267 }else if( nPage<pPager->dbFileSize ){
danielk1977b3175382008-10-17 18:51:52 +00002268 rc = syncJournal(pPager);
2269 if( rc==SQLITE_OK ){
2270 /* Get an exclusive lock on the database before truncating. */
2271 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2272 }
2273 if( rc==SQLITE_OK ){
2274 rc = pager_truncate(pPager, nPage);
danielk19778c0a7912008-08-20 14:49:23 +00002275 }
danielk1977369f27e2004-06-15 11:40:04 +00002276 }
danielk197717221812005-02-15 03:38:05 +00002277
drhf7c57532003-04-25 13:22:51 +00002278 return rc;
2279}
2280
danielk19773460d192008-12-27 15:23:13 +00002281#ifndef SQLITE_OMIT_AUTOVACUUM
2282/*
2283** Truncate the in-memory database file image to nPage pages. Unlike
2284** sqlite3PagerTruncate(), this function does not actually modify the
2285** database file on disk. It just sets the internal state of the pager
2286** object so that the truncation will be done when the current
2287** transaction is committed.
2288*/
2289void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
2290 assert( pPager->dbSizeValid );
2291 assert( pPager->dbSize>=nPage );
2292 pPager->dbSize = nPage;
2293}
2294
2295/*
2296** Return the current size of the database file image in pages. This
2297** function differs from sqlite3PagerPagecount() in two ways:
2298**
2299** a) It may only be called when at least one reference to a database
2300** page is held. This guarantees that the database size is already
2301** known and a call to sqlite3OsFileSize() is not required.
2302**
2303** b) The return value is not adjusted for the locking page.
2304*/
2305Pgno sqlite3PagerImageSize(Pager *pPager){
2306 assert( pPager->dbSizeValid );
2307 return pPager->dbSize;
2308}
2309#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
2310
drhf7c57532003-04-25 13:22:51 +00002311/*
drhed7c8552001-04-11 14:29:21 +00002312** Shutdown the page cache. Free all memory and close all files.
2313**
2314** If a transaction was in progress when this routine is called, that
2315** transaction is rolled back. All outstanding pages are invalidated
2316** and their memory is freed. Any attempt to use a page associated
2317** with this page cache after this function returns will likely
2318** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002319**
2320** This function always succeeds. If a transaction is active an attempt
2321** is made to roll it back. If an error occurs during the rollback
2322** a hot journal may be left in the filesystem but no error is returned
2323** to the caller.
drhed7c8552001-04-11 14:29:21 +00002324*/
danielk19773b8a05f2007-03-19 17:44:26 +00002325int sqlite3PagerClose(Pager *pPager){
danielk197713f72992005-12-18 08:51:22 +00002326
drhbafda092007-01-03 23:36:22 +00002327 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002328 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002329 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002330 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002331 pager_reset(pPager);
danielk1977b3175382008-10-17 18:51:52 +00002332 if( !MEMDB ){
danielk1977f2c31ad2009-01-06 13:40:08 +00002333 /* Set Pager.journalHdr to -1 for the benefit of the pager_playback()
2334 ** call which may be made from within pagerUnlockAndRollback(). If it
2335 ** is not -1, then the unsynced portion of an open journal file may
2336 ** be played back into the database. If a power failure occurs while
2337 ** this is happening, the database may become corrupt.
2338 */
2339 pPager->journalHdr = -1;
danielk1977b3175382008-10-17 18:51:52 +00002340 pagerUnlockAndRollback(pPager);
2341 }
drhbafda092007-01-03 23:36:22 +00002342 enable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002343 sqlite3EndBenignMalloc();
drh30d53702009-01-06 15:58:57 +00002344 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
drhb0603412007-02-28 04:47:26 +00002345 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002346 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002347 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002348 }
drhf5e7bb52008-02-18 14:47:33 +00002349 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977a1fa00d2008-08-27 15:16:33 +00002350 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
danielk1977fd7f0452008-12-17 17:30:26 +00002351 releaseAllSavepoint(pPager);
danielk1977b4b47412007-08-17 15:53:36 +00002352 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002353 /* Temp files are automatically deleted by the OS
2354 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002355 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002356 ** }
2357 */
danielk1977aca790a2005-01-13 11:07:52 +00002358
drhfacf0302008-06-17 15:12:00 +00002359 sqlite3PageFree(pPager->pTmpSpace);
danielk19778c0a7912008-08-20 14:49:23 +00002360 sqlite3PcacheClose(pPager->pPCache);
drh17435752007-08-16 04:30:38 +00002361 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002362 return SQLITE_OK;
2363}
2364
drh87cc3b32007-05-08 21:45:27 +00002365#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002366/*
drh5e00f6c2001-09-13 13:46:56 +00002367** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002368*/
danielk19773b8a05f2007-03-19 17:44:26 +00002369Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002370 return p->pgno;
2371}
drh87cc3b32007-05-08 21:45:27 +00002372#endif
drhed7c8552001-04-11 14:29:21 +00002373
2374/*
drhdf0b3b02001-06-23 11:36:20 +00002375** Increment the reference count for a page. The input pointer is
2376** a reference to the page data.
2377*/
danielk19773b8a05f2007-03-19 17:44:26 +00002378int sqlite3PagerRef(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002379 sqlite3PcacheRef(pPg);
drh8c42ca92001-06-22 19:15:00 +00002380 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002381}
2382
2383/*
drh34e79ce2004-02-08 06:05:46 +00002384** Sync the journal. In other words, make sure all the pages that have
2385** been written to the journal have actually reached the surface of the
2386** disk. It is not safe to modify the original database file until after
2387** the journal has been synced. If the original database is modified before
2388** the journal is synced and a power failure occurs, the unsynced journal
2389** data would be lost and we would be unable to completely rollback the
2390** database changes. Database corruption would occur.
2391**
2392** This routine also updates the nRec field in the header of the journal.
2393** (See comments on the pager_playback() routine for additional information.)
2394** If the sync mode is FULL, two syncs will occur. First the whole journal
2395** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002396**
drh34e79ce2004-02-08 06:05:46 +00002397** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002398** after a power failure, so no sync occurs.
2399**
2400** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2401** the database is stored, then OsSync() is never called on the journal
2402** file. In this case all that is required is to update the nRec field in
2403** the journal header.
drhfa86c412002-02-02 15:01:15 +00002404**
drh34e79ce2004-02-08 06:05:46 +00002405** This routine clears the needSync field of every page current held in
2406** memory.
drh50e5dad2001-09-15 00:57:28 +00002407*/
danielk197776572402004-06-25 02:38:54 +00002408static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002409 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002410
2411 /* Sync the journal before modifying the main database
2412 ** (assuming there is a journal and it needs to be synced.)
2413 */
danielk197776572402004-06-25 02:38:54 +00002414 if( pPager->needSync ){
danielk1977b3175382008-10-17 18:51:52 +00002415 assert( !pPager->tempFile );
2416 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
danielk19774cd2cd52007-08-23 14:48:23 +00002417 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002418 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002419
danielk19774cd2cd52007-08-23 14:48:23 +00002420 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002421 /* Write the nRec value into the journal file header. If in
2422 ** full-synchronous mode, sync the journal first. This ensures that
2423 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002424 ** it as a candidate for rollback.
2425 **
2426 ** This is not required if the persistent media supports the
2427 ** SAFE_APPEND property. Because in this case it is not possible
2428 ** for garbage data to be appended to the file, the nRec field
2429 ** is populated with 0xFFFFFFFF when the journal header is written
2430 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002431 */
danielk19774cd2cd52007-08-23 14:48:23 +00002432 i64 jrnlOff;
2433 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh30d53702009-01-06 15:58:57 +00002434 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
drhb0603412007-02-28 04:47:26 +00002435 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002436 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002437 if( rc!=0 ) return rc;
2438 }
danielk197713adf8a2004-06-03 16:08:41 +00002439
danielk197762079062007-08-15 17:08:46 +00002440 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2441 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2442 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002443 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002444 }
danielk19774cd2cd52007-08-23 14:48:23 +00002445 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh30d53702009-01-06 15:58:57 +00002446 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
danielk19774cd2cd52007-08-23 14:48:23 +00002447 IOTRACE(("JSYNC %p\n", pPager))
2448 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2449 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2450 );
2451 if( rc!=0 ) return rc;
2452 }
drhdb48ee02003-01-16 13:42:43 +00002453 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002454 }
drh50e5dad2001-09-15 00:57:28 +00002455 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002456
2457 /* Erase the needSync flag from every page.
2458 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00002459 sqlite3PcacheClearSyncFlags(pPager->pPCache);
drh50e5dad2001-09-15 00:57:28 +00002460 }
drh03eb96a2002-11-10 23:32:56 +00002461
drh81a20f22001-10-12 17:30:04 +00002462 return rc;
drh50e5dad2001-09-15 00:57:28 +00002463}
2464
2465/*
drh2554f8b2003-01-22 01:26:44 +00002466** Given a list of pages (connected by the PgHdr.pDirty pointer) write
danielk1977a858aa22008-08-22 16:22:17 +00002467** every one of those pages out to the database file. No calls are made
2468** to the page-cache to mark the pages as clean. It is the responsibility
2469** of the caller to use PcacheCleanAll() or PcacheMakeClean() to mark
2470** the pages as clean.
drh2554f8b2003-01-22 01:26:44 +00002471*/
2472static int pager_write_pagelist(PgHdr *pList){
2473 Pager *pPager;
2474 int rc;
2475
2476 if( pList==0 ) return SQLITE_OK;
2477 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002478
2479 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2480 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002481 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002482 **
drha6abd042004-06-09 17:37:22 +00002483 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2484 ** through an intermediate state PENDING. A PENDING lock prevents new
2485 ** readers from attaching to the database but is unsufficient for us to
2486 ** write. The idea of a PENDING lock is to prevent new readers from
2487 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002488 **
drha6abd042004-06-09 17:37:22 +00002489 ** While the pager is in the RESERVED state, the original database file
2490 ** is unchanged and we can rollback without having to playback the
2491 ** journal into the original database file. Once we transition to
2492 ** EXCLUSIVE, it means the database file has been changed and any rollback
2493 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002494 */
drh684917c2004-10-05 02:41:42 +00002495 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002496 if( rc!=SQLITE_OK ){
2497 return rc;
2498 }
2499
drh2554f8b2003-01-22 01:26:44 +00002500 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002501
2502 /* If the file has not yet been opened, open it now. */
2503 if( !pPager->fd->pMethods ){
2504 assert(pPager->tempFile);
danielk197717b90b52008-06-06 11:11:25 +00002505 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002506 if( rc ) return rc;
2507 }
2508
danielk1977687566d2004-11-02 12:56:41 +00002509 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002510 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002511 ** make the file smaller (presumably by auto-vacuum code). Do not write
2512 ** any such pages to the file.
2513 */
danielk197733e32162008-08-23 18:53:08 +00002514 if( pList->pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
danielk197762079062007-08-15 17:08:46 +00002515 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002516 char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6);
danielk197712dd5492008-12-18 15:45:07 +00002517
drh30d53702009-01-06 15:58:57 +00002518 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
2519 PAGERID(pPager), pList->pgno, pager_pagehash(pList)));
drh538f5702007-04-13 02:14:30 +00002520 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002521 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002522 PAGER_INCR(sqlite3_pager_writedb_count);
2523 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002524 if( pList->pgno==1 ){
2525 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2526 }
danielk19773460d192008-12-27 15:23:13 +00002527 if( pList->pgno>pPager->dbFileSize ){
2528 pPager->dbFileSize = pList->pgno;
2529 }
danielk1977687566d2004-11-02 12:56:41 +00002530 }
2531#ifndef NDEBUG
2532 else{
drh30d53702009-01-06 15:58:57 +00002533 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno));
danielk1977687566d2004-11-02 12:56:41 +00002534 }
2535#endif
drh2554f8b2003-01-22 01:26:44 +00002536 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002537#ifdef SQLITE_CHECK_PAGES
2538 pList->pageHash = pager_pagehash(pList);
2539#endif
drh2554f8b2003-01-22 01:26:44 +00002540 pList = pList->pDirty;
2541 }
danielk19778c0a7912008-08-20 14:49:23 +00002542
drh2554f8b2003-01-22 01:26:44 +00002543 return SQLITE_OK;
2544}
2545
2546/*
danielk1977f2c31ad2009-01-06 13:40:08 +00002547** Add the page to the sub-journal. It is the callers responsibility to
2548** use subjRequiresPage() to check that it is really required before
2549** calling this function.
2550*/
2551static int subjournalPage(PgHdr *pPg){
2552 int rc;
2553 void *pData = pPg->pData;
2554 Pager *pPager = pPg->pPager;
2555 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
2556 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
2557
drh30d53702009-01-06 15:58:57 +00002558 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
danielk1977f2c31ad2009-01-06 13:40:08 +00002559
2560 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
2561 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
2562 if( rc==SQLITE_OK ){
2563 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
2564 }
2565 if( rc==SQLITE_OK ){
2566 pPager->stmtNRec++;
2567 assert( pPager->nSavepoint>0 );
2568 rc = addToSavepointBitvecs(pPager, pPg->pgno);
2569 }
2570 return rc;
2571}
2572
2573
2574/*
danielk19778c0a7912008-08-20 14:49:23 +00002575** This function is called by the pcache layer when it has reached some
2576** soft memory limit. The argument is a pointer to a purgeable Pager
2577** object. This function attempts to make a single dirty page that has no
2578** outstanding references (if one exists) clean so that it can be recycled
2579** by the pcache layer.
drh2554f8b2003-01-22 01:26:44 +00002580*/
danielk1977a858aa22008-08-22 16:22:17 +00002581static int pagerStress(void *p, PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002582 Pager *pPager = (Pager *)p;
danielk19778c0a7912008-08-20 14:49:23 +00002583 int rc = SQLITE_OK;
drh8f2e9a12008-02-26 14:46:04 +00002584
danielk19778c200142008-08-25 07:12:28 +00002585 if( pPager->doNotSync ){
2586 return SQLITE_OK;
2587 }
2588
danielk1977a858aa22008-08-22 16:22:17 +00002589 assert( pPg->flags&PGHDR_DIRTY );
2590 if( pPager->errCode==SQLITE_OK ){
2591 if( pPg->flags&PGHDR_NEED_SYNC ){
2592 rc = syncJournal(pPager);
2593 if( rc==SQLITE_OK && pPager->fullSync &&
danielk1977b3175382008-10-17 18:51:52 +00002594 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
danielk1977a858aa22008-08-22 16:22:17 +00002595 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
2596 ){
2597 pPager->nRec = 0;
2598 rc = writeJournalHdr(pPager);
danielk19778c0a7912008-08-20 14:49:23 +00002599 }
danielk19778c0a7912008-08-20 14:49:23 +00002600 }
danielk1977a858aa22008-08-22 16:22:17 +00002601 if( rc==SQLITE_OK ){
2602 pPg->pDirty = 0;
danielk1977f2c31ad2009-01-06 13:40:08 +00002603 if( pPg->pgno>pPager->dbSize && subjRequiresPage(pPg) ){
2604 rc = subjournalPage(pPg);
2605 }
2606 if( rc==SQLITE_OK ){
2607 rc = pager_write_pagelist(pPg);
2608 }
danielk1977a858aa22008-08-22 16:22:17 +00002609 }
2610 if( rc!=SQLITE_OK ){
2611 pager_error(pPager, rc);
2612 }
2613 }
2614
2615 if( rc==SQLITE_OK ){
drh30d53702009-01-06 15:58:57 +00002616 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
danielk1977a858aa22008-08-22 16:22:17 +00002617 sqlite3PcacheMakeClean(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00002618 }
2619 return rc;
drh2554f8b2003-01-22 01:26:44 +00002620}
2621
danielk19778c0a7912008-08-20 14:49:23 +00002622
drh2554f8b2003-01-22 01:26:44 +00002623/*
drh19db9352008-03-27 22:42:51 +00002624** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00002625** A hot journal is one that needs to be played back.
2626**
2627** If the current size of the database file is 0 but a journal file
2628** exists, that is probably an old journal left over from a prior
2629** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00002630**
2631** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00002632**
2633** This routine does not open the journal file to examine its
2634** content. Hence, the journal might contain the name of a master
2635** journal file that has been deleted, and hence not be hot. Or
2636** the header of the journal might be zeroed out. This routine
2637** does not discover these cases of a non-hot journal - if the
2638** journal file exists and is not empty this routine assumes it
2639** is hot. The pager_playback() routine will discover that the
2640** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00002641*/
danielk1977d300b8a2008-08-01 10:50:23 +00002642static int hasHotJournal(Pager *pPager, int *pExists){
danielk1977b4b47412007-08-17 15:53:36 +00002643 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977d300b8a2008-08-01 10:50:23 +00002644 int rc = SQLITE_OK;
drhea678832008-12-10 19:26:22 +00002645 int exists = 0;
2646 int locked = 0;
drh0a846f92008-08-25 17:23:29 +00002647 assert( pPager!=0 );
2648 assert( pPager->useJournal );
2649 assert( pPager->fd->pMethods );
danielk1977d300b8a2008-08-01 10:50:23 +00002650 *pExists = 0;
drh0a846f92008-08-25 17:23:29 +00002651 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
2652 if( rc==SQLITE_OK && exists ){
2653 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
2654 }
2655 if( rc==SQLITE_OK && exists && !locked ){
2656 int nPage;
2657 rc = sqlite3PagerPagecount(pPager, &nPage);
2658 if( rc==SQLITE_OK ){
2659 if( nPage==0 ){
2660 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2661 }else{
2662 *pExists = 1;
danielk1977861f7452008-06-05 11:39:11 +00002663 }
2664 }
drhbb5f18d2007-04-06 18:23:17 +00002665 }
danielk1977d300b8a2008-08-01 10:50:23 +00002666 return rc;
drh165ffe92005-03-15 17:09:30 +00002667}
2668
2669/*
danielk1977e180dd92007-04-05 17:15:52 +00002670** Read the content of page pPg out of the database file.
2671*/
2672static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2673 int rc;
danielk197762079062007-08-15 17:08:46 +00002674 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002675 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002676 assert(pPager->fd->pMethods||pPager->tempFile);
2677 if( !pPager->fd->pMethods ){
2678 return SQLITE_IOERR_SHORT_READ;
2679 }
danielk197762079062007-08-15 17:08:46 +00002680 offset = (pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002681 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002682 PAGER_INCR(sqlite3_pager_readdb_count);
2683 PAGER_INCR(pPager->nRead);
2684 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002685 if( pgno==1 ){
danielk19778c0a7912008-08-20 14:49:23 +00002686 memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24],
drh86a88112007-04-16 15:02:19 +00002687 sizeof(pPager->dbFileVers));
2688 }
danielk197730849522008-08-21 04:35:18 +00002689 CODEC1(pPager, pPg->pData, pPg->pgno, 3);
drh30d53702009-01-06 15:58:57 +00002690 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
2691 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
danielk1977e180dd92007-04-05 17:15:52 +00002692 return rc;
2693}
2694
2695
2696/*
danielk1977e277be02007-03-23 18:12:06 +00002697** This function is called to obtain the shared lock required before
2698** data may be read from the pager cache. If the shared lock has already
2699** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002700**
2701** Immediately after obtaining the shared lock (if required), this function
2702** checks for a hot-journal file. If one is found, an emergency rollback
2703** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002704*/
2705static int pagerSharedLock(Pager *pPager){
2706 int rc = SQLITE_OK;
danielk1977d300b8a2008-08-01 10:50:23 +00002707 int isErrorReset = 0;
danielk1977e277be02007-03-23 18:12:06 +00002708
danielk1977ae72d982007-10-03 08:46:44 +00002709 /* If this database is opened for exclusive access, has no outstanding
2710 ** page references and is in an error-state, now is the chance to clear
2711 ** the error. Discard the contents of the pager-cache and treat any
2712 ** open journal file as a hot-journal.
2713 */
danielk19778c0a7912008-08-20 14:49:23 +00002714 if( !MEMDB && pPager->exclusiveMode
2715 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
2716 ){
danielk1977ae72d982007-10-03 08:46:44 +00002717 if( pPager->journalOpen ){
danielk1977d300b8a2008-08-01 10:50:23 +00002718 isErrorReset = 1;
danielk1977ae72d982007-10-03 08:46:44 +00002719 }
danielk1977ae72d982007-10-03 08:46:44 +00002720 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00002721 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00002722 }
2723
2724 /* If the pager is still in an error state, do not proceed. The error
2725 ** state will be cleared at some point in the future when all page
2726 ** references are dropped and the cache can be discarded.
2727 */
2728 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
2729 return pPager->errCode;
2730 }
2731
danielk1977d300b8a2008-08-01 10:50:23 +00002732 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
danielk1977b4b47412007-08-17 15:53:36 +00002733 sqlite3_vfs *pVfs = pPager->pVfs;
drh4f21c4a2008-12-10 22:15:00 +00002734 int isHotJournal = 0;
shane049fc212008-10-22 16:26:47 +00002735 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00002736 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
2737 if( !pPager->noReadlock ){
2738 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2739 if( rc!=SQLITE_OK ){
2740 assert( pPager->state==PAGER_UNLOCK );
2741 return pager_error(pPager, rc);
danielk1977e277be02007-03-23 18:12:06 +00002742 }
danielk1977b3175382008-10-17 18:51:52 +00002743 assert( pPager->state>=SHARED_LOCK );
2744 }
2745
2746 /* If a journal file exists, and there is no RESERVED lock on the
2747 ** database file, then it either needs to be played back or deleted.
2748 */
2749 if( !isErrorReset ){
2750 rc = hasHotJournal(pPager, &isHotJournal);
2751 if( rc!=SQLITE_OK ){
2752 goto failed;
2753 }
2754 }
2755 if( isErrorReset || isHotJournal ){
2756 /* Get an EXCLUSIVE lock on the database file. At this point it is
2757 ** important that a RESERVED lock is not obtained on the way to the
2758 ** EXCLUSIVE lock. If it were, another process might open the
2759 ** database file, detect the RESERVED lock, and conclude that the
2760 ** database is safe to read while this process is still rolling it
2761 ** back.
2762 **
2763 ** Because the intermediate RESERVED lock is not requested, the
2764 ** second process will get to this point in the code and fail to
2765 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00002766 */
danielk1977b3175382008-10-17 18:51:52 +00002767 if( pPager->state<EXCLUSIVE_LOCK ){
2768 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
danielk1977e277be02007-03-23 18:12:06 +00002769 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002770 rc = pager_error(pPager, rc);
2771 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002772 }
danielk1977b3175382008-10-17 18:51:52 +00002773 pPager->state = PAGER_EXCLUSIVE;
2774 }
2775
2776 /* Open the journal for read/write access. This is because in
2777 ** exclusive-access mode the file descriptor will be kept open and
2778 ** possibly used for a transaction later on. On some systems, the
2779 ** OsTruncate() call used in exclusive-access mode also requires
2780 ** a read/write file handle.
2781 */
2782 if( !isErrorReset && pPager->journalOpen==0 ){
2783 int res;
2784 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
2785 if( rc==SQLITE_OK ){
2786 if( res ){
2787 int fout = 0;
2788 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
2789 assert( !pPager->tempFile );
2790 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
2791 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk1977281d8bd2008-10-29 07:01:56 +00002792 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
2793 rc = SQLITE_CANTOPEN;
danielk1977b3175382008-10-17 18:51:52 +00002794 sqlite3OsClose(pPager->jfd);
2795 }
2796 }else{
2797 /* If the journal does not exist, that means some other process
2798 ** has already rolled it back */
2799 rc = SQLITE_BUSY;
2800 }
2801 }
2802 }
2803 if( rc!=SQLITE_OK ){
danielk1977b3175382008-10-17 18:51:52 +00002804 goto failed;
2805 }
2806 pPager->journalOpen = 1;
2807 pPager->journalStarted = 0;
2808 pPager->journalOff = 0;
2809 pPager->setMaster = 0;
2810 pPager->journalHdr = 0;
2811
2812 /* Playback and delete the journal. Drop the database write
2813 ** lock and reacquire the read lock.
2814 */
2815 rc = pager_playback(pPager, 1);
2816 if( rc!=SQLITE_OK ){
2817 rc = pager_error(pPager, rc);
2818 goto failed;
2819 }
2820 assert(pPager->state==PAGER_SHARED ||
2821 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
2822 );
2823 }
2824
2825 if( sqlite3PcachePagecount(pPager->pPCache)>0 ){
2826 /* The shared-lock has just been acquired on the database file
2827 ** and there are already pages in the cache (from a previous
2828 ** read or write transaction). Check to see if the database
2829 ** has been modified. If the database has changed, flush the
2830 ** cache.
2831 **
2832 ** Database changes is detected by looking at 15 bytes beginning
2833 ** at offset 24 into the file. The first 4 of these 16 bytes are
2834 ** a 32-bit counter that is incremented with each change. The
2835 ** other bytes change randomly with each file change when
2836 ** a codec is in use.
2837 **
2838 ** There is a vanishingly small chance that a change will not be
2839 ** detected. The chance of an undetected change is so small that
2840 ** it can be neglected.
2841 */
2842 char dbFileVers[sizeof(pPager->dbFileVers)];
2843 sqlite3PagerPagecount(pPager, 0);
2844
2845 if( pPager->errCode ){
2846 rc = pPager->errCode;
2847 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002848 }
2849
danielk1977d92db532008-11-17 04:56:24 +00002850 assert( pPager->dbSizeValid );
danielk1977b3175382008-10-17 18:51:52 +00002851 if( pPager->dbSize>0 ){
2852 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
2853 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
2854 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002855 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002856 }
danielk1977b3175382008-10-17 18:51:52 +00002857 }else{
2858 memset(dbFileVers, 0, sizeof(dbFileVers));
2859 }
danielk1977e277be02007-03-23 18:12:06 +00002860
danielk1977b3175382008-10-17 18:51:52 +00002861 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
2862 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002863 }
2864 }
danielk1977c5859712007-03-26 12:26:27 +00002865 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
2866 if( pPager->state==PAGER_UNLOCK ){
2867 pPager->state = PAGER_SHARED;
2868 }
danielk1977e277be02007-03-23 18:12:06 +00002869 }
2870
danielk197752b472a2008-05-05 16:23:55 +00002871 failed:
2872 if( rc!=SQLITE_OK ){
2873 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
2874 pager_unlock(pPager);
2875 }
danielk1977e277be02007-03-23 18:12:06 +00002876 return rc;
2877}
2878
2879/*
drhd33d5a82007-04-26 12:11:28 +00002880** Make sure we have the content for a page. If the page was
2881** previously acquired with noContent==1, then the content was
2882** just initialized to zeros instead of being read from disk.
2883** But now we need the real data off of disk. So make sure we
2884** have it. Read it in if we do not have it already.
2885*/
2886static int pager_get_content(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002887 if( pPg->flags&PGHDR_NEED_READ ){
drhd33d5a82007-04-26 12:11:28 +00002888 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
2889 if( rc==SQLITE_OK ){
danielk19778c0a7912008-08-20 14:49:23 +00002890 pPg->flags &= ~PGHDR_NEED_READ;
drhd33d5a82007-04-26 12:11:28 +00002891 }else{
2892 return rc;
2893 }
2894 }
2895 return SQLITE_OK;
2896}
2897
2898/*
danielk19778c0a7912008-08-20 14:49:23 +00002899** If the reference count has reached zero, and the pager is not in the
2900** middle of a write transaction or opened in exclusive mode, unlock it.
2901*/
2902static void pagerUnlockIfUnused(Pager *pPager){
2903 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
2904 && (!pPager->exclusiveMode || pPager->journalOff>0)
2905 ){
2906 pagerUnlockAndRollback(pPager);
2907 }
2908}
2909
2910/*
2911** Drop a page from the cache using sqlite3PcacheDrop().
2912**
2913** If this means there are now no pages with references to them, a rollback
2914** occurs and the lock on the database is removed.
2915*/
2916static void pagerDropPage(DbPage *pPg){
2917 Pager *pPager = pPg->pPager;
2918 sqlite3PcacheDrop(pPg);
2919 pagerUnlockIfUnused(pPager);
2920}
2921
2922/*
drhd9b02572001-04-15 00:37:09 +00002923** Acquire a page.
2924**
drh58a11682001-11-10 13:51:08 +00002925** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00002926** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00002927**
drhd33d5a82007-04-26 12:11:28 +00002928** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00002929** file is smaller than the requested page, then no actual disk
2930** read occurs and the memory image of the page is initialized to
2931** all zeros. The extra data appended to a page is always initialized
2932** to zeros the first time a page is loaded into memory.
2933**
drhd9b02572001-04-15 00:37:09 +00002934** The acquisition might fail for several reasons. In all cases,
2935** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00002936**
drhd33d5a82007-04-26 12:11:28 +00002937** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00002938** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00002939** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00002940** just returns 0. This routine acquires a read-lock the first time it
2941** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00002942** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00002943** or journal files.
drh0787db62007-03-04 13:15:27 +00002944**
drh538f5702007-04-13 02:14:30 +00002945** If noContent is false, the page contents are actually read from disk.
2946** If noContent is true, it means that we do not care about the contents
2947** of the page at this time, so do not do a disk read. Just fill in the
2948** page content with zeros. But mark the fact that we have not read the
2949** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00002950** sqlite3PagerWrite() is called on this page or if this routine is
2951** called again with noContent==0, that means that the content is needed
2952** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00002953*/
danielk197765e0ff32008-09-19 09:14:43 +00002954int sqlite3PagerAcquire(
drh538f5702007-04-13 02:14:30 +00002955 Pager *pPager, /* The pager open on the database file */
2956 Pgno pgno, /* Page number to fetch */
2957 DbPage **ppPage, /* Write a pointer to the page here */
2958 int noContent /* Do not bother reading content from disk if true */
2959){
danielk19778c0a7912008-08-20 14:49:23 +00002960 PgHdr *pPg = 0;
drh165ffe92005-03-15 17:09:30 +00002961 int rc;
drhed7c8552001-04-11 14:29:21 +00002962
danielk19778c0a7912008-08-20 14:49:23 +00002963 assert( pPager->state==PAGER_UNLOCK
2964 || sqlite3PcacheRefCount(pPager->pPCache)>0
danielk1977b3175382008-10-17 18:51:52 +00002965 || pgno==1
danielk19778c0a7912008-08-20 14:49:23 +00002966 );
danielk1977e277be02007-03-23 18:12:06 +00002967
danielk197726836652005-01-17 01:33:13 +00002968 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
2969 ** number greater than this, or zero, is requested.
2970 */
drh267cb322005-09-16 17:16:52 +00002971 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00002972 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00002973 }
2974
drhd9b02572001-04-15 00:37:09 +00002975 /* Make sure we have not hit any critical errors.
2976 */
drh836faa42003-01-11 13:30:57 +00002977 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00002978 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00002979
danielk197713adf8a2004-06-03 16:08:41 +00002980 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00002981 ** on the database file. pagerSharedLock() is a no-op if
2982 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00002983 */
danielk1977e277be02007-03-23 18:12:06 +00002984 rc = pagerSharedLock(pPager);
2985 if( rc!=SQLITE_OK ){
2986 return rc;
drhed7c8552001-04-11 14:29:21 +00002987 }
danielk1977e277be02007-03-23 18:12:06 +00002988 assert( pPager->state!=PAGER_UNLOCK );
2989
danielk19778c0a7912008-08-20 14:49:23 +00002990 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
2991 if( rc!=SQLITE_OK ){
2992 return rc;
2993 }
danielk19778c0a7912008-08-20 14:49:23 +00002994 if( pPg->pPager==0 ){
2995 /* The pager cache has created a new page. Its content needs to
2996 ** be initialized.
2997 */
danielk1977979f38e2007-03-27 16:19:51 +00002998 int nMax;
drh538f5702007-04-13 02:14:30 +00002999 PAGER_INCR(pPager->nMiss);
danielk19778c0a7912008-08-20 14:49:23 +00003000 pPg->pPager = pPager;
danielk19778c0a7912008-08-20 14:49:23 +00003001 memset(pPg->pExtra, 0, pPager->nExtra);
danielk197724168722007-04-02 05:07:47 +00003002
danielk1977ad0132d2008-06-07 08:58:22 +00003003 rc = sqlite3PagerPagecount(pPager, &nMax);
3004 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00003005 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003006 return rc;
3007 }
danielk197775bab7d2006-01-23 13:09:45 +00003008
danielk1977a1fa00d2008-08-27 15:16:33 +00003009 if( nMax<(int)pgno || MEMDB || noContent ){
drhf8e632b2007-05-08 14:51:36 +00003010 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003011 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003012 return SQLITE_FULL;
3013 }
danielk19778c0a7912008-08-20 14:49:23 +00003014 memset(pPg->pData, 0, pPager->pageSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00003015 if( noContent ){
danielk19778c0a7912008-08-20 14:49:23 +00003016 pPg->flags |= PGHDR_NEED_READ;
3017 }
drh538f5702007-04-13 02:14:30 +00003018 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003019 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003020 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003021 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
danielk19778c0a7912008-08-20 14:49:23 +00003022 /* sqlite3PagerUnref(pPg); */
3023 pagerDropPage(pPg);
drh551b7732006-11-06 21:20:25 +00003024 return rc;
drh81a20f22001-10-12 17:30:04 +00003025 }
drh306dc212001-05-21 13:45:10 +00003026 }
danielk19773c407372005-02-15 02:54:14 +00003027#ifdef SQLITE_CHECK_PAGES
3028 pPg->pageHash = pager_pagehash(pPg);
3029#endif
drhed7c8552001-04-11 14:29:21 +00003030 }else{
drhd9b02572001-04-15 00:37:09 +00003031 /* The requested page is in the page cache. */
danielk19778c0a7912008-08-20 14:49:23 +00003032 assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003033 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003034 if( !noContent ){
3035 rc = pager_get_content(pPg);
3036 if( rc ){
danielk19778c0a7912008-08-20 14:49:23 +00003037 sqlite3PagerUnref(pPg);
drhd33d5a82007-04-26 12:11:28 +00003038 return rc;
3039 }
3040 }
drhed7c8552001-04-11 14:29:21 +00003041 }
danielk19778c0a7912008-08-20 14:49:23 +00003042
danielk19773b8a05f2007-03-19 17:44:26 +00003043 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003044 return SQLITE_OK;
3045}
danielk19778c0a7912008-08-20 14:49:23 +00003046
drhed7c8552001-04-11 14:29:21 +00003047/*
drh7e3b0a02001-04-28 16:52:40 +00003048** Acquire a page if it is already in the in-memory cache. Do
3049** not read the page from disk. Return a pointer to the page,
3050** or 0 if the page is not in cache.
3051**
danielk19773b8a05f2007-03-19 17:44:26 +00003052** See also sqlite3PagerGet(). The difference between this routine
3053** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003054** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003055** returns NULL if the page is not in cache or if a disk I/O error
3056** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003057*/
danielk19773b8a05f2007-03-19 17:44:26 +00003058DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003059 PgHdr *pPg = 0;
drh836faa42003-01-11 13:30:57 +00003060 assert( pPager!=0 );
3061 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003062
danielk19778c0a7912008-08-20 14:49:23 +00003063 if( (pPager->state!=PAGER_UNLOCK)
3064 && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL)
3065 ){
3066 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
danielk1977e277be02007-03-23 18:12:06 +00003067 }
danielk19778c0a7912008-08-20 14:49:23 +00003068
danielk19773b8a05f2007-03-19 17:44:26 +00003069 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003070}
3071
3072/*
drhed7c8552001-04-11 14:29:21 +00003073** Release a page.
3074**
3075** If the number of references to the page drop to zero, then the
3076** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003077** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003078** removed.
3079*/
danielk19773b8a05f2007-03-19 17:44:26 +00003080int sqlite3PagerUnref(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003081 if( pPg ){
3082 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00003083 sqlite3PcacheRelease(pPg);
3084 pagerUnlockIfUnused(pPager);
drhed7c8552001-04-11 14:29:21 +00003085 }
drhd9b02572001-04-15 00:37:09 +00003086 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003087}
3088
danielk1977fd7f0452008-12-17 17:30:26 +00003089static int openSubJournal(Pager *pPager){
3090 int rc = SQLITE_OK;
3091 if( pPager->journalOpen && !pPager->sjfd->pMethods ){
3092 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3093 sqlite3MemJournalOpen(pPager->sjfd);
3094 }else{
3095 rc = sqlite3PagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
3096 }
3097 }
3098 return rc;
3099}
3100
drhed7c8552001-04-11 14:29:21 +00003101/*
drha6abd042004-06-09 17:37:22 +00003102** Create a journal file for pPager. There should already be a RESERVED
3103** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003104**
3105** Return SQLITE_OK if everything. Return an error code and release the
3106** write lock if anything goes wrong.
3107*/
3108static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003109 sqlite3_vfs *pVfs = pPager->pVfs;
3110 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3111
drhda47d772002-12-02 04:25:19 +00003112 int rc;
drha6abd042004-06-09 17:37:22 +00003113 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003114 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003115 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003116 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003117 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00003118 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003119 rc = SQLITE_NOMEM;
3120 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003121 }
danielk1977b4b47412007-08-17 15:53:36 +00003122
drhfdc40e92008-04-17 20:59:37 +00003123 if( pPager->journalOpen==0 ){
3124 if( pPager->tempFile ){
3125 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3126 }else{
3127 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003128 }
danielk1977b3175382008-10-17 18:51:52 +00003129 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
3130 sqlite3MemJournalOpen(pPager->jfd);
3131 rc = SQLITE_OK;
3132 }else{
drhfdc40e92008-04-17 20:59:37 +00003133#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977b3175382008-10-17 18:51:52 +00003134 rc = sqlite3JournalOpen(
3135 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3136 );
drhfdc40e92008-04-17 20:59:37 +00003137#else
danielk1977b3175382008-10-17 18:51:52 +00003138 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
drhfdc40e92008-04-17 20:59:37 +00003139#endif
danielk1977b3175382008-10-17 18:51:52 +00003140 }
drhfdc40e92008-04-17 20:59:37 +00003141 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3142 pPager->journalOff = 0;
3143 pPager->setMaster = 0;
3144 pPager->journalHdr = 0;
3145 if( rc!=SQLITE_OK ){
3146 if( rc==SQLITE_NOMEM ){
3147 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3148 }
3149 goto failed_to_open_journal;
3150 }
drhda47d772002-12-02 04:25:19 +00003151 }
3152 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003153 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003154 pPager->needSync = 0;
drh968af522003-02-11 14:55:40 +00003155 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003156 if( pPager->errCode ){
3157 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003158 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003159 }
danielk19773460d192008-12-27 15:23:13 +00003160 pPager->dbOrigSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003161
danielk197776572402004-06-25 02:38:54 +00003162 rc = writeJournalHdr(pPager);
3163
danielk1977fd7f0452008-12-17 17:30:26 +00003164 if( pPager->nSavepoint && rc==SQLITE_OK ){
3165 rc = openSubJournal(pPager);
drhda47d772002-12-02 04:25:19 +00003166 }
danielk1977ae72d982007-10-03 08:46:44 +00003167 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003168 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003169 if( rc==SQLITE_OK ){
3170 rc = SQLITE_FULL;
3171 }
3172 }
drh9c105bb2004-10-02 20:38:28 +00003173 return rc;
3174
3175failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003176 sqlite3BitvecDestroy(pPager->pInJournal);
3177 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003178 return rc;
drhda47d772002-12-02 04:25:19 +00003179}
3180
3181/*
drh4b845d72002-03-05 12:41:19 +00003182** Acquire a write-lock on the database. The lock is removed when
3183** the any of the following happen:
3184**
drh80e35f42007-03-30 14:06:34 +00003185** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003186** * sqlite3PagerRollback() is called.
3187** * sqlite3PagerClose() is called.
3188** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003189**
danielk197713adf8a2004-06-03 16:08:41 +00003190** The first parameter to this routine is a pointer to any open page of the
3191** database file. Nothing changes about the page - it is used merely to
3192** acquire a pointer to the Pager structure and as proof that there is
3193** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003194**
danielk197713adf8a2004-06-03 16:08:41 +00003195** The second parameter indicates how much space in bytes to reserve for a
3196** master journal file-name at the start of the journal when it is created.
3197**
3198** A journal file is opened if this is not a temporary file. For temporary
3199** files, the opening of the journal file is deferred until there is an
3200** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003201**
drha6abd042004-06-09 17:37:22 +00003202** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003203**
3204** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3205** immediately instead of waiting until we try to flush the cache. The
3206** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003207*/
danielk19773b8a05f2007-03-19 17:44:26 +00003208int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003209 Pager *pPager = pPg->pPager;
3210 int rc = SQLITE_OK;
3211 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003212 assert( pPager->state!=PAGER_UNLOCK );
3213 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003214 assert( pPager->pInJournal==0 );
danielk1977b3175382008-10-17 18:51:52 +00003215 assert( !MEMDB );
danielk1977b3175382008-10-17 18:51:52 +00003216 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
3217 if( rc==SQLITE_OK ){
3218 pPager->state = PAGER_RESERVED;
3219 if( exFlag ){
3220 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh684917c2004-10-05 02:41:42 +00003221 }
danielk1977b3175382008-10-17 18:51:52 +00003222 }
3223 if( rc!=SQLITE_OK ){
3224 return rc;
3225 }
3226 pPager->dirtyCache = 0;
drh30d53702009-01-06 15:58:57 +00003227 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00003228 if( pPager->useJournal && !pPager->tempFile
3229 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
3230 rc = pager_open_journal(pPager);
drh4b845d72002-03-05 12:41:19 +00003231 }
danielk1977334cdb62007-03-26 08:05:12 +00003232 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00003233 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00003234 ** time a (read or write) transaction was successfully concluded
3235 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00003236 ** kept open and either was truncated to 0 bytes or its header was
3237 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00003238 */
3239 assert( pPager->nRec==0 );
danielk19773460d192008-12-27 15:23:13 +00003240 assert( pPager->dbOrigSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003241 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003242 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003243 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhf5e7bb52008-02-18 14:47:33 +00003244 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003245 rc = SQLITE_NOMEM;
3246 }else{
danielk19773460d192008-12-27 15:23:13 +00003247 pPager->dbOrigSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003248 rc = writeJournalHdr(pPager);
3249 }
drh4b845d72002-03-05 12:41:19 +00003250 }
danielk1977334cdb62007-03-26 08:05:12 +00003251 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh4b845d72002-03-05 12:41:19 +00003252 return rc;
3253}
3254
3255/*
drhed7c8552001-04-11 14:29:21 +00003256** Mark a data page as writeable. The page is written into the journal
3257** if it is not there already. This routine must be called before making
3258** changes to a page.
3259**
3260** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003261** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003262** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003263** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003264** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003265**
3266** If the journal file could not be written because the disk is full,
3267** then this routine returns SQLITE_FULL and does an immediate rollback.
3268** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003269** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003270** reset.
drhed7c8552001-04-11 14:29:21 +00003271*/
danielk19773b8a05f2007-03-19 17:44:26 +00003272static int pager_write(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003273 void *pData = pPg->pData;
drh69688d52001-04-14 16:38:23 +00003274 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003275 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003276
drh6446c4d2001-12-15 14:22:18 +00003277 /* Check for errors
3278 */
danielk1977efaaf572006-01-16 11:29:19 +00003279 if( pPager->errCode ){
3280 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003281 }
drh5e00f6c2001-09-13 13:46:56 +00003282 if( pPager->readOnly ){
3283 return SQLITE_PERM;
3284 }
drh6446c4d2001-12-15 14:22:18 +00003285
danielk197776572402004-06-25 02:38:54 +00003286 assert( !pPager->setMaster );
3287
danielk19773c407372005-02-15 02:54:14 +00003288 CHECK_PAGE(pPg);
3289
drh538f5702007-04-13 02:14:30 +00003290 /* If this page was previously acquired with noContent==1, that means
3291 ** we didn't really read in the content of the page. This can happen
3292 ** (for example) when the page is being moved to the freelist. But
3293 ** now we are (perhaps) moving the page off of the freelist for
3294 ** reuse and we need to know its original content so that content
3295 ** can be stored in the rollback journal. So do the read at this
3296 ** time.
3297 */
drhd33d5a82007-04-26 12:11:28 +00003298 rc = pager_get_content(pPg);
3299 if( rc ){
3300 return rc;
drh538f5702007-04-13 02:14:30 +00003301 }
3302
drh6446c4d2001-12-15 14:22:18 +00003303 /* Mark the page as dirty. If the page has already been written
3304 ** to the journal then we can return right away.
3305 */
drhc047b9f2008-11-21 03:23:43 +00003306 sqlite3PcacheMakeDirty(pPg);
danielk19773460d192008-12-27 15:23:13 +00003307 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
drha6abd042004-06-09 17:37:22 +00003308 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003309 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003310 }else{
drh6446c4d2001-12-15 14:22:18 +00003311
danielk1977a0bf2652004-11-04 14:30:04 +00003312 /* If we get this far, it means that the page needs to be
3313 ** written to the transaction journal or the ckeckpoint journal
3314 ** or both.
3315 **
3316 ** First check to see that the transaction journal exists and
3317 ** create it if it does not.
3318 */
3319 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003320 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003321 if( rc!=SQLITE_OK ){
3322 return rc;
3323 }
3324 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00003325 if( !pPager->journalOpen && pPager->useJournal
3326 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00003327 rc = pager_open_journal(pPager);
3328 if( rc!=SQLITE_OK ) return rc;
3329 }
danielk1977a0bf2652004-11-04 14:30:04 +00003330 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003331 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003332
3333 /* The transaction journal now exists and we have a RESERVED or an
3334 ** EXCLUSIVE lock on the main database file. Write the current page to
3335 ** the transaction journal if it is not there already.
3336 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00003337 if( !pageInJournal(pPg) && pPager->journalOpen ){
danielk19773460d192008-12-27 15:23:13 +00003338 if( pPg->pgno<=pPager->dbOrigSize ){
danielk1977b3175382008-10-17 18:51:52 +00003339 u32 cksum;
3340 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00003341
danielk1977b3175382008-10-17 18:51:52 +00003342 /* We should never write to the journal file the page that
3343 ** contains the database locks. The following assert verifies
3344 ** that we do not. */
3345 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
3346 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
3347 cksum = pager_cksum(pPager, (u8*)pData2);
3348 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
3349 if( rc==SQLITE_OK ){
3350 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
3351 pPager->journalOff + 4);
3352 pPager->journalOff += pPager->pageSize+4;
3353 }
3354 if( rc==SQLITE_OK ){
3355 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
3356 pPager->journalOff += 4;
3357 }
3358 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
3359 pPager->journalOff, pPager->pageSize));
3360 PAGER_INCR(sqlite3_pager_writej_count);
drh30d53702009-01-06 15:58:57 +00003361 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
danielk1977b3175382008-10-17 18:51:52 +00003362 PAGERID(pPager), pPg->pgno,
drh30d53702009-01-06 15:58:57 +00003363 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
danielk197707cb5602006-01-20 10:55:05 +00003364
danielk1977f3107512008-12-22 10:58:46 +00003365 /* Even if an IO or diskfull error occurred while journalling the
3366 ** page in the block above, set the need-sync flag for the page.
3367 ** Otherwise, when the transaction is rolled back, the logic in
3368 ** playback_one_page() will think that the page needs to be restored
3369 ** in the database file. And if an IO error occurs while doing so,
3370 ** then corruption may follow.
3371 */
3372 if( !pPager->noSync ){
3373 pPg->flags |= PGHDR_NEED_SYNC;
danielk1977a4124bd2008-12-23 10:37:47 +00003374 pPager->needSync = 1;
danielk1977f3107512008-12-22 10:58:46 +00003375 }
3376
danielk1977b3175382008-10-17 18:51:52 +00003377 /* An error has occured writing to the journal file. The
3378 ** transaction will be rolled back by the layer above.
3379 */
3380 if( rc!=SQLITE_OK ){
3381 return rc;
3382 }
danielk197707cb5602006-01-20 10:55:05 +00003383
danielk1977b3175382008-10-17 18:51:52 +00003384 pPager->nRec++;
3385 assert( pPager->pInJournal!=0 );
drh7539b6b2009-01-02 21:39:39 +00003386 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3387 testcase( rc==SQLITE_NOMEM );
3388 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
3389 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
3390 if( rc!=SQLITE_OK ){
3391 assert( rc==SQLITE_NOMEM );
3392 return rc;
3393 }
danielk197713adf8a2004-06-03 16:08:41 +00003394 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003395 if( !pPager->journalStarted && !pPager->noSync ){
3396 pPg->flags |= PGHDR_NEED_SYNC;
danielk1977a4124bd2008-12-23 10:37:47 +00003397 pPager->needSync = 1;
danielk19778c0a7912008-08-20 14:49:23 +00003398 }
drh30d53702009-01-06 15:58:57 +00003399 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
danielk19778c0a7912008-08-20 14:49:23 +00003400 PAGERID(pPager), pPg->pgno,
drh30d53702009-01-06 15:58:57 +00003401 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
danielk1977a0bf2652004-11-04 14:30:04 +00003402 }
danielk1977a0bf2652004-11-04 14:30:04 +00003403 }
3404
3405 /* If the statement journal is open and the page is not in it,
3406 ** then write the current page to the statement journal. Note that
3407 ** the statement journal format differs from the standard journal format
3408 ** in that it omits the checksums and the header.
3409 */
danielk19773460d192008-12-27 15:23:13 +00003410 if( subjRequiresPage(pPg) ){
danielk1977f2c31ad2009-01-06 13:40:08 +00003411 rc = subjournalPage(pPg);
drhd9b02572001-04-15 00:37:09 +00003412 }
drhfa86c412002-02-02 15:01:15 +00003413 }
3414
3415 /* Update the database size and return.
3416 */
drh1aa2d8b2007-01-03 15:34:29 +00003417 assert( pPager->state>=PAGER_SHARED );
danielk1977d92db532008-11-17 04:56:24 +00003418 if( pPager->dbSize<pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003419 pPager->dbSize = pPg->pgno;
danielk1977d92db532008-11-17 04:56:24 +00003420 if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){
drh1f595712004-06-15 01:40:29 +00003421 pPager->dbSize++;
3422 }
drh306dc212001-05-21 13:45:10 +00003423 }
drh69688d52001-04-14 16:38:23 +00003424 return rc;
drhed7c8552001-04-11 14:29:21 +00003425}
3426
3427/*
danielk19774099f6e2007-03-19 11:25:20 +00003428** This function is used to mark a data-page as writable. It uses
3429** pager_write() to open a journal file (if it is not already open)
3430** and write the page *pData to the journal.
3431**
3432** The difference between this function and pager_write() is that this
3433** function also deals with the special case where 2 or more pages
3434** fit on a single disk sector. In this case all co-resident pages
3435** must have been written to the journal file before returning.
3436*/
danielk19773b8a05f2007-03-19 17:44:26 +00003437int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003438 int rc = SQLITE_OK;
3439
danielk19773b8a05f2007-03-19 17:44:26 +00003440 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003441 Pager *pPager = pPg->pPager;
3442 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3443
danielk1977b3175382008-10-17 18:51:52 +00003444 if( nPagePerSector>1 ){
danielk19774099f6e2007-03-19 11:25:20 +00003445 Pgno nPageCount; /* Total number of pages in database file */
3446 Pgno pg1; /* First page of the sector pPg is located on. */
3447 int nPage; /* Number of pages starting at pg1 to journal */
3448 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003449 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003450
3451 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3452 ** header to be written between the pages journaled by this function.
3453 */
danielk1977b3175382008-10-17 18:51:52 +00003454 assert( !MEMDB );
danielk19774099f6e2007-03-19 11:25:20 +00003455 assert( pPager->doNotSync==0 );
3456 pPager->doNotSync = 1;
3457
3458 /* This trick assumes that both the page-size and sector-size are
3459 ** an integer power of 2. It sets variable pg1 to the identifier
3460 ** of the first page of the sector pPg is located on.
3461 */
3462 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3463
danielk1977ad0132d2008-06-07 08:58:22 +00003464 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00003465 if( pPg->pgno>nPageCount ){
3466 nPage = (pPg->pgno - pg1)+1;
3467 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3468 nPage = nPageCount+1-pg1;
3469 }else{
3470 nPage = nPagePerSector;
3471 }
3472 assert(nPage>0);
3473 assert(pg1<=pPg->pgno);
3474 assert((pg1+nPage)>pPg->pgno);
3475
3476 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3477 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003478 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00003479 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00003480 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003481 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003482 if( rc==SQLITE_OK ){
3483 rc = pager_write(pPage);
danielk19778c0a7912008-08-20 14:49:23 +00003484 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003485 needSync = 1;
danielk1977a4124bd2008-12-23 10:37:47 +00003486 assert(pPager->needSync);
danielk1977dd97a492007-08-22 18:54:32 +00003487 }
danielk19773b8a05f2007-03-19 17:44:26 +00003488 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003489 }
3490 }
drhc81945e2008-03-10 14:12:53 +00003491 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk19778c0a7912008-08-20 14:49:23 +00003492 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003493 needSync = 1;
3494 }
danielk19778c0a7912008-08-20 14:49:23 +00003495 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003496 }
3497 }
3498
danielk1977dd97a492007-08-22 18:54:32 +00003499 /* If the PgHdr.needSync flag is set for any of the nPage pages
3500 ** starting at pg1, then it needs to be set for all of them. Because
3501 ** writing to any of these nPage pages may damage the others, the
3502 ** journal file must contain sync()ed copies of all of them
3503 ** before any of them can be written out to the database file.
3504 */
3505 if( needSync ){
drhb3df2e12008-09-17 20:06:26 +00003506 assert( !MEMDB && pPager->noSync==0 );
danielk1977dd97a492007-08-22 18:54:32 +00003507 for(ii=0; ii<nPage && needSync; ii++){
3508 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
danielk19778c0a7912008-08-20 14:49:23 +00003509 if( pPage ) pPage->flags |= PGHDR_NEED_SYNC;
3510 sqlite3PagerUnref(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00003511 }
3512 assert(pPager->needSync);
3513 }
3514
danielk19774099f6e2007-03-19 11:25:20 +00003515 assert( pPager->doNotSync==1 );
3516 pPager->doNotSync = 0;
3517 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003518 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003519 }
3520 return rc;
3521}
3522
3523/*
drhaacc5432002-01-06 17:07:40 +00003524** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003525** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003526** to change the content of the page.
3527*/
danielk19777d3a6662006-01-23 16:21:05 +00003528#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003529int sqlite3PagerIswriteable(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003530 return pPg->flags&PGHDR_DIRTY;
drh6019e162001-07-02 17:51:45 +00003531}
danielk19777d3a6662006-01-23 16:21:05 +00003532#endif
drh6019e162001-07-02 17:51:45 +00003533
drh001bbcb2003-03-19 03:14:00 +00003534/*
drh30e58752002-03-02 20:41:57 +00003535** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003536** write the information on page pPg back to the disk, even though
drhdfe88ec2008-11-03 20:55:06 +00003537** that page might be marked as dirty. This happens, for example, when
3538** the page has been added as a leaf of the freelist and so its
3539** content no longer matters.
drh30e58752002-03-02 20:41:57 +00003540**
3541** The overlying software layer calls this routine when all of the data
3542** on the given page is unused. The pager marks the page as clean so
3543** that it does not get written to disk.
3544**
3545** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003546** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003547** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003548**
3549** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003550** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003551** will thereafter be ignored. This is necessary to avoid a problem
3552** where a page with data is added to the freelist during one part of
3553** a transaction then removed from the freelist during a later part
3554** of the same transaction and reused for some other purpose. When it
3555** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003556** the sqlite3PagerDontRollback() routine is called. But because the
3557** page contains critical data, we still need to be sure it gets
3558** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003559*/
danielk1977a1fa00d2008-08-27 15:16:33 +00003560int sqlite3PagerDontWrite(DbPage *pDbPage){
drh538f5702007-04-13 02:14:30 +00003561 PgHdr *pPg = pDbPage;
3562 Pager *pPager = pPg->pPager;
danielk1977a1fa00d2008-08-27 15:16:33 +00003563 int rc;
drh8e298f92002-07-06 16:28:47 +00003564
danielk19773460d192008-12-27 15:23:13 +00003565 if( pPg->pgno>pPager->dbOrigSize ){
danielk1977a1fa00d2008-08-27 15:16:33 +00003566 return SQLITE_OK;
3567 }
3568 if( pPager->pAlwaysRollback==0 ){
3569 assert( pPager->pInJournal );
danielk19773460d192008-12-27 15:23:13 +00003570 pPager->pAlwaysRollback = sqlite3BitvecCreate(pPager->dbOrigSize);
danielk1977a1fa00d2008-08-27 15:16:33 +00003571 if( !pPager->pAlwaysRollback ){
3572 return SQLITE_NOMEM;
3573 }
3574 }
3575 rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno);
3576
danielk1977fd7f0452008-12-17 17:30:26 +00003577 if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
drh1aa2d8b2007-01-03 15:34:29 +00003578 assert( pPager->state>=PAGER_SHARED );
danielk19773460d192008-12-27 15:23:13 +00003579 if( pPager->dbSize==pPg->pgno && pPager->dbOrigSize<pPager->dbSize ){
drh8124a302002-06-25 14:43:57 +00003580 /* If this pages is the last page in the file and the file has grown
3581 ** during the current transaction, then do NOT mark the page as clean.
3582 ** When the database file grows, we must make sure that the last page
3583 ** gets written at least once so that the disk file will be the correct
3584 ** size. If you do not write this page and the size of the file
3585 ** on the disk ends up being too small, that can lead to database
3586 ** corruption during the next transaction.
3587 */
3588 }else{
drh30d53702009-01-06 15:58:57 +00003589 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
drh538f5702007-04-13 02:14:30 +00003590 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
danielk197733e32162008-08-23 18:53:08 +00003591 pPg->flags |= PGHDR_DONT_WRITE;
danielk19773c407372005-02-15 02:54:14 +00003592#ifdef SQLITE_CHECK_PAGES
3593 pPg->pageHash = pager_pagehash(pPg);
3594#endif
drh8124a302002-06-25 14:43:57 +00003595 }
drh30e58752002-03-02 20:41:57 +00003596 }
danielk1977a1fa00d2008-08-27 15:16:33 +00003597 return rc;
drh30e58752002-03-02 20:41:57 +00003598}
3599
3600/*
3601** A call to this routine tells the pager that if a rollback occurs,
3602** it is not necessary to restore the data on the given page. This
3603** means that the pager does not have to record the given page in the
3604** rollback journal.
drh538f5702007-04-13 02:14:30 +00003605**
3606** If we have not yet actually read the content of this page (if
3607** the PgHdr.needRead flag is set) then this routine acts as a promise
3608** that we will never need to read the page content in the future.
3609** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00003610*/
danielk19773b8a05f2007-03-19 17:44:26 +00003611void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003612 Pager *pPager = pPg->pPager;
drh7539b6b2009-01-02 21:39:39 +00003613 TESTONLY( int rc; ) /* Return value from sqlite3BitvecSet() */
drh30e58752002-03-02 20:41:57 +00003614
drhd3627af2006-12-18 18:34:51 +00003615 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00003616
3617 /* If the journal file is not open, or DontWrite() has been called on
3618 ** this page (DontWrite() sets the alwaysRollback flag), then this
3619 ** function is a no-op.
3620 */
danielk1977a1fa00d2008-08-27 15:16:33 +00003621 if( pPager->journalOpen==0
3622 || sqlite3BitvecTest(pPager->pAlwaysRollback, pPg->pgno)
danielk19773460d192008-12-27 15:23:13 +00003623 || pPg->pgno>pPager->dbOrigSize
danielk19778c0a7912008-08-20 14:49:23 +00003624 ){
danielk197787c29a92008-01-18 11:33:16 +00003625 return;
3626 }
danielk1977a55e9352008-01-21 13:04:34 +00003627
drhc5d0bd92008-04-14 01:00:57 +00003628#ifdef SQLITE_SECURE_DELETE
drh1feb7dd2008-11-19 18:30:29 +00003629 if( sqlite3BitvecTest(pPager->pInJournal, pPg->pgno)!=0
danielk19773460d192008-12-27 15:23:13 +00003630 || pPg->pgno>pPager->dbOrigSize ){
drhc5d0bd92008-04-14 01:00:57 +00003631 return;
3632 }
3633#endif
3634
3635 /* If SECURE_DELETE is disabled, then there is no way that this
3636 ** routine can be called on a page for which sqlite3PagerDontWrite()
3637 ** has not been previously called during the same transaction.
3638 ** And if DontWrite() has previously been called, the following
3639 ** conditions must be met.
drh10131482008-07-11 03:34:09 +00003640 **
3641 ** (Later:) Not true. If the database is corrupted by having duplicate
3642 ** pages on the freelist (ex: corrupt9.test) then the following is not
3643 ** necessarily true:
danielk1977a55e9352008-01-21 13:04:34 +00003644 */
danielk19773460d192008-12-27 15:23:13 +00003645 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->dbOrigSize ); */
danielk1977a55e9352008-01-21 13:04:34 +00003646
drhf5e7bb52008-02-18 14:47:33 +00003647 assert( pPager->pInJournal!=0 );
danielk19778c0a7912008-08-20 14:49:23 +00003648 pPg->flags &= ~PGHDR_NEED_READ;
drh7539b6b2009-01-02 21:39:39 +00003649
3650 /* Failure to set the bits in the InJournal bit-vectors is benign.
3651 ** It merely means that we might do some extra work to journal a page
drha8a71ba2009-01-03 12:55:17 +00003652 ** that does not need to be journaled. Nevertheless, be sure to test the
drh7539b6b2009-01-02 21:39:39 +00003653 ** case where a malloc error occurs while trying to set a bit in a
3654 ** bit vector.
3655 */
3656 sqlite3BeginBenignMalloc();
3657 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3658 testcase( rc==SQLITE_NOMEM );
3659 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pPg->pgno);
3660 testcase( rc==SQLITE_NOMEM );
3661 sqlite3EndBenignMalloc();
3662
3663
drh30d53702009-01-06 15:58:57 +00003664 PAGERTRACE(("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)));
danielk1977a55e9352008-01-21 13:04:34 +00003665 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00003666}
3667
drhac69b052004-05-12 13:30:07 +00003668
drh30e58752002-03-02 20:41:57 +00003669/*
drh80e35f42007-03-30 14:06:34 +00003670** This routine is called to increment the database file change-counter,
3671** stored at byte 24 of the pager file.
3672*/
danielk1977c7b60172007-08-22 11:22:03 +00003673static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00003674 PgHdr *pPgHdr;
3675 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00003676 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00003677
drh701bb3b2008-08-02 03:50:39 +00003678#ifndef SQLITE_ENABLE_ATOMIC_WRITE
3679 assert( isDirect==0 ); /* isDirect is only true for atomic writes */
3680#endif
danielk197712dd5492008-12-18 15:45:07 +00003681 if( !pPager->changeCountDone && pPager->dbSize>0 ){
drh80e35f42007-03-30 14:06:34 +00003682 /* Open page 1 of the file for writing. */
3683 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3684 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00003685
3686 if( !isDirect ){
3687 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00003688 if( rc!=SQLITE_OK ){
3689 sqlite3PagerUnref(pPgHdr);
3690 return rc;
3691 }
danielk1977c7b60172007-08-22 11:22:03 +00003692 }
3693
drh80e35f42007-03-30 14:06:34 +00003694 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00003695 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00003696 change_counter++;
danielk19778c0a7912008-08-20 14:49:23 +00003697 put32bits(((char*)pPgHdr->pData)+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00003698
drh701bb3b2008-08-02 03:50:39 +00003699#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977c7b60172007-08-22 11:22:03 +00003700 if( isDirect && pPager->fd->pMethods ){
danielk19778c0a7912008-08-20 14:49:23 +00003701 const void *zBuf = pPgHdr->pData;
danielk19773460d192008-12-27 15:23:13 +00003702 assert( pPager->dbFileSize>0 );
danielk1977c7b60172007-08-22 11:22:03 +00003703 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
3704 }
drh701bb3b2008-08-02 03:50:39 +00003705#endif
danielk1977c7b60172007-08-22 11:22:03 +00003706
drh80e35f42007-03-30 14:06:34 +00003707 /* Release the page reference. */
3708 sqlite3PagerUnref(pPgHdr);
3709 pPager->changeCountDone = 1;
3710 }
danielk1977c7b60172007-08-22 11:22:03 +00003711 return rc;
drh80e35f42007-03-30 14:06:34 +00003712}
3713
3714/*
danielk1977f653d782008-03-20 11:04:21 +00003715** Sync the pager file to disk.
3716*/
3717int sqlite3PagerSync(Pager *pPager){
3718 int rc;
drh7426f862008-08-26 21:07:26 +00003719 if( MEMDB ){
3720 rc = SQLITE_OK;
3721 }else{
3722 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3723 }
danielk1977f653d782008-03-20 11:04:21 +00003724 return rc;
3725}
3726
3727/*
drh80e35f42007-03-30 14:06:34 +00003728** Sync the database file for the pager pPager. zMaster points to the name
3729** of a master journal file that should be written into the individual
3730** journal file. zMaster may be NULL, which is interpreted as no master
3731** journal (a single database transaction).
3732**
3733** This routine ensures that the journal is synced, all dirty pages written
3734** to the database file and the database file synced. The only thing that
3735** remains to commit the transaction is to delete the journal file (or
3736** master journal file if specified).
3737**
3738** Note that if zMaster==NULL, this does not overwrite a previous value
3739** passed to an sqlite3PagerCommitPhaseOne() call.
3740**
danielk1977f653d782008-03-20 11:04:21 +00003741** If the final parameter - noSync - is true, then the database file itself
3742** is not synced. The caller must call sqlite3PagerSync() directly to
3743** sync the database file before calling CommitPhaseTwo() to delete the
3744** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00003745*/
danielk1977f653d782008-03-20 11:04:21 +00003746int sqlite3PagerCommitPhaseOne(
3747 Pager *pPager,
3748 const char *zMaster,
danielk1977f653d782008-03-20 11:04:21 +00003749 int noSync
3750){
drh80e35f42007-03-30 14:06:34 +00003751 int rc = SQLITE_OK;
3752
danielk1977dad31b52008-05-15 11:08:07 +00003753 if( pPager->errCode ){
3754 return pPager->errCode;
3755 }
3756
drhd138c012008-05-13 00:58:18 +00003757 /* If no changes have been made, we can leave the transaction early.
3758 */
3759 if( pPager->dbModified==0 &&
3760 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3761 pPager->exclusiveMode!=0) ){
3762 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3763 return SQLITE_OK;
3764 }
3765
drh30d53702009-01-06 15:58:57 +00003766 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
3767 pPager->zFilename, zMaster, pPager->dbSize));
drh80e35f42007-03-30 14:06:34 +00003768
3769 /* If this is an in-memory db, or no pages have been written to, or this
3770 ** function has already been called, it is a no-op.
3771 */
3772 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
3773 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00003774
danielk1977c7b60172007-08-22 11:22:03 +00003775#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3776 /* The atomic-write optimization can be used if all of the
3777 ** following are true:
3778 **
3779 ** + The file-system supports the atomic-write property for
3780 ** blocks of size page-size, and
3781 ** + This commit is not part of a multi-file transaction, and
3782 ** + Exactly one page has been modified and store in the journal file.
3783 **
3784 ** If the optimization can be used, then the journal file will never
3785 ** be created for this transaction.
3786 */
danielk19774d60af92008-09-08 15:35:06 +00003787 int useAtomicWrite;
danielk19778c0a7912008-08-20 14:49:23 +00003788 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
danielk19774d60af92008-09-08 15:35:06 +00003789 useAtomicWrite = (
danielk1977f55b8992007-08-24 08:15:53 +00003790 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00003791 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00003792 pPager->journalOff==jrnlBufferSize(pPager) &&
danielk19773460d192008-12-27 15:23:13 +00003793 pPager->dbSize>=pPager->dbFileSize &&
danielk19778c0a7912008-08-20 14:49:23 +00003794 (pPg==0 || pPg->pDirty==0)
danielk1977f55b8992007-08-24 08:15:53 +00003795 );
danielk1977700b9c52008-04-24 12:37:40 +00003796 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00003797 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00003798 /* Update the nRec field in the journal file. */
3799 int offset = pPager->journalHdr + sizeof(aJournalMagic);
3800 assert(pPager->nRec==1);
3801 rc = write32bits(pPager->jfd, offset, pPager->nRec);
3802
3803 /* Update the db file change counter. The following call will modify
3804 ** the in-memory representation of page 1 to include the updated
3805 ** change counter and then write page 1 directly to the database
3806 ** file. Because of the atomic-write property of the host file-system,
3807 ** this is safe.
3808 */
danielk1977ae72d982007-10-03 08:46:44 +00003809 if( rc==SQLITE_OK ){
3810 rc = pager_incr_changecounter(pPager, 1);
3811 }
danielk1977f55b8992007-08-24 08:15:53 +00003812 }else{
3813 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00003814 }
3815
danielk1977ae72d982007-10-03 08:46:44 +00003816 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00003817#endif
3818
drh80e35f42007-03-30 14:06:34 +00003819 /* If a master journal file name has already been written to the
3820 ** journal file, then no sync is required. This happens when it is
3821 ** written, then the process fails to upgrade from a RESERVED to an
3822 ** EXCLUSIVE lock. The next time the process tries to commit the
3823 ** transaction the m-j name will have already been written.
3824 */
3825 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00003826 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00003827 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00003828 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00003829#ifndef SQLITE_OMIT_AUTOVACUUM
danielk19773460d192008-12-27 15:23:13 +00003830 if( pPager->dbSize<pPager->dbOrigSize ){
danielk197771aa7ff2008-05-20 07:05:09 +00003831 /* If this transaction has made the database smaller, then all pages
3832 ** being discarded by the truncation must be written to the journal
3833 ** file.
3834 */
3835 Pgno i;
danielk1977d92db532008-11-17 04:56:24 +00003836 Pgno iSkip = PAGER_MJ_PGNO(pPager);
danielk19773460d192008-12-27 15:23:13 +00003837 Pgno dbSize = pPager->dbSize;
danielk1977f9bce3c2009-01-06 15:20:58 +00003838 pPager->dbSize = pPager->dbOrigSize;
drh30d53702009-01-06 15:58:57 +00003839 for( i=pPager->dbSize+1; i<=pPager->dbOrigSize; i++ ){
danielk197771aa7ff2008-05-20 07:05:09 +00003840 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
3841 rc = sqlite3PagerGet(pPager, i, &pPg);
3842 if( rc!=SQLITE_OK ) goto sync_exit;
3843 rc = sqlite3PagerWrite(pPg);
3844 sqlite3PagerUnref(pPg);
3845 if( rc!=SQLITE_OK ) goto sync_exit;
3846 }
3847 }
danielk19773460d192008-12-27 15:23:13 +00003848 pPager->dbSize = dbSize;
danielk197771aa7ff2008-05-20 07:05:09 +00003849 }
drh80e35f42007-03-30 14:06:34 +00003850#endif
danielk197771aa7ff2008-05-20 07:05:09 +00003851 rc = writeMasterJournal(pPager, zMaster);
3852 if( rc!=SQLITE_OK ) goto sync_exit;
3853 rc = syncJournal(pPager);
3854 }
drh80e35f42007-03-30 14:06:34 +00003855 }
danielk1977c7b60172007-08-22 11:22:03 +00003856 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00003857
3858#ifndef SQLITE_OMIT_AUTOVACUUM
danielk19773460d192008-12-27 15:23:13 +00003859 if( pPager->dbSize<pPager->dbFileSize ){
3860 rc = sqlite3PagerTruncate(pPager, pPager->dbSize);
drh80e35f42007-03-30 14:06:34 +00003861 if( rc!=SQLITE_OK ) goto sync_exit;
3862 }
3863#endif
3864
3865 /* Write all dirty pages to the database file */
danielk19778c0a7912008-08-20 14:49:23 +00003866 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003867 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00003868 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00003869 assert( rc!=SQLITE_IOERR_BLOCKED );
3870 /* The error might have left the dirty list all fouled up here,
3871 ** but that does not matter because if the if the dirty list did
3872 ** get corrupted, then the transaction will roll back and
3873 ** discard the dirty list. There is an assert in
3874 ** pager_get_all_dirty_pages() that verifies that no attempt
3875 ** is made to use an invalid dirty list.
3876 */
drh153c62c2007-08-24 03:51:33 +00003877 goto sync_exit;
3878 }
danielk19778c0a7912008-08-20 14:49:23 +00003879 sqlite3PcacheCleanAll(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003880
3881 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00003882 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00003883 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00003884 }
3885 IOTRACE(("DBSYNC %p\n", pPager))
3886
3887 pPager->state = PAGER_SYNCED;
danielk19773460d192008-12-27 15:23:13 +00003888 }else if( MEMDB && pPager->dbSize<pPager->dbFileSize ){
3889 rc = sqlite3PagerTruncate(pPager, pPager->dbSize);
drh80e35f42007-03-30 14:06:34 +00003890 }
3891
3892sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00003893 if( rc==SQLITE_IOERR_BLOCKED ){
3894 /* pager_incr_changecounter() may attempt to obtain an exclusive
3895 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00003896 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00003897 * better to return SQLITE_BUSY.
3898 */
3899 rc = SQLITE_BUSY;
3900 }
drh80e35f42007-03-30 14:06:34 +00003901 return rc;
3902}
3903
3904
3905/*
drhed7c8552001-04-11 14:29:21 +00003906** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00003907**
3908** If the commit fails for any reason, a rollback attempt is made
3909** and an error code is returned. If the commit worked, SQLITE_OK
3910** is returned.
drhed7c8552001-04-11 14:29:21 +00003911*/
drh80e35f42007-03-30 14:06:34 +00003912int sqlite3PagerCommitPhaseTwo(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003913 int rc = SQLITE_OK;
drhd9b02572001-04-15 00:37:09 +00003914
danielk1977efaaf572006-01-16 11:29:19 +00003915 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00003916 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003917 }
drha6abd042004-06-09 17:37:22 +00003918 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00003919 return SQLITE_ERROR;
3920 }
drhd138c012008-05-13 00:58:18 +00003921 if( pPager->dbModified==0 &&
3922 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3923 pPager->exclusiveMode!=0) ){
3924 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3925 return SQLITE_OK;
3926 }
drh30d53702009-01-06 15:58:57 +00003927 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00003928 assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dirtyCache );
3929 rc = pager_end_transaction(pPager, pPager->setMaster);
3930 rc = pager_error(pPager, rc);
drh86f8c192007-08-22 00:39:19 +00003931 return rc;
drhed7c8552001-04-11 14:29:21 +00003932}
3933
3934/*
drha6abd042004-06-09 17:37:22 +00003935** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00003936** All in-memory cache pages revert to their original data contents.
3937** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00003938**
3939** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00003940** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00003941** process is writing trash into the journal file (SQLITE_CORRUPT) or
3942** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
3943** codes are returned for all these occasions. Otherwise,
3944** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00003945*/
danielk19773b8a05f2007-03-19 17:44:26 +00003946int sqlite3PagerRollback(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003947 int rc = SQLITE_OK;
drh30d53702009-01-06 15:58:57 +00003948 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
danielk1977b3175382008-10-17 18:51:52 +00003949 if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00003950 rc = pager_end_transaction(pPager, pPager->setMaster);
danielk19778c0a7912008-08-20 14:49:23 +00003951 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00003952 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00003953 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00003954 }
danielk19778c0a7912008-08-20 14:49:23 +00003955 rc = pPager->errCode;
drha6abd042004-06-09 17:37:22 +00003956 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003957 if( pPager->state==PAGER_RESERVED ){
3958 int rc2;
3959 rc = pager_playback(pPager, 0);
3960 rc2 = pager_end_transaction(pPager, pPager->setMaster);
3961 if( rc==SQLITE_OK ){
3962 rc = rc2;
3963 }
3964 }else{
3965 rc = pager_playback(pPager, 0);
3966 }
danielk197707cb5602006-01-20 10:55:05 +00003967
danielk1977b3175382008-10-17 18:51:52 +00003968 if( !MEMDB ){
danielk1977d92db532008-11-17 04:56:24 +00003969 pPager->dbSizeValid = 0;
danielk1977b3175382008-10-17 18:51:52 +00003970 }
danielk19778c0a7912008-08-20 14:49:23 +00003971
3972 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
3973 ** cache. So call pager_error() on the way out to make any error
3974 ** persistent.
3975 */
3976 rc = pager_error(pPager, rc);
3977 }
drh86f8c192007-08-22 00:39:19 +00003978 return rc;
drh98808ba2001-10-18 12:34:46 +00003979}
drhd9b02572001-04-15 00:37:09 +00003980
3981/*
drh5e00f6c2001-09-13 13:46:56 +00003982** Return TRUE if the database file is opened read-only. Return FALSE
3983** if the database is (in theory) writable.
3984*/
drhf49661a2008-12-10 16:45:50 +00003985u8 sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00003986 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00003987}
3988
3989/*
drh0f7eb612006-08-08 13:51:43 +00003990** Return the number of references to the pager.
3991*/
danielk19773b8a05f2007-03-19 17:44:26 +00003992int sqlite3PagerRefcount(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003993 return sqlite3PcacheRefCount(pPager->pPCache);
drh0f7eb612006-08-08 13:51:43 +00003994}
3995
danielk197771d5d2c2008-09-29 11:49:47 +00003996/*
3997** Return the number of references to the specified page.
3998*/
3999int sqlite3PagerPageRefcount(DbPage *pPage){
4000 return sqlite3PcachePageRefcount(pPage);
4001}
4002
drh0f7eb612006-08-08 13:51:43 +00004003#ifdef SQLITE_TEST
4004/*
drhd9b02572001-04-15 00:37:09 +00004005** This routine is used for testing and analysis only.
4006*/
danielk19773b8a05f2007-03-19 17:44:26 +00004007int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004008 static int a[11];
danielk19778c0a7912008-08-20 14:49:23 +00004009 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
4010 a[1] = sqlite3PcachePagecount(pPager->pPCache);
4011 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
danielk1977d92db532008-11-17 04:56:24 +00004012 a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
drhd9b02572001-04-15 00:37:09 +00004013 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004014 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004015 a[6] = pPager->nHit;
4016 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004017 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004018 a[9] = pPager->nRead;
4019 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004020 return a;
4021}
danielk197717b90b52008-06-06 11:11:25 +00004022int sqlite3PagerIsMemdb(Pager *pPager){
4023 return MEMDB;
4024}
drh0f7eb612006-08-08 13:51:43 +00004025#endif
drhdd793422001-06-28 01:54:48 +00004026
drhfa86c412002-02-02 15:01:15 +00004027/*
danielk1977fd7f0452008-12-17 17:30:26 +00004028** Ensure that there are at least nSavepoint savepoints open.
drhfa86c412002-02-02 15:01:15 +00004029*/
danielk1977fd7f0452008-12-17 17:30:26 +00004030int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
4031 int rc = SQLITE_OK;
4032
danielk197712dd5492008-12-18 15:45:07 +00004033 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
danielk1977fd7f0452008-12-17 17:30:26 +00004034 int ii;
drh49b9d332009-01-02 18:10:42 +00004035 PagerSavepoint *aNew;
danielk1977fd7f0452008-12-17 17:30:26 +00004036
4037 /* Either the sub-journal is open or there are no active savepoints. */
4038 assert( pPager->nSavepoint==0 || pPager->sjfd->pMethods );
4039
4040 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
4041 ** if the allocation fails. Otherwise, zero the new portion in case a
4042 ** malloc failure occurs while populating it in the for(...) loop below.
4043 */
drh49b9d332009-01-02 18:10:42 +00004044 aNew = (PagerSavepoint *)sqlite3Realloc(
danielk1977fd7f0452008-12-17 17:30:26 +00004045 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
4046 );
4047 if( !aNew ){
4048 return SQLITE_NOMEM;
4049 }
4050 memset(&aNew[pPager->nSavepoint], 0,
4051 (nSavepoint - pPager->nSavepoint) * sizeof(PagerSavepoint)
4052 );
4053 pPager->aSavepoint = aNew;
4054 ii = pPager->nSavepoint;
4055 pPager->nSavepoint = nSavepoint;
4056
4057 /* Populate the PagerSavepoint structures just allocated. */
4058 for(/* no-op */; ii<nSavepoint; ii++){
danielk197712dd5492008-12-18 15:45:07 +00004059 assert( pPager->dbSizeValid );
danielk1977fd7f0452008-12-17 17:30:26 +00004060 aNew[ii].nOrig = pPager->dbSize;
danielk197767ddef62008-12-23 19:15:56 +00004061 if( pPager->journalOpen && pPager->journalOff>0 ){
4062 aNew[ii].iOffset = pPager->journalOff;
4063 }else{
4064 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
4065 }
danielk1977fd7f0452008-12-17 17:30:26 +00004066 aNew[ii].iSubRec = pPager->stmtNRec;
4067 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
4068 if( !aNew[ii].pInSavepoint ){
4069 return SQLITE_NOMEM;
danielk1977b3175382008-10-17 18:51:52 +00004070 }
drh334b2992007-09-06 23:28:23 +00004071 }
danielk1977fd7f0452008-12-17 17:30:26 +00004072
4073 /* Open the sub-journal, if it is not already opened. */
4074 rc = openSubJournal(pPager);
drh0f892532002-05-30 12:27:03 +00004075 }
danielk1977fd7f0452008-12-17 17:30:26 +00004076
drh86f8c192007-08-22 00:39:19 +00004077 return rc;
4078}
drhfa86c412002-02-02 15:01:15 +00004079
4080/*
danielk1977fd7f0452008-12-17 17:30:26 +00004081** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
4082** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
4083** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
4084** that have occured since savepoint iSavepoint was created.
4085**
4086** In either case, all savepoints with an index greater than iSavepoint
4087** are destroyed.
4088**
4089** If there are less than (iSavepoint+1) active savepoints when this
4090** function is called it is a no-op.
4091*/
4092int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
4093 int rc = SQLITE_OK;
4094
4095 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
4096
4097 if( iSavepoint<pPager->nSavepoint ){
4098 int ii;
4099 int nNew = iSavepoint + (op==SAVEPOINT_ROLLBACK);
4100 for(ii=nNew; ii<pPager->nSavepoint; ii++){
4101 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
4102 }
4103 pPager->nSavepoint = nNew;
4104
danielk19778a7adb02008-12-22 11:43:35 +00004105 if( op==SAVEPOINT_ROLLBACK && pPager->jfd->pMethods ){
danielk1977f3107512008-12-22 10:58:46 +00004106 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
danielk1977fd7f0452008-12-17 17:30:26 +00004107 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
4108 assert(rc!=SQLITE_DONE);
4109 }
danielk1977f3107512008-12-22 10:58:46 +00004110
danielk1977fd7f0452008-12-17 17:30:26 +00004111 /* If this is a release of the outermost savepoint, truncate
4112 ** the sub-journal. */
4113 if( nNew==0 && op==SAVEPOINT_RELEASE && pPager->sjfd->pMethods ){
4114 assert( rc==SQLITE_OK );
4115 rc = sqlite3OsTruncate(pPager->sjfd, 0);
4116 pPager->stmtNRec = 0;
danielk1977b3175382008-10-17 18:51:52 +00004117 }
drh663fc632002-02-02 18:49:19 +00004118 }
drhfa86c412002-02-02 15:01:15 +00004119 return rc;
4120}
4121
drh73509ee2003-04-06 20:44:45 +00004122/*
4123** Return the full pathname of the database file.
4124*/
danielk19773b8a05f2007-03-19 17:44:26 +00004125const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004126 return pPager->zFilename;
4127}
4128
drhb20ea9d2004-02-09 01:20:36 +00004129/*
drhd0679ed2007-08-28 22:24:34 +00004130** Return the VFS structure for the pager.
4131*/
4132const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4133 return pPager->pVfs;
4134}
4135
4136/*
drhcc6bb3e2007-08-31 16:11:35 +00004137** Return the file handle for the database file associated
4138** with the pager. This might return NULL if the file has
4139** not yet been opened.
4140*/
4141sqlite3_file *sqlite3PagerFile(Pager *pPager){
4142 return pPager->fd;
4143}
4144
4145/*
danielk19775865e3d2004-06-14 06:03:57 +00004146** Return the directory of the database file.
4147*/
danielk19773b8a05f2007-03-19 17:44:26 +00004148const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004149 return pPager->zDirectory;
4150}
4151
4152/*
4153** Return the full pathname of the journal file.
4154*/
danielk19773b8a05f2007-03-19 17:44:26 +00004155const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004156 return pPager->zJournal;
4157}
4158
4159/*
drh2c8997b2005-08-27 16:36:48 +00004160** Return true if fsync() calls are disabled for this pager. Return FALSE
4161** if fsync()s are executed normally.
4162*/
danielk19773b8a05f2007-03-19 17:44:26 +00004163int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004164 return pPager->noSync;
4165}
4166
drh7c4ac0c2007-04-05 11:25:58 +00004167#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004168/*
drhb20ea9d2004-02-09 01:20:36 +00004169** Set the codec for this pager
4170*/
danielk19773b8a05f2007-03-19 17:44:26 +00004171void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004172 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004173 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004174 void *pCodecArg
4175){
4176 pPager->xCodec = xCodec;
4177 pPager->pCodecArg = pCodecArg;
4178}
drh7c4ac0c2007-04-05 11:25:58 +00004179#endif
drhb20ea9d2004-02-09 01:20:36 +00004180
danielk1977687566d2004-11-02 12:56:41 +00004181#ifndef SQLITE_OMIT_AUTOVACUUM
4182/*
danielk197787c29a92008-01-18 11:33:16 +00004183** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004184**
drh5e385312007-06-16 04:42:12 +00004185** There must be no references to the page previously located at
4186** pgno (which we call pPgOld) though that page is allowed to be
drhb3df2e12008-09-17 20:06:26 +00004187** in cache. If the page previously located at pgno is not already
drh5e385312007-06-16 04:42:12 +00004188** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004189**
drh5e385312007-06-16 04:42:12 +00004190** References to the page pPg remain valid. Updating any
4191** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004192** allocated along with the page) is the responsibility of the caller.
4193**
danielk19775fd057a2005-03-09 13:09:43 +00004194** A transaction must be active when this routine is called. It used to be
4195** required that a statement transaction was not active, but this restriction
4196** has been removed (CREATE INDEX needs to move a page when a statement
4197** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00004198**
4199** If the fourth argument, isCommit, is non-zero, then this page is being
4200** moved as part of a database reorganization just before the transaction
4201** is being committed. In this case, it is guaranteed that the database page
4202** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00004203*/
danielk19774c999992008-07-16 18:17:55 +00004204int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00004205 PgHdr *pPgOld; /* The page being overwritten. */
danielk197794daf7f2004-11-08 09:26:09 +00004206 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004207
danielk1977687566d2004-11-02 12:56:41 +00004208 assert( pPg->nRef>0 );
4209
drh30d53702009-01-06 15:58:57 +00004210 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
4211 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
drhb0603412007-02-28 04:47:26 +00004212 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004213
danielk1977b4626a32007-04-28 15:47:43 +00004214 pager_get_content(pPg);
danielk19774c999992008-07-16 18:17:55 +00004215
4216 /* If the journal needs to be sync()ed before page pPg->pgno can
4217 ** be written to, store pPg->pgno in local variable needSyncPgno.
4218 **
4219 ** If the isCommit flag is set, there is no need to remember that
4220 ** the journal needs to be sync()ed before database page pPg->pgno
4221 ** can be written to. The caller has already promised not to write to it.
4222 */
danielk19778c0a7912008-08-20 14:49:23 +00004223 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00004224 needSyncPgno = pPg->pgno;
danielk19773460d192008-12-27 15:23:13 +00004225 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
danielk19778c0a7912008-08-20 14:49:23 +00004226 assert( pPg->flags&PGHDR_DIRTY );
danielk1977ae825582004-11-23 09:06:55 +00004227 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004228 }
4229
danielk1977ef73ee92004-11-06 12:26:07 +00004230 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00004231 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00004232 ** page pgno before the 'move' operation, it needs to be retained
4233 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004234 */
danielk1977bc2ca9e2008-11-13 14:28:28 +00004235 pPg->flags &= ~PGHDR_NEED_SYNC;
danielk1977687566d2004-11-02 12:56:41 +00004236 pPgOld = pager_lookup(pPager, pgno);
danielk19778c0a7912008-08-20 14:49:23 +00004237 assert( !pPgOld || pPgOld->nRef==1 );
danielk1977687566d2004-11-02 12:56:41 +00004238 if( pPgOld ){
danielk19778c0a7912008-08-20 14:49:23 +00004239 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
drh5e385312007-06-16 04:42:12 +00004240 }
danielk1977687566d2004-11-02 12:56:41 +00004241
danielk19778c0a7912008-08-20 14:49:23 +00004242 sqlite3PcacheMove(pPg, pgno);
4243 if( pPgOld ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00004244 sqlite3PcacheDrop(pPgOld);
danielk1977f5fdda82004-11-03 08:44:05 +00004245 }
danielk1977f5fdda82004-11-03 08:44:05 +00004246
drhc047b9f2008-11-21 03:23:43 +00004247 sqlite3PcacheMakeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004248 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004249 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00004250
danielk197794daf7f2004-11-08 09:26:09 +00004251 if( needSyncPgno ){
4252 /* If needSyncPgno is non-zero, then the journal file needs to be
4253 ** sync()ed before any data is written to database file page needSyncPgno.
4254 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00004255 ** "is journaled" bitvec flag has been set. This needs to be remedied by
4256 ** loading the page into the pager-cache and setting the PgHdr.needSync
4257 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00004258 **
danielk1977a98d7b42008-01-18 13:42:54 +00004259 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00004260 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00004261 ** array. Otherwise, if the page is loaded and written again in
4262 ** this transaction, it may be written to the database file before
4263 ** it is synced into the journal file. This way, it may end up in
4264 ** the journal file twice, but that is not a problem.
4265 **
danielk19773b8a05f2007-03-19 17:44:26 +00004266 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004267 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004268 */
4269 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004270 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004271 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004272 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00004273 if( rc!=SQLITE_OK ){
danielk19773460d192008-12-27 15:23:13 +00004274 if( pPager->pInJournal && needSyncPgno<=pPager->dbOrigSize ){
drhf5e7bb52008-02-18 14:47:33 +00004275 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00004276 }
danielk197787c29a92008-01-18 11:33:16 +00004277 return rc;
4278 }
danielk1977ae825582004-11-23 09:06:55 +00004279 pPager->needSync = 1;
drhb3df2e12008-09-17 20:06:26 +00004280 assert( pPager->noSync==0 && !MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00004281 pPgHdr->flags |= PGHDR_NEED_SYNC;
drhc047b9f2008-11-21 03:23:43 +00004282 sqlite3PcacheMakeDirty(pPgHdr);
danielk19773b8a05f2007-03-19 17:44:26 +00004283 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004284 }
4285
danielk1977687566d2004-11-02 12:56:41 +00004286 return SQLITE_OK;
4287}
4288#endif
4289
danielk19773b8a05f2007-03-19 17:44:26 +00004290/*
4291** Return a pointer to the data for the specified page.
4292*/
4293void *sqlite3PagerGetData(DbPage *pPg){
danielk197771d5d2c2008-09-29 11:49:47 +00004294 assert( pPg->nRef>0 || pPg->pPager->memDb );
danielk19778c0a7912008-08-20 14:49:23 +00004295 return pPg->pData;
danielk19773b8a05f2007-03-19 17:44:26 +00004296}
4297
4298/*
4299** Return a pointer to the Pager.nExtra bytes of "extra" space
4300** allocated along with the specified page.
4301*/
4302void *sqlite3PagerGetExtra(DbPage *pPg){
4303 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00004304 return (pPager?pPg->pExtra:0);
danielk19773b8a05f2007-03-19 17:44:26 +00004305}
4306
danielk197741483462007-03-24 16:45:04 +00004307/*
4308** Get/set the locking-mode for this pager. Parameter eMode must be one
4309** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4310** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4311** the locking-mode is set to the value specified.
4312**
4313** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4314** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4315** locking-mode.
4316*/
4317int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004318 assert( eMode==PAGER_LOCKINGMODE_QUERY
4319 || eMode==PAGER_LOCKINGMODE_NORMAL
4320 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4321 assert( PAGER_LOCKINGMODE_QUERY<0 );
4322 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4323 if( eMode>=0 && !pPager->tempFile ){
drh1bd10f82008-12-10 21:19:56 +00004324 pPager->exclusiveMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +00004325 }
4326 return (int)pPager->exclusiveMode;
4327}
4328
drh3b020132008-04-17 17:02:01 +00004329/*
drh04335882008-09-26 21:08:08 +00004330** Get/set the journal-mode for this pager. Parameter eMode must be one of:
drh3b020132008-04-17 17:02:01 +00004331**
drh04335882008-09-26 21:08:08 +00004332** PAGER_JOURNALMODE_QUERY
4333** PAGER_JOURNALMODE_DELETE
4334** PAGER_JOURNALMODE_TRUNCATE
4335** PAGER_JOURNALMODE_PERSIST
4336** PAGER_JOURNALMODE_OFF
4337**
4338** If the parameter is not _QUERY, then the journal-mode is set to the
4339** value specified.
4340**
4341** The returned indicate the current (possibly updated)
drh3b020132008-04-17 17:02:01 +00004342** journal-mode.
4343*/
4344int sqlite3PagerJournalMode(Pager *pPager, int eMode){
danielk1977b3175382008-10-17 18:51:52 +00004345 if( !MEMDB ){
4346 assert( eMode==PAGER_JOURNALMODE_QUERY
4347 || eMode==PAGER_JOURNALMODE_DELETE
4348 || eMode==PAGER_JOURNALMODE_TRUNCATE
4349 || eMode==PAGER_JOURNALMODE_PERSIST
4350 || eMode==PAGER_JOURNALMODE_OFF
4351 || eMode==PAGER_JOURNALMODE_MEMORY );
4352 assert( PAGER_JOURNALMODE_QUERY<0 );
4353 if( eMode>=0 ){
drh1bd10f82008-12-10 21:19:56 +00004354 pPager->journalMode = (u8)eMode;
danielk1977b3175382008-10-17 18:51:52 +00004355 }else{
4356 assert( eMode==PAGER_JOURNALMODE_QUERY );
4357 }
drh3b020132008-04-17 17:02:01 +00004358 }
drhfdc40e92008-04-17 20:59:37 +00004359 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00004360}
4361
danielk1977b53e4962008-06-04 06:45:59 +00004362/*
4363** Get/set the size-limit used for persistent journal files.
4364*/
4365i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
4366 if( iLimit>=-1 ){
4367 pPager->journalSizeLimit = iLimit;
4368 }
4369 return pPager->journalSizeLimit;
4370}
4371
drh2e66f0b2005-04-28 17:18:48 +00004372#endif /* SQLITE_OMIT_DISKIO */