blob: ac0eae15191c0dd739971fb8495111b8b36beca0 [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**
drh3b020132008-04-17 17:02:01 +000021** @(#) $Id: pager.c,v 1.428 2008/04/17 17:02:01 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000026#include <string.h>
drhed7c8552001-04-11 14:29:21 +000027
28/*
drhdb48ee02003-01-16 13:42:43 +000029** Macros for troubleshooting. Normally turned off
30*/
danielk1977466be562004-06-10 02:16:01 +000031#if 0
drhd3627af2006-12-18 18:34:51 +000032#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000033#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
34#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
35#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
36#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
37#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000038#else
drh4f0c5872007-03-26 22:05:01 +000039#define PAGERTRACE1(X)
40#define PAGERTRACE2(X,Y)
41#define PAGERTRACE3(X,Y,Z)
42#define PAGERTRACE4(X,Y,Z,W)
43#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000044#endif
45
danielk1977599fcba2004-11-08 07:13:13 +000046/*
drh4f0c5872007-03-26 22:05:01 +000047** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000048** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000049**
drh85b623f2007-12-13 21:54:09 +000050** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000051** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000052** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000053*/
drhc001c582006-03-06 18:23:16 +000054#define PAGERID(p) ((int)(p->fd))
55#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000056
57/*
drhed7c8552001-04-11 14:29:21 +000058** The page cache as a whole is always in one of the following
59** states:
60**
drha6abd042004-06-09 17:37:22 +000061** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000062** writing the database file. There is no
63** data held in memory. This is the initial
64** state.
65**
drha6abd042004-06-09 17:37:22 +000066** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000067** Writing is not permitted. There can be
68** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000069** file at the same time.
drhed7c8552001-04-11 14:29:21 +000070**
drh726de592004-06-10 23:35:50 +000071** PAGER_RESERVED This process has reserved the database for writing
72** but has not yet made any changes. Only one process
73** at a time can reserve the database. The original
74** database file has not been modified so other
75** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000076** database file.
77**
78** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000079** Access is exclusive. No other processes or
80** threads can be reading or writing while one
81** process is writing.
82**
danielk1977aa5ccdf2004-06-14 05:10:42 +000083** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
84** after all dirty pages have been written to the
85** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000086** disk. All that remains to do is to remove or
87** truncate the journal file and the transaction
88** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000089**
drha6abd042004-06-09 17:37:22 +000090** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000091** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000092** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000093** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000094** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000095** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000096** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000097** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000098** PAGER_RESERVED means that there is an open rollback journal.
99** The transition to PAGER_EXCLUSIVE occurs before any changes
100** are made to the database file, though writes to the rollback
101** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
102** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
103** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000104*/
drha6abd042004-06-09 17:37:22 +0000105#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000106#define PAGER_SHARED 1 /* same as SHARED_LOCK */
107#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
108#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
109#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000110
drh684917c2004-10-05 02:41:42 +0000111/*
112** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
113** then failed attempts to get a reserved lock will invoke the busy callback.
114** This is off by default. To see why, consider the following scenario:
115**
116** Suppose thread A already has a shared lock and wants a reserved lock.
117** Thread B already has a reserved lock and wants an exclusive lock. If
118** both threads are using their busy callbacks, it might be a long time
119** be for one of the threads give up and allows the other to proceed.
120** But if the thread trying to get the reserved lock gives up quickly
121** (if it never invokes its busy callback) then the contention will be
122** resolved quickly.
123*/
124#ifndef SQLITE_BUSY_RESERVED_LOCK
125# define SQLITE_BUSY_RESERVED_LOCK 0
126#endif
drhd9b02572001-04-15 00:37:09 +0000127
drhed7c8552001-04-11 14:29:21 +0000128/*
drh887dc4c2004-10-22 16:22:57 +0000129** This macro rounds values up so that if the value is an address it
130** is guaranteed to be an address that is aligned to an 8-byte boundary.
131*/
132#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
133
danielk19779f61c2f2007-08-27 17:27:49 +0000134typedef struct PgHdr PgHdr;
danielk197784f786f2007-08-28 08:00:17 +0000135
136/*
137** Each pager stores all currently unreferenced pages in a list sorted
138** in least-recently-used (LRU) order (i.e. the first item on the list has
139** not been referenced in a long time, the last item has been recently
140** used). An instance of this structure is included as part of each
141** pager structure for this purpose (variable Pager.lru).
142**
143** Additionally, if memory-management is enabled, all unreferenced pages
144** are stored in a global LRU list (global variable sqlite3LruPageList).
145**
146** In both cases, the PagerLruList.pFirstSynced variable points to
147** the first page in the corresponding list that does not require an
drh85b623f2007-12-13 21:54:09 +0000148** fsync() operation before its memory can be reclaimed. If no such
danielk197784f786f2007-08-28 08:00:17 +0000149** page exists, PagerLruList.pFirstSynced is set to NULL.
150*/
151typedef struct PagerLruList PagerLruList;
152struct PagerLruList {
153 PgHdr *pFirst; /* First page in LRU list */
154 PgHdr *pLast; /* Last page in LRU list (the most recently used) */
155 PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0 */
156};
157
158/*
159** The following structure contains the next and previous pointers used
160** to link a PgHdr structure into a PagerLruList linked list.
161*/
danielk19779f61c2f2007-08-27 17:27:49 +0000162typedef struct PagerLruLink PagerLruLink;
163struct PagerLruLink {
164 PgHdr *pNext;
165 PgHdr *pPrev;
166};
167
drh887dc4c2004-10-22 16:22:57 +0000168/*
drhed7c8552001-04-11 14:29:21 +0000169** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +0000170** This header is only visible to this pager module. The client
171** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +0000172**
danielk19773b8a05f2007-03-19 17:44:26 +0000173** Client code should call sqlite3PagerWrite() on a page prior to making
174** any modifications to that page. The first time sqlite3PagerWrite()
drhf6038712004-02-08 18:07:34 +0000175** is called, the original page contents are written into the rollback
176** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
177** the journal page has made it onto the disk surface, PgHdr.needSync
178** is cleared. The modified page cannot be written back into the original
179** database file until the journal pages has been synced to disk and the
180** PgHdr.needSync has been cleared.
181**
danielk19773b8a05f2007-03-19 17:44:26 +0000182** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
drhf6038712004-02-08 18:07:34 +0000183** is cleared again when the page content is written back to the original
184** database file.
drh732c8172007-06-16 11:17:45 +0000185**
186** Details of important structure elements:
187**
188** needSync
189**
190** If this is true, this means that it is not safe to write the page
191** content to the database because the original content needed
192** for rollback has not by synced to the main rollback journal.
193** The original content may have been written to the rollback journal
194** but it has not yet been synced. So we cannot write to the database
195** file because power failure might cause the page in the journal file
196** to never reach the disk. It is as if the write to the journal file
197** does not occur until the journal file is synced.
198**
199** This flag is false if the page content exactly matches what
200** currently exists in the database file. The needSync flag is also
201** false if the original content has been written to the main rollback
202** journal and synced. If the page represents a new page that has
203** been added onto the end of the database during the current
204** transaction, the needSync flag is true until the original database
205** size in the journal header has been synced to disk.
206**
207** inJournal
208**
209** This is true if the original page has been written into the main
210** rollback journal. This is always false for new pages added to
211** the end of the database file during the current transaction.
212** And this flag says nothing about whether or not the journal
213** has been synced to disk. For pages that are in the original
214** database file, the following expression should always be true:
215**
drhf5e7bb52008-02-18 14:47:33 +0000216** inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno)
drh732c8172007-06-16 11:17:45 +0000217**
drhf5e7bb52008-02-18 14:47:33 +0000218** The pPager->pInJournal object is only valid for the original
drh732c8172007-06-16 11:17:45 +0000219** pages of the database, not new pages that are added to the end
220** of the database, so obviously the above expression cannot be
221** valid for new pages. For new pages inJournal is always 0.
222**
223** dirty
224**
225** When true, this means that the content of the page has been
226** modified and needs to be written back to the database file.
227** If false, it means that either the content of the page is
228** unchanged or else the content is unimportant and we do not
229** care whether or not it is preserved.
230**
231** alwaysRollback
232**
233** This means that the sqlite3PagerDontRollback() API should be
234** ignored for this page. The DontRollback() API attempts to say
235** that the content of the page on disk is unimportant (it is an
236** unused page on the freelist) so that it is unnecessary to
237** rollback changes to this page because the content of the page
238** can change without changing the meaning of the database. This
239** flag overrides any DontRollback() attempt. This flag is set
240** when a page that originally contained valid data is added to
241** the freelist. Later in the same transaction, this page might
242** be pulled from the freelist and reused for something different
243** and at that point the DontRollback() API will be called because
244** pages taken from the freelist do not need to be protected by
245** the rollback journal. But this flag says that the page was
246** not originally part of the freelist so that it still needs to
247** be rolled back in spite of any subsequent DontRollback() calls.
248**
249** needRead
250**
251** This flag means (when true) that the content of the page has
252** not yet been loaded from disk. The in-memory content is just
253** garbage. (Actually, we zero the content, but you should not
254** make any assumptions about the content nevertheless.) If the
255** content is needed in the future, it should be read from the
256** original database file.
drhed7c8552001-04-11 14:29:21 +0000257*/
258struct PgHdr {
259 Pager *pPager; /* The pager to which this page belongs */
260 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000261 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
danielk19779f61c2f2007-08-27 17:27:49 +0000262 PagerLruLink free; /* Next and previous free pages */
drhac69b052004-05-12 13:30:07 +0000263 PgHdr *pNextAll; /* A list of all pages */
drh193a6b42002-07-07 16:52:46 +0000264 u8 inJournal; /* TRUE if has been written to journal */
drh193a6b42002-07-07 16:52:46 +0000265 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000266 u8 needSync; /* Sync journal before writing this page */
drh80e35f42007-03-30 14:06:34 +0000267 u8 alwaysRollback; /* Disable DontRollback() for this page */
drh538f5702007-04-13 02:14:30 +0000268 u8 needRead; /* Read content if PagerWrite() is called */
drhac69b052004-05-12 13:30:07 +0000269 short int nRef; /* Number of users of this page */
drhdc3e10b2006-06-15 14:31:06 +0000270 PgHdr *pDirty, *pPrevDirty; /* Dirty pages */
danielk19779f61c2f2007-08-27 17:27:49 +0000271#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
272 PagerLruLink gfree; /* Global list of nRef==0 pages */
273#endif
danielk19773c407372005-02-15 02:54:14 +0000274#ifdef SQLITE_CHECK_PAGES
275 u32 pageHash;
276#endif
drhbf4bca52007-09-06 22:19:14 +0000277 void *pData; /* Page data */
278 /* Pager.nExtra bytes of local data appended to this header */
drhed7c8552001-04-11 14:29:21 +0000279};
280
drhac69b052004-05-12 13:30:07 +0000281/*
282** For an in-memory only database, some extra information is recorded about
283** each page so that changes can be rolled back. (Journal files are not
284** used for in-memory databases.) The following information is added to
285** the end of every EXTRA block for in-memory databases.
286**
287** This information could have been added directly to the PgHdr structure.
288** But then it would take up an extra 8 bytes of storage on every PgHdr
289** even for disk-based databases. Splitting it out saves 8 bytes. This
290** is only a savings of 0.8% but those percentages add up.
291*/
292typedef struct PgHistory PgHistory;
293struct PgHistory {
294 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
295 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000296 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
297 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000298};
drh9eb9e262004-02-11 02:18:05 +0000299
300/*
301** A macro used for invoking the codec if there is one
302*/
303#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000304# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
305# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000306#else
drhc001c582006-03-06 18:23:16 +0000307# define CODEC1(P,D,N,X) /* NO-OP */
308# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000309#endif
310
drhed7c8552001-04-11 14:29:21 +0000311/*
drh69688d52001-04-14 16:38:23 +0000312** Convert a pointer to a PgHdr into a pointer to its data
313** and back again.
drhed7c8552001-04-11 14:29:21 +0000314*/
drhbf4bca52007-09-06 22:19:14 +0000315#define PGHDR_TO_DATA(P) ((P)->pData)
316#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
drhac69b052004-05-12 13:30:07 +0000317#define PGHDR_TO_HIST(P,PGR) \
drhbf4bca52007-09-06 22:19:14 +0000318 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000319
320/*
drhed7c8552001-04-11 14:29:21 +0000321** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000322**
drh4f0ee682007-03-30 20:43:40 +0000323** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000324** or SQLITE_FULL. Once one of the first three errors occurs, it persists
325** and is returned as the result of every major pager API call. The
326** SQLITE_FULL return code is slightly different. It persists only until the
327** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000328** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000329** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000330*/
331struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000332 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000333 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000334 u8 journalStarted; /* True if header of journal is synced */
335 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000336 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000337 u8 stmtOpen; /* True if the statement subjournal is open */
338 u8 stmtInUse; /* True we are in a statement subtransaction */
339 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000340 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000341 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000342 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000343 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000344 u8 tempFile; /* zFilename is a temporary file */
345 u8 readOnly; /* True for a read-only database */
346 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000347 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000348 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000349 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000350 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000351 u8 doNotSync; /* Boolean. While true, do not spill the cache */
352 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drh3b020132008-04-17 17:02:01 +0000353 u8 persistJournal; /* True if persistent_journal=ON */
drh80e35f42007-03-30 14:06:34 +0000354 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000355 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000356 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000357 int dbSize; /* Number of pages in the file */
358 int origDbSize; /* dbSize before the current change */
359 int stmtSize; /* Size of database (in pages) at stmt_begin() */
360 int nRec; /* Number of pages written to the journal */
361 u32 cksumInit; /* Quasi-random value added to every checksum */
362 int stmtNRec; /* Number of records in stmt subjournal */
363 int nExtra; /* Add this many bytes to each in-memory page */
364 int pageSize; /* Number of bytes in a page */
365 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000366 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
367 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000368 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000369 Bitvec *pInJournal; /* One bit for each page in the database file */
370 Bitvec *pInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000371 char *zFilename; /* Name of the database file */
372 char *zJournal; /* Name of the journal file */
373 char *zDirectory; /* Directory hold database and journal files */
drh334b2992007-09-06 23:28:23 +0000374 char *zStmtJrnl; /* Name of the statement journal file */
danielk197762079062007-08-15 17:08:46 +0000375 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
376 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000377 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
danielk19779f61c2f2007-08-27 17:27:49 +0000378 PagerLruList lru; /* LRU list of free pages */
drhd9b02572001-04-15 00:37:09 +0000379 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000380 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000381 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000382 i64 journalOff; /* Current byte offset in the journal file */
383 i64 journalHdr; /* Byte offset to previous journal header */
384 i64 stmtHdrOff; /* First journal header written this statement */
385 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000386 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000387 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000388#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000389 int nHit, nMiss; /* Cache hits and missing */
390 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000391#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000392 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
393 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000394#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000395 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000396 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000397#endif
drh8ca0c722006-05-07 17:49:38 +0000398 int nHash; /* Size of the pager hash table */
399 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000400#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +0000401 Pager *pNext; /* Doubly linked list of pagers on which */
402 Pager *pPrev; /* sqlite3_release_memory() will work */
403 int iInUseMM; /* Non-zero if unavailable to MM */
404 int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
danielk197713f72992005-12-18 08:51:22 +0000405#endif
danielk19778186df82007-03-06 13:45:59 +0000406 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000407 char dbFileVers[16]; /* Changes whenever database file changes */
drhd9b02572001-04-15 00:37:09 +0000408};
409
410/*
drh538f5702007-04-13 02:14:30 +0000411** The following global variables hold counters used for
412** testing purposes only. These variables do not exist in
413** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000414*/
415#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000416int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
417int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
418int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
419int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
420# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000421#else
drh538f5702007-04-13 02:14:30 +0000422# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000423#endif
424
drh86f8c192007-08-22 00:39:19 +0000425/*
426** The following variable points to the head of a double-linked list
427** of all pagers that are eligible for page stealing by the
428** sqlite3_release_memory() interface. Access to this list is
429** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
430*/
431#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
432static Pager *sqlite3PagerList = 0;
danielk19779f61c2f2007-08-27 17:27:49 +0000433static PagerLruList sqlite3LruPageList = {0, 0, 0};
drh86f8c192007-08-22 00:39:19 +0000434#endif
drh538f5702007-04-13 02:14:30 +0000435
436
drhfcd35c72005-05-21 02:48:08 +0000437/*
drh5e00f6c2001-09-13 13:46:56 +0000438** Journal files begin with the following magic string. The data
439** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000440**
drhae2b40c2004-06-09 19:03:54 +0000441** Since version 2.8.0, the journal format contains additional sanity
442** checking information. If the power fails while the journal is begin
443** written, semi-random garbage data might appear in the journal
444** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000445** to roll the journal back, the database could be corrupted. The additional
446** sanity checking data is an attempt to discover the garbage in the
447** journal and ignore it.
448**
drhae2b40c2004-06-09 19:03:54 +0000449** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000450** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000451** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000452** This cksum is initialized to a 32-bit random value that appears in the
453** journal file right after the header. The random initializer is important,
454** because garbage data that appears at the end of a journal is likely
455** data that was once in other files that have now been deleted. If the
456** garbage data came from an obsolete journal file, the checksums might
457** be correct. But by initializing the checksum to random value which
458** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000459*/
drhae2b40c2004-06-09 19:03:54 +0000460static const unsigned char aJournalMagic[] = {
461 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000462};
463
464/*
drh726de592004-06-10 23:35:50 +0000465** The size of the header and of each page in the journal is determined
466** by the following macros.
drh968af522003-02-11 14:55:40 +0000467*/
drhae2b40c2004-06-09 19:03:54 +0000468#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000469
danielk197776572402004-06-25 02:38:54 +0000470/*
471** The journal header size for this pager. In the future, this could be
472** set to some value read from the disk controller. The important
473** characteristic is that it is the same size as a disk sector.
474*/
475#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
476
drhb7f91642004-10-31 02:22:47 +0000477/*
478** The macro MEMDB is true if we are dealing with an in-memory database.
479** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
480** the value of MEMDB will be a constant and the compiler will optimize
481** out code that would never execute.
482*/
483#ifdef SQLITE_OMIT_MEMORYDB
484# define MEMDB 0
485#else
486# define MEMDB pPager->memDb
487#endif
488
489/*
danielk197776572402004-06-25 02:38:54 +0000490** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
491** reserved for working around a windows/posix incompatibility). It is
492** used in the journal to signify that the remainder of the journal file
493** is devoted to storing a master journal name - there are no more pages to
494** roll back. See comments for function writeMasterJournal() for details.
495*/
danielk1977599fcba2004-11-08 07:13:13 +0000496/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
497#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000498
drh968af522003-02-11 14:55:40 +0000499/*
danielk197726836652005-01-17 01:33:13 +0000500** The maximum legal page number is (2^31 - 1).
501*/
502#define PAGER_MAX_PGNO 2147483647
503
504/*
drh86f8c192007-08-22 00:39:19 +0000505** The pagerEnter() and pagerLeave() routines acquire and release
506** a mutex on each pager. The mutex is recursive.
507**
508** This is a special-purpose mutex. It only provides mutual exclusion
509** between the Btree and the Memory Management sqlite3_release_memory()
510** function. It does not prevent, for example, two Btrees from accessing
511** the same pager at the same time. Other general-purpose mutexes in
512** the btree layer handle that chore.
513*/
514#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
515 static void pagerEnter(Pager *p){
516 p->iInUseDB++;
517 if( p->iInUseMM && p->iInUseDB==1 ){
518 sqlite3_mutex *mutex;
519 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
520 p->iInUseDB = 0;
521 sqlite3_mutex_enter(mutex);
522 p->iInUseDB = 1;
523 sqlite3_mutex_leave(mutex);
524 }
525 assert( p->iInUseMM==0 );
526 }
527 static void pagerLeave(Pager *p){
528 p->iInUseDB--;
529 assert( p->iInUseDB>=0 );
530 }
531#else
532# define pagerEnter(X)
533# define pagerLeave(X)
534#endif
535
536/*
danielk197784f786f2007-08-28 08:00:17 +0000537** Add page pPg to the end of the linked list managed by structure
538** pList (pPg becomes the last entry in the list - the most recently
539** used). Argument pLink should point to either pPg->free or pPg->gfree,
540** depending on whether pPg is being added to the pager-specific or
541** global LRU list.
542*/
danielk19779f61c2f2007-08-27 17:27:49 +0000543static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
544 pLink->pNext = 0;
545 pLink->pPrev = pList->pLast;
546
danielk197784f786f2007-08-28 08:00:17 +0000547#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
548 assert(pLink==&pPg->free || pLink==&pPg->gfree);
549 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
550#endif
551
danielk19779f61c2f2007-08-27 17:27:49 +0000552 if( pList->pLast ){
553 int iOff = (char *)pLink - (char *)pPg;
554 PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
555 pLastLink->pNext = pPg;
556 }else{
557 assert(!pList->pFirst);
558 pList->pFirst = pPg;
559 }
560
561 pList->pLast = pPg;
562 if( !pList->pFirstSynced && pPg->needSync==0 ){
563 pList->pFirstSynced = pPg;
564 }
565}
566
danielk197784f786f2007-08-28 08:00:17 +0000567/*
568** Remove pPg from the list managed by the structure pointed to by pList.
569**
570** Argument pLink should point to either pPg->free or pPg->gfree, depending
571** on whether pPg is being added to the pager-specific or global LRU list.
572*/
danielk19779f61c2f2007-08-27 17:27:49 +0000573static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
574 int iOff = (char *)pLink - (char *)pPg;
575
danielk197784f786f2007-08-28 08:00:17 +0000576#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
577 assert(pLink==&pPg->free || pLink==&pPg->gfree);
578 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
579#endif
580
danielk19779f61c2f2007-08-27 17:27:49 +0000581 if( pPg==pList->pFirst ){
582 pList->pFirst = pLink->pNext;
583 }
584 if( pPg==pList->pLast ){
585 pList->pLast = pLink->pPrev;
586 }
587 if( pLink->pPrev ){
588 PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
589 pPrevLink->pNext = pLink->pNext;
590 }
591 if( pLink->pNext ){
592 PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
593 pNextLink->pPrev = pLink->pPrev;
594 }
595 if( pPg==pList->pFirstSynced ){
596 PgHdr *p = pLink->pNext;
597 while( p && p->needSync ){
598 PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
599 p = pL->pNext;
600 }
601 pList->pFirstSynced = p;
602 }
603
604 pLink->pNext = pLink->pPrev = 0;
605}
606
607/*
danielk197784f786f2007-08-28 08:00:17 +0000608** Add page pPg to the list of free pages for the pager. If
609** memory-management is enabled, also add the page to the global
610** list of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000611*/
612static void lruListAdd(PgHdr *pPg){
613 listAdd(&pPg->pPager->lru, &pPg->free, pPg);
614#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
615 if( !pPg->pPager->memDb ){
616 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
617 listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
618 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
619 }
620#endif
621}
622
623/*
danielk197784f786f2007-08-28 08:00:17 +0000624** Remove page pPg from the list of free pages for the associated pager.
625** If memory-management is enabled, also remove pPg from the global list
626** of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000627*/
628static void lruListRemove(PgHdr *pPg){
629 listRemove(&pPg->pPager->lru, &pPg->free, pPg);
630#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
631 if( !pPg->pPager->memDb ){
632 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
633 listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
634 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
635 }
636#endif
637}
638
639/*
danielk197784f786f2007-08-28 08:00:17 +0000640** This function is called just after the needSync flag has been cleared
641** from all pages managed by pPager (usually because the journal file
642** has just been synced). It updates the pPager->lru.pFirstSynced variable
643** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
644** variable also.
danielk19779f61c2f2007-08-27 17:27:49 +0000645*/
646static void lruListSetFirstSynced(Pager *pPager){
647 pPager->lru.pFirstSynced = pPager->lru.pFirst;
648#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
649 if( !pPager->memDb ){
650 PgHdr *p;
651 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
652 for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
653 assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
654 sqlite3LruPageList.pFirstSynced = p;
655 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
656 }
657#endif
658}
659
danielk1977f35843b2007-04-07 15:03:17 +0000660/*
661** Return true if page *pPg has already been written to the statement
662** journal (or statement snapshot has been created, if *pPg is part
663** of an in-memory database).
664*/
665static int pageInStatement(PgHdr *pPg){
666 Pager *pPager = pPg->pPager;
667 if( MEMDB ){
668 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
669 }else{
drhf5e7bb52008-02-18 14:47:33 +0000670 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000671 }
672}
drh8ca0c722006-05-07 17:49:38 +0000673
674/*
675** Change the size of the pager hash table to N. N must be a power
676** of two.
677*/
678static void pager_resize_hash_table(Pager *pPager, int N){
679 PgHdr **aHash, *pPg;
680 assert( N>0 && (N&(N-1))==0 );
drheee4c8c2008-02-18 22:24:57 +0000681#ifdef SQLITE_MALLOC_SOFT_LIMIT
682 if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){
683 N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]);
684 }
685 if( N==pPager->nHash ) return;
686#endif
drhed138fb2007-08-22 22:04:37 +0000687 pagerLeave(pPager);
drh643167f2008-01-22 21:30:53 +0000688 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pPager->aHash!=0);
drh17435752007-08-16 04:30:38 +0000689 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drh643167f2008-01-22 21:30:53 +0000690 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0);
drhed138fb2007-08-22 22:04:37 +0000691 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000692 if( aHash==0 ){
693 /* Failure to rehash is not an error. It is only a performance hit. */
694 return;
695 }
drh17435752007-08-16 04:30:38 +0000696 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000697 pPager->nHash = N;
698 pPager->aHash = aHash;
699 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000700 int h;
701 if( pPg->pgno==0 ){
702 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
703 continue;
704 }
705 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000706 pPg->pNextHash = aHash[h];
707 if( aHash[h] ){
708 aHash[h]->pPrevHash = pPg;
709 }
710 aHash[h] = pPg;
711 pPg->pPrevHash = 0;
712 }
713}
714
drhdd793422001-06-28 01:54:48 +0000715/*
drh34e79ce2004-02-08 06:05:46 +0000716** Read a 32-bit integer from the given file descriptor. Store the integer
717** that is read in *pRes. Return SQLITE_OK if everything worked, or an
718** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000719**
720** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000721*/
danielk197762079062007-08-15 17:08:46 +0000722static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000723 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000724 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000725 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000726 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000727 }
drh94f33312002-08-12 12:29:56 +0000728 return rc;
729}
730
731/*
drh97b57482006-01-10 20:32:32 +0000732** Write a 32-bit integer into a string buffer in big-endian byte order.
733*/
drha3152892007-05-05 11:48:52 +0000734#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000735
736/*
drh34e79ce2004-02-08 06:05:46 +0000737** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
738** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000739*/
danielk197762079062007-08-15 17:08:46 +0000740static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000741 char ac[4];
drh97b57482006-01-10 20:32:32 +0000742 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000743 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000744}
745
drh2554f8b2003-01-22 01:26:44 +0000746/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000747** If file pFd is open, call sqlite3OsUnlock() on it.
748*/
749static int osUnlock(sqlite3_file *pFd, int eLock){
750 if( !pFd->pMethods ){
751 return SQLITE_OK;
752 }
753 return sqlite3OsUnlock(pFd, eLock);
754}
755
756/*
danielk1977c7b60172007-08-22 11:22:03 +0000757** This function determines whether or not the atomic-write optimization
758** can be used with this pager. The optimization can be used if:
759**
760** (a) the value returned by OsDeviceCharacteristics() indicates that
761** a database page may be written atomically, and
762** (b) the value returned by OsSectorSize() is less than or equal
763** to the page size.
764**
765** If the optimization cannot be used, 0 is returned. If it can be used,
766** then the value returned is the size of the journal file when it
767** contains rollback data for exactly one page.
768*/
769#ifdef SQLITE_ENABLE_ATOMIC_WRITE
770static int jrnlBufferSize(Pager *pPager){
771 int dc; /* Device characteristics */
772 int nSector; /* Sector size */
773 int nPage; /* Page size */
774 sqlite3_file *fd = pPager->fd;
775
776 if( fd->pMethods ){
777 dc = sqlite3OsDeviceCharacteristics(fd);
778 nSector = sqlite3OsSectorSize(fd);
779 nPage = pPager->pageSize;
780 }
781
782 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
783 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
784
danielk1977f8940ae2007-08-23 11:07:10 +0000785 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000786 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
787 }
788 return 0;
789}
790#endif
791
792/*
danielk1977aef0bf62005-12-30 16:28:01 +0000793** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000794** code. The first argument is a pointer to the pager structure, the
795** second the error-code about to be returned by a pager API function.
796** The value returned is a copy of the second argument to this function.
797**
drh4f0ee682007-03-30 20:43:40 +0000798** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000799** the error becomes persistent. Until the persisten error is cleared,
800** subsequent API calls on this Pager will immediately return the same
801** error code.
802**
803** A persistent error indicates that the contents of the pager-cache
804** cannot be trusted. This state can be cleared by completely discarding
805** the contents of the pager-cache. If a transaction was active when
806** the persistent error occured, then the rollback journal may need
807** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000808*/
danielk1977ae72d982007-10-03 08:46:44 +0000809static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000810static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000811 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000812 assert(
813 pPager->errCode==SQLITE_FULL ||
814 pPager->errCode==SQLITE_OK ||
815 (pPager->errCode & 0xff)==SQLITE_IOERR
816 );
danielk1977979f38e2007-03-27 16:19:51 +0000817 if(
drh4ac285a2006-09-15 07:28:50 +0000818 rc2==SQLITE_FULL ||
819 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000820 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000821 ){
822 pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000823 if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
824 /* If the pager is already unlocked, call pager_unlock() now to
825 ** clear the error state and ensure that the pager-cache is
826 ** completely empty.
827 */
828 pager_unlock(pPager);
829 }
danielk1977aef0bf62005-12-30 16:28:01 +0000830 }
831 return rc;
832}
833
drh477731b2007-06-16 03:06:27 +0000834/*
835** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
836** on the cache using a hash function. This is used for testing
837** and debugging only.
838*/
danielk19773c407372005-02-15 02:54:14 +0000839#ifdef SQLITE_CHECK_PAGES
840/*
841** Return a 32-bit hash of the page data for pPage.
842*/
drh477731b2007-06-16 03:06:27 +0000843static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000844 u32 hash = 0;
845 int i;
drh477731b2007-06-16 03:06:27 +0000846 for(i=0; i<nByte; i++){
847 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000848 }
849 return hash;
850}
drh477731b2007-06-16 03:06:27 +0000851static u32 pager_pagehash(PgHdr *pPage){
852 return pager_datahash(pPage->pPager->pageSize,
853 (unsigned char *)PGHDR_TO_DATA(pPage));
854}
danielk19773c407372005-02-15 02:54:14 +0000855
856/*
857** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
858** is defined, and NDEBUG is not defined, an assert() statement checks
859** that the page is either dirty or still matches the calculated page-hash.
860*/
861#define CHECK_PAGE(x) checkPage(x)
862static void checkPage(PgHdr *pPg){
863 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000864 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000865 pPg->pageHash==pager_pagehash(pPg) );
866}
867
868#else
drh8ffa8172007-06-18 17:25:17 +0000869#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000870#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000871#define CHECK_PAGE(x)
872#endif
873
drhed7c8552001-04-11 14:29:21 +0000874/*
danielk197776572402004-06-25 02:38:54 +0000875** When this is called the journal file for pager pPager must be open.
876** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000877** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000878**
danielk197765839c62007-08-30 08:08:17 +0000879** zMaster must point to a buffer of at least nMaster bytes allocated by
880** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
881** enough space to write the master journal name). If the master journal
882** name in the journal is longer than nMaster bytes (including a
883** nul-terminator), then this is handled as if no master journal name
884** were present in the journal.
885**
886** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000887** SQLITE_OK returned.
888*/
danielk197765839c62007-08-30 08:08:17 +0000889static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000890 int rc;
891 u32 len;
drheb206252004-10-01 02:00:31 +0000892 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000893 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000894 int i;
danielk197776572402004-06-25 02:38:54 +0000895 unsigned char aMagic[8]; /* A buffer to hold the magic header */
896
danielk197765839c62007-08-30 08:08:17 +0000897 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000898
drh054889e2005-11-30 03:20:31 +0000899 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000900 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000901
danielk197762079062007-08-15 17:08:46 +0000902 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000903 if( rc!=SQLITE_OK ) return rc;
904
danielk197765839c62007-08-30 08:08:17 +0000905 if( len>=nMaster ){
906 return SQLITE_OK;
907 }
908
danielk197762079062007-08-15 17:08:46 +0000909 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000910 if( rc!=SQLITE_OK ) return rc;
911
danielk197762079062007-08-15 17:08:46 +0000912 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000913 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
914
danielk197765839c62007-08-30 08:08:17 +0000915 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000916 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000917 return rc;
918 }
danielk197765839c62007-08-30 08:08:17 +0000919 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000920
921 /* See if the checksum matches the master journal name */
922 for(i=0; i<len; i++){
danielk197765839c62007-08-30 08:08:17 +0000923 cksum -= zMaster[i];
924 }
danielk19778191bff2004-06-28 04:52:30 +0000925 if( cksum ){
926 /* If the checksum doesn't add up, then one or more of the disk sectors
927 ** containing the master journal filename is corrupted. This means
928 ** definitely roll back, so just return SQLITE_OK and report a (nul)
929 ** master-journal filename.
930 */
danielk197765839c62007-08-30 08:08:17 +0000931 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000932 }
danielk197776572402004-06-25 02:38:54 +0000933
934 return SQLITE_OK;
935}
936
937/*
938** Seek the journal file descriptor to the next sector boundary where a
939** journal header may be read or written. Pager.journalOff is updated with
940** the new seek offset.
941**
942** i.e for a sector size of 512:
943**
944** Input Offset Output Offset
945** ---------------------------------------
946** 0 0
947** 512 512
948** 100 512
949** 2000 2048
950**
951*/
danielk197762079062007-08-15 17:08:46 +0000952static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000953 i64 offset = 0;
954 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000955 if( c ){
956 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
957 }
958 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
959 assert( offset>=c );
960 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
961 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000962}
963
964/*
drhf3a87622008-04-17 14:16:42 +0000965** Write zeros over the header of the journal file. This has the
966** effect of invalidating the journal file and committing the
967** transaction.
968*/
969static int zeroJournalHdr(Pager *pPager){
970 int rc;
971 static const char zeroHdr[28];
972
973 IOTRACE(("JZEROHDR %p\n", pPager))
974 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
975 return rc;
976}
977
978/*
danielk197776572402004-06-25 02:38:54 +0000979** The journal file must be open when this routine is called. A journal
980** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
981** current location.
982**
983** The format for the journal header is as follows:
984** - 8 bytes: Magic identifying journal format.
985** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
986** - 4 bytes: Random number used for page hash.
987** - 4 bytes: Initial database page count.
988** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000989** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000990**
danielk197767c007b2008-03-20 04:45:49 +0000991** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000992*/
993static int writeJournalHdr(Pager *pPager){
danielk197767c007b2008-03-20 04:45:49 +0000994 char zHeader[sizeof(aJournalMagic)+20];
danielk19774099f6e2007-03-19 11:25:20 +0000995 int rc;
danielk197776572402004-06-25 02:38:54 +0000996
danielk19774099f6e2007-03-19 11:25:20 +0000997 if( pPager->stmtHdrOff==0 ){
998 pPager->stmtHdrOff = pPager->journalOff;
999 }
1000
danielk197762079062007-08-15 17:08:46 +00001001 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001002 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001003
drh97b57482006-01-10 20:32:32 +00001004 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +00001005
1006 /*
1007 ** Write the nRec Field - the number of page records that follow this
1008 ** journal header. Normally, zero is written to this value at this time.
1009 ** After the records are added to the journal (and the journal synced,
1010 ** if in full-sync mode), the zero is overwritten with the true number
1011 ** of records (see syncJournal()).
1012 **
1013 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
1014 ** reading the journal this value tells SQLite to assume that the
1015 ** rest of the journal file contains valid page records. This assumption
1016 ** is dangerous, as if a failure occured whilst writing to the journal
1017 ** file it may contain some garbage data. There are two scenarios
1018 ** where this risk can be ignored:
1019 **
1020 ** * When the pager is in no-sync mode. Corruption can follow a
1021 ** power failure in this case anyway.
1022 **
1023 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1024 ** that garbage data is never appended to the journal file.
1025 */
1026 assert(pPager->fd->pMethods||pPager->noSync);
1027 if( (pPager->noSync)
1028 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1029 ){
1030 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1031 }else{
1032 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1033 }
1034
drh97b57482006-01-10 20:32:32 +00001035 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +00001036 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +00001037 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1038 /* The initial database size */
1039 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1040 /* The assumed sector size for this process */
1041 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
danielk197767c007b2008-03-20 04:45:49 +00001042 if( pPager->journalHdr==0 ){
1043 /* The page size */
1044 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
1045 }
drhb0603412007-02-28 04:47:26 +00001046 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
danielk197762079062007-08-15 17:08:46 +00001047 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
1048 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197776572402004-06-25 02:38:54 +00001049
1050 /* The journal header has been written successfully. Seek the journal
1051 ** file descriptor to the end of the journal header sector.
1052 */
1053 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +00001054 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
danielk197762079062007-08-15 17:08:46 +00001055 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
danielk197776572402004-06-25 02:38:54 +00001056 }
1057 return rc;
1058}
1059
1060/*
1061** The journal file must be open when this is called. A journal header file
1062** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1063** file. See comments above function writeJournalHdr() for a description of
1064** the journal header format.
1065**
1066** If the header is read successfully, *nRec is set to the number of
1067** page records following this header and *dbSize is set to the size of the
1068** database before the transaction began, in pages. Also, pPager->cksumInit
1069** is set to the value read from the journal header. SQLITE_OK is returned
1070** in this case.
1071**
1072** If the journal header file appears to be corrupted, SQLITE_DONE is
1073** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1074** cannot be read from the journal file an error code is returned.
1075*/
1076static int readJournalHdr(
1077 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001078 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001079 u32 *pNRec,
1080 u32 *pDbSize
1081){
1082 int rc;
1083 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001084 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +00001085 int iPageSize;
danielk197776572402004-06-25 02:38:54 +00001086
danielk197762079062007-08-15 17:08:46 +00001087 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001088 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1089 return SQLITE_DONE;
1090 }
danielk197762079062007-08-15 17:08:46 +00001091 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001092
danielk197762079062007-08-15 17:08:46 +00001093 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001094 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001095 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001096
1097 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1098 return SQLITE_DONE;
1099 }
1100
danielk197762079062007-08-15 17:08:46 +00001101 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001102 if( rc ) return rc;
1103
danielk197762079062007-08-15 17:08:46 +00001104 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001105 if( rc ) return rc;
1106
danielk197762079062007-08-15 17:08:46 +00001107 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001108 if( rc ) return rc;
1109
danielk197767c007b2008-03-20 04:45:49 +00001110 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
1111 if( rc==SQLITE_OK
1112 && iPageSize>=512
1113 && iPageSize<=SQLITE_MAX_PAGE_SIZE
1114 && ((iPageSize-1)&iPageSize)==0
1115 ){
1116 u16 pagesize = iPageSize;
1117 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
1118 }
1119 if( rc ) return rc;
1120
danielk197776572402004-06-25 02:38:54 +00001121 /* Update the assumed sector-size to match the value used by
1122 ** the process that created this journal. If this journal was
1123 ** created by a process other than this one, then this routine
1124 ** is being called from within pager_playback(). The local value
1125 ** of Pager.sectorSize is restored at the end of that routine.
1126 */
danielk197762079062007-08-15 17:08:46 +00001127 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001128 if( rc ) return rc;
1129
1130 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001131 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001132}
1133
1134
1135/*
1136** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001137** pPager at the current location. The master journal name must be the last
1138** thing written to a journal file. If the pager is in full-sync mode, the
1139** journal file descriptor is advanced to the next sector boundary before
1140** anything is written. The format is:
1141**
1142** + 4 bytes: PAGER_MJ_PGNO.
1143** + N bytes: length of master journal name.
1144** + 4 bytes: N
1145** + 4 bytes: Master journal name checksum.
1146** + 8 bytes: aJournalMagic[].
1147**
1148** The master journal page checksum is the sum of the bytes in the master
1149** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001150**
1151** If zMaster is a NULL pointer (occurs for a single database transaction),
1152** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001153*/
1154static int writeMasterJournal(Pager *pPager, const char *zMaster){
1155 int rc;
1156 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001157 int i;
danielk197762079062007-08-15 17:08:46 +00001158 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +00001159 u32 cksum = 0;
1160 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001161
1162 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1163 pPager->setMaster = 1;
1164
1165 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001166 for(i=0; i<len; i++){
1167 cksum += zMaster[i];
1168 }
danielk197776572402004-06-25 02:38:54 +00001169
1170 /* If in full-sync mode, advance to the next disk sector before writing
1171 ** the master journal name. This is in case the previous page written to
1172 ** the journal has already been synced.
1173 */
1174 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001175 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001176 }
danielk197762079062007-08-15 17:08:46 +00001177 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001178 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001179
danielk197762079062007-08-15 17:08:46 +00001180 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001181 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001182 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001183
danielk197762079062007-08-15 17:08:46 +00001184 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001185 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001186 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001187
drh97b57482006-01-10 20:32:32 +00001188 put32bits(zBuf, len);
1189 put32bits(&zBuf[4], cksum);
1190 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001191 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +00001192 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +00001193 return rc;
1194}
1195
1196/*
drh03eb96a2002-11-10 23:32:56 +00001197** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001198** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001199**
1200** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001201** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001202** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001203** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001204*/
drh3aac2dd2004-04-26 14:10:20 +00001205static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001206 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001207 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1208 assert( MEMDB );
1209 if( !pHist->inStmt ){
1210 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1211 if( pPager->pStmt ){
1212 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1213 }
1214 pHist->pNextStmt = pPager->pStmt;
1215 pPager->pStmt = pPg;
1216 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001217 }
drh03eb96a2002-11-10 23:32:56 +00001218}
1219
1220/*
drhed7c8552001-04-11 14:29:21 +00001221** Find a page in the hash table given its page number. Return
1222** a pointer to the page or NULL if not found.
1223*/
drhd9b02572001-04-15 00:37:09 +00001224static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001225 PgHdr *p;
1226 if( pPager->aHash==0 ) return 0;
1227 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001228 while( p && p->pgno!=pgno ){
1229 p = p->pNextHash;
1230 }
1231 return p;
1232}
1233
1234/*
danielk1977e180dd92007-04-05 17:15:52 +00001235** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001236** sets the state of the pager back to what it was when it was first
1237** opened. Any outstanding pages are invalidated and subsequent attempts
1238** to access those pages will likely result in a coredump.
1239*/
drhd9b02572001-04-15 00:37:09 +00001240static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001241 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001242 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001243 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001244 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1245 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001246 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001247 lruListRemove(pPg);
drh0d180202008-02-14 23:26:56 +00001248 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00001249 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001250 }
danielk19779f61c2f2007-08-27 17:27:49 +00001251 assert(pPager->lru.pFirst==0);
1252 assert(pPager->lru.pFirstSynced==0);
1253 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001254 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001255 pPager->pAll = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001256 pPager->pDirty = 0;
drh8ca0c722006-05-07 17:49:38 +00001257 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001258 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001259 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001260 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001261 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001262}
1263
1264/*
danielk1977ae72d982007-10-03 08:46:44 +00001265** Unlock the database file.
1266**
1267** If the pager is currently in error state, discard the contents of
1268** the cache and reset the Pager structure internal state. If there is
1269** an open journal-file, then the next time a shared-lock is obtained
1270** on the pager file (by this or any other process), it will be
1271** treated as a hot-journal and rolled back.
1272*/
1273static void pager_unlock(Pager *pPager){
1274 if( !pPager->exclusiveMode ){
1275 if( !MEMDB ){
drh1aa5af12008-03-07 19:51:14 +00001276 int rc = osUnlock(pPager->fd, NO_LOCK);
drh308aa322008-03-07 20:14:38 +00001277 if( rc ) pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +00001278 pPager->dbSize = -1;
1279 IOTRACE(("UNLOCK %p\n", pPager))
1280
1281 /* If Pager.errCode is set, the contents of the pager cache cannot be
1282 ** trusted. Now that the pager file is unlocked, the contents of the
1283 ** cache can be discarded and the error code safely cleared.
1284 */
1285 if( pPager->errCode ){
drh1aa5af12008-03-07 19:51:14 +00001286 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00001287 pager_reset(pPager);
1288 if( pPager->stmtOpen ){
1289 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +00001290 sqlite3BitvecDestroy(pPager->pInStmt);
1291 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001292 }
1293 if( pPager->journalOpen ){
1294 sqlite3OsClose(pPager->jfd);
1295 pPager->journalOpen = 0;
drhf5e7bb52008-02-18 14:47:33 +00001296 sqlite3BitvecDestroy(pPager->pInJournal);
1297 pPager->pInJournal = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001298 }
1299 pPager->stmtOpen = 0;
1300 pPager->stmtInUse = 0;
1301 pPager->journalOff = 0;
1302 pPager->journalStarted = 0;
1303 pPager->stmtAutoopen = 0;
1304 pPager->origDbSize = 0;
1305 }
1306 }
1307
1308 if( !MEMDB || pPager->errCode==SQLITE_OK ){
1309 pPager->state = PAGER_UNLOCK;
1310 pPager->changeCountDone = 0;
1311 }
1312 }
1313}
1314
1315/*
1316** Execute a rollback if a transaction is active and unlock the
1317** database file. If the pager has already entered the error state,
1318** do not attempt the rollback.
1319*/
1320static void pagerUnlockAndRollback(Pager *p){
1321 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
1322 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
1323 sqlite3PagerRollback(p);
1324 }
1325 pager_unlock(p);
1326 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1327 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
1328}
1329
1330/*
drh80e35f42007-03-30 14:06:34 +00001331** This routine ends a transaction. A transaction is ended by either
1332** a COMMIT or a ROLLBACK.
1333**
drhed7c8552001-04-11 14:29:21 +00001334** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001335** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1336** the database lock and acquires a SHARED lock in its place if that is
1337** the appropriate thing to do. Release locks usually is appropriate,
1338** unless we are in exclusive access mode or unless this is a
1339** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1340**
1341** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001342**
1343** TODO: Consider keeping the journal file open for temporary databases.
1344** This might give a performance improvement on windows where opening
1345** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001346*/
drh80e35f42007-03-30 14:06:34 +00001347static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001348 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001349 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001350 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001351 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001352 if( pPager->state<PAGER_RESERVED ){
1353 return SQLITE_OK;
1354 }
danielk19773b8a05f2007-03-19 17:44:26 +00001355 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001356 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001357 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001358 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001359 }
drhda47d772002-12-02 04:25:19 +00001360 if( pPager->journalOpen ){
drhf3a87622008-04-17 14:16:42 +00001361 if( pPager->exclusiveMode && (rc = zeroJournalHdr(pPager))==SQLITE_OK ){
danielk197741483462007-03-24 16:45:04 +00001362 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001363 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001364 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001365 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001366 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001367 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001368 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001369 }
danielk197741483462007-03-24 16:45:04 +00001370 }
drhf5e7bb52008-02-18 14:47:33 +00001371 sqlite3BitvecDestroy(pPager->pInJournal);
1372 pPager->pInJournal = 0;
drhda47d772002-12-02 04:25:19 +00001373 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1374 pPg->inJournal = 0;
1375 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001376 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001377 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001378#ifdef SQLITE_CHECK_PAGES
1379 pPg->pageHash = pager_pagehash(pPg);
1380#endif
drhda47d772002-12-02 04:25:19 +00001381 }
drh605ad8f2006-05-03 23:34:05 +00001382 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001383 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001384 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001385 }else{
drhf5e7bb52008-02-18 14:47:33 +00001386 assert( pPager->pInJournal==0 );
drha6abd042004-06-09 17:37:22 +00001387 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001388 }
danielk1977979f38e2007-03-27 16:19:51 +00001389
danielk197741483462007-03-24 16:45:04 +00001390 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001391 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001392 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001393 }else if( pPager->state==PAGER_SYNCED ){
1394 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001395 }
danielk1977334cdb62007-03-26 08:05:12 +00001396 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001397 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001398 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001399 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001400 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001401
1402 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001403}
1404
drhed7c8552001-04-11 14:29:21 +00001405/*
drh968af522003-02-11 14:55:40 +00001406** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001407**
1408** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001409** random initial value and the page number. We experimented with
1410** a checksum of the entire data, but that was found to be too slow.
1411**
1412** Note that the page number is stored at the beginning of data and
1413** the checksum is stored at the end. This is important. If journal
1414** corruption occurs due to a power failure, the most likely scenario
1415** is that one end or the other of the record will be changed. It is
1416** much less likely that the two ends of the journal record will be
1417** correct and the middle be corrupt. Thus, this "checksum" scheme,
1418** though fast and simple, catches the mostly likely kind of corruption.
1419**
1420** FIX ME: Consider adding every 200th (or so) byte of the data to the
1421** checksum. That way if a single page spans 3 or more disk sectors and
1422** only the middle sector is corrupt, we will still have a reasonable
1423** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001424*/
drh74161702006-02-24 02:53:49 +00001425static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001426 u32 cksum = pPager->cksumInit;
1427 int i = pPager->pageSize-200;
1428 while( i>0 ){
1429 cksum += aData[i];
1430 i -= 200;
1431 }
drh968af522003-02-11 14:55:40 +00001432 return cksum;
1433}
1434
drh605ad8f2006-05-03 23:34:05 +00001435/* Forward declaration */
1436static void makeClean(PgHdr*);
1437
drh968af522003-02-11 14:55:40 +00001438/*
drhfa86c412002-02-02 15:01:15 +00001439** Read a single page from the journal file opened on file descriptor
1440** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001441**
drh726de592004-06-10 23:35:50 +00001442** If useCksum==0 it means this journal does not use checksums. Checksums
1443** are not used in statement journals because statement journals do not
1444** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001445*/
danielk197762079062007-08-15 17:08:46 +00001446static int pager_playback_one_page(
1447 Pager *pPager,
1448 sqlite3_file *jfd,
1449 i64 offset,
1450 int useCksum
1451){
drhfa86c412002-02-02 15:01:15 +00001452 int rc;
drhae2b40c2004-06-09 19:03:54 +00001453 PgHdr *pPg; /* An existing page in the cache */
1454 Pgno pgno; /* The page number of a page in journal */
1455 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001456 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001457
drh96362842005-03-20 22:47:56 +00001458 /* useCksum should be true for the main journal and false for
1459 ** statement journals. Verify that this is always the case
1460 */
drh9cbe6352005-11-29 03:13:21 +00001461 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001462 assert( aData );
drh96362842005-03-20 22:47:56 +00001463
danielk197762079062007-08-15 17:08:46 +00001464 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001465 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001466 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001467 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001468 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001469
drh968af522003-02-11 14:55:40 +00001470 /* Sanity checking on the page. This is more important that I originally
1471 ** thought. If a power failure occurs while the journal is being written,
1472 ** it could cause invalid data to be written into the journal. We need to
1473 ** detect this invalid data (with high probability) and ignore it.
1474 */
danielk197775edc162004-06-26 01:48:18 +00001475 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001476 return SQLITE_DONE;
1477 }
drhae2b40c2004-06-09 19:03:54 +00001478 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001479 return SQLITE_OK;
1480 }
drhae2b40c2004-06-09 19:03:54 +00001481 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001482 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001483 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001484 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001485 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001486 return SQLITE_DONE;
1487 }
1488 }
drhfa86c412002-02-02 15:01:15 +00001489
danielk1977aa5ccdf2004-06-14 05:10:42 +00001490 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001491
1492 /* If the pager is in RESERVED state, then there must be a copy of this
1493 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001494 ** not the database file. The page is left marked dirty in this case.
1495 **
danielk19772df71c72007-05-24 07:22:42 +00001496 ** An exception to the above rule: If the database is in no-sync mode
1497 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001498 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1499 ** during a Movepage() call, then the page may not be in the cache
1500 ** either. So the condition described in the above paragraph is not
1501 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001502 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001503 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1504 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001505 **
1506 ** Ticket #1171: The statement journal might contain page content that is
1507 ** different from the page content at the start of the transaction.
1508 ** This occurs when a page is changed prior to the start of a statement
1509 ** then changed again within the statement. When rolling back such a
1510 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001511 ** for certain that original page contents are synced into the main rollback
1512 ** journal. Otherwise, a power loss might leave modified data in the
1513 ** database file without an entry in the rollback journal that can
1514 ** restore the database to its original form. Two conditions must be
1515 ** met before writing to the database files. (1) the database must be
1516 ** locked. (2) we know that the original page content is fully synced
1517 ** in the main journal either because the page is not in cache or else
1518 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001519 **
1520 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1521 ** is possible to fail a statement on a database that does not yet exist.
1522 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001523 */
drhae2b40c2004-06-09 19:03:54 +00001524 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001525 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1526 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh4c02a232008-04-14 23:13:45 +00001527 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0)
1528 && pPager->fd->pMethods ){
danielk197762079062007-08-15 17:08:46 +00001529 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1530 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001531 if( pPg ){
1532 makeClean(pPg);
1533 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001534 }
drhfa86c412002-02-02 15:01:15 +00001535 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001536 /* No page should ever be explicitly rolled back that is in use, except
1537 ** for page 1 which is held in use in order to keep the lock on the
1538 ** database active. However such a page may be rolled back as a result
1539 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001540 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001541 */
drhb6f41482004-05-14 01:58:11 +00001542 void *pData;
danielk197728129562005-01-11 10:25:06 +00001543 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001544 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001545 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001546 if( pPager->xReiniter ){
1547 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001548 }
danielk19773c407372005-02-15 02:54:14 +00001549#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001550 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001551#endif
drh86a88112007-04-16 15:02:19 +00001552 /* If this was page 1, then restore the value of Pager.dbFileVers.
1553 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001554 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001555 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001556 }
drh86a88112007-04-16 15:02:19 +00001557
1558 /* Decode the page just read from disk */
1559 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001560 }
1561 return rc;
1562}
1563
1564/*
danielk197713adf8a2004-06-03 16:08:41 +00001565** Parameter zMaster is the name of a master journal file. A single journal
1566** file that referred to the master journal file has just been rolled back.
1567** This routine checks if it is possible to delete the master journal file,
1568** and does so if it is.
drh726de592004-06-10 23:35:50 +00001569**
danielk197765839c62007-08-30 08:08:17 +00001570** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1571** available for use within this function.
1572**
1573**
drh726de592004-06-10 23:35:50 +00001574** The master journal file contains the names of all child journals.
1575** To tell if a master journal can be deleted, check to each of the
1576** children. If all children are either missing or do not refer to
1577** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001578*/
danielk1977b4b47412007-08-17 15:53:36 +00001579static int pager_delmaster(Pager *pPager, const char *zMaster){
1580 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001581 int rc;
1582 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001583 sqlite3_file *pMaster;
1584 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001585 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001586 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001587
1588 /* Open the master journal file exclusively in case some other process
1589 ** is running this routine also. Not that it makes too much difference.
1590 */
danielk1977b4b47412007-08-17 15:53:36 +00001591 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001592 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001593 if( !pMaster ){
1594 rc = SQLITE_NOMEM;
1595 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001596 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1597 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001598 }
danielk197713adf8a2004-06-03 16:08:41 +00001599 if( rc!=SQLITE_OK ) goto delmaster_out;
1600 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001601
1602 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001603 if( rc!=SQLITE_OK ) goto delmaster_out;
1604
1605 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001606 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001607 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001608 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001609
1610 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001611 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001612 */
danielk197765839c62007-08-30 08:08:17 +00001613 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001614 if( !zMasterJournal ){
1615 rc = SQLITE_NOMEM;
1616 goto delmaster_out;
1617 }
danielk197765839c62007-08-30 08:08:17 +00001618 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001619 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001620 if( rc!=SQLITE_OK ) goto delmaster_out;
1621
danielk19775865e3d2004-06-14 06:03:57 +00001622 zJournal = zMasterJournal;
1623 while( (zJournal-zMasterJournal)<nMasterJournal ){
drh19db9352008-03-27 22:42:51 +00001624 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS);
1625 if( rc!=0 && rc!=1 ){
1626 rc = SQLITE_IOERR_NOMEM;
1627 goto delmaster_out;
1628 }
1629 if( rc==1 ){
danielk197713adf8a2004-06-03 16:08:41 +00001630 /* One of the journals pointed to by the master journal exists.
1631 ** Open it and check if it points at the master journal. If
1632 ** so, return without deleting the master journal file.
1633 */
drh3b7b78b2004-11-24 01:16:43 +00001634 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001635 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1636 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001637 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001638 goto delmaster_out;
1639 }
danielk197713adf8a2004-06-03 16:08:41 +00001640
danielk197765839c62007-08-30 08:08:17 +00001641 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001642 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001643 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001644 goto delmaster_out;
1645 }
danielk197776572402004-06-25 02:38:54 +00001646
danielk197765839c62007-08-30 08:08:17 +00001647 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001648 if( c ){
danielk197776572402004-06-25 02:38:54 +00001649 /* We have a match. Do not delete the master journal file. */
1650 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001651 }
1652 }
danielk19775865e3d2004-06-14 06:03:57 +00001653 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001654 }
1655 }
1656
danielk1977fee2d252007-08-18 10:59:19 +00001657 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001658
1659delmaster_out:
1660 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001661 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001662 }
1663 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001664 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001665 }
danielk1977b4b47412007-08-17 15:53:36 +00001666 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001667 return rc;
1668}
1669
drha6abd042004-06-09 17:37:22 +00001670
danielk1977e180dd92007-04-05 17:15:52 +00001671static void pager_truncate_cache(Pager *pPager);
1672
drha6abd042004-06-09 17:37:22 +00001673/*
drhcb4c40b2004-08-18 19:09:43 +00001674** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001675** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001676**
1677** Might might be the case that the file on disk is smaller than nPage.
1678** This can happen, for example, if we are in the middle of a transaction
1679** which has extended the file size and the new pages are still all held
1680** in cache, then an INSERT or UPDATE does a statement rollback. Some
1681** operating system implementations can get confused if you try to
1682** truncate a file to some size that is larger than it currently is,
1683** so detect this case and do not do the truncation.
drhcb4c40b2004-08-18 19:09:43 +00001684*/
1685static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001686 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001687 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001688 i64 currentSize, newSize;
1689 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1690 newSize = pPager->pageSize*(i64)nPage;
1691 if( rc==SQLITE_OK && currentSize>newSize ){
1692 rc = sqlite3OsTruncate(pPager->fd, newSize);
1693 }
danielk1977e180dd92007-04-05 17:15:52 +00001694 }
1695 if( rc==SQLITE_OK ){
1696 pPager->dbSize = nPage;
1697 pager_truncate_cache(pPager);
1698 }
1699 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001700}
1701
1702/*
drhc80f0582007-05-01 16:59:48 +00001703** Set the sectorSize for the given pager.
1704**
drh334c80d2008-03-28 17:41:13 +00001705** The sector size is at least as big as the sector size reported
1706** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001707*/
1708static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001709 assert(pPager->fd->pMethods||pPager->tempFile);
1710 if( !pPager->tempFile ){
1711 /* Sector size doesn't matter for temporary files. Also, the file
1712 ** may not have been opened yet, in whcih case the OsSectorSize()
1713 ** call will segfault.
1714 */
1715 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1716 }
drh334c80d2008-03-28 17:41:13 +00001717 if( pPager->sectorSize<512 ){
1718 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001719 }
1720}
1721
1722/*
drhed7c8552001-04-11 14:29:21 +00001723** Playback the journal and thus restore the database file to
1724** the state it was in before we started making changes.
1725**
drh34e79ce2004-02-08 06:05:46 +00001726** The journal file format is as follows:
1727**
drhae2b40c2004-06-09 19:03:54 +00001728** (1) 8 byte prefix. A copy of aJournalMagic[].
1729** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001730** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001731** number of page records from the journal size.
1732** (3) 4 byte big-endian integer which is the initial value for the
1733** sanity checksum.
1734** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001735** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001736** (5) 4 byte big-endian integer which is the sector size. The header
1737** is this many bytes in size.
1738** (6) 4 byte big-endian integer which is the page case.
1739** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001740** name. The value may be zero (indicate that there is no master
1741** journal.)
drh334c80d2008-03-28 17:41:13 +00001742** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001743** and might be shorter than the value read from (5). If the first byte
1744** of the name is \000 then there is no master journal. The master
1745** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001746** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001747** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001748** + pPager->pageSize bytes of data.
1749** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001750**
drh334c80d2008-03-28 17:41:13 +00001751** When we speak of the journal header, we mean the first 8 items above.
1752** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001753**
1754** Call the value from the second bullet "nRec". nRec is the number of
1755** valid page entries in the journal. In most cases, you can compute the
1756** value of nRec from the size of the journal file. But if a power
1757** failure occurred while the journal was being written, it could be the
1758** case that the size of the journal file had already been increased but
1759** the extra entries had not yet made it safely to disk. In such a case,
1760** the value of nRec computed from the file size would be too large. For
1761** that reason, we always use the nRec value in the header.
1762**
1763** If the nRec value is 0xffffffff it means that nRec should be computed
1764** from the file size. This value is used when the user selects the
1765** no-sync option for the journal. A power failure could lead to corruption
1766** in this case. But for things like temporary table (which will be
1767** deleted when the power is restored) we don't care.
1768**
drhd9b02572001-04-15 00:37:09 +00001769** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001770** journal file then all pages up to the first corrupted page are rolled
1771** back (or no pages if the journal header is corrupted). The journal file
1772** is then deleted and SQLITE_OK returned, just as if no corruption had
1773** been encountered.
1774**
1775** If an I/O or malloc() error occurs, the journal-file is not deleted
1776** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001777*/
danielk1977e277be02007-03-23 18:12:06 +00001778static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001779 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001780 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001781 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001782 int i; /* Loop counter */
1783 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001784 int rc; /* Result code of a subroutine */
danielk1977ce98bba2008-04-03 10:13:01 +00001785 int res = 0; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001786 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001787
drhc3a64ba2001-11-22 00:01:27 +00001788 /* Figure out how many records are in the journal. Abort early if
1789 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001790 */
drh8cfbf082001-09-19 13:22:39 +00001791 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001792 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001793 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001794 goto end_playback;
1795 }
drh240c5792004-02-08 00:40:52 +00001796
danielk197776572402004-06-25 02:38:54 +00001797 /* Read the master journal name from the journal, if it is present.
1798 ** If a master journal file name is specified, but the file is not
1799 ** present on disk, then the journal is not hot and does not need to be
1800 ** played back.
drh240c5792004-02-08 00:40:52 +00001801 */
danielk197765839c62007-08-30 08:08:17 +00001802 zMaster = pPager->pTmpSpace;
1803 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977ce98bba2008-04-03 10:13:01 +00001804 if( rc!=SQLITE_OK || (zMaster[0]
1805 && (res=sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))==0 )
danielk1977b4b47412007-08-17 15:53:36 +00001806 ){
danielk197776572402004-06-25 02:38:54 +00001807 zMaster = 0;
drhc3a64ba2001-11-22 00:01:27 +00001808 goto end_playback;
1809 }
danielk197765839c62007-08-30 08:08:17 +00001810 zMaster = 0;
danielk1977ce98bba2008-04-03 10:13:01 +00001811 if( res<0 ){
1812 rc = SQLITE_IOERR_NOMEM;
1813 goto end_playback;
1814 }
1815 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001816
danielk197776572402004-06-25 02:38:54 +00001817 /* This loop terminates either when the readJournalHdr() call returns
1818 ** SQLITE_DONE or an IO error occurs. */
1819 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001820
danielk197776572402004-06-25 02:38:54 +00001821 /* Read the next journal header from the journal file. If there are
1822 ** not enough bytes left in the journal file for a complete header, or
1823 ** it is corrupted, then a process must of failed while writing it.
1824 ** This indicates nothing more needs to be rolled back.
1825 */
1826 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1827 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001828 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001829 rc = SQLITE_OK;
1830 }
danielk197776572402004-06-25 02:38:54 +00001831 goto end_playback;
1832 }
1833
1834 /* If nRec is 0xffffffff, then this journal was created by a process
1835 ** working in no-sync mode. This means that the rest of the journal
1836 ** file consists of pages, there are no more journal headers. Compute
1837 ** the value of nRec based on this assumption.
1838 */
1839 if( nRec==0xffffffff ){
1840 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1841 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1842 }
1843
danielk1977e277be02007-03-23 18:12:06 +00001844 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001845 ** process and if this is the final header in the journal, then it means
1846 ** that this part of the journal was being filled but has not yet been
1847 ** synced to disk. Compute the number of pages based on the remaining
1848 ** size of the file.
1849 **
1850 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001851 */
drh8940f4e2007-08-11 00:26:20 +00001852 if( nRec==0 && !isHot &&
1853 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001854 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1855 }
1856
danielk197776572402004-06-25 02:38:54 +00001857 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001858 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001859 */
danielk1977e180dd92007-04-05 17:15:52 +00001860 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001861 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001862 if( rc!=SQLITE_OK ){
1863 goto end_playback;
1864 }
danielk197776572402004-06-25 02:38:54 +00001865 }
1866
danielk197776572402004-06-25 02:38:54 +00001867 /* Copy original pages out of the journal and back into the database file.
1868 */
1869 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001870 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001871 if( rc!=SQLITE_OK ){
1872 if( rc==SQLITE_DONE ){
1873 rc = SQLITE_OK;
1874 pPager->journalOff = szJ;
1875 break;
1876 }else{
1877 goto end_playback;
1878 }
1879 }
drh968af522003-02-11 14:55:40 +00001880 }
drhed7c8552001-04-11 14:29:21 +00001881 }
drh580eeaf2006-02-24 03:09:37 +00001882 /*NOTREACHED*/
1883 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001884
1885end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001886 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001887 zMaster = pPager->pTmpSpace;
1888 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1889 }
1890 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001891 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001892 }
danielk197765839c62007-08-30 08:08:17 +00001893 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001894 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001895 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001896 */
danielk197765839c62007-08-30 08:08:17 +00001897 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001898 }
danielk197776572402004-06-25 02:38:54 +00001899
1900 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001901 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001902 ** value. Reset it to the correct value for this process.
1903 */
drhc80f0582007-05-01 16:59:48 +00001904 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001905 return rc;
drhed7c8552001-04-11 14:29:21 +00001906}
1907
1908/*
drhac69b052004-05-12 13:30:07 +00001909** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001910**
1911** This is similar to playing back the transaction journal but with
1912** a few extra twists.
1913**
drh663fc632002-02-02 18:49:19 +00001914** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001915** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001916** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001917**
drhac69b052004-05-12 13:30:07 +00001918** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001919** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001920** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001921*/
drh3aac2dd2004-04-26 14:10:20 +00001922static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001923 i64 szJ; /* Size of the full journal */
1924 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001925 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001926 int i; /* Loop counter */
1927 int rc;
1928
danielk197776572402004-06-25 02:38:54 +00001929 szJ = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001930
danielk19774099f6e2007-03-19 11:25:20 +00001931 /* Set hdrOff to be the offset just after the end of the last journal
1932 ** page written before the first journal-header for this statement
1933 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001934 ** header was written.
1935 */
1936 hdrOff = pPager->stmtHdrOff;
1937 assert( pPager->fullSync || !hdrOff );
1938 if( !hdrOff ){
1939 hdrOff = szJ;
1940 }
1941
drhfa86c412002-02-02 15:01:15 +00001942 /* Truncate the database back to its original size.
1943 */
danielk1977e180dd92007-04-05 17:15:52 +00001944 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001945 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001946
drhac69b052004-05-12 13:30:07 +00001947 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001948 */
drhac69b052004-05-12 13:30:07 +00001949 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001950 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001951
drhac69b052004-05-12 13:30:07 +00001952 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001953 ** database file. Note that the statement journal omits checksums from
1954 ** each record since power-failure recovery is not important to statement
1955 ** journals.
drhfa86c412002-02-02 15:01:15 +00001956 */
danielk197762079062007-08-15 17:08:46 +00001957 for(i=0; i<nRec; i++){
1958 i64 offset = i*(4+pPager->pageSize);
1959 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001960 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001961 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001962 }
1963
danielk197776572402004-06-25 02:38:54 +00001964 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1965 ** was the size of the journal file when this statement was started, so
1966 ** everything after that needs to be rolled back, either into the
1967 ** database, the memory cache, or both.
1968 **
1969 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1970 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001971 */
danielk197776572402004-06-25 02:38:54 +00001972 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001973 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001974 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001975 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001976 assert( rc!=SQLITE_DONE );
1977 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1978 }
1979
1980 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001981 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001982 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001983 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001984 if( rc!=SQLITE_OK ){
1985 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001986 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001987 }
danielk1977f0113002006-01-24 12:09:17 +00001988 if( nJRec==0 ){
1989 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001990 }
danielk1977f0113002006-01-24 12:09:17 +00001991 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001992 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001993 assert( rc!=SQLITE_DONE );
1994 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1995 }
drhfa86c412002-02-02 15:01:15 +00001996 }
danielk197776572402004-06-25 02:38:54 +00001997
1998 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001999
drh3aac2dd2004-04-26 14:10:20 +00002000end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00002001 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00002002 pPager->journalOff = szJ;
2003 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00002004 }
2005 return rc;
2006}
2007
2008/*
drhf57b14a2001-09-14 18:54:08 +00002009** Change the maximum number of in-memory pages that are allowed.
2010*/
danielk19773b8a05f2007-03-19 17:44:26 +00002011void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00002012 if( mxPage>10 ){
2013 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00002014 }else{
2015 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00002016 }
2017}
2018
2019/*
drh973b6e32003-02-12 14:09:42 +00002020** Adjust the robustness of the database to damage due to OS crashes
2021** or power failures by changing the number of syncs()s when writing
2022** the rollback journal. There are three levels:
2023**
drh054889e2005-11-30 03:20:31 +00002024** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00002025** for temporary and transient files.
2026**
2027** NORMAL The journal is synced once before writes begin on the
2028** database. This is normally adequate protection, but
2029** it is theoretically possible, though very unlikely,
2030** that an inopertune power failure could leave the journal
2031** in a state which would cause damage to the database
2032** when it is rolled back.
2033**
2034** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00002035** database (with some additional information - the nRec field
2036** of the journal header - being written in between the two
2037** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00002038** single disk sector is atomic, then this mode provides
2039** assurance that the journal will not be corrupted to the
2040** point of causing damage to the database during rollback.
2041**
2042** Numeric values associated with these states are OFF==1, NORMAL=2,
2043** and FULL=3.
2044*/
danielk197793758c82005-01-21 08:13:14 +00002045#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00002046void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00002047 pPager->noSync = level==1 || pPager->tempFile;
2048 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00002049 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00002050 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00002051}
danielk197793758c82005-01-21 08:13:14 +00002052#endif
drh973b6e32003-02-12 14:09:42 +00002053
2054/*
drhaf6df112005-06-07 02:12:30 +00002055** The following global variable is incremented whenever the library
2056** attempts to open a temporary file. This information is used for
2057** testing and analysis only.
2058*/
drh0f7eb612006-08-08 13:51:43 +00002059#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002060int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00002061#endif
drhaf6df112005-06-07 02:12:30 +00002062
2063/*
drh3f56e6e2007-03-15 01:16:47 +00002064** Open a temporary file.
2065**
2066** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00002067** other error code if we fail. The OS will automatically delete the temporary
2068** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00002069*/
danielk1977b4b47412007-08-17 15:53:36 +00002070static int sqlite3PagerOpentemp(
drh33f4e022007-09-03 15:19:34 +00002071 sqlite3_vfs *pVfs, /* The virtual file system layer */
2072 sqlite3_file *pFile, /* Write the file descriptor here */
2073 char *zFilename, /* Name of the file. Might be NULL */
2074 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00002075){
drhfa86c412002-02-02 15:01:15 +00002076 int rc;
drh334b2992007-09-06 23:28:23 +00002077 assert( zFilename!=0 );
drh3f56e6e2007-03-15 01:16:47 +00002078
drh0f7eb612006-08-08 13:51:43 +00002079#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002080 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002081#endif
danielk1977b4b47412007-08-17 15:53:36 +00002082
drh33f4e022007-09-03 15:19:34 +00002083 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2084 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
2085 rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00002086 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00002087 return rc;
2088}
2089
2090/*
drhed7c8552001-04-11 14:29:21 +00002091** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002092** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002093** the first call to sqlite3PagerGet() and is only held open until the
2094** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002095**
drh6446c4d2001-12-15 14:22:18 +00002096** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002097** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002098** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002099**
2100** If zFilename is ":memory:" then all information is held in cache.
2101** It is never written to disk. This can be used to implement an
2102** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002103*/
danielk19773b8a05f2007-03-19 17:44:26 +00002104int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002105 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002106 Pager **ppPager, /* Return the Pager structure here */
2107 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002108 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002109 int flags, /* flags controlling this file */
2110 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002111){
danielk1977b4b47412007-08-17 15:53:36 +00002112 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002113 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002114 int rc = SQLITE_OK;
2115 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002116 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002117 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002118 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002119 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2120 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002121 int journalFileSize = sqlite3JournalSize(pVfs);
drh1cc8c442007-08-24 16:08:29 +00002122 int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
2123 char *zPathname;
2124 int nPathname;
drh99b90c32008-03-17 13:50:58 +00002125 char *zStmtJrnl;
2126 int nStmtJrnl;
danielk1977b4b47412007-08-17 15:53:36 +00002127
drh86f8c192007-08-22 00:39:19 +00002128 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002129 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002130
drh1cc8c442007-08-24 16:08:29 +00002131 /* Compute the full pathname */
danielk1977adfb9b02007-09-17 07:02:56 +00002132 nPathname = pVfs->mxPathname+1;
drh99b90c32008-03-17 13:50:58 +00002133 zPathname = sqlite3_malloc(nPathname*2);
drh1cc8c442007-08-24 16:08:29 +00002134 if( zPathname==0 ){
2135 return SQLITE_NOMEM;
2136 }
2137 if( zFilename && zFilename[0] ){
2138#ifndef SQLITE_OMIT_MEMORYDB
2139 if( strcmp(zFilename,":memory:")==0 ){
2140 memDb = 1;
2141 zPathname[0] = 0;
2142 }else
2143#endif
2144 {
danielk1977adfb9b02007-09-17 07:02:56 +00002145 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002146 }
2147 }else{
danielk1977adfb9b02007-09-17 07:02:56 +00002148 rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
drhae28c012007-08-24 16:29:23 +00002149 }
2150 if( rc!=SQLITE_OK ){
2151 sqlite3_free(zPathname);
2152 return rc;
drh1cc8c442007-08-24 16:08:29 +00002153 }
2154 nPathname = strlen(zPathname);
2155
drh99b90c32008-03-17 13:50:58 +00002156 /* Put the statement journal in temporary disk space since this is
2157 ** sometimes RAM disk or other optimized storage. Unlikely the main
2158 ** main journal file, the statement journal does not need to be
2159 ** colocated with the database nor does it need to be persistent.
2160 */
2161 zStmtJrnl = &zPathname[nPathname+1];
2162 rc = sqlite3OsGetTempname(pVfs, pVfs->mxPathname+1, zStmtJrnl);
2163 if( rc!=SQLITE_OK ){
2164 sqlite3_free(zPathname);
2165 return rc;
2166 }
2167 nStmtJrnl = strlen(zStmtJrnl);
2168
danielk1977b4b47412007-08-17 15:53:36 +00002169 /* Allocate memory for the pager structure */
2170 pPager = sqlite3MallocZero(
2171 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00002172 journalFileSize + /* The journal file structure */
drh17c0ba22008-02-26 18:40:11 +00002173 pVfs->szOsFile * 3 + /* The main db and two journal files */
drh99b90c32008-03-17 13:50:58 +00002174 3*nPathname + 40 + /* zFilename, zDirectory, zJournal */
2175 nStmtJrnl /* zStmtJrnl */
danielk1977b4b47412007-08-17 15:53:36 +00002176 );
2177 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002178 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002179 return SQLITE_NOMEM;
2180 }
2181 pPtr = (u8 *)&pPager[1];
drh33f4e022007-09-03 15:19:34 +00002182 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002183 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002184 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2185 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2186 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002187 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2188 pPager->zJournal = &pPager->zDirectory[nPathname+1];
drh334b2992007-09-06 23:28:23 +00002189 pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
danielk1977b4b47412007-08-17 15:53:36 +00002190 pPager->pVfs = pVfs;
drh1cc8c442007-08-24 16:08:29 +00002191 memcpy(pPager->zFilename, zPathname, nPathname+1);
drh99b90c32008-03-17 13:50:58 +00002192 memcpy(pPager->zStmtJrnl, zStmtJrnl, nStmtJrnl+1);
drh1cc8c442007-08-24 16:08:29 +00002193 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002194
drh153c62c2007-08-24 03:51:33 +00002195 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002196 */
drhae28c012007-08-24 16:29:23 +00002197 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002198 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2199 rc = SQLITE_CANTOPEN;
2200 }else{
drh1cc8c442007-08-24 16:08:29 +00002201 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002202 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2203 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002204 readOnly = (fout&SQLITE_OPEN_READONLY);
2205
2206 /* If the file was successfully opened for read/write access,
2207 ** choose a default page size in case we have to create the
2208 ** database file. The default page size is the maximum of:
2209 **
2210 ** + SQLITE_DEFAULT_PAGE_SIZE,
2211 ** + The value returned by sqlite3OsSectorSize()
2212 ** + The largest page size that can be written atomically.
2213 */
2214 if( rc==SQLITE_OK && !readOnly ){
2215 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
2216 if( nDefaultPage<iSectorSize ){
2217 nDefaultPage = iSectorSize;
2218 }
danielk19779663b8f2007-08-24 11:52:28 +00002219#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002220 {
2221 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2222 int ii;
2223 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2224 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2225 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2226 for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2227 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002228 }
danielk1977b4b47412007-08-17 15:53:36 +00002229 }
drh1cc8c442007-08-24 16:08:29 +00002230#endif
2231 if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2232 nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2233 }
danielk19778def5ea2004-06-16 10:39:52 +00002234 }
drhac69b052004-05-12 13:30:07 +00002235 }
drh1cc8c442007-08-24 16:08:29 +00002236 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002237 /* If a temporary file is requested, it is not opened immediately.
2238 ** In this case we accept the default page size and delay actually
2239 ** opening the file until the first call to OsWrite().
2240 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002241 tempFile = 1;
2242 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002243 }
danielk1977aef0bf62005-12-30 16:28:01 +00002244
danielk1977b4b47412007-08-17 15:53:36 +00002245 if( pPager && rc==SQLITE_OK ){
danielk19779663b8f2007-08-24 11:52:28 +00002246 pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
drh3e7a6092002-12-07 21:45:14 +00002247 }
danielk1977aef0bf62005-12-30 16:28:01 +00002248
drh153c62c2007-08-24 03:51:33 +00002249 /* If an error occured in either of the blocks above.
2250 ** Free the Pager structure and close the file.
2251 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002252 ** any Pager.errMask variables.
2253 */
danielk1977b4b47412007-08-17 15:53:36 +00002254 if( !pPager || !pPager->pTmpSpace ){
2255 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002256 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002257 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002258 }
danielk1977aef0bf62005-12-30 16:28:01 +00002259
drh153c62c2007-08-24 03:51:33 +00002260 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2261 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002262
danielk1977b4b47412007-08-17 15:53:36 +00002263 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002264 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002265 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002266 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002267
drh99b90c32008-03-17 13:50:58 +00002268 /* Fill in Pager.zJournal[] */
drh1cc8c442007-08-24 16:08:29 +00002269 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2270 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
danielk1977b4b47412007-08-17 15:53:36 +00002271
drh3b59a5c2006-01-15 20:28:28 +00002272 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002273 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002274 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002275 /* pPager->stmtOpen = 0; */
2276 /* pPager->stmtInUse = 0; */
2277 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002278 pPager->dbSize = memDb-1;
danielk19779663b8f2007-08-24 11:52:28 +00002279 pPager->pageSize = nDefaultPage;
drh3b59a5c2006-01-15 20:28:28 +00002280 /* pPager->stmtSize = 0; */
2281 /* pPager->stmtJSize = 0; */
2282 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002283 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002284 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002285 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002286 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002287 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002288 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002289 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2290 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2291 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2292 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002293 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002294 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002295 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002296 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002297 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002298 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002299 /* pPager->pFirst = 0; */
2300 /* pPager->pFirstSynced = 0; */
2301 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002302 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00002303 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002304 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002305 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002306 }
drh3b59a5c2006-01-15 20:28:28 +00002307 /* pPager->pBusyHandler = 0; */
2308 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002309 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002310#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002311 pPager->iInUseMM = 0;
2312 pPager->iInUseDB = 0;
2313 if( !memDb ){
2314 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2315 sqlite3_mutex_enter(mutex);
2316 pPager->pNext = sqlite3PagerList;
2317 if( sqlite3PagerList ){
2318 assert( sqlite3PagerList->pPrev==0 );
2319 sqlite3PagerList->pPrev = pPager;
2320 }
2321 pPager->pPrev = 0;
2322 sqlite3PagerList = pPager;
2323 sqlite3_mutex_leave(mutex);
2324 }
danielk197752622822006-01-09 09:59:49 +00002325#endif
drhed7c8552001-04-11 14:29:21 +00002326 return SQLITE_OK;
2327}
2328
2329/*
drh90f5ecb2004-07-22 01:19:35 +00002330** Set the busy handler function.
2331*/
danielk19773b8a05f2007-03-19 17:44:26 +00002332void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002333 pPager->pBusyHandler = pBusyHandler;
2334}
2335
2336/*
drh72f82862001-05-24 21:06:34 +00002337** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002338** when the reference count on each page reaches zero. The destructor can
2339** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002340**
danielk19773b8a05f2007-03-19 17:44:26 +00002341** The destructor is not called as a result sqlite3PagerClose().
2342** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002343*/
danielk19773b8a05f2007-03-19 17:44:26 +00002344void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002345 pPager->xDestructor = xDesc;
2346}
2347
2348/*
drha6abd042004-06-09 17:37:22 +00002349** Set the reinitializer for this pager. If not NULL, the reinitializer
2350** is called when the content of a page in cache is restored to its original
2351** value as a result of a rollback. The callback gives higher-level code
2352** an opportunity to restore the EXTRA section to agree with the restored
2353** page data.
2354*/
danielk19773b8a05f2007-03-19 17:44:26 +00002355void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002356 pPager->xReiniter = xReinit;
2357}
2358
2359/*
danielk1977a1644fd2007-08-29 12:31:25 +00002360** Set the page size to *pPageSize. If the suggest new page size is
2361** inappropriate, then an alternative page size is set to that
2362** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002363*/
danielk1977a1644fd2007-08-29 12:31:25 +00002364int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2365 int rc = SQLITE_OK;
2366 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002367 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002368 if( pageSize && pageSize!=pPager->pageSize
2369 && !pPager->memDb && pPager->nRef==0
2370 ){
2371 char *pNew = (char *)sqlite3_malloc(pageSize);
2372 if( !pNew ){
2373 rc = SQLITE_NOMEM;
2374 }else{
2375 pagerEnter(pPager);
2376 pager_reset(pPager);
2377 pPager->pageSize = pageSize;
2378 setSectorSize(pPager);
2379 sqlite3_free(pPager->pTmpSpace);
2380 pPager->pTmpSpace = pNew;
2381 pagerLeave(pPager);
2382 }
drh1c7880e2005-05-20 20:01:55 +00002383 }
danielk1977a1644fd2007-08-29 12:31:25 +00002384 *pPageSize = pPager->pageSize;
2385 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002386}
2387
2388/*
drh26b79942007-11-28 16:19:56 +00002389** Return a pointer to the "temporary page" buffer held internally
2390** by the pager. This is a buffer that is big enough to hold the
2391** entire content of a database page. This buffer is used internally
2392** during rollback and will be overwritten whenever a rollback
2393** occurs. But other modules are free to use it too, as long as
2394** no rollbacks are happening.
2395*/
2396void *sqlite3PagerTempSpace(Pager *pPager){
2397 return pPager->pTmpSpace;
2398}
2399
2400/*
drhf8e632b2007-05-08 14:51:36 +00002401** Attempt to set the maximum database page count if mxPage is positive.
2402** Make no changes if mxPage is zero or negative. And never reduce the
2403** maximum page count below the current size of the database.
2404**
2405** Regardless of mxPage, return the current maximum page count.
2406*/
2407int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2408 if( mxPage>0 ){
2409 pPager->mxPgno = mxPage;
2410 }
2411 sqlite3PagerPagecount(pPager);
2412 return pPager->mxPgno;
2413}
2414
2415/*
drhc9ac5ca2005-11-04 22:03:30 +00002416** The following set of routines are used to disable the simulated
2417** I/O error mechanism. These routines are used to avoid simulated
2418** errors in places where we do not care about errors.
2419**
2420** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2421** and generate no code.
2422*/
2423#ifdef SQLITE_TEST
2424extern int sqlite3_io_error_pending;
2425extern int sqlite3_io_error_hit;
2426static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002427void disable_simulated_io_errors(void){
2428 saved_cnt = sqlite3_io_error_pending;
2429 sqlite3_io_error_pending = -1;
2430}
2431void enable_simulated_io_errors(void){
2432 sqlite3_io_error_pending = saved_cnt;
2433}
2434#else
drh152410f2005-11-05 15:11:22 +00002435# define disable_simulated_io_errors()
2436# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002437#endif
2438
2439/*
drh90f5ecb2004-07-22 01:19:35 +00002440** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002441** that pDest points to.
2442**
2443** No error checking is done. The rational for this is that this function
2444** may be called even if the file does not exist or contain a header. In
2445** these cases sqlite3OsRead() will return an error, to which the correct
2446** response is to zero the memory at pDest and continue. A real IO error
2447** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002448*/
danielk19773b8a05f2007-03-19 17:44:26 +00002449int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002450 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002451 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002452 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2453 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002454 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002455 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002456 if( rc==SQLITE_IOERR_SHORT_READ ){
2457 rc = SQLITE_OK;
2458 }
drh90f5ecb2004-07-22 01:19:35 +00002459 }
drh551b7732006-11-06 21:20:25 +00002460 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002461}
2462
2463/*
drh5e00f6c2001-09-13 13:46:56 +00002464** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002465** pPager.
2466**
2467** If the PENDING_BYTE lies on the page directly after the end of the
2468** file, then consider this page part of the file too. For example, if
2469** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2470** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002471*/
danielk19773b8a05f2007-03-19 17:44:26 +00002472int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002473 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002474 int rc;
drhd9b02572001-04-15 00:37:09 +00002475 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002476 if( pPager->errCode ){
danielk197743468d92008-02-26 06:05:31 +00002477 return -1;
drha7aea3d2007-03-15 12:51:16 +00002478 }
drhed7c8552001-04-11 14:29:21 +00002479 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002480 n = pPager->dbSize;
2481 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002482 assert(pPager->fd->pMethods||pPager->tempFile);
2483 if( (pPager->fd->pMethods)
2484 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002485 pPager->nRef++;
drhe49f9822006-09-15 12:29:16 +00002486 pager_error(pPager, rc);
danielk1977ae72d982007-10-03 08:46:44 +00002487 pPager->nRef--;
danielk197743468d92008-02-26 06:05:31 +00002488 return -1;
danielk197715f411d2005-09-16 10:18:45 +00002489 }
2490 if( n>0 && n<pPager->pageSize ){
2491 n = 1;
2492 }else{
2493 n /= pPager->pageSize;
2494 }
2495 if( pPager->state!=PAGER_UNLOCK ){
2496 pPager->dbSize = n;
2497 }
drhed7c8552001-04-11 14:29:21 +00002498 }
danielk197715f411d2005-09-16 10:18:45 +00002499 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002500 n++;
2501 }
drhf8e632b2007-05-08 14:51:36 +00002502 if( n>pPager->mxPgno ){
2503 pPager->mxPgno = n;
2504 }
drhed7c8552001-04-11 14:29:21 +00002505 return n;
2506}
2507
drhf07e7d52006-04-07 13:54:46 +00002508
2509#ifndef SQLITE_OMIT_MEMORYDB
2510/*
2511** Clear a PgHistory block
2512*/
2513static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002514 sqlite3_free(pHist->pOrig);
2515 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002516 pHist->pOrig = 0;
2517 pHist->pStmt = 0;
2518}
2519#else
2520#define clearHistory(x)
2521#endif
2522
drhed7c8552001-04-11 14:29:21 +00002523/*
drhf7c57532003-04-25 13:22:51 +00002524** Forward declaration
2525*/
danielk197776572402004-06-25 02:38:54 +00002526static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002527
2528/*
drh85b623f2007-12-13 21:54:09 +00002529** Unlink pPg from its hash chain. Also set the page number to 0 to indicate
danielk1977f5fdda82004-11-03 08:44:05 +00002530** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002531** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002532** pNextFree/pPrevFree list that is not a part of any hash-chain.
2533*/
2534static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2535 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002536 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002537 return;
2538 }
2539 if( pPg->pNextHash ){
2540 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2541 }
2542 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002543 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002544 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2545 }else{
drh8ca0c722006-05-07 17:49:38 +00002546 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002547 pPager->aHash[h] = pPg->pNextHash;
2548 }
drh5229ae42006-03-23 23:29:04 +00002549 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002550 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2551 }
danielk1977f5fdda82004-11-03 08:44:05 +00002552 pPg->pgno = 0;
2553 pPg->pNextHash = pPg->pPrevHash = 0;
2554}
2555
2556/*
drhac69b052004-05-12 13:30:07 +00002557** Unlink a page from the free list (the list of all pages where nRef==0)
2558** and from its hash collision chain.
2559*/
2560static void unlinkPage(PgHdr *pPg){
2561 Pager *pPager = pPg->pPager;
2562
danielk19779f61c2f2007-08-27 17:27:49 +00002563 /* Unlink from free page list */
2564 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002565
2566 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002567 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002568}
2569
2570/*
danielk1977e180dd92007-04-05 17:15:52 +00002571** This routine is used to truncate the cache when a database
2572** is truncated. Drop from the cache all pages whose pgno is
2573** larger than pPager->dbSize and is unreferenced.
2574**
drhac69b052004-05-12 13:30:07 +00002575** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002576**
2577** Actually, at the point this routine is called, it would be
2578** an error to have a referenced page. But rather than delete
2579** that page and guarantee a subsequent segfault, it seems better
2580** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002581*/
danielk1977e180dd92007-04-05 17:15:52 +00002582static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002583 PgHdr *pPg;
2584 PgHdr **ppPg;
2585 int dbSize = pPager->dbSize;
2586
2587 ppPg = &pPager->pAll;
2588 while( (pPg = *ppPg)!=0 ){
2589 if( pPg->pgno<=dbSize ){
2590 ppPg = &pPg->pNextAll;
2591 }else if( pPg->nRef>0 ){
2592 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2593 ppPg = &pPg->pNextAll;
2594 }else{
2595 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002596 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2597 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002598 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002599 makeClean(pPg);
drh0d180202008-02-14 23:26:56 +00002600 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00002601 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002602 pPager->nPage--;
2603 }
2604 }
2605}
2606
drhf7c57532003-04-25 13:22:51 +00002607/*
danielk197717221812005-02-15 03:38:05 +00002608** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002609** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002610** false or until the lock succeeds.
2611**
2612** Return SQLITE_OK on success and an error code if we cannot obtain
2613** the lock.
2614*/
2615static int pager_wait_on_lock(Pager *pPager, int locktype){
2616 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002617
2618 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002619 assert( PAGER_SHARED==SHARED_LOCK );
2620 assert( PAGER_RESERVED==RESERVED_LOCK );
2621 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002622
2623 /* If the file is currently unlocked then the size must be unknown */
2624 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2625
danielk197717221812005-02-15 03:38:05 +00002626 if( pPager->state>=locktype ){
2627 rc = SQLITE_OK;
2628 }else{
drhbefdf832008-03-14 19:33:05 +00002629 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002630 do {
drh054889e2005-11-30 03:20:31 +00002631 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002632 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002633 if( rc==SQLITE_OK ){
2634 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002635 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002636 }
2637 }
2638 return rc;
2639}
2640
2641/*
drhf7c57532003-04-25 13:22:51 +00002642** Truncate the file to the number of pages specified.
2643*/
danielk19773b8a05f2007-03-19 17:44:26 +00002644int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002645 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002646 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002647 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002648 if( pPager->errCode ){
2649 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002650 return rc;
2651 }
drh7d02cb72003-06-04 16:24:39 +00002652 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002653 return SQLITE_OK;
2654 }
drhb7f91642004-10-31 02:22:47 +00002655 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002656 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002657 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002658 return SQLITE_OK;
2659 }
drh86f8c192007-08-22 00:39:19 +00002660 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002661 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002662 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002663 if( rc!=SQLITE_OK ){
2664 return rc;
2665 }
danielk197717221812005-02-15 03:38:05 +00002666
2667 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002668 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002669 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002670 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002671 if( rc!=SQLITE_OK ){
2672 return rc;
2673 }
2674
drhcb4c40b2004-08-18 19:09:43 +00002675 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002676 return rc;
2677}
2678
2679/*
drhed7c8552001-04-11 14:29:21 +00002680** Shutdown the page cache. Free all memory and close all files.
2681**
2682** If a transaction was in progress when this routine is called, that
2683** transaction is rolled back. All outstanding pages are invalidated
2684** and their memory is freed. Any attempt to use a page associated
2685** with this page cache after this function returns will likely
2686** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002687**
2688** This function always succeeds. If a transaction is active an attempt
2689** is made to roll it back. If an error occurs during the rollback
2690** a hot journal may be left in the filesystem but no error is returned
2691** to the caller.
drhed7c8552001-04-11 14:29:21 +00002692*/
danielk19773b8a05f2007-03-19 17:44:26 +00002693int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002694#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002695 if( !MEMDB ){
2696 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2697 sqlite3_mutex_enter(mutex);
2698 if( pPager->pPrev ){
2699 pPager->pPrev->pNext = pPager->pNext;
2700 }else{
2701 sqlite3PagerList = pPager->pNext;
2702 }
2703 if( pPager->pNext ){
2704 pPager->pNext->pPrev = pPager->pPrev;
2705 }
2706 sqlite3_mutex_leave(mutex);
2707 }
danielk197713f72992005-12-18 08:51:22 +00002708#endif
2709
drhbafda092007-01-03 23:36:22 +00002710 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002711 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002712 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002713 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002714 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002715 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002716 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002717 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002718 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002719 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002720 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002721 }
drhf5e7bb52008-02-18 14:47:33 +00002722 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002723 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002724 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002725 }
danielk1977b4b47412007-08-17 15:53:36 +00002726 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002727 /* Temp files are automatically deleted by the OS
2728 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002729 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002730 ** }
2731 */
danielk1977aca790a2005-01-13 11:07:52 +00002732
drh17435752007-08-16 04:30:38 +00002733 sqlite3_free(pPager->aHash);
2734 sqlite3_free(pPager->pTmpSpace);
2735 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002736 return SQLITE_OK;
2737}
2738
drh87cc3b32007-05-08 21:45:27 +00002739#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002740/*
drh5e00f6c2001-09-13 13:46:56 +00002741** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002742*/
danielk19773b8a05f2007-03-19 17:44:26 +00002743Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002744 return p->pgno;
2745}
drh87cc3b32007-05-08 21:45:27 +00002746#endif
drhed7c8552001-04-11 14:29:21 +00002747
2748/*
drhc8629a12004-05-08 20:07:40 +00002749** The page_ref() function increments the reference count for a page.
2750** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002751** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002752**
2753** For non-test systems, page_ref() is a macro that calls _page_ref()
2754** online of the reference count is zero. For test systems, page_ref()
2755** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002756*/
drh836faa42003-01-11 13:30:57 +00002757static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002758 if( pPg->nRef==0 ){
2759 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002760 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002761 pPg->pPager->nRef++;
2762 }
2763 pPg->nRef++;
drhdf0b3b02001-06-23 11:36:20 +00002764}
danielk1977aca790a2005-01-13 11:07:52 +00002765#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002766 static void page_ref(PgHdr *pPg){
2767 if( pPg->nRef==0 ){
2768 _page_ref(pPg);
2769 }else{
2770 pPg->nRef++;
drhc8629a12004-05-08 20:07:40 +00002771 }
2772 }
2773#else
2774# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2775#endif
drhdf0b3b02001-06-23 11:36:20 +00002776
2777/*
2778** Increment the reference count for a page. The input pointer is
2779** a reference to the page data.
2780*/
danielk19773b8a05f2007-03-19 17:44:26 +00002781int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002782 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002783 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002784 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002785 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002786}
2787
2788/*
drh34e79ce2004-02-08 06:05:46 +00002789** Sync the journal. In other words, make sure all the pages that have
2790** been written to the journal have actually reached the surface of the
2791** disk. It is not safe to modify the original database file until after
2792** the journal has been synced. If the original database is modified before
2793** the journal is synced and a power failure occurs, the unsynced journal
2794** data would be lost and we would be unable to completely rollback the
2795** database changes. Database corruption would occur.
2796**
2797** This routine also updates the nRec field in the header of the journal.
2798** (See comments on the pager_playback() routine for additional information.)
2799** If the sync mode is FULL, two syncs will occur. First the whole journal
2800** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002801**
drh34e79ce2004-02-08 06:05:46 +00002802** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002803** after a power failure, so no sync occurs.
2804**
2805** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2806** the database is stored, then OsSync() is never called on the journal
2807** file. In this case all that is required is to update the nRec field in
2808** the journal header.
drhfa86c412002-02-02 15:01:15 +00002809**
drh34e79ce2004-02-08 06:05:46 +00002810** This routine clears the needSync field of every page current held in
2811** memory.
drh50e5dad2001-09-15 00:57:28 +00002812*/
danielk197776572402004-06-25 02:38:54 +00002813static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002814 PgHdr *pPg;
2815 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002816
danielk19774cd2cd52007-08-23 14:48:23 +00002817
drh03eb96a2002-11-10 23:32:56 +00002818 /* Sync the journal before modifying the main database
2819 ** (assuming there is a journal and it needs to be synced.)
2820 */
danielk197776572402004-06-25 02:38:54 +00002821 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002822 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002823 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002824 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002825
danielk19774cd2cd52007-08-23 14:48:23 +00002826 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002827 /* Write the nRec value into the journal file header. If in
2828 ** full-synchronous mode, sync the journal first. This ensures that
2829 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002830 ** it as a candidate for rollback.
2831 **
2832 ** This is not required if the persistent media supports the
2833 ** SAFE_APPEND property. Because in this case it is not possible
2834 ** for garbage data to be appended to the file, the nRec field
2835 ** is populated with 0xFFFFFFFF when the journal header is written
2836 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002837 */
danielk19774cd2cd52007-08-23 14:48:23 +00002838 i64 jrnlOff;
2839 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002840 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002841 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002842 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002843 if( rc!=0 ) return rc;
2844 }
danielk197713adf8a2004-06-03 16:08:41 +00002845
danielk197762079062007-08-15 17:08:46 +00002846 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2847 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2848 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002849 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002850 }
danielk19774cd2cd52007-08-23 14:48:23 +00002851 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2852 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2853 IOTRACE(("JSYNC %p\n", pPager))
2854 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2855 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2856 );
2857 if( rc!=0 ) return rc;
2858 }
drhdb48ee02003-01-16 13:42:43 +00002859 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002860 }
drh50e5dad2001-09-15 00:57:28 +00002861 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002862
2863 /* Erase the needSync flag from every page.
2864 */
2865 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2866 pPg->needSync = 0;
2867 }
danielk19779f61c2f2007-08-27 17:27:49 +00002868 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002869 }
drh03eb96a2002-11-10 23:32:56 +00002870
drh341eae82003-01-21 02:39:36 +00002871#ifndef NDEBUG
2872 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2873 ** flag must also be clear for all pages. Verify that this
2874 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002875 */
drh341eae82003-01-21 02:39:36 +00002876 else{
2877 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2878 assert( pPg->needSync==0 );
2879 }
danielk19779f61c2f2007-08-27 17:27:49 +00002880 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002881 }
drh341eae82003-01-21 02:39:36 +00002882#endif
drhdb48ee02003-01-16 13:42:43 +00002883
drh81a20f22001-10-12 17:30:04 +00002884 return rc;
drh50e5dad2001-09-15 00:57:28 +00002885}
2886
2887/*
drhdc3e10b2006-06-15 14:31:06 +00002888** Merge two lists of pages connected by pDirty and in pgno order.
2889** Do not both fixing the pPrevDirty pointers.
2890*/
2891static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2892 PgHdr result, *pTail;
2893 pTail = &result;
2894 while( pA && pB ){
2895 if( pA->pgno<pB->pgno ){
2896 pTail->pDirty = pA;
2897 pTail = pA;
2898 pA = pA->pDirty;
2899 }else{
2900 pTail->pDirty = pB;
2901 pTail = pB;
2902 pB = pB->pDirty;
2903 }
2904 }
2905 if( pA ){
2906 pTail->pDirty = pA;
2907 }else if( pB ){
2908 pTail->pDirty = pB;
2909 }else{
2910 pTail->pDirty = 0;
2911 }
2912 return result.pDirty;
2913}
2914
2915/*
2916** Sort the list of pages in accending order by pgno. Pages are
2917** connected by pDirty pointers. The pPrevDirty pointers are
2918** corrupted by this sort.
2919*/
danielk197724168722007-04-02 05:07:47 +00002920#define N_SORT_BUCKET_ALLOC 25
2921#define N_SORT_BUCKET 25
2922#ifdef SQLITE_TEST
2923 int sqlite3_pager_n_sort_bucket = 0;
2924 #undef N_SORT_BUCKET
2925 #define N_SORT_BUCKET \
2926 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2927#endif
drhdc3e10b2006-06-15 14:31:06 +00002928static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002929 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002930 int i;
2931 memset(a, 0, sizeof(a));
2932 while( pIn ){
2933 p = pIn;
2934 pIn = p->pDirty;
2935 p->pDirty = 0;
2936 for(i=0; i<N_SORT_BUCKET-1; i++){
2937 if( a[i]==0 ){
2938 a[i] = p;
2939 break;
2940 }else{
2941 p = merge_pagelist(a[i], p);
2942 a[i] = 0;
2943 }
2944 }
2945 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002946 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2947 ** elements in the input list. This is possible, but impractical.
2948 ** Testing this line is the point of global variable
2949 ** sqlite3_pager_n_sort_bucket.
2950 */
drhdc3e10b2006-06-15 14:31:06 +00002951 a[i] = merge_pagelist(a[i], p);
2952 }
2953 }
2954 p = a[0];
2955 for(i=1; i<N_SORT_BUCKET; i++){
2956 p = merge_pagelist(p, a[i]);
2957 }
2958 return p;
2959}
2960
2961/*
drh2554f8b2003-01-22 01:26:44 +00002962** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2963** every one of those pages out to the database file and mark them all
2964** as clean.
2965*/
2966static int pager_write_pagelist(PgHdr *pList){
2967 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00002968 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00002969 int rc;
2970
2971 if( pList==0 ) return SQLITE_OK;
2972 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002973
2974 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2975 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002976 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002977 **
drha6abd042004-06-09 17:37:22 +00002978 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2979 ** through an intermediate state PENDING. A PENDING lock prevents new
2980 ** readers from attaching to the database but is unsufficient for us to
2981 ** write. The idea of a PENDING lock is to prevent new readers from
2982 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002983 **
drha6abd042004-06-09 17:37:22 +00002984 ** While the pager is in the RESERVED state, the original database file
2985 ** is unchanged and we can rollback without having to playback the
2986 ** journal into the original database file. Once we transition to
2987 ** EXCLUSIVE, it means the database file has been changed and any rollback
2988 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002989 */
drh684917c2004-10-05 02:41:42 +00002990 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002991 if( rc!=SQLITE_OK ){
2992 return rc;
2993 }
2994
drhdc3e10b2006-06-15 14:31:06 +00002995 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00002996 for(p=pList; p; p=p->pDirty){
2997 assert( p->dirty );
2998 p->dirty = 0;
2999 }
drh2554f8b2003-01-22 01:26:44 +00003000 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00003001
3002 /* If the file has not yet been opened, open it now. */
3003 if( !pPager->fd->pMethods ){
3004 assert(pPager->tempFile);
drh33f4e022007-09-03 15:19:34 +00003005 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
3006 pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00003007 if( rc ) return rc;
3008 }
3009
danielk1977687566d2004-11-02 12:56:41 +00003010 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00003011 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00003012 ** make the file smaller (presumably by auto-vacuum code). Do not write
3013 ** any such pages to the file.
3014 */
3015 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00003016 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00003017 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00003018 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
3019 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00003020 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00003021 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003022 PAGER_INCR(sqlite3_pager_writedb_count);
3023 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00003024 if( pList->pgno==1 ){
3025 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
3026 }
danielk1977687566d2004-11-02 12:56:41 +00003027 }
3028#ifndef NDEBUG
3029 else{
drh4f0c5872007-03-26 22:05:01 +00003030 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00003031 }
3032#endif
drh2554f8b2003-01-22 01:26:44 +00003033 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00003034#ifdef SQLITE_CHECK_PAGES
3035 pList->pageHash = pager_pagehash(pList);
3036#endif
drh2554f8b2003-01-22 01:26:44 +00003037 pList = pList->pDirty;
3038 }
3039 return SQLITE_OK;
3040}
3041
3042/*
3043** Collect every dirty page into a dirty list and
3044** return a pointer to the head of that list. All pages are
3045** collected even if they are still in use.
3046*/
3047static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh8f2e9a12008-02-26 14:46:04 +00003048
3049#ifndef NDEBUG
3050 /* Verify the sanity of the dirty list when we are running
3051 ** in debugging mode. This is expensive, so do not
3052 ** do this on a normal build. */
3053 int n1 = 0;
3054 int n2 = 0;
3055 PgHdr *p;
3056 for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; }
3057 for(p=pPager->pDirty; p; p=p->pDirty){ n2++; }
3058 assert( n1==n2 );
3059#endif
3060
drh605ad8f2006-05-03 23:34:05 +00003061 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00003062}
3063
3064/*
drh19db9352008-03-27 22:42:51 +00003065** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00003066** A hot journal is one that needs to be played back.
3067**
3068** If the current size of the database file is 0 but a journal file
3069** exists, that is probably an old journal left over from a prior
3070** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00003071**
3072** Return negative if unable to determine the status of the journal.
drh165ffe92005-03-15 17:09:30 +00003073*/
3074static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003075 sqlite3_vfs *pVfs = pPager->pVfs;
drh19db9352008-03-27 22:42:51 +00003076 int rc;
drh165ffe92005-03-15 17:09:30 +00003077 if( !pPager->useJournal ) return 0;
drhd3cf2472007-11-27 16:55:07 +00003078 if( !pPager->fd->pMethods ) return 0;
drh19db9352008-03-27 22:42:51 +00003079 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS);
3080 if( rc<=0 ){
3081 return rc;
drhbb5f18d2007-04-06 18:23:17 +00003082 }
3083 if( sqlite3OsCheckReservedLock(pPager->fd) ){
3084 return 0;
3085 }
danielk19773b8a05f2007-03-19 17:44:26 +00003086 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003087 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00003088 return 0;
3089 }else{
3090 return 1;
3091 }
3092}
3093
danielk19775591df52005-12-20 09:19:37 +00003094/*
danielk1977aef0bf62005-12-30 16:28:01 +00003095** Try to find a page in the cache that can be recycled.
3096**
3097** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00003098** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00003099*/
danielk1977843e65f2007-09-01 16:16:15 +00003100static int pager_recycle(Pager *pPager, PgHdr **ppPg){
danielk197713f72992005-12-18 08:51:22 +00003101 PgHdr *pPg;
3102 *ppPg = 0;
3103
danielk1977843e65f2007-09-01 16:16:15 +00003104 /* It is illegal to call this function unless the pager object
3105 ** pointed to by pPager has at least one free page (page with nRef==0).
3106 */
danielk1977c551edc2007-04-05 13:12:13 +00003107 assert(!MEMDB);
danielk1977843e65f2007-09-01 16:16:15 +00003108 assert(pPager->lru.pFirst);
danielk1977c551edc2007-04-05 13:12:13 +00003109
danielk197713f72992005-12-18 08:51:22 +00003110 /* Find a page to recycle. Try to locate a page that does not
3111 ** require us to do an fsync() on the journal.
3112 */
danielk19779f61c2f2007-08-27 17:27:49 +00003113 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00003114
3115 /* If we could not find a page that does not require an fsync()
3116 ** on the journal file then fsync the journal file. This is a
3117 ** very slow operation, so we work hard to avoid it. But sometimes
3118 ** it can't be helped.
3119 */
danielk1977843e65f2007-09-01 16:16:15 +00003120 if( pPg==0 && pPager->lru.pFirst){
danielk19774cd2cd52007-08-23 14:48:23 +00003121 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk197713f72992005-12-18 08:51:22 +00003122 int rc = syncJournal(pPager);
3123 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003124 return rc;
danielk197713f72992005-12-18 08:51:22 +00003125 }
danielk19774cd2cd52007-08-23 14:48:23 +00003126 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197713f72992005-12-18 08:51:22 +00003127 /* If in full-sync mode, write a new journal header into the
3128 ** journal file. This is done to avoid ever modifying a journal
3129 ** header that is involved in the rollback of pages that have
3130 ** already been written to the database (in case the header is
3131 ** trashed when the nRec field is updated).
3132 */
3133 pPager->nRec = 0;
3134 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00003135 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00003136 rc = writeJournalHdr(pPager);
3137 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003138 return rc;
danielk197713f72992005-12-18 08:51:22 +00003139 }
3140 }
danielk19779f61c2f2007-08-27 17:27:49 +00003141 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003142 }
danielk197713f72992005-12-18 08:51:22 +00003143
3144 assert( pPg->nRef==0 );
3145
3146 /* Write the page to the database file if it is dirty.
3147 */
3148 if( pPg->dirty ){
3149 int rc;
3150 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003151 makeClean(pPg);
3152 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003153 pPg->pDirty = 0;
3154 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003155 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003156 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003157 return rc;
danielk197713f72992005-12-18 08:51:22 +00003158 }
3159 }
3160 assert( pPg->dirty==0 );
3161
3162 /* If the page we are recycling is marked as alwaysRollback, then
3163 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003164 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003165 ** It is necessary to do this because the page marked alwaysRollback
3166 ** might be reloaded at a later time but at that point we won't remember
3167 ** that is was marked alwaysRollback. This means that all pages must
3168 ** be marked as alwaysRollback from here on out.
3169 */
3170 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003171 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003172 pPager->alwaysRollback = 1;
3173 }
3174
3175 /* Unlink the old page from the free list and the hash table
3176 */
3177 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003178 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003179
3180 *ppPg = pPg;
3181 return SQLITE_OK;
3182}
3183
drh86f8c192007-08-22 00:39:19 +00003184#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003185/*
3186** This function is called to free superfluous dynamically allocated memory
3187** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003188** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003189**
3190** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003191** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003192** of bytes of memory released.
3193*/
danielk19773b8a05f2007-03-19 17:44:26 +00003194int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003195 int nReleased = 0; /* Bytes of memory released so far */
3196 sqlite3_mutex *mutex; /* The MEM2 mutex */
3197 Pager *pPager; /* For looping over pagers */
drhe5fe6902007-12-07 18:55:28 +00003198 BusyHandler *savedBusy; /* Saved copy of the busy handler */
danielk19779f61c2f2007-08-27 17:27:49 +00003199 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003200
drh86f8c192007-08-22 00:39:19 +00003201 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003202 */
drh86f8c192007-08-22 00:39:19 +00003203 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
3204 sqlite3_mutex_enter(mutex);
3205
3206 /* Signal all database connections that memory management wants
3207 ** to have access to the pagers.
3208 */
3209 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3210 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003211 }
3212
danielk19779f61c2f2007-08-27 17:27:49 +00003213 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3214 PgHdr *pPg;
3215 PgHdr *pRecycled;
3216
3217 /* Try to find a page to recycle that does not require a sync(). If
3218 ** this is not possible, find one that does require a sync().
3219 */
3220 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3221 pPg = sqlite3LruPageList.pFirstSynced;
3222 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3223 pPg = pPg->gfree.pNext;
3224 }
3225 if( !pPg ){
3226 pPg = sqlite3LruPageList.pFirst;
3227 while( pPg && pPg->pPager->iInUseDB ){
3228 pPg = pPg->gfree.pNext;
3229 }
3230 }
3231 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003232
danielk197784f786f2007-08-28 08:00:17 +00003233 /* If pPg==0, then the block above has failed to find a page to
3234 ** recycle. In this case return early - no further memory will
3235 ** be released.
3236 */
danielk19779f61c2f2007-08-27 17:27:49 +00003237 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003238
danielk19779f61c2f2007-08-27 17:27:49 +00003239 pPager = pPg->pPager;
3240 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3241 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3242
drhe5fe6902007-12-07 18:55:28 +00003243 savedBusy = pPager->pBusyHandler;
3244 pPager->pBusyHandler = 0;
danielk1977843e65f2007-09-01 16:16:15 +00003245 rc = pager_recycle(pPager, &pRecycled);
drhe5fe6902007-12-07 18:55:28 +00003246 pPager->pBusyHandler = savedBusy;
danielk19779f61c2f2007-08-27 17:27:49 +00003247 assert(pRecycled==pPg || rc!=SQLITE_OK);
3248 if( rc==SQLITE_OK ){
3249 /* We've found a page to free. At this point the page has been
3250 ** removed from the page hash-table, free-list and synced-list
3251 ** (pFirstSynced). It is still in the all pages (pAll) list.
3252 ** Remove it from this list before freeing.
3253 **
3254 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3255 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003256 */
danielk19779f61c2f2007-08-27 17:27:49 +00003257 PgHdr *pTmp;
3258 assert( pPg );
3259 if( pPg==pPager->pAll ){
3260 pPager->pAll = pPg->pNextAll;
3261 }else{
3262 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3263 pTmp->pNextAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00003264 }
danielk19779f61c2f2007-08-27 17:27:49 +00003265 nReleased += (
3266 sizeof(*pPg) + pPager->pageSize
3267 + sizeof(u32) + pPager->nExtra
3268 + MEMDB*sizeof(PgHistory)
3269 );
3270 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3271 PAGER_INCR(sqlite3_pager_pgfree_count);
drh0d180202008-02-14 23:26:56 +00003272 sqlite3_free(pPg->pData);
danielk19779f61c2f2007-08-27 17:27:49 +00003273 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003274 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003275 }else{
3276 /* An error occured whilst writing to the database file or
3277 ** journal in pager_recycle(). The error is not returned to the
3278 ** caller of this function. Instead, set the Pager.errCode variable.
3279 ** The error will be returned to the user (or users, in the case
3280 ** of a shared pager cache) of the pager for which the error occured.
3281 */
3282 assert(
3283 (rc&0xff)==SQLITE_IOERR ||
3284 rc==SQLITE_FULL ||
3285 rc==SQLITE_BUSY
3286 );
3287 assert( pPager->state>=PAGER_RESERVED );
3288 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003289 }
3290 }
danielk1977aef0bf62005-12-30 16:28:01 +00003291
drh86f8c192007-08-22 00:39:19 +00003292 /* Clear the memory management flags and release the mutex
3293 */
3294 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3295 pPager->iInUseMM = 0;
3296 }
3297 sqlite3_mutex_leave(mutex);
3298
3299 /* Return the number of bytes released
3300 */
danielk197713f72992005-12-18 08:51:22 +00003301 return nReleased;
3302}
drh86f8c192007-08-22 00:39:19 +00003303#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003304
drh165ffe92005-03-15 17:09:30 +00003305/*
danielk1977e180dd92007-04-05 17:15:52 +00003306** Read the content of page pPg out of the database file.
3307*/
3308static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3309 int rc;
danielk197762079062007-08-15 17:08:46 +00003310 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003311 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003312 assert(pPager->fd->pMethods||pPager->tempFile);
3313 if( !pPager->fd->pMethods ){
3314 return SQLITE_IOERR_SHORT_READ;
3315 }
danielk197762079062007-08-15 17:08:46 +00003316 offset = (pgno-1)*(i64)pPager->pageSize;
3317 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003318 PAGER_INCR(sqlite3_pager_readdb_count);
3319 PAGER_INCR(pPager->nRead);
3320 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003321 if( pgno==1 ){
3322 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3323 sizeof(pPager->dbFileVers));
3324 }
danielk1977e180dd92007-04-05 17:15:52 +00003325 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003326 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3327 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003328 return rc;
3329}
3330
3331
3332/*
danielk1977e277be02007-03-23 18:12:06 +00003333** This function is called to obtain the shared lock required before
3334** data may be read from the pager cache. If the shared lock has already
3335** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003336**
3337** Immediately after obtaining the shared lock (if required), this function
3338** checks for a hot-journal file. If one is found, an emergency rollback
3339** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003340*/
3341static int pagerSharedLock(Pager *pPager){
3342 int rc = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00003343 int isHot = 0;
danielk1977e277be02007-03-23 18:12:06 +00003344
danielk1977ae72d982007-10-03 08:46:44 +00003345 /* If this database is opened for exclusive access, has no outstanding
3346 ** page references and is in an error-state, now is the chance to clear
3347 ** the error. Discard the contents of the pager-cache and treat any
3348 ** open journal file as a hot-journal.
3349 */
3350 if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
3351 if( pPager->journalOpen ){
3352 isHot = 1;
3353 }
3354 pager_reset(pPager);
3355 pPager->errCode = SQLITE_OK;
3356 }
3357
3358 /* If the pager is still in an error state, do not proceed. The error
3359 ** state will be cleared at some point in the future when all page
3360 ** references are dropped and the cache can be discarded.
3361 */
3362 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3363 return pPager->errCode;
3364 }
3365
3366 if( pPager->state==PAGER_UNLOCK || isHot ){
danielk1977b4b47412007-08-17 15:53:36 +00003367 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003368 if( !MEMDB ){
3369 assert( pPager->nRef==0 );
3370 if( !pPager->noReadlock ){
3371 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3372 if( rc!=SQLITE_OK ){
3373 return pager_error(pPager, rc);
3374 }
3375 assert( pPager->state>=SHARED_LOCK );
3376 }
3377
3378 /* If a journal file exists, and there is no RESERVED lock on the
3379 ** database file, then it either needs to be played back or deleted.
3380 */
drh19db9352008-03-27 22:42:51 +00003381 rc = hasHotJournal(pPager);
3382 if( rc<0 ){
danielk1977a3e52182008-04-14 16:37:10 +00003383 return SQLITE_IOERR_NOMEM;
drh19db9352008-03-27 22:42:51 +00003384 }
3385 if( rc==1 || isHot ){
danielk1977e277be02007-03-23 18:12:06 +00003386 /* Get an EXCLUSIVE lock on the database file. At this point it is
3387 ** important that a RESERVED lock is not obtained on the way to the
3388 ** EXCLUSIVE lock. If it were, another process might open the
3389 ** database file, detect the RESERVED lock, and conclude that the
3390 ** database is safe to read while this process is still rolling it
3391 ** back.
3392 **
3393 ** Because the intermediate RESERVED lock is not requested, the
3394 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00003395 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00003396 */
danielk1977ae72d982007-10-03 08:46:44 +00003397 if( pPager->state<EXCLUSIVE_LOCK ){
3398 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3399 if( rc!=SQLITE_OK ){
3400 pager_unlock(pPager);
3401 return pager_error(pPager, rc);
3402 }
3403 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00003404 }
danielk1977e277be02007-03-23 18:12:06 +00003405
3406 /* Open the journal for reading only. Return SQLITE_BUSY if
3407 ** we are unable to open the journal file.
3408 **
3409 ** The journal file does not need to be locked itself. The
3410 ** journal file is never open unless the main database file holds
3411 ** a write lock, so there is never any chance of two or more
3412 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00003413 **
drhfd131da2007-08-07 17:13:03 +00003414 ** Open the journal for read/write access. This is because in
3415 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003416 ** possibly used for a transaction later on. On some systems, the
3417 ** OsTruncate() call used in exclusive-access mode also requires
3418 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003419 */
danielk1977ae72d982007-10-03 08:46:44 +00003420 if( !isHot ){
danielk1977ce98bba2008-04-03 10:13:01 +00003421 int res = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS);
3422 if( res==1 ){
danielk1977ae72d982007-10-03 08:46:44 +00003423 int fout = 0;
3424 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3425 assert( !pPager->tempFile );
3426 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3427 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3428 if( fout&SQLITE_OPEN_READONLY ){
3429 rc = SQLITE_BUSY;
3430 sqlite3OsClose(pPager->jfd);
3431 }
danielk1977ce98bba2008-04-03 10:13:01 +00003432 }else{
3433 rc = (res==0?SQLITE_BUSY:SQLITE_IOERR_NOMEM);
danielk1977979f38e2007-03-27 16:19:51 +00003434 }
3435 }
danielk1977e277be02007-03-23 18:12:06 +00003436 if( rc!=SQLITE_OK ){
3437 pager_unlock(pPager);
drh1aa5af12008-03-07 19:51:14 +00003438 switch( rc ){
3439 case SQLITE_NOMEM:
3440 case SQLITE_IOERR_UNLOCK:
3441 case SQLITE_IOERR_NOMEM:
3442 return rc;
3443 default:
3444 return SQLITE_BUSY;
3445 }
danielk1977e277be02007-03-23 18:12:06 +00003446 }
3447 pPager->journalOpen = 1;
3448 pPager->journalStarted = 0;
3449 pPager->journalOff = 0;
3450 pPager->setMaster = 0;
3451 pPager->journalHdr = 0;
3452
3453 /* Playback and delete the journal. Drop the database write
3454 ** lock and reacquire the read lock.
3455 */
3456 rc = pager_playback(pPager, 1);
3457 if( rc!=SQLITE_OK ){
3458 return pager_error(pPager, rc);
3459 }
danielk1977c5859712007-03-26 12:26:27 +00003460 assert(pPager->state==PAGER_SHARED ||
3461 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3462 );
danielk1977e277be02007-03-23 18:12:06 +00003463 }
3464
3465 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003466 /* The shared-lock has just been acquired on the database file
3467 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003468 ** read or write transaction). Check to see if the database
3469 ** has been modified. If the database has changed, flush the
3470 ** cache.
3471 **
3472 ** Database changes is detected by looking at 15 bytes beginning
3473 ** at offset 24 into the file. The first 4 of these 16 bytes are
3474 ** a 32-bit counter that is incremented with each change. The
3475 ** other bytes change randomly with each file change when
3476 ** a codec is in use.
3477 **
3478 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003479 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003480 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003481 */
drh86a88112007-04-16 15:02:19 +00003482 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003483 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003484
danielk1977e180dd92007-04-05 17:15:52 +00003485 if( pPager->errCode ){
3486 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00003487 }
3488
danielk1977e180dd92007-04-05 17:15:52 +00003489 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003490 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003491 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003492 if( rc!=SQLITE_OK ){
3493 return rc;
3494 }
drh86a88112007-04-16 15:02:19 +00003495 }else{
3496 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003497 }
3498
drh86a88112007-04-16 15:02:19 +00003499 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003500 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003501 }
3502 }
3503 }
danielk1977c5859712007-03-26 12:26:27 +00003504 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3505 if( pPager->state==PAGER_UNLOCK ){
3506 pPager->state = PAGER_SHARED;
3507 }
danielk1977e277be02007-03-23 18:12:06 +00003508 }
3509
3510 return rc;
3511}
3512
3513/*
danielk1977e180dd92007-04-05 17:15:52 +00003514** Allocate a PgHdr object. Either create a new one or reuse
3515** an existing one that is not otherwise in use.
3516**
3517** A new PgHdr structure is created if any of the following are
3518** true:
3519**
3520** (1) We have not exceeded our maximum allocated cache size
3521** as set by the "PRAGMA cache_size" command.
3522**
3523** (2) There are no unused PgHdr objects available at this time.
3524**
3525** (3) This is an in-memory database.
3526**
3527** (4) There are no PgHdr objects that do not require a journal
3528** file sync and a sync of the journal file is currently
3529** prohibited.
3530**
3531** Otherwise, reuse an existing PgHdr. In other words, reuse an
3532** existing PgHdr if all of the following are true:
3533**
3534** (1) We have reached or exceeded the maximum cache size
3535** allowed by "PRAGMA cache_size".
3536**
3537** (2) There is a PgHdr available with PgHdr->nRef==0
3538**
3539** (3) We are not in an in-memory database
3540**
3541** (4) Either there is an available PgHdr that does not need
3542** to be synced to disk or else disk syncing is currently
3543** allowed.
danielk197724168722007-04-02 05:07:47 +00003544*/
3545static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3546 int rc = SQLITE_OK;
3547 PgHdr *pPg;
drh1e3af332007-10-20 13:17:54 +00003548 int nByteHdr;
danielk197724168722007-04-02 05:07:47 +00003549
danielk1977e180dd92007-04-05 17:15:52 +00003550 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003551 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003552 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003553 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003554 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003555 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003556 ){
drh0d180202008-02-14 23:26:56 +00003557 void *pData;
danielk197724168722007-04-02 05:07:47 +00003558 if( pPager->nPage>=pPager->nHash ){
3559 pager_resize_hash_table(pPager,
3560 pPager->nHash<256 ? 256 : pPager->nHash*2);
3561 if( pPager->nHash==0 ){
3562 rc = SQLITE_NOMEM;
3563 goto pager_allocate_out;
3564 }
3565 }
drhed138fb2007-08-22 22:04:37 +00003566 pagerLeave(pPager);
drh1e3af332007-10-20 13:17:54 +00003567 nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra
3568 + MEMDB*sizeof(PgHistory);
drh0d180202008-02-14 23:26:56 +00003569 pPg = sqlite3_malloc( nByteHdr );
3570 if( pPg ){
3571 pData = sqlite3_malloc( pPager->pageSize );
3572 if( pData==0 ){
3573 sqlite3_free(pPg);
3574 pPg = 0;
3575 }
3576 }
drhed138fb2007-08-22 22:04:37 +00003577 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003578 if( pPg==0 ){
3579 rc = SQLITE_NOMEM;
3580 goto pager_allocate_out;
3581 }
drh1e3af332007-10-20 13:17:54 +00003582 memset(pPg, 0, nByteHdr);
drh0d180202008-02-14 23:26:56 +00003583 pPg->pData = pData;
danielk197724168722007-04-02 05:07:47 +00003584 pPg->pPager = pPager;
3585 pPg->pNextAll = pPager->pAll;
3586 pPager->pAll = pPg;
3587 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003588 }else{
3589 /* Recycle an existing page with a zero ref-count. */
danielk1977843e65f2007-09-01 16:16:15 +00003590 rc = pager_recycle(pPager, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003591 if( rc==SQLITE_BUSY ){
3592 rc = SQLITE_IOERR_BLOCKED;
3593 }
danielk197724168722007-04-02 05:07:47 +00003594 if( rc!=SQLITE_OK ){
3595 goto pager_allocate_out;
3596 }
3597 assert( pPager->state>=SHARED_LOCK );
3598 assert(pPg);
3599 }
3600 *ppPg = pPg;
3601
3602pager_allocate_out:
3603 return rc;
3604}
3605
3606/*
drhd33d5a82007-04-26 12:11:28 +00003607** Make sure we have the content for a page. If the page was
3608** previously acquired with noContent==1, then the content was
3609** just initialized to zeros instead of being read from disk.
3610** But now we need the real data off of disk. So make sure we
3611** have it. Read it in if we do not have it already.
3612*/
3613static int pager_get_content(PgHdr *pPg){
3614 if( pPg->needRead ){
3615 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3616 if( rc==SQLITE_OK ){
3617 pPg->needRead = 0;
3618 }else{
3619 return rc;
3620 }
3621 }
3622 return SQLITE_OK;
3623}
3624
3625/*
drhd9b02572001-04-15 00:37:09 +00003626** Acquire a page.
3627**
drh58a11682001-11-10 13:51:08 +00003628** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003629** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003630**
drhd33d5a82007-04-26 12:11:28 +00003631** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003632** file is smaller than the requested page, then no actual disk
3633** read occurs and the memory image of the page is initialized to
3634** all zeros. The extra data appended to a page is always initialized
3635** to zeros the first time a page is loaded into memory.
3636**
drhd9b02572001-04-15 00:37:09 +00003637** The acquisition might fail for several reasons. In all cases,
3638** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003639**
drhd33d5a82007-04-26 12:11:28 +00003640** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003641** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003642** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003643** just returns 0. This routine acquires a read-lock the first time it
3644** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003645** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003646** or journal files.
drh0787db62007-03-04 13:15:27 +00003647**
drh538f5702007-04-13 02:14:30 +00003648** If noContent is false, the page contents are actually read from disk.
3649** If noContent is true, it means that we do not care about the contents
3650** of the page at this time, so do not do a disk read. Just fill in the
3651** page content with zeros. But mark the fact that we have not read the
3652** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003653** sqlite3PagerWrite() is called on this page or if this routine is
3654** called again with noContent==0, that means that the content is needed
3655** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003656*/
drh86f8c192007-08-22 00:39:19 +00003657static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003658 Pager *pPager, /* The pager open on the database file */
3659 Pgno pgno, /* Page number to fetch */
3660 DbPage **ppPage, /* Write a pointer to the page here */
3661 int noContent /* Do not bother reading content from disk if true */
3662){
drhed7c8552001-04-11 14:29:21 +00003663 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003664 int rc;
drhed7c8552001-04-11 14:29:21 +00003665
danielk1977e277be02007-03-23 18:12:06 +00003666 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3667
danielk197726836652005-01-17 01:33:13 +00003668 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3669 ** number greater than this, or zero, is requested.
3670 */
drh267cb322005-09-16 17:16:52 +00003671 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003672 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003673 }
3674
drhd9b02572001-04-15 00:37:09 +00003675 /* Make sure we have not hit any critical errors.
3676 */
drh836faa42003-01-11 13:30:57 +00003677 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003678 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003679
danielk197713adf8a2004-06-03 16:08:41 +00003680 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003681 ** on the database file. pagerSharedLock() is a no-op if
3682 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003683 */
danielk1977e277be02007-03-23 18:12:06 +00003684 rc = pagerSharedLock(pPager);
3685 if( rc!=SQLITE_OK ){
3686 return rc;
drhed7c8552001-04-11 14:29:21 +00003687 }
danielk1977e277be02007-03-23 18:12:06 +00003688 assert( pPager->state!=PAGER_UNLOCK );
3689
3690 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003691 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003692 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003693 int nMax;
drhed7c8552001-04-11 14:29:21 +00003694 int h;
drh538f5702007-04-13 02:14:30 +00003695 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003696 rc = pagerAllocatePage(pPager, &pPg);
3697 if( rc!=SQLITE_OK ){
3698 return rc;
drhed7c8552001-04-11 14:29:21 +00003699 }
danielk197724168722007-04-02 05:07:47 +00003700
drhed7c8552001-04-11 14:29:21 +00003701 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003702 assert( !MEMDB || pgno>pPager->stmtSize );
drhf5e7bb52008-02-18 14:47:33 +00003703 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
3704 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003705
drh605ad8f2006-05-03 23:34:05 +00003706 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003707 pPg->nRef = 1;
danielk197775bab7d2006-01-23 13:09:45 +00003708
drhd9b02572001-04-15 00:37:09 +00003709 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003710 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003711 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003712 }
danielk1977979f38e2007-03-27 16:19:51 +00003713 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003714 if( pPager->errCode ){
danielk1977efaaf572006-01-16 11:29:19 +00003715 rc = pPager->errCode;
danielk1977ae72d982007-10-03 08:46:44 +00003716 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003717 return rc;
3718 }
danielk197775bab7d2006-01-23 13:09:45 +00003719
3720 /* Populate the page with data, either by reading from the database
3721 ** file, or by setting the entire page to zero.
3722 */
drhd215acf2007-04-13 04:01:58 +00003723 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003724 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003725 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003726 return SQLITE_FULL;
3727 }
drh90f5ecb2004-07-22 01:19:35 +00003728 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003729 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003730 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003731 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003732 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003733 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3734 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003735 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003736 return rc;
drh81a20f22001-10-12 17:30:04 +00003737 }
danielk1977b4626a32007-04-28 15:47:43 +00003738 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003739 }
danielk197775bab7d2006-01-23 13:09:45 +00003740
3741 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003742 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003743 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003744 pPg->pNextHash = pPager->aHash[h];
3745 pPager->aHash[h] = pPg;
3746 if( pPg->pNextHash ){
3747 assert( pPg->pNextHash->pPrevHash==0 );
3748 pPg->pNextHash->pPrevHash = pPg;
3749 }
3750
danielk19773c407372005-02-15 02:54:14 +00003751#ifdef SQLITE_CHECK_PAGES
3752 pPg->pageHash = pager_pagehash(pPg);
3753#endif
drhed7c8552001-04-11 14:29:21 +00003754 }else{
drhd9b02572001-04-15 00:37:09 +00003755 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003756 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003757 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003758 if( !noContent ){
3759 rc = pager_get_content(pPg);
3760 if( rc ){
3761 return rc;
3762 }
3763 }
drhdf0b3b02001-06-23 11:36:20 +00003764 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003765 }
danielk19773b8a05f2007-03-19 17:44:26 +00003766 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003767 return SQLITE_OK;
3768}
drh86f8c192007-08-22 00:39:19 +00003769int sqlite3PagerAcquire(
3770 Pager *pPager, /* The pager open on the database file */
3771 Pgno pgno, /* Page number to fetch */
3772 DbPage **ppPage, /* Write a pointer to the page here */
3773 int noContent /* Do not bother reading content from disk if true */
3774){
3775 int rc;
3776 pagerEnter(pPager);
3777 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3778 pagerLeave(pPager);
3779 return rc;
3780}
3781
drhed7c8552001-04-11 14:29:21 +00003782
3783/*
drh7e3b0a02001-04-28 16:52:40 +00003784** Acquire a page if it is already in the in-memory cache. Do
3785** not read the page from disk. Return a pointer to the page,
3786** or 0 if the page is not in cache.
3787**
danielk19773b8a05f2007-03-19 17:44:26 +00003788** See also sqlite3PagerGet(). The difference between this routine
3789** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003790** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003791** returns NULL if the page is not in cache or if a disk I/O error
3792** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003793*/
danielk19773b8a05f2007-03-19 17:44:26 +00003794DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003795 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003796
drh836faa42003-01-11 13:30:57 +00003797 assert( pPager!=0 );
3798 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003799
drh86f8c192007-08-22 00:39:19 +00003800 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003801 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003802 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003803 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3804 /* Do nothing */
3805 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3806 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003807 }
drh86f8c192007-08-22 00:39:19 +00003808 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003809 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003810}
3811
3812/*
drhed7c8552001-04-11 14:29:21 +00003813** Release a page.
3814**
3815** If the number of references to the page drop to zero, then the
3816** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003817** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003818** removed.
3819*/
danielk19773b8a05f2007-03-19 17:44:26 +00003820int sqlite3PagerUnref(DbPage *pPg){
danielk1977ae72d982007-10-03 08:46:44 +00003821 Pager *pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003822
3823 /* Decrement the reference count for this page
3824 */
drhed7c8552001-04-11 14:29:21 +00003825 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003826 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003827 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +00003828
danielk19773c407372005-02-15 02:54:14 +00003829 CHECK_PAGE(pPg);
3830
drh72f82862001-05-24 21:06:34 +00003831 /* When the number of references to a page reach 0, call the
3832 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003833 */
drhed7c8552001-04-11 14:29:21 +00003834 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003835
3836 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003837 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003838 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003839 }
drhd9b02572001-04-15 00:37:09 +00003840
3841 /* When all pages reach the freelist, drop the read lock from
3842 ** the database file.
3843 */
3844 pPager->nRef--;
3845 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003846 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003847 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003848 }
drhed7c8552001-04-11 14:29:21 +00003849 }
danielk1977ae72d982007-10-03 08:46:44 +00003850 pagerLeave(pPager);
drhd9b02572001-04-15 00:37:09 +00003851 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003852}
3853
3854/*
drha6abd042004-06-09 17:37:22 +00003855** Create a journal file for pPager. There should already be a RESERVED
3856** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003857**
3858** Return SQLITE_OK if everything. Return an error code and release the
3859** write lock if anything goes wrong.
3860*/
3861static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003862 sqlite3_vfs *pVfs = pPager->pVfs;
3863 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3864
drhda47d772002-12-02 04:25:19 +00003865 int rc;
drhb7f91642004-10-31 02:22:47 +00003866 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003867 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003868 assert( pPager->journalOpen==0 );
3869 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003870 assert( pPager->pInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003871 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003872 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003873 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00003874 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003875 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003876 rc = SQLITE_NOMEM;
3877 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003878 }
danielk1977b4b47412007-08-17 15:53:36 +00003879
3880 if( pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00003881 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3882 }else{
3883 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
danielk1977b4b47412007-08-17 15:53:36 +00003884 }
danielk1977c7b60172007-08-22 11:22:03 +00003885#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3886 rc = sqlite3JournalOpen(
3887 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3888 );
3889#else
danielk1977b4b47412007-08-17 15:53:36 +00003890 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
danielk1977c7b60172007-08-22 11:22:03 +00003891#endif
danielk1977b4b47412007-08-17 15:53:36 +00003892 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk197776572402004-06-25 02:38:54 +00003893 pPager->journalOff = 0;
3894 pPager->setMaster = 0;
3895 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003896 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003897 if( rc==SQLITE_NOMEM ){
danielk1977fee2d252007-08-18 10:59:19 +00003898 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh600e46a2007-03-28 01:59:33 +00003899 }
drh9c105bb2004-10-02 20:38:28 +00003900 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003901 }
3902 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003903 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003904 pPager->needSync = 0;
3905 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003906 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003907 if( pPager->errCode ){
3908 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003909 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003910 }
drhda47d772002-12-02 04:25:19 +00003911 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003912
danielk197776572402004-06-25 02:38:54 +00003913 rc = writeJournalHdr(pPager);
3914
drhac69b052004-05-12 13:30:07 +00003915 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003916 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003917 }
danielk1977ae72d982007-10-03 08:46:44 +00003918 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003919 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003920 if( rc==SQLITE_OK ){
3921 rc = SQLITE_FULL;
3922 }
3923 }
drh9c105bb2004-10-02 20:38:28 +00003924 return rc;
3925
3926failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003927 sqlite3BitvecDestroy(pPager->pInJournal);
3928 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003929 return rc;
drhda47d772002-12-02 04:25:19 +00003930}
3931
3932/*
drh4b845d72002-03-05 12:41:19 +00003933** Acquire a write-lock on the database. The lock is removed when
3934** the any of the following happen:
3935**
drh80e35f42007-03-30 14:06:34 +00003936** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003937** * sqlite3PagerRollback() is called.
3938** * sqlite3PagerClose() is called.
3939** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003940**
danielk197713adf8a2004-06-03 16:08:41 +00003941** The first parameter to this routine is a pointer to any open page of the
3942** database file. Nothing changes about the page - it is used merely to
3943** acquire a pointer to the Pager structure and as proof that there is
3944** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003945**
danielk197713adf8a2004-06-03 16:08:41 +00003946** The second parameter indicates how much space in bytes to reserve for a
3947** master journal file-name at the start of the journal when it is created.
3948**
3949** A journal file is opened if this is not a temporary file. For temporary
3950** files, the opening of the journal file is deferred until there is an
3951** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003952**
drha6abd042004-06-09 17:37:22 +00003953** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003954**
3955** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3956** immediately instead of waiting until we try to flush the cache. The
3957** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003958*/
danielk19773b8a05f2007-03-19 17:44:26 +00003959int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003960 Pager *pPager = pPg->pPager;
3961 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003962 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003963 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003964 assert( pPager->state!=PAGER_UNLOCK );
3965 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003966 assert( pPager->pInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003967 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003968 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003969 pPager->origDbSize = pPager->dbSize;
3970 }else{
drh054889e2005-11-30 03:20:31 +00003971 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003972 if( rc==SQLITE_OK ){
3973 pPager->state = PAGER_RESERVED;
3974 if( exFlag ){
3975 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3976 }
3977 }
danielk19779eed5052004-06-04 10:38:30 +00003978 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003979 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003980 return rc;
drhac69b052004-05-12 13:30:07 +00003981 }
drha6abd042004-06-09 17:37:22 +00003982 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003983 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003984 if( pPager->useJournal && !pPager->tempFile ){
3985 rc = pager_open_journal(pPager);
3986 }
drh4b845d72002-03-05 12:41:19 +00003987 }
danielk1977334cdb62007-03-26 08:05:12 +00003988 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3989 /* This happens when the pager was in exclusive-access mode last
3990 ** time a (read or write) transaction was successfully concluded
3991 ** by this connection. Instead of deleting the journal file it was
3992 ** kept open and truncated to 0 bytes.
3993 */
3994 assert( pPager->nRec==0 );
3995 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003996 assert( pPager->pInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003997 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003998 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003999 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhed138fb2007-08-22 22:04:37 +00004000 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004001 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00004002 rc = SQLITE_NOMEM;
4003 }else{
drh369339d2007-03-30 16:01:55 +00004004 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00004005 rc = writeJournalHdr(pPager);
4006 }
drh4b845d72002-03-05 12:41:19 +00004007 }
danielk1977334cdb62007-03-26 08:05:12 +00004008 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00004009 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00004010 return rc;
4011}
4012
4013/*
drh605ad8f2006-05-03 23:34:05 +00004014** Make a page dirty. Set its dirty flag and add it to the dirty
4015** page list.
4016*/
4017static void makeDirty(PgHdr *pPg){
4018 if( pPg->dirty==0 ){
4019 Pager *pPager = pPg->pPager;
4020 pPg->dirty = 1;
4021 pPg->pDirty = pPager->pDirty;
4022 if( pPager->pDirty ){
4023 pPager->pDirty->pPrevDirty = pPg;
4024 }
4025 pPg->pPrevDirty = 0;
4026 pPager->pDirty = pPg;
4027 }
4028}
4029
4030/*
4031** Make a page clean. Clear its dirty bit and remove it from the
4032** dirty page list.
4033*/
4034static void makeClean(PgHdr *pPg){
4035 if( pPg->dirty ){
4036 pPg->dirty = 0;
4037 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00004038 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004039 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
4040 }
4041 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00004042 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004043 pPg->pPrevDirty->pDirty = pPg->pDirty;
4044 }else{
drh153c62c2007-08-24 03:51:33 +00004045 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004046 pPg->pPager->pDirty = pPg->pDirty;
4047 }
4048 }
4049}
4050
4051
4052/*
drhed7c8552001-04-11 14:29:21 +00004053** Mark a data page as writeable. The page is written into the journal
4054** if it is not there already. This routine must be called before making
4055** changes to a page.
4056**
4057** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00004058** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00004059** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00004060** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00004061** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00004062**
4063** If the journal file could not be written because the disk is full,
4064** then this routine returns SQLITE_FULL and does an immediate rollback.
4065** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00004066** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00004067** reset.
drhed7c8552001-04-11 14:29:21 +00004068*/
danielk19773b8a05f2007-03-19 17:44:26 +00004069static int pager_write(PgHdr *pPg){
4070 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00004071 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00004072 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00004073
drh6446c4d2001-12-15 14:22:18 +00004074 /* Check for errors
4075 */
danielk1977efaaf572006-01-16 11:29:19 +00004076 if( pPager->errCode ){
4077 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004078 }
drh5e00f6c2001-09-13 13:46:56 +00004079 if( pPager->readOnly ){
4080 return SQLITE_PERM;
4081 }
drh6446c4d2001-12-15 14:22:18 +00004082
danielk197776572402004-06-25 02:38:54 +00004083 assert( !pPager->setMaster );
4084
danielk19773c407372005-02-15 02:54:14 +00004085 CHECK_PAGE(pPg);
4086
drh538f5702007-04-13 02:14:30 +00004087 /* If this page was previously acquired with noContent==1, that means
4088 ** we didn't really read in the content of the page. This can happen
4089 ** (for example) when the page is being moved to the freelist. But
4090 ** now we are (perhaps) moving the page off of the freelist for
4091 ** reuse and we need to know its original content so that content
4092 ** can be stored in the rollback journal. So do the read at this
4093 ** time.
4094 */
drhd33d5a82007-04-26 12:11:28 +00004095 rc = pager_get_content(pPg);
4096 if( rc ){
4097 return rc;
drh538f5702007-04-13 02:14:30 +00004098 }
4099
drh6446c4d2001-12-15 14:22:18 +00004100 /* Mark the page as dirty. If the page has already been written
4101 ** to the journal then we can return right away.
4102 */
drh605ad8f2006-05-03 23:34:05 +00004103 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00004104 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00004105 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004106 }else{
drh6446c4d2001-12-15 14:22:18 +00004107
danielk1977a0bf2652004-11-04 14:30:04 +00004108 /* If we get this far, it means that the page needs to be
4109 ** written to the transaction journal or the ckeckpoint journal
4110 ** or both.
4111 **
4112 ** First check to see that the transaction journal exists and
4113 ** create it if it does not.
4114 */
4115 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00004116 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00004117 if( rc!=SQLITE_OK ){
4118 return rc;
4119 }
4120 assert( pPager->state>=PAGER_RESERVED );
4121 if( !pPager->journalOpen && pPager->useJournal ){
4122 rc = pager_open_journal(pPager);
4123 if( rc!=SQLITE_OK ) return rc;
4124 }
4125 assert( pPager->journalOpen || !pPager->useJournal );
4126 pPager->dirtyCache = 1;
4127
4128 /* The transaction journal now exists and we have a RESERVED or an
4129 ** EXCLUSIVE lock on the main database file. Write the current page to
4130 ** the transaction journal if it is not there already.
4131 */
4132 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
4133 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00004134 if( MEMDB ){
4135 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00004136 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004137 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00004138 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977662278e2007-11-05 15:30:12 +00004139 if( !pHist->pOrig ){
4140 return SQLITE_NOMEM;
danielk1977a0bf2652004-11-04 14:30:04 +00004141 }
danielk1977662278e2007-11-05 15:30:12 +00004142 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
danielk1977a0bf2652004-11-04 14:30:04 +00004143 }else{
drhbf4bca52007-09-06 22:19:14 +00004144 u32 cksum;
4145 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00004146
drh267cb322005-09-16 17:16:52 +00004147 /* We should never write to the journal file the page that
4148 ** contains the database locks. The following assert verifies
4149 ** that we do not. */
4150 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004151 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004152 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00004153 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4154 if( rc==SQLITE_OK ){
4155 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4156 pPager->journalOff + 4);
4157 pPager->journalOff += pPager->pageSize+4;
4158 }
4159 if( rc==SQLITE_OK ){
4160 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4161 pPager->journalOff += 4;
4162 }
4163 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00004164 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00004165 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00004166 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4167 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00004168
drhfd131da2007-08-07 17:13:03 +00004169 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004170 ** transaction will be rolled back by the layer above.
4171 */
danielk1977a0bf2652004-11-04 14:30:04 +00004172 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004173 return rc;
4174 }
danielk197707cb5602006-01-20 10:55:05 +00004175
danielk1977a0bf2652004-11-04 14:30:04 +00004176 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00004177 assert( pPager->pInJournal!=0 );
4178 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004179 pPg->needSync = !pPager->noSync;
4180 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00004181 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004182 }
drhac69b052004-05-12 13:30:07 +00004183 }
danielk197713adf8a2004-06-03 16:08:41 +00004184 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004185 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004186 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004187 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004188 }
4189 if( pPg->needSync ){
4190 pPager->needSync = 1;
4191 }
4192 pPg->inJournal = 1;
4193 }
4194
4195 /* If the statement journal is open and the page is not in it,
4196 ** then write the current page to the statement journal. Note that
4197 ** the statement journal format differs from the standard journal format
4198 ** in that it omits the checksums and the header.
4199 */
danielk1977f35843b2007-04-07 15:03:17 +00004200 if( pPager->stmtInUse
4201 && !pageInStatement(pPg)
4202 && (int)pPg->pgno<=pPager->stmtSize
4203 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004204 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4205 if( MEMDB ){
4206 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4207 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00004208 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004209 if( pHist->pStmt ){
4210 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4211 }
drh4f0c5872007-03-26 22:05:01 +00004212 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004213 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004214 }else{
danielk197762079062007-08-15 17:08:46 +00004215 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00004216 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4217 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4218 if( rc==SQLITE_OK ){
4219 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4220 }
drh4f0c5872007-03-26 22:05:01 +00004221 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004222 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004223 return rc;
4224 }
danielk1977a0bf2652004-11-04 14:30:04 +00004225 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00004226 assert( pPager->pInStmt!=0 );
4227 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00004228 }
drhd9b02572001-04-15 00:37:09 +00004229 }
drhfa86c412002-02-02 15:01:15 +00004230 }
4231
4232 /* Update the database size and return.
4233 */
drh1aa2d8b2007-01-03 15:34:29 +00004234 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004235 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004236 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004237 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004238 pPager->dbSize++;
4239 }
drh306dc212001-05-21 13:45:10 +00004240 }
drh69688d52001-04-14 16:38:23 +00004241 return rc;
drhed7c8552001-04-11 14:29:21 +00004242}
4243
4244/*
danielk19774099f6e2007-03-19 11:25:20 +00004245** This function is used to mark a data-page as writable. It uses
4246** pager_write() to open a journal file (if it is not already open)
4247** and write the page *pData to the journal.
4248**
4249** The difference between this function and pager_write() is that this
4250** function also deals with the special case where 2 or more pages
4251** fit on a single disk sector. In this case all co-resident pages
4252** must have been written to the journal file before returning.
4253*/
danielk19773b8a05f2007-03-19 17:44:26 +00004254int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004255 int rc = SQLITE_OK;
4256
danielk19773b8a05f2007-03-19 17:44:26 +00004257 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004258 Pager *pPager = pPg->pPager;
4259 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4260
drh86f8c192007-08-22 00:39:19 +00004261 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004262 if( !MEMDB && nPagePerSector>1 ){
4263 Pgno nPageCount; /* Total number of pages in database file */
4264 Pgno pg1; /* First page of the sector pPg is located on. */
4265 int nPage; /* Number of pages starting at pg1 to journal */
4266 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004267 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004268
4269 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4270 ** header to be written between the pages journaled by this function.
4271 */
4272 assert( pPager->doNotSync==0 );
4273 pPager->doNotSync = 1;
4274
4275 /* This trick assumes that both the page-size and sector-size are
4276 ** an integer power of 2. It sets variable pg1 to the identifier
4277 ** of the first page of the sector pPg is located on.
4278 */
4279 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4280
danielk19773b8a05f2007-03-19 17:44:26 +00004281 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004282 if( pPg->pgno>nPageCount ){
4283 nPage = (pPg->pgno - pg1)+1;
4284 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4285 nPage = nPageCount+1-pg1;
4286 }else{
4287 nPage = nPagePerSector;
4288 }
4289 assert(nPage>0);
4290 assert(pg1<=pPg->pgno);
4291 assert((pg1+nPage)>pPg->pgno);
4292
4293 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4294 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004295 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00004296 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00004297 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004298 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004299 if( rc==SQLITE_OK ){
4300 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004301 if( pPage->needSync ){
4302 needSync = 1;
4303 }
danielk19773b8a05f2007-03-19 17:44:26 +00004304 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004305 }
4306 }
drhc81945e2008-03-10 14:12:53 +00004307 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk1977dd97a492007-08-22 18:54:32 +00004308 if( pPage->needSync ){
4309 needSync = 1;
4310 }
danielk19774099f6e2007-03-19 11:25:20 +00004311 }
4312 }
4313
danielk1977dd97a492007-08-22 18:54:32 +00004314 /* If the PgHdr.needSync flag is set for any of the nPage pages
4315 ** starting at pg1, then it needs to be set for all of them. Because
4316 ** writing to any of these nPage pages may damage the others, the
4317 ** journal file must contain sync()ed copies of all of them
4318 ** before any of them can be written out to the database file.
4319 */
4320 if( needSync ){
4321 for(ii=0; ii<nPage && needSync; ii++){
4322 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4323 if( pPage ) pPage->needSync = 1;
4324 }
4325 assert(pPager->needSync);
4326 }
4327
danielk19774099f6e2007-03-19 11:25:20 +00004328 assert( pPager->doNotSync==1 );
4329 pPager->doNotSync = 0;
4330 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004331 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004332 }
drh86f8c192007-08-22 00:39:19 +00004333 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004334 return rc;
4335}
4336
4337/*
drhaacc5432002-01-06 17:07:40 +00004338** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004339** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004340** to change the content of the page.
4341*/
danielk19777d3a6662006-01-23 16:21:05 +00004342#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004343int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004344 return pPg->dirty;
4345}
danielk19777d3a6662006-01-23 16:21:05 +00004346#endif
drh6019e162001-07-02 17:51:45 +00004347
danielk1977b84f96f2005-01-20 11:32:23 +00004348#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00004349/*
drh001bbcb2003-03-19 03:14:00 +00004350** Replace the content of a single page with the information in the third
4351** argument.
4352*/
danielk19773b8a05f2007-03-19 17:44:26 +00004353int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
4354 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00004355 int rc;
4356
drh86f8c192007-08-22 00:39:19 +00004357 pagerEnter(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00004358 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00004359 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004360 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00004361 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004362 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00004363 }
danielk19773b8a05f2007-03-19 17:44:26 +00004364 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00004365 }
drh86f8c192007-08-22 00:39:19 +00004366 pagerLeave(pPager);
drh001bbcb2003-03-19 03:14:00 +00004367 return rc;
4368}
danielk1977b84f96f2005-01-20 11:32:23 +00004369#endif
drh001bbcb2003-03-19 03:14:00 +00004370
4371/*
drh30e58752002-03-02 20:41:57 +00004372** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004373** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004374** that page might be marked as dirty.
4375**
4376** The overlying software layer calls this routine when all of the data
4377** on the given page is unused. The pager marks the page as clean so
4378** that it does not get written to disk.
4379**
4380** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004381** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004382** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004383**
4384** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004385** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004386** will thereafter be ignored. This is necessary to avoid a problem
4387** where a page with data is added to the freelist during one part of
4388** a transaction then removed from the freelist during a later part
4389** of the same transaction and reused for some other purpose. When it
4390** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004391** the sqlite3PagerDontRollback() routine is called. But because the
4392** page contains critical data, we still need to be sure it gets
4393** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004394*/
drh538f5702007-04-13 02:14:30 +00004395void sqlite3PagerDontWrite(DbPage *pDbPage){
4396 PgHdr *pPg = pDbPage;
4397 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004398
drhb7f91642004-10-31 02:22:47 +00004399 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004400 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004401 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004402 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004403 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004404 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4405 /* If this pages is the last page in the file and the file has grown
4406 ** during the current transaction, then do NOT mark the page as clean.
4407 ** When the database file grows, we must make sure that the last page
4408 ** gets written at least once so that the disk file will be the correct
4409 ** size. If you do not write this page and the size of the file
4410 ** on the disk ends up being too small, that can lead to database
4411 ** corruption during the next transaction.
4412 */
4413 }else{
drh538f5702007-04-13 02:14:30 +00004414 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4415 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004416 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004417#ifdef SQLITE_CHECK_PAGES
4418 pPg->pageHash = pager_pagehash(pPg);
4419#endif
drh8124a302002-06-25 14:43:57 +00004420 }
drh30e58752002-03-02 20:41:57 +00004421 }
drh86f8c192007-08-22 00:39:19 +00004422 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004423}
4424
4425/*
4426** A call to this routine tells the pager that if a rollback occurs,
4427** it is not necessary to restore the data on the given page. This
4428** means that the pager does not have to record the given page in the
4429** rollback journal.
drh538f5702007-04-13 02:14:30 +00004430**
4431** If we have not yet actually read the content of this page (if
4432** the PgHdr.needRead flag is set) then this routine acts as a promise
4433** that we will never need to read the page content in the future.
4434** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00004435*/
danielk19773b8a05f2007-03-19 17:44:26 +00004436void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004437 Pager *pPager = pPg->pPager;
4438
drh86f8c192007-08-22 00:39:19 +00004439 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004440 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00004441
4442 /* If the journal file is not open, or DontWrite() has been called on
4443 ** this page (DontWrite() sets the alwaysRollback flag), then this
4444 ** function is a no-op.
4445 */
4446 if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){
danielk197787c29a92008-01-18 11:33:16 +00004447 pagerLeave(pPager);
4448 return;
4449 }
danielk1977a55e9352008-01-21 13:04:34 +00004450 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
4451
drhc5d0bd92008-04-14 01:00:57 +00004452#ifdef SQLITE_SECURE_DELETE
4453 if( pPg->inJournal || (int)pPg->pgno > pPager->origDbSize ){
4454 return;
4455 }
4456#endif
4457
4458 /* If SECURE_DELETE is disabled, then there is no way that this
4459 ** routine can be called on a page for which sqlite3PagerDontWrite()
4460 ** has not been previously called during the same transaction.
4461 ** And if DontWrite() has previously been called, the following
4462 ** conditions must be met.
danielk1977a55e9352008-01-21 13:04:34 +00004463 */
4464 assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize );
4465
drhf5e7bb52008-02-18 14:47:33 +00004466 assert( pPager->pInJournal!=0 );
4467 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a55e9352008-01-21 13:04:34 +00004468 pPg->inJournal = 1;
4469 pPg->needRead = 0;
4470 if( pPager->stmtInUse ){
drhc5d0bd92008-04-14 01:00:57 +00004471 assert( pPager->stmtSize >= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00004472 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00004473 }
danielk1977a55e9352008-01-21 13:04:34 +00004474 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
4475 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh86f8c192007-08-22 00:39:19 +00004476 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004477}
4478
drhac69b052004-05-12 13:30:07 +00004479
drh30e58752002-03-02 20:41:57 +00004480/*
drh80e35f42007-03-30 14:06:34 +00004481** This routine is called to increment the database file change-counter,
4482** stored at byte 24 of the pager file.
4483*/
danielk1977c7b60172007-08-22 11:22:03 +00004484static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004485 PgHdr *pPgHdr;
4486 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004487 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004488
4489 if( !pPager->changeCountDone ){
4490 /* Open page 1 of the file for writing. */
4491 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4492 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004493
4494 if( !isDirect ){
4495 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00004496 if( rc!=SQLITE_OK ){
4497 sqlite3PagerUnref(pPgHdr);
4498 return rc;
4499 }
danielk1977c7b60172007-08-22 11:22:03 +00004500 }
4501
drh80e35f42007-03-30 14:06:34 +00004502 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004503 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004504 change_counter++;
4505 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004506
4507 if( isDirect && pPager->fd->pMethods ){
4508 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4509 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4510 }
4511
drh80e35f42007-03-30 14:06:34 +00004512 /* Release the page reference. */
4513 sqlite3PagerUnref(pPgHdr);
4514 pPager->changeCountDone = 1;
4515 }
danielk1977c7b60172007-08-22 11:22:03 +00004516 return rc;
drh80e35f42007-03-30 14:06:34 +00004517}
4518
4519/*
danielk1977f653d782008-03-20 11:04:21 +00004520** Sync the pager file to disk.
4521*/
4522int sqlite3PagerSync(Pager *pPager){
4523 int rc;
4524 pagerEnter(pPager);
4525 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4526 pagerLeave(pPager);
4527 return rc;
4528}
4529
4530/*
drh80e35f42007-03-30 14:06:34 +00004531** Sync the database file for the pager pPager. zMaster points to the name
4532** of a master journal file that should be written into the individual
4533** journal file. zMaster may be NULL, which is interpreted as no master
4534** journal (a single database transaction).
4535**
4536** This routine ensures that the journal is synced, all dirty pages written
4537** to the database file and the database file synced. The only thing that
4538** remains to commit the transaction is to delete the journal file (or
4539** master journal file if specified).
4540**
4541** Note that if zMaster==NULL, this does not overwrite a previous value
4542** passed to an sqlite3PagerCommitPhaseOne() call.
4543**
4544** If parameter nTrunc is non-zero, then the pager file is truncated to
4545** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00004546**
4547** If the final parameter - noSync - is true, then the database file itself
4548** is not synced. The caller must call sqlite3PagerSync() directly to
4549** sync the database file before calling CommitPhaseTwo() to delete the
4550** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00004551*/
danielk1977f653d782008-03-20 11:04:21 +00004552int sqlite3PagerCommitPhaseOne(
4553 Pager *pPager,
4554 const char *zMaster,
4555 Pgno nTrunc,
4556 int noSync
4557){
drh80e35f42007-03-30 14:06:34 +00004558 int rc = SQLITE_OK;
4559
4560 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4561 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004562 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004563
4564 /* If this is an in-memory db, or no pages have been written to, or this
4565 ** function has already been called, it is a no-op.
4566 */
4567 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4568 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004569
danielk1977c7b60172007-08-22 11:22:03 +00004570#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4571 /* The atomic-write optimization can be used if all of the
4572 ** following are true:
4573 **
4574 ** + The file-system supports the atomic-write property for
4575 ** blocks of size page-size, and
4576 ** + This commit is not part of a multi-file transaction, and
4577 ** + Exactly one page has been modified and store in the journal file.
4578 **
4579 ** If the optimization can be used, then the journal file will never
4580 ** be created for this transaction.
4581 */
danielk1977f55b8992007-08-24 08:15:53 +00004582 int useAtomicWrite = (
4583 !zMaster &&
4584 pPager->journalOff==jrnlBufferSize(pPager) &&
4585 nTrunc==0 &&
4586 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4587 );
4588 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004589 /* Update the nRec field in the journal file. */
4590 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4591 assert(pPager->nRec==1);
4592 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4593
4594 /* Update the db file change counter. The following call will modify
4595 ** the in-memory representation of page 1 to include the updated
4596 ** change counter and then write page 1 directly to the database
4597 ** file. Because of the atomic-write property of the host file-system,
4598 ** this is safe.
4599 */
danielk1977ae72d982007-10-03 08:46:44 +00004600 if( rc==SQLITE_OK ){
4601 rc = pager_incr_changecounter(pPager, 1);
4602 }
danielk1977f55b8992007-08-24 08:15:53 +00004603 }else{
4604 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00004605 }
4606
danielk1977ae72d982007-10-03 08:46:44 +00004607 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00004608#endif
4609
drh80e35f42007-03-30 14:06:34 +00004610 /* If a master journal file name has already been written to the
4611 ** journal file, then no sync is required. This happens when it is
4612 ** written, then the process fails to upgrade from a RESERVED to an
4613 ** EXCLUSIVE lock. The next time the process tries to commit the
4614 ** transaction the m-j name will have already been written.
4615 */
4616 if( !pPager->setMaster ){
danielk1977f55b8992007-08-24 08:15:53 +00004617 assert( pPager->journalOpen );
danielk1977c7b60172007-08-22 11:22:03 +00004618 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004619 if( rc!=SQLITE_OK ) goto sync_exit;
4620#ifndef SQLITE_OMIT_AUTOVACUUM
4621 if( nTrunc!=0 ){
4622 /* If this transaction has made the database smaller, then all pages
4623 ** being discarded by the truncation must be written to the journal
4624 ** file.
4625 */
4626 Pgno i;
4627 int iSkip = PAGER_MJ_PGNO(pPager);
4628 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
drhf5e7bb52008-02-18 14:47:33 +00004629 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
drh80e35f42007-03-30 14:06:34 +00004630 rc = sqlite3PagerGet(pPager, i, &pPg);
4631 if( rc!=SQLITE_OK ) goto sync_exit;
4632 rc = sqlite3PagerWrite(pPg);
4633 sqlite3PagerUnref(pPg);
4634 if( rc!=SQLITE_OK ) goto sync_exit;
4635 }
4636 }
4637 }
4638#endif
4639 rc = writeMasterJournal(pPager, zMaster);
4640 if( rc!=SQLITE_OK ) goto sync_exit;
4641 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004642 }
danielk1977c7b60172007-08-22 11:22:03 +00004643 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004644
4645#ifndef SQLITE_OMIT_AUTOVACUUM
4646 if( nTrunc!=0 ){
4647 rc = sqlite3PagerTruncate(pPager, nTrunc);
4648 if( rc!=SQLITE_OK ) goto sync_exit;
4649 }
4650#endif
4651
4652 /* Write all dirty pages to the database file */
4653 pPg = pager_get_all_dirty_pages(pPager);
4654 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004655 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004656 assert( rc!=SQLITE_IOERR_BLOCKED );
4657 /* The error might have left the dirty list all fouled up here,
4658 ** but that does not matter because if the if the dirty list did
4659 ** get corrupted, then the transaction will roll back and
4660 ** discard the dirty list. There is an assert in
4661 ** pager_get_all_dirty_pages() that verifies that no attempt
4662 ** is made to use an invalid dirty list.
4663 */
drh153c62c2007-08-24 03:51:33 +00004664 goto sync_exit;
4665 }
drh3cdd7d32007-03-30 14:46:01 +00004666 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004667
4668 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004669 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004670 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004671 }
4672 IOTRACE(("DBSYNC %p\n", pPager))
4673
4674 pPager->state = PAGER_SYNCED;
4675 }else if( MEMDB && nTrunc!=0 ){
4676 rc = sqlite3PagerTruncate(pPager, nTrunc);
4677 }
4678
4679sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004680 if( rc==SQLITE_IOERR_BLOCKED ){
4681 /* pager_incr_changecounter() may attempt to obtain an exclusive
4682 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004683 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004684 * better to return SQLITE_BUSY.
4685 */
4686 rc = SQLITE_BUSY;
4687 }
drh86f8c192007-08-22 00:39:19 +00004688 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004689 return rc;
4690}
4691
4692
4693/*
drhed7c8552001-04-11 14:29:21 +00004694** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004695**
4696** If the commit fails for any reason, a rollback attempt is made
4697** and an error code is returned. If the commit worked, SQLITE_OK
4698** is returned.
drhed7c8552001-04-11 14:29:21 +00004699*/
drh80e35f42007-03-30 14:06:34 +00004700int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004701 int rc;
drhed7c8552001-04-11 14:29:21 +00004702 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004703
danielk1977efaaf572006-01-16 11:29:19 +00004704 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004705 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004706 }
drha6abd042004-06-09 17:37:22 +00004707 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004708 return SQLITE_ERROR;
4709 }
drh86f8c192007-08-22 00:39:19 +00004710 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004711 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004712 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004713 pPg = pager_get_all_dirty_pages(pPager);
4714 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004715 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4716 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004717 pPg->dirty = 0;
4718 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004719 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004720 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004721 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004722 pPg = pPg->pDirty;
4723 }
drh605ad8f2006-05-03 23:34:05 +00004724 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004725#ifndef NDEBUG
4726 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4727 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4728 assert( !pPg->alwaysRollback );
4729 assert( !pHist->pOrig );
4730 assert( !pHist->pStmt );
4731 }
4732#endif
drhac69b052004-05-12 13:30:07 +00004733 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004734 pPager->state = PAGER_SHARED;
danielk197787c29a92008-01-18 11:33:16 +00004735 pagerLeave(pPager);
drhac69b052004-05-12 13:30:07 +00004736 return SQLITE_OK;
4737 }
drh3cdd7d32007-03-30 14:46:01 +00004738 assert( pPager->journalOpen || !pPager->dirtyCache );
4739 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4740 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004741 rc = pager_error(pPager, rc);
4742 pagerLeave(pPager);
4743 return rc;
drhed7c8552001-04-11 14:29:21 +00004744}
4745
4746/*
drha6abd042004-06-09 17:37:22 +00004747** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004748** All in-memory cache pages revert to their original data contents.
4749** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004750**
4751** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004752** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004753** process is writing trash into the journal file (SQLITE_CORRUPT) or
4754** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4755** codes are returned for all these occasions. Otherwise,
4756** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004757*/
danielk19773b8a05f2007-03-19 17:44:26 +00004758int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004759 int rc;
drh4f0c5872007-03-26 22:05:01 +00004760 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004761 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004762 PgHdr *p;
4763 for(p=pPager->pAll; p; p=p->pNextAll){
4764 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004765 assert( !p->alwaysRollback );
4766 if( !p->dirty ){
4767 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4768 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4769 continue;
4770 }
4771
drhac69b052004-05-12 13:30:07 +00004772 pHist = PGHDR_TO_HIST(p, pPager);
4773 if( pHist->pOrig ){
4774 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004775 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004776 }else{
drh4f0c5872007-03-26 22:05:01 +00004777 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004778 }
4779 clearHistory(pHist);
4780 p->dirty = 0;
4781 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004782 pHist->inStmt = 0;
4783 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004784 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004785 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004786 }
drhac69b052004-05-12 13:30:07 +00004787 }
drh605ad8f2006-05-03 23:34:05 +00004788 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004789 pPager->pStmt = 0;
4790 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004791 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004792 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004793 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004794 return SQLITE_OK;
4795 }
4796
drh86f8c192007-08-22 00:39:19 +00004797 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004798 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004799 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004800 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004801 return rc;
4802 }
drhdb48ee02003-01-16 13:42:43 +00004803
danielk1977efaaf572006-01-16 11:29:19 +00004804 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004805 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004806 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004807 }
drh86f8c192007-08-22 00:39:19 +00004808 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004809 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004810 }
drha6abd042004-06-09 17:37:22 +00004811 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004812 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004813 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004814 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004815 if( rc==SQLITE_OK ){
4816 rc = rc2;
4817 }
4818 }else{
danielk1977e277be02007-03-23 18:12:06 +00004819 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004820 }
danielk1977e180dd92007-04-05 17:15:52 +00004821 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004822 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004823
4824 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4825 ** cache. So call pager_error() on the way out to make any error
4826 ** persistent.
4827 */
drh86f8c192007-08-22 00:39:19 +00004828 rc = pager_error(pPager, rc);
4829 pagerLeave(pPager);
4830 return rc;
drh98808ba2001-10-18 12:34:46 +00004831}
drhd9b02572001-04-15 00:37:09 +00004832
4833/*
drh5e00f6c2001-09-13 13:46:56 +00004834** Return TRUE if the database file is opened read-only. Return FALSE
4835** if the database is (in theory) writable.
4836*/
danielk19773b8a05f2007-03-19 17:44:26 +00004837int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004838 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004839}
4840
4841/*
drh0f7eb612006-08-08 13:51:43 +00004842** Return the number of references to the pager.
4843*/
danielk19773b8a05f2007-03-19 17:44:26 +00004844int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004845 return pPager->nRef;
4846}
4847
4848#ifdef SQLITE_TEST
4849/*
drhd9b02572001-04-15 00:37:09 +00004850** This routine is used for testing and analysis only.
4851*/
danielk19773b8a05f2007-03-19 17:44:26 +00004852int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004853 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004854 a[0] = pPager->nRef;
4855 a[1] = pPager->nPage;
4856 a[2] = pPager->mxPage;
4857 a[3] = pPager->dbSize;
4858 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004859 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004860 a[6] = pPager->nHit;
4861 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004862 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004863 a[9] = pPager->nRead;
4864 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004865 return a;
4866}
drh0f7eb612006-08-08 13:51:43 +00004867#endif
drhdd793422001-06-28 01:54:48 +00004868
drhfa86c412002-02-02 15:01:15 +00004869/*
drhac69b052004-05-12 13:30:07 +00004870** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004871**
4872** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004873** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004874** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004875*/
drh86f8c192007-08-22 00:39:19 +00004876static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004877 int rc;
drhac69b052004-05-12 13:30:07 +00004878 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004879 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004880 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004881 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004882 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004883 pPager->stmtInUse = 1;
4884 pPager->stmtSize = pPager->dbSize;
4885 return SQLITE_OK;
4886 }
drhda47d772002-12-02 04:25:19 +00004887 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004888 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004889 return SQLITE_OK;
4890 }
drhfa86c412002-02-02 15:01:15 +00004891 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004892 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004893 assert( pPager->pInStmt==0 );
4894 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00004895 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004896 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004897 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004898 return SQLITE_NOMEM;
4899 }
danielk197776572402004-06-25 02:38:54 +00004900 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004901 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004902 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004903 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004904 if( !pPager->stmtOpen ){
drh334b2992007-09-06 23:28:23 +00004905 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
drh33f4e022007-09-03 15:19:34 +00004906 SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00004907 if( rc ){
4908 goto stmt_begin_failed;
4909 }
drhac69b052004-05-12 13:30:07 +00004910 pPager->stmtOpen = 1;
4911 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004912 }
drhac69b052004-05-12 13:30:07 +00004913 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004914 return SQLITE_OK;
4915
drhac69b052004-05-12 13:30:07 +00004916stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00004917 if( pPager->pInStmt ){
4918 sqlite3BitvecDestroy(pPager->pInStmt);
4919 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004920 }
4921 return rc;
4922}
drh86f8c192007-08-22 00:39:19 +00004923int sqlite3PagerStmtBegin(Pager *pPager){
4924 int rc;
4925 pagerEnter(pPager);
4926 rc = pagerStmtBegin(pPager);
4927 pagerLeave(pPager);
4928 return rc;
4929}
drhfa86c412002-02-02 15:01:15 +00004930
4931/*
drhac69b052004-05-12 13:30:07 +00004932** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004933*/
danielk19773b8a05f2007-03-19 17:44:26 +00004934int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004935 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004936 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004937 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004938 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004939 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004940 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00004941 sqlite3BitvecDestroy(pPager->pInStmt);
4942 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004943 }else{
4944 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004945 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004946 pNext = pHist->pNextStmt;
4947 assert( pHist->inStmt );
4948 pHist->inStmt = 0;
4949 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004950 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004951 pHist->pStmt = 0;
4952 }
4953 }
4954 pPager->stmtNRec = 0;
4955 pPager->stmtInUse = 0;
4956 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004957 }
drhac69b052004-05-12 13:30:07 +00004958 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004959 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004960 return SQLITE_OK;
4961}
4962
4963/*
drhac69b052004-05-12 13:30:07 +00004964** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004965*/
danielk19773b8a05f2007-03-19 17:44:26 +00004966int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004967 int rc;
drh86f8c192007-08-22 00:39:19 +00004968 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004969 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004970 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004971 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004972 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004973 PgHistory *pHist;
4974 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4975 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004976 if( pHist->pStmt ){
4977 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004978 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004979 pHist->pStmt = 0;
4980 }
4981 }
4982 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004983 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004984 rc = SQLITE_OK;
4985 }else{
4986 rc = pager_stmt_playback(pPager);
4987 }
danielk19773b8a05f2007-03-19 17:44:26 +00004988 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004989 }else{
4990 rc = SQLITE_OK;
4991 }
drhac69b052004-05-12 13:30:07 +00004992 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004993 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004994 return rc;
4995}
4996
drh73509ee2003-04-06 20:44:45 +00004997/*
4998** Return the full pathname of the database file.
4999*/
danielk19773b8a05f2007-03-19 17:44:26 +00005000const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00005001 return pPager->zFilename;
5002}
5003
drhb20ea9d2004-02-09 01:20:36 +00005004/*
drhd0679ed2007-08-28 22:24:34 +00005005** Return the VFS structure for the pager.
5006*/
5007const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
5008 return pPager->pVfs;
5009}
5010
5011/*
drhcc6bb3e2007-08-31 16:11:35 +00005012** Return the file handle for the database file associated
5013** with the pager. This might return NULL if the file has
5014** not yet been opened.
5015*/
5016sqlite3_file *sqlite3PagerFile(Pager *pPager){
5017 return pPager->fd;
5018}
5019
5020/*
danielk19775865e3d2004-06-14 06:03:57 +00005021** Return the directory of the database file.
5022*/
danielk19773b8a05f2007-03-19 17:44:26 +00005023const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005024 return pPager->zDirectory;
5025}
5026
5027/*
5028** Return the full pathname of the journal file.
5029*/
danielk19773b8a05f2007-03-19 17:44:26 +00005030const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005031 return pPager->zJournal;
5032}
5033
5034/*
drh2c8997b2005-08-27 16:36:48 +00005035** Return true if fsync() calls are disabled for this pager. Return FALSE
5036** if fsync()s are executed normally.
5037*/
danielk19773b8a05f2007-03-19 17:44:26 +00005038int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00005039 return pPager->noSync;
5040}
5041
drh7c4ac0c2007-04-05 11:25:58 +00005042#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00005043/*
drhb20ea9d2004-02-09 01:20:36 +00005044** Set the codec for this pager
5045*/
danielk19773b8a05f2007-03-19 17:44:26 +00005046void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00005047 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00005048 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00005049 void *pCodecArg
5050){
5051 pPager->xCodec = xCodec;
5052 pPager->pCodecArg = pCodecArg;
5053}
drh7c4ac0c2007-04-05 11:25:58 +00005054#endif
drhb20ea9d2004-02-09 01:20:36 +00005055
danielk1977687566d2004-11-02 12:56:41 +00005056#ifndef SQLITE_OMIT_AUTOVACUUM
5057/*
danielk197787c29a92008-01-18 11:33:16 +00005058** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00005059**
drh5e385312007-06-16 04:42:12 +00005060** There must be no references to the page previously located at
5061** pgno (which we call pPgOld) though that page is allowed to be
5062** in cache. If the page previous located at pgno is not already
5063** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00005064**
drh5e385312007-06-16 04:42:12 +00005065** References to the page pPg remain valid. Updating any
5066** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00005067** allocated along with the page) is the responsibility of the caller.
5068**
danielk19775fd057a2005-03-09 13:09:43 +00005069** A transaction must be active when this routine is called. It used to be
5070** required that a statement transaction was not active, but this restriction
5071** has been removed (CREATE INDEX needs to move a page when a statement
5072** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00005073*/
danielk19773b8a05f2007-03-19 17:44:26 +00005074int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00005075 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00005076 int h;
danielk197794daf7f2004-11-08 09:26:09 +00005077 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00005078
drh86f8c192007-08-22 00:39:19 +00005079 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005080 assert( pPg->nRef>0 );
5081
drh4f0c5872007-03-26 22:05:01 +00005082 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00005083 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00005084 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00005085
danielk1977b4626a32007-04-28 15:47:43 +00005086 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00005087 if( pPg->needSync ){
5088 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00005089 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00005090 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00005091 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00005092 }
5093
drh85b623f2007-12-13 21:54:09 +00005094 /* Unlink pPg from its hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00005095 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00005096
danielk1977ef73ee92004-11-06 12:26:07 +00005097 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00005098 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00005099 ** page pgno before the 'move' operation, it needs to be retained
5100 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00005101 */
drh5e385312007-06-16 04:42:12 +00005102 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00005103 pPgOld = pager_lookup(pPager, pgno);
5104 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00005105 assert( pPgOld->nRef==0 );
5106 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00005107 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00005108 pPg->needSync = pPgOld->needSync;
5109 }else{
5110 pPg->needSync = 0;
5111 }
drhf5e7bb52008-02-18 14:47:33 +00005112 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
danielk1977687566d2004-11-02 12:56:41 +00005113
danielk1977f5fdda82004-11-03 08:44:05 +00005114 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00005115 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00005116 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00005117 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00005118 if( pPager->aHash[h] ){
5119 assert( pPager->aHash[h]->pPrevHash==0 );
5120 pPager->aHash[h]->pPrevHash = pPg;
5121 }
5122 pPg->pNextHash = pPager->aHash[h];
5123 pPager->aHash[h] = pPg;
5124 pPg->pPrevHash = 0;
5125
drh605ad8f2006-05-03 23:34:05 +00005126 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00005127 pPager->dirtyCache = 1;
5128
danielk197794daf7f2004-11-08 09:26:09 +00005129 if( needSyncPgno ){
5130 /* If needSyncPgno is non-zero, then the journal file needs to be
5131 ** sync()ed before any data is written to database file page needSyncPgno.
5132 ** Currently, no such page exists in the page-cache and the
drhf5e7bb52008-02-18 14:47:33 +00005133 ** Pager.pInJournal bit has been set. This needs to be remedied by loading
danielk197794daf7f2004-11-08 09:26:09 +00005134 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00005135 **
danielk1977a98d7b42008-01-18 13:42:54 +00005136 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00005137 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00005138 ** array. Otherwise, if the page is loaded and written again in
5139 ** this transaction, it may be written to the database file before
5140 ** it is synced into the journal file. This way, it may end up in
5141 ** the journal file twice, but that is not a problem.
5142 **
danielk19773b8a05f2007-03-19 17:44:26 +00005143 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00005144 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00005145 */
5146 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005147 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00005148 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00005149 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00005150 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00005151 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
5152 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00005153 }
danielk197787c29a92008-01-18 11:33:16 +00005154 pagerLeave(pPager);
5155 return rc;
5156 }
danielk1977ae825582004-11-23 09:06:55 +00005157 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00005158 pPgHdr->needSync = 1;
5159 pPgHdr->inJournal = 1;
5160 makeDirty(pPgHdr);
5161 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00005162 }
5163
drh86f8c192007-08-22 00:39:19 +00005164 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005165 return SQLITE_OK;
5166}
5167#endif
5168
danielk19773b8a05f2007-03-19 17:44:26 +00005169/*
5170** Return a pointer to the data for the specified page.
5171*/
5172void *sqlite3PagerGetData(DbPage *pPg){
5173 return PGHDR_TO_DATA(pPg);
5174}
5175
5176/*
5177** Return a pointer to the Pager.nExtra bytes of "extra" space
5178** allocated along with the specified page.
5179*/
5180void *sqlite3PagerGetExtra(DbPage *pPg){
5181 Pager *pPager = pPg->pPager;
5182 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
5183}
5184
danielk197741483462007-03-24 16:45:04 +00005185/*
5186** Get/set the locking-mode for this pager. Parameter eMode must be one
5187** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
5188** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
5189** the locking-mode is set to the value specified.
5190**
5191** The returned value is either PAGER_LOCKINGMODE_NORMAL or
5192** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
5193** locking-mode.
5194*/
5195int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00005196 assert( eMode==PAGER_LOCKINGMODE_QUERY
5197 || eMode==PAGER_LOCKINGMODE_NORMAL
5198 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5199 assert( PAGER_LOCKINGMODE_QUERY<0 );
5200 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5201 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00005202 pPager->exclusiveMode = eMode;
5203 }
5204 return (int)pPager->exclusiveMode;
5205}
5206
drh3b020132008-04-17 17:02:01 +00005207/*
5208** Get/set the journal-mode for this pager. Parameter eMode must be one
5209** of PAGER_JOURNALMODE_QUERY, PAGER_JOURNALMODE_DELETE or
5210** PAGER_JOURNALMODE_PERSIST. If the parameter is not _QUERY, then
5211** the journal-mode is set to the value specified.
5212**
5213** The returned value is either PAGER_JOURNALMODE_DELETE or
5214** PAGER_JOURNALMODE_PERSIST, indicating the current (possibly updated)
5215** journal-mode.
5216*/
5217int sqlite3PagerJournalMode(Pager *pPager, int eMode){
5218 assert( eMode==PAGER_JOURNALMODE_QUERY
5219 || eMode==PAGER_JOURNALMODE_DELETE
5220 || eMode==PAGER_JOURNALMODE_PERSIST );
5221 assert( PAGER_JOURNALMODE_QUERY<0 );
5222 assert( PAGER_JOURNALMODE_DELETE>=0 && PAGER_JOURNALMODE_PERSIST>=0 );
5223 if( eMode>=0 && !pPager->tempFile ){
5224 pPager->persistJournal = eMode;
5225 }
5226 return (int)pPager->persistJournal;
5227}
5228
drhd919fe12007-12-11 19:34:44 +00005229#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00005230/*
5231** Print a listing of all referenced pages and their ref count.
5232*/
danielk19773b8a05f2007-03-19 17:44:26 +00005233void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00005234 PgHdr *pPg;
5235 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5236 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00005237 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5238 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00005239 }
5240}
5241#endif
drh2e66f0b2005-04-28 17:18:48 +00005242
5243#endif /* SQLITE_OMIT_DISKIO */