blob: 6683f1743d5dddd3997e3713e079ef99ea372cf9 [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**
danielk19778c0a7912008-08-20 14:49:23 +000021** @(#) $Id: pager.c,v 1.470 2008/08/20 14:49:24 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025
26/*
drhdb48ee02003-01-16 13:42:43 +000027** Macros for troubleshooting. Normally turned off
28*/
danielk1977466be562004-06-10 02:16:01 +000029#if 0
drhd3627af2006-12-18 18:34:51 +000030#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000031#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
32#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
33#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
34#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
35#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000036#else
drh4f0c5872007-03-26 22:05:01 +000037#define PAGERTRACE1(X)
38#define PAGERTRACE2(X,Y)
39#define PAGERTRACE3(X,Y,Z)
40#define PAGERTRACE4(X,Y,Z,W)
41#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000042#endif
43
danielk1977599fcba2004-11-08 07:13:13 +000044/*
drh4f0c5872007-03-26 22:05:01 +000045** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000046** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000047**
drh85b623f2007-12-13 21:54:09 +000048** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000049** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000050** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000051*/
drhc001c582006-03-06 18:23:16 +000052#define PAGERID(p) ((int)(p->fd))
53#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000054
55/*
drhed7c8552001-04-11 14:29:21 +000056** The page cache as a whole is always in one of the following
57** states:
58**
drha6abd042004-06-09 17:37:22 +000059** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000060** writing the database file. There is no
61** data held in memory. This is the initial
62** state.
63**
drha6abd042004-06-09 17:37:22 +000064** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000065** Writing is not permitted. There can be
66** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000067** file at the same time.
drhed7c8552001-04-11 14:29:21 +000068**
drh726de592004-06-10 23:35:50 +000069** PAGER_RESERVED This process has reserved the database for writing
70** but has not yet made any changes. Only one process
71** at a time can reserve the database. The original
72** database file has not been modified so other
73** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000074** database file.
75**
76** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000077** Access is exclusive. No other processes or
78** threads can be reading or writing while one
79** process is writing.
80**
danielk1977aa5ccdf2004-06-14 05:10:42 +000081** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
82** after all dirty pages have been written to the
83** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000084** disk. All that remains to do is to remove or
85** truncate the journal file and the transaction
86** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000087**
drha6abd042004-06-09 17:37:22 +000088** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000089** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000090** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000091** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000092** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000093** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000094** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000095** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000096** PAGER_RESERVED means that there is an open rollback journal.
97** The transition to PAGER_EXCLUSIVE occurs before any changes
98** are made to the database file, though writes to the rollback
99** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
100** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
101** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000102*/
drha6abd042004-06-09 17:37:22 +0000103#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000104#define PAGER_SHARED 1 /* same as SHARED_LOCK */
105#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
106#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
107#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000108
drh684917c2004-10-05 02:41:42 +0000109/*
110** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
111** then failed attempts to get a reserved lock will invoke the busy callback.
112** This is off by default. To see why, consider the following scenario:
113**
114** Suppose thread A already has a shared lock and wants a reserved lock.
115** Thread B already has a reserved lock and wants an exclusive lock. If
116** both threads are using their busy callbacks, it might be a long time
117** be for one of the threads give up and allows the other to proceed.
118** But if the thread trying to get the reserved lock gives up quickly
119** (if it never invokes its busy callback) then the contention will be
120** resolved quickly.
121*/
122#ifndef SQLITE_BUSY_RESERVED_LOCK
123# define SQLITE_BUSY_RESERVED_LOCK 0
124#endif
drhd9b02572001-04-15 00:37:09 +0000125
drhed7c8552001-04-11 14:29:21 +0000126/*
drh887dc4c2004-10-22 16:22:57 +0000127** This macro rounds values up so that if the value is an address it
128** is guaranteed to be an address that is aligned to an 8-byte boundary.
129*/
130#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
131
drh9eb9e262004-02-11 02:18:05 +0000132/*
133** A macro used for invoking the codec if there is one
134*/
135#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000136# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
137# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000138#else
drhc001c582006-03-06 18:23:16 +0000139# define CODEC1(P,D,N,X) /* NO-OP */
140# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000141#endif
142
drhed7c8552001-04-11 14:29:21 +0000143/*
drhed7c8552001-04-11 14:29:21 +0000144** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000145**
drh4f0ee682007-03-30 20:43:40 +0000146** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000147** or SQLITE_FULL. Once one of the first three errors occurs, it persists
148** and is returned as the result of every major pager API call. The
149** SQLITE_FULL return code is slightly different. It persists only until the
150** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000151** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000152** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000153*/
154struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000155 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000156 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000157 u8 journalStarted; /* True if header of journal is synced */
158 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000159 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000160 u8 stmtOpen; /* True if the statement subjournal is open */
161 u8 stmtInUse; /* True we are in a statement subtransaction */
162 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000163 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000164 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000165 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000166 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000167 u8 tempFile; /* zFilename is a temporary file */
168 u8 readOnly; /* True for a read-only database */
169 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000170 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000171 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000172 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000173 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000174 u8 doNotSync; /* Boolean. While true, do not spill the cache */
175 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000176 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drhd138c012008-05-13 00:58:18 +0000177 u8 dbModified; /* True if there are any changes to the Db */
drh80e35f42007-03-30 14:06:34 +0000178 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000179 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000180 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000181 int dbSize; /* Number of pages in the file */
182 int origDbSize; /* dbSize before the current change */
183 int stmtSize; /* Size of database (in pages) at stmt_begin() */
184 int nRec; /* Number of pages written to the journal */
185 u32 cksumInit; /* Quasi-random value added to every checksum */
186 int stmtNRec; /* Number of records in stmt subjournal */
187 int nExtra; /* Add this many bytes to each in-memory page */
188 int pageSize; /* Number of bytes in a page */
189 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000190 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000191 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000192 Bitvec *pInJournal; /* One bit for each page in the database file */
193 Bitvec *pInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000194 char *zFilename; /* Name of the database file */
195 char *zJournal; /* Name of the journal file */
196 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000197 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
198 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000199 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
drheb206252004-10-01 02:00:31 +0000200 i64 journalOff; /* Current byte offset in the journal file */
201 i64 journalHdr; /* Byte offset to previous journal header */
202 i64 stmtHdrOff; /* First journal header written this statement */
203 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000204 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000205 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000206#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000207 int nHit, nMiss; /* Cache hits and missing */
208 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000209#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000210 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
211 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000212#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000213 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000214 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000215#endif
danielk19778186df82007-03-06 13:45:59 +0000216 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000217 char dbFileVers[16]; /* Changes whenever database file changes */
danielk1977b53e4962008-06-04 06:45:59 +0000218 i64 journalSizeLimit; /* Size limit for persistent journal files */
danielk19778c0a7912008-08-20 14:49:23 +0000219 PCache *pPCache; /* Pointer to page cache object */
drhd9b02572001-04-15 00:37:09 +0000220};
221
222/*
drh538f5702007-04-13 02:14:30 +0000223** The following global variables hold counters used for
224** testing purposes only. These variables do not exist in
225** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000226*/
227#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000228int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
229int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
230int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
drh538f5702007-04-13 02:14:30 +0000231# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000232#else
drh538f5702007-04-13 02:14:30 +0000233# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000234#endif
235
drh538f5702007-04-13 02:14:30 +0000236
237
drhfcd35c72005-05-21 02:48:08 +0000238/*
drh5e00f6c2001-09-13 13:46:56 +0000239** Journal files begin with the following magic string. The data
240** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000241**
drhae2b40c2004-06-09 19:03:54 +0000242** Since version 2.8.0, the journal format contains additional sanity
243** checking information. If the power fails while the journal is begin
244** written, semi-random garbage data might appear in the journal
245** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000246** to roll the journal back, the database could be corrupted. The additional
247** sanity checking data is an attempt to discover the garbage in the
248** journal and ignore it.
249**
drhae2b40c2004-06-09 19:03:54 +0000250** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000251** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000252** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000253** This cksum is initialized to a 32-bit random value that appears in the
254** journal file right after the header. The random initializer is important,
255** because garbage data that appears at the end of a journal is likely
256** data that was once in other files that have now been deleted. If the
257** garbage data came from an obsolete journal file, the checksums might
258** be correct. But by initializing the checksum to random value which
259** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000260*/
drhae2b40c2004-06-09 19:03:54 +0000261static const unsigned char aJournalMagic[] = {
262 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000263};
264
265/*
drh726de592004-06-10 23:35:50 +0000266** The size of the header and of each page in the journal is determined
267** by the following macros.
drh968af522003-02-11 14:55:40 +0000268*/
drhae2b40c2004-06-09 19:03:54 +0000269#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000270
danielk197776572402004-06-25 02:38:54 +0000271/*
272** The journal header size for this pager. In the future, this could be
273** set to some value read from the disk controller. The important
274** characteristic is that it is the same size as a disk sector.
275*/
276#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
277
drhb7f91642004-10-31 02:22:47 +0000278/*
279** The macro MEMDB is true if we are dealing with an in-memory database.
280** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
281** the value of MEMDB will be a constant and the compiler will optimize
282** out code that would never execute.
283*/
284#ifdef SQLITE_OMIT_MEMORYDB
285# define MEMDB 0
286#else
287# define MEMDB pPager->memDb
288#endif
289
290/*
danielk197776572402004-06-25 02:38:54 +0000291** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
292** reserved for working around a windows/posix incompatibility). It is
293** used in the journal to signify that the remainder of the journal file
294** is devoted to storing a master journal name - there are no more pages to
295** roll back. See comments for function writeMasterJournal() for details.
296*/
danielk1977599fcba2004-11-08 07:13:13 +0000297/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
298#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000299
drh968af522003-02-11 14:55:40 +0000300/*
danielk197726836652005-01-17 01:33:13 +0000301** The maximum legal page number is (2^31 - 1).
302*/
303#define PAGER_MAX_PGNO 2147483647
304
305/*
danielk19778c0a7912008-08-20 14:49:23 +0000306** The following two macros act as a type of recursive mutex. Their
307** only purpose is to provide mutual exclusion between the "normal"
308** users of a pager object (the btree.c module) and the user of the
309** pagerStress() function (the pcache.c module). While the mutex
310** obtained using pagerEnter() is held, the pcache module guarantees
311** that the pagerStress() callback will not be invoked from a thread
312** other than the holder of the mutex.
drh86f8c192007-08-22 00:39:19 +0000313*/
danielk19778c0a7912008-08-20 14:49:23 +0000314#define pagerEnter(p) (sqlite3PcacheLock(p->pPCache))
315#define pagerLeave(p) (sqlite3PcacheUnlock(p->pPCache))
danielk19779f61c2f2007-08-27 17:27:49 +0000316
danielk1977f35843b2007-04-07 15:03:17 +0000317/*
318** Return true if page *pPg has already been written to the statement
319** journal (or statement snapshot has been created, if *pPg is part
320** of an in-memory database).
321*/
322static int pageInStatement(PgHdr *pPg){
323 Pager *pPager = pPg->pPager;
324 if( MEMDB ){
danielk19778c0a7912008-08-20 14:49:23 +0000325 return pPg->apSave[1]!=0;
danielk1977f35843b2007-04-07 15:03:17 +0000326 }else{
drhf5e7bb52008-02-18 14:47:33 +0000327 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000328 }
329}
drh8ca0c722006-05-07 17:49:38 +0000330
331/*
drh34e79ce2004-02-08 06:05:46 +0000332** Read a 32-bit integer from the given file descriptor. Store the integer
333** that is read in *pRes. Return SQLITE_OK if everything worked, or an
334** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000335**
336** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000337*/
danielk197762079062007-08-15 17:08:46 +0000338static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000339 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000340 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000341 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000342 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000343 }
drh94f33312002-08-12 12:29:56 +0000344 return rc;
345}
346
347/*
drh97b57482006-01-10 20:32:32 +0000348** Write a 32-bit integer into a string buffer in big-endian byte order.
349*/
drha3152892007-05-05 11:48:52 +0000350#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000351
352/*
drh34e79ce2004-02-08 06:05:46 +0000353** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
354** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000355*/
danielk197762079062007-08-15 17:08:46 +0000356static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000357 char ac[4];
drh97b57482006-01-10 20:32:32 +0000358 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000359 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000360}
361
drh2554f8b2003-01-22 01:26:44 +0000362/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000363** If file pFd is open, call sqlite3OsUnlock() on it.
364*/
365static int osUnlock(sqlite3_file *pFd, int eLock){
366 if( !pFd->pMethods ){
367 return SQLITE_OK;
368 }
369 return sqlite3OsUnlock(pFd, eLock);
370}
371
372/*
danielk1977c7b60172007-08-22 11:22:03 +0000373** This function determines whether or not the atomic-write optimization
374** can be used with this pager. The optimization can be used if:
375**
376** (a) the value returned by OsDeviceCharacteristics() indicates that
377** a database page may be written atomically, and
378** (b) the value returned by OsSectorSize() is less than or equal
379** to the page size.
380**
381** If the optimization cannot be used, 0 is returned. If it can be used,
382** then the value returned is the size of the journal file when it
383** contains rollback data for exactly one page.
384*/
385#ifdef SQLITE_ENABLE_ATOMIC_WRITE
386static int jrnlBufferSize(Pager *pPager){
387 int dc; /* Device characteristics */
388 int nSector; /* Sector size */
drhfacf0302008-06-17 15:12:00 +0000389 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000390 sqlite3_file *fd = pPager->fd;
391
392 if( fd->pMethods ){
393 dc = sqlite3OsDeviceCharacteristics(fd);
394 nSector = sqlite3OsSectorSize(fd);
drhfacf0302008-06-17 15:12:00 +0000395 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000396 }
397
398 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
399 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
400
drhfacf0302008-06-17 15:12:00 +0000401 if( !fd->pMethods ||
402 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000403 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
404 }
405 return 0;
406}
407#endif
408
409/*
danielk1977aef0bf62005-12-30 16:28:01 +0000410** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000411** code. The first argument is a pointer to the pager structure, the
412** second the error-code about to be returned by a pager API function.
413** The value returned is a copy of the second argument to this function.
414**
drh4f0ee682007-03-30 20:43:40 +0000415** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000416** the error becomes persistent. Until the persisten error is cleared,
417** subsequent API calls on this Pager will immediately return the same
418** error code.
419**
420** A persistent error indicates that the contents of the pager-cache
421** cannot be trusted. This state can be cleared by completely discarding
422** the contents of the pager-cache. If a transaction was active when
423** the persistent error occured, then the rollback journal may need
424** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000425*/
danielk1977ae72d982007-10-03 08:46:44 +0000426static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000427static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000428 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000429 assert(
430 pPager->errCode==SQLITE_FULL ||
431 pPager->errCode==SQLITE_OK ||
432 (pPager->errCode & 0xff)==SQLITE_IOERR
433 );
danielk1977979f38e2007-03-27 16:19:51 +0000434 if(
drh4ac285a2006-09-15 07:28:50 +0000435 rc2==SQLITE_FULL ||
436 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000437 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000438 ){
439 pPager->errCode = rc;
danielk19778c0a7912008-08-20 14:49:23 +0000440 if( pPager->state==PAGER_UNLOCK
441 && sqlite3PcacheRefCount(pPager->pPCache)==0
442 ){
danielk1977ae72d982007-10-03 08:46:44 +0000443 /* If the pager is already unlocked, call pager_unlock() now to
444 ** clear the error state and ensure that the pager-cache is
445 ** completely empty.
446 */
447 pager_unlock(pPager);
448 }
danielk1977aef0bf62005-12-30 16:28:01 +0000449 }
450 return rc;
451}
452
drh477731b2007-06-16 03:06:27 +0000453/*
454** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
455** on the cache using a hash function. This is used for testing
456** and debugging only.
457*/
danielk19773c407372005-02-15 02:54:14 +0000458#ifdef SQLITE_CHECK_PAGES
459/*
460** Return a 32-bit hash of the page data for pPage.
461*/
drh477731b2007-06-16 03:06:27 +0000462static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000463 u32 hash = 0;
464 int i;
drh477731b2007-06-16 03:06:27 +0000465 for(i=0; i<nByte; i++){
466 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000467 }
468 return hash;
469}
drh477731b2007-06-16 03:06:27 +0000470static u32 pager_pagehash(PgHdr *pPage){
danielk19778c0a7912008-08-20 14:49:23 +0000471 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
472}
473static u32 pager_set_pagehash(PgHdr *pPage){
474 pPage->pageHash = pager_pagehash(pPage);
drh477731b2007-06-16 03:06:27 +0000475}
danielk19773c407372005-02-15 02:54:14 +0000476
477/*
478** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
479** is defined, and NDEBUG is not defined, an assert() statement checks
480** that the page is either dirty or still matches the calculated page-hash.
481*/
482#define CHECK_PAGE(x) checkPage(x)
483static void checkPage(PgHdr *pPg){
484 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +0000485 assert( !pPg->pageHash || pPager->errCode || MEMDB
486 || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
danielk19773c407372005-02-15 02:54:14 +0000487}
488
489#else
drh8ffa8172007-06-18 17:25:17 +0000490#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000491#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000492#define CHECK_PAGE(x)
493#endif
494
drhed7c8552001-04-11 14:29:21 +0000495/*
danielk197776572402004-06-25 02:38:54 +0000496** When this is called the journal file for pager pPager must be open.
497** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000498** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000499**
danielk197765839c62007-08-30 08:08:17 +0000500** zMaster must point to a buffer of at least nMaster bytes allocated by
501** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
502** enough space to write the master journal name). If the master journal
503** name in the journal is longer than nMaster bytes (including a
504** nul-terminator), then this is handled as if no master journal name
505** were present in the journal.
506**
507** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000508** SQLITE_OK returned.
509*/
danielk197765839c62007-08-30 08:08:17 +0000510static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000511 int rc;
512 u32 len;
drheb206252004-10-01 02:00:31 +0000513 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000514 u32 cksum;
shane0b8d2762008-07-22 05:18:00 +0000515 u32 u; /* Unsigned loop counter */
danielk197776572402004-06-25 02:38:54 +0000516 unsigned char aMagic[8]; /* A buffer to hold the magic header */
517
danielk197765839c62007-08-30 08:08:17 +0000518 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000519
drh054889e2005-11-30 03:20:31 +0000520 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000521 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000522
danielk197762079062007-08-15 17:08:46 +0000523 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000524 if( rc!=SQLITE_OK ) return rc;
525
danielk197765839c62007-08-30 08:08:17 +0000526 if( len>=nMaster ){
527 return SQLITE_OK;
528 }
529
danielk197762079062007-08-15 17:08:46 +0000530 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000531 if( rc!=SQLITE_OK ) return rc;
532
danielk197762079062007-08-15 17:08:46 +0000533 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000534 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
535
danielk197765839c62007-08-30 08:08:17 +0000536 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000537 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000538 return rc;
539 }
danielk197765839c62007-08-30 08:08:17 +0000540 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000541
542 /* See if the checksum matches the master journal name */
shane0b8d2762008-07-22 05:18:00 +0000543 for(u=0; u<len; u++){
544 cksum -= zMaster[u];
danielk197765839c62007-08-30 08:08:17 +0000545 }
danielk19778191bff2004-06-28 04:52:30 +0000546 if( cksum ){
547 /* If the checksum doesn't add up, then one or more of the disk sectors
548 ** containing the master journal filename is corrupted. This means
549 ** definitely roll back, so just return SQLITE_OK and report a (nul)
550 ** master-journal filename.
551 */
danielk197765839c62007-08-30 08:08:17 +0000552 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000553 }
danielk197776572402004-06-25 02:38:54 +0000554
555 return SQLITE_OK;
556}
557
558/*
559** Seek the journal file descriptor to the next sector boundary where a
560** journal header may be read or written. Pager.journalOff is updated with
561** the new seek offset.
562**
563** i.e for a sector size of 512:
564**
565** Input Offset Output Offset
566** ---------------------------------------
567** 0 0
568** 512 512
569** 100 512
570** 2000 2048
571**
572*/
danielk197762079062007-08-15 17:08:46 +0000573static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000574 i64 offset = 0;
575 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000576 if( c ){
577 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
578 }
579 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
580 assert( offset>=c );
581 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
582 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000583}
584
585/*
drhf3a87622008-04-17 14:16:42 +0000586** Write zeros over the header of the journal file. This has the
587** effect of invalidating the journal file and committing the
588** transaction.
589*/
danielk1977df2566a2008-05-07 19:11:03 +0000590static int zeroJournalHdr(Pager *pPager, int doTruncate){
591 int rc = SQLITE_OK;
drhf3a87622008-04-17 14:16:42 +0000592 static const char zeroHdr[28];
593
danielk1977df2566a2008-05-07 19:11:03 +0000594 if( pPager->journalOff ){
danielk1977b53e4962008-06-04 06:45:59 +0000595 i64 iLimit = pPager->journalSizeLimit;
596
danielk1977df2566a2008-05-07 19:11:03 +0000597 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000598 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000599 rc = sqlite3OsTruncate(pPager->jfd, 0);
600 }else{
601 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
602 }
danielk197781620542008-06-07 05:19:37 +0000603 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000604 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
605 }
danielk1977b53e4962008-06-04 06:45:59 +0000606
607 /* At this point the transaction is committed but the write lock
608 ** is still held on the file. If there is a size limit configured for
609 ** the persistent journal and the journal file currently consumes more
610 ** space than that limit allows for, truncate it now. There is no need
611 ** to sync the file following this operation.
612 */
613 if( rc==SQLITE_OK && iLimit>0 ){
614 i64 sz;
615 rc = sqlite3OsFileSize(pPager->jfd, &sz);
616 if( rc==SQLITE_OK && sz>iLimit ){
617 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
618 }
619 }
drha06ecba2008-04-22 17:15:17 +0000620 }
drhf3a87622008-04-17 14:16:42 +0000621 return rc;
622}
623
624/*
danielk197776572402004-06-25 02:38:54 +0000625** The journal file must be open when this routine is called. A journal
626** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
627** current location.
628**
629** The format for the journal header is as follows:
630** - 8 bytes: Magic identifying journal format.
631** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
632** - 4 bytes: Random number used for page hash.
633** - 4 bytes: Initial database page count.
634** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000635** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000636**
danielk197767c007b2008-03-20 04:45:49 +0000637** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000638*/
639static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +0000640 int rc = SQLITE_OK;
641 char *zHeader = pPager->pTmpSpace;
642 int nHeader = pPager->pageSize;
643 int nWrite;
644
645 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
646 nHeader = JOURNAL_HDR_SZ(pPager);
647 }
danielk197776572402004-06-25 02:38:54 +0000648
danielk19774099f6e2007-03-19 11:25:20 +0000649 if( pPager->stmtHdrOff==0 ){
650 pPager->stmtHdrOff = pPager->journalOff;
651 }
652
danielk197762079062007-08-15 17:08:46 +0000653 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000654 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000655
drh97b57482006-01-10 20:32:32 +0000656 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000657
658 /*
659 ** Write the nRec Field - the number of page records that follow this
660 ** journal header. Normally, zero is written to this value at this time.
661 ** After the records are added to the journal (and the journal synced,
662 ** if in full-sync mode), the zero is overwritten with the true number
663 ** of records (see syncJournal()).
664 **
665 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
666 ** reading the journal this value tells SQLite to assume that the
667 ** rest of the journal file contains valid page records. This assumption
668 ** is dangerous, as if a failure occured whilst writing to the journal
669 ** file it may contain some garbage data. There are two scenarios
670 ** where this risk can be ignored:
671 **
672 ** * When the pager is in no-sync mode. Corruption can follow a
673 ** power failure in this case anyway.
674 **
675 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
676 ** that garbage data is never appended to the journal file.
677 */
678 assert(pPager->fd->pMethods||pPager->noSync);
679 if( (pPager->noSync)
680 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
681 ){
682 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
683 }else{
684 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
685 }
686
drh97b57482006-01-10 20:32:32 +0000687 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +0000688 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +0000689 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
690 /* The initial database size */
691 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
692 /* The assumed sector size for this process */
693 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
danielk197767c007b2008-03-20 04:45:49 +0000694 if( pPager->journalHdr==0 ){
695 /* The page size */
696 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
697 }
danielk197776572402004-06-25 02:38:54 +0000698
danielk1977a664f8e2008-04-22 14:31:48 +0000699 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
700 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
701 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
702 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +0000703 }
danielk1977a664f8e2008-04-22 14:31:48 +0000704
danielk197776572402004-06-25 02:38:54 +0000705 return rc;
706}
707
708/*
709** The journal file must be open when this is called. A journal header file
710** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
711** file. See comments above function writeJournalHdr() for a description of
712** the journal header format.
713**
714** If the header is read successfully, *nRec is set to the number of
715** page records following this header and *dbSize is set to the size of the
716** database before the transaction began, in pages. Also, pPager->cksumInit
717** is set to the value read from the journal header. SQLITE_OK is returned
718** in this case.
719**
720** If the journal header file appears to be corrupted, SQLITE_DONE is
721** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
722** cannot be read from the journal file an error code is returned.
723*/
724static int readJournalHdr(
725 Pager *pPager,
drheb206252004-10-01 02:00:31 +0000726 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +0000727 u32 *pNRec,
728 u32 *pDbSize
729){
730 int rc;
731 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +0000732 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +0000733 int iPageSize;
danielk197776572402004-06-25 02:38:54 +0000734
danielk197762079062007-08-15 17:08:46 +0000735 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000736 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
737 return SQLITE_DONE;
738 }
danielk197762079062007-08-15 17:08:46 +0000739 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000740
danielk197762079062007-08-15 17:08:46 +0000741 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000742 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +0000743 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +0000744
745 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
746 return SQLITE_DONE;
747 }
748
danielk197762079062007-08-15 17:08:46 +0000749 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +0000750 if( rc ) return rc;
751
danielk197762079062007-08-15 17:08:46 +0000752 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +0000753 if( rc ) return rc;
754
danielk197762079062007-08-15 17:08:46 +0000755 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +0000756 if( rc ) return rc;
757
danielk197767c007b2008-03-20 04:45:49 +0000758 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
759 if( rc==SQLITE_OK
760 && iPageSize>=512
761 && iPageSize<=SQLITE_MAX_PAGE_SIZE
762 && ((iPageSize-1)&iPageSize)==0
763 ){
764 u16 pagesize = iPageSize;
765 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
766 }
767 if( rc ) return rc;
768
danielk197776572402004-06-25 02:38:54 +0000769 /* Update the assumed sector-size to match the value used by
770 ** the process that created this journal. If this journal was
771 ** created by a process other than this one, then this routine
772 ** is being called from within pager_playback(). The local value
773 ** of Pager.sectorSize is restored at the end of that routine.
774 */
danielk197762079062007-08-15 17:08:46 +0000775 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +0000776 if( rc ) return rc;
777
778 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +0000779 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +0000780}
781
782
783/*
784** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +0000785** pPager at the current location. The master journal name must be the last
786** thing written to a journal file. If the pager is in full-sync mode, the
787** journal file descriptor is advanced to the next sector boundary before
788** anything is written. The format is:
789**
790** + 4 bytes: PAGER_MJ_PGNO.
791** + N bytes: length of master journal name.
792** + 4 bytes: N
793** + 4 bytes: Master journal name checksum.
794** + 8 bytes: aJournalMagic[].
795**
796** The master journal page checksum is the sum of the bytes in the master
797** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +0000798**
799** If zMaster is a NULL pointer (occurs for a single database transaction),
800** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +0000801*/
802static int writeMasterJournal(Pager *pPager, const char *zMaster){
803 int rc;
804 int len;
danielk1977cafadba2004-06-25 11:11:54 +0000805 int i;
danielk197762079062007-08-15 17:08:46 +0000806 i64 jrnlOff;
danielk1977df2566a2008-05-07 19:11:03 +0000807 i64 jrnlSize;
drh97b57482006-01-10 20:32:32 +0000808 u32 cksum = 0;
809 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +0000810
811 if( !zMaster || pPager->setMaster) return SQLITE_OK;
812 pPager->setMaster = 1;
813
814 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000815 for(i=0; i<len; i++){
816 cksum += zMaster[i];
817 }
danielk197776572402004-06-25 02:38:54 +0000818
819 /* If in full-sync mode, advance to the next disk sector before writing
820 ** the master journal name. This is in case the previous page written to
821 ** the journal has already been synced.
822 */
823 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +0000824 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000825 }
danielk197762079062007-08-15 17:08:46 +0000826 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +0000827 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +0000828
danielk197762079062007-08-15 17:08:46 +0000829 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +0000830 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000831 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +0000832
danielk197762079062007-08-15 17:08:46 +0000833 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +0000834 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +0000835 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +0000836
drh97b57482006-01-10 20:32:32 +0000837 put32bits(zBuf, len);
838 put32bits(&zBuf[4], cksum);
839 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +0000840 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +0000841 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +0000842 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +0000843
844 /* If the pager is in peristent-journal mode, then the physical
845 ** journal-file may extend past the end of the master-journal name
846 ** and 8 bytes of magic data just written to the file. This is
847 ** dangerous because the code to rollback a hot-journal file
848 ** will not be able to find the master-journal name to determine
849 ** whether or not the journal is hot.
850 **
851 ** Easiest thing to do in this scenario is to truncate the journal
852 ** file to the required size.
853 */
854 if( (rc==SQLITE_OK)
855 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
856 && jrnlSize>jrnlOff
857 ){
858 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
859 }
danielk197776572402004-06-25 02:38:54 +0000860 return rc;
861}
862
863/*
drhed7c8552001-04-11 14:29:21 +0000864** Find a page in the hash table given its page number. Return
865** a pointer to the page or NULL if not found.
866*/
drhd9b02572001-04-15 00:37:09 +0000867static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +0000868 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000869 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
drhed7c8552001-04-11 14:29:21 +0000870 return p;
871}
872
873/*
danielk1977e180dd92007-04-05 17:15:52 +0000874** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +0000875** sets the state of the pager back to what it was when it was first
876** opened. Any outstanding pages are invalidated and subsequent attempts
877** to access those pages will likely result in a coredump.
878*/
drhd9b02572001-04-15 00:37:09 +0000879static void pager_reset(Pager *pPager){
danielk1977efaaf572006-01-16 11:29:19 +0000880 if( pPager->errCode ) return;
danielk19778c0a7912008-08-20 14:49:23 +0000881 sqlite3PcacheClear(pPager->pPCache);
danielk1977e277be02007-03-23 18:12:06 +0000882}
883
884/*
danielk1977ae72d982007-10-03 08:46:44 +0000885** Unlock the database file.
886**
887** If the pager is currently in error state, discard the contents of
888** the cache and reset the Pager structure internal state. If there is
889** an open journal-file, then the next time a shared-lock is obtained
890** on the pager file (by this or any other process), it will be
891** treated as a hot-journal and rolled back.
892*/
893static void pager_unlock(Pager *pPager){
894 if( !pPager->exclusiveMode ){
895 if( !MEMDB ){
drh1aa5af12008-03-07 19:51:14 +0000896 int rc = osUnlock(pPager->fd, NO_LOCK);
drh308aa322008-03-07 20:14:38 +0000897 if( rc ) pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000898 pPager->dbSize = -1;
899 IOTRACE(("UNLOCK %p\n", pPager))
900
drh16e45a42008-04-19 20:34:18 +0000901 /* Always close the journal file when dropping the database lock.
902 ** Otherwise, another connection with journal_mode=delete might
903 ** delete the file out from under us.
904 */
905 if( pPager->journalOpen ){
906 sqlite3OsClose(pPager->jfd);
907 pPager->journalOpen = 0;
908 sqlite3BitvecDestroy(pPager->pInJournal);
909 pPager->pInJournal = 0;
910 }
911
danielk1977ae72d982007-10-03 08:46:44 +0000912 /* If Pager.errCode is set, the contents of the pager cache cannot be
913 ** trusted. Now that the pager file is unlocked, the contents of the
914 ** cache can be discarded and the error code safely cleared.
915 */
916 if( pPager->errCode ){
drh1aa5af12008-03-07 19:51:14 +0000917 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +0000918 pager_reset(pPager);
919 if( pPager->stmtOpen ){
920 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +0000921 sqlite3BitvecDestroy(pPager->pInStmt);
922 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +0000923 }
danielk1977ae72d982007-10-03 08:46:44 +0000924 pPager->stmtOpen = 0;
925 pPager->stmtInUse = 0;
926 pPager->journalOff = 0;
927 pPager->journalStarted = 0;
928 pPager->stmtAutoopen = 0;
929 pPager->origDbSize = 0;
930 }
931 }
932
933 if( !MEMDB || pPager->errCode==SQLITE_OK ){
934 pPager->state = PAGER_UNLOCK;
935 pPager->changeCountDone = 0;
936 }
937 }
938}
939
940/*
941** Execute a rollback if a transaction is active and unlock the
942** database file. If the pager has already entered the error state,
943** do not attempt the rollback.
944*/
945static void pagerUnlockAndRollback(Pager *p){
danielk1977ae72d982007-10-03 08:46:44 +0000946 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +0000947 sqlite3BeginBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +0000948 sqlite3PagerRollback(p);
danielk19772d1d86f2008-06-20 14:59:51 +0000949 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +0000950 }
951 pager_unlock(p);
danielk1977ae72d982007-10-03 08:46:44 +0000952}
953
954/*
drh80e35f42007-03-30 14:06:34 +0000955** This routine ends a transaction. A transaction is ended by either
956** a COMMIT or a ROLLBACK.
957**
drhed7c8552001-04-11 14:29:21 +0000958** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +0000959** a RESERVED or EXCLUSIVE lock on the database. This routine will release
960** the database lock and acquires a SHARED lock in its place if that is
961** the appropriate thing to do. Release locks usually is appropriate,
962** unless we are in exclusive access mode or unless this is a
963** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
964**
965** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +0000966**
967** TODO: Consider keeping the journal file open for temporary databases.
968** This might give a performance improvement on windows where opening
969** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +0000970*/
danielk1977df2566a2008-05-07 19:11:03 +0000971static int pager_end_transaction(Pager *pPager, int hasMaster){
danielk197741483462007-03-24 16:45:04 +0000972 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +0000973 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +0000974 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +0000975 if( pPager->state<PAGER_RESERVED ){
976 return SQLITE_OK;
977 }
danielk19773b8a05f2007-03-19 17:44:26 +0000978 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +0000979 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +0000980 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +0000981 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +0000982 }
drhda47d772002-12-02 04:25:19 +0000983 if( pPager->journalOpen ){
danielk197793f7af92008-05-09 16:57:50 +0000984 if( pPager->exclusiveMode
985 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
986 ){
987 rc = zeroJournalHdr(pPager, hasMaster);
988 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +0000989 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +0000990 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +0000991 }else{
danielk1977b4b47412007-08-17 15:53:36 +0000992 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +0000993 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +0000994 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +0000995 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +0000996 }
danielk197741483462007-03-24 16:45:04 +0000997 }
drhf5e7bb52008-02-18 14:47:33 +0000998 sqlite3BitvecDestroy(pPager->pInJournal);
999 pPager->pInJournal = 0;
danielk19778c0a7912008-08-20 14:49:23 +00001000 sqlite3PcacheCleanAll(pPager->pPCache);
danielk19773c407372005-02-15 02:54:14 +00001001#ifdef SQLITE_CHECK_PAGES
danielk19778c0a7912008-08-20 14:49:23 +00001002 sqlite3PcacheIterate(pPager->pPCache, pager_set_pagehash);
danielk19773c407372005-02-15 02:54:14 +00001003#endif
danielk19778c0a7912008-08-20 14:49:23 +00001004 sqlite3PcacheSetFlags(pPager->pPCache,
1005 ~(PGHDR_IN_JOURNAL | PGHDR_NEED_SYNC | PGHDR_ALWAYS_ROLLBACK), 0
1006 );
danielk1977ef317ab2004-06-23 10:43:10 +00001007 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001008 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001009 }else{
drhf5e7bb52008-02-18 14:47:33 +00001010 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001011 }
danielk1977979f38e2007-03-27 16:19:51 +00001012
danielk197741483462007-03-24 16:45:04 +00001013 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001014 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001015 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001016 }else if( pPager->state==PAGER_SYNCED ){
1017 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001018 }
danielk1977334cdb62007-03-26 08:05:12 +00001019 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001020 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001021 pPager->needSync = 0;
danielk19778c0a7912008-08-20 14:49:23 +00001022 /* lruListSetFirstSynced(pPager); */
drh588f5bc2007-01-02 18:41:54 +00001023 pPager->dbSize = -1;
drhd138c012008-05-13 00:58:18 +00001024 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001025
1026 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001027}
1028
drhed7c8552001-04-11 14:29:21 +00001029/*
drh968af522003-02-11 14:55:40 +00001030** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001031**
1032** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001033** random initial value and the page number. We experimented with
1034** a checksum of the entire data, but that was found to be too slow.
1035**
1036** Note that the page number is stored at the beginning of data and
1037** the checksum is stored at the end. This is important. If journal
1038** corruption occurs due to a power failure, the most likely scenario
1039** is that one end or the other of the record will be changed. It is
1040** much less likely that the two ends of the journal record will be
1041** correct and the middle be corrupt. Thus, this "checksum" scheme,
1042** though fast and simple, catches the mostly likely kind of corruption.
1043**
1044** FIX ME: Consider adding every 200th (or so) byte of the data to the
1045** checksum. That way if a single page spans 3 or more disk sectors and
1046** only the middle sector is corrupt, we will still have a reasonable
1047** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001048*/
drh74161702006-02-24 02:53:49 +00001049static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001050 u32 cksum = pPager->cksumInit;
1051 int i = pPager->pageSize-200;
1052 while( i>0 ){
1053 cksum += aData[i];
1054 i -= 200;
1055 }
drh968af522003-02-11 14:55:40 +00001056 return cksum;
1057}
1058
drh605ad8f2006-05-03 23:34:05 +00001059/* Forward declaration */
1060static void makeClean(PgHdr*);
1061
drh968af522003-02-11 14:55:40 +00001062/*
drhfa86c412002-02-02 15:01:15 +00001063** Read a single page from the journal file opened on file descriptor
1064** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001065**
drh726de592004-06-10 23:35:50 +00001066** If useCksum==0 it means this journal does not use checksums. Checksums
1067** are not used in statement journals because statement journals do not
1068** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001069*/
danielk197762079062007-08-15 17:08:46 +00001070static int pager_playback_one_page(
1071 Pager *pPager,
1072 sqlite3_file *jfd,
1073 i64 offset,
1074 int useCksum
1075){
drhfa86c412002-02-02 15:01:15 +00001076 int rc;
drhae2b40c2004-06-09 19:03:54 +00001077 PgHdr *pPg; /* An existing page in the cache */
1078 Pgno pgno; /* The page number of a page in journal */
1079 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001080 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001081
drh96362842005-03-20 22:47:56 +00001082 /* useCksum should be true for the main journal and false for
1083 ** statement journals. Verify that this is always the case
1084 */
drh9cbe6352005-11-29 03:13:21 +00001085 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001086 assert( aData );
drh96362842005-03-20 22:47:56 +00001087
danielk197762079062007-08-15 17:08:46 +00001088 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001089 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001090 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001091 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001092 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001093
drh968af522003-02-11 14:55:40 +00001094 /* Sanity checking on the page. This is more important that I originally
1095 ** thought. If a power failure occurs while the journal is being written,
1096 ** it could cause invalid data to be written into the journal. We need to
1097 ** detect this invalid data (with high probability) and ignore it.
1098 */
danielk197775edc162004-06-26 01:48:18 +00001099 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001100 return SQLITE_DONE;
1101 }
drhae2b40c2004-06-09 19:03:54 +00001102 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001103 return SQLITE_OK;
1104 }
drhae2b40c2004-06-09 19:03:54 +00001105 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001106 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001107 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001108 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001109 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001110 return SQLITE_DONE;
1111 }
1112 }
drhfa86c412002-02-02 15:01:15 +00001113
danielk1977aa5ccdf2004-06-14 05:10:42 +00001114 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001115
1116 /* If the pager is in RESERVED state, then there must be a copy of this
1117 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001118 ** not the database file. The page is left marked dirty in this case.
1119 **
danielk19772df71c72007-05-24 07:22:42 +00001120 ** An exception to the above rule: If the database is in no-sync mode
1121 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001122 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1123 ** during a Movepage() call, then the page may not be in the cache
1124 ** either. So the condition described in the above paragraph is not
1125 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001126 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001127 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1128 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001129 **
1130 ** Ticket #1171: The statement journal might contain page content that is
1131 ** different from the page content at the start of the transaction.
1132 ** This occurs when a page is changed prior to the start of a statement
1133 ** then changed again within the statement. When rolling back such a
1134 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001135 ** for certain that original page contents are synced into the main rollback
1136 ** journal. Otherwise, a power loss might leave modified data in the
1137 ** database file without an entry in the rollback journal that can
1138 ** restore the database to its original form. Two conditions must be
1139 ** met before writing to the database files. (1) the database must be
1140 ** locked. (2) we know that the original page content is fully synced
1141 ** in the main journal either because the page is not in cache or else
1142 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001143 **
1144 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1145 ** is possible to fail a statement on a database that does not yet exist.
1146 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001147 */
drhae2b40c2004-06-09 19:03:54 +00001148 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001149 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1150 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
danielk19778c0a7912008-08-20 14:49:23 +00001151 if( (pPager->state>=PAGER_EXCLUSIVE)
1152 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
1153 && (pPager->fd->pMethods)
1154 ){
danielk197762079062007-08-15 17:08:46 +00001155 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1156 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
danielk1977a3f3a5f2004-06-10 04:32:16 +00001157 }
drhfa86c412002-02-02 15:01:15 +00001158 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001159 /* No page should ever be explicitly rolled back that is in use, except
1160 ** for page 1 which is held in use in order to keep the lock on the
1161 ** database active. However such a page may be rolled back as a result
1162 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001163 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001164 */
drhb6f41482004-05-14 01:58:11 +00001165 void *pData;
danielk197728129562005-01-11 10:25:06 +00001166 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
danielk19778c0a7912008-08-20 14:49:23 +00001167 pData = pPg->pData;
drhae2b40c2004-06-09 19:03:54 +00001168 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001169 if( pPager->xReiniter ){
1170 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001171 }
danielk19778c0a7912008-08-20 14:49:23 +00001172 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00001173#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001174 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001175#endif
drh86a88112007-04-16 15:02:19 +00001176 /* If this was page 1, then restore the value of Pager.dbFileVers.
1177 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001178 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001179 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001180 }
drh86a88112007-04-16 15:02:19 +00001181
1182 /* Decode the page just read from disk */
1183 CODEC1(pPager, pData, pPg->pgno, 3);
danielk19778c0a7912008-08-20 14:49:23 +00001184 sqlite3PcacheRelease(pPg);
drhfa86c412002-02-02 15:01:15 +00001185 }
1186 return rc;
1187}
1188
1189/*
danielk197713adf8a2004-06-03 16:08:41 +00001190** Parameter zMaster is the name of a master journal file. A single journal
1191** file that referred to the master journal file has just been rolled back.
1192** This routine checks if it is possible to delete the master journal file,
1193** and does so if it is.
drh726de592004-06-10 23:35:50 +00001194**
danielk197765839c62007-08-30 08:08:17 +00001195** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1196** available for use within this function.
1197**
1198**
drh726de592004-06-10 23:35:50 +00001199** The master journal file contains the names of all child journals.
1200** To tell if a master journal can be deleted, check to each of the
1201** children. If all children are either missing or do not refer to
1202** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001203*/
danielk1977b4b47412007-08-17 15:53:36 +00001204static int pager_delmaster(Pager *pPager, const char *zMaster){
1205 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001206 int rc;
1207 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001208 sqlite3_file *pMaster;
1209 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001210 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001211 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001212
1213 /* Open the master journal file exclusively in case some other process
1214 ** is running this routine also. Not that it makes too much difference.
1215 */
drhe5ae5732008-06-15 02:51:47 +00001216 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001217 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001218 if( !pMaster ){
1219 rc = SQLITE_NOMEM;
1220 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001221 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1222 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001223 }
danielk197713adf8a2004-06-03 16:08:41 +00001224 if( rc!=SQLITE_OK ) goto delmaster_out;
1225 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001226
1227 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001228 if( rc!=SQLITE_OK ) goto delmaster_out;
1229
1230 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001231 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001232 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001233 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001234
1235 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001236 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001237 */
drhe5ae5732008-06-15 02:51:47 +00001238 zMasterJournal = (char *)sqlite3Malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001239 if( !zMasterJournal ){
1240 rc = SQLITE_NOMEM;
1241 goto delmaster_out;
1242 }
danielk197765839c62007-08-30 08:08:17 +00001243 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001244 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001245 if( rc!=SQLITE_OK ) goto delmaster_out;
1246
danielk19775865e3d2004-06-14 06:03:57 +00001247 zJournal = zMasterJournal;
1248 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001249 int exists;
1250 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1251 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001252 goto delmaster_out;
1253 }
danielk1977861f7452008-06-05 11:39:11 +00001254 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001255 /* One of the journals pointed to by the master journal exists.
1256 ** Open it and check if it points at the master journal. If
1257 ** so, return without deleting the master journal file.
1258 */
drh3b7b78b2004-11-24 01:16:43 +00001259 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001260 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1261 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001262 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001263 goto delmaster_out;
1264 }
danielk197713adf8a2004-06-03 16:08:41 +00001265
danielk197765839c62007-08-30 08:08:17 +00001266 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001267 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001268 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001269 goto delmaster_out;
1270 }
danielk197776572402004-06-25 02:38:54 +00001271
danielk197765839c62007-08-30 08:08:17 +00001272 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001273 if( c ){
danielk197776572402004-06-25 02:38:54 +00001274 /* We have a match. Do not delete the master journal file. */
1275 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001276 }
1277 }
danielk19775865e3d2004-06-14 06:03:57 +00001278 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001279 }
1280 }
1281
danielk1977fee2d252007-08-18 10:59:19 +00001282 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001283
1284delmaster_out:
1285 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001286 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001287 }
1288 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001289 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001290 }
danielk1977b4b47412007-08-17 15:53:36 +00001291 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001292 return rc;
1293}
1294
drha6abd042004-06-09 17:37:22 +00001295
danielk1977e180dd92007-04-05 17:15:52 +00001296static void pager_truncate_cache(Pager *pPager);
1297
drha6abd042004-06-09 17:37:22 +00001298/*
drhcb4c40b2004-08-18 19:09:43 +00001299** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001300** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001301**
1302** Might might be the case that the file on disk is smaller than nPage.
1303** This can happen, for example, if we are in the middle of a transaction
1304** which has extended the file size and the new pages are still all held
1305** in cache, then an INSERT or UPDATE does a statement rollback. Some
1306** operating system implementations can get confused if you try to
1307** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001308** so detect this case and write a single zero byte to the end of the new
1309** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001310*/
1311static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001312 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001313 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001314 i64 currentSize, newSize;
1315 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1316 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001317 if( rc==SQLITE_OK && currentSize!=newSize ){
1318 if( currentSize>newSize ){
1319 rc = sqlite3OsTruncate(pPager->fd, newSize);
1320 }else{
1321 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1322 }
drh7fe3f7e2007-11-29 18:44:27 +00001323 }
danielk1977e180dd92007-04-05 17:15:52 +00001324 }
1325 if( rc==SQLITE_OK ){
1326 pPager->dbSize = nPage;
1327 pager_truncate_cache(pPager);
1328 }
1329 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001330}
1331
1332/*
drhc80f0582007-05-01 16:59:48 +00001333** Set the sectorSize for the given pager.
1334**
drh334c80d2008-03-28 17:41:13 +00001335** The sector size is at least as big as the sector size reported
1336** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001337*/
1338static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001339 assert(pPager->fd->pMethods||pPager->tempFile);
1340 if( !pPager->tempFile ){
1341 /* Sector size doesn't matter for temporary files. Also, the file
1342 ** may not have been opened yet, in whcih case the OsSectorSize()
1343 ** call will segfault.
1344 */
1345 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1346 }
drh334c80d2008-03-28 17:41:13 +00001347 if( pPager->sectorSize<512 ){
1348 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001349 }
1350}
1351
1352/*
drhed7c8552001-04-11 14:29:21 +00001353** Playback the journal and thus restore the database file to
1354** the state it was in before we started making changes.
1355**
drh34e79ce2004-02-08 06:05:46 +00001356** The journal file format is as follows:
1357**
drhae2b40c2004-06-09 19:03:54 +00001358** (1) 8 byte prefix. A copy of aJournalMagic[].
1359** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001360** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001361** number of page records from the journal size.
1362** (3) 4 byte big-endian integer which is the initial value for the
1363** sanity checksum.
1364** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001365** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001366** (5) 4 byte big-endian integer which is the sector size. The header
1367** is this many bytes in size.
1368** (6) 4 byte big-endian integer which is the page case.
1369** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001370** name. The value may be zero (indicate that there is no master
1371** journal.)
drh334c80d2008-03-28 17:41:13 +00001372** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001373** and might be shorter than the value read from (5). If the first byte
1374** of the name is \000 then there is no master journal. The master
1375** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001376** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001377** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001378** + pPager->pageSize bytes of data.
1379** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001380**
drh334c80d2008-03-28 17:41:13 +00001381** When we speak of the journal header, we mean the first 8 items above.
1382** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001383**
1384** Call the value from the second bullet "nRec". nRec is the number of
1385** valid page entries in the journal. In most cases, you can compute the
1386** value of nRec from the size of the journal file. But if a power
1387** failure occurred while the journal was being written, it could be the
1388** case that the size of the journal file had already been increased but
1389** the extra entries had not yet made it safely to disk. In such a case,
1390** the value of nRec computed from the file size would be too large. For
1391** that reason, we always use the nRec value in the header.
1392**
1393** If the nRec value is 0xffffffff it means that nRec should be computed
1394** from the file size. This value is used when the user selects the
1395** no-sync option for the journal. A power failure could lead to corruption
1396** in this case. But for things like temporary table (which will be
1397** deleted when the power is restored) we don't care.
1398**
drhd9b02572001-04-15 00:37:09 +00001399** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001400** journal file then all pages up to the first corrupted page are rolled
1401** back (or no pages if the journal header is corrupted). The journal file
1402** is then deleted and SQLITE_OK returned, just as if no corruption had
1403** been encountered.
1404**
1405** If an I/O or malloc() error occurs, the journal-file is not deleted
1406** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001407*/
danielk1977e277be02007-03-23 18:12:06 +00001408static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001409 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001410 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001411 u32 nRec; /* Number of Records in the journal */
shane0b8d2762008-07-22 05:18:00 +00001412 u32 u; /* Unsigned loop counter */
drhd9b02572001-04-15 00:37:09 +00001413 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001414 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001415 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001416 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001417
drhc3a64ba2001-11-22 00:01:27 +00001418 /* Figure out how many records are in the journal. Abort early if
1419 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001420 */
drh8cfbf082001-09-19 13:22:39 +00001421 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001422 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001423 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001424 goto end_playback;
1425 }
drh240c5792004-02-08 00:40:52 +00001426
danielk197776572402004-06-25 02:38:54 +00001427 /* Read the master journal name from the journal, if it is present.
1428 ** If a master journal file name is specified, but the file is not
1429 ** present on disk, then the journal is not hot and does not need to be
1430 ** played back.
drh240c5792004-02-08 00:40:52 +00001431 */
danielk197765839c62007-08-30 08:08:17 +00001432 zMaster = pPager->pTmpSpace;
1433 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001434 if( rc==SQLITE_OK && zMaster[0] ){
1435 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001436 }
danielk197765839c62007-08-30 08:08:17 +00001437 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001438 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001439 goto end_playback;
1440 }
1441 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001442
danielk197776572402004-06-25 02:38:54 +00001443 /* This loop terminates either when the readJournalHdr() call returns
1444 ** SQLITE_DONE or an IO error occurs. */
1445 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001446
danielk197776572402004-06-25 02:38:54 +00001447 /* Read the next journal header from the journal file. If there are
1448 ** not enough bytes left in the journal file for a complete header, or
1449 ** it is corrupted, then a process must of failed while writing it.
1450 ** This indicates nothing more needs to be rolled back.
1451 */
1452 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1453 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001454 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001455 rc = SQLITE_OK;
1456 }
danielk197776572402004-06-25 02:38:54 +00001457 goto end_playback;
1458 }
1459
1460 /* If nRec is 0xffffffff, then this journal was created by a process
1461 ** working in no-sync mode. This means that the rest of the journal
1462 ** file consists of pages, there are no more journal headers. Compute
1463 ** the value of nRec based on this assumption.
1464 */
1465 if( nRec==0xffffffff ){
1466 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1467 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1468 }
1469
danielk1977e277be02007-03-23 18:12:06 +00001470 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001471 ** process and if this is the final header in the journal, then it means
1472 ** that this part of the journal was being filled but has not yet been
1473 ** synced to disk. Compute the number of pages based on the remaining
1474 ** size of the file.
1475 **
1476 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001477 */
drh8940f4e2007-08-11 00:26:20 +00001478 if( nRec==0 && !isHot &&
1479 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001480 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1481 }
1482
danielk197776572402004-06-25 02:38:54 +00001483 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001484 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001485 */
danielk1977e180dd92007-04-05 17:15:52 +00001486 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001487 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001488 if( rc!=SQLITE_OK ){
1489 goto end_playback;
1490 }
danielk197776572402004-06-25 02:38:54 +00001491 }
1492
danielk197776572402004-06-25 02:38:54 +00001493 /* Copy original pages out of the journal and back into the database file.
1494 */
shane0b8d2762008-07-22 05:18:00 +00001495 for(u=0; u<nRec; u++){
danielk197762079062007-08-15 17:08:46 +00001496 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001497 if( rc!=SQLITE_OK ){
1498 if( rc==SQLITE_DONE ){
1499 rc = SQLITE_OK;
1500 pPager->journalOff = szJ;
1501 break;
1502 }else{
1503 goto end_playback;
1504 }
1505 }
drh968af522003-02-11 14:55:40 +00001506 }
drhed7c8552001-04-11 14:29:21 +00001507 }
drh580eeaf2006-02-24 03:09:37 +00001508 /*NOTREACHED*/
1509 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001510
1511end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001512 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001513 zMaster = pPager->pTmpSpace;
1514 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1515 }
1516 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001517 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001518 }
danielk197765839c62007-08-30 08:08:17 +00001519 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001520 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001521 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001522 */
danielk197765839c62007-08-30 08:08:17 +00001523 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001524 }
danielk197776572402004-06-25 02:38:54 +00001525
1526 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001527 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001528 ** value. Reset it to the correct value for this process.
1529 */
drhc80f0582007-05-01 16:59:48 +00001530 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001531 return rc;
drhed7c8552001-04-11 14:29:21 +00001532}
1533
1534/*
drhac69b052004-05-12 13:30:07 +00001535** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001536**
1537** This is similar to playing back the transaction journal but with
1538** a few extra twists.
1539**
drh663fc632002-02-02 18:49:19 +00001540** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001541** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001542** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001543**
drhac69b052004-05-12 13:30:07 +00001544** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001545** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001546** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001547*/
drh3aac2dd2004-04-26 14:10:20 +00001548static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001549 i64 szJ; /* Size of the full journal */
1550 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001551 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001552 int i; /* Loop counter */
1553 int rc;
1554
danielk197776572402004-06-25 02:38:54 +00001555 szJ = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001556
danielk19774099f6e2007-03-19 11:25:20 +00001557 /* Set hdrOff to be the offset just after the end of the last journal
1558 ** page written before the first journal-header for this statement
1559 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001560 ** header was written.
1561 */
1562 hdrOff = pPager->stmtHdrOff;
1563 assert( pPager->fullSync || !hdrOff );
1564 if( !hdrOff ){
1565 hdrOff = szJ;
1566 }
1567
drhfa86c412002-02-02 15:01:15 +00001568 /* Truncate the database back to its original size.
1569 */
danielk1977e180dd92007-04-05 17:15:52 +00001570 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001571 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001572
drhac69b052004-05-12 13:30:07 +00001573 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001574 */
drhac69b052004-05-12 13:30:07 +00001575 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001576 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001577
drhac69b052004-05-12 13:30:07 +00001578 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001579 ** database file. Note that the statement journal omits checksums from
1580 ** each record since power-failure recovery is not important to statement
1581 ** journals.
drhfa86c412002-02-02 15:01:15 +00001582 */
danielk197762079062007-08-15 17:08:46 +00001583 for(i=0; i<nRec; i++){
1584 i64 offset = i*(4+pPager->pageSize);
1585 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001586 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001587 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001588 }
1589
danielk197776572402004-06-25 02:38:54 +00001590 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1591 ** was the size of the journal file when this statement was started, so
1592 ** everything after that needs to be rolled back, either into the
1593 ** database, the memory cache, or both.
1594 **
1595 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1596 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001597 */
danielk197776572402004-06-25 02:38:54 +00001598 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001599 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001600 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001601 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001602 assert( rc!=SQLITE_DONE );
1603 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1604 }
1605
1606 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001607 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001608 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001609 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001610 if( rc!=SQLITE_OK ){
1611 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001612 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001613 }
danielk1977f0113002006-01-24 12:09:17 +00001614 if( nJRec==0 ){
1615 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001616 }
danielk1977f0113002006-01-24 12:09:17 +00001617 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001618 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001619 assert( rc!=SQLITE_DONE );
1620 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1621 }
drhfa86c412002-02-02 15:01:15 +00001622 }
danielk197776572402004-06-25 02:38:54 +00001623
1624 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001625
drh3aac2dd2004-04-26 14:10:20 +00001626end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001627 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001628 pPager->journalOff = szJ;
1629 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001630 }
1631 return rc;
1632}
1633
1634/*
drhf57b14a2001-09-14 18:54:08 +00001635** Change the maximum number of in-memory pages that are allowed.
1636*/
danielk19773b8a05f2007-03-19 17:44:26 +00001637void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +00001638 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001639}
1640
1641/*
drh973b6e32003-02-12 14:09:42 +00001642** Adjust the robustness of the database to damage due to OS crashes
1643** or power failures by changing the number of syncs()s when writing
1644** the rollback journal. There are three levels:
1645**
drh054889e2005-11-30 03:20:31 +00001646** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001647** for temporary and transient files.
1648**
1649** NORMAL The journal is synced once before writes begin on the
1650** database. This is normally adequate protection, but
1651** it is theoretically possible, though very unlikely,
1652** that an inopertune power failure could leave the journal
1653** in a state which would cause damage to the database
1654** when it is rolled back.
1655**
1656** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001657** database (with some additional information - the nRec field
1658** of the journal header - being written in between the two
1659** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001660** single disk sector is atomic, then this mode provides
1661** assurance that the journal will not be corrupted to the
1662** point of causing damage to the database during rollback.
1663**
1664** Numeric values associated with these states are OFF==1, NORMAL=2,
1665** and FULL=3.
1666*/
danielk197793758c82005-01-21 08:13:14 +00001667#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00001668void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00001669 pPager->noSync = level==1 || pPager->tempFile;
1670 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00001671 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001672 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001673}
danielk197793758c82005-01-21 08:13:14 +00001674#endif
drh973b6e32003-02-12 14:09:42 +00001675
1676/*
drhaf6df112005-06-07 02:12:30 +00001677** The following global variable is incremented whenever the library
1678** attempts to open a temporary file. This information is used for
1679** testing and analysis only.
1680*/
drh0f7eb612006-08-08 13:51:43 +00001681#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001682int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001683#endif
drhaf6df112005-06-07 02:12:30 +00001684
1685/*
drh3f56e6e2007-03-15 01:16:47 +00001686** Open a temporary file.
1687**
1688** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001689** other error code if we fail. The OS will automatically delete the temporary
1690** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001691*/
danielk1977b4b47412007-08-17 15:53:36 +00001692static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00001693 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00001694 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00001695 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00001696){
drhfa86c412002-02-02 15:01:15 +00001697 int rc;
drh3f56e6e2007-03-15 01:16:47 +00001698
drh0f7eb612006-08-08 13:51:43 +00001699#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001700 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001701#endif
danielk1977b4b47412007-08-17 15:53:36 +00001702
drh33f4e022007-09-03 15:19:34 +00001703 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1704 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00001705 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00001706 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00001707 return rc;
1708}
1709
danielk19778c0a7912008-08-20 14:49:23 +00001710static int pagerStress(void *);
1711
drhfa86c412002-02-02 15:01:15 +00001712/*
drhed7c8552001-04-11 14:29:21 +00001713** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001714** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00001715** the first call to sqlite3PagerGet() and is only held open until the
1716** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00001717**
drh6446c4d2001-12-15 14:22:18 +00001718** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00001719** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00001720** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00001721**
1722** If zFilename is ":memory:" then all information is held in cache.
1723** It is never written to disk. This can be used to implement an
1724** in-memory database.
drhed7c8552001-04-11 14:29:21 +00001725*/
danielk19773b8a05f2007-03-19 17:44:26 +00001726int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00001727 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00001728 Pager **ppPager, /* Return the Pager structure here */
1729 const char *zFilename, /* Name of the database file to open */
danielk19778c0a7912008-08-20 14:49:23 +00001730 void (*xDesc)(DbPage*), /* Page destructor function */
drhda47d772002-12-02 04:25:19 +00001731 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00001732 int flags, /* flags controlling this file */
1733 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00001734){
danielk1977b4b47412007-08-17 15:53:36 +00001735 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00001736 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00001737 int rc = SQLITE_OK;
1738 int i;
danielk19778def5ea2004-06-16 10:39:52 +00001739 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00001740 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001741 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00001742 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
1743 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00001744 int journalFileSize = sqlite3JournalSize(pVfs);
danielk19778c0a7912008-08-20 14:49:23 +00001745 int pcacheSize = sqlite3PcacheSize();
drhfacf0302008-06-17 15:12:00 +00001746 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
danielk197717b90b52008-06-06 11:11:25 +00001747 char *zPathname = 0;
1748 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001749
drh86f8c192007-08-22 00:39:19 +00001750 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00001751 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001752
danielk197717b90b52008-06-06 11:11:25 +00001753 /* Compute and store the full pathname in an allocated buffer pointed
1754 ** to by zPathname, length nPathname. Or, if this is a temporary file,
1755 ** leave both nPathname and zPathname set to 0.
1756 */
drh1cc8c442007-08-24 16:08:29 +00001757 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00001758 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001759 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00001760 if( zPathname==0 ){
1761 return SQLITE_NOMEM;
1762 }
drh1cc8c442007-08-24 16:08:29 +00001763#ifndef SQLITE_OMIT_MEMORYDB
1764 if( strcmp(zFilename,":memory:")==0 ){
1765 memDb = 1;
1766 zPathname[0] = 0;
1767 }else
1768#endif
1769 {
danielk1977adfb9b02007-09-17 07:02:56 +00001770 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00001771 }
danielk197717b90b52008-06-06 11:11:25 +00001772 if( rc!=SQLITE_OK ){
1773 sqlite3_free(zPathname);
1774 return rc;
1775 }
1776 nPathname = strlen(zPathname);
drhae28c012007-08-24 16:29:23 +00001777 }
drh99b90c32008-03-17 13:50:58 +00001778
danielk1977b4b47412007-08-17 15:53:36 +00001779 /* Allocate memory for the pager structure */
1780 pPager = sqlite3MallocZero(
1781 sizeof(*pPager) + /* Pager structure */
danielk19778c0a7912008-08-20 14:49:23 +00001782 pcacheSize + /* PCache object */
danielk1977c7b60172007-08-22 11:22:03 +00001783 journalFileSize + /* The journal file structure */
drh17c0ba22008-02-26 18:40:11 +00001784 pVfs->szOsFile * 3 + /* The main db and two journal files */
danielk197717b90b52008-06-06 11:11:25 +00001785 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00001786 );
1787 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00001788 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00001789 return SQLITE_NOMEM;
1790 }
danielk19778c0a7912008-08-20 14:49:23 +00001791 pPager->pPCache = (PCache *)&pPager[1];
1792 pPtr = ((u8 *)&pPager[1]) + pcacheSize;
drh33f4e022007-09-03 15:19:34 +00001793 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00001794 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00001795 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
1796 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
1797 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00001798 pPager->zDirectory = &pPager->zFilename[nPathname+1];
1799 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00001800 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00001801 if( zPathname ){
1802 memcpy(pPager->zFilename, zPathname, nPathname+1);
1803 sqlite3_free(zPathname);
1804 }
danielk1977b4b47412007-08-17 15:53:36 +00001805
drh153c62c2007-08-24 03:51:33 +00001806 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00001807 */
drhae28c012007-08-24 16:29:23 +00001808 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00001809 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
1810 rc = SQLITE_CANTOPEN;
1811 }else{
drh1cc8c442007-08-24 16:08:29 +00001812 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00001813 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
1814 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00001815 readOnly = (fout&SQLITE_OPEN_READONLY);
1816
1817 /* If the file was successfully opened for read/write access,
1818 ** choose a default page size in case we have to create the
1819 ** database file. The default page size is the maximum of:
1820 **
1821 ** + SQLITE_DEFAULT_PAGE_SIZE,
1822 ** + The value returned by sqlite3OsSectorSize()
1823 ** + The largest page size that can be written atomically.
1824 */
1825 if( rc==SQLITE_OK && !readOnly ){
1826 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
drhfacf0302008-06-17 15:12:00 +00001827 if( szPageDflt<iSectorSize ){
1828 szPageDflt = iSectorSize;
drh1cc8c442007-08-24 16:08:29 +00001829 }
danielk19779663b8f2007-08-24 11:52:28 +00001830#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00001831 {
1832 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
1833 int ii;
1834 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
1835 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
1836 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
drhfacf0302008-06-17 15:12:00 +00001837 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
1838 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00001839 }
danielk1977b4b47412007-08-17 15:53:36 +00001840 }
drh1cc8c442007-08-24 16:08:29 +00001841#endif
drhfacf0302008-06-17 15:12:00 +00001842 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
1843 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
drh1cc8c442007-08-24 16:08:29 +00001844 }
danielk19778def5ea2004-06-16 10:39:52 +00001845 }
drhac69b052004-05-12 13:30:07 +00001846 }
drh1cc8c442007-08-24 16:08:29 +00001847 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001848 /* If a temporary file is requested, it is not opened immediately.
1849 ** In this case we accept the default page size and delay actually
1850 ** opening the file until the first call to OsWrite().
1851 */
danielk19777a2b1ee2007-08-21 14:27:01 +00001852 tempFile = 1;
1853 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00001854 }
danielk1977aef0bf62005-12-30 16:28:01 +00001855
danielk1977b4b47412007-08-17 15:53:36 +00001856 if( pPager && rc==SQLITE_OK ){
drhfacf0302008-06-17 15:12:00 +00001857 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00001858 }
danielk1977aef0bf62005-12-30 16:28:01 +00001859
drh153c62c2007-08-24 03:51:33 +00001860 /* If an error occured in either of the blocks above.
1861 ** Free the Pager structure and close the file.
1862 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00001863 ** any Pager.errMask variables.
1864 */
danielk1977b4b47412007-08-17 15:53:36 +00001865 if( !pPager || !pPager->pTmpSpace ){
1866 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00001867 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00001868 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00001869 }
danielk19778c0a7912008-08-20 14:49:23 +00001870 nExtra = FORCE_ALIGNMENT(nExtra);
1871 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb, xDesc, !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
danielk1977aef0bf62005-12-30 16:28:01 +00001872
drh153c62c2007-08-24 03:51:33 +00001873 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
1874 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00001875
danielk1977b4b47412007-08-17 15:53:36 +00001876 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00001877 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00001878 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00001879 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001880
drh99b90c32008-03-17 13:50:58 +00001881 /* Fill in Pager.zJournal[] */
danielk197717b90b52008-06-06 11:11:25 +00001882 if( zPathname ){
1883 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
1884 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
1885 }else{
1886 pPager->zJournal = 0;
1887 }
danielk1977b4b47412007-08-17 15:53:36 +00001888
drh3b59a5c2006-01-15 20:28:28 +00001889 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00001890 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00001891 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001892 /* pPager->stmtOpen = 0; */
1893 /* pPager->stmtInUse = 0; */
1894 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00001895 pPager->dbSize = memDb-1;
drhfacf0302008-06-17 15:12:00 +00001896 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00001897 /* pPager->stmtSize = 0; */
1898 /* pPager->stmtJSize = 0; */
1899 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00001900 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00001901 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00001902 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00001903 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00001904 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00001905 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00001906 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
1907 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
1908 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
1909 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00001910 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001911 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00001912 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00001913 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00001914 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00001915 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00001916 /* pPager->pFirst = 0; */
1917 /* pPager->pFirstSynced = 0; */
1918 /* pPager->pLast = 0; */
danielk19778c0a7912008-08-20 14:49:23 +00001919 pPager->nExtra = nExtra;
danielk1977b53e4962008-06-04 06:45:59 +00001920 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk19777a2b1ee2007-08-21 14:27:01 +00001921 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00001922 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00001923 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00001924 }
drh3b59a5c2006-01-15 20:28:28 +00001925 /* pPager->pBusyHandler = 0; */
1926 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00001927 *ppPager = pPager;
1928 return SQLITE_OK;
1929}
1930
1931/*
drh90f5ecb2004-07-22 01:19:35 +00001932** Set the busy handler function.
1933*/
danielk19773b8a05f2007-03-19 17:44:26 +00001934void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00001935 pPager->pBusyHandler = pBusyHandler;
1936}
1937
1938/*
drh72f82862001-05-24 21:06:34 +00001939** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00001940** when the reference count on each page reaches zero. The destructor can
1941** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00001942**
danielk19773b8a05f2007-03-19 17:44:26 +00001943** The destructor is not called as a result sqlite3PagerClose().
1944** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00001945*/
danielk19773b8a05f2007-03-19 17:44:26 +00001946void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00001947 pPager->xDestructor = xDesc;
1948}
1949
1950/*
drha6abd042004-06-09 17:37:22 +00001951** Set the reinitializer for this pager. If not NULL, the reinitializer
1952** is called when the content of a page in cache is restored to its original
1953** value as a result of a rollback. The callback gives higher-level code
1954** an opportunity to restore the EXTRA section to agree with the restored
1955** page data.
1956*/
danielk19773b8a05f2007-03-19 17:44:26 +00001957void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00001958 pPager->xReiniter = xReinit;
1959}
1960
1961/*
danielk1977a1644fd2007-08-29 12:31:25 +00001962** Set the page size to *pPageSize. If the suggest new page size is
1963** inappropriate, then an alternative page size is set to that
1964** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00001965*/
danielk1977a1644fd2007-08-29 12:31:25 +00001966int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
1967 int rc = SQLITE_OK;
1968 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00001969 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk19778c0a7912008-08-20 14:49:23 +00001970 pagerEnter(pPager);
danielk1977a1644fd2007-08-29 12:31:25 +00001971 if( pageSize && pageSize!=pPager->pageSize
danielk19778c0a7912008-08-20 14:49:23 +00001972 && !pPager->memDb && sqlite3PcacheRefCount(pPager->pPCache)==0
danielk1977a1644fd2007-08-29 12:31:25 +00001973 ){
drhfacf0302008-06-17 15:12:00 +00001974 char *pNew = (char *)sqlite3PageMalloc(pageSize);
danielk1977a1644fd2007-08-29 12:31:25 +00001975 if( !pNew ){
1976 rc = SQLITE_NOMEM;
1977 }else{
danielk1977a1644fd2007-08-29 12:31:25 +00001978 pager_reset(pPager);
1979 pPager->pageSize = pageSize;
1980 setSectorSize(pPager);
drhfacf0302008-06-17 15:12:00 +00001981 sqlite3PageFree(pPager->pTmpSpace);
danielk1977a1644fd2007-08-29 12:31:25 +00001982 pPager->pTmpSpace = pNew;
danielk19778c0a7912008-08-20 14:49:23 +00001983 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
danielk1977a1644fd2007-08-29 12:31:25 +00001984 }
drh1c7880e2005-05-20 20:01:55 +00001985 }
danielk1977a1644fd2007-08-29 12:31:25 +00001986 *pPageSize = pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00001987 pagerLeave(pPager);
danielk1977a1644fd2007-08-29 12:31:25 +00001988 return rc;
drh90f5ecb2004-07-22 01:19:35 +00001989}
1990
1991/*
drh26b79942007-11-28 16:19:56 +00001992** Return a pointer to the "temporary page" buffer held internally
1993** by the pager. This is a buffer that is big enough to hold the
1994** entire content of a database page. This buffer is used internally
1995** during rollback and will be overwritten whenever a rollback
1996** occurs. But other modules are free to use it too, as long as
1997** no rollbacks are happening.
1998*/
1999void *sqlite3PagerTempSpace(Pager *pPager){
2000 return pPager->pTmpSpace;
2001}
2002
2003/*
drhf8e632b2007-05-08 14:51:36 +00002004** Attempt to set the maximum database page count if mxPage is positive.
2005** Make no changes if mxPage is zero or negative. And never reduce the
2006** maximum page count below the current size of the database.
2007**
2008** Regardless of mxPage, return the current maximum page count.
2009*/
2010int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2011 if( mxPage>0 ){
2012 pPager->mxPgno = mxPage;
2013 }
danielk1977ad0132d2008-06-07 08:58:22 +00002014 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002015 return pPager->mxPgno;
2016}
2017
2018/*
drhc9ac5ca2005-11-04 22:03:30 +00002019** The following set of routines are used to disable the simulated
2020** I/O error mechanism. These routines are used to avoid simulated
2021** errors in places where we do not care about errors.
2022**
2023** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2024** and generate no code.
2025*/
2026#ifdef SQLITE_TEST
2027extern int sqlite3_io_error_pending;
2028extern int sqlite3_io_error_hit;
2029static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002030void disable_simulated_io_errors(void){
2031 saved_cnt = sqlite3_io_error_pending;
2032 sqlite3_io_error_pending = -1;
2033}
2034void enable_simulated_io_errors(void){
2035 sqlite3_io_error_pending = saved_cnt;
2036}
2037#else
drh152410f2005-11-05 15:11:22 +00002038# define disable_simulated_io_errors()
2039# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002040#endif
2041
2042/*
drh90f5ecb2004-07-22 01:19:35 +00002043** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002044** that pDest points to.
2045**
2046** No error checking is done. The rational for this is that this function
2047** may be called even if the file does not exist or contain a header. In
2048** these cases sqlite3OsRead() will return an error, to which the correct
2049** response is to zero the memory at pDest and continue. A real IO error
2050** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002051*/
danielk19773b8a05f2007-03-19 17:44:26 +00002052int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002053 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002054 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002055 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
danielk19778c0a7912008-08-20 14:49:23 +00002056 pagerEnter(pPager);
danielk19777a2b1ee2007-08-21 14:27:01 +00002057 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002058 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002059 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002060 if( rc==SQLITE_IOERR_SHORT_READ ){
2061 rc = SQLITE_OK;
2062 }
drh90f5ecb2004-07-22 01:19:35 +00002063 }
danielk19778c0a7912008-08-20 14:49:23 +00002064 pagerLeave(pPager);
drh551b7732006-11-06 21:20:25 +00002065 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002066}
2067
2068/*
drh5e00f6c2001-09-13 13:46:56 +00002069** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002070** pPager.
2071**
2072** If the PENDING_BYTE lies on the page directly after the end of the
2073** file, then consider this page part of the file too. For example, if
2074** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2075** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002076*/
danielk1977ad0132d2008-06-07 08:58:22 +00002077int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk19777a2b1ee2007-08-21 14:27:01 +00002078 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002079 int rc;
drhd9b02572001-04-15 00:37:09 +00002080 assert( pPager!=0 );
danielk19778c0a7912008-08-20 14:49:23 +00002081 pagerEnter(pPager);
drha7aea3d2007-03-15 12:51:16 +00002082 if( pPager->errCode ){
danielk19778c0a7912008-08-20 14:49:23 +00002083 rc = pPager->errCode;
2084 pagerLeave(pPager);
2085 return rc;
drha7aea3d2007-03-15 12:51:16 +00002086 }
drhed7c8552001-04-11 14:29:21 +00002087 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002088 n = pPager->dbSize;
2089 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002090 assert(pPager->fd->pMethods||pPager->tempFile);
2091 if( (pPager->fd->pMethods)
2092 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002093 pager_error(pPager, rc);
danielk19778c0a7912008-08-20 14:49:23 +00002094 pagerLeave(pPager);
danielk1977ad0132d2008-06-07 08:58:22 +00002095 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002096 }
2097 if( n>0 && n<pPager->pageSize ){
2098 n = 1;
2099 }else{
2100 n /= pPager->pageSize;
2101 }
2102 if( pPager->state!=PAGER_UNLOCK ){
2103 pPager->dbSize = n;
2104 }
drhed7c8552001-04-11 14:29:21 +00002105 }
danielk197715f411d2005-09-16 10:18:45 +00002106 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002107 n++;
2108 }
drhf8e632b2007-05-08 14:51:36 +00002109 if( n>pPager->mxPgno ){
2110 pPager->mxPgno = n;
2111 }
danielk1977ad0132d2008-06-07 08:58:22 +00002112 if( pnPage ){
2113 *pnPage = n;
2114 }
danielk19778c0a7912008-08-20 14:49:23 +00002115 pagerLeave(pPager);
danielk1977ad0132d2008-06-07 08:58:22 +00002116 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002117}
2118
2119/*
drhf7c57532003-04-25 13:22:51 +00002120** Forward declaration
2121*/
danielk197776572402004-06-25 02:38:54 +00002122static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002123
2124/*
danielk1977e180dd92007-04-05 17:15:52 +00002125** This routine is used to truncate the cache when a database
2126** is truncated. Drop from the cache all pages whose pgno is
2127** larger than pPager->dbSize and is unreferenced.
2128**
drhac69b052004-05-12 13:30:07 +00002129** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002130**
2131** Actually, at the point this routine is called, it would be
2132** an error to have a referenced page. But rather than delete
2133** that page and guarantee a subsequent segfault, it seems better
2134** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002135*/
danielk1977e180dd92007-04-05 17:15:52 +00002136static void pager_truncate_cache(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00002137 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
drhac69b052004-05-12 13:30:07 +00002138}
2139
drhf7c57532003-04-25 13:22:51 +00002140/*
danielk197717221812005-02-15 03:38:05 +00002141** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002142** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002143** false or until the lock succeeds.
2144**
2145** Return SQLITE_OK on success and an error code if we cannot obtain
2146** the lock.
2147*/
2148static int pager_wait_on_lock(Pager *pPager, int locktype){
2149 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002150
2151 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002152 assert( PAGER_SHARED==SHARED_LOCK );
2153 assert( PAGER_RESERVED==RESERVED_LOCK );
2154 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002155
2156 /* If the file is currently unlocked then the size must be unknown */
2157 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2158
danielk197717221812005-02-15 03:38:05 +00002159 if( pPager->state>=locktype ){
2160 rc = SQLITE_OK;
2161 }else{
drhbefdf832008-03-14 19:33:05 +00002162 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002163 do {
drh054889e2005-11-30 03:20:31 +00002164 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002165 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002166 if( rc==SQLITE_OK ){
2167 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002168 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002169 }
2170 }
2171 return rc;
2172}
2173
2174/*
drhf7c57532003-04-25 13:22:51 +00002175** Truncate the file to the number of pages specified.
2176*/
danielk19773b8a05f2007-03-19 17:44:26 +00002177int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
danielk19778c0a7912008-08-20 14:49:23 +00002178 int rc = SQLITE_OK;
drh1aa2d8b2007-01-03 15:34:29 +00002179 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19778c0a7912008-08-20 14:49:23 +00002180
2181 pagerEnter(pPager);
2182
danielk1977ad0132d2008-06-07 08:58:22 +00002183 sqlite3PagerPagecount(pPager, 0);
danielk1977efaaf572006-01-16 11:29:19 +00002184 if( pPager->errCode ){
2185 rc = pPager->errCode;
danielk19778c0a7912008-08-20 14:49:23 +00002186 }else if( nPage<(unsigned)pPager->dbSize ){
2187 if( MEMDB ){
2188 pPager->dbSize = nPage;
2189 pager_truncate_cache(pPager);
2190 }else{
2191 rc = syncJournal(pPager);
2192 if( rc==SQLITE_OK ){
2193 /* Get an exclusive lock on the database before truncating. */
2194 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2195 }
2196 if( rc==SQLITE_OK ){
2197 rc = pager_truncate(pPager, nPage);
2198 }
2199 }
danielk1977369f27e2004-06-15 11:40:04 +00002200 }
danielk197717221812005-02-15 03:38:05 +00002201
drh86f8c192007-08-22 00:39:19 +00002202 pagerLeave(pPager);
drhf7c57532003-04-25 13:22:51 +00002203 return rc;
2204}
2205
2206/*
drhed7c8552001-04-11 14:29:21 +00002207** Shutdown the page cache. Free all memory and close all files.
2208**
2209** If a transaction was in progress when this routine is called, that
2210** transaction is rolled back. All outstanding pages are invalidated
2211** and their memory is freed. Any attempt to use a page associated
2212** with this page cache after this function returns will likely
2213** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002214**
2215** This function always succeeds. If a transaction is active an attempt
2216** is made to roll it back. If an error occurs during the rollback
2217** a hot journal may be left in the filesystem but no error is returned
2218** to the caller.
drhed7c8552001-04-11 14:29:21 +00002219*/
danielk19773b8a05f2007-03-19 17:44:26 +00002220int sqlite3PagerClose(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00002221 pagerEnter(pPager);
danielk197713f72992005-12-18 08:51:22 +00002222
drhbafda092007-01-03 23:36:22 +00002223 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002224 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002225 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002226 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002227 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002228 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002229 enable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002230 sqlite3EndBenignMalloc();
drh4f0c5872007-03-26 22:05:01 +00002231 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002232 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002233 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002234 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002235 }
drhf5e7bb52008-02-18 14:47:33 +00002236 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002237 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002238 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002239 }
danielk1977b4b47412007-08-17 15:53:36 +00002240 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002241 /* Temp files are automatically deleted by the OS
2242 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002243 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002244 ** }
2245 */
danielk1977aca790a2005-01-13 11:07:52 +00002246
drhfacf0302008-06-17 15:12:00 +00002247 sqlite3PageFree(pPager->pTmpSpace);
danielk19778c0a7912008-08-20 14:49:23 +00002248 sqlite3PcacheClose(pPager->pPCache);
drh17435752007-08-16 04:30:38 +00002249 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002250 return SQLITE_OK;
2251}
2252
drh87cc3b32007-05-08 21:45:27 +00002253#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002254/*
drh5e00f6c2001-09-13 13:46:56 +00002255** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002256*/
danielk19773b8a05f2007-03-19 17:44:26 +00002257Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002258 return p->pgno;
2259}
drh87cc3b32007-05-08 21:45:27 +00002260#endif
drhed7c8552001-04-11 14:29:21 +00002261
2262/*
drhdf0b3b02001-06-23 11:36:20 +00002263** Increment the reference count for a page. The input pointer is
2264** a reference to the page data.
2265*/
danielk19773b8a05f2007-03-19 17:44:26 +00002266int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002267 pagerEnter(pPg->pPager);
danielk19778c0a7912008-08-20 14:49:23 +00002268 sqlite3PcacheRef(pPg);
drh86f8c192007-08-22 00:39:19 +00002269 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002270 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002271}
2272
2273/*
drh34e79ce2004-02-08 06:05:46 +00002274** Sync the journal. In other words, make sure all the pages that have
2275** been written to the journal have actually reached the surface of the
2276** disk. It is not safe to modify the original database file until after
2277** the journal has been synced. If the original database is modified before
2278** the journal is synced and a power failure occurs, the unsynced journal
2279** data would be lost and we would be unable to completely rollback the
2280** database changes. Database corruption would occur.
2281**
2282** This routine also updates the nRec field in the header of the journal.
2283** (See comments on the pager_playback() routine for additional information.)
2284** If the sync mode is FULL, two syncs will occur. First the whole journal
2285** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002286**
drh34e79ce2004-02-08 06:05:46 +00002287** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002288** after a power failure, so no sync occurs.
2289**
2290** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2291** the database is stored, then OsSync() is never called on the journal
2292** file. In this case all that is required is to update the nRec field in
2293** the journal header.
drhfa86c412002-02-02 15:01:15 +00002294**
drh34e79ce2004-02-08 06:05:46 +00002295** This routine clears the needSync field of every page current held in
2296** memory.
drh50e5dad2001-09-15 00:57:28 +00002297*/
danielk197776572402004-06-25 02:38:54 +00002298static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002299 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002300
2301 /* Sync the journal before modifying the main database
2302 ** (assuming there is a journal and it needs to be synced.)
2303 */
danielk197776572402004-06-25 02:38:54 +00002304 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002305 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002306 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002307 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002308
danielk19774cd2cd52007-08-23 14:48:23 +00002309 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002310 /* Write the nRec value into the journal file header. If in
2311 ** full-synchronous mode, sync the journal first. This ensures that
2312 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002313 ** it as a candidate for rollback.
2314 **
2315 ** This is not required if the persistent media supports the
2316 ** SAFE_APPEND property. Because in this case it is not possible
2317 ** for garbage data to be appended to the file, the nRec field
2318 ** is populated with 0xFFFFFFFF when the journal header is written
2319 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002320 */
danielk19774cd2cd52007-08-23 14:48:23 +00002321 i64 jrnlOff;
2322 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002323 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002324 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002325 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002326 if( rc!=0 ) return rc;
2327 }
danielk197713adf8a2004-06-03 16:08:41 +00002328
danielk197762079062007-08-15 17:08:46 +00002329 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2330 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2331 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002332 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002333 }
danielk19774cd2cd52007-08-23 14:48:23 +00002334 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2335 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2336 IOTRACE(("JSYNC %p\n", pPager))
2337 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2338 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2339 );
2340 if( rc!=0 ) return rc;
2341 }
drhdb48ee02003-01-16 13:42:43 +00002342 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002343 }
drh50e5dad2001-09-15 00:57:28 +00002344 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002345
2346 /* Erase the needSync flag from every page.
2347 */
danielk19778c0a7912008-08-20 14:49:23 +00002348 sqlite3PcacheSetFlags(pPager->pPCache, ~PGHDR_NEED_SYNC, 0);
2349 /* lruListSetFirstSynced(pPager); */
drh50e5dad2001-09-15 00:57:28 +00002350 }
drh03eb96a2002-11-10 23:32:56 +00002351
drh341eae82003-01-21 02:39:36 +00002352#ifndef NDEBUG
2353 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2354 ** flag must also be clear for all pages. Verify that this
2355 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002356 */
drh341eae82003-01-21 02:39:36 +00002357 else{
danielk19778c0a7912008-08-20 14:49:23 +00002358 sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_NEED_SYNC);
2359 /* assert( pPager->lru.pFirstSynced==pPager->lru.pFirst ); */
drh03eb96a2002-11-10 23:32:56 +00002360 }
drh341eae82003-01-21 02:39:36 +00002361#endif
drhdb48ee02003-01-16 13:42:43 +00002362
drh81a20f22001-10-12 17:30:04 +00002363 return rc;
drh50e5dad2001-09-15 00:57:28 +00002364}
2365
2366/*
drh2554f8b2003-01-22 01:26:44 +00002367** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2368** every one of those pages out to the database file and mark them all
2369** as clean.
2370*/
2371static int pager_write_pagelist(PgHdr *pList){
2372 Pager *pPager;
2373 int rc;
2374
2375 if( pList==0 ) return SQLITE_OK;
2376 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002377
2378 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2379 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002380 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002381 **
drha6abd042004-06-09 17:37:22 +00002382 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2383 ** through an intermediate state PENDING. A PENDING lock prevents new
2384 ** readers from attaching to the database but is unsufficient for us to
2385 ** write. The idea of a PENDING lock is to prevent new readers from
2386 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002387 **
drha6abd042004-06-09 17:37:22 +00002388 ** While the pager is in the RESERVED state, the original database file
2389 ** is unchanged and we can rollback without having to playback the
2390 ** journal into the original database file. Once we transition to
2391 ** EXCLUSIVE, it means the database file has been changed and any rollback
2392 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002393 */
drh684917c2004-10-05 02:41:42 +00002394 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002395 if( rc!=SQLITE_OK ){
2396 return rc;
2397 }
2398
drh2554f8b2003-01-22 01:26:44 +00002399 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002400
2401 /* If the file has not yet been opened, open it now. */
2402 if( !pPager->fd->pMethods ){
2403 assert(pPager->tempFile);
danielk197717b90b52008-06-06 11:11:25 +00002404 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002405 if( rc ) return rc;
2406 }
2407
danielk1977687566d2004-11-02 12:56:41 +00002408 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002409 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002410 ** make the file smaller (presumably by auto-vacuum code). Do not write
2411 ** any such pages to the file.
2412 */
2413 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00002414 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002415 char *pData = CODEC2(pPager, pList->pData, pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002416 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2417 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002418 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002419 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002420 PAGER_INCR(sqlite3_pager_writedb_count);
2421 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002422 if( pList->pgno==1 ){
2423 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2424 }
danielk1977687566d2004-11-02 12:56:41 +00002425 }
2426#ifndef NDEBUG
2427 else{
drh4f0c5872007-03-26 22:05:01 +00002428 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002429 }
2430#endif
drh2554f8b2003-01-22 01:26:44 +00002431 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002432#ifdef SQLITE_CHECK_PAGES
2433 pList->pageHash = pager_pagehash(pList);
2434#endif
danielk19778c0a7912008-08-20 14:49:23 +00002435 makeClean(pList);
drh2554f8b2003-01-22 01:26:44 +00002436 pList = pList->pDirty;
2437 }
danielk19778c0a7912008-08-20 14:49:23 +00002438
2439 /* sqlite3PcacheCleanAll(pPager->pPCache); */
drh2554f8b2003-01-22 01:26:44 +00002440 return SQLITE_OK;
2441}
2442
2443/*
danielk19778c0a7912008-08-20 14:49:23 +00002444** This function is called by the pcache layer when it has reached some
2445** soft memory limit. The argument is a pointer to a purgeable Pager
2446** object. This function attempts to make a single dirty page that has no
2447** outstanding references (if one exists) clean so that it can be recycled
2448** by the pcache layer.
drh2554f8b2003-01-22 01:26:44 +00002449*/
danielk19778c0a7912008-08-20 14:49:23 +00002450static int pagerStress(void *p){
2451 Pager *pPager = (Pager *)p;
2452 PgHdr *pPg = sqlite3PcacheDirtyPage(pPager->pPCache);
2453 int rc = SQLITE_OK;
drh8f2e9a12008-02-26 14:46:04 +00002454
danielk19778c0a7912008-08-20 14:49:23 +00002455 if( pPg && pPager->errCode==SQLITE_OK ){
2456 assert( pPg->flags&PGHDR_DIRTY );
2457 if( pPg->flags&PGHDR_NEED_SYNC ){
2458 rc = syncJournal(pPager);
2459 if( rc==SQLITE_OK && pPager->fullSync
2460 && !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
2461 ){
2462 pPager->nRec = 0;
2463 rc = writeJournalHdr(pPager);
2464 }
2465 }
2466 if( rc==SQLITE_OK ){
2467 rc = pager_write_pagelist(pPg);
2468 }
2469 if( rc!=SQLITE_OK ){
2470 pager_error(pPager, rc);
2471 }
2472 }
2473 return rc;
drh2554f8b2003-01-22 01:26:44 +00002474}
2475
danielk19778c0a7912008-08-20 14:49:23 +00002476
drh2554f8b2003-01-22 01:26:44 +00002477/*
drh19db9352008-03-27 22:42:51 +00002478** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00002479** A hot journal is one that needs to be played back.
2480**
2481** If the current size of the database file is 0 but a journal file
2482** exists, that is probably an old journal left over from a prior
2483** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00002484**
2485** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00002486**
2487** This routine does not open the journal file to examine its
2488** content. Hence, the journal might contain the name of a master
2489** journal file that has been deleted, and hence not be hot. Or
2490** the header of the journal might be zeroed out. This routine
2491** does not discover these cases of a non-hot journal - if the
2492** journal file exists and is not empty this routine assumes it
2493** is hot. The pager_playback() routine will discover that the
2494** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00002495*/
danielk1977d300b8a2008-08-01 10:50:23 +00002496static int hasHotJournal(Pager *pPager, int *pExists){
danielk1977b4b47412007-08-17 15:53:36 +00002497 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977d300b8a2008-08-01 10:50:23 +00002498 int rc = SQLITE_OK;
2499 *pExists = 0;
danielk1977861f7452008-06-05 11:39:11 +00002500 if( pPager->useJournal && pPager->fd->pMethods ){
danielk1977861f7452008-06-05 11:39:11 +00002501 int exists;
2502 int locked;
2503
2504 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
2505 if( rc==SQLITE_OK && exists ){
2506 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
2507 }
2508
2509 if( rc==SQLITE_OK && exists && !locked ){
danielk1977ad0132d2008-06-07 08:58:22 +00002510 int nPage;
2511 rc = sqlite3PagerPagecount(pPager, &nPage);
danielk1977d300b8a2008-08-01 10:50:23 +00002512 if( rc==SQLITE_OK ){
2513 if( nPage==0 ){
2514 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
2515 }else{
2516 *pExists = 1;
2517 }
danielk1977861f7452008-06-05 11:39:11 +00002518 }
2519 }
drhbb5f18d2007-04-06 18:23:17 +00002520 }
danielk1977861f7452008-06-05 11:39:11 +00002521
danielk1977d300b8a2008-08-01 10:50:23 +00002522 return rc;
drh165ffe92005-03-15 17:09:30 +00002523}
2524
drh86f8c192007-08-22 00:39:19 +00002525#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00002526/*
2527** This function is called to free superfluous dynamically allocated memory
2528** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00002529** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00002530**
2531** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00002532** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00002533** of bytes of memory released.
2534*/
danielk19773b8a05f2007-03-19 17:44:26 +00002535int sqlite3PagerReleaseMemory(int nReq){
danielk19778c0a7912008-08-20 14:49:23 +00002536 return 0;
danielk197713f72992005-12-18 08:51:22 +00002537}
drh86f8c192007-08-22 00:39:19 +00002538#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00002539
drh165ffe92005-03-15 17:09:30 +00002540/*
danielk1977e180dd92007-04-05 17:15:52 +00002541** Read the content of page pPg out of the database file.
2542*/
2543static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
2544 int rc;
danielk197762079062007-08-15 17:08:46 +00002545 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00002546 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00002547 assert(pPager->fd->pMethods||pPager->tempFile);
2548 if( !pPager->fd->pMethods ){
2549 return SQLITE_IOERR_SHORT_READ;
2550 }
danielk197762079062007-08-15 17:08:46 +00002551 offset = (pgno-1)*(i64)pPager->pageSize;
danielk19778c0a7912008-08-20 14:49:23 +00002552 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002553 PAGER_INCR(sqlite3_pager_readdb_count);
2554 PAGER_INCR(pPager->nRead);
2555 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00002556 if( pgno==1 ){
danielk19778c0a7912008-08-20 14:49:23 +00002557 memcpy(&pPager->dbFileVers, &((u8*)pPg->pData)[24],
drh86a88112007-04-16 15:02:19 +00002558 sizeof(pPager->dbFileVers));
2559 }
danielk1977e180dd92007-04-05 17:15:52 +00002560 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00002561 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
2562 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00002563 return rc;
2564}
2565
2566
2567/*
danielk1977e277be02007-03-23 18:12:06 +00002568** This function is called to obtain the shared lock required before
2569** data may be read from the pager cache. If the shared lock has already
2570** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00002571**
2572** Immediately after obtaining the shared lock (if required), this function
2573** checks for a hot-journal file. If one is found, an emergency rollback
2574** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00002575*/
2576static int pagerSharedLock(Pager *pPager){
2577 int rc = SQLITE_OK;
danielk1977d300b8a2008-08-01 10:50:23 +00002578 int isErrorReset = 0;
danielk1977e277be02007-03-23 18:12:06 +00002579
danielk1977ae72d982007-10-03 08:46:44 +00002580 /* If this database is opened for exclusive access, has no outstanding
2581 ** page references and is in an error-state, now is the chance to clear
2582 ** the error. Discard the contents of the pager-cache and treat any
2583 ** open journal file as a hot-journal.
2584 */
danielk19778c0a7912008-08-20 14:49:23 +00002585 if( !MEMDB && pPager->exclusiveMode
2586 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
2587 ){
danielk1977ae72d982007-10-03 08:46:44 +00002588 if( pPager->journalOpen ){
danielk1977d300b8a2008-08-01 10:50:23 +00002589 isErrorReset = 1;
danielk1977ae72d982007-10-03 08:46:44 +00002590 }
danielk1977ae72d982007-10-03 08:46:44 +00002591 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00002592 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00002593 }
2594
2595 /* If the pager is still in an error state, do not proceed. The error
2596 ** state will be cleared at some point in the future when all page
2597 ** references are dropped and the cache can be discarded.
2598 */
2599 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
2600 return pPager->errCode;
2601 }
2602
danielk1977d300b8a2008-08-01 10:50:23 +00002603 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
danielk1977b4b47412007-08-17 15:53:36 +00002604 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00002605 if( !MEMDB ){
danielk1977d300b8a2008-08-01 10:50:23 +00002606 int isHotJournal;
danielk19778c0a7912008-08-20 14:49:23 +00002607 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
danielk1977e277be02007-03-23 18:12:06 +00002608 if( !pPager->noReadlock ){
2609 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
2610 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002611 assert( pPager->state==PAGER_UNLOCK );
danielk1977e277be02007-03-23 18:12:06 +00002612 return pager_error(pPager, rc);
2613 }
2614 assert( pPager->state>=SHARED_LOCK );
2615 }
2616
2617 /* If a journal file exists, and there is no RESERVED lock on the
2618 ** database file, then it either needs to be played back or deleted.
2619 */
danielk1977d300b8a2008-08-01 10:50:23 +00002620 if( !isErrorReset ){
2621 rc = hasHotJournal(pPager, &isHotJournal);
2622 if( rc!=SQLITE_OK ){
2623 goto failed;
2624 }
drh19db9352008-03-27 22:42:51 +00002625 }
danielk1977d300b8a2008-08-01 10:50:23 +00002626 if( isErrorReset || isHotJournal ){
danielk1977e277be02007-03-23 18:12:06 +00002627 /* Get an EXCLUSIVE lock on the database file. At this point it is
2628 ** important that a RESERVED lock is not obtained on the way to the
2629 ** EXCLUSIVE lock. If it were, another process might open the
2630 ** database file, detect the RESERVED lock, and conclude that the
2631 ** database is safe to read while this process is still rolling it
2632 ** back.
2633 **
2634 ** Because the intermediate RESERVED lock is not requested, the
2635 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00002636 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00002637 */
danielk1977ae72d982007-10-03 08:46:44 +00002638 if( pPager->state<EXCLUSIVE_LOCK ){
2639 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
2640 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002641 rc = pager_error(pPager, rc);
2642 goto failed;
danielk1977ae72d982007-10-03 08:46:44 +00002643 }
2644 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00002645 }
danielk1977e277be02007-03-23 18:12:06 +00002646
drh16e45a42008-04-19 20:34:18 +00002647 /* Open the journal for read/write access. This is because in
drhfd131da2007-08-07 17:13:03 +00002648 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00002649 ** possibly used for a transaction later on. On some systems, the
2650 ** OsTruncate() call used in exclusive-access mode also requires
2651 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00002652 */
danielk1977d300b8a2008-08-01 10:50:23 +00002653 if( !isErrorReset && pPager->journalOpen==0 ){
danielk1977861f7452008-06-05 11:39:11 +00002654 int res;
2655 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
2656 if( rc==SQLITE_OK ){
2657 if( res ){
2658 int fout = 0;
2659 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
2660 assert( !pPager->tempFile );
2661 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
2662 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
2663 if( fout&SQLITE_OPEN_READONLY ){
2664 rc = SQLITE_BUSY;
2665 sqlite3OsClose(pPager->jfd);
2666 }
2667 }else{
2668 /* If the journal does not exist, that means some other process
2669 ** has already rolled it back */
danielk1977ae72d982007-10-03 08:46:44 +00002670 rc = SQLITE_BUSY;
danielk1977ae72d982007-10-03 08:46:44 +00002671 }
danielk1977979f38e2007-03-27 16:19:51 +00002672 }
2673 }
danielk1977e277be02007-03-23 18:12:06 +00002674 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002675 if( rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_UNLOCK
2676 && rc!=SQLITE_IOERR_NOMEM
2677 ){
2678 rc = SQLITE_BUSY;
drh1aa5af12008-03-07 19:51:14 +00002679 }
danielk197752b472a2008-05-05 16:23:55 +00002680 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002681 }
2682 pPager->journalOpen = 1;
2683 pPager->journalStarted = 0;
2684 pPager->journalOff = 0;
2685 pPager->setMaster = 0;
2686 pPager->journalHdr = 0;
2687
2688 /* Playback and delete the journal. Drop the database write
2689 ** lock and reacquire the read lock.
2690 */
2691 rc = pager_playback(pPager, 1);
2692 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002693 rc = pager_error(pPager, rc);
2694 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002695 }
danielk1977c5859712007-03-26 12:26:27 +00002696 assert(pPager->state==PAGER_SHARED ||
2697 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
2698 );
danielk1977e277be02007-03-23 18:12:06 +00002699 }
2700
danielk19778c0a7912008-08-20 14:49:23 +00002701 if( sqlite3PcachePagecount(pPager->pPCache)>0 ){
danielk197724168722007-04-02 05:07:47 +00002702 /* The shared-lock has just been acquired on the database file
2703 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00002704 ** read or write transaction). Check to see if the database
2705 ** has been modified. If the database has changed, flush the
2706 ** cache.
2707 **
2708 ** Database changes is detected by looking at 15 bytes beginning
2709 ** at offset 24 into the file. The first 4 of these 16 bytes are
2710 ** a 32-bit counter that is incremented with each change. The
2711 ** other bytes change randomly with each file change when
2712 ** a codec is in use.
2713 **
2714 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00002715 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00002716 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00002717 */
drh86a88112007-04-16 15:02:19 +00002718 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977ad0132d2008-06-07 08:58:22 +00002719 sqlite3PagerPagecount(pPager, 0);
danielk197724168722007-04-02 05:07:47 +00002720
danielk1977e180dd92007-04-05 17:15:52 +00002721 if( pPager->errCode ){
danielk197752b472a2008-05-05 16:23:55 +00002722 rc = pPager->errCode;
2723 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00002724 }
2725
danielk1977e180dd92007-04-05 17:15:52 +00002726 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00002727 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00002728 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00002729 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00002730 goto failed;
danielk1977e180dd92007-04-05 17:15:52 +00002731 }
drh86a88112007-04-16 15:02:19 +00002732 }else{
2733 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00002734 }
2735
drh86a88112007-04-16 15:02:19 +00002736 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00002737 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002738 }
2739 }
2740 }
danielk1977c5859712007-03-26 12:26:27 +00002741 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
2742 if( pPager->state==PAGER_UNLOCK ){
2743 pPager->state = PAGER_SHARED;
2744 }
danielk1977e277be02007-03-23 18:12:06 +00002745 }
2746
danielk197752b472a2008-05-05 16:23:55 +00002747 failed:
2748 if( rc!=SQLITE_OK ){
2749 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
2750 pager_unlock(pPager);
2751 }
danielk1977e277be02007-03-23 18:12:06 +00002752 return rc;
2753}
2754
2755/*
drhd33d5a82007-04-26 12:11:28 +00002756** Make sure we have the content for a page. If the page was
2757** previously acquired with noContent==1, then the content was
2758** just initialized to zeros instead of being read from disk.
2759** But now we need the real data off of disk. So make sure we
2760** have it. Read it in if we do not have it already.
2761*/
2762static int pager_get_content(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002763 if( pPg->flags&PGHDR_NEED_READ ){
drhd33d5a82007-04-26 12:11:28 +00002764 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
2765 if( rc==SQLITE_OK ){
danielk19778c0a7912008-08-20 14:49:23 +00002766 pPg->flags &= ~PGHDR_NEED_READ;
drhd33d5a82007-04-26 12:11:28 +00002767 }else{
2768 return rc;
2769 }
2770 }
2771 return SQLITE_OK;
2772}
2773
2774/*
danielk19778c0a7912008-08-20 14:49:23 +00002775** If the reference count has reached zero, and the pager is not in the
2776** middle of a write transaction or opened in exclusive mode, unlock it.
2777*/
2778static void pagerUnlockIfUnused(Pager *pPager){
2779 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
2780 && (!pPager->exclusiveMode || pPager->journalOff>0)
2781 ){
2782 pagerUnlockAndRollback(pPager);
2783 }
2784}
2785
2786/*
2787** Drop a page from the cache using sqlite3PcacheDrop().
2788**
2789** If this means there are now no pages with references to them, a rollback
2790** occurs and the lock on the database is removed.
2791*/
2792static void pagerDropPage(DbPage *pPg){
2793 Pager *pPager = pPg->pPager;
2794 sqlite3PcacheDrop(pPg);
2795 pagerUnlockIfUnused(pPager);
2796}
2797
2798/*
drhd9b02572001-04-15 00:37:09 +00002799** Acquire a page.
2800**
drh58a11682001-11-10 13:51:08 +00002801** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00002802** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00002803**
drhd33d5a82007-04-26 12:11:28 +00002804** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00002805** file is smaller than the requested page, then no actual disk
2806** read occurs and the memory image of the page is initialized to
2807** all zeros. The extra data appended to a page is always initialized
2808** to zeros the first time a page is loaded into memory.
2809**
drhd9b02572001-04-15 00:37:09 +00002810** The acquisition might fail for several reasons. In all cases,
2811** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00002812**
drhd33d5a82007-04-26 12:11:28 +00002813** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00002814** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00002815** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00002816** just returns 0. This routine acquires a read-lock the first time it
2817** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00002818** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00002819** or journal files.
drh0787db62007-03-04 13:15:27 +00002820**
drh538f5702007-04-13 02:14:30 +00002821** If noContent is false, the page contents are actually read from disk.
2822** If noContent is true, it means that we do not care about the contents
2823** of the page at this time, so do not do a disk read. Just fill in the
2824** page content with zeros. But mark the fact that we have not read the
2825** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00002826** sqlite3PagerWrite() is called on this page or if this routine is
2827** called again with noContent==0, that means that the content is needed
2828** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00002829*/
drh86f8c192007-08-22 00:39:19 +00002830static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00002831 Pager *pPager, /* The pager open on the database file */
2832 Pgno pgno, /* Page number to fetch */
2833 DbPage **ppPage, /* Write a pointer to the page here */
2834 int noContent /* Do not bother reading content from disk if true */
2835){
danielk19778c0a7912008-08-20 14:49:23 +00002836 PgHdr *pPg = 0;
drh165ffe92005-03-15 17:09:30 +00002837 int rc;
drhed7c8552001-04-11 14:29:21 +00002838
danielk19778c0a7912008-08-20 14:49:23 +00002839 assert( pPager->state==PAGER_UNLOCK
2840 || sqlite3PcacheRefCount(pPager->pPCache)>0
2841 || pgno==1
2842 );
danielk1977e277be02007-03-23 18:12:06 +00002843
danielk197726836652005-01-17 01:33:13 +00002844 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
2845 ** number greater than this, or zero, is requested.
2846 */
drh267cb322005-09-16 17:16:52 +00002847 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00002848 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00002849 }
2850
drhd9b02572001-04-15 00:37:09 +00002851 /* Make sure we have not hit any critical errors.
2852 */
drh836faa42003-01-11 13:30:57 +00002853 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00002854 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00002855
danielk197713adf8a2004-06-03 16:08:41 +00002856 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00002857 ** on the database file. pagerSharedLock() is a no-op if
2858 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00002859 */
danielk1977e277be02007-03-23 18:12:06 +00002860 rc = pagerSharedLock(pPager);
2861 if( rc!=SQLITE_OK ){
2862 return rc;
drhed7c8552001-04-11 14:29:21 +00002863 }
danielk1977e277be02007-03-23 18:12:06 +00002864 assert( pPager->state!=PAGER_UNLOCK );
2865
danielk19778c0a7912008-08-20 14:49:23 +00002866 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
2867 if( rc!=SQLITE_OK ){
2868 return rc;
2869 }
2870 if( pPager->errCode && pPg->nRef==1 ){
2871 sqlite3PcacheDrop(pPg);
2872 return pPager->errCode;
2873 }
2874 if( pPg->pPager==0 ){
2875 /* The pager cache has created a new page. Its content needs to
2876 ** be initialized.
2877 */
danielk1977979f38e2007-03-27 16:19:51 +00002878 int nMax;
drh538f5702007-04-13 02:14:30 +00002879 PAGER_INCR(pPager->nMiss);
danielk19778c0a7912008-08-20 14:49:23 +00002880 /* pPager->nRef++; */
2881 pPg->pPager = pPager;
2882 if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){
2883 pPg->flags |= PGHDR_IN_JOURNAL;
drhed7c8552001-04-11 14:29:21 +00002884 }
danielk19778c0a7912008-08-20 14:49:23 +00002885 memset(pPg->pExtra, 0, pPager->nExtra);
danielk197724168722007-04-02 05:07:47 +00002886
danielk1977ad0132d2008-06-07 08:58:22 +00002887 rc = sqlite3PagerPagecount(pPager, &nMax);
2888 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002889 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00002890 return rc;
2891 }
danielk197775bab7d2006-01-23 13:09:45 +00002892
drhd215acf2007-04-13 04:01:58 +00002893 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00002894 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00002895 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00002896 return SQLITE_FULL;
2897 }
danielk19778c0a7912008-08-20 14:49:23 +00002898 memset(pPg->pData, 0, pPager->pageSize);
2899 if( noContent && !pPager->alwaysRollback ){
2900 pPg->flags |= PGHDR_NEED_READ;
2901 }
drh538f5702007-04-13 02:14:30 +00002902 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00002903 }else{
danielk1977e180dd92007-04-05 17:15:52 +00002904 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00002905 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
danielk19778c0a7912008-08-20 14:49:23 +00002906 /* sqlite3PagerUnref(pPg); */
2907 pagerDropPage(pPg);
drh551b7732006-11-06 21:20:25 +00002908 return rc;
drh81a20f22001-10-12 17:30:04 +00002909 }
drh306dc212001-05-21 13:45:10 +00002910 }
danielk19773c407372005-02-15 02:54:14 +00002911#ifdef SQLITE_CHECK_PAGES
2912 pPg->pageHash = pager_pagehash(pPg);
2913#endif
drhed7c8552001-04-11 14:29:21 +00002914 }else{
drhd9b02572001-04-15 00:37:09 +00002915 /* The requested page is in the page cache. */
danielk19778c0a7912008-08-20 14:49:23 +00002916 assert(sqlite3PcacheRefCount(pPager->pPCache)>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00002917 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00002918 if( !noContent ){
2919 rc = pager_get_content(pPg);
2920 if( rc ){
danielk19778c0a7912008-08-20 14:49:23 +00002921 sqlite3PagerUnref(pPg);
drhd33d5a82007-04-26 12:11:28 +00002922 return rc;
2923 }
2924 }
drhed7c8552001-04-11 14:29:21 +00002925 }
danielk19778c0a7912008-08-20 14:49:23 +00002926
danielk19773b8a05f2007-03-19 17:44:26 +00002927 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00002928 return SQLITE_OK;
2929}
danielk19778c0a7912008-08-20 14:49:23 +00002930
drh86f8c192007-08-22 00:39:19 +00002931int sqlite3PagerAcquire(
2932 Pager *pPager, /* The pager open on the database file */
2933 Pgno pgno, /* Page number to fetch */
2934 DbPage **ppPage, /* Write a pointer to the page here */
2935 int noContent /* Do not bother reading content from disk if true */
2936){
2937 int rc;
2938 pagerEnter(pPager);
2939 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
2940 pagerLeave(pPager);
2941 return rc;
2942}
2943
drhed7c8552001-04-11 14:29:21 +00002944
2945/*
drh7e3b0a02001-04-28 16:52:40 +00002946** Acquire a page if it is already in the in-memory cache. Do
2947** not read the page from disk. Return a pointer to the page,
2948** or 0 if the page is not in cache.
2949**
danielk19773b8a05f2007-03-19 17:44:26 +00002950** See also sqlite3PagerGet(). The difference between this routine
2951** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00002952** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00002953** returns NULL if the page is not in cache or if a disk I/O error
2954** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00002955*/
danielk19773b8a05f2007-03-19 17:44:26 +00002956DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00002957 PgHdr *pPg = 0;
drh836faa42003-01-11 13:30:57 +00002958 assert( pPager!=0 );
2959 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00002960
drh86f8c192007-08-22 00:39:19 +00002961 pagerEnter(pPager);
danielk19778c0a7912008-08-20 14:49:23 +00002962 if( (pPager->state!=PAGER_UNLOCK)
2963 && (pPager->errCode==SQLITE_OK || pPager->errCode==SQLITE_FULL)
2964 ){
2965 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
danielk1977e277be02007-03-23 18:12:06 +00002966 }
drh86f8c192007-08-22 00:39:19 +00002967 pagerLeave(pPager);
danielk19778c0a7912008-08-20 14:49:23 +00002968
danielk19773b8a05f2007-03-19 17:44:26 +00002969 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00002970}
2971
2972/*
drhed7c8552001-04-11 14:29:21 +00002973** Release a page.
2974**
2975** If the number of references to the page drop to zero, then the
2976** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00002977** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00002978** removed.
2979*/
danielk19773b8a05f2007-03-19 17:44:26 +00002980int sqlite3PagerUnref(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00002981 if( pPg ){
2982 Pager *pPager = pPg->pPager;
2983 pagerEnter(pPager);
2984 sqlite3PcacheRelease(pPg);
2985 pagerUnlockIfUnused(pPager);
2986 pagerLeave(pPager);
drhed7c8552001-04-11 14:29:21 +00002987 }
drhd9b02572001-04-15 00:37:09 +00002988 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002989}
2990
2991/*
drha6abd042004-06-09 17:37:22 +00002992** Create a journal file for pPager. There should already be a RESERVED
2993** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00002994**
2995** Return SQLITE_OK if everything. Return an error code and release the
2996** write lock if anything goes wrong.
2997*/
2998static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00002999 sqlite3_vfs *pVfs = pPager->pVfs;
3000 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3001
drhda47d772002-12-02 04:25:19 +00003002 int rc;
drhb7f91642004-10-31 02:22:47 +00003003 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003004 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003005 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003006 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003007 sqlite3PagerPagecount(pPager, 0);
drhed138fb2007-08-22 22:04:37 +00003008 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003009 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00003010 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003011 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003012 rc = SQLITE_NOMEM;
3013 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003014 }
danielk1977b4b47412007-08-17 15:53:36 +00003015
drhfdc40e92008-04-17 20:59:37 +00003016 if( pPager->journalOpen==0 ){
3017 if( pPager->tempFile ){
3018 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3019 }else{
3020 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003021 }
drhfdc40e92008-04-17 20:59:37 +00003022#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3023 rc = sqlite3JournalOpen(
3024 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3025 );
3026#else
3027 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
3028#endif
3029 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3030 pPager->journalOff = 0;
3031 pPager->setMaster = 0;
3032 pPager->journalHdr = 0;
3033 if( rc!=SQLITE_OK ){
3034 if( rc==SQLITE_NOMEM ){
3035 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3036 }
3037 goto failed_to_open_journal;
3038 }
drhda47d772002-12-02 04:25:19 +00003039 }
3040 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003041 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003042 pPager->needSync = 0;
3043 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003044 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003045 if( pPager->errCode ){
3046 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003047 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003048 }
drhda47d772002-12-02 04:25:19 +00003049 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003050
danielk197776572402004-06-25 02:38:54 +00003051 rc = writeJournalHdr(pPager);
3052
drhac69b052004-05-12 13:30:07 +00003053 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003054 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003055 }
danielk1977ae72d982007-10-03 08:46:44 +00003056 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003057 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003058 if( rc==SQLITE_OK ){
3059 rc = SQLITE_FULL;
3060 }
3061 }
drh9c105bb2004-10-02 20:38:28 +00003062 return rc;
3063
3064failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003065 sqlite3BitvecDestroy(pPager->pInJournal);
3066 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003067 return rc;
drhda47d772002-12-02 04:25:19 +00003068}
3069
3070/*
drh4b845d72002-03-05 12:41:19 +00003071** Acquire a write-lock on the database. The lock is removed when
3072** the any of the following happen:
3073**
drh80e35f42007-03-30 14:06:34 +00003074** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003075** * sqlite3PagerRollback() is called.
3076** * sqlite3PagerClose() is called.
3077** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003078**
danielk197713adf8a2004-06-03 16:08:41 +00003079** The first parameter to this routine is a pointer to any open page of the
3080** database file. Nothing changes about the page - it is used merely to
3081** acquire a pointer to the Pager structure and as proof that there is
3082** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003083**
danielk197713adf8a2004-06-03 16:08:41 +00003084** The second parameter indicates how much space in bytes to reserve for a
3085** master journal file-name at the start of the journal when it is created.
3086**
3087** A journal file is opened if this is not a temporary file. For temporary
3088** files, the opening of the journal file is deferred until there is an
3089** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003090**
drha6abd042004-06-09 17:37:22 +00003091** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003092**
3093** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3094** immediately instead of waiting until we try to flush the cache. The
3095** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003096*/
danielk19773b8a05f2007-03-19 17:44:26 +00003097int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003098 Pager *pPager = pPg->pPager;
3099 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003100 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003101 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003102 assert( pPager->state!=PAGER_UNLOCK );
3103 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003104 assert( pPager->pInJournal==0 );
danielk19778c0a7912008-08-20 14:49:23 +00003105 sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_IN_JOURNAL);
drhb7f91642004-10-31 02:22:47 +00003106 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003107 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003108 pPager->origDbSize = pPager->dbSize;
3109 }else{
drh054889e2005-11-30 03:20:31 +00003110 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003111 if( rc==SQLITE_OK ){
3112 pPager->state = PAGER_RESERVED;
3113 if( exFlag ){
3114 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3115 }
3116 }
danielk19779eed5052004-06-04 10:38:30 +00003117 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003118 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003119 return rc;
drhac69b052004-05-12 13:30:07 +00003120 }
drha6abd042004-06-09 17:37:22 +00003121 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003122 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhfdc40e92008-04-17 20:59:37 +00003123 if( pPager->useJournal && !pPager->tempFile
3124 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drhac69b052004-05-12 13:30:07 +00003125 rc = pager_open_journal(pPager);
3126 }
drh4b845d72002-03-05 12:41:19 +00003127 }
danielk1977334cdb62007-03-26 08:05:12 +00003128 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00003129 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00003130 ** time a (read or write) transaction was successfully concluded
3131 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00003132 ** kept open and either was truncated to 0 bytes or its header was
3133 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00003134 */
3135 assert( pPager->nRec==0 );
3136 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003137 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003138 sqlite3PagerPagecount(pPager, 0);
drhf5e7bb52008-02-18 14:47:33 +00003139 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhf5e7bb52008-02-18 14:47:33 +00003140 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003141 rc = SQLITE_NOMEM;
3142 }else{
drh369339d2007-03-30 16:01:55 +00003143 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003144 rc = writeJournalHdr(pPager);
3145 }
drh4b845d72002-03-05 12:41:19 +00003146 }
danielk1977334cdb62007-03-26 08:05:12 +00003147 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00003148 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00003149 return rc;
3150}
3151
3152/*
drh605ad8f2006-05-03 23:34:05 +00003153** Make a page dirty. Set its dirty flag and add it to the dirty
3154** page list.
3155*/
3156static void makeDirty(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003157 sqlite3PcacheMakeDirty(pPg);
drh605ad8f2006-05-03 23:34:05 +00003158}
3159
3160/*
3161** Make a page clean. Clear its dirty bit and remove it from the
3162** dirty page list.
3163*/
3164static void makeClean(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003165 sqlite3PcacheMakeClean(pPg);
drh605ad8f2006-05-03 23:34:05 +00003166}
3167
3168
3169/*
drhed7c8552001-04-11 14:29:21 +00003170** Mark a data page as writeable. The page is written into the journal
3171** if it is not there already. This routine must be called before making
3172** changes to a page.
3173**
3174** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003175** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003176** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003177** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003178** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003179**
3180** If the journal file could not be written because the disk is full,
3181** then this routine returns SQLITE_FULL and does an immediate rollback.
3182** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003183** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003184** reset.
drhed7c8552001-04-11 14:29:21 +00003185*/
danielk19773b8a05f2007-03-19 17:44:26 +00003186static int pager_write(PgHdr *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003187 void *pData = pPg->pData;
drh69688d52001-04-14 16:38:23 +00003188 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003189 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003190
drh6446c4d2001-12-15 14:22:18 +00003191 /* Check for errors
3192 */
danielk1977efaaf572006-01-16 11:29:19 +00003193 if( pPager->errCode ){
3194 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003195 }
drh5e00f6c2001-09-13 13:46:56 +00003196 if( pPager->readOnly ){
3197 return SQLITE_PERM;
3198 }
drh6446c4d2001-12-15 14:22:18 +00003199
danielk197776572402004-06-25 02:38:54 +00003200 assert( !pPager->setMaster );
3201
danielk19773c407372005-02-15 02:54:14 +00003202 CHECK_PAGE(pPg);
3203
drh538f5702007-04-13 02:14:30 +00003204 /* If this page was previously acquired with noContent==1, that means
3205 ** we didn't really read in the content of the page. This can happen
3206 ** (for example) when the page is being moved to the freelist. But
3207 ** now we are (perhaps) moving the page off of the freelist for
3208 ** reuse and we need to know its original content so that content
3209 ** can be stored in the rollback journal. So do the read at this
3210 ** time.
3211 */
drhd33d5a82007-04-26 12:11:28 +00003212 rc = pager_get_content(pPg);
3213 if( rc ){
3214 return rc;
drh538f5702007-04-13 02:14:30 +00003215 }
3216
drh6446c4d2001-12-15 14:22:18 +00003217 /* Mark the page as dirty. If the page has already been written
3218 ** to the journal then we can return right away.
3219 */
drh605ad8f2006-05-03 23:34:05 +00003220 makeDirty(pPg);
danielk19778c0a7912008-08-20 14:49:23 +00003221 if( (pPg->flags&PGHDR_IN_JOURNAL)
3222 && (pageInStatement(pPg) || pPager->stmtInUse==0)
3223 ){
drha6abd042004-06-09 17:37:22 +00003224 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003225 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003226 }else{
drh6446c4d2001-12-15 14:22:18 +00003227
danielk1977a0bf2652004-11-04 14:30:04 +00003228 /* If we get this far, it means that the page needs to be
3229 ** written to the transaction journal or the ckeckpoint journal
3230 ** or both.
3231 **
3232 ** First check to see that the transaction journal exists and
3233 ** create it if it does not.
3234 */
3235 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003236 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003237 if( rc!=SQLITE_OK ){
3238 return rc;
3239 }
3240 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00003241 if( !pPager->journalOpen && pPager->useJournal
3242 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00003243 rc = pager_open_journal(pPager);
3244 if( rc!=SQLITE_OK ) return rc;
3245 }
danielk1977a0bf2652004-11-04 14:30:04 +00003246 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00003247 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003248
3249 /* The transaction journal now exists and we have a RESERVED or an
3250 ** EXCLUSIVE lock on the main database file. Write the current page to
3251 ** the transaction journal if it is not there already.
3252 */
danielk19778c0a7912008-08-20 14:49:23 +00003253 if( !(pPg->flags&PGHDR_IN_JOURNAL) && (pPager->journalOpen || MEMDB) ){
danielk1977a0bf2652004-11-04 14:30:04 +00003254 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00003255 if( MEMDB ){
drh4f0c5872007-03-26 22:05:01 +00003256 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003257 rc = sqlite3PcachePreserve(pPg, 0);
3258 if( rc!=SQLITE_OK ){
3259 return rc;
danielk1977a0bf2652004-11-04 14:30:04 +00003260 }
3261 }else{
drhbf4bca52007-09-06 22:19:14 +00003262 u32 cksum;
3263 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00003264
drh267cb322005-09-16 17:16:52 +00003265 /* We should never write to the journal file the page that
3266 ** contains the database locks. The following assert verifies
3267 ** that we do not. */
3268 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00003269 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00003270 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00003271 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
3272 if( rc==SQLITE_OK ){
3273 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
3274 pPager->journalOff + 4);
3275 pPager->journalOff += pPager->pageSize+4;
3276 }
3277 if( rc==SQLITE_OK ){
3278 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
3279 pPager->journalOff += 4;
3280 }
3281 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00003282 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00003283 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00003284 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
danielk19778c0a7912008-08-20 14:49:23 +00003285 PAGERID(pPager), pPg->pgno,
3286 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00003287
drhfd131da2007-08-07 17:13:03 +00003288 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00003289 ** transaction will be rolled back by the layer above.
3290 */
danielk1977a0bf2652004-11-04 14:30:04 +00003291 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00003292 return rc;
3293 }
danielk197707cb5602006-01-20 10:55:05 +00003294
danielk1977a0bf2652004-11-04 14:30:04 +00003295 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00003296 assert( pPager->pInJournal!=0 );
3297 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003298 if( !pPager->noSync ){
3299 pPg->flags |= PGHDR_NEED_SYNC;
3300 }
danielk1977a0bf2652004-11-04 14:30:04 +00003301 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00003302 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00003303 }
drhac69b052004-05-12 13:30:07 +00003304 }
danielk197713adf8a2004-06-03 16:08:41 +00003305 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003306 if( !pPager->journalStarted && !pPager->noSync ){
3307 pPg->flags |= PGHDR_NEED_SYNC;
3308 }
drh4f0c5872007-03-26 22:05:01 +00003309 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk19778c0a7912008-08-20 14:49:23 +00003310 PAGERID(pPager), pPg->pgno,
3311 ((pPg->flags&PGHDR_NEED_SYNC)?1:0));
danielk1977a0bf2652004-11-04 14:30:04 +00003312 }
danielk19778c0a7912008-08-20 14:49:23 +00003313 if( pPg->flags&PGHDR_NEED_SYNC ){
danielk1977a0bf2652004-11-04 14:30:04 +00003314 pPager->needSync = 1;
3315 }
danielk19778c0a7912008-08-20 14:49:23 +00003316 pPg->flags |= PGHDR_IN_JOURNAL;
danielk1977a0bf2652004-11-04 14:30:04 +00003317 }
3318
3319 /* If the statement journal is open and the page is not in it,
3320 ** then write the current page to the statement journal. Note that
3321 ** the statement journal format differs from the standard journal format
3322 ** in that it omits the checksums and the header.
3323 */
danielk1977f35843b2007-04-07 15:03:17 +00003324 if( pPager->stmtInUse
3325 && !pageInStatement(pPg)
3326 && (int)pPg->pgno<=pPager->stmtSize
3327 ){
danielk19778c0a7912008-08-20 14:49:23 +00003328 assert(
3329 (pPg->flags&PGHDR_IN_JOURNAL) || (int)pPg->pgno>pPager->origDbSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003330 if( MEMDB ){
danielk19778c0a7912008-08-20 14:49:23 +00003331 rc = sqlite3PcachePreserve(pPg, 1);
3332 if( rc!=SQLITE_OK ){
3333 return rc;
danielk1977a0bf2652004-11-04 14:30:04 +00003334 }
drh4f0c5872007-03-26 22:05:01 +00003335 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003336 /* page_add_to_stmt_list(pPg); */
danielk1977a0bf2652004-11-04 14:30:04 +00003337 }else{
danielk197762079062007-08-15 17:08:46 +00003338 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00003339 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
3340 rc = write32bits(pPager->stfd, offset, pPg->pgno);
3341 if( rc==SQLITE_OK ){
3342 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
3343 }
drh4f0c5872007-03-26 22:05:01 +00003344 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00003345 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00003346 return rc;
3347 }
danielk1977a0bf2652004-11-04 14:30:04 +00003348 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00003349 assert( pPager->pInStmt!=0 );
3350 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00003351 }
drhd9b02572001-04-15 00:37:09 +00003352 }
drhfa86c412002-02-02 15:01:15 +00003353 }
3354
3355 /* Update the database size and return.
3356 */
drh1aa2d8b2007-01-03 15:34:29 +00003357 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00003358 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00003359 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00003360 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00003361 pPager->dbSize++;
3362 }
drh306dc212001-05-21 13:45:10 +00003363 }
drh69688d52001-04-14 16:38:23 +00003364 return rc;
drhed7c8552001-04-11 14:29:21 +00003365}
3366
3367/*
danielk19774099f6e2007-03-19 11:25:20 +00003368** This function is used to mark a data-page as writable. It uses
3369** pager_write() to open a journal file (if it is not already open)
3370** and write the page *pData to the journal.
3371**
3372** The difference between this function and pager_write() is that this
3373** function also deals with the special case where 2 or more pages
3374** fit on a single disk sector. In this case all co-resident pages
3375** must have been written to the journal file before returning.
3376*/
danielk19773b8a05f2007-03-19 17:44:26 +00003377int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00003378 int rc = SQLITE_OK;
3379
danielk19773b8a05f2007-03-19 17:44:26 +00003380 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00003381 Pager *pPager = pPg->pPager;
3382 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3383
drh86f8c192007-08-22 00:39:19 +00003384 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003385 if( !MEMDB && nPagePerSector>1 ){
3386 Pgno nPageCount; /* Total number of pages in database file */
3387 Pgno pg1; /* First page of the sector pPg is located on. */
3388 int nPage; /* Number of pages starting at pg1 to journal */
3389 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00003390 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00003391
3392 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
3393 ** header to be written between the pages journaled by this function.
3394 */
3395 assert( pPager->doNotSync==0 );
3396 pPager->doNotSync = 1;
3397
3398 /* This trick assumes that both the page-size and sector-size are
3399 ** an integer power of 2. It sets variable pg1 to the identifier
3400 ** of the first page of the sector pPg is located on.
3401 */
3402 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3403
danielk1977ad0132d2008-06-07 08:58:22 +00003404 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00003405 if( pPg->pgno>nPageCount ){
3406 nPage = (pPg->pgno - pg1)+1;
3407 }else if( (pg1+nPagePerSector-1)>nPageCount ){
3408 nPage = nPageCount+1-pg1;
3409 }else{
3410 nPage = nPagePerSector;
3411 }
3412 assert(nPage>0);
3413 assert(pg1<=pPg->pgno);
3414 assert((pg1+nPage)>pPg->pgno);
3415
3416 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3417 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00003418 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00003419 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00003420 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00003421 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003422 if( rc==SQLITE_OK ){
3423 rc = pager_write(pPage);
danielk19778c0a7912008-08-20 14:49:23 +00003424 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003425 needSync = 1;
3426 }
danielk19773b8a05f2007-03-19 17:44:26 +00003427 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003428 }
3429 }
drhc81945e2008-03-10 14:12:53 +00003430 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk19778c0a7912008-08-20 14:49:23 +00003431 if( pPage->flags&PGHDR_NEED_SYNC ){
danielk1977dd97a492007-08-22 18:54:32 +00003432 needSync = 1;
3433 }
danielk19778c0a7912008-08-20 14:49:23 +00003434 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00003435 }
3436 }
3437
danielk1977dd97a492007-08-22 18:54:32 +00003438 /* If the PgHdr.needSync flag is set for any of the nPage pages
3439 ** starting at pg1, then it needs to be set for all of them. Because
3440 ** writing to any of these nPage pages may damage the others, the
3441 ** journal file must contain sync()ed copies of all of them
3442 ** before any of them can be written out to the database file.
3443 */
3444 if( needSync ){
3445 for(ii=0; ii<nPage && needSync; ii++){
3446 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
danielk19778c0a7912008-08-20 14:49:23 +00003447 if( pPage ) pPage->flags |= PGHDR_NEED_SYNC;
3448 sqlite3PagerUnref(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00003449 }
3450 assert(pPager->needSync);
3451 }
3452
danielk19774099f6e2007-03-19 11:25:20 +00003453 assert( pPager->doNotSync==1 );
3454 pPager->doNotSync = 0;
3455 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003456 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00003457 }
drh86f8c192007-08-22 00:39:19 +00003458 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00003459 return rc;
3460}
3461
3462/*
drhaacc5432002-01-06 17:07:40 +00003463** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00003464** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00003465** to change the content of the page.
3466*/
danielk19777d3a6662006-01-23 16:21:05 +00003467#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00003468int sqlite3PagerIswriteable(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00003469 return pPg->flags&PGHDR_DIRTY;
drh6019e162001-07-02 17:51:45 +00003470}
danielk19777d3a6662006-01-23 16:21:05 +00003471#endif
drh6019e162001-07-02 17:51:45 +00003472
drh001bbcb2003-03-19 03:14:00 +00003473/*
drh30e58752002-03-02 20:41:57 +00003474** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00003475** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00003476** that page might be marked as dirty.
3477**
3478** The overlying software layer calls this routine when all of the data
3479** on the given page is unused. The pager marks the page as clean so
3480** that it does not get written to disk.
3481**
3482** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00003483** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00003484** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00003485**
3486** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00003487** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00003488** will thereafter be ignored. This is necessary to avoid a problem
3489** where a page with data is added to the freelist during one part of
3490** a transaction then removed from the freelist during a later part
3491** of the same transaction and reused for some other purpose. When it
3492** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00003493** the sqlite3PagerDontRollback() routine is called. But because the
3494** page contains critical data, we still need to be sure it gets
3495** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00003496*/
drh538f5702007-04-13 02:14:30 +00003497void sqlite3PagerDontWrite(DbPage *pDbPage){
3498 PgHdr *pPg = pDbPage;
3499 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00003500
drhb7f91642004-10-31 02:22:47 +00003501 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00003502 pagerEnter(pPager);
danielk19778c0a7912008-08-20 14:49:23 +00003503 pPg->flags |= PGHDR_ALWAYS_ROLLBACK;
3504 if( (pPg->flags&PGHDR_DIRTY) && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00003505 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00003506 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
3507 /* If this pages is the last page in the file and the file has grown
3508 ** during the current transaction, then do NOT mark the page as clean.
3509 ** When the database file grows, we must make sure that the last page
3510 ** gets written at least once so that the disk file will be the correct
3511 ** size. If you do not write this page and the size of the file
3512 ** on the disk ends up being too small, that can lead to database
3513 ** corruption during the next transaction.
3514 */
3515 }else{
drh538f5702007-04-13 02:14:30 +00003516 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
3517 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00003518 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00003519#ifdef SQLITE_CHECK_PAGES
3520 pPg->pageHash = pager_pagehash(pPg);
3521#endif
drh8124a302002-06-25 14:43:57 +00003522 }
drh30e58752002-03-02 20:41:57 +00003523 }
drh86f8c192007-08-22 00:39:19 +00003524 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00003525}
3526
3527/*
3528** A call to this routine tells the pager that if a rollback occurs,
3529** it is not necessary to restore the data on the given page. This
3530** means that the pager does not have to record the given page in the
3531** rollback journal.
drh538f5702007-04-13 02:14:30 +00003532**
3533** If we have not yet actually read the content of this page (if
3534** the PgHdr.needRead flag is set) then this routine acts as a promise
3535** that we will never need to read the page content in the future.
3536** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00003537*/
danielk19773b8a05f2007-03-19 17:44:26 +00003538void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00003539 Pager *pPager = pPg->pPager;
3540
drh86f8c192007-08-22 00:39:19 +00003541 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00003542 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00003543
3544 /* If the journal file is not open, or DontWrite() has been called on
3545 ** this page (DontWrite() sets the alwaysRollback flag), then this
3546 ** function is a no-op.
3547 */
danielk19778c0a7912008-08-20 14:49:23 +00003548 if( pPager->journalOpen==0 || (pPg->flags&PGHDR_ALWAYS_ROLLBACK)
3549 || pPager->alwaysRollback
3550 ){
danielk197787c29a92008-01-18 11:33:16 +00003551 pagerLeave(pPager);
3552 return;
3553 }
danielk1977a55e9352008-01-21 13:04:34 +00003554 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
3555
drhc5d0bd92008-04-14 01:00:57 +00003556#ifdef SQLITE_SECURE_DELETE
3557 if( pPg->inJournal || (int)pPg->pgno > pPager->origDbSize ){
danielk19778c0a7912008-08-20 14:49:23 +00003558 pagerLeave(pPager);
drhc5d0bd92008-04-14 01:00:57 +00003559 return;
3560 }
3561#endif
3562
3563 /* If SECURE_DELETE is disabled, then there is no way that this
3564 ** routine can be called on a page for which sqlite3PagerDontWrite()
3565 ** has not been previously called during the same transaction.
3566 ** And if DontWrite() has previously been called, the following
3567 ** conditions must be met.
drh10131482008-07-11 03:34:09 +00003568 **
3569 ** (Later:) Not true. If the database is corrupted by having duplicate
3570 ** pages on the freelist (ex: corrupt9.test) then the following is not
3571 ** necessarily true:
danielk1977a55e9352008-01-21 13:04:34 +00003572 */
drh10131482008-07-11 03:34:09 +00003573 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); */
danielk1977a55e9352008-01-21 13:04:34 +00003574
drhf5e7bb52008-02-18 14:47:33 +00003575 assert( pPager->pInJournal!=0 );
3576 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk19778c0a7912008-08-20 14:49:23 +00003577 pPg->flags |= PGHDR_IN_JOURNAL;
3578 pPg->flags &= ~PGHDR_NEED_READ;
danielk1977a55e9352008-01-21 13:04:34 +00003579 if( pPager->stmtInUse ){
drhc5d0bd92008-04-14 01:00:57 +00003580 assert( pPager->stmtSize >= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00003581 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00003582 }
danielk1977a55e9352008-01-21 13:04:34 +00003583 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
3584 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh86f8c192007-08-22 00:39:19 +00003585 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00003586}
3587
drhac69b052004-05-12 13:30:07 +00003588
drh30e58752002-03-02 20:41:57 +00003589/*
drh80e35f42007-03-30 14:06:34 +00003590** This routine is called to increment the database file change-counter,
3591** stored at byte 24 of the pager file.
3592*/
danielk1977c7b60172007-08-22 11:22:03 +00003593static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00003594 PgHdr *pPgHdr;
3595 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00003596 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00003597
drh701bb3b2008-08-02 03:50:39 +00003598#ifndef SQLITE_ENABLE_ATOMIC_WRITE
3599 assert( isDirect==0 ); /* isDirect is only true for atomic writes */
3600#endif
drh80e35f42007-03-30 14:06:34 +00003601 if( !pPager->changeCountDone ){
3602 /* Open page 1 of the file for writing. */
3603 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
3604 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00003605
3606 if( !isDirect ){
3607 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00003608 if( rc!=SQLITE_OK ){
3609 sqlite3PagerUnref(pPgHdr);
3610 return rc;
3611 }
danielk1977c7b60172007-08-22 11:22:03 +00003612 }
3613
drh80e35f42007-03-30 14:06:34 +00003614 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00003615 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00003616 change_counter++;
danielk19778c0a7912008-08-20 14:49:23 +00003617 put32bits(((char*)pPgHdr->pData)+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00003618
drh701bb3b2008-08-02 03:50:39 +00003619#ifdef SQLITE_ENABLE_ATOMIC_WRITE
danielk1977c7b60172007-08-22 11:22:03 +00003620 if( isDirect && pPager->fd->pMethods ){
danielk19778c0a7912008-08-20 14:49:23 +00003621 const void *zBuf = pPgHdr->pData;
danielk1977c7b60172007-08-22 11:22:03 +00003622 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
3623 }
drh701bb3b2008-08-02 03:50:39 +00003624#endif
danielk1977c7b60172007-08-22 11:22:03 +00003625
drh80e35f42007-03-30 14:06:34 +00003626 /* Release the page reference. */
3627 sqlite3PagerUnref(pPgHdr);
3628 pPager->changeCountDone = 1;
3629 }
danielk1977c7b60172007-08-22 11:22:03 +00003630 return rc;
drh80e35f42007-03-30 14:06:34 +00003631}
3632
3633/*
danielk1977f653d782008-03-20 11:04:21 +00003634** Sync the pager file to disk.
3635*/
3636int sqlite3PagerSync(Pager *pPager){
3637 int rc;
3638 pagerEnter(pPager);
3639 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3640 pagerLeave(pPager);
3641 return rc;
3642}
3643
3644/*
drh80e35f42007-03-30 14:06:34 +00003645** Sync the database file for the pager pPager. zMaster points to the name
3646** of a master journal file that should be written into the individual
3647** journal file. zMaster may be NULL, which is interpreted as no master
3648** journal (a single database transaction).
3649**
3650** This routine ensures that the journal is synced, all dirty pages written
3651** to the database file and the database file synced. The only thing that
3652** remains to commit the transaction is to delete the journal file (or
3653** master journal file if specified).
3654**
3655** Note that if zMaster==NULL, this does not overwrite a previous value
3656** passed to an sqlite3PagerCommitPhaseOne() call.
3657**
3658** If parameter nTrunc is non-zero, then the pager file is truncated to
3659** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00003660**
3661** If the final parameter - noSync - is true, then the database file itself
3662** is not synced. The caller must call sqlite3PagerSync() directly to
3663** sync the database file before calling CommitPhaseTwo() to delete the
3664** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00003665*/
danielk1977f653d782008-03-20 11:04:21 +00003666int sqlite3PagerCommitPhaseOne(
3667 Pager *pPager,
3668 const char *zMaster,
3669 Pgno nTrunc,
3670 int noSync
3671){
drh80e35f42007-03-30 14:06:34 +00003672 int rc = SQLITE_OK;
3673
danielk1977dad31b52008-05-15 11:08:07 +00003674 if( pPager->errCode ){
3675 return pPager->errCode;
3676 }
3677
drhd138c012008-05-13 00:58:18 +00003678 /* If no changes have been made, we can leave the transaction early.
3679 */
3680 if( pPager->dbModified==0 &&
3681 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3682 pPager->exclusiveMode!=0) ){
3683 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3684 return SQLITE_OK;
3685 }
3686
drh80e35f42007-03-30 14:06:34 +00003687 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
3688 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00003689 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00003690
3691 /* If this is an in-memory db, or no pages have been written to, or this
3692 ** function has already been called, it is a no-op.
3693 */
3694 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
3695 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00003696
danielk1977c7b60172007-08-22 11:22:03 +00003697#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3698 /* The atomic-write optimization can be used if all of the
3699 ** following are true:
3700 **
3701 ** + The file-system supports the atomic-write property for
3702 ** blocks of size page-size, and
3703 ** + This commit is not part of a multi-file transaction, and
3704 ** + Exactly one page has been modified and store in the journal file.
3705 **
3706 ** If the optimization can be used, then the journal file will never
3707 ** be created for this transaction.
3708 */
danielk19778c0a7912008-08-20 14:49:23 +00003709 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
danielk1977f55b8992007-08-24 08:15:53 +00003710 int useAtomicWrite = (
3711 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00003712 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00003713 pPager->journalOff==jrnlBufferSize(pPager) &&
3714 nTrunc==0 &&
danielk19778c0a7912008-08-20 14:49:23 +00003715 (pPg==0 || pPg->pDirty==0)
danielk1977f55b8992007-08-24 08:15:53 +00003716 );
danielk1977700b9c52008-04-24 12:37:40 +00003717 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00003718 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00003719 /* Update the nRec field in the journal file. */
3720 int offset = pPager->journalHdr + sizeof(aJournalMagic);
3721 assert(pPager->nRec==1);
3722 rc = write32bits(pPager->jfd, offset, pPager->nRec);
3723
3724 /* Update the db file change counter. The following call will modify
3725 ** the in-memory representation of page 1 to include the updated
3726 ** change counter and then write page 1 directly to the database
3727 ** file. Because of the atomic-write property of the host file-system,
3728 ** this is safe.
3729 */
danielk1977ae72d982007-10-03 08:46:44 +00003730 if( rc==SQLITE_OK ){
3731 rc = pager_incr_changecounter(pPager, 1);
3732 }
danielk1977f55b8992007-08-24 08:15:53 +00003733 }else{
3734 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00003735 }
3736
danielk1977ae72d982007-10-03 08:46:44 +00003737 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00003738#endif
3739
drh80e35f42007-03-30 14:06:34 +00003740 /* If a master journal file name has already been written to the
3741 ** journal file, then no sync is required. This happens when it is
3742 ** written, then the process fails to upgrade from a RESERVED to an
3743 ** EXCLUSIVE lock. The next time the process tries to commit the
3744 ** transaction the m-j name will have already been written.
3745 */
3746 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00003747 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00003748 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00003749 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00003750#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197771aa7ff2008-05-20 07:05:09 +00003751 if( nTrunc!=0 ){
3752 /* If this transaction has made the database smaller, then all pages
3753 ** being discarded by the truncation must be written to the journal
3754 ** file.
3755 */
3756 Pgno i;
3757 int iSkip = PAGER_MJ_PGNO(pPager);
3758 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
3759 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
3760 rc = sqlite3PagerGet(pPager, i, &pPg);
3761 if( rc!=SQLITE_OK ) goto sync_exit;
3762 rc = sqlite3PagerWrite(pPg);
3763 sqlite3PagerUnref(pPg);
3764 if( rc!=SQLITE_OK ) goto sync_exit;
3765 }
3766 }
3767 }
drh80e35f42007-03-30 14:06:34 +00003768#endif
danielk197771aa7ff2008-05-20 07:05:09 +00003769 rc = writeMasterJournal(pPager, zMaster);
3770 if( rc!=SQLITE_OK ) goto sync_exit;
3771 rc = syncJournal(pPager);
3772 }
drh80e35f42007-03-30 14:06:34 +00003773 }
danielk1977c7b60172007-08-22 11:22:03 +00003774 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00003775
3776#ifndef SQLITE_OMIT_AUTOVACUUM
3777 if( nTrunc!=0 ){
3778 rc = sqlite3PagerTruncate(pPager, nTrunc);
3779 if( rc!=SQLITE_OK ) goto sync_exit;
3780 }
3781#endif
3782
3783 /* Write all dirty pages to the database file */
danielk19778c0a7912008-08-20 14:49:23 +00003784 pPg = sqlite3PcacheDirtyList(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003785 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00003786 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00003787 assert( rc!=SQLITE_IOERR_BLOCKED );
3788 /* The error might have left the dirty list all fouled up here,
3789 ** but that does not matter because if the if the dirty list did
3790 ** get corrupted, then the transaction will roll back and
3791 ** discard the dirty list. There is an assert in
3792 ** pager_get_all_dirty_pages() that verifies that no attempt
3793 ** is made to use an invalid dirty list.
3794 */
drh153c62c2007-08-24 03:51:33 +00003795 goto sync_exit;
3796 }
danielk19778c0a7912008-08-20 14:49:23 +00003797 sqlite3PcacheCleanAll(pPager->pPCache);
drh80e35f42007-03-30 14:06:34 +00003798
3799 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00003800 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00003801 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00003802 }
3803 IOTRACE(("DBSYNC %p\n", pPager))
3804
3805 pPager->state = PAGER_SYNCED;
3806 }else if( MEMDB && nTrunc!=0 ){
3807 rc = sqlite3PagerTruncate(pPager, nTrunc);
3808 }
3809
3810sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00003811 if( rc==SQLITE_IOERR_BLOCKED ){
3812 /* pager_incr_changecounter() may attempt to obtain an exclusive
3813 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00003814 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00003815 * better to return SQLITE_BUSY.
3816 */
3817 rc = SQLITE_BUSY;
3818 }
drh86f8c192007-08-22 00:39:19 +00003819 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00003820 return rc;
3821}
3822
3823
3824/*
drhed7c8552001-04-11 14:29:21 +00003825** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00003826**
3827** If the commit fails for any reason, a rollback attempt is made
3828** and an error code is returned. If the commit worked, SQLITE_OK
3829** is returned.
drhed7c8552001-04-11 14:29:21 +00003830*/
drh80e35f42007-03-30 14:06:34 +00003831int sqlite3PagerCommitPhaseTwo(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003832 int rc = SQLITE_OK;
drhd9b02572001-04-15 00:37:09 +00003833
danielk1977efaaf572006-01-16 11:29:19 +00003834 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00003835 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003836 }
drha6abd042004-06-09 17:37:22 +00003837 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00003838 return SQLITE_ERROR;
3839 }
drhd138c012008-05-13 00:58:18 +00003840 if( pPager->dbModified==0 &&
3841 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
3842 pPager->exclusiveMode!=0) ){
3843 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
3844 return SQLITE_OK;
3845 }
drh86f8c192007-08-22 00:39:19 +00003846 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00003847 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003848 if( MEMDB ){
danielk19778c0a7912008-08-20 14:49:23 +00003849 sqlite3PcacheCommit(pPager->pPCache, 0);
3850 sqlite3PcacheCleanAll(pPager->pPCache);
3851 sqlite3PcacheSetFlags(pPager->pPCache,
3852 ~(PGHDR_IN_JOURNAL | PGHDR_NEED_SYNC | PGHDR_ALWAYS_ROLLBACK), 0
3853 );
drha6abd042004-06-09 17:37:22 +00003854 pPager->state = PAGER_SHARED;
danielk19778c0a7912008-08-20 14:49:23 +00003855 }else{
3856 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
3857 rc = pager_end_transaction(pPager, pPager->setMaster);
3858 rc = pager_error(pPager, rc);
drhac69b052004-05-12 13:30:07 +00003859 }
drh86f8c192007-08-22 00:39:19 +00003860 pagerLeave(pPager);
3861 return rc;
drhed7c8552001-04-11 14:29:21 +00003862}
3863
3864/*
drha6abd042004-06-09 17:37:22 +00003865** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00003866** All in-memory cache pages revert to their original data contents.
3867** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00003868**
3869** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00003870** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00003871** process is writing trash into the journal file (SQLITE_CORRUPT) or
3872** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
3873** codes are returned for all these occasions. Otherwise,
3874** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00003875*/
danielk19773b8a05f2007-03-19 17:44:26 +00003876int sqlite3PagerRollback(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003877 int rc = SQLITE_OK;
drh4f0c5872007-03-26 22:05:01 +00003878 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
danielk19778c0a7912008-08-20 14:49:23 +00003879 pagerEnter(pPager);
drhb7f91642004-10-31 02:22:47 +00003880 if( MEMDB ){
danielk19778c0a7912008-08-20 14:49:23 +00003881 sqlite3PcacheRollback(pPager->pPCache, 1);
3882 sqlite3PcacheRollback(pPager->pPCache, 0);
3883 sqlite3PcacheCleanAll(pPager->pPCache);
3884 sqlite3PcacheSetFlags(pPager->pPCache,
3885 ~(PGHDR_IN_JOURNAL | PGHDR_NEED_SYNC | PGHDR_ALWAYS_ROLLBACK), 0
3886 );
drhac69b052004-05-12 13:30:07 +00003887 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00003888 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00003889 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00003890 pPager->state = PAGER_SHARED;
danielk19778c0a7912008-08-20 14:49:23 +00003891 }else if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00003892 rc = pager_end_transaction(pPager, pPager->setMaster);
danielk19778c0a7912008-08-20 14:49:23 +00003893 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00003894 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00003895 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00003896 }
danielk19778c0a7912008-08-20 14:49:23 +00003897 rc = pPager->errCode;
drha6abd042004-06-09 17:37:22 +00003898 }else{
danielk19778c0a7912008-08-20 14:49:23 +00003899 if( pPager->state==PAGER_RESERVED ){
3900 int rc2;
3901 rc = pager_playback(pPager, 0);
3902 rc2 = pager_end_transaction(pPager, pPager->setMaster);
3903 if( rc==SQLITE_OK ){
3904 rc = rc2;
3905 }
3906 }else{
3907 rc = pager_playback(pPager, 0);
3908 }
danielk197707cb5602006-01-20 10:55:05 +00003909
danielk19778c0a7912008-08-20 14:49:23 +00003910 pPager->dbSize = -1;
3911
3912 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
3913 ** cache. So call pager_error() on the way out to make any error
3914 ** persistent.
3915 */
3916 rc = pager_error(pPager, rc);
3917 }
drh86f8c192007-08-22 00:39:19 +00003918 pagerLeave(pPager);
3919 return rc;
drh98808ba2001-10-18 12:34:46 +00003920}
drhd9b02572001-04-15 00:37:09 +00003921
3922/*
drh5e00f6c2001-09-13 13:46:56 +00003923** Return TRUE if the database file is opened read-only. Return FALSE
3924** if the database is (in theory) writable.
3925*/
danielk19773b8a05f2007-03-19 17:44:26 +00003926int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00003927 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00003928}
3929
3930/*
drh0f7eb612006-08-08 13:51:43 +00003931** Return the number of references to the pager.
3932*/
danielk19773b8a05f2007-03-19 17:44:26 +00003933int sqlite3PagerRefcount(Pager *pPager){
danielk19778c0a7912008-08-20 14:49:23 +00003934 return sqlite3PcacheRefCount(pPager->pPCache);
drh0f7eb612006-08-08 13:51:43 +00003935}
3936
3937#ifdef SQLITE_TEST
3938/*
drhd9b02572001-04-15 00:37:09 +00003939** This routine is used for testing and analysis only.
3940*/
danielk19773b8a05f2007-03-19 17:44:26 +00003941int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00003942 static int a[11];
danielk19778c0a7912008-08-20 14:49:23 +00003943 pagerEnter(pPager);
3944 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
3945 a[1] = sqlite3PcachePagecount(pPager->pPCache);
3946 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
drhd9b02572001-04-15 00:37:09 +00003947 a[3] = pPager->dbSize;
3948 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00003949 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003950 a[6] = pPager->nHit;
3951 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00003952 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00003953 a[9] = pPager->nRead;
3954 a[10] = pPager->nWrite;
danielk19778c0a7912008-08-20 14:49:23 +00003955 pagerLeave(pPager);
drhd9b02572001-04-15 00:37:09 +00003956 return a;
3957}
danielk197717b90b52008-06-06 11:11:25 +00003958int sqlite3PagerIsMemdb(Pager *pPager){
3959 return MEMDB;
3960}
drh0f7eb612006-08-08 13:51:43 +00003961#endif
drhdd793422001-06-28 01:54:48 +00003962
drhfa86c412002-02-02 15:01:15 +00003963/*
drhac69b052004-05-12 13:30:07 +00003964** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00003965**
3966** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00003967** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00003968** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00003969*/
drh86f8c192007-08-22 00:39:19 +00003970static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00003971 int rc;
drhac69b052004-05-12 13:30:07 +00003972 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00003973 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00003974 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00003975 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00003976 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00003977 pPager->stmtInUse = 1;
3978 pPager->stmtSize = pPager->dbSize;
3979 return SQLITE_OK;
3980 }
drhda47d772002-12-02 04:25:19 +00003981 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00003982 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00003983 return SQLITE_OK;
3984 }
drhfa86c412002-02-02 15:01:15 +00003985 assert( pPager->journalOpen );
drhf5e7bb52008-02-18 14:47:33 +00003986 assert( pPager->pInStmt==0 );
3987 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhf5e7bb52008-02-18 14:47:33 +00003988 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003989 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00003990 return SQLITE_NOMEM;
3991 }
danielk197776572402004-06-25 02:38:54 +00003992 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00003993 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00003994 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00003995 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00003996 if( !pPager->stmtOpen ){
danielk197717b90b52008-06-06 11:11:25 +00003997 rc = sqlite3PagerOpentemp(pPager, pPager->stfd, SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00003998 if( rc ){
3999 goto stmt_begin_failed;
4000 }
drhac69b052004-05-12 13:30:07 +00004001 pPager->stmtOpen = 1;
4002 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004003 }
drhac69b052004-05-12 13:30:07 +00004004 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004005 return SQLITE_OK;
4006
drhac69b052004-05-12 13:30:07 +00004007stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00004008 if( pPager->pInStmt ){
4009 sqlite3BitvecDestroy(pPager->pInStmt);
4010 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004011 }
4012 return rc;
4013}
drh86f8c192007-08-22 00:39:19 +00004014int sqlite3PagerStmtBegin(Pager *pPager){
4015 int rc;
4016 pagerEnter(pPager);
4017 rc = pagerStmtBegin(pPager);
4018 pagerLeave(pPager);
4019 return rc;
4020}
drhfa86c412002-02-02 15:01:15 +00004021
4022/*
drhac69b052004-05-12 13:30:07 +00004023** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004024*/
danielk19773b8a05f2007-03-19 17:44:26 +00004025int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004026 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004027 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004028 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004029 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004030 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00004031 sqlite3BitvecDestroy(pPager->pInStmt);
4032 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004033 }else{
danielk19778c0a7912008-08-20 14:49:23 +00004034 sqlite3PcacheCommit(pPager->pPCache, 1);
drhac69b052004-05-12 13:30:07 +00004035 }
4036 pPager->stmtNRec = 0;
4037 pPager->stmtInUse = 0;
drh663fc632002-02-02 18:49:19 +00004038 }
drhac69b052004-05-12 13:30:07 +00004039 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004040 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004041 return SQLITE_OK;
4042}
4043
4044/*
drhac69b052004-05-12 13:30:07 +00004045** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004046*/
danielk19773b8a05f2007-03-19 17:44:26 +00004047int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004048 int rc;
drh86f8c192007-08-22 00:39:19 +00004049 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004050 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004051 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004052 if( MEMDB ){
danielk19778c0a7912008-08-20 14:49:23 +00004053 sqlite3PcacheRollback(pPager->pPCache, 1);
drhac69b052004-05-12 13:30:07 +00004054 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004055 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004056 rc = SQLITE_OK;
4057 }else{
4058 rc = pager_stmt_playback(pPager);
4059 }
danielk19773b8a05f2007-03-19 17:44:26 +00004060 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004061 }else{
4062 rc = SQLITE_OK;
4063 }
drhac69b052004-05-12 13:30:07 +00004064 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004065 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004066 return rc;
4067}
4068
drh73509ee2003-04-06 20:44:45 +00004069/*
4070** Return the full pathname of the database file.
4071*/
danielk19773b8a05f2007-03-19 17:44:26 +00004072const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004073 return pPager->zFilename;
4074}
4075
drhb20ea9d2004-02-09 01:20:36 +00004076/*
drhd0679ed2007-08-28 22:24:34 +00004077** Return the VFS structure for the pager.
4078*/
4079const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4080 return pPager->pVfs;
4081}
4082
4083/*
drhcc6bb3e2007-08-31 16:11:35 +00004084** Return the file handle for the database file associated
4085** with the pager. This might return NULL if the file has
4086** not yet been opened.
4087*/
4088sqlite3_file *sqlite3PagerFile(Pager *pPager){
4089 return pPager->fd;
4090}
4091
4092/*
danielk19775865e3d2004-06-14 06:03:57 +00004093** Return the directory of the database file.
4094*/
danielk19773b8a05f2007-03-19 17:44:26 +00004095const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004096 return pPager->zDirectory;
4097}
4098
4099/*
4100** Return the full pathname of the journal file.
4101*/
danielk19773b8a05f2007-03-19 17:44:26 +00004102const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004103 return pPager->zJournal;
4104}
4105
4106/*
drh2c8997b2005-08-27 16:36:48 +00004107** Return true if fsync() calls are disabled for this pager. Return FALSE
4108** if fsync()s are executed normally.
4109*/
danielk19773b8a05f2007-03-19 17:44:26 +00004110int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004111 return pPager->noSync;
4112}
4113
drh7c4ac0c2007-04-05 11:25:58 +00004114#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004115/*
drhb20ea9d2004-02-09 01:20:36 +00004116** Set the codec for this pager
4117*/
danielk19773b8a05f2007-03-19 17:44:26 +00004118void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004119 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004120 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004121 void *pCodecArg
4122){
4123 pPager->xCodec = xCodec;
4124 pPager->pCodecArg = pCodecArg;
4125}
drh7c4ac0c2007-04-05 11:25:58 +00004126#endif
drhb20ea9d2004-02-09 01:20:36 +00004127
danielk1977687566d2004-11-02 12:56:41 +00004128#ifndef SQLITE_OMIT_AUTOVACUUM
4129/*
danielk197787c29a92008-01-18 11:33:16 +00004130** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004131**
drh5e385312007-06-16 04:42:12 +00004132** There must be no references to the page previously located at
4133** pgno (which we call pPgOld) though that page is allowed to be
4134** in cache. If the page previous located at pgno is not already
4135** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004136**
drh5e385312007-06-16 04:42:12 +00004137** References to the page pPg remain valid. Updating any
4138** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004139** allocated along with the page) is the responsibility of the caller.
4140**
danielk19775fd057a2005-03-09 13:09:43 +00004141** A transaction must be active when this routine is called. It used to be
4142** required that a statement transaction was not active, but this restriction
4143** has been removed (CREATE INDEX needs to move a page when a statement
4144** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00004145**
4146** If the fourth argument, isCommit, is non-zero, then this page is being
4147** moved as part of a database reorganization just before the transaction
4148** is being committed. In this case, it is guaranteed that the database page
4149** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00004150*/
danielk19774c999992008-07-16 18:17:55 +00004151int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00004152 PgHdr *pPgOld; /* The page being overwritten. */
danielk197794daf7f2004-11-08 09:26:09 +00004153 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004154
danielk1977687566d2004-11-02 12:56:41 +00004155 assert( pPg->nRef>0 );
4156
drh4f0c5872007-03-26 22:05:01 +00004157 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk19778c0a7912008-08-20 14:49:23 +00004158 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno);
drhb0603412007-02-28 04:47:26 +00004159 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004160
danielk19778c0a7912008-08-20 14:49:23 +00004161 pagerEnter(pPager);
4162
danielk1977b4626a32007-04-28 15:47:43 +00004163 pager_get_content(pPg);
danielk19774c999992008-07-16 18:17:55 +00004164
4165 /* If the journal needs to be sync()ed before page pPg->pgno can
4166 ** be written to, store pPg->pgno in local variable needSyncPgno.
4167 **
4168 ** If the isCommit flag is set, there is no need to remember that
4169 ** the journal needs to be sync()ed before database page pPg->pgno
4170 ** can be written to. The caller has already promised not to write to it.
4171 */
danielk19778c0a7912008-08-20 14:49:23 +00004172 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00004173 needSyncPgno = pPg->pgno;
danielk19778c0a7912008-08-20 14:49:23 +00004174 assert( (pPg->flags&PGHDR_IN_JOURNAL) || (int)pgno>pPager->origDbSize );
4175 assert( pPg->flags&PGHDR_DIRTY );
danielk1977ae825582004-11-23 09:06:55 +00004176 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004177 }
4178
danielk1977ef73ee92004-11-06 12:26:07 +00004179 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00004180 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00004181 ** page pgno before the 'move' operation, it needs to be retained
4182 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004183 */
danielk19778c0a7912008-08-20 14:49:23 +00004184 pPg->flags &= ~(PGHDR_NEED_SYNC|PGHDR_IN_JOURNAL);
danielk1977687566d2004-11-02 12:56:41 +00004185 pPgOld = pager_lookup(pPager, pgno);
danielk19778c0a7912008-08-20 14:49:23 +00004186 assert( !pPgOld || pPgOld->nRef==1 );
danielk1977687566d2004-11-02 12:56:41 +00004187 if( pPgOld ){
danielk19778c0a7912008-08-20 14:49:23 +00004188 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
drh5e385312007-06-16 04:42:12 +00004189 }
danielk19778c0a7912008-08-20 14:49:23 +00004190 if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){
4191 pPg->flags |= PGHDR_IN_JOURNAL;
4192 }
danielk1977687566d2004-11-02 12:56:41 +00004193
danielk19778c0a7912008-08-20 14:49:23 +00004194 sqlite3PcacheMove(pPg, pgno);
4195 if( pPgOld ){
4196 sqlite3PcacheMove(pPgOld, 0);
4197 sqlite3PcacheRelease(pPgOld);
danielk1977f5fdda82004-11-03 08:44:05 +00004198 }
danielk1977f5fdda82004-11-03 08:44:05 +00004199
drh605ad8f2006-05-03 23:34:05 +00004200 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004201 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004202 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00004203
danielk197794daf7f2004-11-08 09:26:09 +00004204 if( needSyncPgno ){
4205 /* If needSyncPgno is non-zero, then the journal file needs to be
4206 ** sync()ed before any data is written to database file page needSyncPgno.
4207 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00004208 ** "is journaled" bitvec flag has been set. This needs to be remedied by
4209 ** loading the page into the pager-cache and setting the PgHdr.needSync
4210 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00004211 **
danielk1977a98d7b42008-01-18 13:42:54 +00004212 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00004213 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00004214 ** array. Otherwise, if the page is loaded and written again in
4215 ** this transaction, it may be written to the database file before
4216 ** it is synced into the journal file. This way, it may end up in
4217 ** the journal file twice, but that is not a problem.
4218 **
danielk19773b8a05f2007-03-19 17:44:26 +00004219 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004220 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004221 */
4222 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004223 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004224 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004225 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00004226 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00004227 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
4228 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00004229 }
danielk197787c29a92008-01-18 11:33:16 +00004230 pagerLeave(pPager);
4231 return rc;
4232 }
danielk1977ae825582004-11-23 09:06:55 +00004233 pPager->needSync = 1;
danielk19778c0a7912008-08-20 14:49:23 +00004234 pPgHdr->flags |= PGHDR_NEED_SYNC;
4235 pPgHdr->flags |= PGHDR_IN_JOURNAL;
danielk19773b8a05f2007-03-19 17:44:26 +00004236 makeDirty(pPgHdr);
4237 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004238 }
4239
drh86f8c192007-08-22 00:39:19 +00004240 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004241 return SQLITE_OK;
4242}
4243#endif
4244
danielk19773b8a05f2007-03-19 17:44:26 +00004245/*
4246** Return a pointer to the data for the specified page.
4247*/
4248void *sqlite3PagerGetData(DbPage *pPg){
danielk19778c0a7912008-08-20 14:49:23 +00004249 assert( pPg->nRef>0 );
4250 return pPg->pData;
danielk19773b8a05f2007-03-19 17:44:26 +00004251}
4252
4253/*
4254** Return a pointer to the Pager.nExtra bytes of "extra" space
4255** allocated along with the specified page.
4256*/
4257void *sqlite3PagerGetExtra(DbPage *pPg){
4258 Pager *pPager = pPg->pPager;
danielk19778c0a7912008-08-20 14:49:23 +00004259 return (pPager?pPg->pExtra:0);
danielk19773b8a05f2007-03-19 17:44:26 +00004260}
4261
danielk197741483462007-03-24 16:45:04 +00004262/*
4263** Get/set the locking-mode for this pager. Parameter eMode must be one
4264** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4265** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4266** the locking-mode is set to the value specified.
4267**
4268** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4269** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4270** locking-mode.
4271*/
4272int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004273 assert( eMode==PAGER_LOCKINGMODE_QUERY
4274 || eMode==PAGER_LOCKINGMODE_NORMAL
4275 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4276 assert( PAGER_LOCKINGMODE_QUERY<0 );
4277 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4278 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00004279 pPager->exclusiveMode = eMode;
4280 }
4281 return (int)pPager->exclusiveMode;
4282}
4283
drh3b020132008-04-17 17:02:01 +00004284/*
4285** Get/set the journal-mode for this pager. Parameter eMode must be one
4286** of PAGER_JOURNALMODE_QUERY, PAGER_JOURNALMODE_DELETE or
4287** PAGER_JOURNALMODE_PERSIST. If the parameter is not _QUERY, then
4288** the journal-mode is set to the value specified.
4289**
4290** The returned value is either PAGER_JOURNALMODE_DELETE or
4291** PAGER_JOURNALMODE_PERSIST, indicating the current (possibly updated)
4292** journal-mode.
4293*/
4294int sqlite3PagerJournalMode(Pager *pPager, int eMode){
4295 assert( eMode==PAGER_JOURNALMODE_QUERY
4296 || eMode==PAGER_JOURNALMODE_DELETE
drhfdc40e92008-04-17 20:59:37 +00004297 || eMode==PAGER_JOURNALMODE_PERSIST
4298 || eMode==PAGER_JOURNALMODE_OFF );
drh3b020132008-04-17 17:02:01 +00004299 assert( PAGER_JOURNALMODE_QUERY<0 );
4300 assert( PAGER_JOURNALMODE_DELETE>=0 && PAGER_JOURNALMODE_PERSIST>=0 );
drhfdc40e92008-04-17 20:59:37 +00004301 if( eMode>=0 ){
4302 pPager->journalMode = eMode;
drh3b020132008-04-17 17:02:01 +00004303 }
drhfdc40e92008-04-17 20:59:37 +00004304 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00004305}
4306
danielk1977b53e4962008-06-04 06:45:59 +00004307/*
4308** Get/set the size-limit used for persistent journal files.
4309*/
4310i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
4311 if( iLimit>=-1 ){
4312 pPager->journalSizeLimit = iLimit;
4313 }
4314 return pPager->journalSizeLimit;
4315}
4316
danielk19778c0a7912008-08-20 14:49:23 +00004317void sqlite3PagerAlwaysRollback(Pager *pPager){
4318 pPager->alwaysRollback = 1;
4319}
4320
drh2e66f0b2005-04-28 17:18:48 +00004321#endif /* SQLITE_OMIT_DISKIO */