blob: a3262b2a5060ca8761836131e5bb6fb6e7685b4a [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**
danielk19774c999992008-07-16 18:17:55 +000021** @(#) $Id: pager.c,v 1.466 2008/07/16 18:17:56 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
danielk197721b7ce62008-07-07 11:18:27 +0000272 PgHdr *pPrevAll; /* A list of all pages */
danielk19779f61c2f2007-08-27 17:27:49 +0000273 PagerLruLink gfree; /* Global list of nRef==0 pages */
274#endif
danielk19773c407372005-02-15 02:54:14 +0000275#ifdef SQLITE_CHECK_PAGES
276 u32 pageHash;
277#endif
drhbf4bca52007-09-06 22:19:14 +0000278 void *pData; /* Page data */
279 /* Pager.nExtra bytes of local data appended to this header */
drhed7c8552001-04-11 14:29:21 +0000280};
281
drhac69b052004-05-12 13:30:07 +0000282/*
283** For an in-memory only database, some extra information is recorded about
284** each page so that changes can be rolled back. (Journal files are not
285** used for in-memory databases.) The following information is added to
286** the end of every EXTRA block for in-memory databases.
287**
288** This information could have been added directly to the PgHdr structure.
289** But then it would take up an extra 8 bytes of storage on every PgHdr
290** even for disk-based databases. Splitting it out saves 8 bytes. This
291** is only a savings of 0.8% but those percentages add up.
292*/
293typedef struct PgHistory PgHistory;
294struct PgHistory {
295 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
296 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000297 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
298 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000299};
drh9eb9e262004-02-11 02:18:05 +0000300
301/*
302** A macro used for invoking the codec if there is one
303*/
304#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000305# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
306# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000307#else
drhc001c582006-03-06 18:23:16 +0000308# define CODEC1(P,D,N,X) /* NO-OP */
309# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000310#endif
311
drhed7c8552001-04-11 14:29:21 +0000312/*
drh69688d52001-04-14 16:38:23 +0000313** Convert a pointer to a PgHdr into a pointer to its data
314** and back again.
drhed7c8552001-04-11 14:29:21 +0000315*/
drhbf4bca52007-09-06 22:19:14 +0000316#define PGHDR_TO_DATA(P) ((P)->pData)
317#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
drhac69b052004-05-12 13:30:07 +0000318#define PGHDR_TO_HIST(P,PGR) \
drhbf4bca52007-09-06 22:19:14 +0000319 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000320
321/*
drhed7c8552001-04-11 14:29:21 +0000322** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000323**
drh4f0ee682007-03-30 20:43:40 +0000324** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000325** or SQLITE_FULL. Once one of the first three errors occurs, it persists
326** and is returned as the result of every major pager API call. The
327** SQLITE_FULL return code is slightly different. It persists only until the
328** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000329** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000330** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000331*/
332struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000333 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000334 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000335 u8 journalStarted; /* True if header of journal is synced */
336 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000337 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000338 u8 stmtOpen; /* True if the statement subjournal is open */
339 u8 stmtInUse; /* True we are in a statement subtransaction */
340 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000341 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000342 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000343 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000344 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000345 u8 tempFile; /* zFilename is a temporary file */
346 u8 readOnly; /* True for a read-only database */
347 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000348 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000349 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000350 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000351 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000352 u8 doNotSync; /* Boolean. While true, do not spill the cache */
353 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000354 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drhd138c012008-05-13 00:58:18 +0000355 u8 dbModified; /* True if there are any changes to the Db */
drh80e35f42007-03-30 14:06:34 +0000356 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000357 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000358 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000359 int dbSize; /* Number of pages in the file */
360 int origDbSize; /* dbSize before the current change */
361 int stmtSize; /* Size of database (in pages) at stmt_begin() */
362 int nRec; /* Number of pages written to the journal */
363 u32 cksumInit; /* Quasi-random value added to every checksum */
364 int stmtNRec; /* Number of records in stmt subjournal */
365 int nExtra; /* Add this many bytes to each in-memory page */
366 int pageSize; /* Number of bytes in a page */
367 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000368 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
369 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000370 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000371 Bitvec *pInJournal; /* One bit for each page in the database file */
372 Bitvec *pInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000373 char *zFilename; /* Name of the database file */
374 char *zJournal; /* Name of the journal file */
375 char *zDirectory; /* Directory hold database and journal files */
danielk197762079062007-08-15 17:08:46 +0000376 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
377 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000378 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
danielk19779f61c2f2007-08-27 17:27:49 +0000379 PagerLruList lru; /* LRU list of free pages */
drhd9b02572001-04-15 00:37:09 +0000380 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000381 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000382 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000383 i64 journalOff; /* Current byte offset in the journal file */
384 i64 journalHdr; /* Byte offset to previous journal header */
385 i64 stmtHdrOff; /* First journal header written this statement */
386 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000387 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000388 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000389#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000390 int nHit, nMiss; /* Cache hits and missing */
391 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000392#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000393 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
394 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000395#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000396 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000397 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000398#endif
drh8ca0c722006-05-07 17:49:38 +0000399 int nHash; /* Size of the pager hash table */
400 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000401#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +0000402 Pager *pNext; /* Doubly linked list of pagers on which */
403 Pager *pPrev; /* sqlite3_release_memory() will work */
drh80105af2008-05-21 15:38:14 +0000404 volatile int iInUseMM; /* Non-zero if unavailable to MM */
405 volatile int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
danielk197713f72992005-12-18 08:51:22 +0000406#endif
danielk19778186df82007-03-06 13:45:59 +0000407 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000408 char dbFileVers[16]; /* Changes whenever database file changes */
danielk1977b53e4962008-06-04 06:45:59 +0000409 i64 journalSizeLimit; /* Size limit for persistent journal files */
drhd9b02572001-04-15 00:37:09 +0000410};
411
412/*
drh538f5702007-04-13 02:14:30 +0000413** The following global variables hold counters used for
414** testing purposes only. These variables do not exist in
415** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000416*/
417#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000418int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
419int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
420int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
421int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
422# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000423#else
drh538f5702007-04-13 02:14:30 +0000424# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000425#endif
426
drh86f8c192007-08-22 00:39:19 +0000427/*
428** The following variable points to the head of a double-linked list
429** of all pagers that are eligible for page stealing by the
430** sqlite3_release_memory() interface. Access to this list is
431** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
432*/
433#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
434static Pager *sqlite3PagerList = 0;
danielk19779f61c2f2007-08-27 17:27:49 +0000435static PagerLruList sqlite3LruPageList = {0, 0, 0};
drh86f8c192007-08-22 00:39:19 +0000436#endif
drh538f5702007-04-13 02:14:30 +0000437
438
drhfcd35c72005-05-21 02:48:08 +0000439/*
drh5e00f6c2001-09-13 13:46:56 +0000440** Journal files begin with the following magic string. The data
441** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000442**
drhae2b40c2004-06-09 19:03:54 +0000443** Since version 2.8.0, the journal format contains additional sanity
444** checking information. If the power fails while the journal is begin
445** written, semi-random garbage data might appear in the journal
446** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000447** to roll the journal back, the database could be corrupted. The additional
448** sanity checking data is an attempt to discover the garbage in the
449** journal and ignore it.
450**
drhae2b40c2004-06-09 19:03:54 +0000451** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000452** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000453** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000454** This cksum is initialized to a 32-bit random value that appears in the
455** journal file right after the header. The random initializer is important,
456** because garbage data that appears at the end of a journal is likely
457** data that was once in other files that have now been deleted. If the
458** garbage data came from an obsolete journal file, the checksums might
459** be correct. But by initializing the checksum to random value which
460** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000461*/
drhae2b40c2004-06-09 19:03:54 +0000462static const unsigned char aJournalMagic[] = {
463 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000464};
465
466/*
drh726de592004-06-10 23:35:50 +0000467** The size of the header and of each page in the journal is determined
468** by the following macros.
drh968af522003-02-11 14:55:40 +0000469*/
drhae2b40c2004-06-09 19:03:54 +0000470#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000471
danielk197776572402004-06-25 02:38:54 +0000472/*
473** The journal header size for this pager. In the future, this could be
474** set to some value read from the disk controller. The important
475** characteristic is that it is the same size as a disk sector.
476*/
477#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
478
drhb7f91642004-10-31 02:22:47 +0000479/*
480** The macro MEMDB is true if we are dealing with an in-memory database.
481** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
482** the value of MEMDB will be a constant and the compiler will optimize
483** out code that would never execute.
484*/
485#ifdef SQLITE_OMIT_MEMORYDB
486# define MEMDB 0
487#else
488# define MEMDB pPager->memDb
489#endif
490
491/*
danielk197776572402004-06-25 02:38:54 +0000492** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
493** reserved for working around a windows/posix incompatibility). It is
494** used in the journal to signify that the remainder of the journal file
495** is devoted to storing a master journal name - there are no more pages to
496** roll back. See comments for function writeMasterJournal() for details.
497*/
danielk1977599fcba2004-11-08 07:13:13 +0000498/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
499#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000500
drh968af522003-02-11 14:55:40 +0000501/*
danielk197726836652005-01-17 01:33:13 +0000502** The maximum legal page number is (2^31 - 1).
503*/
504#define PAGER_MAX_PGNO 2147483647
505
506/*
drh86f8c192007-08-22 00:39:19 +0000507** The pagerEnter() and pagerLeave() routines acquire and release
508** a mutex on each pager. The mutex is recursive.
509**
510** This is a special-purpose mutex. It only provides mutual exclusion
511** between the Btree and the Memory Management sqlite3_release_memory()
512** function. It does not prevent, for example, two Btrees from accessing
513** the same pager at the same time. Other general-purpose mutexes in
514** the btree layer handle that chore.
515*/
516#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
517 static void pagerEnter(Pager *p){
518 p->iInUseDB++;
519 if( p->iInUseMM && p->iInUseDB==1 ){
drh26e4a8b2008-05-01 17:16:52 +0000520#ifndef SQLITE_MUTEX_NOOP
drh64e2bb72008-05-07 12:29:55 +0000521 sqlite3_mutex *mutex;
danielk197759f8c082008-06-18 17:09:10 +0000522 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +0000523#endif
drh86f8c192007-08-22 00:39:19 +0000524 p->iInUseDB = 0;
525 sqlite3_mutex_enter(mutex);
526 p->iInUseDB = 1;
527 sqlite3_mutex_leave(mutex);
528 }
529 assert( p->iInUseMM==0 );
530 }
531 static void pagerLeave(Pager *p){
532 p->iInUseDB--;
533 assert( p->iInUseDB>=0 );
534 }
535#else
536# define pagerEnter(X)
537# define pagerLeave(X)
538#endif
539
540/*
danielk197784f786f2007-08-28 08:00:17 +0000541** Add page pPg to the end of the linked list managed by structure
542** pList (pPg becomes the last entry in the list - the most recently
543** used). Argument pLink should point to either pPg->free or pPg->gfree,
544** depending on whether pPg is being added to the pager-specific or
545** global LRU list.
546*/
danielk19779f61c2f2007-08-27 17:27:49 +0000547static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
548 pLink->pNext = 0;
549 pLink->pPrev = pList->pLast;
550
danielk197784f786f2007-08-28 08:00:17 +0000551#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
552 assert(pLink==&pPg->free || pLink==&pPg->gfree);
553 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
554#endif
555
danielk19779f61c2f2007-08-27 17:27:49 +0000556 if( pList->pLast ){
557 int iOff = (char *)pLink - (char *)pPg;
558 PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
559 pLastLink->pNext = pPg;
560 }else{
561 assert(!pList->pFirst);
562 pList->pFirst = pPg;
563 }
564
565 pList->pLast = pPg;
566 if( !pList->pFirstSynced && pPg->needSync==0 ){
567 pList->pFirstSynced = pPg;
568 }
569}
570
danielk197784f786f2007-08-28 08:00:17 +0000571/*
572** Remove pPg from the list managed by the structure pointed to by pList.
573**
574** Argument pLink should point to either pPg->free or pPg->gfree, depending
575** on whether pPg is being added to the pager-specific or global LRU list.
576*/
danielk19779f61c2f2007-08-27 17:27:49 +0000577static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
578 int iOff = (char *)pLink - (char *)pPg;
579
danielk197784f786f2007-08-28 08:00:17 +0000580#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
581 assert(pLink==&pPg->free || pLink==&pPg->gfree);
582 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
583#endif
584
danielk19779f61c2f2007-08-27 17:27:49 +0000585 if( pPg==pList->pFirst ){
586 pList->pFirst = pLink->pNext;
587 }
588 if( pPg==pList->pLast ){
589 pList->pLast = pLink->pPrev;
590 }
591 if( pLink->pPrev ){
592 PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
593 pPrevLink->pNext = pLink->pNext;
594 }
595 if( pLink->pNext ){
596 PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
597 pNextLink->pPrev = pLink->pPrev;
598 }
599 if( pPg==pList->pFirstSynced ){
600 PgHdr *p = pLink->pNext;
601 while( p && p->needSync ){
602 PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
603 p = pL->pNext;
604 }
605 pList->pFirstSynced = p;
606 }
607
608 pLink->pNext = pLink->pPrev = 0;
609}
610
611/*
danielk197784f786f2007-08-28 08:00:17 +0000612** Add page pPg to the list of free pages for the pager. If
613** memory-management is enabled, also add the page to the global
614** list of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000615*/
616static void lruListAdd(PgHdr *pPg){
617 listAdd(&pPg->pPager->lru, &pPg->free, pPg);
618#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
619 if( !pPg->pPager->memDb ){
danielk197759f8c082008-06-18 17:09:10 +0000620 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU));
danielk19779f61c2f2007-08-27 17:27:49 +0000621 listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
danielk197759f8c082008-06-18 17:09:10 +0000622 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU));
danielk19779f61c2f2007-08-27 17:27:49 +0000623 }
624#endif
625}
626
627/*
danielk197784f786f2007-08-28 08:00:17 +0000628** Remove page pPg from the list of free pages for the associated pager.
629** If memory-management is enabled, also remove pPg from the global list
630** of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000631*/
632static void lruListRemove(PgHdr *pPg){
633 listRemove(&pPg->pPager->lru, &pPg->free, pPg);
634#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
635 if( !pPg->pPager->memDb ){
danielk197759f8c082008-06-18 17:09:10 +0000636 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU));
danielk19779f61c2f2007-08-27 17:27:49 +0000637 listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
danielk197759f8c082008-06-18 17:09:10 +0000638 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU));
danielk19779f61c2f2007-08-27 17:27:49 +0000639 }
640#endif
641}
642
643/*
danielk197784f786f2007-08-28 08:00:17 +0000644** This function is called just after the needSync flag has been cleared
645** from all pages managed by pPager (usually because the journal file
646** has just been synced). It updates the pPager->lru.pFirstSynced variable
647** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
648** variable also.
danielk19779f61c2f2007-08-27 17:27:49 +0000649*/
650static void lruListSetFirstSynced(Pager *pPager){
651 pPager->lru.pFirstSynced = pPager->lru.pFirst;
652#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
653 if( !pPager->memDb ){
654 PgHdr *p;
danielk197759f8c082008-06-18 17:09:10 +0000655 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU));
danielk19779f61c2f2007-08-27 17:27:49 +0000656 for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
657 assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
658 sqlite3LruPageList.pFirstSynced = p;
danielk197759f8c082008-06-18 17:09:10 +0000659 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU));
danielk19779f61c2f2007-08-27 17:27:49 +0000660 }
661#endif
662}
663
danielk1977f35843b2007-04-07 15:03:17 +0000664/*
665** Return true if page *pPg has already been written to the statement
666** journal (or statement snapshot has been created, if *pPg is part
667** of an in-memory database).
668*/
669static int pageInStatement(PgHdr *pPg){
670 Pager *pPager = pPg->pPager;
671 if( MEMDB ){
672 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
673 }else{
drhf5e7bb52008-02-18 14:47:33 +0000674 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000675 }
676}
drh8ca0c722006-05-07 17:49:38 +0000677
678/*
679** Change the size of the pager hash table to N. N must be a power
680** of two.
681*/
682static void pager_resize_hash_table(Pager *pPager, int N){
683 PgHdr **aHash, *pPg;
684 assert( N>0 && (N&(N-1))==0 );
drheee4c8c2008-02-18 22:24:57 +0000685#ifdef SQLITE_MALLOC_SOFT_LIMIT
686 if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){
687 N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]);
688 }
689 if( N==pPager->nHash ) return;
690#endif
drhed138fb2007-08-22 22:04:37 +0000691 pagerLeave(pPager);
danielk19772d1d86f2008-06-20 14:59:51 +0000692 if( pPager->aHash!=0 ) sqlite3BeginBenignMalloc();
drh17435752007-08-16 04:30:38 +0000693 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
danielk19772d1d86f2008-06-20 14:59:51 +0000694 if( pPager->aHash!=0 ) sqlite3EndBenignMalloc();
drhed138fb2007-08-22 22:04:37 +0000695 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000696 if( aHash==0 ){
697 /* Failure to rehash is not an error. It is only a performance hit. */
698 return;
699 }
drh17435752007-08-16 04:30:38 +0000700 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000701 pPager->nHash = N;
702 pPager->aHash = aHash;
703 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000704 int h;
705 if( pPg->pgno==0 ){
706 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
707 continue;
708 }
709 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000710 pPg->pNextHash = aHash[h];
711 if( aHash[h] ){
712 aHash[h]->pPrevHash = pPg;
713 }
714 aHash[h] = pPg;
715 pPg->pPrevHash = 0;
716 }
717}
718
drhdd793422001-06-28 01:54:48 +0000719/*
drh34e79ce2004-02-08 06:05:46 +0000720** Read a 32-bit integer from the given file descriptor. Store the integer
721** that is read in *pRes. Return SQLITE_OK if everything worked, or an
722** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000723**
724** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000725*/
danielk197762079062007-08-15 17:08:46 +0000726static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000727 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000728 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000729 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000730 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000731 }
drh94f33312002-08-12 12:29:56 +0000732 return rc;
733}
734
735/*
drh97b57482006-01-10 20:32:32 +0000736** Write a 32-bit integer into a string buffer in big-endian byte order.
737*/
drha3152892007-05-05 11:48:52 +0000738#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000739
740/*
drh34e79ce2004-02-08 06:05:46 +0000741** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
742** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000743*/
danielk197762079062007-08-15 17:08:46 +0000744static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000745 char ac[4];
drh97b57482006-01-10 20:32:32 +0000746 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000747 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000748}
749
drh2554f8b2003-01-22 01:26:44 +0000750/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000751** If file pFd is open, call sqlite3OsUnlock() on it.
752*/
753static int osUnlock(sqlite3_file *pFd, int eLock){
754 if( !pFd->pMethods ){
755 return SQLITE_OK;
756 }
757 return sqlite3OsUnlock(pFd, eLock);
758}
759
760/*
danielk1977c7b60172007-08-22 11:22:03 +0000761** This function determines whether or not the atomic-write optimization
762** can be used with this pager. The optimization can be used if:
763**
764** (a) the value returned by OsDeviceCharacteristics() indicates that
765** a database page may be written atomically, and
766** (b) the value returned by OsSectorSize() is less than or equal
767** to the page size.
768**
769** If the optimization cannot be used, 0 is returned. If it can be used,
770** then the value returned is the size of the journal file when it
771** contains rollback data for exactly one page.
772*/
773#ifdef SQLITE_ENABLE_ATOMIC_WRITE
774static int jrnlBufferSize(Pager *pPager){
775 int dc; /* Device characteristics */
776 int nSector; /* Sector size */
drhfacf0302008-06-17 15:12:00 +0000777 int szPage; /* Page size */
danielk1977c7b60172007-08-22 11:22:03 +0000778 sqlite3_file *fd = pPager->fd;
779
780 if( fd->pMethods ){
781 dc = sqlite3OsDeviceCharacteristics(fd);
782 nSector = sqlite3OsSectorSize(fd);
drhfacf0302008-06-17 15:12:00 +0000783 szPage = pPager->pageSize;
danielk1977c7b60172007-08-22 11:22:03 +0000784 }
785
786 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
787 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
788
drhfacf0302008-06-17 15:12:00 +0000789 if( !fd->pMethods ||
790 (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000791 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
792 }
793 return 0;
794}
795#endif
796
797/*
danielk1977aef0bf62005-12-30 16:28:01 +0000798** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000799** code. The first argument is a pointer to the pager structure, the
800** second the error-code about to be returned by a pager API function.
801** The value returned is a copy of the second argument to this function.
802**
drh4f0ee682007-03-30 20:43:40 +0000803** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000804** the error becomes persistent. Until the persisten error is cleared,
805** subsequent API calls on this Pager will immediately return the same
806** error code.
807**
808** A persistent error indicates that the contents of the pager-cache
809** cannot be trusted. This state can be cleared by completely discarding
810** the contents of the pager-cache. If a transaction was active when
811** the persistent error occured, then the rollback journal may need
812** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000813*/
danielk1977ae72d982007-10-03 08:46:44 +0000814static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000815static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000816 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000817 assert(
818 pPager->errCode==SQLITE_FULL ||
819 pPager->errCode==SQLITE_OK ||
820 (pPager->errCode & 0xff)==SQLITE_IOERR
821 );
danielk1977979f38e2007-03-27 16:19:51 +0000822 if(
drh4ac285a2006-09-15 07:28:50 +0000823 rc2==SQLITE_FULL ||
824 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000825 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000826 ){
827 pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000828 if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
829 /* If the pager is already unlocked, call pager_unlock() now to
830 ** clear the error state and ensure that the pager-cache is
831 ** completely empty.
832 */
833 pager_unlock(pPager);
834 }
danielk1977aef0bf62005-12-30 16:28:01 +0000835 }
836 return rc;
837}
838
drh477731b2007-06-16 03:06:27 +0000839/*
840** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
841** on the cache using a hash function. This is used for testing
842** and debugging only.
843*/
danielk19773c407372005-02-15 02:54:14 +0000844#ifdef SQLITE_CHECK_PAGES
845/*
846** Return a 32-bit hash of the page data for pPage.
847*/
drh477731b2007-06-16 03:06:27 +0000848static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000849 u32 hash = 0;
850 int i;
drh477731b2007-06-16 03:06:27 +0000851 for(i=0; i<nByte; i++){
852 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000853 }
854 return hash;
855}
drh477731b2007-06-16 03:06:27 +0000856static u32 pager_pagehash(PgHdr *pPage){
857 return pager_datahash(pPage->pPager->pageSize,
858 (unsigned char *)PGHDR_TO_DATA(pPage));
859}
danielk19773c407372005-02-15 02:54:14 +0000860
861/*
862** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
863** is defined, and NDEBUG is not defined, an assert() statement checks
864** that the page is either dirty or still matches the calculated page-hash.
865*/
866#define CHECK_PAGE(x) checkPage(x)
867static void checkPage(PgHdr *pPg){
868 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000869 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000870 pPg->pageHash==pager_pagehash(pPg) );
871}
872
873#else
drh8ffa8172007-06-18 17:25:17 +0000874#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000875#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000876#define CHECK_PAGE(x)
877#endif
878
drhed7c8552001-04-11 14:29:21 +0000879/*
danielk197776572402004-06-25 02:38:54 +0000880** When this is called the journal file for pager pPager must be open.
881** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000882** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000883**
danielk197765839c62007-08-30 08:08:17 +0000884** zMaster must point to a buffer of at least nMaster bytes allocated by
885** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
886** enough space to write the master journal name). If the master journal
887** name in the journal is longer than nMaster bytes (including a
888** nul-terminator), then this is handled as if no master journal name
889** were present in the journal.
890**
891** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000892** SQLITE_OK returned.
893*/
danielk197765839c62007-08-30 08:08:17 +0000894static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000895 int rc;
896 u32 len;
drheb206252004-10-01 02:00:31 +0000897 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000898 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000899 int i;
danielk197776572402004-06-25 02:38:54 +0000900 unsigned char aMagic[8]; /* A buffer to hold the magic header */
901
danielk197765839c62007-08-30 08:08:17 +0000902 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000903
drh054889e2005-11-30 03:20:31 +0000904 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000905 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000906
danielk197762079062007-08-15 17:08:46 +0000907 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000908 if( rc!=SQLITE_OK ) return rc;
909
danielk197765839c62007-08-30 08:08:17 +0000910 if( len>=nMaster ){
911 return SQLITE_OK;
912 }
913
danielk197762079062007-08-15 17:08:46 +0000914 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000915 if( rc!=SQLITE_OK ) return rc;
916
danielk197762079062007-08-15 17:08:46 +0000917 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000918 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
919
danielk197765839c62007-08-30 08:08:17 +0000920 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000921 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000922 return rc;
923 }
danielk197765839c62007-08-30 08:08:17 +0000924 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000925
926 /* See if the checksum matches the master journal name */
927 for(i=0; i<len; i++){
danielk197765839c62007-08-30 08:08:17 +0000928 cksum -= zMaster[i];
929 }
danielk19778191bff2004-06-28 04:52:30 +0000930 if( cksum ){
931 /* If the checksum doesn't add up, then one or more of the disk sectors
932 ** containing the master journal filename is corrupted. This means
933 ** definitely roll back, so just return SQLITE_OK and report a (nul)
934 ** master-journal filename.
935 */
danielk197765839c62007-08-30 08:08:17 +0000936 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000937 }
danielk197776572402004-06-25 02:38:54 +0000938
939 return SQLITE_OK;
940}
941
942/*
943** Seek the journal file descriptor to the next sector boundary where a
944** journal header may be read or written. Pager.journalOff is updated with
945** the new seek offset.
946**
947** i.e for a sector size of 512:
948**
949** Input Offset Output Offset
950** ---------------------------------------
951** 0 0
952** 512 512
953** 100 512
954** 2000 2048
955**
956*/
danielk197762079062007-08-15 17:08:46 +0000957static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000958 i64 offset = 0;
959 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000960 if( c ){
961 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
962 }
963 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
964 assert( offset>=c );
965 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
966 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000967}
968
969/*
drhf3a87622008-04-17 14:16:42 +0000970** Write zeros over the header of the journal file. This has the
971** effect of invalidating the journal file and committing the
972** transaction.
973*/
danielk1977df2566a2008-05-07 19:11:03 +0000974static int zeroJournalHdr(Pager *pPager, int doTruncate){
975 int rc = SQLITE_OK;
drhf3a87622008-04-17 14:16:42 +0000976 static const char zeroHdr[28];
977
danielk1977df2566a2008-05-07 19:11:03 +0000978 if( pPager->journalOff ){
danielk1977b53e4962008-06-04 06:45:59 +0000979 i64 iLimit = pPager->journalSizeLimit;
980
danielk1977df2566a2008-05-07 19:11:03 +0000981 IOTRACE(("JZEROHDR %p\n", pPager))
danielk1977b53e4962008-06-04 06:45:59 +0000982 if( doTruncate || iLimit==0 ){
danielk1977df2566a2008-05-07 19:11:03 +0000983 rc = sqlite3OsTruncate(pPager->jfd, 0);
984 }else{
985 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
986 }
danielk197781620542008-06-07 05:19:37 +0000987 if( rc==SQLITE_OK && !pPager->noSync ){
danielk1977df2566a2008-05-07 19:11:03 +0000988 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
989 }
danielk1977b53e4962008-06-04 06:45:59 +0000990
991 /* At this point the transaction is committed but the write lock
992 ** is still held on the file. If there is a size limit configured for
993 ** the persistent journal and the journal file currently consumes more
994 ** space than that limit allows for, truncate it now. There is no need
995 ** to sync the file following this operation.
996 */
997 if( rc==SQLITE_OK && iLimit>0 ){
998 i64 sz;
999 rc = sqlite3OsFileSize(pPager->jfd, &sz);
1000 if( rc==SQLITE_OK && sz>iLimit ){
1001 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
1002 }
1003 }
drha06ecba2008-04-22 17:15:17 +00001004 }
drhf3a87622008-04-17 14:16:42 +00001005 return rc;
1006}
1007
1008/*
danielk197776572402004-06-25 02:38:54 +00001009** The journal file must be open when this routine is called. A journal
1010** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
1011** current location.
1012**
1013** The format for the journal header is as follows:
1014** - 8 bytes: Magic identifying journal format.
1015** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
1016** - 4 bytes: Random number used for page hash.
1017** - 4 bytes: Initial database page count.
1018** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +00001019** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +00001020**
danielk197767c007b2008-03-20 04:45:49 +00001021** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +00001022*/
1023static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +00001024 int rc = SQLITE_OK;
1025 char *zHeader = pPager->pTmpSpace;
1026 int nHeader = pPager->pageSize;
1027 int nWrite;
1028
1029 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
1030 nHeader = JOURNAL_HDR_SZ(pPager);
1031 }
danielk197776572402004-06-25 02:38:54 +00001032
danielk19774099f6e2007-03-19 11:25:20 +00001033 if( pPager->stmtHdrOff==0 ){
1034 pPager->stmtHdrOff = pPager->journalOff;
1035 }
1036
danielk197762079062007-08-15 17:08:46 +00001037 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001038 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001039
drh97b57482006-01-10 20:32:32 +00001040 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +00001041
1042 /*
1043 ** Write the nRec Field - the number of page records that follow this
1044 ** journal header. Normally, zero is written to this value at this time.
1045 ** After the records are added to the journal (and the journal synced,
1046 ** if in full-sync mode), the zero is overwritten with the true number
1047 ** of records (see syncJournal()).
1048 **
1049 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
1050 ** reading the journal this value tells SQLite to assume that the
1051 ** rest of the journal file contains valid page records. This assumption
1052 ** is dangerous, as if a failure occured whilst writing to the journal
1053 ** file it may contain some garbage data. There are two scenarios
1054 ** where this risk can be ignored:
1055 **
1056 ** * When the pager is in no-sync mode. Corruption can follow a
1057 ** power failure in this case anyway.
1058 **
1059 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1060 ** that garbage data is never appended to the journal file.
1061 */
1062 assert(pPager->fd->pMethods||pPager->noSync);
1063 if( (pPager->noSync)
1064 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1065 ){
1066 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1067 }else{
1068 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1069 }
1070
drh97b57482006-01-10 20:32:32 +00001071 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +00001072 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +00001073 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1074 /* The initial database size */
1075 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1076 /* The assumed sector size for this process */
1077 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
danielk197767c007b2008-03-20 04:45:49 +00001078 if( pPager->journalHdr==0 ){
1079 /* The page size */
1080 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
1081 }
danielk197776572402004-06-25 02:38:54 +00001082
danielk1977a664f8e2008-04-22 14:31:48 +00001083 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
1084 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
1085 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
1086 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +00001087 }
danielk1977a664f8e2008-04-22 14:31:48 +00001088
danielk197776572402004-06-25 02:38:54 +00001089 return rc;
1090}
1091
1092/*
1093** The journal file must be open when this is called. A journal header file
1094** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1095** file. See comments above function writeJournalHdr() for a description of
1096** the journal header format.
1097**
1098** If the header is read successfully, *nRec is set to the number of
1099** page records following this header and *dbSize is set to the size of the
1100** database before the transaction began, in pages. Also, pPager->cksumInit
1101** is set to the value read from the journal header. SQLITE_OK is returned
1102** in this case.
1103**
1104** If the journal header file appears to be corrupted, SQLITE_DONE is
1105** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1106** cannot be read from the journal file an error code is returned.
1107*/
1108static int readJournalHdr(
1109 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001110 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001111 u32 *pNRec,
1112 u32 *pDbSize
1113){
1114 int rc;
1115 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001116 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +00001117 int iPageSize;
danielk197776572402004-06-25 02:38:54 +00001118
danielk197762079062007-08-15 17:08:46 +00001119 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001120 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1121 return SQLITE_DONE;
1122 }
danielk197762079062007-08-15 17:08:46 +00001123 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001124
danielk197762079062007-08-15 17:08:46 +00001125 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001126 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001127 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001128
1129 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1130 return SQLITE_DONE;
1131 }
1132
danielk197762079062007-08-15 17:08:46 +00001133 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001134 if( rc ) return rc;
1135
danielk197762079062007-08-15 17:08:46 +00001136 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001137 if( rc ) return rc;
1138
danielk197762079062007-08-15 17:08:46 +00001139 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001140 if( rc ) return rc;
1141
danielk197767c007b2008-03-20 04:45:49 +00001142 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
1143 if( rc==SQLITE_OK
1144 && iPageSize>=512
1145 && iPageSize<=SQLITE_MAX_PAGE_SIZE
1146 && ((iPageSize-1)&iPageSize)==0
1147 ){
1148 u16 pagesize = iPageSize;
1149 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
1150 }
1151 if( rc ) return rc;
1152
danielk197776572402004-06-25 02:38:54 +00001153 /* Update the assumed sector-size to match the value used by
1154 ** the process that created this journal. If this journal was
1155 ** created by a process other than this one, then this routine
1156 ** is being called from within pager_playback(). The local value
1157 ** of Pager.sectorSize is restored at the end of that routine.
1158 */
danielk197762079062007-08-15 17:08:46 +00001159 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001160 if( rc ) return rc;
1161
1162 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001163 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001164}
1165
1166
1167/*
1168** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001169** pPager at the current location. The master journal name must be the last
1170** thing written to a journal file. If the pager is in full-sync mode, the
1171** journal file descriptor is advanced to the next sector boundary before
1172** anything is written. The format is:
1173**
1174** + 4 bytes: PAGER_MJ_PGNO.
1175** + N bytes: length of master journal name.
1176** + 4 bytes: N
1177** + 4 bytes: Master journal name checksum.
1178** + 8 bytes: aJournalMagic[].
1179**
1180** The master journal page checksum is the sum of the bytes in the master
1181** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001182**
1183** If zMaster is a NULL pointer (occurs for a single database transaction),
1184** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001185*/
1186static int writeMasterJournal(Pager *pPager, const char *zMaster){
1187 int rc;
1188 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001189 int i;
danielk197762079062007-08-15 17:08:46 +00001190 i64 jrnlOff;
danielk1977df2566a2008-05-07 19:11:03 +00001191 i64 jrnlSize;
drh97b57482006-01-10 20:32:32 +00001192 u32 cksum = 0;
1193 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001194
1195 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1196 pPager->setMaster = 1;
1197
1198 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001199 for(i=0; i<len; i++){
1200 cksum += zMaster[i];
1201 }
danielk197776572402004-06-25 02:38:54 +00001202
1203 /* If in full-sync mode, advance to the next disk sector before writing
1204 ** the master journal name. This is in case the previous page written to
1205 ** the journal has already been synced.
1206 */
1207 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001208 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001209 }
danielk197762079062007-08-15 17:08:46 +00001210 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001211 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001212
danielk197762079062007-08-15 17:08:46 +00001213 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001214 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001215 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001216
danielk197762079062007-08-15 17:08:46 +00001217 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001218 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001219 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001220
drh97b57482006-01-10 20:32:32 +00001221 put32bits(zBuf, len);
1222 put32bits(&zBuf[4], cksum);
1223 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001224 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +00001225 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +00001226 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +00001227
1228 /* If the pager is in peristent-journal mode, then the physical
1229 ** journal-file may extend past the end of the master-journal name
1230 ** and 8 bytes of magic data just written to the file. This is
1231 ** dangerous because the code to rollback a hot-journal file
1232 ** will not be able to find the master-journal name to determine
1233 ** whether or not the journal is hot.
1234 **
1235 ** Easiest thing to do in this scenario is to truncate the journal
1236 ** file to the required size.
1237 */
1238 if( (rc==SQLITE_OK)
1239 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
1240 && jrnlSize>jrnlOff
1241 ){
1242 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
1243 }
danielk197776572402004-06-25 02:38:54 +00001244 return rc;
1245}
1246
1247/*
drh03eb96a2002-11-10 23:32:56 +00001248** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001249** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001250**
1251** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001252** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001253** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001254** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001255*/
drh3aac2dd2004-04-26 14:10:20 +00001256static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001257 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001258 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1259 assert( MEMDB );
1260 if( !pHist->inStmt ){
1261 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1262 if( pPager->pStmt ){
1263 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1264 }
1265 pHist->pNextStmt = pPager->pStmt;
1266 pPager->pStmt = pPg;
1267 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001268 }
drh03eb96a2002-11-10 23:32:56 +00001269}
1270
1271/*
drhed7c8552001-04-11 14:29:21 +00001272** Find a page in the hash table given its page number. Return
1273** a pointer to the page or NULL if not found.
1274*/
drhd9b02572001-04-15 00:37:09 +00001275static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001276 PgHdr *p;
1277 if( pPager->aHash==0 ) return 0;
1278 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001279 while( p && p->pgno!=pgno ){
1280 p = p->pNextHash;
1281 }
1282 return p;
1283}
1284
1285/*
danielk1977e180dd92007-04-05 17:15:52 +00001286** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001287** sets the state of the pager back to what it was when it was first
1288** opened. Any outstanding pages are invalidated and subsequent attempts
1289** to access those pages will likely result in a coredump.
1290*/
drhd9b02572001-04-15 00:37:09 +00001291static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001292 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001293 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001294 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001295 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1296 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001297 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001298 lruListRemove(pPg);
drhfacf0302008-06-17 15:12:00 +00001299 sqlite3PageFree(pPg->pData);
drh17435752007-08-16 04:30:38 +00001300 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001301 }
danielk19779f61c2f2007-08-27 17:27:49 +00001302 assert(pPager->lru.pFirst==0);
1303 assert(pPager->lru.pFirstSynced==0);
1304 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001305 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001306 pPager->pAll = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001307 pPager->pDirty = 0;
drh8ca0c722006-05-07 17:49:38 +00001308 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001309 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001310 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001311 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001312 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001313}
1314
1315/*
danielk1977ae72d982007-10-03 08:46:44 +00001316** Unlock the database file.
1317**
1318** If the pager is currently in error state, discard the contents of
1319** the cache and reset the Pager structure internal state. If there is
1320** an open journal-file, then the next time a shared-lock is obtained
1321** on the pager file (by this or any other process), it will be
1322** treated as a hot-journal and rolled back.
1323*/
1324static void pager_unlock(Pager *pPager){
1325 if( !pPager->exclusiveMode ){
1326 if( !MEMDB ){
drh1aa5af12008-03-07 19:51:14 +00001327 int rc = osUnlock(pPager->fd, NO_LOCK);
drh308aa322008-03-07 20:14:38 +00001328 if( rc ) pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +00001329 pPager->dbSize = -1;
1330 IOTRACE(("UNLOCK %p\n", pPager))
1331
drh16e45a42008-04-19 20:34:18 +00001332 /* Always close the journal file when dropping the database lock.
1333 ** Otherwise, another connection with journal_mode=delete might
1334 ** delete the file out from under us.
1335 */
1336 if( pPager->journalOpen ){
1337 sqlite3OsClose(pPager->jfd);
1338 pPager->journalOpen = 0;
1339 sqlite3BitvecDestroy(pPager->pInJournal);
1340 pPager->pInJournal = 0;
1341 }
1342
danielk1977ae72d982007-10-03 08:46:44 +00001343 /* If Pager.errCode is set, the contents of the pager cache cannot be
1344 ** trusted. Now that the pager file is unlocked, the contents of the
1345 ** cache can be discarded and the error code safely cleared.
1346 */
1347 if( pPager->errCode ){
drh1aa5af12008-03-07 19:51:14 +00001348 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00001349 pager_reset(pPager);
1350 if( pPager->stmtOpen ){
1351 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +00001352 sqlite3BitvecDestroy(pPager->pInStmt);
1353 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001354 }
danielk1977ae72d982007-10-03 08:46:44 +00001355 pPager->stmtOpen = 0;
1356 pPager->stmtInUse = 0;
1357 pPager->journalOff = 0;
1358 pPager->journalStarted = 0;
1359 pPager->stmtAutoopen = 0;
1360 pPager->origDbSize = 0;
1361 }
1362 }
1363
1364 if( !MEMDB || pPager->errCode==SQLITE_OK ){
1365 pPager->state = PAGER_UNLOCK;
1366 pPager->changeCountDone = 0;
1367 }
1368 }
1369}
1370
1371/*
1372** Execute a rollback if a transaction is active and unlock the
1373** database file. If the pager has already entered the error state,
1374** do not attempt the rollback.
1375*/
1376static void pagerUnlockAndRollback(Pager *p){
drhfdc40e92008-04-17 20:59:37 +00001377 /* assert( p->state>=PAGER_RESERVED || p->journalOpen==0 ); */
danielk1977ae72d982007-10-03 08:46:44 +00001378 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
danielk19772d1d86f2008-06-20 14:59:51 +00001379 sqlite3BeginBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001380 sqlite3PagerRollback(p);
danielk19772d1d86f2008-06-20 14:59:51 +00001381 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001382 }
1383 pager_unlock(p);
drhfdc40e92008-04-17 20:59:37 +00001384#if 0
danielk1977ae72d982007-10-03 08:46:44 +00001385 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1386 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drhfdc40e92008-04-17 20:59:37 +00001387#endif
danielk1977ae72d982007-10-03 08:46:44 +00001388}
1389
1390/*
drh80e35f42007-03-30 14:06:34 +00001391** This routine ends a transaction. A transaction is ended by either
1392** a COMMIT or a ROLLBACK.
1393**
drhed7c8552001-04-11 14:29:21 +00001394** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001395** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1396** the database lock and acquires a SHARED lock in its place if that is
1397** the appropriate thing to do. Release locks usually is appropriate,
1398** unless we are in exclusive access mode or unless this is a
1399** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1400**
1401** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001402**
1403** TODO: Consider keeping the journal file open for temporary databases.
1404** This might give a performance improvement on windows where opening
1405** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001406*/
danielk1977df2566a2008-05-07 19:11:03 +00001407static int pager_end_transaction(Pager *pPager, int hasMaster){
drhd9b02572001-04-15 00:37:09 +00001408 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001409 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001410 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001411 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001412 if( pPager->state<PAGER_RESERVED ){
1413 return SQLITE_OK;
1414 }
danielk19773b8a05f2007-03-19 17:44:26 +00001415 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001416 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001417 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001418 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001419 }
drhda47d772002-12-02 04:25:19 +00001420 if( pPager->journalOpen ){
danielk197793f7af92008-05-09 16:57:50 +00001421 if( pPager->exclusiveMode
1422 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1423 ){
1424 rc = zeroJournalHdr(pPager, hasMaster);
1425 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +00001426 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001427 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001428 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001429 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001430 pPager->journalOpen = 0;
danielk19770f01fda2008-06-06 16:14:02 +00001431 if( rc==SQLITE_OK && !pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00001432 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001433 }
danielk197741483462007-03-24 16:45:04 +00001434 }
drhf5e7bb52008-02-18 14:47:33 +00001435 sqlite3BitvecDestroy(pPager->pInJournal);
1436 pPager->pInJournal = 0;
drhda47d772002-12-02 04:25:19 +00001437 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1438 pPg->inJournal = 0;
1439 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001440 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001441 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001442#ifdef SQLITE_CHECK_PAGES
1443 pPg->pageHash = pager_pagehash(pPg);
1444#endif
drhda47d772002-12-02 04:25:19 +00001445 }
drh605ad8f2006-05-03 23:34:05 +00001446 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001447 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001448 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001449 }else{
drhf5e7bb52008-02-18 14:47:33 +00001450 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001451 }
danielk1977979f38e2007-03-27 16:19:51 +00001452
danielk197741483462007-03-24 16:45:04 +00001453 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001454 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001455 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001456 }else if( pPager->state==PAGER_SYNCED ){
1457 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001458 }
danielk1977334cdb62007-03-26 08:05:12 +00001459 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001460 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001461 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001462 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001463 pPager->dbSize = -1;
drhd138c012008-05-13 00:58:18 +00001464 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001465
1466 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001467}
1468
drhed7c8552001-04-11 14:29:21 +00001469/*
drh968af522003-02-11 14:55:40 +00001470** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001471**
1472** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001473** random initial value and the page number. We experimented with
1474** a checksum of the entire data, but that was found to be too slow.
1475**
1476** Note that the page number is stored at the beginning of data and
1477** the checksum is stored at the end. This is important. If journal
1478** corruption occurs due to a power failure, the most likely scenario
1479** is that one end or the other of the record will be changed. It is
1480** much less likely that the two ends of the journal record will be
1481** correct and the middle be corrupt. Thus, this "checksum" scheme,
1482** though fast and simple, catches the mostly likely kind of corruption.
1483**
1484** FIX ME: Consider adding every 200th (or so) byte of the data to the
1485** checksum. That way if a single page spans 3 or more disk sectors and
1486** only the middle sector is corrupt, we will still have a reasonable
1487** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001488*/
drh74161702006-02-24 02:53:49 +00001489static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001490 u32 cksum = pPager->cksumInit;
1491 int i = pPager->pageSize-200;
1492 while( i>0 ){
1493 cksum += aData[i];
1494 i -= 200;
1495 }
drh968af522003-02-11 14:55:40 +00001496 return cksum;
1497}
1498
drh605ad8f2006-05-03 23:34:05 +00001499/* Forward declaration */
1500static void makeClean(PgHdr*);
1501
drh968af522003-02-11 14:55:40 +00001502/*
drhfa86c412002-02-02 15:01:15 +00001503** Read a single page from the journal file opened on file descriptor
1504** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001505**
drh726de592004-06-10 23:35:50 +00001506** If useCksum==0 it means this journal does not use checksums. Checksums
1507** are not used in statement journals because statement journals do not
1508** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001509*/
danielk197762079062007-08-15 17:08:46 +00001510static int pager_playback_one_page(
1511 Pager *pPager,
1512 sqlite3_file *jfd,
1513 i64 offset,
1514 int useCksum
1515){
drhfa86c412002-02-02 15:01:15 +00001516 int rc;
drhae2b40c2004-06-09 19:03:54 +00001517 PgHdr *pPg; /* An existing page in the cache */
1518 Pgno pgno; /* The page number of a page in journal */
1519 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001520 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001521
drh96362842005-03-20 22:47:56 +00001522 /* useCksum should be true for the main journal and false for
1523 ** statement journals. Verify that this is always the case
1524 */
drh9cbe6352005-11-29 03:13:21 +00001525 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001526 assert( aData );
drh96362842005-03-20 22:47:56 +00001527
danielk197762079062007-08-15 17:08:46 +00001528 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001529 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001530 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001531 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001532 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001533
drh968af522003-02-11 14:55:40 +00001534 /* Sanity checking on the page. This is more important that I originally
1535 ** thought. If a power failure occurs while the journal is being written,
1536 ** it could cause invalid data to be written into the journal. We need to
1537 ** detect this invalid data (with high probability) and ignore it.
1538 */
danielk197775edc162004-06-26 01:48:18 +00001539 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001540 return SQLITE_DONE;
1541 }
drhae2b40c2004-06-09 19:03:54 +00001542 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001543 return SQLITE_OK;
1544 }
drhae2b40c2004-06-09 19:03:54 +00001545 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001546 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001547 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001548 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001549 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001550 return SQLITE_DONE;
1551 }
1552 }
drhfa86c412002-02-02 15:01:15 +00001553
danielk1977aa5ccdf2004-06-14 05:10:42 +00001554 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001555
1556 /* If the pager is in RESERVED state, then there must be a copy of this
1557 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001558 ** not the database file. The page is left marked dirty in this case.
1559 **
danielk19772df71c72007-05-24 07:22:42 +00001560 ** An exception to the above rule: If the database is in no-sync mode
1561 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001562 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1563 ** during a Movepage() call, then the page may not be in the cache
1564 ** either. So the condition described in the above paragraph is not
1565 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001566 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001567 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1568 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001569 **
1570 ** Ticket #1171: The statement journal might contain page content that is
1571 ** different from the page content at the start of the transaction.
1572 ** This occurs when a page is changed prior to the start of a statement
1573 ** then changed again within the statement. When rolling back such a
1574 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001575 ** for certain that original page contents are synced into the main rollback
1576 ** journal. Otherwise, a power loss might leave modified data in the
1577 ** database file without an entry in the rollback journal that can
1578 ** restore the database to its original form. Two conditions must be
1579 ** met before writing to the database files. (1) the database must be
1580 ** locked. (2) we know that the original page content is fully synced
1581 ** in the main journal either because the page is not in cache or else
1582 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001583 **
1584 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1585 ** is possible to fail a statement on a database that does not yet exist.
1586 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001587 */
drhae2b40c2004-06-09 19:03:54 +00001588 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001589 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1590 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh4c02a232008-04-14 23:13:45 +00001591 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0)
1592 && pPager->fd->pMethods ){
danielk197762079062007-08-15 17:08:46 +00001593 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1594 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001595 if( pPg ){
1596 makeClean(pPg);
1597 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001598 }
drhfa86c412002-02-02 15:01:15 +00001599 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001600 /* No page should ever be explicitly rolled back that is in use, except
1601 ** for page 1 which is held in use in order to keep the lock on the
1602 ** database active. However such a page may be rolled back as a result
1603 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001604 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001605 */
drhb6f41482004-05-14 01:58:11 +00001606 void *pData;
danielk197728129562005-01-11 10:25:06 +00001607 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001608 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001609 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001610 if( pPager->xReiniter ){
1611 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001612 }
danielk19773c407372005-02-15 02:54:14 +00001613#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001614 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001615#endif
drh86a88112007-04-16 15:02:19 +00001616 /* If this was page 1, then restore the value of Pager.dbFileVers.
1617 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001618 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001619 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001620 }
drh86a88112007-04-16 15:02:19 +00001621
1622 /* Decode the page just read from disk */
1623 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001624 }
1625 return rc;
1626}
1627
1628/*
danielk197713adf8a2004-06-03 16:08:41 +00001629** Parameter zMaster is the name of a master journal file. A single journal
1630** file that referred to the master journal file has just been rolled back.
1631** This routine checks if it is possible to delete the master journal file,
1632** and does so if it is.
drh726de592004-06-10 23:35:50 +00001633**
danielk197765839c62007-08-30 08:08:17 +00001634** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1635** available for use within this function.
1636**
1637**
drh726de592004-06-10 23:35:50 +00001638** The master journal file contains the names of all child journals.
1639** To tell if a master journal can be deleted, check to each of the
1640** children. If all children are either missing or do not refer to
1641** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001642*/
danielk1977b4b47412007-08-17 15:53:36 +00001643static int pager_delmaster(Pager *pPager, const char *zMaster){
1644 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001645 int rc;
1646 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001647 sqlite3_file *pMaster;
1648 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001649 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001650 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001651
1652 /* Open the master journal file exclusively in case some other process
1653 ** is running this routine also. Not that it makes too much difference.
1654 */
drhe5ae5732008-06-15 02:51:47 +00001655 pMaster = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001656 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001657 if( !pMaster ){
1658 rc = SQLITE_NOMEM;
1659 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001660 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1661 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001662 }
danielk197713adf8a2004-06-03 16:08:41 +00001663 if( rc!=SQLITE_OK ) goto delmaster_out;
1664 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001665
1666 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001667 if( rc!=SQLITE_OK ) goto delmaster_out;
1668
1669 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001670 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001671 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001672 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001673
1674 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001675 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001676 */
drhe5ae5732008-06-15 02:51:47 +00001677 zMasterJournal = (char *)sqlite3Malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001678 if( !zMasterJournal ){
1679 rc = SQLITE_NOMEM;
1680 goto delmaster_out;
1681 }
danielk197765839c62007-08-30 08:08:17 +00001682 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001683 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001684 if( rc!=SQLITE_OK ) goto delmaster_out;
1685
danielk19775865e3d2004-06-14 06:03:57 +00001686 zJournal = zMasterJournal;
1687 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977861f7452008-06-05 11:39:11 +00001688 int exists;
1689 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
1690 if( rc!=SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001691 goto delmaster_out;
1692 }
danielk1977861f7452008-06-05 11:39:11 +00001693 if( exists ){
danielk197713adf8a2004-06-03 16:08:41 +00001694 /* One of the journals pointed to by the master journal exists.
1695 ** Open it and check if it points at the master journal. If
1696 ** so, return without deleting the master journal file.
1697 */
drh3b7b78b2004-11-24 01:16:43 +00001698 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001699 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1700 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001701 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001702 goto delmaster_out;
1703 }
danielk197713adf8a2004-06-03 16:08:41 +00001704
danielk197765839c62007-08-30 08:08:17 +00001705 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001706 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001707 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001708 goto delmaster_out;
1709 }
danielk197776572402004-06-25 02:38:54 +00001710
danielk197765839c62007-08-30 08:08:17 +00001711 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001712 if( c ){
danielk197776572402004-06-25 02:38:54 +00001713 /* We have a match. Do not delete the master journal file. */
1714 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001715 }
1716 }
danielk19775865e3d2004-06-14 06:03:57 +00001717 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001718 }
1719 }
1720
danielk1977fee2d252007-08-18 10:59:19 +00001721 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001722
1723delmaster_out:
1724 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001725 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001726 }
1727 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001728 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001729 }
danielk1977b4b47412007-08-17 15:53:36 +00001730 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001731 return rc;
1732}
1733
drha6abd042004-06-09 17:37:22 +00001734
danielk1977e180dd92007-04-05 17:15:52 +00001735static void pager_truncate_cache(Pager *pPager);
1736
drha6abd042004-06-09 17:37:22 +00001737/*
drhcb4c40b2004-08-18 19:09:43 +00001738** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001739** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001740**
1741** Might might be the case that the file on disk is smaller than nPage.
1742** This can happen, for example, if we are in the middle of a transaction
1743** which has extended the file size and the new pages are still all held
1744** in cache, then an INSERT or UPDATE does a statement rollback. Some
1745** operating system implementations can get confused if you try to
1746** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001747** so detect this case and write a single zero byte to the end of the new
1748** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001749*/
1750static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001751 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001752 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001753 i64 currentSize, newSize;
1754 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1755 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001756 if( rc==SQLITE_OK && currentSize!=newSize ){
1757 if( currentSize>newSize ){
1758 rc = sqlite3OsTruncate(pPager->fd, newSize);
1759 }else{
1760 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1761 }
drh7fe3f7e2007-11-29 18:44:27 +00001762 }
danielk1977e180dd92007-04-05 17:15:52 +00001763 }
1764 if( rc==SQLITE_OK ){
1765 pPager->dbSize = nPage;
1766 pager_truncate_cache(pPager);
1767 }
1768 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001769}
1770
1771/*
drhc80f0582007-05-01 16:59:48 +00001772** Set the sectorSize for the given pager.
1773**
drh334c80d2008-03-28 17:41:13 +00001774** The sector size is at least as big as the sector size reported
1775** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001776*/
1777static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001778 assert(pPager->fd->pMethods||pPager->tempFile);
1779 if( !pPager->tempFile ){
1780 /* Sector size doesn't matter for temporary files. Also, the file
1781 ** may not have been opened yet, in whcih case the OsSectorSize()
1782 ** call will segfault.
1783 */
1784 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1785 }
drh334c80d2008-03-28 17:41:13 +00001786 if( pPager->sectorSize<512 ){
1787 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001788 }
1789}
1790
1791/*
drhed7c8552001-04-11 14:29:21 +00001792** Playback the journal and thus restore the database file to
1793** the state it was in before we started making changes.
1794**
drh34e79ce2004-02-08 06:05:46 +00001795** The journal file format is as follows:
1796**
drhae2b40c2004-06-09 19:03:54 +00001797** (1) 8 byte prefix. A copy of aJournalMagic[].
1798** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001799** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001800** number of page records from the journal size.
1801** (3) 4 byte big-endian integer which is the initial value for the
1802** sanity checksum.
1803** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001804** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001805** (5) 4 byte big-endian integer which is the sector size. The header
1806** is this many bytes in size.
1807** (6) 4 byte big-endian integer which is the page case.
1808** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001809** name. The value may be zero (indicate that there is no master
1810** journal.)
drh334c80d2008-03-28 17:41:13 +00001811** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001812** and might be shorter than the value read from (5). If the first byte
1813** of the name is \000 then there is no master journal. The master
1814** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001815** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001816** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001817** + pPager->pageSize bytes of data.
1818** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001819**
drh334c80d2008-03-28 17:41:13 +00001820** When we speak of the journal header, we mean the first 8 items above.
1821** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001822**
1823** Call the value from the second bullet "nRec". nRec is the number of
1824** valid page entries in the journal. In most cases, you can compute the
1825** value of nRec from the size of the journal file. But if a power
1826** failure occurred while the journal was being written, it could be the
1827** case that the size of the journal file had already been increased but
1828** the extra entries had not yet made it safely to disk. In such a case,
1829** the value of nRec computed from the file size would be too large. For
1830** that reason, we always use the nRec value in the header.
1831**
1832** If the nRec value is 0xffffffff it means that nRec should be computed
1833** from the file size. This value is used when the user selects the
1834** no-sync option for the journal. A power failure could lead to corruption
1835** in this case. But for things like temporary table (which will be
1836** deleted when the power is restored) we don't care.
1837**
drhd9b02572001-04-15 00:37:09 +00001838** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001839** journal file then all pages up to the first corrupted page are rolled
1840** back (or no pages if the journal header is corrupted). The journal file
1841** is then deleted and SQLITE_OK returned, just as if no corruption had
1842** been encountered.
1843**
1844** If an I/O or malloc() error occurs, the journal-file is not deleted
1845** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001846*/
danielk1977e277be02007-03-23 18:12:06 +00001847static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001848 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001849 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001850 u32 nRec; /* Number of Records in the journal */
drh93a960a2008-07-10 00:32:42 +00001851 u32 i; /* Loop counter */
drhd9b02572001-04-15 00:37:09 +00001852 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001853 int rc; /* Result code of a subroutine */
danielk1977861f7452008-06-05 11:39:11 +00001854 int res = 1; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001855 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001856
drhc3a64ba2001-11-22 00:01:27 +00001857 /* Figure out how many records are in the journal. Abort early if
1858 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001859 */
drh8cfbf082001-09-19 13:22:39 +00001860 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001861 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001862 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001863 goto end_playback;
1864 }
drh240c5792004-02-08 00:40:52 +00001865
danielk197776572402004-06-25 02:38:54 +00001866 /* Read the master journal name from the journal, if it is present.
1867 ** If a master journal file name is specified, but the file is not
1868 ** present on disk, then the journal is not hot and does not need to be
1869 ** played back.
drh240c5792004-02-08 00:40:52 +00001870 */
danielk197765839c62007-08-30 08:08:17 +00001871 zMaster = pPager->pTmpSpace;
1872 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977861f7452008-06-05 11:39:11 +00001873 if( rc==SQLITE_OK && zMaster[0] ){
1874 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
drhc3a64ba2001-11-22 00:01:27 +00001875 }
danielk197765839c62007-08-30 08:08:17 +00001876 zMaster = 0;
danielk1977861f7452008-06-05 11:39:11 +00001877 if( rc!=SQLITE_OK || !res ){
danielk1977ce98bba2008-04-03 10:13:01 +00001878 goto end_playback;
1879 }
1880 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001881
danielk197776572402004-06-25 02:38:54 +00001882 /* This loop terminates either when the readJournalHdr() call returns
1883 ** SQLITE_DONE or an IO error occurs. */
1884 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001885
danielk197776572402004-06-25 02:38:54 +00001886 /* Read the next journal header from the journal file. If there are
1887 ** not enough bytes left in the journal file for a complete header, or
1888 ** it is corrupted, then a process must of failed while writing it.
1889 ** This indicates nothing more needs to be rolled back.
1890 */
1891 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1892 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001893 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001894 rc = SQLITE_OK;
1895 }
danielk197776572402004-06-25 02:38:54 +00001896 goto end_playback;
1897 }
1898
1899 /* If nRec is 0xffffffff, then this journal was created by a process
1900 ** working in no-sync mode. This means that the rest of the journal
1901 ** file consists of pages, there are no more journal headers. Compute
1902 ** the value of nRec based on this assumption.
1903 */
1904 if( nRec==0xffffffff ){
1905 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1906 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1907 }
1908
danielk1977e277be02007-03-23 18:12:06 +00001909 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001910 ** process and if this is the final header in the journal, then it means
1911 ** that this part of the journal was being filled but has not yet been
1912 ** synced to disk. Compute the number of pages based on the remaining
1913 ** size of the file.
1914 **
1915 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001916 */
drh8940f4e2007-08-11 00:26:20 +00001917 if( nRec==0 && !isHot &&
1918 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001919 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1920 }
1921
danielk197776572402004-06-25 02:38:54 +00001922 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001923 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001924 */
danielk1977e180dd92007-04-05 17:15:52 +00001925 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001926 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001927 if( rc!=SQLITE_OK ){
1928 goto end_playback;
1929 }
danielk197776572402004-06-25 02:38:54 +00001930 }
1931
danielk197776572402004-06-25 02:38:54 +00001932 /* Copy original pages out of the journal and back into the database file.
1933 */
1934 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001935 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001936 if( rc!=SQLITE_OK ){
1937 if( rc==SQLITE_DONE ){
1938 rc = SQLITE_OK;
1939 pPager->journalOff = szJ;
1940 break;
1941 }else{
1942 goto end_playback;
1943 }
1944 }
drh968af522003-02-11 14:55:40 +00001945 }
drhed7c8552001-04-11 14:29:21 +00001946 }
drh580eeaf2006-02-24 03:09:37 +00001947 /*NOTREACHED*/
1948 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001949
1950end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001951 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001952 zMaster = pPager->pTmpSpace;
1953 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1954 }
1955 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001956 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001957 }
danielk197765839c62007-08-30 08:08:17 +00001958 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001959 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001960 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001961 */
danielk197765839c62007-08-30 08:08:17 +00001962 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001963 }
danielk197776572402004-06-25 02:38:54 +00001964
1965 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001966 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001967 ** value. Reset it to the correct value for this process.
1968 */
drhc80f0582007-05-01 16:59:48 +00001969 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001970 return rc;
drhed7c8552001-04-11 14:29:21 +00001971}
1972
1973/*
drhac69b052004-05-12 13:30:07 +00001974** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001975**
1976** This is similar to playing back the transaction journal but with
1977** a few extra twists.
1978**
drh663fc632002-02-02 18:49:19 +00001979** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001980** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001981** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001982**
drhac69b052004-05-12 13:30:07 +00001983** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001984** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001985** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001986*/
drh3aac2dd2004-04-26 14:10:20 +00001987static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001988 i64 szJ; /* Size of the full journal */
1989 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001990 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001991 int i; /* Loop counter */
1992 int rc;
1993
danielk197776572402004-06-25 02:38:54 +00001994 szJ = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001995
danielk19774099f6e2007-03-19 11:25:20 +00001996 /* Set hdrOff to be the offset just after the end of the last journal
1997 ** page written before the first journal-header for this statement
1998 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001999 ** header was written.
2000 */
2001 hdrOff = pPager->stmtHdrOff;
2002 assert( pPager->fullSync || !hdrOff );
2003 if( !hdrOff ){
2004 hdrOff = szJ;
2005 }
2006
drhfa86c412002-02-02 15:01:15 +00002007 /* Truncate the database back to its original size.
2008 */
danielk1977e180dd92007-04-05 17:15:52 +00002009 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00002010 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00002011
drhac69b052004-05-12 13:30:07 +00002012 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00002013 */
drhac69b052004-05-12 13:30:07 +00002014 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00002015 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00002016
drhac69b052004-05-12 13:30:07 +00002017 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00002018 ** database file. Note that the statement journal omits checksums from
2019 ** each record since power-failure recovery is not important to statement
2020 ** journals.
drhfa86c412002-02-02 15:01:15 +00002021 */
danielk197762079062007-08-15 17:08:46 +00002022 for(i=0; i<nRec; i++){
2023 i64 offset = i*(4+pPager->pageSize);
2024 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00002025 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00002026 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00002027 }
2028
danielk197776572402004-06-25 02:38:54 +00002029 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
2030 ** was the size of the journal file when this statement was started, so
2031 ** everything after that needs to be rolled back, either into the
2032 ** database, the memory cache, or both.
2033 **
2034 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
2035 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00002036 */
danielk197776572402004-06-25 02:38:54 +00002037 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00002038 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00002039 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00002040 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00002041 assert( rc!=SQLITE_DONE );
2042 if( rc!=SQLITE_OK ) goto end_stmt_playback;
2043 }
2044
2045 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00002046 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00002047 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00002048 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00002049 if( rc!=SQLITE_OK ){
2050 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00002051 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00002052 }
danielk1977f0113002006-01-24 12:09:17 +00002053 if( nJRec==0 ){
2054 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00002055 }
danielk1977f0113002006-01-24 12:09:17 +00002056 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00002057 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00002058 assert( rc!=SQLITE_DONE );
2059 if( rc!=SQLITE_OK ) goto end_stmt_playback;
2060 }
drhfa86c412002-02-02 15:01:15 +00002061 }
danielk197776572402004-06-25 02:38:54 +00002062
2063 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00002064
drh3aac2dd2004-04-26 14:10:20 +00002065end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00002066 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00002067 pPager->journalOff = szJ;
2068 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00002069 }
2070 return rc;
2071}
2072
2073/*
drhf57b14a2001-09-14 18:54:08 +00002074** Change the maximum number of in-memory pages that are allowed.
2075*/
danielk19773b8a05f2007-03-19 17:44:26 +00002076void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00002077 if( mxPage>10 ){
2078 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00002079 }else{
2080 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00002081 }
2082}
2083
2084/*
drh973b6e32003-02-12 14:09:42 +00002085** Adjust the robustness of the database to damage due to OS crashes
2086** or power failures by changing the number of syncs()s when writing
2087** the rollback journal. There are three levels:
2088**
drh054889e2005-11-30 03:20:31 +00002089** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00002090** for temporary and transient files.
2091**
2092** NORMAL The journal is synced once before writes begin on the
2093** database. This is normally adequate protection, but
2094** it is theoretically possible, though very unlikely,
2095** that an inopertune power failure could leave the journal
2096** in a state which would cause damage to the database
2097** when it is rolled back.
2098**
2099** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00002100** database (with some additional information - the nRec field
2101** of the journal header - being written in between the two
2102** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00002103** single disk sector is atomic, then this mode provides
2104** assurance that the journal will not be corrupted to the
2105** point of causing damage to the database during rollback.
2106**
2107** Numeric values associated with these states are OFF==1, NORMAL=2,
2108** and FULL=3.
2109*/
danielk197793758c82005-01-21 08:13:14 +00002110#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00002111void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00002112 pPager->noSync = level==1 || pPager->tempFile;
2113 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00002114 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00002115 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00002116}
danielk197793758c82005-01-21 08:13:14 +00002117#endif
drh973b6e32003-02-12 14:09:42 +00002118
2119/*
drhaf6df112005-06-07 02:12:30 +00002120** The following global variable is incremented whenever the library
2121** attempts to open a temporary file. This information is used for
2122** testing and analysis only.
2123*/
drh0f7eb612006-08-08 13:51:43 +00002124#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002125int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00002126#endif
drhaf6df112005-06-07 02:12:30 +00002127
2128/*
drh3f56e6e2007-03-15 01:16:47 +00002129** Open a temporary file.
2130**
2131** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00002132** other error code if we fail. The OS will automatically delete the temporary
2133** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00002134*/
danielk1977b4b47412007-08-17 15:53:36 +00002135static int sqlite3PagerOpentemp(
danielk197717b90b52008-06-06 11:11:25 +00002136 Pager *pPager, /* The pager object */
drh33f4e022007-09-03 15:19:34 +00002137 sqlite3_file *pFile, /* Write the file descriptor here */
drh33f4e022007-09-03 15:19:34 +00002138 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00002139){
drhfa86c412002-02-02 15:01:15 +00002140 int rc;
drh3f56e6e2007-03-15 01:16:47 +00002141
drh0f7eb612006-08-08 13:51:43 +00002142#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002143 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002144#endif
danielk1977b4b47412007-08-17 15:53:36 +00002145
drh33f4e022007-09-03 15:19:34 +00002146 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2147 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
danielk197717b90b52008-06-06 11:11:25 +00002148 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00002149 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00002150 return rc;
2151}
2152
2153/*
drhed7c8552001-04-11 14:29:21 +00002154** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002155** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002156** the first call to sqlite3PagerGet() and is only held open until the
2157** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002158**
drh6446c4d2001-12-15 14:22:18 +00002159** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002160** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002161** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002162**
2163** If zFilename is ":memory:" then all information is held in cache.
2164** It is never written to disk. This can be used to implement an
2165** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002166*/
danielk19773b8a05f2007-03-19 17:44:26 +00002167int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002168 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002169 Pager **ppPager, /* Return the Pager structure here */
2170 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002171 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002172 int flags, /* flags controlling this file */
2173 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002174){
danielk1977b4b47412007-08-17 15:53:36 +00002175 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002176 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002177 int rc = SQLITE_OK;
2178 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002179 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002180 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002181 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002182 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2183 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002184 int journalFileSize = sqlite3JournalSize(pVfs);
drhfacf0302008-06-17 15:12:00 +00002185 int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
danielk197717b90b52008-06-06 11:11:25 +00002186 char *zPathname = 0;
2187 int nPathname = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002188
drh86f8c192007-08-22 00:39:19 +00002189 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002190 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002191
danielk197717b90b52008-06-06 11:11:25 +00002192 /* Compute and store the full pathname in an allocated buffer pointed
2193 ** to by zPathname, length nPathname. Or, if this is a temporary file,
2194 ** leave both nPathname and zPathname set to 0.
2195 */
drh1cc8c442007-08-24 16:08:29 +00002196 if( zFilename && zFilename[0] ){
danielk197717b90b52008-06-06 11:11:25 +00002197 nPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00002198 zPathname = sqlite3Malloc(nPathname*2);
danielk197717b90b52008-06-06 11:11:25 +00002199 if( zPathname==0 ){
2200 return SQLITE_NOMEM;
2201 }
drh1cc8c442007-08-24 16:08:29 +00002202#ifndef SQLITE_OMIT_MEMORYDB
2203 if( strcmp(zFilename,":memory:")==0 ){
2204 memDb = 1;
2205 zPathname[0] = 0;
2206 }else
2207#endif
2208 {
danielk1977adfb9b02007-09-17 07:02:56 +00002209 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002210 }
danielk197717b90b52008-06-06 11:11:25 +00002211 if( rc!=SQLITE_OK ){
2212 sqlite3_free(zPathname);
2213 return rc;
2214 }
2215 nPathname = strlen(zPathname);
drhae28c012007-08-24 16:29:23 +00002216 }
drh99b90c32008-03-17 13:50:58 +00002217
danielk1977b4b47412007-08-17 15:53:36 +00002218 /* Allocate memory for the pager structure */
2219 pPager = sqlite3MallocZero(
2220 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00002221 journalFileSize + /* The journal file structure */
drh17c0ba22008-02-26 18:40:11 +00002222 pVfs->szOsFile * 3 + /* The main db and two journal files */
danielk197717b90b52008-06-06 11:11:25 +00002223 3*nPathname + 40 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00002224 );
2225 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002226 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002227 return SQLITE_NOMEM;
2228 }
2229 pPtr = (u8 *)&pPager[1];
drh33f4e022007-09-03 15:19:34 +00002230 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002231 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002232 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2233 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2234 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002235 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2236 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00002237 pPager->pVfs = pVfs;
danielk197717b90b52008-06-06 11:11:25 +00002238 if( zPathname ){
2239 memcpy(pPager->zFilename, zPathname, nPathname+1);
2240 sqlite3_free(zPathname);
2241 }
danielk1977b4b47412007-08-17 15:53:36 +00002242
drh153c62c2007-08-24 03:51:33 +00002243 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002244 */
drhae28c012007-08-24 16:29:23 +00002245 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002246 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2247 rc = SQLITE_CANTOPEN;
2248 }else{
drh1cc8c442007-08-24 16:08:29 +00002249 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002250 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2251 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002252 readOnly = (fout&SQLITE_OPEN_READONLY);
2253
2254 /* If the file was successfully opened for read/write access,
2255 ** choose a default page size in case we have to create the
2256 ** database file. The default page size is the maximum of:
2257 **
2258 ** + SQLITE_DEFAULT_PAGE_SIZE,
2259 ** + The value returned by sqlite3OsSectorSize()
2260 ** + The largest page size that can be written atomically.
2261 */
2262 if( rc==SQLITE_OK && !readOnly ){
2263 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
drhfacf0302008-06-17 15:12:00 +00002264 if( szPageDflt<iSectorSize ){
2265 szPageDflt = iSectorSize;
drh1cc8c442007-08-24 16:08:29 +00002266 }
danielk19779663b8f2007-08-24 11:52:28 +00002267#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002268 {
2269 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2270 int ii;
2271 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2272 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2273 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
drhfacf0302008-06-17 15:12:00 +00002274 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2275 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002276 }
danielk1977b4b47412007-08-17 15:53:36 +00002277 }
drh1cc8c442007-08-24 16:08:29 +00002278#endif
drhfacf0302008-06-17 15:12:00 +00002279 if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2280 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
drh1cc8c442007-08-24 16:08:29 +00002281 }
danielk19778def5ea2004-06-16 10:39:52 +00002282 }
drhac69b052004-05-12 13:30:07 +00002283 }
drh1cc8c442007-08-24 16:08:29 +00002284 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002285 /* If a temporary file is requested, it is not opened immediately.
2286 ** In this case we accept the default page size and delay actually
2287 ** opening the file until the first call to OsWrite().
2288 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002289 tempFile = 1;
2290 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002291 }
danielk1977aef0bf62005-12-30 16:28:01 +00002292
danielk1977b4b47412007-08-17 15:53:36 +00002293 if( pPager && rc==SQLITE_OK ){
drhfacf0302008-06-17 15:12:00 +00002294 pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
drh3e7a6092002-12-07 21:45:14 +00002295 }
danielk1977aef0bf62005-12-30 16:28:01 +00002296
drh153c62c2007-08-24 03:51:33 +00002297 /* If an error occured in either of the blocks above.
2298 ** Free the Pager structure and close the file.
2299 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002300 ** any Pager.errMask variables.
2301 */
danielk1977b4b47412007-08-17 15:53:36 +00002302 if( !pPager || !pPager->pTmpSpace ){
2303 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002304 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002305 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002306 }
danielk1977aef0bf62005-12-30 16:28:01 +00002307
drh153c62c2007-08-24 03:51:33 +00002308 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2309 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002310
danielk1977b4b47412007-08-17 15:53:36 +00002311 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002312 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002313 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002314 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002315
drh99b90c32008-03-17 13:50:58 +00002316 /* Fill in Pager.zJournal[] */
danielk197717b90b52008-06-06 11:11:25 +00002317 if( zPathname ){
2318 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2319 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
2320 }else{
2321 pPager->zJournal = 0;
2322 }
danielk1977b4b47412007-08-17 15:53:36 +00002323
drh3b59a5c2006-01-15 20:28:28 +00002324 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002325 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002326 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002327 /* pPager->stmtOpen = 0; */
2328 /* pPager->stmtInUse = 0; */
2329 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002330 pPager->dbSize = memDb-1;
drhfacf0302008-06-17 15:12:00 +00002331 pPager->pageSize = szPageDflt;
drh3b59a5c2006-01-15 20:28:28 +00002332 /* pPager->stmtSize = 0; */
2333 /* pPager->stmtJSize = 0; */
2334 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002335 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002336 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002337 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002338 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002339 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002340 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002341 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2342 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2343 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2344 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002345 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002346 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002347 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002348 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002349 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002350 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002351 /* pPager->pFirst = 0; */
2352 /* pPager->pFirstSynced = 0; */
2353 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002354 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk1977b53e4962008-06-04 06:45:59 +00002355 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
danielk19777a2b1ee2007-08-21 14:27:01 +00002356 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002357 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002358 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002359 }
drh3b59a5c2006-01-15 20:28:28 +00002360 /* pPager->pBusyHandler = 0; */
2361 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002362 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002363#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002364 pPager->iInUseMM = 0;
2365 pPager->iInUseDB = 0;
2366 if( !memDb ){
drh26e4a8b2008-05-01 17:16:52 +00002367#ifndef SQLITE_MUTEX_NOOP
danielk197759f8c082008-06-18 17:09:10 +00002368 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00002369#endif
drh86f8c192007-08-22 00:39:19 +00002370 sqlite3_mutex_enter(mutex);
2371 pPager->pNext = sqlite3PagerList;
2372 if( sqlite3PagerList ){
2373 assert( sqlite3PagerList->pPrev==0 );
2374 sqlite3PagerList->pPrev = pPager;
2375 }
2376 pPager->pPrev = 0;
2377 sqlite3PagerList = pPager;
2378 sqlite3_mutex_leave(mutex);
2379 }
danielk197752622822006-01-09 09:59:49 +00002380#endif
drhed7c8552001-04-11 14:29:21 +00002381 return SQLITE_OK;
2382}
2383
2384/*
drh90f5ecb2004-07-22 01:19:35 +00002385** Set the busy handler function.
2386*/
danielk19773b8a05f2007-03-19 17:44:26 +00002387void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002388 pPager->pBusyHandler = pBusyHandler;
2389}
2390
2391/*
drh72f82862001-05-24 21:06:34 +00002392** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002393** when the reference count on each page reaches zero. The destructor can
2394** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002395**
danielk19773b8a05f2007-03-19 17:44:26 +00002396** The destructor is not called as a result sqlite3PagerClose().
2397** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002398*/
danielk19773b8a05f2007-03-19 17:44:26 +00002399void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002400 pPager->xDestructor = xDesc;
2401}
2402
2403/*
drha6abd042004-06-09 17:37:22 +00002404** Set the reinitializer for this pager. If not NULL, the reinitializer
2405** is called when the content of a page in cache is restored to its original
2406** value as a result of a rollback. The callback gives higher-level code
2407** an opportunity to restore the EXTRA section to agree with the restored
2408** page data.
2409*/
danielk19773b8a05f2007-03-19 17:44:26 +00002410void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002411 pPager->xReiniter = xReinit;
2412}
2413
2414/*
danielk1977a1644fd2007-08-29 12:31:25 +00002415** Set the page size to *pPageSize. If the suggest new page size is
2416** inappropriate, then an alternative page size is set to that
2417** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002418*/
danielk1977a1644fd2007-08-29 12:31:25 +00002419int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2420 int rc = SQLITE_OK;
2421 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002422 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002423 if( pageSize && pageSize!=pPager->pageSize
2424 && !pPager->memDb && pPager->nRef==0
2425 ){
drhfacf0302008-06-17 15:12:00 +00002426 char *pNew = (char *)sqlite3PageMalloc(pageSize);
danielk1977a1644fd2007-08-29 12:31:25 +00002427 if( !pNew ){
2428 rc = SQLITE_NOMEM;
2429 }else{
2430 pagerEnter(pPager);
2431 pager_reset(pPager);
2432 pPager->pageSize = pageSize;
2433 setSectorSize(pPager);
drhfacf0302008-06-17 15:12:00 +00002434 sqlite3PageFree(pPager->pTmpSpace);
danielk1977a1644fd2007-08-29 12:31:25 +00002435 pPager->pTmpSpace = pNew;
2436 pagerLeave(pPager);
2437 }
drh1c7880e2005-05-20 20:01:55 +00002438 }
danielk1977a1644fd2007-08-29 12:31:25 +00002439 *pPageSize = pPager->pageSize;
2440 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002441}
2442
2443/*
drh26b79942007-11-28 16:19:56 +00002444** Return a pointer to the "temporary page" buffer held internally
2445** by the pager. This is a buffer that is big enough to hold the
2446** entire content of a database page. This buffer is used internally
2447** during rollback and will be overwritten whenever a rollback
2448** occurs. But other modules are free to use it too, as long as
2449** no rollbacks are happening.
2450*/
2451void *sqlite3PagerTempSpace(Pager *pPager){
2452 return pPager->pTmpSpace;
2453}
2454
2455/*
drhf8e632b2007-05-08 14:51:36 +00002456** Attempt to set the maximum database page count if mxPage is positive.
2457** Make no changes if mxPage is zero or negative. And never reduce the
2458** maximum page count below the current size of the database.
2459**
2460** Regardless of mxPage, return the current maximum page count.
2461*/
2462int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2463 if( mxPage>0 ){
2464 pPager->mxPgno = mxPage;
2465 }
danielk1977ad0132d2008-06-07 08:58:22 +00002466 sqlite3PagerPagecount(pPager, 0);
drhf8e632b2007-05-08 14:51:36 +00002467 return pPager->mxPgno;
2468}
2469
2470/*
drhc9ac5ca2005-11-04 22:03:30 +00002471** The following set of routines are used to disable the simulated
2472** I/O error mechanism. These routines are used to avoid simulated
2473** errors in places where we do not care about errors.
2474**
2475** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2476** and generate no code.
2477*/
2478#ifdef SQLITE_TEST
2479extern int sqlite3_io_error_pending;
2480extern int sqlite3_io_error_hit;
2481static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002482void disable_simulated_io_errors(void){
2483 saved_cnt = sqlite3_io_error_pending;
2484 sqlite3_io_error_pending = -1;
2485}
2486void enable_simulated_io_errors(void){
2487 sqlite3_io_error_pending = saved_cnt;
2488}
2489#else
drh152410f2005-11-05 15:11:22 +00002490# define disable_simulated_io_errors()
2491# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002492#endif
2493
2494/*
drh90f5ecb2004-07-22 01:19:35 +00002495** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002496** that pDest points to.
2497**
2498** No error checking is done. The rational for this is that this function
2499** may be called even if the file does not exist or contain a header. In
2500** these cases sqlite3OsRead() will return an error, to which the correct
2501** response is to zero the memory at pDest and continue. A real IO error
2502** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002503*/
danielk19773b8a05f2007-03-19 17:44:26 +00002504int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002505 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002506 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002507 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2508 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002509 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002510 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002511 if( rc==SQLITE_IOERR_SHORT_READ ){
2512 rc = SQLITE_OK;
2513 }
drh90f5ecb2004-07-22 01:19:35 +00002514 }
drh551b7732006-11-06 21:20:25 +00002515 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002516}
2517
2518/*
drh5e00f6c2001-09-13 13:46:56 +00002519** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002520** pPager.
2521**
2522** If the PENDING_BYTE lies on the page directly after the end of the
2523** file, then consider this page part of the file too. For example, if
2524** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2525** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002526*/
danielk1977ad0132d2008-06-07 08:58:22 +00002527int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
danielk19777a2b1ee2007-08-21 14:27:01 +00002528 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002529 int rc;
drhd9b02572001-04-15 00:37:09 +00002530 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002531 if( pPager->errCode ){
danielk1977ad0132d2008-06-07 08:58:22 +00002532 return pPager->errCode;
drha7aea3d2007-03-15 12:51:16 +00002533 }
drhed7c8552001-04-11 14:29:21 +00002534 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002535 n = pPager->dbSize;
2536 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002537 assert(pPager->fd->pMethods||pPager->tempFile);
2538 if( (pPager->fd->pMethods)
2539 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002540 pPager->nRef++;
drhe49f9822006-09-15 12:29:16 +00002541 pager_error(pPager, rc);
danielk1977ae72d982007-10-03 08:46:44 +00002542 pPager->nRef--;
danielk1977ad0132d2008-06-07 08:58:22 +00002543 return rc;
danielk197715f411d2005-09-16 10:18:45 +00002544 }
2545 if( n>0 && n<pPager->pageSize ){
2546 n = 1;
2547 }else{
2548 n /= pPager->pageSize;
2549 }
2550 if( pPager->state!=PAGER_UNLOCK ){
2551 pPager->dbSize = n;
2552 }
drhed7c8552001-04-11 14:29:21 +00002553 }
danielk197715f411d2005-09-16 10:18:45 +00002554 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002555 n++;
2556 }
drhf8e632b2007-05-08 14:51:36 +00002557 if( n>pPager->mxPgno ){
2558 pPager->mxPgno = n;
2559 }
danielk1977ad0132d2008-06-07 08:58:22 +00002560 if( pnPage ){
2561 *pnPage = n;
2562 }
2563 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00002564}
2565
drhf07e7d52006-04-07 13:54:46 +00002566
2567#ifndef SQLITE_OMIT_MEMORYDB
2568/*
2569** Clear a PgHistory block
2570*/
2571static void clearHistory(PgHistory *pHist){
drhfacf0302008-06-17 15:12:00 +00002572 sqlite3PageFree(pHist->pOrig);
2573 sqlite3PageFree(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002574 pHist->pOrig = 0;
2575 pHist->pStmt = 0;
2576}
2577#else
2578#define clearHistory(x)
2579#endif
2580
drhed7c8552001-04-11 14:29:21 +00002581/*
drhf7c57532003-04-25 13:22:51 +00002582** Forward declaration
2583*/
danielk197776572402004-06-25 02:38:54 +00002584static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002585
2586/*
drh85b623f2007-12-13 21:54:09 +00002587** Unlink pPg from its hash chain. Also set the page number to 0 to indicate
danielk1977f5fdda82004-11-03 08:44:05 +00002588** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002589** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002590** pNextFree/pPrevFree list that is not a part of any hash-chain.
2591*/
2592static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2593 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002594 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002595 return;
2596 }
2597 if( pPg->pNextHash ){
2598 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2599 }
2600 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002601 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002602 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2603 }else{
drh8ca0c722006-05-07 17:49:38 +00002604 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002605 pPager->aHash[h] = pPg->pNextHash;
2606 }
drh5229ae42006-03-23 23:29:04 +00002607 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002608 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2609 }
danielk1977f5fdda82004-11-03 08:44:05 +00002610 pPg->pgno = 0;
2611 pPg->pNextHash = pPg->pPrevHash = 0;
2612}
2613
2614/*
drhac69b052004-05-12 13:30:07 +00002615** Unlink a page from the free list (the list of all pages where nRef==0)
2616** and from its hash collision chain.
2617*/
2618static void unlinkPage(PgHdr *pPg){
2619 Pager *pPager = pPg->pPager;
2620
danielk19779f61c2f2007-08-27 17:27:49 +00002621 /* Unlink from free page list */
2622 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002623
2624 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002625 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002626}
2627
2628/*
danielk1977e180dd92007-04-05 17:15:52 +00002629** This routine is used to truncate the cache when a database
2630** is truncated. Drop from the cache all pages whose pgno is
2631** larger than pPager->dbSize and is unreferenced.
2632**
drhac69b052004-05-12 13:30:07 +00002633** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002634**
2635** Actually, at the point this routine is called, it would be
2636** an error to have a referenced page. But rather than delete
2637** that page and guarantee a subsequent segfault, it seems better
2638** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002639*/
danielk1977e180dd92007-04-05 17:15:52 +00002640static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002641 PgHdr *pPg;
2642 PgHdr **ppPg;
2643 int dbSize = pPager->dbSize;
2644
2645 ppPg = &pPager->pAll;
2646 while( (pPg = *ppPg)!=0 ){
2647 if( pPg->pgno<=dbSize ){
2648 ppPg = &pPg->pNextAll;
2649 }else if( pPg->nRef>0 ){
2650 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2651 ppPg = &pPg->pNextAll;
2652 }else{
2653 *ppPg = pPg->pNextAll;
drhbd0b1b52008-07-07 19:52:09 +00002654#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977da8c8f22008-07-07 18:42:40 +00002655 if( *ppPg ){
2656 (*ppPg)->pPrevAll = pPg->pPrevAll;
2657 }
drhbd0b1b52008-07-07 19:52:09 +00002658#endif
drh538f5702007-04-13 02:14:30 +00002659 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2660 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002661 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002662 makeClean(pPg);
drhfacf0302008-06-17 15:12:00 +00002663 sqlite3PageFree(pPg->pData);
drh17435752007-08-16 04:30:38 +00002664 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002665 pPager->nPage--;
2666 }
2667 }
2668}
2669
drhf7c57532003-04-25 13:22:51 +00002670/*
danielk197717221812005-02-15 03:38:05 +00002671** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002672** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002673** false or until the lock succeeds.
2674**
2675** Return SQLITE_OK on success and an error code if we cannot obtain
2676** the lock.
2677*/
2678static int pager_wait_on_lock(Pager *pPager, int locktype){
2679 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002680
2681 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002682 assert( PAGER_SHARED==SHARED_LOCK );
2683 assert( PAGER_RESERVED==RESERVED_LOCK );
2684 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002685
2686 /* If the file is currently unlocked then the size must be unknown */
2687 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2688
danielk197717221812005-02-15 03:38:05 +00002689 if( pPager->state>=locktype ){
2690 rc = SQLITE_OK;
2691 }else{
drhbefdf832008-03-14 19:33:05 +00002692 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002693 do {
drh054889e2005-11-30 03:20:31 +00002694 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002695 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002696 if( rc==SQLITE_OK ){
2697 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002698 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002699 }
2700 }
2701 return rc;
2702}
2703
2704/*
drhf7c57532003-04-25 13:22:51 +00002705** Truncate the file to the number of pages specified.
2706*/
danielk19773b8a05f2007-03-19 17:44:26 +00002707int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002708 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002709 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk1977ad0132d2008-06-07 08:58:22 +00002710 sqlite3PagerPagecount(pPager, 0);
danielk1977efaaf572006-01-16 11:29:19 +00002711 if( pPager->errCode ){
2712 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002713 return rc;
2714 }
drh7d02cb72003-06-04 16:24:39 +00002715 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002716 return SQLITE_OK;
2717 }
drhb7f91642004-10-31 02:22:47 +00002718 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002719 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002720 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002721 return SQLITE_OK;
2722 }
drh86f8c192007-08-22 00:39:19 +00002723 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002724 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002725 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002726 if( rc!=SQLITE_OK ){
2727 return rc;
2728 }
danielk197717221812005-02-15 03:38:05 +00002729
2730 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002731 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002732 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002733 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002734 if( rc!=SQLITE_OK ){
2735 return rc;
2736 }
2737
drhcb4c40b2004-08-18 19:09:43 +00002738 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002739 return rc;
2740}
2741
2742/*
drhed7c8552001-04-11 14:29:21 +00002743** Shutdown the page cache. Free all memory and close all files.
2744**
2745** If a transaction was in progress when this routine is called, that
2746** transaction is rolled back. All outstanding pages are invalidated
2747** and their memory is freed. Any attempt to use a page associated
2748** with this page cache after this function returns will likely
2749** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002750**
2751** This function always succeeds. If a transaction is active an attempt
2752** is made to roll it back. If an error occurs during the rollback
2753** a hot journal may be left in the filesystem but no error is returned
2754** to the caller.
drhed7c8552001-04-11 14:29:21 +00002755*/
danielk19773b8a05f2007-03-19 17:44:26 +00002756int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002757#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002758 if( !MEMDB ){
drh26e4a8b2008-05-01 17:16:52 +00002759#ifndef SQLITE_MUTEX_NOOP
danielk197759f8c082008-06-18 17:09:10 +00002760 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00002761#endif
drh86f8c192007-08-22 00:39:19 +00002762 sqlite3_mutex_enter(mutex);
2763 if( pPager->pPrev ){
2764 pPager->pPrev->pNext = pPager->pNext;
2765 }else{
2766 sqlite3PagerList = pPager->pNext;
2767 }
2768 if( pPager->pNext ){
2769 pPager->pNext->pPrev = pPager->pPrev;
2770 }
2771 sqlite3_mutex_leave(mutex);
2772 }
danielk197713f72992005-12-18 08:51:22 +00002773#endif
2774
drhbafda092007-01-03 23:36:22 +00002775 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002776 sqlite3BeginBenignMalloc();
drhc2ee76c2007-01-04 14:58:14 +00002777 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002778 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002779 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002780 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002781 enable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002782 sqlite3EndBenignMalloc();
drh4f0c5872007-03-26 22:05:01 +00002783 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002784 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002785 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002786 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002787 }
drhf5e7bb52008-02-18 14:47:33 +00002788 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002789 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002790 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002791 }
danielk1977b4b47412007-08-17 15:53:36 +00002792 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002793 /* Temp files are automatically deleted by the OS
2794 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002795 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002796 ** }
2797 */
danielk1977aca790a2005-01-13 11:07:52 +00002798
drh17435752007-08-16 04:30:38 +00002799 sqlite3_free(pPager->aHash);
drhfacf0302008-06-17 15:12:00 +00002800 sqlite3PageFree(pPager->pTmpSpace);
drh17435752007-08-16 04:30:38 +00002801 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002802 return SQLITE_OK;
2803}
2804
drh87cc3b32007-05-08 21:45:27 +00002805#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002806/*
drh5e00f6c2001-09-13 13:46:56 +00002807** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002808*/
danielk19773b8a05f2007-03-19 17:44:26 +00002809Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002810 return p->pgno;
2811}
drh87cc3b32007-05-08 21:45:27 +00002812#endif
drhed7c8552001-04-11 14:29:21 +00002813
2814/*
drhc8629a12004-05-08 20:07:40 +00002815** The page_ref() function increments the reference count for a page.
2816** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002817** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002818**
2819** For non-test systems, page_ref() is a macro that calls _page_ref()
2820** online of the reference count is zero. For test systems, page_ref()
2821** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002822*/
drh836faa42003-01-11 13:30:57 +00002823static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002824 if( pPg->nRef==0 ){
2825 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002826 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002827 pPg->pPager->nRef++;
2828 }
2829 pPg->nRef++;
drhdf0b3b02001-06-23 11:36:20 +00002830}
danielk1977aca790a2005-01-13 11:07:52 +00002831#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002832 static void page_ref(PgHdr *pPg){
2833 if( pPg->nRef==0 ){
2834 _page_ref(pPg);
2835 }else{
2836 pPg->nRef++;
drhc8629a12004-05-08 20:07:40 +00002837 }
2838 }
2839#else
2840# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2841#endif
drhdf0b3b02001-06-23 11:36:20 +00002842
2843/*
2844** Increment the reference count for a page. The input pointer is
2845** a reference to the page data.
2846*/
danielk19773b8a05f2007-03-19 17:44:26 +00002847int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002848 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002849 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002850 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002851 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002852}
2853
2854/*
drh34e79ce2004-02-08 06:05:46 +00002855** Sync the journal. In other words, make sure all the pages that have
2856** been written to the journal have actually reached the surface of the
2857** disk. It is not safe to modify the original database file until after
2858** the journal has been synced. If the original database is modified before
2859** the journal is synced and a power failure occurs, the unsynced journal
2860** data would be lost and we would be unable to completely rollback the
2861** database changes. Database corruption would occur.
2862**
2863** This routine also updates the nRec field in the header of the journal.
2864** (See comments on the pager_playback() routine for additional information.)
2865** If the sync mode is FULL, two syncs will occur. First the whole journal
2866** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002867**
drh34e79ce2004-02-08 06:05:46 +00002868** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002869** after a power failure, so no sync occurs.
2870**
2871** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2872** the database is stored, then OsSync() is never called on the journal
2873** file. In this case all that is required is to update the nRec field in
2874** the journal header.
drhfa86c412002-02-02 15:01:15 +00002875**
drh34e79ce2004-02-08 06:05:46 +00002876** This routine clears the needSync field of every page current held in
2877** memory.
drh50e5dad2001-09-15 00:57:28 +00002878*/
danielk197776572402004-06-25 02:38:54 +00002879static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002880 PgHdr *pPg;
2881 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002882
2883 /* Sync the journal before modifying the main database
2884 ** (assuming there is a journal and it needs to be synced.)
2885 */
danielk197776572402004-06-25 02:38:54 +00002886 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002887 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002888 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002889 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002890
danielk19774cd2cd52007-08-23 14:48:23 +00002891 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002892 /* Write the nRec value into the journal file header. If in
2893 ** full-synchronous mode, sync the journal first. This ensures that
2894 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002895 ** it as a candidate for rollback.
2896 **
2897 ** This is not required if the persistent media supports the
2898 ** SAFE_APPEND property. Because in this case it is not possible
2899 ** for garbage data to be appended to the file, the nRec field
2900 ** is populated with 0xFFFFFFFF when the journal header is written
2901 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002902 */
danielk19774cd2cd52007-08-23 14:48:23 +00002903 i64 jrnlOff;
2904 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002905 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002906 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002907 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002908 if( rc!=0 ) return rc;
2909 }
danielk197713adf8a2004-06-03 16:08:41 +00002910
danielk197762079062007-08-15 17:08:46 +00002911 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2912 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2913 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002914 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002915 }
danielk19774cd2cd52007-08-23 14:48:23 +00002916 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2917 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2918 IOTRACE(("JSYNC %p\n", pPager))
2919 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2920 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2921 );
2922 if( rc!=0 ) return rc;
2923 }
drhdb48ee02003-01-16 13:42:43 +00002924 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002925 }
drh50e5dad2001-09-15 00:57:28 +00002926 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002927
2928 /* Erase the needSync flag from every page.
2929 */
2930 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2931 pPg->needSync = 0;
2932 }
danielk19779f61c2f2007-08-27 17:27:49 +00002933 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002934 }
drh03eb96a2002-11-10 23:32:56 +00002935
drh341eae82003-01-21 02:39:36 +00002936#ifndef NDEBUG
2937 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2938 ** flag must also be clear for all pages. Verify that this
2939 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002940 */
drh341eae82003-01-21 02:39:36 +00002941 else{
2942 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2943 assert( pPg->needSync==0 );
2944 }
danielk19779f61c2f2007-08-27 17:27:49 +00002945 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002946 }
drh341eae82003-01-21 02:39:36 +00002947#endif
drhdb48ee02003-01-16 13:42:43 +00002948
drh81a20f22001-10-12 17:30:04 +00002949 return rc;
drh50e5dad2001-09-15 00:57:28 +00002950}
2951
2952/*
drhdc3e10b2006-06-15 14:31:06 +00002953** Merge two lists of pages connected by pDirty and in pgno order.
2954** Do not both fixing the pPrevDirty pointers.
2955*/
2956static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2957 PgHdr result, *pTail;
2958 pTail = &result;
2959 while( pA && pB ){
2960 if( pA->pgno<pB->pgno ){
2961 pTail->pDirty = pA;
2962 pTail = pA;
2963 pA = pA->pDirty;
2964 }else{
2965 pTail->pDirty = pB;
2966 pTail = pB;
2967 pB = pB->pDirty;
2968 }
2969 }
2970 if( pA ){
2971 pTail->pDirty = pA;
2972 }else if( pB ){
2973 pTail->pDirty = pB;
2974 }else{
2975 pTail->pDirty = 0;
2976 }
2977 return result.pDirty;
2978}
2979
2980/*
2981** Sort the list of pages in accending order by pgno. Pages are
2982** connected by pDirty pointers. The pPrevDirty pointers are
2983** corrupted by this sort.
2984*/
danielk197724168722007-04-02 05:07:47 +00002985#define N_SORT_BUCKET_ALLOC 25
2986#define N_SORT_BUCKET 25
2987#ifdef SQLITE_TEST
2988 int sqlite3_pager_n_sort_bucket = 0;
2989 #undef N_SORT_BUCKET
2990 #define N_SORT_BUCKET \
2991 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2992#endif
drhdc3e10b2006-06-15 14:31:06 +00002993static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002994 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002995 int i;
2996 memset(a, 0, sizeof(a));
2997 while( pIn ){
2998 p = pIn;
2999 pIn = p->pDirty;
3000 p->pDirty = 0;
3001 for(i=0; i<N_SORT_BUCKET-1; i++){
3002 if( a[i]==0 ){
3003 a[i] = p;
3004 break;
3005 }else{
3006 p = merge_pagelist(a[i], p);
3007 a[i] = 0;
3008 }
3009 }
3010 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00003011 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
3012 ** elements in the input list. This is possible, but impractical.
3013 ** Testing this line is the point of global variable
3014 ** sqlite3_pager_n_sort_bucket.
3015 */
drhdc3e10b2006-06-15 14:31:06 +00003016 a[i] = merge_pagelist(a[i], p);
3017 }
3018 }
3019 p = a[0];
3020 for(i=1; i<N_SORT_BUCKET; i++){
3021 p = merge_pagelist(p, a[i]);
3022 }
3023 return p;
3024}
3025
3026/*
drh2554f8b2003-01-22 01:26:44 +00003027** Given a list of pages (connected by the PgHdr.pDirty pointer) write
3028** every one of those pages out to the database file and mark them all
3029** as clean.
3030*/
3031static int pager_write_pagelist(PgHdr *pList){
3032 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00003033 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00003034 int rc;
3035
3036 if( pList==0 ) return SQLITE_OK;
3037 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00003038
3039 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
3040 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00003041 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00003042 **
drha6abd042004-06-09 17:37:22 +00003043 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
3044 ** through an intermediate state PENDING. A PENDING lock prevents new
3045 ** readers from attaching to the database but is unsufficient for us to
3046 ** write. The idea of a PENDING lock is to prevent new readers from
3047 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00003048 **
drha6abd042004-06-09 17:37:22 +00003049 ** While the pager is in the RESERVED state, the original database file
3050 ** is unchanged and we can rollback without having to playback the
3051 ** journal into the original database file. Once we transition to
3052 ** EXCLUSIVE, it means the database file has been changed and any rollback
3053 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00003054 */
drh684917c2004-10-05 02:41:42 +00003055 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00003056 if( rc!=SQLITE_OK ){
3057 return rc;
3058 }
3059
drhdc3e10b2006-06-15 14:31:06 +00003060 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00003061 for(p=pList; p; p=p->pDirty){
3062 assert( p->dirty );
3063 p->dirty = 0;
3064 }
drh2554f8b2003-01-22 01:26:44 +00003065 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00003066
3067 /* If the file has not yet been opened, open it now. */
3068 if( !pPager->fd->pMethods ){
3069 assert(pPager->tempFile);
danielk197717b90b52008-06-06 11:11:25 +00003070 rc = sqlite3PagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00003071 if( rc ) return rc;
3072 }
3073
danielk1977687566d2004-11-02 12:56:41 +00003074 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00003075 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00003076 ** make the file smaller (presumably by auto-vacuum code). Do not write
3077 ** any such pages to the file.
3078 */
3079 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00003080 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00003081 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00003082 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
3083 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00003084 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00003085 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003086 PAGER_INCR(sqlite3_pager_writedb_count);
3087 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00003088 if( pList->pgno==1 ){
3089 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
3090 }
danielk1977687566d2004-11-02 12:56:41 +00003091 }
3092#ifndef NDEBUG
3093 else{
drh4f0c5872007-03-26 22:05:01 +00003094 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00003095 }
3096#endif
drh2554f8b2003-01-22 01:26:44 +00003097 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00003098#ifdef SQLITE_CHECK_PAGES
3099 pList->pageHash = pager_pagehash(pList);
3100#endif
drh2554f8b2003-01-22 01:26:44 +00003101 pList = pList->pDirty;
3102 }
3103 return SQLITE_OK;
3104}
3105
3106/*
3107** Collect every dirty page into a dirty list and
3108** return a pointer to the head of that list. All pages are
3109** collected even if they are still in use.
3110*/
3111static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh8f2e9a12008-02-26 14:46:04 +00003112
3113#ifndef NDEBUG
3114 /* Verify the sanity of the dirty list when we are running
3115 ** in debugging mode. This is expensive, so do not
3116 ** do this on a normal build. */
3117 int n1 = 0;
3118 int n2 = 0;
3119 PgHdr *p;
3120 for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; }
3121 for(p=pPager->pDirty; p; p=p->pDirty){ n2++; }
3122 assert( n1==n2 );
3123#endif
3124
drh605ad8f2006-05-03 23:34:05 +00003125 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00003126}
3127
3128/*
drh19db9352008-03-27 22:42:51 +00003129** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00003130** A hot journal is one that needs to be played back.
3131**
3132** If the current size of the database file is 0 but a journal file
3133** exists, that is probably an old journal left over from a prior
3134** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00003135**
3136** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00003137**
3138** This routine does not open the journal file to examine its
3139** content. Hence, the journal might contain the name of a master
3140** journal file that has been deleted, and hence not be hot. Or
3141** the header of the journal might be zeroed out. This routine
3142** does not discover these cases of a non-hot journal - if the
3143** journal file exists and is not empty this routine assumes it
3144** is hot. The pager_playback() routine will discover that the
3145** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00003146*/
3147static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003148 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977861f7452008-06-05 11:39:11 +00003149 int res = 0;
3150 if( pPager->useJournal && pPager->fd->pMethods ){
3151 int rc;
3152 int exists;
3153 int locked;
3154
3155 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
3156 if( rc==SQLITE_OK && exists ){
3157 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
3158 }
3159
3160 if( rc==SQLITE_OK && exists && !locked ){
danielk1977ad0132d2008-06-07 08:58:22 +00003161 int nPage;
3162 rc = sqlite3PagerPagecount(pPager, &nPage);
3163 if( rc==SQLITE_OK && nPage==0 ){
danielk1977861f7452008-06-05 11:39:11 +00003164 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3165 exists = 0;
3166 }
3167 }
3168
3169 res = (rc!=SQLITE_OK ? -1 : (exists && !locked));
drhbb5f18d2007-04-06 18:23:17 +00003170 }
danielk1977861f7452008-06-05 11:39:11 +00003171
3172 return res;
drh165ffe92005-03-15 17:09:30 +00003173}
3174
danielk19775591df52005-12-20 09:19:37 +00003175/*
danielk1977aef0bf62005-12-30 16:28:01 +00003176** Try to find a page in the cache that can be recycled.
3177**
3178** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00003179** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00003180*/
danielk1977843e65f2007-09-01 16:16:15 +00003181static int pager_recycle(Pager *pPager, PgHdr **ppPg){
danielk197713f72992005-12-18 08:51:22 +00003182 PgHdr *pPg;
3183 *ppPg = 0;
3184
danielk1977843e65f2007-09-01 16:16:15 +00003185 /* It is illegal to call this function unless the pager object
3186 ** pointed to by pPager has at least one free page (page with nRef==0).
3187 */
danielk1977c551edc2007-04-05 13:12:13 +00003188 assert(!MEMDB);
danielk1977843e65f2007-09-01 16:16:15 +00003189 assert(pPager->lru.pFirst);
danielk1977c551edc2007-04-05 13:12:13 +00003190
danielk197713f72992005-12-18 08:51:22 +00003191 /* Find a page to recycle. Try to locate a page that does not
3192 ** require us to do an fsync() on the journal.
3193 */
danielk19779f61c2f2007-08-27 17:27:49 +00003194 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00003195
3196 /* If we could not find a page that does not require an fsync()
3197 ** on the journal file then fsync the journal file. This is a
3198 ** very slow operation, so we work hard to avoid it. But sometimes
3199 ** it can't be helped.
3200 */
danielk1977c41cc392008-05-15 08:34:54 +00003201 if( pPg==0 && pPager->lru.pFirst ){
3202 if( !pPager->errCode ){
3203 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
3204 int rc = syncJournal(pPager);
danielk197713f72992005-12-18 08:51:22 +00003205 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003206 return rc;
danielk197713f72992005-12-18 08:51:22 +00003207 }
danielk1977c41cc392008-05-15 08:34:54 +00003208 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
3209 /* If in full-sync mode, write a new journal header into the
3210 ** journal file. This is done to avoid ever modifying a journal
3211 ** header that is involved in the rollback of pages that have
3212 ** already been written to the database (in case the header is
3213 ** trashed when the nRec field is updated).
3214 */
3215 pPager->nRec = 0;
3216 assert( pPager->journalOff > 0 );
3217 assert( pPager->doNotSync==0 );
3218 rc = writeJournalHdr(pPager);
3219 if( rc!=0 ){
3220 return rc;
3221 }
3222 }
danielk197713f72992005-12-18 08:51:22 +00003223 }
danielk19779f61c2f2007-08-27 17:27:49 +00003224 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003225 }
danielk197713f72992005-12-18 08:51:22 +00003226
3227 assert( pPg->nRef==0 );
3228
3229 /* Write the page to the database file if it is dirty.
3230 */
danielk1977c41cc392008-05-15 08:34:54 +00003231 if( pPg->dirty && !pPager->errCode ){
danielk197713f72992005-12-18 08:51:22 +00003232 int rc;
3233 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003234 makeClean(pPg);
3235 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003236 pPg->pDirty = 0;
3237 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003238 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003239 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003240 return rc;
danielk197713f72992005-12-18 08:51:22 +00003241 }
3242 }
danielk1977c41cc392008-05-15 08:34:54 +00003243 assert( pPg->dirty==0 || pPager->errCode );
danielk197713f72992005-12-18 08:51:22 +00003244
3245 /* If the page we are recycling is marked as alwaysRollback, then
3246 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003247 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003248 ** It is necessary to do this because the page marked alwaysRollback
3249 ** might be reloaded at a later time but at that point we won't remember
3250 ** that is was marked alwaysRollback. This means that all pages must
3251 ** be marked as alwaysRollback from here on out.
3252 */
3253 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003254 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003255 pPager->alwaysRollback = 1;
3256 }
3257
3258 /* Unlink the old page from the free list and the hash table
3259 */
3260 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003261 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003262
3263 *ppPg = pPg;
3264 return SQLITE_OK;
3265}
3266
drh86f8c192007-08-22 00:39:19 +00003267#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003268/*
3269** This function is called to free superfluous dynamically allocated memory
3270** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003271** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003272**
3273** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003274** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003275** of bytes of memory released.
3276*/
danielk19773b8a05f2007-03-19 17:44:26 +00003277int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003278 int nReleased = 0; /* Bytes of memory released so far */
drh86f8c192007-08-22 00:39:19 +00003279 Pager *pPager; /* For looping over pagers */
drhe5fe6902007-12-07 18:55:28 +00003280 BusyHandler *savedBusy; /* Saved copy of the busy handler */
danielk19779f61c2f2007-08-27 17:27:49 +00003281 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003282
drh86f8c192007-08-22 00:39:19 +00003283 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003284 */
drh26e4a8b2008-05-01 17:16:52 +00003285#ifndef SQLITE_MUTEX_NOOP
drh64e2bb72008-05-07 12:29:55 +00003286 sqlite3_mutex *mutex; /* The MEM2 mutex */
danielk197759f8c082008-06-18 17:09:10 +00003287 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00003288#endif
drh86f8c192007-08-22 00:39:19 +00003289 sqlite3_mutex_enter(mutex);
3290
3291 /* Signal all database connections that memory management wants
3292 ** to have access to the pagers.
3293 */
3294 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3295 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003296 }
3297
danielk19779f61c2f2007-08-27 17:27:49 +00003298 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3299 PgHdr *pPg;
3300 PgHdr *pRecycled;
3301
3302 /* Try to find a page to recycle that does not require a sync(). If
3303 ** this is not possible, find one that does require a sync().
3304 */
danielk197759f8c082008-06-18 17:09:10 +00003305 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU));
danielk19779f61c2f2007-08-27 17:27:49 +00003306 pPg = sqlite3LruPageList.pFirstSynced;
3307 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3308 pPg = pPg->gfree.pNext;
3309 }
3310 if( !pPg ){
3311 pPg = sqlite3LruPageList.pFirst;
3312 while( pPg && pPg->pPager->iInUseDB ){
3313 pPg = pPg->gfree.pNext;
3314 }
3315 }
danielk197759f8c082008-06-18 17:09:10 +00003316 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003317
danielk197784f786f2007-08-28 08:00:17 +00003318 /* If pPg==0, then the block above has failed to find a page to
3319 ** recycle. In this case return early - no further memory will
3320 ** be released.
3321 */
danielk19779f61c2f2007-08-27 17:27:49 +00003322 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003323
danielk19779f61c2f2007-08-27 17:27:49 +00003324 pPager = pPg->pPager;
3325 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3326 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3327
drhe5fe6902007-12-07 18:55:28 +00003328 savedBusy = pPager->pBusyHandler;
3329 pPager->pBusyHandler = 0;
danielk1977843e65f2007-09-01 16:16:15 +00003330 rc = pager_recycle(pPager, &pRecycled);
drhe5fe6902007-12-07 18:55:28 +00003331 pPager->pBusyHandler = savedBusy;
danielk19779f61c2f2007-08-27 17:27:49 +00003332 assert(pRecycled==pPg || rc!=SQLITE_OK);
3333 if( rc==SQLITE_OK ){
3334 /* We've found a page to free. At this point the page has been
3335 ** removed from the page hash-table, free-list and synced-list
3336 ** (pFirstSynced). It is still in the all pages (pAll) list.
3337 ** Remove it from this list before freeing.
3338 **
3339 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3340 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003341 */
danielk19779f61c2f2007-08-27 17:27:49 +00003342 PgHdr *pTmp;
3343 assert( pPg );
3344 if( pPg==pPager->pAll ){
danielk197721b7ce62008-07-07 11:18:27 +00003345 assert(pPg->pPrevAll==0);
3346 assert(pPg->pNextAll==0 || pPg->pNextAll->pPrevAll==pPg);
danielk19779f61c2f2007-08-27 17:27:49 +00003347 pPager->pAll = pPg->pNextAll;
danielk197721b7ce62008-07-07 11:18:27 +00003348 if( pPager->pAll ){
3349 pPager->pAll->pPrevAll = 0;
3350 }
danielk19779f61c2f2007-08-27 17:27:49 +00003351 }else{
danielk197721b7ce62008-07-07 11:18:27 +00003352 assert(pPg->pPrevAll);
3353 assert(pPg->pPrevAll->pNextAll==pPg);
3354 pTmp = pPg->pPrevAll;
3355 pTmp->pNextAll = pPg->pNextAll;
3356 if( pTmp->pNextAll ){
3357 pTmp->pNextAll->pPrevAll = pTmp;
3358 }
danielk197713f72992005-12-18 08:51:22 +00003359 }
danielk19779f61c2f2007-08-27 17:27:49 +00003360 nReleased += (
3361 sizeof(*pPg) + pPager->pageSize
3362 + sizeof(u32) + pPager->nExtra
3363 + MEMDB*sizeof(PgHistory)
3364 );
3365 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3366 PAGER_INCR(sqlite3_pager_pgfree_count);
drhfacf0302008-06-17 15:12:00 +00003367 sqlite3PageFree(pPg->pData);
danielk19779f61c2f2007-08-27 17:27:49 +00003368 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003369 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003370 }else{
3371 /* An error occured whilst writing to the database file or
3372 ** journal in pager_recycle(). The error is not returned to the
3373 ** caller of this function. Instead, set the Pager.errCode variable.
3374 ** The error will be returned to the user (or users, in the case
3375 ** of a shared pager cache) of the pager for which the error occured.
3376 */
3377 assert(
3378 (rc&0xff)==SQLITE_IOERR ||
3379 rc==SQLITE_FULL ||
3380 rc==SQLITE_BUSY
3381 );
3382 assert( pPager->state>=PAGER_RESERVED );
3383 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003384 }
3385 }
danielk1977aef0bf62005-12-30 16:28:01 +00003386
drh86f8c192007-08-22 00:39:19 +00003387 /* Clear the memory management flags and release the mutex
3388 */
3389 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3390 pPager->iInUseMM = 0;
3391 }
3392 sqlite3_mutex_leave(mutex);
3393
3394 /* Return the number of bytes released
3395 */
danielk197713f72992005-12-18 08:51:22 +00003396 return nReleased;
3397}
drh86f8c192007-08-22 00:39:19 +00003398#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003399
drh165ffe92005-03-15 17:09:30 +00003400/*
danielk1977e180dd92007-04-05 17:15:52 +00003401** Read the content of page pPg out of the database file.
3402*/
3403static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3404 int rc;
danielk197762079062007-08-15 17:08:46 +00003405 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003406 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003407 assert(pPager->fd->pMethods||pPager->tempFile);
3408 if( !pPager->fd->pMethods ){
3409 return SQLITE_IOERR_SHORT_READ;
3410 }
danielk197762079062007-08-15 17:08:46 +00003411 offset = (pgno-1)*(i64)pPager->pageSize;
3412 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003413 PAGER_INCR(sqlite3_pager_readdb_count);
3414 PAGER_INCR(pPager->nRead);
3415 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003416 if( pgno==1 ){
3417 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3418 sizeof(pPager->dbFileVers));
3419 }
danielk1977e180dd92007-04-05 17:15:52 +00003420 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003421 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3422 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003423 return rc;
3424}
3425
3426
3427/*
danielk1977e277be02007-03-23 18:12:06 +00003428** This function is called to obtain the shared lock required before
3429** data may be read from the pager cache. If the shared lock has already
3430** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003431**
3432** Immediately after obtaining the shared lock (if required), this function
3433** checks for a hot-journal file. If one is found, an emergency rollback
3434** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003435*/
3436static int pagerSharedLock(Pager *pPager){
3437 int rc = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00003438 int isHot = 0;
danielk1977e277be02007-03-23 18:12:06 +00003439
danielk1977ae72d982007-10-03 08:46:44 +00003440 /* If this database is opened for exclusive access, has no outstanding
3441 ** page references and is in an error-state, now is the chance to clear
3442 ** the error. Discard the contents of the pager-cache and treat any
3443 ** open journal file as a hot-journal.
3444 */
3445 if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
3446 if( pPager->journalOpen ){
3447 isHot = 1;
3448 }
danielk1977ae72d982007-10-03 08:46:44 +00003449 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00003450 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00003451 }
3452
3453 /* If the pager is still in an error state, do not proceed. The error
3454 ** state will be cleared at some point in the future when all page
3455 ** references are dropped and the cache can be discarded.
3456 */
3457 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3458 return pPager->errCode;
3459 }
3460
3461 if( pPager->state==PAGER_UNLOCK || isHot ){
danielk1977b4b47412007-08-17 15:53:36 +00003462 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003463 if( !MEMDB ){
3464 assert( pPager->nRef==0 );
3465 if( !pPager->noReadlock ){
3466 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3467 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003468 assert( pPager->state==PAGER_UNLOCK );
danielk1977e277be02007-03-23 18:12:06 +00003469 return pager_error(pPager, rc);
3470 }
3471 assert( pPager->state>=SHARED_LOCK );
3472 }
3473
3474 /* If a journal file exists, and there is no RESERVED lock on the
3475 ** database file, then it either needs to be played back or deleted.
3476 */
drh19db9352008-03-27 22:42:51 +00003477 rc = hasHotJournal(pPager);
3478 if( rc<0 ){
danielk197752b472a2008-05-05 16:23:55 +00003479 rc = SQLITE_IOERR_NOMEM;
3480 goto failed;
drh19db9352008-03-27 22:42:51 +00003481 }
3482 if( rc==1 || isHot ){
danielk1977e277be02007-03-23 18:12:06 +00003483 /* Get an EXCLUSIVE lock on the database file. At this point it is
3484 ** important that a RESERVED lock is not obtained on the way to the
3485 ** EXCLUSIVE lock. If it were, another process might open the
3486 ** database file, detect the RESERVED lock, and conclude that the
3487 ** database is safe to read while this process is still rolling it
3488 ** back.
3489 **
3490 ** Because the intermediate RESERVED lock is not requested, the
3491 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00003492 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00003493 */
danielk1977ae72d982007-10-03 08:46:44 +00003494 if( pPager->state<EXCLUSIVE_LOCK ){
3495 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3496 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003497 rc = pager_error(pPager, rc);
3498 goto failed;
danielk1977ae72d982007-10-03 08:46:44 +00003499 }
3500 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00003501 }
danielk1977e277be02007-03-23 18:12:06 +00003502
drh16e45a42008-04-19 20:34:18 +00003503 /* Open the journal for read/write access. This is because in
drhfd131da2007-08-07 17:13:03 +00003504 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003505 ** possibly used for a transaction later on. On some systems, the
3506 ** OsTruncate() call used in exclusive-access mode also requires
3507 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003508 */
drh16e45a42008-04-19 20:34:18 +00003509 if( !isHot && pPager->journalOpen==0 ){
danielk1977861f7452008-06-05 11:39:11 +00003510 int res;
3511 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
3512 if( rc==SQLITE_OK ){
3513 if( res ){
3514 int fout = 0;
3515 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3516 assert( !pPager->tempFile );
3517 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3518 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3519 if( fout&SQLITE_OPEN_READONLY ){
3520 rc = SQLITE_BUSY;
3521 sqlite3OsClose(pPager->jfd);
3522 }
3523 }else{
3524 /* If the journal does not exist, that means some other process
3525 ** has already rolled it back */
danielk1977ae72d982007-10-03 08:46:44 +00003526 rc = SQLITE_BUSY;
danielk1977ae72d982007-10-03 08:46:44 +00003527 }
danielk1977979f38e2007-03-27 16:19:51 +00003528 }
3529 }
danielk1977e277be02007-03-23 18:12:06 +00003530 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003531 if( rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_UNLOCK
3532 && rc!=SQLITE_IOERR_NOMEM
3533 ){
3534 rc = SQLITE_BUSY;
drh1aa5af12008-03-07 19:51:14 +00003535 }
danielk197752b472a2008-05-05 16:23:55 +00003536 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003537 }
3538 pPager->journalOpen = 1;
3539 pPager->journalStarted = 0;
3540 pPager->journalOff = 0;
3541 pPager->setMaster = 0;
3542 pPager->journalHdr = 0;
3543
3544 /* Playback and delete the journal. Drop the database write
3545 ** lock and reacquire the read lock.
3546 */
3547 rc = pager_playback(pPager, 1);
3548 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003549 rc = pager_error(pPager, rc);
3550 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003551 }
danielk1977c5859712007-03-26 12:26:27 +00003552 assert(pPager->state==PAGER_SHARED ||
3553 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3554 );
danielk1977e277be02007-03-23 18:12:06 +00003555 }
3556
3557 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003558 /* The shared-lock has just been acquired on the database file
3559 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003560 ** read or write transaction). Check to see if the database
3561 ** has been modified. If the database has changed, flush the
3562 ** cache.
3563 **
3564 ** Database changes is detected by looking at 15 bytes beginning
3565 ** at offset 24 into the file. The first 4 of these 16 bytes are
3566 ** a 32-bit counter that is incremented with each change. The
3567 ** other bytes change randomly with each file change when
3568 ** a codec is in use.
3569 **
3570 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003571 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003572 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003573 */
drh86a88112007-04-16 15:02:19 +00003574 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977ad0132d2008-06-07 08:58:22 +00003575 sqlite3PagerPagecount(pPager, 0);
danielk197724168722007-04-02 05:07:47 +00003576
danielk1977e180dd92007-04-05 17:15:52 +00003577 if( pPager->errCode ){
danielk197752b472a2008-05-05 16:23:55 +00003578 rc = pPager->errCode;
3579 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003580 }
3581
danielk1977e180dd92007-04-05 17:15:52 +00003582 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003583 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003584 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003585 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003586 goto failed;
danielk1977e180dd92007-04-05 17:15:52 +00003587 }
drh86a88112007-04-16 15:02:19 +00003588 }else{
3589 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003590 }
3591
drh86a88112007-04-16 15:02:19 +00003592 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003593 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003594 }
3595 }
3596 }
danielk1977c5859712007-03-26 12:26:27 +00003597 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3598 if( pPager->state==PAGER_UNLOCK ){
3599 pPager->state = PAGER_SHARED;
3600 }
danielk1977e277be02007-03-23 18:12:06 +00003601 }
3602
danielk197752b472a2008-05-05 16:23:55 +00003603 failed:
3604 if( rc!=SQLITE_OK ){
3605 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
3606 pager_unlock(pPager);
3607 }
danielk1977e277be02007-03-23 18:12:06 +00003608 return rc;
3609}
3610
3611/*
danielk1977e180dd92007-04-05 17:15:52 +00003612** Allocate a PgHdr object. Either create a new one or reuse
3613** an existing one that is not otherwise in use.
3614**
3615** A new PgHdr structure is created if any of the following are
3616** true:
3617**
3618** (1) We have not exceeded our maximum allocated cache size
3619** as set by the "PRAGMA cache_size" command.
3620**
3621** (2) There are no unused PgHdr objects available at this time.
3622**
3623** (3) This is an in-memory database.
3624**
3625** (4) There are no PgHdr objects that do not require a journal
3626** file sync and a sync of the journal file is currently
3627** prohibited.
3628**
3629** Otherwise, reuse an existing PgHdr. In other words, reuse an
3630** existing PgHdr if all of the following are true:
3631**
3632** (1) We have reached or exceeded the maximum cache size
3633** allowed by "PRAGMA cache_size".
3634**
3635** (2) There is a PgHdr available with PgHdr->nRef==0
3636**
3637** (3) We are not in an in-memory database
3638**
3639** (4) Either there is an available PgHdr that does not need
3640** to be synced to disk or else disk syncing is currently
3641** allowed.
danielk197724168722007-04-02 05:07:47 +00003642*/
3643static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3644 int rc = SQLITE_OK;
3645 PgHdr *pPg;
drh1e3af332007-10-20 13:17:54 +00003646 int nByteHdr;
danielk197724168722007-04-02 05:07:47 +00003647
danielk1977e180dd92007-04-05 17:15:52 +00003648 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003649 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003650 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003651 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003652 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003653 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003654 ){
drh0d180202008-02-14 23:26:56 +00003655 void *pData;
danielk197724168722007-04-02 05:07:47 +00003656 if( pPager->nPage>=pPager->nHash ){
3657 pager_resize_hash_table(pPager,
3658 pPager->nHash<256 ? 256 : pPager->nHash*2);
3659 if( pPager->nHash==0 ){
3660 rc = SQLITE_NOMEM;
3661 goto pager_allocate_out;
3662 }
3663 }
drhed138fb2007-08-22 22:04:37 +00003664 pagerLeave(pPager);
drh1e3af332007-10-20 13:17:54 +00003665 nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra
3666 + MEMDB*sizeof(PgHistory);
drhe5ae5732008-06-15 02:51:47 +00003667 pPg = sqlite3Malloc( nByteHdr );
drh0d180202008-02-14 23:26:56 +00003668 if( pPg ){
drhfacf0302008-06-17 15:12:00 +00003669 pData = sqlite3PageMalloc( pPager->pageSize );
drh0d180202008-02-14 23:26:56 +00003670 if( pData==0 ){
3671 sqlite3_free(pPg);
3672 pPg = 0;
3673 }
3674 }
drhed138fb2007-08-22 22:04:37 +00003675 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003676 if( pPg==0 ){
3677 rc = SQLITE_NOMEM;
3678 goto pager_allocate_out;
3679 }
drh1e3af332007-10-20 13:17:54 +00003680 memset(pPg, 0, nByteHdr);
drh0d180202008-02-14 23:26:56 +00003681 pPg->pData = pData;
danielk197724168722007-04-02 05:07:47 +00003682 pPg->pPager = pPager;
3683 pPg->pNextAll = pPager->pAll;
danielk197721b7ce62008-07-07 11:18:27 +00003684#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
3685 if( pPg->pNextAll ){
3686 pPg->pNextAll->pPrevAll = pPg;
3687 }
3688#endif
danielk197724168722007-04-02 05:07:47 +00003689 pPager->pAll = pPg;
3690 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003691 }else{
3692 /* Recycle an existing page with a zero ref-count. */
danielk1977843e65f2007-09-01 16:16:15 +00003693 rc = pager_recycle(pPager, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003694 if( rc==SQLITE_BUSY ){
3695 rc = SQLITE_IOERR_BLOCKED;
3696 }
danielk197724168722007-04-02 05:07:47 +00003697 if( rc!=SQLITE_OK ){
3698 goto pager_allocate_out;
3699 }
3700 assert( pPager->state>=SHARED_LOCK );
3701 assert(pPg);
3702 }
3703 *ppPg = pPg;
3704
3705pager_allocate_out:
3706 return rc;
3707}
3708
3709/*
drhd33d5a82007-04-26 12:11:28 +00003710** Make sure we have the content for a page. If the page was
3711** previously acquired with noContent==1, then the content was
3712** just initialized to zeros instead of being read from disk.
3713** But now we need the real data off of disk. So make sure we
3714** have it. Read it in if we do not have it already.
3715*/
3716static int pager_get_content(PgHdr *pPg){
3717 if( pPg->needRead ){
3718 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3719 if( rc==SQLITE_OK ){
3720 pPg->needRead = 0;
3721 }else{
3722 return rc;
3723 }
3724 }
3725 return SQLITE_OK;
3726}
3727
3728/*
drhd9b02572001-04-15 00:37:09 +00003729** Acquire a page.
3730**
drh58a11682001-11-10 13:51:08 +00003731** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003732** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003733**
drhd33d5a82007-04-26 12:11:28 +00003734** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003735** file is smaller than the requested page, then no actual disk
3736** read occurs and the memory image of the page is initialized to
3737** all zeros. The extra data appended to a page is always initialized
3738** to zeros the first time a page is loaded into memory.
3739**
drhd9b02572001-04-15 00:37:09 +00003740** The acquisition might fail for several reasons. In all cases,
3741** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003742**
drhd33d5a82007-04-26 12:11:28 +00003743** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003744** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003745** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003746** just returns 0. This routine acquires a read-lock the first time it
3747** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003748** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003749** or journal files.
drh0787db62007-03-04 13:15:27 +00003750**
drh538f5702007-04-13 02:14:30 +00003751** If noContent is false, the page contents are actually read from disk.
3752** If noContent is true, it means that we do not care about the contents
3753** of the page at this time, so do not do a disk read. Just fill in the
3754** page content with zeros. But mark the fact that we have not read the
3755** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003756** sqlite3PagerWrite() is called on this page or if this routine is
3757** called again with noContent==0, that means that the content is needed
3758** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003759*/
drh86f8c192007-08-22 00:39:19 +00003760static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003761 Pager *pPager, /* The pager open on the database file */
3762 Pgno pgno, /* Page number to fetch */
3763 DbPage **ppPage, /* Write a pointer to the page here */
3764 int noContent /* Do not bother reading content from disk if true */
3765){
drhed7c8552001-04-11 14:29:21 +00003766 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003767 int rc;
drhed7c8552001-04-11 14:29:21 +00003768
danielk1977e277be02007-03-23 18:12:06 +00003769 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3770
danielk197726836652005-01-17 01:33:13 +00003771 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3772 ** number greater than this, or zero, is requested.
3773 */
drh267cb322005-09-16 17:16:52 +00003774 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003775 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003776 }
3777
drhd9b02572001-04-15 00:37:09 +00003778 /* Make sure we have not hit any critical errors.
3779 */
drh836faa42003-01-11 13:30:57 +00003780 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003781 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003782
danielk197713adf8a2004-06-03 16:08:41 +00003783 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003784 ** on the database file. pagerSharedLock() is a no-op if
3785 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003786 */
danielk1977e277be02007-03-23 18:12:06 +00003787 rc = pagerSharedLock(pPager);
3788 if( rc!=SQLITE_OK ){
3789 return rc;
drhed7c8552001-04-11 14:29:21 +00003790 }
danielk1977e277be02007-03-23 18:12:06 +00003791 assert( pPager->state!=PAGER_UNLOCK );
3792
3793 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003794 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003795 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003796 int nMax;
drhed7c8552001-04-11 14:29:21 +00003797 int h;
drh538f5702007-04-13 02:14:30 +00003798 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003799 rc = pagerAllocatePage(pPager, &pPg);
3800 if( rc!=SQLITE_OK ){
3801 return rc;
drhed7c8552001-04-11 14:29:21 +00003802 }
danielk197724168722007-04-02 05:07:47 +00003803
drhed7c8552001-04-11 14:29:21 +00003804 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003805 assert( !MEMDB || pgno>pPager->stmtSize );
drhf5e7bb52008-02-18 14:47:33 +00003806 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
3807 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003808
drh605ad8f2006-05-03 23:34:05 +00003809 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003810 pPg->nRef = 1;
danielk197775bab7d2006-01-23 13:09:45 +00003811
drhd9b02572001-04-15 00:37:09 +00003812 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003813 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003814 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003815 }
danielk1977ad0132d2008-06-07 08:58:22 +00003816 rc = sqlite3PagerPagecount(pPager, &nMax);
3817 if( rc!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00003818 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003819 return rc;
3820 }
danielk197775bab7d2006-01-23 13:09:45 +00003821
3822 /* Populate the page with data, either by reading from the database
3823 ** file, or by setting the entire page to zero.
3824 */
drhd215acf2007-04-13 04:01:58 +00003825 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003826 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003827 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003828 return SQLITE_FULL;
3829 }
drh90f5ecb2004-07-22 01:19:35 +00003830 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003831 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003832 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003833 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003834 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003835 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3836 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003837 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003838 return rc;
drh81a20f22001-10-12 17:30:04 +00003839 }
danielk1977b4626a32007-04-28 15:47:43 +00003840 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003841 }
danielk197775bab7d2006-01-23 13:09:45 +00003842
3843 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003844 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003845 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003846 pPg->pNextHash = pPager->aHash[h];
3847 pPager->aHash[h] = pPg;
3848 if( pPg->pNextHash ){
3849 assert( pPg->pNextHash->pPrevHash==0 );
3850 pPg->pNextHash->pPrevHash = pPg;
3851 }
3852
danielk19773c407372005-02-15 02:54:14 +00003853#ifdef SQLITE_CHECK_PAGES
3854 pPg->pageHash = pager_pagehash(pPg);
3855#endif
drhed7c8552001-04-11 14:29:21 +00003856 }else{
drhd9b02572001-04-15 00:37:09 +00003857 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003858 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003859 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003860 if( !noContent ){
3861 rc = pager_get_content(pPg);
3862 if( rc ){
3863 return rc;
3864 }
3865 }
drhdf0b3b02001-06-23 11:36:20 +00003866 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003867 }
danielk19773b8a05f2007-03-19 17:44:26 +00003868 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003869 return SQLITE_OK;
3870}
drh86f8c192007-08-22 00:39:19 +00003871int sqlite3PagerAcquire(
3872 Pager *pPager, /* The pager open on the database file */
3873 Pgno pgno, /* Page number to fetch */
3874 DbPage **ppPage, /* Write a pointer to the page here */
3875 int noContent /* Do not bother reading content from disk if true */
3876){
3877 int rc;
3878 pagerEnter(pPager);
3879 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3880 pagerLeave(pPager);
3881 return rc;
3882}
3883
drhed7c8552001-04-11 14:29:21 +00003884
3885/*
drh7e3b0a02001-04-28 16:52:40 +00003886** Acquire a page if it is already in the in-memory cache. Do
3887** not read the page from disk. Return a pointer to the page,
3888** or 0 if the page is not in cache.
3889**
danielk19773b8a05f2007-03-19 17:44:26 +00003890** See also sqlite3PagerGet(). The difference between this routine
3891** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003892** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003893** returns NULL if the page is not in cache or if a disk I/O error
3894** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003895*/
danielk19773b8a05f2007-03-19 17:44:26 +00003896DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003897 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003898
drh836faa42003-01-11 13:30:57 +00003899 assert( pPager!=0 );
3900 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003901
drh86f8c192007-08-22 00:39:19 +00003902 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003903 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003904 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003905 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3906 /* Do nothing */
3907 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3908 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003909 }
drh86f8c192007-08-22 00:39:19 +00003910 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003911 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003912}
3913
3914/*
drhed7c8552001-04-11 14:29:21 +00003915** Release a page.
3916**
3917** If the number of references to the page drop to zero, then the
3918** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003919** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003920** removed.
3921*/
danielk19773b8a05f2007-03-19 17:44:26 +00003922int sqlite3PagerUnref(DbPage *pPg){
drh6eac06e2008-05-07 12:45:41 +00003923 Pager *pPager;
3924
3925 if( pPg==0 ) return SQLITE_OK;
3926 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003927
3928 /* Decrement the reference count for this page
3929 */
drhed7c8552001-04-11 14:29:21 +00003930 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003931 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003932 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +00003933
danielk19773c407372005-02-15 02:54:14 +00003934 CHECK_PAGE(pPg);
3935
drh72f82862001-05-24 21:06:34 +00003936 /* When the number of references to a page reach 0, call the
3937 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003938 */
drhed7c8552001-04-11 14:29:21 +00003939 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003940
3941 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003942 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003943 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003944 }
drhd9b02572001-04-15 00:37:09 +00003945
3946 /* When all pages reach the freelist, drop the read lock from
3947 ** the database file.
3948 */
3949 pPager->nRef--;
3950 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003951 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003952 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003953 }
drhed7c8552001-04-11 14:29:21 +00003954 }
danielk1977ae72d982007-10-03 08:46:44 +00003955 pagerLeave(pPager);
drhd9b02572001-04-15 00:37:09 +00003956 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003957}
3958
3959/*
drha6abd042004-06-09 17:37:22 +00003960** Create a journal file for pPager. There should already be a RESERVED
3961** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003962**
3963** Return SQLITE_OK if everything. Return an error code and release the
3964** write lock if anything goes wrong.
3965*/
3966static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003967 sqlite3_vfs *pVfs = pPager->pVfs;
3968 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3969
drhda47d772002-12-02 04:25:19 +00003970 int rc;
drhb7f91642004-10-31 02:22:47 +00003971 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003972 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003973 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003974 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00003975 sqlite3PagerPagecount(pPager, 0);
drhed138fb2007-08-22 22:04:37 +00003976 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003977 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00003978 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003979 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003980 rc = SQLITE_NOMEM;
3981 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003982 }
danielk1977b4b47412007-08-17 15:53:36 +00003983
drhfdc40e92008-04-17 20:59:37 +00003984 if( pPager->journalOpen==0 ){
3985 if( pPager->tempFile ){
3986 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3987 }else{
3988 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003989 }
drhfdc40e92008-04-17 20:59:37 +00003990#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3991 rc = sqlite3JournalOpen(
3992 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3993 );
3994#else
3995 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
3996#endif
3997 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3998 pPager->journalOff = 0;
3999 pPager->setMaster = 0;
4000 pPager->journalHdr = 0;
4001 if( rc!=SQLITE_OK ){
4002 if( rc==SQLITE_NOMEM ){
4003 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
4004 }
4005 goto failed_to_open_journal;
4006 }
drhda47d772002-12-02 04:25:19 +00004007 }
4008 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00004009 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00004010 pPager->needSync = 0;
4011 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00004012 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00004013 if( pPager->errCode ){
4014 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00004015 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00004016 }
drhda47d772002-12-02 04:25:19 +00004017 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00004018
danielk197776572402004-06-25 02:38:54 +00004019 rc = writeJournalHdr(pPager);
4020
drhac69b052004-05-12 13:30:07 +00004021 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004022 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00004023 }
danielk1977ae72d982007-10-03 08:46:44 +00004024 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00004025 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00004026 if( rc==SQLITE_OK ){
4027 rc = SQLITE_FULL;
4028 }
4029 }
drh9c105bb2004-10-02 20:38:28 +00004030 return rc;
4031
4032failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00004033 sqlite3BitvecDestroy(pPager->pInJournal);
4034 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00004035 return rc;
drhda47d772002-12-02 04:25:19 +00004036}
4037
4038/*
drh4b845d72002-03-05 12:41:19 +00004039** Acquire a write-lock on the database. The lock is removed when
4040** the any of the following happen:
4041**
drh80e35f42007-03-30 14:06:34 +00004042** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00004043** * sqlite3PagerRollback() is called.
4044** * sqlite3PagerClose() is called.
4045** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00004046**
danielk197713adf8a2004-06-03 16:08:41 +00004047** The first parameter to this routine is a pointer to any open page of the
4048** database file. Nothing changes about the page - it is used merely to
4049** acquire a pointer to the Pager structure and as proof that there is
4050** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00004051**
danielk197713adf8a2004-06-03 16:08:41 +00004052** The second parameter indicates how much space in bytes to reserve for a
4053** master journal file-name at the start of the journal when it is created.
4054**
4055** A journal file is opened if this is not a temporary file. For temporary
4056** files, the opening of the journal file is deferred until there is an
4057** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00004058**
drha6abd042004-06-09 17:37:22 +00004059** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00004060**
4061** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
4062** immediately instead of waiting until we try to flush the cache. The
4063** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00004064*/
danielk19773b8a05f2007-03-19 17:44:26 +00004065int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00004066 Pager *pPager = pPg->pPager;
4067 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00004068 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00004069 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00004070 assert( pPager->state!=PAGER_UNLOCK );
4071 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00004072 assert( pPager->pInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00004073 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00004074 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00004075 pPager->origDbSize = pPager->dbSize;
4076 }else{
drh054889e2005-11-30 03:20:31 +00004077 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00004078 if( rc==SQLITE_OK ){
4079 pPager->state = PAGER_RESERVED;
4080 if( exFlag ){
4081 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
4082 }
4083 }
danielk19779eed5052004-06-04 10:38:30 +00004084 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00004085 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00004086 return rc;
drhac69b052004-05-12 13:30:07 +00004087 }
drha6abd042004-06-09 17:37:22 +00004088 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00004089 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhfdc40e92008-04-17 20:59:37 +00004090 if( pPager->useJournal && !pPager->tempFile
4091 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drhac69b052004-05-12 13:30:07 +00004092 rc = pager_open_journal(pPager);
4093 }
drh4b845d72002-03-05 12:41:19 +00004094 }
danielk1977334cdb62007-03-26 08:05:12 +00004095 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00004096 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00004097 ** time a (read or write) transaction was successfully concluded
4098 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00004099 ** kept open and either was truncated to 0 bytes or its header was
4100 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00004101 */
4102 assert( pPager->nRec==0 );
4103 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00004104 assert( pPager->pInJournal==0 );
danielk1977ad0132d2008-06-07 08:58:22 +00004105 sqlite3PagerPagecount(pPager, 0);
drhed138fb2007-08-22 22:04:37 +00004106 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004107 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhed138fb2007-08-22 22:04:37 +00004108 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004109 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00004110 rc = SQLITE_NOMEM;
4111 }else{
drh369339d2007-03-30 16:01:55 +00004112 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00004113 rc = writeJournalHdr(pPager);
4114 }
drh4b845d72002-03-05 12:41:19 +00004115 }
danielk1977334cdb62007-03-26 08:05:12 +00004116 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00004117 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00004118 return rc;
4119}
4120
4121/*
drh605ad8f2006-05-03 23:34:05 +00004122** Make a page dirty. Set its dirty flag and add it to the dirty
4123** page list.
4124*/
4125static void makeDirty(PgHdr *pPg){
4126 if( pPg->dirty==0 ){
4127 Pager *pPager = pPg->pPager;
4128 pPg->dirty = 1;
4129 pPg->pDirty = pPager->pDirty;
4130 if( pPager->pDirty ){
4131 pPager->pDirty->pPrevDirty = pPg;
4132 }
4133 pPg->pPrevDirty = 0;
4134 pPager->pDirty = pPg;
4135 }
4136}
4137
4138/*
4139** Make a page clean. Clear its dirty bit and remove it from the
4140** dirty page list.
4141*/
4142static void makeClean(PgHdr *pPg){
4143 if( pPg->dirty ){
4144 pPg->dirty = 0;
4145 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00004146 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004147 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
4148 }
4149 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00004150 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004151 pPg->pPrevDirty->pDirty = pPg->pDirty;
4152 }else{
drh153c62c2007-08-24 03:51:33 +00004153 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004154 pPg->pPager->pDirty = pPg->pDirty;
4155 }
4156 }
4157}
4158
4159
4160/*
drhed7c8552001-04-11 14:29:21 +00004161** Mark a data page as writeable. The page is written into the journal
4162** if it is not there already. This routine must be called before making
4163** changes to a page.
4164**
4165** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00004166** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00004167** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00004168** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00004169** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00004170**
4171** If the journal file could not be written because the disk is full,
4172** then this routine returns SQLITE_FULL and does an immediate rollback.
4173** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00004174** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00004175** reset.
drhed7c8552001-04-11 14:29:21 +00004176*/
danielk19773b8a05f2007-03-19 17:44:26 +00004177static int pager_write(PgHdr *pPg){
4178 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00004179 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00004180 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00004181
drh6446c4d2001-12-15 14:22:18 +00004182 /* Check for errors
4183 */
danielk1977efaaf572006-01-16 11:29:19 +00004184 if( pPager->errCode ){
4185 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004186 }
drh5e00f6c2001-09-13 13:46:56 +00004187 if( pPager->readOnly ){
4188 return SQLITE_PERM;
4189 }
drh6446c4d2001-12-15 14:22:18 +00004190
danielk197776572402004-06-25 02:38:54 +00004191 assert( !pPager->setMaster );
4192
danielk19773c407372005-02-15 02:54:14 +00004193 CHECK_PAGE(pPg);
4194
drh538f5702007-04-13 02:14:30 +00004195 /* If this page was previously acquired with noContent==1, that means
4196 ** we didn't really read in the content of the page. This can happen
4197 ** (for example) when the page is being moved to the freelist. But
4198 ** now we are (perhaps) moving the page off of the freelist for
4199 ** reuse and we need to know its original content so that content
4200 ** can be stored in the rollback journal. So do the read at this
4201 ** time.
4202 */
drhd33d5a82007-04-26 12:11:28 +00004203 rc = pager_get_content(pPg);
4204 if( rc ){
4205 return rc;
drh538f5702007-04-13 02:14:30 +00004206 }
4207
drh6446c4d2001-12-15 14:22:18 +00004208 /* Mark the page as dirty. If the page has already been written
4209 ** to the journal then we can return right away.
4210 */
drh605ad8f2006-05-03 23:34:05 +00004211 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00004212 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00004213 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004214 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004215 }else{
drh6446c4d2001-12-15 14:22:18 +00004216
danielk1977a0bf2652004-11-04 14:30:04 +00004217 /* If we get this far, it means that the page needs to be
4218 ** written to the transaction journal or the ckeckpoint journal
4219 ** or both.
4220 **
4221 ** First check to see that the transaction journal exists and
4222 ** create it if it does not.
4223 */
4224 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00004225 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00004226 if( rc!=SQLITE_OK ){
4227 return rc;
4228 }
4229 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00004230 if( !pPager->journalOpen && pPager->useJournal
4231 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00004232 rc = pager_open_journal(pPager);
4233 if( rc!=SQLITE_OK ) return rc;
4234 }
danielk1977a0bf2652004-11-04 14:30:04 +00004235 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004236 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004237
4238 /* The transaction journal now exists and we have a RESERVED or an
4239 ** EXCLUSIVE lock on the main database file. Write the current page to
4240 ** the transaction journal if it is not there already.
4241 */
drhfdc40e92008-04-17 20:59:37 +00004242 if( !pPg->inJournal && (pPager->journalOpen || MEMDB) ){
danielk1977a0bf2652004-11-04 14:30:04 +00004243 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00004244 if( MEMDB ){
4245 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00004246 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004247 assert( pHist->pOrig==0 );
drhfacf0302008-06-17 15:12:00 +00004248 pHist->pOrig = sqlite3PageMalloc( pPager->pageSize );
danielk1977662278e2007-11-05 15:30:12 +00004249 if( !pHist->pOrig ){
4250 return SQLITE_NOMEM;
danielk1977a0bf2652004-11-04 14:30:04 +00004251 }
danielk1977662278e2007-11-05 15:30:12 +00004252 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
danielk1977a0bf2652004-11-04 14:30:04 +00004253 }else{
drhbf4bca52007-09-06 22:19:14 +00004254 u32 cksum;
4255 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00004256
drh267cb322005-09-16 17:16:52 +00004257 /* We should never write to the journal file the page that
4258 ** contains the database locks. The following assert verifies
4259 ** that we do not. */
4260 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004261 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004262 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00004263 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4264 if( rc==SQLITE_OK ){
4265 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4266 pPager->journalOff + 4);
4267 pPager->journalOff += pPager->pageSize+4;
4268 }
4269 if( rc==SQLITE_OK ){
4270 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4271 pPager->journalOff += 4;
4272 }
4273 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00004274 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00004275 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00004276 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4277 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00004278
drhfd131da2007-08-07 17:13:03 +00004279 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004280 ** transaction will be rolled back by the layer above.
4281 */
danielk1977a0bf2652004-11-04 14:30:04 +00004282 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004283 return rc;
4284 }
danielk197707cb5602006-01-20 10:55:05 +00004285
danielk1977a0bf2652004-11-04 14:30:04 +00004286 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00004287 assert( pPager->pInJournal!=0 );
4288 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004289 pPg->needSync = !pPager->noSync;
4290 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00004291 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004292 }
drhac69b052004-05-12 13:30:07 +00004293 }
danielk197713adf8a2004-06-03 16:08:41 +00004294 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004295 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004296 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004297 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004298 }
4299 if( pPg->needSync ){
4300 pPager->needSync = 1;
4301 }
4302 pPg->inJournal = 1;
4303 }
4304
4305 /* If the statement journal is open and the page is not in it,
4306 ** then write the current page to the statement journal. Note that
4307 ** the statement journal format differs from the standard journal format
4308 ** in that it omits the checksums and the header.
4309 */
danielk1977f35843b2007-04-07 15:03:17 +00004310 if( pPager->stmtInUse
4311 && !pageInStatement(pPg)
4312 && (int)pPg->pgno<=pPager->stmtSize
4313 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004314 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4315 if( MEMDB ){
4316 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4317 assert( pHist->pStmt==0 );
drhfacf0302008-06-17 15:12:00 +00004318 pHist->pStmt = sqlite3PageMalloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004319 if( pHist->pStmt ){
4320 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4321 }
drh4f0c5872007-03-26 22:05:01 +00004322 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004323 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004324 }else{
danielk197762079062007-08-15 17:08:46 +00004325 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00004326 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4327 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4328 if( rc==SQLITE_OK ){
4329 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4330 }
drh4f0c5872007-03-26 22:05:01 +00004331 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004332 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004333 return rc;
4334 }
danielk1977a0bf2652004-11-04 14:30:04 +00004335 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00004336 assert( pPager->pInStmt!=0 );
4337 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00004338 }
drhd9b02572001-04-15 00:37:09 +00004339 }
drhfa86c412002-02-02 15:01:15 +00004340 }
4341
4342 /* Update the database size and return.
4343 */
drh1aa2d8b2007-01-03 15:34:29 +00004344 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004345 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004346 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004347 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004348 pPager->dbSize++;
4349 }
drh306dc212001-05-21 13:45:10 +00004350 }
drh69688d52001-04-14 16:38:23 +00004351 return rc;
drhed7c8552001-04-11 14:29:21 +00004352}
4353
4354/*
danielk19774099f6e2007-03-19 11:25:20 +00004355** This function is used to mark a data-page as writable. It uses
4356** pager_write() to open a journal file (if it is not already open)
4357** and write the page *pData to the journal.
4358**
4359** The difference between this function and pager_write() is that this
4360** function also deals with the special case where 2 or more pages
4361** fit on a single disk sector. In this case all co-resident pages
4362** must have been written to the journal file before returning.
4363*/
danielk19773b8a05f2007-03-19 17:44:26 +00004364int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004365 int rc = SQLITE_OK;
4366
danielk19773b8a05f2007-03-19 17:44:26 +00004367 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004368 Pager *pPager = pPg->pPager;
4369 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4370
drh86f8c192007-08-22 00:39:19 +00004371 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004372 if( !MEMDB && nPagePerSector>1 ){
4373 Pgno nPageCount; /* Total number of pages in database file */
4374 Pgno pg1; /* First page of the sector pPg is located on. */
4375 int nPage; /* Number of pages starting at pg1 to journal */
4376 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004377 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004378
4379 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4380 ** header to be written between the pages journaled by this function.
4381 */
4382 assert( pPager->doNotSync==0 );
4383 pPager->doNotSync = 1;
4384
4385 /* This trick assumes that both the page-size and sector-size are
4386 ** an integer power of 2. It sets variable pg1 to the identifier
4387 ** of the first page of the sector pPg is located on.
4388 */
4389 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4390
danielk1977ad0132d2008-06-07 08:58:22 +00004391 sqlite3PagerPagecount(pPager, (int *)&nPageCount);
danielk19774099f6e2007-03-19 11:25:20 +00004392 if( pPg->pgno>nPageCount ){
4393 nPage = (pPg->pgno - pg1)+1;
4394 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4395 nPage = nPageCount+1-pg1;
4396 }else{
4397 nPage = nPagePerSector;
4398 }
4399 assert(nPage>0);
4400 assert(pg1<=pPg->pgno);
4401 assert((pg1+nPage)>pPg->pgno);
4402
4403 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4404 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004405 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00004406 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00004407 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004408 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004409 if( rc==SQLITE_OK ){
4410 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004411 if( pPage->needSync ){
4412 needSync = 1;
4413 }
danielk19773b8a05f2007-03-19 17:44:26 +00004414 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004415 }
4416 }
drhc81945e2008-03-10 14:12:53 +00004417 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk1977dd97a492007-08-22 18:54:32 +00004418 if( pPage->needSync ){
4419 needSync = 1;
4420 }
danielk19774099f6e2007-03-19 11:25:20 +00004421 }
4422 }
4423
danielk1977dd97a492007-08-22 18:54:32 +00004424 /* If the PgHdr.needSync flag is set for any of the nPage pages
4425 ** starting at pg1, then it needs to be set for all of them. Because
4426 ** writing to any of these nPage pages may damage the others, the
4427 ** journal file must contain sync()ed copies of all of them
4428 ** before any of them can be written out to the database file.
4429 */
4430 if( needSync ){
4431 for(ii=0; ii<nPage && needSync; ii++){
4432 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4433 if( pPage ) pPage->needSync = 1;
4434 }
4435 assert(pPager->needSync);
4436 }
4437
danielk19774099f6e2007-03-19 11:25:20 +00004438 assert( pPager->doNotSync==1 );
4439 pPager->doNotSync = 0;
4440 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004441 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004442 }
drh86f8c192007-08-22 00:39:19 +00004443 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004444 return rc;
4445}
4446
4447/*
drhaacc5432002-01-06 17:07:40 +00004448** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004449** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004450** to change the content of the page.
4451*/
danielk19777d3a6662006-01-23 16:21:05 +00004452#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004453int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004454 return pPg->dirty;
4455}
danielk19777d3a6662006-01-23 16:21:05 +00004456#endif
drh6019e162001-07-02 17:51:45 +00004457
drh001bbcb2003-03-19 03:14:00 +00004458/*
drh30e58752002-03-02 20:41:57 +00004459** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004460** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004461** that page might be marked as dirty.
4462**
4463** The overlying software layer calls this routine when all of the data
4464** on the given page is unused. The pager marks the page as clean so
4465** that it does not get written to disk.
4466**
4467** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004468** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004469** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004470**
4471** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004472** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004473** will thereafter be ignored. This is necessary to avoid a problem
4474** where a page with data is added to the freelist during one part of
4475** a transaction then removed from the freelist during a later part
4476** of the same transaction and reused for some other purpose. When it
4477** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004478** the sqlite3PagerDontRollback() routine is called. But because the
4479** page contains critical data, we still need to be sure it gets
4480** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004481*/
drh538f5702007-04-13 02:14:30 +00004482void sqlite3PagerDontWrite(DbPage *pDbPage){
4483 PgHdr *pPg = pDbPage;
4484 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004485
drhb7f91642004-10-31 02:22:47 +00004486 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004487 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004488 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004489 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004490 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004491 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4492 /* If this pages is the last page in the file and the file has grown
4493 ** during the current transaction, then do NOT mark the page as clean.
4494 ** When the database file grows, we must make sure that the last page
4495 ** gets written at least once so that the disk file will be the correct
4496 ** size. If you do not write this page and the size of the file
4497 ** on the disk ends up being too small, that can lead to database
4498 ** corruption during the next transaction.
4499 */
4500 }else{
drh538f5702007-04-13 02:14:30 +00004501 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4502 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004503 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004504#ifdef SQLITE_CHECK_PAGES
4505 pPg->pageHash = pager_pagehash(pPg);
4506#endif
drh8124a302002-06-25 14:43:57 +00004507 }
drh30e58752002-03-02 20:41:57 +00004508 }
drh86f8c192007-08-22 00:39:19 +00004509 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004510}
4511
4512/*
4513** A call to this routine tells the pager that if a rollback occurs,
4514** it is not necessary to restore the data on the given page. This
4515** means that the pager does not have to record the given page in the
4516** rollback journal.
drh538f5702007-04-13 02:14:30 +00004517**
4518** If we have not yet actually read the content of this page (if
4519** the PgHdr.needRead flag is set) then this routine acts as a promise
4520** that we will never need to read the page content in the future.
4521** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00004522*/
danielk19773b8a05f2007-03-19 17:44:26 +00004523void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004524 Pager *pPager = pPg->pPager;
4525
drh86f8c192007-08-22 00:39:19 +00004526 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004527 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00004528
4529 /* If the journal file is not open, or DontWrite() has been called on
4530 ** this page (DontWrite() sets the alwaysRollback flag), then this
4531 ** function is a no-op.
4532 */
4533 if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){
danielk197787c29a92008-01-18 11:33:16 +00004534 pagerLeave(pPager);
4535 return;
4536 }
danielk1977a55e9352008-01-21 13:04:34 +00004537 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
4538
drhc5d0bd92008-04-14 01:00:57 +00004539#ifdef SQLITE_SECURE_DELETE
4540 if( pPg->inJournal || (int)pPg->pgno > pPager->origDbSize ){
4541 return;
4542 }
4543#endif
4544
4545 /* If SECURE_DELETE is disabled, then there is no way that this
4546 ** routine can be called on a page for which sqlite3PagerDontWrite()
4547 ** has not been previously called during the same transaction.
4548 ** And if DontWrite() has previously been called, the following
4549 ** conditions must be met.
drh10131482008-07-11 03:34:09 +00004550 **
4551 ** (Later:) Not true. If the database is corrupted by having duplicate
4552 ** pages on the freelist (ex: corrupt9.test) then the following is not
4553 ** necessarily true:
danielk1977a55e9352008-01-21 13:04:34 +00004554 */
drh10131482008-07-11 03:34:09 +00004555 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); */
danielk1977a55e9352008-01-21 13:04:34 +00004556
drhf5e7bb52008-02-18 14:47:33 +00004557 assert( pPager->pInJournal!=0 );
4558 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a55e9352008-01-21 13:04:34 +00004559 pPg->inJournal = 1;
4560 pPg->needRead = 0;
4561 if( pPager->stmtInUse ){
drhc5d0bd92008-04-14 01:00:57 +00004562 assert( pPager->stmtSize >= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00004563 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00004564 }
danielk1977a55e9352008-01-21 13:04:34 +00004565 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
4566 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh86f8c192007-08-22 00:39:19 +00004567 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004568}
4569
drhac69b052004-05-12 13:30:07 +00004570
drh30e58752002-03-02 20:41:57 +00004571/*
drh80e35f42007-03-30 14:06:34 +00004572** This routine is called to increment the database file change-counter,
4573** stored at byte 24 of the pager file.
4574*/
danielk1977c7b60172007-08-22 11:22:03 +00004575static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004576 PgHdr *pPgHdr;
4577 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004578 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004579
4580 if( !pPager->changeCountDone ){
4581 /* Open page 1 of the file for writing. */
4582 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4583 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004584
4585 if( !isDirect ){
4586 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00004587 if( rc!=SQLITE_OK ){
4588 sqlite3PagerUnref(pPgHdr);
4589 return rc;
4590 }
danielk1977c7b60172007-08-22 11:22:03 +00004591 }
4592
drh80e35f42007-03-30 14:06:34 +00004593 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004594 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004595 change_counter++;
4596 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004597
4598 if( isDirect && pPager->fd->pMethods ){
4599 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4600 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4601 }
4602
drh80e35f42007-03-30 14:06:34 +00004603 /* Release the page reference. */
4604 sqlite3PagerUnref(pPgHdr);
4605 pPager->changeCountDone = 1;
4606 }
danielk1977c7b60172007-08-22 11:22:03 +00004607 return rc;
drh80e35f42007-03-30 14:06:34 +00004608}
4609
4610/*
danielk1977f653d782008-03-20 11:04:21 +00004611** Sync the pager file to disk.
4612*/
4613int sqlite3PagerSync(Pager *pPager){
4614 int rc;
4615 pagerEnter(pPager);
4616 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4617 pagerLeave(pPager);
4618 return rc;
4619}
4620
4621/*
drh80e35f42007-03-30 14:06:34 +00004622** Sync the database file for the pager pPager. zMaster points to the name
4623** of a master journal file that should be written into the individual
4624** journal file. zMaster may be NULL, which is interpreted as no master
4625** journal (a single database transaction).
4626**
4627** This routine ensures that the journal is synced, all dirty pages written
4628** to the database file and the database file synced. The only thing that
4629** remains to commit the transaction is to delete the journal file (or
4630** master journal file if specified).
4631**
4632** Note that if zMaster==NULL, this does not overwrite a previous value
4633** passed to an sqlite3PagerCommitPhaseOne() call.
4634**
4635** If parameter nTrunc is non-zero, then the pager file is truncated to
4636** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00004637**
4638** If the final parameter - noSync - is true, then the database file itself
4639** is not synced. The caller must call sqlite3PagerSync() directly to
4640** sync the database file before calling CommitPhaseTwo() to delete the
4641** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00004642*/
danielk1977f653d782008-03-20 11:04:21 +00004643int sqlite3PagerCommitPhaseOne(
4644 Pager *pPager,
4645 const char *zMaster,
4646 Pgno nTrunc,
4647 int noSync
4648){
drh80e35f42007-03-30 14:06:34 +00004649 int rc = SQLITE_OK;
4650
danielk1977dad31b52008-05-15 11:08:07 +00004651 if( pPager->errCode ){
4652 return pPager->errCode;
4653 }
4654
drhd138c012008-05-13 00:58:18 +00004655 /* If no changes have been made, we can leave the transaction early.
4656 */
4657 if( pPager->dbModified==0 &&
4658 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
4659 pPager->exclusiveMode!=0) ){
4660 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
4661 return SQLITE_OK;
4662 }
4663
drh80e35f42007-03-30 14:06:34 +00004664 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4665 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004666 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004667
4668 /* If this is an in-memory db, or no pages have been written to, or this
4669 ** function has already been called, it is a no-op.
4670 */
4671 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4672 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004673
danielk1977c7b60172007-08-22 11:22:03 +00004674#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4675 /* The atomic-write optimization can be used if all of the
4676 ** following are true:
4677 **
4678 ** + The file-system supports the atomic-write property for
4679 ** blocks of size page-size, and
4680 ** + This commit is not part of a multi-file transaction, and
4681 ** + Exactly one page has been modified and store in the journal file.
4682 **
4683 ** If the optimization can be used, then the journal file will never
4684 ** be created for this transaction.
4685 */
danielk1977f55b8992007-08-24 08:15:53 +00004686 int useAtomicWrite = (
4687 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00004688 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00004689 pPager->journalOff==jrnlBufferSize(pPager) &&
4690 nTrunc==0 &&
4691 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4692 );
danielk1977700b9c52008-04-24 12:37:40 +00004693 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00004694 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004695 /* Update the nRec field in the journal file. */
4696 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4697 assert(pPager->nRec==1);
4698 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4699
4700 /* Update the db file change counter. The following call will modify
4701 ** the in-memory representation of page 1 to include the updated
4702 ** change counter and then write page 1 directly to the database
4703 ** file. Because of the atomic-write property of the host file-system,
4704 ** this is safe.
4705 */
danielk1977ae72d982007-10-03 08:46:44 +00004706 if( rc==SQLITE_OK ){
4707 rc = pager_incr_changecounter(pPager, 1);
4708 }
danielk1977f55b8992007-08-24 08:15:53 +00004709 }else{
4710 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00004711 }
4712
danielk1977ae72d982007-10-03 08:46:44 +00004713 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00004714#endif
4715
drh80e35f42007-03-30 14:06:34 +00004716 /* If a master journal file name has already been written to the
4717 ** journal file, then no sync is required. This happens when it is
4718 ** written, then the process fails to upgrade from a RESERVED to an
4719 ** EXCLUSIVE lock. The next time the process tries to commit the
4720 ** transaction the m-j name will have already been written.
4721 */
4722 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00004723 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004724 if( rc!=SQLITE_OK ) goto sync_exit;
danielk197771aa7ff2008-05-20 07:05:09 +00004725 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drh80e35f42007-03-30 14:06:34 +00004726#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197771aa7ff2008-05-20 07:05:09 +00004727 if( nTrunc!=0 ){
4728 /* If this transaction has made the database smaller, then all pages
4729 ** being discarded by the truncation must be written to the journal
4730 ** file.
4731 */
4732 Pgno i;
4733 int iSkip = PAGER_MJ_PGNO(pPager);
4734 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
4735 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
4736 rc = sqlite3PagerGet(pPager, i, &pPg);
4737 if( rc!=SQLITE_OK ) goto sync_exit;
4738 rc = sqlite3PagerWrite(pPg);
4739 sqlite3PagerUnref(pPg);
4740 if( rc!=SQLITE_OK ) goto sync_exit;
4741 }
4742 }
4743 }
drh80e35f42007-03-30 14:06:34 +00004744#endif
danielk197771aa7ff2008-05-20 07:05:09 +00004745 rc = writeMasterJournal(pPager, zMaster);
4746 if( rc!=SQLITE_OK ) goto sync_exit;
4747 rc = syncJournal(pPager);
4748 }
drh80e35f42007-03-30 14:06:34 +00004749 }
danielk1977c7b60172007-08-22 11:22:03 +00004750 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004751
4752#ifndef SQLITE_OMIT_AUTOVACUUM
4753 if( nTrunc!=0 ){
4754 rc = sqlite3PagerTruncate(pPager, nTrunc);
4755 if( rc!=SQLITE_OK ) goto sync_exit;
4756 }
4757#endif
4758
4759 /* Write all dirty pages to the database file */
4760 pPg = pager_get_all_dirty_pages(pPager);
4761 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004762 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004763 assert( rc!=SQLITE_IOERR_BLOCKED );
4764 /* The error might have left the dirty list all fouled up here,
4765 ** but that does not matter because if the if the dirty list did
4766 ** get corrupted, then the transaction will roll back and
4767 ** discard the dirty list. There is an assert in
4768 ** pager_get_all_dirty_pages() that verifies that no attempt
4769 ** is made to use an invalid dirty list.
4770 */
drh153c62c2007-08-24 03:51:33 +00004771 goto sync_exit;
4772 }
drh3cdd7d32007-03-30 14:46:01 +00004773 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004774
4775 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004776 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004777 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004778 }
4779 IOTRACE(("DBSYNC %p\n", pPager))
4780
4781 pPager->state = PAGER_SYNCED;
4782 }else if( MEMDB && nTrunc!=0 ){
4783 rc = sqlite3PagerTruncate(pPager, nTrunc);
4784 }
4785
4786sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004787 if( rc==SQLITE_IOERR_BLOCKED ){
4788 /* pager_incr_changecounter() may attempt to obtain an exclusive
4789 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004790 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004791 * better to return SQLITE_BUSY.
4792 */
4793 rc = SQLITE_BUSY;
4794 }
drh86f8c192007-08-22 00:39:19 +00004795 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004796 return rc;
4797}
4798
4799
4800/*
drhed7c8552001-04-11 14:29:21 +00004801** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004802**
4803** If the commit fails for any reason, a rollback attempt is made
4804** and an error code is returned. If the commit worked, SQLITE_OK
4805** is returned.
drhed7c8552001-04-11 14:29:21 +00004806*/
drh80e35f42007-03-30 14:06:34 +00004807int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004808 int rc;
drhed7c8552001-04-11 14:29:21 +00004809 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004810
danielk1977efaaf572006-01-16 11:29:19 +00004811 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004812 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004813 }
drha6abd042004-06-09 17:37:22 +00004814 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004815 return SQLITE_ERROR;
4816 }
drhd138c012008-05-13 00:58:18 +00004817 if( pPager->dbModified==0 &&
4818 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
4819 pPager->exclusiveMode!=0) ){
4820 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
4821 return SQLITE_OK;
4822 }
drh86f8c192007-08-22 00:39:19 +00004823 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004824 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004825 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004826 pPg = pager_get_all_dirty_pages(pPager);
4827 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004828 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4829 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004830 pPg->dirty = 0;
4831 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004832 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004833 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004834 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004835 pPg = pPg->pDirty;
4836 }
drh605ad8f2006-05-03 23:34:05 +00004837 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004838#ifndef NDEBUG
4839 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4840 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4841 assert( !pPg->alwaysRollback );
4842 assert( !pHist->pOrig );
4843 assert( !pHist->pStmt );
4844 }
4845#endif
drhac69b052004-05-12 13:30:07 +00004846 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004847 pPager->state = PAGER_SHARED;
danielk197787c29a92008-01-18 11:33:16 +00004848 pagerLeave(pPager);
drhac69b052004-05-12 13:30:07 +00004849 return SQLITE_OK;
4850 }
drh3cdd7d32007-03-30 14:46:01 +00004851 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
danielk1977df2566a2008-05-07 19:11:03 +00004852 rc = pager_end_transaction(pPager, pPager->setMaster);
drh86f8c192007-08-22 00:39:19 +00004853 rc = pager_error(pPager, rc);
4854 pagerLeave(pPager);
4855 return rc;
drhed7c8552001-04-11 14:29:21 +00004856}
4857
4858/*
drha6abd042004-06-09 17:37:22 +00004859** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004860** All in-memory cache pages revert to their original data contents.
4861** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004862**
4863** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004864** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004865** process is writing trash into the journal file (SQLITE_CORRUPT) or
4866** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4867** codes are returned for all these occasions. Otherwise,
4868** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004869*/
danielk19773b8a05f2007-03-19 17:44:26 +00004870int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004871 int rc;
drh4f0c5872007-03-26 22:05:01 +00004872 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004873 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004874 PgHdr *p;
4875 for(p=pPager->pAll; p; p=p->pNextAll){
4876 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004877 assert( !p->alwaysRollback );
4878 if( !p->dirty ){
4879 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4880 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4881 continue;
4882 }
4883
drhac69b052004-05-12 13:30:07 +00004884 pHist = PGHDR_TO_HIST(p, pPager);
4885 if( pHist->pOrig ){
4886 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004887 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004888 }else{
drh4f0c5872007-03-26 22:05:01 +00004889 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004890 }
4891 clearHistory(pHist);
4892 p->dirty = 0;
4893 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004894 pHist->inStmt = 0;
4895 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004896 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004897 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004898 }
drhac69b052004-05-12 13:30:07 +00004899 }
drh605ad8f2006-05-03 23:34:05 +00004900 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004901 pPager->pStmt = 0;
4902 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004903 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004904 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004905 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004906 return SQLITE_OK;
4907 }
4908
drh86f8c192007-08-22 00:39:19 +00004909 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004910 if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00004911 rc = pager_end_transaction(pPager, pPager->setMaster);
drh86f8c192007-08-22 00:39:19 +00004912 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004913 return rc;
4914 }
drhdb48ee02003-01-16 13:42:43 +00004915
danielk1977efaaf572006-01-16 11:29:19 +00004916 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004917 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004918 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004919 }
drh86f8c192007-08-22 00:39:19 +00004920 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004921 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004922 }
drha6abd042004-06-09 17:37:22 +00004923 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004924 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004925 rc = pager_playback(pPager, 0);
danielk1977df2566a2008-05-07 19:11:03 +00004926 rc2 = pager_end_transaction(pPager, pPager->setMaster);
drha6abd042004-06-09 17:37:22 +00004927 if( rc==SQLITE_OK ){
4928 rc = rc2;
4929 }
4930 }else{
danielk1977e277be02007-03-23 18:12:06 +00004931 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004932 }
danielk1977e180dd92007-04-05 17:15:52 +00004933 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004934 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004935
4936 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4937 ** cache. So call pager_error() on the way out to make any error
4938 ** persistent.
4939 */
drh86f8c192007-08-22 00:39:19 +00004940 rc = pager_error(pPager, rc);
4941 pagerLeave(pPager);
4942 return rc;
drh98808ba2001-10-18 12:34:46 +00004943}
drhd9b02572001-04-15 00:37:09 +00004944
4945/*
drh5e00f6c2001-09-13 13:46:56 +00004946** Return TRUE if the database file is opened read-only. Return FALSE
4947** if the database is (in theory) writable.
4948*/
danielk19773b8a05f2007-03-19 17:44:26 +00004949int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004950 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004951}
4952
4953/*
drh0f7eb612006-08-08 13:51:43 +00004954** Return the number of references to the pager.
4955*/
danielk19773b8a05f2007-03-19 17:44:26 +00004956int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004957 return pPager->nRef;
4958}
4959
4960#ifdef SQLITE_TEST
4961/*
drhd9b02572001-04-15 00:37:09 +00004962** This routine is used for testing and analysis only.
4963*/
danielk19773b8a05f2007-03-19 17:44:26 +00004964int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004965 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004966 a[0] = pPager->nRef;
4967 a[1] = pPager->nPage;
4968 a[2] = pPager->mxPage;
4969 a[3] = pPager->dbSize;
4970 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004971 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004972 a[6] = pPager->nHit;
4973 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004974 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004975 a[9] = pPager->nRead;
4976 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004977 return a;
4978}
danielk197717b90b52008-06-06 11:11:25 +00004979int sqlite3PagerIsMemdb(Pager *pPager){
4980 return MEMDB;
4981}
drh0f7eb612006-08-08 13:51:43 +00004982#endif
drhdd793422001-06-28 01:54:48 +00004983
drhfa86c412002-02-02 15:01:15 +00004984/*
drhac69b052004-05-12 13:30:07 +00004985** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004986**
4987** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004988** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004989** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004990*/
drh86f8c192007-08-22 00:39:19 +00004991static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004992 int rc;
drhac69b052004-05-12 13:30:07 +00004993 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004994 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004995 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004996 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004997 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004998 pPager->stmtInUse = 1;
4999 pPager->stmtSize = pPager->dbSize;
5000 return SQLITE_OK;
5001 }
drhda47d772002-12-02 04:25:19 +00005002 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00005003 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00005004 return SQLITE_OK;
5005 }
drhfa86c412002-02-02 15:01:15 +00005006 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00005007 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00005008 assert( pPager->pInStmt==0 );
5009 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00005010 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00005011 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00005012 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00005013 return SQLITE_NOMEM;
5014 }
danielk197776572402004-06-25 02:38:54 +00005015 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00005016 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00005017 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00005018 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00005019 if( !pPager->stmtOpen ){
danielk197717b90b52008-06-06 11:11:25 +00005020 rc = sqlite3PagerOpentemp(pPager, pPager->stfd, SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00005021 if( rc ){
5022 goto stmt_begin_failed;
5023 }
drhac69b052004-05-12 13:30:07 +00005024 pPager->stmtOpen = 1;
5025 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00005026 }
drhac69b052004-05-12 13:30:07 +00005027 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00005028 return SQLITE_OK;
5029
drhac69b052004-05-12 13:30:07 +00005030stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00005031 if( pPager->pInStmt ){
5032 sqlite3BitvecDestroy(pPager->pInStmt);
5033 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00005034 }
5035 return rc;
5036}
drh86f8c192007-08-22 00:39:19 +00005037int sqlite3PagerStmtBegin(Pager *pPager){
5038 int rc;
5039 pagerEnter(pPager);
5040 rc = pagerStmtBegin(pPager);
5041 pagerLeave(pPager);
5042 return rc;
5043}
drhfa86c412002-02-02 15:01:15 +00005044
5045/*
drhac69b052004-05-12 13:30:07 +00005046** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00005047*/
danielk19773b8a05f2007-03-19 17:44:26 +00005048int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00005049 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00005050 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00005051 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00005052 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00005053 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00005054 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00005055 sqlite3BitvecDestroy(pPager->pInStmt);
5056 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00005057 }else{
5058 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00005059 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00005060 pNext = pHist->pNextStmt;
5061 assert( pHist->inStmt );
5062 pHist->inStmt = 0;
5063 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhfacf0302008-06-17 15:12:00 +00005064 sqlite3PageFree(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00005065 pHist->pStmt = 0;
5066 }
5067 }
5068 pPager->stmtNRec = 0;
5069 pPager->stmtInUse = 0;
5070 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00005071 }
drhac69b052004-05-12 13:30:07 +00005072 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00005073 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00005074 return SQLITE_OK;
5075}
5076
5077/*
drhac69b052004-05-12 13:30:07 +00005078** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00005079*/
danielk19773b8a05f2007-03-19 17:44:26 +00005080int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00005081 int rc;
drh86f8c192007-08-22 00:39:19 +00005082 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00005083 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00005084 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00005085 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00005086 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00005087 PgHistory *pHist;
5088 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
5089 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00005090 if( pHist->pStmt ){
5091 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drhfacf0302008-06-17 15:12:00 +00005092 sqlite3PageFree(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00005093 pHist->pStmt = 0;
5094 }
5095 }
5096 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00005097 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00005098 rc = SQLITE_OK;
5099 }else{
5100 rc = pager_stmt_playback(pPager);
5101 }
danielk19773b8a05f2007-03-19 17:44:26 +00005102 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00005103 }else{
5104 rc = SQLITE_OK;
5105 }
drhac69b052004-05-12 13:30:07 +00005106 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00005107 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00005108 return rc;
5109}
5110
drh73509ee2003-04-06 20:44:45 +00005111/*
5112** Return the full pathname of the database file.
5113*/
danielk19773b8a05f2007-03-19 17:44:26 +00005114const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00005115 return pPager->zFilename;
5116}
5117
drhb20ea9d2004-02-09 01:20:36 +00005118/*
drhd0679ed2007-08-28 22:24:34 +00005119** Return the VFS structure for the pager.
5120*/
5121const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
5122 return pPager->pVfs;
5123}
5124
5125/*
drhcc6bb3e2007-08-31 16:11:35 +00005126** Return the file handle for the database file associated
5127** with the pager. This might return NULL if the file has
5128** not yet been opened.
5129*/
5130sqlite3_file *sqlite3PagerFile(Pager *pPager){
5131 return pPager->fd;
5132}
5133
5134/*
danielk19775865e3d2004-06-14 06:03:57 +00005135** Return the directory of the database file.
5136*/
danielk19773b8a05f2007-03-19 17:44:26 +00005137const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005138 return pPager->zDirectory;
5139}
5140
5141/*
5142** Return the full pathname of the journal file.
5143*/
danielk19773b8a05f2007-03-19 17:44:26 +00005144const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005145 return pPager->zJournal;
5146}
5147
5148/*
drh2c8997b2005-08-27 16:36:48 +00005149** Return true if fsync() calls are disabled for this pager. Return FALSE
5150** if fsync()s are executed normally.
5151*/
danielk19773b8a05f2007-03-19 17:44:26 +00005152int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00005153 return pPager->noSync;
5154}
5155
drh7c4ac0c2007-04-05 11:25:58 +00005156#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00005157/*
drhb20ea9d2004-02-09 01:20:36 +00005158** Set the codec for this pager
5159*/
danielk19773b8a05f2007-03-19 17:44:26 +00005160void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00005161 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00005162 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00005163 void *pCodecArg
5164){
5165 pPager->xCodec = xCodec;
5166 pPager->pCodecArg = pCodecArg;
5167}
drh7c4ac0c2007-04-05 11:25:58 +00005168#endif
drhb20ea9d2004-02-09 01:20:36 +00005169
danielk1977687566d2004-11-02 12:56:41 +00005170#ifndef SQLITE_OMIT_AUTOVACUUM
5171/*
danielk197787c29a92008-01-18 11:33:16 +00005172** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00005173**
drh5e385312007-06-16 04:42:12 +00005174** There must be no references to the page previously located at
5175** pgno (which we call pPgOld) though that page is allowed to be
5176** in cache. If the page previous located at pgno is not already
5177** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00005178**
drh5e385312007-06-16 04:42:12 +00005179** References to the page pPg remain valid. Updating any
5180** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00005181** allocated along with the page) is the responsibility of the caller.
5182**
danielk19775fd057a2005-03-09 13:09:43 +00005183** A transaction must be active when this routine is called. It used to be
5184** required that a statement transaction was not active, but this restriction
5185** has been removed (CREATE INDEX needs to move a page when a statement
5186** transaction is active).
danielk19774c999992008-07-16 18:17:55 +00005187**
5188** If the fourth argument, isCommit, is non-zero, then this page is being
5189** moved as part of a database reorganization just before the transaction
5190** is being committed. In this case, it is guaranteed that the database page
5191** pPg refers to will not be written to again within this transaction.
danielk1977687566d2004-11-02 12:56:41 +00005192*/
danielk19774c999992008-07-16 18:17:55 +00005193int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
drh5e385312007-06-16 04:42:12 +00005194 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00005195 int h;
danielk197794daf7f2004-11-08 09:26:09 +00005196 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00005197
drh86f8c192007-08-22 00:39:19 +00005198 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005199 assert( pPg->nRef>0 );
5200
drh4f0c5872007-03-26 22:05:01 +00005201 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00005202 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00005203 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00005204
danielk1977b4626a32007-04-28 15:47:43 +00005205 pager_get_content(pPg);
danielk19774c999992008-07-16 18:17:55 +00005206
5207 /* If the journal needs to be sync()ed before page pPg->pgno can
5208 ** be written to, store pPg->pgno in local variable needSyncPgno.
5209 **
5210 ** If the isCommit flag is set, there is no need to remember that
5211 ** the journal needs to be sync()ed before database page pPg->pgno
5212 ** can be written to. The caller has already promised not to write to it.
5213 */
5214 if( pPg->needSync && !isCommit ){
danielk197794daf7f2004-11-08 09:26:09 +00005215 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00005216 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00005217 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00005218 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00005219 }
5220
drh85b623f2007-12-13 21:54:09 +00005221 /* Unlink pPg from its hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00005222 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00005223
danielk1977ef73ee92004-11-06 12:26:07 +00005224 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00005225 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00005226 ** page pgno before the 'move' operation, it needs to be retained
5227 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00005228 */
drh5e385312007-06-16 04:42:12 +00005229 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00005230 pPgOld = pager_lookup(pPager, pgno);
5231 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00005232 assert( pPgOld->nRef==0 );
5233 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00005234 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00005235 pPg->needSync = pPgOld->needSync;
5236 }else{
5237 pPg->needSync = 0;
5238 }
drhf5e7bb52008-02-18 14:47:33 +00005239 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
danielk1977687566d2004-11-02 12:56:41 +00005240
danielk1977f5fdda82004-11-03 08:44:05 +00005241 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00005242 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00005243 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00005244 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00005245 if( pPager->aHash[h] ){
5246 assert( pPager->aHash[h]->pPrevHash==0 );
5247 pPager->aHash[h]->pPrevHash = pPg;
5248 }
5249 pPg->pNextHash = pPager->aHash[h];
5250 pPager->aHash[h] = pPg;
5251 pPg->pPrevHash = 0;
5252
drh605ad8f2006-05-03 23:34:05 +00005253 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00005254 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00005255 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00005256
danielk197794daf7f2004-11-08 09:26:09 +00005257 if( needSyncPgno ){
5258 /* If needSyncPgno is non-zero, then the journal file needs to be
5259 ** sync()ed before any data is written to database file page needSyncPgno.
5260 ** Currently, no such page exists in the page-cache and the
danielk19774c999992008-07-16 18:17:55 +00005261 ** "is journaled" bitvec flag has been set. This needs to be remedied by
5262 ** loading the page into the pager-cache and setting the PgHdr.needSync
5263 ** flag.
danielk1977ae825582004-11-23 09:06:55 +00005264 **
danielk1977a98d7b42008-01-18 13:42:54 +00005265 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00005266 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00005267 ** array. Otherwise, if the page is loaded and written again in
5268 ** this transaction, it may be written to the database file before
5269 ** it is synced into the journal file. This way, it may end up in
5270 ** the journal file twice, but that is not a problem.
5271 **
danielk19773b8a05f2007-03-19 17:44:26 +00005272 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00005273 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00005274 */
5275 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005276 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00005277 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00005278 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00005279 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00005280 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
5281 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00005282 }
danielk197787c29a92008-01-18 11:33:16 +00005283 pagerLeave(pPager);
5284 return rc;
5285 }
danielk1977ae825582004-11-23 09:06:55 +00005286 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00005287 pPgHdr->needSync = 1;
5288 pPgHdr->inJournal = 1;
5289 makeDirty(pPgHdr);
5290 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00005291 }
5292
drh86f8c192007-08-22 00:39:19 +00005293 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005294 return SQLITE_OK;
5295}
5296#endif
5297
danielk19773b8a05f2007-03-19 17:44:26 +00005298/*
5299** Return a pointer to the data for the specified page.
5300*/
5301void *sqlite3PagerGetData(DbPage *pPg){
5302 return PGHDR_TO_DATA(pPg);
5303}
5304
5305/*
5306** Return a pointer to the Pager.nExtra bytes of "extra" space
5307** allocated along with the specified page.
5308*/
5309void *sqlite3PagerGetExtra(DbPage *pPg){
5310 Pager *pPager = pPg->pPager;
5311 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
5312}
5313
danielk197741483462007-03-24 16:45:04 +00005314/*
5315** Get/set the locking-mode for this pager. Parameter eMode must be one
5316** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
5317** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
5318** the locking-mode is set to the value specified.
5319**
5320** The returned value is either PAGER_LOCKINGMODE_NORMAL or
5321** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
5322** locking-mode.
5323*/
5324int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00005325 assert( eMode==PAGER_LOCKINGMODE_QUERY
5326 || eMode==PAGER_LOCKINGMODE_NORMAL
5327 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5328 assert( PAGER_LOCKINGMODE_QUERY<0 );
5329 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5330 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00005331 pPager->exclusiveMode = eMode;
5332 }
5333 return (int)pPager->exclusiveMode;
5334}
5335
drh3b020132008-04-17 17:02:01 +00005336/*
5337** Get/set the journal-mode for this pager. Parameter eMode must be one
5338** of PAGER_JOURNALMODE_QUERY, PAGER_JOURNALMODE_DELETE or
5339** PAGER_JOURNALMODE_PERSIST. If the parameter is not _QUERY, then
5340** the journal-mode is set to the value specified.
5341**
5342** The returned value is either PAGER_JOURNALMODE_DELETE or
5343** PAGER_JOURNALMODE_PERSIST, indicating the current (possibly updated)
5344** journal-mode.
5345*/
5346int sqlite3PagerJournalMode(Pager *pPager, int eMode){
5347 assert( eMode==PAGER_JOURNALMODE_QUERY
5348 || eMode==PAGER_JOURNALMODE_DELETE
drhfdc40e92008-04-17 20:59:37 +00005349 || eMode==PAGER_JOURNALMODE_PERSIST
5350 || eMode==PAGER_JOURNALMODE_OFF );
drh3b020132008-04-17 17:02:01 +00005351 assert( PAGER_JOURNALMODE_QUERY<0 );
5352 assert( PAGER_JOURNALMODE_DELETE>=0 && PAGER_JOURNALMODE_PERSIST>=0 );
drhfdc40e92008-04-17 20:59:37 +00005353 if( eMode>=0 ){
5354 pPager->journalMode = eMode;
drh3b020132008-04-17 17:02:01 +00005355 }
drhfdc40e92008-04-17 20:59:37 +00005356 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00005357}
5358
danielk1977b53e4962008-06-04 06:45:59 +00005359/*
5360** Get/set the size-limit used for persistent journal files.
5361*/
5362i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
5363 if( iLimit>=-1 ){
5364 pPager->journalSizeLimit = iLimit;
5365 }
5366 return pPager->journalSizeLimit;
5367}
5368
drh2e66f0b2005-04-28 17:18:48 +00005369#endif /* SQLITE_OMIT_DISKIO */