blob: fca3d3bc9e5666501056d34ae11f92cc7fbd45a7 [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**
danielk197752b472a2008-05-05 16:23:55 +000021** @(#) $Id: pager.c,v 1.439 2008/05/05 16:23:55 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#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 */
drh80e35f42007-03-30 14:06:34 +0000354 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000355 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000356 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000357 int dbSize; /* Number of pages in the file */
358 int origDbSize; /* dbSize before the current change */
359 int stmtSize; /* Size of database (in pages) at stmt_begin() */
360 int nRec; /* Number of pages written to the journal */
361 u32 cksumInit; /* Quasi-random value added to every checksum */
362 int stmtNRec; /* Number of records in stmt subjournal */
363 int nExtra; /* Add this many bytes to each in-memory page */
364 int pageSize; /* Number of bytes in a page */
365 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000366 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
367 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000368 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000369 Bitvec *pInJournal; /* One bit for each page in the database file */
370 Bitvec *pInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000371 char *zFilename; /* Name of the database file */
372 char *zJournal; /* Name of the journal file */
373 char *zDirectory; /* Directory hold database and journal files */
drh334b2992007-09-06 23:28:23 +0000374 char *zStmtJrnl; /* Name of the statement journal file */
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 */
403 int iInUseMM; /* Non-zero if unavailable to MM */
404 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 */
drhd9b02572001-04-15 00:37:09 +0000408};
409
410/*
drh538f5702007-04-13 02:14:30 +0000411** The following global variables hold counters used for
412** testing purposes only. These variables do not exist in
413** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000414*/
415#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000416int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
417int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
418int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
419int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
420# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000421#else
drh538f5702007-04-13 02:14:30 +0000422# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000423#endif
424
drh86f8c192007-08-22 00:39:19 +0000425/*
426** The following variable points to the head of a double-linked list
427** of all pagers that are eligible for page stealing by the
428** sqlite3_release_memory() interface. Access to this list is
429** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
430*/
431#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
432static Pager *sqlite3PagerList = 0;
danielk19779f61c2f2007-08-27 17:27:49 +0000433static PagerLruList sqlite3LruPageList = {0, 0, 0};
drh86f8c192007-08-22 00:39:19 +0000434#endif
drh538f5702007-04-13 02:14:30 +0000435
436
drhfcd35c72005-05-21 02:48:08 +0000437/*
drh5e00f6c2001-09-13 13:46:56 +0000438** Journal files begin with the following magic string. The data
439** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000440**
drhae2b40c2004-06-09 19:03:54 +0000441** Since version 2.8.0, the journal format contains additional sanity
442** checking information. If the power fails while the journal is begin
443** written, semi-random garbage data might appear in the journal
444** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000445** to roll the journal back, the database could be corrupted. The additional
446** sanity checking data is an attempt to discover the garbage in the
447** journal and ignore it.
448**
drhae2b40c2004-06-09 19:03:54 +0000449** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000450** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000451** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000452** This cksum is initialized to a 32-bit random value that appears in the
453** journal file right after the header. The random initializer is important,
454** because garbage data that appears at the end of a journal is likely
455** data that was once in other files that have now been deleted. If the
456** garbage data came from an obsolete journal file, the checksums might
457** be correct. But by initializing the checksum to random value which
458** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000459*/
drhae2b40c2004-06-09 19:03:54 +0000460static const unsigned char aJournalMagic[] = {
461 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000462};
463
464/*
drh726de592004-06-10 23:35:50 +0000465** The size of the header and of each page in the journal is determined
466** by the following macros.
drh968af522003-02-11 14:55:40 +0000467*/
drhae2b40c2004-06-09 19:03:54 +0000468#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000469
danielk197776572402004-06-25 02:38:54 +0000470/*
471** The journal header size for this pager. In the future, this could be
472** set to some value read from the disk controller. The important
473** characteristic is that it is the same size as a disk sector.
474*/
475#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
476
drhb7f91642004-10-31 02:22:47 +0000477/*
478** The macro MEMDB is true if we are dealing with an in-memory database.
479** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
480** the value of MEMDB will be a constant and the compiler will optimize
481** out code that would never execute.
482*/
483#ifdef SQLITE_OMIT_MEMORYDB
484# define MEMDB 0
485#else
486# define MEMDB pPager->memDb
487#endif
488
489/*
danielk197776572402004-06-25 02:38:54 +0000490** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
491** reserved for working around a windows/posix incompatibility). It is
492** used in the journal to signify that the remainder of the journal file
493** is devoted to storing a master journal name - there are no more pages to
494** roll back. See comments for function writeMasterJournal() for details.
495*/
danielk1977599fcba2004-11-08 07:13:13 +0000496/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
497#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000498
drh968af522003-02-11 14:55:40 +0000499/*
danielk197726836652005-01-17 01:33:13 +0000500** The maximum legal page number is (2^31 - 1).
501*/
502#define PAGER_MAX_PGNO 2147483647
503
504/*
drh86f8c192007-08-22 00:39:19 +0000505** The pagerEnter() and pagerLeave() routines acquire and release
506** a mutex on each pager. The mutex is recursive.
507**
508** This is a special-purpose mutex. It only provides mutual exclusion
509** between the Btree and the Memory Management sqlite3_release_memory()
510** function. It does not prevent, for example, two Btrees from accessing
511** the same pager at the same time. Other general-purpose mutexes in
512** the btree layer handle that chore.
513*/
514#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
515 static void pagerEnter(Pager *p){
516 p->iInUseDB++;
517 if( p->iInUseMM && p->iInUseDB==1 ){
518 sqlite3_mutex *mutex;
drh26e4a8b2008-05-01 17:16:52 +0000519#ifndef SQLITE_MUTEX_NOOP
drh86f8c192007-08-22 00:39:19 +0000520 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +0000521#endif
drh86f8c192007-08-22 00:39:19 +0000522 p->iInUseDB = 0;
523 sqlite3_mutex_enter(mutex);
524 p->iInUseDB = 1;
525 sqlite3_mutex_leave(mutex);
526 }
527 assert( p->iInUseMM==0 );
528 }
529 static void pagerLeave(Pager *p){
530 p->iInUseDB--;
531 assert( p->iInUseDB>=0 );
532 }
533#else
534# define pagerEnter(X)
535# define pagerLeave(X)
536#endif
537
538/*
danielk197784f786f2007-08-28 08:00:17 +0000539** Add page pPg to the end of the linked list managed by structure
540** pList (pPg becomes the last entry in the list - the most recently
541** used). Argument pLink should point to either pPg->free or pPg->gfree,
542** depending on whether pPg is being added to the pager-specific or
543** global LRU list.
544*/
danielk19779f61c2f2007-08-27 17:27:49 +0000545static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
546 pLink->pNext = 0;
547 pLink->pPrev = pList->pLast;
548
danielk197784f786f2007-08-28 08:00:17 +0000549#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
550 assert(pLink==&pPg->free || pLink==&pPg->gfree);
551 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
552#endif
553
danielk19779f61c2f2007-08-27 17:27:49 +0000554 if( pList->pLast ){
555 int iOff = (char *)pLink - (char *)pPg;
556 PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
557 pLastLink->pNext = pPg;
558 }else{
559 assert(!pList->pFirst);
560 pList->pFirst = pPg;
561 }
562
563 pList->pLast = pPg;
564 if( !pList->pFirstSynced && pPg->needSync==0 ){
565 pList->pFirstSynced = pPg;
566 }
567}
568
danielk197784f786f2007-08-28 08:00:17 +0000569/*
570** Remove pPg from the list managed by the structure pointed to by pList.
571**
572** Argument pLink should point to either pPg->free or pPg->gfree, depending
573** on whether pPg is being added to the pager-specific or global LRU list.
574*/
danielk19779f61c2f2007-08-27 17:27:49 +0000575static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
576 int iOff = (char *)pLink - (char *)pPg;
577
danielk197784f786f2007-08-28 08:00:17 +0000578#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
579 assert(pLink==&pPg->free || pLink==&pPg->gfree);
580 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
581#endif
582
danielk19779f61c2f2007-08-27 17:27:49 +0000583 if( pPg==pList->pFirst ){
584 pList->pFirst = pLink->pNext;
585 }
586 if( pPg==pList->pLast ){
587 pList->pLast = pLink->pPrev;
588 }
589 if( pLink->pPrev ){
590 PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
591 pPrevLink->pNext = pLink->pNext;
592 }
593 if( pLink->pNext ){
594 PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
595 pNextLink->pPrev = pLink->pPrev;
596 }
597 if( pPg==pList->pFirstSynced ){
598 PgHdr *p = pLink->pNext;
599 while( p && p->needSync ){
600 PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
601 p = pL->pNext;
602 }
603 pList->pFirstSynced = p;
604 }
605
606 pLink->pNext = pLink->pPrev = 0;
607}
608
609/*
danielk197784f786f2007-08-28 08:00:17 +0000610** Add page pPg to the list of free pages for the pager. If
611** memory-management is enabled, also add the page to the global
612** list of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000613*/
614static void lruListAdd(PgHdr *pPg){
615 listAdd(&pPg->pPager->lru, &pPg->free, pPg);
616#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
617 if( !pPg->pPager->memDb ){
618 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
619 listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
620 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
621 }
622#endif
623}
624
625/*
danielk197784f786f2007-08-28 08:00:17 +0000626** Remove page pPg from the list of free pages for the associated pager.
627** If memory-management is enabled, also remove pPg from the global list
628** of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000629*/
630static void lruListRemove(PgHdr *pPg){
631 listRemove(&pPg->pPager->lru, &pPg->free, pPg);
632#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
633 if( !pPg->pPager->memDb ){
634 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
635 listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
636 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
637 }
638#endif
639}
640
641/*
danielk197784f786f2007-08-28 08:00:17 +0000642** This function is called just after the needSync flag has been cleared
643** from all pages managed by pPager (usually because the journal file
644** has just been synced). It updates the pPager->lru.pFirstSynced variable
645** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
646** variable also.
danielk19779f61c2f2007-08-27 17:27:49 +0000647*/
648static void lruListSetFirstSynced(Pager *pPager){
649 pPager->lru.pFirstSynced = pPager->lru.pFirst;
650#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
651 if( !pPager->memDb ){
652 PgHdr *p;
653 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
654 for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
655 assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
656 sqlite3LruPageList.pFirstSynced = p;
657 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
658 }
659#endif
660}
661
danielk1977f35843b2007-04-07 15:03:17 +0000662/*
663** Return true if page *pPg has already been written to the statement
664** journal (or statement snapshot has been created, if *pPg is part
665** of an in-memory database).
666*/
667static int pageInStatement(PgHdr *pPg){
668 Pager *pPager = pPg->pPager;
669 if( MEMDB ){
670 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
671 }else{
drhf5e7bb52008-02-18 14:47:33 +0000672 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000673 }
674}
drh8ca0c722006-05-07 17:49:38 +0000675
676/*
677** Change the size of the pager hash table to N. N must be a power
678** of two.
679*/
680static void pager_resize_hash_table(Pager *pPager, int N){
681 PgHdr **aHash, *pPg;
682 assert( N>0 && (N&(N-1))==0 );
drheee4c8c2008-02-18 22:24:57 +0000683#ifdef SQLITE_MALLOC_SOFT_LIMIT
684 if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){
685 N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]);
686 }
687 if( N==pPager->nHash ) return;
688#endif
drhed138fb2007-08-22 22:04:37 +0000689 pagerLeave(pPager);
drh643167f2008-01-22 21:30:53 +0000690 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pPager->aHash!=0);
drh17435752007-08-16 04:30:38 +0000691 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drh643167f2008-01-22 21:30:53 +0000692 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0);
drhed138fb2007-08-22 22:04:37 +0000693 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000694 if( aHash==0 ){
695 /* Failure to rehash is not an error. It is only a performance hit. */
696 return;
697 }
drh17435752007-08-16 04:30:38 +0000698 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000699 pPager->nHash = N;
700 pPager->aHash = aHash;
701 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000702 int h;
703 if( pPg->pgno==0 ){
704 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
705 continue;
706 }
707 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000708 pPg->pNextHash = aHash[h];
709 if( aHash[h] ){
710 aHash[h]->pPrevHash = pPg;
711 }
712 aHash[h] = pPg;
713 pPg->pPrevHash = 0;
714 }
715}
716
drhdd793422001-06-28 01:54:48 +0000717/*
drh34e79ce2004-02-08 06:05:46 +0000718** Read a 32-bit integer from the given file descriptor. Store the integer
719** that is read in *pRes. Return SQLITE_OK if everything worked, or an
720** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000721**
722** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000723*/
danielk197762079062007-08-15 17:08:46 +0000724static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000725 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000726 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000727 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000728 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000729 }
drh94f33312002-08-12 12:29:56 +0000730 return rc;
731}
732
733/*
drh97b57482006-01-10 20:32:32 +0000734** Write a 32-bit integer into a string buffer in big-endian byte order.
735*/
drha3152892007-05-05 11:48:52 +0000736#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000737
738/*
drh34e79ce2004-02-08 06:05:46 +0000739** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
740** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000741*/
danielk197762079062007-08-15 17:08:46 +0000742static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000743 char ac[4];
drh97b57482006-01-10 20:32:32 +0000744 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000745 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000746}
747
drh2554f8b2003-01-22 01:26:44 +0000748/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000749** If file pFd is open, call sqlite3OsUnlock() on it.
750*/
751static int osUnlock(sqlite3_file *pFd, int eLock){
752 if( !pFd->pMethods ){
753 return SQLITE_OK;
754 }
755 return sqlite3OsUnlock(pFd, eLock);
756}
757
758/*
danielk1977c7b60172007-08-22 11:22:03 +0000759** This function determines whether or not the atomic-write optimization
760** can be used with this pager. The optimization can be used if:
761**
762** (a) the value returned by OsDeviceCharacteristics() indicates that
763** a database page may be written atomically, and
764** (b) the value returned by OsSectorSize() is less than or equal
765** to the page size.
766**
767** If the optimization cannot be used, 0 is returned. If it can be used,
768** then the value returned is the size of the journal file when it
769** contains rollback data for exactly one page.
770*/
771#ifdef SQLITE_ENABLE_ATOMIC_WRITE
772static int jrnlBufferSize(Pager *pPager){
773 int dc; /* Device characteristics */
774 int nSector; /* Sector size */
775 int nPage; /* Page size */
776 sqlite3_file *fd = pPager->fd;
777
778 if( fd->pMethods ){
779 dc = sqlite3OsDeviceCharacteristics(fd);
780 nSector = sqlite3OsSectorSize(fd);
781 nPage = pPager->pageSize;
782 }
783
784 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
785 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
786
danielk1977f8940ae2007-08-23 11:07:10 +0000787 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000788 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
789 }
790 return 0;
791}
792#endif
793
794/*
danielk1977aef0bf62005-12-30 16:28:01 +0000795** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000796** code. The first argument is a pointer to the pager structure, the
797** second the error-code about to be returned by a pager API function.
798** The value returned is a copy of the second argument to this function.
799**
drh4f0ee682007-03-30 20:43:40 +0000800** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000801** the error becomes persistent. Until the persisten error is cleared,
802** subsequent API calls on this Pager will immediately return the same
803** error code.
804**
805** A persistent error indicates that the contents of the pager-cache
806** cannot be trusted. This state can be cleared by completely discarding
807** the contents of the pager-cache. If a transaction was active when
808** the persistent error occured, then the rollback journal may need
809** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000810*/
danielk1977ae72d982007-10-03 08:46:44 +0000811static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000812static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000813 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000814 assert(
815 pPager->errCode==SQLITE_FULL ||
816 pPager->errCode==SQLITE_OK ||
817 (pPager->errCode & 0xff)==SQLITE_IOERR
818 );
danielk1977979f38e2007-03-27 16:19:51 +0000819 if(
drh4ac285a2006-09-15 07:28:50 +0000820 rc2==SQLITE_FULL ||
821 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000822 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000823 ){
824 pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000825 if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
826 /* If the pager is already unlocked, call pager_unlock() now to
827 ** clear the error state and ensure that the pager-cache is
828 ** completely empty.
829 */
830 pager_unlock(pPager);
831 }
danielk1977aef0bf62005-12-30 16:28:01 +0000832 }
833 return rc;
834}
835
drh477731b2007-06-16 03:06:27 +0000836/*
837** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
838** on the cache using a hash function. This is used for testing
839** and debugging only.
840*/
danielk19773c407372005-02-15 02:54:14 +0000841#ifdef SQLITE_CHECK_PAGES
842/*
843** Return a 32-bit hash of the page data for pPage.
844*/
drh477731b2007-06-16 03:06:27 +0000845static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000846 u32 hash = 0;
847 int i;
drh477731b2007-06-16 03:06:27 +0000848 for(i=0; i<nByte; i++){
849 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000850 }
851 return hash;
852}
drh477731b2007-06-16 03:06:27 +0000853static u32 pager_pagehash(PgHdr *pPage){
854 return pager_datahash(pPage->pPager->pageSize,
855 (unsigned char *)PGHDR_TO_DATA(pPage));
856}
danielk19773c407372005-02-15 02:54:14 +0000857
858/*
859** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
860** is defined, and NDEBUG is not defined, an assert() statement checks
861** that the page is either dirty or still matches the calculated page-hash.
862*/
863#define CHECK_PAGE(x) checkPage(x)
864static void checkPage(PgHdr *pPg){
865 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000866 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000867 pPg->pageHash==pager_pagehash(pPg) );
868}
869
870#else
drh8ffa8172007-06-18 17:25:17 +0000871#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000872#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000873#define CHECK_PAGE(x)
874#endif
875
drhed7c8552001-04-11 14:29:21 +0000876/*
danielk197776572402004-06-25 02:38:54 +0000877** When this is called the journal file for pager pPager must be open.
878** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000879** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000880**
danielk197765839c62007-08-30 08:08:17 +0000881** zMaster must point to a buffer of at least nMaster bytes allocated by
882** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
883** enough space to write the master journal name). If the master journal
884** name in the journal is longer than nMaster bytes (including a
885** nul-terminator), then this is handled as if no master journal name
886** were present in the journal.
887**
888** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000889** SQLITE_OK returned.
890*/
danielk197765839c62007-08-30 08:08:17 +0000891static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000892 int rc;
893 u32 len;
drheb206252004-10-01 02:00:31 +0000894 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000895 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000896 int i;
danielk197776572402004-06-25 02:38:54 +0000897 unsigned char aMagic[8]; /* A buffer to hold the magic header */
898
danielk197765839c62007-08-30 08:08:17 +0000899 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000900
drh054889e2005-11-30 03:20:31 +0000901 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000902 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000903
danielk197762079062007-08-15 17:08:46 +0000904 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000905 if( rc!=SQLITE_OK ) return rc;
906
danielk197765839c62007-08-30 08:08:17 +0000907 if( len>=nMaster ){
908 return SQLITE_OK;
909 }
910
danielk197762079062007-08-15 17:08:46 +0000911 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000912 if( rc!=SQLITE_OK ) return rc;
913
danielk197762079062007-08-15 17:08:46 +0000914 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000915 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
916
danielk197765839c62007-08-30 08:08:17 +0000917 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000918 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000919 return rc;
920 }
danielk197765839c62007-08-30 08:08:17 +0000921 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000922
923 /* See if the checksum matches the master journal name */
924 for(i=0; i<len; i++){
danielk197765839c62007-08-30 08:08:17 +0000925 cksum -= zMaster[i];
926 }
danielk19778191bff2004-06-28 04:52:30 +0000927 if( cksum ){
928 /* If the checksum doesn't add up, then one or more of the disk sectors
929 ** containing the master journal filename is corrupted. This means
930 ** definitely roll back, so just return SQLITE_OK and report a (nul)
931 ** master-journal filename.
932 */
danielk197765839c62007-08-30 08:08:17 +0000933 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000934 }
danielk197776572402004-06-25 02:38:54 +0000935
936 return SQLITE_OK;
937}
938
939/*
940** Seek the journal file descriptor to the next sector boundary where a
941** journal header may be read or written. Pager.journalOff is updated with
942** the new seek offset.
943**
944** i.e for a sector size of 512:
945**
946** Input Offset Output Offset
947** ---------------------------------------
948** 0 0
949** 512 512
950** 100 512
951** 2000 2048
952**
953*/
danielk197762079062007-08-15 17:08:46 +0000954static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000955 i64 offset = 0;
956 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000957 if( c ){
958 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
959 }
960 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
961 assert( offset>=c );
962 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
963 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000964}
965
966/*
drhf3a87622008-04-17 14:16:42 +0000967** Write zeros over the header of the journal file. This has the
968** effect of invalidating the journal file and committing the
969** transaction.
970*/
971static int zeroJournalHdr(Pager *pPager){
972 int rc;
973 static const char zeroHdr[28];
974
975 IOTRACE(("JZEROHDR %p\n", pPager))
976 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
drha06ecba2008-04-22 17:15:17 +0000977 if( rc==SQLITE_OK ){
978 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY | pPager->sync_flags);
979 }
drhf3a87622008-04-17 14:16:42 +0000980 return rc;
981}
982
983/*
danielk197776572402004-06-25 02:38:54 +0000984** The journal file must be open when this routine is called. A journal
985** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
986** current location.
987**
988** The format for the journal header is as follows:
989** - 8 bytes: Magic identifying journal format.
990** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
991** - 4 bytes: Random number used for page hash.
992** - 4 bytes: Initial database page count.
993** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000994** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000995**
danielk197767c007b2008-03-20 04:45:49 +0000996** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000997*/
998static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +0000999 int rc = SQLITE_OK;
1000 char *zHeader = pPager->pTmpSpace;
1001 int nHeader = pPager->pageSize;
1002 int nWrite;
1003
1004 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
1005 nHeader = JOURNAL_HDR_SZ(pPager);
1006 }
danielk197776572402004-06-25 02:38:54 +00001007
danielk19774099f6e2007-03-19 11:25:20 +00001008 if( pPager->stmtHdrOff==0 ){
1009 pPager->stmtHdrOff = pPager->journalOff;
1010 }
1011
danielk197762079062007-08-15 17:08:46 +00001012 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001013 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001014
drh97b57482006-01-10 20:32:32 +00001015 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +00001016
1017 /*
1018 ** Write the nRec Field - the number of page records that follow this
1019 ** journal header. Normally, zero is written to this value at this time.
1020 ** After the records are added to the journal (and the journal synced,
1021 ** if in full-sync mode), the zero is overwritten with the true number
1022 ** of records (see syncJournal()).
1023 **
1024 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
1025 ** reading the journal this value tells SQLite to assume that the
1026 ** rest of the journal file contains valid page records. This assumption
1027 ** is dangerous, as if a failure occured whilst writing to the journal
1028 ** file it may contain some garbage data. There are two scenarios
1029 ** where this risk can be ignored:
1030 **
1031 ** * When the pager is in no-sync mode. Corruption can follow a
1032 ** power failure in this case anyway.
1033 **
1034 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1035 ** that garbage data is never appended to the journal file.
1036 */
1037 assert(pPager->fd->pMethods||pPager->noSync);
1038 if( (pPager->noSync)
1039 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1040 ){
1041 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1042 }else{
1043 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1044 }
1045
drh97b57482006-01-10 20:32:32 +00001046 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +00001047 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +00001048 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1049 /* The initial database size */
1050 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1051 /* The assumed sector size for this process */
1052 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
danielk197767c007b2008-03-20 04:45:49 +00001053 if( pPager->journalHdr==0 ){
1054 /* The page size */
1055 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
1056 }
danielk197776572402004-06-25 02:38:54 +00001057
danielk1977a664f8e2008-04-22 14:31:48 +00001058 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
1059 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
1060 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
1061 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +00001062 }
danielk1977a664f8e2008-04-22 14:31:48 +00001063
danielk197776572402004-06-25 02:38:54 +00001064 return rc;
1065}
1066
1067/*
1068** The journal file must be open when this is called. A journal header file
1069** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1070** file. See comments above function writeJournalHdr() for a description of
1071** the journal header format.
1072**
1073** If the header is read successfully, *nRec is set to the number of
1074** page records following this header and *dbSize is set to the size of the
1075** database before the transaction began, in pages. Also, pPager->cksumInit
1076** is set to the value read from the journal header. SQLITE_OK is returned
1077** in this case.
1078**
1079** If the journal header file appears to be corrupted, SQLITE_DONE is
1080** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1081** cannot be read from the journal file an error code is returned.
1082*/
1083static int readJournalHdr(
1084 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001085 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001086 u32 *pNRec,
1087 u32 *pDbSize
1088){
1089 int rc;
1090 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001091 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +00001092 int iPageSize;
danielk197776572402004-06-25 02:38:54 +00001093
danielk197762079062007-08-15 17:08:46 +00001094 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001095 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1096 return SQLITE_DONE;
1097 }
danielk197762079062007-08-15 17:08:46 +00001098 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001099
danielk197762079062007-08-15 17:08:46 +00001100 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001101 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001102 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001103
1104 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1105 return SQLITE_DONE;
1106 }
1107
danielk197762079062007-08-15 17:08:46 +00001108 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001109 if( rc ) return rc;
1110
danielk197762079062007-08-15 17:08:46 +00001111 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001112 if( rc ) return rc;
1113
danielk197762079062007-08-15 17:08:46 +00001114 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001115 if( rc ) return rc;
1116
danielk197767c007b2008-03-20 04:45:49 +00001117 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
1118 if( rc==SQLITE_OK
1119 && iPageSize>=512
1120 && iPageSize<=SQLITE_MAX_PAGE_SIZE
1121 && ((iPageSize-1)&iPageSize)==0
1122 ){
1123 u16 pagesize = iPageSize;
1124 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
1125 }
1126 if( rc ) return rc;
1127
danielk197776572402004-06-25 02:38:54 +00001128 /* Update the assumed sector-size to match the value used by
1129 ** the process that created this journal. If this journal was
1130 ** created by a process other than this one, then this routine
1131 ** is being called from within pager_playback(). The local value
1132 ** of Pager.sectorSize is restored at the end of that routine.
1133 */
danielk197762079062007-08-15 17:08:46 +00001134 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001135 if( rc ) return rc;
1136
1137 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001138 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001139}
1140
1141
1142/*
1143** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001144** pPager at the current location. The master journal name must be the last
1145** thing written to a journal file. If the pager is in full-sync mode, the
1146** journal file descriptor is advanced to the next sector boundary before
1147** anything is written. The format is:
1148**
1149** + 4 bytes: PAGER_MJ_PGNO.
1150** + N bytes: length of master journal name.
1151** + 4 bytes: N
1152** + 4 bytes: Master journal name checksum.
1153** + 8 bytes: aJournalMagic[].
1154**
1155** The master journal page checksum is the sum of the bytes in the master
1156** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001157**
1158** If zMaster is a NULL pointer (occurs for a single database transaction),
1159** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001160*/
1161static int writeMasterJournal(Pager *pPager, const char *zMaster){
1162 int rc;
1163 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001164 int i;
danielk197762079062007-08-15 17:08:46 +00001165 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +00001166 u32 cksum = 0;
1167 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001168
1169 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1170 pPager->setMaster = 1;
1171
1172 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001173 for(i=0; i<len; i++){
1174 cksum += zMaster[i];
1175 }
danielk197776572402004-06-25 02:38:54 +00001176
1177 /* If in full-sync mode, advance to the next disk sector before writing
1178 ** the master journal name. This is in case the previous page written to
1179 ** the journal has already been synced.
1180 */
1181 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001182 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001183 }
danielk197762079062007-08-15 17:08:46 +00001184 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001185 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001186
danielk197762079062007-08-15 17:08:46 +00001187 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001188 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001189 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001190
danielk197762079062007-08-15 17:08:46 +00001191 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001192 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001193 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001194
drh97b57482006-01-10 20:32:32 +00001195 put32bits(zBuf, len);
1196 put32bits(&zBuf[4], cksum);
1197 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001198 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +00001199 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +00001200 return rc;
1201}
1202
1203/*
drh03eb96a2002-11-10 23:32:56 +00001204** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001205** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001206**
1207** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001208** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001209** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001210** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001211*/
drh3aac2dd2004-04-26 14:10:20 +00001212static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001213 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001214 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1215 assert( MEMDB );
1216 if( !pHist->inStmt ){
1217 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1218 if( pPager->pStmt ){
1219 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1220 }
1221 pHist->pNextStmt = pPager->pStmt;
1222 pPager->pStmt = pPg;
1223 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001224 }
drh03eb96a2002-11-10 23:32:56 +00001225}
1226
1227/*
drhed7c8552001-04-11 14:29:21 +00001228** Find a page in the hash table given its page number. Return
1229** a pointer to the page or NULL if not found.
1230*/
drhd9b02572001-04-15 00:37:09 +00001231static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001232 PgHdr *p;
1233 if( pPager->aHash==0 ) return 0;
1234 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001235 while( p && p->pgno!=pgno ){
1236 p = p->pNextHash;
1237 }
1238 return p;
1239}
1240
1241/*
danielk1977e180dd92007-04-05 17:15:52 +00001242** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001243** sets the state of the pager back to what it was when it was first
1244** opened. Any outstanding pages are invalidated and subsequent attempts
1245** to access those pages will likely result in a coredump.
1246*/
drhd9b02572001-04-15 00:37:09 +00001247static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001248 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001249 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001250 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001251 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1252 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001253 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001254 lruListRemove(pPg);
drh0d180202008-02-14 23:26:56 +00001255 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00001256 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001257 }
danielk19779f61c2f2007-08-27 17:27:49 +00001258 assert(pPager->lru.pFirst==0);
1259 assert(pPager->lru.pFirstSynced==0);
1260 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001261 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001262 pPager->pAll = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001263 pPager->pDirty = 0;
drh8ca0c722006-05-07 17:49:38 +00001264 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001265 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001266 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001267 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001268 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001269}
1270
1271/*
danielk1977ae72d982007-10-03 08:46:44 +00001272** Unlock the database file.
1273**
1274** If the pager is currently in error state, discard the contents of
1275** the cache and reset the Pager structure internal state. If there is
1276** an open journal-file, then the next time a shared-lock is obtained
1277** on the pager file (by this or any other process), it will be
1278** treated as a hot-journal and rolled back.
1279*/
1280static void pager_unlock(Pager *pPager){
1281 if( !pPager->exclusiveMode ){
1282 if( !MEMDB ){
drh1aa5af12008-03-07 19:51:14 +00001283 int rc = osUnlock(pPager->fd, NO_LOCK);
drh308aa322008-03-07 20:14:38 +00001284 if( rc ) pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +00001285 pPager->dbSize = -1;
1286 IOTRACE(("UNLOCK %p\n", pPager))
1287
drh16e45a42008-04-19 20:34:18 +00001288 /* Always close the journal file when dropping the database lock.
1289 ** Otherwise, another connection with journal_mode=delete might
1290 ** delete the file out from under us.
1291 */
1292 if( pPager->journalOpen ){
1293 sqlite3OsClose(pPager->jfd);
1294 pPager->journalOpen = 0;
1295 sqlite3BitvecDestroy(pPager->pInJournal);
1296 pPager->pInJournal = 0;
1297 }
1298
danielk1977ae72d982007-10-03 08:46:44 +00001299 /* If Pager.errCode is set, the contents of the pager cache cannot be
1300 ** trusted. Now that the pager file is unlocked, the contents of the
1301 ** cache can be discarded and the error code safely cleared.
1302 */
1303 if( pPager->errCode ){
drh1aa5af12008-03-07 19:51:14 +00001304 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00001305 pager_reset(pPager);
1306 if( pPager->stmtOpen ){
1307 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +00001308 sqlite3BitvecDestroy(pPager->pInStmt);
1309 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001310 }
danielk1977ae72d982007-10-03 08:46:44 +00001311 pPager->stmtOpen = 0;
1312 pPager->stmtInUse = 0;
1313 pPager->journalOff = 0;
1314 pPager->journalStarted = 0;
1315 pPager->stmtAutoopen = 0;
1316 pPager->origDbSize = 0;
1317 }
1318 }
1319
1320 if( !MEMDB || pPager->errCode==SQLITE_OK ){
1321 pPager->state = PAGER_UNLOCK;
1322 pPager->changeCountDone = 0;
1323 }
1324 }
1325}
1326
1327/*
1328** Execute a rollback if a transaction is active and unlock the
1329** database file. If the pager has already entered the error state,
1330** do not attempt the rollback.
1331*/
1332static void pagerUnlockAndRollback(Pager *p){
drhfdc40e92008-04-17 20:59:37 +00001333 /* assert( p->state>=PAGER_RESERVED || p->journalOpen==0 ); */
danielk1977ae72d982007-10-03 08:46:44 +00001334 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
1335 sqlite3PagerRollback(p);
1336 }
1337 pager_unlock(p);
drhfdc40e92008-04-17 20:59:37 +00001338#if 0
danielk1977ae72d982007-10-03 08:46:44 +00001339 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1340 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drhfdc40e92008-04-17 20:59:37 +00001341#endif
danielk1977ae72d982007-10-03 08:46:44 +00001342}
1343
1344/*
drh80e35f42007-03-30 14:06:34 +00001345** This routine ends a transaction. A transaction is ended by either
1346** a COMMIT or a ROLLBACK.
1347**
drhed7c8552001-04-11 14:29:21 +00001348** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001349** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1350** the database lock and acquires a SHARED lock in its place if that is
1351** the appropriate thing to do. Release locks usually is appropriate,
1352** unless we are in exclusive access mode or unless this is a
1353** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1354**
1355** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001356**
1357** TODO: Consider keeping the journal file open for temporary databases.
1358** This might give a performance improvement on windows where opening
1359** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001360*/
drh80e35f42007-03-30 14:06:34 +00001361static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001362 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001363 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001364 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001365 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001366 if( pPager->state<PAGER_RESERVED ){
1367 return SQLITE_OK;
1368 }
danielk19773b8a05f2007-03-19 17:44:26 +00001369 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001370 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001371 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001372 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001373 }
drhda47d772002-12-02 04:25:19 +00001374 if( pPager->journalOpen ){
drhfdc40e92008-04-17 20:59:37 +00001375 if( (pPager->exclusiveMode ||
1376 pPager->journalMode==PAGER_JOURNALMODE_PERSIST)
1377 && (rc = zeroJournalHdr(pPager))==SQLITE_OK ){
danielk197741483462007-03-24 16:45:04 +00001378 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001379 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001380 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001381 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001382 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001383 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001384 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001385 }
danielk197741483462007-03-24 16:45:04 +00001386 }
drhf5e7bb52008-02-18 14:47:33 +00001387 sqlite3BitvecDestroy(pPager->pInJournal);
1388 pPager->pInJournal = 0;
drhda47d772002-12-02 04:25:19 +00001389 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1390 pPg->inJournal = 0;
1391 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001392 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001393 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001394#ifdef SQLITE_CHECK_PAGES
1395 pPg->pageHash = pager_pagehash(pPg);
1396#endif
drhda47d772002-12-02 04:25:19 +00001397 }
drh605ad8f2006-05-03 23:34:05 +00001398 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001399 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001400 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001401 }else{
drhf5e7bb52008-02-18 14:47:33 +00001402 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001403 }
danielk1977979f38e2007-03-27 16:19:51 +00001404
danielk197741483462007-03-24 16:45:04 +00001405 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001406 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001407 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001408 }else if( pPager->state==PAGER_SYNCED ){
1409 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001410 }
danielk1977334cdb62007-03-26 08:05:12 +00001411 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001412 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001413 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001414 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001415 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001416
1417 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001418}
1419
drhed7c8552001-04-11 14:29:21 +00001420/*
drh968af522003-02-11 14:55:40 +00001421** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001422**
1423** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001424** random initial value and the page number. We experimented with
1425** a checksum of the entire data, but that was found to be too slow.
1426**
1427** Note that the page number is stored at the beginning of data and
1428** the checksum is stored at the end. This is important. If journal
1429** corruption occurs due to a power failure, the most likely scenario
1430** is that one end or the other of the record will be changed. It is
1431** much less likely that the two ends of the journal record will be
1432** correct and the middle be corrupt. Thus, this "checksum" scheme,
1433** though fast and simple, catches the mostly likely kind of corruption.
1434**
1435** FIX ME: Consider adding every 200th (or so) byte of the data to the
1436** checksum. That way if a single page spans 3 or more disk sectors and
1437** only the middle sector is corrupt, we will still have a reasonable
1438** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001439*/
drh74161702006-02-24 02:53:49 +00001440static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001441 u32 cksum = pPager->cksumInit;
1442 int i = pPager->pageSize-200;
1443 while( i>0 ){
1444 cksum += aData[i];
1445 i -= 200;
1446 }
drh968af522003-02-11 14:55:40 +00001447 return cksum;
1448}
1449
drh605ad8f2006-05-03 23:34:05 +00001450/* Forward declaration */
1451static void makeClean(PgHdr*);
1452
drh968af522003-02-11 14:55:40 +00001453/*
drhfa86c412002-02-02 15:01:15 +00001454** Read a single page from the journal file opened on file descriptor
1455** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001456**
drh726de592004-06-10 23:35:50 +00001457** If useCksum==0 it means this journal does not use checksums. Checksums
1458** are not used in statement journals because statement journals do not
1459** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001460*/
danielk197762079062007-08-15 17:08:46 +00001461static int pager_playback_one_page(
1462 Pager *pPager,
1463 sqlite3_file *jfd,
1464 i64 offset,
1465 int useCksum
1466){
drhfa86c412002-02-02 15:01:15 +00001467 int rc;
drhae2b40c2004-06-09 19:03:54 +00001468 PgHdr *pPg; /* An existing page in the cache */
1469 Pgno pgno; /* The page number of a page in journal */
1470 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001471 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001472
drh96362842005-03-20 22:47:56 +00001473 /* useCksum should be true for the main journal and false for
1474 ** statement journals. Verify that this is always the case
1475 */
drh9cbe6352005-11-29 03:13:21 +00001476 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001477 assert( aData );
drh96362842005-03-20 22:47:56 +00001478
danielk197762079062007-08-15 17:08:46 +00001479 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001480 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001481 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001482 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001483 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001484
drh968af522003-02-11 14:55:40 +00001485 /* Sanity checking on the page. This is more important that I originally
1486 ** thought. If a power failure occurs while the journal is being written,
1487 ** it could cause invalid data to be written into the journal. We need to
1488 ** detect this invalid data (with high probability) and ignore it.
1489 */
danielk197775edc162004-06-26 01:48:18 +00001490 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001491 return SQLITE_DONE;
1492 }
drhae2b40c2004-06-09 19:03:54 +00001493 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001494 return SQLITE_OK;
1495 }
drhae2b40c2004-06-09 19:03:54 +00001496 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001497 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001498 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001499 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001500 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001501 return SQLITE_DONE;
1502 }
1503 }
drhfa86c412002-02-02 15:01:15 +00001504
danielk1977aa5ccdf2004-06-14 05:10:42 +00001505 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001506
1507 /* If the pager is in RESERVED state, then there must be a copy of this
1508 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001509 ** not the database file. The page is left marked dirty in this case.
1510 **
danielk19772df71c72007-05-24 07:22:42 +00001511 ** An exception to the above rule: If the database is in no-sync mode
1512 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001513 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1514 ** during a Movepage() call, then the page may not be in the cache
1515 ** either. So the condition described in the above paragraph is not
1516 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001517 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001518 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1519 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001520 **
1521 ** Ticket #1171: The statement journal might contain page content that is
1522 ** different from the page content at the start of the transaction.
1523 ** This occurs when a page is changed prior to the start of a statement
1524 ** then changed again within the statement. When rolling back such a
1525 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001526 ** for certain that original page contents are synced into the main rollback
1527 ** journal. Otherwise, a power loss might leave modified data in the
1528 ** database file without an entry in the rollback journal that can
1529 ** restore the database to its original form. Two conditions must be
1530 ** met before writing to the database files. (1) the database must be
1531 ** locked. (2) we know that the original page content is fully synced
1532 ** in the main journal either because the page is not in cache or else
1533 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001534 **
1535 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1536 ** is possible to fail a statement on a database that does not yet exist.
1537 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001538 */
drhae2b40c2004-06-09 19:03:54 +00001539 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001540 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1541 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh4c02a232008-04-14 23:13:45 +00001542 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0)
1543 && pPager->fd->pMethods ){
danielk197762079062007-08-15 17:08:46 +00001544 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1545 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001546 if( pPg ){
1547 makeClean(pPg);
1548 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001549 }
drhfa86c412002-02-02 15:01:15 +00001550 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001551 /* No page should ever be explicitly rolled back that is in use, except
1552 ** for page 1 which is held in use in order to keep the lock on the
1553 ** database active. However such a page may be rolled back as a result
1554 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001555 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001556 */
drhb6f41482004-05-14 01:58:11 +00001557 void *pData;
danielk197728129562005-01-11 10:25:06 +00001558 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001559 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001560 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001561 if( pPager->xReiniter ){
1562 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001563 }
danielk19773c407372005-02-15 02:54:14 +00001564#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001565 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001566#endif
drh86a88112007-04-16 15:02:19 +00001567 /* If this was page 1, then restore the value of Pager.dbFileVers.
1568 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001569 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001570 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001571 }
drh86a88112007-04-16 15:02:19 +00001572
1573 /* Decode the page just read from disk */
1574 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001575 }
1576 return rc;
1577}
1578
1579/*
danielk197713adf8a2004-06-03 16:08:41 +00001580** Parameter zMaster is the name of a master journal file. A single journal
1581** file that referred to the master journal file has just been rolled back.
1582** This routine checks if it is possible to delete the master journal file,
1583** and does so if it is.
drh726de592004-06-10 23:35:50 +00001584**
danielk197765839c62007-08-30 08:08:17 +00001585** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1586** available for use within this function.
1587**
1588**
drh726de592004-06-10 23:35:50 +00001589** The master journal file contains the names of all child journals.
1590** To tell if a master journal can be deleted, check to each of the
1591** children. If all children are either missing or do not refer to
1592** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001593*/
danielk1977b4b47412007-08-17 15:53:36 +00001594static int pager_delmaster(Pager *pPager, const char *zMaster){
1595 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001596 int rc;
1597 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001598 sqlite3_file *pMaster;
1599 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001600 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001601 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001602
1603 /* Open the master journal file exclusively in case some other process
1604 ** is running this routine also. Not that it makes too much difference.
1605 */
danielk1977b4b47412007-08-17 15:53:36 +00001606 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001607 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001608 if( !pMaster ){
1609 rc = SQLITE_NOMEM;
1610 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001611 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1612 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001613 }
danielk197713adf8a2004-06-03 16:08:41 +00001614 if( rc!=SQLITE_OK ) goto delmaster_out;
1615 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001616
1617 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001618 if( rc!=SQLITE_OK ) goto delmaster_out;
1619
1620 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001621 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001622 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001623 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001624
1625 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001626 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001627 */
danielk197765839c62007-08-30 08:08:17 +00001628 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001629 if( !zMasterJournal ){
1630 rc = SQLITE_NOMEM;
1631 goto delmaster_out;
1632 }
danielk197765839c62007-08-30 08:08:17 +00001633 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001634 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001635 if( rc!=SQLITE_OK ) goto delmaster_out;
1636
danielk19775865e3d2004-06-14 06:03:57 +00001637 zJournal = zMasterJournal;
1638 while( (zJournal-zMasterJournal)<nMasterJournal ){
drh19db9352008-03-27 22:42:51 +00001639 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS);
1640 if( rc!=0 && rc!=1 ){
1641 rc = SQLITE_IOERR_NOMEM;
1642 goto delmaster_out;
1643 }
1644 if( rc==1 ){
danielk197713adf8a2004-06-03 16:08:41 +00001645 /* One of the journals pointed to by the master journal exists.
1646 ** Open it and check if it points at the master journal. If
1647 ** so, return without deleting the master journal file.
1648 */
drh3b7b78b2004-11-24 01:16:43 +00001649 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001650 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1651 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001652 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001653 goto delmaster_out;
1654 }
danielk197713adf8a2004-06-03 16:08:41 +00001655
danielk197765839c62007-08-30 08:08:17 +00001656 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001657 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001658 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001659 goto delmaster_out;
1660 }
danielk197776572402004-06-25 02:38:54 +00001661
danielk197765839c62007-08-30 08:08:17 +00001662 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001663 if( c ){
danielk197776572402004-06-25 02:38:54 +00001664 /* We have a match. Do not delete the master journal file. */
1665 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001666 }
1667 }
danielk19775865e3d2004-06-14 06:03:57 +00001668 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001669 }
1670 }
1671
danielk1977fee2d252007-08-18 10:59:19 +00001672 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001673
1674delmaster_out:
1675 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001676 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001677 }
1678 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001679 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001680 }
danielk1977b4b47412007-08-17 15:53:36 +00001681 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001682 return rc;
1683}
1684
drha6abd042004-06-09 17:37:22 +00001685
danielk1977e180dd92007-04-05 17:15:52 +00001686static void pager_truncate_cache(Pager *pPager);
1687
drha6abd042004-06-09 17:37:22 +00001688/*
drhcb4c40b2004-08-18 19:09:43 +00001689** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001690** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001691**
1692** Might might be the case that the file on disk is smaller than nPage.
1693** This can happen, for example, if we are in the middle of a transaction
1694** which has extended the file size and the new pages are still all held
1695** in cache, then an INSERT or UPDATE does a statement rollback. Some
1696** operating system implementations can get confused if you try to
1697** truncate a file to some size that is larger than it currently is,
1698** so detect this case and do not do the truncation.
drhcb4c40b2004-08-18 19:09:43 +00001699*/
1700static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001701 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001702 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001703 i64 currentSize, newSize;
1704 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1705 newSize = pPager->pageSize*(i64)nPage;
1706 if( rc==SQLITE_OK && currentSize>newSize ){
1707 rc = sqlite3OsTruncate(pPager->fd, newSize);
1708 }
danielk1977e180dd92007-04-05 17:15:52 +00001709 }
1710 if( rc==SQLITE_OK ){
1711 pPager->dbSize = nPage;
1712 pager_truncate_cache(pPager);
1713 }
1714 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001715}
1716
1717/*
drhc80f0582007-05-01 16:59:48 +00001718** Set the sectorSize for the given pager.
1719**
drh334c80d2008-03-28 17:41:13 +00001720** The sector size is at least as big as the sector size reported
1721** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001722*/
1723static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001724 assert(pPager->fd->pMethods||pPager->tempFile);
1725 if( !pPager->tempFile ){
1726 /* Sector size doesn't matter for temporary files. Also, the file
1727 ** may not have been opened yet, in whcih case the OsSectorSize()
1728 ** call will segfault.
1729 */
1730 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1731 }
drh334c80d2008-03-28 17:41:13 +00001732 if( pPager->sectorSize<512 ){
1733 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001734 }
1735}
1736
1737/*
drhed7c8552001-04-11 14:29:21 +00001738** Playback the journal and thus restore the database file to
1739** the state it was in before we started making changes.
1740**
drh34e79ce2004-02-08 06:05:46 +00001741** The journal file format is as follows:
1742**
drhae2b40c2004-06-09 19:03:54 +00001743** (1) 8 byte prefix. A copy of aJournalMagic[].
1744** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001745** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001746** number of page records from the journal size.
1747** (3) 4 byte big-endian integer which is the initial value for the
1748** sanity checksum.
1749** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001750** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001751** (5) 4 byte big-endian integer which is the sector size. The header
1752** is this many bytes in size.
1753** (6) 4 byte big-endian integer which is the page case.
1754** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001755** name. The value may be zero (indicate that there is no master
1756** journal.)
drh334c80d2008-03-28 17:41:13 +00001757** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001758** and might be shorter than the value read from (5). If the first byte
1759** of the name is \000 then there is no master journal. The master
1760** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001761** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001762** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001763** + pPager->pageSize bytes of data.
1764** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001765**
drh334c80d2008-03-28 17:41:13 +00001766** When we speak of the journal header, we mean the first 8 items above.
1767** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001768**
1769** Call the value from the second bullet "nRec". nRec is the number of
1770** valid page entries in the journal. In most cases, you can compute the
1771** value of nRec from the size of the journal file. But if a power
1772** failure occurred while the journal was being written, it could be the
1773** case that the size of the journal file had already been increased but
1774** the extra entries had not yet made it safely to disk. In such a case,
1775** the value of nRec computed from the file size would be too large. For
1776** that reason, we always use the nRec value in the header.
1777**
1778** If the nRec value is 0xffffffff it means that nRec should be computed
1779** from the file size. This value is used when the user selects the
1780** no-sync option for the journal. A power failure could lead to corruption
1781** in this case. But for things like temporary table (which will be
1782** deleted when the power is restored) we don't care.
1783**
drhd9b02572001-04-15 00:37:09 +00001784** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001785** journal file then all pages up to the first corrupted page are rolled
1786** back (or no pages if the journal header is corrupted). The journal file
1787** is then deleted and SQLITE_OK returned, just as if no corruption had
1788** been encountered.
1789**
1790** If an I/O or malloc() error occurs, the journal-file is not deleted
1791** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001792*/
danielk1977e277be02007-03-23 18:12:06 +00001793static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001794 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001795 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001796 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001797 int i; /* Loop counter */
1798 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001799 int rc; /* Result code of a subroutine */
danielk1977ce98bba2008-04-03 10:13:01 +00001800 int res = 0; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001801 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001802
drhc3a64ba2001-11-22 00:01:27 +00001803 /* Figure out how many records are in the journal. Abort early if
1804 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001805 */
drh8cfbf082001-09-19 13:22:39 +00001806 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001807 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001808 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001809 goto end_playback;
1810 }
drh240c5792004-02-08 00:40:52 +00001811
danielk197776572402004-06-25 02:38:54 +00001812 /* Read the master journal name from the journal, if it is present.
1813 ** If a master journal file name is specified, but the file is not
1814 ** present on disk, then the journal is not hot and does not need to be
1815 ** played back.
drh240c5792004-02-08 00:40:52 +00001816 */
danielk197765839c62007-08-30 08:08:17 +00001817 zMaster = pPager->pTmpSpace;
1818 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977ce98bba2008-04-03 10:13:01 +00001819 if( rc!=SQLITE_OK || (zMaster[0]
1820 && (res=sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))==0 )
danielk1977b4b47412007-08-17 15:53:36 +00001821 ){
danielk197776572402004-06-25 02:38:54 +00001822 zMaster = 0;
drhc3a64ba2001-11-22 00:01:27 +00001823 goto end_playback;
1824 }
danielk197765839c62007-08-30 08:08:17 +00001825 zMaster = 0;
danielk1977ce98bba2008-04-03 10:13:01 +00001826 if( res<0 ){
1827 rc = SQLITE_IOERR_NOMEM;
1828 goto end_playback;
1829 }
1830 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001831
danielk197776572402004-06-25 02:38:54 +00001832 /* This loop terminates either when the readJournalHdr() call returns
1833 ** SQLITE_DONE or an IO error occurs. */
1834 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001835
danielk197776572402004-06-25 02:38:54 +00001836 /* Read the next journal header from the journal file. If there are
1837 ** not enough bytes left in the journal file for a complete header, or
1838 ** it is corrupted, then a process must of failed while writing it.
1839 ** This indicates nothing more needs to be rolled back.
1840 */
1841 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1842 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001843 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001844 rc = SQLITE_OK;
1845 }
danielk197776572402004-06-25 02:38:54 +00001846 goto end_playback;
1847 }
1848
1849 /* If nRec is 0xffffffff, then this journal was created by a process
1850 ** working in no-sync mode. This means that the rest of the journal
1851 ** file consists of pages, there are no more journal headers. Compute
1852 ** the value of nRec based on this assumption.
1853 */
1854 if( nRec==0xffffffff ){
1855 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1856 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1857 }
1858
danielk1977e277be02007-03-23 18:12:06 +00001859 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001860 ** process and if this is the final header in the journal, then it means
1861 ** that this part of the journal was being filled but has not yet been
1862 ** synced to disk. Compute the number of pages based on the remaining
1863 ** size of the file.
1864 **
1865 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001866 */
drh8940f4e2007-08-11 00:26:20 +00001867 if( nRec==0 && !isHot &&
1868 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001869 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1870 }
1871
danielk197776572402004-06-25 02:38:54 +00001872 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001873 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001874 */
danielk1977e180dd92007-04-05 17:15:52 +00001875 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001876 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001877 if( rc!=SQLITE_OK ){
1878 goto end_playback;
1879 }
danielk197776572402004-06-25 02:38:54 +00001880 }
1881
danielk197776572402004-06-25 02:38:54 +00001882 /* Copy original pages out of the journal and back into the database file.
1883 */
1884 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001885 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001886 if( rc!=SQLITE_OK ){
1887 if( rc==SQLITE_DONE ){
1888 rc = SQLITE_OK;
1889 pPager->journalOff = szJ;
1890 break;
1891 }else{
1892 goto end_playback;
1893 }
1894 }
drh968af522003-02-11 14:55:40 +00001895 }
drhed7c8552001-04-11 14:29:21 +00001896 }
drh580eeaf2006-02-24 03:09:37 +00001897 /*NOTREACHED*/
1898 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001899
1900end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001901 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001902 zMaster = pPager->pTmpSpace;
1903 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1904 }
1905 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001906 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001907 }
danielk197765839c62007-08-30 08:08:17 +00001908 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001909 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001910 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001911 */
danielk197765839c62007-08-30 08:08:17 +00001912 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001913 }
danielk197776572402004-06-25 02:38:54 +00001914
1915 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001916 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001917 ** value. Reset it to the correct value for this process.
1918 */
drhc80f0582007-05-01 16:59:48 +00001919 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001920 return rc;
drhed7c8552001-04-11 14:29:21 +00001921}
1922
1923/*
drhac69b052004-05-12 13:30:07 +00001924** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001925**
1926** This is similar to playing back the transaction journal but with
1927** a few extra twists.
1928**
drh663fc632002-02-02 18:49:19 +00001929** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001930** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001931** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001932**
drhac69b052004-05-12 13:30:07 +00001933** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001934** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001935** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001936*/
drh3aac2dd2004-04-26 14:10:20 +00001937static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001938 i64 szJ; /* Size of the full journal */
1939 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001940 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001941 int i; /* Loop counter */
1942 int rc;
1943
danielk197776572402004-06-25 02:38:54 +00001944 szJ = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001945
danielk19774099f6e2007-03-19 11:25:20 +00001946 /* Set hdrOff to be the offset just after the end of the last journal
1947 ** page written before the first journal-header for this statement
1948 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001949 ** header was written.
1950 */
1951 hdrOff = pPager->stmtHdrOff;
1952 assert( pPager->fullSync || !hdrOff );
1953 if( !hdrOff ){
1954 hdrOff = szJ;
1955 }
1956
drhfa86c412002-02-02 15:01:15 +00001957 /* Truncate the database back to its original size.
1958 */
danielk1977e180dd92007-04-05 17:15:52 +00001959 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001960 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001961
drhac69b052004-05-12 13:30:07 +00001962 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001963 */
drhac69b052004-05-12 13:30:07 +00001964 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001965 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001966
drhac69b052004-05-12 13:30:07 +00001967 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001968 ** database file. Note that the statement journal omits checksums from
1969 ** each record since power-failure recovery is not important to statement
1970 ** journals.
drhfa86c412002-02-02 15:01:15 +00001971 */
danielk197762079062007-08-15 17:08:46 +00001972 for(i=0; i<nRec; i++){
1973 i64 offset = i*(4+pPager->pageSize);
1974 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001975 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001976 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001977 }
1978
danielk197776572402004-06-25 02:38:54 +00001979 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1980 ** was the size of the journal file when this statement was started, so
1981 ** everything after that needs to be rolled back, either into the
1982 ** database, the memory cache, or both.
1983 **
1984 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1985 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001986 */
danielk197776572402004-06-25 02:38:54 +00001987 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001988 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001989 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001990 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001991 assert( rc!=SQLITE_DONE );
1992 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1993 }
1994
1995 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001996 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001997 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001998 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001999 if( rc!=SQLITE_OK ){
2000 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00002001 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00002002 }
danielk1977f0113002006-01-24 12:09:17 +00002003 if( nJRec==0 ){
2004 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00002005 }
danielk1977f0113002006-01-24 12:09:17 +00002006 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00002007 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00002008 assert( rc!=SQLITE_DONE );
2009 if( rc!=SQLITE_OK ) goto end_stmt_playback;
2010 }
drhfa86c412002-02-02 15:01:15 +00002011 }
danielk197776572402004-06-25 02:38:54 +00002012
2013 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00002014
drh3aac2dd2004-04-26 14:10:20 +00002015end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00002016 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00002017 pPager->journalOff = szJ;
2018 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00002019 }
2020 return rc;
2021}
2022
2023/*
drhf57b14a2001-09-14 18:54:08 +00002024** Change the maximum number of in-memory pages that are allowed.
2025*/
danielk19773b8a05f2007-03-19 17:44:26 +00002026void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00002027 if( mxPage>10 ){
2028 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00002029 }else{
2030 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00002031 }
2032}
2033
2034/*
drh973b6e32003-02-12 14:09:42 +00002035** Adjust the robustness of the database to damage due to OS crashes
2036** or power failures by changing the number of syncs()s when writing
2037** the rollback journal. There are three levels:
2038**
drh054889e2005-11-30 03:20:31 +00002039** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00002040** for temporary and transient files.
2041**
2042** NORMAL The journal is synced once before writes begin on the
2043** database. This is normally adequate protection, but
2044** it is theoretically possible, though very unlikely,
2045** that an inopertune power failure could leave the journal
2046** in a state which would cause damage to the database
2047** when it is rolled back.
2048**
2049** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00002050** database (with some additional information - the nRec field
2051** of the journal header - being written in between the two
2052** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00002053** single disk sector is atomic, then this mode provides
2054** assurance that the journal will not be corrupted to the
2055** point of causing damage to the database during rollback.
2056**
2057** Numeric values associated with these states are OFF==1, NORMAL=2,
2058** and FULL=3.
2059*/
danielk197793758c82005-01-21 08:13:14 +00002060#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00002061void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00002062 pPager->noSync = level==1 || pPager->tempFile;
2063 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00002064 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00002065 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00002066}
danielk197793758c82005-01-21 08:13:14 +00002067#endif
drh973b6e32003-02-12 14:09:42 +00002068
2069/*
drhaf6df112005-06-07 02:12:30 +00002070** The following global variable is incremented whenever the library
2071** attempts to open a temporary file. This information is used for
2072** testing and analysis only.
2073*/
drh0f7eb612006-08-08 13:51:43 +00002074#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002075int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00002076#endif
drhaf6df112005-06-07 02:12:30 +00002077
2078/*
drh3f56e6e2007-03-15 01:16:47 +00002079** Open a temporary file.
2080**
2081** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00002082** other error code if we fail. The OS will automatically delete the temporary
2083** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00002084*/
danielk1977b4b47412007-08-17 15:53:36 +00002085static int sqlite3PagerOpentemp(
drh33f4e022007-09-03 15:19:34 +00002086 sqlite3_vfs *pVfs, /* The virtual file system layer */
2087 sqlite3_file *pFile, /* Write the file descriptor here */
2088 char *zFilename, /* Name of the file. Might be NULL */
2089 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00002090){
drhfa86c412002-02-02 15:01:15 +00002091 int rc;
drh334b2992007-09-06 23:28:23 +00002092 assert( zFilename!=0 );
drh3f56e6e2007-03-15 01:16:47 +00002093
drh0f7eb612006-08-08 13:51:43 +00002094#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002095 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002096#endif
danielk1977b4b47412007-08-17 15:53:36 +00002097
drh33f4e022007-09-03 15:19:34 +00002098 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2099 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
2100 rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00002101 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00002102 return rc;
2103}
2104
2105/*
drhed7c8552001-04-11 14:29:21 +00002106** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002107** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002108** the first call to sqlite3PagerGet() and is only held open until the
2109** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002110**
drh6446c4d2001-12-15 14:22:18 +00002111** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002112** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002113** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002114**
2115** If zFilename is ":memory:" then all information is held in cache.
2116** It is never written to disk. This can be used to implement an
2117** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002118*/
danielk19773b8a05f2007-03-19 17:44:26 +00002119int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002120 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002121 Pager **ppPager, /* Return the Pager structure here */
2122 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002123 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002124 int flags, /* flags controlling this file */
2125 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002126){
danielk1977b4b47412007-08-17 15:53:36 +00002127 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002128 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002129 int rc = SQLITE_OK;
2130 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002131 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002132 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002133 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002134 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2135 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002136 int journalFileSize = sqlite3JournalSize(pVfs);
drh1cc8c442007-08-24 16:08:29 +00002137 int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
2138 char *zPathname;
2139 int nPathname;
drh99b90c32008-03-17 13:50:58 +00002140 char *zStmtJrnl;
2141 int nStmtJrnl;
danielk1977b4b47412007-08-17 15:53:36 +00002142
drh86f8c192007-08-22 00:39:19 +00002143 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002144 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002145
drh1cc8c442007-08-24 16:08:29 +00002146 /* Compute the full pathname */
danielk1977adfb9b02007-09-17 07:02:56 +00002147 nPathname = pVfs->mxPathname+1;
drh99b90c32008-03-17 13:50:58 +00002148 zPathname = sqlite3_malloc(nPathname*2);
drh1cc8c442007-08-24 16:08:29 +00002149 if( zPathname==0 ){
2150 return SQLITE_NOMEM;
2151 }
2152 if( zFilename && zFilename[0] ){
2153#ifndef SQLITE_OMIT_MEMORYDB
2154 if( strcmp(zFilename,":memory:")==0 ){
2155 memDb = 1;
2156 zPathname[0] = 0;
2157 }else
2158#endif
2159 {
danielk1977adfb9b02007-09-17 07:02:56 +00002160 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002161 }
2162 }else{
danielk1977adfb9b02007-09-17 07:02:56 +00002163 rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
drhae28c012007-08-24 16:29:23 +00002164 }
2165 if( rc!=SQLITE_OK ){
2166 sqlite3_free(zPathname);
2167 return rc;
drh1cc8c442007-08-24 16:08:29 +00002168 }
2169 nPathname = strlen(zPathname);
2170
drh99b90c32008-03-17 13:50:58 +00002171 /* Put the statement journal in temporary disk space since this is
2172 ** sometimes RAM disk or other optimized storage. Unlikely the main
2173 ** main journal file, the statement journal does not need to be
2174 ** colocated with the database nor does it need to be persistent.
2175 */
2176 zStmtJrnl = &zPathname[nPathname+1];
2177 rc = sqlite3OsGetTempname(pVfs, pVfs->mxPathname+1, zStmtJrnl);
2178 if( rc!=SQLITE_OK ){
2179 sqlite3_free(zPathname);
2180 return rc;
2181 }
2182 nStmtJrnl = strlen(zStmtJrnl);
2183
danielk1977b4b47412007-08-17 15:53:36 +00002184 /* Allocate memory for the pager structure */
2185 pPager = sqlite3MallocZero(
2186 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00002187 journalFileSize + /* The journal file structure */
drh17c0ba22008-02-26 18:40:11 +00002188 pVfs->szOsFile * 3 + /* The main db and two journal files */
drh99b90c32008-03-17 13:50:58 +00002189 3*nPathname + 40 + /* zFilename, zDirectory, zJournal */
2190 nStmtJrnl /* zStmtJrnl */
danielk1977b4b47412007-08-17 15:53:36 +00002191 );
2192 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002193 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002194 return SQLITE_NOMEM;
2195 }
2196 pPtr = (u8 *)&pPager[1];
drh33f4e022007-09-03 15:19:34 +00002197 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002198 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002199 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2200 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2201 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002202 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2203 pPager->zJournal = &pPager->zDirectory[nPathname+1];
drh334b2992007-09-06 23:28:23 +00002204 pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
danielk1977b4b47412007-08-17 15:53:36 +00002205 pPager->pVfs = pVfs;
drh1cc8c442007-08-24 16:08:29 +00002206 memcpy(pPager->zFilename, zPathname, nPathname+1);
drh99b90c32008-03-17 13:50:58 +00002207 memcpy(pPager->zStmtJrnl, zStmtJrnl, nStmtJrnl+1);
drh1cc8c442007-08-24 16:08:29 +00002208 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002209
drh153c62c2007-08-24 03:51:33 +00002210 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002211 */
drhae28c012007-08-24 16:29:23 +00002212 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002213 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2214 rc = SQLITE_CANTOPEN;
2215 }else{
drh1cc8c442007-08-24 16:08:29 +00002216 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002217 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2218 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002219 readOnly = (fout&SQLITE_OPEN_READONLY);
2220
2221 /* If the file was successfully opened for read/write access,
2222 ** choose a default page size in case we have to create the
2223 ** database file. The default page size is the maximum of:
2224 **
2225 ** + SQLITE_DEFAULT_PAGE_SIZE,
2226 ** + The value returned by sqlite3OsSectorSize()
2227 ** + The largest page size that can be written atomically.
2228 */
2229 if( rc==SQLITE_OK && !readOnly ){
2230 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
2231 if( nDefaultPage<iSectorSize ){
2232 nDefaultPage = iSectorSize;
2233 }
danielk19779663b8f2007-08-24 11:52:28 +00002234#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002235 {
2236 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2237 int ii;
2238 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2239 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2240 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2241 for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2242 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002243 }
danielk1977b4b47412007-08-17 15:53:36 +00002244 }
drh1cc8c442007-08-24 16:08:29 +00002245#endif
2246 if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2247 nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2248 }
danielk19778def5ea2004-06-16 10:39:52 +00002249 }
drhac69b052004-05-12 13:30:07 +00002250 }
drh1cc8c442007-08-24 16:08:29 +00002251 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002252 /* If a temporary file is requested, it is not opened immediately.
2253 ** In this case we accept the default page size and delay actually
2254 ** opening the file until the first call to OsWrite().
2255 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002256 tempFile = 1;
2257 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002258 }
danielk1977aef0bf62005-12-30 16:28:01 +00002259
danielk1977b4b47412007-08-17 15:53:36 +00002260 if( pPager && rc==SQLITE_OK ){
drhbf8a4342008-04-29 15:38:58 +00002261 pPager->pTmpSpace = sqlite3MallocZero(nDefaultPage);
drh3e7a6092002-12-07 21:45:14 +00002262 }
danielk1977aef0bf62005-12-30 16:28:01 +00002263
drh153c62c2007-08-24 03:51:33 +00002264 /* If an error occured in either of the blocks above.
2265 ** Free the Pager structure and close the file.
2266 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002267 ** any Pager.errMask variables.
2268 */
danielk1977b4b47412007-08-17 15:53:36 +00002269 if( !pPager || !pPager->pTmpSpace ){
2270 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002271 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002272 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002273 }
danielk1977aef0bf62005-12-30 16:28:01 +00002274
drh153c62c2007-08-24 03:51:33 +00002275 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2276 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002277
danielk1977b4b47412007-08-17 15:53:36 +00002278 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002279 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002280 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002281 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002282
drh99b90c32008-03-17 13:50:58 +00002283 /* Fill in Pager.zJournal[] */
drh1cc8c442007-08-24 16:08:29 +00002284 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2285 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
danielk1977b4b47412007-08-17 15:53:36 +00002286
drh3b59a5c2006-01-15 20:28:28 +00002287 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002288 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002289 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002290 /* pPager->stmtOpen = 0; */
2291 /* pPager->stmtInUse = 0; */
2292 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002293 pPager->dbSize = memDb-1;
danielk19779663b8f2007-08-24 11:52:28 +00002294 pPager->pageSize = nDefaultPage;
drh3b59a5c2006-01-15 20:28:28 +00002295 /* pPager->stmtSize = 0; */
2296 /* pPager->stmtJSize = 0; */
2297 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002298 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002299 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002300 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002301 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002302 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002303 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002304 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2305 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2306 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2307 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002308 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002309 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002310 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002311 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002312 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002313 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002314 /* pPager->pFirst = 0; */
2315 /* pPager->pFirstSynced = 0; */
2316 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002317 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00002318 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002319 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002320 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002321 }
drh3b59a5c2006-01-15 20:28:28 +00002322 /* pPager->pBusyHandler = 0; */
2323 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002324 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002325#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002326 pPager->iInUseMM = 0;
2327 pPager->iInUseDB = 0;
2328 if( !memDb ){
drh26e4a8b2008-05-01 17:16:52 +00002329#ifndef SQLITE_MUTEX_NOOP
drh86f8c192007-08-22 00:39:19 +00002330 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00002331#endif
drh86f8c192007-08-22 00:39:19 +00002332 sqlite3_mutex_enter(mutex);
2333 pPager->pNext = sqlite3PagerList;
2334 if( sqlite3PagerList ){
2335 assert( sqlite3PagerList->pPrev==0 );
2336 sqlite3PagerList->pPrev = pPager;
2337 }
2338 pPager->pPrev = 0;
2339 sqlite3PagerList = pPager;
2340 sqlite3_mutex_leave(mutex);
2341 }
danielk197752622822006-01-09 09:59:49 +00002342#endif
drhed7c8552001-04-11 14:29:21 +00002343 return SQLITE_OK;
2344}
2345
2346/*
drh90f5ecb2004-07-22 01:19:35 +00002347** Set the busy handler function.
2348*/
danielk19773b8a05f2007-03-19 17:44:26 +00002349void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002350 pPager->pBusyHandler = pBusyHandler;
2351}
2352
2353/*
drh72f82862001-05-24 21:06:34 +00002354** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002355** when the reference count on each page reaches zero. The destructor can
2356** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002357**
danielk19773b8a05f2007-03-19 17:44:26 +00002358** The destructor is not called as a result sqlite3PagerClose().
2359** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002360*/
danielk19773b8a05f2007-03-19 17:44:26 +00002361void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002362 pPager->xDestructor = xDesc;
2363}
2364
2365/*
drha6abd042004-06-09 17:37:22 +00002366** Set the reinitializer for this pager. If not NULL, the reinitializer
2367** is called when the content of a page in cache is restored to its original
2368** value as a result of a rollback. The callback gives higher-level code
2369** an opportunity to restore the EXTRA section to agree with the restored
2370** page data.
2371*/
danielk19773b8a05f2007-03-19 17:44:26 +00002372void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002373 pPager->xReiniter = xReinit;
2374}
2375
2376/*
danielk1977a1644fd2007-08-29 12:31:25 +00002377** Set the page size to *pPageSize. If the suggest new page size is
2378** inappropriate, then an alternative page size is set to that
2379** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002380*/
danielk1977a1644fd2007-08-29 12:31:25 +00002381int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2382 int rc = SQLITE_OK;
2383 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002384 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002385 if( pageSize && pageSize!=pPager->pageSize
2386 && !pPager->memDb && pPager->nRef==0
2387 ){
2388 char *pNew = (char *)sqlite3_malloc(pageSize);
2389 if( !pNew ){
2390 rc = SQLITE_NOMEM;
2391 }else{
2392 pagerEnter(pPager);
2393 pager_reset(pPager);
2394 pPager->pageSize = pageSize;
2395 setSectorSize(pPager);
2396 sqlite3_free(pPager->pTmpSpace);
2397 pPager->pTmpSpace = pNew;
2398 pagerLeave(pPager);
2399 }
drh1c7880e2005-05-20 20:01:55 +00002400 }
danielk1977a1644fd2007-08-29 12:31:25 +00002401 *pPageSize = pPager->pageSize;
2402 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002403}
2404
2405/*
drh26b79942007-11-28 16:19:56 +00002406** Return a pointer to the "temporary page" buffer held internally
2407** by the pager. This is a buffer that is big enough to hold the
2408** entire content of a database page. This buffer is used internally
2409** during rollback and will be overwritten whenever a rollback
2410** occurs. But other modules are free to use it too, as long as
2411** no rollbacks are happening.
2412*/
2413void *sqlite3PagerTempSpace(Pager *pPager){
2414 return pPager->pTmpSpace;
2415}
2416
2417/*
drhf8e632b2007-05-08 14:51:36 +00002418** Attempt to set the maximum database page count if mxPage is positive.
2419** Make no changes if mxPage is zero or negative. And never reduce the
2420** maximum page count below the current size of the database.
2421**
2422** Regardless of mxPage, return the current maximum page count.
2423*/
2424int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2425 if( mxPage>0 ){
2426 pPager->mxPgno = mxPage;
2427 }
2428 sqlite3PagerPagecount(pPager);
2429 return pPager->mxPgno;
2430}
2431
2432/*
drhc9ac5ca2005-11-04 22:03:30 +00002433** The following set of routines are used to disable the simulated
2434** I/O error mechanism. These routines are used to avoid simulated
2435** errors in places where we do not care about errors.
2436**
2437** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2438** and generate no code.
2439*/
2440#ifdef SQLITE_TEST
2441extern int sqlite3_io_error_pending;
2442extern int sqlite3_io_error_hit;
2443static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002444void disable_simulated_io_errors(void){
2445 saved_cnt = sqlite3_io_error_pending;
2446 sqlite3_io_error_pending = -1;
2447}
2448void enable_simulated_io_errors(void){
2449 sqlite3_io_error_pending = saved_cnt;
2450}
2451#else
drh152410f2005-11-05 15:11:22 +00002452# define disable_simulated_io_errors()
2453# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002454#endif
2455
2456/*
drh90f5ecb2004-07-22 01:19:35 +00002457** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002458** that pDest points to.
2459**
2460** No error checking is done. The rational for this is that this function
2461** may be called even if the file does not exist or contain a header. In
2462** these cases sqlite3OsRead() will return an error, to which the correct
2463** response is to zero the memory at pDest and continue. A real IO error
2464** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002465*/
danielk19773b8a05f2007-03-19 17:44:26 +00002466int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002467 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002468 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002469 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2470 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002471 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002472 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002473 if( rc==SQLITE_IOERR_SHORT_READ ){
2474 rc = SQLITE_OK;
2475 }
drh90f5ecb2004-07-22 01:19:35 +00002476 }
drh551b7732006-11-06 21:20:25 +00002477 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002478}
2479
2480/*
drh5e00f6c2001-09-13 13:46:56 +00002481** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002482** pPager.
2483**
2484** If the PENDING_BYTE lies on the page directly after the end of the
2485** file, then consider this page part of the file too. For example, if
2486** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2487** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002488*/
danielk19773b8a05f2007-03-19 17:44:26 +00002489int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002490 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002491 int rc;
drhd9b02572001-04-15 00:37:09 +00002492 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002493 if( pPager->errCode ){
danielk197743468d92008-02-26 06:05:31 +00002494 return -1;
drha7aea3d2007-03-15 12:51:16 +00002495 }
drhed7c8552001-04-11 14:29:21 +00002496 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002497 n = pPager->dbSize;
2498 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002499 assert(pPager->fd->pMethods||pPager->tempFile);
2500 if( (pPager->fd->pMethods)
2501 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002502 pPager->nRef++;
drhe49f9822006-09-15 12:29:16 +00002503 pager_error(pPager, rc);
danielk1977ae72d982007-10-03 08:46:44 +00002504 pPager->nRef--;
danielk197743468d92008-02-26 06:05:31 +00002505 return -1;
danielk197715f411d2005-09-16 10:18:45 +00002506 }
2507 if( n>0 && n<pPager->pageSize ){
2508 n = 1;
2509 }else{
2510 n /= pPager->pageSize;
2511 }
2512 if( pPager->state!=PAGER_UNLOCK ){
2513 pPager->dbSize = n;
2514 }
drhed7c8552001-04-11 14:29:21 +00002515 }
danielk197715f411d2005-09-16 10:18:45 +00002516 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002517 n++;
2518 }
drhf8e632b2007-05-08 14:51:36 +00002519 if( n>pPager->mxPgno ){
2520 pPager->mxPgno = n;
2521 }
drhed7c8552001-04-11 14:29:21 +00002522 return n;
2523}
2524
drhf07e7d52006-04-07 13:54:46 +00002525
2526#ifndef SQLITE_OMIT_MEMORYDB
2527/*
2528** Clear a PgHistory block
2529*/
2530static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002531 sqlite3_free(pHist->pOrig);
2532 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002533 pHist->pOrig = 0;
2534 pHist->pStmt = 0;
2535}
2536#else
2537#define clearHistory(x)
2538#endif
2539
drhed7c8552001-04-11 14:29:21 +00002540/*
drhf7c57532003-04-25 13:22:51 +00002541** Forward declaration
2542*/
danielk197776572402004-06-25 02:38:54 +00002543static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002544
2545/*
drh85b623f2007-12-13 21:54:09 +00002546** Unlink pPg from its hash chain. Also set the page number to 0 to indicate
danielk1977f5fdda82004-11-03 08:44:05 +00002547** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002548** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002549** pNextFree/pPrevFree list that is not a part of any hash-chain.
2550*/
2551static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2552 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002553 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002554 return;
2555 }
2556 if( pPg->pNextHash ){
2557 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2558 }
2559 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002560 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002561 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2562 }else{
drh8ca0c722006-05-07 17:49:38 +00002563 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002564 pPager->aHash[h] = pPg->pNextHash;
2565 }
drh5229ae42006-03-23 23:29:04 +00002566 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002567 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2568 }
danielk1977f5fdda82004-11-03 08:44:05 +00002569 pPg->pgno = 0;
2570 pPg->pNextHash = pPg->pPrevHash = 0;
2571}
2572
2573/*
drhac69b052004-05-12 13:30:07 +00002574** Unlink a page from the free list (the list of all pages where nRef==0)
2575** and from its hash collision chain.
2576*/
2577static void unlinkPage(PgHdr *pPg){
2578 Pager *pPager = pPg->pPager;
2579
danielk19779f61c2f2007-08-27 17:27:49 +00002580 /* Unlink from free page list */
2581 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002582
2583 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002584 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002585}
2586
2587/*
danielk1977e180dd92007-04-05 17:15:52 +00002588** This routine is used to truncate the cache when a database
2589** is truncated. Drop from the cache all pages whose pgno is
2590** larger than pPager->dbSize and is unreferenced.
2591**
drhac69b052004-05-12 13:30:07 +00002592** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002593**
2594** Actually, at the point this routine is called, it would be
2595** an error to have a referenced page. But rather than delete
2596** that page and guarantee a subsequent segfault, it seems better
2597** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002598*/
danielk1977e180dd92007-04-05 17:15:52 +00002599static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002600 PgHdr *pPg;
2601 PgHdr **ppPg;
2602 int dbSize = pPager->dbSize;
2603
2604 ppPg = &pPager->pAll;
2605 while( (pPg = *ppPg)!=0 ){
2606 if( pPg->pgno<=dbSize ){
2607 ppPg = &pPg->pNextAll;
2608 }else if( pPg->nRef>0 ){
2609 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2610 ppPg = &pPg->pNextAll;
2611 }else{
2612 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002613 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2614 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002615 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002616 makeClean(pPg);
drh0d180202008-02-14 23:26:56 +00002617 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00002618 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002619 pPager->nPage--;
2620 }
2621 }
2622}
2623
drhf7c57532003-04-25 13:22:51 +00002624/*
danielk197717221812005-02-15 03:38:05 +00002625** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002626** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002627** false or until the lock succeeds.
2628**
2629** Return SQLITE_OK on success and an error code if we cannot obtain
2630** the lock.
2631*/
2632static int pager_wait_on_lock(Pager *pPager, int locktype){
2633 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002634
2635 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002636 assert( PAGER_SHARED==SHARED_LOCK );
2637 assert( PAGER_RESERVED==RESERVED_LOCK );
2638 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002639
2640 /* If the file is currently unlocked then the size must be unknown */
2641 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2642
danielk197717221812005-02-15 03:38:05 +00002643 if( pPager->state>=locktype ){
2644 rc = SQLITE_OK;
2645 }else{
drhbefdf832008-03-14 19:33:05 +00002646 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002647 do {
drh054889e2005-11-30 03:20:31 +00002648 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002649 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002650 if( rc==SQLITE_OK ){
2651 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002652 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002653 }
2654 }
2655 return rc;
2656}
2657
2658/*
drhf7c57532003-04-25 13:22:51 +00002659** Truncate the file to the number of pages specified.
2660*/
danielk19773b8a05f2007-03-19 17:44:26 +00002661int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002662 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002663 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002664 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002665 if( pPager->errCode ){
2666 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002667 return rc;
2668 }
drh7d02cb72003-06-04 16:24:39 +00002669 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002670 return SQLITE_OK;
2671 }
drhb7f91642004-10-31 02:22:47 +00002672 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002673 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002674 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002675 return SQLITE_OK;
2676 }
drh86f8c192007-08-22 00:39:19 +00002677 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002678 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002679 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002680 if( rc!=SQLITE_OK ){
2681 return rc;
2682 }
danielk197717221812005-02-15 03:38:05 +00002683
2684 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002685 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002686 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002687 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002688 if( rc!=SQLITE_OK ){
2689 return rc;
2690 }
2691
drhcb4c40b2004-08-18 19:09:43 +00002692 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002693 return rc;
2694}
2695
2696/*
drhed7c8552001-04-11 14:29:21 +00002697** Shutdown the page cache. Free all memory and close all files.
2698**
2699** If a transaction was in progress when this routine is called, that
2700** transaction is rolled back. All outstanding pages are invalidated
2701** and their memory is freed. Any attempt to use a page associated
2702** with this page cache after this function returns will likely
2703** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002704**
2705** This function always succeeds. If a transaction is active an attempt
2706** is made to roll it back. If an error occurs during the rollback
2707** a hot journal may be left in the filesystem but no error is returned
2708** to the caller.
drhed7c8552001-04-11 14:29:21 +00002709*/
danielk19773b8a05f2007-03-19 17:44:26 +00002710int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002711#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002712 if( !MEMDB ){
drh26e4a8b2008-05-01 17:16:52 +00002713#ifndef SQLITE_MUTEX_NOOP
drh86f8c192007-08-22 00:39:19 +00002714 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00002715#endif
drh86f8c192007-08-22 00:39:19 +00002716 sqlite3_mutex_enter(mutex);
2717 if( pPager->pPrev ){
2718 pPager->pPrev->pNext = pPager->pNext;
2719 }else{
2720 sqlite3PagerList = pPager->pNext;
2721 }
2722 if( pPager->pNext ){
2723 pPager->pNext->pPrev = pPager->pPrev;
2724 }
2725 sqlite3_mutex_leave(mutex);
2726 }
danielk197713f72992005-12-18 08:51:22 +00002727#endif
2728
drhbafda092007-01-03 23:36:22 +00002729 disable_simulated_io_errors();
drh16e45a42008-04-19 20:34:18 +00002730 sqlite3FaultBenign(-1, 1);
drhc2ee76c2007-01-04 14:58:14 +00002731 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002732 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002733 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002734 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002735 enable_simulated_io_errors();
drh16e45a42008-04-19 20:34:18 +00002736 sqlite3FaultBenign(-1, 0);
drh4f0c5872007-03-26 22:05:01 +00002737 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002738 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002739 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002740 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002741 }
drhf5e7bb52008-02-18 14:47:33 +00002742 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002743 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002744 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002745 }
danielk1977b4b47412007-08-17 15:53:36 +00002746 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002747 /* Temp files are automatically deleted by the OS
2748 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002749 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002750 ** }
2751 */
danielk1977aca790a2005-01-13 11:07:52 +00002752
drh17435752007-08-16 04:30:38 +00002753 sqlite3_free(pPager->aHash);
2754 sqlite3_free(pPager->pTmpSpace);
2755 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002756 return SQLITE_OK;
2757}
2758
drh87cc3b32007-05-08 21:45:27 +00002759#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002760/*
drh5e00f6c2001-09-13 13:46:56 +00002761** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002762*/
danielk19773b8a05f2007-03-19 17:44:26 +00002763Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002764 return p->pgno;
2765}
drh87cc3b32007-05-08 21:45:27 +00002766#endif
drhed7c8552001-04-11 14:29:21 +00002767
2768/*
drhc8629a12004-05-08 20:07:40 +00002769** The page_ref() function increments the reference count for a page.
2770** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002771** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002772**
2773** For non-test systems, page_ref() is a macro that calls _page_ref()
2774** online of the reference count is zero. For test systems, page_ref()
2775** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002776*/
drh836faa42003-01-11 13:30:57 +00002777static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002778 if( pPg->nRef==0 ){
2779 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002780 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002781 pPg->pPager->nRef++;
2782 }
2783 pPg->nRef++;
drhdf0b3b02001-06-23 11:36:20 +00002784}
danielk1977aca790a2005-01-13 11:07:52 +00002785#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002786 static void page_ref(PgHdr *pPg){
2787 if( pPg->nRef==0 ){
2788 _page_ref(pPg);
2789 }else{
2790 pPg->nRef++;
drhc8629a12004-05-08 20:07:40 +00002791 }
2792 }
2793#else
2794# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2795#endif
drhdf0b3b02001-06-23 11:36:20 +00002796
2797/*
2798** Increment the reference count for a page. The input pointer is
2799** a reference to the page data.
2800*/
danielk19773b8a05f2007-03-19 17:44:26 +00002801int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002802 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002803 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002804 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002805 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002806}
2807
2808/*
drh34e79ce2004-02-08 06:05:46 +00002809** Sync the journal. In other words, make sure all the pages that have
2810** been written to the journal have actually reached the surface of the
2811** disk. It is not safe to modify the original database file until after
2812** the journal has been synced. If the original database is modified before
2813** the journal is synced and a power failure occurs, the unsynced journal
2814** data would be lost and we would be unable to completely rollback the
2815** database changes. Database corruption would occur.
2816**
2817** This routine also updates the nRec field in the header of the journal.
2818** (See comments on the pager_playback() routine for additional information.)
2819** If the sync mode is FULL, two syncs will occur. First the whole journal
2820** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002821**
drh34e79ce2004-02-08 06:05:46 +00002822** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002823** after a power failure, so no sync occurs.
2824**
2825** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2826** the database is stored, then OsSync() is never called on the journal
2827** file. In this case all that is required is to update the nRec field in
2828** the journal header.
drhfa86c412002-02-02 15:01:15 +00002829**
drh34e79ce2004-02-08 06:05:46 +00002830** This routine clears the needSync field of every page current held in
2831** memory.
drh50e5dad2001-09-15 00:57:28 +00002832*/
danielk197776572402004-06-25 02:38:54 +00002833static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002834 PgHdr *pPg;
2835 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002836
danielk19774cd2cd52007-08-23 14:48:23 +00002837
drh03eb96a2002-11-10 23:32:56 +00002838 /* Sync the journal before modifying the main database
2839 ** (assuming there is a journal and it needs to be synced.)
2840 */
danielk197776572402004-06-25 02:38:54 +00002841 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002842 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002843 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002844 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002845
danielk19774cd2cd52007-08-23 14:48:23 +00002846 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002847 /* Write the nRec value into the journal file header. If in
2848 ** full-synchronous mode, sync the journal first. This ensures that
2849 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002850 ** it as a candidate for rollback.
2851 **
2852 ** This is not required if the persistent media supports the
2853 ** SAFE_APPEND property. Because in this case it is not possible
2854 ** for garbage data to be appended to the file, the nRec field
2855 ** is populated with 0xFFFFFFFF when the journal header is written
2856 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002857 */
danielk19774cd2cd52007-08-23 14:48:23 +00002858 i64 jrnlOff;
2859 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002860 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002861 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002862 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002863 if( rc!=0 ) return rc;
2864 }
danielk197713adf8a2004-06-03 16:08:41 +00002865
danielk197762079062007-08-15 17:08:46 +00002866 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2867 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2868 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002869 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002870 }
danielk19774cd2cd52007-08-23 14:48:23 +00002871 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2872 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2873 IOTRACE(("JSYNC %p\n", pPager))
2874 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2875 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2876 );
2877 if( rc!=0 ) return rc;
2878 }
drhdb48ee02003-01-16 13:42:43 +00002879 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002880 }
drh50e5dad2001-09-15 00:57:28 +00002881 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002882
2883 /* Erase the needSync flag from every page.
2884 */
2885 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2886 pPg->needSync = 0;
2887 }
danielk19779f61c2f2007-08-27 17:27:49 +00002888 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002889 }
drh03eb96a2002-11-10 23:32:56 +00002890
drh341eae82003-01-21 02:39:36 +00002891#ifndef NDEBUG
2892 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2893 ** flag must also be clear for all pages. Verify that this
2894 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002895 */
drh341eae82003-01-21 02:39:36 +00002896 else{
2897 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2898 assert( pPg->needSync==0 );
2899 }
danielk19779f61c2f2007-08-27 17:27:49 +00002900 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002901 }
drh341eae82003-01-21 02:39:36 +00002902#endif
drhdb48ee02003-01-16 13:42:43 +00002903
drh81a20f22001-10-12 17:30:04 +00002904 return rc;
drh50e5dad2001-09-15 00:57:28 +00002905}
2906
2907/*
drhdc3e10b2006-06-15 14:31:06 +00002908** Merge two lists of pages connected by pDirty and in pgno order.
2909** Do not both fixing the pPrevDirty pointers.
2910*/
2911static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2912 PgHdr result, *pTail;
2913 pTail = &result;
2914 while( pA && pB ){
2915 if( pA->pgno<pB->pgno ){
2916 pTail->pDirty = pA;
2917 pTail = pA;
2918 pA = pA->pDirty;
2919 }else{
2920 pTail->pDirty = pB;
2921 pTail = pB;
2922 pB = pB->pDirty;
2923 }
2924 }
2925 if( pA ){
2926 pTail->pDirty = pA;
2927 }else if( pB ){
2928 pTail->pDirty = pB;
2929 }else{
2930 pTail->pDirty = 0;
2931 }
2932 return result.pDirty;
2933}
2934
2935/*
2936** Sort the list of pages in accending order by pgno. Pages are
2937** connected by pDirty pointers. The pPrevDirty pointers are
2938** corrupted by this sort.
2939*/
danielk197724168722007-04-02 05:07:47 +00002940#define N_SORT_BUCKET_ALLOC 25
2941#define N_SORT_BUCKET 25
2942#ifdef SQLITE_TEST
2943 int sqlite3_pager_n_sort_bucket = 0;
2944 #undef N_SORT_BUCKET
2945 #define N_SORT_BUCKET \
2946 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2947#endif
drhdc3e10b2006-06-15 14:31:06 +00002948static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002949 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002950 int i;
2951 memset(a, 0, sizeof(a));
2952 while( pIn ){
2953 p = pIn;
2954 pIn = p->pDirty;
2955 p->pDirty = 0;
2956 for(i=0; i<N_SORT_BUCKET-1; i++){
2957 if( a[i]==0 ){
2958 a[i] = p;
2959 break;
2960 }else{
2961 p = merge_pagelist(a[i], p);
2962 a[i] = 0;
2963 }
2964 }
2965 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002966 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2967 ** elements in the input list. This is possible, but impractical.
2968 ** Testing this line is the point of global variable
2969 ** sqlite3_pager_n_sort_bucket.
2970 */
drhdc3e10b2006-06-15 14:31:06 +00002971 a[i] = merge_pagelist(a[i], p);
2972 }
2973 }
2974 p = a[0];
2975 for(i=1; i<N_SORT_BUCKET; i++){
2976 p = merge_pagelist(p, a[i]);
2977 }
2978 return p;
2979}
2980
2981/*
drh2554f8b2003-01-22 01:26:44 +00002982** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2983** every one of those pages out to the database file and mark them all
2984** as clean.
2985*/
2986static int pager_write_pagelist(PgHdr *pList){
2987 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00002988 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00002989 int rc;
2990
2991 if( pList==0 ) return SQLITE_OK;
2992 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002993
2994 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2995 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002996 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002997 **
drha6abd042004-06-09 17:37:22 +00002998 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2999 ** through an intermediate state PENDING. A PENDING lock prevents new
3000 ** readers from attaching to the database but is unsufficient for us to
3001 ** write. The idea of a PENDING lock is to prevent new readers from
3002 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00003003 **
drha6abd042004-06-09 17:37:22 +00003004 ** While the pager is in the RESERVED state, the original database file
3005 ** is unchanged and we can rollback without having to playback the
3006 ** journal into the original database file. Once we transition to
3007 ** EXCLUSIVE, it means the database file has been changed and any rollback
3008 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00003009 */
drh684917c2004-10-05 02:41:42 +00003010 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00003011 if( rc!=SQLITE_OK ){
3012 return rc;
3013 }
3014
drhdc3e10b2006-06-15 14:31:06 +00003015 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00003016 for(p=pList; p; p=p->pDirty){
3017 assert( p->dirty );
3018 p->dirty = 0;
3019 }
drh2554f8b2003-01-22 01:26:44 +00003020 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00003021
3022 /* If the file has not yet been opened, open it now. */
3023 if( !pPager->fd->pMethods ){
3024 assert(pPager->tempFile);
drh33f4e022007-09-03 15:19:34 +00003025 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
3026 pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00003027 if( rc ) return rc;
3028 }
3029
danielk1977687566d2004-11-02 12:56:41 +00003030 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00003031 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00003032 ** make the file smaller (presumably by auto-vacuum code). Do not write
3033 ** any such pages to the file.
3034 */
3035 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00003036 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00003037 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00003038 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
3039 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00003040 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00003041 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003042 PAGER_INCR(sqlite3_pager_writedb_count);
3043 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00003044 if( pList->pgno==1 ){
3045 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
3046 }
danielk1977687566d2004-11-02 12:56:41 +00003047 }
3048#ifndef NDEBUG
3049 else{
drh4f0c5872007-03-26 22:05:01 +00003050 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00003051 }
3052#endif
drh2554f8b2003-01-22 01:26:44 +00003053 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00003054#ifdef SQLITE_CHECK_PAGES
3055 pList->pageHash = pager_pagehash(pList);
3056#endif
drh2554f8b2003-01-22 01:26:44 +00003057 pList = pList->pDirty;
3058 }
3059 return SQLITE_OK;
3060}
3061
3062/*
3063** Collect every dirty page into a dirty list and
3064** return a pointer to the head of that list. All pages are
3065** collected even if they are still in use.
3066*/
3067static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh8f2e9a12008-02-26 14:46:04 +00003068
3069#ifndef NDEBUG
3070 /* Verify the sanity of the dirty list when we are running
3071 ** in debugging mode. This is expensive, so do not
3072 ** do this on a normal build. */
3073 int n1 = 0;
3074 int n2 = 0;
3075 PgHdr *p;
3076 for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; }
3077 for(p=pPager->pDirty; p; p=p->pDirty){ n2++; }
3078 assert( n1==n2 );
3079#endif
3080
drh605ad8f2006-05-03 23:34:05 +00003081 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00003082}
3083
3084/*
drh19db9352008-03-27 22:42:51 +00003085** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00003086** A hot journal is one that needs to be played back.
3087**
3088** If the current size of the database file is 0 but a journal file
3089** exists, that is probably an old journal left over from a prior
3090** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00003091**
3092** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00003093**
3094** This routine does not open the journal file to examine its
3095** content. Hence, the journal might contain the name of a master
3096** journal file that has been deleted, and hence not be hot. Or
3097** the header of the journal might be zeroed out. This routine
3098** does not discover these cases of a non-hot journal - if the
3099** journal file exists and is not empty this routine assumes it
3100** is hot. The pager_playback() routine will discover that the
3101** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00003102*/
3103static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003104 sqlite3_vfs *pVfs = pPager->pVfs;
drh19db9352008-03-27 22:42:51 +00003105 int rc;
drh165ffe92005-03-15 17:09:30 +00003106 if( !pPager->useJournal ) return 0;
drhd3cf2472007-11-27 16:55:07 +00003107 if( !pPager->fd->pMethods ) return 0;
drh19db9352008-03-27 22:42:51 +00003108 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS);
3109 if( rc<=0 ){
3110 return rc;
drhbb5f18d2007-04-06 18:23:17 +00003111 }
3112 if( sqlite3OsCheckReservedLock(pPager->fd) ){
3113 return 0;
3114 }
danielk19773b8a05f2007-03-19 17:44:26 +00003115 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003116 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00003117 return 0;
3118 }else{
3119 return 1;
3120 }
3121}
3122
danielk19775591df52005-12-20 09:19:37 +00003123/*
danielk1977aef0bf62005-12-30 16:28:01 +00003124** Try to find a page in the cache that can be recycled.
3125**
3126** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00003127** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00003128*/
danielk1977843e65f2007-09-01 16:16:15 +00003129static int pager_recycle(Pager *pPager, PgHdr **ppPg){
danielk197713f72992005-12-18 08:51:22 +00003130 PgHdr *pPg;
3131 *ppPg = 0;
3132
danielk1977843e65f2007-09-01 16:16:15 +00003133 /* It is illegal to call this function unless the pager object
3134 ** pointed to by pPager has at least one free page (page with nRef==0).
3135 */
danielk1977c551edc2007-04-05 13:12:13 +00003136 assert(!MEMDB);
danielk1977843e65f2007-09-01 16:16:15 +00003137 assert(pPager->lru.pFirst);
danielk1977c551edc2007-04-05 13:12:13 +00003138
danielk197713f72992005-12-18 08:51:22 +00003139 /* Find a page to recycle. Try to locate a page that does not
3140 ** require us to do an fsync() on the journal.
3141 */
danielk19779f61c2f2007-08-27 17:27:49 +00003142 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00003143
3144 /* If we could not find a page that does not require an fsync()
3145 ** on the journal file then fsync the journal file. This is a
3146 ** very slow operation, so we work hard to avoid it. But sometimes
3147 ** it can't be helped.
3148 */
danielk1977843e65f2007-09-01 16:16:15 +00003149 if( pPg==0 && pPager->lru.pFirst){
danielk19774cd2cd52007-08-23 14:48:23 +00003150 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk197713f72992005-12-18 08:51:22 +00003151 int rc = syncJournal(pPager);
3152 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003153 return rc;
danielk197713f72992005-12-18 08:51:22 +00003154 }
danielk19774cd2cd52007-08-23 14:48:23 +00003155 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197713f72992005-12-18 08:51:22 +00003156 /* If in full-sync mode, write a new journal header into the
3157 ** journal file. This is done to avoid ever modifying a journal
3158 ** header that is involved in the rollback of pages that have
3159 ** already been written to the database (in case the header is
3160 ** trashed when the nRec field is updated).
3161 */
3162 pPager->nRec = 0;
3163 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00003164 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00003165 rc = writeJournalHdr(pPager);
3166 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003167 return rc;
danielk197713f72992005-12-18 08:51:22 +00003168 }
3169 }
danielk19779f61c2f2007-08-27 17:27:49 +00003170 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003171 }
danielk197713f72992005-12-18 08:51:22 +00003172
3173 assert( pPg->nRef==0 );
3174
3175 /* Write the page to the database file if it is dirty.
3176 */
3177 if( pPg->dirty ){
3178 int rc;
3179 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003180 makeClean(pPg);
3181 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003182 pPg->pDirty = 0;
3183 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003184 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003185 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003186 return rc;
danielk197713f72992005-12-18 08:51:22 +00003187 }
3188 }
3189 assert( pPg->dirty==0 );
3190
3191 /* If the page we are recycling is marked as alwaysRollback, then
3192 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003193 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003194 ** It is necessary to do this because the page marked alwaysRollback
3195 ** might be reloaded at a later time but at that point we won't remember
3196 ** that is was marked alwaysRollback. This means that all pages must
3197 ** be marked as alwaysRollback from here on out.
3198 */
3199 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003200 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003201 pPager->alwaysRollback = 1;
3202 }
3203
3204 /* Unlink the old page from the free list and the hash table
3205 */
3206 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003207 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003208
3209 *ppPg = pPg;
3210 return SQLITE_OK;
3211}
3212
drh86f8c192007-08-22 00:39:19 +00003213#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003214/*
3215** This function is called to free superfluous dynamically allocated memory
3216** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003217** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003218**
3219** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003220** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003221** of bytes of memory released.
3222*/
danielk19773b8a05f2007-03-19 17:44:26 +00003223int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003224 int nReleased = 0; /* Bytes of memory released so far */
3225 sqlite3_mutex *mutex; /* The MEM2 mutex */
3226 Pager *pPager; /* For looping over pagers */
drhe5fe6902007-12-07 18:55:28 +00003227 BusyHandler *savedBusy; /* Saved copy of the busy handler */
danielk19779f61c2f2007-08-27 17:27:49 +00003228 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003229
drh86f8c192007-08-22 00:39:19 +00003230 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003231 */
drh26e4a8b2008-05-01 17:16:52 +00003232#ifndef SQLITE_MUTEX_NOOP
drh86f8c192007-08-22 00:39:19 +00003233 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00003234#endif
drh86f8c192007-08-22 00:39:19 +00003235 sqlite3_mutex_enter(mutex);
3236
3237 /* Signal all database connections that memory management wants
3238 ** to have access to the pagers.
3239 */
3240 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3241 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003242 }
3243
danielk19779f61c2f2007-08-27 17:27:49 +00003244 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3245 PgHdr *pPg;
3246 PgHdr *pRecycled;
3247
3248 /* Try to find a page to recycle that does not require a sync(). If
3249 ** this is not possible, find one that does require a sync().
3250 */
3251 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3252 pPg = sqlite3LruPageList.pFirstSynced;
3253 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3254 pPg = pPg->gfree.pNext;
3255 }
3256 if( !pPg ){
3257 pPg = sqlite3LruPageList.pFirst;
3258 while( pPg && pPg->pPager->iInUseDB ){
3259 pPg = pPg->gfree.pNext;
3260 }
3261 }
3262 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003263
danielk197784f786f2007-08-28 08:00:17 +00003264 /* If pPg==0, then the block above has failed to find a page to
3265 ** recycle. In this case return early - no further memory will
3266 ** be released.
3267 */
danielk19779f61c2f2007-08-27 17:27:49 +00003268 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003269
danielk19779f61c2f2007-08-27 17:27:49 +00003270 pPager = pPg->pPager;
3271 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3272 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3273
drhe5fe6902007-12-07 18:55:28 +00003274 savedBusy = pPager->pBusyHandler;
3275 pPager->pBusyHandler = 0;
danielk1977843e65f2007-09-01 16:16:15 +00003276 rc = pager_recycle(pPager, &pRecycled);
drhe5fe6902007-12-07 18:55:28 +00003277 pPager->pBusyHandler = savedBusy;
danielk19779f61c2f2007-08-27 17:27:49 +00003278 assert(pRecycled==pPg || rc!=SQLITE_OK);
3279 if( rc==SQLITE_OK ){
3280 /* We've found a page to free. At this point the page has been
3281 ** removed from the page hash-table, free-list and synced-list
3282 ** (pFirstSynced). It is still in the all pages (pAll) list.
3283 ** Remove it from this list before freeing.
3284 **
3285 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3286 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003287 */
danielk19779f61c2f2007-08-27 17:27:49 +00003288 PgHdr *pTmp;
3289 assert( pPg );
3290 if( pPg==pPager->pAll ){
3291 pPager->pAll = pPg->pNextAll;
3292 }else{
3293 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3294 pTmp->pNextAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00003295 }
danielk19779f61c2f2007-08-27 17:27:49 +00003296 nReleased += (
3297 sizeof(*pPg) + pPager->pageSize
3298 + sizeof(u32) + pPager->nExtra
3299 + MEMDB*sizeof(PgHistory)
3300 );
3301 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3302 PAGER_INCR(sqlite3_pager_pgfree_count);
drh0d180202008-02-14 23:26:56 +00003303 sqlite3_free(pPg->pData);
danielk19779f61c2f2007-08-27 17:27:49 +00003304 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003305 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003306 }else{
3307 /* An error occured whilst writing to the database file or
3308 ** journal in pager_recycle(). The error is not returned to the
3309 ** caller of this function. Instead, set the Pager.errCode variable.
3310 ** The error will be returned to the user (or users, in the case
3311 ** of a shared pager cache) of the pager for which the error occured.
3312 */
3313 assert(
3314 (rc&0xff)==SQLITE_IOERR ||
3315 rc==SQLITE_FULL ||
3316 rc==SQLITE_BUSY
3317 );
3318 assert( pPager->state>=PAGER_RESERVED );
3319 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003320 }
3321 }
danielk1977aef0bf62005-12-30 16:28:01 +00003322
drh86f8c192007-08-22 00:39:19 +00003323 /* Clear the memory management flags and release the mutex
3324 */
3325 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3326 pPager->iInUseMM = 0;
3327 }
3328 sqlite3_mutex_leave(mutex);
3329
3330 /* Return the number of bytes released
3331 */
danielk197713f72992005-12-18 08:51:22 +00003332 return nReleased;
3333}
drh86f8c192007-08-22 00:39:19 +00003334#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003335
drh165ffe92005-03-15 17:09:30 +00003336/*
danielk1977e180dd92007-04-05 17:15:52 +00003337** Read the content of page pPg out of the database file.
3338*/
3339static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3340 int rc;
danielk197762079062007-08-15 17:08:46 +00003341 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003342 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003343 assert(pPager->fd->pMethods||pPager->tempFile);
3344 if( !pPager->fd->pMethods ){
3345 return SQLITE_IOERR_SHORT_READ;
3346 }
danielk197762079062007-08-15 17:08:46 +00003347 offset = (pgno-1)*(i64)pPager->pageSize;
3348 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003349 PAGER_INCR(sqlite3_pager_readdb_count);
3350 PAGER_INCR(pPager->nRead);
3351 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003352 if( pgno==1 ){
3353 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3354 sizeof(pPager->dbFileVers));
3355 }
danielk1977e180dd92007-04-05 17:15:52 +00003356 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003357 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3358 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003359 return rc;
3360}
3361
3362
3363/*
danielk1977e277be02007-03-23 18:12:06 +00003364** This function is called to obtain the shared lock required before
3365** data may be read from the pager cache. If the shared lock has already
3366** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003367**
3368** Immediately after obtaining the shared lock (if required), this function
3369** checks for a hot-journal file. If one is found, an emergency rollback
3370** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003371*/
3372static int pagerSharedLock(Pager *pPager){
3373 int rc = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00003374 int isHot = 0;
danielk1977e277be02007-03-23 18:12:06 +00003375
danielk1977ae72d982007-10-03 08:46:44 +00003376 /* If this database is opened for exclusive access, has no outstanding
3377 ** page references and is in an error-state, now is the chance to clear
3378 ** the error. Discard the contents of the pager-cache and treat any
3379 ** open journal file as a hot-journal.
3380 */
3381 if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
3382 if( pPager->journalOpen ){
3383 isHot = 1;
3384 }
3385 pager_reset(pPager);
3386 pPager->errCode = SQLITE_OK;
3387 }
3388
3389 /* If the pager is still in an error state, do not proceed. The error
3390 ** state will be cleared at some point in the future when all page
3391 ** references are dropped and the cache can be discarded.
3392 */
3393 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3394 return pPager->errCode;
3395 }
3396
3397 if( pPager->state==PAGER_UNLOCK || isHot ){
danielk1977b4b47412007-08-17 15:53:36 +00003398 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003399 if( !MEMDB ){
3400 assert( pPager->nRef==0 );
3401 if( !pPager->noReadlock ){
3402 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3403 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003404 assert( pPager->state==PAGER_UNLOCK );
danielk1977e277be02007-03-23 18:12:06 +00003405 return pager_error(pPager, rc);
3406 }
3407 assert( pPager->state>=SHARED_LOCK );
3408 }
3409
3410 /* If a journal file exists, and there is no RESERVED lock on the
3411 ** database file, then it either needs to be played back or deleted.
3412 */
drh19db9352008-03-27 22:42:51 +00003413 rc = hasHotJournal(pPager);
3414 if( rc<0 ){
danielk197752b472a2008-05-05 16:23:55 +00003415 rc = SQLITE_IOERR_NOMEM;
3416 goto failed;
drh19db9352008-03-27 22:42:51 +00003417 }
3418 if( rc==1 || isHot ){
danielk1977e277be02007-03-23 18:12:06 +00003419 /* Get an EXCLUSIVE lock on the database file. At this point it is
3420 ** important that a RESERVED lock is not obtained on the way to the
3421 ** EXCLUSIVE lock. If it were, another process might open the
3422 ** database file, detect the RESERVED lock, and conclude that the
3423 ** database is safe to read while this process is still rolling it
3424 ** back.
3425 **
3426 ** Because the intermediate RESERVED lock is not requested, the
3427 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00003428 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00003429 */
danielk1977ae72d982007-10-03 08:46:44 +00003430 if( pPager->state<EXCLUSIVE_LOCK ){
3431 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3432 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003433 rc = pager_error(pPager, rc);
3434 goto failed;
danielk1977ae72d982007-10-03 08:46:44 +00003435 }
3436 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00003437 }
danielk1977e277be02007-03-23 18:12:06 +00003438
drh16e45a42008-04-19 20:34:18 +00003439 /* Open the journal for read/write access. This is because in
drhfd131da2007-08-07 17:13:03 +00003440 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003441 ** possibly used for a transaction later on. On some systems, the
3442 ** OsTruncate() call used in exclusive-access mode also requires
3443 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003444 */
drh16e45a42008-04-19 20:34:18 +00003445 if( !isHot && pPager->journalOpen==0 ){
danielk1977ce98bba2008-04-03 10:13:01 +00003446 int res = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS);
3447 if( res==1 ){
danielk1977ae72d982007-10-03 08:46:44 +00003448 int fout = 0;
3449 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3450 assert( !pPager->tempFile );
3451 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3452 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3453 if( fout&SQLITE_OPEN_READONLY ){
3454 rc = SQLITE_BUSY;
3455 sqlite3OsClose(pPager->jfd);
3456 }
drh16e45a42008-04-19 20:34:18 +00003457 }else if( res==0 ){
3458 /* If the journal does not exist, that means some other process
3459 ** has already rolled it back */
3460 rc = SQLITE_BUSY;
danielk1977ce98bba2008-04-03 10:13:01 +00003461 }else{
drh16e45a42008-04-19 20:34:18 +00003462 /* If sqlite3OsAccess() returns a negative value, that means it
3463 ** failed a memory allocation */
3464 rc = SQLITE_IOERR_NOMEM;
danielk1977979f38e2007-03-27 16:19:51 +00003465 }
3466 }
danielk1977e277be02007-03-23 18:12:06 +00003467 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003468 if( rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_UNLOCK
3469 && rc!=SQLITE_IOERR_NOMEM
3470 ){
3471 rc = SQLITE_BUSY;
drh1aa5af12008-03-07 19:51:14 +00003472 }
danielk197752b472a2008-05-05 16:23:55 +00003473 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003474 }
3475 pPager->journalOpen = 1;
3476 pPager->journalStarted = 0;
3477 pPager->journalOff = 0;
3478 pPager->setMaster = 0;
3479 pPager->journalHdr = 0;
3480
3481 /* Playback and delete the journal. Drop the database write
3482 ** lock and reacquire the read lock.
3483 */
3484 rc = pager_playback(pPager, 1);
3485 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003486 rc = pager_error(pPager, rc);
3487 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003488 }
danielk1977c5859712007-03-26 12:26:27 +00003489 assert(pPager->state==PAGER_SHARED ||
3490 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3491 );
danielk1977e277be02007-03-23 18:12:06 +00003492 }
3493
3494 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003495 /* The shared-lock has just been acquired on the database file
3496 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003497 ** read or write transaction). Check to see if the database
3498 ** has been modified. If the database has changed, flush the
3499 ** cache.
3500 **
3501 ** Database changes is detected by looking at 15 bytes beginning
3502 ** at offset 24 into the file. The first 4 of these 16 bytes are
3503 ** a 32-bit counter that is incremented with each change. The
3504 ** other bytes change randomly with each file change when
3505 ** a codec is in use.
3506 **
3507 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003508 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003509 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003510 */
drh86a88112007-04-16 15:02:19 +00003511 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003512 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003513
danielk1977e180dd92007-04-05 17:15:52 +00003514 if( pPager->errCode ){
danielk197752b472a2008-05-05 16:23:55 +00003515 rc = pPager->errCode;
3516 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003517 }
3518
danielk1977e180dd92007-04-05 17:15:52 +00003519 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003520 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003521 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003522 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003523 goto failed;
danielk1977e180dd92007-04-05 17:15:52 +00003524 }
drh86a88112007-04-16 15:02:19 +00003525 }else{
3526 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003527 }
3528
drh86a88112007-04-16 15:02:19 +00003529 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003530 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003531 }
3532 }
3533 }
danielk1977c5859712007-03-26 12:26:27 +00003534 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3535 if( pPager->state==PAGER_UNLOCK ){
3536 pPager->state = PAGER_SHARED;
3537 }
danielk1977e277be02007-03-23 18:12:06 +00003538 }
3539
danielk197752b472a2008-05-05 16:23:55 +00003540 failed:
3541 if( rc!=SQLITE_OK ){
3542 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
3543 pager_unlock(pPager);
3544 }
danielk1977e277be02007-03-23 18:12:06 +00003545 return rc;
3546}
3547
3548/*
danielk1977e180dd92007-04-05 17:15:52 +00003549** Allocate a PgHdr object. Either create a new one or reuse
3550** an existing one that is not otherwise in use.
3551**
3552** A new PgHdr structure is created if any of the following are
3553** true:
3554**
3555** (1) We have not exceeded our maximum allocated cache size
3556** as set by the "PRAGMA cache_size" command.
3557**
3558** (2) There are no unused PgHdr objects available at this time.
3559**
3560** (3) This is an in-memory database.
3561**
3562** (4) There are no PgHdr objects that do not require a journal
3563** file sync and a sync of the journal file is currently
3564** prohibited.
3565**
3566** Otherwise, reuse an existing PgHdr. In other words, reuse an
3567** existing PgHdr if all of the following are true:
3568**
3569** (1) We have reached or exceeded the maximum cache size
3570** allowed by "PRAGMA cache_size".
3571**
3572** (2) There is a PgHdr available with PgHdr->nRef==0
3573**
3574** (3) We are not in an in-memory database
3575**
3576** (4) Either there is an available PgHdr that does not need
3577** to be synced to disk or else disk syncing is currently
3578** allowed.
danielk197724168722007-04-02 05:07:47 +00003579*/
3580static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3581 int rc = SQLITE_OK;
3582 PgHdr *pPg;
drh1e3af332007-10-20 13:17:54 +00003583 int nByteHdr;
danielk197724168722007-04-02 05:07:47 +00003584
danielk1977e180dd92007-04-05 17:15:52 +00003585 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003586 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003587 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003588 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003589 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003590 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003591 ){
drh0d180202008-02-14 23:26:56 +00003592 void *pData;
danielk197724168722007-04-02 05:07:47 +00003593 if( pPager->nPage>=pPager->nHash ){
3594 pager_resize_hash_table(pPager,
3595 pPager->nHash<256 ? 256 : pPager->nHash*2);
3596 if( pPager->nHash==0 ){
3597 rc = SQLITE_NOMEM;
3598 goto pager_allocate_out;
3599 }
3600 }
drhed138fb2007-08-22 22:04:37 +00003601 pagerLeave(pPager);
drh1e3af332007-10-20 13:17:54 +00003602 nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra
3603 + MEMDB*sizeof(PgHistory);
drh0d180202008-02-14 23:26:56 +00003604 pPg = sqlite3_malloc( nByteHdr );
3605 if( pPg ){
3606 pData = sqlite3_malloc( pPager->pageSize );
3607 if( pData==0 ){
3608 sqlite3_free(pPg);
3609 pPg = 0;
3610 }
3611 }
drhed138fb2007-08-22 22:04:37 +00003612 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003613 if( pPg==0 ){
3614 rc = SQLITE_NOMEM;
3615 goto pager_allocate_out;
3616 }
drh1e3af332007-10-20 13:17:54 +00003617 memset(pPg, 0, nByteHdr);
drh0d180202008-02-14 23:26:56 +00003618 pPg->pData = pData;
danielk197724168722007-04-02 05:07:47 +00003619 pPg->pPager = pPager;
3620 pPg->pNextAll = pPager->pAll;
3621 pPager->pAll = pPg;
3622 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003623 }else{
3624 /* Recycle an existing page with a zero ref-count. */
danielk1977843e65f2007-09-01 16:16:15 +00003625 rc = pager_recycle(pPager, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003626 if( rc==SQLITE_BUSY ){
3627 rc = SQLITE_IOERR_BLOCKED;
3628 }
danielk197724168722007-04-02 05:07:47 +00003629 if( rc!=SQLITE_OK ){
3630 goto pager_allocate_out;
3631 }
3632 assert( pPager->state>=SHARED_LOCK );
3633 assert(pPg);
3634 }
3635 *ppPg = pPg;
3636
3637pager_allocate_out:
3638 return rc;
3639}
3640
3641/*
drhd33d5a82007-04-26 12:11:28 +00003642** Make sure we have the content for a page. If the page was
3643** previously acquired with noContent==1, then the content was
3644** just initialized to zeros instead of being read from disk.
3645** But now we need the real data off of disk. So make sure we
3646** have it. Read it in if we do not have it already.
3647*/
3648static int pager_get_content(PgHdr *pPg){
3649 if( pPg->needRead ){
3650 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3651 if( rc==SQLITE_OK ){
3652 pPg->needRead = 0;
3653 }else{
3654 return rc;
3655 }
3656 }
3657 return SQLITE_OK;
3658}
3659
3660/*
drhd9b02572001-04-15 00:37:09 +00003661** Acquire a page.
3662**
drh58a11682001-11-10 13:51:08 +00003663** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003664** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003665**
drhd33d5a82007-04-26 12:11:28 +00003666** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003667** file is smaller than the requested page, then no actual disk
3668** read occurs and the memory image of the page is initialized to
3669** all zeros. The extra data appended to a page is always initialized
3670** to zeros the first time a page is loaded into memory.
3671**
drhd9b02572001-04-15 00:37:09 +00003672** The acquisition might fail for several reasons. In all cases,
3673** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003674**
drhd33d5a82007-04-26 12:11:28 +00003675** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003676** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003677** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003678** just returns 0. This routine acquires a read-lock the first time it
3679** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003680** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003681** or journal files.
drh0787db62007-03-04 13:15:27 +00003682**
drh538f5702007-04-13 02:14:30 +00003683** If noContent is false, the page contents are actually read from disk.
3684** If noContent is true, it means that we do not care about the contents
3685** of the page at this time, so do not do a disk read. Just fill in the
3686** page content with zeros. But mark the fact that we have not read the
3687** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003688** sqlite3PagerWrite() is called on this page or if this routine is
3689** called again with noContent==0, that means that the content is needed
3690** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003691*/
drh86f8c192007-08-22 00:39:19 +00003692static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003693 Pager *pPager, /* The pager open on the database file */
3694 Pgno pgno, /* Page number to fetch */
3695 DbPage **ppPage, /* Write a pointer to the page here */
3696 int noContent /* Do not bother reading content from disk if true */
3697){
drhed7c8552001-04-11 14:29:21 +00003698 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003699 int rc;
drhed7c8552001-04-11 14:29:21 +00003700
danielk1977e277be02007-03-23 18:12:06 +00003701 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3702
danielk197726836652005-01-17 01:33:13 +00003703 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3704 ** number greater than this, or zero, is requested.
3705 */
drh267cb322005-09-16 17:16:52 +00003706 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003707 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003708 }
3709
drhd9b02572001-04-15 00:37:09 +00003710 /* Make sure we have not hit any critical errors.
3711 */
drh836faa42003-01-11 13:30:57 +00003712 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003713 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003714
danielk197713adf8a2004-06-03 16:08:41 +00003715 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003716 ** on the database file. pagerSharedLock() is a no-op if
3717 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003718 */
danielk1977e277be02007-03-23 18:12:06 +00003719 rc = pagerSharedLock(pPager);
3720 if( rc!=SQLITE_OK ){
3721 return rc;
drhed7c8552001-04-11 14:29:21 +00003722 }
danielk1977e277be02007-03-23 18:12:06 +00003723 assert( pPager->state!=PAGER_UNLOCK );
3724
3725 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003726 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003727 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003728 int nMax;
drhed7c8552001-04-11 14:29:21 +00003729 int h;
drh538f5702007-04-13 02:14:30 +00003730 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003731 rc = pagerAllocatePage(pPager, &pPg);
3732 if( rc!=SQLITE_OK ){
3733 return rc;
drhed7c8552001-04-11 14:29:21 +00003734 }
danielk197724168722007-04-02 05:07:47 +00003735
drhed7c8552001-04-11 14:29:21 +00003736 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003737 assert( !MEMDB || pgno>pPager->stmtSize );
drhf5e7bb52008-02-18 14:47:33 +00003738 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
3739 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003740
drh605ad8f2006-05-03 23:34:05 +00003741 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003742 pPg->nRef = 1;
danielk197775bab7d2006-01-23 13:09:45 +00003743
drhd9b02572001-04-15 00:37:09 +00003744 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003745 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003746 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003747 }
danielk1977979f38e2007-03-27 16:19:51 +00003748 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003749 if( pPager->errCode ){
danielk1977efaaf572006-01-16 11:29:19 +00003750 rc = pPager->errCode;
danielk1977ae72d982007-10-03 08:46:44 +00003751 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003752 return rc;
3753 }
danielk197775bab7d2006-01-23 13:09:45 +00003754
3755 /* Populate the page with data, either by reading from the database
3756 ** file, or by setting the entire page to zero.
3757 */
drhd215acf2007-04-13 04:01:58 +00003758 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003759 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003760 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003761 return SQLITE_FULL;
3762 }
drh90f5ecb2004-07-22 01:19:35 +00003763 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003764 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003765 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003766 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003767 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003768 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3769 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003770 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003771 return rc;
drh81a20f22001-10-12 17:30:04 +00003772 }
danielk1977b4626a32007-04-28 15:47:43 +00003773 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003774 }
danielk197775bab7d2006-01-23 13:09:45 +00003775
3776 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003777 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003778 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003779 pPg->pNextHash = pPager->aHash[h];
3780 pPager->aHash[h] = pPg;
3781 if( pPg->pNextHash ){
3782 assert( pPg->pNextHash->pPrevHash==0 );
3783 pPg->pNextHash->pPrevHash = pPg;
3784 }
3785
danielk19773c407372005-02-15 02:54:14 +00003786#ifdef SQLITE_CHECK_PAGES
3787 pPg->pageHash = pager_pagehash(pPg);
3788#endif
drhed7c8552001-04-11 14:29:21 +00003789 }else{
drhd9b02572001-04-15 00:37:09 +00003790 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003791 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003792 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003793 if( !noContent ){
3794 rc = pager_get_content(pPg);
3795 if( rc ){
3796 return rc;
3797 }
3798 }
drhdf0b3b02001-06-23 11:36:20 +00003799 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003800 }
danielk19773b8a05f2007-03-19 17:44:26 +00003801 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003802 return SQLITE_OK;
3803}
drh86f8c192007-08-22 00:39:19 +00003804int sqlite3PagerAcquire(
3805 Pager *pPager, /* The pager open on the database file */
3806 Pgno pgno, /* Page number to fetch */
3807 DbPage **ppPage, /* Write a pointer to the page here */
3808 int noContent /* Do not bother reading content from disk if true */
3809){
3810 int rc;
3811 pagerEnter(pPager);
3812 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3813 pagerLeave(pPager);
3814 return rc;
3815}
3816
drhed7c8552001-04-11 14:29:21 +00003817
3818/*
drh7e3b0a02001-04-28 16:52:40 +00003819** Acquire a page if it is already in the in-memory cache. Do
3820** not read the page from disk. Return a pointer to the page,
3821** or 0 if the page is not in cache.
3822**
danielk19773b8a05f2007-03-19 17:44:26 +00003823** See also sqlite3PagerGet(). The difference between this routine
3824** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003825** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003826** returns NULL if the page is not in cache or if a disk I/O error
3827** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003828*/
danielk19773b8a05f2007-03-19 17:44:26 +00003829DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003830 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003831
drh836faa42003-01-11 13:30:57 +00003832 assert( pPager!=0 );
3833 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003834
drh86f8c192007-08-22 00:39:19 +00003835 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003836 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003837 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003838 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3839 /* Do nothing */
3840 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3841 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003842 }
drh86f8c192007-08-22 00:39:19 +00003843 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003844 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003845}
3846
3847/*
drhed7c8552001-04-11 14:29:21 +00003848** Release a page.
3849**
3850** If the number of references to the page drop to zero, then the
3851** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003852** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003853** removed.
3854*/
danielk19773b8a05f2007-03-19 17:44:26 +00003855int sqlite3PagerUnref(DbPage *pPg){
danielk1977ae72d982007-10-03 08:46:44 +00003856 Pager *pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003857
3858 /* Decrement the reference count for this page
3859 */
drhed7c8552001-04-11 14:29:21 +00003860 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003861 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003862 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +00003863
danielk19773c407372005-02-15 02:54:14 +00003864 CHECK_PAGE(pPg);
3865
drh72f82862001-05-24 21:06:34 +00003866 /* When the number of references to a page reach 0, call the
3867 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003868 */
drhed7c8552001-04-11 14:29:21 +00003869 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003870
3871 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003872 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003873 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003874 }
drhd9b02572001-04-15 00:37:09 +00003875
3876 /* When all pages reach the freelist, drop the read lock from
3877 ** the database file.
3878 */
3879 pPager->nRef--;
3880 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003881 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003882 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003883 }
drhed7c8552001-04-11 14:29:21 +00003884 }
danielk1977ae72d982007-10-03 08:46:44 +00003885 pagerLeave(pPager);
drhd9b02572001-04-15 00:37:09 +00003886 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003887}
3888
3889/*
drha6abd042004-06-09 17:37:22 +00003890** Create a journal file for pPager. There should already be a RESERVED
3891** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003892**
3893** Return SQLITE_OK if everything. Return an error code and release the
3894** write lock if anything goes wrong.
3895*/
3896static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003897 sqlite3_vfs *pVfs = pPager->pVfs;
3898 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3899
drhda47d772002-12-02 04:25:19 +00003900 int rc;
drhb7f91642004-10-31 02:22:47 +00003901 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003902 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003903 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003904 assert( pPager->pInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003905 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003906 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003907 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00003908 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003909 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003910 rc = SQLITE_NOMEM;
3911 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003912 }
danielk1977b4b47412007-08-17 15:53:36 +00003913
drhfdc40e92008-04-17 20:59:37 +00003914 if( pPager->journalOpen==0 ){
3915 if( pPager->tempFile ){
3916 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3917 }else{
3918 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003919 }
drhfdc40e92008-04-17 20:59:37 +00003920#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3921 rc = sqlite3JournalOpen(
3922 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3923 );
3924#else
3925 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
3926#endif
3927 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3928 pPager->journalOff = 0;
3929 pPager->setMaster = 0;
3930 pPager->journalHdr = 0;
3931 if( rc!=SQLITE_OK ){
3932 if( rc==SQLITE_NOMEM ){
3933 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3934 }
3935 goto failed_to_open_journal;
3936 }
drhda47d772002-12-02 04:25:19 +00003937 }
3938 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003939 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003940 pPager->needSync = 0;
3941 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003942 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003943 if( pPager->errCode ){
3944 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003945 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003946 }
drhda47d772002-12-02 04:25:19 +00003947 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003948
danielk197776572402004-06-25 02:38:54 +00003949 rc = writeJournalHdr(pPager);
3950
drhac69b052004-05-12 13:30:07 +00003951 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003952 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003953 }
danielk1977ae72d982007-10-03 08:46:44 +00003954 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003955 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003956 if( rc==SQLITE_OK ){
3957 rc = SQLITE_FULL;
3958 }
3959 }
drh9c105bb2004-10-02 20:38:28 +00003960 return rc;
3961
3962failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003963 sqlite3BitvecDestroy(pPager->pInJournal);
3964 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003965 return rc;
drhda47d772002-12-02 04:25:19 +00003966}
3967
3968/*
drh4b845d72002-03-05 12:41:19 +00003969** Acquire a write-lock on the database. The lock is removed when
3970** the any of the following happen:
3971**
drh80e35f42007-03-30 14:06:34 +00003972** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003973** * sqlite3PagerRollback() is called.
3974** * sqlite3PagerClose() is called.
3975** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003976**
danielk197713adf8a2004-06-03 16:08:41 +00003977** The first parameter to this routine is a pointer to any open page of the
3978** database file. Nothing changes about the page - it is used merely to
3979** acquire a pointer to the Pager structure and as proof that there is
3980** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003981**
danielk197713adf8a2004-06-03 16:08:41 +00003982** The second parameter indicates how much space in bytes to reserve for a
3983** master journal file-name at the start of the journal when it is created.
3984**
3985** A journal file is opened if this is not a temporary file. For temporary
3986** files, the opening of the journal file is deferred until there is an
3987** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003988**
drha6abd042004-06-09 17:37:22 +00003989** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003990**
3991** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3992** immediately instead of waiting until we try to flush the cache. The
3993** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003994*/
danielk19773b8a05f2007-03-19 17:44:26 +00003995int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003996 Pager *pPager = pPg->pPager;
3997 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003998 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003999 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00004000 assert( pPager->state!=PAGER_UNLOCK );
4001 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00004002 assert( pPager->pInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00004003 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00004004 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00004005 pPager->origDbSize = pPager->dbSize;
4006 }else{
drh054889e2005-11-30 03:20:31 +00004007 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00004008 if( rc==SQLITE_OK ){
4009 pPager->state = PAGER_RESERVED;
4010 if( exFlag ){
4011 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
4012 }
4013 }
danielk19779eed5052004-06-04 10:38:30 +00004014 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00004015 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00004016 return rc;
drhac69b052004-05-12 13:30:07 +00004017 }
drha6abd042004-06-09 17:37:22 +00004018 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00004019 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhfdc40e92008-04-17 20:59:37 +00004020 if( pPager->useJournal && !pPager->tempFile
4021 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drhac69b052004-05-12 13:30:07 +00004022 rc = pager_open_journal(pPager);
4023 }
drh4b845d72002-03-05 12:41:19 +00004024 }
danielk1977334cdb62007-03-26 08:05:12 +00004025 }else if( pPager->journalOpen && pPager->journalOff==0 ){
4026 /* This happens when the pager was in exclusive-access mode last
4027 ** time a (read or write) transaction was successfully concluded
4028 ** by this connection. Instead of deleting the journal file it was
4029 ** kept open and truncated to 0 bytes.
4030 */
4031 assert( pPager->nRec==0 );
4032 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00004033 assert( pPager->pInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00004034 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00004035 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004036 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhed138fb2007-08-22 22:04:37 +00004037 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004038 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00004039 rc = SQLITE_NOMEM;
4040 }else{
drh369339d2007-03-30 16:01:55 +00004041 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00004042 rc = writeJournalHdr(pPager);
4043 }
drh4b845d72002-03-05 12:41:19 +00004044 }
danielk1977334cdb62007-03-26 08:05:12 +00004045 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00004046 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00004047 return rc;
4048}
4049
4050/*
drh605ad8f2006-05-03 23:34:05 +00004051** Make a page dirty. Set its dirty flag and add it to the dirty
4052** page list.
4053*/
4054static void makeDirty(PgHdr *pPg){
4055 if( pPg->dirty==0 ){
4056 Pager *pPager = pPg->pPager;
4057 pPg->dirty = 1;
4058 pPg->pDirty = pPager->pDirty;
4059 if( pPager->pDirty ){
4060 pPager->pDirty->pPrevDirty = pPg;
4061 }
4062 pPg->pPrevDirty = 0;
4063 pPager->pDirty = pPg;
4064 }
4065}
4066
4067/*
4068** Make a page clean. Clear its dirty bit and remove it from the
4069** dirty page list.
4070*/
4071static void makeClean(PgHdr *pPg){
4072 if( pPg->dirty ){
4073 pPg->dirty = 0;
4074 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00004075 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004076 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
4077 }
4078 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00004079 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004080 pPg->pPrevDirty->pDirty = pPg->pDirty;
4081 }else{
drh153c62c2007-08-24 03:51:33 +00004082 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004083 pPg->pPager->pDirty = pPg->pDirty;
4084 }
4085 }
4086}
4087
4088
4089/*
drhed7c8552001-04-11 14:29:21 +00004090** Mark a data page as writeable. The page is written into the journal
4091** if it is not there already. This routine must be called before making
4092** changes to a page.
4093**
4094** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00004095** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00004096** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00004097** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00004098** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00004099**
4100** If the journal file could not be written because the disk is full,
4101** then this routine returns SQLITE_FULL and does an immediate rollback.
4102** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00004103** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00004104** reset.
drhed7c8552001-04-11 14:29:21 +00004105*/
danielk19773b8a05f2007-03-19 17:44:26 +00004106static int pager_write(PgHdr *pPg){
4107 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00004108 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00004109 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00004110
drh6446c4d2001-12-15 14:22:18 +00004111 /* Check for errors
4112 */
danielk1977efaaf572006-01-16 11:29:19 +00004113 if( pPager->errCode ){
4114 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004115 }
drh5e00f6c2001-09-13 13:46:56 +00004116 if( pPager->readOnly ){
4117 return SQLITE_PERM;
4118 }
drh6446c4d2001-12-15 14:22:18 +00004119
danielk197776572402004-06-25 02:38:54 +00004120 assert( !pPager->setMaster );
4121
danielk19773c407372005-02-15 02:54:14 +00004122 CHECK_PAGE(pPg);
4123
drh538f5702007-04-13 02:14:30 +00004124 /* If this page was previously acquired with noContent==1, that means
4125 ** we didn't really read in the content of the page. This can happen
4126 ** (for example) when the page is being moved to the freelist. But
4127 ** now we are (perhaps) moving the page off of the freelist for
4128 ** reuse and we need to know its original content so that content
4129 ** can be stored in the rollback journal. So do the read at this
4130 ** time.
4131 */
drhd33d5a82007-04-26 12:11:28 +00004132 rc = pager_get_content(pPg);
4133 if( rc ){
4134 return rc;
drh538f5702007-04-13 02:14:30 +00004135 }
4136
drh6446c4d2001-12-15 14:22:18 +00004137 /* Mark the page as dirty. If the page has already been written
4138 ** to the journal then we can return right away.
4139 */
drh605ad8f2006-05-03 23:34:05 +00004140 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00004141 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00004142 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004143 }else{
drh6446c4d2001-12-15 14:22:18 +00004144
danielk1977a0bf2652004-11-04 14:30:04 +00004145 /* If we get this far, it means that the page needs to be
4146 ** written to the transaction journal or the ckeckpoint journal
4147 ** or both.
4148 **
4149 ** First check to see that the transaction journal exists and
4150 ** create it if it does not.
4151 */
4152 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00004153 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00004154 if( rc!=SQLITE_OK ){
4155 return rc;
4156 }
4157 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00004158 if( !pPager->journalOpen && pPager->useJournal
4159 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00004160 rc = pager_open_journal(pPager);
4161 if( rc!=SQLITE_OK ) return rc;
4162 }
danielk1977a0bf2652004-11-04 14:30:04 +00004163 pPager->dirtyCache = 1;
4164
4165 /* The transaction journal now exists and we have a RESERVED or an
4166 ** EXCLUSIVE lock on the main database file. Write the current page to
4167 ** the transaction journal if it is not there already.
4168 */
drhfdc40e92008-04-17 20:59:37 +00004169 if( !pPg->inJournal && (pPager->journalOpen || MEMDB) ){
danielk1977a0bf2652004-11-04 14:30:04 +00004170 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00004171 if( MEMDB ){
4172 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00004173 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004174 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00004175 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977662278e2007-11-05 15:30:12 +00004176 if( !pHist->pOrig ){
4177 return SQLITE_NOMEM;
danielk1977a0bf2652004-11-04 14:30:04 +00004178 }
danielk1977662278e2007-11-05 15:30:12 +00004179 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
danielk1977a0bf2652004-11-04 14:30:04 +00004180 }else{
drhbf4bca52007-09-06 22:19:14 +00004181 u32 cksum;
4182 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00004183
drh267cb322005-09-16 17:16:52 +00004184 /* We should never write to the journal file the page that
4185 ** contains the database locks. The following assert verifies
4186 ** that we do not. */
4187 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004188 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004189 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00004190 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4191 if( rc==SQLITE_OK ){
4192 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4193 pPager->journalOff + 4);
4194 pPager->journalOff += pPager->pageSize+4;
4195 }
4196 if( rc==SQLITE_OK ){
4197 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4198 pPager->journalOff += 4;
4199 }
4200 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00004201 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00004202 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00004203 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4204 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00004205
drhfd131da2007-08-07 17:13:03 +00004206 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004207 ** transaction will be rolled back by the layer above.
4208 */
danielk1977a0bf2652004-11-04 14:30:04 +00004209 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004210 return rc;
4211 }
danielk197707cb5602006-01-20 10:55:05 +00004212
danielk1977a0bf2652004-11-04 14:30:04 +00004213 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00004214 assert( pPager->pInJournal!=0 );
4215 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004216 pPg->needSync = !pPager->noSync;
4217 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00004218 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004219 }
drhac69b052004-05-12 13:30:07 +00004220 }
danielk197713adf8a2004-06-03 16:08:41 +00004221 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004222 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004223 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004224 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004225 }
4226 if( pPg->needSync ){
4227 pPager->needSync = 1;
4228 }
4229 pPg->inJournal = 1;
4230 }
4231
4232 /* If the statement journal is open and the page is not in it,
4233 ** then write the current page to the statement journal. Note that
4234 ** the statement journal format differs from the standard journal format
4235 ** in that it omits the checksums and the header.
4236 */
danielk1977f35843b2007-04-07 15:03:17 +00004237 if( pPager->stmtInUse
4238 && !pageInStatement(pPg)
4239 && (int)pPg->pgno<=pPager->stmtSize
4240 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004241 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4242 if( MEMDB ){
4243 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4244 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00004245 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004246 if( pHist->pStmt ){
4247 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4248 }
drh4f0c5872007-03-26 22:05:01 +00004249 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004250 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004251 }else{
danielk197762079062007-08-15 17:08:46 +00004252 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00004253 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4254 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4255 if( rc==SQLITE_OK ){
4256 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4257 }
drh4f0c5872007-03-26 22:05:01 +00004258 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004259 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004260 return rc;
4261 }
danielk1977a0bf2652004-11-04 14:30:04 +00004262 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00004263 assert( pPager->pInStmt!=0 );
4264 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00004265 }
drhd9b02572001-04-15 00:37:09 +00004266 }
drhfa86c412002-02-02 15:01:15 +00004267 }
4268
4269 /* Update the database size and return.
4270 */
drh1aa2d8b2007-01-03 15:34:29 +00004271 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004272 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004273 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004274 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004275 pPager->dbSize++;
4276 }
drh306dc212001-05-21 13:45:10 +00004277 }
drh69688d52001-04-14 16:38:23 +00004278 return rc;
drhed7c8552001-04-11 14:29:21 +00004279}
4280
4281/*
danielk19774099f6e2007-03-19 11:25:20 +00004282** This function is used to mark a data-page as writable. It uses
4283** pager_write() to open a journal file (if it is not already open)
4284** and write the page *pData to the journal.
4285**
4286** The difference between this function and pager_write() is that this
4287** function also deals with the special case where 2 or more pages
4288** fit on a single disk sector. In this case all co-resident pages
4289** must have been written to the journal file before returning.
4290*/
danielk19773b8a05f2007-03-19 17:44:26 +00004291int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004292 int rc = SQLITE_OK;
4293
danielk19773b8a05f2007-03-19 17:44:26 +00004294 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004295 Pager *pPager = pPg->pPager;
4296 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4297
drh86f8c192007-08-22 00:39:19 +00004298 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004299 if( !MEMDB && nPagePerSector>1 ){
4300 Pgno nPageCount; /* Total number of pages in database file */
4301 Pgno pg1; /* First page of the sector pPg is located on. */
4302 int nPage; /* Number of pages starting at pg1 to journal */
4303 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004304 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004305
4306 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4307 ** header to be written between the pages journaled by this function.
4308 */
4309 assert( pPager->doNotSync==0 );
4310 pPager->doNotSync = 1;
4311
4312 /* This trick assumes that both the page-size and sector-size are
4313 ** an integer power of 2. It sets variable pg1 to the identifier
4314 ** of the first page of the sector pPg is located on.
4315 */
4316 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4317
danielk19773b8a05f2007-03-19 17:44:26 +00004318 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004319 if( pPg->pgno>nPageCount ){
4320 nPage = (pPg->pgno - pg1)+1;
4321 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4322 nPage = nPageCount+1-pg1;
4323 }else{
4324 nPage = nPagePerSector;
4325 }
4326 assert(nPage>0);
4327 assert(pg1<=pPg->pgno);
4328 assert((pg1+nPage)>pPg->pgno);
4329
4330 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4331 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004332 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00004333 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00004334 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004335 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004336 if( rc==SQLITE_OK ){
4337 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004338 if( pPage->needSync ){
4339 needSync = 1;
4340 }
danielk19773b8a05f2007-03-19 17:44:26 +00004341 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004342 }
4343 }
drhc81945e2008-03-10 14:12:53 +00004344 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk1977dd97a492007-08-22 18:54:32 +00004345 if( pPage->needSync ){
4346 needSync = 1;
4347 }
danielk19774099f6e2007-03-19 11:25:20 +00004348 }
4349 }
4350
danielk1977dd97a492007-08-22 18:54:32 +00004351 /* If the PgHdr.needSync flag is set for any of the nPage pages
4352 ** starting at pg1, then it needs to be set for all of them. Because
4353 ** writing to any of these nPage pages may damage the others, the
4354 ** journal file must contain sync()ed copies of all of them
4355 ** before any of them can be written out to the database file.
4356 */
4357 if( needSync ){
4358 for(ii=0; ii<nPage && needSync; ii++){
4359 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4360 if( pPage ) pPage->needSync = 1;
4361 }
4362 assert(pPager->needSync);
4363 }
4364
danielk19774099f6e2007-03-19 11:25:20 +00004365 assert( pPager->doNotSync==1 );
4366 pPager->doNotSync = 0;
4367 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004368 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004369 }
drh86f8c192007-08-22 00:39:19 +00004370 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004371 return rc;
4372}
4373
4374/*
drhaacc5432002-01-06 17:07:40 +00004375** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004376** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004377** to change the content of the page.
4378*/
danielk19777d3a6662006-01-23 16:21:05 +00004379#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004380int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004381 return pPg->dirty;
4382}
danielk19777d3a6662006-01-23 16:21:05 +00004383#endif
drh6019e162001-07-02 17:51:45 +00004384
drh001bbcb2003-03-19 03:14:00 +00004385/*
drh30e58752002-03-02 20:41:57 +00004386** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004387** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004388** that page might be marked as dirty.
4389**
4390** The overlying software layer calls this routine when all of the data
4391** on the given page is unused. The pager marks the page as clean so
4392** that it does not get written to disk.
4393**
4394** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004395** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004396** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004397**
4398** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004399** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004400** will thereafter be ignored. This is necessary to avoid a problem
4401** where a page with data is added to the freelist during one part of
4402** a transaction then removed from the freelist during a later part
4403** of the same transaction and reused for some other purpose. When it
4404** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004405** the sqlite3PagerDontRollback() routine is called. But because the
4406** page contains critical data, we still need to be sure it gets
4407** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004408*/
drh538f5702007-04-13 02:14:30 +00004409void sqlite3PagerDontWrite(DbPage *pDbPage){
4410 PgHdr *pPg = pDbPage;
4411 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004412
drhb7f91642004-10-31 02:22:47 +00004413 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004414 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004415 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004416 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004417 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004418 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4419 /* If this pages is the last page in the file and the file has grown
4420 ** during the current transaction, then do NOT mark the page as clean.
4421 ** When the database file grows, we must make sure that the last page
4422 ** gets written at least once so that the disk file will be the correct
4423 ** size. If you do not write this page and the size of the file
4424 ** on the disk ends up being too small, that can lead to database
4425 ** corruption during the next transaction.
4426 */
4427 }else{
drh538f5702007-04-13 02:14:30 +00004428 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4429 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004430 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004431#ifdef SQLITE_CHECK_PAGES
4432 pPg->pageHash = pager_pagehash(pPg);
4433#endif
drh8124a302002-06-25 14:43:57 +00004434 }
drh30e58752002-03-02 20:41:57 +00004435 }
drh86f8c192007-08-22 00:39:19 +00004436 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004437}
4438
4439/*
4440** A call to this routine tells the pager that if a rollback occurs,
4441** it is not necessary to restore the data on the given page. This
4442** means that the pager does not have to record the given page in the
4443** rollback journal.
drh538f5702007-04-13 02:14:30 +00004444**
4445** If we have not yet actually read the content of this page (if
4446** the PgHdr.needRead flag is set) then this routine acts as a promise
4447** that we will never need to read the page content in the future.
4448** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00004449*/
danielk19773b8a05f2007-03-19 17:44:26 +00004450void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004451 Pager *pPager = pPg->pPager;
4452
drh86f8c192007-08-22 00:39:19 +00004453 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004454 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00004455
4456 /* If the journal file is not open, or DontWrite() has been called on
4457 ** this page (DontWrite() sets the alwaysRollback flag), then this
4458 ** function is a no-op.
4459 */
4460 if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){
danielk197787c29a92008-01-18 11:33:16 +00004461 pagerLeave(pPager);
4462 return;
4463 }
danielk1977a55e9352008-01-21 13:04:34 +00004464 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
4465
drhc5d0bd92008-04-14 01:00:57 +00004466#ifdef SQLITE_SECURE_DELETE
4467 if( pPg->inJournal || (int)pPg->pgno > pPager->origDbSize ){
4468 return;
4469 }
4470#endif
4471
4472 /* If SECURE_DELETE is disabled, then there is no way that this
4473 ** routine can be called on a page for which sqlite3PagerDontWrite()
4474 ** has not been previously called during the same transaction.
4475 ** And if DontWrite() has previously been called, the following
4476 ** conditions must be met.
danielk1977a55e9352008-01-21 13:04:34 +00004477 */
4478 assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize );
4479
drhf5e7bb52008-02-18 14:47:33 +00004480 assert( pPager->pInJournal!=0 );
4481 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a55e9352008-01-21 13:04:34 +00004482 pPg->inJournal = 1;
4483 pPg->needRead = 0;
4484 if( pPager->stmtInUse ){
drhc5d0bd92008-04-14 01:00:57 +00004485 assert( pPager->stmtSize >= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00004486 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00004487 }
danielk1977a55e9352008-01-21 13:04:34 +00004488 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
4489 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh86f8c192007-08-22 00:39:19 +00004490 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004491}
4492
drhac69b052004-05-12 13:30:07 +00004493
drh30e58752002-03-02 20:41:57 +00004494/*
drh80e35f42007-03-30 14:06:34 +00004495** This routine is called to increment the database file change-counter,
4496** stored at byte 24 of the pager file.
4497*/
danielk1977c7b60172007-08-22 11:22:03 +00004498static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004499 PgHdr *pPgHdr;
4500 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004501 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004502
4503 if( !pPager->changeCountDone ){
4504 /* Open page 1 of the file for writing. */
4505 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4506 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004507
4508 if( !isDirect ){
4509 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00004510 if( rc!=SQLITE_OK ){
4511 sqlite3PagerUnref(pPgHdr);
4512 return rc;
4513 }
danielk1977c7b60172007-08-22 11:22:03 +00004514 }
4515
drh80e35f42007-03-30 14:06:34 +00004516 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004517 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004518 change_counter++;
4519 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004520
4521 if( isDirect && pPager->fd->pMethods ){
4522 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4523 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4524 }
4525
drh80e35f42007-03-30 14:06:34 +00004526 /* Release the page reference. */
4527 sqlite3PagerUnref(pPgHdr);
4528 pPager->changeCountDone = 1;
4529 }
danielk1977c7b60172007-08-22 11:22:03 +00004530 return rc;
drh80e35f42007-03-30 14:06:34 +00004531}
4532
4533/*
danielk1977f653d782008-03-20 11:04:21 +00004534** Sync the pager file to disk.
4535*/
4536int sqlite3PagerSync(Pager *pPager){
4537 int rc;
4538 pagerEnter(pPager);
4539 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4540 pagerLeave(pPager);
4541 return rc;
4542}
4543
4544/*
drh80e35f42007-03-30 14:06:34 +00004545** Sync the database file for the pager pPager. zMaster points to the name
4546** of a master journal file that should be written into the individual
4547** journal file. zMaster may be NULL, which is interpreted as no master
4548** journal (a single database transaction).
4549**
4550** This routine ensures that the journal is synced, all dirty pages written
4551** to the database file and the database file synced. The only thing that
4552** remains to commit the transaction is to delete the journal file (or
4553** master journal file if specified).
4554**
4555** Note that if zMaster==NULL, this does not overwrite a previous value
4556** passed to an sqlite3PagerCommitPhaseOne() call.
4557**
4558** If parameter nTrunc is non-zero, then the pager file is truncated to
4559** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00004560**
4561** If the final parameter - noSync - is true, then the database file itself
4562** is not synced. The caller must call sqlite3PagerSync() directly to
4563** sync the database file before calling CommitPhaseTwo() to delete the
4564** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00004565*/
danielk1977f653d782008-03-20 11:04:21 +00004566int sqlite3PagerCommitPhaseOne(
4567 Pager *pPager,
4568 const char *zMaster,
4569 Pgno nTrunc,
4570 int noSync
4571){
drh80e35f42007-03-30 14:06:34 +00004572 int rc = SQLITE_OK;
4573
4574 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4575 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004576 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004577
4578 /* If this is an in-memory db, or no pages have been written to, or this
4579 ** function has already been called, it is a no-op.
4580 */
4581 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4582 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004583
danielk1977c7b60172007-08-22 11:22:03 +00004584#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4585 /* The atomic-write optimization can be used if all of the
4586 ** following are true:
4587 **
4588 ** + The file-system supports the atomic-write property for
4589 ** blocks of size page-size, and
4590 ** + This commit is not part of a multi-file transaction, and
4591 ** + Exactly one page has been modified and store in the journal file.
4592 **
4593 ** If the optimization can be used, then the journal file will never
4594 ** be created for this transaction.
4595 */
danielk1977f55b8992007-08-24 08:15:53 +00004596 int useAtomicWrite = (
4597 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00004598 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00004599 pPager->journalOff==jrnlBufferSize(pPager) &&
4600 nTrunc==0 &&
4601 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4602 );
danielk1977700b9c52008-04-24 12:37:40 +00004603 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00004604 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004605 /* Update the nRec field in the journal file. */
4606 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4607 assert(pPager->nRec==1);
4608 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4609
4610 /* Update the db file change counter. The following call will modify
4611 ** the in-memory representation of page 1 to include the updated
4612 ** change counter and then write page 1 directly to the database
4613 ** file. Because of the atomic-write property of the host file-system,
4614 ** this is safe.
4615 */
danielk1977ae72d982007-10-03 08:46:44 +00004616 if( rc==SQLITE_OK ){
4617 rc = pager_incr_changecounter(pPager, 1);
4618 }
danielk1977f55b8992007-08-24 08:15:53 +00004619 }else{
4620 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00004621 }
4622
danielk1977ae72d982007-10-03 08:46:44 +00004623 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00004624#endif
4625
drh80e35f42007-03-30 14:06:34 +00004626 /* If a master journal file name has already been written to the
4627 ** journal file, then no sync is required. This happens when it is
4628 ** written, then the process fails to upgrade from a RESERVED to an
4629 ** EXCLUSIVE lock. The next time the process tries to commit the
4630 ** transaction the m-j name will have already been written.
4631 */
4632 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00004633 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004634 if( rc!=SQLITE_OK ) goto sync_exit;
4635#ifndef SQLITE_OMIT_AUTOVACUUM
4636 if( nTrunc!=0 ){
4637 /* If this transaction has made the database smaller, then all pages
4638 ** being discarded by the truncation must be written to the journal
4639 ** file.
4640 */
4641 Pgno i;
4642 int iSkip = PAGER_MJ_PGNO(pPager);
4643 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
drhf5e7bb52008-02-18 14:47:33 +00004644 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
drh80e35f42007-03-30 14:06:34 +00004645 rc = sqlite3PagerGet(pPager, i, &pPg);
4646 if( rc!=SQLITE_OK ) goto sync_exit;
4647 rc = sqlite3PagerWrite(pPg);
4648 sqlite3PagerUnref(pPg);
4649 if( rc!=SQLITE_OK ) goto sync_exit;
4650 }
4651 }
4652 }
4653#endif
4654 rc = writeMasterJournal(pPager, zMaster);
4655 if( rc!=SQLITE_OK ) goto sync_exit;
4656 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004657 }
danielk1977c7b60172007-08-22 11:22:03 +00004658 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004659
4660#ifndef SQLITE_OMIT_AUTOVACUUM
4661 if( nTrunc!=0 ){
4662 rc = sqlite3PagerTruncate(pPager, nTrunc);
4663 if( rc!=SQLITE_OK ) goto sync_exit;
4664 }
4665#endif
4666
4667 /* Write all dirty pages to the database file */
4668 pPg = pager_get_all_dirty_pages(pPager);
4669 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004670 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004671 assert( rc!=SQLITE_IOERR_BLOCKED );
4672 /* The error might have left the dirty list all fouled up here,
4673 ** but that does not matter because if the if the dirty list did
4674 ** get corrupted, then the transaction will roll back and
4675 ** discard the dirty list. There is an assert in
4676 ** pager_get_all_dirty_pages() that verifies that no attempt
4677 ** is made to use an invalid dirty list.
4678 */
drh153c62c2007-08-24 03:51:33 +00004679 goto sync_exit;
4680 }
drh3cdd7d32007-03-30 14:46:01 +00004681 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004682
4683 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004684 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004685 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004686 }
4687 IOTRACE(("DBSYNC %p\n", pPager))
4688
4689 pPager->state = PAGER_SYNCED;
4690 }else if( MEMDB && nTrunc!=0 ){
4691 rc = sqlite3PagerTruncate(pPager, nTrunc);
4692 }
4693
4694sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004695 if( rc==SQLITE_IOERR_BLOCKED ){
4696 /* pager_incr_changecounter() may attempt to obtain an exclusive
4697 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004698 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004699 * better to return SQLITE_BUSY.
4700 */
4701 rc = SQLITE_BUSY;
4702 }
drh86f8c192007-08-22 00:39:19 +00004703 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004704 return rc;
4705}
4706
4707
4708/*
drhed7c8552001-04-11 14:29:21 +00004709** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004710**
4711** If the commit fails for any reason, a rollback attempt is made
4712** and an error code is returned. If the commit worked, SQLITE_OK
4713** is returned.
drhed7c8552001-04-11 14:29:21 +00004714*/
drh80e35f42007-03-30 14:06:34 +00004715int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004716 int rc;
drhed7c8552001-04-11 14:29:21 +00004717 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004718
danielk1977efaaf572006-01-16 11:29:19 +00004719 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004720 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004721 }
drha6abd042004-06-09 17:37:22 +00004722 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004723 return SQLITE_ERROR;
4724 }
drh86f8c192007-08-22 00:39:19 +00004725 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004726 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004727 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004728 pPg = pager_get_all_dirty_pages(pPager);
4729 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004730 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4731 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004732 pPg->dirty = 0;
4733 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004734 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004735 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004736 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004737 pPg = pPg->pDirty;
4738 }
drh605ad8f2006-05-03 23:34:05 +00004739 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004740#ifndef NDEBUG
4741 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4742 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4743 assert( !pPg->alwaysRollback );
4744 assert( !pHist->pOrig );
4745 assert( !pHist->pStmt );
4746 }
4747#endif
drhac69b052004-05-12 13:30:07 +00004748 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004749 pPager->state = PAGER_SHARED;
danielk197787c29a92008-01-18 11:33:16 +00004750 pagerLeave(pPager);
drhac69b052004-05-12 13:30:07 +00004751 return SQLITE_OK;
4752 }
drh3cdd7d32007-03-30 14:46:01 +00004753 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4754 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004755 rc = pager_error(pPager, rc);
4756 pagerLeave(pPager);
4757 return rc;
drhed7c8552001-04-11 14:29:21 +00004758}
4759
4760/*
drha6abd042004-06-09 17:37:22 +00004761** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004762** All in-memory cache pages revert to their original data contents.
4763** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004764**
4765** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004766** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004767** process is writing trash into the journal file (SQLITE_CORRUPT) or
4768** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4769** codes are returned for all these occasions. Otherwise,
4770** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004771*/
danielk19773b8a05f2007-03-19 17:44:26 +00004772int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004773 int rc;
drh4f0c5872007-03-26 22:05:01 +00004774 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004775 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004776 PgHdr *p;
4777 for(p=pPager->pAll; p; p=p->pNextAll){
4778 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004779 assert( !p->alwaysRollback );
4780 if( !p->dirty ){
4781 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4782 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4783 continue;
4784 }
4785
drhac69b052004-05-12 13:30:07 +00004786 pHist = PGHDR_TO_HIST(p, pPager);
4787 if( pHist->pOrig ){
4788 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004789 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004790 }else{
drh4f0c5872007-03-26 22:05:01 +00004791 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004792 }
4793 clearHistory(pHist);
4794 p->dirty = 0;
4795 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004796 pHist->inStmt = 0;
4797 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004798 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004799 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004800 }
drhac69b052004-05-12 13:30:07 +00004801 }
drh605ad8f2006-05-03 23:34:05 +00004802 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004803 pPager->pStmt = 0;
4804 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004805 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004806 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004807 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004808 return SQLITE_OK;
4809 }
4810
drh86f8c192007-08-22 00:39:19 +00004811 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004812 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004813 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004814 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004815 return rc;
4816 }
drhdb48ee02003-01-16 13:42:43 +00004817
danielk1977efaaf572006-01-16 11:29:19 +00004818 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004819 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004820 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004821 }
drh86f8c192007-08-22 00:39:19 +00004822 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004823 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004824 }
drha6abd042004-06-09 17:37:22 +00004825 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004826 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004827 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004828 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004829 if( rc==SQLITE_OK ){
4830 rc = rc2;
4831 }
4832 }else{
danielk1977e277be02007-03-23 18:12:06 +00004833 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004834 }
danielk1977e180dd92007-04-05 17:15:52 +00004835 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004836 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004837
4838 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4839 ** cache. So call pager_error() on the way out to make any error
4840 ** persistent.
4841 */
drh86f8c192007-08-22 00:39:19 +00004842 rc = pager_error(pPager, rc);
4843 pagerLeave(pPager);
4844 return rc;
drh98808ba2001-10-18 12:34:46 +00004845}
drhd9b02572001-04-15 00:37:09 +00004846
4847/*
drh5e00f6c2001-09-13 13:46:56 +00004848** Return TRUE if the database file is opened read-only. Return FALSE
4849** if the database is (in theory) writable.
4850*/
danielk19773b8a05f2007-03-19 17:44:26 +00004851int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004852 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004853}
4854
4855/*
drh0f7eb612006-08-08 13:51:43 +00004856** Return the number of references to the pager.
4857*/
danielk19773b8a05f2007-03-19 17:44:26 +00004858int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004859 return pPager->nRef;
4860}
4861
4862#ifdef SQLITE_TEST
4863/*
drhd9b02572001-04-15 00:37:09 +00004864** This routine is used for testing and analysis only.
4865*/
danielk19773b8a05f2007-03-19 17:44:26 +00004866int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004867 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004868 a[0] = pPager->nRef;
4869 a[1] = pPager->nPage;
4870 a[2] = pPager->mxPage;
4871 a[3] = pPager->dbSize;
4872 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004873 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004874 a[6] = pPager->nHit;
4875 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004876 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004877 a[9] = pPager->nRead;
4878 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004879 return a;
4880}
drh0f7eb612006-08-08 13:51:43 +00004881#endif
drhdd793422001-06-28 01:54:48 +00004882
drhfa86c412002-02-02 15:01:15 +00004883/*
drhac69b052004-05-12 13:30:07 +00004884** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004885**
4886** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004887** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004888** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004889*/
drh86f8c192007-08-22 00:39:19 +00004890static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004891 int rc;
drhac69b052004-05-12 13:30:07 +00004892 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004893 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004894 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004895 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004896 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004897 pPager->stmtInUse = 1;
4898 pPager->stmtSize = pPager->dbSize;
4899 return SQLITE_OK;
4900 }
drhda47d772002-12-02 04:25:19 +00004901 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004902 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004903 return SQLITE_OK;
4904 }
drhfa86c412002-02-02 15:01:15 +00004905 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004906 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004907 assert( pPager->pInStmt==0 );
4908 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00004909 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004910 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004911 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004912 return SQLITE_NOMEM;
4913 }
danielk197776572402004-06-25 02:38:54 +00004914 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004915 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004916 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004917 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004918 if( !pPager->stmtOpen ){
drh334b2992007-09-06 23:28:23 +00004919 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
drh33f4e022007-09-03 15:19:34 +00004920 SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00004921 if( rc ){
4922 goto stmt_begin_failed;
4923 }
drhac69b052004-05-12 13:30:07 +00004924 pPager->stmtOpen = 1;
4925 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004926 }
drhac69b052004-05-12 13:30:07 +00004927 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004928 return SQLITE_OK;
4929
drhac69b052004-05-12 13:30:07 +00004930stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00004931 if( pPager->pInStmt ){
4932 sqlite3BitvecDestroy(pPager->pInStmt);
4933 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004934 }
4935 return rc;
4936}
drh86f8c192007-08-22 00:39:19 +00004937int sqlite3PagerStmtBegin(Pager *pPager){
4938 int rc;
4939 pagerEnter(pPager);
4940 rc = pagerStmtBegin(pPager);
4941 pagerLeave(pPager);
4942 return rc;
4943}
drhfa86c412002-02-02 15:01:15 +00004944
4945/*
drhac69b052004-05-12 13:30:07 +00004946** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004947*/
danielk19773b8a05f2007-03-19 17:44:26 +00004948int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004949 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004950 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004951 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004952 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004953 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004954 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00004955 sqlite3BitvecDestroy(pPager->pInStmt);
4956 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004957 }else{
4958 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004959 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004960 pNext = pHist->pNextStmt;
4961 assert( pHist->inStmt );
4962 pHist->inStmt = 0;
4963 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004964 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004965 pHist->pStmt = 0;
4966 }
4967 }
4968 pPager->stmtNRec = 0;
4969 pPager->stmtInUse = 0;
4970 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004971 }
drhac69b052004-05-12 13:30:07 +00004972 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004973 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004974 return SQLITE_OK;
4975}
4976
4977/*
drhac69b052004-05-12 13:30:07 +00004978** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004979*/
danielk19773b8a05f2007-03-19 17:44:26 +00004980int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004981 int rc;
drh86f8c192007-08-22 00:39:19 +00004982 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004983 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004984 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004985 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004986 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004987 PgHistory *pHist;
4988 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4989 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004990 if( pHist->pStmt ){
4991 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004992 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004993 pHist->pStmt = 0;
4994 }
4995 }
4996 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004997 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004998 rc = SQLITE_OK;
4999 }else{
5000 rc = pager_stmt_playback(pPager);
5001 }
danielk19773b8a05f2007-03-19 17:44:26 +00005002 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00005003 }else{
5004 rc = SQLITE_OK;
5005 }
drhac69b052004-05-12 13:30:07 +00005006 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00005007 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00005008 return rc;
5009}
5010
drh73509ee2003-04-06 20:44:45 +00005011/*
5012** Return the full pathname of the database file.
5013*/
danielk19773b8a05f2007-03-19 17:44:26 +00005014const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00005015 return pPager->zFilename;
5016}
5017
drhb20ea9d2004-02-09 01:20:36 +00005018/*
drhd0679ed2007-08-28 22:24:34 +00005019** Return the VFS structure for the pager.
5020*/
5021const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
5022 return pPager->pVfs;
5023}
5024
5025/*
drhcc6bb3e2007-08-31 16:11:35 +00005026** Return the file handle for the database file associated
5027** with the pager. This might return NULL if the file has
5028** not yet been opened.
5029*/
5030sqlite3_file *sqlite3PagerFile(Pager *pPager){
5031 return pPager->fd;
5032}
5033
5034/*
danielk19775865e3d2004-06-14 06:03:57 +00005035** Return the directory of the database file.
5036*/
danielk19773b8a05f2007-03-19 17:44:26 +00005037const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005038 return pPager->zDirectory;
5039}
5040
5041/*
5042** Return the full pathname of the journal file.
5043*/
danielk19773b8a05f2007-03-19 17:44:26 +00005044const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005045 return pPager->zJournal;
5046}
5047
5048/*
drh2c8997b2005-08-27 16:36:48 +00005049** Return true if fsync() calls are disabled for this pager. Return FALSE
5050** if fsync()s are executed normally.
5051*/
danielk19773b8a05f2007-03-19 17:44:26 +00005052int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00005053 return pPager->noSync;
5054}
5055
drh7c4ac0c2007-04-05 11:25:58 +00005056#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00005057/*
drhb20ea9d2004-02-09 01:20:36 +00005058** Set the codec for this pager
5059*/
danielk19773b8a05f2007-03-19 17:44:26 +00005060void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00005061 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00005062 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00005063 void *pCodecArg
5064){
5065 pPager->xCodec = xCodec;
5066 pPager->pCodecArg = pCodecArg;
5067}
drh7c4ac0c2007-04-05 11:25:58 +00005068#endif
drhb20ea9d2004-02-09 01:20:36 +00005069
danielk1977687566d2004-11-02 12:56:41 +00005070#ifndef SQLITE_OMIT_AUTOVACUUM
5071/*
danielk197787c29a92008-01-18 11:33:16 +00005072** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00005073**
drh5e385312007-06-16 04:42:12 +00005074** There must be no references to the page previously located at
5075** pgno (which we call pPgOld) though that page is allowed to be
5076** in cache. If the page previous located at pgno is not already
5077** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00005078**
drh5e385312007-06-16 04:42:12 +00005079** References to the page pPg remain valid. Updating any
5080** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00005081** allocated along with the page) is the responsibility of the caller.
5082**
danielk19775fd057a2005-03-09 13:09:43 +00005083** A transaction must be active when this routine is called. It used to be
5084** required that a statement transaction was not active, but this restriction
5085** has been removed (CREATE INDEX needs to move a page when a statement
5086** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00005087*/
danielk19773b8a05f2007-03-19 17:44:26 +00005088int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00005089 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00005090 int h;
danielk197794daf7f2004-11-08 09:26:09 +00005091 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00005092
drh86f8c192007-08-22 00:39:19 +00005093 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005094 assert( pPg->nRef>0 );
5095
drh4f0c5872007-03-26 22:05:01 +00005096 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00005097 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00005098 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00005099
danielk1977b4626a32007-04-28 15:47:43 +00005100 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00005101 if( pPg->needSync ){
5102 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00005103 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00005104 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00005105 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00005106 }
5107
drh85b623f2007-12-13 21:54:09 +00005108 /* Unlink pPg from its hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00005109 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00005110
danielk1977ef73ee92004-11-06 12:26:07 +00005111 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00005112 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00005113 ** page pgno before the 'move' operation, it needs to be retained
5114 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00005115 */
drh5e385312007-06-16 04:42:12 +00005116 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00005117 pPgOld = pager_lookup(pPager, pgno);
5118 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00005119 assert( pPgOld->nRef==0 );
5120 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00005121 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00005122 pPg->needSync = pPgOld->needSync;
5123 }else{
5124 pPg->needSync = 0;
5125 }
drhf5e7bb52008-02-18 14:47:33 +00005126 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
danielk1977687566d2004-11-02 12:56:41 +00005127
danielk1977f5fdda82004-11-03 08:44:05 +00005128 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00005129 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00005130 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00005131 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00005132 if( pPager->aHash[h] ){
5133 assert( pPager->aHash[h]->pPrevHash==0 );
5134 pPager->aHash[h]->pPrevHash = pPg;
5135 }
5136 pPg->pNextHash = pPager->aHash[h];
5137 pPager->aHash[h] = pPg;
5138 pPg->pPrevHash = 0;
5139
drh605ad8f2006-05-03 23:34:05 +00005140 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00005141 pPager->dirtyCache = 1;
5142
danielk197794daf7f2004-11-08 09:26:09 +00005143 if( needSyncPgno ){
5144 /* If needSyncPgno is non-zero, then the journal file needs to be
5145 ** sync()ed before any data is written to database file page needSyncPgno.
5146 ** Currently, no such page exists in the page-cache and the
drhf5e7bb52008-02-18 14:47:33 +00005147 ** Pager.pInJournal bit has been set. This needs to be remedied by loading
danielk197794daf7f2004-11-08 09:26:09 +00005148 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00005149 **
danielk1977a98d7b42008-01-18 13:42:54 +00005150 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00005151 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00005152 ** array. Otherwise, if the page is loaded and written again in
5153 ** this transaction, it may be written to the database file before
5154 ** it is synced into the journal file. This way, it may end up in
5155 ** the journal file twice, but that is not a problem.
5156 **
danielk19773b8a05f2007-03-19 17:44:26 +00005157 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00005158 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00005159 */
5160 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005161 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00005162 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00005163 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00005164 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00005165 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
5166 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00005167 }
danielk197787c29a92008-01-18 11:33:16 +00005168 pagerLeave(pPager);
5169 return rc;
5170 }
danielk1977ae825582004-11-23 09:06:55 +00005171 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00005172 pPgHdr->needSync = 1;
5173 pPgHdr->inJournal = 1;
5174 makeDirty(pPgHdr);
5175 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00005176 }
5177
drh86f8c192007-08-22 00:39:19 +00005178 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005179 return SQLITE_OK;
5180}
5181#endif
5182
danielk19773b8a05f2007-03-19 17:44:26 +00005183/*
5184** Return a pointer to the data for the specified page.
5185*/
5186void *sqlite3PagerGetData(DbPage *pPg){
5187 return PGHDR_TO_DATA(pPg);
5188}
5189
5190/*
5191** Return a pointer to the Pager.nExtra bytes of "extra" space
5192** allocated along with the specified page.
5193*/
5194void *sqlite3PagerGetExtra(DbPage *pPg){
5195 Pager *pPager = pPg->pPager;
5196 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
5197}
5198
danielk197741483462007-03-24 16:45:04 +00005199/*
5200** Get/set the locking-mode for this pager. Parameter eMode must be one
5201** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
5202** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
5203** the locking-mode is set to the value specified.
5204**
5205** The returned value is either PAGER_LOCKINGMODE_NORMAL or
5206** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
5207** locking-mode.
5208*/
5209int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00005210 assert( eMode==PAGER_LOCKINGMODE_QUERY
5211 || eMode==PAGER_LOCKINGMODE_NORMAL
5212 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5213 assert( PAGER_LOCKINGMODE_QUERY<0 );
5214 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5215 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00005216 pPager->exclusiveMode = eMode;
5217 }
5218 return (int)pPager->exclusiveMode;
5219}
5220
drh3b020132008-04-17 17:02:01 +00005221/*
5222** Get/set the journal-mode for this pager. Parameter eMode must be one
5223** of PAGER_JOURNALMODE_QUERY, PAGER_JOURNALMODE_DELETE or
5224** PAGER_JOURNALMODE_PERSIST. If the parameter is not _QUERY, then
5225** the journal-mode is set to the value specified.
5226**
5227** The returned value is either PAGER_JOURNALMODE_DELETE or
5228** PAGER_JOURNALMODE_PERSIST, indicating the current (possibly updated)
5229** journal-mode.
5230*/
5231int sqlite3PagerJournalMode(Pager *pPager, int eMode){
5232 assert( eMode==PAGER_JOURNALMODE_QUERY
5233 || eMode==PAGER_JOURNALMODE_DELETE
drhfdc40e92008-04-17 20:59:37 +00005234 || eMode==PAGER_JOURNALMODE_PERSIST
5235 || eMode==PAGER_JOURNALMODE_OFF );
drh3b020132008-04-17 17:02:01 +00005236 assert( PAGER_JOURNALMODE_QUERY<0 );
5237 assert( PAGER_JOURNALMODE_DELETE>=0 && PAGER_JOURNALMODE_PERSIST>=0 );
drhfdc40e92008-04-17 20:59:37 +00005238 if( eMode>=0 ){
5239 pPager->journalMode = eMode;
drh3b020132008-04-17 17:02:01 +00005240 }
drhfdc40e92008-04-17 20:59:37 +00005241 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00005242}
5243
drhd919fe12007-12-11 19:34:44 +00005244#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00005245/*
5246** Print a listing of all referenced pages and their ref count.
5247*/
danielk19773b8a05f2007-03-19 17:44:26 +00005248void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00005249 PgHdr *pPg;
5250 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5251 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00005252 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5253 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00005254 }
5255}
5256#endif
drh2e66f0b2005-04-28 17:18:48 +00005257
5258#endif /* SQLITE_OMIT_DISKIO */