blob: 58b69cb2d3890e3a3696bdacb7effee86f0217db [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**
drhd138c012008-05-13 00:58:18 +000021** @(#) $Id: pager.c,v 1.445 2008/05/13 00:58:18 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000026#include <string.h>
drhed7c8552001-04-11 14:29:21 +000027
28/*
drhdb48ee02003-01-16 13:42:43 +000029** Macros for troubleshooting. Normally turned off
30*/
danielk1977466be562004-06-10 02:16:01 +000031#if 0
drhd3627af2006-12-18 18:34:51 +000032#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000033#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
34#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
35#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
36#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
37#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000038#else
drh4f0c5872007-03-26 22:05:01 +000039#define PAGERTRACE1(X)
40#define PAGERTRACE2(X,Y)
41#define PAGERTRACE3(X,Y,Z)
42#define PAGERTRACE4(X,Y,Z,W)
43#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000044#endif
45
danielk1977599fcba2004-11-08 07:13:13 +000046/*
drh4f0c5872007-03-26 22:05:01 +000047** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000048** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000049**
drh85b623f2007-12-13 21:54:09 +000050** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000051** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000052** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000053*/
drhc001c582006-03-06 18:23:16 +000054#define PAGERID(p) ((int)(p->fd))
55#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000056
57/*
drhed7c8552001-04-11 14:29:21 +000058** The page cache as a whole is always in one of the following
59** states:
60**
drha6abd042004-06-09 17:37:22 +000061** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000062** writing the database file. There is no
63** data held in memory. This is the initial
64** state.
65**
drha6abd042004-06-09 17:37:22 +000066** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000067** Writing is not permitted. There can be
68** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000069** file at the same time.
drhed7c8552001-04-11 14:29:21 +000070**
drh726de592004-06-10 23:35:50 +000071** PAGER_RESERVED This process has reserved the database for writing
72** but has not yet made any changes. Only one process
73** at a time can reserve the database. The original
74** database file has not been modified so other
75** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000076** database file.
77**
78** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000079** Access is exclusive. No other processes or
80** threads can be reading or writing while one
81** process is writing.
82**
danielk1977aa5ccdf2004-06-14 05:10:42 +000083** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
84** after all dirty pages have been written to the
85** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000086** disk. All that remains to do is to remove or
87** truncate the journal file and the transaction
88** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000089**
drha6abd042004-06-09 17:37:22 +000090** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000091** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000092** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000093** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000094** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000095** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000096** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000097** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000098** PAGER_RESERVED means that there is an open rollback journal.
99** The transition to PAGER_EXCLUSIVE occurs before any changes
100** are made to the database file, though writes to the rollback
101** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
102** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
103** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000104*/
drha6abd042004-06-09 17:37:22 +0000105#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000106#define PAGER_SHARED 1 /* same as SHARED_LOCK */
107#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
108#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
109#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000110
drh684917c2004-10-05 02:41:42 +0000111/*
112** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
113** then failed attempts to get a reserved lock will invoke the busy callback.
114** This is off by default. To see why, consider the following scenario:
115**
116** Suppose thread A already has a shared lock and wants a reserved lock.
117** Thread B already has a reserved lock and wants an exclusive lock. If
118** both threads are using their busy callbacks, it might be a long time
119** be for one of the threads give up and allows the other to proceed.
120** But if the thread trying to get the reserved lock gives up quickly
121** (if it never invokes its busy callback) then the contention will be
122** resolved quickly.
123*/
124#ifndef SQLITE_BUSY_RESERVED_LOCK
125# define SQLITE_BUSY_RESERVED_LOCK 0
126#endif
drhd9b02572001-04-15 00:37:09 +0000127
drhed7c8552001-04-11 14:29:21 +0000128/*
drh887dc4c2004-10-22 16:22:57 +0000129** This macro rounds values up so that if the value is an address it
130** is guaranteed to be an address that is aligned to an 8-byte boundary.
131*/
132#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
133
danielk19779f61c2f2007-08-27 17:27:49 +0000134typedef struct PgHdr PgHdr;
danielk197784f786f2007-08-28 08:00:17 +0000135
136/*
137** Each pager stores all currently unreferenced pages in a list sorted
138** in least-recently-used (LRU) order (i.e. the first item on the list has
139** not been referenced in a long time, the last item has been recently
140** used). An instance of this structure is included as part of each
141** pager structure for this purpose (variable Pager.lru).
142**
143** Additionally, if memory-management is enabled, all unreferenced pages
144** are stored in a global LRU list (global variable sqlite3LruPageList).
145**
146** In both cases, the PagerLruList.pFirstSynced variable points to
147** the first page in the corresponding list that does not require an
drh85b623f2007-12-13 21:54:09 +0000148** fsync() operation before its memory can be reclaimed. If no such
danielk197784f786f2007-08-28 08:00:17 +0000149** page exists, PagerLruList.pFirstSynced is set to NULL.
150*/
151typedef struct PagerLruList PagerLruList;
152struct PagerLruList {
153 PgHdr *pFirst; /* First page in LRU list */
154 PgHdr *pLast; /* Last page in LRU list (the most recently used) */
155 PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0 */
156};
157
158/*
159** The following structure contains the next and previous pointers used
160** to link a PgHdr structure into a PagerLruList linked list.
161*/
danielk19779f61c2f2007-08-27 17:27:49 +0000162typedef struct PagerLruLink PagerLruLink;
163struct PagerLruLink {
164 PgHdr *pNext;
165 PgHdr *pPrev;
166};
167
drh887dc4c2004-10-22 16:22:57 +0000168/*
drhed7c8552001-04-11 14:29:21 +0000169** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +0000170** This header is only visible to this pager module. The client
171** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +0000172**
danielk19773b8a05f2007-03-19 17:44:26 +0000173** Client code should call sqlite3PagerWrite() on a page prior to making
174** any modifications to that page. The first time sqlite3PagerWrite()
drhf6038712004-02-08 18:07:34 +0000175** is called, the original page contents are written into the rollback
176** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
177** the journal page has made it onto the disk surface, PgHdr.needSync
178** is cleared. The modified page cannot be written back into the original
179** database file until the journal pages has been synced to disk and the
180** PgHdr.needSync has been cleared.
181**
danielk19773b8a05f2007-03-19 17:44:26 +0000182** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
drhf6038712004-02-08 18:07:34 +0000183** is cleared again when the page content is written back to the original
184** database file.
drh732c8172007-06-16 11:17:45 +0000185**
186** Details of important structure elements:
187**
188** needSync
189**
190** If this is true, this means that it is not safe to write the page
191** content to the database because the original content needed
192** for rollback has not by synced to the main rollback journal.
193** The original content may have been written to the rollback journal
194** but it has not yet been synced. So we cannot write to the database
195** file because power failure might cause the page in the journal file
196** to never reach the disk. It is as if the write to the journal file
197** does not occur until the journal file is synced.
198**
199** This flag is false if the page content exactly matches what
200** currently exists in the database file. The needSync flag is also
201** false if the original content has been written to the main rollback
202** journal and synced. If the page represents a new page that has
203** been added onto the end of the database during the current
204** transaction, the needSync flag is true until the original database
205** size in the journal header has been synced to disk.
206**
207** inJournal
208**
209** This is true if the original page has been written into the main
210** rollback journal. This is always false for new pages added to
211** the end of the database file during the current transaction.
212** And this flag says nothing about whether or not the journal
213** has been synced to disk. For pages that are in the original
214** database file, the following expression should always be true:
215**
drhf5e7bb52008-02-18 14:47:33 +0000216** inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno)
drh732c8172007-06-16 11:17:45 +0000217**
drhf5e7bb52008-02-18 14:47:33 +0000218** The pPager->pInJournal object is only valid for the original
drh732c8172007-06-16 11:17:45 +0000219** pages of the database, not new pages that are added to the end
220** of the database, so obviously the above expression cannot be
221** valid for new pages. For new pages inJournal is always 0.
222**
223** dirty
224**
225** When true, this means that the content of the page has been
226** modified and needs to be written back to the database file.
227** If false, it means that either the content of the page is
228** unchanged or else the content is unimportant and we do not
229** care whether or not it is preserved.
230**
231** alwaysRollback
232**
233** This means that the sqlite3PagerDontRollback() API should be
234** ignored for this page. The DontRollback() API attempts to say
235** that the content of the page on disk is unimportant (it is an
236** unused page on the freelist) so that it is unnecessary to
237** rollback changes to this page because the content of the page
238** can change without changing the meaning of the database. This
239** flag overrides any DontRollback() attempt. This flag is set
240** when a page that originally contained valid data is added to
241** the freelist. Later in the same transaction, this page might
242** be pulled from the freelist and reused for something different
243** and at that point the DontRollback() API will be called because
244** pages taken from the freelist do not need to be protected by
245** the rollback journal. But this flag says that the page was
246** not originally part of the freelist so that it still needs to
247** be rolled back in spite of any subsequent DontRollback() calls.
248**
249** needRead
250**
251** This flag means (when true) that the content of the page has
252** not yet been loaded from disk. The in-memory content is just
253** garbage. (Actually, we zero the content, but you should not
254** make any assumptions about the content nevertheless.) If the
255** content is needed in the future, it should be read from the
256** original database file.
drhed7c8552001-04-11 14:29:21 +0000257*/
258struct PgHdr {
259 Pager *pPager; /* The pager to which this page belongs */
260 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000261 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
danielk19779f61c2f2007-08-27 17:27:49 +0000262 PagerLruLink free; /* Next and previous free pages */
drhac69b052004-05-12 13:30:07 +0000263 PgHdr *pNextAll; /* A list of all pages */
drh193a6b42002-07-07 16:52:46 +0000264 u8 inJournal; /* TRUE if has been written to journal */
drh193a6b42002-07-07 16:52:46 +0000265 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000266 u8 needSync; /* Sync journal before writing this page */
drh80e35f42007-03-30 14:06:34 +0000267 u8 alwaysRollback; /* Disable DontRollback() for this page */
drh538f5702007-04-13 02:14:30 +0000268 u8 needRead; /* Read content if PagerWrite() is called */
drhac69b052004-05-12 13:30:07 +0000269 short int nRef; /* Number of users of this page */
drhdc3e10b2006-06-15 14:31:06 +0000270 PgHdr *pDirty, *pPrevDirty; /* Dirty pages */
danielk19779f61c2f2007-08-27 17:27:49 +0000271#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
272 PagerLruLink gfree; /* Global list of nRef==0 pages */
273#endif
danielk19773c407372005-02-15 02:54:14 +0000274#ifdef SQLITE_CHECK_PAGES
275 u32 pageHash;
276#endif
drhbf4bca52007-09-06 22:19:14 +0000277 void *pData; /* Page data */
278 /* Pager.nExtra bytes of local data appended to this header */
drhed7c8552001-04-11 14:29:21 +0000279};
280
drhac69b052004-05-12 13:30:07 +0000281/*
282** For an in-memory only database, some extra information is recorded about
283** each page so that changes can be rolled back. (Journal files are not
284** used for in-memory databases.) The following information is added to
285** the end of every EXTRA block for in-memory databases.
286**
287** This information could have been added directly to the PgHdr structure.
288** But then it would take up an extra 8 bytes of storage on every PgHdr
289** even for disk-based databases. Splitting it out saves 8 bytes. This
290** is only a savings of 0.8% but those percentages add up.
291*/
292typedef struct PgHistory PgHistory;
293struct PgHistory {
294 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
295 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000296 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
297 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000298};
drh9eb9e262004-02-11 02:18:05 +0000299
300/*
301** A macro used for invoking the codec if there is one
302*/
303#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000304# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
305# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000306#else
drhc001c582006-03-06 18:23:16 +0000307# define CODEC1(P,D,N,X) /* NO-OP */
308# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000309#endif
310
drhed7c8552001-04-11 14:29:21 +0000311/*
drh69688d52001-04-14 16:38:23 +0000312** Convert a pointer to a PgHdr into a pointer to its data
313** and back again.
drhed7c8552001-04-11 14:29:21 +0000314*/
drhbf4bca52007-09-06 22:19:14 +0000315#define PGHDR_TO_DATA(P) ((P)->pData)
316#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
drhac69b052004-05-12 13:30:07 +0000317#define PGHDR_TO_HIST(P,PGR) \
drhbf4bca52007-09-06 22:19:14 +0000318 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000319
320/*
drhed7c8552001-04-11 14:29:21 +0000321** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000322**
drh4f0ee682007-03-30 20:43:40 +0000323** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000324** or SQLITE_FULL. Once one of the first three errors occurs, it persists
325** and is returned as the result of every major pager API call. The
326** SQLITE_FULL return code is slightly different. It persists only until the
327** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000328** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000329** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000330*/
331struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000332 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000333 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000334 u8 journalStarted; /* True if header of journal is synced */
335 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000336 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000337 u8 stmtOpen; /* True if the statement subjournal is open */
338 u8 stmtInUse; /* True we are in a statement subtransaction */
339 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000340 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000341 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000342 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000343 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000344 u8 tempFile; /* zFilename is a temporary file */
345 u8 readOnly; /* True for a read-only database */
346 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000347 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000348 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000349 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000350 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000351 u8 doNotSync; /* Boolean. While true, do not spill the cache */
352 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000353 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drhd138c012008-05-13 00:58:18 +0000354 u8 dbModified; /* True if there are any changes to the Db */
drh80e35f42007-03-30 14:06:34 +0000355 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000356 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000357 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000358 int dbSize; /* Number of pages in the file */
359 int origDbSize; /* dbSize before the current change */
360 int stmtSize; /* Size of database (in pages) at stmt_begin() */
361 int nRec; /* Number of pages written to the journal */
362 u32 cksumInit; /* Quasi-random value added to every checksum */
363 int stmtNRec; /* Number of records in stmt subjournal */
364 int nExtra; /* Add this many bytes to each in-memory page */
365 int pageSize; /* Number of bytes in a page */
366 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000367 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
368 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000369 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000370 Bitvec *pInJournal; /* One bit for each page in the database file */
371 Bitvec *pInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000372 char *zFilename; /* Name of the database file */
373 char *zJournal; /* Name of the journal file */
374 char *zDirectory; /* Directory hold database and journal files */
drh334b2992007-09-06 23:28:23 +0000375 char *zStmtJrnl; /* Name of the statement journal file */
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 */
404 int iInUseMM; /* Non-zero if unavailable to MM */
405 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 */
drhd9b02572001-04-15 00:37:09 +0000409};
410
411/*
drh538f5702007-04-13 02:14:30 +0000412** The following global variables hold counters used for
413** testing purposes only. These variables do not exist in
414** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000415*/
416#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000417int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
418int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
419int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
420int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
421# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000422#else
drh538f5702007-04-13 02:14:30 +0000423# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000424#endif
425
drh86f8c192007-08-22 00:39:19 +0000426/*
427** The following variable points to the head of a double-linked list
428** of all pagers that are eligible for page stealing by the
429** sqlite3_release_memory() interface. Access to this list is
430** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
431*/
432#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
433static Pager *sqlite3PagerList = 0;
danielk19779f61c2f2007-08-27 17:27:49 +0000434static PagerLruList sqlite3LruPageList = {0, 0, 0};
drh86f8c192007-08-22 00:39:19 +0000435#endif
drh538f5702007-04-13 02:14:30 +0000436
437
drhfcd35c72005-05-21 02:48:08 +0000438/*
drh5e00f6c2001-09-13 13:46:56 +0000439** Journal files begin with the following magic string. The data
440** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000441**
drhae2b40c2004-06-09 19:03:54 +0000442** Since version 2.8.0, the journal format contains additional sanity
443** checking information. If the power fails while the journal is begin
444** written, semi-random garbage data might appear in the journal
445** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000446** to roll the journal back, the database could be corrupted. The additional
447** sanity checking data is an attempt to discover the garbage in the
448** journal and ignore it.
449**
drhae2b40c2004-06-09 19:03:54 +0000450** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000451** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000452** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000453** This cksum is initialized to a 32-bit random value that appears in the
454** journal file right after the header. The random initializer is important,
455** because garbage data that appears at the end of a journal is likely
456** data that was once in other files that have now been deleted. If the
457** garbage data came from an obsolete journal file, the checksums might
458** be correct. But by initializing the checksum to random value which
459** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000460*/
drhae2b40c2004-06-09 19:03:54 +0000461static const unsigned char aJournalMagic[] = {
462 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000463};
464
465/*
drh726de592004-06-10 23:35:50 +0000466** The size of the header and of each page in the journal is determined
467** by the following macros.
drh968af522003-02-11 14:55:40 +0000468*/
drhae2b40c2004-06-09 19:03:54 +0000469#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000470
danielk197776572402004-06-25 02:38:54 +0000471/*
472** The journal header size for this pager. In the future, this could be
473** set to some value read from the disk controller. The important
474** characteristic is that it is the same size as a disk sector.
475*/
476#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
477
drhb7f91642004-10-31 02:22:47 +0000478/*
479** The macro MEMDB is true if we are dealing with an in-memory database.
480** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
481** the value of MEMDB will be a constant and the compiler will optimize
482** out code that would never execute.
483*/
484#ifdef SQLITE_OMIT_MEMORYDB
485# define MEMDB 0
486#else
487# define MEMDB pPager->memDb
488#endif
489
490/*
danielk197776572402004-06-25 02:38:54 +0000491** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
492** reserved for working around a windows/posix incompatibility). It is
493** used in the journal to signify that the remainder of the journal file
494** is devoted to storing a master journal name - there are no more pages to
495** roll back. See comments for function writeMasterJournal() for details.
496*/
danielk1977599fcba2004-11-08 07:13:13 +0000497/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
498#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000499
drh968af522003-02-11 14:55:40 +0000500/*
danielk197726836652005-01-17 01:33:13 +0000501** The maximum legal page number is (2^31 - 1).
502*/
503#define PAGER_MAX_PGNO 2147483647
504
505/*
drh86f8c192007-08-22 00:39:19 +0000506** The pagerEnter() and pagerLeave() routines acquire and release
507** a mutex on each pager. The mutex is recursive.
508**
509** This is a special-purpose mutex. It only provides mutual exclusion
510** between the Btree and the Memory Management sqlite3_release_memory()
511** function. It does not prevent, for example, two Btrees from accessing
512** the same pager at the same time. Other general-purpose mutexes in
513** the btree layer handle that chore.
514*/
515#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
516 static void pagerEnter(Pager *p){
517 p->iInUseDB++;
518 if( p->iInUseMM && p->iInUseDB==1 ){
drh26e4a8b2008-05-01 17:16:52 +0000519#ifndef SQLITE_MUTEX_NOOP
drh64e2bb72008-05-07 12:29:55 +0000520 sqlite3_mutex *mutex;
drh86f8c192007-08-22 00:39:19 +0000521 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +0000522#endif
drh86f8c192007-08-22 00:39:19 +0000523 p->iInUseDB = 0;
524 sqlite3_mutex_enter(mutex);
525 p->iInUseDB = 1;
526 sqlite3_mutex_leave(mutex);
527 }
528 assert( p->iInUseMM==0 );
529 }
530 static void pagerLeave(Pager *p){
531 p->iInUseDB--;
532 assert( p->iInUseDB>=0 );
533 }
534#else
535# define pagerEnter(X)
536# define pagerLeave(X)
537#endif
538
539/*
danielk197784f786f2007-08-28 08:00:17 +0000540** Add page pPg to the end of the linked list managed by structure
541** pList (pPg becomes the last entry in the list - the most recently
542** used). Argument pLink should point to either pPg->free or pPg->gfree,
543** depending on whether pPg is being added to the pager-specific or
544** global LRU list.
545*/
danielk19779f61c2f2007-08-27 17:27:49 +0000546static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
547 pLink->pNext = 0;
548 pLink->pPrev = pList->pLast;
549
danielk197784f786f2007-08-28 08:00:17 +0000550#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
551 assert(pLink==&pPg->free || pLink==&pPg->gfree);
552 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
553#endif
554
danielk19779f61c2f2007-08-27 17:27:49 +0000555 if( pList->pLast ){
556 int iOff = (char *)pLink - (char *)pPg;
557 PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
558 pLastLink->pNext = pPg;
559 }else{
560 assert(!pList->pFirst);
561 pList->pFirst = pPg;
562 }
563
564 pList->pLast = pPg;
565 if( !pList->pFirstSynced && pPg->needSync==0 ){
566 pList->pFirstSynced = pPg;
567 }
568}
569
danielk197784f786f2007-08-28 08:00:17 +0000570/*
571** Remove pPg from the list managed by the structure pointed to by pList.
572**
573** Argument pLink should point to either pPg->free or pPg->gfree, depending
574** on whether pPg is being added to the pager-specific or global LRU list.
575*/
danielk19779f61c2f2007-08-27 17:27:49 +0000576static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
577 int iOff = (char *)pLink - (char *)pPg;
578
danielk197784f786f2007-08-28 08:00:17 +0000579#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
580 assert(pLink==&pPg->free || pLink==&pPg->gfree);
581 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
582#endif
583
danielk19779f61c2f2007-08-27 17:27:49 +0000584 if( pPg==pList->pFirst ){
585 pList->pFirst = pLink->pNext;
586 }
587 if( pPg==pList->pLast ){
588 pList->pLast = pLink->pPrev;
589 }
590 if( pLink->pPrev ){
591 PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
592 pPrevLink->pNext = pLink->pNext;
593 }
594 if( pLink->pNext ){
595 PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
596 pNextLink->pPrev = pLink->pPrev;
597 }
598 if( pPg==pList->pFirstSynced ){
599 PgHdr *p = pLink->pNext;
600 while( p && p->needSync ){
601 PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
602 p = pL->pNext;
603 }
604 pList->pFirstSynced = p;
605 }
606
607 pLink->pNext = pLink->pPrev = 0;
608}
609
610/*
danielk197784f786f2007-08-28 08:00:17 +0000611** Add page pPg to the list of free pages for the pager. If
612** memory-management is enabled, also add the page to the global
613** list of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000614*/
615static void lruListAdd(PgHdr *pPg){
616 listAdd(&pPg->pPager->lru, &pPg->free, pPg);
617#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
618 if( !pPg->pPager->memDb ){
619 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
620 listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
621 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
622 }
623#endif
624}
625
626/*
danielk197784f786f2007-08-28 08:00:17 +0000627** Remove page pPg from the list of free pages for the associated pager.
628** If memory-management is enabled, also remove pPg from the global list
629** of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000630*/
631static void lruListRemove(PgHdr *pPg){
632 listRemove(&pPg->pPager->lru, &pPg->free, pPg);
633#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
634 if( !pPg->pPager->memDb ){
635 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
636 listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
637 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
638 }
639#endif
640}
641
642/*
danielk197784f786f2007-08-28 08:00:17 +0000643** This function is called just after the needSync flag has been cleared
644** from all pages managed by pPager (usually because the journal file
645** has just been synced). It updates the pPager->lru.pFirstSynced variable
646** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
647** variable also.
danielk19779f61c2f2007-08-27 17:27:49 +0000648*/
649static void lruListSetFirstSynced(Pager *pPager){
650 pPager->lru.pFirstSynced = pPager->lru.pFirst;
651#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
652 if( !pPager->memDb ){
653 PgHdr *p;
654 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
655 for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
656 assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
657 sqlite3LruPageList.pFirstSynced = p;
658 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
659 }
660#endif
661}
662
danielk1977f35843b2007-04-07 15:03:17 +0000663/*
664** Return true if page *pPg has already been written to the statement
665** journal (or statement snapshot has been created, if *pPg is part
666** of an in-memory database).
667*/
668static int pageInStatement(PgHdr *pPg){
669 Pager *pPager = pPg->pPager;
670 if( MEMDB ){
671 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
672 }else{
drhf5e7bb52008-02-18 14:47:33 +0000673 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000674 }
675}
drh8ca0c722006-05-07 17:49:38 +0000676
677/*
678** Change the size of the pager hash table to N. N must be a power
679** of two.
680*/
681static void pager_resize_hash_table(Pager *pPager, int N){
682 PgHdr **aHash, *pPg;
683 assert( N>0 && (N&(N-1))==0 );
drheee4c8c2008-02-18 22:24:57 +0000684#ifdef SQLITE_MALLOC_SOFT_LIMIT
685 if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){
686 N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]);
687 }
688 if( N==pPager->nHash ) return;
689#endif
drhed138fb2007-08-22 22:04:37 +0000690 pagerLeave(pPager);
drh643167f2008-01-22 21:30:53 +0000691 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pPager->aHash!=0);
drh17435752007-08-16 04:30:38 +0000692 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drh643167f2008-01-22 21:30:53 +0000693 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0);
drhed138fb2007-08-22 22:04:37 +0000694 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000695 if( aHash==0 ){
696 /* Failure to rehash is not an error. It is only a performance hit. */
697 return;
698 }
drh17435752007-08-16 04:30:38 +0000699 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000700 pPager->nHash = N;
701 pPager->aHash = aHash;
702 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000703 int h;
704 if( pPg->pgno==0 ){
705 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
706 continue;
707 }
708 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000709 pPg->pNextHash = aHash[h];
710 if( aHash[h] ){
711 aHash[h]->pPrevHash = pPg;
712 }
713 aHash[h] = pPg;
714 pPg->pPrevHash = 0;
715 }
716}
717
drhdd793422001-06-28 01:54:48 +0000718/*
drh34e79ce2004-02-08 06:05:46 +0000719** Read a 32-bit integer from the given file descriptor. Store the integer
720** that is read in *pRes. Return SQLITE_OK if everything worked, or an
721** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000722**
723** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000724*/
danielk197762079062007-08-15 17:08:46 +0000725static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000726 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000727 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000728 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000729 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000730 }
drh94f33312002-08-12 12:29:56 +0000731 return rc;
732}
733
734/*
drh97b57482006-01-10 20:32:32 +0000735** Write a 32-bit integer into a string buffer in big-endian byte order.
736*/
drha3152892007-05-05 11:48:52 +0000737#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000738
739/*
drh34e79ce2004-02-08 06:05:46 +0000740** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
741** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000742*/
danielk197762079062007-08-15 17:08:46 +0000743static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000744 char ac[4];
drh97b57482006-01-10 20:32:32 +0000745 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000746 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000747}
748
drh2554f8b2003-01-22 01:26:44 +0000749/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000750** If file pFd is open, call sqlite3OsUnlock() on it.
751*/
752static int osUnlock(sqlite3_file *pFd, int eLock){
753 if( !pFd->pMethods ){
754 return SQLITE_OK;
755 }
756 return sqlite3OsUnlock(pFd, eLock);
757}
758
759/*
danielk1977c7b60172007-08-22 11:22:03 +0000760** This function determines whether or not the atomic-write optimization
761** can be used with this pager. The optimization can be used if:
762**
763** (a) the value returned by OsDeviceCharacteristics() indicates that
764** a database page may be written atomically, and
765** (b) the value returned by OsSectorSize() is less than or equal
766** to the page size.
767**
768** If the optimization cannot be used, 0 is returned. If it can be used,
769** then the value returned is the size of the journal file when it
770** contains rollback data for exactly one page.
771*/
772#ifdef SQLITE_ENABLE_ATOMIC_WRITE
773static int jrnlBufferSize(Pager *pPager){
774 int dc; /* Device characteristics */
775 int nSector; /* Sector size */
776 int nPage; /* Page size */
777 sqlite3_file *fd = pPager->fd;
778
779 if( fd->pMethods ){
780 dc = sqlite3OsDeviceCharacteristics(fd);
781 nSector = sqlite3OsSectorSize(fd);
782 nPage = pPager->pageSize;
783 }
784
785 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
786 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
787
danielk1977f8940ae2007-08-23 11:07:10 +0000788 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000789 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
790 }
791 return 0;
792}
793#endif
794
795/*
danielk1977aef0bf62005-12-30 16:28:01 +0000796** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000797** code. The first argument is a pointer to the pager structure, the
798** second the error-code about to be returned by a pager API function.
799** The value returned is a copy of the second argument to this function.
800**
drh4f0ee682007-03-30 20:43:40 +0000801** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000802** the error becomes persistent. Until the persisten error is cleared,
803** subsequent API calls on this Pager will immediately return the same
804** error code.
805**
806** A persistent error indicates that the contents of the pager-cache
807** cannot be trusted. This state can be cleared by completely discarding
808** the contents of the pager-cache. If a transaction was active when
809** the persistent error occured, then the rollback journal may need
810** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000811*/
danielk1977ae72d982007-10-03 08:46:44 +0000812static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000813static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000814 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000815 assert(
816 pPager->errCode==SQLITE_FULL ||
817 pPager->errCode==SQLITE_OK ||
818 (pPager->errCode & 0xff)==SQLITE_IOERR
819 );
danielk1977979f38e2007-03-27 16:19:51 +0000820 if(
drh4ac285a2006-09-15 07:28:50 +0000821 rc2==SQLITE_FULL ||
822 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000823 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000824 ){
825 pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000826 if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
827 /* If the pager is already unlocked, call pager_unlock() now to
828 ** clear the error state and ensure that the pager-cache is
829 ** completely empty.
830 */
831 pager_unlock(pPager);
832 }
danielk1977aef0bf62005-12-30 16:28:01 +0000833 }
834 return rc;
835}
836
drh477731b2007-06-16 03:06:27 +0000837/*
838** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
839** on the cache using a hash function. This is used for testing
840** and debugging only.
841*/
danielk19773c407372005-02-15 02:54:14 +0000842#ifdef SQLITE_CHECK_PAGES
843/*
844** Return a 32-bit hash of the page data for pPage.
845*/
drh477731b2007-06-16 03:06:27 +0000846static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000847 u32 hash = 0;
848 int i;
drh477731b2007-06-16 03:06:27 +0000849 for(i=0; i<nByte; i++){
850 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000851 }
852 return hash;
853}
drh477731b2007-06-16 03:06:27 +0000854static u32 pager_pagehash(PgHdr *pPage){
855 return pager_datahash(pPage->pPager->pageSize,
856 (unsigned char *)PGHDR_TO_DATA(pPage));
857}
danielk19773c407372005-02-15 02:54:14 +0000858
859/*
860** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
861** is defined, and NDEBUG is not defined, an assert() statement checks
862** that the page is either dirty or still matches the calculated page-hash.
863*/
864#define CHECK_PAGE(x) checkPage(x)
865static void checkPage(PgHdr *pPg){
866 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000867 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000868 pPg->pageHash==pager_pagehash(pPg) );
869}
870
871#else
drh8ffa8172007-06-18 17:25:17 +0000872#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000873#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000874#define CHECK_PAGE(x)
875#endif
876
drhed7c8552001-04-11 14:29:21 +0000877/*
danielk197776572402004-06-25 02:38:54 +0000878** When this is called the journal file for pager pPager must be open.
879** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000880** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000881**
danielk197765839c62007-08-30 08:08:17 +0000882** zMaster must point to a buffer of at least nMaster bytes allocated by
883** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
884** enough space to write the master journal name). If the master journal
885** name in the journal is longer than nMaster bytes (including a
886** nul-terminator), then this is handled as if no master journal name
887** were present in the journal.
888**
889** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000890** SQLITE_OK returned.
891*/
danielk197765839c62007-08-30 08:08:17 +0000892static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000893 int rc;
894 u32 len;
drheb206252004-10-01 02:00:31 +0000895 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000896 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000897 int i;
danielk197776572402004-06-25 02:38:54 +0000898 unsigned char aMagic[8]; /* A buffer to hold the magic header */
899
danielk197765839c62007-08-30 08:08:17 +0000900 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000901
drh054889e2005-11-30 03:20:31 +0000902 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000903 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000904
danielk197762079062007-08-15 17:08:46 +0000905 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000906 if( rc!=SQLITE_OK ) return rc;
907
danielk197765839c62007-08-30 08:08:17 +0000908 if( len>=nMaster ){
909 return SQLITE_OK;
910 }
911
danielk197762079062007-08-15 17:08:46 +0000912 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000913 if( rc!=SQLITE_OK ) return rc;
914
danielk197762079062007-08-15 17:08:46 +0000915 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000916 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
917
danielk197765839c62007-08-30 08:08:17 +0000918 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000919 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000920 return rc;
921 }
danielk197765839c62007-08-30 08:08:17 +0000922 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000923
924 /* See if the checksum matches the master journal name */
925 for(i=0; i<len; i++){
danielk197765839c62007-08-30 08:08:17 +0000926 cksum -= zMaster[i];
927 }
danielk19778191bff2004-06-28 04:52:30 +0000928 if( cksum ){
929 /* If the checksum doesn't add up, then one or more of the disk sectors
930 ** containing the master journal filename is corrupted. This means
931 ** definitely roll back, so just return SQLITE_OK and report a (nul)
932 ** master-journal filename.
933 */
danielk197765839c62007-08-30 08:08:17 +0000934 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000935 }
danielk197776572402004-06-25 02:38:54 +0000936
937 return SQLITE_OK;
938}
939
940/*
941** Seek the journal file descriptor to the next sector boundary where a
942** journal header may be read or written. Pager.journalOff is updated with
943** the new seek offset.
944**
945** i.e for a sector size of 512:
946**
947** Input Offset Output Offset
948** ---------------------------------------
949** 0 0
950** 512 512
951** 100 512
952** 2000 2048
953**
954*/
danielk197762079062007-08-15 17:08:46 +0000955static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000956 i64 offset = 0;
957 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000958 if( c ){
959 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
960 }
961 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
962 assert( offset>=c );
963 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
964 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000965}
966
967/*
drhf3a87622008-04-17 14:16:42 +0000968** Write zeros over the header of the journal file. This has the
969** effect of invalidating the journal file and committing the
970** transaction.
971*/
danielk1977df2566a2008-05-07 19:11:03 +0000972static int zeroJournalHdr(Pager *pPager, int doTruncate){
973 int rc = SQLITE_OK;
drhf3a87622008-04-17 14:16:42 +0000974 static const char zeroHdr[28];
975
danielk1977df2566a2008-05-07 19:11:03 +0000976 if( pPager->journalOff ){
977 IOTRACE(("JZEROHDR %p\n", pPager))
978 if( doTruncate ){
979 rc = sqlite3OsTruncate(pPager->jfd, 0);
980 }else{
981 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
982 }
983 if( rc==SQLITE_OK ){
984 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
985 }
drha06ecba2008-04-22 17:15:17 +0000986 }
drhf3a87622008-04-17 14:16:42 +0000987 return rc;
988}
989
990/*
danielk197776572402004-06-25 02:38:54 +0000991** The journal file must be open when this routine is called. A journal
992** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
993** current location.
994**
995** The format for the journal header is as follows:
996** - 8 bytes: Magic identifying journal format.
997** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
998** - 4 bytes: Random number used for page hash.
999** - 4 bytes: Initial database page count.
1000** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +00001001** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +00001002**
danielk197767c007b2008-03-20 04:45:49 +00001003** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +00001004*/
1005static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +00001006 int rc = SQLITE_OK;
1007 char *zHeader = pPager->pTmpSpace;
1008 int nHeader = pPager->pageSize;
1009 int nWrite;
1010
1011 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
1012 nHeader = JOURNAL_HDR_SZ(pPager);
1013 }
danielk197776572402004-06-25 02:38:54 +00001014
danielk19774099f6e2007-03-19 11:25:20 +00001015 if( pPager->stmtHdrOff==0 ){
1016 pPager->stmtHdrOff = pPager->journalOff;
1017 }
1018
danielk197762079062007-08-15 17:08:46 +00001019 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001020 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001021
drh97b57482006-01-10 20:32:32 +00001022 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +00001023
1024 /*
1025 ** Write the nRec Field - the number of page records that follow this
1026 ** journal header. Normally, zero is written to this value at this time.
1027 ** After the records are added to the journal (and the journal synced,
1028 ** if in full-sync mode), the zero is overwritten with the true number
1029 ** of records (see syncJournal()).
1030 **
1031 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
1032 ** reading the journal this value tells SQLite to assume that the
1033 ** rest of the journal file contains valid page records. This assumption
1034 ** is dangerous, as if a failure occured whilst writing to the journal
1035 ** file it may contain some garbage data. There are two scenarios
1036 ** where this risk can be ignored:
1037 **
1038 ** * When the pager is in no-sync mode. Corruption can follow a
1039 ** power failure in this case anyway.
1040 **
1041 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1042 ** that garbage data is never appended to the journal file.
1043 */
1044 assert(pPager->fd->pMethods||pPager->noSync);
1045 if( (pPager->noSync)
1046 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1047 ){
1048 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1049 }else{
1050 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1051 }
1052
drh97b57482006-01-10 20:32:32 +00001053 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +00001054 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +00001055 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1056 /* The initial database size */
1057 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1058 /* The assumed sector size for this process */
1059 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
danielk197767c007b2008-03-20 04:45:49 +00001060 if( pPager->journalHdr==0 ){
1061 /* The page size */
1062 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
1063 }
danielk197776572402004-06-25 02:38:54 +00001064
danielk1977a664f8e2008-04-22 14:31:48 +00001065 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
1066 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
1067 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
1068 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +00001069 }
danielk1977a664f8e2008-04-22 14:31:48 +00001070
danielk197776572402004-06-25 02:38:54 +00001071 return rc;
1072}
1073
1074/*
1075** The journal file must be open when this is called. A journal header file
1076** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1077** file. See comments above function writeJournalHdr() for a description of
1078** the journal header format.
1079**
1080** If the header is read successfully, *nRec is set to the number of
1081** page records following this header and *dbSize is set to the size of the
1082** database before the transaction began, in pages. Also, pPager->cksumInit
1083** is set to the value read from the journal header. SQLITE_OK is returned
1084** in this case.
1085**
1086** If the journal header file appears to be corrupted, SQLITE_DONE is
1087** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1088** cannot be read from the journal file an error code is returned.
1089*/
1090static int readJournalHdr(
1091 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001092 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001093 u32 *pNRec,
1094 u32 *pDbSize
1095){
1096 int rc;
1097 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001098 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +00001099 int iPageSize;
danielk197776572402004-06-25 02:38:54 +00001100
danielk197762079062007-08-15 17:08:46 +00001101 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001102 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1103 return SQLITE_DONE;
1104 }
danielk197762079062007-08-15 17:08:46 +00001105 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001106
danielk197762079062007-08-15 17:08:46 +00001107 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001108 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001109 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001110
1111 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1112 return SQLITE_DONE;
1113 }
1114
danielk197762079062007-08-15 17:08:46 +00001115 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001116 if( rc ) return rc;
1117
danielk197762079062007-08-15 17:08:46 +00001118 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001119 if( rc ) return rc;
1120
danielk197762079062007-08-15 17:08:46 +00001121 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001122 if( rc ) return rc;
1123
danielk197767c007b2008-03-20 04:45:49 +00001124 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
1125 if( rc==SQLITE_OK
1126 && iPageSize>=512
1127 && iPageSize<=SQLITE_MAX_PAGE_SIZE
1128 && ((iPageSize-1)&iPageSize)==0
1129 ){
1130 u16 pagesize = iPageSize;
1131 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
1132 }
1133 if( rc ) return rc;
1134
danielk197776572402004-06-25 02:38:54 +00001135 /* Update the assumed sector-size to match the value used by
1136 ** the process that created this journal. If this journal was
1137 ** created by a process other than this one, then this routine
1138 ** is being called from within pager_playback(). The local value
1139 ** of Pager.sectorSize is restored at the end of that routine.
1140 */
danielk197762079062007-08-15 17:08:46 +00001141 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001142 if( rc ) return rc;
1143
1144 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001145 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001146}
1147
1148
1149/*
1150** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001151** pPager at the current location. The master journal name must be the last
1152** thing written to a journal file. If the pager is in full-sync mode, the
1153** journal file descriptor is advanced to the next sector boundary before
1154** anything is written. The format is:
1155**
1156** + 4 bytes: PAGER_MJ_PGNO.
1157** + N bytes: length of master journal name.
1158** + 4 bytes: N
1159** + 4 bytes: Master journal name checksum.
1160** + 8 bytes: aJournalMagic[].
1161**
1162** The master journal page checksum is the sum of the bytes in the master
1163** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001164**
1165** If zMaster is a NULL pointer (occurs for a single database transaction),
1166** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001167*/
1168static int writeMasterJournal(Pager *pPager, const char *zMaster){
1169 int rc;
1170 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001171 int i;
danielk197762079062007-08-15 17:08:46 +00001172 i64 jrnlOff;
danielk1977df2566a2008-05-07 19:11:03 +00001173 i64 jrnlSize;
drh97b57482006-01-10 20:32:32 +00001174 u32 cksum = 0;
1175 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001176
1177 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1178 pPager->setMaster = 1;
1179
1180 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001181 for(i=0; i<len; i++){
1182 cksum += zMaster[i];
1183 }
danielk197776572402004-06-25 02:38:54 +00001184
1185 /* If in full-sync mode, advance to the next disk sector before writing
1186 ** the master journal name. This is in case the previous page written to
1187 ** the journal has already been synced.
1188 */
1189 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001190 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001191 }
danielk197762079062007-08-15 17:08:46 +00001192 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001193 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001194
danielk197762079062007-08-15 17:08:46 +00001195 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001196 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001197 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001198
danielk197762079062007-08-15 17:08:46 +00001199 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001200 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001201 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001202
drh97b57482006-01-10 20:32:32 +00001203 put32bits(zBuf, len);
1204 put32bits(&zBuf[4], cksum);
1205 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001206 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
danielk1977df2566a2008-05-07 19:11:03 +00001207 jrnlOff += 8+sizeof(aJournalMagic);
drh2c8997b2005-08-27 16:36:48 +00001208 pPager->needSync = !pPager->noSync;
danielk1977df2566a2008-05-07 19:11:03 +00001209
1210 /* If the pager is in peristent-journal mode, then the physical
1211 ** journal-file may extend past the end of the master-journal name
1212 ** and 8 bytes of magic data just written to the file. This is
1213 ** dangerous because the code to rollback a hot-journal file
1214 ** will not be able to find the master-journal name to determine
1215 ** whether or not the journal is hot.
1216 **
1217 ** Easiest thing to do in this scenario is to truncate the journal
1218 ** file to the required size.
1219 */
1220 if( (rc==SQLITE_OK)
1221 && (rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))==SQLITE_OK
1222 && jrnlSize>jrnlOff
1223 ){
1224 rc = sqlite3OsTruncate(pPager->jfd, jrnlOff);
1225 }
danielk197776572402004-06-25 02:38:54 +00001226 return rc;
1227}
1228
1229/*
drh03eb96a2002-11-10 23:32:56 +00001230** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001231** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001232**
1233** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001234** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001235** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001236** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001237*/
drh3aac2dd2004-04-26 14:10:20 +00001238static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001239 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001240 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1241 assert( MEMDB );
1242 if( !pHist->inStmt ){
1243 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1244 if( pPager->pStmt ){
1245 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1246 }
1247 pHist->pNextStmt = pPager->pStmt;
1248 pPager->pStmt = pPg;
1249 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001250 }
drh03eb96a2002-11-10 23:32:56 +00001251}
1252
1253/*
drhed7c8552001-04-11 14:29:21 +00001254** Find a page in the hash table given its page number. Return
1255** a pointer to the page or NULL if not found.
1256*/
drhd9b02572001-04-15 00:37:09 +00001257static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001258 PgHdr *p;
1259 if( pPager->aHash==0 ) return 0;
1260 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001261 while( p && p->pgno!=pgno ){
1262 p = p->pNextHash;
1263 }
1264 return p;
1265}
1266
1267/*
danielk1977e180dd92007-04-05 17:15:52 +00001268** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001269** sets the state of the pager back to what it was when it was first
1270** opened. Any outstanding pages are invalidated and subsequent attempts
1271** to access those pages will likely result in a coredump.
1272*/
drhd9b02572001-04-15 00:37:09 +00001273static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001274 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001275 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001276 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001277 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1278 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001279 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001280 lruListRemove(pPg);
drh0d180202008-02-14 23:26:56 +00001281 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00001282 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001283 }
danielk19779f61c2f2007-08-27 17:27:49 +00001284 assert(pPager->lru.pFirst==0);
1285 assert(pPager->lru.pFirstSynced==0);
1286 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001287 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001288 pPager->pAll = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001289 pPager->pDirty = 0;
drh8ca0c722006-05-07 17:49:38 +00001290 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001291 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001292 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001293 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001294 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001295}
1296
1297/*
danielk1977ae72d982007-10-03 08:46:44 +00001298** Unlock the database file.
1299**
1300** If the pager is currently in error state, discard the contents of
1301** the cache and reset the Pager structure internal state. If there is
1302** an open journal-file, then the next time a shared-lock is obtained
1303** on the pager file (by this or any other process), it will be
1304** treated as a hot-journal and rolled back.
1305*/
1306static void pager_unlock(Pager *pPager){
1307 if( !pPager->exclusiveMode ){
1308 if( !MEMDB ){
drh1aa5af12008-03-07 19:51:14 +00001309 int rc = osUnlock(pPager->fd, NO_LOCK);
drh308aa322008-03-07 20:14:38 +00001310 if( rc ) pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +00001311 pPager->dbSize = -1;
1312 IOTRACE(("UNLOCK %p\n", pPager))
1313
drh16e45a42008-04-19 20:34:18 +00001314 /* Always close the journal file when dropping the database lock.
1315 ** Otherwise, another connection with journal_mode=delete might
1316 ** delete the file out from under us.
1317 */
1318 if( pPager->journalOpen ){
1319 sqlite3OsClose(pPager->jfd);
1320 pPager->journalOpen = 0;
1321 sqlite3BitvecDestroy(pPager->pInJournal);
1322 pPager->pInJournal = 0;
1323 }
1324
danielk1977ae72d982007-10-03 08:46:44 +00001325 /* If Pager.errCode is set, the contents of the pager cache cannot be
1326 ** trusted. Now that the pager file is unlocked, the contents of the
1327 ** cache can be discarded and the error code safely cleared.
1328 */
1329 if( pPager->errCode ){
drh1aa5af12008-03-07 19:51:14 +00001330 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00001331 pager_reset(pPager);
1332 if( pPager->stmtOpen ){
1333 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +00001334 sqlite3BitvecDestroy(pPager->pInStmt);
1335 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001336 }
danielk1977ae72d982007-10-03 08:46:44 +00001337 pPager->stmtOpen = 0;
1338 pPager->stmtInUse = 0;
1339 pPager->journalOff = 0;
1340 pPager->journalStarted = 0;
1341 pPager->stmtAutoopen = 0;
1342 pPager->origDbSize = 0;
1343 }
1344 }
1345
1346 if( !MEMDB || pPager->errCode==SQLITE_OK ){
1347 pPager->state = PAGER_UNLOCK;
1348 pPager->changeCountDone = 0;
1349 }
1350 }
1351}
1352
1353/*
1354** Execute a rollback if a transaction is active and unlock the
1355** database file. If the pager has already entered the error state,
1356** do not attempt the rollback.
1357*/
1358static void pagerUnlockAndRollback(Pager *p){
drhfdc40e92008-04-17 20:59:37 +00001359 /* assert( p->state>=PAGER_RESERVED || p->journalOpen==0 ); */
danielk1977ae72d982007-10-03 08:46:44 +00001360 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
1361 sqlite3PagerRollback(p);
1362 }
1363 pager_unlock(p);
drhfdc40e92008-04-17 20:59:37 +00001364#if 0
danielk1977ae72d982007-10-03 08:46:44 +00001365 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1366 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drhfdc40e92008-04-17 20:59:37 +00001367#endif
danielk1977ae72d982007-10-03 08:46:44 +00001368}
1369
1370/*
drh80e35f42007-03-30 14:06:34 +00001371** This routine ends a transaction. A transaction is ended by either
1372** a COMMIT or a ROLLBACK.
1373**
drhed7c8552001-04-11 14:29:21 +00001374** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001375** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1376** the database lock and acquires a SHARED lock in its place if that is
1377** the appropriate thing to do. Release locks usually is appropriate,
1378** unless we are in exclusive access mode or unless this is a
1379** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1380**
1381** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001382**
1383** TODO: Consider keeping the journal file open for temporary databases.
1384** This might give a performance improvement on windows where opening
1385** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001386*/
danielk1977df2566a2008-05-07 19:11:03 +00001387static int pager_end_transaction(Pager *pPager, int hasMaster){
drhd9b02572001-04-15 00:37:09 +00001388 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001389 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001390 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001391 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001392 if( pPager->state<PAGER_RESERVED ){
1393 return SQLITE_OK;
1394 }
danielk19773b8a05f2007-03-19 17:44:26 +00001395 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001396 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001397 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001398 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001399 }
drhda47d772002-12-02 04:25:19 +00001400 if( pPager->journalOpen ){
danielk197793f7af92008-05-09 16:57:50 +00001401 if( pPager->exclusiveMode
1402 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1403 ){
1404 rc = zeroJournalHdr(pPager, hasMaster);
1405 pager_error(pPager, rc);
danielk197741483462007-03-24 16:45:04 +00001406 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001407 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001408 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001409 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001410 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001411 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001412 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001413 }
danielk197741483462007-03-24 16:45:04 +00001414 }
drhf5e7bb52008-02-18 14:47:33 +00001415 sqlite3BitvecDestroy(pPager->pInJournal);
1416 pPager->pInJournal = 0;
drhda47d772002-12-02 04:25:19 +00001417 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1418 pPg->inJournal = 0;
1419 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001420 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001421 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001422#ifdef SQLITE_CHECK_PAGES
1423 pPg->pageHash = pager_pagehash(pPg);
1424#endif
drhda47d772002-12-02 04:25:19 +00001425 }
drh605ad8f2006-05-03 23:34:05 +00001426 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001427 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001428 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001429 }else{
drhf5e7bb52008-02-18 14:47:33 +00001430 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001431 }
danielk1977979f38e2007-03-27 16:19:51 +00001432
danielk197741483462007-03-24 16:45:04 +00001433 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001434 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001435 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001436 }else if( pPager->state==PAGER_SYNCED ){
1437 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001438 }
danielk1977334cdb62007-03-26 08:05:12 +00001439 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001440 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001441 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001442 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001443 pPager->dbSize = -1;
drhd138c012008-05-13 00:58:18 +00001444 pPager->dbModified = 0;
danielk1977979f38e2007-03-27 16:19:51 +00001445
1446 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001447}
1448
drhed7c8552001-04-11 14:29:21 +00001449/*
drh968af522003-02-11 14:55:40 +00001450** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001451**
1452** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001453** random initial value and the page number. We experimented with
1454** a checksum of the entire data, but that was found to be too slow.
1455**
1456** Note that the page number is stored at the beginning of data and
1457** the checksum is stored at the end. This is important. If journal
1458** corruption occurs due to a power failure, the most likely scenario
1459** is that one end or the other of the record will be changed. It is
1460** much less likely that the two ends of the journal record will be
1461** correct and the middle be corrupt. Thus, this "checksum" scheme,
1462** though fast and simple, catches the mostly likely kind of corruption.
1463**
1464** FIX ME: Consider adding every 200th (or so) byte of the data to the
1465** checksum. That way if a single page spans 3 or more disk sectors and
1466** only the middle sector is corrupt, we will still have a reasonable
1467** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001468*/
drh74161702006-02-24 02:53:49 +00001469static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001470 u32 cksum = pPager->cksumInit;
1471 int i = pPager->pageSize-200;
1472 while( i>0 ){
1473 cksum += aData[i];
1474 i -= 200;
1475 }
drh968af522003-02-11 14:55:40 +00001476 return cksum;
1477}
1478
drh605ad8f2006-05-03 23:34:05 +00001479/* Forward declaration */
1480static void makeClean(PgHdr*);
1481
drh968af522003-02-11 14:55:40 +00001482/*
drhfa86c412002-02-02 15:01:15 +00001483** Read a single page from the journal file opened on file descriptor
1484** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001485**
drh726de592004-06-10 23:35:50 +00001486** If useCksum==0 it means this journal does not use checksums. Checksums
1487** are not used in statement journals because statement journals do not
1488** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001489*/
danielk197762079062007-08-15 17:08:46 +00001490static int pager_playback_one_page(
1491 Pager *pPager,
1492 sqlite3_file *jfd,
1493 i64 offset,
1494 int useCksum
1495){
drhfa86c412002-02-02 15:01:15 +00001496 int rc;
drhae2b40c2004-06-09 19:03:54 +00001497 PgHdr *pPg; /* An existing page in the cache */
1498 Pgno pgno; /* The page number of a page in journal */
1499 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001500 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001501
drh96362842005-03-20 22:47:56 +00001502 /* useCksum should be true for the main journal and false for
1503 ** statement journals. Verify that this is always the case
1504 */
drh9cbe6352005-11-29 03:13:21 +00001505 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001506 assert( aData );
drh96362842005-03-20 22:47:56 +00001507
danielk197762079062007-08-15 17:08:46 +00001508 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001509 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001510 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001511 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001512 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001513
drh968af522003-02-11 14:55:40 +00001514 /* Sanity checking on the page. This is more important that I originally
1515 ** thought. If a power failure occurs while the journal is being written,
1516 ** it could cause invalid data to be written into the journal. We need to
1517 ** detect this invalid data (with high probability) and ignore it.
1518 */
danielk197775edc162004-06-26 01:48:18 +00001519 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001520 return SQLITE_DONE;
1521 }
drhae2b40c2004-06-09 19:03:54 +00001522 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001523 return SQLITE_OK;
1524 }
drhae2b40c2004-06-09 19:03:54 +00001525 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001526 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001527 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001528 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001529 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001530 return SQLITE_DONE;
1531 }
1532 }
drhfa86c412002-02-02 15:01:15 +00001533
danielk1977aa5ccdf2004-06-14 05:10:42 +00001534 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001535
1536 /* If the pager is in RESERVED state, then there must be a copy of this
1537 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001538 ** not the database file. The page is left marked dirty in this case.
1539 **
danielk19772df71c72007-05-24 07:22:42 +00001540 ** An exception to the above rule: If the database is in no-sync mode
1541 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001542 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1543 ** during a Movepage() call, then the page may not be in the cache
1544 ** either. So the condition described in the above paragraph is not
1545 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001546 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001547 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1548 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001549 **
1550 ** Ticket #1171: The statement journal might contain page content that is
1551 ** different from the page content at the start of the transaction.
1552 ** This occurs when a page is changed prior to the start of a statement
1553 ** then changed again within the statement. When rolling back such a
1554 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001555 ** for certain that original page contents are synced into the main rollback
1556 ** journal. Otherwise, a power loss might leave modified data in the
1557 ** database file without an entry in the rollback journal that can
1558 ** restore the database to its original form. Two conditions must be
1559 ** met before writing to the database files. (1) the database must be
1560 ** locked. (2) we know that the original page content is fully synced
1561 ** in the main journal either because the page is not in cache or else
1562 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001563 **
1564 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1565 ** is possible to fail a statement on a database that does not yet exist.
1566 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001567 */
drhae2b40c2004-06-09 19:03:54 +00001568 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001569 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1570 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh4c02a232008-04-14 23:13:45 +00001571 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0)
1572 && pPager->fd->pMethods ){
danielk197762079062007-08-15 17:08:46 +00001573 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1574 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001575 if( pPg ){
1576 makeClean(pPg);
1577 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001578 }
drhfa86c412002-02-02 15:01:15 +00001579 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001580 /* No page should ever be explicitly rolled back that is in use, except
1581 ** for page 1 which is held in use in order to keep the lock on the
1582 ** database active. However such a page may be rolled back as a result
1583 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001584 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001585 */
drhb6f41482004-05-14 01:58:11 +00001586 void *pData;
danielk197728129562005-01-11 10:25:06 +00001587 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001588 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001589 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001590 if( pPager->xReiniter ){
1591 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001592 }
danielk19773c407372005-02-15 02:54:14 +00001593#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001594 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001595#endif
drh86a88112007-04-16 15:02:19 +00001596 /* If this was page 1, then restore the value of Pager.dbFileVers.
1597 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001598 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001599 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001600 }
drh86a88112007-04-16 15:02:19 +00001601
1602 /* Decode the page just read from disk */
1603 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001604 }
1605 return rc;
1606}
1607
1608/*
danielk197713adf8a2004-06-03 16:08:41 +00001609** Parameter zMaster is the name of a master journal file. A single journal
1610** file that referred to the master journal file has just been rolled back.
1611** This routine checks if it is possible to delete the master journal file,
1612** and does so if it is.
drh726de592004-06-10 23:35:50 +00001613**
danielk197765839c62007-08-30 08:08:17 +00001614** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1615** available for use within this function.
1616**
1617**
drh726de592004-06-10 23:35:50 +00001618** The master journal file contains the names of all child journals.
1619** To tell if a master journal can be deleted, check to each of the
1620** children. If all children are either missing or do not refer to
1621** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001622*/
danielk1977b4b47412007-08-17 15:53:36 +00001623static int pager_delmaster(Pager *pPager, const char *zMaster){
1624 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001625 int rc;
1626 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001627 sqlite3_file *pMaster;
1628 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001629 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001630 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001631
1632 /* Open the master journal file exclusively in case some other process
1633 ** is running this routine also. Not that it makes too much difference.
1634 */
danielk1977b4b47412007-08-17 15:53:36 +00001635 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001636 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001637 if( !pMaster ){
1638 rc = SQLITE_NOMEM;
1639 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001640 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1641 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001642 }
danielk197713adf8a2004-06-03 16:08:41 +00001643 if( rc!=SQLITE_OK ) goto delmaster_out;
1644 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001645
1646 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001647 if( rc!=SQLITE_OK ) goto delmaster_out;
1648
1649 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001650 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001651 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001652 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001653
1654 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001655 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001656 */
danielk197765839c62007-08-30 08:08:17 +00001657 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001658 if( !zMasterJournal ){
1659 rc = SQLITE_NOMEM;
1660 goto delmaster_out;
1661 }
danielk197765839c62007-08-30 08:08:17 +00001662 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001663 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001664 if( rc!=SQLITE_OK ) goto delmaster_out;
1665
danielk19775865e3d2004-06-14 06:03:57 +00001666 zJournal = zMasterJournal;
1667 while( (zJournal-zMasterJournal)<nMasterJournal ){
drh19db9352008-03-27 22:42:51 +00001668 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS);
1669 if( rc!=0 && rc!=1 ){
1670 rc = SQLITE_IOERR_NOMEM;
1671 goto delmaster_out;
1672 }
1673 if( rc==1 ){
danielk197713adf8a2004-06-03 16:08:41 +00001674 /* One of the journals pointed to by the master journal exists.
1675 ** Open it and check if it points at the master journal. If
1676 ** so, return without deleting the master journal file.
1677 */
drh3b7b78b2004-11-24 01:16:43 +00001678 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001679 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1680 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001681 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001682 goto delmaster_out;
1683 }
danielk197713adf8a2004-06-03 16:08:41 +00001684
danielk197765839c62007-08-30 08:08:17 +00001685 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001686 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001687 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001688 goto delmaster_out;
1689 }
danielk197776572402004-06-25 02:38:54 +00001690
danielk197765839c62007-08-30 08:08:17 +00001691 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001692 if( c ){
danielk197776572402004-06-25 02:38:54 +00001693 /* We have a match. Do not delete the master journal file. */
1694 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001695 }
1696 }
danielk19775865e3d2004-06-14 06:03:57 +00001697 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001698 }
1699 }
1700
danielk1977fee2d252007-08-18 10:59:19 +00001701 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001702
1703delmaster_out:
1704 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001705 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001706 }
1707 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001708 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001709 }
danielk1977b4b47412007-08-17 15:53:36 +00001710 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001711 return rc;
1712}
1713
drha6abd042004-06-09 17:37:22 +00001714
danielk1977e180dd92007-04-05 17:15:52 +00001715static void pager_truncate_cache(Pager *pPager);
1716
drha6abd042004-06-09 17:37:22 +00001717/*
drhcb4c40b2004-08-18 19:09:43 +00001718** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001719** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001720**
1721** Might might be the case that the file on disk is smaller than nPage.
1722** This can happen, for example, if we are in the middle of a transaction
1723** which has extended the file size and the new pages are still all held
1724** in cache, then an INSERT or UPDATE does a statement rollback. Some
1725** operating system implementations can get confused if you try to
1726** truncate a file to some size that is larger than it currently is,
danielk197706e11af2008-05-06 18:13:26 +00001727** so detect this case and write a single zero byte to the end of the new
1728** file instead.
drhcb4c40b2004-08-18 19:09:43 +00001729*/
1730static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001731 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001732 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001733 i64 currentSize, newSize;
1734 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1735 newSize = pPager->pageSize*(i64)nPage;
danielk197706e11af2008-05-06 18:13:26 +00001736 if( rc==SQLITE_OK && currentSize!=newSize ){
1737 if( currentSize>newSize ){
1738 rc = sqlite3OsTruncate(pPager->fd, newSize);
1739 }else{
1740 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
1741 }
drh7fe3f7e2007-11-29 18:44:27 +00001742 }
danielk1977e180dd92007-04-05 17:15:52 +00001743 }
1744 if( rc==SQLITE_OK ){
1745 pPager->dbSize = nPage;
1746 pager_truncate_cache(pPager);
1747 }
1748 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001749}
1750
1751/*
drhc80f0582007-05-01 16:59:48 +00001752** Set the sectorSize for the given pager.
1753**
drh334c80d2008-03-28 17:41:13 +00001754** The sector size is at least as big as the sector size reported
1755** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001756*/
1757static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001758 assert(pPager->fd->pMethods||pPager->tempFile);
1759 if( !pPager->tempFile ){
1760 /* Sector size doesn't matter for temporary files. Also, the file
1761 ** may not have been opened yet, in whcih case the OsSectorSize()
1762 ** call will segfault.
1763 */
1764 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1765 }
drh334c80d2008-03-28 17:41:13 +00001766 if( pPager->sectorSize<512 ){
1767 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001768 }
1769}
1770
1771/*
drhed7c8552001-04-11 14:29:21 +00001772** Playback the journal and thus restore the database file to
1773** the state it was in before we started making changes.
1774**
drh34e79ce2004-02-08 06:05:46 +00001775** The journal file format is as follows:
1776**
drhae2b40c2004-06-09 19:03:54 +00001777** (1) 8 byte prefix. A copy of aJournalMagic[].
1778** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001779** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001780** number of page records from the journal size.
1781** (3) 4 byte big-endian integer which is the initial value for the
1782** sanity checksum.
1783** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001784** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001785** (5) 4 byte big-endian integer which is the sector size. The header
1786** is this many bytes in size.
1787** (6) 4 byte big-endian integer which is the page case.
1788** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001789** name. The value may be zero (indicate that there is no master
1790** journal.)
drh334c80d2008-03-28 17:41:13 +00001791** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001792** and might be shorter than the value read from (5). If the first byte
1793** of the name is \000 then there is no master journal. The master
1794** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001795** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001796** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001797** + pPager->pageSize bytes of data.
1798** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001799**
drh334c80d2008-03-28 17:41:13 +00001800** When we speak of the journal header, we mean the first 8 items above.
1801** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001802**
1803** Call the value from the second bullet "nRec". nRec is the number of
1804** valid page entries in the journal. In most cases, you can compute the
1805** value of nRec from the size of the journal file. But if a power
1806** failure occurred while the journal was being written, it could be the
1807** case that the size of the journal file had already been increased but
1808** the extra entries had not yet made it safely to disk. In such a case,
1809** the value of nRec computed from the file size would be too large. For
1810** that reason, we always use the nRec value in the header.
1811**
1812** If the nRec value is 0xffffffff it means that nRec should be computed
1813** from the file size. This value is used when the user selects the
1814** no-sync option for the journal. A power failure could lead to corruption
1815** in this case. But for things like temporary table (which will be
1816** deleted when the power is restored) we don't care.
1817**
drhd9b02572001-04-15 00:37:09 +00001818** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001819** journal file then all pages up to the first corrupted page are rolled
1820** back (or no pages if the journal header is corrupted). The journal file
1821** is then deleted and SQLITE_OK returned, just as if no corruption had
1822** been encountered.
1823**
1824** If an I/O or malloc() error occurs, the journal-file is not deleted
1825** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001826*/
danielk1977e277be02007-03-23 18:12:06 +00001827static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001828 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001829 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001830 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001831 int i; /* Loop counter */
1832 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001833 int rc; /* Result code of a subroutine */
danielk1977ce98bba2008-04-03 10:13:01 +00001834 int res = 0; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001835 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001836
drhc3a64ba2001-11-22 00:01:27 +00001837 /* Figure out how many records are in the journal. Abort early if
1838 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001839 */
drh8cfbf082001-09-19 13:22:39 +00001840 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001841 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001842 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001843 goto end_playback;
1844 }
drh240c5792004-02-08 00:40:52 +00001845
danielk197776572402004-06-25 02:38:54 +00001846 /* Read the master journal name from the journal, if it is present.
1847 ** If a master journal file name is specified, but the file is not
1848 ** present on disk, then the journal is not hot and does not need to be
1849 ** played back.
drh240c5792004-02-08 00:40:52 +00001850 */
danielk197765839c62007-08-30 08:08:17 +00001851 zMaster = pPager->pTmpSpace;
1852 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977ce98bba2008-04-03 10:13:01 +00001853 if( rc!=SQLITE_OK || (zMaster[0]
1854 && (res=sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))==0 )
danielk1977b4b47412007-08-17 15:53:36 +00001855 ){
danielk197776572402004-06-25 02:38:54 +00001856 zMaster = 0;
drhc3a64ba2001-11-22 00:01:27 +00001857 goto end_playback;
1858 }
danielk197765839c62007-08-30 08:08:17 +00001859 zMaster = 0;
danielk1977ce98bba2008-04-03 10:13:01 +00001860 if( res<0 ){
1861 rc = SQLITE_IOERR_NOMEM;
1862 goto end_playback;
1863 }
1864 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001865
danielk197776572402004-06-25 02:38:54 +00001866 /* This loop terminates either when the readJournalHdr() call returns
1867 ** SQLITE_DONE or an IO error occurs. */
1868 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001869
danielk197776572402004-06-25 02:38:54 +00001870 /* Read the next journal header from the journal file. If there are
1871 ** not enough bytes left in the journal file for a complete header, or
1872 ** it is corrupted, then a process must of failed while writing it.
1873 ** This indicates nothing more needs to be rolled back.
1874 */
1875 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1876 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001877 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001878 rc = SQLITE_OK;
1879 }
danielk197776572402004-06-25 02:38:54 +00001880 goto end_playback;
1881 }
1882
1883 /* If nRec is 0xffffffff, then this journal was created by a process
1884 ** working in no-sync mode. This means that the rest of the journal
1885 ** file consists of pages, there are no more journal headers. Compute
1886 ** the value of nRec based on this assumption.
1887 */
1888 if( nRec==0xffffffff ){
1889 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1890 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1891 }
1892
danielk1977e277be02007-03-23 18:12:06 +00001893 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001894 ** process and if this is the final header in the journal, then it means
1895 ** that this part of the journal was being filled but has not yet been
1896 ** synced to disk. Compute the number of pages based on the remaining
1897 ** size of the file.
1898 **
1899 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001900 */
drh8940f4e2007-08-11 00:26:20 +00001901 if( nRec==0 && !isHot &&
1902 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001903 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1904 }
1905
danielk197776572402004-06-25 02:38:54 +00001906 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001907 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001908 */
danielk1977e180dd92007-04-05 17:15:52 +00001909 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001910 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001911 if( rc!=SQLITE_OK ){
1912 goto end_playback;
1913 }
danielk197776572402004-06-25 02:38:54 +00001914 }
1915
danielk197776572402004-06-25 02:38:54 +00001916 /* Copy original pages out of the journal and back into the database file.
1917 */
1918 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001919 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001920 if( rc!=SQLITE_OK ){
1921 if( rc==SQLITE_DONE ){
1922 rc = SQLITE_OK;
1923 pPager->journalOff = szJ;
1924 break;
1925 }else{
1926 goto end_playback;
1927 }
1928 }
drh968af522003-02-11 14:55:40 +00001929 }
drhed7c8552001-04-11 14:29:21 +00001930 }
drh580eeaf2006-02-24 03:09:37 +00001931 /*NOTREACHED*/
1932 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001933
1934end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001935 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001936 zMaster = pPager->pTmpSpace;
1937 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1938 }
1939 if( rc==SQLITE_OK ){
danielk1977df2566a2008-05-07 19:11:03 +00001940 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
danielk19778191bff2004-06-28 04:52:30 +00001941 }
danielk197765839c62007-08-30 08:08:17 +00001942 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001943 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001944 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001945 */
danielk197765839c62007-08-30 08:08:17 +00001946 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001947 }
danielk197776572402004-06-25 02:38:54 +00001948
1949 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001950 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001951 ** value. Reset it to the correct value for this process.
1952 */
drhc80f0582007-05-01 16:59:48 +00001953 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001954 return rc;
drhed7c8552001-04-11 14:29:21 +00001955}
1956
1957/*
drhac69b052004-05-12 13:30:07 +00001958** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001959**
1960** This is similar to playing back the transaction journal but with
1961** a few extra twists.
1962**
drh663fc632002-02-02 18:49:19 +00001963** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001964** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001965** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001966**
drhac69b052004-05-12 13:30:07 +00001967** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001968** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001969** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001970*/
drh3aac2dd2004-04-26 14:10:20 +00001971static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001972 i64 szJ; /* Size of the full journal */
1973 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001974 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001975 int i; /* Loop counter */
1976 int rc;
1977
danielk197776572402004-06-25 02:38:54 +00001978 szJ = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001979
danielk19774099f6e2007-03-19 11:25:20 +00001980 /* Set hdrOff to be the offset just after the end of the last journal
1981 ** page written before the first journal-header for this statement
1982 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001983 ** header was written.
1984 */
1985 hdrOff = pPager->stmtHdrOff;
1986 assert( pPager->fullSync || !hdrOff );
1987 if( !hdrOff ){
1988 hdrOff = szJ;
1989 }
1990
drhfa86c412002-02-02 15:01:15 +00001991 /* Truncate the database back to its original size.
1992 */
danielk1977e180dd92007-04-05 17:15:52 +00001993 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001994 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001995
drhac69b052004-05-12 13:30:07 +00001996 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001997 */
drhac69b052004-05-12 13:30:07 +00001998 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001999 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00002000
drhac69b052004-05-12 13:30:07 +00002001 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00002002 ** database file. Note that the statement journal omits checksums from
2003 ** each record since power-failure recovery is not important to statement
2004 ** journals.
drhfa86c412002-02-02 15:01:15 +00002005 */
danielk197762079062007-08-15 17:08:46 +00002006 for(i=0; i<nRec; i++){
2007 i64 offset = i*(4+pPager->pageSize);
2008 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00002009 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00002010 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00002011 }
2012
danielk197776572402004-06-25 02:38:54 +00002013 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
2014 ** was the size of the journal file when this statement was started, so
2015 ** everything after that needs to be rolled back, either into the
2016 ** database, the memory cache, or both.
2017 **
2018 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
2019 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00002020 */
danielk197776572402004-06-25 02:38:54 +00002021 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00002022 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00002023 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00002024 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00002025 assert( rc!=SQLITE_DONE );
2026 if( rc!=SQLITE_OK ) goto end_stmt_playback;
2027 }
2028
2029 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00002030 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00002031 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00002032 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00002033 if( rc!=SQLITE_OK ){
2034 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00002035 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00002036 }
danielk1977f0113002006-01-24 12:09:17 +00002037 if( nJRec==0 ){
2038 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00002039 }
danielk1977f0113002006-01-24 12:09:17 +00002040 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00002041 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00002042 assert( rc!=SQLITE_DONE );
2043 if( rc!=SQLITE_OK ) goto end_stmt_playback;
2044 }
drhfa86c412002-02-02 15:01:15 +00002045 }
danielk197776572402004-06-25 02:38:54 +00002046
2047 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00002048
drh3aac2dd2004-04-26 14:10:20 +00002049end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00002050 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00002051 pPager->journalOff = szJ;
2052 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00002053 }
2054 return rc;
2055}
2056
2057/*
drhf57b14a2001-09-14 18:54:08 +00002058** Change the maximum number of in-memory pages that are allowed.
2059*/
danielk19773b8a05f2007-03-19 17:44:26 +00002060void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00002061 if( mxPage>10 ){
2062 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00002063 }else{
2064 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00002065 }
2066}
2067
2068/*
drh973b6e32003-02-12 14:09:42 +00002069** Adjust the robustness of the database to damage due to OS crashes
2070** or power failures by changing the number of syncs()s when writing
2071** the rollback journal. There are three levels:
2072**
drh054889e2005-11-30 03:20:31 +00002073** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00002074** for temporary and transient files.
2075**
2076** NORMAL The journal is synced once before writes begin on the
2077** database. This is normally adequate protection, but
2078** it is theoretically possible, though very unlikely,
2079** that an inopertune power failure could leave the journal
2080** in a state which would cause damage to the database
2081** when it is rolled back.
2082**
2083** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00002084** database (with some additional information - the nRec field
2085** of the journal header - being written in between the two
2086** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00002087** single disk sector is atomic, then this mode provides
2088** assurance that the journal will not be corrupted to the
2089** point of causing damage to the database during rollback.
2090**
2091** Numeric values associated with these states are OFF==1, NORMAL=2,
2092** and FULL=3.
2093*/
danielk197793758c82005-01-21 08:13:14 +00002094#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00002095void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00002096 pPager->noSync = level==1 || pPager->tempFile;
2097 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00002098 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00002099 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00002100}
danielk197793758c82005-01-21 08:13:14 +00002101#endif
drh973b6e32003-02-12 14:09:42 +00002102
2103/*
drhaf6df112005-06-07 02:12:30 +00002104** The following global variable is incremented whenever the library
2105** attempts to open a temporary file. This information is used for
2106** testing and analysis only.
2107*/
drh0f7eb612006-08-08 13:51:43 +00002108#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002109int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00002110#endif
drhaf6df112005-06-07 02:12:30 +00002111
2112/*
drh3f56e6e2007-03-15 01:16:47 +00002113** Open a temporary file.
2114**
2115** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00002116** other error code if we fail. The OS will automatically delete the temporary
2117** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00002118*/
danielk1977b4b47412007-08-17 15:53:36 +00002119static int sqlite3PagerOpentemp(
drh33f4e022007-09-03 15:19:34 +00002120 sqlite3_vfs *pVfs, /* The virtual file system layer */
2121 sqlite3_file *pFile, /* Write the file descriptor here */
2122 char *zFilename, /* Name of the file. Might be NULL */
2123 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00002124){
drhfa86c412002-02-02 15:01:15 +00002125 int rc;
drh334b2992007-09-06 23:28:23 +00002126 assert( zFilename!=0 );
drh3f56e6e2007-03-15 01:16:47 +00002127
drh0f7eb612006-08-08 13:51:43 +00002128#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002129 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002130#endif
danielk1977b4b47412007-08-17 15:53:36 +00002131
drh33f4e022007-09-03 15:19:34 +00002132 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2133 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
2134 rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00002135 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00002136 return rc;
2137}
2138
2139/*
drhed7c8552001-04-11 14:29:21 +00002140** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002141** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002142** the first call to sqlite3PagerGet() and is only held open until the
2143** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002144**
drh6446c4d2001-12-15 14:22:18 +00002145** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002146** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002147** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002148**
2149** If zFilename is ":memory:" then all information is held in cache.
2150** It is never written to disk. This can be used to implement an
2151** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002152*/
danielk19773b8a05f2007-03-19 17:44:26 +00002153int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002154 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002155 Pager **ppPager, /* Return the Pager structure here */
2156 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002157 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002158 int flags, /* flags controlling this file */
2159 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002160){
danielk1977b4b47412007-08-17 15:53:36 +00002161 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002162 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002163 int rc = SQLITE_OK;
2164 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002165 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002166 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002167 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002168 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2169 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002170 int journalFileSize = sqlite3JournalSize(pVfs);
drh1cc8c442007-08-24 16:08:29 +00002171 int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
2172 char *zPathname;
2173 int nPathname;
drh99b90c32008-03-17 13:50:58 +00002174 char *zStmtJrnl;
2175 int nStmtJrnl;
danielk1977b4b47412007-08-17 15:53:36 +00002176
drh86f8c192007-08-22 00:39:19 +00002177 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002178 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002179
drh1cc8c442007-08-24 16:08:29 +00002180 /* Compute the full pathname */
danielk1977adfb9b02007-09-17 07:02:56 +00002181 nPathname = pVfs->mxPathname+1;
drh99b90c32008-03-17 13:50:58 +00002182 zPathname = sqlite3_malloc(nPathname*2);
drh1cc8c442007-08-24 16:08:29 +00002183 if( zPathname==0 ){
2184 return SQLITE_NOMEM;
2185 }
2186 if( zFilename && zFilename[0] ){
2187#ifndef SQLITE_OMIT_MEMORYDB
2188 if( strcmp(zFilename,":memory:")==0 ){
2189 memDb = 1;
2190 zPathname[0] = 0;
2191 }else
2192#endif
2193 {
danielk1977adfb9b02007-09-17 07:02:56 +00002194 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002195 }
2196 }else{
danielk1977adfb9b02007-09-17 07:02:56 +00002197 rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
drhae28c012007-08-24 16:29:23 +00002198 }
2199 if( rc!=SQLITE_OK ){
2200 sqlite3_free(zPathname);
2201 return rc;
drh1cc8c442007-08-24 16:08:29 +00002202 }
2203 nPathname = strlen(zPathname);
2204
drh99b90c32008-03-17 13:50:58 +00002205 /* Put the statement journal in temporary disk space since this is
2206 ** sometimes RAM disk or other optimized storage. Unlikely the main
2207 ** main journal file, the statement journal does not need to be
2208 ** colocated with the database nor does it need to be persistent.
2209 */
2210 zStmtJrnl = &zPathname[nPathname+1];
2211 rc = sqlite3OsGetTempname(pVfs, pVfs->mxPathname+1, zStmtJrnl);
2212 if( rc!=SQLITE_OK ){
2213 sqlite3_free(zPathname);
2214 return rc;
2215 }
2216 nStmtJrnl = strlen(zStmtJrnl);
2217
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 */
drh99b90c32008-03-17 13:50:58 +00002223 3*nPathname + 40 + /* zFilename, zDirectory, zJournal */
2224 nStmtJrnl /* zStmtJrnl */
danielk1977b4b47412007-08-17 15:53:36 +00002225 );
2226 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002227 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002228 return SQLITE_NOMEM;
2229 }
2230 pPtr = (u8 *)&pPager[1];
drh33f4e022007-09-03 15:19:34 +00002231 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002232 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002233 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2234 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2235 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002236 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2237 pPager->zJournal = &pPager->zDirectory[nPathname+1];
drh334b2992007-09-06 23:28:23 +00002238 pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
danielk1977b4b47412007-08-17 15:53:36 +00002239 pPager->pVfs = pVfs;
drh1cc8c442007-08-24 16:08:29 +00002240 memcpy(pPager->zFilename, zPathname, nPathname+1);
drh99b90c32008-03-17 13:50:58 +00002241 memcpy(pPager->zStmtJrnl, zStmtJrnl, nStmtJrnl+1);
drh1cc8c442007-08-24 16:08:29 +00002242 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002243
drh153c62c2007-08-24 03:51:33 +00002244 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002245 */
drhae28c012007-08-24 16:29:23 +00002246 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002247 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2248 rc = SQLITE_CANTOPEN;
2249 }else{
drh1cc8c442007-08-24 16:08:29 +00002250 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002251 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2252 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002253 readOnly = (fout&SQLITE_OPEN_READONLY);
2254
2255 /* If the file was successfully opened for read/write access,
2256 ** choose a default page size in case we have to create the
2257 ** database file. The default page size is the maximum of:
2258 **
2259 ** + SQLITE_DEFAULT_PAGE_SIZE,
2260 ** + The value returned by sqlite3OsSectorSize()
2261 ** + The largest page size that can be written atomically.
2262 */
2263 if( rc==SQLITE_OK && !readOnly ){
2264 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
2265 if( nDefaultPage<iSectorSize ){
2266 nDefaultPage = iSectorSize;
2267 }
danielk19779663b8f2007-08-24 11:52:28 +00002268#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002269 {
2270 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2271 int ii;
2272 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2273 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2274 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2275 for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2276 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002277 }
danielk1977b4b47412007-08-17 15:53:36 +00002278 }
drh1cc8c442007-08-24 16:08:29 +00002279#endif
2280 if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2281 nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2282 }
danielk19778def5ea2004-06-16 10:39:52 +00002283 }
drhac69b052004-05-12 13:30:07 +00002284 }
drh1cc8c442007-08-24 16:08:29 +00002285 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002286 /* If a temporary file is requested, it is not opened immediately.
2287 ** In this case we accept the default page size and delay actually
2288 ** opening the file until the first call to OsWrite().
2289 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002290 tempFile = 1;
2291 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002292 }
danielk1977aef0bf62005-12-30 16:28:01 +00002293
danielk1977b4b47412007-08-17 15:53:36 +00002294 if( pPager && rc==SQLITE_OK ){
drhbf8a4342008-04-29 15:38:58 +00002295 pPager->pTmpSpace = sqlite3MallocZero(nDefaultPage);
drh3e7a6092002-12-07 21:45:14 +00002296 }
danielk1977aef0bf62005-12-30 16:28:01 +00002297
drh153c62c2007-08-24 03:51:33 +00002298 /* If an error occured in either of the blocks above.
2299 ** Free the Pager structure and close the file.
2300 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002301 ** any Pager.errMask variables.
2302 */
danielk1977b4b47412007-08-17 15:53:36 +00002303 if( !pPager || !pPager->pTmpSpace ){
2304 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002305 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002306 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002307 }
danielk1977aef0bf62005-12-30 16:28:01 +00002308
drh153c62c2007-08-24 03:51:33 +00002309 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2310 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002311
danielk1977b4b47412007-08-17 15:53:36 +00002312 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002313 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002314 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002315 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002316
drh99b90c32008-03-17 13:50:58 +00002317 /* Fill in Pager.zJournal[] */
drh1cc8c442007-08-24 16:08:29 +00002318 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2319 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
danielk1977b4b47412007-08-17 15:53:36 +00002320
drh3b59a5c2006-01-15 20:28:28 +00002321 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002322 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002323 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002324 /* pPager->stmtOpen = 0; */
2325 /* pPager->stmtInUse = 0; */
2326 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002327 pPager->dbSize = memDb-1;
danielk19779663b8f2007-08-24 11:52:28 +00002328 pPager->pageSize = nDefaultPage;
drh3b59a5c2006-01-15 20:28:28 +00002329 /* pPager->stmtSize = 0; */
2330 /* pPager->stmtJSize = 0; */
2331 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002332 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002333 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002334 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002335 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002336 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002337 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002338 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2339 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2340 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2341 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002342 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002343 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002344 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002345 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002346 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002347 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002348 /* pPager->pFirst = 0; */
2349 /* pPager->pFirstSynced = 0; */
2350 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002351 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00002352 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002353 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002354 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002355 }
drh3b59a5c2006-01-15 20:28:28 +00002356 /* pPager->pBusyHandler = 0; */
2357 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002358 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002359#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002360 pPager->iInUseMM = 0;
2361 pPager->iInUseDB = 0;
2362 if( !memDb ){
drh26e4a8b2008-05-01 17:16:52 +00002363#ifndef SQLITE_MUTEX_NOOP
drh86f8c192007-08-22 00:39:19 +00002364 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00002365#endif
drh86f8c192007-08-22 00:39:19 +00002366 sqlite3_mutex_enter(mutex);
2367 pPager->pNext = sqlite3PagerList;
2368 if( sqlite3PagerList ){
2369 assert( sqlite3PagerList->pPrev==0 );
2370 sqlite3PagerList->pPrev = pPager;
2371 }
2372 pPager->pPrev = 0;
2373 sqlite3PagerList = pPager;
2374 sqlite3_mutex_leave(mutex);
2375 }
danielk197752622822006-01-09 09:59:49 +00002376#endif
drhed7c8552001-04-11 14:29:21 +00002377 return SQLITE_OK;
2378}
2379
2380/*
drh90f5ecb2004-07-22 01:19:35 +00002381** Set the busy handler function.
2382*/
danielk19773b8a05f2007-03-19 17:44:26 +00002383void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002384 pPager->pBusyHandler = pBusyHandler;
2385}
2386
2387/*
drh72f82862001-05-24 21:06:34 +00002388** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002389** when the reference count on each page reaches zero. The destructor can
2390** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002391**
danielk19773b8a05f2007-03-19 17:44:26 +00002392** The destructor is not called as a result sqlite3PagerClose().
2393** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002394*/
danielk19773b8a05f2007-03-19 17:44:26 +00002395void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002396 pPager->xDestructor = xDesc;
2397}
2398
2399/*
drha6abd042004-06-09 17:37:22 +00002400** Set the reinitializer for this pager. If not NULL, the reinitializer
2401** is called when the content of a page in cache is restored to its original
2402** value as a result of a rollback. The callback gives higher-level code
2403** an opportunity to restore the EXTRA section to agree with the restored
2404** page data.
2405*/
danielk19773b8a05f2007-03-19 17:44:26 +00002406void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002407 pPager->xReiniter = xReinit;
2408}
2409
2410/*
danielk1977a1644fd2007-08-29 12:31:25 +00002411** Set the page size to *pPageSize. If the suggest new page size is
2412** inappropriate, then an alternative page size is set to that
2413** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002414*/
danielk1977a1644fd2007-08-29 12:31:25 +00002415int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2416 int rc = SQLITE_OK;
2417 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002418 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002419 if( pageSize && pageSize!=pPager->pageSize
2420 && !pPager->memDb && pPager->nRef==0
2421 ){
2422 char *pNew = (char *)sqlite3_malloc(pageSize);
2423 if( !pNew ){
2424 rc = SQLITE_NOMEM;
2425 }else{
2426 pagerEnter(pPager);
2427 pager_reset(pPager);
2428 pPager->pageSize = pageSize;
2429 setSectorSize(pPager);
2430 sqlite3_free(pPager->pTmpSpace);
2431 pPager->pTmpSpace = pNew;
2432 pagerLeave(pPager);
2433 }
drh1c7880e2005-05-20 20:01:55 +00002434 }
danielk1977a1644fd2007-08-29 12:31:25 +00002435 *pPageSize = pPager->pageSize;
2436 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002437}
2438
2439/*
drh26b79942007-11-28 16:19:56 +00002440** Return a pointer to the "temporary page" buffer held internally
2441** by the pager. This is a buffer that is big enough to hold the
2442** entire content of a database page. This buffer is used internally
2443** during rollback and will be overwritten whenever a rollback
2444** occurs. But other modules are free to use it too, as long as
2445** no rollbacks are happening.
2446*/
2447void *sqlite3PagerTempSpace(Pager *pPager){
2448 return pPager->pTmpSpace;
2449}
2450
2451/*
drhf8e632b2007-05-08 14:51:36 +00002452** Attempt to set the maximum database page count if mxPage is positive.
2453** Make no changes if mxPage is zero or negative. And never reduce the
2454** maximum page count below the current size of the database.
2455**
2456** Regardless of mxPage, return the current maximum page count.
2457*/
2458int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2459 if( mxPage>0 ){
2460 pPager->mxPgno = mxPage;
2461 }
2462 sqlite3PagerPagecount(pPager);
2463 return pPager->mxPgno;
2464}
2465
2466/*
drhc9ac5ca2005-11-04 22:03:30 +00002467** The following set of routines are used to disable the simulated
2468** I/O error mechanism. These routines are used to avoid simulated
2469** errors in places where we do not care about errors.
2470**
2471** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2472** and generate no code.
2473*/
2474#ifdef SQLITE_TEST
2475extern int sqlite3_io_error_pending;
2476extern int sqlite3_io_error_hit;
2477static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002478void disable_simulated_io_errors(void){
2479 saved_cnt = sqlite3_io_error_pending;
2480 sqlite3_io_error_pending = -1;
2481}
2482void enable_simulated_io_errors(void){
2483 sqlite3_io_error_pending = saved_cnt;
2484}
2485#else
drh152410f2005-11-05 15:11:22 +00002486# define disable_simulated_io_errors()
2487# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002488#endif
2489
2490/*
drh90f5ecb2004-07-22 01:19:35 +00002491** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002492** that pDest points to.
2493**
2494** No error checking is done. The rational for this is that this function
2495** may be called even if the file does not exist or contain a header. In
2496** these cases sqlite3OsRead() will return an error, to which the correct
2497** response is to zero the memory at pDest and continue. A real IO error
2498** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002499*/
danielk19773b8a05f2007-03-19 17:44:26 +00002500int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002501 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002502 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002503 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2504 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002505 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002506 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002507 if( rc==SQLITE_IOERR_SHORT_READ ){
2508 rc = SQLITE_OK;
2509 }
drh90f5ecb2004-07-22 01:19:35 +00002510 }
drh551b7732006-11-06 21:20:25 +00002511 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002512}
2513
2514/*
drh5e00f6c2001-09-13 13:46:56 +00002515** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002516** pPager.
2517**
2518** If the PENDING_BYTE lies on the page directly after the end of the
2519** file, then consider this page part of the file too. For example, if
2520** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2521** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002522*/
danielk19773b8a05f2007-03-19 17:44:26 +00002523int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002524 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002525 int rc;
drhd9b02572001-04-15 00:37:09 +00002526 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002527 if( pPager->errCode ){
danielk197743468d92008-02-26 06:05:31 +00002528 return -1;
drha7aea3d2007-03-15 12:51:16 +00002529 }
drhed7c8552001-04-11 14:29:21 +00002530 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002531 n = pPager->dbSize;
2532 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002533 assert(pPager->fd->pMethods||pPager->tempFile);
2534 if( (pPager->fd->pMethods)
2535 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002536 pPager->nRef++;
drhe49f9822006-09-15 12:29:16 +00002537 pager_error(pPager, rc);
danielk1977ae72d982007-10-03 08:46:44 +00002538 pPager->nRef--;
danielk197743468d92008-02-26 06:05:31 +00002539 return -1;
danielk197715f411d2005-09-16 10:18:45 +00002540 }
2541 if( n>0 && n<pPager->pageSize ){
2542 n = 1;
2543 }else{
2544 n /= pPager->pageSize;
2545 }
2546 if( pPager->state!=PAGER_UNLOCK ){
2547 pPager->dbSize = n;
2548 }
drhed7c8552001-04-11 14:29:21 +00002549 }
danielk197715f411d2005-09-16 10:18:45 +00002550 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002551 n++;
2552 }
drhf8e632b2007-05-08 14:51:36 +00002553 if( n>pPager->mxPgno ){
2554 pPager->mxPgno = n;
2555 }
drhed7c8552001-04-11 14:29:21 +00002556 return n;
2557}
2558
drhf07e7d52006-04-07 13:54:46 +00002559
2560#ifndef SQLITE_OMIT_MEMORYDB
2561/*
2562** Clear a PgHistory block
2563*/
2564static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002565 sqlite3_free(pHist->pOrig);
2566 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002567 pHist->pOrig = 0;
2568 pHist->pStmt = 0;
2569}
2570#else
2571#define clearHistory(x)
2572#endif
2573
drhed7c8552001-04-11 14:29:21 +00002574/*
drhf7c57532003-04-25 13:22:51 +00002575** Forward declaration
2576*/
danielk197776572402004-06-25 02:38:54 +00002577static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002578
2579/*
drh85b623f2007-12-13 21:54:09 +00002580** Unlink pPg from its hash chain. Also set the page number to 0 to indicate
danielk1977f5fdda82004-11-03 08:44:05 +00002581** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002582** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002583** pNextFree/pPrevFree list that is not a part of any hash-chain.
2584*/
2585static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2586 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002587 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002588 return;
2589 }
2590 if( pPg->pNextHash ){
2591 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2592 }
2593 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002594 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002595 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2596 }else{
drh8ca0c722006-05-07 17:49:38 +00002597 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002598 pPager->aHash[h] = pPg->pNextHash;
2599 }
drh5229ae42006-03-23 23:29:04 +00002600 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002601 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2602 }
danielk1977f5fdda82004-11-03 08:44:05 +00002603 pPg->pgno = 0;
2604 pPg->pNextHash = pPg->pPrevHash = 0;
2605}
2606
2607/*
drhac69b052004-05-12 13:30:07 +00002608** Unlink a page from the free list (the list of all pages where nRef==0)
2609** and from its hash collision chain.
2610*/
2611static void unlinkPage(PgHdr *pPg){
2612 Pager *pPager = pPg->pPager;
2613
danielk19779f61c2f2007-08-27 17:27:49 +00002614 /* Unlink from free page list */
2615 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002616
2617 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002618 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002619}
2620
2621/*
danielk1977e180dd92007-04-05 17:15:52 +00002622** This routine is used to truncate the cache when a database
2623** is truncated. Drop from the cache all pages whose pgno is
2624** larger than pPager->dbSize and is unreferenced.
2625**
drhac69b052004-05-12 13:30:07 +00002626** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002627**
2628** Actually, at the point this routine is called, it would be
2629** an error to have a referenced page. But rather than delete
2630** that page and guarantee a subsequent segfault, it seems better
2631** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002632*/
danielk1977e180dd92007-04-05 17:15:52 +00002633static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002634 PgHdr *pPg;
2635 PgHdr **ppPg;
2636 int dbSize = pPager->dbSize;
2637
2638 ppPg = &pPager->pAll;
2639 while( (pPg = *ppPg)!=0 ){
2640 if( pPg->pgno<=dbSize ){
2641 ppPg = &pPg->pNextAll;
2642 }else if( pPg->nRef>0 ){
2643 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2644 ppPg = &pPg->pNextAll;
2645 }else{
2646 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002647 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2648 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002649 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002650 makeClean(pPg);
drh0d180202008-02-14 23:26:56 +00002651 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00002652 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002653 pPager->nPage--;
2654 }
2655 }
2656}
2657
drhf7c57532003-04-25 13:22:51 +00002658/*
danielk197717221812005-02-15 03:38:05 +00002659** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002660** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002661** false or until the lock succeeds.
2662**
2663** Return SQLITE_OK on success and an error code if we cannot obtain
2664** the lock.
2665*/
2666static int pager_wait_on_lock(Pager *pPager, int locktype){
2667 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002668
2669 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002670 assert( PAGER_SHARED==SHARED_LOCK );
2671 assert( PAGER_RESERVED==RESERVED_LOCK );
2672 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002673
2674 /* If the file is currently unlocked then the size must be unknown */
2675 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2676
danielk197717221812005-02-15 03:38:05 +00002677 if( pPager->state>=locktype ){
2678 rc = SQLITE_OK;
2679 }else{
drhbefdf832008-03-14 19:33:05 +00002680 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002681 do {
drh054889e2005-11-30 03:20:31 +00002682 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002683 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002684 if( rc==SQLITE_OK ){
2685 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002686 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002687 }
2688 }
2689 return rc;
2690}
2691
2692/*
drhf7c57532003-04-25 13:22:51 +00002693** Truncate the file to the number of pages specified.
2694*/
danielk19773b8a05f2007-03-19 17:44:26 +00002695int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002696 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002697 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002698 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002699 if( pPager->errCode ){
2700 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002701 return rc;
2702 }
drh7d02cb72003-06-04 16:24:39 +00002703 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002704 return SQLITE_OK;
2705 }
drhb7f91642004-10-31 02:22:47 +00002706 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002707 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002708 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002709 return SQLITE_OK;
2710 }
drh86f8c192007-08-22 00:39:19 +00002711 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002712 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002713 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002714 if( rc!=SQLITE_OK ){
2715 return rc;
2716 }
danielk197717221812005-02-15 03:38:05 +00002717
2718 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002719 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002720 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002721 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002722 if( rc!=SQLITE_OK ){
2723 return rc;
2724 }
2725
drhcb4c40b2004-08-18 19:09:43 +00002726 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002727 return rc;
2728}
2729
2730/*
drhed7c8552001-04-11 14:29:21 +00002731** Shutdown the page cache. Free all memory and close all files.
2732**
2733** If a transaction was in progress when this routine is called, that
2734** transaction is rolled back. All outstanding pages are invalidated
2735** and their memory is freed. Any attempt to use a page associated
2736** with this page cache after this function returns will likely
2737** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002738**
2739** This function always succeeds. If a transaction is active an attempt
2740** is made to roll it back. If an error occurs during the rollback
2741** a hot journal may be left in the filesystem but no error is returned
2742** to the caller.
drhed7c8552001-04-11 14:29:21 +00002743*/
danielk19773b8a05f2007-03-19 17:44:26 +00002744int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002745#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002746 if( !MEMDB ){
drh26e4a8b2008-05-01 17:16:52 +00002747#ifndef SQLITE_MUTEX_NOOP
drh86f8c192007-08-22 00:39:19 +00002748 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00002749#endif
drh86f8c192007-08-22 00:39:19 +00002750 sqlite3_mutex_enter(mutex);
2751 if( pPager->pPrev ){
2752 pPager->pPrev->pNext = pPager->pNext;
2753 }else{
2754 sqlite3PagerList = pPager->pNext;
2755 }
2756 if( pPager->pNext ){
2757 pPager->pNext->pPrev = pPager->pPrev;
2758 }
2759 sqlite3_mutex_leave(mutex);
2760 }
danielk197713f72992005-12-18 08:51:22 +00002761#endif
2762
drhbafda092007-01-03 23:36:22 +00002763 disable_simulated_io_errors();
drh16e45a42008-04-19 20:34:18 +00002764 sqlite3FaultBenign(-1, 1);
drhc2ee76c2007-01-04 14:58:14 +00002765 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002766 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002767 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002768 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002769 enable_simulated_io_errors();
drh16e45a42008-04-19 20:34:18 +00002770 sqlite3FaultBenign(-1, 0);
drh4f0c5872007-03-26 22:05:01 +00002771 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002772 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002773 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002774 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002775 }
drhf5e7bb52008-02-18 14:47:33 +00002776 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002777 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002778 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002779 }
danielk1977b4b47412007-08-17 15:53:36 +00002780 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002781 /* Temp files are automatically deleted by the OS
2782 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002783 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002784 ** }
2785 */
danielk1977aca790a2005-01-13 11:07:52 +00002786
drh17435752007-08-16 04:30:38 +00002787 sqlite3_free(pPager->aHash);
2788 sqlite3_free(pPager->pTmpSpace);
2789 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002790 return SQLITE_OK;
2791}
2792
drh87cc3b32007-05-08 21:45:27 +00002793#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002794/*
drh5e00f6c2001-09-13 13:46:56 +00002795** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002796*/
danielk19773b8a05f2007-03-19 17:44:26 +00002797Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002798 return p->pgno;
2799}
drh87cc3b32007-05-08 21:45:27 +00002800#endif
drhed7c8552001-04-11 14:29:21 +00002801
2802/*
drhc8629a12004-05-08 20:07:40 +00002803** The page_ref() function increments the reference count for a page.
2804** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002805** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002806**
2807** For non-test systems, page_ref() is a macro that calls _page_ref()
2808** online of the reference count is zero. For test systems, page_ref()
2809** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002810*/
drh836faa42003-01-11 13:30:57 +00002811static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002812 if( pPg->nRef==0 ){
2813 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002814 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002815 pPg->pPager->nRef++;
2816 }
2817 pPg->nRef++;
drhdf0b3b02001-06-23 11:36:20 +00002818}
danielk1977aca790a2005-01-13 11:07:52 +00002819#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002820 static void page_ref(PgHdr *pPg){
2821 if( pPg->nRef==0 ){
2822 _page_ref(pPg);
2823 }else{
2824 pPg->nRef++;
drhc8629a12004-05-08 20:07:40 +00002825 }
2826 }
2827#else
2828# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2829#endif
drhdf0b3b02001-06-23 11:36:20 +00002830
2831/*
2832** Increment the reference count for a page. The input pointer is
2833** a reference to the page data.
2834*/
danielk19773b8a05f2007-03-19 17:44:26 +00002835int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002836 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002837 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002838 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002839 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002840}
2841
2842/*
drh34e79ce2004-02-08 06:05:46 +00002843** Sync the journal. In other words, make sure all the pages that have
2844** been written to the journal have actually reached the surface of the
2845** disk. It is not safe to modify the original database file until after
2846** the journal has been synced. If the original database is modified before
2847** the journal is synced and a power failure occurs, the unsynced journal
2848** data would be lost and we would be unable to completely rollback the
2849** database changes. Database corruption would occur.
2850**
2851** This routine also updates the nRec field in the header of the journal.
2852** (See comments on the pager_playback() routine for additional information.)
2853** If the sync mode is FULL, two syncs will occur. First the whole journal
2854** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002855**
drh34e79ce2004-02-08 06:05:46 +00002856** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002857** after a power failure, so no sync occurs.
2858**
2859** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2860** the database is stored, then OsSync() is never called on the journal
2861** file. In this case all that is required is to update the nRec field in
2862** the journal header.
drhfa86c412002-02-02 15:01:15 +00002863**
drh34e79ce2004-02-08 06:05:46 +00002864** This routine clears the needSync field of every page current held in
2865** memory.
drh50e5dad2001-09-15 00:57:28 +00002866*/
danielk197776572402004-06-25 02:38:54 +00002867static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002868 PgHdr *pPg;
2869 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002870
danielk19774cd2cd52007-08-23 14:48:23 +00002871
drh03eb96a2002-11-10 23:32:56 +00002872 /* Sync the journal before modifying the main database
2873 ** (assuming there is a journal and it needs to be synced.)
2874 */
danielk197776572402004-06-25 02:38:54 +00002875 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002876 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002877 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002878 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002879
danielk19774cd2cd52007-08-23 14:48:23 +00002880 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002881 /* Write the nRec value into the journal file header. If in
2882 ** full-synchronous mode, sync the journal first. This ensures that
2883 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002884 ** it as a candidate for rollback.
2885 **
2886 ** This is not required if the persistent media supports the
2887 ** SAFE_APPEND property. Because in this case it is not possible
2888 ** for garbage data to be appended to the file, the nRec field
2889 ** is populated with 0xFFFFFFFF when the journal header is written
2890 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002891 */
danielk19774cd2cd52007-08-23 14:48:23 +00002892 i64 jrnlOff;
2893 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002894 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002895 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002896 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002897 if( rc!=0 ) return rc;
2898 }
danielk197713adf8a2004-06-03 16:08:41 +00002899
danielk197762079062007-08-15 17:08:46 +00002900 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2901 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2902 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002903 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002904 }
danielk19774cd2cd52007-08-23 14:48:23 +00002905 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2906 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2907 IOTRACE(("JSYNC %p\n", pPager))
2908 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2909 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2910 );
2911 if( rc!=0 ) return rc;
2912 }
drhdb48ee02003-01-16 13:42:43 +00002913 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002914 }
drh50e5dad2001-09-15 00:57:28 +00002915 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002916
2917 /* Erase the needSync flag from every page.
2918 */
2919 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2920 pPg->needSync = 0;
2921 }
danielk19779f61c2f2007-08-27 17:27:49 +00002922 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002923 }
drh03eb96a2002-11-10 23:32:56 +00002924
drh341eae82003-01-21 02:39:36 +00002925#ifndef NDEBUG
2926 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2927 ** flag must also be clear for all pages. Verify that this
2928 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002929 */
drh341eae82003-01-21 02:39:36 +00002930 else{
2931 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2932 assert( pPg->needSync==0 );
2933 }
danielk19779f61c2f2007-08-27 17:27:49 +00002934 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002935 }
drh341eae82003-01-21 02:39:36 +00002936#endif
drhdb48ee02003-01-16 13:42:43 +00002937
drh81a20f22001-10-12 17:30:04 +00002938 return rc;
drh50e5dad2001-09-15 00:57:28 +00002939}
2940
2941/*
drhdc3e10b2006-06-15 14:31:06 +00002942** Merge two lists of pages connected by pDirty and in pgno order.
2943** Do not both fixing the pPrevDirty pointers.
2944*/
2945static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2946 PgHdr result, *pTail;
2947 pTail = &result;
2948 while( pA && pB ){
2949 if( pA->pgno<pB->pgno ){
2950 pTail->pDirty = pA;
2951 pTail = pA;
2952 pA = pA->pDirty;
2953 }else{
2954 pTail->pDirty = pB;
2955 pTail = pB;
2956 pB = pB->pDirty;
2957 }
2958 }
2959 if( pA ){
2960 pTail->pDirty = pA;
2961 }else if( pB ){
2962 pTail->pDirty = pB;
2963 }else{
2964 pTail->pDirty = 0;
2965 }
2966 return result.pDirty;
2967}
2968
2969/*
2970** Sort the list of pages in accending order by pgno. Pages are
2971** connected by pDirty pointers. The pPrevDirty pointers are
2972** corrupted by this sort.
2973*/
danielk197724168722007-04-02 05:07:47 +00002974#define N_SORT_BUCKET_ALLOC 25
2975#define N_SORT_BUCKET 25
2976#ifdef SQLITE_TEST
2977 int sqlite3_pager_n_sort_bucket = 0;
2978 #undef N_SORT_BUCKET
2979 #define N_SORT_BUCKET \
2980 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2981#endif
drhdc3e10b2006-06-15 14:31:06 +00002982static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002983 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002984 int i;
2985 memset(a, 0, sizeof(a));
2986 while( pIn ){
2987 p = pIn;
2988 pIn = p->pDirty;
2989 p->pDirty = 0;
2990 for(i=0; i<N_SORT_BUCKET-1; i++){
2991 if( a[i]==0 ){
2992 a[i] = p;
2993 break;
2994 }else{
2995 p = merge_pagelist(a[i], p);
2996 a[i] = 0;
2997 }
2998 }
2999 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00003000 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
3001 ** elements in the input list. This is possible, but impractical.
3002 ** Testing this line is the point of global variable
3003 ** sqlite3_pager_n_sort_bucket.
3004 */
drhdc3e10b2006-06-15 14:31:06 +00003005 a[i] = merge_pagelist(a[i], p);
3006 }
3007 }
3008 p = a[0];
3009 for(i=1; i<N_SORT_BUCKET; i++){
3010 p = merge_pagelist(p, a[i]);
3011 }
3012 return p;
3013}
3014
3015/*
drh2554f8b2003-01-22 01:26:44 +00003016** Given a list of pages (connected by the PgHdr.pDirty pointer) write
3017** every one of those pages out to the database file and mark them all
3018** as clean.
3019*/
3020static int pager_write_pagelist(PgHdr *pList){
3021 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00003022 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00003023 int rc;
3024
3025 if( pList==0 ) return SQLITE_OK;
3026 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00003027
3028 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
3029 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00003030 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00003031 **
drha6abd042004-06-09 17:37:22 +00003032 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
3033 ** through an intermediate state PENDING. A PENDING lock prevents new
3034 ** readers from attaching to the database but is unsufficient for us to
3035 ** write. The idea of a PENDING lock is to prevent new readers from
3036 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00003037 **
drha6abd042004-06-09 17:37:22 +00003038 ** While the pager is in the RESERVED state, the original database file
3039 ** is unchanged and we can rollback without having to playback the
3040 ** journal into the original database file. Once we transition to
3041 ** EXCLUSIVE, it means the database file has been changed and any rollback
3042 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00003043 */
drh684917c2004-10-05 02:41:42 +00003044 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00003045 if( rc!=SQLITE_OK ){
3046 return rc;
3047 }
3048
drhdc3e10b2006-06-15 14:31:06 +00003049 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00003050 for(p=pList; p; p=p->pDirty){
3051 assert( p->dirty );
3052 p->dirty = 0;
3053 }
drh2554f8b2003-01-22 01:26:44 +00003054 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00003055
3056 /* If the file has not yet been opened, open it now. */
3057 if( !pPager->fd->pMethods ){
3058 assert(pPager->tempFile);
drh33f4e022007-09-03 15:19:34 +00003059 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
3060 pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00003061 if( rc ) return rc;
3062 }
3063
danielk1977687566d2004-11-02 12:56:41 +00003064 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00003065 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00003066 ** make the file smaller (presumably by auto-vacuum code). Do not write
3067 ** any such pages to the file.
3068 */
3069 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00003070 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00003071 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00003072 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
3073 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00003074 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00003075 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003076 PAGER_INCR(sqlite3_pager_writedb_count);
3077 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00003078 if( pList->pgno==1 ){
3079 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
3080 }
danielk1977687566d2004-11-02 12:56:41 +00003081 }
3082#ifndef NDEBUG
3083 else{
drh4f0c5872007-03-26 22:05:01 +00003084 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00003085 }
3086#endif
drh2554f8b2003-01-22 01:26:44 +00003087 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00003088#ifdef SQLITE_CHECK_PAGES
3089 pList->pageHash = pager_pagehash(pList);
3090#endif
drh2554f8b2003-01-22 01:26:44 +00003091 pList = pList->pDirty;
3092 }
3093 return SQLITE_OK;
3094}
3095
3096/*
3097** Collect every dirty page into a dirty list and
3098** return a pointer to the head of that list. All pages are
3099** collected even if they are still in use.
3100*/
3101static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh8f2e9a12008-02-26 14:46:04 +00003102
3103#ifndef NDEBUG
3104 /* Verify the sanity of the dirty list when we are running
3105 ** in debugging mode. This is expensive, so do not
3106 ** do this on a normal build. */
3107 int n1 = 0;
3108 int n2 = 0;
3109 PgHdr *p;
3110 for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; }
3111 for(p=pPager->pDirty; p; p=p->pDirty){ n2++; }
3112 assert( n1==n2 );
3113#endif
3114
drh605ad8f2006-05-03 23:34:05 +00003115 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00003116}
3117
3118/*
drh19db9352008-03-27 22:42:51 +00003119** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00003120** A hot journal is one that needs to be played back.
3121**
3122** If the current size of the database file is 0 but a journal file
3123** exists, that is probably an old journal left over from a prior
3124** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00003125**
3126** Return negative if unable to determine the status of the journal.
drh82ed1e52008-04-25 12:25:42 +00003127**
3128** This routine does not open the journal file to examine its
3129** content. Hence, the journal might contain the name of a master
3130** journal file that has been deleted, and hence not be hot. Or
3131** the header of the journal might be zeroed out. This routine
3132** does not discover these cases of a non-hot journal - if the
3133** journal file exists and is not empty this routine assumes it
3134** is hot. The pager_playback() routine will discover that the
3135** journal file is not really hot and will no-op.
drh165ffe92005-03-15 17:09:30 +00003136*/
3137static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003138 sqlite3_vfs *pVfs = pPager->pVfs;
drh19db9352008-03-27 22:42:51 +00003139 int rc;
drh165ffe92005-03-15 17:09:30 +00003140 if( !pPager->useJournal ) return 0;
drhd3cf2472007-11-27 16:55:07 +00003141 if( !pPager->fd->pMethods ) return 0;
drh19db9352008-03-27 22:42:51 +00003142 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS);
3143 if( rc<=0 ){
3144 return rc;
drhbb5f18d2007-04-06 18:23:17 +00003145 }
3146 if( sqlite3OsCheckReservedLock(pPager->fd) ){
3147 return 0;
3148 }
danielk19773b8a05f2007-03-19 17:44:26 +00003149 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003150 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00003151 return 0;
3152 }else{
3153 return 1;
3154 }
3155}
3156
danielk19775591df52005-12-20 09:19:37 +00003157/*
danielk1977aef0bf62005-12-30 16:28:01 +00003158** Try to find a page in the cache that can be recycled.
3159**
3160** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00003161** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00003162*/
danielk1977843e65f2007-09-01 16:16:15 +00003163static int pager_recycle(Pager *pPager, PgHdr **ppPg){
danielk197713f72992005-12-18 08:51:22 +00003164 PgHdr *pPg;
3165 *ppPg = 0;
3166
danielk1977843e65f2007-09-01 16:16:15 +00003167 /* It is illegal to call this function unless the pager object
3168 ** pointed to by pPager has at least one free page (page with nRef==0).
3169 */
danielk1977c551edc2007-04-05 13:12:13 +00003170 assert(!MEMDB);
danielk1977843e65f2007-09-01 16:16:15 +00003171 assert(pPager->lru.pFirst);
danielk1977c551edc2007-04-05 13:12:13 +00003172
danielk197713f72992005-12-18 08:51:22 +00003173 /* Find a page to recycle. Try to locate a page that does not
3174 ** require us to do an fsync() on the journal.
3175 */
danielk19779f61c2f2007-08-27 17:27:49 +00003176 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00003177
3178 /* If we could not find a page that does not require an fsync()
3179 ** on the journal file then fsync the journal file. This is a
3180 ** very slow operation, so we work hard to avoid it. But sometimes
3181 ** it can't be helped.
3182 */
danielk1977843e65f2007-09-01 16:16:15 +00003183 if( pPg==0 && pPager->lru.pFirst){
danielk19774cd2cd52007-08-23 14:48:23 +00003184 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk197713f72992005-12-18 08:51:22 +00003185 int rc = syncJournal(pPager);
3186 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003187 return rc;
danielk197713f72992005-12-18 08:51:22 +00003188 }
danielk19774cd2cd52007-08-23 14:48:23 +00003189 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197713f72992005-12-18 08:51:22 +00003190 /* If in full-sync mode, write a new journal header into the
3191 ** journal file. This is done to avoid ever modifying a journal
3192 ** header that is involved in the rollback of pages that have
3193 ** already been written to the database (in case the header is
3194 ** trashed when the nRec field is updated).
3195 */
3196 pPager->nRec = 0;
3197 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00003198 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00003199 rc = writeJournalHdr(pPager);
3200 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003201 return rc;
danielk197713f72992005-12-18 08:51:22 +00003202 }
3203 }
danielk19779f61c2f2007-08-27 17:27:49 +00003204 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003205 }
danielk197713f72992005-12-18 08:51:22 +00003206
3207 assert( pPg->nRef==0 );
3208
3209 /* Write the page to the database file if it is dirty.
3210 */
3211 if( pPg->dirty ){
3212 int rc;
3213 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003214 makeClean(pPg);
3215 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003216 pPg->pDirty = 0;
3217 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003218 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003219 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003220 return rc;
danielk197713f72992005-12-18 08:51:22 +00003221 }
3222 }
3223 assert( pPg->dirty==0 );
3224
3225 /* If the page we are recycling is marked as alwaysRollback, then
3226 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003227 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003228 ** It is necessary to do this because the page marked alwaysRollback
3229 ** might be reloaded at a later time but at that point we won't remember
3230 ** that is was marked alwaysRollback. This means that all pages must
3231 ** be marked as alwaysRollback from here on out.
3232 */
3233 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003234 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003235 pPager->alwaysRollback = 1;
3236 }
3237
3238 /* Unlink the old page from the free list and the hash table
3239 */
3240 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003241 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003242
3243 *ppPg = pPg;
3244 return SQLITE_OK;
3245}
3246
drh86f8c192007-08-22 00:39:19 +00003247#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003248/*
3249** This function is called to free superfluous dynamically allocated memory
3250** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003251** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003252**
3253** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003254** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003255** of bytes of memory released.
3256*/
danielk19773b8a05f2007-03-19 17:44:26 +00003257int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003258 int nReleased = 0; /* Bytes of memory released so far */
drh86f8c192007-08-22 00:39:19 +00003259 Pager *pPager; /* For looping over pagers */
drhe5fe6902007-12-07 18:55:28 +00003260 BusyHandler *savedBusy; /* Saved copy of the busy handler */
danielk19779f61c2f2007-08-27 17:27:49 +00003261 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003262
drh86f8c192007-08-22 00:39:19 +00003263 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003264 */
drh26e4a8b2008-05-01 17:16:52 +00003265#ifndef SQLITE_MUTEX_NOOP
drh64e2bb72008-05-07 12:29:55 +00003266 sqlite3_mutex *mutex; /* The MEM2 mutex */
drh86f8c192007-08-22 00:39:19 +00003267 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
drh26e4a8b2008-05-01 17:16:52 +00003268#endif
drh86f8c192007-08-22 00:39:19 +00003269 sqlite3_mutex_enter(mutex);
3270
3271 /* Signal all database connections that memory management wants
3272 ** to have access to the pagers.
3273 */
3274 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3275 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003276 }
3277
danielk19779f61c2f2007-08-27 17:27:49 +00003278 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3279 PgHdr *pPg;
3280 PgHdr *pRecycled;
3281
3282 /* Try to find a page to recycle that does not require a sync(). If
3283 ** this is not possible, find one that does require a sync().
3284 */
3285 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3286 pPg = sqlite3LruPageList.pFirstSynced;
3287 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3288 pPg = pPg->gfree.pNext;
3289 }
3290 if( !pPg ){
3291 pPg = sqlite3LruPageList.pFirst;
3292 while( pPg && pPg->pPager->iInUseDB ){
3293 pPg = pPg->gfree.pNext;
3294 }
3295 }
3296 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003297
danielk197784f786f2007-08-28 08:00:17 +00003298 /* If pPg==0, then the block above has failed to find a page to
3299 ** recycle. In this case return early - no further memory will
3300 ** be released.
3301 */
danielk19779f61c2f2007-08-27 17:27:49 +00003302 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003303
danielk19779f61c2f2007-08-27 17:27:49 +00003304 pPager = pPg->pPager;
3305 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3306 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3307
drhe5fe6902007-12-07 18:55:28 +00003308 savedBusy = pPager->pBusyHandler;
3309 pPager->pBusyHandler = 0;
danielk1977843e65f2007-09-01 16:16:15 +00003310 rc = pager_recycle(pPager, &pRecycled);
drhe5fe6902007-12-07 18:55:28 +00003311 pPager->pBusyHandler = savedBusy;
danielk19779f61c2f2007-08-27 17:27:49 +00003312 assert(pRecycled==pPg || rc!=SQLITE_OK);
3313 if( rc==SQLITE_OK ){
3314 /* We've found a page to free. At this point the page has been
3315 ** removed from the page hash-table, free-list and synced-list
3316 ** (pFirstSynced). It is still in the all pages (pAll) list.
3317 ** Remove it from this list before freeing.
3318 **
3319 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3320 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003321 */
danielk19779f61c2f2007-08-27 17:27:49 +00003322 PgHdr *pTmp;
3323 assert( pPg );
3324 if( pPg==pPager->pAll ){
3325 pPager->pAll = pPg->pNextAll;
3326 }else{
3327 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3328 pTmp->pNextAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00003329 }
danielk19779f61c2f2007-08-27 17:27:49 +00003330 nReleased += (
3331 sizeof(*pPg) + pPager->pageSize
3332 + sizeof(u32) + pPager->nExtra
3333 + MEMDB*sizeof(PgHistory)
3334 );
3335 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3336 PAGER_INCR(sqlite3_pager_pgfree_count);
drh0d180202008-02-14 23:26:56 +00003337 sqlite3_free(pPg->pData);
danielk19779f61c2f2007-08-27 17:27:49 +00003338 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003339 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003340 }else{
3341 /* An error occured whilst writing to the database file or
3342 ** journal in pager_recycle(). The error is not returned to the
3343 ** caller of this function. Instead, set the Pager.errCode variable.
3344 ** The error will be returned to the user (or users, in the case
3345 ** of a shared pager cache) of the pager for which the error occured.
3346 */
3347 assert(
3348 (rc&0xff)==SQLITE_IOERR ||
3349 rc==SQLITE_FULL ||
3350 rc==SQLITE_BUSY
3351 );
3352 assert( pPager->state>=PAGER_RESERVED );
3353 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003354 }
3355 }
danielk1977aef0bf62005-12-30 16:28:01 +00003356
drh86f8c192007-08-22 00:39:19 +00003357 /* Clear the memory management flags and release the mutex
3358 */
3359 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3360 pPager->iInUseMM = 0;
3361 }
3362 sqlite3_mutex_leave(mutex);
3363
3364 /* Return the number of bytes released
3365 */
danielk197713f72992005-12-18 08:51:22 +00003366 return nReleased;
3367}
drh86f8c192007-08-22 00:39:19 +00003368#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003369
drh165ffe92005-03-15 17:09:30 +00003370/*
danielk1977e180dd92007-04-05 17:15:52 +00003371** Read the content of page pPg out of the database file.
3372*/
3373static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3374 int rc;
danielk197762079062007-08-15 17:08:46 +00003375 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003376 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003377 assert(pPager->fd->pMethods||pPager->tempFile);
3378 if( !pPager->fd->pMethods ){
3379 return SQLITE_IOERR_SHORT_READ;
3380 }
danielk197762079062007-08-15 17:08:46 +00003381 offset = (pgno-1)*(i64)pPager->pageSize;
3382 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003383 PAGER_INCR(sqlite3_pager_readdb_count);
3384 PAGER_INCR(pPager->nRead);
3385 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003386 if( pgno==1 ){
3387 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3388 sizeof(pPager->dbFileVers));
3389 }
danielk1977e180dd92007-04-05 17:15:52 +00003390 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003391 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3392 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003393 return rc;
3394}
3395
3396
3397/*
danielk1977e277be02007-03-23 18:12:06 +00003398** This function is called to obtain the shared lock required before
3399** data may be read from the pager cache. If the shared lock has already
3400** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003401**
3402** Immediately after obtaining the shared lock (if required), this function
3403** checks for a hot-journal file. If one is found, an emergency rollback
3404** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003405*/
3406static int pagerSharedLock(Pager *pPager){
3407 int rc = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00003408 int isHot = 0;
danielk1977e277be02007-03-23 18:12:06 +00003409
danielk1977ae72d982007-10-03 08:46:44 +00003410 /* If this database is opened for exclusive access, has no outstanding
3411 ** page references and is in an error-state, now is the chance to clear
3412 ** the error. Discard the contents of the pager-cache and treat any
3413 ** open journal file as a hot-journal.
3414 */
3415 if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
3416 if( pPager->journalOpen ){
3417 isHot = 1;
3418 }
danielk1977ae72d982007-10-03 08:46:44 +00003419 pPager->errCode = SQLITE_OK;
danielk197793f7af92008-05-09 16:57:50 +00003420 pager_reset(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00003421 }
3422
3423 /* If the pager is still in an error state, do not proceed. The error
3424 ** state will be cleared at some point in the future when all page
3425 ** references are dropped and the cache can be discarded.
3426 */
3427 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3428 return pPager->errCode;
3429 }
3430
3431 if( pPager->state==PAGER_UNLOCK || isHot ){
danielk1977b4b47412007-08-17 15:53:36 +00003432 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003433 if( !MEMDB ){
3434 assert( pPager->nRef==0 );
3435 if( !pPager->noReadlock ){
3436 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3437 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003438 assert( pPager->state==PAGER_UNLOCK );
danielk1977e277be02007-03-23 18:12:06 +00003439 return pager_error(pPager, rc);
3440 }
3441 assert( pPager->state>=SHARED_LOCK );
3442 }
3443
3444 /* If a journal file exists, and there is no RESERVED lock on the
3445 ** database file, then it either needs to be played back or deleted.
3446 */
drh19db9352008-03-27 22:42:51 +00003447 rc = hasHotJournal(pPager);
3448 if( rc<0 ){
danielk197752b472a2008-05-05 16:23:55 +00003449 rc = SQLITE_IOERR_NOMEM;
3450 goto failed;
drh19db9352008-03-27 22:42:51 +00003451 }
3452 if( rc==1 || isHot ){
danielk1977e277be02007-03-23 18:12:06 +00003453 /* Get an EXCLUSIVE lock on the database file. At this point it is
3454 ** important that a RESERVED lock is not obtained on the way to the
3455 ** EXCLUSIVE lock. If it were, another process might open the
3456 ** database file, detect the RESERVED lock, and conclude that the
3457 ** database is safe to read while this process is still rolling it
3458 ** back.
3459 **
3460 ** Because the intermediate RESERVED lock is not requested, the
3461 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00003462 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00003463 */
danielk1977ae72d982007-10-03 08:46:44 +00003464 if( pPager->state<EXCLUSIVE_LOCK ){
3465 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3466 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003467 rc = pager_error(pPager, rc);
3468 goto failed;
danielk1977ae72d982007-10-03 08:46:44 +00003469 }
3470 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00003471 }
danielk1977e277be02007-03-23 18:12:06 +00003472
drh16e45a42008-04-19 20:34:18 +00003473 /* Open the journal for read/write access. This is because in
drhfd131da2007-08-07 17:13:03 +00003474 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003475 ** possibly used for a transaction later on. On some systems, the
3476 ** OsTruncate() call used in exclusive-access mode also requires
3477 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003478 */
drh16e45a42008-04-19 20:34:18 +00003479 if( !isHot && pPager->journalOpen==0 ){
danielk1977ce98bba2008-04-03 10:13:01 +00003480 int res = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS);
3481 if( res==1 ){
danielk1977ae72d982007-10-03 08:46:44 +00003482 int fout = 0;
3483 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3484 assert( !pPager->tempFile );
3485 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3486 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3487 if( fout&SQLITE_OPEN_READONLY ){
3488 rc = SQLITE_BUSY;
3489 sqlite3OsClose(pPager->jfd);
3490 }
drh16e45a42008-04-19 20:34:18 +00003491 }else if( res==0 ){
3492 /* If the journal does not exist, that means some other process
3493 ** has already rolled it back */
3494 rc = SQLITE_BUSY;
danielk1977ce98bba2008-04-03 10:13:01 +00003495 }else{
drh16e45a42008-04-19 20:34:18 +00003496 /* If sqlite3OsAccess() returns a negative value, that means it
3497 ** failed a memory allocation */
3498 rc = SQLITE_IOERR_NOMEM;
danielk1977979f38e2007-03-27 16:19:51 +00003499 }
3500 }
danielk1977e277be02007-03-23 18:12:06 +00003501 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003502 if( rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_UNLOCK
3503 && rc!=SQLITE_IOERR_NOMEM
3504 ){
3505 rc = SQLITE_BUSY;
drh1aa5af12008-03-07 19:51:14 +00003506 }
danielk197752b472a2008-05-05 16:23:55 +00003507 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003508 }
3509 pPager->journalOpen = 1;
3510 pPager->journalStarted = 0;
3511 pPager->journalOff = 0;
3512 pPager->setMaster = 0;
3513 pPager->journalHdr = 0;
3514
3515 /* Playback and delete the journal. Drop the database write
3516 ** lock and reacquire the read lock.
3517 */
3518 rc = pager_playback(pPager, 1);
3519 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003520 rc = pager_error(pPager, rc);
3521 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003522 }
danielk1977c5859712007-03-26 12:26:27 +00003523 assert(pPager->state==PAGER_SHARED ||
3524 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3525 );
danielk1977e277be02007-03-23 18:12:06 +00003526 }
3527
3528 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003529 /* The shared-lock has just been acquired on the database file
3530 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003531 ** read or write transaction). Check to see if the database
3532 ** has been modified. If the database has changed, flush the
3533 ** cache.
3534 **
3535 ** Database changes is detected by looking at 15 bytes beginning
3536 ** at offset 24 into the file. The first 4 of these 16 bytes are
3537 ** a 32-bit counter that is incremented with each change. The
3538 ** other bytes change randomly with each file change when
3539 ** a codec is in use.
3540 **
3541 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003542 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003543 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003544 */
drh86a88112007-04-16 15:02:19 +00003545 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003546 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003547
danielk1977e180dd92007-04-05 17:15:52 +00003548 if( pPager->errCode ){
danielk197752b472a2008-05-05 16:23:55 +00003549 rc = pPager->errCode;
3550 goto failed;
danielk1977e277be02007-03-23 18:12:06 +00003551 }
3552
danielk1977e180dd92007-04-05 17:15:52 +00003553 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003554 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003555 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003556 if( rc!=SQLITE_OK ){
danielk197752b472a2008-05-05 16:23:55 +00003557 goto failed;
danielk1977e180dd92007-04-05 17:15:52 +00003558 }
drh86a88112007-04-16 15:02:19 +00003559 }else{
3560 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003561 }
3562
drh86a88112007-04-16 15:02:19 +00003563 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003564 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003565 }
3566 }
3567 }
danielk1977c5859712007-03-26 12:26:27 +00003568 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3569 if( pPager->state==PAGER_UNLOCK ){
3570 pPager->state = PAGER_SHARED;
3571 }
danielk1977e277be02007-03-23 18:12:06 +00003572 }
3573
danielk197752b472a2008-05-05 16:23:55 +00003574 failed:
3575 if( rc!=SQLITE_OK ){
3576 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
3577 pager_unlock(pPager);
3578 }
danielk1977e277be02007-03-23 18:12:06 +00003579 return rc;
3580}
3581
3582/*
danielk1977e180dd92007-04-05 17:15:52 +00003583** Allocate a PgHdr object. Either create a new one or reuse
3584** an existing one that is not otherwise in use.
3585**
3586** A new PgHdr structure is created if any of the following are
3587** true:
3588**
3589** (1) We have not exceeded our maximum allocated cache size
3590** as set by the "PRAGMA cache_size" command.
3591**
3592** (2) There are no unused PgHdr objects available at this time.
3593**
3594** (3) This is an in-memory database.
3595**
3596** (4) There are no PgHdr objects that do not require a journal
3597** file sync and a sync of the journal file is currently
3598** prohibited.
3599**
3600** Otherwise, reuse an existing PgHdr. In other words, reuse an
3601** existing PgHdr if all of the following are true:
3602**
3603** (1) We have reached or exceeded the maximum cache size
3604** allowed by "PRAGMA cache_size".
3605**
3606** (2) There is a PgHdr available with PgHdr->nRef==0
3607**
3608** (3) We are not in an in-memory database
3609**
3610** (4) Either there is an available PgHdr that does not need
3611** to be synced to disk or else disk syncing is currently
3612** allowed.
danielk197724168722007-04-02 05:07:47 +00003613*/
3614static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3615 int rc = SQLITE_OK;
3616 PgHdr *pPg;
drh1e3af332007-10-20 13:17:54 +00003617 int nByteHdr;
danielk197724168722007-04-02 05:07:47 +00003618
danielk1977e180dd92007-04-05 17:15:52 +00003619 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003620 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003621 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003622 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003623 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003624 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003625 ){
drh0d180202008-02-14 23:26:56 +00003626 void *pData;
danielk197724168722007-04-02 05:07:47 +00003627 if( pPager->nPage>=pPager->nHash ){
3628 pager_resize_hash_table(pPager,
3629 pPager->nHash<256 ? 256 : pPager->nHash*2);
3630 if( pPager->nHash==0 ){
3631 rc = SQLITE_NOMEM;
3632 goto pager_allocate_out;
3633 }
3634 }
drhed138fb2007-08-22 22:04:37 +00003635 pagerLeave(pPager);
drh1e3af332007-10-20 13:17:54 +00003636 nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra
3637 + MEMDB*sizeof(PgHistory);
drh0d180202008-02-14 23:26:56 +00003638 pPg = sqlite3_malloc( nByteHdr );
3639 if( pPg ){
3640 pData = sqlite3_malloc( pPager->pageSize );
3641 if( pData==0 ){
3642 sqlite3_free(pPg);
3643 pPg = 0;
3644 }
3645 }
drhed138fb2007-08-22 22:04:37 +00003646 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003647 if( pPg==0 ){
3648 rc = SQLITE_NOMEM;
3649 goto pager_allocate_out;
3650 }
drh1e3af332007-10-20 13:17:54 +00003651 memset(pPg, 0, nByteHdr);
drh0d180202008-02-14 23:26:56 +00003652 pPg->pData = pData;
danielk197724168722007-04-02 05:07:47 +00003653 pPg->pPager = pPager;
3654 pPg->pNextAll = pPager->pAll;
3655 pPager->pAll = pPg;
3656 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003657 }else{
3658 /* Recycle an existing page with a zero ref-count. */
danielk1977843e65f2007-09-01 16:16:15 +00003659 rc = pager_recycle(pPager, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003660 if( rc==SQLITE_BUSY ){
3661 rc = SQLITE_IOERR_BLOCKED;
3662 }
danielk197724168722007-04-02 05:07:47 +00003663 if( rc!=SQLITE_OK ){
3664 goto pager_allocate_out;
3665 }
3666 assert( pPager->state>=SHARED_LOCK );
3667 assert(pPg);
3668 }
3669 *ppPg = pPg;
3670
3671pager_allocate_out:
3672 return rc;
3673}
3674
3675/*
drhd33d5a82007-04-26 12:11:28 +00003676** Make sure we have the content for a page. If the page was
3677** previously acquired with noContent==1, then the content was
3678** just initialized to zeros instead of being read from disk.
3679** But now we need the real data off of disk. So make sure we
3680** have it. Read it in if we do not have it already.
3681*/
3682static int pager_get_content(PgHdr *pPg){
3683 if( pPg->needRead ){
3684 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3685 if( rc==SQLITE_OK ){
3686 pPg->needRead = 0;
3687 }else{
3688 return rc;
3689 }
3690 }
3691 return SQLITE_OK;
3692}
3693
3694/*
drhd9b02572001-04-15 00:37:09 +00003695** Acquire a page.
3696**
drh58a11682001-11-10 13:51:08 +00003697** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003698** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003699**
drhd33d5a82007-04-26 12:11:28 +00003700** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003701** file is smaller than the requested page, then no actual disk
3702** read occurs and the memory image of the page is initialized to
3703** all zeros. The extra data appended to a page is always initialized
3704** to zeros the first time a page is loaded into memory.
3705**
drhd9b02572001-04-15 00:37:09 +00003706** The acquisition might fail for several reasons. In all cases,
3707** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003708**
drhd33d5a82007-04-26 12:11:28 +00003709** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003710** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003711** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003712** just returns 0. This routine acquires a read-lock the first time it
3713** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003714** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003715** or journal files.
drh0787db62007-03-04 13:15:27 +00003716**
drh538f5702007-04-13 02:14:30 +00003717** If noContent is false, the page contents are actually read from disk.
3718** If noContent is true, it means that we do not care about the contents
3719** of the page at this time, so do not do a disk read. Just fill in the
3720** page content with zeros. But mark the fact that we have not read the
3721** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003722** sqlite3PagerWrite() is called on this page or if this routine is
3723** called again with noContent==0, that means that the content is needed
3724** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003725*/
drh86f8c192007-08-22 00:39:19 +00003726static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003727 Pager *pPager, /* The pager open on the database file */
3728 Pgno pgno, /* Page number to fetch */
3729 DbPage **ppPage, /* Write a pointer to the page here */
3730 int noContent /* Do not bother reading content from disk if true */
3731){
drhed7c8552001-04-11 14:29:21 +00003732 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003733 int rc;
drhed7c8552001-04-11 14:29:21 +00003734
danielk1977e277be02007-03-23 18:12:06 +00003735 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3736
danielk197726836652005-01-17 01:33:13 +00003737 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3738 ** number greater than this, or zero, is requested.
3739 */
drh267cb322005-09-16 17:16:52 +00003740 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003741 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003742 }
3743
drhd9b02572001-04-15 00:37:09 +00003744 /* Make sure we have not hit any critical errors.
3745 */
drh836faa42003-01-11 13:30:57 +00003746 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003747 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003748
danielk197713adf8a2004-06-03 16:08:41 +00003749 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003750 ** on the database file. pagerSharedLock() is a no-op if
3751 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003752 */
danielk1977e277be02007-03-23 18:12:06 +00003753 rc = pagerSharedLock(pPager);
3754 if( rc!=SQLITE_OK ){
3755 return rc;
drhed7c8552001-04-11 14:29:21 +00003756 }
danielk1977e277be02007-03-23 18:12:06 +00003757 assert( pPager->state!=PAGER_UNLOCK );
3758
3759 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003760 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003761 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003762 int nMax;
drhed7c8552001-04-11 14:29:21 +00003763 int h;
drh538f5702007-04-13 02:14:30 +00003764 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003765 rc = pagerAllocatePage(pPager, &pPg);
3766 if( rc!=SQLITE_OK ){
3767 return rc;
drhed7c8552001-04-11 14:29:21 +00003768 }
danielk197724168722007-04-02 05:07:47 +00003769
drhed7c8552001-04-11 14:29:21 +00003770 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003771 assert( !MEMDB || pgno>pPager->stmtSize );
drhf5e7bb52008-02-18 14:47:33 +00003772 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
3773 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003774
drh605ad8f2006-05-03 23:34:05 +00003775 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003776 pPg->nRef = 1;
danielk197775bab7d2006-01-23 13:09:45 +00003777
drhd9b02572001-04-15 00:37:09 +00003778 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003779 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003780 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003781 }
danielk1977979f38e2007-03-27 16:19:51 +00003782 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003783 if( pPager->errCode ){
danielk1977efaaf572006-01-16 11:29:19 +00003784 rc = pPager->errCode;
danielk1977ae72d982007-10-03 08:46:44 +00003785 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003786 return rc;
3787 }
danielk197775bab7d2006-01-23 13:09:45 +00003788
3789 /* Populate the page with data, either by reading from the database
3790 ** file, or by setting the entire page to zero.
3791 */
drhd215acf2007-04-13 04:01:58 +00003792 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003793 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003794 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003795 return SQLITE_FULL;
3796 }
drh90f5ecb2004-07-22 01:19:35 +00003797 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003798 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003799 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003800 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003801 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003802 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3803 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003804 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003805 return rc;
drh81a20f22001-10-12 17:30:04 +00003806 }
danielk1977b4626a32007-04-28 15:47:43 +00003807 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003808 }
danielk197775bab7d2006-01-23 13:09:45 +00003809
3810 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003811 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003812 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003813 pPg->pNextHash = pPager->aHash[h];
3814 pPager->aHash[h] = pPg;
3815 if( pPg->pNextHash ){
3816 assert( pPg->pNextHash->pPrevHash==0 );
3817 pPg->pNextHash->pPrevHash = pPg;
3818 }
3819
danielk19773c407372005-02-15 02:54:14 +00003820#ifdef SQLITE_CHECK_PAGES
3821 pPg->pageHash = pager_pagehash(pPg);
3822#endif
drhed7c8552001-04-11 14:29:21 +00003823 }else{
drhd9b02572001-04-15 00:37:09 +00003824 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003825 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003826 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003827 if( !noContent ){
3828 rc = pager_get_content(pPg);
3829 if( rc ){
3830 return rc;
3831 }
3832 }
drhdf0b3b02001-06-23 11:36:20 +00003833 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003834 }
danielk19773b8a05f2007-03-19 17:44:26 +00003835 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003836 return SQLITE_OK;
3837}
drh86f8c192007-08-22 00:39:19 +00003838int sqlite3PagerAcquire(
3839 Pager *pPager, /* The pager open on the database file */
3840 Pgno pgno, /* Page number to fetch */
3841 DbPage **ppPage, /* Write a pointer to the page here */
3842 int noContent /* Do not bother reading content from disk if true */
3843){
3844 int rc;
3845 pagerEnter(pPager);
3846 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3847 pagerLeave(pPager);
3848 return rc;
3849}
3850
drhed7c8552001-04-11 14:29:21 +00003851
3852/*
drh7e3b0a02001-04-28 16:52:40 +00003853** Acquire a page if it is already in the in-memory cache. Do
3854** not read the page from disk. Return a pointer to the page,
3855** or 0 if the page is not in cache.
3856**
danielk19773b8a05f2007-03-19 17:44:26 +00003857** See also sqlite3PagerGet(). The difference between this routine
3858** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003859** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003860** returns NULL if the page is not in cache or if a disk I/O error
3861** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003862*/
danielk19773b8a05f2007-03-19 17:44:26 +00003863DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003864 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003865
drh836faa42003-01-11 13:30:57 +00003866 assert( pPager!=0 );
3867 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003868
drh86f8c192007-08-22 00:39:19 +00003869 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003870 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003871 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003872 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3873 /* Do nothing */
3874 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3875 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003876 }
drh86f8c192007-08-22 00:39:19 +00003877 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003878 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003879}
3880
3881/*
drhed7c8552001-04-11 14:29:21 +00003882** Release a page.
3883**
3884** If the number of references to the page drop to zero, then the
3885** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003886** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003887** removed.
3888*/
danielk19773b8a05f2007-03-19 17:44:26 +00003889int sqlite3PagerUnref(DbPage *pPg){
drh6eac06e2008-05-07 12:45:41 +00003890 Pager *pPager;
3891
3892 if( pPg==0 ) return SQLITE_OK;
3893 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003894
3895 /* Decrement the reference count for this page
3896 */
drhed7c8552001-04-11 14:29:21 +00003897 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003898 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003899 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +00003900
danielk19773c407372005-02-15 02:54:14 +00003901 CHECK_PAGE(pPg);
3902
drh72f82862001-05-24 21:06:34 +00003903 /* When the number of references to a page reach 0, call the
3904 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003905 */
drhed7c8552001-04-11 14:29:21 +00003906 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003907
3908 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003909 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003910 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003911 }
drhd9b02572001-04-15 00:37:09 +00003912
3913 /* When all pages reach the freelist, drop the read lock from
3914 ** the database file.
3915 */
3916 pPager->nRef--;
3917 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003918 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003919 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003920 }
drhed7c8552001-04-11 14:29:21 +00003921 }
danielk1977ae72d982007-10-03 08:46:44 +00003922 pagerLeave(pPager);
drhd9b02572001-04-15 00:37:09 +00003923 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003924}
3925
3926/*
drha6abd042004-06-09 17:37:22 +00003927** Create a journal file for pPager. There should already be a RESERVED
3928** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003929**
3930** Return SQLITE_OK if everything. Return an error code and release the
3931** write lock if anything goes wrong.
3932*/
3933static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003934 sqlite3_vfs *pVfs = pPager->pVfs;
3935 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3936
drhda47d772002-12-02 04:25:19 +00003937 int rc;
drhb7f91642004-10-31 02:22:47 +00003938 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003939 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003940 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003941 assert( pPager->pInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003942 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003943 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003944 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00003945 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003946 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003947 rc = SQLITE_NOMEM;
3948 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003949 }
danielk1977b4b47412007-08-17 15:53:36 +00003950
drhfdc40e92008-04-17 20:59:37 +00003951 if( pPager->journalOpen==0 ){
3952 if( pPager->tempFile ){
3953 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3954 }else{
3955 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003956 }
drhfdc40e92008-04-17 20:59:37 +00003957#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3958 rc = sqlite3JournalOpen(
3959 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3960 );
3961#else
3962 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
3963#endif
3964 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3965 pPager->journalOff = 0;
3966 pPager->setMaster = 0;
3967 pPager->journalHdr = 0;
3968 if( rc!=SQLITE_OK ){
3969 if( rc==SQLITE_NOMEM ){
3970 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3971 }
3972 goto failed_to_open_journal;
3973 }
drhda47d772002-12-02 04:25:19 +00003974 }
3975 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003976 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003977 pPager->needSync = 0;
3978 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003979 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003980 if( pPager->errCode ){
3981 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003982 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003983 }
drhda47d772002-12-02 04:25:19 +00003984 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003985
danielk197776572402004-06-25 02:38:54 +00003986 rc = writeJournalHdr(pPager);
3987
drhac69b052004-05-12 13:30:07 +00003988 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003989 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003990 }
danielk1977ae72d982007-10-03 08:46:44 +00003991 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
danielk1977df2566a2008-05-07 19:11:03 +00003992 rc = pager_end_transaction(pPager, 0);
drhda47d772002-12-02 04:25:19 +00003993 if( rc==SQLITE_OK ){
3994 rc = SQLITE_FULL;
3995 }
3996 }
drh9c105bb2004-10-02 20:38:28 +00003997 return rc;
3998
3999failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00004000 sqlite3BitvecDestroy(pPager->pInJournal);
4001 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00004002 return rc;
drhda47d772002-12-02 04:25:19 +00004003}
4004
4005/*
drh4b845d72002-03-05 12:41:19 +00004006** Acquire a write-lock on the database. The lock is removed when
4007** the any of the following happen:
4008**
drh80e35f42007-03-30 14:06:34 +00004009** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00004010** * sqlite3PagerRollback() is called.
4011** * sqlite3PagerClose() is called.
4012** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00004013**
danielk197713adf8a2004-06-03 16:08:41 +00004014** The first parameter to this routine is a pointer to any open page of the
4015** database file. Nothing changes about the page - it is used merely to
4016** acquire a pointer to the Pager structure and as proof that there is
4017** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00004018**
danielk197713adf8a2004-06-03 16:08:41 +00004019** The second parameter indicates how much space in bytes to reserve for a
4020** master journal file-name at the start of the journal when it is created.
4021**
4022** A journal file is opened if this is not a temporary file. For temporary
4023** files, the opening of the journal file is deferred until there is an
4024** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00004025**
drha6abd042004-06-09 17:37:22 +00004026** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00004027**
4028** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
4029** immediately instead of waiting until we try to flush the cache. The
4030** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00004031*/
danielk19773b8a05f2007-03-19 17:44:26 +00004032int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00004033 Pager *pPager = pPg->pPager;
4034 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00004035 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00004036 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00004037 assert( pPager->state!=PAGER_UNLOCK );
4038 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00004039 assert( pPager->pInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00004040 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00004041 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00004042 pPager->origDbSize = pPager->dbSize;
4043 }else{
drh054889e2005-11-30 03:20:31 +00004044 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00004045 if( rc==SQLITE_OK ){
4046 pPager->state = PAGER_RESERVED;
4047 if( exFlag ){
4048 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
4049 }
4050 }
danielk19779eed5052004-06-04 10:38:30 +00004051 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00004052 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00004053 return rc;
drhac69b052004-05-12 13:30:07 +00004054 }
drha6abd042004-06-09 17:37:22 +00004055 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00004056 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhfdc40e92008-04-17 20:59:37 +00004057 if( pPager->useJournal && !pPager->tempFile
4058 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drhac69b052004-05-12 13:30:07 +00004059 rc = pager_open_journal(pPager);
4060 }
drh4b845d72002-03-05 12:41:19 +00004061 }
danielk1977334cdb62007-03-26 08:05:12 +00004062 }else if( pPager->journalOpen && pPager->journalOff==0 ){
drhd138c012008-05-13 00:58:18 +00004063 /* This happens when the pager was in exclusive-access mode the last
danielk1977334cdb62007-03-26 08:05:12 +00004064 ** time a (read or write) transaction was successfully concluded
4065 ** by this connection. Instead of deleting the journal file it was
drhd138c012008-05-13 00:58:18 +00004066 ** kept open and either was truncated to 0 bytes or its header was
4067 ** overwritten with zeros.
danielk1977334cdb62007-03-26 08:05:12 +00004068 */
4069 assert( pPager->nRec==0 );
4070 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00004071 assert( pPager->pInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00004072 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00004073 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004074 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhed138fb2007-08-22 22:04:37 +00004075 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004076 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00004077 rc = SQLITE_NOMEM;
4078 }else{
drh369339d2007-03-30 16:01:55 +00004079 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00004080 rc = writeJournalHdr(pPager);
4081 }
drh4b845d72002-03-05 12:41:19 +00004082 }
danielk1977334cdb62007-03-26 08:05:12 +00004083 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00004084 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00004085 return rc;
4086}
4087
4088/*
drh605ad8f2006-05-03 23:34:05 +00004089** Make a page dirty. Set its dirty flag and add it to the dirty
4090** page list.
4091*/
4092static void makeDirty(PgHdr *pPg){
4093 if( pPg->dirty==0 ){
4094 Pager *pPager = pPg->pPager;
4095 pPg->dirty = 1;
4096 pPg->pDirty = pPager->pDirty;
4097 if( pPager->pDirty ){
4098 pPager->pDirty->pPrevDirty = pPg;
4099 }
4100 pPg->pPrevDirty = 0;
4101 pPager->pDirty = pPg;
4102 }
4103}
4104
4105/*
4106** Make a page clean. Clear its dirty bit and remove it from the
4107** dirty page list.
4108*/
4109static void makeClean(PgHdr *pPg){
4110 if( pPg->dirty ){
4111 pPg->dirty = 0;
4112 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00004113 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004114 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
4115 }
4116 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00004117 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004118 pPg->pPrevDirty->pDirty = pPg->pDirty;
4119 }else{
drh153c62c2007-08-24 03:51:33 +00004120 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004121 pPg->pPager->pDirty = pPg->pDirty;
4122 }
4123 }
4124}
4125
4126
4127/*
drhed7c8552001-04-11 14:29:21 +00004128** Mark a data page as writeable. The page is written into the journal
4129** if it is not there already. This routine must be called before making
4130** changes to a page.
4131**
4132** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00004133** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00004134** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00004135** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00004136** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00004137**
4138** If the journal file could not be written because the disk is full,
4139** then this routine returns SQLITE_FULL and does an immediate rollback.
4140** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00004141** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00004142** reset.
drhed7c8552001-04-11 14:29:21 +00004143*/
danielk19773b8a05f2007-03-19 17:44:26 +00004144static int pager_write(PgHdr *pPg){
4145 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00004146 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00004147 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00004148
drh6446c4d2001-12-15 14:22:18 +00004149 /* Check for errors
4150 */
danielk1977efaaf572006-01-16 11:29:19 +00004151 if( pPager->errCode ){
4152 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004153 }
drh5e00f6c2001-09-13 13:46:56 +00004154 if( pPager->readOnly ){
4155 return SQLITE_PERM;
4156 }
drh6446c4d2001-12-15 14:22:18 +00004157
danielk197776572402004-06-25 02:38:54 +00004158 assert( !pPager->setMaster );
4159
danielk19773c407372005-02-15 02:54:14 +00004160 CHECK_PAGE(pPg);
4161
drh538f5702007-04-13 02:14:30 +00004162 /* If this page was previously acquired with noContent==1, that means
4163 ** we didn't really read in the content of the page. This can happen
4164 ** (for example) when the page is being moved to the freelist. But
4165 ** now we are (perhaps) moving the page off of the freelist for
4166 ** reuse and we need to know its original content so that content
4167 ** can be stored in the rollback journal. So do the read at this
4168 ** time.
4169 */
drhd33d5a82007-04-26 12:11:28 +00004170 rc = pager_get_content(pPg);
4171 if( rc ){
4172 return rc;
drh538f5702007-04-13 02:14:30 +00004173 }
4174
drh6446c4d2001-12-15 14:22:18 +00004175 /* Mark the page as dirty. If the page has already been written
4176 ** to the journal then we can return right away.
4177 */
drh605ad8f2006-05-03 23:34:05 +00004178 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00004179 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00004180 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004181 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004182 }else{
drh6446c4d2001-12-15 14:22:18 +00004183
danielk1977a0bf2652004-11-04 14:30:04 +00004184 /* If we get this far, it means that the page needs to be
4185 ** written to the transaction journal or the ckeckpoint journal
4186 ** or both.
4187 **
4188 ** First check to see that the transaction journal exists and
4189 ** create it if it does not.
4190 */
4191 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00004192 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00004193 if( rc!=SQLITE_OK ){
4194 return rc;
4195 }
4196 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00004197 if( !pPager->journalOpen && pPager->useJournal
4198 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00004199 rc = pager_open_journal(pPager);
4200 if( rc!=SQLITE_OK ) return rc;
4201 }
danielk1977a0bf2652004-11-04 14:30:04 +00004202 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00004203 pPager->dbModified = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004204
4205 /* The transaction journal now exists and we have a RESERVED or an
4206 ** EXCLUSIVE lock on the main database file. Write the current page to
4207 ** the transaction journal if it is not there already.
4208 */
drhfdc40e92008-04-17 20:59:37 +00004209 if( !pPg->inJournal && (pPager->journalOpen || MEMDB) ){
danielk1977a0bf2652004-11-04 14:30:04 +00004210 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00004211 if( MEMDB ){
4212 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00004213 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004214 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00004215 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977662278e2007-11-05 15:30:12 +00004216 if( !pHist->pOrig ){
4217 return SQLITE_NOMEM;
danielk1977a0bf2652004-11-04 14:30:04 +00004218 }
danielk1977662278e2007-11-05 15:30:12 +00004219 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
danielk1977a0bf2652004-11-04 14:30:04 +00004220 }else{
drhbf4bca52007-09-06 22:19:14 +00004221 u32 cksum;
4222 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00004223
drh267cb322005-09-16 17:16:52 +00004224 /* We should never write to the journal file the page that
4225 ** contains the database locks. The following assert verifies
4226 ** that we do not. */
4227 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004228 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004229 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00004230 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4231 if( rc==SQLITE_OK ){
4232 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4233 pPager->journalOff + 4);
4234 pPager->journalOff += pPager->pageSize+4;
4235 }
4236 if( rc==SQLITE_OK ){
4237 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4238 pPager->journalOff += 4;
4239 }
4240 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00004241 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00004242 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00004243 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4244 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00004245
drhfd131da2007-08-07 17:13:03 +00004246 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004247 ** transaction will be rolled back by the layer above.
4248 */
danielk1977a0bf2652004-11-04 14:30:04 +00004249 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004250 return rc;
4251 }
danielk197707cb5602006-01-20 10:55:05 +00004252
danielk1977a0bf2652004-11-04 14:30:04 +00004253 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00004254 assert( pPager->pInJournal!=0 );
4255 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004256 pPg->needSync = !pPager->noSync;
4257 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00004258 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004259 }
drhac69b052004-05-12 13:30:07 +00004260 }
danielk197713adf8a2004-06-03 16:08:41 +00004261 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004262 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004263 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004264 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004265 }
4266 if( pPg->needSync ){
4267 pPager->needSync = 1;
4268 }
4269 pPg->inJournal = 1;
4270 }
4271
4272 /* If the statement journal is open and the page is not in it,
4273 ** then write the current page to the statement journal. Note that
4274 ** the statement journal format differs from the standard journal format
4275 ** in that it omits the checksums and the header.
4276 */
danielk1977f35843b2007-04-07 15:03:17 +00004277 if( pPager->stmtInUse
4278 && !pageInStatement(pPg)
4279 && (int)pPg->pgno<=pPager->stmtSize
4280 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004281 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4282 if( MEMDB ){
4283 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4284 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00004285 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004286 if( pHist->pStmt ){
4287 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4288 }
drh4f0c5872007-03-26 22:05:01 +00004289 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004290 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004291 }else{
danielk197762079062007-08-15 17:08:46 +00004292 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00004293 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4294 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4295 if( rc==SQLITE_OK ){
4296 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4297 }
drh4f0c5872007-03-26 22:05:01 +00004298 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004299 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004300 return rc;
4301 }
danielk1977a0bf2652004-11-04 14:30:04 +00004302 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00004303 assert( pPager->pInStmt!=0 );
4304 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00004305 }
drhd9b02572001-04-15 00:37:09 +00004306 }
drhfa86c412002-02-02 15:01:15 +00004307 }
4308
4309 /* Update the database size and return.
4310 */
drh1aa2d8b2007-01-03 15:34:29 +00004311 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004312 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004313 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004314 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004315 pPager->dbSize++;
4316 }
drh306dc212001-05-21 13:45:10 +00004317 }
drh69688d52001-04-14 16:38:23 +00004318 return rc;
drhed7c8552001-04-11 14:29:21 +00004319}
4320
4321/*
danielk19774099f6e2007-03-19 11:25:20 +00004322** This function is used to mark a data-page as writable. It uses
4323** pager_write() to open a journal file (if it is not already open)
4324** and write the page *pData to the journal.
4325**
4326** The difference between this function and pager_write() is that this
4327** function also deals with the special case where 2 or more pages
4328** fit on a single disk sector. In this case all co-resident pages
4329** must have been written to the journal file before returning.
4330*/
danielk19773b8a05f2007-03-19 17:44:26 +00004331int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004332 int rc = SQLITE_OK;
4333
danielk19773b8a05f2007-03-19 17:44:26 +00004334 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004335 Pager *pPager = pPg->pPager;
4336 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4337
drh86f8c192007-08-22 00:39:19 +00004338 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004339 if( !MEMDB && nPagePerSector>1 ){
4340 Pgno nPageCount; /* Total number of pages in database file */
4341 Pgno pg1; /* First page of the sector pPg is located on. */
4342 int nPage; /* Number of pages starting at pg1 to journal */
4343 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004344 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004345
4346 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4347 ** header to be written between the pages journaled by this function.
4348 */
4349 assert( pPager->doNotSync==0 );
4350 pPager->doNotSync = 1;
4351
4352 /* This trick assumes that both the page-size and sector-size are
4353 ** an integer power of 2. It sets variable pg1 to the identifier
4354 ** of the first page of the sector pPg is located on.
4355 */
4356 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4357
danielk19773b8a05f2007-03-19 17:44:26 +00004358 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004359 if( pPg->pgno>nPageCount ){
4360 nPage = (pPg->pgno - pg1)+1;
4361 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4362 nPage = nPageCount+1-pg1;
4363 }else{
4364 nPage = nPagePerSector;
4365 }
4366 assert(nPage>0);
4367 assert(pg1<=pPg->pgno);
4368 assert((pg1+nPage)>pPg->pgno);
4369
4370 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4371 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004372 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00004373 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00004374 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004375 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004376 if( rc==SQLITE_OK ){
4377 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004378 if( pPage->needSync ){
4379 needSync = 1;
4380 }
danielk19773b8a05f2007-03-19 17:44:26 +00004381 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004382 }
4383 }
drhc81945e2008-03-10 14:12:53 +00004384 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk1977dd97a492007-08-22 18:54:32 +00004385 if( pPage->needSync ){
4386 needSync = 1;
4387 }
danielk19774099f6e2007-03-19 11:25:20 +00004388 }
4389 }
4390
danielk1977dd97a492007-08-22 18:54:32 +00004391 /* If the PgHdr.needSync flag is set for any of the nPage pages
4392 ** starting at pg1, then it needs to be set for all of them. Because
4393 ** writing to any of these nPage pages may damage the others, the
4394 ** journal file must contain sync()ed copies of all of them
4395 ** before any of them can be written out to the database file.
4396 */
4397 if( needSync ){
4398 for(ii=0; ii<nPage && needSync; ii++){
4399 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4400 if( pPage ) pPage->needSync = 1;
4401 }
4402 assert(pPager->needSync);
4403 }
4404
danielk19774099f6e2007-03-19 11:25:20 +00004405 assert( pPager->doNotSync==1 );
4406 pPager->doNotSync = 0;
4407 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004408 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004409 }
drh86f8c192007-08-22 00:39:19 +00004410 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004411 return rc;
4412}
4413
4414/*
drhaacc5432002-01-06 17:07:40 +00004415** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004416** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004417** to change the content of the page.
4418*/
danielk19777d3a6662006-01-23 16:21:05 +00004419#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004420int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004421 return pPg->dirty;
4422}
danielk19777d3a6662006-01-23 16:21:05 +00004423#endif
drh6019e162001-07-02 17:51:45 +00004424
drh001bbcb2003-03-19 03:14:00 +00004425/*
drh30e58752002-03-02 20:41:57 +00004426** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004427** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004428** that page might be marked as dirty.
4429**
4430** The overlying software layer calls this routine when all of the data
4431** on the given page is unused. The pager marks the page as clean so
4432** that it does not get written to disk.
4433**
4434** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004435** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004436** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004437**
4438** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004439** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004440** will thereafter be ignored. This is necessary to avoid a problem
4441** where a page with data is added to the freelist during one part of
4442** a transaction then removed from the freelist during a later part
4443** of the same transaction and reused for some other purpose. When it
4444** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004445** the sqlite3PagerDontRollback() routine is called. But because the
4446** page contains critical data, we still need to be sure it gets
4447** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004448*/
drh538f5702007-04-13 02:14:30 +00004449void sqlite3PagerDontWrite(DbPage *pDbPage){
4450 PgHdr *pPg = pDbPage;
4451 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004452
drhb7f91642004-10-31 02:22:47 +00004453 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004454 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004455 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004456 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004457 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004458 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4459 /* If this pages is the last page in the file and the file has grown
4460 ** during the current transaction, then do NOT mark the page as clean.
4461 ** When the database file grows, we must make sure that the last page
4462 ** gets written at least once so that the disk file will be the correct
4463 ** size. If you do not write this page and the size of the file
4464 ** on the disk ends up being too small, that can lead to database
4465 ** corruption during the next transaction.
4466 */
4467 }else{
drh538f5702007-04-13 02:14:30 +00004468 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4469 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004470 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004471#ifdef SQLITE_CHECK_PAGES
4472 pPg->pageHash = pager_pagehash(pPg);
4473#endif
drh8124a302002-06-25 14:43:57 +00004474 }
drh30e58752002-03-02 20:41:57 +00004475 }
drh86f8c192007-08-22 00:39:19 +00004476 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004477}
4478
4479/*
4480** A call to this routine tells the pager that if a rollback occurs,
4481** it is not necessary to restore the data on the given page. This
4482** means that the pager does not have to record the given page in the
4483** rollback journal.
drh538f5702007-04-13 02:14:30 +00004484**
4485** If we have not yet actually read the content of this page (if
4486** the PgHdr.needRead flag is set) then this routine acts as a promise
4487** that we will never need to read the page content in the future.
4488** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00004489*/
danielk19773b8a05f2007-03-19 17:44:26 +00004490void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004491 Pager *pPager = pPg->pPager;
4492
drh86f8c192007-08-22 00:39:19 +00004493 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004494 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00004495
4496 /* If the journal file is not open, or DontWrite() has been called on
4497 ** this page (DontWrite() sets the alwaysRollback flag), then this
4498 ** function is a no-op.
4499 */
4500 if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){
danielk197787c29a92008-01-18 11:33:16 +00004501 pagerLeave(pPager);
4502 return;
4503 }
danielk1977a55e9352008-01-21 13:04:34 +00004504 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
4505
drhc5d0bd92008-04-14 01:00:57 +00004506#ifdef SQLITE_SECURE_DELETE
4507 if( pPg->inJournal || (int)pPg->pgno > pPager->origDbSize ){
4508 return;
4509 }
4510#endif
4511
4512 /* If SECURE_DELETE is disabled, then there is no way that this
4513 ** routine can be called on a page for which sqlite3PagerDontWrite()
4514 ** has not been previously called during the same transaction.
4515 ** And if DontWrite() has previously been called, the following
4516 ** conditions must be met.
danielk1977a55e9352008-01-21 13:04:34 +00004517 */
4518 assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize );
4519
drhf5e7bb52008-02-18 14:47:33 +00004520 assert( pPager->pInJournal!=0 );
4521 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a55e9352008-01-21 13:04:34 +00004522 pPg->inJournal = 1;
4523 pPg->needRead = 0;
4524 if( pPager->stmtInUse ){
drhc5d0bd92008-04-14 01:00:57 +00004525 assert( pPager->stmtSize >= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00004526 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00004527 }
danielk1977a55e9352008-01-21 13:04:34 +00004528 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
4529 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh86f8c192007-08-22 00:39:19 +00004530 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004531}
4532
drhac69b052004-05-12 13:30:07 +00004533
drh30e58752002-03-02 20:41:57 +00004534/*
drh80e35f42007-03-30 14:06:34 +00004535** This routine is called to increment the database file change-counter,
4536** stored at byte 24 of the pager file.
4537*/
danielk1977c7b60172007-08-22 11:22:03 +00004538static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004539 PgHdr *pPgHdr;
4540 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004541 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004542
4543 if( !pPager->changeCountDone ){
4544 /* Open page 1 of the file for writing. */
4545 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4546 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004547
4548 if( !isDirect ){
4549 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00004550 if( rc!=SQLITE_OK ){
4551 sqlite3PagerUnref(pPgHdr);
4552 return rc;
4553 }
danielk1977c7b60172007-08-22 11:22:03 +00004554 }
4555
drh80e35f42007-03-30 14:06:34 +00004556 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004557 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004558 change_counter++;
4559 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004560
4561 if( isDirect && pPager->fd->pMethods ){
4562 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4563 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4564 }
4565
drh80e35f42007-03-30 14:06:34 +00004566 /* Release the page reference. */
4567 sqlite3PagerUnref(pPgHdr);
4568 pPager->changeCountDone = 1;
4569 }
danielk1977c7b60172007-08-22 11:22:03 +00004570 return rc;
drh80e35f42007-03-30 14:06:34 +00004571}
4572
4573/*
danielk1977f653d782008-03-20 11:04:21 +00004574** Sync the pager file to disk.
4575*/
4576int sqlite3PagerSync(Pager *pPager){
4577 int rc;
4578 pagerEnter(pPager);
4579 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4580 pagerLeave(pPager);
4581 return rc;
4582}
4583
4584/*
drh80e35f42007-03-30 14:06:34 +00004585** Sync the database file for the pager pPager. zMaster points to the name
4586** of a master journal file that should be written into the individual
4587** journal file. zMaster may be NULL, which is interpreted as no master
4588** journal (a single database transaction).
4589**
4590** This routine ensures that the journal is synced, all dirty pages written
4591** to the database file and the database file synced. The only thing that
4592** remains to commit the transaction is to delete the journal file (or
4593** master journal file if specified).
4594**
4595** Note that if zMaster==NULL, this does not overwrite a previous value
4596** passed to an sqlite3PagerCommitPhaseOne() call.
4597**
4598** If parameter nTrunc is non-zero, then the pager file is truncated to
4599** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00004600**
4601** If the final parameter - noSync - is true, then the database file itself
4602** is not synced. The caller must call sqlite3PagerSync() directly to
4603** sync the database file before calling CommitPhaseTwo() to delete the
4604** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00004605*/
danielk1977f653d782008-03-20 11:04:21 +00004606int sqlite3PagerCommitPhaseOne(
4607 Pager *pPager,
4608 const char *zMaster,
4609 Pgno nTrunc,
4610 int noSync
4611){
drh80e35f42007-03-30 14:06:34 +00004612 int rc = SQLITE_OK;
4613
drhd138c012008-05-13 00:58:18 +00004614 /* If no changes have been made, we can leave the transaction early.
4615 */
4616 if( pPager->dbModified==0 &&
4617 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
4618 pPager->exclusiveMode!=0) ){
4619 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
4620 return SQLITE_OK;
4621 }
4622
drh80e35f42007-03-30 14:06:34 +00004623 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4624 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004625 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004626
4627 /* If this is an in-memory db, or no pages have been written to, or this
4628 ** function has already been called, it is a no-op.
4629 */
4630 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4631 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004632
danielk1977c7b60172007-08-22 11:22:03 +00004633#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4634 /* The atomic-write optimization can be used if all of the
4635 ** following are true:
4636 **
4637 ** + The file-system supports the atomic-write property for
4638 ** blocks of size page-size, and
4639 ** + This commit is not part of a multi-file transaction, and
4640 ** + Exactly one page has been modified and store in the journal file.
4641 **
4642 ** If the optimization can be used, then the journal file will never
4643 ** be created for this transaction.
4644 */
danielk1977f55b8992007-08-24 08:15:53 +00004645 int useAtomicWrite = (
4646 !zMaster &&
danielk1977700b9c52008-04-24 12:37:40 +00004647 pPager->journalOpen &&
danielk1977f55b8992007-08-24 08:15:53 +00004648 pPager->journalOff==jrnlBufferSize(pPager) &&
4649 nTrunc==0 &&
4650 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4651 );
danielk1977700b9c52008-04-24 12:37:40 +00004652 assert( pPager->journalOpen || pPager->journalMode==PAGER_JOURNALMODE_OFF );
danielk1977f55b8992007-08-24 08:15:53 +00004653 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004654 /* Update the nRec field in the journal file. */
4655 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4656 assert(pPager->nRec==1);
4657 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4658
4659 /* Update the db file change counter. The following call will modify
4660 ** the in-memory representation of page 1 to include the updated
4661 ** change counter and then write page 1 directly to the database
4662 ** file. Because of the atomic-write property of the host file-system,
4663 ** this is safe.
4664 */
danielk1977ae72d982007-10-03 08:46:44 +00004665 if( rc==SQLITE_OK ){
4666 rc = pager_incr_changecounter(pPager, 1);
4667 }
danielk1977f55b8992007-08-24 08:15:53 +00004668 }else{
4669 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00004670 }
4671
danielk1977ae72d982007-10-03 08:46:44 +00004672 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00004673#endif
4674
drh80e35f42007-03-30 14:06:34 +00004675 /* If a master journal file name has already been written to the
4676 ** journal file, then no sync is required. This happens when it is
4677 ** written, then the process fails to upgrade from a RESERVED to an
4678 ** EXCLUSIVE lock. The next time the process tries to commit the
4679 ** transaction the m-j name will have already been written.
4680 */
4681 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00004682 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004683 if( rc!=SQLITE_OK ) goto sync_exit;
4684#ifndef SQLITE_OMIT_AUTOVACUUM
4685 if( nTrunc!=0 ){
4686 /* If this transaction has made the database smaller, then all pages
4687 ** being discarded by the truncation must be written to the journal
4688 ** file.
4689 */
4690 Pgno i;
4691 int iSkip = PAGER_MJ_PGNO(pPager);
4692 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
drhf5e7bb52008-02-18 14:47:33 +00004693 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
drh80e35f42007-03-30 14:06:34 +00004694 rc = sqlite3PagerGet(pPager, i, &pPg);
4695 if( rc!=SQLITE_OK ) goto sync_exit;
4696 rc = sqlite3PagerWrite(pPg);
4697 sqlite3PagerUnref(pPg);
4698 if( rc!=SQLITE_OK ) goto sync_exit;
4699 }
4700 }
4701 }
4702#endif
4703 rc = writeMasterJournal(pPager, zMaster);
4704 if( rc!=SQLITE_OK ) goto sync_exit;
4705 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004706 }
danielk1977c7b60172007-08-22 11:22:03 +00004707 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004708
4709#ifndef SQLITE_OMIT_AUTOVACUUM
4710 if( nTrunc!=0 ){
4711 rc = sqlite3PagerTruncate(pPager, nTrunc);
4712 if( rc!=SQLITE_OK ) goto sync_exit;
4713 }
4714#endif
4715
4716 /* Write all dirty pages to the database file */
4717 pPg = pager_get_all_dirty_pages(pPager);
4718 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004719 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004720 assert( rc!=SQLITE_IOERR_BLOCKED );
4721 /* The error might have left the dirty list all fouled up here,
4722 ** but that does not matter because if the if the dirty list did
4723 ** get corrupted, then the transaction will roll back and
4724 ** discard the dirty list. There is an assert in
4725 ** pager_get_all_dirty_pages() that verifies that no attempt
4726 ** is made to use an invalid dirty list.
4727 */
drh153c62c2007-08-24 03:51:33 +00004728 goto sync_exit;
4729 }
drh3cdd7d32007-03-30 14:46:01 +00004730 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004731
4732 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004733 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004734 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004735 }
4736 IOTRACE(("DBSYNC %p\n", pPager))
4737
4738 pPager->state = PAGER_SYNCED;
4739 }else if( MEMDB && nTrunc!=0 ){
4740 rc = sqlite3PagerTruncate(pPager, nTrunc);
4741 }
4742
4743sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004744 if( rc==SQLITE_IOERR_BLOCKED ){
4745 /* pager_incr_changecounter() may attempt to obtain an exclusive
4746 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004747 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004748 * better to return SQLITE_BUSY.
4749 */
4750 rc = SQLITE_BUSY;
4751 }
drh86f8c192007-08-22 00:39:19 +00004752 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004753 return rc;
4754}
4755
4756
4757/*
drhed7c8552001-04-11 14:29:21 +00004758** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004759**
4760** If the commit fails for any reason, a rollback attempt is made
4761** and an error code is returned. If the commit worked, SQLITE_OK
4762** is returned.
drhed7c8552001-04-11 14:29:21 +00004763*/
drh80e35f42007-03-30 14:06:34 +00004764int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004765 int rc;
drhed7c8552001-04-11 14:29:21 +00004766 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004767
danielk1977efaaf572006-01-16 11:29:19 +00004768 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004769 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004770 }
drha6abd042004-06-09 17:37:22 +00004771 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004772 return SQLITE_ERROR;
4773 }
drhd138c012008-05-13 00:58:18 +00004774 if( pPager->dbModified==0 &&
4775 (pPager->journalMode!=PAGER_JOURNALMODE_DELETE ||
4776 pPager->exclusiveMode!=0) ){
4777 assert( pPager->dirtyCache==0 || pPager->journalOpen==0 );
4778 return SQLITE_OK;
4779 }
drh86f8c192007-08-22 00:39:19 +00004780 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004781 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004782 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004783 pPg = pager_get_all_dirty_pages(pPager);
4784 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004785 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4786 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004787 pPg->dirty = 0;
4788 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004789 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004790 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004791 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004792 pPg = pPg->pDirty;
4793 }
drh605ad8f2006-05-03 23:34:05 +00004794 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004795#ifndef NDEBUG
4796 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4797 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4798 assert( !pPg->alwaysRollback );
4799 assert( !pHist->pOrig );
4800 assert( !pHist->pStmt );
4801 }
4802#endif
drhac69b052004-05-12 13:30:07 +00004803 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004804 pPager->state = PAGER_SHARED;
danielk197787c29a92008-01-18 11:33:16 +00004805 pagerLeave(pPager);
drhac69b052004-05-12 13:30:07 +00004806 return SQLITE_OK;
4807 }
drh3cdd7d32007-03-30 14:46:01 +00004808 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
danielk1977df2566a2008-05-07 19:11:03 +00004809 rc = pager_end_transaction(pPager, pPager->setMaster);
drh86f8c192007-08-22 00:39:19 +00004810 rc = pager_error(pPager, rc);
4811 pagerLeave(pPager);
4812 return rc;
drhed7c8552001-04-11 14:29:21 +00004813}
4814
4815/*
drha6abd042004-06-09 17:37:22 +00004816** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004817** All in-memory cache pages revert to their original data contents.
4818** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004819**
4820** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004821** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004822** process is writing trash into the journal file (SQLITE_CORRUPT) or
4823** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4824** codes are returned for all these occasions. Otherwise,
4825** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004826*/
danielk19773b8a05f2007-03-19 17:44:26 +00004827int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004828 int rc;
drh4f0c5872007-03-26 22:05:01 +00004829 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004830 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004831 PgHdr *p;
4832 for(p=pPager->pAll; p; p=p->pNextAll){
4833 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004834 assert( !p->alwaysRollback );
4835 if( !p->dirty ){
4836 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4837 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4838 continue;
4839 }
4840
drhac69b052004-05-12 13:30:07 +00004841 pHist = PGHDR_TO_HIST(p, pPager);
4842 if( pHist->pOrig ){
4843 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004844 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004845 }else{
drh4f0c5872007-03-26 22:05:01 +00004846 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004847 }
4848 clearHistory(pHist);
4849 p->dirty = 0;
4850 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004851 pHist->inStmt = 0;
4852 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004853 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004854 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004855 }
drhac69b052004-05-12 13:30:07 +00004856 }
drh605ad8f2006-05-03 23:34:05 +00004857 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004858 pPager->pStmt = 0;
4859 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004860 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004861 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004862 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004863 return SQLITE_OK;
4864 }
4865
drh86f8c192007-08-22 00:39:19 +00004866 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004867 if( !pPager->dirtyCache || !pPager->journalOpen ){
danielk1977df2566a2008-05-07 19:11:03 +00004868 rc = pager_end_transaction(pPager, pPager->setMaster);
drh86f8c192007-08-22 00:39:19 +00004869 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004870 return rc;
4871 }
drhdb48ee02003-01-16 13:42:43 +00004872
danielk1977efaaf572006-01-16 11:29:19 +00004873 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004874 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004875 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004876 }
drh86f8c192007-08-22 00:39:19 +00004877 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004878 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004879 }
drha6abd042004-06-09 17:37:22 +00004880 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004881 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004882 rc = pager_playback(pPager, 0);
danielk1977df2566a2008-05-07 19:11:03 +00004883 rc2 = pager_end_transaction(pPager, pPager->setMaster);
drha6abd042004-06-09 17:37:22 +00004884 if( rc==SQLITE_OK ){
4885 rc = rc2;
4886 }
4887 }else{
danielk1977e277be02007-03-23 18:12:06 +00004888 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004889 }
danielk1977e180dd92007-04-05 17:15:52 +00004890 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004891 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004892
4893 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4894 ** cache. So call pager_error() on the way out to make any error
4895 ** persistent.
4896 */
drh86f8c192007-08-22 00:39:19 +00004897 rc = pager_error(pPager, rc);
4898 pagerLeave(pPager);
4899 return rc;
drh98808ba2001-10-18 12:34:46 +00004900}
drhd9b02572001-04-15 00:37:09 +00004901
4902/*
drh5e00f6c2001-09-13 13:46:56 +00004903** Return TRUE if the database file is opened read-only. Return FALSE
4904** if the database is (in theory) writable.
4905*/
danielk19773b8a05f2007-03-19 17:44:26 +00004906int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004907 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004908}
4909
4910/*
drh0f7eb612006-08-08 13:51:43 +00004911** Return the number of references to the pager.
4912*/
danielk19773b8a05f2007-03-19 17:44:26 +00004913int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004914 return pPager->nRef;
4915}
4916
4917#ifdef SQLITE_TEST
4918/*
drhd9b02572001-04-15 00:37:09 +00004919** This routine is used for testing and analysis only.
4920*/
danielk19773b8a05f2007-03-19 17:44:26 +00004921int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004922 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004923 a[0] = pPager->nRef;
4924 a[1] = pPager->nPage;
4925 a[2] = pPager->mxPage;
4926 a[3] = pPager->dbSize;
4927 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004928 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004929 a[6] = pPager->nHit;
4930 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004931 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004932 a[9] = pPager->nRead;
4933 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004934 return a;
4935}
drh0f7eb612006-08-08 13:51:43 +00004936#endif
drhdd793422001-06-28 01:54:48 +00004937
drhfa86c412002-02-02 15:01:15 +00004938/*
drhac69b052004-05-12 13:30:07 +00004939** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004940**
4941** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004942** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004943** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004944*/
drh86f8c192007-08-22 00:39:19 +00004945static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004946 int rc;
drhac69b052004-05-12 13:30:07 +00004947 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004948 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004949 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004950 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004951 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004952 pPager->stmtInUse = 1;
4953 pPager->stmtSize = pPager->dbSize;
4954 return SQLITE_OK;
4955 }
drhda47d772002-12-02 04:25:19 +00004956 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004957 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004958 return SQLITE_OK;
4959 }
drhfa86c412002-02-02 15:01:15 +00004960 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004961 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004962 assert( pPager->pInStmt==0 );
4963 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00004964 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004965 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004966 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004967 return SQLITE_NOMEM;
4968 }
danielk197776572402004-06-25 02:38:54 +00004969 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004970 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004971 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004972 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004973 if( !pPager->stmtOpen ){
drh334b2992007-09-06 23:28:23 +00004974 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
drh33f4e022007-09-03 15:19:34 +00004975 SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00004976 if( rc ){
4977 goto stmt_begin_failed;
4978 }
drhac69b052004-05-12 13:30:07 +00004979 pPager->stmtOpen = 1;
4980 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004981 }
drhac69b052004-05-12 13:30:07 +00004982 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004983 return SQLITE_OK;
4984
drhac69b052004-05-12 13:30:07 +00004985stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00004986 if( pPager->pInStmt ){
4987 sqlite3BitvecDestroy(pPager->pInStmt);
4988 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004989 }
4990 return rc;
4991}
drh86f8c192007-08-22 00:39:19 +00004992int sqlite3PagerStmtBegin(Pager *pPager){
4993 int rc;
4994 pagerEnter(pPager);
4995 rc = pagerStmtBegin(pPager);
4996 pagerLeave(pPager);
4997 return rc;
4998}
drhfa86c412002-02-02 15:01:15 +00004999
5000/*
drhac69b052004-05-12 13:30:07 +00005001** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00005002*/
danielk19773b8a05f2007-03-19 17:44:26 +00005003int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00005004 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00005005 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00005006 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00005007 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00005008 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00005009 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00005010 sqlite3BitvecDestroy(pPager->pInStmt);
5011 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00005012 }else{
5013 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00005014 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00005015 pNext = pHist->pNextStmt;
5016 assert( pHist->inStmt );
5017 pHist->inStmt = 0;
5018 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00005019 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00005020 pHist->pStmt = 0;
5021 }
5022 }
5023 pPager->stmtNRec = 0;
5024 pPager->stmtInUse = 0;
5025 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00005026 }
drhac69b052004-05-12 13:30:07 +00005027 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00005028 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00005029 return SQLITE_OK;
5030}
5031
5032/*
drhac69b052004-05-12 13:30:07 +00005033** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00005034*/
danielk19773b8a05f2007-03-19 17:44:26 +00005035int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00005036 int rc;
drh86f8c192007-08-22 00:39:19 +00005037 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00005038 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00005039 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00005040 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00005041 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00005042 PgHistory *pHist;
5043 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
5044 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00005045 if( pHist->pStmt ){
5046 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00005047 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00005048 pHist->pStmt = 0;
5049 }
5050 }
5051 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00005052 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00005053 rc = SQLITE_OK;
5054 }else{
5055 rc = pager_stmt_playback(pPager);
5056 }
danielk19773b8a05f2007-03-19 17:44:26 +00005057 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00005058 }else{
5059 rc = SQLITE_OK;
5060 }
drhac69b052004-05-12 13:30:07 +00005061 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00005062 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00005063 return rc;
5064}
5065
drh73509ee2003-04-06 20:44:45 +00005066/*
5067** Return the full pathname of the database file.
5068*/
danielk19773b8a05f2007-03-19 17:44:26 +00005069const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00005070 return pPager->zFilename;
5071}
5072
drhb20ea9d2004-02-09 01:20:36 +00005073/*
drhd0679ed2007-08-28 22:24:34 +00005074** Return the VFS structure for the pager.
5075*/
5076const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
5077 return pPager->pVfs;
5078}
5079
5080/*
drhcc6bb3e2007-08-31 16:11:35 +00005081** Return the file handle for the database file associated
5082** with the pager. This might return NULL if the file has
5083** not yet been opened.
5084*/
5085sqlite3_file *sqlite3PagerFile(Pager *pPager){
5086 return pPager->fd;
5087}
5088
5089/*
danielk19775865e3d2004-06-14 06:03:57 +00005090** Return the directory of the database file.
5091*/
danielk19773b8a05f2007-03-19 17:44:26 +00005092const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005093 return pPager->zDirectory;
5094}
5095
5096/*
5097** Return the full pathname of the journal file.
5098*/
danielk19773b8a05f2007-03-19 17:44:26 +00005099const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005100 return pPager->zJournal;
5101}
5102
5103/*
drh2c8997b2005-08-27 16:36:48 +00005104** Return true if fsync() calls are disabled for this pager. Return FALSE
5105** if fsync()s are executed normally.
5106*/
danielk19773b8a05f2007-03-19 17:44:26 +00005107int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00005108 return pPager->noSync;
5109}
5110
drh7c4ac0c2007-04-05 11:25:58 +00005111#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00005112/*
drhb20ea9d2004-02-09 01:20:36 +00005113** Set the codec for this pager
5114*/
danielk19773b8a05f2007-03-19 17:44:26 +00005115void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00005116 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00005117 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00005118 void *pCodecArg
5119){
5120 pPager->xCodec = xCodec;
5121 pPager->pCodecArg = pCodecArg;
5122}
drh7c4ac0c2007-04-05 11:25:58 +00005123#endif
drhb20ea9d2004-02-09 01:20:36 +00005124
danielk1977687566d2004-11-02 12:56:41 +00005125#ifndef SQLITE_OMIT_AUTOVACUUM
5126/*
danielk197787c29a92008-01-18 11:33:16 +00005127** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00005128**
drh5e385312007-06-16 04:42:12 +00005129** There must be no references to the page previously located at
5130** pgno (which we call pPgOld) though that page is allowed to be
5131** in cache. If the page previous located at pgno is not already
5132** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00005133**
drh5e385312007-06-16 04:42:12 +00005134** References to the page pPg remain valid. Updating any
5135** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00005136** allocated along with the page) is the responsibility of the caller.
5137**
danielk19775fd057a2005-03-09 13:09:43 +00005138** A transaction must be active when this routine is called. It used to be
5139** required that a statement transaction was not active, but this restriction
5140** has been removed (CREATE INDEX needs to move a page when a statement
5141** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00005142*/
danielk19773b8a05f2007-03-19 17:44:26 +00005143int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00005144 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00005145 int h;
danielk197794daf7f2004-11-08 09:26:09 +00005146 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00005147
drh86f8c192007-08-22 00:39:19 +00005148 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005149 assert( pPg->nRef>0 );
5150
drh4f0c5872007-03-26 22:05:01 +00005151 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00005152 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00005153 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00005154
danielk1977b4626a32007-04-28 15:47:43 +00005155 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00005156 if( pPg->needSync ){
5157 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00005158 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00005159 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00005160 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00005161 }
5162
drh85b623f2007-12-13 21:54:09 +00005163 /* Unlink pPg from its hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00005164 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00005165
danielk1977ef73ee92004-11-06 12:26:07 +00005166 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00005167 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00005168 ** page pgno before the 'move' operation, it needs to be retained
5169 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00005170 */
drh5e385312007-06-16 04:42:12 +00005171 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00005172 pPgOld = pager_lookup(pPager, pgno);
5173 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00005174 assert( pPgOld->nRef==0 );
5175 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00005176 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00005177 pPg->needSync = pPgOld->needSync;
5178 }else{
5179 pPg->needSync = 0;
5180 }
drhf5e7bb52008-02-18 14:47:33 +00005181 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
danielk1977687566d2004-11-02 12:56:41 +00005182
danielk1977f5fdda82004-11-03 08:44:05 +00005183 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00005184 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00005185 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00005186 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00005187 if( pPager->aHash[h] ){
5188 assert( pPager->aHash[h]->pPrevHash==0 );
5189 pPager->aHash[h]->pPrevHash = pPg;
5190 }
5191 pPg->pNextHash = pPager->aHash[h];
5192 pPager->aHash[h] = pPg;
5193 pPg->pPrevHash = 0;
5194
drh605ad8f2006-05-03 23:34:05 +00005195 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00005196 pPager->dirtyCache = 1;
drhd138c012008-05-13 00:58:18 +00005197 pPager->dbModified = 1;
danielk1977687566d2004-11-02 12:56:41 +00005198
danielk197794daf7f2004-11-08 09:26:09 +00005199 if( needSyncPgno ){
5200 /* If needSyncPgno is non-zero, then the journal file needs to be
5201 ** sync()ed before any data is written to database file page needSyncPgno.
5202 ** Currently, no such page exists in the page-cache and the
drhf5e7bb52008-02-18 14:47:33 +00005203 ** Pager.pInJournal bit has been set. This needs to be remedied by loading
danielk197794daf7f2004-11-08 09:26:09 +00005204 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00005205 **
danielk1977a98d7b42008-01-18 13:42:54 +00005206 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00005207 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00005208 ** array. Otherwise, if the page is loaded and written again in
5209 ** this transaction, it may be written to the database file before
5210 ** it is synced into the journal file. This way, it may end up in
5211 ** the journal file twice, but that is not a problem.
5212 **
danielk19773b8a05f2007-03-19 17:44:26 +00005213 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00005214 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00005215 */
5216 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005217 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00005218 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00005219 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00005220 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00005221 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
5222 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00005223 }
danielk197787c29a92008-01-18 11:33:16 +00005224 pagerLeave(pPager);
5225 return rc;
5226 }
danielk1977ae825582004-11-23 09:06:55 +00005227 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00005228 pPgHdr->needSync = 1;
5229 pPgHdr->inJournal = 1;
5230 makeDirty(pPgHdr);
5231 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00005232 }
5233
drh86f8c192007-08-22 00:39:19 +00005234 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005235 return SQLITE_OK;
5236}
5237#endif
5238
danielk19773b8a05f2007-03-19 17:44:26 +00005239/*
5240** Return a pointer to the data for the specified page.
5241*/
5242void *sqlite3PagerGetData(DbPage *pPg){
5243 return PGHDR_TO_DATA(pPg);
5244}
5245
5246/*
5247** Return a pointer to the Pager.nExtra bytes of "extra" space
5248** allocated along with the specified page.
5249*/
5250void *sqlite3PagerGetExtra(DbPage *pPg){
5251 Pager *pPager = pPg->pPager;
5252 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
5253}
5254
danielk197741483462007-03-24 16:45:04 +00005255/*
5256** Get/set the locking-mode for this pager. Parameter eMode must be one
5257** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
5258** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
5259** the locking-mode is set to the value specified.
5260**
5261** The returned value is either PAGER_LOCKINGMODE_NORMAL or
5262** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
5263** locking-mode.
5264*/
5265int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00005266 assert( eMode==PAGER_LOCKINGMODE_QUERY
5267 || eMode==PAGER_LOCKINGMODE_NORMAL
5268 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5269 assert( PAGER_LOCKINGMODE_QUERY<0 );
5270 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5271 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00005272 pPager->exclusiveMode = eMode;
5273 }
5274 return (int)pPager->exclusiveMode;
5275}
5276
drh3b020132008-04-17 17:02:01 +00005277/*
5278** Get/set the journal-mode for this pager. Parameter eMode must be one
5279** of PAGER_JOURNALMODE_QUERY, PAGER_JOURNALMODE_DELETE or
5280** PAGER_JOURNALMODE_PERSIST. If the parameter is not _QUERY, then
5281** the journal-mode is set to the value specified.
5282**
5283** The returned value is either PAGER_JOURNALMODE_DELETE or
5284** PAGER_JOURNALMODE_PERSIST, indicating the current (possibly updated)
5285** journal-mode.
5286*/
5287int sqlite3PagerJournalMode(Pager *pPager, int eMode){
5288 assert( eMode==PAGER_JOURNALMODE_QUERY
5289 || eMode==PAGER_JOURNALMODE_DELETE
drhfdc40e92008-04-17 20:59:37 +00005290 || eMode==PAGER_JOURNALMODE_PERSIST
5291 || eMode==PAGER_JOURNALMODE_OFF );
drh3b020132008-04-17 17:02:01 +00005292 assert( PAGER_JOURNALMODE_QUERY<0 );
5293 assert( PAGER_JOURNALMODE_DELETE>=0 && PAGER_JOURNALMODE_PERSIST>=0 );
drhfdc40e92008-04-17 20:59:37 +00005294 if( eMode>=0 ){
5295 pPager->journalMode = eMode;
drh3b020132008-04-17 17:02:01 +00005296 }
drhfdc40e92008-04-17 20:59:37 +00005297 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00005298}
5299
drhd919fe12007-12-11 19:34:44 +00005300#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00005301/*
5302** Print a listing of all referenced pages and their ref count.
5303*/
danielk19773b8a05f2007-03-19 17:44:26 +00005304void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00005305 PgHdr *pPg;
5306 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5307 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00005308 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5309 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00005310 }
5311}
5312#endif
drh2e66f0b2005-04-28 17:18:48 +00005313
5314#endif /* SQLITE_OMIT_DISKIO */