blob: b1e08e848af8e8c3a62f0754e477cd6d2b33b23b [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**
drhfacf0302008-06-17 15:12:00 +000021** @(#) $Id: pager.c,v 1.458 2008/06/17 15:12:01 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000026#include <string.h>
drhed7c8552001-04-11 14:29:21 +000027
28/*
drhdb48ee02003-01-16 13:42:43 +000029** Macros for troubleshooting. Normally turned off
30*/
danielk1977466be562004-06-10 02:16:01 +000031#if 0
drhd3627af2006-12-18 18:34:51 +000032#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000033#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
34#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
35#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
36#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
37#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000038#else
drh4f0c5872007-03-26 22:05:01 +000039#define PAGERTRACE1(X)
40#define PAGERTRACE2(X,Y)
41#define PAGERTRACE3(X,Y,Z)
42#define PAGERTRACE4(X,Y,Z,W)
43#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000044#endif
45
danielk1977599fcba2004-11-08 07:13:13 +000046/*
drh4f0c5872007-03-26 22:05:01 +000047** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000048** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000049**
drh85b623f2007-12-13 21:54:09 +000050** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000051** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000052** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000053*/
drhc001c582006-03-06 18:23:16 +000054#define PAGERID(p) ((int)(p->fd))
55#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000056
57/*
drhed7c8552001-04-11 14:29:21 +000058** The page cache as a whole is always in one of the following
59** states:
60**
drha6abd042004-06-09 17:37:22 +000061** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000062** writing the database file. There is no
63** data held in memory. This is the initial
64** state.
65**
drha6abd042004-06-09 17:37:22 +000066** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000067** Writing is not permitted. There can be
68** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000069** file at the same time.
drhed7c8552001-04-11 14:29:21 +000070**
drh726de592004-06-10 23:35:50 +000071** PAGER_RESERVED This process has reserved the database for writing
72** but has not yet made any changes. Only one process
73** at a time can reserve the database. The original
74** database file has not been modified so other
75** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000076** database file.
77**
78** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000079** Access is exclusive. No other processes or
80** threads can be reading or writing while one
81** process is writing.
82**
danielk1977aa5ccdf2004-06-14 05:10:42 +000083** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
84** after all dirty pages have been written to the
85** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000086** disk. All that remains to do is to remove or
87** truncate the journal file and the transaction
88** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000089**
drha6abd042004-06-09 17:37:22 +000090** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000091** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000092** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000093** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000094** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000095** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000096** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000097** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000098** PAGER_RESERVED means that there is an open rollback journal.
99** The transition to PAGER_EXCLUSIVE occurs before any changes
100** are made to the database file, though writes to the rollback
101** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
102** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
103** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000104*/
drha6abd042004-06-09 17:37:22 +0000105#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000106#define PAGER_SHARED 1 /* same as SHARED_LOCK */
107#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
108#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
109#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000110
drh684917c2004-10-05 02:41:42 +0000111/*
112** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
113** then failed attempts to get a reserved lock will invoke the busy callback.
114** This is off by default. To see why, consider the following scenario:
115**
116** Suppose thread A already has a shared lock and wants a reserved lock.
117** Thread B already has a reserved lock and wants an exclusive lock. If
118** both threads are using their busy callbacks, it might be a long time
119** be for one of the threads give up and allows the other to proceed.
120** But if the thread trying to get the reserved lock gives up quickly
121** (if it never invokes its busy callback) then the contention will be
122** resolved quickly.
123*/
124#ifndef SQLITE_BUSY_RESERVED_LOCK
125# define SQLITE_BUSY_RESERVED_LOCK 0
126#endif
drhd9b02572001-04-15 00:37:09 +0000127
drhed7c8552001-04-11 14:29:21 +0000128/*
drh887dc4c2004-10-22 16:22:57 +0000129** This macro rounds values up so that if the value is an address it
130** is guaranteed to be an address that is aligned to an 8-byte boundary.
131*/
132#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
133
danielk19779f61c2f2007-08-27 17:27:49 +0000134typedef struct PgHdr PgHdr;
danielk197784f786f2007-08-28 08:00:17 +0000135
136/*
137** Each pager stores all currently unreferenced pages in a list sorted
138** in least-recently-used (LRU) order (i.e. the first item on the list has
139** not been referenced in a long time, the last item has been recently
140** used). An instance of this structure is included as part of each
141** pager structure for this purpose (variable Pager.lru).
142**
143** Additionally, if memory-management is enabled, all unreferenced pages
144** are stored in a global LRU list (global variable sqlite3LruPageList).
145**
146** In both cases, the PagerLruList.pFirstSynced variable points to
147** the first page in the corresponding list that does not require an
drh85b623f2007-12-13 21:54:09 +0000148** fsync() operation before its memory can be reclaimed. If no such
danielk197784f786f2007-08-28 08:00:17 +0000149** page exists, PagerLruList.pFirstSynced is set to NULL.
150*/
151typedef struct PagerLruList PagerLruList;
152struct PagerLruList {
153 PgHdr *pFirst; /* First page in LRU list */
154 PgHdr *pLast; /* Last page in LRU list (the most recently used) */
155 PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0 */
156};
157
158/*
159** The following structure contains the next and previous pointers used
160** to link a PgHdr structure into a PagerLruList linked list.
161*/
danielk19779f61c2f2007-08-27 17:27:49 +0000162typedef struct PagerLruLink PagerLruLink;
163struct PagerLruLink {
164 PgHdr *pNext;
165 PgHdr *pPrev;
166};
167
drh887dc4c2004-10-22 16:22:57 +0000168/*
drhed7c8552001-04-11 14:29:21 +0000169** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +0000170** This header is only visible to this pager module. The client
171** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +0000172**
danielk19773b8a05f2007-03-19 17:44:26 +0000173** Client code should call sqlite3PagerWrite() on a page prior to making
174** any modifications to that page. The first time sqlite3PagerWrite()
drhf6038712004-02-08 18:07:34 +0000175** is called, the original page contents are written into the rollback
176** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
177** the journal page has made it onto the disk surface, PgHdr.needSync
178** is cleared. The modified page cannot be written back into the original
179** database file until the journal pages has been synced to disk and the
180** PgHdr.needSync has been cleared.
181**
danielk19773b8a05f2007-03-19 17:44:26 +0000182** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
drhf6038712004-02-08 18:07:34 +0000183** is cleared again when the page content is written back to the original
184** database file.
drh732c8172007-06-16 11:17:45 +0000185**
186** Details of important structure elements:
187**
188** needSync
189**
190** If this is true, this means that it is not safe to write the page
191** content to the database because the original content needed
192** for rollback has not by synced to the main rollback journal.
193** The original content may have been written to the rollback journal
194** but it has not yet been synced. So we cannot write to the database
195** file because power failure might cause the page in the journal file
196** to never reach the disk. It is as if the write to the journal file
197** does not occur until the journal file is synced.
198**
199** This flag is false if the page content exactly matches what
200** currently exists in the database file. The needSync flag is also
201** false if the original content has been written to the main rollback
202** journal and synced. If the page represents a new page that has
203** been added onto the end of the database during the current
204** transaction, the needSync flag is true until the original database
205** size in the journal header has been synced to disk.
206**
207** inJournal
208**
209** This is true if the original page has been written into the main
210** rollback journal. This is always false for new pages added to
211** the end of the database file during the current transaction.
212** And this flag says nothing about whether or not the journal
213** has been synced to disk. For pages that are in the original
214** database file, the following expression should always be true:
215**
drhf5e7bb52008-02-18 14:47:33 +0000216** inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno)
drh732c8172007-06-16 11:17:45 +0000217**
drhf5e7bb52008-02-18 14:47:33 +0000218** The pPager->pInJournal object is only valid for the original
drh732c8172007-06-16 11:17:45 +0000219** pages of the database, not new pages that are added to the end
220** of the database, so obviously the above expression cannot be
221** valid for new pages. For new pages inJournal is always 0.
222**
223** dirty
224**
225** When true, this means that the content of the page has been
226** modified and needs to be written back to the database file.
227** If false, it means that either the content of the page is
228** unchanged or else the content is unimportant and we do not
229** care whether or not it is preserved.
230**
231** alwaysRollback
232**
233** This means that the sqlite3PagerDontRollback() API should be
234** ignored for this page. The DontRollback() API attempts to say
235** that the content of the page on disk is unimportant (it is an
236** unused page on the freelist) so that it is unnecessary to
237** rollback changes to this page because the content of the page
238** can change without changing the meaning of the database. This
239** flag overrides any DontRollback() attempt. This flag is set
240** when a page that originally contained valid data is added to
241** the freelist. Later in the same transaction, this page might
242** be pulled from the freelist and reused for something different
243** and at that point the DontRollback() API will be called because
244** pages taken from the freelist do not need to be protected by
245** the rollback journal. But this flag says that the page was
246** not originally part of the freelist so that it still needs to
247** be rolled back in spite of any subsequent DontRollback() calls.
248**
249** needRead
250**
251** This flag means (when true) that the content of the page has
252** not yet been loaded from disk. The in-memory content is just
253** garbage. (Actually, we zero the content, but you should not
254** make any assumptions about the content nevertheless.) If the
255** content is needed in the future, it should be read from the
256** original database file.
drhed7c8552001-04-11 14:29:21 +0000257*/
258struct PgHdr {
259 Pager *pPager; /* The pager to which this page belongs */
260 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000261 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
danielk19779f61c2f2007-08-27 17:27:49 +0000262 PagerLruLink free; /* Next and previous free pages */
drhac69b052004-05-12 13:30:07 +0000263 PgHdr *pNextAll; /* A list of all pages */
drh193a6b42002-07-07 16:52:46 +0000264 u8 inJournal; /* TRUE if has been written to journal */
drh193a6b42002-07-07 16:52:46 +0000265 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000266 u8 needSync; /* Sync journal before writing this page */
drh80e35f42007-03-30 14:06:34 +0000267 u8 alwaysRollback; /* Disable DontRollback() for this page */
drh538f5702007-04-13 02:14:30 +0000268 u8 needRead; /* Read content if PagerWrite() is called */
drhac69b052004-05-12 13:30:07 +0000269 short int nRef; /* Number of users of this page */
drhdc3e10b2006-06-15 14:31:06 +0000270 PgHdr *pDirty, *pPrevDirty; /* Dirty pages */
danielk19779f61c2f2007-08-27 17:27:49 +0000271#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
272 PagerLruLink gfree; /* Global list of nRef==0 pages */
273#endif
danielk19773c407372005-02-15 02:54:14 +0000274#ifdef SQLITE_CHECK_PAGES
275 u32 pageHash;
276#endif
drhbf4bca52007-09-06 22:19:14 +0000277 void *pData; /* Page data */
278 /* Pager.nExtra bytes of local data appended to this header */
drhed7c8552001-04-11 14:29:21 +0000279};
280
drhac69b052004-05-12 13:30:07 +0000281/*
282** For an in-memory only database, some extra information is recorded about
283** each page so that changes can be rolled back. (Journal files are not
284** used for in-memory databases.) The following information is added to
285** the end of every EXTRA block for in-memory databases.
286**
287** This information could have been added directly to the PgHdr structure.
288** But then it would take up an extra 8 bytes of storage on every PgHdr
289** even for disk-based databases. Splitting it out saves 8 bytes. This
290** is only a savings of 0.8% but those percentages add up.
291*/
292typedef struct PgHistory PgHistory;
293struct PgHistory {
294 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
295 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000296 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
297 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000298};
drh9eb9e262004-02-11 02:18:05 +0000299
300/*
301** A macro used for invoking the codec if there is one
302*/
303#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000304# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
305# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000306#else
drhc001c582006-03-06 18:23:16 +0000307# define CODEC1(P,D,N,X) /* NO-OP */
308# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000309#endif
310
drhed7c8552001-04-11 14:29:21 +0000311/*
drh69688d52001-04-14 16:38:23 +0000312** Convert a pointer to a PgHdr into a pointer to its data
313** and back again.
drhed7c8552001-04-11 14:29:21 +0000314*/
drhbf4bca52007-09-06 22:19:14 +0000315#define PGHDR_TO_DATA(P) ((P)->pData)
316#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
drhac69b052004-05-12 13:30:07 +0000317#define PGHDR_TO_HIST(P,PGR) \
drhbf4bca52007-09-06 22:19:14 +0000318 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000319
320/*
drhed7c8552001-04-11 14:29:21 +0000321** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000322**
drh4f0ee682007-03-30 20:43:40 +0000323** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000324** or SQLITE_FULL. Once one of the first three errors occurs, it persists
325** and is returned as the result of every major pager API call. The
326** SQLITE_FULL return code is slightly different. It persists only until the
327** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000328** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000329** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000330*/
331struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000332 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000333 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000334 u8 journalStarted; /* True if header of journal is synced */
335 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000336 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000337 u8 stmtOpen; /* True if the statement subjournal is open */
338 u8 stmtInUse; /* True we are in a statement subtransaction */
339 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000340 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000341 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000342 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000343 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000344 u8 tempFile; /* zFilename is a temporary file */
345 u8 readOnly; /* True for a read-only database */
346 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000347 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000348 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000349 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000350 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000351 u8 doNotSync; /* Boolean. While true, do not spill the cache */
352 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000353 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drhd138c012008-05-13 00:58:18 +0000354 u8 dbModified; /* True if there are any changes to the Db */
drh80e35f42007-03-30 14:06:34 +0000355 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000356 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000357 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000358 int dbSize; /* Number of pages in the file */
359 int origDbSize; /* dbSize before the current change */
360 int stmtSize; /* Size of database (in pages) at stmt_begin() */
361 int nRec; /* Number of pages written to the journal */
362 u32 cksumInit; /* Quasi-random value added to every checksum */
363 int stmtNRec; /* Number of records in stmt subjournal */
364 int nExtra; /* Add this many bytes to each in-memory page */
365 int pageSize; /* Number of bytes in a page */
366 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000367 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
368 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000369 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000370 Bitvec *pInJournal; /* One bit for each page in the database file */
371 Bitvec *pInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000372 char *zFilename; /* Name of the database file */
373 char *zJournal; /* Name of the journal file */
374 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000375 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
376 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000377 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
danielk19779f61c2f2007-08-27 17:27:49 +0000378 PagerLruList lru; /* LRU list of free pages */
drhd9b02572001-04-15 00:37:09 +0000379 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000380 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000381 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000382 i64 journalOff; /* Current byte offset in the journal file */
383 i64 journalHdr; /* Byte offset to previous journal header */
384 i64 stmtHdrOff; /* First journal header written this statement */
385 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000386 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000387 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000388#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000389 int nHit, nMiss; /* Cache hits and missing */
390 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000391#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000392 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
393 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000394#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000395 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000396 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000397#endif
drh8ca0c722006-05-07 17:49:38 +0000398 int nHash; /* Size of the pager hash table */
399 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000400#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +0000401 Pager *pNext; /* Doubly linked list of pagers on which */
402 Pager *pPrev; /* sqlite3_release_memory() will work */
drh80105af2008-05-21 15:38:14 +0000403 volatile int iInUseMM; /* Non-zero if unavailable to MM */
404 volatile int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
danielk197713f72992005-12-18 08:51:22 +0000405#endif
danielk19778186df82007-03-06 13:45:59 +0000406 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000407 char dbFileVers[16]; /* Changes whenever database file changes */
danielk1977b53e4962008-06-04 06:45:59 +0000408 i64 journalSizeLimit; /* Size limit for persistent journal files */
drhd9b02572001-04-15 00:37:09 +0000409};
410
411/*
drh538f5702007-04-13 02:14:30 +0000412** The following global variables hold counters used for
413** testing purposes only. These variables do not exist in
414** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000415*/
416#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000417int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
418int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
419int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
420int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
421# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000422#else
drh538f5702007-04-13 02:14:30 +0000423# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000424#endif
425
drh86f8c192007-08-22 00:39:19 +0000426/*
427** The following variable points to the head of a double-linked list
428** of all pagers that are eligible for page stealing by the
429** sqlite3_release_memory() interface. Access to this list is
430** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
431*/
432#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
433static Pager *sqlite3PagerList = 0;
danielk19779f61c2f2007-08-27 17:27:49 +0000434static PagerLruList sqlite3LruPageList = {0, 0, 0};
drh86f8c192007-08-22 00:39:19 +0000435#endif
drh538f5702007-04-13 02:14:30 +0000436
437
drhfcd35c72005-05-21 02:48:08 +0000438/*
drh5e00f6c2001-09-13 13:46:56 +0000439** Journal files begin with the following magic string. The data
440** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000441**
drhae2b40c2004-06-09 19:03:54 +0000442** Since version 2.8.0, the journal format contains additional sanity
443** checking information. If the power fails while the journal is begin
444** written, semi-random garbage data might appear in the journal
445** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000446** to roll the journal back, the database could be corrupted. The additional
447** sanity checking data is an attempt to discover the garbage in the
448** journal and ignore it.
449**
drhae2b40c2004-06-09 19:03:54 +0000450** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000451** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000452** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000453** This cksum is initialized to a 32-bit random value that appears in the
454** journal file right after the header. The random initializer is important,
455** because garbage data that appears at the end of a journal is likely
456** data that was once in other files that have now been deleted. If the
457** garbage data came from an obsolete journal file, the checksums might
458** be correct. But by initializing the checksum to random value which
459** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000460*/
drhae2b40c2004-06-09 19:03:54 +0000461static const unsigned char aJournalMagic[] = {
462 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000463};
464
465/*
drh726de592004-06-10 23:35:50 +0000466** The size of the header and of each page in the journal is determined
467** by the following macros.
drh968af522003-02-11 14:55:40 +0000468*/
drhae2b40c2004-06-09 19:03:54 +0000469#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000470
danielk197776572402004-06-25 02:38:54 +0000471/*
472** The journal header size for this pager. In the future, this could be
473** set to some value read from the disk controller. The important
474** characteristic is that it is the same size as a disk sector.
475*/
476#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
477
drhb7f91642004-10-31 02:22:47 +0000478/*
479** The macro MEMDB is true if we are dealing with an in-memory database.
480** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
481** the value of MEMDB will be a constant and the compiler will optimize
482** out code that would never execute.
483*/
484#ifdef SQLITE_OMIT_MEMORYDB
485# define MEMDB 0
486#else
487# define MEMDB pPager->memDb
488#endif
489
490/*
danielk197776572402004-06-25 02:38:54 +0000491** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
492** reserved for working around a windows/posix incompatibility). It is
493** used in the journal to signify that the remainder of the journal file
494** is devoted to storing a master journal name - there are no more pages to
495** roll back. See comments for function writeMasterJournal() for details.
496*/
danielk1977599fcba2004-11-08 07:13:13 +0000497/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
498#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000499
drh968af522003-02-11 14:55:40 +0000500/*
danielk197726836652005-01-17 01:33:13 +0000501** The maximum legal page number is (2^31 - 1).
502*/
503#define PAGER_MAX_PGNO 2147483647
504
505/*
drh86f8c192007-08-22 00:39:19 +0000506** The pagerEnter() and pagerLeave() routines acquire and release
507** a mutex on each pager. The mutex is recursive.
508**
509** This is a special-purpose mutex. It only provides mutual exclusion
510** between the Btree and the Memory Management sqlite3_release_memory()
511** function. It does not prevent, for example, two Btrees from accessing
512** the same pager at the same time. Other general-purpose mutexes in
513** the btree layer handle that chore.
514*/
515#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
516 static void pagerEnter(Pager *p){
517 p->iInUseDB++;
518 if( p->iInUseMM && p->iInUseDB==1 ){
drh26e4a8b2008-05-01 17:16:52 +0000519#ifndef SQLITE_MUTEX_NOOP
drh64e2bb72008-05-07 12:29:55 +0000520 sqlite3_mutex *mutex;
drh86f8c192007-08-22 00:39:19 +0000521 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +0000522#endif
drh86f8c192007-08-22 00:39:19 +0000523 p->iInUseDB = 0;
524 sqlite3_mutex_enter(mutex);
525 p->iInUseDB = 1;
526 sqlite3_mutex_leave(mutex);
527 }
528 assert( p->iInUseMM==0 );
529 }
530 static void pagerLeave(Pager *p){
531 p->iInUseDB--;
532 assert( p->iInUseDB>=0 );
533 }
534#else
535# define pagerEnter(X)
536# define pagerLeave(X)
537#endif
538
539/*
danielk197784f786f2007-08-28 08:00:17 +0000540** Add page pPg to the end of the linked list managed by structure
541** pList (pPg becomes the last entry in the list - the most recently
542** used). Argument pLink should point to either pPg->free or pPg->gfree,
543** depending on whether pPg is being added to the pager-specific or
544** global LRU list.
545*/
danielk19779f61c2f2007-08-27 17:27:49 +0000546static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
547 pLink->pNext = 0;
548 pLink->pPrev = pList->pLast;
549
danielk197784f786f2007-08-28 08:00:17 +0000550#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
551 assert(pLink==&pPg->free || pLink==&pPg->gfree);
552 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
553#endif
554
danielk19779f61c2f2007-08-27 17:27:49 +0000555 if( pList->pLast ){
556 int iOff = (char *)pLink - (char *)pPg;
557 PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
558 pLastLink->pNext = pPg;
559 }else{
560 assert(!pList->pFirst);
561 pList->pFirst = pPg;
562 }
563
564 pList->pLast = pPg;
565 if( !pList->pFirstSynced && pPg->needSync==0 ){
566 pList->pFirstSynced = pPg;
567 }
568}
569
danielk197784f786f2007-08-28 08:00:17 +0000570/*
571** Remove pPg from the list managed by the structure pointed to by pList.
572**
573** Argument pLink should point to either pPg->free or pPg->gfree, depending
574** on whether pPg is being added to the pager-specific or global LRU list.
575*/
danielk19779f61c2f2007-08-27 17:27:49 +0000576static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
577 int iOff = (char *)pLink - (char *)pPg;
578
danielk197784f786f2007-08-28 08:00:17 +0000579#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
580 assert(pLink==&pPg->free || pLink==&pPg->gfree);
581 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
582#endif
583
danielk19779f61c2f2007-08-27 17:27:49 +0000584 if( pPg==pList->pFirst ){
585 pList->pFirst = pLink->pNext;
586 }
587 if( pPg==pList->pLast ){
588 pList->pLast = pLink->pPrev;
589 }
590 if( pLink->pPrev ){
591 PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
592 pPrevLink->pNext = pLink->pNext;
593 }
594 if( pLink->pNext ){
595 PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
596 pNextLink->pPrev = pLink->pPrev;
597 }
598 if( pPg==pList->pFirstSynced ){
599 PgHdr *p = pLink->pNext;
600 while( p && p->needSync ){
601 PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
602 p = pL->pNext;
603 }
604 pList->pFirstSynced = p;
605 }
606
607 pLink->pNext = pLink->pPrev = 0;
608}
609
610/*
danielk197784f786f2007-08-28 08:00:17 +0000611** Add page pPg to the list of free pages for the pager. If
612** memory-management is enabled, also add the page to the global
613** list of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000614*/
615static void lruListAdd(PgHdr *pPg){
616 listAdd(&pPg->pPager->lru, &pPg->free, pPg);
617#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
618 if( !pPg->pPager->memDb ){
619 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
620 listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
621 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
622 }
623#endif
624}
625
626/*
danielk197784f786f2007-08-28 08:00:17 +0000627** Remove page pPg from the list of free pages for the associated pager.
628** If memory-management is enabled, also remove pPg from the global list
629** of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000630*/
631static void lruListRemove(PgHdr *pPg){
632 listRemove(&pPg->pPager->lru, &pPg->free, pPg);
633#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
634 if( !pPg->pPager->memDb ){
635 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
636 listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
637 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
638 }
639#endif
640}
641
642/*
danielk197784f786f2007-08-28 08:00:17 +0000643** This function is called just after the needSync flag has been cleared
644** from all pages managed by pPager (usually because the journal file
645** has just been synced). It updates the pPager->lru.pFirstSynced variable
646** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
647** variable also.
danielk19779f61c2f2007-08-27 17:27:49 +0000648*/
649static void lruListSetFirstSynced(Pager *pPager){
650 pPager->lru.pFirstSynced = pPager->lru.pFirst;
651#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
652 if( !pPager->memDb ){
653 PgHdr *p;
654 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
655 for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
656 assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
657 sqlite3LruPageList.pFirstSynced = p;
658 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
659 }
660#endif
661}
662
danielk1977f35843b2007-04-07 15:03:17 +0000663/*
664** Return true if page *pPg has already been written to the statement
665** journal (or statement snapshot has been created, if *pPg is part
666** of an in-memory database).
667*/
668static int pageInStatement(PgHdr *pPg){
669 Pager *pPager = pPg->pPager;
670 if( MEMDB ){
671 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
672 }else{
drhf5e7bb52008-02-18 14:47:33 +0000673 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000674 }
675}
drh8ca0c722006-05-07 17:49:38 +0000676
677/*
678** Change the size of the pager hash table to N. N must be a power
679** of two.
680*/
681static void pager_resize_hash_table(Pager *pPager, int N){
682 PgHdr **aHash, *pPg;
683 assert( N>0 && (N&(N-1))==0 );
drheee4c8c2008-02-18 22:24:57 +0000684#ifdef SQLITE_MALLOC_SOFT_LIMIT
685 if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){
686 N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]);
687 }
688 if( N==pPager->nHash ) return;
689#endif
drhed138fb2007-08-22 22:04:37 +0000690 pagerLeave(pPager);
drh4873d5f2008-05-13 13:27:33 +0000691 if( pPager->aHash!=0 ) sqlite3FaultBeginBenign(SQLITE_FAULTINJECTOR_MALLOC);
drh17435752007-08-16 04:30:38 +0000692 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drh4873d5f2008-05-13 13:27:33 +0000693 if( pPager->aHash!=0 ) sqlite3FaultEndBenign(SQLITE_FAULTINJECTOR_MALLOC);
drhed138fb2007-08-22 22:04:37 +0000694 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000695 if( aHash==0 ){
696 /* Failure to rehash is not an error. It is only a performance hit. */
697 return;
698 }
drh17435752007-08-16 04:30:38 +0000699 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000700 pPager->nHash = N;
701 pPager->aHash = aHash;
702 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000703 int h;
704 if( pPg->pgno==0 ){
705 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
706 continue;
707 }
708 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000709 pPg->pNextHash = aHash[h];
710 if( aHash[h] ){
711 aHash[h]->pPrevHash = pPg;
712 }
713 aHash[h] = pPg;
714 pPg->pPrevHash = 0;
715 }
716}
717
drhdd793422001-06-28 01:54:48 +0000718/*
drh34e79ce2004-02-08 06:05:46 +0000719** Read a 32-bit integer from the given file descriptor. Store the integer
720** that is read in *pRes. Return SQLITE_OK if everything worked, or an
721** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000722**
723** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000724*/
danielk197762079062007-08-15 17:08:46 +0000725static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000726 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000727 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000728 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000729 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000730 }
drh94f33312002-08-12 12:29:56 +0000731 return rc;
732}
733
734/*
drh97b57482006-01-10 20:32:32 +0000735** Write a 32-bit integer into a string buffer in big-endian byte order.
736*/
drha3152892007-05-05 11:48:52 +0000737#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000738
739/*
drh34e79ce2004-02-08 06:05:46 +0000740** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
741** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000742*/
danielk197762079062007-08-15 17:08:46 +0000743static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000744 char ac[4];
drh97b57482006-01-10 20:32:32 +0000745 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000746 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000747}
748
drh2554f8b2003-01-22 01:26:44 +0000749/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000750** If file pFd is open, call sqlite3OsUnlock() on it.
751*/
752static int osUnlock(sqlite3_file *pFd, int eLock){
753 if( !pFd->pMethods ){
754 return SQLITE_OK;
755 }
756 return sqlite3OsUnlock(pFd, eLock);
757}
758
759/*
danielk1977c7b60172007-08-22 11:22:03 +0000760** This function determines whether or not the atomic-write optimization
761** can be used with this pager. The optimization can be used if:
762**
763** (a) the value returned by OsDeviceCharacteristics() indicates that
764** a database page may be written atomically, and
765** (b) the value returned by OsSectorSize() is less than or equal
766** to the page size.
767**
768** If the optimization cannot be used, 0 is returned. If it can be used,
769** then the value returned is the size of the journal file when it
770** contains rollback data for exactly one page.
771*/
772#ifdef SQLITE_ENABLE_ATOMIC_WRITE
773static int jrnlBufferSize(Pager *pPager){
774 int dc; /* Device characteristics */
775 int nSector; /* Sector size */
drhfacf0302008-06-17 15:12:00 +0000776 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000777 sqlite3_file *fd = pPager->fd;
778
779 if( fd->pMethods ){
780 dc = sqlite3OsDeviceCharacteristics(fd);
781 nSector = sqlite3OsSectorSize(fd);
drhfacf0302008-06-17 15:12:00 +0000782 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000783 }
784
785 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
786 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
787
drhfacf0302008-06-17 15:12:00 +0000788 if( !fd->pMethods ||
789 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000790 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
791 }
792 return 0;
793}
794#endif
795
796/*
danielk1977aef0bf62005-12-30 16:28:01 +0000797** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000798** code. The first argument is a pointer to the pager structure, the
799** second the error-code about to be returned by a pager API function.
800** The value returned is a copy of the second argument to this function.
801**
drh4f0ee682007-03-30 20:43:40 +0000802** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000803** the error becomes persistent. Until the persisten error is cleared,
804** subsequent API calls on this Pager will immediately return the same
805** error code.
806**
807** A persistent error indicates that the contents of the pager-cache
808** cannot be trusted. This state can be cleared by completely discarding
809** the contents of the pager-cache. If a transaction was active when
810** the persistent error occured, then the rollback journal may need
811** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000812*/
danielk1977ae72d982007-10-03 08:46:44 +0000813static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000814static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000815 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000816 assert(
817 pPager->errCode==SQLITE_FULL ||
818 pPager->errCode==SQLITE_OK ||
819 (pPager->errCode & 0xff)==SQLITE_IOERR
820 );
danielk1977979f38e2007-03-27 16:19:51 +0000821 if(
drh4ac285a2006-09-15 07:28:50 +0000822 rc2==SQLITE_FULL ||
823 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000824 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000825 ){
826 pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000827 if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
828 /* If the pager is already unlocked, call pager_unlock() now to
829 ** clear the error state and ensure that the pager-cache is
830 ** completely empty.
831 */
832 pager_unlock(pPager);
833 }
danielk1977aef0bf62005-12-30 16:28:01 +0000834 }
835 return rc;
836}
837
drh477731b2007-06-16 03:06:27 +0000838/*
839** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
840** on the cache using a hash function. This is used for testing
841** and debugging only.
842*/
danielk19773c407372005-02-15 02:54:14 +0000843#ifdef SQLITE_CHECK_PAGES
844/*
845** Return a 32-bit hash of the page data for pPage.
846*/
drh477731b2007-06-16 03:06:27 +0000847static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000848 u32 hash = 0;
849 int i;
drh477731b2007-06-16 03:06:27 +0000850 for(i=0; i<nByte; i++){
851 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000852 }
853 return hash;
854}
drh477731b2007-06-16 03:06:27 +0000855static u32 pager_pagehash(PgHdr *pPage){
856 return pager_datahash(pPage->pPager->pageSize,
857 (unsigned char *)PGHDR_TO_DATA(pPage));
858}
danielk19773c407372005-02-15 02:54:14 +0000859
860/*
861** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
862** is defined, and NDEBUG is not defined, an assert() statement checks
863** that the page is either dirty or still matches the calculated page-hash.
864*/
865#define CHECK_PAGE(x) checkPage(x)
866static void checkPage(PgHdr *pPg){
867 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000868 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000869 pPg->pageHash==pager_pagehash(pPg) );
870}
871
872#else
drh8ffa8172007-06-18 17:25:17 +0000873#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000874#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000875#define CHECK_PAGE(x)
876#endif
877
drhed7c8552001-04-11 14:29:21 +0000878/*
danielk197776572402004-06-25 02:38:54 +0000879** When this is called the journal file for pager pPager must be open.
880** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000881** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000882**
danielk197765839c62007-08-30 08:08:17 +0000883** zMaster must point to a buffer of at least nMaster bytes allocated by
884** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
885** enough space to write the master journal name). If the master journal
886** name in the journal is longer than nMaster bytes (including a
887** nul-terminator), then this is handled as if no master journal name
888** were present in the journal.
889**
890** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000891** SQLITE_OK returned.
892*/
danielk197765839c62007-08-30 08:08:17 +0000893static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000894 int rc;
895 u32 len;
drheb206252004-10-01 02:00:31 +0000896 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000897 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000898 int i;
danielk197776572402004-06-25 02:38:54 +0000899 unsigned char aMagic[8]; /* A buffer to hold the magic header */
900
danielk197765839c62007-08-30 08:08:17 +0000901 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000902
drh054889e2005-11-30 03:20:31 +0000903 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000904 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000905
danielk197762079062007-08-15 17:08:46 +0000906 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000907 if( rc!=SQLITE_OK ) return rc;
908
danielk197765839c62007-08-30 08:08:17 +0000909 if( len>=nMaster ){
910 return SQLITE_OK;
911 }
912
danielk197762079062007-08-15 17:08:46 +0000913 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000914 if( rc!=SQLITE_OK ) return rc;
915
danielk197762079062007-08-15 17:08:46 +0000916 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000917 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
918
danielk197765839c62007-08-30 08:08:17 +0000919 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000920 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000921 return rc;
922 }
danielk197765839c62007-08-30 08:08:17 +0000923 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000924
925 /* See if the checksum matches the master journal name */
926 for(i=0; i<len; i++){
danielk197765839c62007-08-30 08:08:17 +0000927 cksum -= zMaster[i];
928 }
danielk19778191bff2004-06-28 04:52:30 +0000929 if( cksum ){
930 /* If the checksum doesn't add up, then one or more of the disk sectors
931 ** containing the master journal filename is corrupted. This means
932 ** definitely roll back, so just return SQLITE_OK and report a (nul)
933 ** master-journal filename.
934 */
danielk197765839c62007-08-30 08:08:17 +0000935 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000936 }
danielk197776572402004-06-25 02:38:54 +0000937
938 return SQLITE_OK;
939}
940
941/*
942** Seek the journal file descriptor to the next sector boundary where a
943** journal header may be read or written. Pager.journalOff is updated with
944** the new seek offset.
945**
946** i.e for a sector size of 512:
947**
948** Input Offset Output Offset
949** ---------------------------------------
950** 0 0
951** 512 512
952** 100 512
953** 2000 2048
954**
955*/
danielk197762079062007-08-15 17:08:46 +0000956static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000957 i64 offset = 0;
958 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000959 if( c ){
960 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
961 }
962 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
963 assert( offset>=c );
964 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
965 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000966}
967
968/*
drhf3a87622008-04-17 14:16:42 +0000969** Write zeros over the header of the journal file. This has the
970** effect of invalidating the journal file and committing the
971** transaction.
972*/
danielk1977df2566a2008-05-07 19:11:03 +0000973static int zeroJournalHdr(Pager *pPager, int doTruncate){
974 int rc = SQLITE_OK;
drhf3a87622008-04-17 14:16:42 +0000975 static const char zeroHdr[28];
976
danielk1977df2566a2008-05-07 19:11:03 +0000977 if( pPager->journalOff ){
danielk1977b53e4962008-06-04 06:45:59 +0000978 i64 iLimit = pPager->journalSizeLimit;
979
danielk1977df2566a2008-05-07 19:11:03 +0000980 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000981 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000982 rc = sqlite3OsTruncate(pPager->jfd, 0);
983 }else{
984 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
985 }
danielk197781620542008-06-07 05:19:37 +0000986 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000987 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
988 }
danielk1977b53e4962008-06-04 06:45:59 +0000989
990 /* At this point the transaction is committed but the write lock
991 ** is still held on the file. If there is a size limit configured for
992 ** the persistent journal and the journal file currently consumes more
993 ** space than that limit allows for, truncate it now. There is no need
994 ** to sync the file following this operation.
995 */
996 if( rc==SQLITE_OK && iLimit>0 ){
997 i64 sz;
998 rc = sqlite3OsFileSize(pPager->jfd, &sz);
999 if( rc==SQLITE_OK && sz>iLimit ){
1000 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
1001 }
1002 }
drha06ecba2008-04-22 17:15:17 +00001003 }
drhf3a87622008-04-17 14:16:42 +00001004 return rc;
1005}
1006
1007/*
danielk197776572402004-06-25 02:38:54 +00001008** The journal file must be open when this routine is called. A journal
1009** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
1010** current location.
1011**
1012** The format for the journal header is as follows:
1013** - 8 bytes: Magic identifying journal format.
1014** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
1015** - 4 bytes: Random number used for page hash.
1016** - 4 bytes: Initial database page count.
1017** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +00001018** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +00001019**
danielk197767c007b2008-03-20 04:45:49 +00001020** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +00001021*/
1022static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +00001023 int rc = SQLITE_OK;
1024 char *zHeader = pPager->pTmpSpace;
1025 int nHeader = pPager->pageSize;
1026 int nWrite;
1027
1028 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
1029 nHeader = JOURNAL_HDR_SZ(pPager);
1030 }
danielk197776572402004-06-25 02:38:54 +00001031
danielk19774099f6e2007-03-19 11:25:20 +00001032 if( pPager->stmtHdrOff==0 ){
1033 pPager->stmtHdrOff = pPager->journalOff;
1034 }
1035
danielk197762079062007-08-15 17:08:46 +00001036 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001037 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001038
drh97b57482006-01-10 20:32:32 +00001039 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +00001040
1041 /*
1042 ** Write the nRec Field - the number of page records that follow this
1043 ** journal header. Normally, zero is written to this value at this time.
1044 ** After the records are added to the journal (and the journal synced,
1045 ** if in full-sync mode), the zero is overwritten with the true number
1046 ** of records (see syncJournal()).
1047 **
1048 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
1049 ** reading the journal this value tells SQLite to assume that the
1050 ** rest of the journal file contains valid page records. This assumption
1051 ** is dangerous, as if a failure occured whilst writing to the journal
1052 ** file it may contain some garbage data. There are two scenarios
1053 ** where this risk can be ignored:
1054 **
1055 ** * When the pager is in no-sync mode. Corruption can follow a
1056 ** power failure in this case anyway.
1057 **
1058 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1059 ** that garbage data is never appended to the journal file.
1060 */
1061 assert(pPager->fd->pMethods||pPager->noSync);
1062 if( (pPager->noSync)
1063 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1064 ){
1065 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1066 }else{
1067 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1068 }
1069
drh97b57482006-01-10 20:32:32 +00001070 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +00001071 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +00001072 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1073 /* The initial database size */
1074 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1075 /* The assumed sector size for this process */
1076 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
danielk197767c007b2008-03-20 04:45:49 +00001077 if( pPager->journalHdr==0 ){
1078 /* The page size */
1079 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
1080 }
danielk197776572402004-06-25 02:38:54 +00001081
danielk1977a664f8e2008-04-22 14:31:48 +00001082 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
1083 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
1084 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
1085 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +00001086 }
danielk1977a664f8e2008-04-22 14:31:48 +00001087
danielk197776572402004-06-25 02:38:54 +00001088 return rc;
1089}
1090
1091/*
1092** The journal file must be open when this is called. A journal header file
1093** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1094** file. See comments above function writeJournalHdr() for a description of
1095** the journal header format.
1096**
1097** If the header is read successfully, *nRec is set to the number of
1098** page records following this header and *dbSize is set to the size of the
1099** database before the transaction began, in pages. Also, pPager->cksumInit
1100** is set to the value read from the journal header. SQLITE_OK is returned
1101** in this case.
1102**
1103** If the journal header file appears to be corrupted, SQLITE_DONE is
1104** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1105** cannot be read from the journal file an error code is returned.
1106*/
1107static int readJournalHdr(
1108 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001109 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001110 u32 *pNRec,
1111 u32 *pDbSize
1112){
1113 int rc;
1114 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001115 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +00001116 int iPageSize;
danielk197776572402004-06-25 02:38:54 +00001117
danielk197762079062007-08-15 17:08:46 +00001118 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001119 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1120 return SQLITE_DONE;
1121 }
danielk197762079062007-08-15 17:08:46 +00001122 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001123
danielk197762079062007-08-15 17:08:46 +00001124 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001125 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001126 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001127
1128 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1129 return SQLITE_DONE;
1130 }
1131
danielk197762079062007-08-15 17:08:46 +00001132 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001133 if( rc ) return rc;
1134
danielk197762079062007-08-15 17:08:46 +00001135 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001136 if( rc ) return rc;
1137
danielk197762079062007-08-15 17:08:46 +00001138 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001139 if( rc ) return rc;
1140
danielk197767c007b2008-03-20 04:45:49 +00001141 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
1142 if( rc==SQLITE_OK
1143 && iPageSize>=512
1144 && iPageSize<=SQLITE_MAX_PAGE_SIZE
1145 && ((iPageSize-1)&iPageSize)==0
1146 ){
1147 u16 pagesize = iPageSize;
1148 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
1149 }
1150 if( rc ) return rc;
1151
danielk197776572402004-06-25 02:38:54 +00001152 /* Update the assumed sector-size to match the value used by
1153 ** the process that created this journal. If this journal was
1154 ** created by a process other than this one, then this routine
1155 ** is being called from within pager_playback(). The local value
1156 ** of Pager.sectorSize is restored at the end of that routine.
1157 */
danielk197762079062007-08-15 17:08:46 +00001158 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001159 if( rc ) return rc;
1160
1161 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001162 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001163}
1164
1165
1166/*
1167** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001168** pPager at the current location. The master journal name must be the last
1169** thing written to a journal file. If the pager is in full-sync mode, the
1170** journal file descriptor is advanced to the next sector boundary before
1171** anything is written. The format is:
1172**
1173** + 4 bytes: PAGER_MJ_PGNO.
1174** + N bytes: length of master journal name.
1175** + 4 bytes: N
1176** + 4 bytes: Master journal name checksum.
1177** + 8 bytes: aJournalMagic[].
1178**
1179** The master journal page checksum is the sum of the bytes in the master
1180** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001181**
1182** If zMaster is a NULL pointer (occurs for a single database transaction),
1183** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001184*/
1185static int writeMasterJournal(Pager *pPager, const char *zMaster){
1186 int rc;
1187 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001188 int i;
danielk197762079062007-08-15 17:08:46 +00001189 i64 jrnlOff;
danielk1977df2566a2008-05-07 19:11:03 +00001190 i64 jrnlSize;
drh97b57482006-01-10 20:32:32 +00001191 u32 cksum = 0;
1192 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001193
1194 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1195 pPager->setMaster = 1;
1196
1197 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001198 for(i=0; i<len; i++){
1199 cksum += zMaster[i];
1200 }
danielk197776572402004-06-25 02:38:54 +00001201
1202 /* If in full-sync mode, advance to the next disk sector before writing
1203 ** the master journal name. This is in case the previous page written to
1204 ** the journal has already been synced.
1205 */
1206 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001207 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001208 }
danielk197762079062007-08-15 17:08:46 +00001209 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001210 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001211
danielk197762079062007-08-15 17:08:46 +00001212 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001213 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001214 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001215
danielk197762079062007-08-15 17:08:46 +00001216 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001217 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001218 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001219
drh97b57482006-01-10 20:32:32 +00001220 put32bits(zBuf, len);
1221 put32bits(&zBuf[4], cksum);
1222 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001223 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +00001224 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +00001225 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +00001226
1227 /* If the pager is in peristent-journal mode, then the physical
1228 ** journal-file may extend past the end of the master-journal name
1229 ** and 8 bytes of magic data just written to the file. This is
1230 ** dangerous because the code to rollback a hot-journal file
1231 ** will not be able to find the master-journal name to determine
1232 ** whether or not the journal is hot.
1233 **
1234 ** Easiest thing to do in this scenario is to truncate the journal
1235 ** file to the required size.
1236 */
1237 if( (rc==SQLITE_OK)
1238 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
1239 && jrnlSize>jrnlOff
1240 ){
1241 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
1242 }
danielk197776572402004-06-25 02:38:54 +00001243 return rc;
1244}
1245
1246/*
drh03eb96a2002-11-10 23:32:56 +00001247** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001248** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001249**
1250** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001251** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001252** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001253** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001254*/
drh3aac2dd2004-04-26 14:10:20 +00001255static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001256 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001257 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1258 assert( MEMDB );
1259 if( !pHist->inStmt ){
1260 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1261 if( pPager->pStmt ){
1262 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1263 }
1264 pHist->pNextStmt = pPager->pStmt;
1265 pPager->pStmt = pPg;
1266 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001267 }
drh03eb96a2002-11-10 23:32:56 +00001268}
1269
1270/*
drhed7c8552001-04-11 14:29:21 +00001271** Find a page in the hash table given its page number. Return
1272** a pointer to the page or NULL if not found.
1273*/
drhd9b02572001-04-15 00:37:09 +00001274static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001275 PgHdr *p;
1276 if( pPager->aHash==0 ) return 0;
1277 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001278 while( p && p->pgno!=pgno ){
1279 p = p->pNextHash;
1280 }
1281 return p;
1282}
1283
1284/*
danielk1977e180dd92007-04-05 17:15:52 +00001285** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001286** sets the state of the pager back to what it was when it was first
1287** opened. Any outstanding pages are invalidated and subsequent attempts
1288** to access those pages will likely result in a coredump.
1289*/
drhd9b02572001-04-15 00:37:09 +00001290static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001291 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001292 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001293 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001294 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1295 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001296 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001297 lruListRemove(pPg);
drhfacf0302008-06-17 15:12:00 +00001298 sqlite3PageFree(pPg->pData);
drh17435752007-08-16 04:30:38 +00001299 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001300 }
danielk19779f61c2f2007-08-27 17:27:49 +00001301 assert(pPager->lru.pFirst==0);
1302 assert(pPager->lru.pFirstSynced==0);
1303 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001304 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001305 pPager->pAll = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001306 pPager->pDirty = 0;
drh8ca0c722006-05-07 17:49:38 +00001307 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001308 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001309 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001310 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001311 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001312}
1313
1314/*
danielk1977ae72d982007-10-03 08:46:44 +00001315** Unlock the database file.
1316**
1317** If the pager is currently in error state, discard the contents of
1318** the cache and reset the Pager structure internal state. If there is
1319** an open journal-file, then the next time a shared-lock is obtained
1320** on the pager file (by this or any other process), it will be
1321** treated as a hot-journal and rolled back.
1322*/
1323static void pager_unlock(Pager *pPager){
1324 if( !pPager->exclusiveMode ){
1325 if( !MEMDB ){
drh1aa5af12008-03-07 19:51:14 +00001326 int rc = osUnlock(pPager->fd, NO_LOCK);
drh308aa322008-03-07 20:14:38 +00001327 if( rc ) pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +00001328 pPager->dbSize = -1;
1329 IOTRACE(("UNLOCK %p\n", pPager))
1330
drh16e45a42008-04-19 20:34:18 +00001331 /* Always close the journal file when dropping the database lock.
1332 ** Otherwise, another connection with journal_mode=delete might
1333 ** delete the file out from under us.
1334 */
1335 if( pPager->journalOpen ){
1336 sqlite3OsClose(pPager->jfd);
1337 pPager->journalOpen = 0;
1338 sqlite3BitvecDestroy(pPager->pInJournal);
1339 pPager->pInJournal = 0;
1340 }
1341
danielk1977ae72d982007-10-03 08:46:44 +00001342 /* If Pager.errCode is set, the contents of the pager cache cannot be
1343 ** trusted. Now that the pager file is unlocked, the contents of the
1344 ** cache can be discarded and the error code safely cleared.
1345 */
1346 if( pPager->errCode ){
drh1aa5af12008-03-07 19:51:14 +00001347 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00001348 pager_reset(pPager);
1349 if( pPager->stmtOpen ){
1350 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +00001351 sqlite3BitvecDestroy(pPager->pInStmt);
1352 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001353 }
danielk1977ae72d982007-10-03 08:46:44 +00001354 pPager->stmtOpen = 0;
1355 pPager->stmtInUse = 0;
1356 pPager->journalOff = 0;
1357 pPager->journalStarted = 0;
1358 pPager->stmtAutoopen = 0;
1359 pPager->origDbSize = 0;
1360 }
1361 }
1362
1363 if( !MEMDB || pPager->errCode==SQLITE_OK ){
1364 pPager->state = PAGER_UNLOCK;
1365 pPager->changeCountDone = 0;
1366 }
1367 }
1368}
1369
1370/*
1371** Execute a rollback if a transaction is active and unlock the
1372** database file. If the pager has already entered the error state,
1373** do not attempt the rollback.
1374*/
1375static void pagerUnlockAndRollback(Pager *p){
drhfdc40e92008-04-17 20:59:37 +00001376 /* assert( p->state>=PAGER_RESERVED || p->journalOpen==0 ); */
danielk1977ae72d982007-10-03 08:46:44 +00001377 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
drh4873d5f2008-05-13 13:27:33 +00001378 sqlite3FaultBeginBenign(-1);
danielk1977ae72d982007-10-03 08:46:44 +00001379 sqlite3PagerRollback(p);
drh4873d5f2008-05-13 13:27:33 +00001380 sqlite3FaultEndBenign(-1);
danielk1977ae72d982007-10-03 08:46:44 +00001381 }
1382 pager_unlock(p);
drhfdc40e92008-04-17 20:59:37 +00001383#if 0
danielk1977ae72d982007-10-03 08:46:44 +00001384 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1385 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drhfdc40e92008-04-17 20:59:37 +00001386#endif
danielk1977ae72d982007-10-03 08:46:44 +00001387}
1388
1389/*
drh80e35f42007-03-30 14:06:34 +00001390** This routine ends a transaction. A transaction is ended by either
1391** a COMMIT or a ROLLBACK.
1392**
drhed7c8552001-04-11 14:29:21 +00001393** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001394** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1395** the database lock and acquires a SHARED lock in its place if that is
1396** the appropriate thing to do. Release locks usually is appropriate,
1397** unless we are in exclusive access mode or unless this is a
1398** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1399**
1400** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001401**
1402** TODO: Consider keeping the journal file open for temporary databases.
1403** This might give a performance improvement on windows where opening
1404** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001405*/
danielk1977df2566a2008-05-07 19:11:03 +00001406static int pager_end_transaction(Pager *pPager, int hasMaster){
drhd9b02572001-04-15 00:37:09 +00001407 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001408 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001409 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001410 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001411 if( pPager->state<PAGER_RESERVED ){
1412 return SQLITE_OK;
1413 }
danielk19773b8a05f2007-03-19 17:44:26 +00001414 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001415 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001416 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001417 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001418 }
drhda47d772002-12-02 04:25:19 +00001419 if( pPager->journalOpen ){
danielk197793f7af92008-05-09 16:57:50 +00001420 if( pPager->exclusiveMode
1421 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1422 ){
1423 rc = zeroJournalHdr(pPager, hasMaster);
1424 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +00001425 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001426 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001427 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001428 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001429 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +00001430 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00001431 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001432 }
danielk197741483462007-03-24 16:45:04 +00001433 }
drhf5e7bb52008-02-18 14:47:33 +00001434 sqlite3BitvecDestroy(pPager->pInJournal);
1435 pPager->pInJournal = 0;
drhda47d772002-12-02 04:25:19 +00001436 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1437 pPg->inJournal = 0;
1438 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001439 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001440 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001441#ifdef SQLITE_CHECK_PAGES
1442 pPg->pageHash = pager_pagehash(pPg);
1443#endif
drhda47d772002-12-02 04:25:19 +00001444 }
drh605ad8f2006-05-03 23:34:05 +00001445 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001446 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001447 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001448 }else{
drhf5e7bb52008-02-18 14:47:33 +00001449 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001450 }
danielk1977979f38e2007-03-27 16:19:51 +00001451
danielk197741483462007-03-24 16:45:04 +00001452 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001453 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001454 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001455 }else if( pPager->state==PAGER_SYNCED ){
1456 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001457 }
danielk1977334cdb62007-03-26 08:05:12 +00001458 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001459 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001460 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001461 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001462 pPager->dbSize = -1;
drhd138c012008-05-13 00:58:18 +00001463 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001464
1465 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001466}
1467
drhed7c8552001-04-11 14:29:21 +00001468/*
drh968af522003-02-11 14:55:40 +00001469** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001470**
1471** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001472** random initial value and the page number. We experimented with
1473** a checksum of the entire data, but that was found to be too slow.
1474**
1475** Note that the page number is stored at the beginning of data and
1476** the checksum is stored at the end. This is important. If journal
1477** corruption occurs due to a power failure, the most likely scenario
1478** is that one end or the other of the record will be changed. It is
1479** much less likely that the two ends of the journal record will be
1480** correct and the middle be corrupt. Thus, this "checksum" scheme,
1481** though fast and simple, catches the mostly likely kind of corruption.
1482**
1483** FIX ME: Consider adding every 200th (or so) byte of the data to the
1484** checksum. That way if a single page spans 3 or more disk sectors and
1485** only the middle sector is corrupt, we will still have a reasonable
1486** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001487*/
drh74161702006-02-24 02:53:49 +00001488static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001489 u32 cksum = pPager->cksumInit;
1490 int i = pPager->pageSize-200;
1491 while( i>0 ){
1492 cksum += aData[i];
1493 i -= 200;
1494 }
drh968af522003-02-11 14:55:40 +00001495 return cksum;
1496}
1497
drh605ad8f2006-05-03 23:34:05 +00001498/* Forward declaration */
1499static void makeClean(PgHdr*);
1500
drh968af522003-02-11 14:55:40 +00001501/*
drhfa86c412002-02-02 15:01:15 +00001502** Read a single page from the journal file opened on file descriptor
1503** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001504**
drh726de592004-06-10 23:35:50 +00001505** If useCksum==0 it means this journal does not use checksums. Checksums
1506** are not used in statement journals because statement journals do not
1507** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001508*/
danielk197762079062007-08-15 17:08:46 +00001509static int pager_playback_one_page(
1510 Pager *pPager,
1511 sqlite3_file *jfd,
1512 i64 offset,
1513 int useCksum
1514){
drhfa86c412002-02-02 15:01:15 +00001515 int rc;
drhae2b40c2004-06-09 19:03:54 +00001516 PgHdr *pPg; /* An existing page in the cache */
1517 Pgno pgno; /* The page number of a page in journal */
1518 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001519 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001520
drh96362842005-03-20 22:47:56 +00001521 /* useCksum should be true for the main journal and false for
1522 ** statement journals. Verify that this is always the case
1523 */
drh9cbe6352005-11-29 03:13:21 +00001524 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001525 assert( aData );
drh96362842005-03-20 22:47:56 +00001526
danielk197762079062007-08-15 17:08:46 +00001527 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001528 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001529 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001530 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001531 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001532
drh968af522003-02-11 14:55:40 +00001533 /* Sanity checking on the page. This is more important that I originally
1534 ** thought. If a power failure occurs while the journal is being written,
1535 ** it could cause invalid data to be written into the journal. We need to
1536 ** detect this invalid data (with high probability) and ignore it.
1537 */
danielk197775edc162004-06-26 01:48:18 +00001538 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001539 return SQLITE_DONE;
1540 }
drhae2b40c2004-06-09 19:03:54 +00001541 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001542 return SQLITE_OK;
1543 }
drhae2b40c2004-06-09 19:03:54 +00001544 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001545 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001546 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001547 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001548 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001549 return SQLITE_DONE;
1550 }
1551 }
drhfa86c412002-02-02 15:01:15 +00001552
danielk1977aa5ccdf2004-06-14 05:10:42 +00001553 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001554
1555 /* If the pager is in RESERVED state, then there must be a copy of this
1556 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001557 ** not the database file. The page is left marked dirty in this case.
1558 **
danielk19772df71c72007-05-24 07:22:42 +00001559 ** An exception to the above rule: If the database is in no-sync mode
1560 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001561 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1562 ** during a Movepage() call, then the page may not be in the cache
1563 ** either. So the condition described in the above paragraph is not
1564 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001565 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001566 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1567 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001568 **
1569 ** Ticket #1171: The statement journal might contain page content that is
1570 ** different from the page content at the start of the transaction.
1571 ** This occurs when a page is changed prior to the start of a statement
1572 ** then changed again within the statement. When rolling back such a
1573 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001574 ** for certain that original page contents are synced into the main rollback
1575 ** journal. Otherwise, a power loss might leave modified data in the
1576 ** database file without an entry in the rollback journal that can
1577 ** restore the database to its original form. Two conditions must be
1578 ** met before writing to the database files. (1) the database must be
1579 ** locked. (2) we know that the original page content is fully synced
1580 ** in the main journal either because the page is not in cache or else
1581 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001582 **
1583 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1584 ** is possible to fail a statement on a database that does not yet exist.
1585 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001586 */
drhae2b40c2004-06-09 19:03:54 +00001587 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001588 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1589 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh4c02a232008-04-14 23:13:45 +00001590 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0)
1591 && pPager->fd->pMethods ){
danielk197762079062007-08-15 17:08:46 +00001592 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1593 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001594 if( pPg ){
1595 makeClean(pPg);
1596 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001597 }
drhfa86c412002-02-02 15:01:15 +00001598 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001599 /* No page should ever be explicitly rolled back that is in use, except
1600 ** for page 1 which is held in use in order to keep the lock on the
1601 ** database active. However such a page may be rolled back as a result
1602 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001603 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001604 */
drhb6f41482004-05-14 01:58:11 +00001605 void *pData;
danielk197728129562005-01-11 10:25:06 +00001606 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001607 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001608 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001609 if( pPager->xReiniter ){
1610 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001611 }
danielk19773c407372005-02-15 02:54:14 +00001612#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001613 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001614#endif
drh86a88112007-04-16 15:02:19 +00001615 /* If this was page 1, then restore the value of Pager.dbFileVers.
1616 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001617 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001618 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001619 }
drh86a88112007-04-16 15:02:19 +00001620
1621 /* Decode the page just read from disk */
1622 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001623 }
1624 return rc;
1625}
1626
1627/*
danielk197713adf8a2004-06-03 16:08:41 +00001628** Parameter zMaster is the name of a master journal file. A single journal
1629** file that referred to the master journal file has just been rolled back.
1630** This routine checks if it is possible to delete the master journal file,
1631** and does so if it is.
drh726de592004-06-10 23:35:50 +00001632**
danielk197765839c62007-08-30 08:08:17 +00001633** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1634** available for use within this function.
1635**
1636**
drh726de592004-06-10 23:35:50 +00001637** The master journal file contains the names of all child journals.
1638** To tell if a master journal can be deleted, check to each of the
1639** children. If all children are either missing or do not refer to
1640** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001641*/
danielk1977b4b47412007-08-17 15:53:36 +00001642static int pager_delmaster(Pager *pPager, const char *zMaster){
1643 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001644 int rc;
1645 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001646 sqlite3_file *pMaster;
1647 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001648 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001649 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001650
1651 /* Open the master journal file exclusively in case some other process
1652 ** is running this routine also. Not that it makes too much difference.
1653 */
drhe5ae5732008-06-15 02:51:47 +00001654 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001655 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001656 if( !pMaster ){
1657 rc = SQLITE_NOMEM;
1658 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001659 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1660 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001661 }
danielk197713adf8a2004-06-03 16:08:41 +00001662 if( rc!=SQLITE_OK ) goto delmaster_out;
1663 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001664
1665 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001666 if( rc!=SQLITE_OK ) goto delmaster_out;
1667
1668 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001669 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001670 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001671 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001672
1673 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001674 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001675 */
drhe5ae5732008-06-15 02:51:47 +00001676 zMasterJournal = (char *)sqlite3Malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001677 if( !zMasterJournal ){
1678 rc = SQLITE_NOMEM;
1679 goto delmaster_out;
1680 }
danielk197765839c62007-08-30 08:08:17 +00001681 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001682 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001683 if( rc!=SQLITE_OK ) goto delmaster_out;
1684
danielk19775865e3d2004-06-14 06:03:57 +00001685 zJournal = zMasterJournal;
1686 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001687 int exists;
1688 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1689 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001690 goto delmaster_out;
1691 }
danielk1977861f7452008-06-05 11:39:11 +00001692 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001693 /* One of the journals pointed to by the master journal exists.
1694 ** Open it and check if it points at the master journal. If
1695 ** so, return without deleting the master journal file.
1696 */
drh3b7b78b2004-11-24 01:16:43 +00001697 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001698 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1699 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001700 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001701 goto delmaster_out;
1702 }
danielk197713adf8a2004-06-03 16:08:41 +00001703
danielk197765839c62007-08-30 08:08:17 +00001704 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001705 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001706 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001707 goto delmaster_out;
1708 }
danielk197776572402004-06-25 02:38:54 +00001709
danielk197765839c62007-08-30 08:08:17 +00001710 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001711 if( c ){
danielk197776572402004-06-25 02:38:54 +00001712 /* We have a match. Do not delete the master journal file. */
1713 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001714 }
1715 }
danielk19775865e3d2004-06-14 06:03:57 +00001716 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001717 }
1718 }
1719
danielk1977fee2d252007-08-18 10:59:19 +00001720 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001721
1722delmaster_out:
1723 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001724 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001725 }
1726 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001727 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001728 }
danielk1977b4b47412007-08-17 15:53:36 +00001729 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001730 return rc;
1731}
1732
drha6abd042004-06-09 17:37:22 +00001733
danielk1977e180dd92007-04-05 17:15:52 +00001734static void pager_truncate_cache(Pager *pPager);
1735
drha6abd042004-06-09 17:37:22 +00001736/*
drhcb4c40b2004-08-18 19:09:43 +00001737** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001738** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001739**
1740** Might might be the case that the file on disk is smaller than nPage.
1741** This can happen, for example, if we are in the middle of a transaction
1742** which has extended the file size and the new pages are still all held
1743** in cache, then an INSERT or UPDATE does a statement rollback. Some
1744** operating system implementations can get confused if you try to
1745** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001746** so detect this case and write a single zero byte to the end of the new
1747** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001748*/
1749static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001750 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001751 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001752 i64 currentSize, newSize;
1753 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1754 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001755 if( rc==SQLITE_OK && currentSize!=newSize ){
1756 if( currentSize>newSize ){
1757 rc = sqlite3OsTruncate(pPager->fd, newSize);
1758 }else{
1759 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1760 }
drh7fe3f7e2007-11-29 18:44:27 +00001761 }
danielk1977e180dd92007-04-05 17:15:52 +00001762 }
1763 if( rc==SQLITE_OK ){
1764 pPager->dbSize = nPage;
1765 pager_truncate_cache(pPager);
1766 }
1767 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001768}
1769
1770/*
drhc80f0582007-05-01 16:59:48 +00001771** Set the sectorSize for the given pager.
1772**
drh334c80d2008-03-28 17:41:13 +00001773** The sector size is at least as big as the sector size reported
1774** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001775*/
1776static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001777 assert(pPager->fd->pMethods||pPager->tempFile);
1778 if( !pPager->tempFile ){
1779 /* Sector size doesn't matter for temporary files. Also, the file
1780 ** may not have been opened yet, in whcih case the OsSectorSize()
1781 ** call will segfault.
1782 */
1783 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1784 }
drh334c80d2008-03-28 17:41:13 +00001785 if( pPager->sectorSize<512 ){
1786 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001787 }
1788}
1789
1790/*
drhed7c8552001-04-11 14:29:21 +00001791** Playback the journal and thus restore the database file to
1792** the state it was in before we started making changes.
1793**
drh34e79ce2004-02-08 06:05:46 +00001794** The journal file format is as follows:
1795**
drhae2b40c2004-06-09 19:03:54 +00001796** (1) 8 byte prefix. A copy of aJournalMagic[].
1797** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001798** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001799** number of page records from the journal size.
1800** (3) 4 byte big-endian integer which is the initial value for the
1801** sanity checksum.
1802** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001803** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001804** (5) 4 byte big-endian integer which is the sector size. The header
1805** is this many bytes in size.
1806** (6) 4 byte big-endian integer which is the page case.
1807** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001808** name. The value may be zero (indicate that there is no master
1809** journal.)
drh334c80d2008-03-28 17:41:13 +00001810** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001811** and might be shorter than the value read from (5). If the first byte
1812** of the name is \000 then there is no master journal. The master
1813** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001814** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001815** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001816** + pPager->pageSize bytes of data.
1817** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001818**
drh334c80d2008-03-28 17:41:13 +00001819** When we speak of the journal header, we mean the first 8 items above.
1820** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001821**
1822** Call the value from the second bullet "nRec". nRec is the number of
1823** valid page entries in the journal. In most cases, you can compute the
1824** value of nRec from the size of the journal file. But if a power
1825** failure occurred while the journal was being written, it could be the
1826** case that the size of the journal file had already been increased but
1827** the extra entries had not yet made it safely to disk. In such a case,
1828** the value of nRec computed from the file size would be too large. For
1829** that reason, we always use the nRec value in the header.
1830**
1831** If the nRec value is 0xffffffff it means that nRec should be computed
1832** from the file size. This value is used when the user selects the
1833** no-sync option for the journal. A power failure could lead to corruption
1834** in this case. But for things like temporary table (which will be
1835** deleted when the power is restored) we don't care.
1836**
drhd9b02572001-04-15 00:37:09 +00001837** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001838** journal file then all pages up to the first corrupted page are rolled
1839** back (or no pages if the journal header is corrupted). The journal file
1840** is then deleted and SQLITE_OK returned, just as if no corruption had
1841** been encountered.
1842**
1843** If an I/O or malloc() error occurs, the journal-file is not deleted
1844** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001845*/
danielk1977e277be02007-03-23 18:12:06 +00001846static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001847 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001848 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001849 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001850 int i; /* Loop counter */
1851 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001852 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001853 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001854 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001855
drhc3a64ba2001-11-22 00:01:27 +00001856 /* Figure out how many records are in the journal. Abort early if
1857 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001858 */
drh8cfbf082001-09-19 13:22:39 +00001859 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001860 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001861 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001862 goto end_playback;
1863 }
drh240c5792004-02-08 00:40:52 +00001864
danielk197776572402004-06-25 02:38:54 +00001865 /* Read the master journal name from the journal, if it is present.
1866 ** If a master journal file name is specified, but the file is not
1867 ** present on disk, then the journal is not hot and does not need to be
1868 ** played back.
drh240c5792004-02-08 00:40:52 +00001869 */
danielk197765839c62007-08-30 08:08:17 +00001870 zMaster = pPager->pTmpSpace;
1871 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001872 if( rc==SQLITE_OK && zMaster[0] ){
1873 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001874 }
danielk197765839c62007-08-30 08:08:17 +00001875 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001876 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001877 goto end_playback;
1878 }
1879 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001880
danielk197776572402004-06-25 02:38:54 +00001881 /* This loop terminates either when the readJournalHdr() call returns
1882 ** SQLITE_DONE or an IO error occurs. */
1883 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001884
danielk197776572402004-06-25 02:38:54 +00001885 /* Read the next journal header from the journal file. If there are
1886 ** not enough bytes left in the journal file for a complete header, or
1887 ** it is corrupted, then a process must of failed while writing it.
1888 ** This indicates nothing more needs to be rolled back.
1889 */
1890 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1891 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001892 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001893 rc = SQLITE_OK;
1894 }
danielk197776572402004-06-25 02:38:54 +00001895 goto end_playback;
1896 }
1897
1898 /* If nRec is 0xffffffff, then this journal was created by a process
1899 ** working in no-sync mode. This means that the rest of the journal
1900 ** file consists of pages, there are no more journal headers. Compute
1901 ** the value of nRec based on this assumption.
1902 */
1903 if( nRec==0xffffffff ){
1904 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1905 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1906 }
1907
danielk1977e277be02007-03-23 18:12:06 +00001908 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001909 ** process and if this is the final header in the journal, then it means
1910 ** that this part of the journal was being filled but has not yet been
1911 ** synced to disk. Compute the number of pages based on the remaining
1912 ** size of the file.
1913 **
1914 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001915 */
drh8940f4e2007-08-11 00:26:20 +00001916 if( nRec==0 && !isHot &&
1917 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001918 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1919 }
1920
danielk197776572402004-06-25 02:38:54 +00001921 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001922 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001923 */
danielk1977e180dd92007-04-05 17:15:52 +00001924 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001925 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001926 if( rc!=SQLITE_OK ){
1927 goto end_playback;
1928 }
danielk197776572402004-06-25 02:38:54 +00001929 }
1930
danielk197776572402004-06-25 02:38:54 +00001931 /* Copy original pages out of the journal and back into the database file.
1932 */
1933 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001934 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001935 if( rc!=SQLITE_OK ){
1936 if( rc==SQLITE_DONE ){
1937 rc = SQLITE_OK;
1938 pPager->journalOff = szJ;
1939 break;
1940 }else{
1941 goto end_playback;
1942 }
1943 }
drh968af522003-02-11 14:55:40 +00001944 }
drhed7c8552001-04-11 14:29:21 +00001945 }
drh580eeaf2006-02-24 03:09:37 +00001946 /*NOTREACHED*/
1947 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001948
1949end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001950 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001951 zMaster = pPager->pTmpSpace;
1952 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1953 }
1954 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001955 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001956 }
danielk197765839c62007-08-30 08:08:17 +00001957 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001958 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001959 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001960 */
danielk197765839c62007-08-30 08:08:17 +00001961 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001962 }
danielk197776572402004-06-25 02:38:54 +00001963
1964 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001965 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001966 ** value. Reset it to the correct value for this process.
1967 */
drhc80f0582007-05-01 16:59:48 +00001968 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001969 return rc;
drhed7c8552001-04-11 14:29:21 +00001970}
1971
1972/*
drhac69b052004-05-12 13:30:07 +00001973** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001974**
1975** This is similar to playing back the transaction journal but with
1976** a few extra twists.
1977**
drh663fc632002-02-02 18:49:19 +00001978** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001979** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001980** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001981**
drhac69b052004-05-12 13:30:07 +00001982** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001983** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001984** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001985*/
drh3aac2dd2004-04-26 14:10:20 +00001986static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001987 i64 szJ; /* Size of the full journal */
1988 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001989 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001990 int i; /* Loop counter */
1991 int rc;
1992
danielk197776572402004-06-25 02:38:54 +00001993 szJ = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001994
danielk19774099f6e2007-03-19 11:25:20 +00001995 /* Set hdrOff to be the offset just after the end of the last journal
1996 ** page written before the first journal-header for this statement
1997 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001998 ** header was written.
1999 */
2000 hdrOff = pPager->stmtHdrOff;
2001 assert( pPager->fullSync || !hdrOff );
2002 if( !hdrOff ){
2003 hdrOff = szJ;
2004 }
2005
drhfa86c412002-02-02 15:01:15 +00002006 /* Truncate the database back to its original size.
2007 */
danielk1977e180dd92007-04-05 17:15:52 +00002008 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00002009 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00002010
drhac69b052004-05-12 13:30:07 +00002011 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00002012 */
drhac69b052004-05-12 13:30:07 +00002013 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00002014 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00002015
drhac69b052004-05-12 13:30:07 +00002016 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00002017 ** database file. Note that the statement journal omits checksums from
2018 ** each record since power-failure recovery is not important to statement
2019 ** journals.
drhfa86c412002-02-02 15:01:15 +00002020 */
danielk197762079062007-08-15 17:08:46 +00002021 for(i=0; i<nRec; i++){
2022 i64 offset = i*(4+pPager->pageSize);
2023 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00002024 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00002025 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00002026 }
2027
danielk197776572402004-06-25 02:38:54 +00002028 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
2029 ** was the size of the journal file when this statement was started, so
2030 ** everything after that needs to be rolled back, either into the
2031 ** database, the memory cache, or both.
2032 **
2033 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
2034 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00002035 */
danielk197776572402004-06-25 02:38:54 +00002036 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00002037 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00002038 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00002039 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00002040 assert( rc!=SQLITE_DONE );
2041 if( rc!=SQLITE_OK ) goto end_stmt_playback;
2042 }
2043
2044 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00002045 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00002046 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00002047 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00002048 if( rc!=SQLITE_OK ){
2049 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00002050 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00002051 }
danielk1977f0113002006-01-24 12:09:17 +00002052 if( nJRec==0 ){
2053 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00002054 }
danielk1977f0113002006-01-24 12:09:17 +00002055 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00002056 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00002057 assert( rc!=SQLITE_DONE );
2058 if( rc!=SQLITE_OK ) goto end_stmt_playback;
2059 }
drhfa86c412002-02-02 15:01:15 +00002060 }
danielk197776572402004-06-25 02:38:54 +00002061
2062 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00002063
drh3aac2dd2004-04-26 14:10:20 +00002064end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00002065 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00002066 pPager->journalOff = szJ;
2067 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00002068 }
2069 return rc;
2070}
2071
2072/*
drhf57b14a2001-09-14 18:54:08 +00002073** Change the maximum number of in-memory pages that are allowed.
2074*/
danielk19773b8a05f2007-03-19 17:44:26 +00002075void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00002076 if( mxPage>10 ){
2077 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00002078 }else{
2079 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00002080 }
2081}
2082
2083/*
drh973b6e32003-02-12 14:09:42 +00002084** Adjust the robustness of the database to damage due to OS crashes
2085** or power failures by changing the number of syncs()s when writing
2086** the rollback journal. There are three levels:
2087**
drh054889e2005-11-30 03:20:31 +00002088** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00002089** for temporary and transient files.
2090**
2091** NORMAL The journal is synced once before writes begin on the
2092** database. This is normally adequate protection, but
2093** it is theoretically possible, though very unlikely,
2094** that an inopertune power failure could leave the journal
2095** in a state which would cause damage to the database
2096** when it is rolled back.
2097**
2098** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00002099** database (with some additional information - the nRec field
2100** of the journal header - being written in between the two
2101** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00002102** single disk sector is atomic, then this mode provides
2103** assurance that the journal will not be corrupted to the
2104** point of causing damage to the database during rollback.
2105**
2106** Numeric values associated with these states are OFF==1, NORMAL=2,
2107** and FULL=3.
2108*/
danielk197793758c82005-01-21 08:13:14 +00002109#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00002110void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00002111 pPager->noSync = level==1 || pPager->tempFile;
2112 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00002113 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00002114 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00002115}
danielk197793758c82005-01-21 08:13:14 +00002116#endif
drh973b6e32003-02-12 14:09:42 +00002117
2118/*
drhaf6df112005-06-07 02:12:30 +00002119** The following global variable is incremented whenever the library
2120** attempts to open a temporary file. This information is used for
2121** testing and analysis only.
2122*/
drh0f7eb612006-08-08 13:51:43 +00002123#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002124int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00002125#endif
drhaf6df112005-06-07 02:12:30 +00002126
2127/*
drh3f56e6e2007-03-15 01:16:47 +00002128** Open a temporary file.
2129**
2130** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00002131** other error code if we fail. The OS will automatically delete the temporary
2132** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00002133*/
danielk1977b4b47412007-08-17 15:53:36 +00002134static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00002135 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00002136 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00002137 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00002138){
drhfa86c412002-02-02 15:01:15 +00002139 int rc;
drh3f56e6e2007-03-15 01:16:47 +00002140
drh0f7eb612006-08-08 13:51:43 +00002141#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002142 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002143#endif
danielk1977b4b47412007-08-17 15:53:36 +00002144
drh33f4e022007-09-03 15:19:34 +00002145 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2146 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00002147 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00002148 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00002149 return rc;
2150}
2151
2152/*
drhed7c8552001-04-11 14:29:21 +00002153** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002154** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002155** the first call to sqlite3PagerGet() and is only held open until the
2156** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002157**
drh6446c4d2001-12-15 14:22:18 +00002158** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002159** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002160** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002161**
2162** If zFilename is ":memory:" then all information is held in cache.
2163** It is never written to disk. This can be used to implement an
2164** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002165*/
danielk19773b8a05f2007-03-19 17:44:26 +00002166int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002167 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002168 Pager **ppPager, /* Return the Pager structure here */
2169 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002170 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002171 int flags, /* flags controlling this file */
2172 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002173){
danielk1977b4b47412007-08-17 15:53:36 +00002174 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002175 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002176 int rc = SQLITE_OK;
2177 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002178 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002179 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002180 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002181 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2182 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002183 int journalFileSize = sqlite3JournalSize(pVfs);
drhfacf0302008-06-17 15:12:00 +00002184 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
danielk197717b90b52008-06-06 11:11:25 +00002185 char *zPathname = 0;
2186 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002187
drh86f8c192007-08-22 00:39:19 +00002188 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002189 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002190
danielk197717b90b52008-06-06 11:11:25 +00002191 /* Compute and store the full pathname in an allocated buffer pointed
2192 ** to by zPathname, length nPathname. Or, if this is a temporary file,
2193 ** leave both nPathname and zPathname set to 0.
2194 */
drh1cc8c442007-08-24 16:08:29 +00002195 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00002196 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00002197 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00002198 if( zPathname==0 ){
2199 return SQLITE_NOMEM;
2200 }
drh1cc8c442007-08-24 16:08:29 +00002201#ifndef SQLITE_OMIT_MEMORYDB
2202 if( strcmp(zFilename,":memory:")==0 ){
2203 memDb = 1;
2204 zPathname[0] = 0;
2205 }else
2206#endif
2207 {
danielk1977adfb9b02007-09-17 07:02:56 +00002208 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002209 }
danielk197717b90b52008-06-06 11:11:25 +00002210 if( rc!=SQLITE_OK ){
2211 sqlite3_free(zPathname);
2212 return rc;
2213 }
2214 nPathname = strlen(zPathname);
drhae28c012007-08-24 16:29:23 +00002215 }
drh99b90c32008-03-17 13:50:58 +00002216
danielk1977b4b47412007-08-17 15:53:36 +00002217 /* Allocate memory for the pager structure */
2218 pPager = sqlite3MallocZero(
2219 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00002220 journalFileSize + /* The journal file structure */
drh17c0ba22008-02-26 18:40:11 +00002221 pVfs->szOsFile * 3 + /* The main db and two journal files */
danielk197717b90b52008-06-06 11:11:25 +00002222 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00002223 );
2224 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002225 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002226 return SQLITE_NOMEM;
2227 }
2228 pPtr = (u8 *)&pPager[1];
drh33f4e022007-09-03 15:19:34 +00002229 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002230 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002231 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2232 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2233 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002234 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2235 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00002236 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00002237 if( zPathname ){
2238 memcpy(pPager->zFilename, zPathname, nPathname+1);
2239 sqlite3_free(zPathname);
2240 }
danielk1977b4b47412007-08-17 15:53:36 +00002241
drh153c62c2007-08-24 03:51:33 +00002242 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002243 */
drhae28c012007-08-24 16:29:23 +00002244 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002245 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2246 rc = SQLITE_CANTOPEN;
2247 }else{
drh1cc8c442007-08-24 16:08:29 +00002248 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002249 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2250 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002251 readOnly = (fout&SQLITE_OPEN_READONLY);
2252
2253 /* If the file was successfully opened for read/write access,
2254 ** choose a default page size in case we have to create the
2255 ** database file. The default page size is the maximum of:
2256 **
2257 ** + SQLITE_DEFAULT_PAGE_SIZE,
2258 ** + The value returned by sqlite3OsSectorSize()
2259 ** + The largest page size that can be written atomically.
2260 */
2261 if( rc==SQLITE_OK && !readOnly ){
2262 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
drhfacf0302008-06-17 15:12:00 +00002263 if( szPageDflt<iSectorSize ){
2264 szPageDflt = iSectorSize;
drh1cc8c442007-08-24 16:08:29 +00002265 }
danielk19779663b8f2007-08-24 11:52:28 +00002266#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002267 {
2268 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2269 int ii;
2270 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2271 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2272 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
drhfacf0302008-06-17 15:12:00 +00002273 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2274 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002275 }
danielk1977b4b47412007-08-17 15:53:36 +00002276 }
drh1cc8c442007-08-24 16:08:29 +00002277#endif
drhfacf0302008-06-17 15:12:00 +00002278 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2279 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
drh1cc8c442007-08-24 16:08:29 +00002280 }
danielk19778def5ea2004-06-16 10:39:52 +00002281 }
drhac69b052004-05-12 13:30:07 +00002282 }
drh1cc8c442007-08-24 16:08:29 +00002283 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002284 /* If a temporary file is requested, it is not opened immediately.
2285 ** In this case we accept the default page size and delay actually
2286 ** opening the file until the first call to OsWrite().
2287 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002288 tempFile = 1;
2289 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002290 }
danielk1977aef0bf62005-12-30 16:28:01 +00002291
danielk1977b4b47412007-08-17 15:53:36 +00002292 if( pPager && rc==SQLITE_OK ){
drhfacf0302008-06-17 15:12:00 +00002293 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00002294 }
danielk1977aef0bf62005-12-30 16:28:01 +00002295
drh153c62c2007-08-24 03:51:33 +00002296 /* If an error occured in either of the blocks above.
2297 ** Free the Pager structure and close the file.
2298 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002299 ** any Pager.errMask variables.
2300 */
danielk1977b4b47412007-08-17 15:53:36 +00002301 if( !pPager || !pPager->pTmpSpace ){
2302 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002303 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002304 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002305 }
danielk1977aef0bf62005-12-30 16:28:01 +00002306
drh153c62c2007-08-24 03:51:33 +00002307 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2308 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002309
danielk1977b4b47412007-08-17 15:53:36 +00002310 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002311 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002312 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002313 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002314
drh99b90c32008-03-17 13:50:58 +00002315 /* Fill in Pager.zJournal[] */
danielk197717b90b52008-06-06 11:11:25 +00002316 if( zPathname ){
2317 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2318 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
2319 }else{
2320 pPager->zJournal = 0;
2321 }
danielk1977b4b47412007-08-17 15:53:36 +00002322
drh3b59a5c2006-01-15 20:28:28 +00002323 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002324 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002325 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002326 /* pPager->stmtOpen = 0; */
2327 /* pPager->stmtInUse = 0; */
2328 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002329 pPager->dbSize = memDb-1;
drhfacf0302008-06-17 15:12:00 +00002330 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00002331 /* pPager->stmtSize = 0; */
2332 /* pPager->stmtJSize = 0; */
2333 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002334 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002335 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002336 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002337 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002338 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002339 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002340 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2341 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2342 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2343 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002344 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002345 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002346 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002347 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002348 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002349 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002350 /* pPager->pFirst = 0; */
2351 /* pPager->pFirstSynced = 0; */
2352 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002353 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk1977b53e4962008-06-04 06:45:59 +00002354 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk19777a2b1ee2007-08-21 14:27:01 +00002355 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002356 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002357 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002358 }
drh3b59a5c2006-01-15 20:28:28 +00002359 /* pPager->pBusyHandler = 0; */
2360 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002361 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002362#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002363 pPager->iInUseMM = 0;
2364 pPager->iInUseDB = 0;
2365 if( !memDb ){
drh26e4a8b2008-05-01 17:16:52 +00002366#ifndef SQLITE_MUTEX_NOOP
drh86f8c192007-08-22 00:39:19 +00002367 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00002368#endif
drh86f8c192007-08-22 00:39:19 +00002369 sqlite3_mutex_enter(mutex);
2370 pPager->pNext = sqlite3PagerList;
2371 if( sqlite3PagerList ){
2372 assert( sqlite3PagerList->pPrev==0 );
2373 sqlite3PagerList->pPrev = pPager;
2374 }
2375 pPager->pPrev = 0;
2376 sqlite3PagerList = pPager;
2377 sqlite3_mutex_leave(mutex);
2378 }
danielk197752622822006-01-09 09:59:49 +00002379#endif
drhed7c8552001-04-11 14:29:21 +00002380 return SQLITE_OK;
2381}
2382
2383/*
drh90f5ecb2004-07-22 01:19:35 +00002384** Set the busy handler function.
2385*/
danielk19773b8a05f2007-03-19 17:44:26 +00002386void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002387 pPager->pBusyHandler = pBusyHandler;
2388}
2389
2390/*
drh72f82862001-05-24 21:06:34 +00002391** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002392** when the reference count on each page reaches zero. The destructor can
2393** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002394**
danielk19773b8a05f2007-03-19 17:44:26 +00002395** The destructor is not called as a result sqlite3PagerClose().
2396** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002397*/
danielk19773b8a05f2007-03-19 17:44:26 +00002398void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002399 pPager->xDestructor = xDesc;
2400}
2401
2402/*
drha6abd042004-06-09 17:37:22 +00002403** Set the reinitializer for this pager. If not NULL, the reinitializer
2404** is called when the content of a page in cache is restored to its original
2405** value as a result of a rollback. The callback gives higher-level code
2406** an opportunity to restore the EXTRA section to agree with the restored
2407** page data.
2408*/
danielk19773b8a05f2007-03-19 17:44:26 +00002409void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002410 pPager->xReiniter = xReinit;
2411}
2412
2413/*
danielk1977a1644fd2007-08-29 12:31:25 +00002414** Set the page size to *pPageSize. If the suggest new page size is
2415** inappropriate, then an alternative page size is set to that
2416** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002417*/
danielk1977a1644fd2007-08-29 12:31:25 +00002418int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2419 int rc = SQLITE_OK;
2420 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002421 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002422 if( pageSize && pageSize!=pPager->pageSize
2423 && !pPager->memDb && pPager->nRef==0
2424 ){
drhfacf0302008-06-17 15:12:00 +00002425 char *pNew = (char *)sqlite3PageMalloc(pageSize);
danielk1977a1644fd2007-08-29 12:31:25 +00002426 if( !pNew ){
2427 rc = SQLITE_NOMEM;
2428 }else{
2429 pagerEnter(pPager);
2430 pager_reset(pPager);
2431 pPager->pageSize = pageSize;
2432 setSectorSize(pPager);
drhfacf0302008-06-17 15:12:00 +00002433 sqlite3PageFree(pPager->pTmpSpace);
danielk1977a1644fd2007-08-29 12:31:25 +00002434 pPager->pTmpSpace = pNew;
2435 pagerLeave(pPager);
2436 }
drh1c7880e2005-05-20 20:01:55 +00002437 }
danielk1977a1644fd2007-08-29 12:31:25 +00002438 *pPageSize = pPager->pageSize;
2439 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002440}
2441
2442/*
drh26b79942007-11-28 16:19:56 +00002443** Return a pointer to the "temporary page" buffer held internally
2444** by the pager. This is a buffer that is big enough to hold the
2445** entire content of a database page. This buffer is used internally
2446** during rollback and will be overwritten whenever a rollback
2447** occurs. But other modules are free to use it too, as long as
2448** no rollbacks are happening.
2449*/
2450void *sqlite3PagerTempSpace(Pager *pPager){
2451 return pPager->pTmpSpace;
2452}
2453
2454/*
drhf8e632b2007-05-08 14:51:36 +00002455** Attempt to set the maximum database page count if mxPage is positive.
2456** Make no changes if mxPage is zero or negative. And never reduce the
2457** maximum page count below the current size of the database.
2458**
2459** Regardless of mxPage, return the current maximum page count.
2460*/
2461int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2462 if( mxPage>0 ){
2463 pPager->mxPgno = mxPage;
2464 }
danielk1977ad0132d2008-06-07 08:58:22 +00002465 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002466 return pPager->mxPgno;
2467}
2468
2469/*
drhc9ac5ca2005-11-04 22:03:30 +00002470** The following set of routines are used to disable the simulated
2471** I/O error mechanism. These routines are used to avoid simulated
2472** errors in places where we do not care about errors.
2473**
2474** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2475** and generate no code.
2476*/
2477#ifdef SQLITE_TEST
2478extern int sqlite3_io_error_pending;
2479extern int sqlite3_io_error_hit;
2480static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002481void disable_simulated_io_errors(void){
2482 saved_cnt = sqlite3_io_error_pending;
2483 sqlite3_io_error_pending = -1;
2484}
2485void enable_simulated_io_errors(void){
2486 sqlite3_io_error_pending = saved_cnt;
2487}
2488#else
drh152410f2005-11-05 15:11:22 +00002489# define disable_simulated_io_errors()
2490# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002491#endif
2492
2493/*
drh90f5ecb2004-07-22 01:19:35 +00002494** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002495** that pDest points to.
2496**
2497** No error checking is done. The rational for this is that this function
2498** may be called even if the file does not exist or contain a header. In
2499** these cases sqlite3OsRead() will return an error, to which the correct
2500** response is to zero the memory at pDest and continue. A real IO error
2501** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002502*/
danielk19773b8a05f2007-03-19 17:44:26 +00002503int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002504 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002505 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002506 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2507 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002508 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002509 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002510 if( rc==SQLITE_IOERR_SHORT_READ ){
2511 rc = SQLITE_OK;
2512 }
drh90f5ecb2004-07-22 01:19:35 +00002513 }
drh551b7732006-11-06 21:20:25 +00002514 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002515}
2516
2517/*
drh5e00f6c2001-09-13 13:46:56 +00002518** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002519** pPager.
2520**
2521** If the PENDING_BYTE lies on the page directly after the end of the
2522** file, then consider this page part of the file too. For example, if
2523** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2524** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002525*/
danielk1977ad0132d2008-06-07 08:58:22 +00002526int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk19777a2b1ee2007-08-21 14:27:01 +00002527 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002528 int rc;
drhd9b02572001-04-15 00:37:09 +00002529 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002530 if( pPager->errCode ){
danielk1977ad0132d2008-06-07 08:58:22 +00002531 return pPager->errCode;
drha7aea3d2007-03-15 12:51:16 +00002532 }
drhed7c8552001-04-11 14:29:21 +00002533 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002534 n = pPager->dbSize;
2535 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002536 assert(pPager->fd->pMethods||pPager->tempFile);
2537 if( (pPager->fd->pMethods)
2538 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002539 pPager->nRef++;
drhe49f9822006-09-15 12:29:16 +00002540 pager_error(pPager, rc);
danielk1977ae72d982007-10-03 08:46:44 +00002541 pPager->nRef--;
danielk1977ad0132d2008-06-07 08:58:22 +00002542 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002543 }
2544 if( n>0 && n<pPager->pageSize ){
2545 n = 1;
2546 }else{
2547 n /= pPager->pageSize;
2548 }
2549 if( pPager->state!=PAGER_UNLOCK ){
2550 pPager->dbSize = n;
2551 }
drhed7c8552001-04-11 14:29:21 +00002552 }
danielk197715f411d2005-09-16 10:18:45 +00002553 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002554 n++;
2555 }
drhf8e632b2007-05-08 14:51:36 +00002556 if( n>pPager->mxPgno ){
2557 pPager->mxPgno = n;
2558 }
danielk1977ad0132d2008-06-07 08:58:22 +00002559 if( pnPage ){
2560 *pnPage = n;
2561 }
2562 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002563}
2564
drhf07e7d52006-04-07 13:54:46 +00002565
2566#ifndef SQLITE_OMIT_MEMORYDB
2567/*
2568** Clear a PgHistory block
2569*/
2570static void clearHistory(PgHistory *pHist){
drhfacf0302008-06-17 15:12:00 +00002571 sqlite3PageFree(pHist->pOrig);
2572 sqlite3PageFree(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002573 pHist->pOrig = 0;
2574 pHist->pStmt = 0;
2575}
2576#else
2577#define clearHistory(x)
2578#endif
2579
drhed7c8552001-04-11 14:29:21 +00002580/*
drhf7c57532003-04-25 13:22:51 +00002581** Forward declaration
2582*/
danielk197776572402004-06-25 02:38:54 +00002583static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002584
2585/*
drh85b623f2007-12-13 21:54:09 +00002586** Unlink pPg from its hash chain. Also set the page number to 0 to indicate
danielk1977f5fdda82004-11-03 08:44:05 +00002587** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002588** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002589** pNextFree/pPrevFree list that is not a part of any hash-chain.
2590*/
2591static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2592 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002593 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002594 return;
2595 }
2596 if( pPg->pNextHash ){
2597 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2598 }
2599 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002600 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002601 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2602 }else{
drh8ca0c722006-05-07 17:49:38 +00002603 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002604 pPager->aHash[h] = pPg->pNextHash;
2605 }
drh5229ae42006-03-23 23:29:04 +00002606 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002607 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2608 }
danielk1977f5fdda82004-11-03 08:44:05 +00002609 pPg->pgno = 0;
2610 pPg->pNextHash = pPg->pPrevHash = 0;
2611}
2612
2613/*
drhac69b052004-05-12 13:30:07 +00002614** Unlink a page from the free list (the list of all pages where nRef==0)
2615** and from its hash collision chain.
2616*/
2617static void unlinkPage(PgHdr *pPg){
2618 Pager *pPager = pPg->pPager;
2619
danielk19779f61c2f2007-08-27 17:27:49 +00002620 /* Unlink from free page list */
2621 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002622
2623 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002624 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002625}
2626
2627/*
danielk1977e180dd92007-04-05 17:15:52 +00002628** This routine is used to truncate the cache when a database
2629** is truncated. Drop from the cache all pages whose pgno is
2630** larger than pPager->dbSize and is unreferenced.
2631**
drhac69b052004-05-12 13:30:07 +00002632** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002633**
2634** Actually, at the point this routine is called, it would be
2635** an error to have a referenced page. But rather than delete
2636** that page and guarantee a subsequent segfault, it seems better
2637** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002638*/
danielk1977e180dd92007-04-05 17:15:52 +00002639static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002640 PgHdr *pPg;
2641 PgHdr **ppPg;
2642 int dbSize = pPager->dbSize;
2643
2644 ppPg = &pPager->pAll;
2645 while( (pPg = *ppPg)!=0 ){
2646 if( pPg->pgno<=dbSize ){
2647 ppPg = &pPg->pNextAll;
2648 }else if( pPg->nRef>0 ){
2649 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2650 ppPg = &pPg->pNextAll;
2651 }else{
2652 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002653 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2654 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002655 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002656 makeClean(pPg);
drhfacf0302008-06-17 15:12:00 +00002657 sqlite3PageFree(pPg->pData);
drh17435752007-08-16 04:30:38 +00002658 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002659 pPager->nPage--;
2660 }
2661 }
2662}
2663
drhf7c57532003-04-25 13:22:51 +00002664/*
danielk197717221812005-02-15 03:38:05 +00002665** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002666** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002667** false or until the lock succeeds.
2668**
2669** Return SQLITE_OK on success and an error code if we cannot obtain
2670** the lock.
2671*/
2672static int pager_wait_on_lock(Pager *pPager, int locktype){
2673 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002674
2675 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002676 assert( PAGER_SHARED==SHARED_LOCK );
2677 assert( PAGER_RESERVED==RESERVED_LOCK );
2678 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002679
2680 /* If the file is currently unlocked then the size must be unknown */
2681 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2682
danielk197717221812005-02-15 03:38:05 +00002683 if( pPager->state>=locktype ){
2684 rc = SQLITE_OK;
2685 }else{
drhbefdf832008-03-14 19:33:05 +00002686 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002687 do {
drh054889e2005-11-30 03:20:31 +00002688 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002689 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002690 if( rc==SQLITE_OK ){
2691 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002692 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002693 }
2694 }
2695 return rc;
2696}
2697
2698/*
drhf7c57532003-04-25 13:22:51 +00002699** Truncate the file to the number of pages specified.
2700*/
danielk19773b8a05f2007-03-19 17:44:26 +00002701int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002702 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002703 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk1977ad0132d2008-06-07 08:58:22 +00002704 sqlite3PagerPagecount(pPager, 0);
danielk1977efaaf572006-01-16 11:29:19 +00002705 if( pPager->errCode ){
2706 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002707 return rc;
2708 }
drh7d02cb72003-06-04 16:24:39 +00002709 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002710 return SQLITE_OK;
2711 }
drhb7f91642004-10-31 02:22:47 +00002712 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002713 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002714 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002715 return SQLITE_OK;
2716 }
drh86f8c192007-08-22 00:39:19 +00002717 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002718 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002719 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002720 if( rc!=SQLITE_OK ){
2721 return rc;
2722 }
danielk197717221812005-02-15 03:38:05 +00002723
2724 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002725 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002726 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002727 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002728 if( rc!=SQLITE_OK ){
2729 return rc;
2730 }
2731
drhcb4c40b2004-08-18 19:09:43 +00002732 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002733 return rc;
2734}
2735
2736/*
drhed7c8552001-04-11 14:29:21 +00002737** Shutdown the page cache. Free all memory and close all files.
2738**
2739** If a transaction was in progress when this routine is called, that
2740** transaction is rolled back. All outstanding pages are invalidated
2741** and their memory is freed. Any attempt to use a page associated
2742** with this page cache after this function returns will likely
2743** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002744**
2745** This function always succeeds. If a transaction is active an attempt
2746** is made to roll it back. If an error occurs during the rollback
2747** a hot journal may be left in the filesystem but no error is returned
2748** to the caller.
drhed7c8552001-04-11 14:29:21 +00002749*/
danielk19773b8a05f2007-03-19 17:44:26 +00002750int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002751#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002752 if( !MEMDB ){
drh26e4a8b2008-05-01 17:16:52 +00002753#ifndef SQLITE_MUTEX_NOOP
drh86f8c192007-08-22 00:39:19 +00002754 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00002755#endif
drh86f8c192007-08-22 00:39:19 +00002756 sqlite3_mutex_enter(mutex);
2757 if( pPager->pPrev ){
2758 pPager->pPrev->pNext = pPager->pNext;
2759 }else{
2760 sqlite3PagerList = pPager->pNext;
2761 }
2762 if( pPager->pNext ){
2763 pPager->pNext->pPrev = pPager->pPrev;
2764 }
2765 sqlite3_mutex_leave(mutex);
2766 }
danielk197713f72992005-12-18 08:51:22 +00002767#endif
2768
drhbafda092007-01-03 23:36:22 +00002769 disable_simulated_io_errors();
drh4873d5f2008-05-13 13:27:33 +00002770 sqlite3FaultBeginBenign(-1);
drhc2ee76c2007-01-04 14:58:14 +00002771 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002772 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002773 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002774 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002775 enable_simulated_io_errors();
drh4873d5f2008-05-13 13:27:33 +00002776 sqlite3FaultEndBenign(-1);
drh4f0c5872007-03-26 22:05:01 +00002777 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002778 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002779 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002780 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002781 }
drhf5e7bb52008-02-18 14:47:33 +00002782 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002783 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002784 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002785 }
danielk1977b4b47412007-08-17 15:53:36 +00002786 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002787 /* Temp files are automatically deleted by the OS
2788 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002789 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002790 ** }
2791 */
danielk1977aca790a2005-01-13 11:07:52 +00002792
drh17435752007-08-16 04:30:38 +00002793 sqlite3_free(pPager->aHash);
drhfacf0302008-06-17 15:12:00 +00002794 sqlite3PageFree(pPager->pTmpSpace);
drh17435752007-08-16 04:30:38 +00002795 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002796 return SQLITE_OK;
2797}
2798
drh87cc3b32007-05-08 21:45:27 +00002799#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002800/*
drh5e00f6c2001-09-13 13:46:56 +00002801** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002802*/
danielk19773b8a05f2007-03-19 17:44:26 +00002803Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002804 return p->pgno;
2805}
drh87cc3b32007-05-08 21:45:27 +00002806#endif
drhed7c8552001-04-11 14:29:21 +00002807
2808/*
drhc8629a12004-05-08 20:07:40 +00002809** The page_ref() function increments the reference count for a page.
2810** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002811** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002812**
2813** For non-test systems, page_ref() is a macro that calls _page_ref()
2814** online of the reference count is zero. For test systems, page_ref()
2815** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002816*/
drh836faa42003-01-11 13:30:57 +00002817static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002818 if( pPg->nRef==0 ){
2819 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002820 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002821 pPg->pPager->nRef++;
2822 }
2823 pPg->nRef++;
drhdf0b3b02001-06-23 11:36:20 +00002824}
danielk1977aca790a2005-01-13 11:07:52 +00002825#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002826 static void page_ref(PgHdr *pPg){
2827 if( pPg->nRef==0 ){
2828 _page_ref(pPg);
2829 }else{
2830 pPg->nRef++;
drhc8629a12004-05-08 20:07:40 +00002831 }
2832 }
2833#else
2834# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2835#endif
drhdf0b3b02001-06-23 11:36:20 +00002836
2837/*
2838** Increment the reference count for a page. The input pointer is
2839** a reference to the page data.
2840*/
danielk19773b8a05f2007-03-19 17:44:26 +00002841int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002842 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002843 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002844 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002845 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002846}
2847
2848/*
drh34e79ce2004-02-08 06:05:46 +00002849** Sync the journal. In other words, make sure all the pages that have
2850** been written to the journal have actually reached the surface of the
2851** disk. It is not safe to modify the original database file until after
2852** the journal has been synced. If the original database is modified before
2853** the journal is synced and a power failure occurs, the unsynced journal
2854** data would be lost and we would be unable to completely rollback the
2855** database changes. Database corruption would occur.
2856**
2857** This routine also updates the nRec field in the header of the journal.
2858** (See comments on the pager_playback() routine for additional information.)
2859** If the sync mode is FULL, two syncs will occur. First the whole journal
2860** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002861**
drh34e79ce2004-02-08 06:05:46 +00002862** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002863** after a power failure, so no sync occurs.
2864**
2865** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2866** the database is stored, then OsSync() is never called on the journal
2867** file. In this case all that is required is to update the nRec field in
2868** the journal header.
drhfa86c412002-02-02 15:01:15 +00002869**
drh34e79ce2004-02-08 06:05:46 +00002870** This routine clears the needSync field of every page current held in
2871** memory.
drh50e5dad2001-09-15 00:57:28 +00002872*/
danielk197776572402004-06-25 02:38:54 +00002873static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002874 PgHdr *pPg;
2875 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002876
2877 /* Sync the journal before modifying the main database
2878 ** (assuming there is a journal and it needs to be synced.)
2879 */
danielk197776572402004-06-25 02:38:54 +00002880 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002881 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002882 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002883 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002884
danielk19774cd2cd52007-08-23 14:48:23 +00002885 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002886 /* Write the nRec value into the journal file header. If in
2887 ** full-synchronous mode, sync the journal first. This ensures that
2888 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002889 ** it as a candidate for rollback.
2890 **
2891 ** This is not required if the persistent media supports the
2892 ** SAFE_APPEND property. Because in this case it is not possible
2893 ** for garbage data to be appended to the file, the nRec field
2894 ** is populated with 0xFFFFFFFF when the journal header is written
2895 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002896 */
danielk19774cd2cd52007-08-23 14:48:23 +00002897 i64 jrnlOff;
2898 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002899 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002900 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002901 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002902 if( rc!=0 ) return rc;
2903 }
danielk197713adf8a2004-06-03 16:08:41 +00002904
danielk197762079062007-08-15 17:08:46 +00002905 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2906 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2907 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002908 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002909 }
danielk19774cd2cd52007-08-23 14:48:23 +00002910 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2911 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2912 IOTRACE(("JSYNC %p\n", pPager))
2913 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2914 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2915 );
2916 if( rc!=0 ) return rc;
2917 }
drhdb48ee02003-01-16 13:42:43 +00002918 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002919 }
drh50e5dad2001-09-15 00:57:28 +00002920 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002921
2922 /* Erase the needSync flag from every page.
2923 */
2924 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2925 pPg->needSync = 0;
2926 }
danielk19779f61c2f2007-08-27 17:27:49 +00002927 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002928 }
drh03eb96a2002-11-10 23:32:56 +00002929
drh341eae82003-01-21 02:39:36 +00002930#ifndef NDEBUG
2931 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2932 ** flag must also be clear for all pages. Verify that this
2933 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002934 */
drh341eae82003-01-21 02:39:36 +00002935 else{
2936 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2937 assert( pPg->needSync==0 );
2938 }
danielk19779f61c2f2007-08-27 17:27:49 +00002939 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002940 }
drh341eae82003-01-21 02:39:36 +00002941#endif
drhdb48ee02003-01-16 13:42:43 +00002942
drh81a20f22001-10-12 17:30:04 +00002943 return rc;
drh50e5dad2001-09-15 00:57:28 +00002944}
2945
2946/*
drhdc3e10b2006-06-15 14:31:06 +00002947** Merge two lists of pages connected by pDirty and in pgno order.
2948** Do not both fixing the pPrevDirty pointers.
2949*/
2950static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2951 PgHdr result, *pTail;
2952 pTail = &result;
2953 while( pA && pB ){
2954 if( pA->pgno<pB->pgno ){
2955 pTail->pDirty = pA;
2956 pTail = pA;
2957 pA = pA->pDirty;
2958 }else{
2959 pTail->pDirty = pB;
2960 pTail = pB;
2961 pB = pB->pDirty;
2962 }
2963 }
2964 if( pA ){
2965 pTail->pDirty = pA;
2966 }else if( pB ){
2967 pTail->pDirty = pB;
2968 }else{
2969 pTail->pDirty = 0;
2970 }
2971 return result.pDirty;
2972}
2973
2974/*
2975** Sort the list of pages in accending order by pgno. Pages are
2976** connected by pDirty pointers. The pPrevDirty pointers are
2977** corrupted by this sort.
2978*/
danielk197724168722007-04-02 05:07:47 +00002979#define N_SORT_BUCKET_ALLOC 25
2980#define N_SORT_BUCKET 25
2981#ifdef SQLITE_TEST
2982 int sqlite3_pager_n_sort_bucket = 0;
2983 #undef N_SORT_BUCKET
2984 #define N_SORT_BUCKET \
2985 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2986#endif
drhdc3e10b2006-06-15 14:31:06 +00002987static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002988 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002989 int i;
2990 memset(a, 0, sizeof(a));
2991 while( pIn ){
2992 p = pIn;
2993 pIn = p->pDirty;
2994 p->pDirty = 0;
2995 for(i=0; i<N_SORT_BUCKET-1; i++){
2996 if( a[i]==0 ){
2997 a[i] = p;
2998 break;
2999 }else{
3000 p = merge_pagelist(a[i], p);
3001 a[i] = 0;
3002 }
3003 }
3004 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00003005 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
3006 ** elements in the input list. This is possible, but impractical.
3007 ** Testing this line is the point of global variable
3008 ** sqlite3_pager_n_sort_bucket.
3009 */
drhdc3e10b2006-06-15 14:31:06 +00003010 a[i] = merge_pagelist(a[i], p);
3011 }
3012 }
3013 p = a[0];
3014 for(i=1; i<N_SORT_BUCKET; i++){
3015 p = merge_pagelist(p, a[i]);
3016 }
3017 return p;
3018}
3019
3020/*
drh2554f8b2003-01-22 01:26:44 +00003021** Given a list of pages (connected by the PgHdr.pDirty pointer) write
3022** every one of those pages out to the database file and mark them all
3023** as clean.
3024*/
3025static int pager_write_pagelist(PgHdr *pList){
3026 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00003027 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00003028 int rc;
3029
3030 if( pList==0 ) return SQLITE_OK;
3031 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00003032
3033 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
3034 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00003035 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00003036 **
drha6abd042004-06-09 17:37:22 +00003037 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
3038 ** through an intermediate state PENDING. A PENDING lock prevents new
3039 ** readers from attaching to the database but is unsufficient for us to
3040 ** write. The idea of a PENDING lock is to prevent new readers from
3041 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00003042 **
drha6abd042004-06-09 17:37:22 +00003043 ** While the pager is in the RESERVED state, the original database file
3044 ** is unchanged and we can rollback without having to playback the
3045 ** journal into the original database file. Once we transition to
3046 ** EXCLUSIVE, it means the database file has been changed and any rollback
3047 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00003048 */
drh684917c2004-10-05 02:41:42 +00003049 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00003050 if( rc!=SQLITE_OK ){
3051 return rc;
3052 }
3053
drhdc3e10b2006-06-15 14:31:06 +00003054 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00003055 for(p=pList; p; p=p->pDirty){
3056 assert( p->dirty );
3057 p->dirty = 0;
3058 }
drh2554f8b2003-01-22 01:26:44 +00003059 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00003060
3061 /* If the file has not yet been opened, open it now. */
3062 if( !pPager->fd->pMethods ){
3063 assert(pPager->tempFile);
danielk197717b90b52008-06-06 11:11:25 +00003064 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00003065 if( rc ) return rc;
3066 }
3067
danielk1977687566d2004-11-02 12:56:41 +00003068 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00003069 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00003070 ** make the file smaller (presumably by auto-vacuum code). Do not write
3071 ** any such pages to the file.
3072 */
3073 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00003074 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00003075 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00003076 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
3077 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00003078 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00003079 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003080 PAGER_INCR(sqlite3_pager_writedb_count);
3081 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00003082 if( pList->pgno==1 ){
3083 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
3084 }
danielk1977687566d2004-11-02 12:56:41 +00003085 }
3086#ifndef NDEBUG
3087 else{
drh4f0c5872007-03-26 22:05:01 +00003088 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00003089 }
3090#endif
drh2554f8b2003-01-22 01:26:44 +00003091 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00003092#ifdef SQLITE_CHECK_PAGES
3093 pList->pageHash = pager_pagehash(pList);
3094#endif
drh2554f8b2003-01-22 01:26:44 +00003095 pList = pList->pDirty;
3096 }
3097 return SQLITE_OK;
3098}
3099
3100/*
3101** Collect every dirty page into a dirty list and
3102** return a pointer to the head of that list. All pages are
3103** collected even if they are still in use.
3104*/
3105static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh8f2e9a12008-02-26 14:46:04 +00003106
3107#ifndef NDEBUG
3108 /* Verify the sanity of the dirty list when we are running
3109 ** in debugging mode. This is expensive, so do not
3110 ** do this on a normal build. */
3111 int n1 = 0;
3112 int n2 = 0;
3113 PgHdr *p;
3114 for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; }
3115 for(p=pPager->pDirty; p; p=p->pDirty){ n2++; }
3116 assert( n1==n2 );
3117#endif
3118
drh605ad8f2006-05-03 23:34:05 +00003119 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00003120}
3121
3122/*
drh19db9352008-03-27 22:42:51 +00003123** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00003124** A hot journal is one that needs to be played back.
3125**
3126** If the current size of the database file is 0 but a journal file
3127** exists, that is probably an old journal left over from a prior
3128** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00003129**
3130** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00003131**
3132** This routine does not open the journal file to examine its
3133** content. Hence, the journal might contain the name of a master
3134** journal file that has been deleted, and hence not be hot. Or
3135** the header of the journal might be zeroed out. This routine
3136** does not discover these cases of a non-hot journal - if the
3137** journal file exists and is not empty this routine assumes it
3138** is hot. The pager_playback() routine will discover that the
3139** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00003140*/
3141static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003142 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977861f7452008-06-05 11:39:11 +00003143 int res = 0;
3144 if( pPager->useJournal && pPager->fd->pMethods ){
3145 int rc;
3146 int exists;
3147 int locked;
3148
3149 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
3150 if( rc==SQLITE_OK && exists ){
3151 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
3152 }
3153
3154 if( rc==SQLITE_OK && exists && !locked ){
danielk1977ad0132d2008-06-07 08:58:22 +00003155 int nPage;
3156 rc = sqlite3PagerPagecount(pPager, &nPage);
3157 if( rc==SQLITE_OK && nPage==0 ){
danielk1977861f7452008-06-05 11:39:11 +00003158 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3159 exists = 0;
3160 }
3161 }
3162
3163 res = (rc!=SQLITE_OK ? -1 : (exists && !locked));
drhbb5f18d2007-04-06 18:23:17 +00003164 }
danielk1977861f7452008-06-05 11:39:11 +00003165
3166 return res;
drh165ffe92005-03-15 17:09:30 +00003167}
3168
danielk19775591df52005-12-20 09:19:37 +00003169/*
danielk1977aef0bf62005-12-30 16:28:01 +00003170** Try to find a page in the cache that can be recycled.
3171**
3172** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00003173** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00003174*/
danielk1977843e65f2007-09-01 16:16:15 +00003175static int pager_recycle(Pager *pPager, PgHdr **ppPg){
danielk197713f72992005-12-18 08:51:22 +00003176 PgHdr *pPg;
3177 *ppPg = 0;
3178
danielk1977843e65f2007-09-01 16:16:15 +00003179 /* It is illegal to call this function unless the pager object
3180 ** pointed to by pPager has at least one free page (page with nRef==0).
3181 */
danielk1977c551edc2007-04-05 13:12:13 +00003182 assert(!MEMDB);
danielk1977843e65f2007-09-01 16:16:15 +00003183 assert(pPager->lru.pFirst);
danielk1977c551edc2007-04-05 13:12:13 +00003184
danielk197713f72992005-12-18 08:51:22 +00003185 /* Find a page to recycle. Try to locate a page that does not
3186 ** require us to do an fsync() on the journal.
3187 */
danielk19779f61c2f2007-08-27 17:27:49 +00003188 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00003189
3190 /* If we could not find a page that does not require an fsync()
3191 ** on the journal file then fsync the journal file. This is a
3192 ** very slow operation, so we work hard to avoid it. But sometimes
3193 ** it can't be helped.
3194 */
danielk1977c41cc392008-05-15 08:34:54 +00003195 if( pPg==0 && pPager->lru.pFirst ){
3196 if( !pPager->errCode ){
3197 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
3198 int rc = syncJournal(pPager);
danielk197713f72992005-12-18 08:51:22 +00003199 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003200 return rc;
danielk197713f72992005-12-18 08:51:22 +00003201 }
danielk1977c41cc392008-05-15 08:34:54 +00003202 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
3203 /* If in full-sync mode, write a new journal header into the
3204 ** journal file. This is done to avoid ever modifying a journal
3205 ** header that is involved in the rollback of pages that have
3206 ** already been written to the database (in case the header is
3207 ** trashed when the nRec field is updated).
3208 */
3209 pPager->nRec = 0;
3210 assert( pPager->journalOff > 0 );
3211 assert( pPager->doNotSync==0 );
3212 rc = writeJournalHdr(pPager);
3213 if( rc!=0 ){
3214 return rc;
3215 }
3216 }
danielk197713f72992005-12-18 08:51:22 +00003217 }
danielk19779f61c2f2007-08-27 17:27:49 +00003218 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003219 }
danielk197713f72992005-12-18 08:51:22 +00003220
3221 assert( pPg->nRef==0 );
3222
3223 /* Write the page to the database file if it is dirty.
3224 */
danielk1977c41cc392008-05-15 08:34:54 +00003225 if( pPg->dirty && !pPager->errCode ){
danielk197713f72992005-12-18 08:51:22 +00003226 int rc;
3227 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003228 makeClean(pPg);
3229 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003230 pPg->pDirty = 0;
3231 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003232 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003233 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003234 return rc;
danielk197713f72992005-12-18 08:51:22 +00003235 }
3236 }
danielk1977c41cc392008-05-15 08:34:54 +00003237 assert( pPg->dirty==0 || pPager->errCode );
danielk197713f72992005-12-18 08:51:22 +00003238
3239 /* If the page we are recycling is marked as alwaysRollback, then
3240 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003241 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003242 ** It is necessary to do this because the page marked alwaysRollback
3243 ** might be reloaded at a later time but at that point we won't remember
3244 ** that is was marked alwaysRollback. This means that all pages must
3245 ** be marked as alwaysRollback from here on out.
3246 */
3247 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003248 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003249 pPager->alwaysRollback = 1;
3250 }
3251
3252 /* Unlink the old page from the free list and the hash table
3253 */
3254 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003255 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003256
3257 *ppPg = pPg;
3258 return SQLITE_OK;
3259}
3260
drh86f8c192007-08-22 00:39:19 +00003261#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003262/*
3263** This function is called to free superfluous dynamically allocated memory
3264** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003265** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003266**
3267** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003268** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003269** of bytes of memory released.
3270*/
danielk19773b8a05f2007-03-19 17:44:26 +00003271int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003272 int nReleased = 0; /* Bytes of memory released so far */
drh86f8c192007-08-22 00:39:19 +00003273 Pager *pPager; /* For looping over pagers */
drhe5fe6902007-12-07 18:55:28 +00003274 BusyHandler *savedBusy; /* Saved copy of the busy handler */
danielk19779f61c2f2007-08-27 17:27:49 +00003275 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003276
drh86f8c192007-08-22 00:39:19 +00003277 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003278 */
drh26e4a8b2008-05-01 17:16:52 +00003279#ifndef SQLITE_MUTEX_NOOP
drh64e2bb72008-05-07 12:29:55 +00003280 sqlite3_mutex *mutex; /* The MEM2 mutex */
drh86f8c192007-08-22 00:39:19 +00003281 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00003282#endif
drh86f8c192007-08-22 00:39:19 +00003283 sqlite3_mutex_enter(mutex);
3284
3285 /* Signal all database connections that memory management wants
3286 ** to have access to the pagers.
3287 */
3288 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3289 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003290 }
3291
danielk19779f61c2f2007-08-27 17:27:49 +00003292 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3293 PgHdr *pPg;
3294 PgHdr *pRecycled;
3295
3296 /* Try to find a page to recycle that does not require a sync(). If
3297 ** this is not possible, find one that does require a sync().
3298 */
3299 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3300 pPg = sqlite3LruPageList.pFirstSynced;
3301 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3302 pPg = pPg->gfree.pNext;
3303 }
3304 if( !pPg ){
3305 pPg = sqlite3LruPageList.pFirst;
3306 while( pPg && pPg->pPager->iInUseDB ){
3307 pPg = pPg->gfree.pNext;
3308 }
3309 }
3310 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003311
danielk197784f786f2007-08-28 08:00:17 +00003312 /* If pPg==0, then the block above has failed to find a page to
3313 ** recycle. In this case return early - no further memory will
3314 ** be released.
3315 */
danielk19779f61c2f2007-08-27 17:27:49 +00003316 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003317
danielk19779f61c2f2007-08-27 17:27:49 +00003318 pPager = pPg->pPager;
3319 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3320 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3321
drhe5fe6902007-12-07 18:55:28 +00003322 savedBusy = pPager->pBusyHandler;
3323 pPager->pBusyHandler = 0;
danielk1977843e65f2007-09-01 16:16:15 +00003324 rc = pager_recycle(pPager, &pRecycled);
drhe5fe6902007-12-07 18:55:28 +00003325 pPager->pBusyHandler = savedBusy;
danielk19779f61c2f2007-08-27 17:27:49 +00003326 assert(pRecycled==pPg || rc!=SQLITE_OK);
3327 if( rc==SQLITE_OK ){
3328 /* We've found a page to free. At this point the page has been
3329 ** removed from the page hash-table, free-list and synced-list
3330 ** (pFirstSynced). It is still in the all pages (pAll) list.
3331 ** Remove it from this list before freeing.
3332 **
3333 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3334 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003335 */
danielk19779f61c2f2007-08-27 17:27:49 +00003336 PgHdr *pTmp;
3337 assert( pPg );
3338 if( pPg==pPager->pAll ){
3339 pPager->pAll = pPg->pNextAll;
3340 }else{
3341 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3342 pTmp->pNextAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00003343 }
danielk19779f61c2f2007-08-27 17:27:49 +00003344 nReleased += (
3345 sizeof(*pPg) + pPager->pageSize
3346 + sizeof(u32) + pPager->nExtra
3347 + MEMDB*sizeof(PgHistory)
3348 );
3349 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3350 PAGER_INCR(sqlite3_pager_pgfree_count);
drhfacf0302008-06-17 15:12:00 +00003351 sqlite3PageFree(pPg->pData);
danielk19779f61c2f2007-08-27 17:27:49 +00003352 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003353 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003354 }else{
3355 /* An error occured whilst writing to the database file or
3356 ** journal in pager_recycle(). The error is not returned to the
3357 ** caller of this function. Instead, set the Pager.errCode variable.
3358 ** The error will be returned to the user (or users, in the case
3359 ** of a shared pager cache) of the pager for which the error occured.
3360 */
3361 assert(
3362 (rc&0xff)==SQLITE_IOERR ||
3363 rc==SQLITE_FULL ||
3364 rc==SQLITE_BUSY
3365 );
3366 assert( pPager->state>=PAGER_RESERVED );
3367 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003368 }
3369 }
danielk1977aef0bf62005-12-30 16:28:01 +00003370
drh86f8c192007-08-22 00:39:19 +00003371 /* Clear the memory management flags and release the mutex
3372 */
3373 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3374 pPager->iInUseMM = 0;
3375 }
3376 sqlite3_mutex_leave(mutex);
3377
3378 /* Return the number of bytes released
3379 */
danielk197713f72992005-12-18 08:51:22 +00003380 return nReleased;
3381}
drh86f8c192007-08-22 00:39:19 +00003382#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003383
drh165ffe92005-03-15 17:09:30 +00003384/*
danielk1977e180dd92007-04-05 17:15:52 +00003385** Read the content of page pPg out of the database file.
3386*/
3387static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3388 int rc;
danielk197762079062007-08-15 17:08:46 +00003389 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003390 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003391 assert(pPager->fd->pMethods||pPager->tempFile);
3392 if( !pPager->fd->pMethods ){
3393 return SQLITE_IOERR_SHORT_READ;
3394 }
danielk197762079062007-08-15 17:08:46 +00003395 offset = (pgno-1)*(i64)pPager->pageSize;
3396 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003397 PAGER_INCR(sqlite3_pager_readdb_count);
3398 PAGER_INCR(pPager->nRead);
3399 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003400 if( pgno==1 ){
3401 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3402 sizeof(pPager->dbFileVers));
3403 }
danielk1977e180dd92007-04-05 17:15:52 +00003404 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003405 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3406 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003407 return rc;
3408}
3409
3410
3411/*
danielk1977e277be02007-03-23 18:12:06 +00003412** This function is called to obtain the shared lock required before
3413** data may be read from the pager cache. If the shared lock has already
3414** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003415**
3416** Immediately after obtaining the shared lock (if required), this function
3417** checks for a hot-journal file. If one is found, an emergency rollback
3418** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003419*/
3420static int pagerSharedLock(Pager *pPager){
3421 int rc = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00003422 int isHot = 0;
danielk1977e277be02007-03-23 18:12:06 +00003423
danielk1977ae72d982007-10-03 08:46:44 +00003424 /* If this database is opened for exclusive access, has no outstanding
3425 ** page references and is in an error-state, now is the chance to clear
3426 ** the error. Discard the contents of the pager-cache and treat any
3427 ** open journal file as a hot-journal.
3428 */
3429 if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
3430 if( pPager->journalOpen ){
3431 isHot = 1;
3432 }
danielk1977ae72d982007-10-03 08:46:44 +00003433 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00003434 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00003435 }
3436
3437 /* If the pager is still in an error state, do not proceed. The error
3438 ** state will be cleared at some point in the future when all page
3439 ** references are dropped and the cache can be discarded.
3440 */
3441 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3442 return pPager->errCode;
3443 }
3444
3445 if( pPager->state==PAGER_UNLOCK || isHot ){
danielk1977b4b47412007-08-17 15:53:36 +00003446 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003447 if( !MEMDB ){
3448 assert( pPager->nRef==0 );
3449 if( !pPager->noReadlock ){
3450 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3451 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003452 assert( pPager->state==PAGER_UNLOCK );
danielk1977e277be02007-03-23 18:12:06 +00003453 return pager_error(pPager, rc);
3454 }
3455 assert( pPager->state>=SHARED_LOCK );
3456 }
3457
3458 /* If a journal file exists, and there is no RESERVED lock on the
3459 ** database file, then it either needs to be played back or deleted.
3460 */
drh19db9352008-03-27 22:42:51 +00003461 rc = hasHotJournal(pPager);
3462 if( rc<0 ){
danielk197752b472a2008-05-05 16:23:55 +00003463 rc = SQLITE_IOERR_NOMEM;
3464 goto failed;
drh19db9352008-03-27 22:42:51 +00003465 }
3466 if( rc==1 || isHot ){
danielk1977e277be02007-03-23 18:12:06 +00003467 /* Get an EXCLUSIVE lock on the database file. At this point it is
3468 ** important that a RESERVED lock is not obtained on the way to the
3469 ** EXCLUSIVE lock. If it were, another process might open the
3470 ** database file, detect the RESERVED lock, and conclude that the
3471 ** database is safe to read while this process is still rolling it
3472 ** back.
3473 **
3474 ** Because the intermediate RESERVED lock is not requested, the
3475 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00003476 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00003477 */
danielk1977ae72d982007-10-03 08:46:44 +00003478 if( pPager->state<EXCLUSIVE_LOCK ){
3479 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3480 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003481 rc = pager_error(pPager, rc);
3482 goto failed;
danielk1977ae72d982007-10-03 08:46:44 +00003483 }
3484 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00003485 }
danielk1977e277be02007-03-23 18:12:06 +00003486
drh16e45a42008-04-19 20:34:18 +00003487 /* Open the journal for read/write access. This is because in
drhfd131da2007-08-07 17:13:03 +00003488 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003489 ** possibly used for a transaction later on. On some systems, the
3490 ** OsTruncate() call used in exclusive-access mode also requires
3491 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003492 */
drh16e45a42008-04-19 20:34:18 +00003493 if( !isHot && pPager->journalOpen==0 ){
danielk1977861f7452008-06-05 11:39:11 +00003494 int res;
3495 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
3496 if( rc==SQLITE_OK ){
3497 if( res ){
3498 int fout = 0;
3499 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3500 assert( !pPager->tempFile );
3501 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3502 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3503 if( fout&SQLITE_OPEN_READONLY ){
3504 rc = SQLITE_BUSY;
3505 sqlite3OsClose(pPager->jfd);
3506 }
3507 }else{
3508 /* If the journal does not exist, that means some other process
3509 ** has already rolled it back */
danielk1977ae72d982007-10-03 08:46:44 +00003510 rc = SQLITE_BUSY;
danielk1977ae72d982007-10-03 08:46:44 +00003511 }
danielk1977979f38e2007-03-27 16:19:51 +00003512 }
3513 }
danielk1977e277be02007-03-23 18:12:06 +00003514 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003515 if( rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_UNLOCK
3516 && rc!=SQLITE_IOERR_NOMEM
3517 ){
3518 rc = SQLITE_BUSY;
drh1aa5af12008-03-07 19:51:14 +00003519 }
danielk197752b472a2008-05-05 16:23:55 +00003520 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003521 }
3522 pPager->journalOpen = 1;
3523 pPager->journalStarted = 0;
3524 pPager->journalOff = 0;
3525 pPager->setMaster = 0;
3526 pPager->journalHdr = 0;
3527
3528 /* Playback and delete the journal. Drop the database write
3529 ** lock and reacquire the read lock.
3530 */
3531 rc = pager_playback(pPager, 1);
3532 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003533 rc = pager_error(pPager, rc);
3534 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003535 }
danielk1977c5859712007-03-26 12:26:27 +00003536 assert(pPager->state==PAGER_SHARED ||
3537 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3538 );
danielk1977e277be02007-03-23 18:12:06 +00003539 }
3540
3541 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003542 /* The shared-lock has just been acquired on the database file
3543 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003544 ** read or write transaction). Check to see if the database
3545 ** has been modified. If the database has changed, flush the
3546 ** cache.
3547 **
3548 ** Database changes is detected by looking at 15 bytes beginning
3549 ** at offset 24 into the file. The first 4 of these 16 bytes are
3550 ** a 32-bit counter that is incremented with each change. The
3551 ** other bytes change randomly with each file change when
3552 ** a codec is in use.
3553 **
3554 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003555 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003556 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003557 */
drh86a88112007-04-16 15:02:19 +00003558 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977ad0132d2008-06-07 08:58:22 +00003559 sqlite3PagerPagecount(pPager, 0);
danielk197724168722007-04-02 05:07:47 +00003560
danielk1977e180dd92007-04-05 17:15:52 +00003561 if( pPager->errCode ){
danielk197752b472a2008-05-05 16:23:55 +00003562 rc = pPager->errCode;
3563 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003564 }
3565
danielk1977e180dd92007-04-05 17:15:52 +00003566 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003567 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003568 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003569 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003570 goto failed;
danielk1977e180dd92007-04-05 17:15:52 +00003571 }
drh86a88112007-04-16 15:02:19 +00003572 }else{
3573 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003574 }
3575
drh86a88112007-04-16 15:02:19 +00003576 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003577 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003578 }
3579 }
3580 }
danielk1977c5859712007-03-26 12:26:27 +00003581 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3582 if( pPager->state==PAGER_UNLOCK ){
3583 pPager->state = PAGER_SHARED;
3584 }
danielk1977e277be02007-03-23 18:12:06 +00003585 }
3586
danielk197752b472a2008-05-05 16:23:55 +00003587 failed:
3588 if( rc!=SQLITE_OK ){
3589 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
3590 pager_unlock(pPager);
3591 }
danielk1977e277be02007-03-23 18:12:06 +00003592 return rc;
3593}
3594
3595/*
danielk1977e180dd92007-04-05 17:15:52 +00003596** Allocate a PgHdr object. Either create a new one or reuse
3597** an existing one that is not otherwise in use.
3598**
3599** A new PgHdr structure is created if any of the following are
3600** true:
3601**
3602** (1) We have not exceeded our maximum allocated cache size
3603** as set by the "PRAGMA cache_size" command.
3604**
3605** (2) There are no unused PgHdr objects available at this time.
3606**
3607** (3) This is an in-memory database.
3608**
3609** (4) There are no PgHdr objects that do not require a journal
3610** file sync and a sync of the journal file is currently
3611** prohibited.
3612**
3613** Otherwise, reuse an existing PgHdr. In other words, reuse an
3614** existing PgHdr if all of the following are true:
3615**
3616** (1) We have reached or exceeded the maximum cache size
3617** allowed by "PRAGMA cache_size".
3618**
3619** (2) There is a PgHdr available with PgHdr->nRef==0
3620**
3621** (3) We are not in an in-memory database
3622**
3623** (4) Either there is an available PgHdr that does not need
3624** to be synced to disk or else disk syncing is currently
3625** allowed.
danielk197724168722007-04-02 05:07:47 +00003626*/
3627static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3628 int rc = SQLITE_OK;
3629 PgHdr *pPg;
drh1e3af332007-10-20 13:17:54 +00003630 int nByteHdr;
danielk197724168722007-04-02 05:07:47 +00003631
danielk1977e180dd92007-04-05 17:15:52 +00003632 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003633 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003634 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003635 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003636 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003637 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003638 ){
drh0d180202008-02-14 23:26:56 +00003639 void *pData;
danielk197724168722007-04-02 05:07:47 +00003640 if( pPager->nPage>=pPager->nHash ){
3641 pager_resize_hash_table(pPager,
3642 pPager->nHash<256 ? 256 : pPager->nHash*2);
3643 if( pPager->nHash==0 ){
3644 rc = SQLITE_NOMEM;
3645 goto pager_allocate_out;
3646 }
3647 }
drhed138fb2007-08-22 22:04:37 +00003648 pagerLeave(pPager);
drh1e3af332007-10-20 13:17:54 +00003649 nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra
3650 + MEMDB*sizeof(PgHistory);
drhe5ae5732008-06-15 02:51:47 +00003651 pPg = sqlite3Malloc( nByteHdr );
drh0d180202008-02-14 23:26:56 +00003652 if( pPg ){
drhfacf0302008-06-17 15:12:00 +00003653 pData = sqlite3PageMalloc( pPager->pageSize );
drh0d180202008-02-14 23:26:56 +00003654 if( pData==0 ){
3655 sqlite3_free(pPg);
3656 pPg = 0;
3657 }
3658 }
drhed138fb2007-08-22 22:04:37 +00003659 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003660 if( pPg==0 ){
3661 rc = SQLITE_NOMEM;
3662 goto pager_allocate_out;
3663 }
drh1e3af332007-10-20 13:17:54 +00003664 memset(pPg, 0, nByteHdr);
drh0d180202008-02-14 23:26:56 +00003665 pPg->pData = pData;
danielk197724168722007-04-02 05:07:47 +00003666 pPg->pPager = pPager;
3667 pPg->pNextAll = pPager->pAll;
3668 pPager->pAll = pPg;
3669 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003670 }else{
3671 /* Recycle an existing page with a zero ref-count. */
danielk1977843e65f2007-09-01 16:16:15 +00003672 rc = pager_recycle(pPager, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003673 if( rc==SQLITE_BUSY ){
3674 rc = SQLITE_IOERR_BLOCKED;
3675 }
danielk197724168722007-04-02 05:07:47 +00003676 if( rc!=SQLITE_OK ){
3677 goto pager_allocate_out;
3678 }
3679 assert( pPager->state>=SHARED_LOCK );
3680 assert(pPg);
3681 }
3682 *ppPg = pPg;
3683
3684pager_allocate_out:
3685 return rc;
3686}
3687
3688/*
drhd33d5a82007-04-26 12:11:28 +00003689** Make sure we have the content for a page. If the page was
3690** previously acquired with noContent==1, then the content was
3691** just initialized to zeros instead of being read from disk.
3692** But now we need the real data off of disk. So make sure we
3693** have it. Read it in if we do not have it already.
3694*/
3695static int pager_get_content(PgHdr *pPg){
3696 if( pPg->needRead ){
3697 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3698 if( rc==SQLITE_OK ){
3699 pPg->needRead = 0;
3700 }else{
3701 return rc;
3702 }
3703 }
3704 return SQLITE_OK;
3705}
3706
3707/*
drhd9b02572001-04-15 00:37:09 +00003708** Acquire a page.
3709**
drh58a11682001-11-10 13:51:08 +00003710** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003711** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003712**
drhd33d5a82007-04-26 12:11:28 +00003713** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003714** file is smaller than the requested page, then no actual disk
3715** read occurs and the memory image of the page is initialized to
3716** all zeros. The extra data appended to a page is always initialized
3717** to zeros the first time a page is loaded into memory.
3718**
drhd9b02572001-04-15 00:37:09 +00003719** The acquisition might fail for several reasons. In all cases,
3720** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003721**
drhd33d5a82007-04-26 12:11:28 +00003722** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003723** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003724** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003725** just returns 0. This routine acquires a read-lock the first time it
3726** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003727** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003728** or journal files.
drh0787db62007-03-04 13:15:27 +00003729**
drh538f5702007-04-13 02:14:30 +00003730** If noContent is false, the page contents are actually read from disk.
3731** If noContent is true, it means that we do not care about the contents
3732** of the page at this time, so do not do a disk read. Just fill in the
3733** page content with zeros. But mark the fact that we have not read the
3734** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003735** sqlite3PagerWrite() is called on this page or if this routine is
3736** called again with noContent==0, that means that the content is needed
3737** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003738*/
drh86f8c192007-08-22 00:39:19 +00003739static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003740 Pager *pPager, /* The pager open on the database file */
3741 Pgno pgno, /* Page number to fetch */
3742 DbPage **ppPage, /* Write a pointer to the page here */
3743 int noContent /* Do not bother reading content from disk if true */
3744){
drhed7c8552001-04-11 14:29:21 +00003745 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003746 int rc;
drhed7c8552001-04-11 14:29:21 +00003747
danielk1977e277be02007-03-23 18:12:06 +00003748 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3749
danielk197726836652005-01-17 01:33:13 +00003750 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3751 ** number greater than this, or zero, is requested.
3752 */
drh267cb322005-09-16 17:16:52 +00003753 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003754 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003755 }
3756
drhd9b02572001-04-15 00:37:09 +00003757 /* Make sure we have not hit any critical errors.
3758 */
drh836faa42003-01-11 13:30:57 +00003759 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003760 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003761
danielk197713adf8a2004-06-03 16:08:41 +00003762 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003763 ** on the database file. pagerSharedLock() is a no-op if
3764 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003765 */
danielk1977e277be02007-03-23 18:12:06 +00003766 rc = pagerSharedLock(pPager);
3767 if( rc!=SQLITE_OK ){
3768 return rc;
drhed7c8552001-04-11 14:29:21 +00003769 }
danielk1977e277be02007-03-23 18:12:06 +00003770 assert( pPager->state!=PAGER_UNLOCK );
3771
3772 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003773 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003774 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003775 int nMax;
drhed7c8552001-04-11 14:29:21 +00003776 int h;
drh538f5702007-04-13 02:14:30 +00003777 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003778 rc = pagerAllocatePage(pPager, &pPg);
3779 if( rc!=SQLITE_OK ){
3780 return rc;
drhed7c8552001-04-11 14:29:21 +00003781 }
danielk197724168722007-04-02 05:07:47 +00003782
drhed7c8552001-04-11 14:29:21 +00003783 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003784 assert( !MEMDB || pgno>pPager->stmtSize );
drhf5e7bb52008-02-18 14:47:33 +00003785 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
3786 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003787
drh605ad8f2006-05-03 23:34:05 +00003788 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003789 pPg->nRef = 1;
danielk197775bab7d2006-01-23 13:09:45 +00003790
drhd9b02572001-04-15 00:37:09 +00003791 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003792 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003793 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003794 }
danielk1977ad0132d2008-06-07 08:58:22 +00003795 rc = sqlite3PagerPagecount(pPager, &nMax);
3796 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00003797 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003798 return rc;
3799 }
danielk197775bab7d2006-01-23 13:09:45 +00003800
3801 /* Populate the page with data, either by reading from the database
3802 ** file, or by setting the entire page to zero.
3803 */
drhd215acf2007-04-13 04:01:58 +00003804 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003805 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003806 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003807 return SQLITE_FULL;
3808 }
drh90f5ecb2004-07-22 01:19:35 +00003809 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003810 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003811 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003812 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003813 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003814 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3815 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003816 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003817 return rc;
drh81a20f22001-10-12 17:30:04 +00003818 }
danielk1977b4626a32007-04-28 15:47:43 +00003819 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003820 }
danielk197775bab7d2006-01-23 13:09:45 +00003821
3822 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003823 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003824 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003825 pPg->pNextHash = pPager->aHash[h];
3826 pPager->aHash[h] = pPg;
3827 if( pPg->pNextHash ){
3828 assert( pPg->pNextHash->pPrevHash==0 );
3829 pPg->pNextHash->pPrevHash = pPg;
3830 }
3831
danielk19773c407372005-02-15 02:54:14 +00003832#ifdef SQLITE_CHECK_PAGES
3833 pPg->pageHash = pager_pagehash(pPg);
3834#endif
drhed7c8552001-04-11 14:29:21 +00003835 }else{
drhd9b02572001-04-15 00:37:09 +00003836 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003837 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003838 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003839 if( !noContent ){
3840 rc = pager_get_content(pPg);
3841 if( rc ){
3842 return rc;
3843 }
3844 }
drhdf0b3b02001-06-23 11:36:20 +00003845 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003846 }
danielk19773b8a05f2007-03-19 17:44:26 +00003847 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003848 return SQLITE_OK;
3849}
drh86f8c192007-08-22 00:39:19 +00003850int sqlite3PagerAcquire(
3851 Pager *pPager, /* The pager open on the database file */
3852 Pgno pgno, /* Page number to fetch */
3853 DbPage **ppPage, /* Write a pointer to the page here */
3854 int noContent /* Do not bother reading content from disk if true */
3855){
3856 int rc;
3857 pagerEnter(pPager);
3858 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3859 pagerLeave(pPager);
3860 return rc;
3861}
3862
drhed7c8552001-04-11 14:29:21 +00003863
3864/*
drh7e3b0a02001-04-28 16:52:40 +00003865** Acquire a page if it is already in the in-memory cache. Do
3866** not read the page from disk. Return a pointer to the page,
3867** or 0 if the page is not in cache.
3868**
danielk19773b8a05f2007-03-19 17:44:26 +00003869** See also sqlite3PagerGet(). The difference between this routine
3870** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003871** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003872** returns NULL if the page is not in cache or if a disk I/O error
3873** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003874*/
danielk19773b8a05f2007-03-19 17:44:26 +00003875DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003876 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003877
drh836faa42003-01-11 13:30:57 +00003878 assert( pPager!=0 );
3879 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003880
drh86f8c192007-08-22 00:39:19 +00003881 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003882 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003883 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003884 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3885 /* Do nothing */
3886 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3887 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003888 }
drh86f8c192007-08-22 00:39:19 +00003889 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003890 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003891}
3892
3893/*
drhed7c8552001-04-11 14:29:21 +00003894** Release a page.
3895**
3896** If the number of references to the page drop to zero, then the
3897** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003898** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003899** removed.
3900*/
danielk19773b8a05f2007-03-19 17:44:26 +00003901int sqlite3PagerUnref(DbPage *pPg){
drh6eac06e2008-05-07 12:45:41 +00003902 Pager *pPager;
3903
3904 if( pPg==0 ) return SQLITE_OK;
3905 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003906
3907 /* Decrement the reference count for this page
3908 */
drhed7c8552001-04-11 14:29:21 +00003909 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003910 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003911 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +00003912
danielk19773c407372005-02-15 02:54:14 +00003913 CHECK_PAGE(pPg);
3914
drh72f82862001-05-24 21:06:34 +00003915 /* When the number of references to a page reach 0, call the
3916 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003917 */
drhed7c8552001-04-11 14:29:21 +00003918 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003919
3920 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003921 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003922 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003923 }
drhd9b02572001-04-15 00:37:09 +00003924
3925 /* When all pages reach the freelist, drop the read lock from
3926 ** the database file.
3927 */
3928 pPager->nRef--;
3929 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003930 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003931 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003932 }
drhed7c8552001-04-11 14:29:21 +00003933 }
danielk1977ae72d982007-10-03 08:46:44 +00003934 pagerLeave(pPager);
drhd9b02572001-04-15 00:37:09 +00003935 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003936}
3937
3938/*
drha6abd042004-06-09 17:37:22 +00003939** Create a journal file for pPager. There should already be a RESERVED
3940** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003941**
3942** Return SQLITE_OK if everything. Return an error code and release the
3943** write lock if anything goes wrong.
3944*/
3945static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003946 sqlite3_vfs *pVfs = pPager->pVfs;
3947 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3948
drhda47d772002-12-02 04:25:19 +00003949 int rc;
drhb7f91642004-10-31 02:22:47 +00003950 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003951 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003952 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003953 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003954 sqlite3PagerPagecount(pPager, 0);
drhed138fb2007-08-22 22:04:37 +00003955 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003956 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00003957 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003958 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003959 rc = SQLITE_NOMEM;
3960 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003961 }
danielk1977b4b47412007-08-17 15:53:36 +00003962
drhfdc40e92008-04-17 20:59:37 +00003963 if( pPager->journalOpen==0 ){
3964 if( pPager->tempFile ){
3965 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3966 }else{
3967 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003968 }
drhfdc40e92008-04-17 20:59:37 +00003969#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3970 rc = sqlite3JournalOpen(
3971 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3972 );
3973#else
3974 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
3975#endif
3976 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3977 pPager->journalOff = 0;
3978 pPager->setMaster = 0;
3979 pPager->journalHdr = 0;
3980 if( rc!=SQLITE_OK ){
3981 if( rc==SQLITE_NOMEM ){
3982 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3983 }
3984 goto failed_to_open_journal;
3985 }
drhda47d772002-12-02 04:25:19 +00003986 }
3987 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003988 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003989 pPager->needSync = 0;
3990 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003991 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003992 if( pPager->errCode ){
3993 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003994 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003995 }
drhda47d772002-12-02 04:25:19 +00003996 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003997
danielk197776572402004-06-25 02:38:54 +00003998 rc = writeJournalHdr(pPager);
3999
drhac69b052004-05-12 13:30:07 +00004000 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004001 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00004002 }
danielk1977ae72d982007-10-03 08:46:44 +00004003 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00004004 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00004005 if( rc==SQLITE_OK ){
4006 rc = SQLITE_FULL;
4007 }
4008 }
drh9c105bb2004-10-02 20:38:28 +00004009 return rc;
4010
4011failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00004012 sqlite3BitvecDestroy(pPager->pInJournal);
4013 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00004014 return rc;
drhda47d772002-12-02 04:25:19 +00004015}
4016
4017/*
drh4b845d72002-03-05 12:41:19 +00004018** Acquire a write-lock on the database. The lock is removed when
4019** the any of the following happen:
4020**
drh80e35f42007-03-30 14:06:34 +00004021** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00004022** * sqlite3PagerRollback() is called.
4023** * sqlite3PagerClose() is called.
4024** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00004025**
danielk197713adf8a2004-06-03 16:08:41 +00004026** The first parameter to this routine is a pointer to any open page of the
4027** database file. Nothing changes about the page - it is used merely to
4028** acquire a pointer to the Pager structure and as proof that there is
4029** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00004030**
danielk197713adf8a2004-06-03 16:08:41 +00004031** The second parameter indicates how much space in bytes to reserve for a
4032** master journal file-name at the start of the journal when it is created.
4033**
4034** A journal file is opened if this is not a temporary file. For temporary
4035** files, the opening of the journal file is deferred until there is an
4036** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00004037**
drha6abd042004-06-09 17:37:22 +00004038** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00004039**
4040** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
4041** immediately instead of waiting until we try to flush the cache. The
4042** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00004043*/
danielk19773b8a05f2007-03-19 17:44:26 +00004044int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00004045 Pager *pPager = pPg->pPager;
4046 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00004047 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00004048 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00004049 assert( pPager->state!=PAGER_UNLOCK );
4050 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00004051 assert( pPager->pInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00004052 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00004053 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00004054 pPager->origDbSize = pPager->dbSize;
4055 }else{
drh054889e2005-11-30 03:20:31 +00004056 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00004057 if( rc==SQLITE_OK ){
4058 pPager->state = PAGER_RESERVED;
4059 if( exFlag ){
4060 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
4061 }
4062 }
danielk19779eed5052004-06-04 10:38:30 +00004063 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00004064 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00004065 return rc;
drhac69b052004-05-12 13:30:07 +00004066 }
drha6abd042004-06-09 17:37:22 +00004067 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00004068 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhfdc40e92008-04-17 20:59:37 +00004069 if( pPager->useJournal && !pPager->tempFile
4070 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drhac69b052004-05-12 13:30:07 +00004071 rc = pager_open_journal(pPager);
4072 }
drh4b845d72002-03-05 12:41:19 +00004073 }
danielk1977334cdb62007-03-26 08:05:12 +00004074 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00004075 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00004076 ** time a (read or write) transaction was successfully concluded
4077 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00004078 ** kept open and either was truncated to 0 bytes or its header was
4079 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00004080 */
4081 assert( pPager->nRec==0 );
4082 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00004083 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00004084 sqlite3PagerPagecount(pPager, 0);
drhed138fb2007-08-22 22:04:37 +00004085 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004086 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhed138fb2007-08-22 22:04:37 +00004087 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004088 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00004089 rc = SQLITE_NOMEM;
4090 }else{
drh369339d2007-03-30 16:01:55 +00004091 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00004092 rc = writeJournalHdr(pPager);
4093 }
drh4b845d72002-03-05 12:41:19 +00004094 }
danielk1977334cdb62007-03-26 08:05:12 +00004095 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00004096 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00004097 return rc;
4098}
4099
4100/*
drh605ad8f2006-05-03 23:34:05 +00004101** Make a page dirty. Set its dirty flag and add it to the dirty
4102** page list.
4103*/
4104static void makeDirty(PgHdr *pPg){
4105 if( pPg->dirty==0 ){
4106 Pager *pPager = pPg->pPager;
4107 pPg->dirty = 1;
4108 pPg->pDirty = pPager->pDirty;
4109 if( pPager->pDirty ){
4110 pPager->pDirty->pPrevDirty = pPg;
4111 }
4112 pPg->pPrevDirty = 0;
4113 pPager->pDirty = pPg;
4114 }
4115}
4116
4117/*
4118** Make a page clean. Clear its dirty bit and remove it from the
4119** dirty page list.
4120*/
4121static void makeClean(PgHdr *pPg){
4122 if( pPg->dirty ){
4123 pPg->dirty = 0;
4124 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00004125 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004126 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
4127 }
4128 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00004129 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004130 pPg->pPrevDirty->pDirty = pPg->pDirty;
4131 }else{
drh153c62c2007-08-24 03:51:33 +00004132 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004133 pPg->pPager->pDirty = pPg->pDirty;
4134 }
4135 }
4136}
4137
4138
4139/*
drhed7c8552001-04-11 14:29:21 +00004140** Mark a data page as writeable. The page is written into the journal
4141** if it is not there already. This routine must be called before making
4142** changes to a page.
4143**
4144** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00004145** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00004146** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00004147** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00004148** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00004149**
4150** If the journal file could not be written because the disk is full,
4151** then this routine returns SQLITE_FULL and does an immediate rollback.
4152** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00004153** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00004154** reset.
drhed7c8552001-04-11 14:29:21 +00004155*/
danielk19773b8a05f2007-03-19 17:44:26 +00004156static int pager_write(PgHdr *pPg){
4157 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00004158 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00004159 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00004160
drh6446c4d2001-12-15 14:22:18 +00004161 /* Check for errors
4162 */
danielk1977efaaf572006-01-16 11:29:19 +00004163 if( pPager->errCode ){
4164 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004165 }
drh5e00f6c2001-09-13 13:46:56 +00004166 if( pPager->readOnly ){
4167 return SQLITE_PERM;
4168 }
drh6446c4d2001-12-15 14:22:18 +00004169
danielk197776572402004-06-25 02:38:54 +00004170 assert( !pPager->setMaster );
4171
danielk19773c407372005-02-15 02:54:14 +00004172 CHECK_PAGE(pPg);
4173
drh538f5702007-04-13 02:14:30 +00004174 /* If this page was previously acquired with noContent==1, that means
4175 ** we didn't really read in the content of the page. This can happen
4176 ** (for example) when the page is being moved to the freelist. But
4177 ** now we are (perhaps) moving the page off of the freelist for
4178 ** reuse and we need to know its original content so that content
4179 ** can be stored in the rollback journal. So do the read at this
4180 ** time.
4181 */
drhd33d5a82007-04-26 12:11:28 +00004182 rc = pager_get_content(pPg);
4183 if( rc ){
4184 return rc;
drh538f5702007-04-13 02:14:30 +00004185 }
4186
drh6446c4d2001-12-15 14:22:18 +00004187 /* Mark the page as dirty. If the page has already been written
4188 ** to the journal then we can return right away.
4189 */
drh605ad8f2006-05-03 23:34:05 +00004190 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00004191 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00004192 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004193 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004194 }else{
drh6446c4d2001-12-15 14:22:18 +00004195
danielk1977a0bf2652004-11-04 14:30:04 +00004196 /* If we get this far, it means that the page needs to be
4197 ** written to the transaction journal or the ckeckpoint journal
4198 ** or both.
4199 **
4200 ** First check to see that the transaction journal exists and
4201 ** create it if it does not.
4202 */
4203 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00004204 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00004205 if( rc!=SQLITE_OK ){
4206 return rc;
4207 }
4208 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00004209 if( !pPager->journalOpen && pPager->useJournal
4210 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00004211 rc = pager_open_journal(pPager);
4212 if( rc!=SQLITE_OK ) return rc;
4213 }
danielk1977a0bf2652004-11-04 14:30:04 +00004214 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004215 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004216
4217 /* The transaction journal now exists and we have a RESERVED or an
4218 ** EXCLUSIVE lock on the main database file. Write the current page to
4219 ** the transaction journal if it is not there already.
4220 */
drhfdc40e92008-04-17 20:59:37 +00004221 if( !pPg->inJournal && (pPager->journalOpen || MEMDB) ){
danielk1977a0bf2652004-11-04 14:30:04 +00004222 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00004223 if( MEMDB ){
4224 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00004225 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004226 assert( pHist->pOrig==0 );
drhfacf0302008-06-17 15:12:00 +00004227 pHist->pOrig = sqlite3PageMalloc( pPager->pageSize );
danielk1977662278e2007-11-05 15:30:12 +00004228 if( !pHist->pOrig ){
4229 return SQLITE_NOMEM;
danielk1977a0bf2652004-11-04 14:30:04 +00004230 }
danielk1977662278e2007-11-05 15:30:12 +00004231 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
danielk1977a0bf2652004-11-04 14:30:04 +00004232 }else{
drhbf4bca52007-09-06 22:19:14 +00004233 u32 cksum;
4234 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00004235
drh267cb322005-09-16 17:16:52 +00004236 /* We should never write to the journal file the page that
4237 ** contains the database locks. The following assert verifies
4238 ** that we do not. */
4239 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004240 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004241 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00004242 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4243 if( rc==SQLITE_OK ){
4244 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4245 pPager->journalOff + 4);
4246 pPager->journalOff += pPager->pageSize+4;
4247 }
4248 if( rc==SQLITE_OK ){
4249 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4250 pPager->journalOff += 4;
4251 }
4252 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00004253 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00004254 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00004255 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4256 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00004257
drhfd131da2007-08-07 17:13:03 +00004258 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004259 ** transaction will be rolled back by the layer above.
4260 */
danielk1977a0bf2652004-11-04 14:30:04 +00004261 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004262 return rc;
4263 }
danielk197707cb5602006-01-20 10:55:05 +00004264
danielk1977a0bf2652004-11-04 14:30:04 +00004265 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00004266 assert( pPager->pInJournal!=0 );
4267 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004268 pPg->needSync = !pPager->noSync;
4269 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00004270 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004271 }
drhac69b052004-05-12 13:30:07 +00004272 }
danielk197713adf8a2004-06-03 16:08:41 +00004273 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004274 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004275 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004276 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004277 }
4278 if( pPg->needSync ){
4279 pPager->needSync = 1;
4280 }
4281 pPg->inJournal = 1;
4282 }
4283
4284 /* If the statement journal is open and the page is not in it,
4285 ** then write the current page to the statement journal. Note that
4286 ** the statement journal format differs from the standard journal format
4287 ** in that it omits the checksums and the header.
4288 */
danielk1977f35843b2007-04-07 15:03:17 +00004289 if( pPager->stmtInUse
4290 && !pageInStatement(pPg)
4291 && (int)pPg->pgno<=pPager->stmtSize
4292 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004293 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4294 if( MEMDB ){
4295 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4296 assert( pHist->pStmt==0 );
drhfacf0302008-06-17 15:12:00 +00004297 pHist->pStmt = sqlite3PageMalloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004298 if( pHist->pStmt ){
4299 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4300 }
drh4f0c5872007-03-26 22:05:01 +00004301 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004302 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004303 }else{
danielk197762079062007-08-15 17:08:46 +00004304 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00004305 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4306 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4307 if( rc==SQLITE_OK ){
4308 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4309 }
drh4f0c5872007-03-26 22:05:01 +00004310 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004311 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004312 return rc;
4313 }
danielk1977a0bf2652004-11-04 14:30:04 +00004314 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00004315 assert( pPager->pInStmt!=0 );
4316 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00004317 }
drhd9b02572001-04-15 00:37:09 +00004318 }
drhfa86c412002-02-02 15:01:15 +00004319 }
4320
4321 /* Update the database size and return.
4322 */
drh1aa2d8b2007-01-03 15:34:29 +00004323 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004324 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004325 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004326 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004327 pPager->dbSize++;
4328 }
drh306dc212001-05-21 13:45:10 +00004329 }
drh69688d52001-04-14 16:38:23 +00004330 return rc;
drhed7c8552001-04-11 14:29:21 +00004331}
4332
4333/*
danielk19774099f6e2007-03-19 11:25:20 +00004334** This function is used to mark a data-page as writable. It uses
4335** pager_write() to open a journal file (if it is not already open)
4336** and write the page *pData to the journal.
4337**
4338** The difference between this function and pager_write() is that this
4339** function also deals with the special case where 2 or more pages
4340** fit on a single disk sector. In this case all co-resident pages
4341** must have been written to the journal file before returning.
4342*/
danielk19773b8a05f2007-03-19 17:44:26 +00004343int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004344 int rc = SQLITE_OK;
4345
danielk19773b8a05f2007-03-19 17:44:26 +00004346 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004347 Pager *pPager = pPg->pPager;
4348 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4349
drh86f8c192007-08-22 00:39:19 +00004350 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004351 if( !MEMDB && nPagePerSector>1 ){
4352 Pgno nPageCount; /* Total number of pages in database file */
4353 Pgno pg1; /* First page of the sector pPg is located on. */
4354 int nPage; /* Number of pages starting at pg1 to journal */
4355 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004356 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004357
4358 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4359 ** header to be written between the pages journaled by this function.
4360 */
4361 assert( pPager->doNotSync==0 );
4362 pPager->doNotSync = 1;
4363
4364 /* This trick assumes that both the page-size and sector-size are
4365 ** an integer power of 2. It sets variable pg1 to the identifier
4366 ** of the first page of the sector pPg is located on.
4367 */
4368 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4369
danielk1977ad0132d2008-06-07 08:58:22 +00004370 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00004371 if( pPg->pgno>nPageCount ){
4372 nPage = (pPg->pgno - pg1)+1;
4373 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4374 nPage = nPageCount+1-pg1;
4375 }else{
4376 nPage = nPagePerSector;
4377 }
4378 assert(nPage>0);
4379 assert(pg1<=pPg->pgno);
4380 assert((pg1+nPage)>pPg->pgno);
4381
4382 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4383 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004384 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00004385 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00004386 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004387 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004388 if( rc==SQLITE_OK ){
4389 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004390 if( pPage->needSync ){
4391 needSync = 1;
4392 }
danielk19773b8a05f2007-03-19 17:44:26 +00004393 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004394 }
4395 }
drhc81945e2008-03-10 14:12:53 +00004396 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk1977dd97a492007-08-22 18:54:32 +00004397 if( pPage->needSync ){
4398 needSync = 1;
4399 }
danielk19774099f6e2007-03-19 11:25:20 +00004400 }
4401 }
4402
danielk1977dd97a492007-08-22 18:54:32 +00004403 /* If the PgHdr.needSync flag is set for any of the nPage pages
4404 ** starting at pg1, then it needs to be set for all of them. Because
4405 ** writing to any of these nPage pages may damage the others, the
4406 ** journal file must contain sync()ed copies of all of them
4407 ** before any of them can be written out to the database file.
4408 */
4409 if( needSync ){
4410 for(ii=0; ii<nPage && needSync; ii++){
4411 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4412 if( pPage ) pPage->needSync = 1;
4413 }
4414 assert(pPager->needSync);
4415 }
4416
danielk19774099f6e2007-03-19 11:25:20 +00004417 assert( pPager->doNotSync==1 );
4418 pPager->doNotSync = 0;
4419 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004420 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004421 }
drh86f8c192007-08-22 00:39:19 +00004422 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004423 return rc;
4424}
4425
4426/*
drhaacc5432002-01-06 17:07:40 +00004427** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004428** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004429** to change the content of the page.
4430*/
danielk19777d3a6662006-01-23 16:21:05 +00004431#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004432int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004433 return pPg->dirty;
4434}
danielk19777d3a6662006-01-23 16:21:05 +00004435#endif
drh6019e162001-07-02 17:51:45 +00004436
drh001bbcb2003-03-19 03:14:00 +00004437/*
drh30e58752002-03-02 20:41:57 +00004438** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004439** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004440** that page might be marked as dirty.
4441**
4442** The overlying software layer calls this routine when all of the data
4443** on the given page is unused. The pager marks the page as clean so
4444** that it does not get written to disk.
4445**
4446** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004447** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004448** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004449**
4450** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004451** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004452** will thereafter be ignored. This is necessary to avoid a problem
4453** where a page with data is added to the freelist during one part of
4454** a transaction then removed from the freelist during a later part
4455** of the same transaction and reused for some other purpose. When it
4456** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004457** the sqlite3PagerDontRollback() routine is called. But because the
4458** page contains critical data, we still need to be sure it gets
4459** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004460*/
drh538f5702007-04-13 02:14:30 +00004461void sqlite3PagerDontWrite(DbPage *pDbPage){
4462 PgHdr *pPg = pDbPage;
4463 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004464
drhb7f91642004-10-31 02:22:47 +00004465 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004466 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004467 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004468 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004469 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004470 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4471 /* If this pages is the last page in the file and the file has grown
4472 ** during the current transaction, then do NOT mark the page as clean.
4473 ** When the database file grows, we must make sure that the last page
4474 ** gets written at least once so that the disk file will be the correct
4475 ** size. If you do not write this page and the size of the file
4476 ** on the disk ends up being too small, that can lead to database
4477 ** corruption during the next transaction.
4478 */
4479 }else{
drh538f5702007-04-13 02:14:30 +00004480 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4481 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004482 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004483#ifdef SQLITE_CHECK_PAGES
4484 pPg->pageHash = pager_pagehash(pPg);
4485#endif
drh8124a302002-06-25 14:43:57 +00004486 }
drh30e58752002-03-02 20:41:57 +00004487 }
drh86f8c192007-08-22 00:39:19 +00004488 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004489}
4490
4491/*
4492** A call to this routine tells the pager that if a rollback occurs,
4493** it is not necessary to restore the data on the given page. This
4494** means that the pager does not have to record the given page in the
4495** rollback journal.
drh538f5702007-04-13 02:14:30 +00004496**
4497** If we have not yet actually read the content of this page (if
4498** the PgHdr.needRead flag is set) then this routine acts as a promise
4499** that we will never need to read the page content in the future.
4500** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00004501*/
danielk19773b8a05f2007-03-19 17:44:26 +00004502void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004503 Pager *pPager = pPg->pPager;
4504
drh86f8c192007-08-22 00:39:19 +00004505 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004506 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00004507
4508 /* If the journal file is not open, or DontWrite() has been called on
4509 ** this page (DontWrite() sets the alwaysRollback flag), then this
4510 ** function is a no-op.
4511 */
4512 if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){
danielk197787c29a92008-01-18 11:33:16 +00004513 pagerLeave(pPager);
4514 return;
4515 }
danielk1977a55e9352008-01-21 13:04:34 +00004516 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
4517
drhc5d0bd92008-04-14 01:00:57 +00004518#ifdef SQLITE_SECURE_DELETE
4519 if( pPg->inJournal || (int)pPg->pgno > pPager->origDbSize ){
4520 return;
4521 }
4522#endif
4523
4524 /* If SECURE_DELETE is disabled, then there is no way that this
4525 ** routine can be called on a page for which sqlite3PagerDontWrite()
4526 ** has not been previously called during the same transaction.
4527 ** And if DontWrite() has previously been called, the following
4528 ** conditions must be met.
danielk1977a55e9352008-01-21 13:04:34 +00004529 */
4530 assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize );
4531
drhf5e7bb52008-02-18 14:47:33 +00004532 assert( pPager->pInJournal!=0 );
4533 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a55e9352008-01-21 13:04:34 +00004534 pPg->inJournal = 1;
4535 pPg->needRead = 0;
4536 if( pPager->stmtInUse ){
drhc5d0bd92008-04-14 01:00:57 +00004537 assert( pPager->stmtSize >= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00004538 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00004539 }
danielk1977a55e9352008-01-21 13:04:34 +00004540 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
4541 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh86f8c192007-08-22 00:39:19 +00004542 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004543}
4544
drhac69b052004-05-12 13:30:07 +00004545
drh30e58752002-03-02 20:41:57 +00004546/*
drh80e35f42007-03-30 14:06:34 +00004547** This routine is called to increment the database file change-counter,
4548** stored at byte 24 of the pager file.
4549*/
danielk1977c7b60172007-08-22 11:22:03 +00004550static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004551 PgHdr *pPgHdr;
4552 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004553 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004554
4555 if( !pPager->changeCountDone ){
4556 /* Open page 1 of the file for writing. */
4557 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4558 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004559
4560 if( !isDirect ){
4561 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00004562 if( rc!=SQLITE_OK ){
4563 sqlite3PagerUnref(pPgHdr);
4564 return rc;
4565 }
danielk1977c7b60172007-08-22 11:22:03 +00004566 }
4567
drh80e35f42007-03-30 14:06:34 +00004568 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004569 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004570 change_counter++;
4571 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004572
4573 if( isDirect && pPager->fd->pMethods ){
4574 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4575 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4576 }
4577
drh80e35f42007-03-30 14:06:34 +00004578 /* Release the page reference. */
4579 sqlite3PagerUnref(pPgHdr);
4580 pPager->changeCountDone = 1;
4581 }
danielk1977c7b60172007-08-22 11:22:03 +00004582 return rc;
drh80e35f42007-03-30 14:06:34 +00004583}
4584
4585/*
danielk1977f653d782008-03-20 11:04:21 +00004586** Sync the pager file to disk.
4587*/
4588int sqlite3PagerSync(Pager *pPager){
4589 int rc;
4590 pagerEnter(pPager);
4591 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4592 pagerLeave(pPager);
4593 return rc;
4594}
4595
4596/*
drh80e35f42007-03-30 14:06:34 +00004597** Sync the database file for the pager pPager. zMaster points to the name
4598** of a master journal file that should be written into the individual
4599** journal file. zMaster may be NULL, which is interpreted as no master
4600** journal (a single database transaction).
4601**
4602** This routine ensures that the journal is synced, all dirty pages written
4603** to the database file and the database file synced. The only thing that
4604** remains to commit the transaction is to delete the journal file (or
4605** master journal file if specified).
4606**
4607** Note that if zMaster==NULL, this does not overwrite a previous value
4608** passed to an sqlite3PagerCommitPhaseOne() call.
4609**
4610** If parameter nTrunc is non-zero, then the pager file is truncated to
4611** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00004612**
4613** If the final parameter - noSync - is true, then the database file itself
4614** is not synced. The caller must call sqlite3PagerSync() directly to
4615** sync the database file before calling CommitPhaseTwo() to delete the
4616** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00004617*/
danielk1977f653d782008-03-20 11:04:21 +00004618int sqlite3PagerCommitPhaseOne(
4619 Pager *pPager,
4620 const char *zMaster,
4621 Pgno nTrunc,
4622 int noSync
4623){
drh80e35f42007-03-30 14:06:34 +00004624 int rc = SQLITE_OK;
4625
danielk1977dad31b52008-05-15 11:08:07 +00004626 if( pPager->errCode ){
4627 return pPager->errCode;
4628 }
4629
drhd138c012008-05-13 00:58:18 +00004630 /* If no changes have been made, we can leave the transaction early.
4631 */
4632 if( pPager->dbModified==0 &&
4633 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
4634 pPager->exclusiveMode!=0) ){
4635 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
4636 return SQLITE_OK;
4637 }
4638
drh80e35f42007-03-30 14:06:34 +00004639 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4640 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004641 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004642
4643 /* If this is an in-memory db, or no pages have been written to, or this
4644 ** function has already been called, it is a no-op.
4645 */
4646 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4647 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004648
danielk1977c7b60172007-08-22 11:22:03 +00004649#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4650 /* The atomic-write optimization can be used if all of the
4651 ** following are true:
4652 **
4653 ** + The file-system supports the atomic-write property for
4654 ** blocks of size page-size, and
4655 ** + This commit is not part of a multi-file transaction, and
4656 ** + Exactly one page has been modified and store in the journal file.
4657 **
4658 ** If the optimization can be used, then the journal file will never
4659 ** be created for this transaction.
4660 */
danielk1977f55b8992007-08-24 08:15:53 +00004661 int useAtomicWrite = (
4662 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00004663 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00004664 pPager->journalOff==jrnlBufferSize(pPager) &&
4665 nTrunc==0 &&
4666 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4667 );
danielk1977700b9c52008-04-24 12:37:40 +00004668 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00004669 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004670 /* Update the nRec field in the journal file. */
4671 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4672 assert(pPager->nRec==1);
4673 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4674
4675 /* Update the db file change counter. The following call will modify
4676 ** the in-memory representation of page 1 to include the updated
4677 ** change counter and then write page 1 directly to the database
4678 ** file. Because of the atomic-write property of the host file-system,
4679 ** this is safe.
4680 */
danielk1977ae72d982007-10-03 08:46:44 +00004681 if( rc==SQLITE_OK ){
4682 rc = pager_incr_changecounter(pPager, 1);
4683 }
danielk1977f55b8992007-08-24 08:15:53 +00004684 }else{
4685 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00004686 }
4687
danielk1977ae72d982007-10-03 08:46:44 +00004688 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00004689#endif
4690
drh80e35f42007-03-30 14:06:34 +00004691 /* If a master journal file name has already been written to the
4692 ** journal file, then no sync is required. This happens when it is
4693 ** written, then the process fails to upgrade from a RESERVED to an
4694 ** EXCLUSIVE lock. The next time the process tries to commit the
4695 ** transaction the m-j name will have already been written.
4696 */
4697 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00004698 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004699 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00004700 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00004701#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197771aa7ff2008-05-20 07:05:09 +00004702 if( nTrunc!=0 ){
4703 /* If this transaction has made the database smaller, then all pages
4704 ** being discarded by the truncation must be written to the journal
4705 ** file.
4706 */
4707 Pgno i;
4708 int iSkip = PAGER_MJ_PGNO(pPager);
4709 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
4710 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
4711 rc = sqlite3PagerGet(pPager, i, &pPg);
4712 if( rc!=SQLITE_OK ) goto sync_exit;
4713 rc = sqlite3PagerWrite(pPg);
4714 sqlite3PagerUnref(pPg);
4715 if( rc!=SQLITE_OK ) goto sync_exit;
4716 }
4717 }
4718 }
drh80e35f42007-03-30 14:06:34 +00004719#endif
danielk197771aa7ff2008-05-20 07:05:09 +00004720 rc = writeMasterJournal(pPager, zMaster);
4721 if( rc!=SQLITE_OK ) goto sync_exit;
4722 rc = syncJournal(pPager);
4723 }
drh80e35f42007-03-30 14:06:34 +00004724 }
danielk1977c7b60172007-08-22 11:22:03 +00004725 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004726
4727#ifndef SQLITE_OMIT_AUTOVACUUM
4728 if( nTrunc!=0 ){
4729 rc = sqlite3PagerTruncate(pPager, nTrunc);
4730 if( rc!=SQLITE_OK ) goto sync_exit;
4731 }
4732#endif
4733
4734 /* Write all dirty pages to the database file */
4735 pPg = pager_get_all_dirty_pages(pPager);
4736 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004737 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004738 assert( rc!=SQLITE_IOERR_BLOCKED );
4739 /* The error might have left the dirty list all fouled up here,
4740 ** but that does not matter because if the if the dirty list did
4741 ** get corrupted, then the transaction will roll back and
4742 ** discard the dirty list. There is an assert in
4743 ** pager_get_all_dirty_pages() that verifies that no attempt
4744 ** is made to use an invalid dirty list.
4745 */
drh153c62c2007-08-24 03:51:33 +00004746 goto sync_exit;
4747 }
drh3cdd7d32007-03-30 14:46:01 +00004748 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004749
4750 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004751 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004752 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004753 }
4754 IOTRACE(("DBSYNC %p\n", pPager))
4755
4756 pPager->state = PAGER_SYNCED;
4757 }else if( MEMDB && nTrunc!=0 ){
4758 rc = sqlite3PagerTruncate(pPager, nTrunc);
4759 }
4760
4761sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004762 if( rc==SQLITE_IOERR_BLOCKED ){
4763 /* pager_incr_changecounter() may attempt to obtain an exclusive
4764 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004765 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004766 * better to return SQLITE_BUSY.
4767 */
4768 rc = SQLITE_BUSY;
4769 }
drh86f8c192007-08-22 00:39:19 +00004770 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004771 return rc;
4772}
4773
4774
4775/*
drhed7c8552001-04-11 14:29:21 +00004776** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004777**
4778** If the commit fails for any reason, a rollback attempt is made
4779** and an error code is returned. If the commit worked, SQLITE_OK
4780** is returned.
drhed7c8552001-04-11 14:29:21 +00004781*/
drh80e35f42007-03-30 14:06:34 +00004782int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004783 int rc;
drhed7c8552001-04-11 14:29:21 +00004784 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004785
danielk1977efaaf572006-01-16 11:29:19 +00004786 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004787 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004788 }
drha6abd042004-06-09 17:37:22 +00004789 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004790 return SQLITE_ERROR;
4791 }
drhd138c012008-05-13 00:58:18 +00004792 if( pPager->dbModified==0 &&
4793 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
4794 pPager->exclusiveMode!=0) ){
4795 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
4796 return SQLITE_OK;
4797 }
drh86f8c192007-08-22 00:39:19 +00004798 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004799 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004800 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004801 pPg = pager_get_all_dirty_pages(pPager);
4802 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004803 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4804 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004805 pPg->dirty = 0;
4806 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004807 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004808 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004809 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004810 pPg = pPg->pDirty;
4811 }
drh605ad8f2006-05-03 23:34:05 +00004812 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004813#ifndef NDEBUG
4814 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4815 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4816 assert( !pPg->alwaysRollback );
4817 assert( !pHist->pOrig );
4818 assert( !pHist->pStmt );
4819 }
4820#endif
drhac69b052004-05-12 13:30:07 +00004821 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004822 pPager->state = PAGER_SHARED;
danielk197787c29a92008-01-18 11:33:16 +00004823 pagerLeave(pPager);
drhac69b052004-05-12 13:30:07 +00004824 return SQLITE_OK;
4825 }
drh3cdd7d32007-03-30 14:46:01 +00004826 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
danielk1977df2566a2008-05-07 19:11:03 +00004827 rc = pager_end_transaction(pPager, pPager->setMaster);
drh86f8c192007-08-22 00:39:19 +00004828 rc = pager_error(pPager, rc);
4829 pagerLeave(pPager);
4830 return rc;
drhed7c8552001-04-11 14:29:21 +00004831}
4832
4833/*
drha6abd042004-06-09 17:37:22 +00004834** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004835** All in-memory cache pages revert to their original data contents.
4836** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004837**
4838** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004839** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004840** process is writing trash into the journal file (SQLITE_CORRUPT) or
4841** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4842** codes are returned for all these occasions. Otherwise,
4843** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004844*/
danielk19773b8a05f2007-03-19 17:44:26 +00004845int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004846 int rc;
drh4f0c5872007-03-26 22:05:01 +00004847 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004848 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004849 PgHdr *p;
4850 for(p=pPager->pAll; p; p=p->pNextAll){
4851 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004852 assert( !p->alwaysRollback );
4853 if( !p->dirty ){
4854 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4855 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4856 continue;
4857 }
4858
drhac69b052004-05-12 13:30:07 +00004859 pHist = PGHDR_TO_HIST(p, pPager);
4860 if( pHist->pOrig ){
4861 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004862 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004863 }else{
drh4f0c5872007-03-26 22:05:01 +00004864 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004865 }
4866 clearHistory(pHist);
4867 p->dirty = 0;
4868 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004869 pHist->inStmt = 0;
4870 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004871 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004872 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004873 }
drhac69b052004-05-12 13:30:07 +00004874 }
drh605ad8f2006-05-03 23:34:05 +00004875 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004876 pPager->pStmt = 0;
4877 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004878 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004879 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004880 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004881 return SQLITE_OK;
4882 }
4883
drh86f8c192007-08-22 00:39:19 +00004884 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004885 if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00004886 rc = pager_end_transaction(pPager, pPager->setMaster);
drh86f8c192007-08-22 00:39:19 +00004887 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004888 return rc;
4889 }
drhdb48ee02003-01-16 13:42:43 +00004890
danielk1977efaaf572006-01-16 11:29:19 +00004891 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004892 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004893 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004894 }
drh86f8c192007-08-22 00:39:19 +00004895 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004896 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004897 }
drha6abd042004-06-09 17:37:22 +00004898 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004899 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004900 rc = pager_playback(pPager, 0);
danielk1977df2566a2008-05-07 19:11:03 +00004901 rc2 = pager_end_transaction(pPager, pPager->setMaster);
drha6abd042004-06-09 17:37:22 +00004902 if( rc==SQLITE_OK ){
4903 rc = rc2;
4904 }
4905 }else{
danielk1977e277be02007-03-23 18:12:06 +00004906 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004907 }
danielk1977e180dd92007-04-05 17:15:52 +00004908 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004909 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004910
4911 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4912 ** cache. So call pager_error() on the way out to make any error
4913 ** persistent.
4914 */
drh86f8c192007-08-22 00:39:19 +00004915 rc = pager_error(pPager, rc);
4916 pagerLeave(pPager);
4917 return rc;
drh98808ba2001-10-18 12:34:46 +00004918}
drhd9b02572001-04-15 00:37:09 +00004919
4920/*
drh5e00f6c2001-09-13 13:46:56 +00004921** Return TRUE if the database file is opened read-only. Return FALSE
4922** if the database is (in theory) writable.
4923*/
danielk19773b8a05f2007-03-19 17:44:26 +00004924int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004925 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004926}
4927
4928/*
drh0f7eb612006-08-08 13:51:43 +00004929** Return the number of references to the pager.
4930*/
danielk19773b8a05f2007-03-19 17:44:26 +00004931int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004932 return pPager->nRef;
4933}
4934
4935#ifdef SQLITE_TEST
4936/*
drhd9b02572001-04-15 00:37:09 +00004937** This routine is used for testing and analysis only.
4938*/
danielk19773b8a05f2007-03-19 17:44:26 +00004939int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004940 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004941 a[0] = pPager->nRef;
4942 a[1] = pPager->nPage;
4943 a[2] = pPager->mxPage;
4944 a[3] = pPager->dbSize;
4945 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004946 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004947 a[6] = pPager->nHit;
4948 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004949 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004950 a[9] = pPager->nRead;
4951 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004952 return a;
4953}
danielk197717b90b52008-06-06 11:11:25 +00004954int sqlite3PagerIsMemdb(Pager *pPager){
4955 return MEMDB;
4956}
drh0f7eb612006-08-08 13:51:43 +00004957#endif
drhdd793422001-06-28 01:54:48 +00004958
drhfa86c412002-02-02 15:01:15 +00004959/*
drhac69b052004-05-12 13:30:07 +00004960** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004961**
4962** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004963** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004964** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004965*/
drh86f8c192007-08-22 00:39:19 +00004966static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004967 int rc;
drhac69b052004-05-12 13:30:07 +00004968 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004969 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004970 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004971 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004972 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004973 pPager->stmtInUse = 1;
4974 pPager->stmtSize = pPager->dbSize;
4975 return SQLITE_OK;
4976 }
drhda47d772002-12-02 04:25:19 +00004977 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004978 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004979 return SQLITE_OK;
4980 }
drhfa86c412002-02-02 15:01:15 +00004981 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004982 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004983 assert( pPager->pInStmt==0 );
4984 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00004985 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004986 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004987 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004988 return SQLITE_NOMEM;
4989 }
danielk197776572402004-06-25 02:38:54 +00004990 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004991 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004992 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004993 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004994 if( !pPager->stmtOpen ){
danielk197717b90b52008-06-06 11:11:25 +00004995 rc = sqlite3PagerOpentemp(pPager, pPager->stfd, SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00004996 if( rc ){
4997 goto stmt_begin_failed;
4998 }
drhac69b052004-05-12 13:30:07 +00004999 pPager->stmtOpen = 1;
5000 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00005001 }
drhac69b052004-05-12 13:30:07 +00005002 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00005003 return SQLITE_OK;
5004
drhac69b052004-05-12 13:30:07 +00005005stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00005006 if( pPager->pInStmt ){
5007 sqlite3BitvecDestroy(pPager->pInStmt);
5008 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00005009 }
5010 return rc;
5011}
drh86f8c192007-08-22 00:39:19 +00005012int sqlite3PagerStmtBegin(Pager *pPager){
5013 int rc;
5014 pagerEnter(pPager);
5015 rc = pagerStmtBegin(pPager);
5016 pagerLeave(pPager);
5017 return rc;
5018}
drhfa86c412002-02-02 15:01:15 +00005019
5020/*
drhac69b052004-05-12 13:30:07 +00005021** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00005022*/
danielk19773b8a05f2007-03-19 17:44:26 +00005023int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00005024 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00005025 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00005026 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00005027 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00005028 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00005029 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00005030 sqlite3BitvecDestroy(pPager->pInStmt);
5031 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00005032 }else{
5033 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00005034 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00005035 pNext = pHist->pNextStmt;
5036 assert( pHist->inStmt );
5037 pHist->inStmt = 0;
5038 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhfacf0302008-06-17 15:12:00 +00005039 sqlite3PageFree(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00005040 pHist->pStmt = 0;
5041 }
5042 }
5043 pPager->stmtNRec = 0;
5044 pPager->stmtInUse = 0;
5045 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00005046 }
drhac69b052004-05-12 13:30:07 +00005047 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00005048 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00005049 return SQLITE_OK;
5050}
5051
5052/*
drhac69b052004-05-12 13:30:07 +00005053** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00005054*/
danielk19773b8a05f2007-03-19 17:44:26 +00005055int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00005056 int rc;
drh86f8c192007-08-22 00:39:19 +00005057 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00005058 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00005059 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00005060 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00005061 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00005062 PgHistory *pHist;
5063 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
5064 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00005065 if( pHist->pStmt ){
5066 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drhfacf0302008-06-17 15:12:00 +00005067 sqlite3PageFree(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00005068 pHist->pStmt = 0;
5069 }
5070 }
5071 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00005072 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00005073 rc = SQLITE_OK;
5074 }else{
5075 rc = pager_stmt_playback(pPager);
5076 }
danielk19773b8a05f2007-03-19 17:44:26 +00005077 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00005078 }else{
5079 rc = SQLITE_OK;
5080 }
drhac69b052004-05-12 13:30:07 +00005081 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00005082 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00005083 return rc;
5084}
5085
drh73509ee2003-04-06 20:44:45 +00005086/*
5087** Return the full pathname of the database file.
5088*/
danielk19773b8a05f2007-03-19 17:44:26 +00005089const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00005090 return pPager->zFilename;
5091}
5092
drhb20ea9d2004-02-09 01:20:36 +00005093/*
drhd0679ed2007-08-28 22:24:34 +00005094** Return the VFS structure for the pager.
5095*/
5096const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
5097 return pPager->pVfs;
5098}
5099
5100/*
drhcc6bb3e2007-08-31 16:11:35 +00005101** Return the file handle for the database file associated
5102** with the pager. This might return NULL if the file has
5103** not yet been opened.
5104*/
5105sqlite3_file *sqlite3PagerFile(Pager *pPager){
5106 return pPager->fd;
5107}
5108
5109/*
danielk19775865e3d2004-06-14 06:03:57 +00005110** Return the directory of the database file.
5111*/
danielk19773b8a05f2007-03-19 17:44:26 +00005112const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005113 return pPager->zDirectory;
5114}
5115
5116/*
5117** Return the full pathname of the journal file.
5118*/
danielk19773b8a05f2007-03-19 17:44:26 +00005119const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005120 return pPager->zJournal;
5121}
5122
5123/*
drh2c8997b2005-08-27 16:36:48 +00005124** Return true if fsync() calls are disabled for this pager. Return FALSE
5125** if fsync()s are executed normally.
5126*/
danielk19773b8a05f2007-03-19 17:44:26 +00005127int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00005128 return pPager->noSync;
5129}
5130
drh7c4ac0c2007-04-05 11:25:58 +00005131#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00005132/*
drhb20ea9d2004-02-09 01:20:36 +00005133** Set the codec for this pager
5134*/
danielk19773b8a05f2007-03-19 17:44:26 +00005135void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00005136 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00005137 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00005138 void *pCodecArg
5139){
5140 pPager->xCodec = xCodec;
5141 pPager->pCodecArg = pCodecArg;
5142}
drh7c4ac0c2007-04-05 11:25:58 +00005143#endif
drhb20ea9d2004-02-09 01:20:36 +00005144
danielk1977687566d2004-11-02 12:56:41 +00005145#ifndef SQLITE_OMIT_AUTOVACUUM
5146/*
danielk197787c29a92008-01-18 11:33:16 +00005147** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00005148**
drh5e385312007-06-16 04:42:12 +00005149** There must be no references to the page previously located at
5150** pgno (which we call pPgOld) though that page is allowed to be
5151** in cache. If the page previous located at pgno is not already
5152** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00005153**
drh5e385312007-06-16 04:42:12 +00005154** References to the page pPg remain valid. Updating any
5155** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00005156** allocated along with the page) is the responsibility of the caller.
5157**
danielk19775fd057a2005-03-09 13:09:43 +00005158** A transaction must be active when this routine is called. It used to be
5159** required that a statement transaction was not active, but this restriction
5160** has been removed (CREATE INDEX needs to move a page when a statement
5161** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00005162*/
danielk19773b8a05f2007-03-19 17:44:26 +00005163int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00005164 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00005165 int h;
danielk197794daf7f2004-11-08 09:26:09 +00005166 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00005167
drh86f8c192007-08-22 00:39:19 +00005168 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005169 assert( pPg->nRef>0 );
5170
drh4f0c5872007-03-26 22:05:01 +00005171 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00005172 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00005173 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00005174
danielk1977b4626a32007-04-28 15:47:43 +00005175 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00005176 if( pPg->needSync ){
5177 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00005178 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00005179 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00005180 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00005181 }
5182
drh85b623f2007-12-13 21:54:09 +00005183 /* Unlink pPg from its hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00005184 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00005185
danielk1977ef73ee92004-11-06 12:26:07 +00005186 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00005187 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00005188 ** page pgno before the 'move' operation, it needs to be retained
5189 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00005190 */
drh5e385312007-06-16 04:42:12 +00005191 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00005192 pPgOld = pager_lookup(pPager, pgno);
5193 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00005194 assert( pPgOld->nRef==0 );
5195 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00005196 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00005197 pPg->needSync = pPgOld->needSync;
5198 }else{
5199 pPg->needSync = 0;
5200 }
drhf5e7bb52008-02-18 14:47:33 +00005201 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
danielk1977687566d2004-11-02 12:56:41 +00005202
danielk1977f5fdda82004-11-03 08:44:05 +00005203 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00005204 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00005205 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00005206 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00005207 if( pPager->aHash[h] ){
5208 assert( pPager->aHash[h]->pPrevHash==0 );
5209 pPager->aHash[h]->pPrevHash = pPg;
5210 }
5211 pPg->pNextHash = pPager->aHash[h];
5212 pPager->aHash[h] = pPg;
5213 pPg->pPrevHash = 0;
5214
drh605ad8f2006-05-03 23:34:05 +00005215 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00005216 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00005217 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00005218
danielk197794daf7f2004-11-08 09:26:09 +00005219 if( needSyncPgno ){
5220 /* If needSyncPgno is non-zero, then the journal file needs to be
5221 ** sync()ed before any data is written to database file page needSyncPgno.
5222 ** Currently, no such page exists in the page-cache and the
drhf5e7bb52008-02-18 14:47:33 +00005223 ** Pager.pInJournal bit has been set. This needs to be remedied by loading
danielk197794daf7f2004-11-08 09:26:09 +00005224 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00005225 **
danielk1977a98d7b42008-01-18 13:42:54 +00005226 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00005227 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00005228 ** array. Otherwise, if the page is loaded and written again in
5229 ** this transaction, it may be written to the database file before
5230 ** it is synced into the journal file. This way, it may end up in
5231 ** the journal file twice, but that is not a problem.
5232 **
danielk19773b8a05f2007-03-19 17:44:26 +00005233 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00005234 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00005235 */
5236 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005237 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00005238 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00005239 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00005240 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00005241 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
5242 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00005243 }
danielk197787c29a92008-01-18 11:33:16 +00005244 pagerLeave(pPager);
5245 return rc;
5246 }
danielk1977ae825582004-11-23 09:06:55 +00005247 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00005248 pPgHdr->needSync = 1;
5249 pPgHdr->inJournal = 1;
5250 makeDirty(pPgHdr);
5251 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00005252 }
5253
drh86f8c192007-08-22 00:39:19 +00005254 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005255 return SQLITE_OK;
5256}
5257#endif
5258
danielk19773b8a05f2007-03-19 17:44:26 +00005259/*
5260** Return a pointer to the data for the specified page.
5261*/
5262void *sqlite3PagerGetData(DbPage *pPg){
5263 return PGHDR_TO_DATA(pPg);
5264}
5265
5266/*
5267** Return a pointer to the Pager.nExtra bytes of "extra" space
5268** allocated along with the specified page.
5269*/
5270void *sqlite3PagerGetExtra(DbPage *pPg){
5271 Pager *pPager = pPg->pPager;
5272 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
5273}
5274
danielk197741483462007-03-24 16:45:04 +00005275/*
5276** Get/set the locking-mode for this pager. Parameter eMode must be one
5277** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
5278** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
5279** the locking-mode is set to the value specified.
5280**
5281** The returned value is either PAGER_LOCKINGMODE_NORMAL or
5282** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
5283** locking-mode.
5284*/
5285int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00005286 assert( eMode==PAGER_LOCKINGMODE_QUERY
5287 || eMode==PAGER_LOCKINGMODE_NORMAL
5288 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5289 assert( PAGER_LOCKINGMODE_QUERY<0 );
5290 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5291 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00005292 pPager->exclusiveMode = eMode;
5293 }
5294 return (int)pPager->exclusiveMode;
5295}
5296
drh3b020132008-04-17 17:02:01 +00005297/*
5298** Get/set the journal-mode for this pager. Parameter eMode must be one
5299** of PAGER_JOURNALMODE_QUERY, PAGER_JOURNALMODE_DELETE or
5300** PAGER_JOURNALMODE_PERSIST. If the parameter is not _QUERY, then
5301** the journal-mode is set to the value specified.
5302**
5303** The returned value is either PAGER_JOURNALMODE_DELETE or
5304** PAGER_JOURNALMODE_PERSIST, indicating the current (possibly updated)
5305** journal-mode.
5306*/
5307int sqlite3PagerJournalMode(Pager *pPager, int eMode){
5308 assert( eMode==PAGER_JOURNALMODE_QUERY
5309 || eMode==PAGER_JOURNALMODE_DELETE
drhfdc40e92008-04-17 20:59:37 +00005310 || eMode==PAGER_JOURNALMODE_PERSIST
5311 || eMode==PAGER_JOURNALMODE_OFF );
drh3b020132008-04-17 17:02:01 +00005312 assert( PAGER_JOURNALMODE_QUERY<0 );
5313 assert( PAGER_JOURNALMODE_DELETE>=0 && PAGER_JOURNALMODE_PERSIST>=0 );
drhfdc40e92008-04-17 20:59:37 +00005314 if( eMode>=0 ){
5315 pPager->journalMode = eMode;
drh3b020132008-04-17 17:02:01 +00005316 }
drhfdc40e92008-04-17 20:59:37 +00005317 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00005318}
5319
danielk1977b53e4962008-06-04 06:45:59 +00005320/*
5321** Get/set the size-limit used for persistent journal files.
5322*/
5323i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
5324 if( iLimit>=-1 ){
5325 pPager->journalSizeLimit = iLimit;
5326 }
5327 return pPager->journalSizeLimit;
5328}
5329
drhd919fe12007-12-11 19:34:44 +00005330#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00005331/*
5332** Print a listing of all referenced pages and their ref count.
5333*/
danielk19773b8a05f2007-03-19 17:44:26 +00005334void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00005335 PgHdr *pPg;
5336 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5337 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00005338 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5339 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00005340 }
5341}
5342#endif
drh2e66f0b2005-04-28 17:18:48 +00005343
5344#endif /* SQLITE_OMIT_DISKIO */